소스 검색

update source && READMME

flyingcys 8 년 전
부모
커밋
c8d8bf2002

+ 16 - 1
README.md

@@ -1,2 +1,17 @@
 # GAgent
-GAgent of Gizwits in RT-Thread
+# GAgent of Gizwits in RT-Thread  
+### 1.概述:  
+GAgent是机智云物联网整体解决方案中可入网设备(如WiFi模组、GPRS模组)接入部分,是一套运行在可入网模组(如WiFi模组、GPRS模组)上,使用机智云协议接入机智云平台,并与手机APP通信的程序。  
+开发者通过使用GAgent of Gizwits in RT-Thread该Packages,配合RT-Thread的Env工具,可快速将设备接入移植机智云平台。该Packages与硬件设备无关,开发者只需完成RT-Thread移植,将设备连接互联网后即可使用。    
+应用GAgent of Gizwits in RT-Thread后,开发者可更多关注设备本身的功能开发,而无需关心设备与机智云平台通讯交互过程。
+
+### 2.相关链接:
+1. 机智云开发者中心:  
+[https://dev.gizwits.com/zh-cn/developer/](https://dev.gizwits.com/zh-cn/developer/)  
+2. Env工具获取:`RT-Thread官网`->`资源`->`下载`  
+[https://www.rt-thread.org/page/download.html](https://www.rt-thread.org/page/download.html)  
+3. Env工具使用手册:`RT-Thread官网`->`文档`->`用户手册`->`RT-Thread工具手册`  
+[https://www.rt-thread.org/document/site/zh/5chapters/01-chapter_env_manual/#env](https://www.rt-thread.org/document/site/zh/5chapters/01-chapter_env_manual/#env)  
+
+### 3.GAgent使用:
+GAgent使用示例请参考example目录下gagent_cloud_demo.c文件,详情可阅读[《GAgent of Gizwits in RT-Thread使用手册》](./docs/README.md)。

+ 1 - 1
SConscript

@@ -8,6 +8,6 @@ if GetDepend(['PKG_USING_GAGENT_CLOUD_EXAMPLE']):
 
 CPPPATH = [cwd]
 
-group = DefineGroup('gagent_cloud', src, depend = ['PKG_USING_GAGENT_CLOUD', 'RT_USING_LWIP','PKG_USING_WEBCLIENT', 'PKG_USING_PAHOMQTT'], CPPPATH = CPPPATH)
+group = DefineGroup('gagent_cloud', src, depend = ['PKG_USING_GAGENT_CLOUD', 'PKG_USING_WEBCLIENT', 'PKG_USING_PAHOMQTT', 'PKG_USING_TINYCRYPT', 'RT_USING_LWIP'], CPPPATH = CPPPATH)
 
 Return('group')

+ 206 - 0
docs/README.md

@@ -0,0 +1,206 @@
+## GAgent of Gizwits in RT-Thread使用手册
+### 1. GAgent Package获取:
+GAgent of Gizwits in RT-Thread是运行在RT-Thread上的机智云接入package,代码已开源在[https://github.com/RT-Thread-packages/GAgent](https://github.com/RT-Thread-packages/GAgent) 上。
+#### 1.1 RT-Thread env工具:
+* 强烈推荐使用RT-Thread env工具下载配置Package。env请在`RT-Thread官网`->`资源`->`下载`处获取。  
+下载地址:[https://www.rt-thread.org/page/download.html](https://www.rt-thread.org/page/download.html)   
+
+![RT-Thread env](images/env.png)  
+
+* env使用手册请参照`RT-Thread官网`->`文档`->`用户手册`->`RT-Thread工具手册`  
+手册地址:[https://www.rt-thread.org/document/site/zh/5chapters/01-chapter_env_manual/#env](https://www.rt-thread.org/document/site/zh/5chapters/01-chapter_env_manual/#env)  
+
+![RT-Thread package](images/manuals.png)  
+
+#### 1.2 Package下载:
+* 运行env工具,进入RT-Thread已支持menuconfig配置的bsp目录,如stm32f429-apollo(也可自行配置Kconfig,方法如上文档),运行`pkgs --upgrade`更新Packages list,成功后运行menuconfig命令。  
+
+![menuconfig](images/menuconfig.png)  
+
+* 进入`RT-Thread online packages`菜单,选择`IoT -internet of things`菜单,进入后选中`GAgent: GAgent of Gizwits in RT-Thread`。  
+* GAgent有`example`和`debug`选项可配置:  
+> 选中`example`,package内含GAgent运行demo,可通过finsh或msh启动。  
+> 选中`debug`,将打印GAgent运行期间日志。  
+
+![Gagent](images/Gagent.png)  
+
+* GAgent 不断优化升级中,推荐使用`latest`版本。
+
+![version](images/version.png)  
+
+* GAgent用到的`IoT package`包括`WebClient`、`Paho MQTT`。这2个Packages已被默认选中。GAgent使用了2个`subscribe topic`,所以需要配置`Paho MQTT`中的`Max pahomqtt subscribe topic handlers`为2。  
+
+![pahomqtt](images/pahomqtt.png)  
+
+* GAgent同时还用到了`security package`中的`TinyCrypt`中的AES加解密,已默认选中。其他加密算法暂时没有用到,可手工关闭。  
+
+![TinyCrypt](images/TinyCrypt.png)  
+
+* 在env工具下输入`pkgs --update`下载Package,等待下载完成后,当前stm32f429-apollo目录下的packages下已经有GAgent及其他相关packages。  
+
+![packages](images/packages.png)
+
+### 2. 运行前准备:
+#### 2.1 开发者账号注册:
+前往机智云开发者中心[https://dev.gizwits.com/zh-cn/developer/](https://dev.gizwits.com/zh-cn/developer/) 注册开发者账号  
+
+![deverloper](images/deverloper.png)  
+
+#### 2.2 产品创建与定义:  
+2.2.1 产品创建:  
+在开发者中心创建新产品页面创建新产品[https://dev.gizwits.com/zh-cn/developer/](https://dev.gizwits.com/zh-cn/developer/product/create)  
+
+![product](images/product.png)  
+
+2.2.2 产品功能定义:  
+2.2.2.1 在`开发向导`中定义数据点:  
+
+![datapoint](images/datapoint.png)  
+
+2.2.2.2 创建并修改数据点:  
+> 数据点创建/修改请参照机智云相关文档  
+
+2.2.3 通讯协议下载:  
+在开发向导页面下载当前定义产品的通讯协议文档,后续开发会用到。  
+
+![protocol](images/protocol.png)  
+2.2.4. 获取产品基本信息:  
+`Product_key`及`Product_Secret`是2个创建产品的唯一标示,需要在GAgent初始化时输入。   
+
+#### 2.3 `机智云`APP下载:  
+在`下载中心`->`开发与调试工具`下载`机智云Wi-Fi/移动产品调试APP`  
+
+![app](images/app.png)  
+
+
+### 3. 运行GAgent:  
+#### 3.1 GAgent example:  
+* 如在menuconfig中选中`Enable GAgent Example`,在编译过程中会加入`GAgent Package`包中`example`目录下的`gagent_cloud_demo.c`。该文件完成GAgent初始化,可通过finsh/msh启动运行 
+``` C
+int gagent_cloud(void)
+{
+    int rc = RT_EOK;
+
+    rt_memset(&gagent_param, 0, sizeof(gagent_param));
+    //
+    strcpy(gagent_param.product_key, DEMO_PRODUCT_KEY);
+    strcpy(gagent_param.product_secret, DEMO_PRODUCT_KEY_SECRET);
+    strcpy(gagent_param.mac, DEMO_MAC);
+    gagent_param.read_param_callback = gagent_read_param;
+    gagent_param.write_param_callback = gagent_write_param;
+    gagent_param.recv_packet_callback = gagent_recv_packet;
+    //
+    gagent_cloud_start(&gagent_param);
+    
+    return rc;
+}
+#ifdef RT_USING_FINSH
+MSH_CMD_EXPORT(gagent_cloud, gagent cloud demo);
+
+FINSH_FUNCTION_EXPORT(gagent_cloud, "gagent cloud test");
+#endif
+```  
+#### 3.2 GAgent流程
+3.2.1 将新产品注册时候`product_key`和`product_secret`填入`gagent_param.product_key`和`gagent_param.product_secret`。  
+3.2.2. `gagent_param.mac`填入当前产品的mac地址,每个产品下的mac地址不能重复。  
+3.2.3. 分别设置`read_param_callback`、`write_param_callback`回调函数,分别对应`读参数`、`写参数`;这2个回调函数必须实现正确的参数读取与写入。  
+``` C
+int gagent_read_param(struct gagent_config *param, rt_uint32_t len)
+{
+    /* read param */
+#ifdef RT_USING_DFS
+    int fd;
+
+    fd = open("/sdcard/demo", O_RDONLY, 0);
+    if(fd >= 0)
+    {
+        read(fd, param, len);
+        close(fd);
+    }
+    else
+        return -RT_EOK;
+#endif
+    return RT_EOK;
+}
+
+int gagent_write_param(struct gagent_config *param, rt_uint32_t len)
+{
+    /* write param */
+
+    rt_kprintf("mac:%s", param->mac);
+    rt_kprintf("did:%s", param->did);
+    rt_kprintf("passcode:%s", param->passcode);
+    rt_kprintf("pk:%s", param->pk);
+    rt_kprintf("pk_secret:%s", param->pk_secret);
+    rt_kprintf("hard_version:%s", param->hard_version);
+    rt_kprintf("soft_version:%s", param->soft_version);
+    
+#ifdef RT_USING_DFS
+    int fd;
+
+    fd = open("/sdcard/demo", O_WRONLY | O_CREAT |O_TRUNC | O_BINARY, 0);
+    if(fd >= 0)
+    {
+
+        write(fd, param, len);
+        close(fd);
+    }
+#endif
+    return RT_EOK;
+```
+
+3.2.4. 设置`recv_packet_callback`回调函数,该回调为APP下发数据包处理函数。  
+1. 数据包协议请参照`2.2.4通讯协议文档`。  
+2. 设备收到APP下发`ACTION_CONTROL`命令完成处理后,需使用`ACTION_REPORT_STATUS`同步状态。
+``` C
+int gagent_recv_packet(rt_uint8_t from, rt_uint8_t action, rt_uint8_t *kv, rt_uint16_t kv_len)
+{
+    /* please read product protocol */
+    uint8_t power;
+    
+    switch(action)
+    {
+        case ACTION_CONTROL: 
+            rt_kprintf("ACTION_CONTROL\r\n");
+            power = *(kv + 1);
+            rt_kprintf("power:%d\n", power);
+            gagent_cloud_send_packet(ACTION_REPORT_STATUS, &power, 1);
+        break;
+
+        case ACTION_READ_STATUS:
+            rt_kprintf("ACTION_READ_STATUS\r\n");
+//            gagent_cloud_send_packet(ACTION_READ_STATUS_ACK, buf, buf_len);
+        break;
+
+        case ACTION_TRANS_RECV:
+            rt_kprintf("this is your raw data from app\r\n");
+        break;
+        
+        case ACTION_PUSH_OTA:
+            rt_kprintf("ACTION_PUSH_OTA\r\n");
+        break;
+    }
+    
+    return RT_EOK;
+}
+```  
+3.2.5. 调用`gagent_cloud_start`启动GAgent。  
+
+#### 3.3 设备主动上报:  
+设备主动上报使用`gagent_cloud_send_packet`函数。  
+`int gagent_cloud_send_packet(rt_uint8_t action, rt_uint8_t *buf, rt_uint16_t buf_len);`
+
+#### 3.5 设备发现与绑定:
+设备运行正常后,需要与APP正常绑定后才能通过APP控制设备,初始状态APP绑定设备需确保APP与设备处于同一在局域网,否则APP将无法发现设备,发现设备后点击即可绑定。  
+
+![discove](images/discove.png)  
+
+设备控制  
+
+![control](images/control.png)  
+
+#### 3.6 注意事项:
+3.4.1 请关注设备内存大小,内存过小会导致GAgent运行失败。  
+3.4.2 运行GAgent前请确保设备已连接互联网,并且dns设置正确。  
+3.4.3 请正确输入`product_key`和`product_secret`,否则将导致GAgent连接MQTT服务器失败。  
+3.4.4 请正确完成GAgent参数保存与读取,否则GAgent将每次都重新注册设备;重新注册设备后,APP上将找不到该设备,需重新绑定。  

BIN
docs/images/Gagent.png


BIN
docs/images/TinyCrypt.png


BIN
docs/images/app.png


BIN
docs/images/control.png


BIN
docs/images/datapoint.png


BIN
docs/images/deverloper.png


BIN
docs/images/discove.png


BIN
docs/images/env.png


BIN
docs/images/example.png


BIN
docs/images/info.png


BIN
docs/images/manuals.png


BIN
docs/images/menuconfig.png


BIN
docs/images/packages.png


BIN
docs/images/pahomqtt.png


BIN
docs/images/product.png


BIN
docs/images/protocol.png


BIN
docs/images/version.png


+ 37 - 3
example/gagent_cloud_demo.c

@@ -29,23 +29,57 @@
 #include <finsh.h>
 #endif
 
+#ifdef RT_USING_DFS
+#include <dfs_posix.h>
+#endif
+
 #define     DEMO_PRODUCT_KEY                    "1371df627fa64e849f32fe17ebd5fd38"
 #define     DEMO_PRODUCT_KEY_SECRET             "067a83b650544d979bb3d4d147f32034"   
 #define     DEMO_MAC                            "\xBC\x12\x34\x56\x78\x23"
 
 static gagent_cloud_param gagent_param;
 
-int gagent_read_param(void *param, rt_uint32_t len)
+int gagent_read_param(struct gagent_config *param, rt_uint32_t len)
 {
     /* read param */
-    
+#ifdef RT_USING_DFS
+    int fd;
+
+    fd = open("/sdcard/demo", O_RDONLY, 0);
+    if(fd >= 0)
+    {
+        read(fd, param, len);
+        close(fd);
+    }
+    else
+        return -RT_EOK;
+#endif
     return RT_EOK;
 }
 
-int gagent_write_param(void *param, rt_uint32_t len)
+int gagent_write_param(struct gagent_config *param, rt_uint32_t len)
 {
     /* write param */
+
+    rt_kprintf("mac:%s", param->mac);
+    rt_kprintf("did:%s", param->did);
+    rt_kprintf("passcode:%s", param->passcode);
+    rt_kprintf("pk:%s", param->pk);
+    rt_kprintf("pk_secret:%s", param->pk_secret);
+    rt_kprintf("hard_version:%s", param->hard_version);
+    rt_kprintf("soft_version:%s", param->soft_version);
     
+#ifdef RT_USING_DFS
+    int fd;
+
+    fd = open("/sdcard/demo", O_WRONLY | O_CREAT |O_TRUNC | O_BINARY, 0);
+    if(fd >= 0)
+    {
+
+        write(fd, param, len);
+        close(fd);
+    }
+#endif
     return RT_EOK;
 }
 

+ 54 - 46
gagent_cloud.c

@@ -1,26 +1,26 @@
 /*
- * File      : gagent_cloud.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2018, RT-Thread Development Team
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License along
- *  with this program; if not, write to the Free Software Foundation, Inc.,
- *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Change Logs:
- * Date           Author       Notes
- * 2018-01-03     flyingcys    first version
- */
+* File      : gagent_cloud.c
+* This file is part of RT-Thread RTOS
+* COPYRIGHT (C) 2018, RT-Thread Development Team
+*
+*  This program is free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  This program is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  You should have received a copy of the GNU General Public License along
+*  with this program; if not, write to the Free Software Foundation, Inc.,
+*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*
+* Change Logs:
+* Date           Author       Notes
+* 2018-01-03     flyingcys    first version
+*/
 #include <rtthread.h>
 
 #include "gagent_def.h"
@@ -32,11 +32,11 @@
 #endif
 
 
-static con_st con;        //config info
+static con_st con;                          //config info
 static gagent_cloud_param con_param = {0};
 //
-cloud_st *cloud = RT_NULL;     //cloud handle
-lan_st *lan = RT_NULL;       //lan handle
+cloud_st *cloud = RT_NULL;                  //cloud handle
+lan_st *lan = RT_NULL;                      //lan handle
 
 
 
@@ -57,7 +57,7 @@ int gagent_cloud_send_packet(rt_uint8_t action, rt_uint8_t *buf, rt_uint16_t buf
     {
         gagent_err("gagent_lan_send_packet failed:%d\n", rc);
     }
-    
+
     return rc;
 }
 
@@ -65,10 +65,10 @@ int gagent_cloud_recv_packet(rt_uint8_t from, rt_uint8_t action, rt_uint8_t *kv,
 {
     int rc = RT_EOK;
     gagent_dbg("from:%s\n", (from == CMD_FROM_LAN) ? "lan packet" : "mqtt packet");
-    
+
     if(con_param.recv_packet_callback != RT_NULL)
         rc = con_param.recv_packet_callback(from, action, kv, kv_len);
-        
+
     return rc;
 }
 
@@ -80,9 +80,11 @@ static int gagent_cloud_parse_config(con_st *con)
 
     //read file
     memset(con, 0, sizeof(con_st));
-            
+
     if(con_param.read_param_callback(con, sizeof(con_st)) != RT_EOK)
+    {
         gagent_err("read param failed!\n");
+    }
     
     if(con->mac[0] == 0 || memcmp(con->mac, con_param.mac, sizeof(con->mac)) != 0)
     {
@@ -90,6 +92,7 @@ static int gagent_cloud_parse_config(con_st *con)
         //
         rt_memset(con->mac, 0, sizeof(con->mac));
         rt_memcpy(con->mac, con_param.mac, sizeof(con->mac) - 1);
+        //
         write_flag = RT_TRUE;
         gagent_dbg("con->mac changed!\n");
     }
@@ -100,32 +103,36 @@ static int gagent_cloud_parse_config(con_st *con)
         //
         rt_memset(con->pk, 0, sizeof(con->pk));
         rt_strncpy(con->pk, con_param.product_key, sizeof(con->pk) - 1);
+        //
         write_flag = RT_TRUE;
         gagent_dbg("con->pk changed!\n");
     }
-    
+
     if(con->pk_secret[0] == 0 || strcmp(con->pk_secret, con_param.product_secret) != 0)
     {
         rt_memset(con->did, 0, sizeof(con->did));
         //
         rt_memset(con->pk_secret, 0, sizeof(con->pk_secret));
         rt_strncpy(con->pk_secret, con_param.product_secret, sizeof(con->pk_secret) - 1);
+        //
         write_flag = RT_TRUE;
         gagent_dbg("product secret changed!\n");
     }
-    
+
     if(con->hard_version[0] == 0 || strcmp(con->hard_version, HARD_VERSION) != 0)
     {
         rt_memset(con->hard_version, 0, sizeof(con->hard_version));
         rt_strncpy(con->hard_version, HARD_VERSION, sizeof(con->hard_version) - 1);
+        //
         write_flag = RT_TRUE;
         gagent_dbg("hard_version changed!\n");
     }
-    
+
     if(con->soft_version[0] == 0 || strcmp(con->soft_version, SOFT_VERSION) != 0)
     {
         rt_memset(con->soft_version, 0, sizeof(con->soft_version));
         rt_strncpy(con->soft_version, SOFT_VERSION, sizeof(con->soft_version) - 1);
+        //
         write_flag = RT_TRUE;
         gagent_dbg("soft_verson changed!\n");
     }
@@ -135,13 +142,14 @@ static int gagent_cloud_parse_config(con_st *con)
         //passcode is empty
         rt_memset(con->passcode, 0, sizeof(con->passcode));
         rt_memcpy(con->passcode, con->pk, 10);
+        //
         write_flag = RT_TRUE;
         gagent_dbg("passcode empty!\n");
     }
 
 #ifdef PKG_GAGENT_CLOUD_DEBUG
-	{
-		rt_uint8_t i;
+    {
+        rt_uint8_t i;
         rt_kprintf("mac: ");
         for(i = 0; i < MAX_MAC_LEN; i ++)
         {
@@ -154,15 +162,15 @@ static int gagent_cloud_parse_config(con_st *con)
         rt_kprintf("pk_secret:%s\n", con->pk_secret);
         rt_kprintf("hard_version:%s\n", con->hard_version);
         rt_kprintf("soft_version:%s\n", con->soft_version);
-	}
+    }
 #endif
-	
+
     if(write_flag)
     {
         gagent_dbg("write param!\n");
-         con_param.write_param_callback(con, sizeof(con_st));
+        con_param.write_param_callback(con, sizeof(con_st));
     }
-    
+
     return RT_EOK;
 }
 
@@ -187,7 +195,6 @@ static int gagent_cloud_init(cloud_st *cloud)
     rc = gagent_cloud_provision(cloud);
 
     return RT_EOK;
-
 }
 
 void gagent_cloud_thread(void *parameter)
@@ -214,7 +221,7 @@ void gagent_cloud_thread(void *parameter)
         gagent_err("gagent_cloud_lan_init failed!\n");
         goto __exit;
     }
-		
+
     rc = gagent_mqtt_init(cloud);
     if(rc != RT_EOK)
     {
@@ -232,7 +239,7 @@ int gagent_cloud_start(gagent_cloud_param *param)
 {
     int rc = RT_EOK;
     rt_thread_t thread = RT_NULL;
-    
+
     RT_ASSERT(param != RT_NULL);
     RT_ASSERT(param->product_key[0] != RT_NULL);
     RT_ASSERT(param->product_secret[0] != RT_NULL);
@@ -269,6 +276,7 @@ int gagent_cloud_start(gagent_cloud_param *param)
                                 4096, 
                                 RT_THREAD_PRIORITY_MAX / 3, 
                                 20);
+    
     if(RT_NULL != thread)
     {
         rt_thread_startup(thread);
@@ -278,15 +286,15 @@ int gagent_cloud_start(gagent_cloud_param *param)
         rc = -RT_ERROR;
         goto __exit;
     }
-    
+
     return rc;
-    
+
 __exit:
-    if(cloud != RT_NULL)
+    if(RT_NULL != cloud)
         rt_free(cloud);
 
-    if(lan != RT_NULL)
+    if(RT_NULL != lan)
         rt_free(lan);
-        
+
     return rc;    
 }

+ 14 - 2
gagent_cloud.h

@@ -32,13 +32,25 @@ extern "C" {
 #define     PRODUCT_SECERT_LEN          32
 #define     MAX_MAC_LEN                 32
 
+struct gagent_config
+{
+    char mac[32 + 1];
+    char did[32 + 1];
+    char passcode[16 + 1];
+    char pk[48 + 1];
+    char pk_secret[48 + 1];
+    char hard_version[16 + 1];
+    char soft_version[16 + 1];
+}; 
+
+
 typedef struct
 {
     char product_key[PRODUCT_KEY_LEN + 1];
     char product_secret[PRODUCT_SECERT_LEN + 1];
     char mac[MAX_MAC_LEN + 1];
-    int (*read_param_callback)(void *param, rt_uint32_t len);
-    int (*write_param_callback)(void *param, rt_uint32_t len);
+    int (*read_param_callback)(struct gagent_config *param, rt_uint32_t len);
+    int (*write_param_callback)(struct gagent_config *param, rt_uint32_t len);
     int (*recv_packet_callback)(rt_uint8_t from, rt_uint8_t action, rt_uint8_t *kv, rt_uint16_t kv_len);
 } gagent_cloud_param;
 

+ 47 - 49
gagent_def.h

@@ -21,6 +21,14 @@
  * Date           Author       Notes
  * 2018-01-03     flyingcys    first version
  */
+#ifndef __GAGENT_DEF_H__
+#define __GAGENT_DEF_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
 #include <rtthread.h>
 #include <string.h>
 
@@ -36,27 +44,26 @@
 #define     gagent_err(...)
 #endif
 
-#define MAX_CLIENT			8
+#define MAX_CLIENT              8
 
 
-#define BUF_LEN 				2048 
+#define BUF_LEN                 2048 
 
 #define HEAD_LEN                5       
 #define UDP_RECV_ERROR          199
 #define TCP_RECV_ERROR_BASE     100
-#define LOG_IP_BUF_LENGTH		16
-
-#define HARD_VERSION			"01RTT001"
-#define SOFT_VERSION			"04020020"
+#define LOG_IP_BUF_LENGTH       16
 
+#define HARD_VERSION            "01RTT001"
+#define SOFT_VERSION            "04020020"
 
-#define G_SERVICE_DOMAIN 	"api.gizwits.com"
-#define G_SERVICE_PORT		"80"
-#define G_M2M_DOMAIN		"sandbox.gizwits.com"
-#define G_M2M_PORT			"1883"
-#define G_M2M_SSL_PORT		"8883"
+#define G_SERVICE_DOMAIN        "api.gizwits.com"
+#define G_SERVICE_PORT          "80"
+#define G_M2M_DOMAIN            "sandbox.gizwits.com"
+#define G_M2M_PORT              "1883"
+#define G_M2M_SSL_PORT          "8883"
 
-#define DID_LENGTH			22
+#define DID_LENGTH              22
 
 
 enum GAGENT_HARD_TYPE
@@ -65,47 +72,33 @@ enum GAGENT_HARD_TYPE
     GAGENT_HARD_MCU = 2,
 };
 
-
-typedef struct _con_st	con_st;
-
-struct _con_st
-{
-    char	mac[32 + 1];
-    char	did[32 + 1];
-    char	passcode[16 + 1];
-    char	pk[48 + 1];
-    char	pk_secret[48 + 1];
-    char	hard_version[16 + 1];
-    char	soft_version[16 + 1];
-}; 
-
-
-typedef struct _cloud_st cloud_st;
+typedef struct gagent_config con_st;
 
 struct _cloud_st
 {
-	con_st		*con;
+    con_st *con;
+    //
+    char mqtt_server[128];
+    int mqtt_port;
+    char sub_topic[2][128];
     //
-	char mqtt_server[128];
-	int mqtt_port;
-	char sub_topic[2][128];
-	//
-	char recv_buf[BUF_LEN];
-	int recv_len;
-	char send_buf[BUF_LEN];
-	int send_len;
+    char recv_buf[BUF_LEN];
+    int recv_len;
+    char send_buf[BUF_LEN];
+    int send_len;
     //
-	char ota_info[128];
-	int sn;
+    char ota_info[128];
+    int sn;
 };
+typedef struct _cloud_st cloud_st;
 
 typedef struct _lan_st lan_st;
 
 struct _lan_st
 {
-	con_st	*con;
-	//
-	int	client_fd[MAX_CLIENT];
+    con_st *con;
+    //
+    int	client_fd[MAX_CLIENT];
     int tcp_server;
     int udp_server;
     struct sockaddr_in tcp_server_addr;
@@ -116,12 +109,12 @@ struct _lan_st
     int local_sock;
     int local_port;
     //
-	char recv_buf[BUF_LEN];
-	int	recv_len;
-	char send_buf[BUF_LEN];
-	int	send_len;
-	//
-	int	sn;
+    char recv_buf[BUF_LEN];
+    int	recv_len;
+    char send_buf[BUF_LEN];
+    int	send_len;
+    //
+    int	sn;
 };
 
 
@@ -165,4 +158,9 @@ int gagent_mqtt_send_packet(cloud_st *cloud, rt_uint8_t action, rt_uint8_t *kv,
 
 int gagent_mqtt_init(cloud_st *cloud);
 
-//#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

+ 22 - 21
gagent_httpc.c

@@ -22,6 +22,7 @@
  * 2018-01-03     flyingcys    first version
  */
 #include "gagent_def.h"
+#include "gagent_cloud.h"
 #include "aes.h"
 
 #define     MAX_HTTPC_URL_LEN           1024
@@ -94,7 +95,7 @@ static int webclient_common(int method, const char *URI,
     {
         gagent_dbg("chunk_sz:%d\n", session->chunk_sz);
         total = session->chunk_sz;
-   }
+    }
     else if(session->content_length > 0)
     {
         gagent_dbg("content_length:%d content_length_remainder:%d\n", session->content_length, session->content_length_remainder);
@@ -327,7 +328,7 @@ int gagent_cloud_provision(cloud_st *cloud)
 {
     int rc = RT_EOK;
     int content_len, aes_len, i;
-    
+
     char *url = RT_NULL;
     char *content = RT_NULL;
     char *ptr = RT_NULL;
@@ -336,14 +337,14 @@ int gagent_cloud_provision(cloud_st *cloud)
     char aes_key[16];
     char *aes_buf = RT_NULL;
     aes_context *aes_ctx = RT_NULL;
-    
+
     content = (char *)rt_malloc(256);
     if(RT_NULL == content)
     {
         rc = -RT_ENOMEM;
         goto __exit;
     }
-    
+
     aes_buf = (char *)rt_malloc(256);
     if(RT_NULL == aes_buf)
     {
@@ -402,13 +403,13 @@ int gagent_cloud_provision(cloud_st *cloud)
 
     ptr = url;
     rt_memset(url, 0, MAX_HTTPC_URL_LEN);
-    
+
     //url
     ptr += rt_snprintf(ptr, MAX_HTTPC_URL_LEN - (ptr - url), "http://%s:%s", G_SERVICE_DOMAIN, G_SERVICE_PORT);
     ptr += rt_snprintf(ptr, MAX_HTTPC_URL_LEN - (ptr - url), "/dev/%s/device?", cloud->con->pk);
     ptr += rt_snprintf(ptr, MAX_HTTPC_URL_LEN - (ptr - url), "did=%s", content);
     gagent_dbg("url:%s\n", url);
-    
+
     //
     rt_memset(cloud->mqtt_server, 0, sizeof(cloud->mqtt_server));
     rt_memcpy(cloud->mqtt_server, G_M2M_DOMAIN, sizeof(G_M2M_DOMAIN));
@@ -423,23 +424,23 @@ int gagent_cloud_provision(cloud_st *cloud)
         rc = -RT_ENOMEM;
         goto __exit;
     }
-    
+
     rc = webclient_get((const char *)url, gagent_cloud_httpc_cb);
     if(rc != RT_EOK)
     {
         rt_kprintf("weblient_post failed!\n");
         goto __exit;
     }
-    
+
     rt_memset(content, 0, 256);
     aes_len = gagent_strtohex(content, httpc.recv_buf, httpc.recv_len);
 
     rt_memset(aes_ctx, 0, sizeof(aes_context));
     aes_setkey_dec(aes_ctx, (uint8_t *)aes_key, 128);
-    
+
     rt_memset(aes_buf, 0, 256);
     for(i = 0; i < aes_len; i += 16)
-        aes_crypt_ecb(aes_ctx, AES_DECRYPT, (uint8_t *)content + i, (uint8_t *)aes_buf + i);
+    aes_crypt_ecb(aes_ctx, AES_DECRYPT, (uint8_t *)content + i, (uint8_t *)aes_buf + i);
 
     gagent_dbg("%s\n", aes_buf);
 
@@ -466,7 +467,7 @@ int gagent_cloud_provision(cloud_st *cloud)
         if(ptr_tail > 0)
         {
             *ptr_tail = '\0';
-            
+
             cloud->mqtt_port = 0;
             cloud->mqtt_port = atoi(ptr);
         }
@@ -480,7 +481,7 @@ __exit:
         rt_free(httpc.recv_buf);
         httpc.recv_buf = RT_NULL;
     }
-    
+
     if(RT_NULL != url)
     {
         rt_free(url);
@@ -498,7 +499,7 @@ __exit:
         rt_free(aes_buf);
         aes_buf = RT_NULL;
     }
-    
+
     if(RT_NULL != content)
     {
         rt_free(content);
@@ -515,7 +516,7 @@ int gagent_cloud_check_ota(cloud_st *cloud)
     char *url = RT_NULL;
     char *content = RT_NULL;
     char *ptr = RT_NULL;
-    
+
     content = (char *)rt_malloc(256);
     if(RT_NULL == content)
     {
@@ -527,7 +528,7 @@ int gagent_cloud_check_ota(cloud_st *cloud)
     //data
     rt_memset(content, 0, 256);
     rt_snprintf((char *)content, 256, "passcode=%s&type=%d&hard_version=%s&soft_version=%s", \
-               cloud->con->passcode, GAGENT_HARD_SOC, cloud->con->hard_version, cloud->con->soft_version);
+    cloud->con->passcode, GAGENT_HARD_SOC, cloud->con->hard_version, cloud->con->soft_version);
 
     gagent_dbg("content:%s\n", content);
 
@@ -540,11 +541,11 @@ int gagent_cloud_check_ota(cloud_st *cloud)
 
     ptr = url;
     rt_memset(url, 0, MAX_HTTPC_URL_LEN);
-    
+
     //url
     ptr += rt_snprintf(ptr, MAX_HTTPC_URL_LEN - (ptr - url), "http://%s:%s", G_SERVICE_DOMAIN, G_SERVICE_PORT);
     ptr += rt_snprintf(ptr, MAX_HTTPC_URL_LEN - (ptr - url), "/dev/ota/v4.1/update_and_check/%s", cloud->con->did);
-    
+
     gagent_dbg("url:%s\n", url);
 
     httpc.recv_buf = RT_NULL;
@@ -561,16 +562,16 @@ int gagent_cloud_check_ota(cloud_st *cloud)
         gagent_err("weblient_post failed!\n");
         goto __exit;
     }
-    
+
 __exit:
     if(RT_NULL != httpc.recv_buf)
-        rt_free(httpc.recv_buf);
+    rt_free(httpc.recv_buf);
 
     if(RT_NULL != url)
-        rt_free(url);
+    rt_free(url);
 
     if(RT_NULL != content)
-        rt_free(content);
+    rt_free(content);
 
     return rc;
 }

+ 116 - 129
gagent_lan.c

@@ -1,26 +1,26 @@
 /*
- * File      : gagent_lan.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2018, RT-Thread Development Team
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License along
- *  with this program; if not, write to the Free Software Foundation, Inc.,
- *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Change Logs:
- * Date           Author       Notes
- * 2018-01-03     flyingcys    first version
- */
+* File      : gagent_lan.c
+* This file is part of RT-Thread RTOS
+* COPYRIGHT (C) 2018, RT-Thread Development Team
+*
+*  This program is free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  This program is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  You should have received a copy of the GNU General Public License along
+*  with this program; if not, write to the Free Software Foundation, Inc.,
+*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*
+* Change Logs:
+* Date           Author       Notes
+* 2018-01-03     flyingcys    first version
+*/
 #include "gagent_def.h"
 #include "gagent_cloud.h"
 
@@ -49,7 +49,7 @@ static int local_port = 7100;
 static int lan_send_device_info(lan_st *lan, rt_uint8_t send_type)
 {
     char *index, *len_index;
-    
+
     memset(lan->send_buf, 0, sizeof(lan->send_buf));
 
     index = lan->send_buf;
@@ -61,7 +61,6 @@ static int lan_send_device_info(lan_st *lan, rt_uint8_t send_type)
     *index ++ = 0x03;
 
     //len
-//    *index ++ = 0x0a;
     len_index = index;
     index ++;
 
@@ -81,7 +80,7 @@ static int lan_send_device_info(lan_st *lan, rt_uint8_t send_type)
     //did
     memcpy(index, lan->con->did, 0x16);
     index += strlen(lan->con->did);
-    
+
     //mac_len
     *index ++ = 0x00;
     *index ++ = 0x06;
@@ -107,11 +106,10 @@ static int lan_send_device_info(lan_st *lan, rt_uint8_t send_type)
 
     lan->send_len = index - lan->send_buf;
     *len_index = lan->send_len - HEAD_LEN;
-    
+
     return RT_EOK;
 }
 
-
 static int lan_udp_do_packet(lan_st *lan)
 {
     char *one_packet;
@@ -127,19 +125,18 @@ static int lan_udp_do_packet(lan_st *lan)
 
         memcpy(&cmd, one_packet + 6, 2);
         cmd = ntohs(cmd);
-        
+
         gagent_dbg("cmd:%d\n", cmd);
-        
         switch(cmd)
         {
-            case 0x03:
-                rc = lan_send_device_info(lan, UDP_SEND_TYPE_DISCOVER);
+        case 0x03:
+            rc = lan_send_device_info(lan, UDP_SEND_TYPE_DISCOVER);
             break;
         }
-    
+
         one_packet += (data_len + len_num + HEAD_LEN);
         lan->recv_len -= (data_len + len_num + HEAD_LEN);
-        
+
         if(rc != 0)
             return rc;
     }
@@ -167,7 +164,7 @@ static int lan_get_passcode(lan_st *lan)
 
     //flag
     *index ++ = 0x00;
-    
+
     //cmd
     *index ++ = 0x00;
     *index ++ = 0x07;
@@ -182,7 +179,7 @@ static int lan_get_passcode(lan_st *lan)
     //
     lan->send_len = (index - lan->send_buf);
     *len_index = lan->send_len - HEAD_LEN;
-    
+
     return RT_EOK;
 }
 
@@ -206,7 +203,7 @@ static int lan_get_device_info(lan_st *lan)
 
     //flag
     *index ++ = 0x00;
-    
+
     //cmd
     *index ++ = 0x00;
     *index ++ = 0x14;
@@ -225,27 +222,27 @@ static int lan_get_device_info(lan_st *lan)
     //mcu_soft_version
     index += 8;
 
-	//p0 version
-	index += 8;
+    //p0 version
+    index += 8;
 
-	//remain1
-	index += 8;
+    //remain1
+    index += 8;
 
     //remain2_len
-	*index ++ = 0x00;
-	*index ++ = 0x00;
+    *index ++ = 0x00;
+    *index ++ = 0x00;
 
-	//pk_len
+    //pk_len
     *index ++ = 0x00;
     *index ++ = strlen(lan->con->pk);
 
     //pk
     memcpy(index, lan->con->pk, strlen(lan->con->pk));
     index += strlen(lan->con->pk);
-	
+
     lan->send_len = (index - lan->send_buf);
     *len_index = lan->send_len - HEAD_LEN;
-    
+
     return RT_EOK;
 }
 
@@ -253,14 +250,14 @@ static int lan_login_device(lan_st *lan, char *packet)
 {
     char *index, *len_index;
     char passcode_recv[32];
-    uint16_t passcode_len;
+    rt_uint16_t passcode_len;
 
     memcpy(&passcode_len, packet + HEAD_LEN + 1 + 2, 2);
     passcode_len = ntohs(passcode_len);
 
     memset(passcode_recv, 0x00, sizeof(passcode_recv));
     memcpy(passcode_recv, packet + HEAD_LEN + 1 + 2 + 2, passcode_len);
-        
+
     index = lan->send_buf;
 
     memset(index, 0, sizeof(lan->send_buf));
@@ -297,31 +294,31 @@ static int lan_login_device(lan_st *lan, char *packet)
 
 static int lan_trans_data(lan_st *lan, char *packet)
 {
-    uint16_t len, cmd;
+    rt_uint16_t len, cmd;
     char *index, *kv;
-    uint16_t kv_len;
-    uint8_t length_len, action;
+    rt_uint16_t kv_len;
+    rt_uint8_t length_len, action;
 
     lan->send_len = 0;
     //
     action = 0;
     kv_len = 0;
     kv = 0;
-    
+
     index = packet;
-	len = gagent_parse_rem_len((const uint8_t *)index + 4);
-	length_len = gagent_num_rem_len_bytes((const uint8_t*)index + 4);
+    len = gagent_parse_rem_len((const uint8_t *)index + 4);
+    length_len = gagent_num_rem_len_bytes((const uint8_t*)index + 4);
 
     index += (HEAD_LEN + length_len);
-    
+
     rt_memcpy(&cmd, index, 2);
     index += 2;
 
     cmd = ntohs(cmd);
     gagent_dbg("cmd:%0x\n", cmd);
-    
-	// 00 00 00 03 06 00 00 90 01 01 01 
-	// 00 00 00 03 0A 00 00 93 00 00 00 00 01 01 01 
+
+    // 00 00 00 03 06 00 00 90 01 01 01 
+    // 00 00 00 03 0A 00 00 93 00 00 00 00 01 01 01 
     if(cmd == 0x90)
     {
         action = *index ++;
@@ -334,14 +331,14 @@ static int lan_trans_data(lan_st *lan, char *packet)
         memcpy(&lan->sn, index, 4);
         index += 4;
         gagent_dbg("lan_sn:%d\n", lan->sn);
-        
+
         action = *index ++;
         kv = index;
         kv_len = len - 8;
     }
     else
         return -RT_ERROR;
-    
+
     return gagent_cloud_recv_packet(CMD_FROM_LAN, action, (uint8_t *)kv, kv_len);
 }
 
@@ -395,34 +392,34 @@ static int lan_tcp_do_packet(lan_st *lan)
         gagent_dbg("lan_cmd:%x\n",cmd);
         switch(cmd)
         {
-            case 0x06:
-                rc = lan_get_passcode(lan);
+        case 0x06:
+            rc = lan_get_passcode(lan);
             break;
 
-            case 0x08:
-                rc = lan_login_device(lan, one_packet);
+        case 0x08:
+            rc = lan_login_device(lan, one_packet);
             break;
 
-            case 0x90:
-            case 0x93:
-                rc = lan_trans_data(lan, one_packet);
+        case 0x90:
+        case 0x93:
+            rc = lan_trans_data(lan, one_packet);
             break;
 
-            case 0x13:
-                rc = lan_get_device_info(lan);
+        case 0x13:
+            rc = lan_get_device_info(lan);
             break;
 
-            case 0x15:
-                rc = lan_heart_beat(lan);
+        case 0x15:
+            rc = lan_heart_beat(lan);
             break;
 
-            default:
+        default:
             break;
         }
 
         one_packet += (data_len + len_num + HEAD_LEN);
         lan->recv_len -= (data_len + len_num + HEAD_LEN);
-        
+
         if(rc != 0)
             return rc;
     }
@@ -436,23 +433,23 @@ static int gagent_create_tcp_socket(lan_st *lan)
     int opt;
 
     if(lan->tcp_server != -1)
-        return RT_EOK;
-        
+    return RT_EOK;
+
     lan->tcp_server = lwip_socket(AF_INET, SOCK_STREAM, 0);
     if(lan->tcp_server < 0)
     {
         gagent_err("tcp socket create failed!\n");
         return -RT_ERROR;
     }
-    
+
     lan->tcp_server_addr.sin_family = AF_INET;
     lan->tcp_server_addr.sin_port = htons(GAGENT_CLOUD_TCP_PORT);
     lan->tcp_server_addr.sin_addr.s_addr = INADDR_ANY;
     memset(&lan->tcp_server_addr.sin_zero, 0, sizeof(lan->tcp_server_addr.sin_zero));
-        
+
     opt = 1;
     setsockopt(lan->tcp_server, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
-    
+
     rc = lwip_bind(lan->tcp_server, (struct sockaddr *)&lan->tcp_server_addr, (socklen_t)sizeof(lan->tcp_server_addr));
     if(rc < 0)
     {
@@ -460,7 +457,7 @@ static int gagent_create_tcp_socket(lan_st *lan)
         lwip_close(lan->tcp_server);
         return -RT_ERROR;
     }
-    
+
     rc = lwip_listen(lan->tcp_server, MAX_CLIENT);
     if(rc < 0)
     {
@@ -469,7 +466,6 @@ static int gagent_create_tcp_socket(lan_st *lan)
         return -RT_ERROR;
     }
 
-
     return RT_EOK;
 }
 
@@ -481,7 +477,7 @@ static int gagent_create_udp_socket(lan_st *lan)
 
     if(lan->udp_server != -1)
         return RT_EOK;
-        
+
     lan->udp_server = lwip_socket(AF_INET, SOCK_DGRAM, 0);
     if(lan->udp_server < 0)
     {
@@ -511,14 +507,14 @@ static int gagent_create_udp_socket(lan_st *lan)
     lan->broadcast_to.sin_family = AF_INET;
     lan->broadcast_to.sin_port = htons(GAGENT_CLOUD_BROADCAST);
     lan->broadcast_to.sin_addr.s_addr = inet_addr("255.255.255.255");
-    
+
     return RT_EOK;
 }
 
 static int gagent_create_localudp_socket(lan_st *lan)
 {
     struct sockaddr_in local_udp_server;
-    
+
     if(lan->local_sock != -1)
         return RT_EOK;
 
@@ -550,7 +546,7 @@ static int gagent_close_tcp_socket(lan_st *lan)
         lwip_close(lan->tcp_server);
         lan->tcp_server = -1;
     }
-    
+
     return RT_EOK;
 }
 
@@ -594,7 +590,7 @@ void gagent_lan_thread(void *parameter)
     lan->tcp_server = -1;
     lan->udp_server = -1;
     lan->local_sock = -1;
-    
+
     while(1)
     {
         if(gagent_create_tcp_socket(lan) < 0)
@@ -609,31 +605,31 @@ void gagent_lan_thread(void *parameter)
             continue;
         }
 
-				if(gagent_create_localudp_socket(lan) < 0)
+        if(gagent_create_localudp_socket(lan) < 0)
         {
             rt_thread_delay(rt_tick_from_millisecond(3000));
             continue;
         }
-        
+
         for(id = 0; id < MAX_CLIENT; id ++)
-				{
+        {
             lan->client_fd[id] = -1;
         } 
-        
+
         while(1)
         {
             FD_ZERO(&readfds);
             FD_SET(lan->tcp_server, &readfds);
             FD_SET(lan->udp_server, &readfds);
             FD_SET(lan->local_sock, &readfds);
-            
+
             maxfd = lan->tcp_server;
             if(lan->udp_server > maxfd)
                 maxfd = lan->udp_server;
 
             if(lan->local_sock > maxfd)
                 maxfd = lan->local_sock;
-            
+
             for(id = 0; id < MAX_CLIENT; id ++)
             {
                 if(lan->client_fd[id] == -1)
@@ -650,26 +646,26 @@ void gagent_lan_thread(void *parameter)
             rc = lwip_select(maxfd + 1, &readfds, 0, 0, &timeout);
             if(rc < 0)
             {
-               gagent_err("socket select failed!\n");
+                gagent_err("socket select failed!\n");
                 break;
             }
             else if(rc == 0)
             {
                 if(tcp_client_count >= MAX_CLIENT)
                     continue;
-                    
+
                 //broadcast    
                 rc = lan_send_device_info(lan, UDP_SEND_TYPE_BOARDCAST);
                 if(rc == RT_EOK && lan->send_len > 0)
                 {
                     rc = lwip_sendto(lan->udp_server, lan->send_buf, lan->send_len, 0,
-                                    (struct sockaddr *)&lan->broadcast_to, (socklen_t)sizeof(lan->broadcast_to));
+                                        (struct sockaddr *)&lan->broadcast_to, (socklen_t)sizeof(lan->broadcast_to));
                     if(rc <= 0)
                     {
                         gagent_err("udp socket broadcast failed! errno:%d\n", errno);
                         break;
                     }
-                 }   
+                }   
 
                 continue;
             }
@@ -681,13 +677,11 @@ void gagent_lan_thread(void *parameter)
                 struct sockaddr_in local_client_addr;
                 int addr_len = sizeof(local_client_addr);
                 rt_uint8_t i;
-                
+
                 lan->recv_len = lwip_recvfrom(lan->local_sock, lan->recv_buf, sizeof(lan->recv_buf), 0,
                                                 (struct sockaddr *)&local_client_addr, (socklen_t *)&addr_len);
                 if(local_client_addr.sin_addr.s_addr != *(uint32_t *)&netif_default->ip_addr)
-                {
                     continue;
-                }
 
                 if(lan->recv_len <= 0)
                 {
@@ -707,7 +701,7 @@ void gagent_lan_thread(void *parameter)
                         lwip_close(lan->client_fd[i]);
                         lan->client_fd[i] = -1;
                         if(tcp_client_count > 0)
-                            tcp_client_count --;
+                        tcp_client_count --;
                     }
                     gagent_dbg("send client send len:%d\n", rc);
                 }
@@ -718,25 +712,25 @@ void gagent_lan_thread(void *parameter)
                 //udp socket can read
                 addr_len = sizeof(client_addr);
                 lan->recv_len = lwip_recvfrom(lan->udp_server, lan->recv_buf, sizeof(lan->recv_buf), 0,
-                                                (struct sockaddr *)&client_addr, (socklen_t *)&addr_len);
+                        (struct sockaddr *)&client_addr, (socklen_t *)&addr_len);
                 if(lan->recv_len <= 0)
                 {
                     gagent_err("udp socket recv from failed! errno:%d\n", errno);
                     break;
                 }
-                
+
 #ifdef LAN_RECV_DEBUG
                 {
                     int i;
                     gagent_dbg("udp client:%s port:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
                     rt_kprintf("recv len:%d ", lan->recv_len);
                     for(i = 0; i < lan->recv_len; i ++)
-                    {
                         rt_kprintf("%02x ", lan->recv_buf[i]);
-                    }
+
                     rt_kprintf("\r\n");
                 }
 #endif
+
                 rc = lan_udp_do_packet(lan);
                 if(rc == RT_EOK && lan->send_len > 0)
                 {
@@ -745,9 +739,8 @@ void gagent_lan_thread(void *parameter)
                     {
                         int t;
                         for(t = 0; t < lan->send_len; t ++)
-                        {
                             rt_kprintf("%02x ", lan->send_buf[t]);
-                        }
+
                         rt_kprintf("\r\n");
                     }
 #endif
@@ -761,7 +754,7 @@ void gagent_lan_thread(void *parameter)
                     gagent_dbg("udp socket sendto len:%d\n", rc);
                 }
             }
-            
+
             if(FD_ISSET(lan->tcp_server, &readfds))
             {
                 //tcp socket can read
@@ -774,11 +767,12 @@ void gagent_lan_thread(void *parameter)
                 }
                 gagent_dbg("client_sock:%d\n", client_sock);
 
-                for(id = 0; id < MAX_CLIENT; id ++) {
+                for(id = 0; id < MAX_CLIENT; id ++)
+                {
                     if(lan->client_fd[id] == -1)
                         break;
                 }
-                
+
                 if(id >= MAX_CLIENT)
                 {
                     gagent_dbg("max client!\n");
@@ -795,7 +789,7 @@ void gagent_lan_thread(void *parameter)
             {
                 if(lan->client_fd[id] == -1)
                     continue;
-                    
+
                 if(FD_ISSET(lan->client_fd[id], &readfds))
                 {
                     memset(lan->recv_buf, 0, sizeof(lan->recv_buf));
@@ -803,7 +797,7 @@ void gagent_lan_thread(void *parameter)
                     if(lan->recv_len <= 0)
                     {
                         gagent_err("lan:%d lan->recv_len:%d errno:%d\n", id, lan->recv_len, errno);
-                        
+
                         lwip_close(lan->client_fd[id]);
                         lan->client_fd[id] = -1;
                         if(tcp_client_count > 0)
@@ -816,9 +810,8 @@ void gagent_lan_thread(void *parameter)
                         int t;
                         gagent_dbg("lan %d recv len:%d\n", id, lan->recv_len);
                         for(t = 0; t < lan->recv_len; t ++)
-                        {
                             rt_kprintf("%02x ", lan->recv_buf[t]);
-                        }
+
                         rt_kprintf("\r\n");
                     }
 #endif
@@ -831,17 +824,17 @@ void gagent_lan_thread(void *parameter)
                         {
                             int t;
                             for(t = 0; t < lan->send_len; t ++)
-                            {
                                 rt_kprintf("%02x ", lan->send_buf[t]);
-                            }
+
                             rt_kprintf("\r\n");
                         }
 #endif
+
                         lan->send_len = lwip_send(lan->client_fd[id], lan->send_buf, lan->send_len, 0);
                         if(lan->send_len <= 0)
                         {
                             gagent_err("tcp client [%d] send failed! errno:%d\n", id, errno);
-                            
+
                             lwip_close(lan->client_fd[id]);
                             lan->client_fd[id] = -1;
                             if(tcp_client_count > 0)
@@ -861,12 +854,11 @@ void gagent_lan_thread(void *parameter)
                 lan->client_fd[id] = -1;
             }
         }
-        
+
         tcp_client_count = 0;
         gagent_close_tcp_socket(lan);
         gagent_close_udp_socket(lan);
         gagent_close_localudp_socket(lan);
-        
     }
 }
 
@@ -874,7 +866,7 @@ static int lan_local_send(lan_st *lan, void *data, int len)
 {
     struct sockaddr_in server_addr = {0};
     int send_len;
-    
+
     server_addr.sin_family = AF_INET;
     server_addr.sin_port = lan->local_port;
     server_addr.sin_addr = *((const struct in_addr *)&netif_default->ip_addr);
@@ -888,34 +880,29 @@ static int lan_local_send(lan_st *lan, void *data, int len)
 int gagent_lan_send_packet(lan_st *lan, rt_uint8_t action, rt_uint8_t *buf, rt_uint16_t buf_len)
 {
     int rc = RT_EOK;
-    
+
     memset(lan->send_buf, 0, sizeof(lan->send_buf));
     lan->send_len = 0;
 
     lan->send_len = gagent_set_one_packet(lan->send_buf, action, buf, buf_len);
-    
+
 #ifdef LAN_SEND_DEBUG
-	{
+    {
         uint32_t i;
         rt_kprintf("lan send_len:%d\n", lan->send_len);
         for(i = 0; i < lan->send_len; i ++)
-        {
             rt_kprintf("%02x ", lan->send_buf[i]);
-        }
+
         rt_kprintf("\r\n");
-	}
+    }
 #endif
 
     rc = lan_local_send(lan, lan->send_buf, lan->send_len);
     if (rc == lan->send_len)
-    {
         rc = RT_EOK;
-    }
     else
-    {
         rc = -RT_ERROR;
-    }
-    
+
     return rc;
 }
 
@@ -935,6 +922,6 @@ int gagent_lan_init(lan_st *lan)
         rt_free(lan);
         return -RT_ERROR;
     }
-        
+
     return RT_EOK;
 }

+ 65 - 67
gagent_mqtt.c

@@ -1,26 +1,26 @@
 /*
- * File      : gagent_mqtt.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2018, RT-Thread Development Team
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License along
- *  with this program; if not, write to the Free Software Foundation, Inc.,
- *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Change Logs:
- * Date           Author       Notes
- * 2018-01-03     flyingcys    first version
- */
+* File      : gagent_mqtt.c
+* This file is part of RT-Thread RTOS
+* COPYRIGHT (C) 2018, RT-Thread Development Team
+*
+*  This program is free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  This program is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  You should have received a copy of the GNU General Public License along
+*  with this program; if not, write to the Free Software Foundation, Inc.,
+*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*
+* Change Logs:
+* Date           Author       Notes
+* 2018-01-03     flyingcys    first version
+*/
 #include "gagent_def.h"
 #include "gagent_cloud.h"
 
@@ -39,15 +39,15 @@ static int mqtt_push_ota(cloud_st *cloud, char *packet)
 
 static int mqtt_trans_data(cloud_st *cloud, char *packet)
 {
-    uint16_t len, cmd;
+    rt_uint16_t len, cmd;
     char *index, *kv;
-    uint16_t kv_len;
-    uint8_t length_len, action;
+    rt_uint16_t kv_len;
+    rt_uint8_t length_len, action;
 
     action = 0;
     kv = 0;
     kv_len = 0;
-    
+
     index = packet;
     len = gagent_parse_rem_len((const uint8_t *)index + 4);
     length_len = gagent_num_rem_len_bytes((const uint8_t *)index + 4);
@@ -56,7 +56,7 @@ static int mqtt_trans_data(cloud_st *cloud, char *packet)
 
     rt_memcpy(&cmd, index, 2);
     index += 2;
-    
+
     cmd = ntohs(cmd);
     gagent_dbg("cmd:%x\n", cmd);
 
@@ -79,7 +79,7 @@ static int mqtt_trans_data(cloud_st *cloud, char *packet)
     }
     else
         return -RT_ERROR;
-    
+
     return gagent_cloud_recv_packet(CMD_FROM_MQTT, action, (rt_uint8_t *)kv, kv_len);
 }
 
@@ -94,18 +94,17 @@ static void gagent_mqtt_callback(MQTTClient *c, MessageData *msg_data)
     int rc = RT_EOK;
 
 #ifdef MQTT_RECV_DEBUG
-	{
-    size_t len;
-    char *data = (char *)msg_data->message->payload;
-    gagent_dbg("mqtt_callback topic_name: %d %s\n", msg_data->topicName->lenstring.len, msg_data->topicName->lenstring.data);
-
-    rt_kprintf("mqtt recv_len:%d\n", msg_data->message->payloadlen);
-    for(len = 0; len < msg_data->message->payloadlen; len ++)
     {
-        rt_kprintf("%02x ", data[len]);
+        size_t len;
+        char *data = (char *)msg_data->message->payload;
+        gagent_dbg("mqtt_callback topic_name: %d %s\n", msg_data->topicName->lenstring.len, msg_data->topicName->lenstring.data);
+
+        rt_kprintf("mqtt recv_len:%d\n", msg_data->message->payloadlen);
+        for(len = 0; len < msg_data->message->payloadlen; len ++)
+            rt_kprintf("%02x ", data[len]);
+
+        rt_kprintf("\r\n");
     }
-    rt_kprintf("\r\n");
-	}
 #endif
 
     one_packet = (char *)msg_data->message->payload;
@@ -114,27 +113,26 @@ static void gagent_mqtt_callback(MQTTClient *c, MessageData *msg_data)
     {
         memcpy(&cmd, one_packet + 6, 2);
         cmd = ntohs(cmd);
-        
+
         gagent_dbg("mqtt_cmd:%x\n", cmd);
         switch(cmd)
         {
-            case 0x10:                     //log on/off
-            break;
-            
-            case 0x90:                     //trans data
-            case 0x93:
-                rc = mqtt_trans_data(cloud, one_packet);
+        case 0x10:                     //log on/off
             break;
 
-            case 0x210:                 //app number change
+        case 0x90:                     //trans data
+        case 0x93:
+            rc = mqtt_trans_data(cloud, one_packet);
+            break;
 
+        case 0x210:                 //app number change
             break;
 
-            case 0x211:                 //push ota
-                rc = mqtt_push_ota(cloud, one_packet);
+        case 0x211:                 //push ota
+            rc = mqtt_push_ota(cloud, one_packet);
             break;
 
-            default:
+        default:
             break;
         }
         one_packet += (data_len + len_num + HEAD_LEN);
@@ -182,7 +180,7 @@ int gagent_mqtt_send_packet(cloud_st *cloud, rt_uint8_t action, rt_uint8_t *buf,
 {
     int rc = RT_EOK;
     char topic[128];
-    
+
     memset(topic, 0, sizeof(topic));
     rt_snprintf(topic, sizeof(topic), "dev2app/%s", cloud->con->did);
     gagent_dbg("pub_topic:%s\n", topic);
@@ -193,15 +191,15 @@ int gagent_mqtt_send_packet(cloud_st *cloud, rt_uint8_t action, rt_uint8_t *buf,
     cloud->send_len = gagent_set_one_packet(cloud->send_buf, action, buf, buf_len);
 
 #ifdef MQTT_SEND_DEBUG
-	{
-    uint32_t i;
-    rt_kprintf("mqtt send_len:%d\n", cloud->send_len);
-    for(i = 0; i < cloud->send_len; i ++)
     {
-        rt_kprintf("%02x ", cloud->send_buf[i]);
+        uint32_t i;
+        rt_kprintf("mqtt send_len:%d\n", cloud->send_len);
+        for(i = 0; i < cloud->send_len; i ++)
+        {
+            rt_kprintf("%02x ", cloud->send_buf[i]);
+        }
+        rt_kprintf("\r\n");
     }
-    rt_kprintf("\r\n");
-	}
 #endif
 
     rc = gagent_mqtt_client_publish(topic, cloud->send_buf, cloud->send_len);
@@ -211,15 +209,15 @@ int gagent_mqtt_send_packet(cloud_st *cloud, rt_uint8_t action, rt_uint8_t *buf,
 int gagent_mqtt_init(cloud_st *cloud)
 {
     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
- 
-		RT_ASSERT(cloud != RT_NULL);
+
+    RT_ASSERT(cloud != RT_NULL);
 
     memset(&gagent_mqtt, 0, sizeof(gagent_mqtt));
-    
+
     gagent_mqtt.host = cloud->mqtt_server;
     gagent_mqtt.port = cloud->mqtt_port;
 
-    
+
     memcpy(&gagent_mqtt.condata, &data, sizeof(MQTTPacket_connectData));
     gagent_mqtt.condata.keepAliveInterval = 30;
     gagent_mqtt.condata.MQTTVersion = 3;
@@ -227,7 +225,7 @@ int gagent_mqtt_init(cloud_st *cloud)
     gagent_mqtt.condata.clientID.cstring = cloud->con->did;
     gagent_mqtt.condata.username.cstring = cloud->con->did;
     gagent_mqtt.condata.password.cstring = cloud->con->passcode;
-    
+
     gagent_mqtt.buf_size = gagent_mqtt.readbuf_size = 1024;
     gagent_mqtt.buf = malloc(gagent_mqtt.buf_size);
     gagent_mqtt.readbuf = malloc(gagent_mqtt.readbuf_size);
@@ -252,14 +250,14 @@ int gagent_mqtt_init(cloud_st *cloud)
     gagent_mqtt.messageHandlers[1].callback = gagent_mqtt_callback;
 
     gagent_mqtt.defaultMessageHandler = gagent_mqtt_callback;
-    
+
     gagent_dbg("host:%s port:%d\n", gagent_mqtt.host, gagent_mqtt.port);
     gagent_dbg("clientID:%s username:%s password:%s\n", 
-                    gagent_mqtt.condata.clientID.cstring, 
-                    gagent_mqtt.condata.username.cstring,
-                    gagent_mqtt.condata.password.cstring);
+    gagent_mqtt.condata.clientID.cstring, 
+    gagent_mqtt.condata.username.cstring,
+    gagent_mqtt.condata.password.cstring);
     gagent_dbg("topic:%s\n", gagent_mqtt.messageHandlers[0].topicFilter);
-		
+
     return paho_mqtt_start(&gagent_mqtt);
 }
 

+ 82 - 91
gagent_tool.c

@@ -1,119 +1,114 @@
 /*
- * File      : gagent_tool.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2018, RT-Thread Development Team
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License along
- *  with this program; if not, write to the Free Software Foundation, Inc.,
- *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Change Logs:
- * Date           Author       Notes
- * 2018-01-03     flyingcys    first version
- */
+* File      : gagent_tool.c
+* This file is part of RT-Thread RTOS
+* COPYRIGHT (C) 2018, RT-Thread Development Team
+*
+*  This program is free software; you can redistribute it and/or modify
+*  it under the terms of the GNU General Public License as published by
+*  the Free Software Foundation; either version 2 of the License, or
+*  (at your option) any later version.
+*
+*  This program is distributed in the hope that it will be useful,
+*  but WITHOUT ANY WARRANTY; without even the implied warranty of
+*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*  GNU General Public License for more details.
+*
+*  You should have received a copy of the GNU General Public License along
+*  with this program; if not, write to the Free Software Foundation, Inc.,
+*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*
+* Change Logs:
+* Date           Author       Notes
+* 2018-01-03     flyingcys    first version
+*/
 #include "gagent_def.h"
 
 int gagent_add_pkcs(char *src, int len)
 {    
     char pkcs[16];
     int i, cs = 16 - len % 16;    
-    
+
     rt_memset(pkcs, 0, sizeof(pkcs));   
-    
+
     for(i = 0; i < cs; i ++ )
-    {        
         pkcs[i] = cs;    
-    }
+
+    rt_memcpy(src + len, pkcs, cs);
     
-    rt_memcpy(src + len, pkcs, cs);    
     return (len + cs);
 }
 
+rt_uint16_t gagent_parse_rem_len(const rt_uint8_t *buf)
+{
+    rt_uint16_t multiplier = 1;
+    rt_uint16_t value = 0;
+    rt_uint8_t digit;
 
+    do
+    {
+        digit = *buf;
+        value += (digit & 0x7F) * multiplier;
+        multiplier *= 0x80;
+        buf++;
+    }while((digit & 0x80) != 0);
 
-uint16_t gagent_parse_rem_len(const uint8_t* buf)
-{
-	uint16_t multiplier = 1;
-	uint16_t value = 0;
-	uint8_t digit;
-		
-	do
-	{
-		digit = *buf;
-		value += (digit & 0x7F) * multiplier;
-		multiplier *= 0x80;
-		buf++;
-	} while((digit & 0x80) != 0);
-
-	return value;
+    return value;
 }
 
-uint8_t gagent_num_rem_len_bytes(const uint8_t* buf)
+rt_uint8_t gagent_num_rem_len_bytes(const uint8_t* buf)
 {
-	uint8_t num_bytes = 1;
-		
-	if ((buf[0] & 0x80) == 0x80)
-	{
-		num_bytes++;
-		if ((buf[1] & 0x80) == 0x80)
-		{
-			num_bytes ++;
-			if ((buf[2] & 0x80) == 0x80)
-			{
-				num_bytes ++;
-			}
-		}
-	}
-	return num_bytes;
+    rt_uint8_t num_bytes = 1;
+
+    if((buf[0] & 0x80) == 0x80)
+    {
+        num_bytes++;
+        if((buf[1] & 0x80) == 0x80)
+        {
+            num_bytes ++;
+            if ((buf[2] & 0x80) == 0x80)
+                num_bytes ++;
+        }
+    }
+    return num_bytes;
 }
 
-uint8_t gagent_get_rem_len(int length, char *buf)
+rt_uint8_t gagent_get_rem_len(int length, char *buf)
 {
-    uint8_t cnt = 0, digit;
+    rt_uint8_t cnt = 0, digit;
 
     do
     {
         digit = length % 128;
         length /= 128;
-        
-    	if (length > 0) 
-        	digit = digit | 0x80;
+
+        if (length > 0) 
+        digit = digit | 0x80;
 
         buf[cnt] = digit;
         cnt ++;
-    } while(length > 0);
+    }while(length > 0);
 
-	return cnt;
+    return cnt;
 }
 
 int gagent_get_one_packet(char *packet, int *data_len, rt_uint8_t *len_num, int remain_len)
 {
-	char			*index;
-	uint16_t		len;
-	uint8_t			length_len;
-	
-	if(packet == NULL || data_len == NULL || len_num == NULL || remain_len <= 0) 
-	    return -1;
-
-	index = packet;
-
-	len = gagent_parse_rem_len((const uint8_t *)index + 4);
-	length_len = gagent_num_rem_len_bytes((const uint8_t*)index + 4);
-
-	*data_len = len;
-	*len_num = length_len;
-	
-	return RT_EOK;
+    char *index;
+    rt_uint16_t len;
+    rt_uint8_t length_len;
+
+    if(packet == NULL || data_len == NULL || len_num == NULL || remain_len <= 0) 
+        return -1;
+
+    index = packet;
+
+    len = gagent_parse_rem_len((const uint8_t *)index + 4);
+    length_len = gagent_num_rem_len_bytes((const uint8_t*)index + 4);
+
+    *data_len = len;
+    *len_num = length_len;
+
+    return RT_EOK;
 }
 
 int gagent_set_one_packet(char *packet, uint8_t action, uint8_t *buf, uint32_t buf_len)
@@ -122,7 +117,7 @@ int gagent_set_one_packet(char *packet, uint8_t action, uint8_t *buf, uint32_t b
     rt_uint32_t packet_len;
     rt_uint8_t length_num, i;
     char *index = packet;
-    
+
     //head
     *index ++ = 0x00;
     *index ++ = 0x00;
@@ -132,16 +127,12 @@ int gagent_set_one_packet(char *packet, uint8_t action, uint8_t *buf, uint32_t b
     //
     packet_len = buf_len + 3;           //flag + cmd * 2
     if(buf && (buf_len > 0))
-    {
         packet_len += 1;                //action
-    }
 
     memset(length_bytes, 0, sizeof(length_bytes));
     length_num = gagent_get_rem_len(packet_len, length_bytes);
     for(i = 0; i < length_num; i ++)
-    {
         *index ++ = length_bytes[i];
-    }
 
     //flag
     *index ++ = 0x00;
@@ -151,7 +142,7 @@ int gagent_set_one_packet(char *packet, uint8_t action, uint8_t *buf, uint32_t b
     *index ++ = 0x91;
 
     *index ++ = action;
-        
+
     if(buf && buf_len > 0)
     {
         memcpy(index, buf, buf_len);
@@ -166,7 +157,7 @@ int gagent_strtohex(char *dst, char *src, int len)
     char h1,h2;
     int i;
 
-    for (i = 0; i< len; i += 2)
+    for(i = 0; i< len; i += 2)
     {   
         h1 = src[i];
         if((h1 >= 'A') && (h1 <= 'F'))
@@ -175,7 +166,7 @@ int gagent_strtohex(char *dst, char *src, int len)
             h1 = h1 - 'a' + 10;
         else
             h1 = h1 - '0';
-        
+
         h2 = src[i + 1];
         if((h2 >= 'A') && (h2 <= 'F'))
             h2 = h2 - 'A' + 10;
@@ -186,8 +177,8 @@ int gagent_strtohex(char *dst, char *src, int len)
 
         dst[i / 2] = ((h1 << 4) & 0xf0) + (h2 & 0x0f);
     }
-		
-		return len / 2;
+
+    return len / 2;
 }