|
@@ -2,7 +2,7 @@
|
|
|
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
|
|
|
*
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
-*/
|
|
|
+ */
|
|
|
|
|
|
#include "esp_log.h"
|
|
|
#include "nvs_flash.h"
|
|
@@ -20,34 +20,56 @@
|
|
|
|
|
|
bool is_conn = false;
|
|
|
|
|
|
-
|
|
|
#ifdef CONFIG_PRE_ENC_OTA
|
|
|
-#define SECTOR_END_ID 2
|
|
|
-#define ENC_HEADER 512
|
|
|
+#define SECTOR_END_ID 2
|
|
|
+#define ENC_HEADER 512
|
|
|
esp_decrypt_handle_t decrypt_handle_cmp;
|
|
|
#endif
|
|
|
|
|
|
-#define BUF_LENGTH 4096
|
|
|
-#define OTA_IDX_NB 4
|
|
|
-#define CMD_ACK_LENGTH 20
|
|
|
+#define BUF_LENGTH 4096
|
|
|
+#define OTA_IDX_NB 4
|
|
|
+#define CMD_ACK_LENGTH 20
|
|
|
|
|
|
-#define character_declaration_uuid BLE_ATT_UUID_CHARACTERISTIC
|
|
|
-#define character_client_config_uuid BLE_ATT_UUID_CHARACTERISTIC
|
|
|
+#define character_declaration_uuid BLE_ATT_UUID_CHARACTERISTIC
|
|
|
+#define character_client_config_uuid BLE_ATT_UUID_CHARACTERISTIC
|
|
|
|
|
|
-#define GATT_SVR_SVC_ALERT_UUID 0x1811
|
|
|
-#define BLE_OTA_SERVICE_UUID 0x8018
|
|
|
+#define GATT_SVR_SVC_ALERT_UUID 0x1811
|
|
|
+#define BLE_OTA_SERVICE_UUID 0x8018
|
|
|
|
|
|
-#define RECV_FW_UUID 0x8020
|
|
|
-#define OTA_BAR_UUID 0x8021
|
|
|
-#define COMMAND_UUID 0x8022
|
|
|
-#define CUSTOMER_UUID 0x8023
|
|
|
+#define RECV_FW_UUID 0x8020
|
|
|
+#define OTA_BAR_UUID 0x8021
|
|
|
+#define COMMAND_UUID 0x8022
|
|
|
+#define CUSTOMER_UUID 0x8023
|
|
|
|
|
|
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
|
|
static uint8_t ext_adv_pattern[] = {
|
|
|
- 0x02, 0x01, 0x06,
|
|
|
- 0x03, 0x03, 0xab, 0xcd,
|
|
|
- 0x03, 0x03, 0x18, 0x11,
|
|
|
- 0x0f, 0X09, 'n', 'i', 'm', 'b', 'l', 'e', '-', 'o', 't', 'a', '-', 'e', 'x', 't',
|
|
|
+ 0x02,
|
|
|
+ 0x01,
|
|
|
+ 0x06,
|
|
|
+ 0x03,
|
|
|
+ 0x03,
|
|
|
+ 0xab,
|
|
|
+ 0xcd,
|
|
|
+ 0x03,
|
|
|
+ 0x03,
|
|
|
+ 0x18,
|
|
|
+ 0x11,
|
|
|
+ 0x0f,
|
|
|
+ 0X09,
|
|
|
+ 'n',
|
|
|
+ 'i',
|
|
|
+ 'm',
|
|
|
+ 'b',
|
|
|
+ 'l',
|
|
|
+ 'e',
|
|
|
+ '-',
|
|
|
+ 'o',
|
|
|
+ 't',
|
|
|
+ 'a',
|
|
|
+ '-',
|
|
|
+ 'e',
|
|
|
+ 'x',
|
|
|
+ 't',
|
|
|
};
|
|
|
#endif
|
|
|
|
|
@@ -64,8 +86,7 @@ static uint32_t ota_total_len = 0;
|
|
|
static uint32_t ota_block_size = BUF_LENGTH;
|
|
|
|
|
|
esp_ble_ota_callback_funs_t ota_cb_fun_t = {
|
|
|
- .recv_fw_cb = NULL
|
|
|
-};
|
|
|
+ .recv_fw_cb = NULL};
|
|
|
|
|
|
#ifndef CONFIG_OTA_WITH_PROTOCOMM
|
|
|
esp_ble_ota_notification_check_t ota_notification = {
|
|
@@ -97,7 +118,7 @@ esp_ble_ota_notification_data(uint16_t conn_handle, uint16_t attr_handle, uint8_
|
|
|
#endif
|
|
|
|
|
|
/*----------------------------------------------------
|
|
|
- * Common api's
|
|
|
+ * Common api's
|
|
|
*----------------------------------------------------*/
|
|
|
|
|
|
void ble_store_config_init(void);
|
|
@@ -119,7 +140,8 @@ esp_ble_ota_write_chr(struct os_mbuf *om)
|
|
|
|
|
|
pargs.data_in_len = om->om_len - 3;
|
|
|
|
|
|
- if (SLIST_NEXT(om, om_next) != NULL) {
|
|
|
+ if (SLIST_NEXT(om, om_next) != NULL)
|
|
|
+ {
|
|
|
struct os_mbuf *temp2 = SLIST_NEXT(om, om_next);
|
|
|
pargs.data_in_len += temp2->om_len;
|
|
|
}
|
|
@@ -127,12 +149,14 @@ esp_ble_ota_write_chr(struct os_mbuf *om)
|
|
|
pargs.data_in = (const char *)malloc(pargs.data_in_len * sizeof(char *));
|
|
|
err = os_mbuf_copydata(om, 3, pargs.data_in_len, pargs.data_in);
|
|
|
|
|
|
- if (om->om_data[2] == 0xff) {
|
|
|
+ if (om->om_data[2] == 0xff)
|
|
|
+ {
|
|
|
pargs.data_in_len -= SECTOR_END_ID;
|
|
|
}
|
|
|
|
|
|
err = esp_encrypted_img_decrypt_data(decrypt_handle_cmp, &pargs);
|
|
|
- if (err != ESP_OK && err != ESP_ERR_NOT_FINISHED) {
|
|
|
+ if (err != ESP_OK && err != ESP_ERR_NOT_FINISHED)
|
|
|
+ {
|
|
|
return;
|
|
|
}
|
|
|
#endif
|
|
@@ -140,22 +164,25 @@ esp_ble_ota_write_chr(struct os_mbuf *om)
|
|
|
uint8_t cmd_ack[CMD_ACK_LENGTH] = {0x03, 0x00, 0x00, 0x00, 0x00,
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
- 0x00, 0x00, 0x00, 0x00, 0x00
|
|
|
- };
|
|
|
+ 0x00, 0x00, 0x00, 0x00, 0x00};
|
|
|
uint16_t crc16;
|
|
|
|
|
|
- if ((om->om_data[0] + (om->om_data[1] * 256)) != cur_sector) {
|
|
|
+ if ((om->om_data[0] + (om->om_data[1] * 256)) != cur_sector)
|
|
|
+ {
|
|
|
// sector error
|
|
|
- if ((om->om_data[0] == 0xff) && (om->om_data[1] == 0xff)) {
|
|
|
+ if ((om->om_data[0] == 0xff) && (om->om_data[1] == 0xff))
|
|
|
+ {
|
|
|
// last sector
|
|
|
ESP_LOGD(TAG, "Last sector");
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
// sector error
|
|
|
ESP_LOGE(TAG, "%s - sector index error, cur: %" PRIu32 ", recv: %d", __func__,
|
|
|
cur_sector, (om->om_data[0] + (om->om_data[1] * 256)));
|
|
|
cmd_ack[0] = om->om_data[0];
|
|
|
cmd_ack[1] = om->om_data[1];
|
|
|
- cmd_ack[2] = 0x02; //sector index error
|
|
|
+ cmd_ack[2] = 0x02; // sector index error
|
|
|
cmd_ack[3] = 0x00;
|
|
|
cmd_ack[4] = cur_sector & 0xff;
|
|
|
cmd_ack[5] = (cur_sector & 0xff00) >> 8;
|
|
@@ -168,11 +195,15 @@ esp_ble_ota_write_chr(struct os_mbuf *om)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (om->om_data[2] != cur_packet) { // packet seq error
|
|
|
- if (om->om_data[2] == 0xff) { // last packet
|
|
|
+ if (om->om_data[2] != cur_packet)
|
|
|
+ { // packet seq error
|
|
|
+ if (om->om_data[2] == 0xff)
|
|
|
+ { // last packet
|
|
|
ESP_LOGD(TAG, "last packet");
|
|
|
goto write_ota_data;
|
|
|
- } else { // packet seq error
|
|
|
+ }
|
|
|
+ else
|
|
|
+ { // packet seq error
|
|
|
ESP_LOGE(TAG, "%s - packet index error, cur: %" PRIu32 ", recv: %d", __func__,
|
|
|
cur_packet, om->om_data[2]);
|
|
|
}
|
|
@@ -180,7 +211,8 @@ esp_ble_ota_write_chr(struct os_mbuf *om)
|
|
|
|
|
|
write_ota_data:
|
|
|
#ifdef CONFIG_PRE_ENC_OTA
|
|
|
- if(pargs.data_out_len > 0) {
|
|
|
+ if (pargs.data_out_len > 0)
|
|
|
+ {
|
|
|
memcpy(fw_buf + fw_buf_offset, pargs.data_out, pargs.data_out_len);
|
|
|
|
|
|
free(pargs.data_out);
|
|
@@ -194,7 +226,8 @@ write_ota_data:
|
|
|
memcpy(fw_buf + fw_buf_offset, om->om_data + 3, om->om_len - 3);
|
|
|
fw_buf_offset += om->om_len - 3;
|
|
|
|
|
|
- if (SLIST_NEXT(om, om_next) != NULL) {
|
|
|
+ if (SLIST_NEXT(om, om_next) != NULL)
|
|
|
+ {
|
|
|
struct os_mbuf *temp = SLIST_NEXT(om, om_next);
|
|
|
|
|
|
memcpy(fw_buf + fw_buf_offset, temp->om_data, temp->om_len);
|
|
@@ -206,21 +239,27 @@ write_ota_data:
|
|
|
ESP_LOGD(TAG, "DEBUG: Sector:%" PRIu32 ", total length:%" PRIu32 ", length:%d", cur_sector,
|
|
|
fw_buf_offset, om->om_len - 3);
|
|
|
#endif
|
|
|
- if (om->om_data[2] == 0xff) {
|
|
|
+ if (om->om_data[2] == 0xff)
|
|
|
+ {
|
|
|
cur_packet = 0;
|
|
|
cur_sector++;
|
|
|
ESP_LOGD(TAG, "DEBUG: recv %" PRIu32 " sector", cur_sector);
|
|
|
goto sector_end;
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
ESP_LOGD(TAG, "DEBUG: wait next packet");
|
|
|
cur_packet++;
|
|
|
}
|
|
|
return;
|
|
|
|
|
|
sector_end:
|
|
|
- if (fw_buf_offset < ota_block_size) {
|
|
|
+ if (fw_buf_offset < ota_block_size)
|
|
|
+ {
|
|
|
esp_ble_ota_recv_fw_handler(fw_buf, fw_buf_offset);
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
esp_ble_ota_recv_fw_handler(fw_buf, ota_block_size);
|
|
|
}
|
|
|
fw_buf_offset = 0;
|
|
@@ -228,7 +267,7 @@ sector_end:
|
|
|
|
|
|
cmd_ack[0] = om->om_data[0];
|
|
|
cmd_ack[1] = om->om_data[1];
|
|
|
- cmd_ack[2] = 0x00; //success
|
|
|
+ cmd_ack[2] = 0x00; // success
|
|
|
cmd_ack[3] = 0x00;
|
|
|
crc16 = crc16_ccitt(cmd_ack, 18);
|
|
|
cmd_ack[18] = crc16 & 0xff;
|
|
@@ -244,13 +283,18 @@ static uint16_t crc16_ccitt(const unsigned char *buf, int len)
|
|
|
uint16_t crc16 = 0;
|
|
|
int32_t i;
|
|
|
|
|
|
- while (len--) {
|
|
|
+ while (len--)
|
|
|
+ {
|
|
|
crc16 ^= *buf++ << 8;
|
|
|
|
|
|
- for (i = 0; i < 8; i++) {
|
|
|
- if (crc16 & 0x8000) {
|
|
|
+ for (i = 0; i < 8; i++)
|
|
|
+ {
|
|
|
+ if (crc16 & 0x8000)
|
|
|
+ {
|
|
|
crc16 = (crc16 << 1) ^ 0x1021;
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
crc16 = crc16 << 1;
|
|
|
}
|
|
|
}
|
|
@@ -272,7 +316,8 @@ unsigned int esp_ble_ota_get_fw_length(void)
|
|
|
static esp_err_t
|
|
|
esp_ble_ota_recv_fw_handler(uint8_t *buf, uint32_t length)
|
|
|
{
|
|
|
- if (ota_cb_fun_t.recv_fw_cb) {
|
|
|
+ if (ota_cb_fun_t.recv_fw_cb)
|
|
|
+ {
|
|
|
ota_cb_fun_t.recv_fw_cb(buf, length);
|
|
|
}
|
|
|
|
|
@@ -286,7 +331,7 @@ esp_err_t esp_ble_ota_recv_fw_data_callback(esp_ble_ota_recv_fw_cb_t callback)
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
#else
|
|
|
-esp_err_t esp_ble_ota_recv_fw_data_callback(esp_ble_ota_recv_fw_cb_t callback,
|
|
|
+esp_err_t esp_ble_ota_recv_fw_data_callback(esp_ble_ota_recv_fw_cb_t callback,
|
|
|
esp_decrypt_handle_t esp_decrypt_handle)
|
|
|
{
|
|
|
decrypt_handle_cmp = esp_decrypt_handle;
|
|
@@ -300,15 +345,13 @@ esp_err_t esp_ble_ota_recv_fw_data_callback(esp_ble_ota_recv_fw_cb_t callback,
|
|
|
*----------------------------------------------------*/
|
|
|
|
|
|
#ifdef CONFIG_OTA_WITH_PROTOCOMM
|
|
|
-void
|
|
|
-esp_ble_ota_set_sizes(size_t file_size, size_t block_size)
|
|
|
+void esp_ble_ota_set_sizes(size_t file_size, size_t block_size)
|
|
|
{
|
|
|
ota_total_len = file_size;
|
|
|
ota_block_size = block_size;
|
|
|
}
|
|
|
|
|
|
-void
|
|
|
-esp_ble_ota_finish(void)
|
|
|
+void esp_ble_ota_finish(void)
|
|
|
{
|
|
|
ESP_LOGI(TAG, "Received OTA end command");
|
|
|
start_ota = false;
|
|
@@ -318,8 +361,7 @@ esp_ble_ota_finish(void)
|
|
|
fw_buf = NULL;
|
|
|
}
|
|
|
|
|
|
-void
|
|
|
-esp_ble_ota_write(uint8_t *file, size_t length)
|
|
|
+void esp_ble_ota_write(uint8_t *file, size_t length)
|
|
|
{
|
|
|
struct os_mbuf *om = ble_hs_mbuf_from_flat(file, length);
|
|
|
esp_ble_ota_write_chr(om);
|
|
@@ -336,12 +378,12 @@ ble_ota_start_write_chr(struct os_mbuf *om)
|
|
|
uint8_t cmd_ack[CMD_ACK_LENGTH] = {0x03, 0x00, 0x00, 0x00, 0x00,
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
- 0x00, 0x00, 0x00, 0x00, 0x00
|
|
|
- };
|
|
|
+ 0x00, 0x00, 0x00, 0x00, 0x00};
|
|
|
uint16_t crc16;
|
|
|
|
|
|
esp_ble_ota_char_t ota_char = find_ota_char_and_desr_by_handle(attribute_handle);
|
|
|
- if ((om->om_data[0] == 0x01) && (om->om_data[1] == 0x00)) {
|
|
|
+ if ((om->om_data[0] == 0x01) && (om->om_data[1] == 0x00))
|
|
|
+ {
|
|
|
start_ota = true;
|
|
|
|
|
|
ota_total_len = (om->om_data[2]) + (om->om_data[3] * 256) +
|
|
@@ -355,12 +397,15 @@ ble_ota_start_write_chr(struct os_mbuf *om)
|
|
|
fw_buf = (uint8_t *)malloc(ota_block_size * sizeof(uint8_t));
|
|
|
#else
|
|
|
|
|
|
- fw_buf = (uint8_t *)heap_caps_malloc(ota_block_size * sizeof(uint8_t),MALLOC_CAP_8BIT|MALLOC_CAP_SPIRAM);
|
|
|
+ fw_buf = (uint8_t *)heap_caps_malloc(ota_block_size * sizeof(uint8_t), MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
|
|
|
|
|
|
#endif
|
|
|
- if (fw_buf == NULL) {
|
|
|
+ if (fw_buf == NULL)
|
|
|
+ {
|
|
|
ESP_LOGE(TAG, "%s - malloc fail", __func__);
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
memset(fw_buf, 0x0, ota_block_size);
|
|
|
}
|
|
|
cmd_ack[2] = 0x01;
|
|
@@ -369,11 +414,14 @@ ble_ota_start_write_chr(struct os_mbuf *om)
|
|
|
cmd_ack[18] = crc16 & 0xff;
|
|
|
cmd_ack[19] = (crc16 & 0xff00) >> 8;
|
|
|
esp_ble_ota_notification_data(connection_handle, attribute_handle, cmd_ack, ota_char);
|
|
|
- } else if ((om->om_data[0] == 0x02) && (om->om_data[1] == 0x00)) {
|
|
|
+ }
|
|
|
+ else if ((om->om_data[0] == 0x02) && (om->om_data[1] == 0x00))
|
|
|
+ {
|
|
|
printf("\nCMD_CHAR -> 0 : %d, 1 : %d", om->om_data[0],
|
|
|
om->om_data[1]);
|
|
|
#ifdef CONFIG_PRE_ENC_OTA
|
|
|
- if (esp_encrypted_img_decrypt_end(decrypt_handle_cmp) != ESP_OK) {
|
|
|
+ if (esp_encrypted_img_decrypt_end(decrypt_handle_cmp) != ESP_OK)
|
|
|
+ {
|
|
|
ESP_LOGI(TAG, "Decryption end failed");
|
|
|
}
|
|
|
#endif
|
|
@@ -396,7 +444,8 @@ gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
|
|
|
{
|
|
|
char buf[BLE_UUID_STR_LEN];
|
|
|
|
|
|
- switch (ctxt->op) {
|
|
|
+ switch (ctxt->op)
|
|
|
+ {
|
|
|
case BLE_GATT_REGISTER_OP_SVC:
|
|
|
ESP_LOGD(TAG, "registered service %s with handle=%d\n",
|
|
|
ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf),
|
|
@@ -405,7 +454,7 @@ gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
|
|
|
|
|
|
case BLE_GATT_REGISTER_OP_CHR:
|
|
|
ESP_LOGD(TAG, "registering characteristic %s with "
|
|
|
- "def_handle=%d val_handle=%d\n",
|
|
|
+ "def_handle=%d val_handle=%d\n",
|
|
|
ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf),
|
|
|
ctxt->chr.def_handle,
|
|
|
ctxt->chr.val_handle);
|
|
@@ -432,7 +481,8 @@ ble_ota_gatt_handler(uint16_t conn_handle, uint16_t attr_handle,
|
|
|
|
|
|
attribute_handle = attr_handle;
|
|
|
|
|
|
- switch (ctxt->op) {
|
|
|
+ switch (ctxt->op)
|
|
|
+ {
|
|
|
case BLE_GATT_ACCESS_OP_READ_CHR:
|
|
|
ota_char = find_ota_char_and_desr_by_handle(attr_handle);
|
|
|
ESP_LOGI(TAG, "client read, ota_char: %d", ota_char);
|
|
@@ -443,14 +493,19 @@ ble_ota_gatt_handler(uint16_t conn_handle, uint16_t attr_handle,
|
|
|
ota_char = find_ota_char_and_desr_by_handle(attr_handle);
|
|
|
ESP_LOGD(TAG, "client write; len = %d", ctxt->om->om_len);
|
|
|
|
|
|
- if (ota_char == RECV_FW_CHAR) {
|
|
|
- if (start_ota) {
|
|
|
+ if (ota_char == RECV_FW_CHAR)
|
|
|
+ {
|
|
|
+ if (start_ota)
|
|
|
+ {
|
|
|
esp_ble_ota_write_chr(ctxt->om);
|
|
|
-
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
ESP_LOGE(TAG, "%s - don't receive the start cmd", __func__);
|
|
|
}
|
|
|
- } else if (ota_char == CMD_CHAR) {
|
|
|
+ }
|
|
|
+ else if (ota_char == CMD_CHAR)
|
|
|
+ {
|
|
|
ble_ota_start_write_chr(ctxt->om);
|
|
|
}
|
|
|
break;
|
|
@@ -466,36 +521,38 @@ static const struct ble_gatt_svc_def ota_gatt_db[] = {
|
|
|
/* OTA Service Declaration */
|
|
|
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
|
|
.uuid = BLE_UUID16_DECLARE(BLE_OTA_SERVICE_UUID),
|
|
|
- .characteristics = (struct ble_gatt_chr_def[])
|
|
|
- {
|
|
|
+ .characteristics = (struct ble_gatt_chr_def[]){
|
|
|
{
|
|
|
/* Receive Firmware Characteristic */
|
|
|
.uuid = BLE_UUID16_DECLARE(RECV_FW_UUID),
|
|
|
.access_cb = ble_ota_gatt_handler,
|
|
|
.val_handle = &receive_fw_val,
|
|
|
.flags = BLE_GATT_CHR_F_INDICATE | BLE_GATT_CHR_F_WRITE,
|
|
|
- }, {
|
|
|
+ },
|
|
|
+ {
|
|
|
/* OTA Characteristic */
|
|
|
.uuid = BLE_UUID16_DECLARE(OTA_BAR_UUID),
|
|
|
.access_cb = ble_ota_gatt_handler,
|
|
|
.val_handle = &ota_status_val,
|
|
|
.flags = BLE_GATT_CHR_F_INDICATE | BLE_GATT_CHR_F_READ,
|
|
|
- }, {
|
|
|
+ },
|
|
|
+ {
|
|
|
/* Command Characteristic */
|
|
|
.uuid = BLE_UUID16_DECLARE(COMMAND_UUID),
|
|
|
.access_cb = ble_ota_gatt_handler,
|
|
|
.val_handle = &command_val,
|
|
|
.flags = BLE_GATT_CHR_F_INDICATE | BLE_GATT_CHR_F_WRITE,
|
|
|
- }, {
|
|
|
+ },
|
|
|
+ {
|
|
|
/* Customer characteristic */
|
|
|
.uuid = BLE_UUID16_DECLARE(CUSTOMER_UUID),
|
|
|
.access_cb = ble_ota_gatt_handler,
|
|
|
.val_handle = &custom_val,
|
|
|
.flags = BLE_GATT_CHR_F_INDICATE | BLE_GATT_CHR_F_WRITE,
|
|
|
- }, {
|
|
|
+ },
|
|
|
+ {
|
|
|
0, /* No more characteristics in this service */
|
|
|
- }
|
|
|
- },
|
|
|
+ }},
|
|
|
},
|
|
|
{
|
|
|
0, /* No more services */
|
|
@@ -507,9 +564,12 @@ find_ota_char_and_desr_by_handle(uint16_t handle)
|
|
|
{
|
|
|
esp_ble_ota_char_t ret = INVALID_CHAR;
|
|
|
|
|
|
- for (int i = 0; i < OTA_IDX_NB ; i++) {
|
|
|
- if (handle == ota_handle_table[i]) {
|
|
|
- switch (i) {
|
|
|
+ for (int i = 0; i < OTA_IDX_NB; i++)
|
|
|
+ {
|
|
|
+ if (handle == ota_handle_table[i])
|
|
|
+ {
|
|
|
+ switch (i)
|
|
|
+ {
|
|
|
case RECV_FW_CHAR_VAL_IDX:
|
|
|
ret = RECV_FW_CHAR;
|
|
|
break;
|
|
@@ -540,24 +600,29 @@ esp_ble_ota_notification_data(uint16_t conn_handle, uint16_t attr_handle, uint8_
|
|
|
int rc;
|
|
|
txom = ble_hs_mbuf_from_flat(cmd_ack, CMD_ACK_LENGTH);
|
|
|
|
|
|
- switch (ota_char) {
|
|
|
+ switch (ota_char)
|
|
|
+ {
|
|
|
case RECV_FW_CHAR:
|
|
|
- if (ota_notification.recv_fw_ntf_enable) {
|
|
|
+ if (ota_notification.recv_fw_ntf_enable)
|
|
|
+ {
|
|
|
notify_enable = true;
|
|
|
}
|
|
|
break;
|
|
|
case OTA_STATUS_CHAR:
|
|
|
- if (ota_notification.process_bar_ntf_enable) {
|
|
|
+ if (ota_notification.process_bar_ntf_enable)
|
|
|
+ {
|
|
|
notify_enable = true;
|
|
|
}
|
|
|
break;
|
|
|
case CMD_CHAR:
|
|
|
- if (ota_notification.command_ntf_enable) {
|
|
|
+ if (ota_notification.command_ntf_enable)
|
|
|
+ {
|
|
|
notify_enable = true;
|
|
|
}
|
|
|
break;
|
|
|
case CUS_CHAR:
|
|
|
- if (ota_notification.customer_ntf_enable) {
|
|
|
+ if (ota_notification.customer_ntf_enable)
|
|
|
+ {
|
|
|
notify_enable = true;
|
|
|
}
|
|
|
break;
|
|
@@ -565,11 +630,15 @@ esp_ble_ota_notification_data(uint16_t conn_handle, uint16_t attr_handle, uint8_
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- if (notify_enable) {
|
|
|
+ if (notify_enable)
|
|
|
+ {
|
|
|
rc = ble_gattc_notify_custom(conn_handle, attr_handle, txom);
|
|
|
- if (rc == 0) {
|
|
|
+ if (rc == 0)
|
|
|
+ {
|
|
|
ESP_LOGD(TAG, "Notification sent, attr_handle = %d", attr_handle);
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
ESP_LOGE(TAG, "Error in sending notification, rc = %d", rc);
|
|
|
}
|
|
|
return rc;
|
|
@@ -618,7 +687,7 @@ esp_ble_ota_print_conn_desc(struct ble_gap_conn_desc *desc)
|
|
|
desc->peer_id_addr.type);
|
|
|
print_addr(desc->peer_id_addr.val);
|
|
|
ESP_LOGI(TAG, " conn_itvl=%d conn_latency=%d supervision_timeout=%d "
|
|
|
- "encrypted=%d authenticated=%d bonded=%d\n",
|
|
|
+ "encrypted=%d authenticated=%d bonded=%d\n",
|
|
|
desc->conn_itvl, desc->conn_latency,
|
|
|
desc->supervision_timeout,
|
|
|
desc->sec_state.encrypted,
|
|
@@ -641,11 +710,11 @@ esp_ble_ota_ext_advertise(void)
|
|
|
int rc;
|
|
|
|
|
|
/* First check if any instance is already active */
|
|
|
- if(ble_gap_adv_active())
|
|
|
+ if (ble_gap_adv_active())
|
|
|
return;
|
|
|
|
|
|
/* use defaults for non-set params */
|
|
|
- memset (¶ms, 0, sizeof(params));
|
|
|
+ memset(¶ms, 0, sizeof(params));
|
|
|
|
|
|
/* enable connectable advertising */
|
|
|
params.connectable = 1;
|
|
@@ -655,7 +724,7 @@ esp_ble_ota_ext_advertise(void)
|
|
|
|
|
|
params.primary_phy = BLE_HCI_LE_PHY_1M;
|
|
|
params.secondary_phy = BLE_HCI_LE_PHY_2M;
|
|
|
- //params.tx_power = 127;
|
|
|
+ // params.tx_power = 127;
|
|
|
params.sid = 1;
|
|
|
|
|
|
params.itvl_min = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
|
|
@@ -664,7 +733,7 @@ esp_ble_ota_ext_advertise(void)
|
|
|
/* configure instance 0 */
|
|
|
rc = ble_gap_ext_adv_configure(instance, ¶ms, NULL,
|
|
|
esp_ble_ota_gap_event, NULL);
|
|
|
- assert (rc == 0);
|
|
|
+ assert(rc == 0);
|
|
|
/* in this case only scan response is allowed */
|
|
|
|
|
|
/* get mbuf for scan rsp data */
|
|
@@ -676,11 +745,11 @@ esp_ble_ota_ext_advertise(void)
|
|
|
assert(rc == 0);
|
|
|
|
|
|
rc = ble_gap_ext_adv_set_data(instance, data);
|
|
|
- assert (rc == 0);
|
|
|
+ assert(rc == 0);
|
|
|
|
|
|
/* start advertising */
|
|
|
rc = ble_gap_ext_adv_start(instance, 0, 0);
|
|
|
- assert (rc == 0);
|
|
|
+ assert(rc == 0);
|
|
|
}
|
|
|
#else
|
|
|
static void
|
|
@@ -720,14 +789,14 @@ esp_ble_ota_advertise(void)
|
|
|
fields.name_len = strlen(name);
|
|
|
fields.name_is_complete = 1;
|
|
|
|
|
|
- fields.uuids16 = (ble_uuid16_t[]) {
|
|
|
- BLE_UUID16_INIT(GATT_SVR_SVC_ALERT_UUID)
|
|
|
- };
|
|
|
+ fields.uuids16 = (ble_uuid16_t[]){
|
|
|
+ BLE_UUID16_INIT(GATT_SVR_SVC_ALERT_UUID)};
|
|
|
fields.num_uuids16 = 1;
|
|
|
fields.uuids16_is_complete = 1;
|
|
|
|
|
|
rc = ble_gap_adv_set_fields(&fields);
|
|
|
- if (rc != 0) {
|
|
|
+ if (rc != 0)
|
|
|
+ {
|
|
|
ESP_LOGE(TAG, "error setting advertisement data; rc=%d\n", rc);
|
|
|
return;
|
|
|
}
|
|
@@ -738,7 +807,8 @@ esp_ble_ota_advertise(void)
|
|
|
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
|
|
|
rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER,
|
|
|
&adv_params, esp_ble_ota_gap_event, NULL);
|
|
|
- if (rc != 0) {
|
|
|
+ if (rc != 0)
|
|
|
+ {
|
|
|
ESP_LOGE(TAG, "error enabling advertisement; rc=%d\n", rc);
|
|
|
return;
|
|
|
}
|
|
@@ -767,20 +837,24 @@ esp_ble_ota_gap_event(struct ble_gap_event *event, void *arg)
|
|
|
int rc;
|
|
|
esp_ble_ota_char_t ota_char;
|
|
|
|
|
|
- switch (event->type) {
|
|
|
+ switch (event->type)
|
|
|
+ {
|
|
|
case BLE_GAP_EVENT_CONNECT:
|
|
|
|
|
|
/* A new connection was established or a connection attempt failed. */
|
|
|
ESP_LOGI(TAG, "connection %s; status=%d ",
|
|
|
event->connect.status == 0 ? "established" : "failed",
|
|
|
event->connect.status);
|
|
|
- if (event->connect.status == 0) {
|
|
|
+ if (event->connect.status == 0)
|
|
|
+ {
|
|
|
is_conn = true;
|
|
|
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
|
|
|
assert(rc == 0);
|
|
|
esp_ble_ota_print_conn_desc(&desc);
|
|
|
connection_handle = event->connect.conn_handle;
|
|
|
- } else {
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
/* Connection failed; resume advertising. */
|
|
|
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
|
|
esp_ble_ota_ext_advertise();
|
|
@@ -799,7 +873,7 @@ esp_ble_ota_gap_event(struct ble_gap_event *event, void *arg)
|
|
|
/* Connection terminated; resume advertising. */
|
|
|
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
|
|
esp_ble_ota_ext_advertise();
|
|
|
-#else
|
|
|
+#else
|
|
|
esp_ble_ota_advertise();
|
|
|
#endif
|
|
|
return 0;
|
|
@@ -837,7 +911,7 @@ esp_ble_ota_gap_event(struct ble_gap_event *event, void *arg)
|
|
|
ESP_LOGI(TAG, "client subscribe ble_gap_event, ota_char: %d", ota_char);
|
|
|
|
|
|
ESP_LOGI(TAG, "subscribe event; conn_handle=%d attr_handle=%d "
|
|
|
- "reason=%d prevn=%d curn=%d previ=%d curi=%d\n",
|
|
|
+ "reason=%d prevn=%d curn=%d previ=%d curi=%d\n",
|
|
|
event->subscribe.conn_handle,
|
|
|
event->subscribe.attr_handle,
|
|
|
event->subscribe.reason,
|
|
@@ -846,7 +920,8 @@ esp_ble_ota_gap_event(struct ble_gap_event *event, void *arg)
|
|
|
event->subscribe.prev_indicate,
|
|
|
event->subscribe.cur_indicate);
|
|
|
|
|
|
- switch (ota_char) {
|
|
|
+ switch (ota_char)
|
|
|
+ {
|
|
|
case RECV_FW_CHAR:
|
|
|
ota_notification.recv_fw_ntf_enable = true;
|
|
|
break;
|
|
@@ -895,8 +970,7 @@ esp_ble_ota_gap_event(struct ble_gap_event *event, void *arg)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-int
|
|
|
-ble_ota_gatt_svr_init(void)
|
|
|
+int ble_ota_gatt_svr_init(void)
|
|
|
{
|
|
|
int rc;
|
|
|
|
|
@@ -904,12 +978,14 @@ ble_ota_gatt_svr_init(void)
|
|
|
ble_svc_gatt_init();
|
|
|
|
|
|
rc = ble_gatts_count_cfg(ota_gatt_db);
|
|
|
- if (rc != 0) {
|
|
|
+ if (rc != 0)
|
|
|
+ {
|
|
|
return rc;
|
|
|
}
|
|
|
|
|
|
rc = ble_gatts_add_svcs(ota_gatt_db);
|
|
|
- if (rc != 0) {
|
|
|
+ if (rc != 0)
|
|
|
+ {
|
|
|
return rc;
|
|
|
}
|
|
|
|
|
@@ -932,7 +1008,8 @@ esp_ble_ota_on_sync(void)
|
|
|
|
|
|
/* Figure out address to use while advertising (no privacy for now) */
|
|
|
rc = ble_hs_id_infer_auto(0, &own_addr_type);
|
|
|
- if (rc != 0) {
|
|
|
+ if (rc != 0)
|
|
|
+ {
|
|
|
ESP_LOGE(TAG, "error determining address type; rc=%d\n", rc);
|
|
|
return;
|
|
|
}
|
|
@@ -946,10 +1023,9 @@ esp_ble_ota_on_sync(void)
|
|
|
/* Begin advertising. */
|
|
|
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
|
|
esp_ble_ota_ext_advertise();
|
|
|
-#else
|
|
|
+#else
|
|
|
esp_ble_ota_advertise();
|
|
|
#endif
|
|
|
-
|
|
|
}
|
|
|
|
|
|
void esp_ble_ota_host_task(void *param)
|
|
@@ -967,7 +1043,8 @@ esp_ble_ota_host_init(void)
|
|
|
int rc;
|
|
|
|
|
|
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
|
|
- if (esp_nimble_init() != 0) {
|
|
|
+ if (esp_nimble_init() != 0)
|
|
|
+ {
|
|
|
ESP_LOGE(TAG, "nimble host init failed\n");
|
|
|
return ESP_FAIL;
|
|
|
}
|
|
@@ -990,25 +1067,24 @@ esp_ble_ota_host_init(void)
|
|
|
rc = ble_ota_gatt_svr_init();
|
|
|
assert(rc == 0);
|
|
|
|
|
|
- #include "esp_mac.h"
|
|
|
- char* device_name = (char*)malloc(sizeof("YC-OTA#")+4);
|
|
|
- memcpy(device_name,"YC-OTA#",sizeof("YC-OTA#"));
|
|
|
-
|
|
|
-
|
|
|
- uint8_t macAddr[7];
|
|
|
- char mac_5[4];
|
|
|
- char mac_6[4];
|
|
|
- esp_read_mac((uint8_t *)macAddr, ESP_MAC_WIFI_STA);
|
|
|
- ESP_LOG_BUFFER_HEX(TAG,&macAddr,6);
|
|
|
- itoa(macAddr[4],mac_5,16);
|
|
|
- itoa(macAddr[5],mac_6,16);
|
|
|
-
|
|
|
- strcat(device_name,mac_5);
|
|
|
- strcat(device_name,mac_6);
|
|
|
- ESP_LOGW(TAG,"ble name[%s]",device_name);
|
|
|
+#include "esp_mac.h"
|
|
|
+ char *device_name = (char *)malloc(sizeof("YC-OTA#") + 4);
|
|
|
+ memcpy(device_name, "YC-OTA#", sizeof("YC-OTA#"));
|
|
|
+
|
|
|
+ uint8_t macAddr[7];
|
|
|
+ char mac_5[4];
|
|
|
+ char mac_6[4];
|
|
|
+ esp_read_mac((uint8_t *)macAddr, ESP_MAC_WIFI_STA);
|
|
|
+ ESP_LOG_BUFFER_HEX(TAG, &macAddr, 6);
|
|
|
+ itoa(macAddr[4], mac_5, 16);
|
|
|
+ itoa(macAddr[5], mac_6, 16);
|
|
|
+
|
|
|
+ strcat(device_name, mac_5);
|
|
|
+ strcat(device_name, mac_6);
|
|
|
+ ESP_LOGW(TAG, "ble name[%s]", device_name);
|
|
|
/* Set the default device name. */
|
|
|
rc = ble_svc_gap_device_name_set(device_name);
|
|
|
- free(device_name);
|
|
|
+ free(device_name);
|
|
|
assert(rc == 0);
|
|
|
|
|
|
/* XXX Need to have template for store */
|