esp_ble_ota.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. TimerHandle_t ota_timer_handle;
  16. bool is_adv = false;
  17. void ota_timer_Callback(TimerHandle_t xTimer);
  18. void esp_ble_ota_delete(void);
  19. uint8_t ota_timeout_data[3] = {0x65,0x72,0x72};//err
  20. #define OTA_RINGBUF_SIZE 8192
  21. #define OTA_TASK_SIZE 8192
  22. static const char *TAG = "ESP_BLE_OTA";
  23. #if CONFIG_EXAMPLE_USE_PRE_ENC_OTA
  24. extern const char rsa_private_pem_start[] asm("_binary_private_pem_start");
  25. extern const char rsa_private_pem_end[] asm("_binary_private_pem_end");
  26. esp_decrypt_handle_t decrypt_handle;
  27. #endif
  28. #ifdef CONFIG_EXAMPLE_USE_PROTOCOMM
  29. #include "manager.h"
  30. #include "scheme_ble.h"
  31. #include "esp_netif.h"
  32. #if CONFIG_EXAMPLE_OTA_SEC2_DEV_MODE
  33. static const char sec2_salt[] = {
  34. 0x03, 0x6e, 0xe0, 0xc7, 0xbc, 0xb9, 0xed, 0xa8, 0x4c, 0x9e, 0xac, 0x97, 0xd9, 0x3d, 0xec, 0xf4
  35. };
  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. };
  62. #endif
  63. static esp_err_t
  64. example_get_sec2_salt(const char **salt, uint16_t *salt_len)
  65. {
  66. #if CONFIG_EXAMPLE_OTA_SEC2_DEV_MODE
  67. ESP_LOGI(TAG, "Development mode: using hard coded salt");
  68. *salt = sec2_salt;
  69. *salt_len = sizeof(sec2_salt);
  70. return ESP_OK;
  71. #elif CONFIG_EXAMPLE_OTA_SEC2_PROD_MODE
  72. ESP_LOGE(TAG, "Not implemented!");
  73. return ESP_FAIL;
  74. #endif
  75. return ESP_FAIL;
  76. }
  77. static esp_err_t
  78. example_get_sec2_verifier(const char **verifier, uint16_t *verifier_len)
  79. {
  80. #if CONFIG_EXAMPLE_OTA_SEC2_DEV_MODE
  81. ESP_LOGI(TAG, "Development mode: using hard coded verifier");
  82. *verifier = sec2_verifier;
  83. *verifier_len = sizeof(sec2_verifier);
  84. return ESP_OK;
  85. #elif CONFIG_EXAMPLE_OTA_SEC2_PROD_MODE
  86. /* This code needs to be updated with appropriate implementation to provide verifier */
  87. ESP_LOGE(TAG, "Not implemented!");
  88. return ESP_FAIL;
  89. #endif
  90. return ESP_FAIL;
  91. }
  92. /* Event handler for catching system events */
  93. static void
  94. event_handler(void *arg, esp_event_base_t event_base,
  95. int32_t event_id, void *event_data)
  96. {
  97. if (event_base == ESP_BLE_OTA_EVENT) {
  98. switch (event_id) {
  99. case OTA_FILE_RCV:
  100. ESP_LOGD(TAG, "File received in appln layer :");
  101. ota_recv_fw_cb(event_data, 4096);
  102. break;
  103. default:
  104. break;
  105. }
  106. } else if (event_base == PROTOCOMM_TRANSPORT_BLE_EVENT) {
  107. switch (event_id) {
  108. case PROTOCOMM_TRANSPORT_BLE_CONNECTED:
  109. ESP_LOGI(TAG, "BLE transport: Connected!");
  110. break;
  111. case PROTOCOMM_TRANSPORT_BLE_DISCONNECTED:
  112. ESP_LOGI(TAG, "BLE transport: Disconnected!");
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. }
  119. static void
  120. get_device_service_name(char *service_name, size_t size)
  121. {
  122. char *svc_name = "OTA_123456";
  123. strlcpy(service_name, svc_name, size);
  124. }
  125. #endif /* CONFIG_EXAMPLE_USE_PROTOCOMM */
  126. static RingbufHandle_t s_ringbuf = NULL;
  127. bool
  128. ble_ota_ringbuf_init(uint32_t ringbuf_size)
  129. {
  130. s_ringbuf = xRingbufferCreate(ringbuf_size, RINGBUF_TYPE_BYTEBUF);
  131. if (s_ringbuf == NULL) {
  132. return false;
  133. }
  134. return true;
  135. }
  136. size_t
  137. write_to_ringbuf(const uint8_t *data, size_t size)
  138. {
  139. BaseType_t done = xRingbufferSend(s_ringbuf, (void *)data, size, (TickType_t)portMAX_DELAY);
  140. if (done) {
  141. return size;
  142. } else {
  143. return 0;
  144. }
  145. }
  146. void
  147. ota_task(void *arg)
  148. {
  149. ota_timer_handle = xTimerCreate(
  150. "ota_timer_handle",
  151. pdMS_TO_TICKS(20*1000),
  152. pdFALSE,
  153. 0,
  154. ota_timer_Callback
  155. );
  156. xTimerStart(ota_timer_handle, 0);
  157. esp_partition_t *partition_ptr = NULL;
  158. esp_partition_t partition;
  159. const esp_partition_t *next_partition = NULL;
  160. esp_ota_handle_t out_handle = 0;
  161. uint32_t recv_len = 0;
  162. uint8_t *data = NULL;
  163. size_t item_size = 0;
  164. ESP_LOGI(TAG, "ota_task start");
  165. // search ota partition
  166. partition_ptr = (esp_partition_t *)esp_ota_get_boot_partition();
  167. if (partition_ptr == NULL) {
  168. ESP_LOGE(TAG, "boot partition NULL!\r\n");
  169. goto OTA_ERROR;
  170. }
  171. if (partition_ptr->type != ESP_PARTITION_TYPE_APP) {
  172. ESP_LOGE(TAG, "esp_current_partition->type != ESP_PARTITION_TYPE_APP\r\n");
  173. goto OTA_ERROR;
  174. }
  175. if (partition_ptr->subtype == ESP_PARTITION_SUBTYPE_APP_FACTORY) {
  176. partition.subtype = ESP_PARTITION_SUBTYPE_APP_OTA_0;
  177. } else {
  178. next_partition = esp_ota_get_next_update_partition(partition_ptr);
  179. if (next_partition) {
  180. partition.subtype = next_partition->subtype;
  181. } else {
  182. partition.subtype = ESP_PARTITION_SUBTYPE_APP_OTA_0;
  183. }
  184. }
  185. partition.type = ESP_PARTITION_TYPE_APP;
  186. partition_ptr = (esp_partition_t *)esp_partition_find_first(partition.type, partition.subtype, NULL);
  187. if (partition_ptr == NULL) {
  188. ESP_LOGE(TAG, "partition NULL!\r\n");
  189. goto OTA_ERROR;
  190. }
  191. memcpy(&partition, partition_ptr, sizeof(esp_partition_t));
  192. if (esp_ota_begin(&partition, OTA_SIZE_UNKNOWN, &out_handle) != ESP_OK) {
  193. ESP_LOGE(TAG, "esp_ota_begin failed!\r\n");
  194. goto OTA_ERROR;
  195. }
  196. ESP_LOGE(TAG, "wait for data from ringbuf! fw_len = %u", esp_ble_ota_get_fw_length());
  197. /*deal with all receive packet*/
  198. for (;;) {
  199. data = (uint8_t *)xRingbufferReceive(s_ringbuf, &item_size, (TickType_t)portMAX_DELAY);
  200. if((item_size == 3) )
  201. {
  202. extern bool is_conn;
  203. ESP_LOGI(TAG,"is_conn = %s",is_conn?"true":"false");
  204. ESP_LOGI(TAG," [%02x] [%02x] --- [%02x] [%02x]",data[0],data[1],ota_timeout_data[0],ota_timeout_data[1]);
  205. if(!is_conn && (data[0] == 0x65)&& (data[1] == 0x72)&&(data[2] == 0x72))//
  206. {
  207. goto OTA_ERROR;
  208. }
  209. else
  210. {
  211. vRingbufferReturnItem(s_ringbuf, (void *)data);//
  212. continue;
  213. }
  214. }
  215. ESP_LOGI(TAG, "recv: %u, recv_total:%"PRIu32"\n", item_size, recv_len + item_size);
  216. if (item_size != 0) {
  217. if (esp_ota_write(out_handle, (const void *)data, item_size) != ESP_OK) {
  218. ESP_LOGE(TAG, "esp_ota_write failed!\r\n");
  219. goto OTA_ERROR;
  220. }
  221. recv_len += item_size;
  222. vRingbufferReturnItem(s_ringbuf, (void *)data);
  223. if (recv_len >= esp_ble_ota_get_fw_length()) {
  224. break;
  225. }
  226. }
  227. }
  228. if (esp_ota_end(out_handle) != ESP_OK) {
  229. ESP_LOGE(TAG, "esp_ota_end failed!\r\n");
  230. goto OTA_ERROR;
  231. }
  232. if (esp_ota_set_boot_partition(&partition) != ESP_OK) {
  233. ESP_LOGE(TAG, "esp_ota_set_boot_partition failed!\r\n");
  234. goto OTA_ERROR;
  235. }
  236. ESP_LOGE(TAG, "存数据 esp_restart");
  237. #include "SPIFFS.h"
  238. extern Machine_info_t Machine_info;
  239. spiffs_write(&Machine_info);
  240. esp_restart();
  241. OTA_ERROR:
  242. ESP_LOGE(TAG, "OTA failed");
  243. esp_ble_ota_delete();
  244. vTaskDelete(NULL);
  245. extern void sleep_timer_start(int ms);
  246. sleep_timer_start(100); //开始进入倒计时休眠
  247. }
  248. void
  249. ota_recv_fw_cb(uint8_t *buf, uint32_t length)
  250. {
  251. write_to_ringbuf(buf, length);
  252. }
  253. static void
  254. ota_task_init(void)
  255. {
  256. xTaskCreate(&ota_task, "ota_task", OTA_TASK_SIZE, NULL, 5, NULL);
  257. return;
  258. }
  259. void
  260. esp_ble_ota(void)
  261. {
  262. if(is_adv)
  263. {
  264. ESP_LOGI(TAG, " is adv ,return");
  265. return;
  266. }
  267. esp_err_t ret;
  268. esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  269. // Initialize NVS
  270. ret = nvs_flash_init();
  271. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  272. ESP_ERROR_CHECK(nvs_flash_erase());
  273. ret = nvs_flash_init();
  274. }
  275. ESP_ERROR_CHECK(ret);
  276. if (!ble_ota_ringbuf_init(OTA_RINGBUF_SIZE)) {
  277. ESP_LOGE(TAG, "%s init ringbuf fail", __func__);
  278. return;
  279. }
  280. #if CONFIG_EXAMPLE_USE_PROTOCOMM
  281. /* Initialize TCP/IP */
  282. ESP_ERROR_CHECK(esp_netif_init());
  283. /* Initialize the event loop */
  284. ESP_ERROR_CHECK(esp_event_loop_create_default());
  285. /* Register our event handler for Wi-Fi, IP and Provisioning related events */
  286. ESP_ERROR_CHECK(esp_event_handler_register(ESP_BLE_OTA_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
  287. ESP_ERROR_CHECK(esp_event_handler_register(PROTOCOMM_TRANSPORT_BLE_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
  288. /* Configuration for the ota manager */
  289. esp_ble_ota_config_t config = {
  290. .scheme = esp_ble_ota_scheme_ble,
  291. .scheme_event_handler = ESP_BLE_OTA_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM
  292. };
  293. /* Initialize ota manager with the
  294. * configuration parameters set above */
  295. ESP_ERROR_CHECK(esp_ble_ota_init(config));
  296. char service_name[12];
  297. get_device_service_name(service_name, sizeof(service_name));
  298. esp_ble_ota_security_t security = ESP_BLE_OTA_SECURITY_2;
  299. #ifdef CONFIG_EXAMPLE_OTA_SECURITY_VERSION_1
  300. security = ESP_BLE_OTA_SECURITY_1;
  301. /* Do we want a proof-of-possession (ignored if Security 0 is selected):
  302. * - this should be a string with length > 0
  303. * - NULL if not used
  304. */
  305. const char *pop = "abcd1234";
  306. /* This is the structure for passing security parameters
  307. * for the protocomm security 1.
  308. */
  309. esp_ble_ota_security1_params_t *sec_params = pop;
  310. #elif CONFIG_EXAMPLE_OTA_SECURITY_VERSION_2
  311. /* The username must be the same one, which has been used in the generation of salt and verifier */
  312. esp_ble_ota_security2_params_t sec2_params = {};
  313. ESP_ERROR_CHECK(example_get_sec2_salt(&sec2_params.salt, &sec2_params.salt_len));
  314. ESP_ERROR_CHECK(example_get_sec2_verifier(&sec2_params.verifier, &sec2_params.verifier_len));
  315. esp_ble_ota_security2_params_t *sec_params = &sec2_params;
  316. #endif
  317. const char *service_key = NULL;
  318. ESP_ERROR_CHECK(esp_ble_ota_start(security, (const void *) sec_params, service_name, service_key));
  319. #else
  320. ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
  321. ret = esp_bt_controller_init(&bt_cfg);
  322. if (ret) {
  323. ESP_LOGE(TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
  324. return;
  325. }
  326. ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
  327. if (ret) {
  328. ESP_LOGE(TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
  329. return;
  330. }
  331. if (esp_ble_ota_host_init() != ESP_OK) {
  332. ESP_LOGE(TAG, "%s initialoze ble host fail: %s\n", __func__, esp_err_to_name(ret));
  333. return;
  334. }
  335. #if CONFIG_EXAMPLE_USE_PRE_ENC_OTA
  336. esp_decrypt_cfg_t cfg = {};
  337. cfg.rsa_pub_key = rsa_private_pem_start;
  338. cfg.rsa_pub_key_len = rsa_private_pem_end - rsa_private_pem_start;
  339. decrypt_handle = esp_encrypted_img_decrypt_start(&cfg);
  340. if (!decrypt_handle) {
  341. ESP_LOGE(TAG, "OTA upgrade failed");
  342. vTaskDelete(NULL);
  343. }
  344. esp_ble_ota_recv_fw_data_callback(ota_recv_fw_cb, decrypt_handle);
  345. #else
  346. esp_ble_ota_recv_fw_data_callback(ota_recv_fw_cb);
  347. #endif /* CONFIG_EXAMPLE_USE_PRE_ENC_OTA */
  348. #endif /* CONFIG_EXAMPLE_USE_PROTOCOMM */
  349. is_adv = true;
  350. ota_task_init();
  351. }
  352. void ota_timer_Callback(TimerHandle_t xTimer)
  353. {
  354. extern bool is_conn;
  355. if(!is_conn)
  356. {
  357. ESP_LOGI(TAG, "ota_timer_Callback");
  358. xRingbufferSend(s_ringbuf, (void *)ota_timeout_data, 3, (TickType_t)portMAX_DELAY);
  359. }
  360. xTimerDelete(ota_timer_handle,0);
  361. }
  362. #include "nimble/nimble_port.h"
  363. void esp_ble_ota_delete(void)
  364. {
  365. ESP_LOGI(TAG, "nimble deinit");
  366. nimble_port_deinit();
  367. esp_bt_controller_disable();
  368. esp_bt_controller_deinit();
  369. vRingbufferDelete(s_ringbuf);
  370. is_adv = false;
  371. }