data_template_sample.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * Tencent is pleased to support the open source community by making IoT Hub available.
  3. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  4. * Licensed under the MIT License (the "License"); you may not use this file except in
  5. * compliance with the License. You may obtain a copy of the License at
  6. * http://opensource.org/licenses/MIT
  7. * Unless required by applicable law or agreed to in writing, software distributed under the License is
  8. * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. * either express or implied. See the License for the specific language governing permissions and
  10. * limitations under the License.
  11. *
  12. */
  13. #include "ql_api_datacall.h"
  14. #include "ql_api_osi.h"
  15. #include "qcloud_iot_export.h"
  16. #include "qcloud_iot_import.h"
  17. #include "lite-utils.h"
  18. #include "data_config.c"
  19. #define PROFILE_IDX 1
  20. #ifdef DEBUG_DEV_INFO_USED
  21. extern int device_info_copy(void *pdst, void *psrc, uint8_t max_len);
  22. #endif
  23. #ifdef DEBUG_DEV_INFO_USED
  24. #define PRODUCT_ID "KSS57PUDXP"
  25. #define DEVICE_NAME "Smart_light_03"
  26. #define DEVICE_SECRET "HdpsormITQtQvaQiKff5UA=="
  27. #define REGION "china"
  28. #define DEVICE_CERT "./certsSmart_light_03_cert.crt"
  29. #define DEVICE_PRIVATE "./certsSmart_light_03_private.key"
  30. #else
  31. #define DEVICE_INFO_JSON_DOC "UFS:device_info.json"
  32. #endif
  33. #ifdef AUTH_MODE_CERT
  34. static char sg_cert_file[PATH_MAX + 1]; // full path of device cert file
  35. static char sg_key_file[PATH_MAX + 1]; // full path of device key file
  36. #endif
  37. static DeviceInfo sg_devInfo;
  38. static MQTTEventType sg_subscribe_event_result = MQTT_EVENT_UNDEF;
  39. static bool sg_control_msg_arrived = false;
  40. static char sg_data_report_buffer[2048];
  41. static size_t sg_data_report_buffersize = sizeof(sg_data_report_buffer) / sizeof(sg_data_report_buffer[0]);
  42. #ifdef EVENT_POST_ENABLED
  43. #include "events_config.c"
  44. static void update_events_timestamp(sEvent *pEvents, int count)
  45. {
  46. int i;
  47. for (i = 0; i < count; i++) {
  48. if (NULL == (&pEvents[i])) {
  49. Log_e("null event pointer");
  50. return;
  51. }
  52. #ifdef EVENT_TIMESTAMP_USED
  53. pEvents[i].timestamp = HAL_GetTimeMs(); // should be UTC and accurate
  54. #else
  55. pEvents[i].timestamp = 0;
  56. #endif
  57. }
  58. }
  59. static void event_post_cb(void *pClient, MQTTMessage *msg)
  60. {
  61. Log_d("Reply:%.*s", msg->payload_len, msg->payload);
  62. // IOT_Event_clearFlag(pClient, FLAG_EVENT0 | FLAG_EVENT1 | FLAG_EVENT2);
  63. }
  64. // event check and post
  65. static void eventPostCheck(void *client)
  66. {
  67. int i;
  68. int rc;
  69. uint32_t eflag;
  70. uint8_t event_count;
  71. sEvent * pEventList[EVENT_COUNTS];
  72. eflag = IOT_Event_getFlag(client);
  73. if ((EVENT_COUNTS > 0) && (eflag > 0)) {
  74. event_count = 0;
  75. for (i = 0; i < EVENT_COUNTS; i++) {
  76. if ((eflag & (1 << i)) & ALL_EVENTS_MASK) {
  77. pEventList[event_count++] = &(g_events[i]);
  78. update_events_timestamp(&g_events[i], 1);
  79. IOT_Event_clearFlag(client, (1 << i) & ALL_EVENTS_MASK);
  80. }
  81. }
  82. rc = IOT_Post_Event(client, sg_data_report_buffer, sg_data_report_buffersize, event_count, pEventList,
  83. event_post_cb);
  84. if (rc < 0) {
  85. Log_e("events post failed: %d", rc);
  86. }
  87. }
  88. }
  89. #endif
  90. #ifdef ACTION_ENABLED
  91. #include "action_config.c"
  92. // action : regist action and set the action handle callback, add your aciton logic here
  93. static void OnActionCallback(void *pClient, const char *pClientToken, DeviceAction *pAction)
  94. {
  95. int i;
  96. sReplyPara replyPara;
  97. // do something base on input, just print as an sample
  98. DeviceProperty *pActionInput = pAction->pInput;
  99. for (i = 0; i < pAction->input_num; i++) {
  100. if (JSTRING == pActionInput[i].type) {
  101. Log_d("Input:[%s], data:[%s]", pActionInput[i].key, pActionInput[i].data);
  102. HAL_Free(pActionInput[i].data);
  103. } else {
  104. if (JINT32 == pActionInput[i].type) {
  105. Log_d("Input:[%s], data:[%d]", pActionInput[i].key, *((int *)pActionInput[i].data));
  106. } else if (JFLOAT == pActionInput[i].type) {
  107. Log_d("Input:[%s], data:[%f]", pActionInput[i].key, *((float *)pActionInput[i].data));
  108. } else if (JUINT32 == pActionInput[i].type) {
  109. Log_d("Input:[%s], data:[%u]", pActionInput[i].key, *((uint32_t *)pActionInput[i].data));
  110. }
  111. }
  112. }
  113. // construct output
  114. memset((char *)&replyPara, 0, sizeof(sReplyPara));
  115. replyPara.code = eDEAL_SUCCESS;
  116. replyPara.timeout_ms = QCLOUD_IOT_MQTT_COMMAND_TIMEOUT;
  117. strcpy(replyPara.status_msg, "action execute success!"); // add the message about the action resault
  118. DeviceProperty *pActionOutnput = pAction->pOutput;
  119. (void)pActionOutnput; // elimate warning
  120. // TO DO: add your aciont logic here and set output properties which will be reported by action_reply
  121. IOT_ACTION_REPLY(pClient, pClientToken, sg_data_report_buffer, sg_data_report_buffersize, pAction, &replyPara);
  122. }
  123. static int _register_data_template_action(void *pTemplate_client)
  124. {
  125. int i, rc;
  126. for (i = 0; i < TOTAL_ACTION_COUNTS; i++) {
  127. rc = IOT_Template_Register_Action(pTemplate_client, &g_actions[i], OnActionCallback);
  128. if (rc != QCLOUD_RET_SUCCESS) {
  129. rc = IOT_Template_Destroy(pTemplate_client);
  130. Log_e("register device data template action failed, err: %d", rc);
  131. return rc;
  132. } else {
  133. Log_i("data template action=%s registered.", g_actions[i].pActionId);
  134. }
  135. }
  136. return QCLOUD_RET_SUCCESS;
  137. }
  138. #endif
  139. static void event_handler(void *pclient, void *handle_context, MQTTEventMsg *msg)
  140. {
  141. uintptr_t packet_id = (uintptr_t)msg->msg;
  142. switch (msg->event_type) {
  143. case MQTT_EVENT_UNDEF:
  144. Log_i("undefined event occur.");
  145. break;
  146. case MQTT_EVENT_DISCONNECT:
  147. Log_i("MQTT disconnect.");
  148. break;
  149. case MQTT_EVENT_RECONNECT:
  150. Log_i("MQTT reconnect.");
  151. break;
  152. case MQTT_EVENT_SUBCRIBE_SUCCESS:
  153. sg_subscribe_event_result = msg->event_type;
  154. Log_i("subscribe success, packet-id=%u", packet_id);
  155. break;
  156. case MQTT_EVENT_SUBCRIBE_TIMEOUT:
  157. sg_subscribe_event_result = msg->event_type;
  158. Log_i("subscribe wait ack timeout, packet-id=%u", packet_id);
  159. break;
  160. case MQTT_EVENT_SUBCRIBE_NACK:
  161. sg_subscribe_event_result = msg->event_type;
  162. Log_i("subscribe nack, packet-id=%u", packet_id);
  163. break;
  164. case MQTT_EVENT_PUBLISH_SUCCESS:
  165. Log_i("publish success, packet-id=%u", (unsigned int)packet_id);
  166. break;
  167. case MQTT_EVENT_PUBLISH_TIMEOUT:
  168. Log_i("publish timeout, packet-id=%u", (unsigned int)packet_id);
  169. break;
  170. case MQTT_EVENT_PUBLISH_NACK:
  171. Log_i("publish nack, packet-id=%u", (unsigned int)packet_id);
  172. break;
  173. default:
  174. Log_i("Should NOT arrive here.");
  175. break;
  176. }
  177. }
  178. /*add user init code, like sensor init*/
  179. static void _usr_init(void)
  180. {
  181. Log_d("add your init code here");
  182. }
  183. // Setup MQTT construct parameters
  184. static int _setup_connect_init_params(TemplateInitParams *initParams)
  185. {
  186. int ret;
  187. #ifdef DEBUG_DEV_INFO_USED
  188. ret = HAL_GetDevInfo((void *)&sg_devInfo);
  189. #else
  190. ret = HAL_GetDevInfoFromFile(DEVICE_INFO_JSON_DOC, (void *)&sg_devInfo);
  191. #endif
  192. if (QCLOUD_RET_SUCCESS != ret) {
  193. return ret;
  194. }
  195. initParams->device_name = sg_devInfo.device_name;
  196. initParams->product_id = sg_devInfo.product_id;
  197. #ifdef AUTH_MODE_CERT
  198. /* TLS with certs*/
  199. // char certs_dir[PATH_MAX + 1] = "certs";
  200. // char current_path[PATH_MAX + 1];
  201. // char *cwd = getcwd(current_path, sizeof(current_path));
  202. // if (cwd == NULL) {
  203. // Log_e("getcwd return NULL");
  204. // return QCLOUD_ERR_FAILURE;
  205. // }
  206. sprintf(sg_cert_file, "%s", sg_devInfo.dev_cert_file_name);
  207. sprintf(sg_key_file, "%s", sg_devInfo.dev_key_file_name);
  208. initParams->cert_file = sg_cert_file;
  209. initParams->key_file = sg_key_file;
  210. #else
  211. initParams->device_secret = sg_devInfo.device_secret;
  212. #endif
  213. initParams->command_timeout = QCLOUD_IOT_MQTT_COMMAND_TIMEOUT;
  214. initParams->keep_alive_interval_ms = QCLOUD_IOT_MQTT_KEEP_ALIVE_INTERNAL;
  215. initParams->auto_connect_enable = 1;
  216. initParams->event_handle.h_fp = event_handler;
  217. return QCLOUD_RET_SUCCESS;
  218. }
  219. static void OnControlMsgCallback(void *pClient, const char *pJsonValueBuffer, uint32_t valueLength,
  220. DeviceProperty *pProperty)
  221. {
  222. int i = 0;
  223. for (i = 0; i < TOTAL_PROPERTY_COUNT; i++) {
  224. /* handle self defined string/json here. Other properties are dealed in _handle_delta()*/
  225. if (strcmp(sg_DataTemplate[i].data_property.key, pProperty->key) == 0) {
  226. sg_DataTemplate[i].state = eCHANGED;
  227. Log_i("Property=%s changed", pProperty->key);
  228. sg_control_msg_arrived = true;
  229. return;
  230. }
  231. }
  232. Log_e("Property=%s changed no match", pProperty->key);
  233. }
  234. static void OnReportReplyCallback(void *pClient, Method method, ReplyAck replyAck, const char *pJsonDocument,
  235. void *pUserdata)
  236. {
  237. Log_i("recv report reply response, reply ack: %d", replyAck);
  238. }
  239. // register data template properties
  240. static int _register_data_template_property(void *pTemplate_client)
  241. {
  242. int i, rc;
  243. for (i = 0; i < TOTAL_PROPERTY_COUNT; i++) {
  244. rc = IOT_Template_Register_Property(pTemplate_client, &sg_DataTemplate[i].data_property, OnControlMsgCallback);
  245. if (rc != QCLOUD_RET_SUCCESS) {
  246. rc = IOT_Template_Destroy(pTemplate_client);
  247. Log_e("register device data template property failed, err: %d", rc);
  248. return rc;
  249. } else {
  250. Log_i("data template property=%s registered.", sg_DataTemplate[i].data_property.key);
  251. }
  252. }
  253. return QCLOUD_RET_SUCCESS;
  254. }
  255. // when control msg received, data_template's properties has been parsed in pData you should add your logic how to use pData
  256. void deal_down_stream_user_logic(void *client, ProductDataDefine *pData)
  257. {
  258. Log_d("someting about your own product logic wait to be done");
  259. #ifdef EVENT_POST_ENABLED
  260. // IOT_Event_setFlag(client, FLAG_EVENT0); //set the events flag when the evnts your defined occured, see
  261. // events_config.c
  262. #endif
  263. }
  264. /*get local property data, like sensor data*/
  265. static void _refresh_local_property(void)
  266. {
  267. // add your local property refresh logic
  268. }
  269. /*find propery need report*/
  270. static int find_wait_report_property(DeviceProperty *pReportDataList[])
  271. {
  272. int i, j;
  273. for (i = 0, j = 0; i < TOTAL_PROPERTY_COUNT; i++) {
  274. if (eCHANGED == sg_DataTemplate[i].state) {
  275. pReportDataList[j++] = &(sg_DataTemplate[i].data_property);
  276. sg_DataTemplate[i].state = eNOCHANGE;
  277. }
  278. }
  279. return j;
  280. }
  281. // demo for up-stream
  282. // add changed properties to pReportDataList, then the changed properties would be reported
  283. // you should add your own logic for how to get the changed properties
  284. int deal_up_stream_user_logic(DeviceProperty *pReportDataList[], int *pCount)
  285. {
  286. // refresh local property
  287. _refresh_local_property();
  288. /*find propery need report*/
  289. *pCount = find_wait_report_property(pReportDataList);
  290. return (*pCount > 0) ? QCLOUD_RET_SUCCESS : QCLOUD_ERR_FAILURE;
  291. }
  292. /*You should get the real info for your device, here just for example*/
  293. static int _get_sys_info(void *handle, char *pJsonDoc, size_t sizeOfBuffer)
  294. {
  295. /*platform info has at least one of module_hardinfo/module_softinfo/fw_ver property*/
  296. DeviceProperty plat_info[] = {
  297. {.key = "module_hardinfo", .type = TYPE_TEMPLATE_STRING, .data = "ESP8266"},
  298. {.key = "module_softinfo", .type = TYPE_TEMPLATE_STRING, .data = "V1.0"},
  299. {.key = "fw_ver", .type = TYPE_TEMPLATE_STRING, .data = QCLOUD_IOT_DEVICE_SDK_VERSION},
  300. {.key = "imei", .type = TYPE_TEMPLATE_STRING, .data = "11-22-33-44"},
  301. {.key = "lat", .type = TYPE_TEMPLATE_STRING, .data = "22.546015"},
  302. {.key = "lon", .type = TYPE_TEMPLATE_STRING, .data = "113.941125"},
  303. {NULL, NULL, 0} // end
  304. };
  305. /*self define info*/
  306. DeviceProperty self_info[] = {
  307. {.key = "append_info", .type = TYPE_TEMPLATE_STRING, .data = "your self define info"}, {NULL, NULL, 0} // end
  308. };
  309. return IOT_Template_JSON_ConstructSysInfo(handle, pJsonDoc, sizeOfBuffer, plat_info, self_info);
  310. }
  311. static int datacall_satrt(void)
  312. {
  313. int ret = 0;
  314. int i = 0;
  315. ql_data_call_info_s info;
  316. char ip4_addr_str[16] = {0};
  317. uint8_t nSim = 0;
  318. ql_rtos_task_sleep_s(10);
  319. Log_e("wait for network register done");
  320. while ((ret = ql_network_register_wait(nSim, 120)) != 0 && i < 10)
  321. {
  322. i++;
  323. ql_rtos_task_sleep_s(1);
  324. }
  325. if (ret == 0)
  326. {
  327. i = 0;
  328. Log_e("====network registered!!!!====");
  329. }
  330. else
  331. {
  332. Log_e("====network register failure!!!!!====");
  333. goto exit;
  334. }
  335. ql_set_data_call_asyn_mode(nSim, PROFILE_IDX, 0);
  336. Log_e("===start data call====");
  337. ret = ql_start_data_call(nSim, PROFILE_IDX, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  338. Log_e("===data call result:%d", ret);
  339. if (ret != 0)
  340. {
  341. Log_e("====data call failure!!!!=====");
  342. }
  343. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  344. ret = ql_get_data_call_info(nSim, PROFILE_IDX, &info);
  345. if (ret != 0)
  346. {
  347. Log_e("ql_get_data_call_info ret: %d", ret);
  348. ql_stop_data_call(nSim, PROFILE_IDX);
  349. goto exit;
  350. }
  351. Log_i("info.profile_idx: %d", info.profile_idx);
  352. Log_i("info.ip_version: %d", info.ip_version);
  353. Log_i("info.v4.state: %d", info.v4.state);
  354. //Log_i("info.v4.reconnect: %d", info.v4.reconnect);
  355. inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  356. Log_i("info.v4.addr.ip: %s", ip4_addr_str);
  357. inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  358. Log_i("info.v4.addr.pri_dns: %s", ip4_addr_str);
  359. inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  360. Log_i("info.v4.addr.sec_dns: %s", ip4_addr_str);
  361. return 0;
  362. exit:
  363. return -1;
  364. }
  365. static void data_template_sample(void *argv)
  366. {
  367. int rc;
  368. sReplyPara replyPara;
  369. DeviceProperty *pReportDataList[TOTAL_PROPERTY_COUNT];
  370. int ReportCont;
  371. void *client=NULL;
  372. // init log level
  373. IOT_Log_Set_Level(eLOG_DEBUG);
  374. /*执行拨号操作,设备联网*/
  375. if(datacall_satrt() != 0)
  376. {
  377. goto exit;
  378. }
  379. #ifdef DEBUG_DEV_INFO_USED
  380. DeviceInfo devInfo =
  381. {
  382. .product_id = PRODUCT_ID,
  383. .device_name = DEVICE_NAME,
  384. .client_id = ""PRODUCT_ID"_"DEVICE_NAME"",
  385. .region = REGION,
  386. .dev_type = eCOMMON_DEV,
  387. };
  388. #ifndef AUTH_MODE_CERT
  389. device_info_copy(devInfo.device_secret, DEVICE_SECRET,MAX_SIZE_OF_DEVICE_SECRET); // set dev cert file name,
  390. #else
  391. device_info_copy(devInfo.dev_cert_file_name, DEVICE_CERT,MAX_SIZE_OF_DEVICE_CERT_FILE_NAME); // set dev cert file name
  392. device_info_copy(devInfo.dev_key_file_name, DEVICE_PRIVATE,MAX_SIZE_OF_DEVICE_SECRET_FILE_NAME); // set dev cert file name
  393. #endif
  394. HAL_SetDevInfo(&devInfo);
  395. #endif
  396. // init connection
  397. TemplateInitParams init_params = DEFAULT_TEMPLATE_INIT_PARAMS;
  398. rc = _setup_connect_init_params(&init_params);
  399. if (rc != QCLOUD_RET_SUCCESS) {
  400. Log_e("init params err,rc=%d", rc);
  401. ql_rtos_task_delete(NULL);
  402. }
  403. client = IOT_Template_Construct(&init_params, NULL);
  404. if (client != NULL) {
  405. Log_i("Cloud Device Construct Success");
  406. } else {
  407. Log_e("Cloud Device Construct Failed");
  408. ql_rtos_task_delete(NULL);
  409. }
  410. #ifdef MULTITHREAD_ENABLED
  411. if (QCLOUD_RET_SUCCESS != IOT_Template_Start_Yield_Thread(client)) {
  412. Log_e("start template yield thread fail");
  413. goto exit;
  414. }
  415. #endif
  416. // user init
  417. _usr_init();
  418. // init data template
  419. _init_data_template();
  420. // register data template propertys here
  421. rc = _register_data_template_property(client);
  422. if (rc == QCLOUD_RET_SUCCESS) {
  423. Log_i("Register data template propertys Success");
  424. } else {
  425. Log_e("Register data template propertys Failed: %d", rc);
  426. goto exit;
  427. }
  428. // register data template actions here
  429. #ifdef ACTION_ENABLED
  430. rc = _register_data_template_action(client);
  431. if (rc == QCLOUD_RET_SUCCESS) {
  432. Log_i("Register data template actions Success");
  433. } else {
  434. Log_e("Register data template actions Failed: %d", rc);
  435. goto exit;
  436. }
  437. #endif
  438. // report device info, then you can manager your product by these info, like position
  439. rc = _get_sys_info(client, sg_data_report_buffer, sg_data_report_buffersize);
  440. if (QCLOUD_RET_SUCCESS == rc) {
  441. rc = IOT_Template_Report_SysInfo_Sync(client, sg_data_report_buffer, sg_data_report_buffersize,
  442. QCLOUD_IOT_MQTT_COMMAND_TIMEOUT);
  443. if (rc != QCLOUD_RET_SUCCESS) {
  444. Log_e("Report system info fail, err: %d", rc);
  445. }
  446. } else {
  447. Log_e("Get system info fail, err: %d", rc);
  448. }
  449. // get the property changed during offline
  450. rc = IOT_Template_GetStatus_sync(client, QCLOUD_IOT_MQTT_COMMAND_TIMEOUT);
  451. if (rc != QCLOUD_RET_SUCCESS) {
  452. Log_e("Get data status fail, err: %d", rc);
  453. } else {
  454. Log_d("Get data status success");
  455. }
  456. while (IOT_Template_IsConnected(client) || rc == QCLOUD_ERR_MQTT_ATTEMPTING_RECONNECT ||
  457. rc == QCLOUD_RET_MQTT_RECONNECTED || QCLOUD_RET_SUCCESS == rc) {
  458. #ifndef MULTITHREAD_ENABLED
  459. rc = IOT_Template_Yield(client, 200);
  460. if (rc == QCLOUD_ERR_MQTT_ATTEMPTING_RECONNECT) {
  461. HAL_SleepMs(1000);
  462. continue;
  463. } else if (rc != QCLOUD_RET_SUCCESS) {
  464. Log_e("Exit loop caused of errCode: %d", rc);
  465. }
  466. #endif
  467. /* handle control msg from server */
  468. if (sg_control_msg_arrived) {
  469. deal_down_stream_user_logic(client, &sg_ProductData);
  470. /* control msg should reply, otherwise server treat device didn't receive and retain the msg which would be
  471. * get by get status*/
  472. memset((char *)&replyPara, 0, sizeof(sReplyPara));
  473. replyPara.code = eDEAL_SUCCESS;
  474. replyPara.timeout_ms = QCLOUD_IOT_MQTT_COMMAND_TIMEOUT;
  475. replyPara.status_msg[0] = '\0'; // add extra info to replyPara.status_msg when error occured
  476. rc = IOT_Template_ControlReply(client, sg_data_report_buffer, sg_data_report_buffersize, &replyPara);
  477. if (rc == QCLOUD_RET_SUCCESS) {
  478. Log_d("Contol msg reply success");
  479. sg_control_msg_arrived = false;
  480. } else {
  481. Log_e("Contol msg reply failed, err: %d", rc);
  482. }
  483. } else {
  484. Log_d("No control msg received...");
  485. }
  486. /*report msg to server*/
  487. /*report the lastest properties's status*/
  488. if (QCLOUD_RET_SUCCESS == deal_up_stream_user_logic(pReportDataList, &ReportCont)) {
  489. rc = IOT_Template_JSON_ConstructReportArray(client, sg_data_report_buffer, sg_data_report_buffersize,
  490. ReportCont, pReportDataList);
  491. if (rc == QCLOUD_RET_SUCCESS) {
  492. rc = IOT_Template_Report(client, sg_data_report_buffer, sg_data_report_buffersize,
  493. OnReportReplyCallback, NULL, QCLOUD_IOT_MQTT_COMMAND_TIMEOUT);
  494. if (rc == QCLOUD_RET_SUCCESS) {
  495. Log_i("data template reporte success");
  496. } else {
  497. Log_e("data template reporte failed, err: %d", rc);
  498. }
  499. } else {
  500. Log_e("construct reporte data failed, err: %d", rc);
  501. }
  502. } else {
  503. // Log_d("no data need to be reported or someting goes wrong");
  504. }
  505. #ifdef EVENT_POST_ENABLED
  506. eventPostCheck(client);
  507. #endif
  508. HAL_SleepMs(3000);
  509. }
  510. exit:
  511. Log_e("task exit!");
  512. #ifdef MULTITHREAD_ENABLED
  513. IOT_Template_Stop_Yield_Thread(client);
  514. #endif
  515. if(client!=NULL)
  516. {
  517. rc = IOT_Template_Destroy(client);
  518. }
  519. ql_rtos_task_delete(NULL);
  520. }
  521. //static void ql_nw_status_callback(int profile_idx, int nw_status)
  522. //{
  523. // Log_i("profile(%d) status: %d", profile_idx, nw_status);
  524. //}
  525. //
  526. //static int datacall_satrt(void)
  527. //{
  528. // struct ql_data_call_info_s info ;
  529. // memset(&info, 0x00, sizeof(ql_data_call_info_s));
  530. // char ip4_addr_str[16] = {0};
  531. //
  532. // Log_d("wait for network register done");
  533. //
  534. // if(ql_network_register_wait(120) != 0)
  535. // {
  536. // Log_e("*** network register fail ***");
  537. // return -1;
  538. // }
  539. // else
  540. // {
  541. // Log_d("doing network activating ...");
  542. //
  543. // ql_wan_start(ql_nw_status_callback);
  544. // ql_set_auto_connect(PROFILE_IDX, TRUE);
  545. // if(ql_start_data_call(PROFILE_IDX, 0, "3gnet.mnc001.mcc460.gprs", NULL, NULL, 0) == 0)
  546. // {
  547. // ql_get_data_call_info(PROFILE_IDX, 0, &info);
  548. //
  549. // Log_i("info.profile_idx: %d", info.profile_idx);
  550. // Log_i("info.ip_version: %d", info.ip_version);
  551. // Log_i("info.v4.state: %d", info.v4.state);
  552. // Log_i("info.v4.reconnect: %d", info.v4.reconnect);
  553. //
  554. // inet_ntop(AF_INET, &info.v4.addr.ip, ip4_addr_str, sizeof(ip4_addr_str));
  555. // Log_i("info.v4.addr.ip: %s", ip4_addr_str);
  556. //
  557. // inet_ntop(AF_INET, &info.v4.addr.pri_dns, ip4_addr_str, sizeof(ip4_addr_str));
  558. // Log_i("info.v4.addr.pri_dns: %s", ip4_addr_str);
  559. //
  560. // inet_ntop(AF_INET, &info.v4.addr.sec_dns, ip4_addr_str, sizeof(ip4_addr_str));
  561. // Log_i("info.v4.addr.sec_dns: %s", ip4_addr_str);
  562. //
  563. // return 0;
  564. // }
  565. //
  566. // Log_e("*** network activated fail ***");
  567. // return -1;
  568. // }
  569. //}
  570. int data_template_sample_start(void)
  571. {
  572. int err = 0;
  573. int count = 0;
  574. ql_task_t data_template_sample_task = NULL;
  575. /* 主循环进入休眠 */
  576. while (count < 10)
  577. {
  578. count++;
  579. ql_rtos_task_sleep_s(1);
  580. Log_e("data_template_sample_start_init -0x%04X\n", err);
  581. }
  582. err = ql_rtos_task_create(&data_template_sample_task, 10*1024, APP_PRIORITY_NORMAL, "data_template", data_template_sample, NULL, 5);
  583. if (err != QL_OSI_SUCCESS)
  584. {
  585. return -1;
  586. }
  587. return 0;
  588. }