diff --git a/APP_Framework/Framework/connection/adapter_agent.c b/APP_Framework/Framework/connection/adapter_agent.c index 4ffb2de1a..0dc75b52c 100755 --- a/APP_Framework/Framework/connection/adapter_agent.c +++ b/APP_Framework/Framework/connection/adapter_agent.c @@ -124,7 +124,9 @@ int ParseATReply(char *str, const char *format, ...) void ATSprintf(int fd, const char *format, va_list params) { last_cmd_len = vsnprintf(send_buf, sizeof(send_buf), format, params); +#ifdef CONNECTION_FRAMEWORK_DEBUG printf("AT send %s len %u\n",send_buf, last_cmd_len); +#endif PrivWrite(fd, send_buf, last_cmd_len); } @@ -279,7 +281,9 @@ int EntmSend(ATAgentType agent, const char *data, int len) PrivWrite(agent->fd, send_buff, len); PrivMutexAbandon(&agent->lock); +#ifdef CONNECTION_FRAMEWORK_DEBUG printf("entm send length %d\n", len); +#endif PrivFree(send_buff); @@ -301,13 +305,16 @@ int EntmRecv(ATAgentType agent, char *rev_buffer, int buffer_len, int timeout_s) PrivMutexAbandon(&agent->lock); //PrivTaskDelay(1000); if (PrivSemaphoreObtainWait(&agent->entm_rx_notice, &abstime)) { +#ifdef CONNECTION_FRAMEWORK_DEBUG printf("wait sem[%d] timeout\n",agent->entm_rx_notice); +#endif + agent->entm_recv_len = 0; return -1; } PrivMutexObtain(&agent->lock); - +#ifdef CONNECTION_FRAMEWORK_DEBUG printf("EntmRecv once len %d.\n", agent->entm_recv_len); - +#endif memcpy(rev_buffer, agent->entm_recv_buf, agent->entm_recv_len); memset(agent->entm_recv_buf, 0, ENTM_RECV_MAX); @@ -352,7 +359,9 @@ static int GetCompleteATReply(ATAgentType agent) PrivMutexAbandon(&agent->lock); continue; } else { +#ifdef CONNECTION_FRAMEWORK_DEBUG printf("ENTM_MODE recv %d Bytes done.\n",agent->entm_recv_len); +#endif agent->receive_mode = DEFAULT_MODE; PrivSemaphoreAbandon(&agent->entm_rx_notice); } diff --git a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/.defconfig b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/.defconfig index 7d4fa4771..5717d7a8f 100644 --- a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/.defconfig +++ b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/.defconfig @@ -129,7 +129,7 @@ CONFIG_ZOMBIE_KTASK_STACKSIZE=2048 # CONFIG_KERNEL_CONSOLE=y CONFIG_KERNEL_BANNER=y -CONFIG_KERNEL_CONSOLEBUF_SIZE=128 +CONFIG_KERNEL_CONSOLEBUF_SIZE=256 # # Kernel Hook diff --git a/Ubiquitous/XiZi_IIoT/tool/Kconfig b/Ubiquitous/XiZi_IIoT/tool/Kconfig index 1989064ce..0c3aa9494 100644 --- a/Ubiquitous/XiZi_IIoT/tool/Kconfig +++ b/Ubiquitous/XiZi_IIoT/tool/Kconfig @@ -1,5 +1,6 @@ menu "Tool feature" source "$KERNEL_DIR/tool/bootloader/Kconfig" + source "$KERNEL_DIR/tool/mqtt/Kconfig" endmenu diff --git a/Ubiquitous/XiZi_IIoT/tool/Makefile b/Ubiquitous/XiZi_IIoT/tool/Makefile index cd148dd4f..e18b07080 100644 --- a/Ubiquitous/XiZi_IIoT/tool/Makefile +++ b/Ubiquitous/XiZi_IIoT/tool/Makefile @@ -8,4 +8,8 @@ ifeq ($(CONFIG_TOOL_USING_OTA),y) SRC_DIR += bootloader endif +ifeq ($(CONFIG_TOOL_USING_MQTT),y) + SRC_DIR += mqtt +endif + include $(KERNEL_ROOT)/compiler.mk diff --git a/Ubiquitous/XiZi_IIoT/tool/mqtt/Kconfig b/Ubiquitous/XiZi_IIoT/tool/mqtt/Kconfig new file mode 100644 index 000000000..f8c512ab7 --- /dev/null +++ b/Ubiquitous/XiZi_IIoT/tool/mqtt/Kconfig @@ -0,0 +1,12 @@ +menu "MQTT function" + + menuconfig TOOL_USING_MQTT + bool "Enable support MQTT function" + default n + select SUPPORT_CONNECTION_FRAMEWORK + select CONNECTION_ADAPTER_4G + + if TOOL_USING_MQTT + endif + +endmenu diff --git a/Ubiquitous/XiZi_IIoT/tool/mqtt/Makefile b/Ubiquitous/XiZi_IIoT/tool/mqtt/Makefile new file mode 100644 index 000000000..e181150ae --- /dev/null +++ b/Ubiquitous/XiZi_IIoT/tool/mqtt/Makefile @@ -0,0 +1,3 @@ +SRC_FILES := aliyun_mqtt.c + +include $(KERNEL_ROOT)/compiler.mk \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/tool/mqtt/aliyun_mqtt.c b/Ubiquitous/XiZi_IIoT/tool/mqtt/aliyun_mqtt.c new file mode 100644 index 000000000..4d5f38148 --- /dev/null +++ b/Ubiquitous/XiZi_IIoT/tool/mqtt/aliyun_mqtt.c @@ -0,0 +1,404 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** +* @file: aliyun_mqtt.c +* @brief: connect aliyun mqtt +* @version: 1.0 +* @author: AIIT XUOS Lab +* @date: 2023/6/14 +* +*/ + +#include +#include +#include +#include +#include "shell.h" +#include "xsconfig.h" +#include +#include "aliyun_mqtt.h" + + +static struct Adapter *adapter; +static MQTT_TCB Aliyun_mqtt; //创建一个用于连接阿里云mqtt的结构体 +static const uint8_t parket_connetAck[] = {0x20,0x02,0x00,0x00}; //连接成功服务器回应报文 +static const uint8_t parket_disconnet[] = {0xe0,0x00}; //客户端主动断开连接发送报文 +static const uint8_t parket_heart[] = {0xc0,0x00}; //客户端发送保活心跳包 +static const uint8_t parket_subAck[] = {0x90,0x03,0x00,0x0A,0x01}; //订阅成功服务器回应报文 +static const uint8_t parket_unsubAck[] = {0xB0,0x02,0x00,0x0A}; //取消订阅成功服务器回应报文 +static uint8_t mqtt_rxbuf[512]; + + +/******************************************************************************* +* 函 数 名: AdapterNetActive +* 功能描述: 使能设备的网络模块连接,连接TCP服务器并进入透传模式,当前使用4G方式 +* 形 参: 无 +* 返 回 值: 0表示成功,其他值表示失败 +*******************************************************************************/ +int AdapterNetActive(void) +{ + int ret = 0; + uint32_t baud_rate = BAUD_RATE_115200; + adapter = AdapterDeviceFindByName(ADAPTER_4G_NAME); + adapter->socket.socket_id = 0; + + ret = AdapterDeviceOpen(adapter); + if (ret < 0) + { + goto out; + } + + ret = AdapterDeviceControl(adapter, OPE_INT, &baud_rate); + if (ret < 0) + { + goto out; + } + + ret = AdapterDeviceConnect(adapter, CLIENT, SERVERIP, SERVERPORT, IPV4); + if (ret < 0) + { + goto out; + } + +out: + if (ret < 0) + { + AdapterDeviceClose(adapter); + } + + return ret; +} + + +/******************************************************************************* +* 函 数 名: MQTT_Connect +* 功能描述: 登录MQTT服务器 +* 形 参: 无 +* 返 回 值: 0表示成功,1表示失败 +*******************************************************************************/ +int MQTT_Connect(void) +{ + uint8_t TryConnect_time = 10; //尝试登录次数 + + Aliyun_mqtt.MessageID = 0; //报文标识符清零,CONNECT报文虽然不需要添加报文标识符,但是CONNECT报文是第一个发送的报文,在此清零报文标识符为后续报文做准备 + Aliyun_mqtt.Fixed_len = 1; //CONNECT报文固定报头长度暂定为1 + Aliyun_mqtt.Variable_len = 10; //CONNECT报文可变报头长度为10 + Aliyun_mqtt.Payload_len = (2+strlen(CLIENTID)) + (2+strlen(USERNAME)) + (2+strlen(PASSWORD)); //CONNECT报文中负载长度 + Aliyun_mqtt.Remaining_len = Aliyun_mqtt.Variable_len + Aliyun_mqtt.Payload_len; //剩余长度=可变报头长度+负载长度 + memset(Aliyun_mqtt.Pack_buff,0,sizeof(Aliyun_mqtt.Pack_buff)); + + Aliyun_mqtt.Pack_buff[0] = 0x10; //CONNECT报文 固定报头第1个字节0x10 + do{ + if((Aliyun_mqtt.Remaining_len/128) == 0) + { + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len] = Aliyun_mqtt.Remaining_len; + } + else + { + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len] = (Aliyun_mqtt.Remaining_len%128)|0x80; + } + Aliyun_mqtt.Fixed_len++; + Aliyun_mqtt.Remaining_len = Aliyun_mqtt.Remaining_len/128; + }while(Aliyun_mqtt.Remaining_len); + + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+0] = 0x00; //CONNECT报文,可变报头第1个字节:固定0x00 + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+1] = 0x04; //CONNECT报文,可变报头第2个字节:固定0x04 + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+2] = 0x4D; //CONNECT报文,可变报头第3个字节:固定0x4D,大写字母M + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+3] = 0x51; //CONNECT报文,可变报头第4个字节:固定0x51,大写字母Q + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+4] = 0x54; //CONNECT报文,可变报头第5个字节:固定0x54,大写字母T + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+5] = 0x54; //CONNECT报文,可变报头第6个字节:固定0x54,大写字母T + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+6] = 0x04; //CONNECT报文,可变报头第7个字节:固定0x04 + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+7] = 0xC2; //CONNECT报文,可变报头第8个字节:使能用户名和密码校验,不使用遗嘱功能,不保留会话功能 + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+8] = 0x00; //CONNECT报文,可变报头第9个字节:保活时间高字节 + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+9] = 0x64; //CONNECT报文,可变报头第10个字节:保活时间高字节 + + /* CLIENT_ID */ + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+10] = strlen(CLIENTID)/256; //客户端ID长度高字节 + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+11] = strlen(CLIENTID)%256; //客户端ID长度低字节 + memcpy(&Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+12],CLIENTID,strlen(CLIENTID)); //复制过来客户端ID字串 + /* USER_NAME */ + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+12+strlen(CLIENTID)] = strlen(USERNAME)/256; //用户名长度高字节 + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+13+strlen(CLIENTID)] = strlen(USERNAME)%256; //用户名长度低字节 + memcpy(&Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+14+strlen(CLIENTID)],USERNAME,strlen(USERNAME)); //复制过来用户名字串 + /* PASSWARD */ + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+14+strlen(CLIENTID)+strlen(USERNAME)] = strlen(PASSWORD)/256; //密码长度高字节 + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+15+strlen(CLIENTID)+strlen(USERNAME)] = strlen(PASSWORD)%256; //密码长度低字节 + memcpy(&Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+16+strlen(CLIENTID)+strlen(USERNAME)],PASSWORD,strlen(PASSWORD)); //复制过来密码字串 + + while(TryConnect_time > 0) + { + memset(mqtt_rxbuf,0,sizeof(mqtt_rxbuf)); + AdapterDeviceSend(adapter,Aliyun_mqtt.Pack_buff,Aliyun_mqtt.Fixed_len + Aliyun_mqtt.Variable_len + Aliyun_mqtt.Payload_len); + PrivTaskDelay(50); + AdapterDeviceRecv(adapter, mqtt_rxbuf, 4); + if(mqtt_rxbuf[0] == parket_connetAck[0] && mqtt_rxbuf[1] == parket_connetAck[1]) //连接成功 + { + return 0; + } + TryConnect_time--; + } + return 1; +} + + +/******************************************************************************* +* 函 数 名: MQTT_Disconnect +* 功能描述: 断开与MQTT服务器的连接 +* 形 参: 无 +* 返 回 值: 无 +*******************************************************************************/ +void MQTT_Disconnect(void) +{ + while(AdapterDeviceSend(adapter,parket_disconnet,sizeof(parket_disconnet)) < 0); +} + + +/******************************************************************************* +* 函 数 名: MQTT_SubscribeTopic +* 功能描述: MQTT订阅单个主题 +* 形 参: topic_name:要订阅的主题 +* 返 回 值: 0表示订阅成功,1表示订阅失败 +*******************************************************************************/ +int MQTT_SubscribeTopic(uint8_t *topic_name) +{ + uint8_t TrySub_time = 10; //尝试订阅次数 + + Aliyun_mqtt.Fixed_len = 1; //SUBSCRIBE报文,固定报头长度暂定为1 + Aliyun_mqtt.Variable_len = 2;//SUBSCRIBE报文,可变报头长度=2,2为字节报文标识符 + Aliyun_mqtt.Payload_len = 0; //SUBSCRIBE报文,负载数据长度暂定为0 + + Aliyun_mqtt.Payload_len = strlen(topic_name) + 2 + 1; //每个需要订阅的topic除了本身的字符串长度,还包含表示topic字符串长度的2字节,以及订阅等级1字节 + Aliyun_mqtt.Remaining_len = Aliyun_mqtt.Variable_len + Aliyun_mqtt.Payload_len; //计算剩余长度=可变报头长度+负载长度 + memset(Aliyun_mqtt.Pack_buff,0,sizeof(Aliyun_mqtt.Pack_buff)); + + Aliyun_mqtt.Pack_buff[0]=0x82; //SUBSCRIBE报文,固定报头第1个字节0x82 + do{ + if((Aliyun_mqtt.Remaining_len/128) == 0) + { + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len] = Aliyun_mqtt.Remaining_len; + } + else + { + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len] = (Aliyun_mqtt.Remaining_len%128)|0x80; + } + Aliyun_mqtt.Fixed_len++; + Aliyun_mqtt.Remaining_len = Aliyun_mqtt.Remaining_len/128; + }while(Aliyun_mqtt.Remaining_len); + + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+0] = Aliyun_mqtt.MessageID/256; //报文标识符高字节 + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+1] = Aliyun_mqtt.MessageID%256; //报文标识符低字节 + Aliyun_mqtt.MessageID++; //每用一次MessageID加1 + + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+2] = strlen(topic_name)/256; //主题长度高字节 + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+3] = strlen(topic_name)%256; //主题长度低字节 + memcpy(&Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+4],topic_name,strlen(topic_name)); //复制主题字串 + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+4+strlen(topic_name)] = 0; //QOS等级设置为0 + + while(TrySub_time > 0) + { + memset(mqtt_rxbuf,0,sizeof(mqtt_rxbuf)); + AdapterDeviceSend(adapter,Aliyun_mqtt.Pack_buff,Aliyun_mqtt.Fixed_len + Aliyun_mqtt.Variable_len + Aliyun_mqtt.Payload_len); + PrivTaskDelay(50); + AdapterDeviceRecv(adapter, mqtt_rxbuf, 5); + if(mqtt_rxbuf[0] == parket_subAck[0] && mqtt_rxbuf[1] == parket_subAck[1]) //订阅成功 + { + return 0; + } + TrySub_time--; + } + return 1; +} + + +/******************************************************************************* +* 函 数 名: MQTT_UnSubscribeTopic +* 功能描述: MQTT取消订阅单个主题 +* 形 参: topic_name:要取消订阅的主题 +* 返 回 值: 0表示订阅成功,1表示订阅失败 +*******************************************************************************/ +int MQTT_UnSubscribeTopic(uint8_t *topic_name) +{ + uint8_t TryUnSub_time = 10; //尝试取消订阅次数 + + Aliyun_mqtt.Fixed_len = 1; //UNSUBSCRIBE报文,固定报头长度暂定为1 + Aliyun_mqtt.Variable_len = 2;//UNSUBSCRIBE报文,可变报头长度=2,2为字节报文标识符 + Aliyun_mqtt.Payload_len = strlen(topic_name) + 2; //每个需要取消的订阅topic除了本身的字符串长度,还包含表示topic字符串长度的2字节 + Aliyun_mqtt.Remaining_len = Aliyun_mqtt.Variable_len + Aliyun_mqtt.Payload_len; //计算剩余长度=可变报头长度+负载长度 + memset(Aliyun_mqtt.Pack_buff,0,sizeof(Aliyun_mqtt.Pack_buff)); + + Aliyun_mqtt.Pack_buff[0]=0xA0; //UNSUBSCRIBE报文,固定报头第1个字节0xA0 + do{ + if((Aliyun_mqtt.Remaining_len/128) == 0) + { + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len] = Aliyun_mqtt.Remaining_len; + } + else + { + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len] = (Aliyun_mqtt.Remaining_len%128)|0x80; + } + Aliyun_mqtt.Fixed_len++; + Aliyun_mqtt.Remaining_len = Aliyun_mqtt.Remaining_len/128; + }while(Aliyun_mqtt.Remaining_len); + + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+0] = Aliyun_mqtt.MessageID/256; //报文标识符高字节 + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+1] = Aliyun_mqtt.MessageID%256; //报文标识符低字节 + Aliyun_mqtt.MessageID++; //每用一次MessageID加1 + + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+2] = strlen(topic_name)/256; //主题长度高字节 + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+3] = strlen(topic_name)%256; //主题长度低字节 + memcpy(&Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+4],topic_name,strlen(topic_name)); //复制主题字串 + + while(TryUnSub_time > 0) + { + memset(mqtt_rxbuf,0,sizeof(mqtt_rxbuf)); + AdapterDeviceSend(adapter,Aliyun_mqtt.Pack_buff,Aliyun_mqtt.Fixed_len + Aliyun_mqtt.Variable_len + Aliyun_mqtt.Payload_len); + PrivTaskDelay(50); + AdapterDeviceRecv(adapter, mqtt_rxbuf, 4); + if(mqtt_rxbuf[0] == parket_unsubAck[0] && mqtt_rxbuf[1] == parket_unsubAck[1]) //取消订阅成功 + { + return 0; + } + TryUnSub_time--; + } + return 1; +} + + +/******************************************************************************* +* 函 数 名: MQTT_PublishQs0 +* 功能描述: MQTT发布QS0的主题 +* 形 参: topic_name:主题名称 + data:数据buffer + data_len:数据长度 +* 返 回 值: 发布Qs=0的消息服务器不返回确认消息 +*******************************************************************************/ +void MQTT_PublishQs0(uint8_t *topic_name,uint8_t *data, uint16_t data_len) +{ + Aliyun_mqtt.Fixed_len = 1; //PUBLISH等级0报文固定报头长度暂定为1 + Aliyun_mqtt.Variable_len = 2 + strlen(topic_name); //PUBLISH等级0报文,可变报头长度=2字节(topic长度)标识字节+topic字符串的长度 + Aliyun_mqtt.Payload_len = data_len; //PUBLISH等级0报文,负载数据长度=data_len + Aliyun_mqtt.Remaining_len = Aliyun_mqtt.Variable_len + Aliyun_mqtt.Payload_len; //计算剩余长度=可变报头长度+负载长度 + memset(Aliyun_mqtt.Pack_buff,0,sizeof(Aliyun_mqtt.Pack_buff)); + + Aliyun_mqtt.Pack_buff[0]=0x30; //PUBLISH等级0报文固定报头第1个字节0x30 + do{ + if((Aliyun_mqtt.Remaining_len/128) == 0) + { + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len] = Aliyun_mqtt.Remaining_len; + } + else + { + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len] = (Aliyun_mqtt.Remaining_len%128)|0x80; + } + Aliyun_mqtt.Fixed_len++; + Aliyun_mqtt.Remaining_len = Aliyun_mqtt.Remaining_len/128; + }while(Aliyun_mqtt.Remaining_len); + + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+0]=strlen(topic_name)/256; //主题长度高字节 + Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+1]=strlen(topic_name)%256; //主题长度低字节 + memcpy(&Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+2],topic_name,strlen(topic_name)); //复制主题字串 + memcpy(&Aliyun_mqtt.Pack_buff[Aliyun_mqtt.Fixed_len+2+strlen(topic_name)],data,data_len); //复制data数据 + + AdapterDeviceSend(adapter,Aliyun_mqtt.Pack_buff, Aliyun_mqtt.Fixed_len + Aliyun_mqtt.Variable_len + Aliyun_mqtt.Payload_len);//整个报文数据直到发送成功 +} + + +/******************************************************************************* +* 函 数 名: MQTT_SendHeart +* 功能描述: 发送心跳保活包 +* 形 参: 无 +* 返 回 值: 0表示发送成功,其他值表示发送失败 +*******************************************************************************/ +int MQTT_SendHeart(void) +{ + uint8_t TrySentHeart_time = 10; //尝试发送心跳保活次数 + while(TrySentHeart_time > 0) + { + memset(mqtt_rxbuf,0,sizeof(mqtt_rxbuf)); + AdapterDeviceSend(adapter,parket_heart,sizeof(parket_heart)); + PrivTaskDelay(50); + AdapterDeviceRecv(adapter, mqtt_rxbuf, 2); + if(mqtt_rxbuf[0] == 0xD0 && mqtt_rxbuf[1] == 0x00) + { + return 0; + } + TrySentHeart_time--; + } + return 1; +} + + +/******************************************************************************* +* 函 数 名: MQTT_DealPublishData +* 功能描述: 处理服务器发来的等级0的推送数据,附带topic信息 +* 形 参: redata:接收的数据,data_len:要处理的数据长度 +* 返 回 值: 无 +*******************************************************************************/ +void MQTT_DealPublishData(uint8_t *data, uint16_t data_len) +{ + uint8_t i; + uint16_t cmdpos,cmdlen; + + for(i = 1;i < 5;i++) + { + if((data[i] & 0x80) == 0) + break; + } + + cmdpos = 1+i+2; + cmdlen = data_len-(1+i+2); + + if(data_len <= CMD_SIZE) + { + memset(Aliyun_mqtt.cmdbuff, 0, CMD_SIZE); + memcpy(Aliyun_mqtt.cmdbuff, &data[cmdpos], cmdlen); + } +} + + +void testmqtt(void) +{ + int ret = 0; + int len; + ret = AdapterNetActive(); + if(ret == 0) + { + KPrintf("The network connection is successful.\n"); + } + ret = MQTT_Connect(); + if(ret == 0) + { + KPrintf("Log in to aliyun mqtt successfully.\n"); + } + PrivTaskDelay(2000); + ret = MQTT_SubscribeTopic(TOPIC); + if(ret == 0) + { + KPrintf("mqtt sub successfully.\n"); + } + while(1) + { + memset(mqtt_rxbuf,0,sizeof(mqtt_rxbuf)); + len = AdapterDeviceRecv(adapter, mqtt_rxbuf, 256); + if(len > 0 && (mqtt_rxbuf[0] == 0x30)) + { + MQTT_DealPublishData(mqtt_rxbuf, len); + KPrintf("%s",Aliyun_mqtt.cmdbuff); + KPrintf("\r\n"); + } + PrivTaskDelay(200); + MQTT_SendHeart(); + } +} + +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0),mqtt, testmqtt, test mqtt); \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/tool/mqtt/aliyun_mqtt.h b/Ubiquitous/XiZi_IIoT/tool/mqtt/aliyun_mqtt.h new file mode 100644 index 000000000..b04fb57a5 --- /dev/null +++ b/Ubiquitous/XiZi_IIoT/tool/mqtt/aliyun_mqtt.h @@ -0,0 +1,55 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** +* @file: aliyun_mqtt.h +* @brief: connect aliyun mqtt +* @version: 1.0 +* @author: AIIT XUOS Lab +* @date: 2023/6/14 +* +*/ + +#ifndef _ALIYUN_MQTT_H_ +#define _ALIYUN_MQTT_H_ + +#include + +#define CLIENTID "iv74JbFgzhv.D001|securemode=2,signmethod=hmacsha256,timestamp=1686617772433|" +#define USERNAME "D001&iv74JbFgzhv" +#define PASSWORD "2e5e585ec5fc8665dd8bc1a17444fc8ffcb07ed515b220785883d478e49666e5" +#define SERVERIP "101.133.196.127" +#define SERVERPORT "1883" +#define TOPIC "/sys/iv74JbFgzhv/D001/thing/service/property/set" + +#define PACK_SIZE 512 //存放报文数据缓冲区大小 +#define CMD_SIZE 512 //保存推送的PUBLISH报文中的数据缓冲区大小 + +typedef struct{ + uint8_t Pack_buff[PACK_SIZE]; //存放发送报文数据缓冲区 + uint16_t MessageID; //记录报文标识符 + uint16_t Fixed_len; //固定报头长度 + uint16_t Variable_len; //可变报头长度 + uint16_t Payload_len; //有效负荷长度 + uint16_t Remaining_len; //保存报文剩余长度字节 + uint8_t cmdbuff[CMD_SIZE]; //保存推送的PUBLISH报文中的数据缓冲区 +}MQTT_TCB; + +int AdapterNetActive(void); +int MQTT_Connect(void); +void MQTT_Disconnect(void); +int MQTT_SubscribeTopic(uint8_t *topic_name); +int MQTT_UnSubscribeTopic(uint8_t *topic_name); +void MQTT_PublishQs0(uint8_t *topic_name,uint8_t *data, uint16_t data_len); +int MQTT_SendHeart(void); +void MQTT_DealPublishData(uint8_t *data, uint16_t data_len); +#endif \ No newline at end of file