From ffc5ab7deb057edcb0deb3749b2b834c19989901 Mon Sep 17 00:00:00 2001 From: h2zero Date: Sun, 29 Jan 2023 08:03:42 -0700 Subject: [PATCH 1/2] Update NimBLE core to esp-nimble @f566133 --- src/nimble/nimble/host/include/host/ble_gap.h | 196 +++++++++++++++- .../nimble/host/include/host/ble_gatt.h | 21 ++ src/nimble/nimble/host/include/host/ble_hs.h | 2 + .../nimble/host/include/host/ble_uuid.h | 14 +- src/nimble/nimble/host/mesh/src/proxy.c | 4 +- .../host/services/ans/src/ble_svc_ans.c | 4 +- src/nimble/nimble/host/src/ble_att_clt.c | 2 +- src/nimble/nimble/host/src/ble_gap.c | 222 +++++++++++++++++- src/nimble/nimble/host/src/ble_gap_priv.h | 7 + src/nimble/nimble/host/src/ble_gatt_priv.h | 2 +- src/nimble/nimble/host/src/ble_gattc.c | 66 ++++-- src/nimble/nimble/host/src/ble_gatts.c | 10 +- src/nimble/nimble/host/src/ble_hs.c | 44 ++-- src/nimble/nimble/host/src/ble_hs_hci.c | 13 + src/nimble/nimble/host/src/ble_hs_hci_cmd.c | 4 +- src/nimble/nimble/host/src/ble_hs_hci_evt.c | 148 ++++++++++-- src/nimble/nimble/host/src/ble_hs_mbuf.c | 8 + src/nimble/nimble/host/src/ble_hs_startup.c | 20 ++ src/nimble/nimble/include/nimble/hci_common.h | 96 ++++++++ .../porting/nimble/include/os/os_mbuf.h | 9 +- src/nimble/porting/nimble/src/nimble_port.c | 28 ++- .../npl/freertos/src/npl_os_freertos.c | 2 +- 22 files changed, 836 insertions(+), 86 deletions(-) diff --git a/src/nimble/nimble/host/include/host/ble_gap.h b/src/nimble/nimble/host/include/host/ble_gap.h index 7f77d7fb..e75dd73c 100644 --- a/src/nimble/nimble/host/include/host/ble_gap.h +++ b/src/nimble/nimble/host/include/host/ble_gap.h @@ -44,6 +44,7 @@ struct hci_conn_update; #define BLE_GAP_SCAN_WIN_MS(t) ((t) * 1000 / BLE_HCI_SCAN_ITVL) #define BLE_GAP_CONN_ITVL_MS(t) ((t) * 1000 / BLE_HCI_CONN_ITVL) #define BLE_GAP_SUPERVISION_TIMEOUT_MS(t) ((t) / 10) +#define BLE_GAP_PERIODIC_ITVL_MS(t) ((t) * 1000 / BLE_HCI_PERIODIC_ADV_ITVL) /** 30 ms. */ #define BLE_GAP_ADV_FAST_INTERVAL1_MIN BLE_GAP_ADV_ITVL_MS(30) @@ -135,6 +136,9 @@ struct hci_conn_update; #define BLE_GAP_EVENT_PERIODIC_SYNC_LOST 22 #define BLE_GAP_EVENT_SCAN_REQ_RCVD 23 #define BLE_GAP_EVENT_PERIODIC_TRANSFER 24 +#define BLE_GAP_EVENT_PATHLOSS_THRESHOLD 25 +#define BLE_GAP_EVENT_TRANSMIT_POWER 26 +#define BLE_GAP_EVENT_SUBRATE_CHANGE 27 /*** Reason codes for the subscribe GAP event. */ @@ -973,6 +977,82 @@ struct ble_gap_event { uint8_t adv_clk_accuracy; } periodic_transfer; #endif + +#if MYNEWT_VAL(BLE_POWER_CONTROL) + /** + * Represents a change in either local transmit power or remote transmit + * power. Valid for the following event types: + * o BLE_GAP_EVENT_PATHLOSS_THRESHOLD + */ + + struct { + /** Connection handle */ + uint16_t conn_handle; + + /** Current Path Loss */ + uint8_t current_path_loss; + + /** Entered Zone */ + uint8_t zone_entered; + } pathloss_threshold; + + /** + * Represents crossing of path loss threshold set via LE Set Path Loss + * Reporting Parameter command. Valid for the following event types: + * o BLE_GAP_EVENT_TRANSMIT_POWER + */ + + struct { + /** BLE_ERR_SUCCESS on success or error code on failure */ + uint8_t status; + + /** Connection Handle */ + uint16_t conn_handle; + + /** Reason indicating why event was sent */ + uint8_t reason; + + /** Advertising PHY */ + uint8_t phy; + + /** Transmit power Level */ + uint8_t transmit_power_level; + + /** Transmit Power Level Flag */ + uint8_t transmit_power_level_flag; + + /** Delta indicating change in transmit Power Level */ + uint8_t delta; + } transmit_power; +#endif + +#if MYNEWT_VAL(BLE_CONN_SUBRATING) + /** + * Represents a subrate change event that indicates connection subrate update procedure + * has completed and some parameters have changed Valid for + * the following event types: + * o BLE_GAP_EVENT_SUBRATE_CHANGE + */ + struct { + /** BLE_ERR_SUCCESS on success or error code on failure */ + uint8_t status; + + /** Connection Handle */ + uint16_t conn_handle; + + /** Subrate Factor */ + uint16_t subrate_factor; + + /** Peripheral Latency */ + uint16_t periph_latency; + + /** Continuation Number */ + uint16_t cont_num; + + /** Supervision Timeout */ + uint16_t supervision_tmo; + } subrate_change; +#endif }; }; @@ -1373,12 +1453,12 @@ struct ble_gap_periodic_adv_params { /** If include TX power in advertising PDU */ unsigned int include_tx_power:1; - /** Minimum advertising interval in 0.625ms units, if 0 stack use sane + /** Minimum advertising interval in 1.25ms units, if 0 stack use sane * defaults */ uint16_t itvl_min; - /** Maximum advertising interval in 0.625ms units, if 0 stack use sane + /** Maximum advertising interval in 1.25ms units, if 0 stack use sane * defaults */ uint16_t itvl_max; @@ -2113,6 +2193,45 @@ int ble_gap_set_prefered_default_le_phy(uint8_t tx_phys_mask, int ble_gap_set_prefered_le_phy(uint16_t conn_handle, uint8_t tx_phys_mask, uint8_t rx_phys_mask, uint16_t phy_opts); +#if MYNEWT_VAL(BLE_CONN_SUBRATING) +/** + * Set default subrate + * + * @param subrate_min Min subrate factor allowed in request by a peripheral + * @param subrate_max Max subrate factor allowed in request by a peripheral + * @param max_latency Max peripheral latency allowed in units of + * subrated conn interval. + * + * @param cont_num Min number of underlying conn event to remain active + * after a packet containing PDU with non-zero length field + * is sent or received in request by a peripheral. + * + * @param supervision_timeout Max supervision timeout allowed in request by a peripheral + */ +int ble_gap_set_default_subrate(uint16_t subrate_min, uint16_t subrate_max, uint16_t max_latency, + uint16_t cont_num, uint16_t supervision_timeout); + +/** + * Subrate Request + * + * @param conn_handle Connection Handle of the ACL. + * @param subrate_min Min subrate factor to be applied + * @param subrate_max Max subrate factor to be applied + * @param max_latency Max peripheral latency allowed in units of + * subrated conn interval. + * + * @param cont_num Min number of underlying conn event to remain active + * after a packet containing PDU with non-zero length field + * is sent or received in request by a peripheral. + * + * @param supervision_timeout Max supervision timeout allowed for this connection + */ + +int +ble_gap_subrate_req(uint16_t conn_handle, uint16_t subrate_min, uint16_t subrate_max, + uint16_t max_latency, uint16_t cont_num, + uint16_t supervision_timeout); +#endif /** * Event listener structure * @@ -2152,6 +2271,79 @@ int ble_gap_event_listener_register(struct ble_gap_event_listener *listener, */ int ble_gap_event_listener_unregister(struct ble_gap_event_listener *listener); +/** + * Enable Set Path Loss Reporting. + * + * @param conn_handle Connection handle + * @params enable 1: Enable + * 0: Disable + * + * @return 0 on success; nonzero on failure. + */ + +int ble_gap_set_path_loss_reporting_enable(uint16_t conn_handle, uint8_t enable); + +/** + * Enable Reporting of Transmit Power + * + * @param conn_handle Connection handle + * @params local_enable 1: Enable local transmit power reports + * 0: Disable local transmit power reports + * + * @params remote_enable 1: Enable remote transmit power reports + * 0: Disable remote transmit power reports + * + * @return 0 on success; nonzero on failure. + */ +int ble_gap_set_transmit_power_reporting_enable(uint16_t conn_handle, + uint8_t local_enable, + uint8_t remote_enable); + +/** + * LE Enhanced Read Transmit Power Level + * + * @param conn_handle Connection handle + * @params phy Advertising Phy + * + * @params status 0 on success; nonzero on failure. + * @params conn_handle Connection handle + * @params phy Advertising Phy + * + * @params curr_tx_power_level Current trasnmit Power Level + * + * @params mx_tx_power_level Maximum transmit power level + * + * @return 0 on success; nonzero on failure. + */ +int ble_gap_enh_read_transmit_power_level(uint16_t conn_handle, uint8_t phy, uint8_t *out_status, uint8_t *out_phy , + uint8_t *out_curr_tx_power_level, uint8_t *out_max_tx_power_level); + +/** + * Read Remote Transmit Power Level + * + * @param conn_handle Connection handle + * @params phy Advertising Phy + * + * @return 0 on success; nonzero on failure. + */ +int ble_gap_read_remote_transmit_power_level(uint16_t conn_handle, uint8_t phy); + +/** + * Set Path Loss Reproting Param + * + * @param conn_handle Connection handle + * @params high_threshold High Threshold value for path loss + * @params high_hysteresis Hysteresis value for high threshold + * @params low_threshold Low Threshold value for path loss + * @params low_hysteresis Hysteresis value for low threshold + * @params min_time_spent Minimum time controller observes the path loss + * + * @return 0 on success; nonzero on failure. + */ +int ble_gap_set_path_loss_reporting_param(uint16_t conn_handle, uint8_t high_threshold, + uint8_t high_hysteresis, uint8_t low_threshold, + uint8_t low_hysteresis, uint16_t min_time_spent); + #ifdef __cplusplus } #endif diff --git a/src/nimble/nimble/host/include/host/ble_gatt.h b/src/nimble/nimble/host/include/host/ble_gatt.h index ee8f177f..942936cd 100644 --- a/src/nimble/nimble/host/include/host/ble_gatt.h +++ b/src/nimble/nimble/host/include/host/ble_gatt.h @@ -470,6 +470,12 @@ int ble_gattc_write_reliable(uint16_t conn_handle, * * @return 0 on success; nonzero on failure. */ +int ble_gatts_notify_custom(uint16_t conn_handle, uint16_t att_handle, + struct os_mbuf *om); + +/** + * Deprecated. Should not be used. Use ble_gatts_notify_custom instead. + */ int ble_gattc_notify_custom(uint16_t conn_handle, uint16_t att_handle, struct os_mbuf *om); @@ -485,6 +491,11 @@ int ble_gattc_notify_custom(uint16_t conn_handle, uint16_t att_handle, * * @return 0 on success; nonzero on failure. */ +int ble_gatts_notify(uint16_t conn_handle, uint16_t chr_val_handle); + +/** + * Deprecated. Should not be used. Use ble_gatts_notify instead. + */ int ble_gattc_notify(uint16_t conn_handle, uint16_t chr_val_handle); /** @@ -501,6 +512,11 @@ int ble_gattc_notify(uint16_t conn_handle, uint16_t chr_val_handle); * * @return 0 on success; nonzero on failure. */ +int ble_gatts_indicate_custom(uint16_t conn_handle, uint16_t chr_val_handle, + struct os_mbuf *txom); +/** + * Deprecated. Should not be used. Use ble_gatts_indicate_custom instead. + */ int ble_gattc_indicate_custom(uint16_t conn_handle, uint16_t chr_val_handle, struct os_mbuf *txom); @@ -516,6 +532,11 @@ int ble_gattc_indicate_custom(uint16_t conn_handle, uint16_t chr_val_handle, * * @return 0 on success; nonzero on failure. */ +int ble_gatts_indicate(uint16_t conn_handle, uint16_t chr_val_handle); + +/** + * Deprecated. Should not be used. Use ble_gatts_indicate instead. + */ int ble_gattc_indicate(uint16_t conn_handle, uint16_t chr_val_handle); int ble_gattc_init(void); diff --git a/src/nimble/nimble/host/include/host/ble_hs.h b/src/nimble/nimble/host/include/host/ble_hs.h index f24b8a9e..e3fa5488 100644 --- a/src/nimble/nimble/host/include/host/ble_hs.h +++ b/src/nimble/nimble/host/include/host/ble_hs.h @@ -54,6 +54,8 @@ extern "C" { /** Connection handle not present */ #define BLE_HS_CONN_HANDLE_NONE 0xffff +#define BLE_HS_CTRL_DATA_HDR_SZ 0x4 + /** * @brief Bluetooth Host Error Code * @defgroup bt_host_err Bluetooth Host Error Code diff --git a/src/nimble/nimble/host/include/host/ble_uuid.h b/src/nimble/nimble/host/include/host/ble_uuid.h index d3576c59..cc368368 100644 --- a/src/nimble/nimble/host/include/host/ble_uuid.h +++ b/src/nimble/nimble/host/include/host/ble_uuid.h @@ -82,19 +82,25 @@ typedef union { #define BLE_UUID16_INIT(uuid16) \ { \ - .u.type = BLE_UUID_TYPE_16, \ + .u = { \ + .type = BLE_UUID_TYPE_16, \ + }, \ .value = (uuid16), \ } #define BLE_UUID32_INIT(uuid32) \ { \ - .u.type = BLE_UUID_TYPE_32, \ + .u = { \ + .type = BLE_UUID_TYPE_32, \ + }, \ .value = (uuid32), \ } -#define BLE_UUID128_INIT(uuid128...) \ +#define BLE_UUID128_INIT(uuid128 ...) \ { \ - .u.type = BLE_UUID_TYPE_128, \ + .u = { \ + .type = BLE_UUID_TYPE_128, \ + }, \ .value = { uuid128 }, \ } diff --git a/src/nimble/nimble/host/mesh/src/proxy.c b/src/nimble/nimble/host/mesh/src/proxy.c index d8b67930..bfd0336b 100644 --- a/src/nimble/nimble/host/mesh/src/proxy.c +++ b/src/nimble/nimble/host/mesh/src/proxy.c @@ -999,7 +999,7 @@ static int proxy_send(uint16_t conn_handle, const void *data, uint16_t len) if (gatt_svc == MESH_GATT_PROXY) { om = ble_hs_mbuf_from_flat(data, len); assert(om); - err = ble_gattc_notify_custom(conn_handle, svc_handles.proxy_data_out_h, om); + err = ble_gatts_notify_custom(conn_handle, svc_handles.proxy_data_out_h, om); notify_complete(); } #endif @@ -1008,7 +1008,7 @@ static int proxy_send(uint16_t conn_handle, const void *data, uint16_t len) if (gatt_svc == MESH_GATT_PROV) { om = ble_hs_mbuf_from_flat(data, len); assert(om); - err = ble_gattc_notify_custom(conn_handle, svc_handles.prov_data_out_h, om); + err = ble_gatts_notify_custom(conn_handle, svc_handles.prov_data_out_h, om); notify_complete(); } #endif diff --git a/src/nimble/nimble/host/services/ans/src/ble_svc_ans.c b/src/nimble/nimble/host/services/ans/src/ble_svc_ans.c index 2fc50c4b..eb40b056 100644 --- a/src/nimble/nimble/host/services/ans/src/ble_svc_ans.c +++ b/src/nimble/nimble/host/services/ans/src/ble_svc_ans.c @@ -390,7 +390,7 @@ ble_svc_ans_new_alert_notify(uint8_t cat_id, const char * info_str) memcpy(&ble_svc_ans_new_alert_val[2], info_str, info_str_len); } } - return ble_gattc_notify(ble_svc_ans_conn_handle, + return ble_gatts_notify(ble_svc_ans_conn_handle, ble_svc_ans_new_alert_val_handle); } @@ -407,7 +407,7 @@ ble_svc_ans_unr_alert_notify(uint8_t cat_id) { ble_svc_ans_unr_alert_stat[0] = cat_id; ble_svc_ans_unr_alert_stat[1] = ble_svc_ans_unr_alert_cnt[cat_id]; - return ble_gattc_notify(ble_svc_ans_conn_handle, + return ble_gatts_notify(ble_svc_ans_conn_handle, ble_svc_ans_unr_alert_val_handle); } diff --git a/src/nimble/nimble/host/src/ble_att_clt.c b/src/nimble/nimble/host/src/ble_att_clt.c index a61def73..938b8a6a 100644 --- a/src/nimble/nimble/host/src/ble_att_clt.c +++ b/src/nimble/nimble/host/src/ble_att_clt.c @@ -952,7 +952,7 @@ ble_att_clt_rx_indicate(uint16_t conn_handle, struct os_mbuf **rxom) #endif /* No payload. */ - ble_gattc_rx_indicate_rsp(conn_handle); + ble_gatts_rx_indicate_rsp(conn_handle); return 0; } diff --git a/src/nimble/nimble/host/src/ble_gap.c b/src/nimble/nimble/host/src/ble_gap.c index 1d4ffb44..fffe74b3 100644 --- a/src/nimble/nimble/host/src/ble_gap.c +++ b/src/nimble/nimble/host/src/ble_gap.c @@ -1771,6 +1771,42 @@ ble_gap_rx_periodic_adv_sync_lost(const struct ble_hci_ev_le_subev_periodic_adv_ } #endif +#if MYNEWT_VAL(BLE_POWER_CONTROL) +void +ble_gap_rx_le_pathloss_threshold(const struct ble_hci_ev_le_subev_path_loss_threshold *ev) +{ + struct ble_gap_event event; + + memset(&event, 0, sizeof event); + + event.type = BLE_GAP_EVENT_PATHLOSS_THRESHOLD; + event.pathloss_threshold.conn_handle = le16toh(ev->conn_handle); + event.pathloss_threshold.current_path_loss = ev->current_path_loss; + event.pathloss_threshold.zone_entered = ev->zone_entered; + + ble_gap_event_listener_call(&event); +} + +void +ble_gap_rx_transmit_power_report(const struct ble_hci_ev_le_subev_transmit_power_report *ev) +{ + struct ble_gap_event event; + + memset(&event, 0, sizeof event); + + event.type = BLE_GAP_EVENT_TRANSMIT_POWER; + event.transmit_power.status = ev->status; + event.transmit_power.conn_handle = le16toh(ev->conn_handle); + event.transmit_power.reason = ev->reason; + event.transmit_power.phy = ev->phy; + event.transmit_power.transmit_power_level = ev->transmit_power_level; + event.transmit_power.transmit_power_level_flag = ev->transmit_power_level_flag; + event.transmit_power.delta = ev->delta; + + ble_gap_event_listener_call(&event); +} +#endif + #if MYNEWT_VAL(BLE_PERIODIC_ADV_SYNC_TRANSFER) static int periodic_adv_transfer_disable(uint16_t conn_handle) @@ -1875,6 +1911,26 @@ ble_gap_rx_periodic_adv_sync_transfer(const struct ble_hci_ev_le_subev_periodic_ } #endif +#if MYNEWT_VAL(BLE_CONN_SUBRATING) +void +ble_gap_rx_subrate_change(const struct ble_hci_ev_le_subev_subrate_change *ev) +{ + struct ble_gap_event event; + + memset(&event, 0x0, sizeof event); + + event.type = BLE_GAP_EVENT_SUBRATE_CHANGE; + event.subrate_change.status = ev->status; + event.subrate_change.conn_handle = le16toh(ev->conn_handle); + event.subrate_change.subrate_factor = le16toh(ev->subrate_factor); + event.subrate_change.periph_latency = le16toh(ev->periph_latency); + event.subrate_change.cont_num = le16toh(ev->cont_num); + event.subrate_change.supervision_tmo = le16toh(ev->supervision_tmo); + + ble_gap_event_listener_call(&event); +} +#endif + #if NIMBLE_BLE_CONNECT static int ble_gap_rd_rem_sup_feat_tx(uint16_t handle) @@ -3556,12 +3612,11 @@ ble_gap_periodic_adv_params_tx(uint8_t instance, /* Fill optional fields if application did not specify them. */ if (params->itvl_min == 0 && params->itvl_max == 0) { - /* TODO defines for those */ - cmd.min_itvl = htole16(30 / 1.25); //30 ms - cmd.max_itvl = htole16(60 / 1.25); //150 ms + cmd.min_itvl = BLE_GAP_PERIODIC_ITVL_MS(30); + cmd.max_itvl = BLE_GAP_PERIODIC_ITVL_MS(60); } else { - cmd.min_itvl = htole16( params->itvl_min); + cmd.min_itvl = htole16(params->itvl_min); cmd.max_itvl = htole16(params->itvl_max); } @@ -4894,6 +4949,46 @@ ble_gap_conn_create_tx(uint8_t own_addr_type, const ble_addr_t *peer_addr, } #endif +#if MYNEWT_VAL(BLE_CONN_SUBRATING) +int +ble_gap_set_default_subrate(uint16_t subrate_min, uint16_t subrate_max, uint16_t max_latency, + uint16_t cont_num, uint16_t supervision_tmo) +{ + struct ble_hci_le_set_default_subrate_cp cmd; + uint16_t opcode; + + cmd.subrate_min = htole16(subrate_min); + cmd.subrate_max = htole16(subrate_max); + cmd.max_latency = htole16(max_latency); + cmd.cont_num = htole16(cont_num); + cmd.supervision_tmo = htole16(supervision_tmo); + + opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_DEFAULT_SUBRATE); + + return ble_hs_hci_cmd_tx(opcode, &cmd, sizeof(cmd), NULL, 0); +} + +int +ble_gap_subrate_req(uint16_t conn_handle, uint16_t subrate_min, uint16_t subrate_max, + uint16_t max_latency, uint16_t cont_num, + uint16_t supervision_tmo) +{ + struct ble_hci_le_subrate_req_cp cmd; + uint16_t opcode; + + cmd.conn_handle = htole16(conn_handle); + cmd.subrate_min = htole16(subrate_min); + cmd.subrate_max = htole16(subrate_max); + cmd.max_latency = htole16(max_latency); + cmd.cont_num = htole16(cont_num); + cmd.supervision_tmo = htole16(supervision_tmo); + + opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SUBRATE_REQ); + + return ble_hs_hci_cmd_tx(opcode, &cmd, sizeof(cmd), NULL, 0); +} +#endif + #if MYNEWT_VAL(BLE_EXT_ADV) #if MYNEWT_VAL(BLE_ROLE_CENTRAL) static int @@ -6559,3 +6654,122 @@ ble_gap_deinit(void) { ble_npl_mutex_deinit(&preempt_done_mutex); } + +int +ble_gap_enh_read_transmit_power_level(uint16_t conn_handle, uint8_t phy, uint8_t *out_status, uint8_t *out_phy , + uint8_t *out_curr_tx_power_level, uint8_t *out_max_tx_power_level) + +{ +#if MYNEWT_VAL(BLE_POWER_CONTROL) + struct ble_hci_le_enh_read_transmit_power_level_cp cmd; + struct ble_hci_le_enh_read_transmit_power_level_rp rsp; + uint16_t opcode; + int rc; + + opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_ENH_READ_TRANSMIT_POWER_LEVEL); + + cmd.conn_handle = htole16(conn_handle); + cmd.phy = phy; + + rc = ble_hs_hci_cmd_tx(opcode, &cmd, sizeof(cmd), &rsp, sizeof(rsp)); + + if (rc!=0) { + return rc; + } + + *out_status = rsp.status; + *out_phy = rsp.phy; + *out_curr_tx_power_level = rsp.curr_tx_power_level; + *out_max_tx_power_level = rsp.max_tx_power_level; + + return 0; +#else + return BLE_HS_ENOTSUP; +#endif +} + +int +ble_gap_read_remote_transmit_power_level(uint16_t conn_handle, + uint8_t phy) +{ +#if MYNEWT_VAL(BLE_POWER_CONTROL) + struct ble_hci_le_read_remote_transmit_power_level_cp cmd; + uint16_t opcode; + + opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_READ_REMOTE_TRANSMIT_POWER_LEVEL); + + cmd.conn_handle = htole16(conn_handle); + cmd.phy = phy; + + return ble_hs_hci_cmd_tx(opcode, &cmd, sizeof(cmd), NULL, 0); +#else + return BLE_HS_ENOTSUP; +#endif +} + +int +ble_gap_set_path_loss_reporting_param(uint16_t conn_handle, + uint8_t high_threshold, + uint8_t high_hysteresis, + uint8_t low_threshold, + uint8_t low_hysteresis, + uint16_t min_time_spent) +{ +#if MYNEWT_VAL(BLE_POWER_CONTROL) + struct ble_hci_le_set_path_loss_report_param_cp cmd; + uint16_t opcode; + + opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_PATH_LOSS_REPORT_PARAM); + + cmd.conn_handle = htole16(conn_handle); + cmd.high_threshold = high_threshold; + cmd.high_hysteresis = high_hysteresis; + cmd.low_threshold = low_threshold; + cmd.low_hysteresis = low_hysteresis; + cmd.min_time_spent = min_time_spent; + + return ble_hs_hci_cmd_tx(opcode, &cmd, sizeof(cmd), NULL, 0); +#else + return BLE_HS_ENOTSUP; +#endif +} + +int +ble_gap_set_path_loss_reporting_enable(uint16_t conn_handle, + uint8_t enable) +{ +#if MYNEWT_VAL(BLE_POWER_CONTROL) + struct ble_hci_le_set_path_loss_report_enable_cp cmd; + uint16_t opcode; + + opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_PATH_LOSS_REPORT_ENABLE); + + cmd.conn_handle = htole16(conn_handle); + cmd.enable = enable; + + return ble_hs_hci_cmd_tx(opcode, &cmd, sizeof(cmd), NULL, 0); +#else + return BLE_HS_ENOTSUP; +#endif +} + +int +ble_gap_set_transmit_power_reporting_enable(uint16_t conn_handle, + uint8_t local_enable, + uint8_t remote_enable) +{ +#if MYNEWT_VAL(BLE_POWER_CONTROL) + struct ble_hci_le_set_transmit_power_report_enable_cp cmd; + uint16_t opcode; + + opcode = BLE_HCI_OP(BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_TRANS_PWR_REPORT_ENABLE); + + cmd.conn_handle = htole16(conn_handle); + cmd.local_enable = local_enable; + cmd.remote_enable = remote_enable; + + return ble_hs_hci_cmd_tx(opcode, &cmd, sizeof(cmd), NULL, 0); +#else + return BLE_HS_ENOTSUP; +#endif +} diff --git a/src/nimble/nimble/host/src/ble_gap_priv.h b/src/nimble/nimble/host/src/ble_gap_priv.h index 218501ed..61eb6dda 100644 --- a/src/nimble/nimble/host/src/ble_gap_priv.h +++ b/src/nimble/nimble/host/src/ble_gap_priv.h @@ -92,6 +92,13 @@ void ble_gap_rx_scan_req_rcvd(const struct ble_hci_ev_le_subev_scan_req_rcvd *ev #endif void ble_gap_rx_adv_report(struct ble_gap_disc_desc *desc); void ble_gap_rx_rd_rem_sup_feat_complete(const struct ble_hci_ev_le_subev_rd_rem_used_feat *ev); +#if MYNEWT_VAL(BLE_CONN_SUBRATING) +void ble_gap_rx_subrate_change(const struct ble_hci_ev_le_subev_subrate_change *ev); +#endif +#if MYNEWT_VAL(BLE_POWER_CONTROL) +void ble_gap_rx_transmit_power_report(const struct ble_hci_ev_le_subev_transmit_power_report *ev); +void ble_gap_rx_le_pathloss_threshold(const struct ble_hci_ev_le_subev_path_loss_threshold *ev); +#endif struct ble_gap_conn_complete { diff --git a/src/nimble/nimble/host/src/ble_gatt_priv.h b/src/nimble/nimble/host/src/ble_gatt_priv.h index 07616d01..4071f77c 100644 --- a/src/nimble/nimble/host/src/ble_gatt_priv.h +++ b/src/nimble/nimble/host/src/ble_gatt_priv.h @@ -125,7 +125,7 @@ void ble_gattc_rx_prep_write_rsp(uint16_t conn_handle, int status, uint16_t handle, uint16_t offset, struct os_mbuf **rxom); void ble_gattc_rx_exec_write_rsp(uint16_t conn_handle, int status); -void ble_gattc_rx_indicate_rsp(uint16_t conn_handle); +void ble_gatts_rx_indicate_rsp(uint16_t conn_handle); void ble_gattc_rx_find_info_idata(uint16_t conn_handle, struct ble_att_find_info_idata *idata); void ble_gattc_rx_find_info_complete(uint16_t conn_handle, int status); diff --git a/src/nimble/nimble/host/src/ble_gattc.c b/src/nimble/nimble/host/src/ble_gattc.c index 29c34577..c0d79c89 100644 --- a/src/nimble/nimble/host/src/ble_gattc.c +++ b/src/nimble/nimble/host/src/ble_gattc.c @@ -232,7 +232,7 @@ static ble_gattc_err_fn ble_gattc_read_mult_err; static ble_gattc_err_fn ble_gattc_write_err; static ble_gattc_err_fn ble_gattc_write_long_err; static ble_gattc_err_fn ble_gattc_write_reliable_err; -static ble_gattc_err_fn ble_gattc_indicate_err; +static ble_gattc_err_fn ble_gatts_indicate_err; static ble_gattc_err_fn * const ble_gattc_err_dispatch[BLE_GATT_OP_CNT] = { [BLE_GATT_OP_MTU] = ble_gattc_mtu_err, @@ -249,7 +249,7 @@ static ble_gattc_err_fn * const ble_gattc_err_dispatch[BLE_GATT_OP_CNT] = { [BLE_GATT_OP_WRITE] = ble_gattc_write_err, [BLE_GATT_OP_WRITE_LONG] = ble_gattc_write_long_err, [BLE_GATT_OP_WRITE_RELIABLE] = ble_gattc_write_reliable_err, - [BLE_GATT_OP_INDICATE] = ble_gattc_indicate_err, + [BLE_GATT_OP_INDICATE] = ble_gatts_indicate_err, }; /** @@ -307,7 +307,7 @@ static ble_gattc_tmo_fn ble_gattc_read_mult_tmo; static ble_gattc_tmo_fn ble_gattc_write_tmo; static ble_gattc_tmo_fn ble_gattc_write_long_tmo; static ble_gattc_tmo_fn ble_gattc_write_reliable_tmo; -static ble_gattc_tmo_fn ble_gattc_indicate_tmo; +static ble_gattc_tmo_fn ble_gatts_indicate_tmo; static ble_gattc_tmo_fn * const ble_gattc_tmo_dispatch[BLE_GATT_OP_CNT] = { @@ -325,7 +325,7 @@ ble_gattc_tmo_dispatch[BLE_GATT_OP_CNT] = { [BLE_GATT_OP_WRITE] = ble_gattc_write_tmo, [BLE_GATT_OP_WRITE_LONG] = ble_gattc_write_long_tmo, [BLE_GATT_OP_WRITE_RELIABLE] = ble_gattc_write_reliable_tmo, - [BLE_GATT_OP_INDICATE] = ble_gattc_indicate_tmo, + [BLE_GATT_OP_INDICATE] = ble_gatts_indicate_tmo, }; /** @@ -4149,7 +4149,7 @@ ble_gattc_write_reliable(uint16_t conn_handle, *****************************************************************************/ int -ble_gattc_notify_custom(uint16_t conn_handle, uint16_t chr_val_handle, +ble_gatts_notify_custom(uint16_t conn_handle, uint16_t chr_val_handle, struct os_mbuf *txom) { #if !MYNEWT_VAL(BLE_GATT_NOTIFY) @@ -4199,8 +4199,18 @@ ble_gattc_notify_custom(uint16_t conn_handle, uint16_t chr_val_handle, return rc; } +/** + * Deprecated. Should not be used. Use ble_gatts_notify_custom instead. + */ int -ble_gattc_notify(uint16_t conn_handle, uint16_t chr_val_handle) +ble_gattc_notify_custom(uint16_t conn_handle, uint16_t chr_val_handle, + struct os_mbuf *txom) +{ + return ble_gatts_notify_custom(conn_handle, chr_val_handle, txom); +} + +int +ble_gatts_notify(uint16_t conn_handle, uint16_t chr_val_handle) { #if !MYNEWT_VAL(BLE_GATT_NOTIFY) return BLE_HS_ENOTSUP; @@ -4208,11 +4218,20 @@ ble_gattc_notify(uint16_t conn_handle, uint16_t chr_val_handle) int rc; - rc = ble_gattc_notify_custom(conn_handle, chr_val_handle, NULL); + rc = ble_gatts_notify_custom(conn_handle, chr_val_handle, NULL); return rc; } +/** + * Deprecated. Should not be used. Use ble_gatts_notify instead. + */ +int +ble_gattc_notify(uint16_t conn_handle, uint16_t chr_val_handle) +{ + return ble_gatts_notify(conn_handle, chr_val_handle); +} + /***************************************************************************** * $indicate * *****************************************************************************/ @@ -4224,7 +4243,7 @@ ble_gattc_notify(uint16_t conn_handle, uint16_t chr_val_handle) * error status to the application. */ static void -ble_gattc_indicate_err(struct ble_gattc_proc *proc, int status, +ble_gatts_indicate_err(struct ble_gattc_proc *proc, int status, uint16_t att_handle) { int rc; @@ -4248,7 +4267,7 @@ ble_gattc_indicate_err(struct ble_gattc_proc *proc, int status, } static void -ble_gattc_indicate_tmo(struct ble_gattc_proc *proc) +ble_gatts_indicate_tmo(struct ble_gattc_proc *proc) { BLE_HS_DBG_ASSERT(!ble_hs_locked_by_cur_task()); ble_gattc_dbg_assert_proc_not_inserted(proc); @@ -4262,7 +4281,7 @@ ble_gattc_indicate_tmo(struct ble_gattc_proc *proc) * proc. */ static void -ble_gattc_indicate_rx_rsp(struct ble_gattc_proc *proc) +ble_gatts_indicate_rx_rsp(struct ble_gattc_proc *proc) { int rc; @@ -4293,7 +4312,7 @@ ble_gatts_indicate_fail_notconn(uint16_t conn_handle) } int -ble_gattc_indicate_custom(uint16_t conn_handle, uint16_t chr_val_handle, +ble_gatts_indicate_custom(uint16_t conn_handle, uint16_t chr_val_handle, struct os_mbuf *txom) { #if !MYNEWT_VAL(BLE_GATT_INDICATE) @@ -4365,10 +4384,29 @@ ble_gattc_indicate_custom(uint16_t conn_handle, uint16_t chr_val_handle, return rc; } +/** + * Deprecated. Should not be used. Use ble_gatts_indicate_custom instead. + */ +int +ble_gattc_indicate_custom(uint16_t conn_handle, uint16_t chr_val_handle, + struct os_mbuf *txom) +{ + return ble_gatts_indicate_custom(conn_handle, chr_val_handle, txom); +} + +int +ble_gatts_indicate(uint16_t conn_handle, uint16_t chr_val_handle) +{ + return ble_gatts_indicate_custom(conn_handle, chr_val_handle, NULL); +} + +/** + * Deprecated. Should not be used. Use ble_gatts_indicate instead. + */ int ble_gattc_indicate(uint16_t conn_handle, uint16_t chr_val_handle) { - return ble_gattc_indicate_custom(conn_handle, chr_val_handle, NULL); + return ble_gatts_indicate(conn_handle, chr_val_handle); } /***************************************************************************** @@ -4739,7 +4777,7 @@ ble_gattc_rx_exec_write_rsp(uint16_t conn_handle, int status) * active GATT procedure. */ void -ble_gattc_rx_indicate_rsp(uint16_t conn_handle) +ble_gatts_rx_indicate_rsp(uint16_t conn_handle) { #if !NIMBLE_BLE_ATT_CLT_INDICATE return; @@ -4750,7 +4788,7 @@ ble_gattc_rx_indicate_rsp(uint16_t conn_handle) proc = ble_gattc_extract_first_by_conn_op(conn_handle, BLE_GATT_OP_INDICATE); if (proc != NULL) { - ble_gattc_indicate_rx_rsp(proc); + ble_gatts_indicate_rx_rsp(proc); ble_gattc_process_status(proc, BLE_HS_EDONE); } } diff --git a/src/nimble/nimble/host/src/ble_gatts.c b/src/nimble/nimble/host/src/ble_gatts.c index c805d2ab..778839d5 100644 --- a/src/nimble/nimble/host/src/ble_gatts.c +++ b/src/nimble/nimble/host/src/ble_gatts.c @@ -1434,7 +1434,7 @@ ble_gatts_send_next_indicate(uint16_t conn_handle) return BLE_HS_ENOENT; } - rc = ble_gattc_indicate(conn_handle, chr_val_handle); + rc = ble_gatts_indicate(conn_handle, chr_val_handle); if (rc != 0) { return rc; } @@ -1672,11 +1672,11 @@ ble_gatts_tx_notifications_one_chr(uint16_t chr_val_handle) break; case BLE_ATT_OP_NOTIFY_REQ: - ble_gattc_notify(conn_handle, chr_val_handle); + ble_gatts_notify(conn_handle, chr_val_handle); break; case BLE_ATT_OP_INDICATE_REQ: - ble_gattc_indicate(conn_handle, chr_val_handle); + ble_gatts_indicate(conn_handle, chr_val_handle); break; default: @@ -1819,7 +1819,7 @@ ble_gatts_bonding_restored(uint16_t conn_handle) break; case BLE_ATT_OP_NOTIFY_REQ: - rc = ble_gattc_notify(conn_handle, cccd_value.chr_val_handle); + rc = ble_gatts_notify(conn_handle, cccd_value.chr_val_handle); if (rc == 0) { cccd_value.value_changed = 0; ble_store_write_cccd(&cccd_value); @@ -1827,7 +1827,7 @@ ble_gatts_bonding_restored(uint16_t conn_handle) break; case BLE_ATT_OP_INDICATE_REQ: - ble_gattc_indicate(conn_handle, cccd_value.chr_val_handle); + ble_gatts_indicate(conn_handle, cccd_value.chr_val_handle); break; default: diff --git a/src/nimble/nimble/host/src/ble_hs.c b/src/nimble/nimble/host/src/ble_hs.c index 2a9d45d9..3687db0e 100644 --- a/src/nimble/nimble/host/src/ble_hs.c +++ b/src/nimble/nimble/host/src/ble_hs.c @@ -605,11 +605,16 @@ ble_hs_enqueue_hci_event(uint8_t *hci_evt) struct ble_npl_event *ev; ev = os_memblock_get(&ble_hs_hci_ev_pool); - if (ev == NULL) { - ble_hci_trans_buf_free(hci_evt); - } else { +#if CONFIG_NIMBLE_STACK_USE_MEM_POOLS + if (ev && ble_hs_evq->eventq) { +#else + if (ev && ble_hs_evq->q) { +#endif ble_npl_event_init(ev, ble_hs_event_rx_hci_ev, hci_evt); ble_npl_eventq_put(ble_hs_evq, ev); + } else { + /* Either ev is NULL or queue doesn't exist */ + ble_hci_trans_buf_free(hci_evt); } } @@ -857,35 +862,36 @@ ble_hs_init(void) void ble_hs_deinit(void) { - ble_gatts_stop(); + ble_hs_flow_deinit(); - ble_npl_callout_deinit(&ble_hs_timer); +#if BLE_MONITOR + ble_monitor_deinit(); +#endif + + ble_hci_trans_cfg_hs(NULL, NULL, NULL, NULL); ble_npl_mutex_deinit(&ble_hs_mutex); + ble_mqueue_deinit(&ble_hs_rx_q); + + ble_hs_stop_deinit(); + ble_gap_deinit(); ble_hs_hci_deinit(); - ble_hs_flow_stop(); + ble_npl_event_deinit(&ble_hs_ev_start_stage2); -#if NIMBLE_BLE_CONNECT - ble_npl_event_deinit(&ble_hs_ev_tx_notifications); -#endif + ble_npl_event_deinit(&ble_hs_ev_start_stage1); ble_npl_event_deinit(&ble_hs_ev_reset); - ble_npl_event_deinit(&ble_hs_ev_start_stage1); +#if NIMBLE_BLE_CONNECT + ble_npl_event_deinit(&ble_hs_ev_tx_notifications); - ble_npl_event_deinit(&ble_hs_ev_start_stage2); + ble_gatts_stop(); +#endif - ble_mqueue_deinit(&ble_hs_rx_q); - - ble_hs_flow_deinit(); - - ble_hs_stop_deinit(); + ble_npl_callout_deinit(&ble_hs_timer); -#if BLE_MONITOR - ble_monitor_deinit(); -#endif } diff --git a/src/nimble/nimble/host/src/ble_hs_hci.c b/src/nimble/nimble/host/src/ble_hs_hci.c index 271d9258..4b1bd6d7 100644 --- a/src/nimble/nimble/host/src/ble_hs_hci.c +++ b/src/nimble/nimble/host/src/ble_hs_hci.c @@ -41,11 +41,20 @@ static uint32_t ble_hs_hci_sup_feat; static uint8_t ble_hs_hci_version; +#if CONFIG_BT_NIMBLE_LEGACY_VHCI_ENABLE #define BLE_HS_HCI_FRAG_DATABUF_SIZE \ (BLE_ACL_MAX_PKT_SIZE + \ BLE_HCI_DATA_HDR_SZ + \ sizeof (struct os_mbuf_pkthdr) + \ sizeof (struct os_mbuf)) +#else +#define BLE_HS_HCI_FRAG_DATABUF_SIZE \ + (BLE_ACL_MAX_PKT_SIZE + \ + BLE_HCI_DATA_HDR_SZ + \ + BLE_HS_CTRL_DATA_HDR_SZ + \ + sizeof (struct os_mbuf_pkthdr) + \ + sizeof (struct os_mbuf)) +#endif #define BLE_HS_HCI_FRAG_MEMBLOCK_SIZE \ (OS_ALIGN(BLE_HS_HCI_FRAG_DATABUF_SIZE, 4)) @@ -423,7 +432,11 @@ ble_hs_hci_frag_alloc(uint16_t frag_size, void *arg) /* Prefer the dedicated one-element fragment pool. */ om = os_mbuf_get_pkthdr(&ble_hs_hci_frag_mbuf_pool, 0); if (om != NULL) { +#if CONFIG_BT_NIMBLE_LEGACY_VHCI_ENABLE om->om_data += BLE_HCI_DATA_HDR_SZ; +#else + om->om_data += BLE_HCI_DATA_HDR_SZ + BLE_HS_CTRL_DATA_HDR_SZ; +#endif return om; } diff --git a/src/nimble/nimble/host/src/ble_hs_hci_cmd.c b/src/nimble/nimble/host/src/ble_hs_hci_cmd.c index f4b53763..736a5743 100644 --- a/src/nimble/nimble/host/src/ble_hs_hci_cmd.c +++ b/src/nimble/nimble/host/src/ble_hs_hci_cmd.c @@ -79,7 +79,7 @@ ble_hs_hci_cmd_send(uint16_t opcode, uint8_t len, const void *cmddata) buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_CMD); BLE_HS_DBG_ASSERT(buf != NULL); -#if !(SOC_ESP_NIMBLE_CONTROLLER) +#if !(SOC_ESP_NIMBLE_CONTROLLER) && !CONFIG_BT_CONTROLLER_DISABLED /* Hack for avoiding memcpy while handling tx pkt to VHCI, * keep one byte for type field*/ buf++; @@ -96,7 +96,7 @@ ble_hs_hci_cmd_send(uint16_t opcode, uint8_t len, const void *cmddata) BLE_HS_LOG(DEBUG, "\n"); #endif -#if !(SOC_ESP_NIMBLE_CONTROLLER) +#if !(SOC_ESP_NIMBLE_CONTROLLER) && !CONFIG_BT_CONTROLLER_DISABLED buf--; #endif diff --git a/src/nimble/nimble/host/src/ble_hs_hci_evt.c b/src/nimble/nimble/host/src/ble_hs_hci_evt.c index f8d52d58..6189200a 100644 --- a/src/nimble/nimble/host/src/ble_hs_hci_evt.c +++ b/src/nimble/nimble/host/src/ble_hs_hci_evt.c @@ -29,7 +29,12 @@ #include "ble_hs_resolv_priv.h" #if CONFIG_BT_NIMBLE_ENABLE_CONN_REATTEMPT -static uint16_t reattempt_conn[MYNEWT_VAL(BLE_MAX_CONNECTIONS)]; +struct ble_gap_reattempt_ctxt { + ble_addr_t peer_addr; + uint8_t count; +}; + +static struct ble_gap_reattempt_ctxt reattempt_conn[MYNEWT_VAL(BLE_MAX_CONNECTIONS)]; extern int ble_gap_master_connect_reattempt(uint16_t conn_handle); #ifdef CONFIG_BT_NIMBLE_MAX_CONN_REATTEMPT @@ -74,10 +79,16 @@ static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_periodic_adv_rpt; static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_periodic_adv_sync_lost; static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_scan_req_rcvd; static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_periodic_adv_sync_transfer; +#if MYNEWT_VAL(BLE_POWER_CONTROL) +static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_pathloss_threshold; +static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_transmit_power_report; +#endif +#if MYNEWT_VAL(BLE_CONN_SUBRATING) +static ble_hs_hci_evt_le_fn ble_hs_hci_evt_le_subrate_change; +#endif /* Statistics */ -struct host_hci_stats -{ +struct host_hci_stats { uint32_t events_rxd; uint32_t good_acks_rxd; uint32_t bad_acks_rxd; @@ -130,6 +141,13 @@ static ble_hs_hci_evt_le_fn * const ble_hs_hci_evt_le_dispatch[] = { [BLE_HCI_LE_SUBEV_ADV_SET_TERMINATED] = ble_hs_hci_evt_le_adv_set_terminated, [BLE_HCI_LE_SUBEV_SCAN_REQ_RCVD] = ble_hs_hci_evt_le_scan_req_rcvd, [BLE_HCI_LE_SUBEV_PERIODIC_ADV_SYNC_TRANSFER] = ble_hs_hci_evt_le_periodic_adv_sync_transfer, +#if MYNEWT_VAL(BLE_POWER_CONTROL) + [BLE_HCI_LE_SUBEV_PATH_LOSS_THRESHOLD] = ble_hs_hci_evt_le_pathloss_threshold, + [BLE_HCI_LE_SUBEV_TRANSMIT_POWER_REPORT] = ble_hs_hci_evt_le_transmit_power_report, +#endif +#if MYNEWT_VAL(BLE_CONN_SUBRATING) + [BLE_HCI_LE_SUBEV_SUBRATE_CHANGE] = ble_hs_hci_evt_le_subrate_change, +#endif }; #define BLE_HS_HCI_EVT_LE_DISPATCH_SZ \ @@ -161,6 +179,22 @@ ble_hs_hci_evt_le_dispatch_find(uint8_t event_code) return ble_hs_hci_evt_le_dispatch[event_code]; } +#if CONFIG_BT_NIMBLE_ENABLE_CONN_REATTEMPT +static int +ble_gap_find_reattempt_conn_idx(const struct ble_hs_conn *conn) +{ + int i; + + for (i = 0; i < MYNEWT_VAL(BLE_MAX_CONNECTIONS); i++) { + if (memcmp(&reattempt_conn[i].peer_addr, &conn->bhc_peer_addr, sizeof(ble_addr_t)) == 0) { + return i; + } + } + /* No matching entry found. Return invalid index */ + return MYNEWT_VAL(BLE_MAX_CONNECTIONS); +} +#endif + #if NIMBLE_BLE_CONNECT static int ble_hs_hci_evt_disconn_complete(uint8_t event_code, const void *data, @@ -181,29 +215,54 @@ ble_hs_hci_evt_disconn_complete(uint8_t event_code, const void *data, ble_hs_unlock(); #if CONFIG_BT_NIMBLE_ENABLE_CONN_REATTEMPT - int rc; + if (ev->reason == BLE_ERR_CONN_ESTABLISHMENT) { + int rc, i, idx; + + idx = ble_gap_find_reattempt_conn_idx(conn); + + if (idx == MYNEWT_VAL(BLE_MAX_CONNECTIONS)) { + /* This means, no matching addr exists in databse. So create a new one */ + for (i = 0; i < MYNEWT_VAL(BLE_MAX_CONNECTIONS); i++) { + if (reattempt_conn[i].count == 0) { + idx = i; + break; + } + } + } + + if (idx == MYNEWT_VAL(BLE_MAX_CONNECTIONS)) { + BLE_HS_LOG(DEBUG, "No space left in array "); + goto done; + } - if (ev->reason == BLE_ERR_CONN_ESTABLISHMENT && (conn != NULL)) { - BLE_HS_LOG(DEBUG, "Reattempt connection; reason = 0x%x, status = %d," - " reattempt count = %d ", ev->reason, ev->status, - reattempt_conn[ev->conn_handle]); - if (conn->bhc_flags & BLE_HS_CONN_F_MASTER) { - if (reattempt_conn[ev->conn_handle] < MAX_REATTEMPT_ALLOWED) { - reattempt_conn[ev->conn_handle] += 1; - rc = ble_gap_master_connect_reattempt(ev->conn_handle); - if (rc != 0) { - BLE_HS_LOG(DEBUG, "Master reconnect attempt failed; rc = %d", rc); + if (conn != NULL) { + BLE_HS_LOG(DEBUG, "Reattempt connection; reason = 0x%x, status = %d," + "reattempt count = %d ", ev->reason, ev->status, + reattempt_conn[idx].count); + if (conn->bhc_flags & BLE_HS_CONN_F_MASTER) { + if (reattempt_conn[idx].count < MAX_REATTEMPT_ALLOWED) { + reattempt_conn[idx].count += 1; + memcpy(&reattempt_conn[idx].peer_addr, &conn->bhc_peer_addr, BLE_DEV_ADDR_LEN); + + rc = ble_gap_master_connect_reattempt(ev->conn_handle); + if (rc != 0) { + BLE_HS_LOG(DEBUG, "Master reconnect attempt failed; rc = %d", rc); + } + } else { + memset(&reattempt_conn[idx].peer_addr, 0x0, BLE_DEV_ADDR_LEN); + reattempt_conn[idx].count = 0; } - } else { - reattempt_conn[ev->conn_handle] = 0; } + } else { + /* Disconnect completed with some other reason than + * BLE_ERR_CONN_ESTABLISHMENT, reset the corresponding reattempt count + * */ + memset(&reattempt_conn[idx].peer_addr, 0x0, BLE_DEV_ADDR_LEN); + reattempt_conn[idx].count = 0; } - } else { - /* Disconnect completed with some other reason than - * BLE_ERR_CONN_ESTABLISHMENT, reset the corresponding reattempt count - * */ - reattempt_conn[ev->conn_handle] = 0; } +done: + #endif ble_gap_rx_disconn_complete(ev); @@ -709,6 +768,36 @@ ble_hs_hci_evt_le_periodic_adv_sync_lost(uint8_t subevent, const void *data, return 0; } +#if MYNEWT_VAL(BLE_POWER_CONTROL) +static int +ble_hs_hci_evt_le_pathloss_threshold(uint8_t subevent, const void *data, + unsigned int len) +{ + const struct ble_hci_ev_le_subev_path_loss_threshold *ev = data; + + if (len != sizeof(*ev)) { + return BLE_HS_EBADDATA; + } + + ble_gap_rx_le_pathloss_threshold(ev); + return 0; +} + +static int +ble_hs_hci_evt_le_transmit_power_report(uint8_t subevent, const void *data, + unsigned int len) +{ + const struct ble_hci_ev_le_subev_transmit_power_report *ev = data; + + if (len != sizeof(*ev)) { + return BLE_HS_EBADDATA; + } + + ble_gap_rx_transmit_power_report(ev); + return 0; +} +#endif + static int ble_hs_hci_evt_le_periodic_adv_sync_transfer(uint8_t subevent, const void *data, unsigned int len) @@ -780,6 +869,23 @@ ble_hs_hci_evt_le_scan_req_rcvd(uint8_t subevent, const void *data, return 0; } +#if MYNEWT_VAL(BLE_CONN_SUBRATING) +static int +ble_hs_hci_evt_le_subrate_change(uint8_t subevent, const void *data, + unsigned int len) +{ + const struct ble_hci_ev_le_subev_subrate_change *ev = data; + + if (len != sizeof(*ev)) { + return BLE_HS_ECONTROLLER; + } + + ble_gap_rx_subrate_change(ev); + + return 0; +} +#endif + #if NIMBLE_BLE_CONNECT static int ble_hs_hci_evt_le_conn_upd_complete(uint8_t subevent, const void *data, diff --git a/src/nimble/nimble/host/src/ble_hs_mbuf.c b/src/nimble/nimble/host/src/ble_hs_mbuf.c index 680fbe38..1855e34d 100644 --- a/src/nimble/nimble/host/src/ble_hs_mbuf.c +++ b/src/nimble/nimble/host/src/ble_hs_mbuf.c @@ -66,7 +66,11 @@ ble_hs_mbuf_bare_pkt(void) struct os_mbuf * ble_hs_mbuf_acl_pkt(void) { +#if CONFIG_BT_NIMBLE_LEGACY_VHCI_ENABLE return ble_hs_mbuf_gen_pkt(BLE_HCI_DATA_HDR_SZ + 1); +#else + return ble_hs_mbuf_gen_pkt(BLE_HCI_DATA_HDR_SZ + BLE_HS_CTRL_DATA_HDR_SZ + 1); +#endif } /** @@ -81,7 +85,11 @@ ble_hs_mbuf_acl_pkt(void) struct os_mbuf * ble_hs_mbuf_l2cap_pkt(void) { +#if CONFIG_BT_NIMBLE_LEGACY_VHCI_ENABLE return ble_hs_mbuf_gen_pkt(BLE_HCI_DATA_HDR_SZ + BLE_L2CAP_HDR_SZ + 1); +#else + return ble_hs_mbuf_gen_pkt(BLE_HCI_DATA_HDR_SZ + BLE_L2CAP_HDR_SZ + BLE_HS_CTRL_DATA_HDR_SZ + 1); +#endif } struct os_mbuf * diff --git a/src/nimble/nimble/host/src/ble_hs_startup.c b/src/nimble/nimble/host/src/ble_hs_startup.c index 2ec17a13..dd1b9838 100644 --- a/src/nimble/nimble/host/src/ble_hs_startup.c +++ b/src/nimble/nimble/host/src/ble_hs_startup.c @@ -238,6 +238,26 @@ ble_hs_startup_le_set_evmask_tx(void) } #endif +#if MYNEWT_VAL(BLE_CONN_SUBRATING) + if (version >= BLE_HCI_VER_BCS_5_3) { + /** + * Enable the following LE events: + * 0x0000000400000000 LE Subrate change event + */ + mask |= 0x0000000400000000; +#endif + +#if MYNEWT_VAL(BLE_POWER_CONTROL) + if (version >= BLE_HCI_VER_BCS_5_2) { + /** + * Enable the following LE events: + * 0x0000000080000000 LE Path Loss Threshold event + * 0x0000000100000000 LE Transmit Power Reporting event + */ + mask |= 0x0000000180000000; + } +#endif + cmd.event_mask = htole64(mask); rc = ble_hs_hci_cmd_tx(BLE_HCI_OP(BLE_HCI_OGF_LE, diff --git a/src/nimble/nimble/include/nimble/hci_common.h b/src/nimble/nimble/include/nimble/hci_common.h index 69c8d78e..7a2ba51b 100644 --- a/src/nimble/nimble/include/nimble/hci_common.h +++ b/src/nimble/nimble/include/nimble/hci_common.h @@ -1061,6 +1061,67 @@ struct ble_hci_le_set_host_feat_cp { uint8_t val; } __attribute__((packed)); +#define BLE_HCI_OCF_LE_ENH_READ_TRANSMIT_POWER_LEVEL (0x0076) +struct ble_hci_le_enh_read_transmit_power_level_cp { + uint16_t conn_handle; + uint8_t phy; +} __attribute__((packed)); +struct ble_hci_le_enh_read_transmit_power_level_rp { + uint8_t status; + uint16_t conn_handle; + uint8_t phy; + uint8_t curr_tx_power_level; + uint8_t max_tx_power_level; +} __attribute__((packed)); + +#define BLE_HCI_OCF_LE_READ_REMOTE_TRANSMIT_POWER_LEVEL (0x0077) +struct ble_hci_le_read_remote_transmit_power_level_cp { + uint16_t conn_handle; + uint8_t phy; +} __attribute__((packed)); + +#define BLE_HCI_OCF_LE_SET_PATH_LOSS_REPORT_PARAM (0x0078) +struct ble_hci_le_set_path_loss_report_param_cp { + uint16_t conn_handle; + uint8_t high_threshold; + uint8_t high_hysteresis; + uint8_t low_threshold; + uint8_t low_hysteresis; + uint16_t min_time_spent; +} __attribute__((packed)); + +#define BLE_HCI_OCF_LE_SET_PATH_LOSS_REPORT_ENABLE (0x0079) +struct ble_hci_le_set_path_loss_report_enable_cp { + uint16_t conn_handle; + uint8_t enable; +} __attribute__((packed)); + +#define BLE_HCI_OCF_LE_SET_TRANS_PWR_REPORT_ENABLE (0x007A) +struct ble_hci_le_set_transmit_power_report_enable_cp { + uint16_t conn_handle; + uint8_t local_enable; + uint8_t remote_enable; +} __attribute__((packed)); + +#define BLE_HCI_OCF_LE_SET_DEFAULT_SUBRATE (0x007D) +struct ble_hci_le_set_default_subrate_cp { + uint16_t subrate_min; + uint16_t subrate_max; + uint16_t max_latency; + uint16_t cont_num; + uint16_t supervision_tmo; +} __attribute__((packed)); + +#define BLE_HCI_OCF_LE_SUBRATE_REQ (0x007E) +struct ble_hci_le_subrate_req_cp { + uint16_t conn_handle; + uint16_t subrate_min; + uint16_t subrate_max; + uint16_t max_latency; + uint16_t cont_num; + uint16_t supervision_tmo; +} __attribute__((packed)); + /* --- Vendor specific commands (OGF 0x00FF) */ #define BLE_HCI_OCF_VS_RD_STATIC_ADDR (0x0001) struct ble_hci_vs_rd_static_addr_rp { @@ -1146,6 +1207,8 @@ struct ble_hci_vs_rd_static_addr_rp { #define BLE_HCI_ADV_ITVL_DEF (0x800) /* 1.28 seconds */ #define BLE_HCI_ADV_CHANMASK_DEF (0x7) /* all channels */ +#define BLE_HCI_PERIODIC_ADV_ITVL (1250) /* usecs */ + /* Set scan parameters */ #define BLE_HCI_SCAN_TYPE_PASSIVE (0) #define BLE_HCI_SCAN_TYPE_ACTIVE (1) @@ -1772,6 +1835,26 @@ struct ble_hci_ev_le_subev_peer_sca_complete { uint8_t sca; } __attribute__((packed)); +#define BLE_HCI_LE_SUBEV_PATH_LOSS_THRESHOLD (0x20) +struct ble_hci_ev_le_subev_path_loss_threshold { + uint8_t subev_code; + uint16_t conn_handle; + uint8_t current_path_loss; + uint8_t zone_entered; +} __attribute__((packed)); + +#define BLE_HCI_LE_SUBEV_TRANSMIT_POWER_REPORT (0x21) +struct ble_hci_ev_le_subev_transmit_power_report { + uint8_t subev_code; + uint8_t status; + uint16_t conn_handle; + uint8_t reason; + uint8_t phy; + uint8_t transmit_power_level; + uint8_t transmit_power_level_flag; + uint8_t delta; +} __attribute__((packed)); + #define BLE_HCI_LE_SUBEV_BIGINFO_ADV_REPORT (0x22) struct ble_hci_ev_le_subev_biginfo_adv_report { uint8_t subev_code; @@ -1790,6 +1873,17 @@ struct ble_hci_ev_le_subev_biginfo_adv_report { uint8_t encryption; } __attribute__((packed)); +#define BLE_HCI_LE_SUBEV_SUBRATE_CHANGE (0x23) +struct ble_hci_ev_le_subev_subrate_change { + uint8_t subev_code; + uint8_t status; + uint16_t conn_handle; + uint16_t subrate_factor; + uint16_t periph_latency; + uint16_t cont_num; + uint16_t supervision_tmo; +} __attribute__((packed)); + /* Data buffer overflow event */ #define BLE_HCI_EVENT_ACL_BUF_OVERFLOW (0x01) @@ -1832,6 +1926,7 @@ struct ble_hci_ev_le_subev_biginfo_adv_report { #define BLE_HCI_VER_BCS_5_0 (9) #define BLE_HCI_VER_BCS_5_1 (10) #define BLE_HCI_VER_BCS_5_2 (11) +#define BLE_HCI_VER_BCS_5_3 (12) #define BLE_LMP_VER_BCS_1_0b (0) #define BLE_LMP_VER_BCS_1_1 (1) @@ -1845,6 +1940,7 @@ struct ble_hci_ev_le_subev_biginfo_adv_report { #define BLE_LMP_VER_BCS_5_0 (9) #define BLE_LMP_VER_BCS_5_1 (10) #define BLE_LMP_VER_BCS_5_2 (11) +#define BLE_LMP_VER_BCS_5_3 (12) /* selected HCI and LMP version */ #if MYNEWT_VAL(BLE_VERSION) == 50 diff --git a/src/nimble/porting/nimble/include/os/os_mbuf.h b/src/nimble/porting/nimble/include/os/os_mbuf.h index 77ecef00..d09f3402 100644 --- a/src/nimble/porting/nimble/include/os/os_mbuf.h +++ b/src/nimble/porting/nimble/include/os/os_mbuf.h @@ -1,3 +1,10 @@ +/* + * SPDX-FileCopyrightText: 2015-2022 The Apache Software Foundation (ASF) + * + * SPDX-License-Identifier: Apache-2.0 + * + * SPDX-FileContributor: 2019-2022 Espressif Systems (Shanghai) CO LTD + */ /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -483,7 +490,7 @@ int r_os_mbuf_appendfrom(struct os_mbuf *dst, const struct os_mbuf *src, * * @return 0 on success, -1 on failure */ -int os_mbuf_free(struct os_mbuf *mb); +int r_os_mbuf_free(struct os_mbuf *mb); #define os_mbuf_free r_os_mbuf_free diff --git a/src/nimble/porting/nimble/src/nimble_port.c b/src/nimble/porting/nimble/src/nimble_port.c index d8914f2c..b10a4d25 100644 --- a/src/nimble/porting/nimble/src/nimble_port.c +++ b/src/nimble/porting/nimble/src/nimble_port.c @@ -52,8 +52,10 @@ #ifdef ESP_PLATFORM #include "esp_intr_alloc.h" #include "esp_bt.h" +#if !SOC_ESP_NIMBLE_CONTROLLER #include "nimble/esp_port/esp-hci/include/esp_nimble_hci.h" #endif +#endif #include "freertos/FreeRTOS.h" #include "freertos/task.h" @@ -102,13 +104,13 @@ esp_err_t esp_nimble_init(void) npl_freertos_mempool_init(); #endif -#if false //need delete esp_nimble_hci_and_controller_init then can be use + +#if false // Arduino disabled if(esp_nimble_hci_init() != ESP_OK) { ESP_LOGE(NIMBLE_PORT_LOG_TAG, "hci inits failed\n"); return ESP_FAIL; } - printf("esp_nimble_hci_init\n"); -#endif +#endif // Arduino disabled /* Initialize default event queue */ ble_npl_eventq_init(&g_eventq_dflt); @@ -129,16 +131,22 @@ esp_err_t esp_nimble_init(void) */ esp_err_t esp_nimble_deinit(void) { -#if false && !SOC_ESP_NIMBLE_CONTROLLER //need delete esp_nimble_hci_and_controller_init then can be use +#if !SOC_ESP_NIMBLE_CONTROLLER +#if false // Arduino disabled if(esp_nimble_hci_deinit() != ESP_OK) { ESP_LOGE(NIMBLE_PORT_LOG_TAG, "hci deinit failed\n"); return ESP_FAIL; } -#endif -#if !(SOC_ESP_NIMBLE_CONTROLLER && CONFIG_BT_CONTROLLER_ENABLED) +#endif // Arduino disabled ble_npl_eventq_deinit(&g_eventq_dflt); #endif ble_hs_deinit(); + +#if !SOC_ESP_NIMBLE_CONTROLLER +#if CONFIG_NIMBLE_STACK_USE_MEM_POOLS + npl_freertos_funcs_deinit(); +#endif +#endif return ESP_OK; } #endif @@ -147,7 +155,12 @@ void nimble_port_init(void) { #ifdef ESP_PLATFORM -#if SOC_ESP_NIMBLE_CONTROLLER && CONFIG_BT_CONTROLLER_ENABLED +#if false // Arduino disabled + +#if CONFIG_IDF_TARGET_ESP32 + esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT); +#endif +#if CONFIG_BT_CONTROLLER_ENABLED esp_bt_controller_config_t config_opts = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); if(esp_bt_controller_init(&config_opts) != ESP_OK) { ESP_LOGE(NIMBLE_PORT_LOG_TAG, "controller init failed\n"); @@ -158,6 +171,7 @@ nimble_port_init(void) return; } #endif +#endif // Arduino disabled if(esp_nimble_init() != 0) { ESP_LOGE(NIMBLE_PORT_LOG_TAG, "nimble host init failed\n"); diff --git a/src/nimble/porting/npl/freertos/src/npl_os_freertos.c b/src/nimble/porting/npl/freertos/src/npl_os_freertos.c index a821841d..27338b26 100644 --- a/src/nimble/porting/npl/freertos/src/npl_os_freertos.c +++ b/src/nimble/porting/npl/freertos/src/npl_os_freertos.c @@ -860,7 +860,7 @@ IRAM_ATTR npl_freertos_callout_deinit(struct ble_npl_callout *co) #if CONFIG_BT_NIMBLE_USE_ESP_TIMER if(esp_timer_stop(callout->handle)) - ESP_LOGW(TAG, "Timer not stopped"); + ESP_LOGD(TAG, "Timer not stopped"); if(esp_timer_delete(callout->handle)) ESP_LOGW(TAG, "Timer not deleted"); From 6a3da5112b13d46198052c6051977d3245c55037 Mon Sep 17 00:00:00 2001 From: h2zero Date: Thu, 6 Apr 2023 09:11:34 -0600 Subject: [PATCH 2/2] Update to esp-nimble (1.4.0 branch) @0fc6282 --- .../esp_port/port/include/esp_nimble_cfg.h | 4 ++ src/nimble/nimble/controller/src/ble_ll.c | 8 +++ .../nimble/controller/src/ble_ll_conn.c | 8 +++ src/nimble/nimble/controller/src/ble_ll_hci.c | 10 +++- src/nimble/nimble/drivers/nrf51/src/ble_phy.c | 8 +++ src/nimble/nimble/drivers/nrf52/src/ble_phy.c | 8 +++ src/nimble/nimble/host/include/host/ble_gap.h | 12 ++++ src/nimble/nimble/host/mesh/src/adv.c | 8 +++ src/nimble/nimble/host/mesh/src/cfg_srv.c | 8 +++ src/nimble/nimble/host/mesh/src/glue.c | 8 +++ src/nimble/nimble/host/mesh/src/proxy.c | 8 +++ src/nimble/nimble/host/mesh/src/transport.c | 8 +++ src/nimble/nimble/host/src/ble_att.c | 9 +++ src/nimble/nimble/host/src/ble_att_clt.c | 11 +++- src/nimble/nimble/host/src/ble_att_svr.c | 5 +- src/nimble/nimble/host/src/ble_gap.c | 18 ++++++ src/nimble/nimble/host/src/ble_gattc.c | 9 +++ src/nimble/nimble/host/src/ble_hs_hci.c | 9 ++- src/nimble/nimble/host/src/ble_hs_hci_util.c | 8 +++ src/nimble/nimble/host/src/ble_hs_resolv.c | 22 ++----- src/nimble/nimble/host/src/ble_l2cap_coc.c | 8 +++ src/nimble/nimble/host/src/ble_sm.c | 9 +++ .../nimble/include/nimble/nimble_port.h | 27 +++++++-- src/nimble/porting/nimble/include/os/os.h | 8 --- .../porting/nimble/include/syscfg/syscfg.h | 4 ++ src/nimble/porting/nimble/src/nimble_port.c | 60 +++++++++++++++---- src/nimble/porting/nimble/src/os_mbuf.c | 8 +++ 27 files changed, 267 insertions(+), 46 deletions(-) diff --git a/src/nimble/esp_port/port/include/esp_nimble_cfg.h b/src/nimble/esp_port/port/include/esp_nimble_cfg.h index fb00ae11..44c6fd4a 100644 --- a/src/nimble/esp_port/port/include/esp_nimble_cfg.h +++ b/src/nimble/esp_port/port/include/esp_nimble_cfg.h @@ -451,6 +451,10 @@ #define MYNEWT_VAL_BLE_GATT_WRITE_RELIABLE (MYNEWT_VAL_BLE_ROLE_CENTRAL) #endif +#ifndef MYNEWT_VAL_BLE_GATT_BLOB_TRANSFER +#define MYNEWT_VAL_BLE_GATT_BLOB_TRANSFER (0) +#endif + #ifndef MYNEWT_VAL_BLE_HOST #define MYNEWT_VAL_BLE_HOST (1) #endif diff --git a/src/nimble/nimble/controller/src/ble_ll.c b/src/nimble/nimble/controller/src/ble_ll.c index 9e4cf41c..d20f757b 100644 --- a/src/nimble/nimble/controller/src/ble_ll.c +++ b/src/nimble/nimble/controller/src/ble_ll.c @@ -242,6 +242,14 @@ OS_TASK_STACK_DEFINE(g_ble_ll_stack, BLE_LL_STACK_SIZE); #endif /* MYNEWT */ +#ifndef min +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + /** Our global device address (public) */ uint8_t g_dev_addr[BLE_DEV_ADDR_LEN]; diff --git a/src/nimble/nimble/controller/src/ble_ll_conn.c b/src/nimble/nimble/controller/src/ble_ll_conn.c index c77e9a8b..8727ef74 100644 --- a/src/nimble/nimble/controller/src/ble_ll_conn.c +++ b/src/nimble/nimble/controller/src/ble_ll_conn.c @@ -55,6 +55,14 @@ extern void bletest_completed_pkt(uint16_t handle); #endif +#ifndef min +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + /* XXX TODO * 1) I think if we are initiating and we already have a connection with * a device that we will still try and connect to it. Fix this. diff --git a/src/nimble/nimble/controller/src/ble_ll_hci.c b/src/nimble/nimble/controller/src/ble_ll_hci.c index f3faac63..2c3dcd65 100644 --- a/src/nimble/nimble/controller/src/ble_ll_hci.c +++ b/src/nimble/nimble/controller/src/ble_ll_hci.c @@ -15,7 +15,7 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. - */ + */ #ifndef ESP_PLATFORM #include @@ -43,6 +43,14 @@ #include "ble_ll_dtm_priv.h" #endif +#ifndef min +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + static void ble_ll_hci_cmd_proc(struct ble_npl_event *ev); /* OS event to enqueue command */ diff --git a/src/nimble/nimble/drivers/nrf51/src/ble_phy.c b/src/nimble/nimble/drivers/nrf51/src/ble_phy.c index 908664a7..1c4754c4 100644 --- a/src/nimble/nimble/drivers/nrf51/src/ble_phy.c +++ b/src/nimble/nimble/drivers/nrf51/src/ble_phy.c @@ -47,6 +47,14 @@ #error LE Coded PHY cannot be enabled on nRF51 #endif +#ifndef min +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + /* XXX: 4) Make sure RF is higher priority interrupt than schedule */ /* diff --git a/src/nimble/nimble/drivers/nrf52/src/ble_phy.c b/src/nimble/nimble/drivers/nrf52/src/ble_phy.c index 08511c34..0780151d 100644 --- a/src/nimble/nimble/drivers/nrf52/src/ble_phy.c +++ b/src/nimble/nimble/drivers/nrf52/src/ble_phy.c @@ -46,6 +46,14 @@ #endif #endif +#ifndef min +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + /* * NOTE: This code uses a couple of PPI channels so care should be taken when * using PPI somewhere else. diff --git a/src/nimble/nimble/host/include/host/ble_gap.h b/src/nimble/nimble/host/include/host/ble_gap.h index e75dd73c..58225eae 100644 --- a/src/nimble/nimble/host/include/host/ble_gap.h +++ b/src/nimble/nimble/host/include/host/ble_gap.h @@ -1443,6 +1443,18 @@ int ble_gap_ext_adv_remove(uint8_t instance); * other error code on failure. */ int ble_gap_ext_adv_clear(void); + +/** + * Indicates whether an advertisement procedure is currently in progress on + * the specified Instance + * + * @param instance Instance Id + * + * @return 0 if there is no active advertising procedure for the instance, + * 1 otherwise + * + */ +int ble_gap_ext_adv_active(uint8_t instance); #endif /* Periodic Advertising */ diff --git a/src/nimble/nimble/host/mesh/src/adv.c b/src/nimble/nimble/host/mesh/src/adv.c index 95960ddb..f1f86cf5 100644 --- a/src/nimble/nimble/host/mesh/src/adv.c +++ b/src/nimble/nimble/host/mesh/src/adv.c @@ -40,6 +40,14 @@ #define ADV_INT_DEFAULT_MS 100 #define ADV_INT_FAST_MS 20 +#ifndef min +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + static int32_t adv_int_min = ADV_INT_DEFAULT_MS; /* TinyCrypt PRNG consumes a lot of stack space, so we need to have diff --git a/src/nimble/nimble/host/mesh/src/cfg_srv.c b/src/nimble/nimble/host/mesh/src/cfg_srv.c index 14ada332..fe7501fd 100644 --- a/src/nimble/nimble/host/mesh/src/cfg_srv.c +++ b/src/nimble/nimble/host/mesh/src/cfg_srv.c @@ -34,6 +34,14 @@ #include "settings.h" #include "cfg.h" +#ifndef min +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + void (*bt_mesh_app_key_cb_list[1]) (uint16_t app_idx, uint16_t net_idx, enum bt_mesh_key_evt evt); diff --git a/src/nimble/nimble/host/mesh/src/glue.c b/src/nimble/nimble/host/mesh/src/glue.c index 34d46cfd..980921f6 100644 --- a/src/nimble/nimble/host/mesh/src/glue.c +++ b/src/nimble/nimble/host/mesh/src/glue.c @@ -41,6 +41,14 @@ extern uint8_t g_mesh_addr_type; static struct ble_gap_adv_params ble_adv_cur_conf[2]; #endif +#ifndef min +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + const char * bt_hex(const void *buf, size_t len) { diff --git a/src/nimble/nimble/host/mesh/src/proxy.c b/src/nimble/nimble/host/mesh/src/proxy.c index bfd0336b..50214dfd 100644 --- a/src/nimble/nimble/host/mesh/src/proxy.c +++ b/src/nimble/nimble/host/mesh/src/proxy.c @@ -90,6 +90,14 @@ ble_uuid16_t BT_UUID_MESH_PROXY_DATA_OUT = BLE_UUID16_INIT(0x2ade); #define CLIENT_BUF_SIZE 68 +#ifndef min +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + static const struct ble_gap_adv_params slow_adv_param = { .conn_mode = (BLE_GAP_CONN_MODE_UND), .disc_mode = (BLE_GAP_DISC_MODE_GEN), diff --git a/src/nimble/nimble/host/mesh/src/transport.c b/src/nimble/nimble/host/mesh/src/transport.c index 8ec7dcfe..0b08289c 100644 --- a/src/nimble/nimble/host/mesh/src/transport.c +++ b/src/nimble/nimble/host/mesh/src/transport.c @@ -70,6 +70,14 @@ /* How long to wait for available buffers before giving up */ #define BUF_TIMEOUT K_NO_WAIT +#ifndef min +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + static struct seg_tx { struct bt_mesh_subnet *sub; void *seg[CONFIG_BT_MESH_TX_SEG_MAX]; diff --git a/src/nimble/nimble/host/src/ble_att.c b/src/nimble/nimble/host/src/ble_att.c index 8aab7f91..b8484e51 100644 --- a/src/nimble/nimble/host/src/ble_att.c +++ b/src/nimble/nimble/host/src/ble_att.c @@ -22,6 +22,15 @@ #include "ble_hs_priv.h" #if NIMBLE_BLE_CONNECT + +#ifndef min +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + static uint16_t ble_att_preferred_mtu_val; /** Dispatch table for incoming ATT requests. Sorted by op code. */ diff --git a/src/nimble/nimble/host/src/ble_att_clt.c b/src/nimble/nimble/host/src/ble_att_clt.c index 938b8a6a..43cf3843 100644 --- a/src/nimble/nimble/host/src/ble_att_clt.c +++ b/src/nimble/nimble/host/src/ble_att_clt.c @@ -707,6 +707,8 @@ ble_att_clt_tx_write_cmd(uint16_t conn_handle, uint16_t handle, struct ble_att_write_cmd *cmd; struct os_mbuf *txom2; + +#if MYNEWT_VAL(BLE_HS_DEBUG) uint8_t b; int rc; int i; @@ -720,7 +722,7 @@ ble_att_clt_tx_write_cmd(uint16_t conn_handle, uint16_t handle, assert(rc == 0); BLE_HS_LOG(DEBUG, "0x%02x", b); } - +#endif cmd = ble_att_cmd_get(BLE_ATT_OP_WRITE_CMD, sizeof(*cmd), &txom2); if (cmd == NULL) { @@ -767,10 +769,17 @@ ble_att_clt_tx_prep_write(uint16_t conn_handle, uint16_t handle, goto err; } +#if MYNEWT_VAL(BLE_GATT_BLOB_TRANSFER) + if (OS_MBUF_PKTLEN(txom) > BLE_ATT_ATTR_MAX_LEN) { + rc = BLE_HS_EINVAL; + goto err; + } +#else if (offset + OS_MBUF_PKTLEN(txom) > BLE_ATT_ATTR_MAX_LEN) { rc = BLE_HS_EINVAL; goto err; } +#endif if (OS_MBUF_PKTLEN(txom) > ble_att_mtu(conn_handle) - BLE_ATT_PREP_WRITE_CMD_BASE_SZ) { diff --git a/src/nimble/nimble/host/src/ble_att_svr.c b/src/nimble/nimble/host/src/ble_att_svr.c index fb96c72f..76388b79 100644 --- a/src/nimble/nimble/host/src/ble_att_svr.c +++ b/src/nimble/nimble/host/src/ble_att_svr.c @@ -2133,7 +2133,9 @@ ble_att_svr_prep_validate(struct ble_att_prep_entry_list *prep_list, { struct ble_att_prep_entry *entry; struct ble_att_prep_entry *prev; +#if !MYNEWT_VAL(BLE_GATT_BLOB_TRANSFER) int cur_len; +#endif prev = NULL; SLIST_FOREACH(entry, prep_list, bape_next) { @@ -2153,12 +2155,13 @@ ble_att_svr_prep_validate(struct ble_att_prep_entry_list *prep_list, } } +#if !MYNEWT_VAL(BLE_GATT_BLOB_TRANSFER) cur_len = entry->bape_offset + OS_MBUF_PKTLEN(entry->bape_value); if (cur_len > BLE_ATT_ATTR_MAX_LEN) { *err_handle = entry->bape_handle; return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; } - +#endif prev = entry; } diff --git a/src/nimble/nimble/host/src/ble_gap.c b/src/nimble/nimble/host/src/ble_gap.c index fffe74b3..a8f55e6a 100644 --- a/src/nimble/nimble/host/src/ble_gap.c +++ b/src/nimble/nimble/host/src/ble_gap.c @@ -26,6 +26,14 @@ #include "ble_hs_priv.h" #include "ble_hs_resolv_priv.h" +#ifndef min +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + #if MYNEWT #include "bsp/bsp.h" #else @@ -1445,6 +1453,16 @@ ble_gap_adv_active_instance(uint8_t instance) } #endif +#if MYNEWT_VAL(BLE_EXT_ADV) +int ble_gap_ext_adv_active(uint8_t instance) +{ + if (instance >= BLE_ADV_INSTANCES) { + return 0; + } + return ble_gap_adv_active_instance(instance); +} +#endif + /** * Clears advertisement and discovery state. This function is necessary * when the controller loses its active state (e.g. on stack reset). diff --git a/src/nimble/nimble/host/src/ble_gattc.c b/src/nimble/nimble/host/src/ble_gattc.c index c0d79c89..13bcfdc8 100644 --- a/src/nimble/nimble/host/src/ble_gattc.c +++ b/src/nimble/nimble/host/src/ble_gattc.c @@ -61,6 +61,15 @@ #include "ble_hs_priv.h" #if NIMBLE_BLE_CONNECT + +#ifndef min +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + /***************************************************************************** * $definitions / declarations * *****************************************************************************/ diff --git a/src/nimble/nimble/host/src/ble_hs_hci.c b/src/nimble/nimble/host/src/ble_hs_hci.c index 4b1bd6d7..bf9d1bef 100644 --- a/src/nimble/nimble/host/src/ble_hs_hci.c +++ b/src/nimble/nimble/host/src/ble_hs_hci.c @@ -407,6 +407,7 @@ ble_hs_hci_rx_evt(uint8_t *hci_ev, void *arg) return 0; } +#if !(SOC_ESP_NIMBLE_CONTROLLER) /** * Calculates the largest ACL payload that the controller can accept. */ @@ -420,6 +421,7 @@ ble_hs_hci_max_acl_payload_sz(void) */ return ble_hs_hci_buf_sz; } +#endif /** * Allocates an mbuf to contain an outgoing ACL data fragment. @@ -524,8 +526,11 @@ ble_hs_hci_acl_tx_now(struct ble_hs_conn *conn, struct os_mbuf **om) /* Send fragments until the entire packet has been sent. */ while (txom != NULL && ble_hs_hci_avail_pkts > 0) { - frag = mem_split_frag(&txom, ble_hs_hci_max_acl_payload_sz(), - ble_hs_hci_frag_alloc, NULL); +#if SOC_ESP_NIMBLE_CONTROLLER + frag = mem_split_frag(&txom, BLE_ACL_MAX_PKT_SIZE, ble_hs_hci_frag_alloc, NULL); +#else + frag = mem_split_frag(&txom, ble_hs_hci_max_acl_payload_sz(), ble_hs_hci_frag_alloc, NULL); +#endif if (frag == NULL) { *om = txom; return BLE_HS_EAGAIN; diff --git a/src/nimble/nimble/host/src/ble_hs_hci_util.c b/src/nimble/nimble/host/src/ble_hs_hci_util.c index d5d50585..f583c85e 100644 --- a/src/nimble/nimble/host/src/ble_hs_hci_util.c +++ b/src/nimble/nimble/host/src/ble_hs_hci_util.c @@ -22,6 +22,14 @@ #include "nimble/nimble/host/include/host/ble_hs_hci.h" #include "ble_hs_priv.h" +#ifndef min +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + uint16_t ble_hs_hci_util_handle_pb_bc_join(uint16_t handle, uint8_t pb, uint8_t bc) { diff --git a/src/nimble/nimble/host/src/ble_hs_resolv.c b/src/nimble/nimble/host/src/ble_hs_resolv.c index d70a9bdc..1c79356e 100644 --- a/src/nimble/nimble/host/src/ble_hs_resolv.c +++ b/src/nimble/nimble/host/src/ble_hs_resolv.c @@ -111,17 +111,16 @@ static void ble_rpa_peer_dev_rec_clear_all(void) { uint8_t i; + int num_peer_dev_rec = ble_store_num_peer_dev_rec; /* As NVS record need to be deleted one by one, we loop through * peer_records */ - for (i = 0; i < ble_store_num_peer_dev_rec; i++) { + for (i = 0; i < num_peer_dev_rec; i++) { ble_store_num_peer_dev_rec--; - - if ((i != ble_store_num_peer_dev_rec) && (ble_store_num_peer_dev_rec != 0)) { - memmove(&peer_dev_rec[i], &peer_dev_rec[i + 1], - (ble_store_num_peer_dev_rec - i + 1) * sizeof(struct ble_hs_dev_records )); - } - + memmove(&peer_dev_rec[0], &peer_dev_rec[1], + ble_store_num_peer_dev_rec * sizeof(struct ble_hs_dev_records)); + memset(&peer_dev_rec[ble_store_num_peer_dev_rec], 0, + sizeof(struct ble_hs_dev_records)); ble_store_persist_peer_records(); } return; @@ -230,15 +229,6 @@ ble_rpa_resolv_add_peer_rec(uint8_t *peer_addr) { struct ble_hs_dev_records *p_dev_rec = &peer_dev_rec[ble_store_num_peer_dev_rec]; - uint8_t idx = 0; - - while (p_dev_rec->rec_used) { - p_dev_rec++; - idx++; - if (idx > MYNEWT_VAL(BLE_STORE_MAX_BONDS)) { - return BLE_HS_ESTORE_CAP; - } - } p_dev_rec->rec_used = 1; memcpy(p_dev_rec->pseudo_addr, peer_addr, BLE_DEV_ADDR_LEN); diff --git a/src/nimble/nimble/host/src/ble_l2cap_coc.c b/src/nimble/nimble/host/src/ble_l2cap_coc.c index fa9e61be..83b55731 100644 --- a/src/nimble/nimble/host/src/ble_l2cap_coc.c +++ b/src/nimble/nimble/host/src/ble_l2cap_coc.c @@ -40,6 +40,14 @@ static os_membuf_t ble_l2cap_coc_srv_mem[ static struct os_mempool ble_l2cap_coc_srv_pool; +#ifndef min +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + static void ble_l2cap_coc_dbg_assert_srv_not_inserted(struct ble_l2cap_coc_srv *srv) { diff --git a/src/nimble/nimble/host/src/ble_sm.c b/src/nimble/nimble/host/src/ble_sm.c index 5a7ffbb4..ca16faac 100644 --- a/src/nimble/nimble/host/src/ble_sm.c +++ b/src/nimble/nimble/host/src/ble_sm.c @@ -51,6 +51,15 @@ #include "../store/config/src/ble_store_config_priv.h" #if NIMBLE_BLE_CONNECT + +#ifndef min +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + #if NIMBLE_BLE_SM /** Procedure timeout; 30 seconds. */ diff --git a/src/nimble/porting/nimble/include/nimble/nimble_port.h b/src/nimble/porting/nimble/include/nimble/nimble_port.h index 677b43d0..27360b25 100644 --- a/src/nimble/porting/nimble/include/nimble/nimble_port.h +++ b/src/nimble/porting/nimble/include/nimble/nimble_port.h @@ -40,13 +40,24 @@ extern "C" { #endif -void nimble_port_init(void); -void nimble_port_deinit(void); +#ifdef ESP_PLATFORM +/** +* @brief nimble_port_init - Initialize controller and NimBLE host stack +* +* @return esp_err_t - ESP_OK ( if success) +* Error code in case of failure +*/ +esp_err_t nimble_port_init(void); + +/** +* @brief nimble_port_deinit - Deinitialize controller and NimBLE host stack +* +* @return esp_err_t - ESP_OK ( if success) +* Error code in case of failure +*/ +esp_err_t nimble_port_deinit(void); -void nimble_port_run(void); -int nimble_port_stop(void); -#ifdef ESP_PLATFORM /** * @brief esp_nimble_init - Initialize the NimBLE host stack * @@ -60,8 +71,14 @@ esp_err_t esp_nimble_init(void); * @return esp_err_t */ esp_err_t esp_nimble_deinit(void); +#else +void nimble_port_init(void); +void nimble_port_deinit(void); #endif // ESP_PLATFORM +void nimble_port_run(void); +int nimble_port_stop(void); + struct ble_npl_eventq *nimble_port_get_dflt_eventq(void); #if NIMBLE_CFG_CONTROLLER diff --git a/src/nimble/porting/nimble/include/os/os.h b/src/nimble/porting/nimble/include/os/os.h index bc456b2a..c07f3cfc 100644 --- a/src/nimble/porting/nimble/include/os/os.h +++ b/src/nimble/porting/nimble/include/os/os.h @@ -30,14 +30,6 @@ extern "C" { #define static_assert _Static_assert #endif -#ifndef min -#define min(a, b) ((a)<(b)?(a):(b)) -#endif - -#ifndef max -#define max(a, b) ((a)>(b)?(a):(b)) -#endif - #include "../syscfg/syscfg.h" #include "nimble/nimble/include/nimble/nimble_npl.h" diff --git a/src/nimble/porting/nimble/include/syscfg/syscfg.h b/src/nimble/porting/nimble/include/syscfg/syscfg.h index e9721312..91ac58d5 100644 --- a/src/nimble/porting/nimble/include/syscfg/syscfg.h +++ b/src/nimble/porting/nimble/include/syscfg/syscfg.h @@ -745,6 +745,10 @@ #define MYNEWT_VAL_BLE_GATT_WRITE_RELIABLE (MYNEWT_VAL_BLE_ROLE_CENTRAL) #endif +#ifndef MYNEWT_VAL_BLE_GATT_BLOB_TRANSFER +#define MYNEWT_VAL_BLE_GATT_BLOB_TRANSFER (0) +#endif + #ifndef MYNEWT_VAL_BLE_HOST #define MYNEWT_VAL_BLE_HOST (1) #endif diff --git a/src/nimble/porting/nimble/src/nimble_port.c b/src/nimble/porting/nimble/src/nimble_port.c index b10a4d25..14ba0ae8 100644 --- a/src/nimble/porting/nimble/src/nimble_port.c +++ b/src/nimble/porting/nimble/src/nimble_port.c @@ -151,10 +151,20 @@ esp_err_t esp_nimble_deinit(void) } #endif +#ifdef ESP_PLATFORM +/** + * @brief nimble_port_init - Initialize controller and NimBLE host stack + * + * @return esp_err_t + */ +esp_err_t +#else void +#endif nimble_port_init(void) { #ifdef ESP_PLATFORM + esp_err_t ret; #if false // Arduino disabled #if CONFIG_IDF_TARGET_ESP32 @@ -162,21 +172,28 @@ nimble_port_init(void) #endif #if CONFIG_BT_CONTROLLER_ENABLED esp_bt_controller_config_t config_opts = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); - if(esp_bt_controller_init(&config_opts) != ESP_OK) { + + ret = esp_bt_controller_init(&config_opts); + if (ret != ESP_OK) { ESP_LOGE(NIMBLE_PORT_LOG_TAG, "controller init failed\n"); - return; + return ret; } - if(esp_bt_controller_enable(ESP_BT_MODE_BLE) != ESP_OK) { + + ret = esp_bt_controller_enable(ESP_BT_MODE_BLE); + if (ret != ESP_OK) { ESP_LOGE(NIMBLE_PORT_LOG_TAG, "controller enable failed\n"); - return; + return ret; } #endif #endif // Arduino disabled - if(esp_nimble_init() != 0) { + ret = esp_nimble_init(); + if (ret != ESP_OK) { ESP_LOGE(NIMBLE_PORT_LOG_TAG, "nimble host init failed\n"); - return; + return ret; } + + return ESP_OK; #else #if CONFIG_NIMBLE_STACK_USE_MEM_POOLS /* Initialize the function pointers for OS porting */ @@ -201,24 +218,43 @@ nimble_port_init(void) #endif // ESP_PLATFORM } +#ifdef ESP_PLATFORM +/** + * @brief nimble_port_deinit - Deinitialize controller and NimBLE host stack + * + * @return esp_err_t + */ + +esp_err_t +#else void +#endif nimble_port_deinit(void) { #ifdef ESP_PLATFORM - if(esp_nimble_deinit() != 0) { + esp_err_t ret; + + ret = esp_nimble_deinit(); + if(ret != ESP_OK) { ESP_LOGE(NIMBLE_PORT_LOG_TAG, "nimble host deinit failed\n"); - return; + return ret; } + #if CONFIG_BT_CONTROLLER_ENABLED - if(esp_bt_controller_disable() != ESP_OK) { + ret = esp_bt_controller_disable(); + if(ret != ESP_OK) { ESP_LOGE(NIMBLE_PORT_LOG_TAG, "controller disable failed\n"); - return; + return ret; } - if(esp_bt_controller_deinit() != ESP_OK) { + + ret = esp_bt_controller_deinit(); + if(ret != ESP_OK) { ESP_LOGE(NIMBLE_PORT_LOG_TAG, "controller deinit failed\n"); - return; + return ret; } #endif + + return ESP_OK; #else ble_npl_eventq_deinit(&g_eventq_dflt); ble_hs_deinit(); diff --git a/src/nimble/porting/nimble/src/os_mbuf.c b/src/nimble/porting/nimble/src/os_mbuf.c index a6bf0414..a3236e43 100644 --- a/src/nimble/porting/nimble/src/os_mbuf.c +++ b/src/nimble/porting/nimble/src/os_mbuf.c @@ -41,6 +41,14 @@ #include #include +#ifndef min +#define min(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a, b) ((a) > (b) ? (a) : (b)) +#endif + /** * @addtogroup OSKernel * @{