减少使用TimeWheel
This commit is contained in:
parent
849ff61678
commit
61cc2b71ea
68
App/IR.cpp
68
App/IR.cpp
|
@ -29,7 +29,7 @@ typedef enum
|
||||||
WaitRev,
|
WaitRev,
|
||||||
Rev,
|
Rev,
|
||||||
RevOver,
|
RevOver,
|
||||||
|
|
||||||
Sending,
|
Sending,
|
||||||
SendOver,
|
SendOver,
|
||||||
}IRStat;
|
}IRStat;
|
||||||
|
@ -45,7 +45,7 @@ bool IR::Send(const Buffer& bs)
|
||||||
#ifdef STM32F0
|
#ifdef STM32F0
|
||||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
||||||
TIM_DeInit(TIM2);
|
TIM_DeInit(TIM2);
|
||||||
|
|
||||||
if(_Tim == nullptr)
|
if(_Tim == nullptr)
|
||||||
{
|
{
|
||||||
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
||||||
|
@ -54,7 +54,7 @@ bool IR::Send(const Buffer& bs)
|
||||||
// _Tim->SetFrequency(50); // 5Hz 20ms
|
// _Tim->SetFrequency(50); // 5Hz 20ms
|
||||||
// 使用一个字节 需要自行配置分频系数
|
// 使用一个字节 需要自行配置分频系数
|
||||||
_Tim->Prescaler = 400; // 分频 需要结果是 Period < 256 时能计数 20ms
|
_Tim->Prescaler = 400; // 分频 需要结果是 Period < 256 时能计数 20ms
|
||||||
|
|
||||||
SendP = (ushort *)bs.GetBuffer();
|
SendP = (ushort *)bs.GetBuffer();
|
||||||
SendBufLen = bs.Length()/2;
|
SendBufLen = bs.Length()/2;
|
||||||
SendIndex = 0;
|
SendIndex = 0;
|
||||||
|
@ -63,24 +63,27 @@ bool IR::Send(const Buffer& bs)
|
||||||
// 取消接收时候的配置
|
// 取消接收时候的配置
|
||||||
_Tim->Opened = true;
|
_Tim->Opened = true;
|
||||||
_Tim->Close();
|
_Tim->Close();
|
||||||
|
|
||||||
// 注册中断
|
// 注册中断
|
||||||
_Tim->Register(OnSend,this);
|
_Tim->Register(OnSend,this);
|
||||||
// 重新配置
|
// 重新配置
|
||||||
_Tim->SetCounter(0x00000000);
|
_Tim->SetCounter(0x00000000);
|
||||||
_Tim->Config();
|
_Tim->Config();
|
||||||
|
|
||||||
Stat = Sending;
|
Stat = Sending;
|
||||||
debug_printf("Start %04X\r\n",SendP[0]);
|
debug_printf("Start %04X\r\n",SendP[0]);
|
||||||
|
|
||||||
// 暂时避过 定时器一启动立马中断的错误
|
// 暂时避过 定时器一启动立马中断的错误
|
||||||
ErrorIRQ = true;
|
ErrorIRQ = true;
|
||||||
// 开始 中断内处理数据
|
// 开始 中断内处理数据
|
||||||
// _Pwm->Open();
|
// _Pwm->Open();
|
||||||
_Tim->Open();
|
_Tim->Open();
|
||||||
|
|
||||||
// 等待结束
|
// 等待结束
|
||||||
TimeWheel tw(2);
|
_SendOK = false;
|
||||||
while(!tw.Expired() && Stat != SendOver) Sys.Sleep(50);
|
WaitHandle wh;
|
||||||
|
wh.WaitOne(2000, _SendOK);
|
||||||
|
|
||||||
// 结束 不论是超时还是发送结束都关闭pwm和定时器
|
// 结束 不论是超时还是发送结束都关闭pwm和定时器
|
||||||
//_Pwm->Close();
|
//_Pwm->Close();
|
||||||
_Tim->Register(nullptr,nullptr);
|
_Tim->Register(nullptr,nullptr);
|
||||||
|
@ -88,7 +91,7 @@ bool IR::Send(const Buffer& bs)
|
||||||
// 清空使用的数据
|
// 清空使用的数据
|
||||||
SendIndex = 0;
|
SendIndex = 0;
|
||||||
SendP = nullptr;
|
SendP = nullptr;
|
||||||
|
|
||||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, DISABLE);
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, DISABLE);
|
||||||
#endif
|
#endif
|
||||||
return true;
|
return true;
|
||||||
|
@ -111,7 +114,7 @@ void IR::OnSend(void* sender, void* param)
|
||||||
// PWM归位
|
// PWM归位
|
||||||
TIM_SetCounter(ti, 0x00000000);
|
TIM_SetCounter(ti, 0x00000000);
|
||||||
// PWM变化
|
// PWM变化
|
||||||
//if(SendIndex % 2)
|
//if(SendIndex % 2)
|
||||||
if(SendIndex & 1) //不行 暂不知问题所在
|
if(SendIndex & 1) //不行 暂不知问题所在
|
||||||
{
|
{
|
||||||
TIM_Cmd(ti, ENABLE);
|
TIM_Cmd(ti, ENABLE);
|
||||||
|
@ -135,6 +138,8 @@ void IR::OnSend(void* sender, void* param)
|
||||||
//ir->_Pwm->Close();
|
//ir->_Pwm->Close();
|
||||||
//ir->_Tim->Close();
|
//ir->_Tim->Close();
|
||||||
Stat = SendOver;
|
Stat = SendOver;
|
||||||
|
_SendOK = true;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -146,7 +151,7 @@ int IR::Receive(Buffer& bs, int sTimeout)
|
||||||
#ifdef STM32F0
|
#ifdef STM32F0
|
||||||
uint bufLen = bs.Length();
|
uint bufLen = bs.Length();
|
||||||
uint DmaLen = bufLen/2 -1;
|
uint DmaLen = bufLen/2 -1;
|
||||||
|
|
||||||
// 捕获引脚初始化
|
// 捕获引脚初始化
|
||||||
if(_Port == nullptr)
|
if(_Port == nullptr)
|
||||||
{
|
{
|
||||||
|
@ -156,39 +161,39 @@ int IR::Receive(Buffer& bs, int sTimeout)
|
||||||
_Port->AFConfig(Port::AF_2);
|
_Port->AFConfig(Port::AF_2);
|
||||||
}
|
}
|
||||||
if(_Tim == nullptr) _Tim = Timer::Create(0x01); // 直接占用TIMER2
|
if(_Tim == nullptr) _Tim = Timer::Create(0x01); // 直接占用TIMER2
|
||||||
|
|
||||||
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, DISABLE);
|
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, DISABLE);
|
||||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
||||||
// TIM_DeInit(TIM2);
|
// TIM_DeInit(TIM2);
|
||||||
|
|
||||||
// _Tim->SetFrequency(50); //Prescaler Period 需要定制
|
// _Tim->SetFrequency(50); //Prescaler Period 需要定制
|
||||||
_Tim->Prescaler = 400;
|
_Tim->Prescaler = 400;
|
||||||
// 波形间隔悬殊,用一个字节误差太大
|
// 波形间隔悬殊,用一个字节误差太大
|
||||||
_Tim->Period = 65536;
|
_Tim->Period = 65536;
|
||||||
_Tim->Config();
|
_Tim->Config();
|
||||||
|
|
||||||
/* 使能自动装载 */
|
/* 使能自动装载 */
|
||||||
TIM_ARRPreloadConfig(TIM2, ENABLE);
|
TIM_ARRPreloadConfig(TIM2, ENABLE);
|
||||||
// 捕获设置
|
// 捕获设置
|
||||||
// 2通道映射到1通道 配套使用DMA1 Channel5
|
// 2通道映射到1通道 配套使用DMA1 Channel5
|
||||||
TIM_ICInitTypeDef TIM_ICInitStructure;
|
TIM_ICInitTypeDef TIM_ICInitStructure;
|
||||||
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
|
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
|
||||||
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
|
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
|
||||||
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI;
|
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI;
|
||||||
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
|
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
|
||||||
TIM_ICInitStructure.TIM_ICFilter = 0x0;
|
TIM_ICInitStructure.TIM_ICFilter = 0x0;
|
||||||
TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
|
TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
|
||||||
|
|
||||||
/* Select the TIM2 Input Trigger: TI2FP2 */
|
/* Select the TIM2 Input Trigger: TI2FP2 */
|
||||||
TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
|
TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
|
||||||
/* 捕获的同时清空定时器计数,不能使用 这个复位是以电平为准,不是沿
|
/* 捕获的同时清空定时器计数,不能使用 这个复位是以电平为准,不是沿
|
||||||
使用会使采集到的数据一半为零 后期数据处理时候做差就好 */
|
使用会使采集到的数据一半为零 后期数据处理时候做差就好 */
|
||||||
// TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
|
// TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
|
||||||
// TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);
|
// TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);
|
||||||
// DMA 读取CNT寄存器,一字节设置 无法解决数据冗余
|
// DMA 读取CNT寄存器,一字节设置 无法解决数据冗余
|
||||||
// TIM_DMAConfig(TIM2, TIM_DMABase_CNT, TIM_DMABurstLength_1Transfer);
|
// TIM_DMAConfig(TIM2, TIM_DMABase_CNT, TIM_DMABurstLength_1Transfer);
|
||||||
//TIM_Cmd(TIM2, ENABLE);
|
//TIM_Cmd(TIM2, ENABLE);
|
||||||
|
|
||||||
// DMA 时钟
|
// DMA 时钟
|
||||||
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
|
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
|
||||||
// 使用通道5
|
// 使用通道5
|
||||||
|
@ -208,34 +213,35 @@ int IR::Receive(Buffer& bs, int sTimeout)
|
||||||
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
|
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
|
||||||
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
|
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
|
||||||
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
|
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
|
||||||
|
|
||||||
// 开启DMA TIME
|
// 开启DMA TIME
|
||||||
DMA_Cmd(DMA1_Channel5, ENABLE);
|
DMA_Cmd(DMA1_Channel5, ENABLE);
|
||||||
TIM_Cmd(TIM2, ENABLE);
|
TIM_Cmd(TIM2, ENABLE);
|
||||||
// 连接DMA 触发机制
|
// 连接DMA 触发机制
|
||||||
TIM_DMACmd(TIM2, TIM_DMA_CC1, ENABLE);
|
TIM_DMACmd(TIM2, TIM_DMA_CC1, ENABLE);
|
||||||
|
|
||||||
// 等待起始条件,DMA 开始搬运数据
|
// 等待起始条件,DMA 开始搬运数据
|
||||||
Stat = WaitRev;
|
Stat = WaitRev;
|
||||||
TimeWheel tw(sTimeout);
|
|
||||||
while(!tw.Expired()
|
|
||||||
&& DMA_GetCurrDataCounter(DMA1_Channel5) == DmaLen) Sys.Sleep(50);
|
|
||||||
|
|
||||||
if(tw.Expired())
|
TimeWheel tw(sTimeout);
|
||||||
{
|
tw.Sleep = 50;
|
||||||
|
while(!tw.Expired() && DMA_GetCurrDataCounter(DMA1_Channel5) == DmaLen);
|
||||||
|
|
||||||
|
if(tw.Expired())
|
||||||
|
{
|
||||||
// 超时没有收到学习
|
// 超时没有收到学习
|
||||||
Stat = RevOver;
|
Stat = RevOver;
|
||||||
// 关闭Time DMA
|
// 关闭Time DMA
|
||||||
DMA_Cmd(DMA1_Channel5, DISABLE);
|
DMA_Cmd(DMA1_Channel5, DISABLE);
|
||||||
TIM_Cmd(TIM2, DISABLE);
|
TIM_Cmd(TIM2, DISABLE);
|
||||||
|
|
||||||
int length = DMA_GetCurrDataCounter(DMA1_Channel5);
|
int length = DMA_GetCurrDataCounter(DMA1_Channel5);
|
||||||
debug_printf("\r\n DMA::Counter %d \r\n", length);
|
debug_printf("\r\n DMA::Counter %d \r\n", length);
|
||||||
|
|
||||||
bs.SetLength(0);
|
bs.SetLength(0);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 进入接收状态
|
// 进入接收状态
|
||||||
Stat = Rev;
|
Stat = Rev;
|
||||||
debug_printf("\r\n开始接收数据\r\n");
|
debug_printf("\r\n开始接收数据\r\n");
|
||||||
|
@ -243,9 +249,9 @@ int IR::Receive(Buffer& bs, int sTimeout)
|
||||||
// 不能使用 定时器 中断作为超时依据 只能等。。
|
// 不能使用 定时器 中断作为超时依据 只能等。。
|
||||||
tw.Reset(0,400);
|
tw.Reset(0,400);
|
||||||
while(!tw.Expired()) Sys.Sleep(50);
|
while(!tw.Expired()) Sys.Sleep(50);
|
||||||
|
|
||||||
// 接收结束
|
// 接收结束
|
||||||
// 关闭Time DMA
|
// 关闭Time DMA
|
||||||
DMA_Cmd(DMA1_Channel5, DISABLE);
|
DMA_Cmd(DMA1_Channel5, DISABLE);
|
||||||
TIM_Cmd(TIM2, DISABLE);
|
TIM_Cmd(TIM2, DISABLE);
|
||||||
// 本次接收结束,关闭 RCC 防止这些配置贻害无穷
|
// 本次接收结束,关闭 RCC 防止这些配置贻害无穷
|
||||||
|
@ -253,7 +259,7 @@ int IR::Receive(Buffer& bs, int sTimeout)
|
||||||
// 拿到数据长度
|
// 拿到数据长度
|
||||||
uint len = DmaLen - DMA_GetCurrDataCounter(DMA1_Channel5);
|
uint len = DmaLen - DMA_GetCurrDataCounter(DMA1_Channel5);
|
||||||
debug_printf("DMA REV %d byte\r\n",len);
|
debug_printf("DMA REV %d byte\r\n",len);
|
||||||
|
|
||||||
if(len > 30)
|
if(len > 30)
|
||||||
{
|
{
|
||||||
if(len >= DmaLen)
|
if(len >= DmaLen)
|
||||||
|
|
10
App/IR.h
10
App/IR.h
|
@ -22,10 +22,6 @@
|
||||||
|
|
||||||
class IR
|
class IR
|
||||||
{
|
{
|
||||||
private:
|
|
||||||
PWM* _Pwm = nullptr;
|
|
||||||
Timer* _Tim = nullptr;
|
|
||||||
AlternatePort * _Port = nullptr;
|
|
||||||
public:
|
public:
|
||||||
IR(PWM * pwm);
|
IR(PWM * pwm);
|
||||||
|
|
||||||
|
@ -36,6 +32,12 @@ public:
|
||||||
int Receive(Buffer& bs, int sTimeout = 10);
|
int Receive(Buffer& bs, int sTimeout = 10);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
PWM* _Pwm = nullptr;
|
||||||
|
Timer* _Tim = nullptr;
|
||||||
|
AlternatePort * _Port = nullptr;
|
||||||
|
bool _SendOK;
|
||||||
|
//bool _RecvOK;
|
||||||
|
|
||||||
static void OnSend(void* sender, void* param);
|
static void OnSend(void* sender, void* param);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -50,22 +50,25 @@ void IC74HC165MOR::Trigger()
|
||||||
for(int i = 0; i < _Bs.Length(); i++)
|
for(int i = 0; i < _Bs.Length(); i++)
|
||||||
{
|
{
|
||||||
byte temp = 0x00;
|
byte temp = 0x00;
|
||||||
TimeWheel tw(0,2);
|
int j = 0;
|
||||||
for(int j = 0; j < 8; j++)
|
for(j = 0; j < 8; j++)
|
||||||
{
|
{
|
||||||
while(!tw.Expired() && _SCK != true ); // 等待 高电平
|
int times = 2000;
|
||||||
|
while(!_SCK && --times); // 等待 高电平
|
||||||
if(tw.Expired()) break;
|
if(!times) break;
|
||||||
|
|
||||||
if(_In) temp |= 0x01;
|
if(_In) temp |= 0x01;
|
||||||
else temp &= 0xfe;
|
else temp &= 0xfe;
|
||||||
|
|
||||||
while(!tw.Expired() && _SCK != false ); // 等待低电平
|
times = 2000;
|
||||||
|
while(_SCK && --times); // 等待低电平
|
||||||
|
if(!times) break;
|
||||||
|
|
||||||
temp <<= 1;
|
temp <<= 1;
|
||||||
}
|
}
|
||||||
_Bs[i] = temp;
|
_Bs[i] = temp;
|
||||||
if(tw.Expired())return; // 2ms 不结束 就强制退出
|
//if(tw.Expired())return; // 2ms 不结束 就强制退出
|
||||||
|
if(j < 8) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -100,8 +100,8 @@ uint HX711::Read()
|
||||||
|
|
||||||
uint temp = 0x00000000;
|
uint temp = 0x00000000;
|
||||||
// 等待 IC 数据
|
// 等待 IC 数据
|
||||||
TimeWheel tw(0, 30);
|
int times = 30000;
|
||||||
while(!tw.Expired() && DOUT);
|
while(DOUT && --times);
|
||||||
|
|
||||||
// 读取数据
|
// 读取数据
|
||||||
for(int i = 0;i < 24; i++)
|
for(int i = 0;i < 24; i++)
|
||||||
|
|
|
@ -857,7 +857,7 @@ bool NRF24L01::SendTo(const Buffer& bs, const Buffer& addr)
|
||||||
// https://devzone.nordicsemi.com/question/17074/nrf24l01-data-loss/
|
// https://devzone.nordicsemi.com/question/17074/nrf24l01-data-loss/
|
||||||
// It is important never to keep the nRF24L01+ in TX mode for more than 4ms at a time.
|
// It is important never to keep the nRF24L01+ in TX mode for more than 4ms at a time.
|
||||||
// If the Enhanced ShockBurst™ features are enabled, nRF24L01+ is never in TX mode longer than 4ms
|
// If the Enhanced ShockBurst™ features are enabled, nRF24L01+ is never in TX mode longer than 4ms
|
||||||
TimeWheel tw(0, ms);
|
auto end = Sys.Ms() + ms;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
Status = ReadReg(STATUS);
|
Status = ReadReg(STATUS);
|
||||||
|
@ -894,7 +894,7 @@ bool NRF24L01::SendTo(const Buffer& bs, const Buffer& addr)
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}while(!tw.Expired());
|
}while(Sys.Ms() < end);
|
||||||
|
|
||||||
// 这里开始到CE拉高结束,大概耗时186us。不拉高CE大概20us
|
// 这里开始到CE拉高结束,大概耗时186us。不拉高CE大概20us
|
||||||
//_CE = true;
|
//_CE = true;
|
||||||
|
|
|
@ -3,8 +3,6 @@
|
||||||
|
|
||||||
#include "Sys.h"
|
#include "Sys.h"
|
||||||
|
|
||||||
class DateTime;
|
|
||||||
|
|
||||||
// 时间类
|
// 时间类
|
||||||
// 使用双计数时钟,TIMx专用于毫秒级系统计时,SysTick用于微秒级延迟,秒级以及以上采用全局整数累加
|
// 使用双计数时钟,TIMx专用于毫秒级系统计时,SysTick用于微秒级延迟,秒级以及以上采用全局整数累加
|
||||||
// 这样子可以避免频繁使用微秒时带来长整型乘除法
|
// 这样子可以避免频繁使用微秒时带来长整型乘除法
|
||||||
|
|
|
@ -1,23 +1,21 @@
|
||||||
#include "Time.h"
|
#include "Time.h"
|
||||||
#include "Arp.h"
|
#include "Arp.h"
|
||||||
|
|
||||||
|
#include "WaitHandle.h"
|
||||||
|
|
||||||
#define NET_DEBUG 0
|
#define NET_DEBUG 0
|
||||||
|
|
||||||
class ArpSession
|
class ArpSession
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
IPAddress IP;
|
const IPAddress& IP;
|
||||||
MacAddress Mac;
|
MacAddress Mac;
|
||||||
bool Success;
|
WaitHandle Handle;
|
||||||
|
|
||||||
ArpSession(const IPAddress& ip)
|
ArpSession(const IPAddress& ip) : IP(ip) { }
|
||||||
{
|
|
||||||
IP = ip;
|
|
||||||
Success = false;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ArpSession* _ArpSession;
|
ArpSession* _ArpSession = nullptr;
|
||||||
|
|
||||||
ArpSocket::ArpSocket(TinyIP* tip) : TinySocket(tip, IP_NONE)
|
ArpSocket::ArpSocket(TinyIP* tip) : TinySocket(tip, IP_NONE)
|
||||||
{
|
{
|
||||||
|
@ -80,7 +78,9 @@ bool ArpSocket::Process(IP_HEADER& ip, Stream& ms)
|
||||||
if(arp->Option == 0x0200 && _ArpSession && _ArpSession->IP.Value == arp->SrcIP)
|
if(arp->Option == 0x0200 && _ArpSession && _ArpSession->IP.Value == arp->SrcIP)
|
||||||
{
|
{
|
||||||
_ArpSession->Mac = arp->SrcMac.Value();
|
_ArpSession->Mac = arp->SrcMac.Value();
|
||||||
_ArpSession->Success = true;
|
_ArpSession->Handle.Set();
|
||||||
|
_ArpSession = nullptr;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,28 +164,14 @@ bool ArpSocket::Request(const IPAddress& ip, MacAddress& mac, int timeout)
|
||||||
// 如果没有超时时间,表示异步请求,不用等待结果
|
// 如果没有超时时间,表示异步请求,不用等待结果
|
||||||
if(timeout <= 0) return false;
|
if(timeout <= 0) return false;
|
||||||
|
|
||||||
// 等待反馈
|
// 等待响应
|
||||||
//if(Tip->LoopWait(RequestCallback, &mac, timeout * 1000)) return true;
|
|
||||||
|
|
||||||
ArpSession ss(ip);
|
ArpSession ss(ip);
|
||||||
_ArpSession = &ss;
|
_ArpSession = &ss;
|
||||||
|
|
||||||
// 等待响应
|
if(!ss.Handle.WaitOne(timeout)) return false;
|
||||||
TimeWheel tw(timeout);
|
|
||||||
tw.Sleep = 1;
|
|
||||||
do{
|
|
||||||
if(ss.Success) break;
|
|
||||||
}while(!tw.Expired());
|
|
||||||
|
|
||||||
if(ss.Success)
|
mac = ss.Mac;
|
||||||
{
|
return true;
|
||||||
mac = ss.Mac;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
_ArpSession = nullptr;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ArpSocket::Resolve(const IPAddress& ip, MacAddress& mac)
|
bool ArpSocket::Resolve(const IPAddress& ip, MacAddress& mac)
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
#include "Icmp.h"
|
#include "Icmp.h"
|
||||||
#include "Arp.h"
|
#include "Arp.h"
|
||||||
|
|
||||||
|
#include "WaitHandle.h"
|
||||||
|
|
||||||
#define NET_DEBUG DEBUG
|
#define NET_DEBUG DEBUG
|
||||||
|
|
||||||
class PingSession
|
class PingSession
|
||||||
|
@ -10,14 +12,13 @@ public:
|
||||||
IPAddress Address;
|
IPAddress Address;
|
||||||
ushort Identifier;
|
ushort Identifier;
|
||||||
ushort Sequence;
|
ushort Sequence;
|
||||||
bool Success;
|
WaitHandle Handle;
|
||||||
|
|
||||||
PingSession(IPAddress& ip, ushort id, ushort seq)
|
PingSession(IPAddress& ip, ushort id, ushort seq)
|
||||||
{
|
{
|
||||||
Address = ip;
|
Address = ip;
|
||||||
Identifier = id;
|
Identifier = id;
|
||||||
Sequence = seq;
|
Sequence = seq;
|
||||||
Success = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Check(IPAddress& remote, ICMP_HEADER* icmp)
|
bool Check(IPAddress& remote, ICMP_HEADER* icmp)
|
||||||
|
@ -50,7 +51,9 @@ bool IcmpSocket::Process(IP_HEADER& ip, Stream& ms)
|
||||||
// 检查有没有会话等待
|
// 检查有没有会话等待
|
||||||
if(icmp->Type == 0 && _IcmpSession != nullptr && _IcmpSession->Check(remote, icmp))
|
if(icmp->Type == 0 && _IcmpSession != nullptr && _IcmpSession->Check(remote, icmp))
|
||||||
{
|
{
|
||||||
_IcmpSession->Success = true;
|
_IcmpSession->Handle.Set();
|
||||||
|
_IcmpSession = nullptr;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,25 +150,15 @@ bool IcmpSocket::Ping(IPAddress& ip, uint payloadLength)
|
||||||
#endif
|
#endif
|
||||||
Tip->SendIP(IP_ICMP, ip, (byte*)icmp, sizeof(ICMP_HEADER) + payloadLength);
|
Tip->SendIP(IP_ICMP, ip, (byte*)icmp, sizeof(ICMP_HEADER) + payloadLength);
|
||||||
|
|
||||||
// 等待时间
|
|
||||||
//int ps[] = {ip.Value, id, seq};
|
|
||||||
//if(Tip->LoopWait(PingCallback, ps, 1000)) return true;
|
|
||||||
|
|
||||||
PingSession ps(ip, id, seq);
|
PingSession ps(ip, id, seq);
|
||||||
_IcmpSession = &ps;
|
_IcmpSession = &ps;
|
||||||
|
|
||||||
// 等待响应
|
// 等待响应
|
||||||
TimeWheel tw(0, 1000);
|
bool rs = ps.Handle.WaitOne(1000);
|
||||||
tw.Sleep = 1;
|
|
||||||
do{
|
|
||||||
if(ps.Success) break;
|
|
||||||
}while(!tw.Expired());
|
|
||||||
|
|
||||||
_IcmpSession = nullptr;
|
|
||||||
|
|
||||||
#if NET_DEBUG
|
#if NET_DEBUG
|
||||||
uint cost = ct.Elapsed() / 1000;
|
uint cost = ct.Elapsed() / 1000;
|
||||||
if(ps.Success)
|
if(rs)
|
||||||
debug_printf(" 成功 %dms\r\n", cost);
|
debug_printf(" 成功 %dms\r\n", cost);
|
||||||
else
|
else
|
||||||
debug_printf(" 失败 %dms\r\n", cost);
|
debug_printf(" 失败 %dms\r\n", cost);
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
#include "Time.h"
|
#include "Time.h"
|
||||||
#include "Tcp.h"
|
#include "Tcp.h"
|
||||||
|
|
||||||
|
#include "WaitHandle.h"
|
||||||
|
|
||||||
#define NET_DEBUG 0
|
#define NET_DEBUG 0
|
||||||
//#define NET_DEBUG DEBUG
|
//#define NET_DEBUG DEBUG
|
||||||
|
|
||||||
bool* WaitAck;
|
|
||||||
|
|
||||||
bool Callback(TinyIP* tip, void* param, Stream& ms);
|
bool Callback(TinyIP* tip, void* param, Stream& ms);
|
||||||
|
|
||||||
TcpSocket::TcpSocket(TinyIP* tip) : TinySocket(tip, IP_TCP)
|
TcpSocket::TcpSocket(TinyIP* tip) : TinySocket(tip, IP_TCP)
|
||||||
|
@ -79,9 +79,10 @@ bool TcpSocket::Process(IP_HEADER& ip, Stream& ms)
|
||||||
//Local.Port = port;
|
//Local.Port = port;
|
||||||
//Local.Address = ip.DestIP;
|
//Local.Address = ip.DestIP;
|
||||||
|
|
||||||
if(WaitAck && (tcp->Flags & TCP_FLAGS_ACK))
|
if(_wait && (tcp->Flags & TCP_FLAGS_ACK))
|
||||||
{
|
{
|
||||||
*WaitAck = true;
|
((WaitHandle*)_wait)->Set();
|
||||||
|
_wait = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
OnProcess(*tcp, ms);
|
OnProcess(*tcp, ms);
|
||||||
|
@ -408,26 +409,19 @@ bool TcpSocket::Send(const Buffer& bs)
|
||||||
//debug_printf("Seq=0x%04x Ack=0x%04x \r\n", Seq, Ack);
|
//debug_printf("Seq=0x%04x Ack=0x%04x \r\n", Seq, Ack);
|
||||||
if(!SendPacket(*tcp, bs.Length(), TCP_FLAGS_PUSH | TCP_FLAGS_ACK)) return false;
|
if(!SendPacket(*tcp, bs.Length(), TCP_FLAGS_PUSH | TCP_FLAGS_ACK)) return false;
|
||||||
|
|
||||||
bool wait = false;
|
|
||||||
WaitAck = &wait;
|
|
||||||
|
|
||||||
// 等待响应
|
// 等待响应
|
||||||
TimeWheel tw(0, 3000);
|
WaitHandle wh;
|
||||||
tw.Sleep = 1;
|
_wait = &wh;
|
||||||
do{
|
bool rs = wh.WaitOne(3000);
|
||||||
if(wait) break;
|
|
||||||
}while(!tw.Expired());
|
|
||||||
|
|
||||||
WaitAck = nullptr;
|
|
||||||
|
|
||||||
#if NET_DEBUG
|
#if NET_DEBUG
|
||||||
if(wait)
|
if(rs)
|
||||||
debug_printf("发送成功!\r\n");
|
debug_printf("发送成功!\r\n");
|
||||||
else
|
else
|
||||||
debug_printf("发送失败!\r\n");
|
debug_printf("发送失败!\r\n");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return wait;
|
return rs;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint TcpSocket::Receive(Buffer& bs)
|
uint TcpSocket::Receive(Buffer& bs)
|
||||||
|
@ -474,19 +468,11 @@ bool TcpSocket::Connect(IPAddress& ip, ushort port)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wait = false;
|
|
||||||
WaitAck = &wait;
|
|
||||||
|
|
||||||
// 等待响应
|
// 等待响应
|
||||||
TimeWheel tw(0, 3000);
|
WaitHandle wh;
|
||||||
tw.Sleep = 1;
|
_wait = &wh;
|
||||||
do{
|
|
||||||
if(wait) break;
|
|
||||||
}while(!tw.Expired());
|
|
||||||
|
|
||||||
WaitAck = nullptr;
|
if(wh.WaitOne(3000))
|
||||||
|
|
||||||
if(wait)
|
|
||||||
{
|
{
|
||||||
if(Status == Established)
|
if(Status == Established)
|
||||||
{
|
{
|
||||||
|
|
|
@ -70,6 +70,9 @@ protected:
|
||||||
|
|
||||||
virtual bool OnWrite(const Buffer& bs);
|
virtual bool OnWrite(const Buffer& bs);
|
||||||
virtual uint OnRead(Buffer& bs);
|
virtual uint OnRead(Buffer& bs);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void* _wait = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue