esp_ble_ota.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/ringbuf.h"
  9. #include "freertos/timers.h"
  10. #include "esp_log.h"
  11. #include "esp_ota_ops.h"
  12. #include "nvs_flash.h"
  13. #include "esp_bt.h"
  14. #include "ble_ota.h"
  15. #include "SPIFFS.h"
  16. TimerHandle_t ota_timer_handle;
  17. bool is_adv = false;
  18. void ota_timer_Callback(TimerHandle_t xTimer);
  19. void esp_ble_ota_delete(void);
  20. uint8_t ota_timeout_data[3] = {0x65, 0x72, 0x72}; // err
  21. #define OTA_RINGBUF_SIZE 8192
  22. #define OTA_TASK_SIZE 8192
  23. static const char *TAG = "ESP_BLE_OTA";
  24. #if CONFIG_EXAMPLE_USE_PRE_ENC_OTA
  25. extern const char rsa_private_pem_start[] asm("_binary_private_pem_start");
  26. extern const char rsa_private_pem_end[] asm("_binary_private_pem_end");
  27. esp_decrypt_handle_t decrypt_handle;
  28. #endif
  29. #ifdef CONFIG_EXAMPLE_USE_PROTOCOMM
  30. #include "manager.h"
  31. #include "scheme_ble.h"
  32. #include "esp_netif.h"
  33. #if CONFIG_EXAMPLE_OTA_SEC2_DEV_MODE
  34. static const char sec2_salt[] = {
  35. 0x03, 0x6e, 0xe0, 0xc7, 0xbc, 0xb9, 0xed, 0xa8, 0x4c, 0x9e, 0xac, 0x97, 0xd9, 0x3d, 0xec, 0xf4};
  36. static const char sec2_verifier[] = {
  37. 0x7c, 0x7c, 0x85, 0x47, 0x65, 0x08, 0x94, 0x6d, 0xd6, 0x36, 0xaf, 0x37, 0xd7, 0xe8, 0x91, 0x43,
  38. 0x78, 0xcf, 0xfd, 0x61, 0x6c, 0x59, 0xd2, 0xf8, 0x39, 0x08, 0x12, 0x72, 0x38, 0xde, 0x9e, 0x24,
  39. 0xa4, 0x70, 0x26, 0x1c, 0xdf, 0xa9, 0x03, 0xc2, 0xb2, 0x70, 0xe7, 0xb1, 0x32, 0x24, 0xda, 0x11,
  40. 0x1d, 0x97, 0x18, 0xdc, 0x60, 0x72, 0x08, 0xcc, 0x9a, 0xc9, 0x0c, 0x48, 0x27, 0xe2, 0xae, 0x89,
  41. 0xaa, 0x16, 0x25, 0xb8, 0x04, 0xd2, 0x1a, 0x9b, 0x3a, 0x8f, 0x37, 0xf6, 0xe4, 0x3a, 0x71, 0x2e,
  42. 0xe1, 0x27, 0x86, 0x6e, 0xad, 0xce, 0x28, 0xff, 0x54, 0x46, 0x60, 0x1f, 0xb9, 0x96, 0x87, 0xdc,
  43. 0x57, 0x40, 0xa7, 0xd4, 0x6c, 0xc9, 0x77, 0x54, 0xdc, 0x16, 0x82, 0xf0, 0xed, 0x35, 0x6a, 0xc4,
  44. 0x70, 0xad, 0x3d, 0x90, 0xb5, 0x81, 0x94, 0x70, 0xd7, 0xbc, 0x65, 0xb2, 0xd5, 0x18, 0xe0, 0x2e,
  45. 0xc3, 0xa5, 0xf9, 0x68, 0xdd, 0x64, 0x7b, 0xb8, 0xb7, 0x3c, 0x9c, 0xfc, 0x00, 0xd8, 0x71, 0x7e,
  46. 0xb7, 0x9a, 0x7c, 0xb1, 0xb7, 0xc2, 0xc3, 0x18, 0x34, 0x29, 0x32, 0x43, 0x3e, 0x00, 0x99, 0xe9,
  47. 0x82, 0x94, 0xe3, 0xd8, 0x2a, 0xb0, 0x96, 0x29, 0xb7, 0xdf, 0x0e, 0x5f, 0x08, 0x33, 0x40, 0x76,
  48. 0x52, 0x91, 0x32, 0x00, 0x9f, 0x97, 0x2c, 0x89, 0x6c, 0x39, 0x1e, 0xc8, 0x28, 0x05, 0x44, 0x17,
  49. 0x3f, 0x68, 0x02, 0x8a, 0x9f, 0x44, 0x61, 0xd1, 0xf5, 0xa1, 0x7e, 0x5a, 0x70, 0xd2, 0xc7, 0x23,
  50. 0x81, 0xcb, 0x38, 0x68, 0xe4, 0x2c, 0x20, 0xbc, 0x40, 0x57, 0x76, 0x17, 0xbd, 0x08, 0xb8, 0x96,
  51. 0xbc, 0x26, 0xeb, 0x32, 0x46, 0x69, 0x35, 0x05, 0x8c, 0x15, 0x70, 0xd9, 0x1b, 0xe9, 0xbe, 0xcc,
  52. 0xa9, 0x38, 0xa6, 0x67, 0xf0, 0xad, 0x50, 0x13, 0x19, 0x72, 0x64, 0xbf, 0x52, 0xc2, 0x34, 0xe2,
  53. 0x1b, 0x11, 0x79, 0x74, 0x72, 0xbd, 0x34, 0x5b, 0xb1, 0xe2, 0xfd, 0x66, 0x73, 0xfe, 0x71, 0x64,
  54. 0x74, 0xd0, 0x4e, 0xbc, 0x51, 0x24, 0x19, 0x40, 0x87, 0x0e, 0x92, 0x40, 0xe6, 0x21, 0xe7, 0x2d,
  55. 0x4e, 0x37, 0x76, 0x2f, 0x2e, 0xe2, 0x68, 0xc7, 0x89, 0xe8, 0x32, 0x13, 0x42, 0x06, 0x84, 0x84,
  56. 0x53, 0x4a, 0xb3, 0x0c, 0x1b, 0x4c, 0x8d, 0x1c, 0x51, 0x97, 0x19, 0xab, 0xae, 0x77, 0xff, 0xdb,
  57. 0xec, 0xf0, 0x10, 0x95, 0x34, 0x33, 0x6b, 0xcb, 0x3e, 0x84, 0x0f, 0xb9, 0xd8, 0x5f, 0xb8, 0xa0,
  58. 0xb8, 0x55, 0x53, 0x3e, 0x70, 0xf7, 0x18, 0xf5, 0xce, 0x7b, 0x4e, 0xbf, 0x27, 0xce, 0xce, 0xa8,
  59. 0xb3, 0xbe, 0x40, 0xc5, 0xc5, 0x32, 0x29, 0x3e, 0x71, 0x64, 0x9e, 0xde, 0x8c, 0xf6, 0x75, 0xa1,
  60. 0xe6, 0xf6, 0x53, 0xc8, 0x31, 0xa8, 0x78, 0xde, 0x50, 0x40, 0xf7, 0x62, 0xde, 0x36, 0xb2, 0xba};
  61. #endif
  62. static esp_err_t
  63. example_get_sec2_salt(const char **salt, uint16_t *salt_len)
  64. {
  65. #if CONFIG_EXAMPLE_OTA_SEC2_DEV_MODE
  66. ESP_LOGI(TAG, "Development mode: using hard coded salt");
  67. *salt = sec2_salt;
  68. *salt_len = sizeof(sec2_salt);
  69. return ESP_OK;
  70. #elif CONFIG_EXAMPLE_OTA_SEC2_PROD_MODE
  71. ESP_LOGE(TAG, "Not implemented!");
  72. return ESP_FAIL;
  73. #endif
  74. return ESP_FAIL;
  75. }
  76. static esp_err_t
  77. example_get_sec2_verifier(const char **verifier, uint16_t *verifier_len)
  78. {
  79. #if CONFIG_EXAMPLE_OTA_SEC2_DEV_MODE
  80. ESP_LOGI(TAG, "Development mode: using hard coded verifier");
  81. *verifier = sec2_verifier;
  82. *verifier_len = sizeof(sec2_verifier);
  83. return ESP_OK;
  84. #elif CONFIG_EXAMPLE_OTA_SEC2_PROD_MODE
  85. /* This code needs to be updated with appropriate implementation to provide verifier */
  86. ESP_LOGE(TAG, "Not implemented!");
  87. return ESP_FAIL;
  88. #endif
  89. return ESP_FAIL;
  90. }
  91. /* Event handler for catching system events */
  92. static void
  93. event_handler(void *arg, esp_event_base_t event_base,
  94. int32_t event_id, void *event_data)
  95. {
  96. if (event_base == ESP_BLE_OTA_EVENT)
  97. {
  98. switch (event_id)
  99. {
  100. case OTA_FILE_RCV:
  101. ESP_LOGD(TAG, "File received in appln layer :");
  102. ota_recv_fw_cb(event_data, 4096);
  103. break;
  104. default:
  105. break;
  106. }
  107. }
  108. else if (event_base == PROTOCOMM_TRANSPORT_BLE_EVENT)
  109. {
  110. switch (event_id)
  111. {
  112. case PROTOCOMM_TRANSPORT_BLE_CONNECTED:
  113. ESP_LOGI(TAG, "BLE transport: Connected!");
  114. break;
  115. case PROTOCOMM_TRANSPORT_BLE_DISCONNECTED:
  116. ESP_LOGI(TAG, "BLE transport: Disconnected!");
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. }
  123. static void
  124. get_device_service_name(char *service_name, size_t size)
  125. {
  126. char *svc_name = "OTA_123456";
  127. strlcpy(service_name, svc_name, size);
  128. }
  129. #endif /* CONFIG_EXAMPLE_USE_PROTOCOMM */
  130. static RingbufHandle_t s_ringbuf = NULL;
  131. bool ble_ota_ringbuf_init(uint32_t ringbuf_size)
  132. {
  133. s_ringbuf = xRingbufferCreate(ringbuf_size, RINGBUF_TYPE_BYTEBUF);
  134. if (s_ringbuf == NULL)
  135. {
  136. return false;
  137. }
  138. return true;
  139. }
  140. size_t
  141. write_to_ringbuf(const uint8_t *data, size_t size)
  142. {
  143. BaseType_t done = xRingbufferSend(s_ringbuf, (void *)data, size, (TickType_t)portMAX_DELAY);
  144. if (done)
  145. {
  146. return size;
  147. }
  148. else
  149. {
  150. return 0;
  151. }
  152. }
  153. void ota_task(void *arg)
  154. {
  155. ota_timer_handle = xTimerCreate(
  156. "ota_timer_handle",
  157. pdMS_TO_TICKS(20 * 1000),
  158. pdFALSE,
  159. 0,
  160. ota_timer_Callback);
  161. xTimerStart(ota_timer_handle, 0);
  162. esp_partition_t *partition_ptr = NULL;
  163. esp_partition_t partition;
  164. const esp_partition_t *next_partition = NULL;
  165. esp_ota_handle_t out_handle = 0;
  166. uint32_t recv_len = 0;
  167. uint8_t *data = NULL;
  168. size_t item_size = 0;
  169. ESP_LOGI(TAG, "ota_task start");
  170. // search ota partition
  171. partition_ptr = (esp_partition_t *)esp_ota_get_boot_partition();
  172. if (partition_ptr == NULL)
  173. {
  174. ESP_LOGE(TAG, "boot partition NULL!\r\n");
  175. goto OTA_ERROR;
  176. }
  177. if (partition_ptr->type != ESP_PARTITION_TYPE_APP)
  178. {
  179. ESP_LOGE(TAG, "esp_current_partition->type != ESP_PARTITION_TYPE_APP\r\n");
  180. goto OTA_ERROR;
  181. }
  182. if (partition_ptr->subtype == ESP_PARTITION_SUBTYPE_APP_FACTORY)
  183. {
  184. partition.subtype = ESP_PARTITION_SUBTYPE_APP_OTA_0;
  185. }
  186. else
  187. {
  188. next_partition = esp_ota_get_next_update_partition(partition_ptr);
  189. if (next_partition)
  190. {
  191. partition.subtype = next_partition->subtype;
  192. }
  193. else
  194. {
  195. partition.subtype = ESP_PARTITION_SUBTYPE_APP_OTA_0;
  196. }
  197. }
  198. partition.type = ESP_PARTITION_TYPE_APP;
  199. partition_ptr = (esp_partition_t *)esp_partition_find_first(partition.type, partition.subtype, NULL);
  200. if (partition_ptr == NULL)
  201. {
  202. ESP_LOGE(TAG, "partition NULL!\r\n");
  203. goto OTA_ERROR;
  204. }
  205. memcpy(&partition, partition_ptr, sizeof(esp_partition_t));
  206. if (esp_ota_begin(&partition, OTA_SIZE_UNKNOWN, &out_handle) != ESP_OK)
  207. {
  208. ESP_LOGE(TAG, "esp_ota_begin failed!\r\n");
  209. goto OTA_ERROR;
  210. }
  211. ESP_LOGE(TAG, "wait for data from ringbuf! fw_len = %u", esp_ble_ota_get_fw_length());
  212. /*deal with all receive packet*/
  213. for (;;)
  214. {
  215. data = (uint8_t *)xRingbufferReceive(s_ringbuf, &item_size, (TickType_t)portMAX_DELAY);
  216. if ((item_size == 3))
  217. {
  218. extern bool is_conn;
  219. ESP_LOGI(TAG, "is_conn = %s", is_conn ? "true" : "false");
  220. // ESP_LOGI(TAG," [%02x] [%02x] --- [%02x] [%02x]",data[0],data[1],ota_timeout_data[0],ota_timeout_data[1]);
  221. if (!is_conn && (data[0] == 0x65) && (data[1] == 0x72) && (data[2] == 0x72)) //
  222. {
  223. goto OTA_ERROR;
  224. }
  225. else
  226. {
  227. vRingbufferReturnItem(s_ringbuf, (void *)data); //
  228. continue;
  229. }
  230. }
  231. ESP_LOGI(TAG, "recv: %u, recv_total:%" PRIu32 "\n", item_size, recv_len + item_size);
  232. if (item_size != 0)
  233. {
  234. if (esp_ota_write(out_handle, (const void *)data, item_size) != ESP_OK)
  235. {
  236. ESP_LOGE(TAG, "esp_ota_write failed!\r\n");
  237. goto OTA_ERROR;
  238. }
  239. recv_len += item_size;
  240. vRingbufferReturnItem(s_ringbuf, (void *)data);
  241. if (recv_len >= esp_ble_ota_get_fw_length())
  242. {
  243. break;
  244. }
  245. }
  246. }
  247. if (esp_ota_end(out_handle) != ESP_OK)
  248. {
  249. ESP_LOGE(TAG, "esp_ota_end failed!\r\n");
  250. goto OTA_ERROR;
  251. }
  252. if (esp_ota_set_boot_partition(&partition) != ESP_OK)
  253. {
  254. ESP_LOGE(TAG, "esp_ota_set_boot_partition failed!\r\n");
  255. goto OTA_ERROR;
  256. }
  257. // extern Machine_info_t Machine_info;
  258. // spiffs_write(&Machine_info);
  259. extern void reset_default(bool is_dis, uint8_t power_status);
  260. reset_default(false, 0);
  261. ESP_LOGE(TAG, "存数据 esp_restart");
  262. vTaskDelay(500 / portTICK_PERIOD_MS);
  263. esp_restart();
  264. OTA_ERROR:
  265. ESP_LOGE(TAG, "OTA failed");
  266. extern void change_is_down(void);
  267. change_is_down();
  268. esp_ble_ota_delete();
  269. vTaskDelete(NULL);
  270. #include "iot_button.h"
  271. iot_button_resume();
  272. extern void sleep_timer_start(int ms);
  273. sleep_timer_start(100); // 开始进入倒计时休眠
  274. }
  275. void ota_recv_fw_cb(uint8_t *buf, uint32_t length)
  276. {
  277. write_to_ringbuf(buf, length);
  278. }
  279. static void
  280. ota_task_init(void)
  281. {
  282. xTaskCreate(&ota_task, "ota_task", OTA_TASK_SIZE, NULL, 5, NULL);
  283. return;
  284. }
  285. void esp_ble_ota(void)
  286. {
  287. if (is_adv)
  288. {
  289. ESP_LOGI(TAG, " is adv ,return");
  290. return;
  291. }
  292. esp_err_t ret;
  293. esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  294. // Initialize NVS
  295. ret = nvs_flash_init();
  296. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
  297. {
  298. ESP_ERROR_CHECK(nvs_flash_erase());
  299. ret = nvs_flash_init();
  300. }
  301. ESP_ERROR_CHECK(ret);
  302. if (!ble_ota_ringbuf_init(OTA_RINGBUF_SIZE))
  303. {
  304. ESP_LOGE(TAG, "%s init ringbuf fail", __func__);
  305. return;
  306. }
  307. #if CONFIG_EXAMPLE_USE_PROTOCOMM
  308. /* Initialize TCP/IP */
  309. ESP_ERROR_CHECK(esp_netif_init());
  310. /* Initialize the event loop */
  311. ESP_ERROR_CHECK(esp_event_loop_create_default());
  312. /* Register our event handler for Wi-Fi, IP and Provisioning related events */
  313. ESP_ERROR_CHECK(esp_event_handler_register(ESP_BLE_OTA_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
  314. ESP_ERROR_CHECK(esp_event_handler_register(PROTOCOMM_TRANSPORT_BLE_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
  315. /* Configuration for the ota manager */
  316. esp_ble_ota_config_t config = {
  317. .scheme = esp_ble_ota_scheme_ble,
  318. .scheme_event_handler = ESP_BLE_OTA_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM};
  319. /* Initialize ota manager with the
  320. * configuration parameters set above */
  321. ESP_ERROR_CHECK(esp_ble_ota_init(config));
  322. char service_name[12];
  323. get_device_service_name(service_name, sizeof(service_name));
  324. esp_ble_ota_security_t security = ESP_BLE_OTA_SECURITY_2;
  325. #ifdef CONFIG_EXAMPLE_OTA_SECURITY_VERSION_1
  326. security = ESP_BLE_OTA_SECURITY_1;
  327. /* Do we want a proof-of-possession (ignored if Security 0 is selected):
  328. * - this should be a string with length > 0
  329. * - NULL if not used
  330. */
  331. const char *pop = "abcd1234";
  332. /* This is the structure for passing security parameters
  333. * for the protocomm security 1.
  334. */
  335. esp_ble_ota_security1_params_t *sec_params = pop;
  336. #elif CONFIG_EXAMPLE_OTA_SECURITY_VERSION_2
  337. /* The username must be the same one, which has been used in the generation of salt and verifier */
  338. esp_ble_ota_security2_params_t sec2_params = {};
  339. ESP_ERROR_CHECK(example_get_sec2_salt(&sec2_params.salt, &sec2_params.salt_len));
  340. ESP_ERROR_CHECK(example_get_sec2_verifier(&sec2_params.verifier, &sec2_params.verifier_len));
  341. esp_ble_ota_security2_params_t *sec_params = &sec2_params;
  342. #endif
  343. const char *service_key = NULL;
  344. ESP_ERROR_CHECK(esp_ble_ota_start(security, (const void *)sec_params, service_name, service_key));
  345. #else
  346. ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
  347. ret = esp_bt_controller_init(&bt_cfg);
  348. if (ret)
  349. {
  350. ESP_LOGE(TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
  351. return;
  352. }
  353. ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
  354. if (ret)
  355. {
  356. ESP_LOGE(TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
  357. return;
  358. }
  359. if (esp_ble_ota_host_init() != ESP_OK)
  360. {
  361. ESP_LOGE(TAG, "%s initialoze ble host fail: %s\n", __func__, esp_err_to_name(ret));
  362. return;
  363. }
  364. #if CONFIG_EXAMPLE_USE_PRE_ENC_OTA
  365. esp_decrypt_cfg_t cfg = {};
  366. cfg.rsa_pub_key = rsa_private_pem_start;
  367. cfg.rsa_pub_key_len = rsa_private_pem_end - rsa_private_pem_start;
  368. decrypt_handle = esp_encrypted_img_decrypt_start(&cfg);
  369. if (!decrypt_handle)
  370. {
  371. ESP_LOGE(TAG, "OTA upgrade failed");
  372. vTaskDelete(NULL);
  373. }
  374. esp_ble_ota_recv_fw_data_callback(ota_recv_fw_cb, decrypt_handle);
  375. #else
  376. esp_ble_ota_recv_fw_data_callback(ota_recv_fw_cb);
  377. #endif /* CONFIG_EXAMPLE_USE_PRE_ENC_OTA */
  378. #endif /* CONFIG_EXAMPLE_USE_PROTOCOMM */
  379. is_adv = true;
  380. ota_task_init();
  381. }
  382. void ota_timer_Callback(TimerHandle_t xTimer)
  383. {
  384. extern bool is_conn;
  385. if (!is_conn)
  386. {
  387. ESP_LOGI(TAG, "ota_timer_Callback");
  388. xRingbufferSend(s_ringbuf, (void *)ota_timeout_data, 3, (TickType_t)portMAX_DELAY);
  389. }
  390. xTimerDelete(ota_timer_handle, 0);
  391. }
  392. #include "nimble/nimble_port.h"
  393. void esp_ble_ota_delete(void)
  394. {
  395. ESP_LOGI(TAG, "nimble deinit");
  396. nimble_port_deinit();
  397. esp_bt_controller_disable();
  398. esp_bt_controller_deinit();
  399. vRingbufferDelete(s_ringbuf);
  400. is_adv = false;
  401. }