ota_mqtt-example.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. *
  4. * Again edit by rt-thread group
  5. * Change Logs:
  6. * Date Author Notes
  7. * 2019-07-21 MurphyZhao first edit
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdarg.h>
  13. #include "infra_compat.h"
  14. #include "mqtt_api.h"
  15. #include "ota_api.h"
  16. #include "rtthread.h"
  17. void *HAL_Malloc(uint32_t size);
  18. void HAL_Free(void *ptr);
  19. void HAL_Printf(const char *fmt, ...);
  20. int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN]);
  21. int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN]);
  22. int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN]);
  23. void HAL_SleepMs(uint32_t ms);
  24. char g_product_key[IOTX_PRODUCT_KEY_LEN + 1];
  25. char g_product_secret[IOTX_PRODUCT_SECRET_LEN + 1];
  26. char g_device_name[IOTX_DEVICE_NAME_LEN + 1];
  27. char g_device_secret[IOTX_DEVICE_SECRET_LEN + 1];
  28. #define OTA_MQTT_MSGLEN (2048)
  29. #define EXAMPLE_TRACE(fmt, ...) \
  30. do { \
  31. HAL_Printf("%s|%03d :: ", __func__, __LINE__); \
  32. HAL_Printf(fmt, ##__VA_ARGS__); \
  33. HAL_Printf("%s", "\r\n"); \
  34. } while(0)
  35. static void event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
  36. {
  37. uintptr_t packet_id = (uintptr_t)msg->msg;
  38. iotx_mqtt_topic_info_pt topic_info = (iotx_mqtt_topic_info_pt)msg->msg;
  39. switch (msg->event_type) {
  40. case IOTX_MQTT_EVENT_UNDEF:
  41. EXAMPLE_TRACE("undefined event occur.");
  42. break;
  43. case IOTX_MQTT_EVENT_DISCONNECT:
  44. EXAMPLE_TRACE("MQTT disconnect.");
  45. break;
  46. case IOTX_MQTT_EVENT_RECONNECT:
  47. EXAMPLE_TRACE("MQTT reconnect.");
  48. break;
  49. case IOTX_MQTT_EVENT_SUBCRIBE_SUCCESS:
  50. EXAMPLE_TRACE("subscribe success, packet-id=%u", (unsigned int)packet_id);
  51. break;
  52. case IOTX_MQTT_EVENT_SUBCRIBE_TIMEOUT:
  53. EXAMPLE_TRACE("subscribe wait ack timeout, packet-id=%u", (unsigned int)packet_id);
  54. break;
  55. case IOTX_MQTT_EVENT_SUBCRIBE_NACK:
  56. EXAMPLE_TRACE("subscribe nack, packet-id=%u", (unsigned int)packet_id);
  57. break;
  58. case IOTX_MQTT_EVENT_UNSUBCRIBE_SUCCESS:
  59. EXAMPLE_TRACE("unsubscribe success, packet-id=%u", (unsigned int)packet_id);
  60. break;
  61. case IOTX_MQTT_EVENT_UNSUBCRIBE_TIMEOUT:
  62. EXAMPLE_TRACE("unsubscribe timeout, packet-id=%u", (unsigned int)packet_id);
  63. break;
  64. case IOTX_MQTT_EVENT_UNSUBCRIBE_NACK:
  65. EXAMPLE_TRACE("unsubscribe nack, packet-id=%u", (unsigned int)packet_id);
  66. break;
  67. case IOTX_MQTT_EVENT_PUBLISH_SUCCESS:
  68. EXAMPLE_TRACE("publish success, packet-id=%u", (unsigned int)packet_id);
  69. break;
  70. case IOTX_MQTT_EVENT_PUBLISH_TIMEOUT:
  71. EXAMPLE_TRACE("publish timeout, packet-id=%u", (unsigned int)packet_id);
  72. break;
  73. case IOTX_MQTT_EVENT_PUBLISH_NACK:
  74. EXAMPLE_TRACE("publish nack, packet-id=%u", (unsigned int)packet_id);
  75. break;
  76. case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
  77. EXAMPLE_TRACE("topic message arrived but without any related handle: topic=%.*s, topic_msg=%.*s",
  78. topic_info->topic_len,
  79. topic_info->ptopic,
  80. topic_info->payload_len,
  81. topic_info->payload);
  82. break;
  83. default:
  84. EXAMPLE_TRACE("Should NOT arrive here.");
  85. break;
  86. }
  87. }
  88. static int _ota_mqtt_client(void)
  89. {
  90. #define OTA_BUF_LEN (5000)
  91. int rc = 0, ota_over = 0;
  92. void *pclient = NULL, *h_ota = NULL;
  93. iotx_conn_info_pt pconn_info;
  94. iotx_mqtt_param_t mqtt_params;
  95. char *msg_buf = NULL, *msg_readbuf = NULL;
  96. // FILE *fp;
  97. char buf_ota[OTA_BUF_LEN];
  98. // if (NULL == (fp = fopen("ota.bin", "wb+"))) {
  99. // EXAMPLE_TRACE("open file failed");
  100. // goto do_exit;
  101. // }
  102. if (NULL == (msg_buf = (char *)HAL_Malloc(OTA_MQTT_MSGLEN))) {
  103. EXAMPLE_TRACE("not enough memory");
  104. rc = -1;
  105. goto do_exit;
  106. }
  107. if (NULL == (msg_readbuf = (char *)HAL_Malloc(OTA_MQTT_MSGLEN))) {
  108. EXAMPLE_TRACE("not enough memory");
  109. rc = -1;
  110. goto do_exit;
  111. }
  112. /**< get device info*/
  113. HAL_GetProductKey(g_product_key);
  114. HAL_GetDeviceName(g_device_name);
  115. HAL_GetDeviceSecret(g_device_secret);
  116. /**< end*/
  117. /* Device AUTH */
  118. if (0 != IOT_SetupConnInfo(g_product_key, g_device_name, g_device_secret, (void **)&pconn_info)) {
  119. EXAMPLE_TRACE("AUTH request failed!");
  120. rc = -1;
  121. goto do_exit;
  122. }
  123. /* Initialize MQTT parameter */
  124. memset(&mqtt_params, 0x0, sizeof(mqtt_params));
  125. mqtt_params.port = pconn_info->port;
  126. mqtt_params.host = pconn_info->host_name;
  127. mqtt_params.client_id = pconn_info->client_id;
  128. mqtt_params.username = pconn_info->username;
  129. mqtt_params.password = pconn_info->password;
  130. mqtt_params.pub_key = pconn_info->pub_key;
  131. mqtt_params.request_timeout_ms = 2000;
  132. mqtt_params.clean_session = 0;
  133. mqtt_params.keepalive_interval_ms = 60000;
  134. mqtt_params.read_buf_size = OTA_MQTT_MSGLEN;
  135. mqtt_params.write_buf_size = OTA_MQTT_MSGLEN;
  136. mqtt_params.handle_event.h_fp = event_handle;
  137. mqtt_params.handle_event.pcontext = NULL;
  138. /* Construct a MQTT client with specify parameter */
  139. pclient = IOT_MQTT_Construct(&mqtt_params);
  140. if (NULL == pclient) {
  141. EXAMPLE_TRACE("MQTT construct failed");
  142. rc = -1;
  143. goto do_exit;
  144. }
  145. h_ota = IOT_OTA_Init(g_product_key, g_device_name, pclient);
  146. if (NULL == h_ota) {
  147. rc = -1;
  148. EXAMPLE_TRACE("initialize OTA failed");
  149. goto do_exit;
  150. }
  151. /* if (0 != IOT_OTA_ReportVersion(h_ota, "iotx_ver_1.1.0")) { */
  152. /* rc = -1; */
  153. /* EXAMPLE_TRACE("report OTA version failed"); */
  154. /* goto do_exit; */
  155. /* } */
  156. HAL_SleepMs(1000);
  157. do {
  158. uint32_t firmware_valid;
  159. EXAMPLE_TRACE("wait ota upgrade command....");
  160. /* handle the MQTT packet received from TCP or SSL connection */
  161. IOT_MQTT_Yield(pclient, 200);
  162. if (IOT_OTA_IsFetching(h_ota)) {
  163. uint32_t last_percent = 0, percent = 0;
  164. char md5sum[33];
  165. char version[128] = {0};
  166. uint32_t len, size_downloaded, size_file;
  167. do {
  168. len = IOT_OTA_FetchYield(h_ota, buf_ota, OTA_BUF_LEN, 1);
  169. if (len > 0) {
  170. // if (1 != fwrite(buf_ota, len, 1, fp)) {
  171. // EXAMPLE_TRACE("write data to file failed");
  172. // rc = -1;
  173. // break;
  174. // }
  175. EXAMPLE_TRACE("Here write OTA data to file or flash....");
  176. } else {
  177. IOT_OTA_ReportProgress(h_ota, IOT_OTAP_FETCH_FAILED, NULL);
  178. EXAMPLE_TRACE("ota fetch fail");
  179. }
  180. /* get OTA information */
  181. IOT_OTA_Ioctl(h_ota, IOT_OTAG_FETCHED_SIZE, &size_downloaded, 4);
  182. IOT_OTA_Ioctl(h_ota, IOT_OTAG_FILE_SIZE, &size_file, 4);
  183. IOT_OTA_Ioctl(h_ota, IOT_OTAG_MD5SUM, md5sum, 33);
  184. IOT_OTA_Ioctl(h_ota, IOT_OTAG_VERSION, version, 128);
  185. last_percent = percent;
  186. percent = (size_downloaded * 100) / size_file;
  187. if (percent - last_percent > 0) {
  188. IOT_OTA_ReportProgress(h_ota, percent, NULL);
  189. IOT_OTA_ReportProgress(h_ota, percent, "hello");
  190. }
  191. IOT_MQTT_Yield(pclient, 100);
  192. } while (!IOT_OTA_IsFetchFinish(h_ota));
  193. IOT_OTA_Ioctl(h_ota, IOT_OTAG_CHECK_FIRMWARE, &firmware_valid, 4);
  194. if (0 == firmware_valid) {
  195. EXAMPLE_TRACE("The firmware is invalid");
  196. } else {
  197. EXAMPLE_TRACE("The firmware is valid");
  198. }
  199. ota_over = 1;
  200. }
  201. HAL_SleepMs(2000);
  202. } while (!ota_over);
  203. HAL_SleepMs(200);
  204. do_exit:
  205. if (NULL != h_ota) {
  206. IOT_OTA_Deinit(h_ota);
  207. }
  208. if (NULL != pclient) {
  209. IOT_MQTT_Destroy(&pclient);
  210. }
  211. if (NULL != msg_buf) {
  212. HAL_Free(msg_buf);
  213. }
  214. if (NULL != msg_readbuf) {
  215. HAL_Free(msg_readbuf);
  216. }
  217. // if (NULL != fp) {
  218. // fclose(fp);
  219. // }
  220. return rc;
  221. }
  222. static int ota_example_main(int argc, char *argv[])
  223. {
  224. EXAMPLE_TRACE("hello main func");
  225. IOT_SetLogLevel(IOT_LOG_DEBUG);
  226. _ota_mqtt_client();
  227. IOT_DumpMemoryStats(IOT_LOG_DEBUG);
  228. EXAMPLE_TRACE("out of sample!");
  229. return 0;
  230. }
  231. #ifdef FINSH_USING_MSH
  232. MSH_CMD_EXPORT_ALIAS(ota_example_main, ali_ota_sample, ali ota sample);
  233. #endif