Преглед изворни кода

优化代码

Signed-off-by: MurphyZhao <d2014zjt@163.com>
MurphyZhao пре 7 година
родитељ
комит
89c5c9f8e7

+ 2 - 1
SConscript

@@ -123,7 +123,8 @@ if GetDepend(['PKG_USING_ALI_IOTKIT_COAP_OTA']):
 
 CPPDEFINES += ['IOTX_NET_INIT_WITH_PK_EXT', '_PLATFORM_IS_RTTHREAD_', 'IOTX_WITHOUT_ITLS']
 
-CPPDEFINES += ['IOTX_PRJ_VERSION=\\"V2.10\\"']
+if rtconfig.CROSS_TOOL == 'gcc' :
+	CPPDEFINES += ['IOTX_PRJ_VERSION=\\"V2.10\\"']
 
 group = DefineGroup('ali-iotkit', src, depend = ['PKG_USING_ALI_IOTKIT'], CPPPATH = CPPPATH, LOCAL_CCFLAGS = LOCAL_CCFLAGS, CPPDEFINES = CPPDEFINES)
 Return('group')

+ 2 - 0
docs/samples.md

@@ -303,6 +303,8 @@ mqtt_client|294 :: OTA FW version: v10
 
 ![升级进度](./figures/AliLinkDevelopOTAUpgrading.png)
 
+升级进度 100% 后,再次运行 **`ali_ota_test start`** 命令,将最新的版本号上传到云端,版本号匹配成功后,云端显示升级成功,如下图所示:
+
 ![升级成功](./figures/AliLinkDevelopOTAUpgradSuccess.png)
 
 **退出 OTA 例程**

+ 4 - 4
iotkit-embedded/src/system/iotkit-system/src/sdk-impl.c

@@ -29,12 +29,12 @@ void IOT_OpenLog(const char *ident)
         mod = "---";
     }
 
-    return LITE_openlog(mod);
+    LITE_openlog(mod);
 }
 
 void IOT_CloseLog(void)
 {
-    return LITE_closelog();
+    LITE_closelog();
 }
 
 void IOT_SetLogLevel(IOT_LogLevel level)
@@ -48,7 +48,7 @@ void IOT_SetLogLevel(IOT_LogLevel level)
         return;
     }
 
-    return LITE_set_loglevel(lvl);
+    LITE_set_loglevel(lvl);
 }
 
 void IOT_DumpMemoryStats(IOT_LogLevel level)
@@ -60,7 +60,7 @@ void IOT_DumpMemoryStats(IOT_LogLevel level)
         log_warning("Invalid input level, using default: %d => %d", level, lvl);
     }
 
-    return LITE_dump_malloc_free_stats(lvl);
+    LITE_dump_malloc_free_stats(lvl);
 }
 
 #if defined(MQTT_COMM_ENABLED)

+ 9 - 6
ports/rtthread/HAL_OS_rtthread.c

@@ -61,6 +61,7 @@ void *HAL_MutexCreate(void)
 void HAL_MutexDestroy(_IN_ void *mutex)
 {
     int err_num;
+    err_num = err_num;
     if (0 != (err_num = rt_mutex_delete((rt_mutex_t)mutex))) {
         perror("destroy mutex failed");
     }
@@ -69,6 +70,7 @@ void HAL_MutexDestroy(_IN_ void *mutex)
 void HAL_MutexLock(_IN_ void *mutex)
 {
     int err_num;
+	  err_num = err_num;
     if (0 != (err_num = rt_mutex_take((rt_mutex_t)mutex, RT_WAITING_FOREVER))) {
         perror("lock mutex failed");
     }
@@ -77,6 +79,7 @@ void HAL_MutexLock(_IN_ void *mutex)
 void HAL_MutexUnlock(_IN_ void *mutex)
 {
     int err_num;
+    err_num = err_num;
     if (0 != (err_num = rt_mutex_release((rt_mutex_t)mutex))) {
         perror("unlock mutex failed");
     }
@@ -114,12 +117,12 @@ void HAL_SleepMs(_IN_ uint32_t ms)
 
 void HAL_Srandom(uint32_t seed)
 {
-    srandom(seed);
+    srand(seed);
 }
 
 uint32_t HAL_Random(uint32_t region)
 {
-    return (region > 0) ? (random() % region) : 0;
+    return (region > 0) ? (rand() % region) : 0;
 }
 
 int HAL_Snprintf(_IN_ char *str, const int len, const char *fmt, ...)
@@ -128,7 +131,7 @@ int HAL_Snprintf(_IN_ char *str, const int len, const char *fmt, ...)
     int     rc;
 
     va_start(args, fmt);
-    rc = vsnprintf(str, len, fmt, args);
+    rc = rt_vsnprintf(str, len, fmt, args);
     va_end(args);
 
     return rc;
@@ -136,7 +139,7 @@ int HAL_Snprintf(_IN_ char *str, const int len, const char *fmt, ...)
 
 int HAL_Vsnprintf(_IN_ char *str, _IN_ const int len, _IN_ const char *format, va_list ap)
 {
-    return vsnprintf(str, len, format, ap);
+    return rt_vsnprintf(str, len, format, ap);
 }
 
 void HAL_Printf(_IN_ const char *fmt, ...)
@@ -144,9 +147,9 @@ void HAL_Printf(_IN_ const char *fmt, ...)
     va_list args;
     
     va_start(args, fmt);
-    vsnprintf(log_buf, HAL_OS_LOG_MAXLEN, fmt, args);
+    rt_vsnprintf(log_buf, HAL_OS_LOG_MAXLEN, fmt, args);
     va_end(args);
-    printf("%s", log_buf);
+    rt_kprintf("%s", log_buf);
 }
 
 int HAL_GetPartnerID(char* pid_str)

+ 3 - 3
ports/rtthread/HAL_UDP_rtthread.c

@@ -21,10 +21,10 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <sys/time.h>
 #include <sys/select.h>
 #include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
 #include <netdb.h>
 #include <unistd.h>
 
@@ -67,7 +67,7 @@ void *HAL_UDP_create(char *host, unsigned short port)
         if (AF_INET == ainfo->ai_family) {
             sa = (struct sockaddr_in *)ainfo->ai_addr;
 
-            strncpy(addr, NETWORK_ADDR_LEN, inet_ntoa(sa->sin_addr));
+            strncpy(addr, inet_ntoa(sa->sin_addr), NETWORK_ADDR_LEN);
 
             fprintf(stderr, "The host IP %s, port is %d\r\n", addr, ntohs(sa->sin_port));
 

+ 0 - 195
samples/coap/coap-example-1.c

@@ -1,195 +0,0 @@
-/*
- * Copyright (c) 2014-2018 RT-Thread Group. All rights reserved.
- * License-Identifier: Apache-2.0
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may
- * not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-#include "iot_import.h"
-#include "iot_export.h"
-
-#include "rtthread.h"
-
-#define IOTX_DAILY_DTLS_SERVER_URI      "coaps://iot-as-coap.alibaba.net:5684"
-#define IOTX_PRE_DTLS_SERVER_URI        "coaps://pre.iot-as-coap.cn-shanghai.aliyuncs.com:5684"
-#define IOTX_PRE_NOSEC_SERVER_URI       "coap://pre.iot-as-coap.cn-shanghai.aliyuncs.com:5683"
-
-#define IOTX_ONLINE_DTLS_SERVER_URL     "coaps://a1l6az94hso.iot-as-coap.cn-shanghai.aliyuncs.com:5684"
-
-static char m_coap_client_running = 0;
-
-static void iotx_response_handler(void *arg, void *p_response)
-{
-    int            len       = 0;
-    unsigned char *p_payload = NULL;
-    iotx_coap_resp_code_t resp_code;
-    IOT_CoAP_GetMessageCode(p_response, &resp_code);
-    IOT_CoAP_GetMessagePayload(p_response, &p_payload, &len);
-    HAL_Printf("[APPL]: Message response code: 0x%x\r\n", resp_code);
-    HAL_Printf("[APPL]: Len: %d, Payload: %s, \r\n", len, p_payload);
-}
-
-#ifdef TEST_COAP_DAILY
-    #define IOTX_PRODUCT_KEY         "zPygj0yP3UF"
-    #define IOTX_DEVICE_NAME         "device_2"
-    #define IOTX_DEVICE_SECRET       "5FQbVOPWNwhEuCvnVcP1Mvyjmvt8ECQi"
-    #define IOTX_DEVICE_ID           "device_2"
-#else
-    #define IOTX_PRODUCT_KEY         "a1l6az94hso"
-    #define IOTX_DEVICE_NAME         "TestCoap0723"
-    #define IOTX_DEVICE_SECRET       "HaQyecD3x8W5NjU1dzsW0kYE7CUrKNHW"
-    #define IOTX_DEVICE_ID           "TestCoap0723"
-#endif
-
-static int iotx_set_devinfo(iotx_deviceinfo_t *p_devinfo)
-{
-    if (NULL == p_devinfo) {
-        return IOTX_ERR_INVALID_PARAM;
-    }
-
-    memset(p_devinfo, 0x00, sizeof(iotx_deviceinfo_t));
-
-    /**< get device info*/
-    HAL_GetProductKey(p_devinfo->product_key);
-    HAL_GetDeviceName(p_devinfo->device_name);
-    HAL_GetDeviceSecret(p_devinfo->device_secret);
-    HAL_GetDeviceID(p_devinfo->device_id);
-    /**< end*/
-
-    fprintf(stderr, "*****The Product Key  : %s *****\r\n", p_devinfo->product_key);
-    fprintf(stderr, "*****The Device Name  : %s *****\r\n", p_devinfo->device_name);
-    //fprintf(stderr, "*****The Device Secret: %s *****\r\n", p_devinfo->device_secret);
-    fprintf(stderr, "*****The Device ID    : %s *****\r\n", p_devinfo->device_id);
-    return IOTX_SUCCESS;
-}
-
-static void iotx_post_data_to_server(void *param)
-{
-    char               path[IOTX_URI_MAX_LEN + 1] = {0};
-    iotx_message_t     message;
-    iotx_deviceinfo_t  devinfo;
-    message.p_payload = (unsigned char *)"{\"name\":\"hello world\"}";
-    message.payload_len = strlen("{\"name\":\"hello world\"}");
-    message.resp_callback = iotx_response_handler;
-    message.msg_type = IOTX_MESSAGE_CON;
-    message.content_type = IOTX_CONTENT_TYPE_JSON;
-    iotx_coap_context_t *p_ctx = (iotx_coap_context_t *)param;
-
-    iotx_set_devinfo(&devinfo);
-    snprintf(path, IOTX_URI_MAX_LEN, "/topic/%s/%s/update/", (char *)devinfo.product_key,
-             (char *)devinfo.device_name);
-
-    IOT_CoAP_SendMessage(p_ctx, path, &message);
-}
-
-static int ali_coap_main(int argc, char **argv)
-{
-    int                     count = 0;
-    char                    secur[32] = {0};
-    char                    env[32] = {0};
-    int                     opt;
-    iotx_coap_config_t      config;
-    iotx_deviceinfo_t       deviceinfo;
-
-    /**< set device info*/
-    HAL_SetProductKey(IOTX_PRODUCT_KEY);
-    HAL_SetDeviceName(IOTX_DEVICE_NAME);
-    HAL_SetDeviceSecret(IOTX_DEVICE_SECRET);
-    /**< end*/
-    IOT_OpenLog("coap");
-    IOT_SetLogLevel(IOT_LOG_DEBUG);
-
-    HAL_Printf("[COAP-Client]: Enter Coap Client\r\n");
-    while ((opt = getopt(argc, argv, "e:s:lh")) != -1) {
-        switch (opt) {
-            case 's':
-                strncpy(secur, optarg, strlen(optarg));
-                break;
-            case 'e':
-                strncpy(env, optarg, strlen(optarg));
-                break;
-            case 'l':
-                m_coap_client_running = 1;
-                break;
-            case 'h':
-                /* TODO: */
-                break;
-            default:
-                break;
-        }
-    }
-
-    memset(&config, 0x00, sizeof(iotx_coap_config_t));
-    if (0 == strncmp(env, "pre", strlen("pre"))) {
-        if (0 == strncmp(secur, "dtls", strlen("dtls"))) {
-            config.p_url = IOTX_PRE_DTLS_SERVER_URI;
-        } else {
-            config.p_url = IOTX_PRE_NOSEC_SERVER_URI;
-        }
-    } else if (0 == strncmp(env, "online", strlen("online"))) {
-        if (0 == strncmp(secur, "dtls", strlen("dtls"))) {
-            char url[256] = {0};
-            snprintf(url, sizeof(url), IOTX_ONLINE_DTLS_SERVER_URL, IOTX_PRODUCT_KEY);
-            config.p_url = url;
-        } else {
-            HAL_Printf("Online environment must access with DTLS\r\n");
-            IOT_CloseLog();
-            return -1;
-        }
-    }
-
-#ifdef TEST_COAP_DAILY
-    config.p_url = IOTX_DAILY_DTLS_SERVER_URI;
-#endif
-
-    // config.p_url = IOTX_ONLINE_DTLS_SERVER_URL;
-
-    HAL_Printf("will iotx_set_devinfo...\r\n");
-
-    iotx_set_devinfo(&deviceinfo);
-    config.p_devinfo = (iotx_device_info_t*)&deviceinfo;
-    config.wait_time_ms = 3000;
-
-    iotx_coap_context_t *p_ctx = NULL;
-    p_ctx = IOT_CoAP_Init(&config);
-    if (NULL != p_ctx) {
-        IOT_CoAP_DeviceNameAuth(p_ctx);
-        do {
-            count ++;
-            if (count == 11) {
-                iotx_post_data_to_server((void *)p_ctx);
-                count = 1;
-            }
-            IOT_CoAP_Yield(p_ctx);
-        } while (m_coap_client_running);
-
-        IOT_CoAP_Deinit(&p_ctx);
-    } else {
-        HAL_Printf("IoTx CoAP init failed\r\n");
-    }
-
-    IOT_CloseLog();
-    return 0;
-}
-#ifdef RT_USING_FINSH
-#include <finsh.h>
-
-MSH_CMD_EXPORT_ALIAS(ali_coap_main, ali_coap_test, Example: ali_coap_test or ali_mqtt_test loop);
-#endif

+ 0 - 186
samples/coap/coap-example.c

@@ -1,186 +0,0 @@
-/*
- * Copyright (c) 2006-2018 RT-Thread Development Team. All rights reserved.
- * License-Identifier: Apache-2.0
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may
- * not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-#include "rtthread.h"
-
-#include "iot_import.h"
-#include "iot_export.h"
-
-#define IOTX_DAILY_DTLS_SERVER_URI      "coaps://iot-as-coap.alibaba.net:5684"
-#define IOTX_PRE_DTLS_SERVER_URI        "coaps://pre.iot-as-coap.cn-shanghai.aliyuncs.com:5684"
-#define IOTX_PRE_NOSEC_SERVER_URI       "coap://pre.iot-as-coap.cn-shanghai.aliyuncs.com:5683"
-
-#define IOTX_ONLINE_DTLS_SERVER_URL     "coaps://%s.iot-as-coap.cn-shanghai.aliyuncs.com:5684"
-
-static char m_coap_client_running = 0;
-
-static void iotx_response_handler(void *arg, void *p_response)
-{
-    int            len       = 0;
-    unsigned char *p_payload = NULL;
-    iotx_coap_resp_code_t resp_code;
-    IOT_CoAP_GetMessageCode(p_response, &resp_code);
-    IOT_CoAP_GetMessagePayload(p_response, &p_payload, &len);
-    HAL_Printf("[APPL]: Message response code: 0x%x\r\n", resp_code);
-    HAL_Printf("[APPL]: Len: %d, Payload: %s, \r\n", len, p_payload);
-}
-
-#ifdef TEST_COAP_DAILY
-    #define IOTX_PRODUCT_KEY         "zPygj0yP3UF"
-    #define IOTX_DEVICE_NAME         "device_2"
-    #define IOTX_DEVICE_SECRET       "5FQbVOPWNwhEuCvnVcP1Mvyjmvt8ECQi"
-    #define IOTX_DEVICE_ID           "device_2"
-#else
-    #define IOTX_PRODUCT_KEY         "vtkkbrpmxmF"
-    #define IOTX_DEVICE_NAME         "IoTxCoAPTestDev"
-    #define IOTX_DEVICE_SECRET       "Stk4IUErQUBc1tWRWEKWb5ACra4hFDYF"
-    #define IOTX_DEVICE_ID           "IoTxCoAPTestDev.1"
-#endif
-
-static int iotx_set_devinfo(iotx_deviceinfo_t *p_devinfo)
-{
-    if (NULL == p_devinfo) {
-        return IOTX_ERR_INVALID_PARAM;
-    }
-
-    memset(p_devinfo, 0x00, sizeof(iotx_deviceinfo_t));
-    strncpy(p_devinfo->device_id,    IOTX_DEVICE_ID,   IOTX_DEVICE_ID_LEN);
-    strncpy(p_devinfo->product_key,  IOTX_PRODUCT_KEY, IOTX_PRODUCT_KEY_LEN);
-    strncpy(p_devinfo->device_secret, IOTX_DEVICE_SECRET, IOTX_DEVICE_SECRET_LEN);
-    strncpy(p_devinfo->device_name,  IOTX_DEVICE_NAME, IOTX_DEVICE_NAME_LEN);
-
-    fprintf(stderr, "*****The Product Key  : %s *****\r\n", p_devinfo->product_key);
-    fprintf(stderr, "*****The Device Name  : %s *****\r\n", p_devinfo->device_name);
-    //fprintf(stderr, "*****The Device Secret: %s *****\r\n", p_devinfo->device_secret);
-    fprintf(stderr, "*****The Device ID    : %s *****\r\n", p_devinfo->device_id);
-    return IOTX_SUCCESS;
-}
-
-static void iotx_post_data_to_server(void *param)
-{
-    char               path[IOTX_URI_MAX_LEN + 1] = {0};
-    iotx_message_t     message;
-    iotx_deviceinfo_t  devinfo;
-    message.p_payload = (unsigned char *)"{\"name\":\"hello world\"}";
-    message.payload_len = strlen("{\"name\":\"hello world\"}");
-    message.resp_callback = iotx_response_handler;
-    message.msg_type = IOTX_MESSAGE_CON;
-    message.content_type = IOTX_CONTENT_TYPE_JSON;
-    iotx_coap_context_t *p_ctx = (iotx_coap_context_t *)param;
-
-    iotx_set_devinfo(&devinfo);
-    snprintf(path, IOTX_URI_MAX_LEN, "/topic/%s/%s/update/", (char *)devinfo.product_key,
-             (char *)devinfo.device_name);
-
-    IOT_CoAP_SendMessage(p_ctx, path, &message);
-}
-
-static int ali_coap_main(int argc, char **argv)
-{
-    int                     count = 0;
-    char                    secur[32] = {0};
-    char                    env[32] = {0};
-    int                     opt;
-    iotx_coap_config_t      config;
-    iotx_deviceinfo_t       deviceinfo;
-
-    IOT_OpenLog("coap");
-    IOT_SetLogLevel(IOT_LOG_DEBUG);
-
-    HAL_Printf("[COAP-Client]: Enter Coap Client\r\n");
-    while ((opt = getopt(argc, argv, "e:s:lh")) != -1) {
-        switch (opt) {
-            case 's':
-                strncpy(secur, optarg, strlen(optarg));
-                break;
-            case 'e':
-                strncpy(env, optarg, strlen(optarg));
-                break;
-            case 'l':
-                m_coap_client_running = 1;
-                break;
-            case 'h':
-                /* TODO: */
-                break;
-            default:
-                break;
-        }
-    }
-
-    memset(&config, 0x00, sizeof(iotx_coap_config_t));
-    if (0 == strncmp(env, "pre", strlen("pre"))) {
-        if (0 == strncmp(secur, "dtls", strlen("dtls"))) {
-            config.p_url = IOTX_PRE_DTLS_SERVER_URI;
-        } else {
-            config.p_url = IOTX_PRE_NOSEC_SERVER_URI;
-        }
-    } else if (0 == strncmp(env, "online", strlen("online"))) {
-        if (0 == strncmp(secur, "dtls", strlen("dtls"))) {
-            char url[256] = {0};
-            snprintf(url, sizeof(url), IOTX_ONLINE_DTLS_SERVER_URL, IOTX_PRODUCT_KEY);
-            config.p_url = url;
-        } else {
-            HAL_Printf("Online environment must access with DTLS\r\n");
-            IOT_CloseLog();
-            return -1;
-        }
-    }
-
-#ifdef TEST_COAP_DAILY
-    config.p_url = IOTX_DAILY_DTLS_SERVER_URI;
-#endif
-
-    iotx_set_devinfo(&deviceinfo);
-    config.p_devinfo = &deviceinfo;
-    config.wait_time_ms = 5000;
-
-    /* Convert uppercase letters in host to lowercase */
-	// rt_kprintf("host: %s\r\n", strlwr((char*)mqtt_params.host));
-
-    iotx_coap_context_t *p_ctx = NULL;
-    p_ctx = IOT_CoAP_Init(&config);
-    if (NULL != p_ctx) {
-        IOT_CoAP_DeviceNameAuth(p_ctx);
-        do {
-            count ++;
-            if (count == 11) {
-                iotx_post_data_to_server((void *)p_ctx);
-                count = 1;
-            }
-            IOT_CoAP_Yield(p_ctx);
-        } while (m_coap_client_running);
-
-        IOT_CoAP_Deinit(&p_ctx);
-    } else {
-        HAL_Printf("IoTx CoAP init failed\r\n");
-    }
-
-    IOT_CloseLog();
-    return 0;
-}
-#ifdef RT_USING_FINSH
-#include <finsh.h>
-
-MSH_CMD_EXPORT_ALIAS(ali_coap_main, ali_coap_test, Example: ali_coap_test or ali_mqtt_test loop);
-#endif

+ 19 - 9
samples/mqtt/mqtt-example.c

@@ -96,6 +96,21 @@ static char   *user_param = NULL;
 static void   *pclient;
 
 static uint8_t is_running = 0;
+		
+static char* strlwr(char *str)
+ {
+    if(str == NULL)
+        return NULL;
+         
+    char *p = str;
+    while (*p != '\0')
+    {
+        if(*p >= 'A' && *p <= 'Z')
+            *p = (*p) + 0x20;
+        p++;
+    }
+    return str;
+}
 
 static void event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
 {
@@ -193,7 +208,7 @@ static void _demo_message_arrive(void *pcontext, void *pclient, iotx_mqtt_event_
 }
 
 #ifndef MQTT_ID2_AUTH
-static int mqtt_client(void)
+static void mqtt_client(void)
 {
     int rc = 0;
 
@@ -327,15 +342,13 @@ do_exit:
     is_running = 0;
 
     EXAMPLE_TRACE("out of sample!");
-
-    return rc;
 }
 #endif /* MQTT_ID2_AUTH */
 
 #ifdef MQTT_ID2_AUTH
 #include "tfs.h"
 static char __device_id2[TFS_ID2_LEN + 1];
-static int mqtt_client_secure()
+static void mqtt_client_secure()
 {
     int rc = 0, msg_len, cnt = 0;
     void *pclient;
@@ -488,10 +501,7 @@ do_exit:
     IOT_DumpMemoryStats(IOT_LOG_DEBUG);
     IOT_CloseLog();
 
-    EXAMPLE_TRACE("out of sample!");    
-
-    return rc;
-
+    EXAMPLE_TRACE("out of sample!");
 }
 #endif /* MQTT_ID2_AUTH*/
 
@@ -640,7 +650,7 @@ static int ali_mqtt_main(int argc, char **argv)
     }
 
 #ifdef IOTX_PRJ_VERSION
-    EXAMPLE_TRACE("iotkit-embedded sdk version: %s", IOTX_PRJ_VERSION);
+    HAL_Printf("iotkit-embedded sdk version: %s\n", IOTX_PRJ_VERSION);
 #endif
 
     HAL_SetProductKey(PRODUCT_KEY);

+ 0 - 207
samples/ota/ota_coap-example.c

@@ -1,207 +0,0 @@
-/*
- * Copyright (c) 2006-2018 RT-Thread Development Team. All rights reserved.
- * License-Identifier: Apache-2.0
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may
- * not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <stdarg.h>
-
-#include "iot_import.h"
-#include "iot_export.h"
-
-#define IOTX_PRODUCT_KEY        "stMRPCR0yQu"
-#define IOTX_DEVICE_NAME        "xikan_ota"
-#define IOTX_DEVICE_SECRET      "XZjhoJclBJpvcEoa8eLYSxnb3ksRVd3W"
-#define IOTX_DEVICE_ID          "stMRPCR0yQu.xikan_ota"
-
-//#define IOTX_PRE_DTLS_SERVER_URI "coaps://pre.iot-as-coap.cn-shanghai.aliyuncs.com:5684"
-#define IOTX_PRE_NOSEC_SERVER_URI "coap://pre.iot-as-coap.cn-shanghai.aliyuncs.com:5683"
-
-
-//#define IOTX_PRE_NOSEC_SERVER_URI "coap://iot-as-coap.alibaba.net:5683"
-
-#define EXAMPLE_TRACE(fmt, ...)  \
-    do { \
-        HAL_Printf("%s|%03d :: ", __func__, __LINE__); \
-        HAL_Printf(fmt, ##__VA_ARGS__); \
-        HAL_Printf("%s", "\r\n"); \
-    } while(0)
-
-
-#define OTA_BUF_LEN        (5000)
-
-extern int iotx_get_well_known(iotx_coap_context_t *p_context);
-
-static int fetch_ota(void *h_ota, void *h_coap)
-{
-    int rc = 1;
-    FILE *fp;
-    uint32_t last_percent = 0, percent = 0;
-    char version[128], md5sum[33];
-    int32_t len, size_downloaded, size_file;
-    char buf_ota[OTA_BUF_LEN];
-    int32_t firmware_valid;
-
-    if (NULL == (fp = fopen("ota.bin", "wb+"))) {
-        EXAMPLE_TRACE("open file failed");
-        return -1;
-    }
-
-    do {
-        len = IOT_OTA_FetchYield(h_ota, buf_ota, OTA_BUF_LEN, 1);
-        if (len > 0) {
-            if (1 != fwrite(buf_ota, len, 1, fp)) {
-                EXAMPLE_TRACE("write data to file failed");
-                rc = -1;
-                break;
-            }
-        }else {
-            IOT_OTA_ReportProgress(h_ota, IOT_OTAP_FETCH_FAILED, NULL);
-            EXAMPLE_TRACE("ota fetch fail");
-        }
-
-        /* get OTA information */
-        IOT_OTA_Ioctl(h_ota, IOT_OTAG_FETCHED_SIZE, &size_downloaded, 4);
-        IOT_OTA_Ioctl(h_ota, IOT_OTAG_FILE_SIZE, &size_file, 4);
-        IOT_OTA_Ioctl(h_ota, IOT_OTAG_MD5SUM, md5sum, 33);
-        IOT_OTA_Ioctl(h_ota, IOT_OTAG_VERSION, version, 128);
-
-        last_percent = percent;
-        percent = (size_downloaded * 100) / size_file;
-        if (percent - last_percent > 0) {
-            IOT_OTA_ReportProgress(h_ota, percent, NULL);
-            IOT_OTA_ReportProgress(h_ota, percent, "hello");
-        }
-
-        IOT_CoAP_Yield(h_coap);
-
-    } while (!IOT_OTA_IsFetchFinish(h_ota));
-
-    while (1 == rc) {
-        IOT_OTA_Ioctl(h_ota, IOT_OTAG_CHECK_FIRMWARE, &firmware_valid, 4);
-        if (0 == firmware_valid) {
-            EXAMPLE_TRACE("The firmware is invalid");
-            rc = -1;
-            break;
-        } else {
-            EXAMPLE_TRACE("The firmware is valid");
-            rc = -1;
-            break;
-        }
-    }
-
-    if (NULL != fp) {
-        fclose(fp);
-    }
-
-    return rc;
-}
-
-
-//-1, fetch failed
-//0, no any ota firmware
-//1, fetch successfully
-static int try_fetch_ota(void *h_ota, void *h_coap)
-{
-    if (IOT_OTA_IsFetching(h_ota)) {
-        return fetch_ota(h_ota, h_coap);
-    }
-
-    return 0;
-}
-
-static int iotx_set_devinfo(iotx_deviceinfo_t *p_devinfo)
-{
-    if (NULL == p_devinfo) {
-        return IOTX_ERR_INVALID_PARAM;
-    }
-
-    memset(p_devinfo, 0x00, sizeof(iotx_deviceinfo_t));
-    /**< get device info*/
-    HAL_GetProductKey(p_devinfo->product_key);
-    HAL_GetDeviceSecret(p_devinfo->device_secret);
-    HAL_GetDeviceName(p_devinfo->device_name);
-    HAL_GetDeviceID(p_devinfo->device_id);
-    /**< end*/
-
-    return IOTX_SUCCESS;
-}
-
-
-static int ali_coap_main(int argc, char **argv)
-{
-    int rc = 0;
-    void *h_ota = NULL;
-    iotx_coap_config_t config;
-    iotx_deviceinfo_t deviceinfo;
-    iotx_coap_context_t *h_coap = NULL;
-
-    IOT_OpenLog("coap-ota");
-    IOT_SetLogLevel(IOT_LOG_DEBUG);
-
-    /**< set device info */
-    HAL_SetProductKey(IOTX_PRODUCT_KEY);
-    HAL_SetDeviceName(IOTX_DEVICE_NAME);
-    HAL_SetDeviceSecret(IOTX_DEVICE_SECRET);
-    /**< end */
-    iotx_set_devinfo(&deviceinfo);
-
-    memset(&config, 0x00, sizeof(iotx_coap_config_t));
-    config.p_devinfo = (iotx_deviceinfo_t *)&deviceinfo;
-    config.p_url = IOTX_PRE_NOSEC_SERVER_URI;
-    h_coap = IOT_CoAP_Init(&config);
-    if (NULL == h_coap) {
-        rc = -1;
-        EXAMPLE_TRACE("initialize CoAP failed");
-        return -1;
-    }
-
-    IOT_CoAP_DeviceNameAuth(h_coap);
-
-    h_ota = IOT_OTA_Init(deviceinfo.product_key, deviceinfo.device_name, h_coap);
-    if (NULL == h_ota) {
-        rc = -1;
-        EXAMPLE_TRACE("initialize OTA failed");
-        goto do_exit;
-    }
-
-    int ota_code = 0;
-    do {
-
-        IOT_CoAP_Yield(h_coap);
-
-        IOT_OTA_ReportVersion(h_ota, "iotx_ver_1.0.0");
-
-        HAL_SleepMs(2000);
-
-        ota_code = try_fetch_ota(h_ota, h_coap);
-
-    } while (1 != ota_code);
-
-    EXAMPLE_TRACE("OTA success");
-
-do_exit:
-    if (NULL != h_ota) {
-        IOT_OTA_Deinit(h_ota);
-    }
-    if (NULL != h_coap) {
-        IOT_CoAP_Deinit(&h_coap);
-    }
-
-    return rc;
-}
-

+ 22 - 20
samples/ota/ota_mqtt-example.c

@@ -65,6 +65,8 @@ static char g_product_key[PRODUCT_KEY_LEN + 1];
 static char g_device_name[DEVICE_NAME_LEN + 1];
 static char g_device_secret[DEVICE_SECRET_LEN + 1];
 
+static char g_ota_version[64] = "iotx_ver_1.0.0";
+
 /* These are pre-defined topics */
 #define TOPIC_UPDATE            "/"PRODUCT_KEY"/"DEVICE_NAME"/update"
 #define TOPIC_ERROR             "/"PRODUCT_KEY"/"DEVICE_NAME"/update/error"
@@ -83,6 +85,21 @@ static char g_device_secret[DEVICE_SECRET_LEN + 1];
 static int      user_argc;
 static uint8_t is_running = 0;
 
+static char* strlwr(char *str)
+ {
+    if(str == NULL)
+        return NULL;
+         
+    char *p = str;
+    while (*p != '\0')
+    {
+        if(*p >= 'A' && *p <= 'Z')
+            *p = (*p) + 0x20;
+        p++;
+    }
+    return str;
+}
+
 static void event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
 {
     uintptr_t packet_id = (uintptr_t)msg->msg;
@@ -209,7 +226,7 @@ static int mqtt_client(void)
     mqtt_params.handle_event.pcontext = NULL;
 
     /* Convert uppercase letters in host to lowercase */
-	rt_kprintf("host: %s\r\n", strlwr((char*)mqtt_params.host));
+	EXAMPLE_TRACE("host: %s", strlwr((char*)mqtt_params.host));
 
     /* Construct a MQTT client with specify parameter */
     pclient = IOT_MQTT_Construct(&mqtt_params);
@@ -225,13 +242,12 @@ static int mqtt_client(void)
         goto do_exit;
     }
 
-    if (0 != IOT_OTA_ReportVersion(h_ota, "iotx_ver_1.0.0")) {
+    if (0 != IOT_OTA_ReportVersion(h_ota, g_ota_version)) {
         rc = -1;
         EXAMPLE_TRACE("report OTA version failed");
         goto do_exit;
     }
 
-
     HAL_SleepMs(1000);
 
     do {
@@ -279,19 +295,8 @@ static int mqtt_client(void)
             } else {
                 EXAMPLE_TRACE("The firmware is valid!  Download firmware successfully.");
 
-                /* For test, here report new firmware version. 
-                 * In actual application, please report the new version number after the firmware is successfully updated. 
-                 */
-                if (0 != IOT_OTA_ReportVersion(h_ota, version)) {
-                    rc = -1;
-                    EXAMPLE_TRACE("report OTA version failed");
-                    goto do_exit;
-                }
-
-                EXAMPLE_TRACE("OTA FW version: %s", version);
-
-                /* handle the MQTT packet received from TCP or SSL connection */
-                IOT_MQTT_Yield(pclient, 200);
+                snprintf(g_ota_version, sizeof(g_ota_version), "%s", version);
+                EXAMPLE_TRACE("OTA FW version: %s", g_ota_version);
 
                 goto do_exit;
             }
@@ -299,8 +304,6 @@ static int mqtt_client(void)
         HAL_SleepMs(2000);
     } while (is_running);
 
-    HAL_SleepMs(200);
-
 do_exit:
 
     if (NULL != h_ota) {
@@ -356,7 +359,6 @@ static int ali_ota_main(int argc, char **argv)
                 return 0;
             }
             is_running = 0;
-            // stop ota test
             return 0;
         }
         else
@@ -372,7 +374,7 @@ static int ali_ota_main(int argc, char **argv)
     }
 
 #ifdef IOTX_PRJ_VERSION
-    EXAMPLE_TRACE("iotkit-embedded sdk version: %s", IOTX_PRJ_VERSION);
+    HAL_Printf("iotkit-embedded sdk version: %s\n", IOTX_PRJ_VERSION);
 #endif
 
     /**< set device info*/