esp_ble_ota.c 15 KB

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