Browse Source

【同步】阿里 c-sdk-v3.0.1-269691d1b45b15fb9045a8eb178efa54b262aca1c-sdk.git 版本更新

Signed-off-by: Murphy <d2014zjt@163.com>
Murphy 6 years ago
parent
commit
b5e4655246
32 changed files with 320 additions and 100 deletions
  1. 4 0
      README.md
  2. 25 7
      iotkit-embedded/src/coap/server/CoAPResource.c
  3. 1 1
      iotkit-embedded/src/coap/server/CoAPResource.h
  4. 32 4
      iotkit-embedded/src/coap/server/CoAPServer.c
  5. 66 3
      iotkit-embedded/src/dev_bind/impl/awss_notify.c
  6. 2 2
      iotkit-embedded/src/dev_bind/impl/awss_reset/awss_dev_reset.c
  7. 0 3
      iotkit-embedded/src/dev_bind/impl/iot_import_awss.h
  8. 7 0
      iotkit-embedded/src/dev_model/alcs/alcs_api.c
  9. 4 3
      iotkit-embedded/src/dev_model/alcs/alcs_server.c
  10. 5 0
      iotkit-embedded/src/dev_model/dm_wrapper.h
  11. 8 5
      iotkit-embedded/src/dev_model/examples/linkkit_example_gateway.c
  12. 7 6
      iotkit-embedded/src/dev_model/examples/linkkit_example_solo.c
  13. 5 0
      iotkit-embedded/src/dev_model/impl_linkkit.c
  14. 8 1
      iotkit-embedded/src/dev_model/iotx_cm_mqtt.c
  15. 1 1
      iotkit-embedded/src/dev_sign/dev_sign_config.h
  16. 19 2
      iotkit-embedded/src/dev_sign/dev_sign_mqtt.c
  17. 9 6
      iotkit-embedded/src/http/examples/http_example.c
  18. 1 1
      iotkit-embedded/src/http2/iotx_http2.c
  19. 15 1
      iotkit-embedded/src/infra/infra_compat.c
  20. 2 0
      iotkit-embedded/src/infra/infra_compat.h
  21. 1 34
      iotkit-embedded/src/infra/infra_net.c
  22. 11 2
      iotkit-embedded/src/mqtt/impl/iotx_mqtt_client.c
  23. 35 7
      iotkit-embedded/src/mqtt/mqtt_api.c
  24. 2 2
      iotkit-embedded/src/mqtt/mqtt_api.h
  25. 1 0
      iotkit-embedded/src/mqtt/mqtt_wrapper.h
  26. 1 0
      iotkit-embedded/src/wifi_provision/dev_ap/dev_ap_wrapper.h
  27. 5 0
      iotkit-embedded/src/wifi_provision/frameworks/zconfig_vendor_common.c
  28. 4 1
      iotkit-embedded/src/wifi_provision/p2p/awss_wps.c
  29. 5 0
      iotkit-embedded/src/wifi_provision/smartconfig/awss_smartconfig.c
  30. 4 4
      iotkit-embedded/tools/misc/xtrc_wrapper_rules
  31. 13 1
      iotkit-embedded/wrappers/os/ubuntu/HAL_TCP_linux.c
  32. 17 3
      iotkit-embedded/wrappers/tls/HAL_TLS_mbedtls.c

+ 4 - 0
README.md

@@ -1,5 +1,9 @@
 # Ali IoTKit
 # Ali IoTKit
 
 
+```
+Ali IoTKit 软件包已经同步更新
+```
+
 ## 1. 介绍
 ## 1. 介绍
 
 
 **ali-iotkit** 是 RT-Thread 移植的用于连接阿里云 IoT 平台的软件包。基础 SDK 是阿里提供的 [**iotkit-embedded C-SDK**](https://github.com/aliyun/iotkit-embedded)。
 **ali-iotkit** 是 RT-Thread 移植的用于连接阿里云 IoT 平台的软件包。基础 SDK 是阿里提供的 [**iotkit-embedded C-SDK**](https://github.com/aliyun/iotkit-embedded)。

+ 25 - 7
iotkit-embedded/src/coap/server/CoAPResource.c

@@ -152,7 +152,7 @@ int CoAPResource_register(CoAPContext *context, const char *path,
                 break;
                 break;
             }
             }
         } else if (type == PATH_FILTER && node->path_type == PATH_FILTER) {
         } else if (type == PATH_FILTER && node->path_type == PATH_FILTER) {
-            if (strlen(path) == strlen(node->filter_path) && 0 == strncpy((char *)path, node->filter_path, strlen(path))) {
+            if (0 == strncmp((char *)path, node->filter_path, strlen(path))) {
                 /*Alread exist, re-write it*/
                 /*Alread exist, re-write it*/
                 COAP_INFO("CoAPResource_register:Alread exist");
                 COAP_INFO("CoAPResource_register:Alread exist");
                 exist = 1;
                 exist = 1;
@@ -189,6 +189,22 @@ int CoAPResource_unregister(CoAPContext *context, const char *path)
     return COAP_ERROR_UNSUPPORTED;
     return COAP_ERROR_UNSUPPORTED;
 }
 }
 
 
+int CoAPResource_topicFilterMatch(const char *filter, const char *topic)
+{
+    if (filter == NULL || topic == NULL) {
+        return -1;
+    }
+    if (strncmp(filter, topic, strlen(filter) - 1) == 0) {
+        if (strlen(topic) > strlen(filter) - 1) {
+            const char *more = topic + (strlen(filter) - 1);
+            if (strstr(more, "/") == NULL) {
+                return 0;
+            }
+        }
+    }
+    return -1;
+}
+
 CoAPResource *CoAPResourceByPath_get(CoAPContext *context, const char *path)
 CoAPResource *CoAPResourceByPath_get(CoAPContext *context, const char *path)
 {
 {
     char path_calc[COAP_PATH_DEFAULT_SUM_LEN] = {0};
     char path_calc[COAP_PATH_DEFAULT_SUM_LEN] = {0};
@@ -210,13 +226,15 @@ CoAPResource *CoAPResourceByPath_get(CoAPContext *context, const char *path)
             COAP_DEBUG("Found the resource: %s", path);
             COAP_DEBUG("Found the resource: %s", path);
             return node;
             return node;
         }
         }
-        if (node->path_type == PATH_FILTER && strlen(node->filter_path) > 0
-            && 0 == strncmp(path, node->filter_path, strlen(node->filter_path) - 1)) {
-            HAL_MutexUnlock(ctx->resource.list_mutex);
-            COAP_DEBUG("Found the resource: %s", path);
-            return node;
+    }
+    
+    list_for_each_entry(node, &ctx->resource.list, reslist, CoAPResource) {
+        if (node->path_type == PATH_FILTER && strlen(node->filter_path) > 0) {
+            if (CoAPResource_topicFilterMatch(node->filter_path, path) == 0) {
+                HAL_MutexUnlock(ctx->resource.list_mutex);
+                return node;
+            }
         }
         }
-
     }
     }
     HAL_MutexUnlock(ctx->resource.list_mutex);
     HAL_MutexUnlock(ctx->resource.list_mutex);
 
 

+ 1 - 1
iotkit-embedded/src/coap/server/CoAPResource.h

@@ -38,7 +38,7 @@ int CoAPResource_register(CoAPContext *context, const char *path,
                           unsigned int maxage, CoAPRecvMsgHandler callback);
                           unsigned int maxage, CoAPRecvMsgHandler callback);
 
 
 CoAPResource *CoAPResourceByPath_get(CoAPContext *context, const char *path);
 CoAPResource *CoAPResourceByPath_get(CoAPContext *context, const char *path);
-
+int CoAPResource_topicFilterMatch(const char *filter, const char *topic);
 int CoAPResource_deinit(CoAPContext *context);
 int CoAPResource_deinit(CoAPContext *context);
 
 
 #ifdef __cplusplus
 #ifdef __cplusplus

+ 32 - 4
iotkit-embedded/src/coap/server/CoAPServer.c

@@ -102,7 +102,7 @@ void CoAPServer_add_timer(void (*on_timer)(void *))
 }
 }
 
 
 
 
-
+static void *coap_init_mutex = NULL;
 CoAPContext *CoAPServer_init()
 CoAPContext *CoAPServer_init()
 {
 {
     CoAPInitParam param = {0};
     CoAPInitParam param = {0};
@@ -111,6 +111,15 @@ CoAPContext *CoAPServer_init()
     hal_os_thread_param_t task_parms = {0};
     hal_os_thread_param_t task_parms = {0};
 #endif
 #endif
 
 
+    if (NULL == coap_init_mutex) {
+        coap_init_mutex = HAL_MutexCreate();
+
+        if (NULL == coap_init_mutex) {
+            return NULL;
+        }
+    }
+    HAL_MutexLock(coap_init_mutex);
+
     if (NULL == g_context) {
     if (NULL == g_context) {
         param.appdata = NULL;
         param.appdata = NULL;
         param.group = "224.0.1.187";
         param.group = "224.0.1.187";
@@ -125,6 +134,7 @@ CoAPContext *CoAPServer_init()
         g_semphore  = HAL_SemaphoreCreate();
         g_semphore  = HAL_SemaphoreCreate();
         if (NULL == g_semphore) {
         if (NULL == g_semphore) {
             COAP_ERR("Semaphore Create failed");
             COAP_ERR("Semaphore Create failed");
+            HAL_MutexUnlock(coap_init_mutex);
             return NULL;
             return NULL;
         }
         }
 
 
@@ -133,6 +143,7 @@ CoAPContext *CoAPServer_init()
             COAP_ERR("coap_yield_mutex Create failed");
             COAP_ERR("coap_yield_mutex Create failed");
             HAL_SemaphoreDestroy(g_semphore);
             HAL_SemaphoreDestroy(g_semphore);
             g_semphore = NULL;
             g_semphore = NULL;
+            HAL_MutexUnlock(coap_init_mutex);
             return NULL;
             return NULL;
         }
         }
 #endif
 #endif
@@ -146,6 +157,7 @@ CoAPContext *CoAPServer_init()
             coap_yield_mutex = NULL;
             coap_yield_mutex = NULL;
 #endif
 #endif
             COAP_ERR("CoAP Context Create failed");
             COAP_ERR("CoAP Context Create failed");
+            HAL_MutexUnlock(coap_init_mutex);
             return NULL;
             return NULL;
         }
         }
 #ifdef COAP_SERV_MULTITHREAD
 #ifdef COAP_SERV_MULTITHREAD
@@ -159,6 +171,7 @@ CoAPContext *CoAPServer_init()
         COAP_INFO("The CoAP Server already init");
         COAP_INFO("The CoAP Server already init");
     }
     }
 
 
+    HAL_MutexUnlock(coap_init_mutex);
     return (CoAPContext *)g_context;
     return (CoAPContext *)g_context;
 }
 }
 
 
@@ -169,6 +182,13 @@ void CoAPServer_deinit(CoAPContext *context)
         return;
         return;
     }
     }
 
 
+    if (NULL == coap_init_mutex) {
+        COAP_ERR("CoAP init mutex is NULL");
+        return;
+    }
+
+    HAL_MutexLock(coap_init_mutex);
+
     COAP_INFO("CoAP Server deinit");
     COAP_INFO("CoAP Server deinit");
     g_coap_running = 0;
     g_coap_running = 0;
 
 
@@ -188,6 +208,10 @@ void CoAPServer_deinit(CoAPContext *context)
         CoAPContext_free(context);
         CoAPContext_free(context);
         g_context = NULL;
         g_context = NULL;
     }
     }
+
+    HAL_MutexUnlock(coap_init_mutex);
+    HAL_MutexDestroy(coap_init_mutex);
+    coap_init_mutex = NULL;
 }
 }
 
 
 int CoAPServer_register(CoAPContext *context, const char *uri, CoAPRecvMsgHandler callback)
 int CoAPServer_register(CoAPContext *context, const char *uri, CoAPRecvMsgHandler callback)
@@ -225,7 +249,9 @@ int CoAPServerMultiCast_send(CoAPContext *context, NetworkAddr *remote, const ch
     CoAPServerPath_2_option((char *)uri, &message);
     CoAPServerPath_2_option((char *)uri, &message);
     CoAPUintOption_add(&message, COAP_OPTION_CONTENT_FORMAT, COAP_CT_APP_JSON);
     CoAPUintOption_add(&message, COAP_OPTION_CONTENT_FORMAT, COAP_CT_APP_JSON);
     CoAPMessagePayload_set(&message, buff, len);
     CoAPMessagePayload_set(&message, buff, len);
-    if (msgid) *msgid = message.header.msgid;
+    if (msgid) {
+        *msgid = message.header.msgid;
+    }
     ret = CoAPMessage_send(context, remote, &message);
     ret = CoAPMessage_send(context, remote, &message);
 
 
     CoAPMessage_destory(&message);
     CoAPMessage_destory(&message);
@@ -247,12 +273,14 @@ int CoAPServerResp_send(CoAPContext *context, NetworkAddr *remote, unsigned char
     }
     }
 
 
     CoAPMessage_init(&response);
     CoAPMessage_init(&response);
-    CoAPMessageType_set(&response, qos == 0 ? COAP_MESSAGE_TYPE_NON :COAP_MESSAGE_TYPE_CON);
+    CoAPMessageType_set(&response, qos == 0 ? COAP_MESSAGE_TYPE_NON : COAP_MESSAGE_TYPE_CON);
     CoAPMessageCode_set(&response, COAP_MSG_CODE_205_CONTENT);
     CoAPMessageCode_set(&response, COAP_MSG_CODE_205_CONTENT);
     CoAPMessageId_set(&response, request->header.msgid);
     CoAPMessageId_set(&response, request->header.msgid);
     CoAPMessageToken_set(&response, request->token, request->header.tokenlen);
     CoAPMessageToken_set(&response, request->token, request->header.tokenlen);
     CoAPMessageHandler_set(&response, callback);
     CoAPMessageHandler_set(&response, callback);
-    if (msgid) *msgid = response.header.msgid;
+    if (msgid) {
+        *msgid = response.header.msgid;
+    }
 
 
     ret = CoAPUintOption_get(request, COAP_OPTION_OBSERVE, &observe);
     ret = CoAPUintOption_get(request, COAP_OPTION_OBSERVE, &observe);
     if (COAP_SUCCESS == ret && 0 == observe) {
     if (COAP_SUCCESS == ret && 0 == observe) {

+ 66 - 3
iotkit-embedded/src/dev_bind/impl/awss_notify.c

@@ -161,6 +161,69 @@ static int awss_notify_response(int type, int result, void *message)
     return awss_notify_resp[type];
     return awss_notify_resp[type];
 }
 }
 
 
+/**
+ * @brief alternate bcast addr
+ *
+ * @param bcast_addr[out] bcast addr output
+ * @param mask_level 0 ~ 4(255), 1 ~ 3(255), 2 ~ 2(255), 3 ~ 1(255), others ~ invalid,
+ */
+static int awss_get_broadcast_addr(platform_netaddr_t *bcast_addr)
+{
+    static uint8_t mask_level = 3;
+    char ip[20] = {0};
+    uint8_t level = 0;
+
+    if (bcast_addr == NULL) {
+        return -1;
+    }
+
+    /* update mask_level */
+    if (++mask_level >= 4) {
+        mask_level = 0;
+    }
+
+    /* setup port */
+    bcast_addr->port = AWSS_NOTIFY_PORT;
+
+    /* setup ip */
+    HAL_Wifi_Get_IP(ip, NULL);
+
+    if (ip[0] != '\0' && mask_level != 0) {
+        uint8_t i = 0;
+        for (i=0; i<strlen(ip); i++) {
+            bcast_addr->host[i] = ip[i];
+            if (ip[i] == '.') {
+                if (++level == mask_level) {
+                    break;
+                }
+            }
+        }
+
+        if (mask_level == 1) {
+            if (i + strlen("255.255.255") < 16) {
+                memcpy(bcast_addr->host + strlen(bcast_addr->host), "255.255.255", strlen("255.255.255"));
+                return 0;
+            }
+        }
+        else if (mask_level == 2) {
+            if (i + strlen("255.255") < 16) {
+                memcpy(bcast_addr->host + strlen(bcast_addr->host), "255.255", strlen("255.255"));
+                return 0;
+            }
+        }
+        else if (mask_level == 3) {
+            if (i + strlen("255") < 16) {
+                memcpy(bcast_addr->host + strlen(bcast_addr->host), "255", strlen("255"));
+                return 0;
+            }
+        }
+    }
+
+    memcpy(bcast_addr->host, AWSS_NOTIFY_HOST, strlen(AWSS_NOTIFY_HOST));
+    return 0;
+}
+
+
 int awss_notify_dev_info(int type, int count)
 int awss_notify_dev_info(int type, int count)
 {
 {
     char *buf = NULL;
     char *buf = NULL;
@@ -195,8 +258,8 @@ int awss_notify_dev_info(int type, int count)
         }
         }
 
 
         memset(&notify_sa, 0, sizeof(notify_sa));
         memset(&notify_sa, 0, sizeof(notify_sa));
-        memcpy(notify_sa.host, AWSS_NOTIFY_HOST, strlen(AWSS_NOTIFY_HOST));
-        notify_sa.port = AWSS_NOTIFY_PORT;
+        awss_get_broadcast_addr(&notify_sa);
+        awss_info("bcast ip = %s\n", notify_sa.host);
 
 
         awss_build_dev_info(type, dev_info, DEV_INFO_LEN_MAX);
         awss_build_dev_info(type, dev_info, DEV_INFO_LEN_MAX);
 
 
@@ -273,7 +336,7 @@ static int awss_process_get_devinfo()
         memset(req_msg_id, 0, sizeof(req_msg_id));
         memset(req_msg_id, 0, sizeof(req_msg_id));
 
 
         if(id_len > MSG_REQ_ID_LEN) {
         if(id_len > MSG_REQ_ID_LEN) {
-            goto GET_DEV_INFO_ERR;     
+            goto GET_DEV_INFO_ERR;
         }
         }
         memcpy(req_msg_id, id, id_len);
         memcpy(req_msg_id, id, id_len);
 
 

+ 2 - 2
iotkit-embedded/src/dev_bind/impl/awss_reset/awss_dev_reset.c

@@ -101,8 +101,8 @@ int awss_report_reset_to_cloud()
 
 
         HAL_Snprintf(topic, topic_len, TOPIC_RESET_REPORT_REPLY, pk, dn);
         HAL_Snprintf(topic, topic_len, TOPIC_RESET_REPORT_REPLY, pk, dn);
 
 
-        ret = IOT_MQTT_Subscribe_Sync(NULL, topic, IOTX_MQTT_QOS0,
-                                      (iotx_mqtt_event_handle_func_fpt)awss_report_reset_reply, NULL, 1000);
+        ret = IOT_MQTT_Subscribe(NULL, topic, IOTX_MQTT_QOS0,
+                                      (iotx_mqtt_event_handle_func_fpt)awss_report_reset_reply, NULL);
         if (ret < 0) {
         if (ret < 0) {
             goto REPORT_RST_ERR;
             goto REPORT_RST_ERR;
         }
         }

+ 0 - 3
iotkit-embedded/src/dev_bind/impl/iot_import_awss.h

@@ -12,9 +12,6 @@
 extern "C" {
 extern "C" {
 #endif
 #endif
 
 
-#ifndef 
-  #define 
-#endif
 #ifndef _IN_ 
 #ifndef _IN_ 
   #define _IN_ 
   #define _IN_ 
 #endif
 #endif

+ 7 - 0
iotkit-embedded/src/dev_model/alcs/alcs_api.c

@@ -257,6 +257,10 @@ void alcs_auth_deinit(void)
 #ifdef SUPPORT_MULTI_DEVICES
 #ifdef SUPPORT_MULTI_DEVICES
     device_auth_list *node = NULL, *next = NULL;
     device_auth_list *node = NULL, *next = NULL;
 #endif
 #endif
+    if (is_inited == 0) {
+        return;
+    }
+    is_inited = 0;
 
 
     alcs_resource_cb_deinit();
     alcs_resource_cb_deinit();
     alcs_auth_list_deinit();
     alcs_auth_list_deinit();
@@ -265,11 +269,14 @@ void alcs_auth_deinit(void)
     list_for_each_entry_safe(node, next, &device_list, lst, device_auth_list) {
     list_for_each_entry_safe(node, next, &device_list, lst, device_auth_list) {
         if (node->lst_auth.list_mutex) {
         if (node->lst_auth.list_mutex) {
             HAL_MutexDestroy(node->lst_auth.list_mutex);
             HAL_MutexDestroy(node->lst_auth.list_mutex);
+            node->lst_auth.list_mutex = NULL;
         }
         }
     }
     }
 #else
 #else
     if (_device.lst_auth.list_mutex) {
     if (_device.lst_auth.list_mutex) {
         HAL_MutexDestroy(_device.lst_auth.list_mutex);
         HAL_MutexDestroy(_device.lst_auth.list_mutex);
+        _device.lst_auth.list_mutex = NULL;
+
     }
     }
 #endif
 #endif
 }
 }

+ 4 - 3
iotkit-embedded/src/dev_model/alcs/alcs_server.c

@@ -350,13 +350,14 @@ static int add_svr_key(CoAPContext *ctx, const char *keyprefix, const char *secr
         HAL_MutexUnlock(lst->list_mutex);
         HAL_MutexUnlock(lst->list_mutex);
         return COAP_ERROR_MALLOC;
         return COAP_ERROR_MALLOC;
     }
     }
-
+    memset(item, 0, sizeof(svr_key_item));
     item->keyInfo.secret = (char *) coap_malloc(strlen(secret) + 1);
     item->keyInfo.secret = (char *) coap_malloc(strlen(secret) + 1);
     if (!item->keyInfo.secret) {
     if (!item->keyInfo.secret) {
         HAL_MutexUnlock(lst->list_mutex);
         HAL_MutexUnlock(lst->list_mutex);
         coap_free(item);
         coap_free(item);
         return COAP_ERROR_MALLOC;
         return COAP_ERROR_MALLOC;
     }
     }
+    memset(item->keyInfo.secret, 0, strlen(secret) + 1);
     strcpy(item->keyInfo.secret, secret);
     strcpy(item->keyInfo.secret, secret);
     memcpy(item->keyInfo.keyprefix, keyprefix, KEYPREFIX_LEN);
     memcpy(item->keyInfo.keyprefix, keyprefix, KEYPREFIX_LEN);
     item->keyInfo.priority = priority;
     item->keyInfo.priority = priority;
@@ -464,7 +465,7 @@ static secure_resource_cb_item *get_resource_by_path(const char *path)
                 return node;
                 return node;
             }
             }
         } else if (strlen(node->filter_path) > 0) {
         } else if (strlen(node->filter_path) > 0) {
-            if (strncmp(node->filter_path, path, strlen(node->filter_path) - 1) == 0) {
+            if (CoAPResource_topicFilterMatch(node->filter_path, path) == 0) {
                 return node;
                 return node;
             }
             }
         }
         }
@@ -541,7 +542,7 @@ int alcs_resource_register_secure(CoAPContext *context, const char *pk, const ch
                 dup = 1;
                 dup = 1;
             }
             }
         } else if (item->path_type == PATH_FILTER && node->path_type == PATH_FILTER) {
         } else if (item->path_type == PATH_FILTER && node->path_type == PATH_FILTER) {
-            if (strncpy(node->filter_path, item->filter_path, strlen(item->filter_path)) == 0) {
+            if (strncmp(node->filter_path, path, strlen(path)) == 0) {
                 dup = 1;
                 dup = 1;
             }
             }
         }
         }

+ 5 - 0
iotkit-embedded/src/dev_model/dm_wrapper.h

@@ -9,6 +9,11 @@ int HAL_GetProductSecret(char product_secret[IOTX_PRODUCT_SECRET_LEN + 1]);
 int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN + 1]);
 int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN + 1]);
 int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN + 1]);
 int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN + 1]);
 
 
+int HAL_SetDeviceName(char *device_name);
+int HAL_SetDeviceSecret(char *device_secret);
+int HAL_SetProductKey(char *product_key);
+int HAL_SetProductSecret(char *product_secret);
+
 void *HAL_MutexCreate(void);
 void *HAL_MutexCreate(void);
 void HAL_MutexDestroy(void *mutex);
 void HAL_MutexDestroy(void *mutex);
 void HAL_MutexLock(void *mutex);
 void HAL_MutexLock(void *mutex);

+ 8 - 5
iotkit-embedded/src/dev_model/examples/linkkit_example_gateway.c

@@ -442,11 +442,14 @@ int main(int argc, char **argv)
     IOT_Ioctl(IOTX_IOCTL_RECV_EVENT_REPLY, (void *)&post_event_reply);
     IOT_Ioctl(IOTX_IOCTL_RECV_EVENT_REPLY, (void *)&post_event_reply);
 
 
     /* Start Connect Aliyun Server */
     /* Start Connect Aliyun Server */
-    res = IOT_Linkkit_Connect(user_example_ctx->master_devid);
-    if (res < 0) {
-        EXAMPLE_TRACE("IOT_Linkkit_Connect Failed\n");
-        return -1;
-    }
+    do {
+        res = IOT_Linkkit_Connect(user_example_ctx->master_devid);
+        if (res < 0) {
+            EXAMPLE_TRACE("IOT_Linkkit_Connect failed, retry after 5s...\n");
+            HAL_SleepMs(5000);
+        }
+    } while (res < 0);
+
 
 
     user_example_ctx->g_user_dispatch_thread_running = 1;
     user_example_ctx->g_user_dispatch_thread_running = 1;
     res = HAL_ThreadCreate(&user_example_ctx->g_user_dispatch_thread, user_dispatch_yield, NULL, NULL, NULL);
     res = HAL_ThreadCreate(&user_example_ctx->g_user_dispatch_thread, user_dispatch_yield, NULL, NULL, NULL);

+ 7 - 6
iotkit-embedded/src/dev_model/examples/linkkit_example_solo.c

@@ -26,7 +26,6 @@ int HAL_Snprintf(char *str, const int len, const char *fmt, ...);
     #include "at_api.h"
     #include "at_api.h"
 #endif
 #endif
 
 
-
 #define EXAMPLE_TRACE(...)                                          \
 #define EXAMPLE_TRACE(...)                                          \
     do {                                                            \
     do {                                                            \
         HAL_Printf("\033[1;32;40m%s.%d: ", __func__, __LINE__);     \
         HAL_Printf("\033[1;32;40m%s.%d: ", __func__, __LINE__);     \
@@ -323,11 +322,13 @@ int main(int argc, char **argv)
     }
     }
 
 
     /* Start Connect Aliyun Server */
     /* Start Connect Aliyun Server */
-    res = IOT_Linkkit_Connect(g_user_example_ctx.master_devid);
-    if (res < 0) {
-        EXAMPLE_TRACE("IOT_Linkkit_Connect Failed\n");
-        return -1;
-    }
+    do {
+        res = IOT_Linkkit_Connect(g_user_example_ctx.master_devid);
+        if (res < 0) {
+            EXAMPLE_TRACE("IOT_Linkkit_Connect failed, retry after 5s...\n");
+            HAL_SleepMs(5000);
+        }
+    } while (res < 0);
 
 
     while (1) {
     while (1) {
         IOT_Linkkit_Yield(EXAMPLE_YIELD_TIMEOUT_MS);
         IOT_Linkkit_Yield(EXAMPLE_YIELD_TIMEOUT_MS);

+ 5 - 0
iotkit-embedded/src/dev_model/impl_linkkit.c

@@ -887,6 +887,11 @@ static int _iotx_linkkit_master_open(iotx_linkkit_dev_meta_info_t *meta_info)
     }
     }
     ctx->is_opened = 1;
     ctx->is_opened = 1;
 
 
+    HAL_SetProductKey(meta_info->product_key);
+    HAL_SetProductSecret(meta_info->product_secret);
+    HAL_SetDeviceName(meta_info->device_name);
+    HAL_SetDeviceSecret(meta_info->device_secret);
+
     /* Create Mutex */
     /* Create Mutex */
     ctx->mutex = HAL_MutexCreate();
     ctx->mutex = HAL_MutexCreate();
     if (ctx->mutex == NULL) {
     if (ctx->mutex == NULL) {

+ 8 - 1
iotkit-embedded/src/dev_model/iotx_cm_mqtt.c

@@ -28,12 +28,15 @@ iotx_cm_connection_t *iotx_cm_open_mqtt(iotx_cm_init_param_t *params)
         cm_err("_mqtt_conncection malloc failed!");
         cm_err("_mqtt_conncection malloc failed!");
         goto failed;
         goto failed;
     }
     }
+    memset(_mqtt_conncection, 0, sizeof(iotx_cm_connection_t));
 
 
     mqtt_param = (iotx_mqtt_param_t *)cm_malloc(sizeof(iotx_mqtt_param_t));
     mqtt_param = (iotx_mqtt_param_t *)cm_malloc(sizeof(iotx_mqtt_param_t));
     if (mqtt_param == NULL) {
     if (mqtt_param == NULL) {
         cm_err("mqtt_param malloc failed!");
         cm_err("mqtt_param malloc failed!");
         goto failed;
         goto failed;
     }
     }
+    memset(mqtt_param, 0, sizeof(iotx_mqtt_param_t));
+
     _mqtt_conncection->open_params = mqtt_param;
     _mqtt_conncection->open_params = mqtt_param;
 
 
     mqtt_param->request_timeout_ms = params->request_timeout_ms;
     mqtt_param->request_timeout_ms = params->request_timeout_ms;
@@ -207,7 +210,7 @@ static void iotx_cloud_conn_mqtt_event_handle(void *pcontext, void *pclient, iot
     }
     }
 }
 }
 
 
-
+extern sdk_impl_ctx_t g_sdk_impl_ctx;
 static int  _mqtt_connect(uint32_t timeout)
 static int  _mqtt_connect(uint32_t timeout)
 {
 {
     void *pclient;
     void *pclient;
@@ -251,6 +254,10 @@ static int  _mqtt_connect(uint32_t timeout)
         HAL_SleepMs(500);
         HAL_SleepMs(500);
     } while (!utils_time_is_expired(&timer));
     } while (!utils_time_is_expired(&timer));
 
 
+    if (g_sdk_impl_ctx.mqtt_customzie_info) {
+        ((iotx_mqtt_param_t *)_mqtt_conncection->open_params)->customize_info = g_sdk_impl_ctx.mqtt_customzie_info;
+    }
+
     do {
     do {
         pclient = IOT_MQTT_Construct((iotx_mqtt_param_t *)_mqtt_conncection->open_params);
         pclient = IOT_MQTT_Construct((iotx_mqtt_param_t *)_mqtt_conncection->open_params);
         if (pclient != NULL) {
         if (pclient != NULL) {

+ 1 - 1
iotkit-embedded/src/dev_sign/dev_sign_config.h

@@ -2,7 +2,7 @@
 #define _DEV_SIGN_CONFIG_H_
 #define _DEV_SIGN_CONFIG_H_
 
 
 #define DEV_SIGN_SOURCE_MAXLEN    (200)
 #define DEV_SIGN_SOURCE_MAXLEN    (200)
-#define DEV_SIGN_HOSTNAME_MAXLEN  (64)
+#define DEV_SIGN_HOSTNAME_MAXLEN  (128)
 #define DEV_SIGN_CLIENT_ID_MAXLEN (200)
 #define DEV_SIGN_CLIENT_ID_MAXLEN (200)
 #define DEV_SIGN_USERNAME_MAXLEN  (64)
 #define DEV_SIGN_USERNAME_MAXLEN  (64)
 #define DEV_SIGN_PASSWORD_MAXLEN  (65)
 #define DEV_SIGN_PASSWORD_MAXLEN  (65)

+ 19 - 2
iotkit-embedded/src/dev_sign/dev_sign_mqtt.c

@@ -14,6 +14,7 @@
 #define MODE_TLS_GUIDER             "-1"
 #define MODE_TLS_GUIDER             "-1"
 #define MODE_TLS_DIRECT             "2"
 #define MODE_TLS_DIRECT             "2"
 #define MODE_TCP_DIRECT_PLAIN       "3"
 #define MODE_TCP_DIRECT_PLAIN       "3"
+#define MODE_ITLS_DNS_ID2           "8"
 
 
 #ifdef MQTT_PRE_AUTH
 #ifdef MQTT_PRE_AUTH
     #define SECURE_MODE             MODE_TLS_GUIDER
     #define SECURE_MODE             MODE_TLS_GUIDER
@@ -70,7 +71,7 @@ static void _hex2str(uint8_t *input, uint16_t input_len, char *output)
     }
     }
 }
 }
 
 
-int _sign_get_clientid(char *clientid_string, const char *device_id)
+int _sign_get_clientid(char *clientid_string, const char *device_id, const char *custom_kv, uint8_t enable_itls)
 {
 {
     uint8_t i;
     uint8_t i;
 
 
@@ -82,6 +83,13 @@ int _sign_get_clientid(char *clientid_string, const char *device_id)
     memcpy(clientid_string, device_id, strlen(device_id));
     memcpy(clientid_string, device_id, strlen(device_id));
     memcpy(clientid_string + strlen(clientid_string), "|", 1);
     memcpy(clientid_string + strlen(clientid_string), "|", 1);
 
 
+    if (enable_itls > 0) {
+        clientid_kv[2][1] = MODE_ITLS_DNS_ID2;
+    }
+    else {
+        clientid_kv[2][1] = SECURE_MODE;
+    }
+
     for (i = 0; i < (sizeof(clientid_kv) / (sizeof(clientid_kv[0]))); i++) {
     for (i = 0; i < (sizeof(clientid_kv) / (sizeof(clientid_kv[0]))); i++) {
         if ((strlen(clientid_string) + strlen(clientid_kv[i][0]) + strlen(clientid_kv[i][1]) + 2) >=
         if ((strlen(clientid_string) + strlen(clientid_kv[i][0]) + strlen(clientid_kv[i][1]) + 2) >=
             DEV_SIGN_CLIENT_ID_MAXLEN) {
             DEV_SIGN_CLIENT_ID_MAXLEN) {
@@ -94,6 +102,14 @@ int _sign_get_clientid(char *clientid_string, const char *device_id)
         memcpy(clientid_string + strlen(clientid_string), ",", 1);
         memcpy(clientid_string + strlen(clientid_string), ",", 1);
     }
     }
 
 
+    if (custom_kv != NULL) {
+        if ((strlen(clientid_string) + strlen(custom_kv) + 1) >= DEV_SIGN_CLIENT_ID_MAXLEN) {
+            return FAIL_RETURN;
+        }
+        memcpy(clientid_string + strlen(clientid_string), custom_kv, strlen(custom_kv));
+        memcpy(clientid_string + strlen(clientid_string), ",", 1);
+    }
+
     memcpy(clientid_string + strlen(clientid_string) - 1, "|", 1);
     memcpy(clientid_string + strlen(clientid_string) - 1, "|", 1);
 
 
     return SUCCESS_RETURN;
     return SUCCESS_RETURN;
@@ -143,6 +159,7 @@ static char* rt_strlwr(char *str)
     }
     }
     return str;
     return str;
 }
 }
+
 int32_t IOT_Sign_MQTT(iotx_mqtt_region_types_t region, iotx_dev_meta_info_t *meta, iotx_sign_mqtt_t *signout)
 int32_t IOT_Sign_MQTT(iotx_mqtt_region_types_t region, iotx_dev_meta_info_t *meta, iotx_sign_mqtt_t *signout)
 {
 {
     uint16_t length = 0;
     uint16_t length = 0;
@@ -160,7 +177,7 @@ int32_t IOT_Sign_MQTT(iotx_mqtt_region_types_t region, iotx_dev_meta_info_t *met
     memcpy(device_id + strlen(device_id), meta->device_name, strlen(meta->device_name));
     memcpy(device_id + strlen(device_id), meta->device_name, strlen(meta->device_name));
 
 
     /* setup clientid */
     /* setup clientid */
-    if (_sign_get_clientid(signout->clientid, device_id) != SUCCESS_RETURN) {
+    if (_sign_get_clientid(signout->clientid, device_id, NULL, 0) != SUCCESS_RETURN) {
         return ERROR_DEV_SIGN_CLIENT_ID_TOO_SHORT;
         return ERROR_DEV_SIGN_CLIENT_ID_TOO_SHORT;
     }
     }
 
 

+ 9 - 6
iotkit-embedded/src/http/examples/http_example.c

@@ -21,10 +21,10 @@
 #define DEFAULT_TIMEOUT_MS 5000
 #define DEFAULT_TIMEOUT_MS 5000
 
 
 /*  on line */
 /*  on line */
-#define IOTX_PRODUCT_KEY         "a13Npv1vjZ4"
-#define IOTX_DEVICE_NAME         "example_zc"
-#define IOTX_DEVICE_SECRET       "ZlexLJ4G0aXiSwkGmUFWuZBLLySKcG8h"
-#define IOTX_DEVICE_ID           "a13Npv1vjZ4.example_zc"
+#define IOTX_PRODUCT_KEY         "a1KqSriAwh0"
+#define IOTX_DEVICE_NAME         "basic_test_01"
+#define IOTX_DEVICE_SECRET       "5gme06iFl3W1g8non2ksVs5e8Qlus4Hw"
+#define IOTX_DEVICE_ID           "a1KqSriAwh0.basic_test_01"
 
 
 static char request_buf[MAX_BUF_LEN];
 static char request_buf[MAX_BUF_LEN];
 static char response_buf[MAX_BUF_LEN];
 static char response_buf[MAX_BUF_LEN];
@@ -43,7 +43,7 @@ void looptest(void *handle)
 
 
     HAL_Snprintf(request_buf, MAX_BUF_LEN, "{\"name\":\"hello world\"}");
     HAL_Snprintf(request_buf, MAX_BUF_LEN, "{\"name\":\"hello world\"}");
     memset(response_buf, 0x00, MAX_BUF_LEN);
     memset(response_buf, 0x00, MAX_BUF_LEN);
-    HAL_Snprintf(path, IOTX_URI_MAX_LEN, "/topic/%s/%s/data",
+    HAL_Snprintf(path, IOTX_URI_MAX_LEN, "/topic/%s/%s/user/update",
                  IOTX_PRODUCT_KEY,
                  IOTX_PRODUCT_KEY,
                  IOTX_DEVICE_NAME);
                  IOTX_DEVICE_NAME);
     msg.request_payload = request_buf;
     msg.request_payload = request_buf;
@@ -69,6 +69,10 @@ static int http_upload_test()
     void                   *handle = NULL;
     void                   *handle = NULL;
     memset(&http_param, 0, sizeof(http_param));
     memset(&http_param, 0, sizeof(http_param));
 
 
+    HAL_SetProductKey((char *)IOTX_PRODUCT_KEY);
+    HAL_SetDeviceName((char *)IOTX_DEVICE_NAME);
+    HAL_SetDeviceSecret((char *)IOTX_DEVICE_SECRET);
+
     strncpy(device_info.product_key,  IOTX_PRODUCT_KEY, IOTX_PRODUCT_KEY_LEN);
     strncpy(device_info.product_key,  IOTX_PRODUCT_KEY, IOTX_PRODUCT_KEY_LEN);
     strncpy(device_info.device_secret, IOTX_DEVICE_SECRET, IOTX_DEVICE_SECRET_LEN);
     strncpy(device_info.device_secret, IOTX_DEVICE_SECRET, IOTX_DEVICE_SECRET_LEN);
     strncpy(device_info.device_name,  IOTX_DEVICE_NAME, IOTX_DEVICE_NAME_LEN);
     strncpy(device_info.device_name,  IOTX_DEVICE_NAME, IOTX_DEVICE_NAME_LEN);
@@ -98,7 +102,6 @@ int main(int argc, char **argv)
     EXAMPLE_TRACE("the device name/device secrete/product name is only valid for ONLINE, EXIT!");
     EXAMPLE_TRACE("the device name/device secrete/product name is only valid for ONLINE, EXIT!");
     return 0;
     return 0;
 #endif
 #endif
-
     int ret;
     int ret;
     ret = http_upload_test();
     ret = http_upload_test();
     return ret;
     return ret;

+ 1 - 1
iotkit-embedded/src/http2/iotx_http2.c

@@ -70,7 +70,7 @@ static ssize_t send_callback(nghttp2_session *session, const uint8_t *data,
     client = (httpclient_t *)connection->network;
     client = (httpclient_t *)connection->network;
     rv = client->net.write(&client->net, (char *)data, length, 5000);
     rv = client->net.write(&client->net, (char *)data, length, 5000);
     NGHTTP2_DBG("send_callback data ends len = %d!\r\n", rv);
     NGHTTP2_DBG("send_callback data ends len = %d!\r\n", rv);
-    if (rv < 0) {
+    if (rv < 0 || rv < length) {
         rv = NGHTTP2_ERR_CALLBACK_FAILURE;
         rv = NGHTTP2_ERR_CALLBACK_FAILURE;
     }
     }
     return rv;
     return rv;

+ 15 - 1
iotkit-embedded/src/infra/infra_compat.c

@@ -26,7 +26,7 @@ void IOT_SetLogLevel(IOT_LogLevel level) {}
 void *HAL_Malloc(uint32_t size);
 void *HAL_Malloc(uint32_t size);
 void HAL_Free(void *ptr);
 void HAL_Free(void *ptr);
 
 
-static sdk_impl_ctx_t g_sdk_impl_ctx = {0};
+sdk_impl_ctx_t g_sdk_impl_ctx = {0};
 /* global variable for mqtt construction */
 /* global variable for mqtt construction */
 static iotx_conn_info_t g_iotx_conn_info = {0};
 static iotx_conn_info_t g_iotx_conn_info = {0};
 static char g_empty_string[1] = "";
 static char g_empty_string[1] = "";
@@ -184,6 +184,20 @@ int IOT_Ioctl(int option, void *data)
         }
         }
         break;
         break;
 #endif
 #endif
+        case IOTX_IOCTL_SET_CUSTOMIZE_INFO: {
+            if (ctx->mqtt_customzie_info) {
+                HAL_Free(ctx->mqtt_customzie_info);
+                ctx->mqtt_customzie_info = NULL;
+            }
+            ctx->mqtt_customzie_info = HAL_Malloc(strlen((char *)data) + 1);
+            if (ctx->mqtt_customzie_info == NULL) {
+                return FAIL_RETURN;
+            }
+            memset(ctx->mqtt_customzie_info, 0, strlen((char *)data) + 1);
+            memcpy(ctx->mqtt_customzie_info, data, strlen((char *)data));
+            res = SUCCESS_RETURN;
+        }
+        break;
         default: {
         default: {
             res = FAIL_RETURN;
             res = FAIL_RETURN;
         }
         }

+ 2 - 0
iotkit-embedded/src/infra/infra_compat.h

@@ -216,6 +216,7 @@ typedef struct {
     int dynamic_register;
     int dynamic_register;
     char *cloud_custom_domain;
     char *cloud_custom_domain;
     char *http_custom_domain;
     char *http_custom_domain;
+    char *mqtt_customzie_info;
 } sdk_impl_ctx_t;
 } sdk_impl_ctx_t;
 
 
 typedef enum {
 typedef enum {
@@ -232,6 +233,7 @@ typedef enum {
     IOTX_IOCTL_GET_SUBDEV_LOGIN,        /* value(int*): 0 - SubDev is logout; 1 - SubDev is login */
     IOTX_IOCTL_GET_SUBDEV_LOGIN,        /* value(int*): 0 - SubDev is logout; 1 - SubDev is login */
     IOTX_IOCTL_SET_OTA_DEV_ID,          /* value(int*):     select the device to do OTA according to devid */
     IOTX_IOCTL_SET_OTA_DEV_ID,          /* value(int*):     select the device to do OTA according to devid */
     IOTX_IOCTL_QUERY_DEVID,             /* value(iotx_dev_meta_info_t*): device meta info, only productKey and deviceName is required, ret value is subdev_id or -1 */
     IOTX_IOCTL_QUERY_DEVID,             /* value(iotx_dev_meta_info_t*): device meta info, only productKey and deviceName is required, ret value is subdev_id or -1 */
+    IOTX_IOCTL_SET_CUSTOMIZE_INFO,      /* value(char*): set mqtt clientID customize information */
 } iotx_ioctl_option_t;
 } iotx_ioctl_option_t;
 
 
 typedef enum {
 typedef enum {

+ 1 - 34
iotkit-embedded/src/infra/infra_net.c

@@ -44,7 +44,7 @@ static void ssl_free(void *ptr)
 }
 }
 #endif
 #endif
 
 
-#if  defined(SUPPORT_TLS) || defined(SUPPORT_ITLS)
+#if  defined(SUPPORT_TLS)
 uintptr_t HAL_SSL_Establish(const char *host, uint16_t port, const char *ca_crt, uint32_t ca_crt_len);
 uintptr_t HAL_SSL_Establish(const char *host, uint16_t port, const char *ca_crt, uint32_t ca_crt_len);
 int32_t HAL_SSL_Destroy(uintptr_t handle);
 int32_t HAL_SSL_Destroy(uintptr_t handle);
 int HAL_SSL_Read(uintptr_t handle, char *buf, int len, int timeout_ms);
 int HAL_SSL_Read(uintptr_t handle, char *buf, int len, int timeout_ms);
@@ -95,22 +95,6 @@ static int connect_ssl(utils_network_pt pNetwork)
         return 1;
         return 1;
     }
     }
 
 
-#if defined(SUPPORT_ITLS)
-    char pkps[IOTX_PRODUCT_KEY_LEN + IOTX_PRODUCT_SECRET_LEN + 3] = {0};
-
-    HAL_GetProductKey(pkps);
-    int len = strlen(pkps);
-    HAL_GetProductSecret(pkps + len + 1);
-    len += strlen(pkps + len + 1) + 2;
-
-    if (0 != (pNetwork->handle = (intptr_t)HAL_SSL_Establish(
-            pNetwork->pHostAddress,
-            pNetwork->port,
-            pkps, len))) {
-        return 0;
-    }
-#else
-
 #ifdef INFRA_MEM_STATS
 #ifdef INFRA_MEM_STATS
     memset(&ssl_hooks, 0, sizeof(ssl_hooks_t));
     memset(&ssl_hooks, 0, sizeof(ssl_hooks_t));
     ssl_hooks.malloc = ssl_malloc;
     ssl_hooks.malloc = ssl_malloc;
@@ -128,7 +112,6 @@ static int connect_ssl(utils_network_pt pNetwork)
             pNetwork->ca_crt_len + 1))) {
             pNetwork->ca_crt_len + 1))) {
         return 0;
         return 0;
     }
     }
-#endif
     else {
     else {
         /* TODO SHOLUD not remove this handle space */
         /* TODO SHOLUD not remove this handle space */
         /* The space will be freed by calling disconnect_ssl() */
         /* The space will be freed by calling disconnect_ssl() */
@@ -244,11 +227,7 @@ int utils_net_read(utils_network_pt pNetwork, char *buffer, uint32_t len, uint32
     }
     }
 #else
 #else
     if (NULL == pNetwork->ca_crt) {
     if (NULL == pNetwork->ca_crt) {
-#ifdef SUPPORT_ITLS
-        ret = read_ssl(pNetwork, buffer, len, timeout_ms);
-#else
         ret = read_tcp(pNetwork, buffer, len, timeout_ms);
         ret = read_tcp(pNetwork, buffer, len, timeout_ms);
-#endif
     }
     }
 #endif
 #endif
     else {
     else {
@@ -268,11 +247,7 @@ int utils_net_write(utils_network_pt pNetwork, const char *buffer, uint32_t len,
     }
     }
 #else
 #else
     if (NULL == pNetwork->ca_crt) {
     if (NULL == pNetwork->ca_crt) {
-#ifdef SUPPORT_ITLS
-        ret = write_ssl(pNetwork, buffer, len, timeout_ms);
-#else
         ret = write_tcp(pNetwork, buffer, len, timeout_ms);
         ret = write_tcp(pNetwork, buffer, len, timeout_ms);
-#endif
     }
     }
 #endif
 #endif
 
 
@@ -293,11 +268,7 @@ int iotx_net_disconnect(utils_network_pt pNetwork)
     }
     }
 #else
 #else
     if (NULL == pNetwork->ca_crt) {
     if (NULL == pNetwork->ca_crt) {
-#ifdef SUPPORT_ITLS
-        ret = disconnect_ssl(pNetwork);
-#else
         ret = disconnect_tcp(pNetwork);
         ret = disconnect_tcp(pNetwork);
-#endif
     }
     }
 #endif
 #endif
     else {
     else {
@@ -317,11 +288,7 @@ int iotx_net_connect(utils_network_pt pNetwork)
     }
     }
 #else
 #else
     if (NULL == pNetwork->ca_crt) {
     if (NULL == pNetwork->ca_crt) {
-#ifdef SUPPORT_ITLS
-        ret = connect_ssl(pNetwork);
-#else
         ret = connect_tcp(pNetwork);
         ret = connect_tcp(pNetwork);
-#endif
     }
     }
 #endif
 #endif
     else {
     else {

+ 11 - 2
iotkit-embedded/src/mqtt/impl/iotx_mqtt_client.c

@@ -534,7 +534,7 @@ static int iotx_mc_read_packet(iotx_mc_client_t *c, iotx_time_t *timer, unsigned
     left_t = (left_t == 0) ? 1 : left_t;
     left_t = (left_t == 0) ? 1 : left_t;
     rc = c->ipstack.read(&c->ipstack, c->buf_read, 1, left_t);
     rc = c->ipstack.read(&c->ipstack, c->buf_read, 1, left_t);
     if (0 == rc) { /* timeout */
     if (0 == rc) { /* timeout */
-        *packet_type = 0;
+        *packet_type = MQTT_CPT_RESERVED;
         HAL_MutexUnlock(c->lock_read_buf);
         HAL_MutexUnlock(c->lock_read_buf);
         return SUCCESS_RETURN;
         return SUCCESS_RETURN;
     } else if (1 != rc) {
     } else if (1 != rc) {
@@ -630,6 +630,7 @@ static int iotx_mc_read_packet(iotx_mc_client_t *c, iotx_time_t *timer, unsigned
         remainDataBuf = NULL;
         remainDataBuf = NULL;
 #endif
 #endif
         HAL_MutexUnlock(c->lock_read_buf);
         HAL_MutexUnlock(c->lock_read_buf);
+        *packet_type = MQTT_CPT_RESERVED;
         if (NULL != c->handle_event.h_fp) {
         if (NULL != c->handle_event.h_fp) {
             iotx_mqtt_event_msg_t msg;
             iotx_mqtt_event_msg_t msg;
 
 
@@ -766,11 +767,13 @@ static int _mqtt_connect(void *client)
     int rc = FAIL_RETURN;
     int rc = FAIL_RETURN;
     int try_count = 1;
     int try_count = 1;
     iotx_mc_client_t *pClient = (iotx_mc_client_t *)client;
     iotx_mc_client_t *pClient = (iotx_mc_client_t *)client;
+    int userKeepAliveInterval = 0;
 
 
     if (NULL == pClient) {
     if (NULL == pClient) {
         return NULL_VALUE_ERROR;
         return NULL_VALUE_ERROR;
     }
     }
-
+    userKeepAliveInterval = pClient->connect_data.keepAliveInterval;
+    pClient->connect_data.keepAliveInterval = CONFIG_MQTT_KEEPALIVE_INTERVAL_MAX;
     mqtt_info("connect params: MQTTVersion=%d, clientID=%s, keepAliveInterval=%d, username=%s",
     mqtt_info("connect params: MQTTVersion=%d, clientID=%s, keepAliveInterval=%d, username=%s",
               pClient->connect_data.MQTTVersion,
               pClient->connect_data.MQTTVersion,
               pClient->connect_data.clientID.cstring,
               pClient->connect_data.clientID.cstring,
@@ -780,6 +783,7 @@ static int _mqtt_connect(void *client)
     /* Establish TCP or TLS connection */
     /* Establish TCP or TLS connection */
     do {
     do {
         rc = MQTTConnect(pClient);
         rc = MQTTConnect(pClient);
+        pClient->connect_data.keepAliveInterval = userKeepAliveInterval;
 
 
         if (rc != SUCCESS_RETURN) {
         if (rc != SUCCESS_RETURN) {
             pClient->ipstack.disconnect(&pClient->ipstack);
             pClient->ipstack.disconnect(&pClient->ipstack);
@@ -1380,6 +1384,11 @@ static int iotx_mc_handle_recv_PUBLISH(iotx_mc_client_t *c)
     topic_msg.qos = (unsigned char)qos;
     topic_msg.qos = (unsigned char)qos;
     topic_msg.payload_len = payload_len;
     topic_msg.payload_len = payload_len;
 
 
+    if (topicName.lenstring.len == 0 || topicName.lenstring.data == NULL) {
+        mqtt_err("Null topicName");
+        return MQTT_PUBLISH_PACKET_ERROR;
+    }
+
 #ifdef INFRA_LOG_NETWORK_PAYLOAD
 #ifdef INFRA_LOG_NETWORK_PAYLOAD
 
 
     json_payload = (const char *)topic_msg.payload;
     json_payload = (const char *)topic_msg.payload;

+ 35 - 7
iotkit-embedded/src/mqtt/mqtt_api.c

@@ -43,6 +43,7 @@
 
 
 static void        *g_mqtt_client = NULL;
 static void        *g_mqtt_client = NULL;
 iotx_sign_mqtt_t    g_default_sign;
 iotx_sign_mqtt_t    g_default_sign;
+static char         iotx_ca_crt_itls[IOTX_PRODUCT_KEY_LEN + IOTX_PRODUCT_SECRET_LEN + 2] = {0};
 
 
 /* Handle structure of subscribed topic */
 /* Handle structure of subscribed topic */
 typedef struct  {
 typedef struct  {
@@ -330,7 +331,6 @@ static int _iotx_dynamic_register(iotx_http_region_types_t region, iotx_dev_meta
 
 
 #ifdef MQTT_PRE_AUTH
 #ifdef MQTT_PRE_AUTH
 #include "infra_preauth.h"
 #include "infra_preauth.h"
-extern int _sign_get_clientid(char *clientid_string, const char *device_id);
 extern int _iotx_generate_sign_string(const char *device_id, const char *device_name, const char *product_key,
 extern int _iotx_generate_sign_string(const char *device_id, const char *device_name, const char *product_key,
                                       const char *device_secret, char *sign_string);
                                       const char *device_secret, char *sign_string);
 
 
@@ -349,11 +349,6 @@ static int _iotx_preauth(iotx_mqtt_region_types_t region, iotx_dev_meta_info_t *
     memcpy(device_id + strlen(device_id), ".", strlen("."));
     memcpy(device_id + strlen(device_id), ".", strlen("."));
     memcpy(device_id + strlen(device_id), meta->device_name, strlen(meta->device_name));
     memcpy(device_id + strlen(device_id), meta->device_name, strlen(meta->device_name));
 
 
-    /* setup clientid */
-    if (_sign_get_clientid(preauth_out->clientid, device_id) != SUCCESS_RETURN) {
-        return ERROR_DEV_SIGN_CLIENT_ID_TOO_SHORT;
-    }
-
     /* setup sign_string */
     /* setup sign_string */
     res = _iotx_generate_sign_string(device_id, meta->device_name, meta->product_key, meta->device_secret, sign_string);
     res = _iotx_generate_sign_string(device_id, meta->device_name, meta->product_key, meta->device_secret, sign_string);
     if (res < SUCCESS_RETURN) {
     if (res < SUCCESS_RETURN) {
@@ -364,14 +359,18 @@ static int _iotx_preauth(iotx_mqtt_region_types_t region, iotx_dev_meta_info_t *
 }
 }
 #endif /* #ifdef MQTT_PRE_AUTH */
 #endif /* #ifdef MQTT_PRE_AUTH */
 
 
+extern int _sign_get_clientid(char *clientid_string, const char *device_id, const char *custom_kv, uint8_t enable_itls);
+
 /************************  Public Interface ************************/
 /************************  Public Interface ************************/
 void *IOT_MQTT_Construct(iotx_mqtt_param_t *pInitParams)
 void *IOT_MQTT_Construct(iotx_mqtt_param_t *pInitParams)
 {
 {
     void *pclient;
     void *pclient;
     iotx_dev_meta_info_t meta_info;
     iotx_dev_meta_info_t meta_info;
     iotx_mqtt_param_t mqtt_params;
     iotx_mqtt_param_t mqtt_params;
+    char device_id[IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 1] = {0};
     int region = 0;
     int region = 0;
     int dynamic = 0;
     int dynamic = 0;
+    uint8_t enalbe_itls = 0;
     int ret;
     int ret;
     void *callback;
     void *callback;
 
 
@@ -442,13 +441,42 @@ void *IOT_MQTT_Construct(iotx_mqtt_param_t *pInitParams)
     }
     }
 #endif /* #ifdef MQTT_PRE_AUTH */
 #endif /* #ifdef MQTT_PRE_AUTH */
 
 
+    /* setup device_id */
+    memcpy(device_id, meta_info.product_key, strlen(meta_info.product_key));
+    memcpy(device_id + strlen(device_id), ".", strlen("."));
+    memcpy(device_id + strlen(device_id), meta_info.device_name, strlen(meta_info.device_name));
+
+    /* reconfig clientid, append custome clientKV and itls switch flag */
+    if (pInitParams != NULL && pInitParams->customize_info != NULL) {
+        if (strstr(pInitParams->customize_info, "authtype=id2") != NULL) {
+            enalbe_itls = 1;
+        }
+        else {
+            enalbe_itls = 0;
+        }
+    }
+
+    if (_sign_get_clientid(g_default_sign.clientid, device_id,
+                           (pInitParams != NULL) ? pInitParams->customize_info : NULL, enalbe_itls) != SUCCESS_RETURN) {
+        return NULL;
+    }
+
     /* Initialize MQTT parameter */
     /* Initialize MQTT parameter */
     memset(&mqtt_params, 0x0, sizeof(iotx_mqtt_param_t));
     memset(&mqtt_params, 0x0, sizeof(iotx_mqtt_param_t));
 
 
 #ifdef SUPPORT_TLS
 #ifdef SUPPORT_TLS
     {
     {
         extern const char *iotx_ca_crt;
         extern const char *iotx_ca_crt;
-        mqtt_params.pub_key = iotx_ca_crt;
+        if (enalbe_itls == 0) {
+            mqtt_params.pub_key = iotx_ca_crt;
+        }
+        else {
+            memset(iotx_ca_crt_itls, 0, sizeof(iotx_ca_crt_itls));
+            HAL_GetProductKey(iotx_ca_crt_itls);
+            iotx_ca_crt_itls[strlen(iotx_ca_crt_itls)] = '.';
+            HAL_GetProductSecret(iotx_ca_crt_itls + strlen(iotx_ca_crt_itls));
+            mqtt_params.pub_key = iotx_ca_crt_itls;
+        }
     }
     }
 #endif
 #endif
     mqtt_params.request_timeout_ms    = CONFIG_MQTT_REQUEST_TIMEOUT;
     mqtt_params.request_timeout_ms    = CONFIG_MQTT_REQUEST_TIMEOUT;

+ 2 - 2
iotkit-embedded/src/mqtt/mqtt_api.h

@@ -140,7 +140,7 @@ typedef struct {
     const char                 *client_id;              /* Specify MQTT connection client id*/
     const char                 *client_id;              /* Specify MQTT connection client id*/
     const char                 *username;               /* Specify MQTT user name */
     const char                 *username;               /* Specify MQTT user name */
     const char                 *password;               /* Specify MQTT password */
     const char                 *password;               /* Specify MQTT password */
-
+    const char                 *customize_info;         /* Specify User custom information */
     /* Specify MQTT transport channel and key.
     /* Specify MQTT transport channel and key.
      * If the value is NULL, it means that use TCP channel,
      * If the value is NULL, it means that use TCP channel,
      * If the value is NOT NULL, it means that use SSL/TLS channel and
      * If the value is NOT NULL, it means that use SSL/TLS channel and
@@ -376,7 +376,7 @@ int IOT_MQTT_Nwk_Event_Handler(void *handle, iotx_mqtt_nwk_event_t event, iotx_m
 #define CONFIG_MQTT_KEEPALIVE_INTERVAL_MIN      (30)
 #define CONFIG_MQTT_KEEPALIVE_INTERVAL_MIN      (30)
 
 
 /* Maximum keepalive interval of MQTT request in second */
 /* Maximum keepalive interval of MQTT request in second */
-#define CONFIG_MQTT_KEEPALIVE_INTERVAL_MAX      (180)
+#define CONFIG_MQTT_KEEPALIVE_INTERVAL_MAX      (1200)
 
 
 #if defined(__cplusplus)
 #if defined(__cplusplus)
 }
 }

+ 1 - 0
iotkit-embedded/src/mqtt/mqtt_wrapper.h

@@ -17,6 +17,7 @@ void HAL_MutexLock(void *mutex);
 void HAL_MutexUnlock(void *mutex);
 void HAL_MutexUnlock(void *mutex);
 
 
 int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN + 1]);
 int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN + 1]);
+int HAL_GetProductSecret(char product_secret[IOTX_PRODUCT_SECRET_LEN + 1]);
 int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN + 1]);
 int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN + 1]);
 int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN + 1]);
 int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN + 1]);
 int HAL_GetFirmwareVersion(char *version);
 int HAL_GetFirmwareVersion(char *version);

+ 1 - 0
iotkit-embedded/src/wifi_provision/dev_ap/dev_ap_wrapper.h

@@ -51,5 +51,6 @@ int HAL_Awss_Connect_Ap(
             _IN_OPT_ enum AWSS_ENC_TYPE encry,
             _IN_OPT_ enum AWSS_ENC_TYPE encry,
             _IN_OPT_ uint8_t bssid[ETH_ALEN],
             _IN_OPT_ uint8_t bssid[ETH_ALEN],
             _IN_OPT_ uint8_t channel);
             _IN_OPT_ uint8_t channel);
+int HAL_Awss_Close_Ap();
 /*************************************** dev-ap special hals ***************************************/
 /*************************************** dev-ap special hals ***************************************/
 int HAL_Awss_Open_Ap(const char *ssid, const char *passwd, int beacon_interval, int hide);
 int HAL_Awss_Open_Ap(const char *ssid, const char *passwd, int beacon_interval, int hide);

+ 5 - 0
iotkit-embedded/src/wifi_provision/frameworks/zconfig_vendor_common.c

@@ -341,6 +341,11 @@ rescanning:
 
 
         if (aws_state == AWS_SCANNING) {
         if (aws_state == AWS_SCANNING) {
             awss_debug("channel rescanning...\n");
             awss_debug("channel rescanning...\n");
+            if (zconfig_data != NULL) {
+                void *tmp_mutex = zc_mutex;
+                memset(zconfig_data, 0, sizeof(struct zconfig_data));
+                zc_mutex = tmp_mutex;
+            }
             goto rescanning;
             goto rescanning;
         }
         }
     }
     }

+ 4 - 1
iotkit-embedded/src/wifi_provision/p2p/awss_wps.c

@@ -212,10 +212,13 @@ static int get_ssid_passwd_from_w(uint8_t *in, int total_len, uint8_t *src, uint
             }
             }
             break;
             break;
         }
         }
-        default:
+        default: {
+            void *tmp_mutex = zc_mutex;
             awss_warn("p2p encypt:%d not support\r\n", encrypt);
             awss_warn("p2p encypt:%d not support\r\n", encrypt);
             memset(zconfig_data, 0, sizeof(*zconfig_data));
             memset(zconfig_data, 0, sizeof(*zconfig_data));
+            zc_mutex = tmp_mutex;
             return GOT_NOTHING;
             return GOT_NOTHING;
+        }
     }
     }
 
 
     awss_debug("ssid:%s, tlen:%d\r\n", tmp_ssid, total_len);
     awss_debug("ssid:%s, tlen:%d\r\n", tmp_ssid, total_len);

+ 5 - 0
iotkit-embedded/src/wifi_provision/smartconfig/awss_smartconfig.c

@@ -359,14 +359,17 @@ int zconfig_get_ssid_passwd(uint8_t tods)
         aes_decrypt_string((char *)tmp, (char *)zc_passwd, passwd_len,
         aes_decrypt_string((char *)tmp, (char *)zc_passwd, passwd_len,
                            1, awss_get_encrypt_type(), 0, NULL);
                            1, awss_get_encrypt_type(), 0, NULL);
         if (is_utf8((const char *)zc_passwd, passwd_len) == 0) {
         if (is_utf8((const char *)zc_passwd, passwd_len) == 0) {
+            void *tmp_mutex = zc_mutex;
             awss_trace("passwd err\r\n");
             awss_trace("passwd err\r\n");
             memset(zconfig_data, 0, sizeof(*zconfig_data));
             memset(zconfig_data, 0, sizeof(*zconfig_data));
+            zc_mutex = tmp_mutex;
             awss_event_post(IOTX_AWSS_PASSWD_ERR);
             awss_event_post(IOTX_AWSS_PASSWD_ERR);
             AWSS_UPDATE_STATIS(AWSS_STATIS_SM_IDX, AWSS_STATIS_TYPE_PASSWD_ERR);
             AWSS_UPDATE_STATIS(AWSS_STATIS_SM_IDX, AWSS_STATIS_TYPE_PASSWD_ERR);
             ret = -1;
             ret = -1;
             goto exit;
             goto exit;
         }
         }
     } else {
     } else {
+        void *tmp_mutex;
         memcpy((void *)tmp, (const void *)pbuf, passwd_len);
         memcpy((void *)tmp, (const void *)pbuf, passwd_len);
         tmp[passwd_len] = '\0';
         tmp[passwd_len] = '\0';
         for (i = 0; i < passwd_len; i ++) {
         for (i = 0; i < passwd_len; i ++) {
@@ -375,7 +378,9 @@ int zconfig_get_ssid_passwd(uint8_t tods)
         strncpy((char *)zc_passwd, (const char *)tmp, ZC_MAX_PASSWD_LEN - 1);
         strncpy((char *)zc_passwd, (const char *)tmp, ZC_MAX_PASSWD_LEN - 1);
 
 
         awss_trace("encrypt:%d not support\r\n", passwd_encrypt);
         awss_trace("encrypt:%d not support\r\n", passwd_encrypt);
+        tmp_mutex = zc_mutex;
         memset(zconfig_data, 0, sizeof(*zconfig_data));
         memset(zconfig_data, 0, sizeof(*zconfig_data));
+        zc_mutex = tmp_mutex;
         ret = -1;
         ret = -1;
         goto exit;
         goto exit;
     }
     }

+ 4 - 4
iotkit-embedded/tools/misc/xtrc_wrapper_rules

@@ -82,10 +82,10 @@ DEVICE_MODEL_ENABLED&ALCS_ENABLED||HAL_Aes128_Init|
 DEVICE_MODEL_ENABLED&ALCS_ENABLED||HAL_Aes128_Cbc_Encrypt|
 DEVICE_MODEL_ENABLED&ALCS_ENABLED||HAL_Aes128_Cbc_Encrypt|
 DEVICE_MODEL_ENABLED&ALCS_ENABLED||HAL_Aes128_Destroy|
 DEVICE_MODEL_ENABLED&ALCS_ENABLED||HAL_Aes128_Destroy|
 DEVICE_MODEL_ENABLED&ALCS_ENABLED||HAL_Aes128_Cbc_Decrypt|
 DEVICE_MODEL_ENABLED&ALCS_ENABLED||HAL_Aes128_Cbc_Decrypt|
-DEVICE_MODEL_ENABLED&DEPRECATED_LINKKIT||HAL_SetProductKey|
-DEVICE_MODEL_ENABLED&DEPRECATED_LINKKIT||HAL_SetProductSecret|
-DEVICE_MODEL_ENABLED&DEPRECATED_LINKKIT||HAL_SetDeviceName|
-DEVICE_MODEL_ENABLED&DEPRECATED_LINKKIT||HAL_SetDeviceSecret|
+DEVICE_MODEL_ENABLED||HAL_SetProductKey|
+DEVICE_MODEL_ENABLED||HAL_SetProductSecret|
+DEVICE_MODEL_ENABLED||HAL_SetDeviceName|
+DEVICE_MODEL_ENABLED||HAL_SetDeviceSecret|
 
 
 COAP_SERVER||HAL_UDP_recvfrom|
 COAP_SERVER||HAL_UDP_recvfrom|
 COAP_SERVER||HAL_UDP_sendto|
 COAP_SERVER||HAL_UDP_sendto|

+ 13 - 1
iotkit-embedded/wrappers/os/ubuntu/HAL_TCP_linux.c

@@ -47,6 +47,7 @@ uintptr_t HAL_TCP_Establish(const char *host, uint16_t port)
     int fd = 0;
     int fd = 0;
     int rc = 0;
     int rc = 0;
     char service[6];
     char service[6];
+    uint8_t dns_retry = 0;
 
 
     memset(&hints, 0, sizeof(hints));
     memset(&hints, 0, sizeof(hints));
 
 
@@ -57,7 +58,18 @@ uintptr_t HAL_TCP_Establish(const char *host, uint16_t port)
     hints.ai_protocol = IPPROTO_TCP;
     hints.ai_protocol = IPPROTO_TCP;
     sprintf(service, "%u", port);
     sprintf(service, "%u", port);
 
 
-    if ((rc = getaddrinfo(host, service, &hints, &addrInfoList)) != 0) {
+    while(dns_retry++ < 8) {
+        rc = getaddrinfo(host, service, &hints, &addrInfoList);
+        if (rc != 0) {
+            printf("getaddrinfo error[%d], res: %s, host: %s, port: %s\n", dns_retry, gai_strerror(rc), host, service);
+            sleep(1);
+            continue;
+        }else{
+            break;
+        }
+    }
+
+    if (rc != 0) {
         printf("getaddrinfo error(%d), host = '%s', port = [%d]\n", rc, host, port);
         printf("getaddrinfo error(%d), host = '%s', port = [%d]\n", rc, host, port);
         return (uintptr_t)(-1);
         return (uintptr_t)(-1);
     }
     }

+ 17 - 3
iotkit-embedded/wrappers/tls/HAL_TLS_mbedtls.c

@@ -6,7 +6,7 @@
 
 
 #include <stdio.h>
 #include <stdio.h>
 #include <string.h>
 #include <string.h>
-// #include <memory.h>
+#include <memory.h>
 #include <stdlib.h>
 #include <stdlib.h>
 #include <string.h>
 #include <string.h>
 #if defined(_PLATFORM_IS_LINUX_)
 #if defined(_PLATFORM_IS_LINUX_)
@@ -304,6 +304,7 @@ static int mbedtls_net_connect_timeout(mbedtls_net_context *ctx, const char *hos
     int ret;
     int ret;
     struct addrinfo hints, *addr_list, *cur;
     struct addrinfo hints, *addr_list, *cur;
     struct timeval sendtimeout;
     struct timeval sendtimeout;
+    uint8_t dns_retry = 0;
 
 
     if ((ret = net_prepare()) != 0) {
     if ((ret = net_prepare()) != 0) {
         return (ret);
         return (ret);
@@ -315,7 +316,18 @@ static int mbedtls_net_connect_timeout(mbedtls_net_context *ctx, const char *hos
     hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
     hints.ai_socktype = proto == MBEDTLS_NET_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM;
     hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
     hints.ai_protocol = proto == MBEDTLS_NET_PROTO_UDP ? IPPROTO_UDP : IPPROTO_TCP;
 
 
-    if (getaddrinfo(host, port, &hints, &addr_list) != 0) {
+    while(dns_retry++ < 8) {
+        ret = getaddrinfo(host, port, &hints, &addr_list);
+        if (ret != 0) {
+            printf("getaddrinfo error[%d], res: %s, host: %s, port: %s\n", dns_retry, gai_strerror(ret), host, port);
+            sleep(1);
+            continue;
+        }else{
+            break;
+        }
+    }
+
+    if (ret != 0) {
         return (MBEDTLS_ERR_NET_UNKNOWN_HOST);
         return (MBEDTLS_ERR_NET_UNKNOWN_HOST);
     }
     }
 
 
@@ -707,6 +719,9 @@ static int _network_ssl_write(TLSDataParams_t *pTlsData, const char *buffer, int
         if (res < 0) {
         if (res < 0) {
             if (res != MBEDTLS_ERR_SSL_WANT_READ &&
             if (res != MBEDTLS_ERR_SSL_WANT_READ &&
                 res != MBEDTLS_ERR_SSL_WANT_WRITE) {
                 res != MBEDTLS_ERR_SSL_WANT_WRITE) {
+                if (write_bytes == 0) {
+                    return -1;
+                }
                 break;
                 break;
             }
             }
         }else if (res == 0) {
         }else if (res == 0) {
@@ -843,4 +858,3 @@ uintptr_t HAL_SSL_Establish(const char *host,
     return (uintptr_t)pTlsData;
     return (uintptr_t)pTlsData;
 }
 }
 
 
-