Просмотр исходного кода

1.[modify] update code license and modify part of the function name
2.[update] README.md sample use way.

Signed-off-by: chenyong <1521761801@qq.com>

chenyong 7 лет назад
Родитель
Сommit
c98aca0e51
6 измененных файлов с 119 добавлено и 42 удалено
  1. 4 2
      README.md
  2. 19 5
      inc/onenet.h
  3. 16 2
      ports/onenet_port.c
  4. 25 10
      samples/onenet_sample.c
  5. 29 10
      src/onenet_http.c
  6. 26 13
      src/onenet_mqtt.c

+ 4 - 2
README.md

@@ -72,9 +72,9 @@ int onenet_port_data_process(char *recv_data, rt_size_t size)
 
 ### 4.2. 运行示例
 
-该示例用于设备连接 OneNET 云端后,需要`联网成功`之后 msh 中执行 `onenet_send_data_cycle` 命令,可5秒一次循环发送数据流名为 `temperature` 的随机数据到 OneNET 云端,云端建立[数据流模板](https://open.iot.10086.cn/doc/art402.html#97)后即可实时查看上传数据信息。具体过程如下:
+该示例用于设备连接 OneNET 云端后,需要`联网成功`之后 msh 中执行 `onenet_mqtt_init` 初始化MQTT协议设备上线,然后执行 `onenet_upload_cycle` 命令,可5秒一次循环发送数据流名为 `temperature` 的随机数据到 OneNET 云端,云端建立[数据流模板](https://open.iot.10086.cn/doc/art402.html#97)后即可实时查看上传数据信息。具体过程如下:
 
-    msh />onenet_send_data_cycle
+    msh />onenet_mqtt_init
     Enter mqtt_connect_callback!
     [MQTT] ipv4 address port: 6002
     [MQTT] HOST = '183.230.40.39'
@@ -82,6 +82,8 @@ int onenet_port_data_process(char *recv_data, rt_size_t size)
     OneNET cloud(V0.1.0) initialize success.
     [MQTT] Subscribe #0 /test_topic OK!
     Enter mqtt_online_callback!             //初始化完成,设备上线成功
+    msh />
+    msh />onenet_upload_cycle
     buffer : {"temperature":8}
     buffer : {"temperature":56}
     buffer : {"temperature":19}             

+ 19 - 5
inc/onenet.h

@@ -1,10 +1,24 @@
 /*
  * File      : onenet.h
- * COPYRIGHT (C) 2012-2018, Shanghai Real-Thread Technology Co., Ltd
+ * COPYRIGHT (C) 2006 - 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-04-23    chenyong     the first version
+ * 2018-04-24     chenyong     first version
  */
 #ifndef _ONENET_H_
 #define _ONENET_H_
@@ -114,14 +128,14 @@ struct rt_onenet_info
 typedef struct rt_onenet_info *rt_onenet_info_t;
 
 /* OneNET MQTT initialize. */
-int onenet_init(void);
+int onenet_mqtt_init(void);
 
 /* Publish MQTT data to subscribe topic. */
 int onenet_mqtt_publish(const char *topic, const char *msg_str);
 
 /* Device send data to OneNET cloud. */
-rt_err_t onenet_send_digit(const char *name, int digit);
-rt_err_t onenet_send_string(const char *name, char *str);
+rt_err_t onenet_http_upload_digit(const char *name, int digit);
+rt_err_t onenet_http_upload_string(const char *name, char *str);
 
 /* ========================== User port function ============================ */
 

+ 16 - 2
ports/onenet_port.c

@@ -1,10 +1,24 @@
 /*
  * File      : onenet_port.c
- * COPYRIGHT (C) 2012-2018, Shanghai Real-Thread Technology Co., Ltd
+ * COPYRIGHT (C) 2006 - 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-04-20    chenyong     the first version
+ * 2018-04-24     chenyong     first version
  */
 #include <stdlib.h>
 

+ 25 - 10
samples/onenet_sample.c

@@ -1,16 +1,33 @@
 /*
  * File      : onenet_sample.c
- * COPYRIGHT (C) 2012-2018, Shanghai Real-Thread Technology Co., Ltd
+ * COPYRIGHT (C) 2006 - 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-04-23    chenyong     the first version
+ * 2018-04-24     chenyong     first version
  */
 #include <stdlib.h>
 
 #include <onenet.h>
 
-static void onenet_send_data_entry(void *parameter)
+#ifdef FINSH_USING_MSH
+#include <finsh.h>
+
+static void onenet_upload_entry(void *parameter)
 {
     int value = 0;
 
@@ -18,7 +35,7 @@ static void onenet_send_data_entry(void *parameter)
     {
         value = rand() % 100;
 
-        if (onenet_send_digit("temperature", value) < 0)
+        if (onenet_http_upload_digit("temperature", value) < 0)
         {
             break;
         }
@@ -27,12 +44,12 @@ static void onenet_send_data_entry(void *parameter)
     }
 }
 
-int onenet_send_data_cycle(void)
+int onenet_upload_cycle(void)
 {
     rt_thread_t tid;
 
     tid = rt_thread_create("onenet_send",
-            onenet_send_data_entry,
+            onenet_upload_entry,
             RT_NULL,
             2 * 1024,
             RT_THREAD_PRIORITY_MAX / 3 - 1,
@@ -44,8 +61,6 @@ int onenet_send_data_cycle(void)
 
     return 0;
 }
+MSH_CMD_EXPORT(onenet_upload_cycle, send data to OneNET cloud cycle);
 
-#ifdef FINSH_USING_MSH
-#include <finsh.h>
-MSH_CMD_EXPORT(onenet_send_data_cycle, send data to OneNET cloud cycle);
-#endif
+#endif /* FINSH_USING_MSH */

+ 29 - 10
src/onenet_send_data.c → src/onenet_http.c

@@ -1,10 +1,24 @@
 /*
- * File      : rt_onenet_send_data.c
- * COPYRIGHT (C) 2012-2018, Shanghai Real-Thread Technology Co., Ltd
+ * File      : onenet_http.c
+ * COPYRIGHT (C) 2006 - 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-04-23   chenyong     the first version
+ * 2018-04-24     chenyong     first version
  */
 #include <stdlib.h>
 #include <string.h>
@@ -18,7 +32,7 @@
 #define ONENET_HEAD_DATA_LEN           256
 #define ONENET_CON_URI_LEN             256
  
- static rt_err_t onenet_send_data(char *send_buffer)
+ static rt_err_t onenet_upload_data(char *send_buffer)
 {
     struct webclient_session* session = NULL;
     char *header = RT_NULL, *header_ptr;
@@ -201,15 +215,17 @@ __exit:
     return result;
 }
 
-rt_err_t onenet_send_digit(const char *name, int digit)
+rt_err_t onenet_http_upload_digit(const char *name, int digit)
 {
     char *send_buffer = RT_NULL;
     rt_err_t result = RT_EOK;
 
+    assert(name);
+
     send_buffer = ONENET_CALLOC(1, ONENET_SEND_DATA_LEN);
     if (!send_buffer)
     {
-        log_e("RT ONENET send digit failed! No memory for send buffer!");
+        log_e("ONENET HTTP upload digit failed! No memory for send buffer!");
         return -RT_ENOMEM;
     }
 
@@ -221,7 +237,7 @@ rt_err_t onenet_send_digit(const char *name, int digit)
     }
 
     /* send data to cloud by HTTP */
-    result = onenet_send_data(send_buffer);
+    result = onenet_upload_data(send_buffer);
     if (result < 0)
     {
         goto __exit;
@@ -236,15 +252,18 @@ __exit:
     return result;
 }
 
-rt_err_t onenet_send_string(const char *name, char *str)
+rt_err_t onenet_http_upload_string(const char *name, char *str)
 {
     char *send_buffer = RT_NULL;
     rt_err_t result = RT_EOK;
 
+    assert(name);
+    assert(str);
+
     send_buffer = RT_ONENET_CALLOC(1, ONENET_SEND_DATA_LEN);
     if (!send_buffer)
     {
-        log_e("RT ONENET send digit failed! No memory for send buffer!");
+        log_e("ONENET HTTP upload digit failed! No memory for send buffer!");
         return -RT_ENOMEM;
     }
 
@@ -256,7 +275,7 @@ rt_err_t onenet_send_string(const char *name, char *str)
     }
 
     /* send data to cloud by HTTP */
-    result = onenet_send_data(send_buffer);
+    result = onenet_upload_data(send_buffer);
     if (result < 0)
     {
         goto __exit;

+ 26 - 13
src/onenet_mqtt.c

@@ -1,10 +1,24 @@
 /*
  * File      : onenet_mqtt.c
- * COPYRIGHT (C) 2012-2018, Shanghai Real-Thread Technology Co., Ltd
+ * COPYRIGHT (C) 2006 - 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-04-20    chenyong     the first version
+ * 2018-04-24     chenyong     first version
  */
 #include <stdlib.h>
 #include <string.h>
@@ -37,7 +51,7 @@ static void mqtt_offline_callback(MQTTClient *c)
     log_d("Enter mqtt_offline_callback!");
 }
 
-static rt_err_t onenet_mqtt_init(void)
+static rt_err_t onenet_mqtt_entry(void)
 {
     MQTTPacket_connectData condata = MQTTPacket_connectData_initializer;
 
@@ -70,7 +84,6 @@ static rt_err_t onenet_mqtt_init(void)
     mq_client.defaultMessageHandler = mqtt_callback;
 
     paho_mqtt_start(&mq_client);
-    log_i("OneNET MQTT is startup!");
     
     return RT_EOK;
 }
@@ -84,7 +97,7 @@ static void onenet_get_info(void)
     strncpy(onenet_info.server_uri, ONENET_SERVER_URL, strlen(ONENET_SERVER_URL));  
 } 
 
-int onenet_init(void)
+int onenet_mqtt_init(void)
 {
     int result = 0;
     
@@ -95,7 +108,7 @@ int onenet_init(void)
     
     onenet_get_info();
     
-    if(onenet_mqtt_init() < 0)
+    if(onenet_mqtt_entry() < 0)
     {
         result = -1;
     }
@@ -103,11 +116,11 @@ int onenet_init(void)
     if(!result)
     {
         init_ok = RT_TRUE;
-        log_i("OneNET cloud(V%s) initialize success.", ONENET_SW_VERSION);
+        log_i("OneNET MQTT initialize success.");
     }
     else
     {
-        log_e("OneNET cloud(V%s) initialize failed(%d).", ONENET_SW_VERSION, result);
+        log_e("OneNET MQTT initialize failed(%d).", result);
     }
 
     return result;
@@ -129,6 +142,9 @@ int onenet_mqtt_publish(const char *topic, const char *msg_str)
     return 0;
 }
 
+#ifdef FINSH_USING_MSH
+#include <finsh.h>
+
 int onenet_publish(int argc, char **argv)
 {
     if (argc != 3)
@@ -141,10 +157,7 @@ int onenet_publish(int argc, char **argv)
 
     return 0;
 }
-
-#ifdef FINSH_USING_MSH
-#include <finsh.h>
-MSH_CMD_EXPORT(onenet_init, OneNET cloud mqtt initializate);
-MSH_CMD_EXPORT(onenet_publish, OneNET cloud send data to subscribe topic);
+MSH_CMD_EXPORT(onenet_mqtt_init, OneNET cloud mqtt initializate);
+MSH_CMD_EXPORT_ALIAS(onenet_publish, onenet_mqtt_publish, OneNET cloud send data to subscribe topic);
 #endif