log_upload.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #include "qcloud_iot_export.h"
  23. #include "qcloud_iot_import.h"
  24. #ifdef LOG_UPLOAD
  25. #include <stdbool.h>
  26. #include <stdint.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "lite-utils.h"
  31. #include "utils_param_check.h"
  32. #include "log_upload.h"
  33. #include "qcloud_iot_common.h"
  34. #include "utils_hmac.h"
  35. #include "utils_httpc.h"
  36. #include "utils_timer.h"
  37. /* log post header format */
  38. #define TIMESTAMP_SIZE (10)
  39. #define SIGNATURE_SIZE (40)
  40. #define CTRL_BYTES_SIZE (4)
  41. // LOG_BUF_FIXED_HEADER_SIZE = 112
  42. #define LOG_BUF_FIXED_HEADER_SIZE \
  43. (SIGNATURE_SIZE + CTRL_BYTES_SIZE + MAX_SIZE_OF_PRODUCT_ID + \
  44. MAX_SIZE_OF_DEVICE_NAME + TIMESTAMP_SIZE)
  45. /* do immediate log update if buffer is lower than this threshold (about two max log item) */
  46. #define LOG_LOW_BUFFER_THRESHOLD (LOG_UPLOAD_BUFFER_SIZE / 4)
  47. #define SIGN_KEY_SIZE (24)
  48. #define POINTER_CHECK_RET_ERR(ptr, err) \
  49. do { \
  50. if (NULL == (ptr)) { \
  51. return (err); \
  52. } \
  53. } while (0)
  54. #define POINTER_CHECK_RET(ptr) \
  55. do { \
  56. if (NULL == (ptr)) { \
  57. return; \
  58. } \
  59. } while (0)
  60. /* Log upload feature switch */
  61. /* To check log http server return msg or not */
  62. #define LOG_CHECK_HTTP_RET_CODE
  63. /*Log http client*/
  64. typedef struct {
  65. const char *url;
  66. const char *ca_crt;
  67. int port;
  68. HTTPClient http; /* http client */
  69. HTTPClientData http_data; /* http client data */
  70. } LogHTTPStruct;
  71. /*Log client*/
  72. typedef struct {
  73. DeviceInfo dev_info;
  74. void *mqtt_client;
  75. LogHTTPStruct *http_client;
  76. bool upload_only_in_comm_err;
  77. char *log_buffer;
  78. uint32_t log_index;
  79. void *lock_buf;
  80. char sign_key[SIGN_KEY_SIZE + 1];
  81. long system_time;
  82. Timer upload_timer;
  83. Timer time_update_timer;
  84. LogSaveFunc save_func;
  85. LogReadFunc read_func;
  86. LogDelFunc del_func;
  87. LogGetSizeFunc get_size_func;
  88. bool log_save_enabled;
  89. bool log_client_init_done;
  90. } Qcloud_IoT_Log;
  91. static Qcloud_IoT_Log *sg_log_client = NULL;
  92. static void _set_log_client(void *client)
  93. {
  94. sg_log_client = client;
  95. }
  96. Qcloud_IoT_Log *get_log_client(void)
  97. {
  98. return sg_log_client;
  99. }
  100. void *get_log_mqtt_client(void)
  101. {
  102. Qcloud_IoT_Log *pLogClient = get_log_client();
  103. if(pLogClient) {
  104. return pLogClient->mqtt_client;
  105. } else {
  106. return NULL;
  107. }
  108. }
  109. void * get_log_dev_info(void)
  110. {
  111. Qcloud_IoT_Log *pLogClient = get_log_client();
  112. if(pLogClient) {
  113. return (void *)&pLogClient->dev_info;
  114. } else {
  115. return NULL;
  116. }
  117. }
  118. bool is_log_uploader_init(void)
  119. {
  120. Qcloud_IoT_Log *pLogClient = get_log_client();
  121. if(pLogClient) {
  122. return pLogClient->log_client_init_done;
  123. } else {
  124. return NULL;
  125. }
  126. }
  127. #ifdef AUTH_MODE_CERT
  128. static int _gen_key_from_cert_file(char *sign_key, const char *file_path)
  129. {
  130. FILE *fp;
  131. int len;
  132. char line_buf[128] = {0};
  133. if ((fp = fopen(file_path, "r")) == NULL) {
  134. UPLOAD_ERR("fail to open cert file %s", file_path);
  135. return -1;
  136. }
  137. /* find the begin line */
  138. do {
  139. if (NULL == fgets(line_buf, sizeof(line_buf), fp)) {
  140. UPLOAD_ERR("fail to fgets file %s", file_path);
  141. return -1;
  142. }
  143. } while (strstr(line_buf, "-----BEGIN ") == NULL);
  144. if (feof(fp)) {
  145. UPLOAD_ERR("invalid cert file %s", file_path);
  146. return -1;
  147. }
  148. if (NULL == fgets(line_buf, sizeof(line_buf), fp)) {
  149. UPLOAD_ERR("fail to fgets file %s", file_path);
  150. return -1;
  151. }
  152. len = strlen(line_buf);
  153. memcpy(sign_key, line_buf, len > SIGN_KEY_SIZE ? SIGN_KEY_SIZE : len);
  154. UPLOAD_DBG("sign key %s", sign_key);
  155. fclose(fp);
  156. return 0;
  157. }
  158. #endif
  159. static void _reset_log_buffer(Qcloud_IoT_Log *pLogClient)
  160. {
  161. pLogClient->log_index = LOG_BUF_FIXED_HEADER_SIZE;
  162. memset(pLogClient->log_buffer + LOG_BUF_FIXED_HEADER_SIZE, 0,\
  163. LOG_UPLOAD_BUFFER_SIZE - LOG_BUF_FIXED_HEADER_SIZE);
  164. }
  165. int init_log_uploader(LogUploadInitParams *init_params)
  166. {
  167. Qcloud_IoT_Log *pLogClient = NULL;
  168. if (is_log_uploader_init()) {
  169. UPLOAD_DBG("log client ");
  170. return QCLOUD_RET_SUCCESS;
  171. }
  172. if (init_params == NULL || init_params->product_id == NULL
  173. || init_params->device_name == NULL || init_params->sign_key == NULL) {
  174. UPLOAD_ERR("invalid init parameters");
  175. return QCLOUD_ERR_INVAL;
  176. }
  177. int key_len = strlen(init_params->sign_key);
  178. if (key_len == 0) {
  179. UPLOAD_ERR("invalid key length");
  180. return QCLOUD_ERR_INVAL;
  181. }
  182. pLogClient = HAL_Malloc(sizeof(Qcloud_IoT_Log));
  183. if (NULL == pLogClient) {
  184. UPLOAD_ERR("allocate for log client failed");
  185. goto err_exit;
  186. }
  187. memset(pLogClient, 0, sizeof(Qcloud_IoT_Log));
  188. memset(pLogClient->dev_info.product_id, '\0', MAX_SIZE_OF_PRODUCT_ID);
  189. memset(pLogClient->dev_info.device_name, '\0', MAX_SIZE_OF_DEVICE_NAME);
  190. strncpy(pLogClient->dev_info.product_id, init_params->product_id, MAX_SIZE_OF_PRODUCT_ID);
  191. strncpy(pLogClient->dev_info.device_name, init_params->device_name, MAX_SIZE_OF_DEVICE_NAME);
  192. pLogClient->mqtt_client = NULL;
  193. pLogClient->system_time = 0;
  194. pLogClient->upload_only_in_comm_err = false;
  195. /* all the call back functions are necessary to handle log save and re-upload*/
  196. if (init_params->save_func != NULL && init_params->read_func != NULL
  197. && init_params->del_func != NULL && init_params->get_size_func) {
  198. pLogClient->save_func = init_params->save_func;
  199. pLogClient->read_func = init_params->read_func;
  200. pLogClient->del_func = init_params->del_func;
  201. pLogClient->get_size_func = init_params->get_size_func;
  202. pLogClient->log_save_enabled = true;
  203. } else {
  204. pLogClient->log_save_enabled = false;
  205. }
  206. InitTimer(&pLogClient->upload_timer);
  207. InitTimer(&pLogClient->time_update_timer);
  208. if ((pLogClient->lock_buf = HAL_MutexCreate()) == NULL) {
  209. UPLOAD_ERR("mutex create failed");
  210. goto err_exit;
  211. }
  212. pLogClient->log_buffer = HAL_Malloc(LOG_UPLOAD_BUFFER_SIZE);
  213. if (pLogClient->log_buffer == NULL) {
  214. UPLOAD_ERR("malloc log buffer failed");
  215. goto err_exit;
  216. }
  217. memset(pLogClient->log_buffer, '#', LOG_BUF_FIXED_HEADER_SIZE);
  218. /*init sign key*/
  219. memset(pLogClient->sign_key, 0, SIGN_KEY_SIZE);
  220. #ifdef AUTH_MODE_CERT
  221. if (_gen_key_from_cert_file(pLogClient->sign_key, init_params->sign_key) != 0) {
  222. UPLOAD_ERR("gen_key_from_file failed");
  223. goto err_exit;
  224. }
  225. pLogClient->log_buffer[SIGNATURE_SIZE] = 'C';
  226. #else
  227. memcpy(pLogClient->sign_key, init_params->sign_key, key_len > SIGN_KEY_SIZE ? SIGN_KEY_SIZE : key_len);
  228. pLogClient->log_buffer[SIGNATURE_SIZE] = 'P';
  229. #endif
  230. memcpy(pLogClient->log_buffer + SIGNATURE_SIZE + CTRL_BYTES_SIZE,\
  231. init_params->product_id, MAX_SIZE_OF_PRODUCT_ID);
  232. memcpy(pLogClient->log_buffer + SIGNATURE_SIZE + CTRL_BYTES_SIZE + MAX_SIZE_OF_PRODUCT_ID,\
  233. init_params->device_name, strlen(init_params->device_name));
  234. pLogClient->http_client = HAL_Malloc(sizeof(LogHTTPStruct));
  235. if (NULL == pLogClient->http_client) {
  236. UPLOAD_ERR("allocate for LogHTTPStruct failed");
  237. goto err_exit;
  238. }
  239. memset(pLogClient->http_client, 0, sizeof(LogHTTPStruct));
  240. /* set http request-header parameter */
  241. pLogClient->http_client->http.header = "Accept:application/json;*/*\r\n";
  242. pLogClient->http_client->url = LOG_UPLOAD_SERVER_URL;
  243. pLogClient->http_client->port = LOG_UPLOAD_SERVER_PORT;
  244. pLogClient->http_client->ca_crt = NULL;
  245. pLogClient->http_client->http.profile_idx = init_params->profile_idx;
  246. _reset_log_buffer(pLogClient);
  247. _set_log_client(pLogClient);
  248. pLogClient->log_client_init_done = true;
  249. return QCLOUD_RET_SUCCESS;
  250. err_exit:
  251. if(pLogClient) {
  252. if(pLogClient->http_client) {
  253. HAL_Free(pLogClient->http_client);
  254. pLogClient->http_client = NULL;
  255. }
  256. if(pLogClient->log_buffer) {
  257. HAL_Free(pLogClient->log_buffer);
  258. pLogClient->log_buffer = NULL;
  259. }
  260. if(pLogClient->lock_buf) {
  261. HAL_MutexDestroy(pLogClient->lock_buf);
  262. pLogClient->lock_buf = NULL;
  263. }
  264. HAL_Free(pLogClient);
  265. pLogClient = NULL;
  266. }
  267. return QCLOUD_ERR_FAILURE;
  268. }
  269. void fini_log_uploader(void)
  270. {
  271. Qcloud_IoT_Log *pLogClient = get_log_client();
  272. POINTER_CHECK_RET(pLogClient);
  273. if (!pLogClient->log_client_init_done) {
  274. return;
  275. }
  276. HAL_MutexLock(pLogClient->lock_buf);
  277. HAL_Free(pLogClient->http_client);
  278. pLogClient->http_client = NULL;
  279. HAL_Free(pLogClient->log_buffer);
  280. pLogClient->log_buffer = NULL;
  281. HAL_MutexUnlock(pLogClient->lock_buf);
  282. HAL_MutexDestroy(pLogClient->lock_buf);
  283. HAL_Free(pLogClient);
  284. pLogClient = NULL;
  285. }
  286. void set_log_mqtt_client(void *client)
  287. {
  288. Qcloud_IoT_Log *pLogClient = get_log_client();
  289. POINTER_CHECK_RET(pLogClient);
  290. if (!pLogClient->log_client_init_done) {
  291. return;
  292. }
  293. pLogClient->mqtt_client = client;
  294. }
  295. void set_log_upload_in_comm_err(bool value)
  296. {
  297. Qcloud_IoT_Log *pLogClient = get_log_client();
  298. POINTER_CHECK_RET(pLogClient);
  299. if (!pLogClient->log_client_init_done) {
  300. return;
  301. }
  302. pLogClient->upload_only_in_comm_err = value;
  303. }
  304. //int append_to_upload_buffer(const char *log_content, size_t log_size)
  305. //{
  306. // Qcloud_IoT_Log *pLogClient = get_log_client();
  307. // POINTER_CHECK_RET_ERR(pLogClient, -1);
  308. // if (!pLogClient->log_client_init_done) {
  309. // return -1;
  310. // }
  311. //
  312. // if (log_content == NULL || log_size == 0) {
  313. // UPLOAD_ERR("invalid log content!");
  314. // return -1;
  315. // }
  316. //
  317. // if (HAL_MutexTryLock(pLogClient->lock_buf) != 0) {
  318. // UPLOAD_ERR("trylock buffer failed!");
  319. // return -1;
  320. // }
  321. //
  322. // if ((pLogClient->log_index + log_size + 1) > LOG_UPLOAD_BUFFER_SIZE) {
  323. // countdown_ms(&pLogClient->upload_timer, 0);
  324. // HAL_MutexUnlock(pLogClient->lock_buf);
  325. // UPLOAD_ERR("log upload buffer is not enough!");
  326. // return -1;
  327. // }
  328. //
  329. // memcpy(pLogClient->log_buffer + pLogClient->log_index, log_content, log_size);
  330. // pLogClient->log_index += log_size;
  331. //
  332. // /* replace \r\n to \n\f as delimiter */
  333. // pLogClient->log_buffer[pLogClient->log_index - 1] = '\f';
  334. // pLogClient->log_buffer[pLogClient->log_index - 2] = '\n';
  335. //
  336. // HAL_MutexUnlock(pLogClient->lock_buf);
  337. // return 0;
  338. //}
  339. void clear_upload_buffer(void)
  340. {
  341. Qcloud_IoT_Log *pLogClient = get_log_client();
  342. POINTER_CHECK_RET(pLogClient);
  343. if (!pLogClient->log_client_init_done) {
  344. return;
  345. }
  346. HAL_MutexLock(pLogClient->lock_buf);
  347. _reset_log_buffer(pLogClient);
  348. HAL_MutexUnlock(pLogClient->lock_buf);
  349. }
  350. static long _get_system_time(Qcloud_IoT_Log *pLogClient)
  351. {
  352. #ifdef SYSTEM_COMM
  353. long sys_time = 0;
  354. int rc = IOT_Get_Sys_Resource(pLogClient->mqtt_client, eRESOURCE_TIME, &pLogClient->dev_info, &sys_time);
  355. if (rc == QCLOUD_RET_SUCCESS)
  356. return sys_time;
  357. else
  358. return 0;
  359. #else
  360. return 0;
  361. #endif
  362. }
  363. static void _update_system_time(Qcloud_IoT_Log *pLogClient)
  364. {
  365. /* to avoid frequent get_system_time */
  366. #define LOG_TIME_UPDATE_INTERVAL 2
  367. if (!expired(&pLogClient->time_update_timer)) {
  368. return;
  369. }
  370. pLogClient->system_time = _get_system_time(pLogClient);
  371. countdown(&pLogClient->time_update_timer, LOG_TIME_UPDATE_INTERVAL);
  372. }
  373. static int _check_server_connection(Qcloud_IoT_Log *pLogClient)
  374. {
  375. int rc;
  376. rc = qcloud_http_client_connect(&pLogClient->http_client->http, pLogClient->http_client->url, \
  377. pLogClient->http_client->port, pLogClient->http_client->ca_crt);
  378. if (rc != QCLOUD_RET_SUCCESS) {
  379. return rc;
  380. }
  381. qcloud_http_client_close(&pLogClient->http_client->http);
  382. return QCLOUD_RET_SUCCESS;
  383. }
  384. #ifdef LOG_CHECK_HTTP_RET_CODE
  385. static bool _get_json_ret_code(char *json, int32_t *res)
  386. {
  387. char *v = LITE_json_value_of("Retcode", json);
  388. if (v == NULL) {
  389. UPLOAD_ERR("Invalid json content: %s", json);
  390. return false;
  391. }
  392. if (LITE_get_int32(res, v) != QCLOUD_RET_SUCCESS) {
  393. UPLOAD_ERR("Invalid json content: %s", json);
  394. HAL_Free(v);
  395. return false;
  396. }
  397. HAL_Free(v);
  398. return true;
  399. }
  400. #endif
  401. static int _post_one_http_to_server(unsigned char profile_idx, char *post_buf, size_t post_size)
  402. {
  403. int rc = 0;
  404. Qcloud_IoT_Log *pLogClient = get_log_client();
  405. POINTER_CHECK_RET_ERR(pLogClient, QCLOUD_ERR_INVAL);
  406. POINTER_CHECK_RET_ERR(pLogClient->http_client, QCLOUD_ERR_INVAL);
  407. if (!pLogClient->log_client_init_done) {
  408. return QCLOUD_ERR_FAILURE;
  409. }
  410. pLogClient->http_client->http.profile_idx = profile_idx;
  411. pLogClient->http_client->http_data.post_content_type = "text/plain;charset=utf-8";
  412. pLogClient->http_client->http_data.post_buf = post_buf;
  413. pLogClient->http_client->http_data.post_buf_len = post_size;
  414. rc = qcloud_http_client_common(&pLogClient->http_client->http, pLogClient->http_client->url, \
  415. pLogClient->http_client->port, pLogClient->http_client->ca_crt, \
  416. HTTP_POST, &pLogClient->http_client->http_data);
  417. if (rc != QCLOUD_RET_SUCCESS) {
  418. UPLOAD_ERR("qcloud_http_client_common failed, rc = %d", rc);
  419. return rc;
  420. }
  421. UPLOAD_DBG("Log client POST size: %d", post_size);
  422. #ifdef LOG_CHECK_HTTP_RET_CODE
  423. /* TODO: handle recv data from log server */
  424. #define HTTP_RET_JSON_LENGTH 256
  425. #define HTTP_WAIT_RET_TIMEOUT_MS 1000
  426. char buf[HTTP_RET_JSON_LENGTH] = {0};
  427. pLogClient->http_client->http_data.response_buf = buf;
  428. pLogClient->http_client->http_data.response_buf_len = sizeof(buf);
  429. rc = qcloud_http_recv_data(&pLogClient->http_client->http, HTTP_WAIT_RET_TIMEOUT_MS, &pLogClient->http_client->http_data);
  430. if (QCLOUD_RET_SUCCESS != rc) {
  431. UPLOAD_ERR("qcloud_http_recv_data failed, rc = %d", rc);
  432. } else {
  433. int32_t ret = -1;
  434. buf[HTTP_RET_JSON_LENGTH - 1] = '\0'; // json_parse relies on a string
  435. if (strlen(buf) > 0 && _get_json_ret_code(buf, &ret) && ret == 0) {
  436. UPLOAD_DBG("Log server return SUCCESS: %s", buf);
  437. } else {
  438. UPLOAD_ERR("Log server return FAIL(%d): %s", ret, buf);
  439. rc = QCLOUD_ERR_HTTP;
  440. }
  441. }
  442. #endif
  443. qcloud_http_client_close(&pLogClient->http_client->http);
  444. return rc;
  445. }
  446. static void update_time_and_signature(char *log_buf, size_t log_size)
  447. {
  448. char timestamp[TIMESTAMP_SIZE + 1] = {0};
  449. char signature[SIGNATURE_SIZE + 1] = {0};
  450. Qcloud_IoT_Log *pLogClient = get_log_client();
  451. POINTER_CHECK_RET(pLogClient);
  452. if (!pLogClient->log_client_init_done) {
  453. return;
  454. }
  455. /* get system time from IoT hub first */
  456. _update_system_time(pLogClient);
  457. /* record the timestamp for this log uploading */
  458. HAL_Snprintf(timestamp, TIMESTAMP_SIZE + 1, "%010ld", pLogClient->system_time);
  459. memcpy(log_buf + LOG_BUF_FIXED_HEADER_SIZE - TIMESTAMP_SIZE, timestamp, strlen(timestamp));
  460. /* signature of this log uploading */
  461. utils_hmac_sha1(log_buf + SIGNATURE_SIZE, log_size - SIGNATURE_SIZE, signature, pLogClient->sign_key, strlen(pLogClient->sign_key));
  462. memcpy(log_buf, signature, SIGNATURE_SIZE);
  463. }
  464. static int post_log_to_server(unsigned char profile_idx, char *post_buf, size_t post_size, size_t *actual_post_payload)
  465. {
  466. #define LOG_DELIMITER "\n\f"
  467. int ret = QCLOUD_RET_SUCCESS;
  468. /* one shot upload */
  469. if (post_size < MAX_HTTP_LOG_POST_SIZE) {
  470. update_time_and_signature(post_buf, post_size);
  471. ret = _post_one_http_to_server(profile_idx, post_buf, post_size);
  472. if (QCLOUD_RET_SUCCESS == ret) {
  473. *actual_post_payload = post_size - LOG_BUF_FIXED_HEADER_SIZE;
  474. } else {
  475. UPLOAD_ERR("one time log send failed");
  476. *actual_post_payload = 0;
  477. }
  478. return ret;
  479. }
  480. /* Log size is larger than one HTTP post size */
  481. /* Fragment the log and upload multi-times */
  482. UPLOAD_DBG("to post large log size %d", post_size);
  483. *actual_post_payload = 0;
  484. size_t delimiter_len = strlen(LOG_DELIMITER);
  485. size_t orig_post_size = post_size;
  486. size_t post_payload, upload_size, possible_size;
  487. do {
  488. char *next_log_buf = NULL;
  489. possible_size = 0;
  490. while (possible_size < MAX_HTTP_LOG_POST_SIZE) {
  491. /*remember last valid position */
  492. upload_size = possible_size;
  493. /* locate the delimiter */
  494. next_log_buf = strstr(post_buf + upload_size, LOG_DELIMITER);
  495. if (next_log_buf == NULL) {
  496. UPLOAD_ERR("Invalid log delimiter. Total sent: %d. Left: %d",
  497. *actual_post_payload + LOG_BUF_FIXED_HEADER_SIZE, post_size);
  498. return QCLOUD_ERR_INVAL;
  499. }
  500. possible_size = (size_t)(next_log_buf - post_buf + delimiter_len);
  501. /* end of log */
  502. if (next_log_buf[delimiter_len] == 0 && possible_size < MAX_HTTP_LOG_POST_SIZE) {
  503. upload_size = possible_size;
  504. break;
  505. }
  506. }
  507. if (upload_size == 0) {
  508. UPLOAD_ERR("Upload size should not be 0! Total sent: %d. Left: %d",
  509. *actual_post_payload + LOG_BUF_FIXED_HEADER_SIZE, post_size);
  510. return QCLOUD_ERR_FAILURE;
  511. }
  512. update_time_and_signature(post_buf, upload_size);
  513. ret = _post_one_http_to_server(profile_idx,post_buf, upload_size);
  514. if (QCLOUD_RET_SUCCESS != ret) {
  515. UPLOAD_ERR("Send log failed. Total sent: %d. Left: %d", *actual_post_payload + LOG_BUF_FIXED_HEADER_SIZE,
  516. post_size);
  517. return QCLOUD_ERR_FAILURE;
  518. }
  519. /* move the left log forward and do next upload */
  520. memmove(post_buf + LOG_BUF_FIXED_HEADER_SIZE, post_buf + upload_size, post_size - upload_size);
  521. post_payload = upload_size - LOG_BUF_FIXED_HEADER_SIZE;
  522. post_size -= post_payload;
  523. *actual_post_payload += post_payload;
  524. memset(post_buf + post_size, 0, orig_post_size - post_size);
  525. UPLOAD_DBG("post log %d OK. Total sent: %d. Left: %d", upload_size,
  526. *actual_post_payload + LOG_BUF_FIXED_HEADER_SIZE, post_size);
  527. } while (post_size > LOG_BUF_FIXED_HEADER_SIZE);
  528. return QCLOUD_RET_SUCCESS;
  529. }
  530. static int _save_log(char *log_buf, size_t log_size)
  531. {
  532. int rc = 0;
  533. size_t write_size, current_size;
  534. Qcloud_IoT_Log *pLogClient = get_log_client();
  535. POINTER_CHECK_RET_ERR(pLogClient, QCLOUD_ERR_INVAL);
  536. if (!pLogClient->log_client_init_done) {
  537. return QCLOUD_ERR_FAILURE;
  538. }
  539. current_size = pLogClient->get_size_func();
  540. /* overwrite the previous saved log to avoid too many saved logs */
  541. if ((current_size + log_size) > MAX_LOG_SAVE_SIZE) {
  542. UPLOAD_ERR("overwrite the previous saved log. %d", current_size);
  543. rc = pLogClient->del_func();
  544. if (rc) {
  545. Log_e("fail to delete previous log");
  546. }
  547. }
  548. write_size = pLogClient->save_func(log_buf, log_size);
  549. if (write_size != log_size) {
  550. Log_e("fail to save log. RC %d - log size %d", write_size, log_size);
  551. rc = -1;
  552. } else
  553. rc = 0;
  554. return rc;
  555. }
  556. static int _handle_saved_log(void)
  557. {
  558. int rc = QCLOUD_RET_SUCCESS;
  559. Qcloud_IoT_Log *pLogClient = get_log_client();
  560. POINTER_CHECK_RET_ERR(pLogClient, QCLOUD_ERR_INVAL);
  561. if (!pLogClient->log_client_init_done) {
  562. return QCLOUD_ERR_FAILURE;
  563. }
  564. size_t whole_log_size = pLogClient->get_size_func();
  565. if (whole_log_size > 0) {
  566. /* only do the job when connection is OK */
  567. if (_check_server_connection(pLogClient) != QCLOUD_RET_SUCCESS)
  568. return QCLOUD_ERR_FAILURE;
  569. size_t buf_size = whole_log_size + LOG_BUF_FIXED_HEADER_SIZE + 1;
  570. char * log_buf = HAL_Malloc(buf_size);
  571. if (log_buf != NULL) {
  572. /* read the whole log to buffer */
  573. size_t read_len = pLogClient->read_func(log_buf + LOG_BUF_FIXED_HEADER_SIZE, whole_log_size);
  574. if (read_len == whole_log_size) {
  575. size_t upload_size = whole_log_size + LOG_BUF_FIXED_HEADER_SIZE;
  576. /* copy header from global log buffer */
  577. memcpy(log_buf, pLogClient->log_buffer, LOG_BUF_FIXED_HEADER_SIZE);
  578. log_buf[buf_size - 1] = 0;
  579. size_t actual_post_payload;
  580. rc = post_log_to_server(pLogClient->http_client->http.profile_idx, log_buf, upload_size, &actual_post_payload);
  581. if (rc == QCLOUD_RET_SUCCESS || rc == QCLOUD_ERR_INVAL) {
  582. Log_d("handle saved log done! Size: %d. upload paylod: %d", whole_log_size, actual_post_payload);
  583. pLogClient->del_func();
  584. }
  585. HAL_Free(log_buf);
  586. } else {
  587. Log_e("fail to read whole saved log. Size: %u - read: %u", whole_log_size, read_len);
  588. HAL_Free(log_buf);
  589. return QCLOUD_ERR_FAILURE;
  590. }
  591. } else {
  592. Log_e("Malloc failed, size: %u", buf_size);
  593. return QCLOUD_ERR_FAILURE;
  594. }
  595. }
  596. return rc;
  597. }
  598. static bool _check_force_upload( bool force_upload)
  599. {
  600. Qcloud_IoT_Log *pLogClient = get_log_client();
  601. if (!force_upload) {
  602. /* Double check if the buffer is low */
  603. HAL_MutexLock(pLogClient->lock_buf);
  604. bool is_low_buffer = (LOG_UPLOAD_BUFFER_SIZE - pLogClient->log_index) < LOG_LOW_BUFFER_THRESHOLD ? true : false;
  605. /* force_upload is false and upload_only_in_comm_err is true */
  606. if (pLogClient->upload_only_in_comm_err) {
  607. /* buffer is low but we couldn't upload now, reset buffer */
  608. if (is_low_buffer) {
  609. _reset_log_buffer(pLogClient);
  610. }
  611. HAL_MutexUnlock(pLogClient->lock_buf);
  612. countdown_ms(&pLogClient->upload_timer, LOG_UPLOAD_INTERVAL_MS);
  613. return false;
  614. }
  615. HAL_MutexUnlock(pLogClient->lock_buf);
  616. if (is_low_buffer) {
  617. /* buffer is low, handle it right now */
  618. return true;
  619. } else {
  620. return expired(&pLogClient->upload_timer);
  621. }
  622. } else {
  623. return true;
  624. }
  625. }
  626. int do_log_upload(bool force_upload)
  627. {
  628. int rc;
  629. int upload_log_size = 0;
  630. static bool unhandle_saved_log = true;
  631. Qcloud_IoT_Log *pLogClient = get_log_client();
  632. POINTER_CHECK_RET_ERR(pLogClient, QCLOUD_ERR_INVAL);
  633. if (!pLogClient->log_client_init_done) {
  634. return QCLOUD_ERR_FAILURE;
  635. }
  636. /* double check force upload */
  637. if (!_check_force_upload(force_upload))
  638. return QCLOUD_RET_SUCCESS;
  639. /* handle previously saved log */
  640. if (pLogClient->log_save_enabled && unhandle_saved_log) {
  641. rc = _handle_saved_log();
  642. if (rc == QCLOUD_RET_SUCCESS) {
  643. unhandle_saved_log = false;
  644. }
  645. }
  646. /* no more log in buffer */
  647. if (pLogClient->log_index == LOG_BUF_FIXED_HEADER_SIZE) {
  648. return QCLOUD_RET_SUCCESS;
  649. }
  650. HAL_MutexLock(pLogClient->lock_buf);
  651. upload_log_size = pLogClient->log_index;
  652. HAL_MutexUnlock(pLogClient->lock_buf);
  653. size_t actual_post_payload;
  654. rc = post_log_to_server(pLogClient->http_client->http.profile_idx, pLogClient->log_buffer, upload_log_size, &actual_post_payload);
  655. if (rc != QCLOUD_RET_SUCCESS) {
  656. /* save log via user callbacks when log upload fail */
  657. if (pLogClient->log_save_enabled) {
  658. /* new error logs should have been added, update log size */
  659. HAL_MutexLock(pLogClient->lock_buf);
  660. /* parts of log were uploaded succesfully. Need to move the new logs
  661. * forward */
  662. if (actual_post_payload) {
  663. UPLOAD_DBG("move the new log %d forward", actual_post_payload);
  664. memmove(pLogClient->log_buffer + upload_log_size - actual_post_payload,\
  665. pLogClient->log_buffer + upload_log_size,\
  666. pLogClient->log_index - upload_log_size);
  667. pLogClient->log_index = pLogClient->log_index - actual_post_payload;
  668. memset(pLogClient->log_buffer + pLogClient->log_index, 0,\
  669. LOG_UPLOAD_BUFFER_SIZE - pLogClient->log_index);
  670. }
  671. upload_log_size = pLogClient->log_index;
  672. HAL_MutexUnlock(pLogClient->lock_buf);
  673. _save_log(pLogClient->log_buffer + LOG_BUF_FIXED_HEADER_SIZE,\
  674. upload_log_size - LOG_BUF_FIXED_HEADER_SIZE);
  675. unhandle_saved_log = true;
  676. }
  677. }
  678. /* move the new log during send_log_to_server */
  679. HAL_MutexLock(pLogClient->lock_buf);
  680. if (upload_log_size == pLogClient->log_index) {
  681. _reset_log_buffer(pLogClient);
  682. } else {
  683. memmove(pLogClient->log_buffer + LOG_BUF_FIXED_HEADER_SIZE,\
  684. pLogClient->log_buffer + upload_log_size, pLogClient->log_index - upload_log_size);
  685. pLogClient->log_index = pLogClient->log_index - upload_log_size + LOG_BUF_FIXED_HEADER_SIZE;
  686. memset(pLogClient->log_buffer + pLogClient->log_index, 0,
  687. LOG_UPLOAD_BUFFER_SIZE - pLogClient->log_index);
  688. }
  689. HAL_MutexUnlock(pLogClient->lock_buf);
  690. countdown_ms(&pLogClient->upload_timer, LOG_UPLOAD_INTERVAL_MS);
  691. return QCLOUD_RET_SUCCESS;
  692. }
  693. #endif
  694. #ifdef __cplusplus
  695. }
  696. #endif