diff options
Diffstat (limited to 'src/libs/mynewt-nimble/nimble/host/src/ble_gap.c')
| -rw-r--r-- | src/libs/mynewt-nimble/nimble/host/src/ble_gap.c | 128 |
1 files changed, 109 insertions, 19 deletions
diff --git a/src/libs/mynewt-nimble/nimble/host/src/ble_gap.c b/src/libs/mynewt-nimble/nimble/host/src/ble_gap.c index e32482e..bbedc31 100644 --- a/src/libs/mynewt-nimble/nimble/host/src/ble_gap.c +++ b/src/libs/mynewt-nimble/nimble/host/src/ble_gap.c @@ -310,14 +310,12 @@ ble_gap_log_conn(uint8_t own_addr_type, const ble_addr_t *peer_addr, BLE_HS_LOG_ADDR(INFO, peer_addr->val); } - /* // NRF LOG support max 6 params in log BLE_HS_LOG(INFO, " scan_itvl=%d scan_window=%d itvl_min=%d itvl_max=%d " "latency=%d supervision_timeout=%d min_ce_len=%d " "max_ce_len=%d own_addr_type=%d", params->scan_itvl, params->scan_window, params->itvl_min, params->itvl_max, params->latency, params->supervision_timeout, params->min_ce_len, params->max_ce_len, own_addr_type); - */ } #endif @@ -341,10 +339,10 @@ ble_gap_log_update(uint16_t conn_handle, { BLE_HS_LOG(INFO, "connection parameter update; " "conn_handle=%d itvl_min=%d itvl_max=%d latency=%d " - "supervision_timeout=%d min_ce_len=%d max_ce_len=???", + "supervision_timeout=%d min_ce_len=%d max_ce_len=%d", conn_handle, params->itvl_min, params->itvl_max, params->latency, params->supervision_timeout, - params->min_ce_len); + params->min_ce_len, params->max_ce_len); } #endif @@ -747,7 +745,7 @@ ble_gap_has_client(struct ble_gap_master_state *out_state) return 0; } - return out_state->cb; + return out_state->cb != NULL; } static void @@ -932,7 +930,7 @@ ble_gap_master_ticks_until_exp(void) return 0; } -#if !MYNEWT_VAL(BLE_EXT_ADV) +#if NIMBLE_BLE_ADVERTISE && !MYNEWT_VAL(BLE_EXT_ADV) static uint32_t ble_gap_slave_ticks_until_exp(void) { @@ -1000,7 +998,7 @@ ble_gap_update_next_exp(int32_t *out_ticks_from_now) } -#if MYNEWT_VAL(BLE_ROLE_CENTRAL) +#if NIMBLE_BLE_SCAN static void ble_gap_master_set_timer(uint32_t ticks_from_now) { @@ -1022,7 +1020,7 @@ ble_gap_slave_set_timer(uint32_t ticks_from_now) } #endif -#if NIMBLE_BLE_CONNECT +#if (NIMBLE_BLE_CONNECT || NIMBLE_BLE_SCAN) /** * Called when an error is encountered while the master-connection-fsm is * active. @@ -1036,12 +1034,22 @@ ble_gap_master_failed(int status) ble_gap_master_connect_failure(status); break; +#if NIMBLE_BLE_SCAN + case BLE_GAP_OP_M_DISC: + STATS_INC(ble_gap_stats, initiate_fail); + ble_gap_disc_complete(); + ble_gap_master_reset_state(); + break; +#endif + default: BLE_HS_DBG_ASSERT(0); break; } } +#endif +#if NIMBLE_BLE_CONNECT static void ble_gap_update_failed(uint16_t conn_handle, int status) { @@ -1239,6 +1247,46 @@ ble_gap_adv_active_instance(uint8_t instance) return ble_gap_slave[instance].op == BLE_GAP_OP_S_ADV; } +/** + * Clears advertisement and discovery state. This function is necessary + * when the controller loses its active state (e.g. on stack reset). + */ +void +ble_gap_reset_state(int reason) +{ + uint16_t conn_handle; + + while (1) { + conn_handle = ble_hs_atomic_first_conn_handle(); + if (conn_handle == BLE_HS_CONN_HANDLE_NONE) { + break; + } + + ble_gap_conn_broken(conn_handle, reason); + } + +#if NIMBLE_BLE_ADVERTISE +#if MYNEWT_VAL(BLE_EXT_ADV) + uint8_t i; + for (i = 0; i < BLE_ADV_INSTANCES; i++) { + if (ble_gap_adv_active_instance(i)) { + /* Indicate to application that advertising has stopped. */ + ble_gap_adv_finished(i, reason, 0, 0); + } + } +#else + if (ble_gap_adv_active_instance(0)) { + /* Indicate to application that advertising has stopped. */ + ble_gap_adv_finished(0, reason, 0, 0); + } +#endif +#endif + +#if (NIMBLE_BLE_SCAN || NIMBLE_BLE_CONNECT) + ble_gap_master_failed(reason); +#endif +} + #if NIMBLE_BLE_CONNECT static int ble_gap_accept_master_conn(void) @@ -1893,7 +1941,7 @@ ble_gap_master_timer(void) return BLE_HS_FOREVER; } -#if !MYNEWT_VAL(BLE_EXT_ADV) +#if NIMBLE_BLE_ADVERTISE && !MYNEWT_VAL(BLE_EXT_ADV) static int32_t ble_gap_slave_timer(void) { @@ -1945,7 +1993,6 @@ ble_gap_update_timer(void) ble_hs_unlock(); if (entry != NULL) { - ble_gap_update_notify(conn_handle, BLE_HS_ETIMEOUT); ble_gap_update_entry_free(entry); } } while (entry != NULL); @@ -1993,7 +2040,7 @@ ble_gap_timer(void) min_ticks = min(master_ticks, update_ticks); -#if !MYNEWT_VAL(BLE_EXT_ADV) +#if NIMBLE_BLE_ADVERTISE && !MYNEWT_VAL(BLE_EXT_ADV) min_ticks = min(min_ticks, ble_gap_slave_timer()); #endif @@ -5547,7 +5594,7 @@ ble_gap_unpair_oldest_peer(void) } if (num_peers == 0) { - return 0; + return BLE_HS_ENOENT; } rc = ble_gap_unpair(&oldest_peer_id_addr); @@ -5558,6 +5605,36 @@ ble_gap_unpair_oldest_peer(void) return 0; } +int +ble_gap_unpair_oldest_except(const ble_addr_t *peer_addr) +{ + ble_addr_t peer_id_addrs[MYNEWT_VAL(BLE_STORE_MAX_BONDS)]; + int num_peers; + int rc, i; + + rc = ble_store_util_bonded_peers( + &peer_id_addrs[0], &num_peers, MYNEWT_VAL(BLE_STORE_MAX_BONDS)); + if (rc != 0) { + return rc; + } + + if (num_peers == 0) { + return BLE_HS_ENOENT; + } + + for (i = 0; i < num_peers; i++) { + if (ble_addr_cmp(peer_addr, &peer_id_addrs[i]) != 0) { + break; + } + } + + if (i >= num_peers) { + return BLE_HS_ENOMEM; + } + + return ble_gap_unpair(&peer_id_addrs[i]); +} + void ble_gap_passkey_event(uint16_t conn_handle, struct ble_gap_passkey_params *passkey_params) @@ -5577,7 +5654,8 @@ ble_gap_passkey_event(uint16_t conn_handle, } void -ble_gap_enc_event(uint16_t conn_handle, int status, int security_restored) +ble_gap_enc_event(uint16_t conn_handle, int status, + int security_restored, int bonded) { #if NIMBLE_BLE_SM struct ble_gap_event event; @@ -5590,12 +5668,24 @@ ble_gap_enc_event(uint16_t conn_handle, int status, int security_restored) ble_gap_event_listener_call(&event); ble_gap_call_conn_event_cb(&event, conn_handle); - if (status == 0) { - if (security_restored) { - ble_gatts_bonding_restored(conn_handle); - } else { - ble_gatts_bonding_established(conn_handle); - } + if (status != 0) { + return; + } + + /* If encryption succeded and encryption has been restored for bonded device, + * notify gatt server so it has chance to send notification/indication if needed. + */ + if (security_restored) { + ble_gatts_bonding_restored(conn_handle); + return; + } + + /* If this is fresh pairing and bonding has been established, + * notify gatt server about that so previous subscriptions (before bonding) + * can be stored. + */ + if (bonded) { + ble_gatts_bonding_established(conn_handle); } #endif } |
