ota_coap-example.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2014-2016 Alibaba Group. All rights reserved.
  3. * License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <stdarg.h>
  21. #include "iot_import.h"
  22. #include "iot_export.h"
  23. //#define IOTX_PRODUCT_KEY "21ESeH6RYT7"
  24. //#define IOTX_DEVICE_NAME "ota_demo"
  25. //#define IOTX_DEVICE_SECRET "HTVEzMoQrNLV5CjmjKs2l16aDpmXi1Aa"
  26. //#define IOTX_DEVICE_ID "21ESeH6RYT7.ota_demo"
  27. #define IOTX_PRODUCT_KEY "stMRPCR0yQu"
  28. #define IOTX_DEVICE_NAME "xikan_ota"
  29. #define IOTX_DEVICE_SECRET "XZjhoJclBJpvcEoa8eLYSxnb3ksRVd3W"
  30. #define IOTX_DEVICE_ID "stMRPCR0yQu.xikan_ota"
  31. //#define IOTX_PRE_DTLS_SERVER_URI "coaps://pre.iot-as-coap.cn-shanghai.aliyuncs.com:5684"
  32. #define IOTX_PRE_NOSEC_SERVER_URI "coap://pre.iot-as-coap.cn-shanghai.aliyuncs.com:5683"
  33. //#define IOTX_PRE_NOSEC_SERVER_URI "coap://iot-as-coap.alibaba.net:5683"
  34. #define EXAMPLE_TRACE(fmt, ...) \
  35. do { \
  36. HAL_Printf("%s|%03d :: ", __func__, __LINE__); \
  37. HAL_Printf(fmt, ##__VA_ARGS__); \
  38. HAL_Printf("%s", "\r\n"); \
  39. } while(0)
  40. #define OTA_BUF_LEN (5000)
  41. extern int iotx_get_well_known(iotx_coap_context_t *p_context);
  42. static int fetch_ota(void *h_ota, void *h_coap)
  43. {
  44. int rc = 1;
  45. FILE *fp;
  46. uint32_t last_percent = 0, percent = 0;
  47. char version[128], md5sum[33];
  48. int32_t len, size_downloaded, size_file;
  49. char buf_ota[OTA_BUF_LEN];
  50. int32_t firmware_valid;
  51. if (NULL == (fp = fopen("ota.bin", "wb+"))) {
  52. EXAMPLE_TRACE("open file failed");
  53. return -1;
  54. }
  55. do {
  56. len = IOT_OTA_FetchYield(h_ota, buf_ota, OTA_BUF_LEN, 1);
  57. if (len > 0) {
  58. if (1 != fwrite(buf_ota, len, 1, fp)) {
  59. EXAMPLE_TRACE("write data to file failed");
  60. rc = -1;
  61. break;
  62. }
  63. }else {
  64. IOT_OTA_ReportProgress(h_ota, IOT_OTAP_FETCH_FAILED, NULL);
  65. EXAMPLE_TRACE("ota fetch fail");
  66. }
  67. /* get OTA information */
  68. IOT_OTA_Ioctl(h_ota, IOT_OTAG_FETCHED_SIZE, &size_downloaded, 4);
  69. IOT_OTA_Ioctl(h_ota, IOT_OTAG_FILE_SIZE, &size_file, 4);
  70. IOT_OTA_Ioctl(h_ota, IOT_OTAG_MD5SUM, md5sum, 33);
  71. IOT_OTA_Ioctl(h_ota, IOT_OTAG_VERSION, version, 128);
  72. last_percent = percent;
  73. percent = (size_downloaded * 100) / size_file;
  74. if (percent - last_percent > 0) {
  75. IOT_OTA_ReportProgress(h_ota, percent, NULL);
  76. IOT_OTA_ReportProgress(h_ota, percent, "hello");
  77. }
  78. IOT_CoAP_Yield(h_coap);
  79. } while (!IOT_OTA_IsFetchFinish(h_ota));
  80. while (1 == rc) {
  81. IOT_OTA_Ioctl(h_ota, IOT_OTAG_CHECK_FIRMWARE, &firmware_valid, 4);
  82. if (0 == firmware_valid) {
  83. EXAMPLE_TRACE("The firmware is invalid");
  84. rc = -1;
  85. break;
  86. } else {
  87. EXAMPLE_TRACE("The firmware is valid");
  88. rc = -1;
  89. break;
  90. }
  91. }
  92. if (NULL != fp) {
  93. fclose(fp);
  94. }
  95. return rc;
  96. }
  97. //-1, fetch failed
  98. //0, no any ota firmware
  99. //1, fetch successfully
  100. static int try_fetch_ota(void *h_ota, void *h_coap)
  101. {
  102. if (IOT_OTA_IsFetching(h_ota)) {
  103. return fetch_ota(h_ota, h_coap);
  104. }
  105. return 0;
  106. }
  107. int iotx_set_devinfo(iotx_deviceinfo_t *p_devinfo)
  108. {
  109. if (NULL == p_devinfo) {
  110. return IOTX_ERR_INVALID_PARAM;
  111. }
  112. memset(p_devinfo, 0x00, sizeof(iotx_deviceinfo_t));
  113. /**< get device info*/
  114. HAL_GetProductKey(p_devinfo->product_key);
  115. HAL_GetDeviceSecret(p_devinfo->device_secret);
  116. HAL_GetDeviceName(p_devinfo->device_name);
  117. HAL_GetDeviceID(p_devinfo->device_id);
  118. /**< end*/
  119. return IOTX_SUCCESS;
  120. }
  121. int main(int argc, char **argv)
  122. {
  123. int rc = 0;
  124. void *h_ota = NULL;
  125. iotx_coap_config_t config;
  126. iotx_deviceinfo_t deviceinfo;
  127. iotx_coap_context_t *h_coap = NULL;
  128. IOT_OpenLog("coap-ota");
  129. IOT_SetLogLevel(IOT_LOG_DEBUG);
  130. /**< set device info */
  131. HAL_SetProductKey(IOTX_PRODUCT_KEY);
  132. HAL_SetDeviceName(IOTX_DEVICE_NAME);
  133. HAL_SetDeviceSecret(IOTX_DEVICE_SECRET);
  134. /**< end */
  135. iotx_set_devinfo(&deviceinfo);
  136. memset(&config, 0x00, sizeof(iotx_coap_config_t));
  137. config.p_devinfo = (iotx_deviceinfo_t *)&deviceinfo;
  138. config.p_url = IOTX_PRE_NOSEC_SERVER_URI;
  139. h_coap = IOT_CoAP_Init(&config);
  140. if (NULL == h_coap) {
  141. rc = -1;
  142. EXAMPLE_TRACE("initialize CoAP failed");
  143. return -1;
  144. }
  145. IOT_CoAP_DeviceNameAuth(h_coap);
  146. h_ota = IOT_OTA_Init(deviceinfo.product_key, deviceinfo.device_name, h_coap);
  147. if (NULL == h_ota) {
  148. rc = -1;
  149. EXAMPLE_TRACE("initialize OTA failed");
  150. goto do_exit;
  151. }
  152. int ota_code = 0;
  153. do {
  154. IOT_CoAP_Yield(h_coap);
  155. IOT_OTA_ReportVersion(h_ota, "iotx_ver_1.0.0");
  156. HAL_SleepMs(2000);
  157. ota_code = try_fetch_ota(h_ota, h_coap);
  158. } while (1 != ota_code);
  159. EXAMPLE_TRACE("OTA success");
  160. do_exit:
  161. if (NULL != h_ota) {
  162. IOT_OTA_Deinit(h_ota);
  163. }
  164. if (NULL != h_coap) {
  165. IOT_CoAP_Deinit(&h_coap);
  166. }
  167. return rc;
  168. }