forked from xuos/xiuos
Modify the return value of the mqtt function
This commit is contained in:
parent
5ef61d14b1
commit
60a7835ef8
|
@ -106,9 +106,9 @@ int MQTT_Recv(uint8_t* buf, int buflen)
|
||||||
* 函 数 名: MQTT_Connect
|
* 函 数 名: MQTT_Connect
|
||||||
* 功能描述: 登录MQTT服务器
|
* 功能描述: 登录MQTT服务器
|
||||||
* 形 参: 无
|
* 形 参: 无
|
||||||
* 返 回 值: 0表示成功,1表示失败
|
* 返 回 值: true表示成功,false表示失败
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
int MQTT_Connect(void)
|
bool MQTT_Connect(void)
|
||||||
{
|
{
|
||||||
uint8_t TryConnect_time = 10; //尝试登录次数
|
uint8_t TryConnect_time = 10; //尝试登录次数
|
||||||
|
|
||||||
|
@ -180,11 +180,11 @@ int MQTT_Connect(void)
|
||||||
MQTT_Recv(mqtt_rxbuf, 4);
|
MQTT_Recv(mqtt_rxbuf, 4);
|
||||||
if(mqtt_rxbuf[0] == parket_connetAck[0] && mqtt_rxbuf[1] == parket_connetAck[1]) //连接成功
|
if(mqtt_rxbuf[0] == parket_connetAck[0] && mqtt_rxbuf[1] == parket_connetAck[1]) //连接成功
|
||||||
{
|
{
|
||||||
return 0;
|
return true;
|
||||||
}
|
}
|
||||||
TryConnect_time--;
|
TryConnect_time--;
|
||||||
}
|
}
|
||||||
return 1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -204,9 +204,9 @@ void MQTT_Disconnect(void)
|
||||||
* 函 数 名: MQTT_SubscribeTopic
|
* 函 数 名: MQTT_SubscribeTopic
|
||||||
* 功能描述: MQTT订阅单个主题
|
* 功能描述: MQTT订阅单个主题
|
||||||
* 形 参: topic_name:要订阅的主题
|
* 形 参: topic_name:要订阅的主题
|
||||||
* 返 回 值: 0表示订阅成功,1表示订阅失败
|
* 返 回 值: true表示订阅成功,false表示订阅失败
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
int MQTT_SubscribeTopic(uint8_t *topic_name)
|
bool MQTT_SubscribeTopic(uint8_t *topic_name)
|
||||||
{
|
{
|
||||||
uint8_t TrySub_time = 10; //尝试订阅次数
|
uint8_t TrySub_time = 10; //尝试订阅次数
|
||||||
|
|
||||||
|
@ -249,11 +249,11 @@ int MQTT_SubscribeTopic(uint8_t *topic_name)
|
||||||
MQTT_Recv(mqtt_rxbuf, 5);
|
MQTT_Recv(mqtt_rxbuf, 5);
|
||||||
if(mqtt_rxbuf[0] == parket_subAck[0] && mqtt_rxbuf[1] == parket_subAck[1]) //订阅成功
|
if(mqtt_rxbuf[0] == parket_subAck[0] && mqtt_rxbuf[1] == parket_subAck[1]) //订阅成功
|
||||||
{
|
{
|
||||||
return 0;
|
return true;
|
||||||
}
|
}
|
||||||
TrySub_time--;
|
TrySub_time--;
|
||||||
}
|
}
|
||||||
return 1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -261,9 +261,9 @@ int MQTT_SubscribeTopic(uint8_t *topic_name)
|
||||||
* 函 数 名: MQTT_UnSubscribeTopic
|
* 函 数 名: MQTT_UnSubscribeTopic
|
||||||
* 功能描述: MQTT取消订阅单个主题
|
* 功能描述: MQTT取消订阅单个主题
|
||||||
* 形 参: topic_name:要取消订阅的主题
|
* 形 参: topic_name:要取消订阅的主题
|
||||||
* 返 回 值: 0表示订阅成功,1表示订阅失败
|
* 返 回 值: true表示取消订阅成功,false表示取消订阅失败
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
int MQTT_UnSubscribeTopic(uint8_t *topic_name)
|
bool MQTT_UnSubscribeTopic(uint8_t *topic_name)
|
||||||
{
|
{
|
||||||
uint8_t TryUnSub_time = 10; //尝试取消订阅次数
|
uint8_t TryUnSub_time = 10; //尝试取消订阅次数
|
||||||
|
|
||||||
|
@ -303,11 +303,11 @@ int MQTT_UnSubscribeTopic(uint8_t *topic_name)
|
||||||
MQTT_Recv(mqtt_rxbuf, 4);
|
MQTT_Recv(mqtt_rxbuf, 4);
|
||||||
if(mqtt_rxbuf[0] == parket_unsubAck[0] && mqtt_rxbuf[1] == parket_unsubAck[1]) //取消订阅成功
|
if(mqtt_rxbuf[0] == parket_unsubAck[0] && mqtt_rxbuf[1] == parket_unsubAck[1]) //取消订阅成功
|
||||||
{
|
{
|
||||||
return 0;
|
return true;
|
||||||
}
|
}
|
||||||
TryUnSub_time--;
|
TryUnSub_time--;
|
||||||
}
|
}
|
||||||
return 1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -397,9 +397,9 @@ void MQTT_PublishDataQs1(uint8_t *topic_name,uint8_t *data, uint16_t data_len)
|
||||||
* 函 数 名: MQTT_SendHeart
|
* 函 数 名: MQTT_SendHeart
|
||||||
* 功能描述: 发送心跳保活包
|
* 功能描述: 发送心跳保活包
|
||||||
* 形 参: 无
|
* 形 参: 无
|
||||||
* 返 回 值: 0表示发送成功,其他值表示发送失败
|
* 返 回 值: true表示发送成功,false表示发送失败
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
int MQTT_SendHeart(void)
|
bool MQTT_SendHeart(void)
|
||||||
{
|
{
|
||||||
uint8_t TrySentHeart_time = 10; //尝试发送心跳保活次数
|
uint8_t TrySentHeart_time = 10; //尝试发送心跳保活次数
|
||||||
while(TrySentHeart_time > 0)
|
while(TrySentHeart_time > 0)
|
||||||
|
@ -410,11 +410,11 @@ int MQTT_SendHeart(void)
|
||||||
MQTT_Recv(mqtt_rxbuf, 2);
|
MQTT_Recv(mqtt_rxbuf, 2);
|
||||||
if(mqtt_rxbuf[0] == 0xD0 && mqtt_rxbuf[1] == 0x00)
|
if(mqtt_rxbuf[0] == 0xD0 && mqtt_rxbuf[1] == 0x00)
|
||||||
{
|
{
|
||||||
return 0;
|
return true;
|
||||||
}
|
}
|
||||||
TrySentHeart_time--;
|
TrySentHeart_time--;
|
||||||
}
|
}
|
||||||
return 1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
#define _PLATFORM_MQTT_H_
|
#define _PLATFORM_MQTT_H_
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#define KEEPALIVE_TIME 300 //保活时间(单位s),300s
|
#define KEEPALIVE_TIME 300 //保活时间(单位s),300s
|
||||||
#define HEART_TIME 200000 //空闲时发送心跳包的时间间隔(单位ms),200s
|
#define HEART_TIME 200000 //空闲时发送心跳包的时间间隔(单位ms),200s
|
||||||
|
@ -50,12 +51,12 @@ extern MQTT_TCB Platform_mqtt; //外部变量声明
|
||||||
int AdapterNetActive(void);
|
int AdapterNetActive(void);
|
||||||
int MQTT_Send(const uint8_t* buf, int buflen);
|
int MQTT_Send(const uint8_t* buf, int buflen);
|
||||||
int MQTT_Recv(uint8_t* buf, int buflen);
|
int MQTT_Recv(uint8_t* buf, int buflen);
|
||||||
int MQTT_Connect(void);
|
bool MQTT_Connect(void);
|
||||||
void MQTT_Disconnect(void);
|
void MQTT_Disconnect(void);
|
||||||
int MQTT_SubscribeTopic(uint8_t *topic_name);
|
bool MQTT_SubscribeTopic(uint8_t *topic_name);
|
||||||
int MQTT_UnSubscribeTopic(uint8_t *topic_name);
|
bool MQTT_UnSubscribeTopic(uint8_t *topic_name);
|
||||||
void MQTT_PublishDataQs0(uint8_t *topic_name,uint8_t *data, uint16_t data_len);
|
void MQTT_PublishDataQs0(uint8_t *topic_name,uint8_t *data, uint16_t data_len);
|
||||||
void MQTT_PublishDataQs1(uint8_t *topic_name,uint8_t *data, uint16_t data_len);
|
void MQTT_PublishDataQs1(uint8_t *topic_name,uint8_t *data, uint16_t data_len);
|
||||||
int MQTT_SendHeart(void);
|
bool MQTT_SendHeart(void);
|
||||||
void MQTT_DealPublishData(uint8_t *data, uint16_t data_len);
|
void MQTT_DealPublishData(uint8_t *data, uint16_t data_len);
|
||||||
#endif
|
#endif
|
|
@ -863,7 +863,7 @@ static void app_ota_by_platform(void* parameter)
|
||||||
sprintf(topicdatabuff[1],"ota/%s/files",CLIENTID);
|
sprintf(topicdatabuff[1],"ota/%s/files",CLIENTID);
|
||||||
|
|
||||||
reconnect:
|
reconnect:
|
||||||
if((AdapterNetActive() == 0) && (MQTT_Connect() == 0) && MQTT_SubscribeTopic(topicdatabuff[0]) == 0 && MQTT_SubscribeTopic(topicdatabuff[1]) == 0)
|
if((AdapterNetActive() == 0) && MQTT_Connect() && MQTT_SubscribeTopic(topicdatabuff[0]) && MQTT_SubscribeTopic(topicdatabuff[1]))
|
||||||
{
|
{
|
||||||
KPrintf("Log in to the cloud platform and subscribe to the topic successfully.\n");
|
KPrintf("Log in to the cloud platform and subscribe to the topic successfully.\n");
|
||||||
PropertyVersion();
|
PropertyVersion();
|
||||||
|
@ -885,7 +885,7 @@ reconnect:
|
||||||
{
|
{
|
||||||
heart_time = CalculateTimeMsFromTick(CurrentTicksGain());
|
heart_time = CalculateTimeMsFromTick(CurrentTicksGain());
|
||||||
freecnt = 0;
|
freecnt = 0;
|
||||||
if(MQTT_SendHeart() != 0) //发送心跳包失败可能连接断开,需要重连
|
if(!MQTT_SendHeart()) //发送心跳包失败可能连接断开,需要重连
|
||||||
{
|
{
|
||||||
KPrintf("The connection has been disconnected, reconnecting!\n");
|
KPrintf("The connection has been disconnected, reconnecting!\n");
|
||||||
goto reconnect;
|
goto reconnect;
|
||||||
|
@ -897,11 +897,12 @@ reconnect:
|
||||||
{
|
{
|
||||||
freecnt = 0;
|
freecnt = 0;
|
||||||
MQTT_DealPublishData(MqttRxbuf, datalen);
|
MQTT_DealPublishData(MqttRxbuf, datalen);
|
||||||
|
|
||||||
|
// 1.获取新版本固件大小及版本信息
|
||||||
ptr1 = strstr((char *)Platform_mqtt.cmdbuff,topicdatabuff[0]);
|
ptr1 = strstr((char *)Platform_mqtt.cmdbuff,topicdatabuff[0]);
|
||||||
ptr2 = strstr((char *)Platform_mqtt.cmdbuff,"{\"fileSize\":");
|
ptr2 = strstr((char *)Platform_mqtt.cmdbuff,"{\"fileSize\":");
|
||||||
if((ptr1 != NULL) &&(ptr2 != NULL))
|
if((ptr1 != NULL) &&(ptr2 != NULL))
|
||||||
{
|
{
|
||||||
// 1.获取新版本固件大小及版本信息
|
|
||||||
if(sscanf(ptr2,"{\"fileSize\":%d,\"version\":\"%11s\",\"fileId\":%d,\"md5\"",&platform_ota.size,platform_ota.version,&platform_ota.streamId)==3)
|
if(sscanf(ptr2,"{\"fileSize\":%d,\"version\":\"%11s\",\"fileId\":%d,\"md5\"",&platform_ota.size,platform_ota.version,&platform_ota.streamId)==3)
|
||||||
{
|
{
|
||||||
KPrintf("------Start the firmware file transfer!------\r\n");
|
KPrintf("------Start the firmware file transfer!------\r\n");
|
||||||
|
@ -1001,7 +1002,7 @@ reconnect:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3.新版本固件接收完毕,写入描述信息
|
// 新版本固件接收完毕,写入描述信息
|
||||||
if(0 == ret)
|
if(0 == ret)
|
||||||
{
|
{
|
||||||
ota_info.down.size = platform_ota.size;
|
ota_info.down.size = platform_ota.size;
|
||||||
|
@ -1107,7 +1108,7 @@ static void app_ota_by_platform(void* parameter)
|
||||||
sprintf(topicdatabuff,"/sys/%s/%s/thing/file/download_reply",PLATFORM_PRODUCTKEY,CLIENT_DEVICENAME);
|
sprintf(topicdatabuff,"/sys/%s/%s/thing/file/download_reply",PLATFORM_PRODUCTKEY,CLIENT_DEVICENAME);
|
||||||
|
|
||||||
reconnect:
|
reconnect:
|
||||||
if((AdapterNetActive() == 0) && (MQTT_Connect() == 0) && MQTT_SubscribeTopic(topicdatabuff) == 0)
|
if((AdapterNetActive() == 0) && MQTT_Connect() && MQTT_SubscribeTopic(topicdatabuff))
|
||||||
{
|
{
|
||||||
KPrintf("Log in to the cloud platform and subscribe to the topic successfully.\n");
|
KPrintf("Log in to the cloud platform and subscribe to the topic successfully.\n");
|
||||||
PropertyVersion();
|
PropertyVersion();
|
||||||
|
@ -1129,7 +1130,7 @@ reconnect:
|
||||||
{
|
{
|
||||||
heart_time = CalculateTimeMsFromTick(CurrentTicksGain());
|
heart_time = CalculateTimeMsFromTick(CurrentTicksGain());
|
||||||
freecnt = 0;
|
freecnt = 0;
|
||||||
if(MQTT_SendHeart() != 0) //发送心跳包失败可能连接断开,需要重连
|
if(!MQTT_SendHeart()) //发送心跳包失败可能连接断开,需要重连
|
||||||
{
|
{
|
||||||
KPrintf("The connection has been disconnected, reconnecting!\n");
|
KPrintf("The connection has been disconnected, reconnecting!\n");
|
||||||
goto reconnect;
|
goto reconnect;
|
||||||
|
@ -1141,10 +1142,11 @@ reconnect:
|
||||||
{
|
{
|
||||||
freecnt = 0;
|
freecnt = 0;
|
||||||
MQTT_DealPublishData(MqttRxbuf, datalen);
|
MQTT_DealPublishData(MqttRxbuf, datalen);
|
||||||
ptr = strstr((char *)Platform_mqtt.cmdbuff,"{\"code\":\"1000\"");
|
|
||||||
|
// 1.获取新版本固件大小及版本信息
|
||||||
|
ptr = strstr((char *)Platform_mqtt.cmdbuff,"{\"code\":\"1000\"");
|
||||||
if(ptr != NULL)
|
if(ptr != NULL)
|
||||||
{
|
{
|
||||||
// 1.获取新版本固件大小及版本信息
|
|
||||||
if(sscanf(ptr,"{\"code\":\"1000\",\"data\":{\"size\":%d,\"streamId\":%d,\"sign\":\"%*32s\",\"dProtocol\":\"mqtt\",\"version\":\"%11s\"",&platform_ota.size,&platform_ota.streamId,platform_ota.version)==3)
|
if(sscanf(ptr,"{\"code\":\"1000\",\"data\":{\"size\":%d,\"streamId\":%d,\"sign\":\"%*32s\",\"dProtocol\":\"mqtt\",\"version\":\"%11s\"",&platform_ota.size,&platform_ota.streamId,platform_ota.version)==3)
|
||||||
{
|
{
|
||||||
KPrintf("------Start the firmware file transfer!------\r\n");
|
KPrintf("------Start the firmware file transfer!------\r\n");
|
||||||
|
@ -1240,7 +1242,7 @@ reconnect:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3.新版本固件接收完毕,写入描述信息
|
// 新版本固件接收完毕,写入描述信息
|
||||||
if(0 == ret)
|
if(0 == ret)
|
||||||
{
|
{
|
||||||
ota_info.down.size = platform_ota.size;
|
ota_info.down.size = platform_ota.size;
|
||||||
|
|
Loading…
Reference in New Issue