gateway_common.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * Tencent is pleased to support the open source community by making IoT Hub
  3. available.
  4. * Copyright (C) 2018-2020 THL A29 Limited, a Tencent company. All rights
  5. reserved.
  6. * Licensed under the MIT License (the "License"); you may not use this file
  7. except in
  8. * compliance with the License. You may obtain a copy of the License at
  9. * http://opensource.org/licenses/MIT
  10. * Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is
  12. * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND,
  14. * either express or implied. See the License for the specific language
  15. governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. #include "gateway_common.h"
  20. #include "lite-utils.h"
  21. #include "mqtt_client.h"
  22. #include "utils_base64.h"
  23. #include "utils_md5.h"
  24. #include "utils_hmac.h"
  25. #include "ql_fs.h"
  26. #define QUECTEL_OPEN_CPU
  27. static char cloud_rcv_buf[GATEWAY_RECEIVE_BUFFER_LEN];
  28. static bool get_json_type(char *json, char **v)
  29. {
  30. *v = LITE_json_value_of("type", json);
  31. return *v == NULL ? false : true;
  32. }
  33. static bool get_json_devices(char *json, char **v)
  34. {
  35. *v = LITE_json_value_of("payload.devices", json);
  36. return *v == NULL ? false : true;
  37. }
  38. static bool get_json_result(char *json, int32_t *res)
  39. {
  40. char *v = LITE_json_value_of("result", json);
  41. if (v == NULL) {
  42. return false;
  43. }
  44. if (LITE_get_int32(res, v) != QCLOUD_RET_SUCCESS) {
  45. HAL_Free(v);
  46. return false;
  47. }
  48. HAL_Free(v);
  49. return true;
  50. }
  51. static bool get_json_product_id(char *json, char **v)
  52. {
  53. *v = LITE_json_value_of("product_id", json);
  54. return *v == NULL ? false : true;
  55. }
  56. static bool get_json_device_name(char *json, char **v)
  57. {
  58. *v = LITE_json_value_of("device_name", json);
  59. return *v == NULL ? false : true;
  60. }
  61. static void _gateway_message_handler(void *client, MQTTMessage *message, void *user_data)
  62. {
  63. Qcloud_IoT_Client *mqtt = NULL;
  64. Gateway * gateway = NULL;
  65. char * topic = NULL;
  66. size_t topic_len = 0;
  67. int cloud_rcv_len = 0;
  68. char * type = NULL;
  69. char * devices = NULL, *devices_strip = NULL;
  70. char * product_id = NULL;
  71. char * device_name = NULL;
  72. int32_t result = 0;
  73. char client_id[MAX_SIZE_OF_CLIENT_ID + 1] = {0};
  74. int size = 0;
  75. POINTER_SANITY_CHECK_RTN(client);
  76. POINTER_SANITY_CHECK_RTN(message);
  77. mqtt = (Qcloud_IoT_Client *)client;
  78. gateway = (Gateway *)mqtt->event_handle.context;
  79. POINTER_SANITY_CHECK_RTN(gateway);
  80. topic = (char *)message->ptopic;
  81. topic_len = message->topic_len;
  82. if (NULL == topic || topic_len <= 0) {
  83. Log_e("topic == NULL or topic_len <= 0.");
  84. return;
  85. }
  86. if (message->payload_len > GATEWAY_RECEIVE_BUFFER_LEN) {
  87. Log_e("message->payload_len > GATEWAY_RECEIVE_BUFFER_LEN.");
  88. return;
  89. }
  90. cloud_rcv_len = Min(GATEWAY_RECEIVE_BUFFER_LEN - 1, message->payload_len);
  91. memcpy(cloud_rcv_buf, message->payload, cloud_rcv_len + 1);
  92. cloud_rcv_buf[cloud_rcv_len] = '\0'; // jsmn_parse relies on a string
  93. // Log_d("recv:%s", cloud_rcv_buf);
  94. if (!get_json_type(cloud_rcv_buf, &type)) {
  95. Log_e("Fail to parse type from msg: %s", cloud_rcv_buf);
  96. return;
  97. }
  98. if (!get_json_devices(cloud_rcv_buf, &devices)) {
  99. Log_e("Fail to parse devices from msg: %s", cloud_rcv_buf);
  100. HAL_Free(type);
  101. return;
  102. }
  103. if (devices[0] == '[') {
  104. devices_strip = devices + 1;
  105. } else {
  106. devices_strip = devices;
  107. }
  108. if (!get_json_result(devices_strip, &result)) {
  109. Log_e("Fail to parse result from msg: %s", cloud_rcv_buf);
  110. HAL_Free(type);
  111. HAL_Free(devices);
  112. return;
  113. }
  114. if (!get_json_product_id(devices_strip, &product_id)) {
  115. Log_e("Fail to parse product_id from msg: %s", cloud_rcv_buf);
  116. HAL_Free(type);
  117. HAL_Free(devices);
  118. return;
  119. }
  120. if (!get_json_device_name(devices_strip, &device_name)) {
  121. Log_e("Fail to parse device_name from msg: %s", cloud_rcv_buf);
  122. HAL_Free(type);
  123. HAL_Free(devices);
  124. HAL_Free(product_id);
  125. return;
  126. }
  127. size = HAL_Snprintf(client_id, MAX_SIZE_OF_CLIENT_ID + 1, GATEWAY_CLIENT_ID_FMT, product_id, device_name);
  128. if (size < 0 || size > MAX_SIZE_OF_CLIENT_ID) {
  129. Log_e("generate client_id fail.");
  130. HAL_Free(type);
  131. HAL_Free(devices);
  132. HAL_Free(product_id);
  133. HAL_Free(device_name);
  134. return;
  135. }
  136. if (strncmp(type, GATEWAY_ONLINE_OP_STR, sizeof(GATEWAY_ONLINE_OP_STR) - 1) == 0) {
  137. if (strncmp(client_id, gateway->gateway_data.online.client_id, size) == 0) {
  138. Log_i("client_id(%s), online result %d", client_id, result);
  139. gateway->gateway_data.online.result = result;
  140. }
  141. } else if (strncmp(type, GATEWAY_OFFLIN_OP_STR, sizeof(GATEWAY_OFFLIN_OP_STR) - 1) == 0) {
  142. if (strncmp(client_id, gateway->gateway_data.offline.client_id, size) == 0) {
  143. Log_i("client_id(%s), offline result %d", client_id, result);
  144. gateway->gateway_data.offline.result = result;
  145. }
  146. } else if(strncmp(type, GATEWAY_BIND_OP_STR, sizeof(GATEWAY_BIND_OP_STR) - 1) == 0) {
  147. if (strncmp(client_id, gateway->gateway_data.bind.client_id, size) == 0) {
  148. gateway->gateway_data.bind.result = (result > 0)? -result: result;
  149. Log_i("client_id(%s), bind result %d", client_id, gateway->gateway_data.bind.result);
  150. }
  151. } else if(strncmp(type, GATEWAY_UNBIND_OP_STR, sizeof(GATEWAY_UNBIND_OP_STR) - 1) == 0) {
  152. if (strncmp(client_id, gateway->gateway_data.unbind.client_id, size) == 0) {
  153. gateway->gateway_data.unbind.result = (result > 0)? -result: result;
  154. Log_i("client_id(%s), unbind result %d", client_id, gateway->gateway_data.unbind.result);
  155. }
  156. }
  157. HAL_Free(type);
  158. HAL_Free(devices);
  159. HAL_Free(product_id);
  160. HAL_Free(device_name);
  161. return;
  162. }
  163. int gateway_subscribe_unsubscribe_topic(Gateway *gateway, char *topic_filter, SubscribeParams *params, int is_subscribe)
  164. {
  165. int rc = 0;
  166. int loop_count = 0;
  167. uint32_t status = -1;
  168. POINTER_SANITY_CHECK(gateway, QCLOUD_ERR_INVAL);
  169. POINTER_SANITY_CHECK(params, QCLOUD_ERR_INVAL);
  170. STRING_PTR_SANITY_CHECK(topic_filter, QCLOUD_ERR_INVAL);
  171. params->qos = QOS1;
  172. gateway->gateway_data.sync_status = status;
  173. if (is_subscribe) {
  174. /* subscribe */
  175. rc = IOT_MQTT_Subscribe(gateway->mqtt, topic_filter, params);
  176. } else {
  177. /* unsubscribe */
  178. rc = IOT_MQTT_Unsubscribe(gateway->mqtt, topic_filter);
  179. }
  180. if (rc < 0) {
  181. Log_e("subscribe or un(%d), result(%d)", is_subscribe, rc);
  182. IOT_FUNC_EXIT_RC(rc);
  183. }
  184. gateway->gateway_data.sync_status = status = rc;
  185. while (status == gateway->gateway_data.sync_status) {
  186. if (loop_count > GATEWAY_LOOP_MAX_COUNT) {
  187. Log_i("loop max count, time out");
  188. IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE);
  189. }
  190. IOT_Gateway_Yield(gateway, 200);
  191. loop_count++;
  192. }
  193. if (gateway->gateway_data.sync_status != 0) {
  194. Log_e("gateway->gateway_data.sync_status(%u) != 0", gateway->gateway_data.sync_status);
  195. IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE);
  196. }
  197. IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
  198. }
  199. int gateway_subscribe_unsubscribe_default(Gateway *gateway, GatewayParam *param)
  200. {
  201. int rc = 0;
  202. int size = 0;
  203. char topic_filter[MAX_SIZE_OF_CLOUD_TOPIC + 1] = {0};
  204. SubscribeParams subscribe_params = DEFAULT_SUB_PARAMS;
  205. POINTER_SANITY_CHECK(param, QCLOUD_ERR_INVAL);
  206. STRING_PTR_SANITY_CHECK(param->product_id, QCLOUD_ERR_INVAL);
  207. STRING_PTR_SANITY_CHECK(param->device_name, QCLOUD_ERR_INVAL);
  208. // subscribe online/offline operation reslut
  209. size = HAL_Snprintf(topic_filter, MAX_SIZE_OF_CLOUD_TOPIC + 1, GATEWAY_TOPIC_OPERATION_RESULT_FMT,
  210. param->product_id, param->device_name);
  211. if (size < 0 || size > MAX_SIZE_OF_CLOUD_TOPIC) {
  212. Log_e("buf size < topic length!");
  213. IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE);
  214. }
  215. subscribe_params.on_message_handler = _gateway_message_handler;
  216. rc = gateway_subscribe_unsubscribe_topic(gateway, topic_filter, &subscribe_params, IOT_TRUE);
  217. if (QCLOUD_RET_SUCCESS != rc) {
  218. IOT_FUNC_EXIT_RC(rc);
  219. }
  220. IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
  221. }
  222. SubdevSession *subdev_find_session(Gateway *gateway, char *product_id, char *device_name)
  223. {
  224. SubdevSession *session = NULL;
  225. POINTER_SANITY_CHECK(gateway, NULL);
  226. STRING_PTR_SANITY_CHECK(product_id, NULL);
  227. STRING_PTR_SANITY_CHECK(device_name, NULL);
  228. session = gateway->session_list;
  229. /* session is exist */
  230. while (session) {
  231. if (0 == strcmp(session->product_id, product_id) && 0 == strcmp(session->device_name, device_name)) {
  232. IOT_FUNC_EXIT_RC(session);
  233. }
  234. session = session->next;
  235. }
  236. IOT_FUNC_EXIT_RC(NULL);
  237. }
  238. SubdevSession *subdev_add_session(Gateway *gateway, char *product_id, char *device_name)
  239. {
  240. SubdevSession *session = NULL;
  241. POINTER_SANITY_CHECK(gateway, NULL);
  242. STRING_PTR_SANITY_CHECK(product_id, NULL);
  243. STRING_PTR_SANITY_CHECK(device_name, NULL);
  244. session = HAL_Malloc(sizeof(SubdevSession));
  245. if (session == NULL) {
  246. Log_e("Not enough memory");
  247. IOT_FUNC_EXIT_RC(NULL);
  248. }
  249. /* add session to list */
  250. session->next = gateway->session_list;
  251. gateway->session_list = session;
  252. int size = strlen(product_id);
  253. strncpy(session->product_id, product_id, size);
  254. session->product_id[size] = '\0';
  255. size = strlen(device_name);
  256. strncpy(session->device_name, device_name, size);
  257. session->device_name[size] = '\0';
  258. session->session_status = SUBDEV_SEESION_STATUS_INIT;
  259. IOT_FUNC_EXIT_RC(session);
  260. }
  261. int subdev_remove_session(Gateway *gateway, char *product_id, char *device_name)
  262. {
  263. SubdevSession *cur_session = NULL;
  264. SubdevSession *pre_session = NULL;
  265. POINTER_SANITY_CHECK(gateway, QCLOUD_ERR_FAILURE);
  266. STRING_PTR_SANITY_CHECK(product_id, QCLOUD_ERR_FAILURE);
  267. STRING_PTR_SANITY_CHECK(device_name, QCLOUD_ERR_FAILURE);
  268. pre_session = cur_session = gateway->session_list;
  269. if (NULL == cur_session) {
  270. Log_e("session list is empty");
  271. IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
  272. }
  273. /* session is exist */
  274. while (cur_session) {
  275. if (0 == strcmp(cur_session->product_id, product_id) && 0 == strcmp(cur_session->device_name, device_name)) {
  276. if (cur_session == gateway->session_list) {
  277. gateway->session_list = cur_session->next;
  278. } else {
  279. pre_session->next = cur_session->next;
  280. }
  281. HAL_Free(cur_session);
  282. IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
  283. }
  284. pre_session = cur_session;
  285. cur_session = cur_session->next;
  286. }
  287. IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE);
  288. }
  289. int gateway_publish_sync(Gateway *gateway, char *topic, PublishParams *params, int32_t *result)
  290. {
  291. int rc = 0;
  292. int loop_count = 0;
  293. int32_t res = *result;
  294. POINTER_SANITY_CHECK(gateway, QCLOUD_ERR_INVAL);
  295. rc = IOT_Gateway_Publish(gateway, topic, params);
  296. if (rc < 0) {
  297. Log_e("publish fail.");
  298. IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE);
  299. }
  300. /* wait for response */
  301. while (res == *result) {
  302. if (loop_count > GATEWAY_LOOP_MAX_COUNT) {
  303. Log_i("loop max count, time out.");
  304. IOT_FUNC_EXIT_RC(QCLOUD_ERR_GATEWAY_SESSION_TIMEOUT);
  305. }
  306. #ifdef MULTITHREAD_ENABLED
  307. if((gateway->yield_thread_running)) {
  308. HAL_SleepMs(200);
  309. } else
  310. #endif
  311. {
  312. IOT_Gateway_Yield(gateway, 200);
  313. }
  314. loop_count++;
  315. }
  316. if (*result != 0) {
  317. IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE);
  318. }
  319. IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
  320. }
  321. #ifdef AUTH_MODE_CERT
  322. static int gen_key_from_cert_file(const char *file_path, char *keybuff, int buff_len)
  323. {
  324. QFILE fp;
  325. uint32_t length;
  326. int ret = QCLOUD_RET_SUCCESS;
  327. fp = ql_fopen(file_path, "r");
  328. if (fp <0) {
  329. Log_e("fail to open cert file %s", file_path);
  330. return QCLOUD_ERR_FAILURE;
  331. }
  332. #ifdef QUECTEL_OPEN_CPU
  333. ql_fseek(fp, 0L, SEEK_END);
  334. length = ql_ftell(fp);
  335. #else
  336. fseek(fp, 0L, SEEK_END);
  337. length = ftell(fp);
  338. #endif
  339. uint8_t *data = HAL_Malloc(length + 1);
  340. if(!data) {
  341. Log_e("malloc mem err");
  342. return QCLOUD_ERR_MALLOC;
  343. }
  344. ql_fseek(fp, 0, SEEK_SET);
  345. if(length != ql_fread(data, 1, length, fp)) {
  346. Log_e("read data len fail");
  347. ret = QCLOUD_ERR_FAILURE;
  348. goto exit;
  349. }
  350. utils_md5_str(data, length, (uint8_t *)keybuff);
  351. Log_d("sign key: %s", keybuff);
  352. exit:
  353. HAL_Free(data);
  354. #ifdef QUECTEL_OPEN_CPU
  355. ql_fclose(fp);
  356. #else
  357. fclose(fp);
  358. #endif
  359. return ret;
  360. }
  361. #endif
  362. int subdev_bind_hmac_sha1_cal(DeviceInfo *pDevInfo, char *signout, int max_signlen, int nonce, long timestamp)
  363. {
  364. int text_len,ret;
  365. size_t olen = 0;
  366. char * pSignText = NULL;
  367. const char *sign_fmt = "%s%s;%d;%d"; //${product_id}${device_name};${random};${expiration_time}
  368. /*format sign data*/
  369. text_len = strlen(sign_fmt) + strlen(pDevInfo->device_name) + strlen(pDevInfo->product_id) + sizeof(int) + sizeof(long) + 10;
  370. pSignText = HAL_Malloc(text_len);
  371. if (pSignText == NULL) {
  372. Log_e("malloc sign source buff fail");
  373. return QCLOUD_ERR_FAILURE;
  374. }
  375. memset(pSignText, 0, text_len);
  376. HAL_Snprintf((char *)pSignText, text_len, sign_fmt, pDevInfo->product_id, pDevInfo->device_name, nonce, timestamp);
  377. //gen digest key
  378. char key[BIND_SIGN_KEY_SIZE + 1] = {0};
  379. #ifdef AUTH_MODE_CERT
  380. ret = gen_key_from_cert_file(pDevInfo->dev_cert_file_name, key, BIND_SIGN_KEY_SIZE);
  381. if(QCLOUD_RET_SUCCESS != ret) {
  382. Log_e("gen key from cert file fail, ret:%d",ret);
  383. HAL_Free(pSignText);
  384. return ret;
  385. }
  386. #else
  387. strncpy(key, pDevInfo->device_secret, strlen(pDevInfo->device_secret));
  388. #endif
  389. /*cal hmac sha1*/
  390. char sign[SUBDEV_BIND_SIGN_LEN] = {0};
  391. int sign_len = utils_hmac_sha1_hex(pSignText, strlen(pSignText), sign, key, strlen(key));
  392. /*base64 encode*/
  393. ret = qcloud_iot_utils_base64encode((uint8_t *)signout, max_signlen, &olen, (const uint8_t *)sign, sign_len);
  394. HAL_Free(pSignText);
  395. return (olen > max_signlen) ? QCLOUD_ERR_FAILURE : ret;
  396. }