Skip to content

Commit

Permalink
Update nimble core to esp-nimble commit #e3cbdc0
Browse files Browse the repository at this point in the history
* Commit #3fa3334 support hci log for nimble removed
  • Loading branch information
h2zero committed Jun 10, 2024
1 parent 685fb5e commit d378739
Show file tree
Hide file tree
Showing 29 changed files with 842 additions and 289 deletions.
4 changes: 4 additions & 0 deletions src/nimble/esp_port/port/include/esp_nimble_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,10 @@
#define MYNEWT_VAL_BLE_GATT_READ_MULT (MYNEWT_VAL_BLE_ROLE_CENTRAL)
#endif

#ifndef MYNEWT_VAL_BLE_GATT_READ_MULT_VAR
#define MYNEWT_VAL_BLE_GATT_READ_MULT_VAR (MYNEWT_VAL_BLE_ROLE_CENTRAL)
#endif

#ifndef MYNEWT_VAL_BLE_GATT_READ_UUID
#define MYNEWT_VAL_BLE_GATT_READ_UUID (MYNEWT_VAL_BLE_ROLE_CENTRAL)
#endif
Expand Down
22 changes: 22 additions & 0 deletions src/nimble/nimble/host/include/host/ble_esp_gap.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,28 @@ int ble_gap_wl_read_size(uint8_t *size);
*/
int ble_gap_host_check_status(void);

/**
* This API is called to get local used address and address type.
*
* @param addr On success, locally used address will be stored here.
*
* @return 0 on success; nonzero on failure.
*/
int ble_gap_get_local_used_addr(ble_addr_t *addr);

/**
* This API is called to get ADV data for a specific type.
*
* @param adv_data Pointer of ADV data (unsigned 8 bit integer) which to be resolved.
* @param adv_type Finding ADV data type.
* @param adv_data_len Total length of Advertising data.
* @param length Return the length of ADV data not including type.
*
* @return Pointer (unsigned 8 bit integer) of type specific ADV data.
*/
uint8_t* ble_resolve_adv_data(const uint8_t *adv_data, uint8_t adv_type, uint8_t adv_data_len , uint8_t * length);

#if MYNEWT_VAL(BLE_HCI_VS)
#if MYNEWT_VAL(BLE_POWER_CONTROL)

Expand Down
27 changes: 25 additions & 2 deletions src/nimble/nimble/host/include/host/ble_gap.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ struct hci_conn_update;
#define BLE_GAP_EVENT_REATTEMPT_COUNT 29
#define BLE_GAP_EVENT_AUTHORIZE 30
#define BLE_GAP_EVENT_TEST_UPDATE 31

#define BLE_GAP_EVENT_DATA_LEN_CHG 32

/* DTM events */
#define BLE_GAP_DTM_TX_START_EVT 0
Expand Down Expand Up @@ -1174,8 +1174,31 @@ struct ble_gap_event {
* Valid only for BLE_GAP_DTM_END_EVT
* shall be 0 for a transmitter.
*/
uint8_t num_pkt;
uint16_t num_pkt;
} dtm_state;

/**
* Represent an event for LE Data length change
*
* Valid for the following event types:
* o BLE_GAP_EVENT_DATA_LEN_CHG
*/
struct {
/* Connection handle */
uint16_t conn_handle;

/* Max Tx Payload octotes */
uint16_t max_tx_octets;

/* Max Tx Time */
uint16_t max_tx_time;

/* Max Rx payload octet */
uint16_t max_rx_octets;

/* Max Rx Time */
uint16_t max_rx_time;
} data_len_chg;
};
};

Expand Down
17 changes: 16 additions & 1 deletion src/nimble/nimble/host/include/host/ble_gatt.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,17 @@ typedef int ble_gatt_attr_fn(uint16_t conn_handle,
struct ble_gatt_attr *attr,
void *arg);

/**
* The host will free the attribute mbuf automatically after the callback is
* executed. The application can take ownership of the mbuf and prevent it
* from being freed by assigning NULL to attr->om.
*/
typedef int ble_gatt_attr_mult_fn(uint16_t conn_handle,
const struct ble_gatt_error *error,
struct ble_gatt_attr *attrs,
uint8_t num_attrs,
void *arg);

/**
* The host will free the attribute mbufs automatically after the callback is
* executed. The application can take ownership of the mbufs and prevent them
Expand Down Expand Up @@ -545,6 +556,10 @@ int ble_gattc_read_mult(uint16_t conn_handle, const uint16_t *handles,
uint8_t num_handles, ble_gatt_attr_fn *cb,
void *cb_arg);

int ble_gattc_read_mult_var(uint16_t conn_handle, const uint16_t *handles,
uint8_t num_handles, ble_gatt_attr_mult_fn *cb,
void *cb_arg);

/**
* Initiates GATT procedure: Write Without Response. This function consumes
* the supplied mbuf regardless of the outcome.
Expand Down Expand Up @@ -882,7 +897,7 @@ struct ble_gatt_cpfd {
uint16_t unit;

/** The name space of the description. */
uint8_t namespace_;
uint8_t name_space;

/** The description of this characteristic. Depends on name space. */
uint16_t description;
Expand Down
1 change: 1 addition & 0 deletions src/nimble/nimble/host/include/host/ble_hs_pvcy.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ void ble_hs_resolv_deinit(void);
#endif

int ble_hs_pvcy_set_resolve_enabled(int enable);
void ble_hs_set_rpa_timeout(uint16_t timeout);

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions src/nimble/nimble/host/include/host/ble_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ struct ble_store_key_sec {
*/
struct ble_store_value_sec {
ble_addr_t peer_addr;
uint16_t bond_count;

uint8_t key_size;
uint16_t ediv;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ struct ble_hs_cfg;
#if MYNEWT_VAL(BLE_GATT_CACHING)
#define BLE_SVC_GATT_CHR_DATABASE_HASH_UUID16 0x2b2a

uint16_t ble_svc_gatt_changed_handle();
uint16_t ble_svc_gatt_hash_handle();
uint16_t ble_svc_gatt_csf_handle();
uint8_t ble_svc_gatt_get_csfs();
uint16_t ble_svc_gatt_changed_handle(void);
uint16_t ble_svc_gatt_hash_handle(void);
uint16_t ble_svc_gatt_csf_handle(void);
uint8_t ble_svc_gatt_get_csfs(void);
#endif

uint8_t ble_svc_gatt_get_local_cl_supported_feat(void);
Expand Down
1 change: 1 addition & 0 deletions src/nimble/nimble/host/src/ble_att.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ static const struct ble_att_rx_dispatch_entry ble_att_rx_dispatch[] = {
{ BLE_ATT_OP_INDICATE_REQ, ble_att_svr_rx_indicate },
{ BLE_ATT_OP_INDICATE_RSP, ble_att_clt_rx_indicate },
{ BLE_ATT_OP_READ_MULT_VAR_REQ, ble_att_svr_rx_read_mult_var },
{ BLE_ATT_OP_READ_MULT_VAR_RSP, ble_att_clt_rx_read_mult_var },
{ BLE_ATT_OP_WRITE_CMD, ble_att_svr_rx_write_no_rsp },
#ifdef ESP_PLATFORM
{ BLE_ATT_OP_SIGNED_WRITE_CMD, ble_att_svr_rx_signed_write },
Expand Down
46 changes: 36 additions & 10 deletions src/nimble/nimble/host/src/ble_att_clt.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ ble_att_clt_rx_read_blob(uint16_t conn_handle, struct os_mbuf **rxom)
*****************************************************************************/
int
ble_att_clt_tx_read_mult(uint16_t conn_handle, const uint16_t *handles,
int num_handles)
int num_handles, bool variable)
{
#if !NIMBLE_BLE_ATT_CLT_READ_MULT
return BLE_HS_ENOTSUP;
Expand All @@ -546,12 +546,15 @@ ble_att_clt_tx_read_mult(uint16_t conn_handle, const uint16_t *handles,
struct ble_att_read_mult_req *req;
struct os_mbuf *txom;
int i;
uint8_t op;

if (num_handles < 1) {
return BLE_HS_EINVAL;
}

req = ble_att_cmd_get(BLE_ATT_OP_READ_MULT_REQ,
op = variable ? BLE_ATT_OP_READ_MULT_VAR_REQ : BLE_ATT_OP_READ_MULT_REQ;

req = ble_att_cmd_get(op,
sizeof(req->handles[0]) * num_handles,
&txom);
if (req == NULL) {
Expand All @@ -573,7 +576,19 @@ ble_att_clt_rx_read_mult(uint16_t conn_handle, struct os_mbuf **rxom)
#endif

/* Pass the Attribute Value field to GATT. */
ble_gattc_rx_read_mult_rsp(conn_handle, 0, rxom);
ble_gattc_rx_read_mult_rsp(conn_handle, 0, rxom, false);
return 0;
}

int
ble_att_clt_rx_read_mult_var(uint16_t conn_handle, struct os_mbuf **rxom)
{
#if !NIMBLE_BLE_ATT_CLT_READ_MULT_VAR
return BLE_HS_ENOTSUP;
#endif

/* Pass the Attribute Value field to GATT. */
ble_gattc_rx_read_mult_rsp(conn_handle, 0, rxom, true);
return 0;
}

Expand Down Expand Up @@ -765,6 +780,7 @@ ble_att_clt_tx_signed_write_cmd(uint16_t conn_handle, uint16_t handle, uint8_t *
struct os_mbuf *txom2;
uint8_t cmac[16];
uint8_t *message = NULL;
uint8_t len;
int rc;
int i;

Expand All @@ -781,27 +797,37 @@ ble_att_clt_tx_signed_write_cmd(uint16_t conn_handle, uint16_t handle, uint8_t *
}
cmd->handle = htole16(handle);

/* Message to be signed is message||sign_counter,
/* Message to be signed is opcode||handle||message||sign_counter,
* where || represents concatenation
*/
message = nimble_platform_mem_malloc(OS_MBUF_PKTLEN(txom) + sizeof(counter));
rc = os_mbuf_copydata(txom, 0, OS_MBUF_PKTLEN(txom), message);
len = BLE_ATT_SIGNED_WRITE_DATA_OFFSET + OS_MBUF_PKTLEN(txom) + sizeof(counter);
message = nimble_platform_mem_malloc(len);

/** Copying opcode and handle */
rc = os_mbuf_copydata(txom2, 0, BLE_ATT_SIGNED_WRITE_DATA_OFFSET, message);
if (rc != 0) {
goto err;
}

/** Copying message */
rc = os_mbuf_copydata(txom, 0, OS_MBUF_PKTLEN(txom), &message[BLE_ATT_SIGNED_WRITE_DATA_OFFSET]);
if (rc != 0) {
goto err;
}
memcpy(&message[OS_MBUF_PKTLEN(txom)], &counter, sizeof(counter));

/** Copying sign counter */
memcpy(&message[BLE_ATT_SIGNED_WRITE_DATA_OFFSET + OS_MBUF_PKTLEN(txom)], &counter, sizeof(counter));

/* ble_sm_alg_aes_cmac takes data in little-endian format,
* so converting it to LE.
*/
swap_in_place(message, OS_MBUF_PKTLEN(txom) + sizeof(counter));
swap_in_place(message, len);

/* Getting the CMAC (Cipher-based Message Authentication Code)
* for the message using our CSRK for this connection.
*/
memset(cmac, 0, sizeof cmac);
rc = ble_sm_alg_aes_cmac(csrk, message,
OS_MBUF_PKTLEN(txom) + sizeof(counter), cmac);
rc = ble_sm_alg_aes_cmac(csrk, message, len, cmac);
if (rc != 0) {
goto err;
}
Expand Down
3 changes: 2 additions & 1 deletion src/nimble/nimble/host/src/ble_att_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,9 @@ int ble_att_clt_tx_read_blob(uint16_t conn_handle, uint16_t handle,
uint16_t offset);
int ble_att_clt_rx_read_blob(uint16_t conn_handle, struct os_mbuf **rxom);
int ble_att_clt_tx_read_mult(uint16_t conn_handle,
const uint16_t *handles, int num_handles);
const uint16_t *handles, int num_handles, bool variable);
int ble_att_clt_rx_read_mult(uint16_t conn_handle, struct os_mbuf **rxom);
int ble_att_clt_rx_read_mult_var(uint16_t conn_handle, struct os_mbuf **rxom);
int ble_att_clt_tx_read_type(uint16_t conn_handle, uint16_t start_handle,
uint16_t end_handle, const ble_uuid_t *uuid);
int ble_att_clt_rx_read_type(uint16_t conn_handle, struct os_mbuf **rxom);
Expand Down
27 changes: 17 additions & 10 deletions src/nimble/nimble/host/src/ble_att_svr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,7 @@ ble_att_svr_build_read_mult_rsp_var(uint16_t conn_handle,
int
ble_att_svr_rx_read_mult_var(uint16_t conn_handle, struct os_mbuf **rxom)
{
#if (!MYNEWT_VAL(BLE_ATT_SVR_READ_MULT) || (MYNEWT_VAL(BLE_VERSION) < 52))
#if !MYNEWT_VAL(BLE_ATT_SVR_READ_MULT)
return BLE_HS_ENOTSUP;
#endif

Expand Down Expand Up @@ -2358,7 +2358,9 @@ ble_att_svr_rx_signed_write(uint16_t conn_handle, struct os_mbuf **rxom)
uint16_t handle;
uint8_t sign[12];
uint8_t cmac[16];
uint8_t csrk[16];
uint8_t *message = NULL;
uint16_t len;
int rc;

rc = ble_gap_conn_find(conn_handle, &desc);
Expand Down Expand Up @@ -2388,9 +2390,6 @@ ble_att_svr_rx_signed_write(uint16_t conn_handle, struct os_mbuf **rxom)

handle = le16toh(req->handle);

/* Strip the request base from the front of the mbuf. */
os_mbuf_adj(*rxom, sizeof(*req));

os_mbuf_copydata(*rxom,
OS_MBUF_PKTLEN(*rxom) - (BLE_ATT_SIGNED_WRITE_CMD_BASE_SZ - BLE_ATT_SIGNED_WRITE_DATA_OFFSET),
BLE_ATT_SIGNED_WRITE_CMD_BASE_SZ - BLE_ATT_SIGNED_WRITE_DATA_OFFSET,
Expand All @@ -2400,16 +2399,22 @@ ble_att_svr_rx_signed_write(uint16_t conn_handle, struct os_mbuf **rxom)
os_mbuf_adj(*rxom, -(BLE_ATT_SIGNED_WRITE_CMD_BASE_SZ - BLE_ATT_SIGNED_WRITE_DATA_OFFSET));

/* Authentication procedure */
message = nimble_platform_mem_malloc(OS_MBUF_PKTLEN(*rxom) + sizeof(value_sec.sign_counter));
os_mbuf_copydata(*rxom, 0, OS_MBUF_PKTLEN(*rxom), message);
memcpy(&message[OS_MBUF_PKTLEN(*rxom)], &value_sec.sign_counter, sizeof(value_sec.sign_counter));
len = OS_MBUF_PKTLEN(*rxom) + sizeof(value_sec.sign_counter) + 1;
message = nimble_platform_mem_malloc(len);

message[0] = BLE_ATT_OP_SIGNED_WRITE_CMD;
os_mbuf_copydata(*rxom, 0, OS_MBUF_PKTLEN(*rxom), &message[1]);
memcpy(&message[1 + OS_MBUF_PKTLEN(*rxom)], &value_sec.sign_counter, sizeof(value_sec.sign_counter));

/* Converting message into little endian format */
swap_in_place(message, OS_MBUF_PKTLEN(*rxom) + sizeof(value_sec.sign_counter));
swap_in_place(message, len);

/* Converting CSRK into little endian format */
swap_buf(csrk, value_sec.csrk, 16);

/* Using AES-CMAC to get the CMAC from the message and CSRK of this device */
memset(cmac, 0, sizeof cmac);
rc = ble_sm_alg_aes_cmac(value_sec.csrk, message, OS_MBUF_PKTLEN(*rxom) + sizeof(value_sec.sign_counter), cmac);
rc = ble_sm_alg_aes_cmac(csrk, message, len, cmac);
if (rc != 0) {
goto err;
}
Expand All @@ -2435,6 +2440,9 @@ ble_att_svr_rx_signed_write(uint16_t conn_handle, struct os_mbuf **rxom)
goto err;
}

/* Strip the request base from the front of the mbuf. */
os_mbuf_adj(*rxom, sizeof(*req));

rc = ble_att_svr_write_handle(conn_handle, handle, 0, rxom, &att_err);
if (rc != 0) {
goto err;
Expand All @@ -2444,7 +2452,6 @@ ble_att_svr_rx_signed_write(uint16_t conn_handle, struct os_mbuf **rxom)
return 0;
err:
if(message != NULL) nimble_platform_mem_free(message);
ble_gap_terminate(conn_handle, BLE_ERR_AUTH_FAIL);
return rc;
}
#endif
Expand Down
Loading

0 comments on commit d378739

Please sign in to comment.