dynreg_dev_sample.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Tencent is pleased to support the open source community by making IoT Hub
  3. available.
  4. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  5. * Licensed under the MIT License (the "License"); you may not use this file
  6. except in
  7. * compliance with the License. You may obtain a copy of the License at
  8. * http://opensource.org/licenses/MIT
  9. * Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is
  11. * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  12. KIND,
  13. * either express or implied. See the License for the specific language
  14. governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <stdbool.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "ql_api_osi.h"
  23. #include "ql_api_datacall.h"
  24. #include "qcloud_iot_export.h"
  25. #include "qcloud_iot_import.h"
  26. #include "utils_getopt.h"
  27. #ifdef AUTH_MODE_CERT
  28. /* NULL cert file */
  29. #define QCLOUD_IOT_NULL_CERT_FILENAME "./certs/Smart_light_01_cert.crt"
  30. /* NULL key file */
  31. #define QCLOUD_IOT_NULL_KEY_FILENAME "./certs/Smart_light_01_private.key"
  32. #else
  33. /* NULL device secret */
  34. #define QCLOUD_IOT_NULL_DEVICE_SECRET "HdpsormITQtQvaQiKff5UA=="
  35. #endif
  36. #define PROFILE_IDX 1
  37. static int datacall_satrt(void)
  38. {
  39. int ret = 0;
  40. int i = 0;
  41. ql_data_call_info_s info;
  42. char ip4_addr_str[16] = {0};
  43. uint8_t nSim = 0;
  44. ql_rtos_task_sleep_s(10);
  45. Log_e("wait for network register done");
  46. while ((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10)
  47. {
  48. i++;
  49. ql_rtos_task_sleep_s(1);
  50. }
  51. if (ret == 0)
  52. {
  53. i = 0;
  54. Log_e("====network registered!!!!====");
  55. }
  56. else
  57. {
  58. Log_e("====network register failure!!!!!====");
  59. goto exit;
  60. }
  61. ql_set_data_call_asyn_mode(nSim, PROFILE_IDX, 0);
  62. Log_e("===start data call====");
  63. ret = ql_start_data_call(nSim, PROFILE_IDX, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  64. Log_e("===data call result:%d", ret);
  65. if (ret != 0)
  66. {
  67. Log_e("====data call failure!!!!=====");
  68. }
  69. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  70. ret = ql_get_data_call_info(nSim, PROFILE_IDX, &info);
  71. if (ret != 0)
  72. {
  73. Log_e("ql_get_data_call_info ret: %d", ret);
  74. ql_stop_data_call(nSim, PROFILE_IDX);
  75. goto exit;
  76. }
  77. Log_i("info.profile_idx: %d", info.profile_idx);
  78. Log_i("info.ip_version: %d", info.ip_version);
  79. Log_i("info.v4.state: %d", info.v4.state);
  80. //Log_i("info.v4.reconnect: %d", info.v4.reconnect);
  81. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  82. Log_i("info.v4.addr.ip: %s", ip4_addr_str);
  83. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  84. Log_i("info.v4.addr.pri_dns: %s", ip4_addr_str);
  85. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  86. Log_i("info.v4.addr.sec_dns: %s", ip4_addr_str);
  87. return 0;
  88. exit:
  89. return -1;
  90. }
  91. void dynreg_dev_sample(void *arg)
  92. {
  93. // int c;
  94. // while ((c = utils_getopt(argc, argv, "c:")) != EOF) switch (c) {
  95. // case 'c':
  96. // if (HAL_SetDevInfoFile(utils_optarg))
  97. // return -1;
  98. // break;
  99. //
  100. // default:
  101. // HAL_Printf(
  102. // "usage: %s [options]\n"
  103. // " [-c <config file for DeviceInfo>] \n",
  104. // argv[0]);
  105. //
  106. // return -1;
  107. // }
  108. // init log level
  109. IOT_Log_Set_Level(eLOG_DEBUG);
  110. /*执行拨号操作,设备联网*/
  111. if(datacall_satrt() != 0)
  112. {
  113. Log_e("Cloud Device datacall Failed");
  114. goto exit;
  115. }
  116. int ret;
  117. DeviceInfo sDevInfo;
  118. bool infoNullFlag = false;
  119. memset((char *)&sDevInfo, 0, sizeof(DeviceInfo));
  120. ret = HAL_GetDevInfo(&sDevInfo);
  121. #ifndef GATEWAY_ENABLED
  122. sDevInfo.dev_type = eCOMMON_DEV;
  123. #else
  124. sDevInfo.dev_type = eGW_SUB_DEV;
  125. #endif
  126. #ifdef AUTH_MODE_CERT
  127. /* just demo the cert/key files are empty */
  128. if (!strcmp(sDevInfo.dev_cert_file_name, QCLOUD_IOT_NULL_CERT_FILENAME) ||
  129. !strcmp(sDevInfo.dev_key_file_name, QCLOUD_IOT_NULL_KEY_FILENAME)) {
  130. Log_d("dev Cert not exist!");
  131. infoNullFlag = true;
  132. } else {
  133. Log_d("dev Cert exist");
  134. }
  135. #else
  136. /* just demo the PSK is empty */
  137. if (!strcmp(sDevInfo.device_secret, QCLOUD_IOT_NULL_DEVICE_SECRET)) {
  138. Log_d("dev psk not exist!");
  139. infoNullFlag = true;
  140. } else {
  141. Log_d("dev psk exist");
  142. }
  143. #endif
  144. /* device cert/key files or PSK is empty, do dynamic register to fetch */
  145. if (infoNullFlag)
  146. {
  147. if (QCLOUD_RET_SUCCESS == IOT_DynReg_Device(PROFILE_IDX, &sDevInfo))
  148. {
  149. ret = HAL_SetDevInfo(&sDevInfo);
  150. if (QCLOUD_RET_SUCCESS != ret)
  151. {
  152. Log_e("devices info save fail");
  153. }
  154. else
  155. {
  156. #ifdef AUTH_MODE_CERT
  157. Log_d(
  158. "dynamic register success, productID: %s, devName: %s, CertFile: "
  159. "%s, KeyFile: %s",
  160. sDevInfo.product_id, sDevInfo.device_name, sDevInfo.dev_cert_file_name, sDevInfo.dev_key_file_name);
  161. #else
  162. Log_d(
  163. "dynamic register success,productID: %s, devName: %s, "
  164. "device_secret: %s",
  165. sDevInfo.product_id, sDevInfo.device_name, sDevInfo.device_secret);
  166. #endif
  167. }
  168. }
  169. else
  170. {
  171. Log_e("%s dynamic register fail", sDevInfo.device_name);
  172. }
  173. }
  174. exit:
  175. ql_rtos_task_delete(NULL);
  176. }
  177. int dynreg_dev_sample_start(void)
  178. {
  179. int err = 0;
  180. int count = 0;
  181. ql_task_t dynreg_dev_sample_task = NULL;
  182. /* 主循环进入休眠 */
  183. while (count < 10)
  184. {
  185. count++;
  186. ql_rtos_task_sleep_s(1);
  187. Log_e("ql_qcloud_sdk_dynreg_dev_sample_start_init -0x%04X\n", err);
  188. }
  189. err = ql_rtos_task_create(&dynreg_dev_sample_task, 10*1024, APP_PRIORITY_NORMAL, "dynreg_dev", dynreg_dev_sample, NULL, 5);
  190. if (err != QL_OSI_SUCCESS)
  191. {
  192. return -1;
  193. }
  194. return 0;
  195. }