ota_sample.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <limits.h>
  18. #include <string.h>
  19. #include "uiot_export.h"
  20. #include "uiot_import.h"
  21. #include "uiot_export_ota.h"
  22. #define OTA_BUF_LEN (1024)
  23. static int running_state = 0;
  24. static void event_handler(void *pClient, void *handle_context, MQTTEventMsg *msg)
  25. {
  26. switch(msg->event_type) {
  27. case MQTT_EVENT_UNDEF:
  28. LOG_INFO("undefined event occur.\n");
  29. break;
  30. case MQTT_EVENT_DISCONNECT:
  31. LOG_INFO("MQTT disconnect.\n");
  32. break;
  33. case MQTT_EVENT_RECONNECT:
  34. LOG_INFO("MQTT reconnect.\n");
  35. break;
  36. case MQTT_EVENT_SUBSCRIBE_SUCCESS:
  37. LOG_INFO("subscribe success.\n");
  38. break;
  39. case MQTT_EVENT_SUBSCRIBE_TIMEOUT:
  40. LOG_INFO("subscribe wait ack timeout.\n");
  41. break;
  42. case MQTT_EVENT_SUBSCRIBE_NACK:
  43. LOG_INFO("subscribe nack.\n");
  44. break;
  45. case MQTT_EVENT_PUBLISH_SUCCESS:
  46. LOG_INFO("publish success.\n");
  47. break;
  48. case MQTT_EVENT_PUBLISH_TIMEOUT:
  49. LOG_INFO("publish timeout.\n");
  50. break;
  51. case MQTT_EVENT_PUBLISH_NACK:
  52. LOG_INFO("publish nack.\n");
  53. break;
  54. default:
  55. LOG_INFO("Should NOT arrive here.\n");
  56. break;
  57. }
  58. }
  59. static int _setup_connect_init_params(MQTTInitParams* initParams)
  60. {
  61. initParams->device_sn = PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN;
  62. initParams->product_sn = PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN;
  63. initParams->device_secret = PKG_USING_UCLOUD_IOT_SDK_DEVICE_SECRET;
  64. initParams->command_timeout = UIOT_MQTT_COMMAND_TIMEOUT;
  65. initParams->keep_alive_interval = UIOT_MQTT_KEEP_ALIVE_INTERNAL;
  66. initParams->auto_connect_enable = 1;
  67. initParams->event_handler.h_fp = event_handler;
  68. return SUCCESS_RET;
  69. }
  70. static void ota_test_thread(void)
  71. {
  72. int rc;
  73. MQTTInitParams init_params = DEFAULT_MQTT_INIT_PARAMS;
  74. rc = _setup_connect_init_params(&init_params);
  75. if (rc != SUCCESS_RET) {
  76. return;
  77. }
  78. void *client = IOT_MQTT_Construct(&init_params);
  79. if (client != NULL) {
  80. LOG_INFO("MQTT Construct Success");
  81. } else {
  82. LOG_ERROR("MQTT Construct Failed");
  83. return;
  84. }
  85. void *h_ota = IOT_OTA_Init(PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN, client);
  86. if (NULL == h_ota) {
  87. IOT_MQTT_Destroy(&client);
  88. LOG_ERROR("init OTA failed");
  89. return;
  90. }
  91. /* Must report version first */
  92. if (IOT_OTA_ReportVersion(h_ota, "1.0.0") < 0) {
  93. LOG_ERROR("report OTA version failed");
  94. return;
  95. }
  96. if (IOT_OTA_RequestFirmware(h_ota, "1.0.0") < 0) {
  97. LOG_ERROR("Request firmware failed");
  98. return;
  99. }
  100. do {
  101. IOT_MQTT_Yield(client, 10);
  102. } while(1);
  103. }
  104. static int ota_test_example(int argc, char **argv)
  105. {
  106. rt_thread_t tid;
  107. int stack_size = 10240;
  108. if (2 == argc)
  109. {
  110. if (!strcmp("start", argv[1]))
  111. {
  112. if (1 == running_state)
  113. {
  114. HAL_Printf("mqtt_ota_example is already running\n");
  115. return 0;
  116. }
  117. }
  118. else if (!strcmp("stop", argv[1]))
  119. {
  120. if (0 == running_state)
  121. {
  122. HAL_Printf("mqtt_ota_example is already stopped\n");
  123. return 0;
  124. }
  125. running_state = 0;
  126. return 0;
  127. }
  128. else
  129. {
  130. HAL_Printf("Usage: mqtt_ota_example start/stop");
  131. return 0;
  132. }
  133. }
  134. else
  135. {
  136. HAL_Printf("Para err, usage: mqtt_ota_example start/stop");
  137. return 0;
  138. }
  139. tid = rt_thread_create("ota_test", (void (*)(void *))ota_test_thread,
  140. NULL, stack_size, RT_THREAD_PRIORITY_MAX / 2 - 1, 100);
  141. if (tid != RT_NULL)
  142. {
  143. rt_thread_startup(tid);
  144. }
  145. return 0;
  146. }
  147. #ifdef RT_USING_FINSH
  148. #include <finsh.h>
  149. FINSH_FUNCTION_EXPORT(ota_test_example, startup mqtt ota example);
  150. #endif
  151. #ifdef FINSH_USING_MSH
  152. MSH_CMD_EXPORT(ota_test_example, startup mqtt ota example);
  153. #endif