From 9898fed80634c99c9dbe8ee8682dfdc259463654 Mon Sep 17 00:00:00 2001 From: JF Date: Tue, 16 Jun 2020 20:36:24 +0200 Subject: Update to nimble 1.3.0 + WIP : debug BLE connection issue with android phones. --- .../nimble/host/include/host/ble_gap.h | 14 ---- .../host/pts/tpg/94654-20170317-085122560.tpg | 4 +- .../host/pts/tpg/94654-20170317-085441153.pts | 2 +- src/libs/mynewt-nimble/nimble/host/src/ble_gap.c | 85 +--------------------- .../mynewt-nimble/nimble/host/src/ble_gap_priv.h | 1 - src/libs/mynewt-nimble/nimble/host/src/ble_hs.c | 11 ++- .../mynewt-nimble/nimble/host/src/ble_hs_conn.c | 9 --- .../mynewt-nimble/nimble/host/src/ble_l2cap_coc.c | 6 +- .../mynewt-nimble/nimble/host/src/ble_monitor.c | 25 +++++-- .../mynewt-nimble/nimble/host/src/ble_sm_alg.c | 2 + .../mynewt-nimble/nimble/host/src/ble_store_util.c | 16 ++-- .../nimble/host/test/src/ble_gap_test.c | 67 +++++++++++++++++ 12 files changed, 112 insertions(+), 130 deletions(-) (limited to 'src/libs/mynewt-nimble/nimble/host') diff --git a/src/libs/mynewt-nimble/nimble/host/include/host/ble_gap.h b/src/libs/mynewt-nimble/nimble/host/include/host/ble_gap.h index b58f350f..20e7dab7 100644 --- a/src/libs/mynewt-nimble/nimble/host/include/host/ble_gap.h +++ b/src/libs/mynewt-nimble/nimble/host/include/host/ble_gap.h @@ -1896,20 +1896,6 @@ int ble_gap_unpair(const ble_addr_t *peer_addr); */ int ble_gap_unpair_oldest_peer(void); -/** - * Similar to `ble_gap_unpair_oldest_peer()`, except it makes sure that the - * peer received in input parameters is not deleted. - * - * @param peer_addr Address of the peer (not to be deleted) - * - * @return 0 on success; - * A BLE host HCI return code if the controller - * rejected the request; - * A BLE host core return code on unexpected - * error. - */ -int ble_gap_unpair_oldest_except(const ble_addr_t *peer_addr); - #define BLE_GAP_PRIVATE_MODE_NETWORK 0 #define BLE_GAP_PRIVATE_MODE_DEVICE 1 diff --git a/src/libs/mynewt-nimble/nimble/host/pts/tpg/94654-20170317-085122560.tpg b/src/libs/mynewt-nimble/nimble/host/pts/tpg/94654-20170317-085122560.tpg index 3cc985fa..f5bb0ce9 100644 --- a/src/libs/mynewt-nimble/nimble/host/pts/tpg/94654-20170317-085122560.tpg +++ b/src/libs/mynewt-nimble/nimble/host/pts/tpg/94654-20170317-085122560.tpg @@ -1,4 +1,4 @@ - + M'&/JP\$+#X83?)"[ M2E=\N*7*U 5)JSKT(5#=>@^/#]IP*[GTZ0%0]$F(/$ MEK[V7>349??;!JZJNFZ CK=QGD^FOYV^FU>"DK<@0U*##HV\H9&_%["5IK@1 MA9F,FJO2P^3:4_]04%-0T%9=^XCMWKF"L$[)%)G>_I 0Q?@:H)&1HX)7VY&. @@ -1023,4 +1023,4 @@ M<74&Z:0BS4[#$;M2_1""EZVO@$B^"0'V'W:7^@-]G?*\B>0M(- M6MF1FM+ ?,?3[N*1EI"$AXF#@6P-FU/4\QF?O!&%CE5*6J2'@QDYV?_04-O) M7Z 6N;C7E_4 + diff --git a/src/libs/mynewt-nimble/nimble/host/pts/tpg/94654-20170317-085441153.pts b/src/libs/mynewt-nimble/nimble/host/pts/tpg/94654-20170317-085441153.pts index 6760d5ec..92507b13 100644 --- a/src/libs/mynewt-nimble/nimble/host/pts/tpg/94654-20170317-085441153.pts +++ b/src/libs/mynewt-nimble/nimble/host/pts/tpg/94654-20170317-085441153.pts @@ -286,4 +286,4 @@ 7
3
- + 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 7d1c5252..be4a1cb3 100644 --- a/src/libs/mynewt-nimble/nimble/host/src/ble_gap.c +++ b/src/libs/mynewt-nimble/nimble/host/src/ble_gap.c @@ -1020,7 +1020,7 @@ ble_gap_slave_set_timer(uint32_t ticks_from_now) } #endif -#if (NIMBLE_BLE_CONNECT || NIMBLE_BLE_SCAN) +#if NIMBLE_BLE_CONNECT /** * Called when an error is encountered while the master-connection-fsm is * active. @@ -1034,22 +1034,12 @@ 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) { @@ -1247,46 +1237,6 @@ 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) @@ -1993,6 +1943,7 @@ 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); @@ -5594,7 +5545,7 @@ ble_gap_unpair_oldest_peer(void) } if (num_peers == 0) { - return BLE_HS_ENOENT; + return 0; } rc = ble_gap_unpair(&oldest_peer_id_addr); @@ -5605,36 +5556,6 @@ 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) diff --git a/src/libs/mynewt-nimble/nimble/host/src/ble_gap_priv.h b/src/libs/mynewt-nimble/nimble/host/src/ble_gap_priv.h index c050435f..ce443198 100644 --- a/src/libs/mynewt-nimble/nimble/host/src/ble_gap_priv.h +++ b/src/libs/mynewt-nimble/nimble/host/src/ble_gap_priv.h @@ -136,7 +136,6 @@ void ble_gap_preempt(void); void ble_gap_preempt_done(void); int ble_gap_terminate_with_conn(struct ble_hs_conn *conn, uint8_t hci_reason); -void ble_gap_reset_state(int reason); void ble_gap_conn_broken(uint16_t conn_handle, int reason); int32_t ble_gap_timer(void); diff --git a/src/libs/mynewt-nimble/nimble/host/src/ble_hs.c b/src/libs/mynewt-nimble/nimble/host/src/ble_hs.c index b41064fe..23ac1d14 100644 --- a/src/libs/mynewt-nimble/nimble/host/src/ble_hs.c +++ b/src/libs/mynewt-nimble/nimble/host/src/ble_hs.c @@ -361,6 +361,7 @@ ble_hs_sync(void) static int ble_hs_reset(void) { + uint16_t conn_handle; int rc; STATS_INC(ble_hs_stats, reset); @@ -375,8 +376,14 @@ ble_hs_reset(void) ble_hs_clear_rx_queue(); - /* Clear adverising and scanning states. */ - ble_gap_reset_state(ble_hs_reset_reason); + 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, ble_hs_reset_reason); + } /* Clear configured addresses. */ ble_hs_id_reset(); diff --git a/src/libs/mynewt-nimble/nimble/host/src/ble_hs_conn.c b/src/libs/mynewt-nimble/nimble/host/src/ble_hs_conn.c index 70695fa6..f7edb624 100644 --- a/src/libs/mynewt-nimble/nimble/host/src/ble_hs_conn.c +++ b/src/libs/mynewt-nimble/nimble/host/src/ble_hs_conn.c @@ -327,7 +327,6 @@ ble_hs_conn_find_by_addr(const ble_addr_t *addr) #endif struct ble_hs_conn *conn; - struct ble_hs_conn_addrs addrs; BLE_HS_DBG_ASSERT(ble_hs_locked_by_cur_task()); @@ -344,14 +343,6 @@ ble_hs_conn_find_by_addr(const ble_addr_t *addr) if (ble_addr_cmp(&conn->bhc_peer_addr, addr) == 0) { return conn; } - if (conn->bhc_peer_addr.type < BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT) { - continue; - } - /*If type 0x02 or 0x03 is used, let's double check if address is good */ - ble_hs_conn_addrs(conn, &addrs); - if (ble_addr_cmp(&addrs.peer_id_addr, addr) == 0) { - return conn; - } } } diff --git a/src/libs/mynewt-nimble/nimble/host/src/ble_l2cap_coc.c b/src/libs/mynewt-nimble/nimble/host/src/ble_l2cap_coc.c index b15646f6..41a83156 100644 --- a/src/libs/mynewt-nimble/nimble/host/src/ble_l2cap_coc.c +++ b/src/libs/mynewt-nimble/nimble/host/src/ble_l2cap_coc.c @@ -73,7 +73,7 @@ ble_l2cap_coc_create_server(uint16_t psm, uint16_t mtu, srv = ble_l2cap_coc_srv_alloc(); if (!srv) { - return BLE_HS_ENOMEM; + return BLE_HS_ENOMEM; } srv->psm = psm; @@ -440,7 +440,6 @@ ble_l2cap_coc_continue_tx(struct ble_l2cap_chan *chan) BLE_HS_LOG(DEBUG, "Sending SDU len=%d\n", OS_MBUF_PKTLEN(tx->sdu)); rc = os_mbuf_append(txom, &l, sizeof(uint16_t)); if (rc) { - rc = BLE_HS_ENOMEM; BLE_HS_LOG(DEBUG, "Could not append data rc=%d", rc); goto failed; } @@ -453,9 +452,8 @@ ble_l2cap_coc_continue_tx(struct ble_l2cap_chan *chan) rc = os_mbuf_appendfrom(txom, tx->sdu, tx->data_offset, len - sdu_size_offset); if (rc) { - rc = BLE_HS_ENOMEM; BLE_HS_LOG(DEBUG, "Could not append data rc=%d", rc); - goto failed; + goto failed; } ble_hs_lock(); diff --git a/src/libs/mynewt-nimble/nimble/host/src/ble_monitor.c b/src/libs/mynewt-nimble/nimble/host/src/ble_monitor.c index e6db48b8..6057e7e2 100644 --- a/src/libs/mynewt-nimble/nimble/host/src/ble_monitor.c +++ b/src/libs/mynewt-nimble/nimble/host/src/ble_monitor.c @@ -17,6 +17,7 @@ * under the License. */ +#include #include "host/ble_monitor.h" #if BLE_MONITOR @@ -28,13 +29,14 @@ #include #include #include +#include #include "os/os.h" #include "log/log.h" #if MYNEWT_VAL(BLE_MONITOR_UART) #include "uart/uart.h" #endif #if MYNEWT_VAL(BLE_MONITOR_RTT) -#include "rtt/SEGGER_RTT.h" +#include "SEGGER_RTT.h" #endif #include "ble_hs_priv.h" #include "ble_monitor_priv.h" @@ -236,7 +238,7 @@ monitor_write_header(uint16_t opcode, uint16_t len) hdr.flags = 0; /* Use uptime for timestamp */ - ts = os_get_uptime_usec(); + ts = os_cputime_ticks_to_usecs(os_cputime_get32()); /* * btsnoop specification states that fields of extended header must be @@ -266,6 +268,17 @@ btmon_write(FILE *instance, const char *bp, size_t n) return n; } +struct File_methods +{ + size_t (*write)(FILE* instance, const char *bp, size_t n); + size_t (*read)(FILE* instance, char *bp, size_t n); +}; + +struct File +{ + const struct File_methods *vmt; +}; + static FILE *btmon = (FILE *) &(struct File) { .vmt = &(struct File_methods) { .write = btmon_write, @@ -276,7 +289,7 @@ static FILE *btmon = (FILE *) &(struct File) { static void drops_tmp_cb(struct ble_npl_event *ev) { - ble_npl_mutex_pend(&lock, OS_TIMEOUT_NEVER); + ble_npl_mutex_pend(&lock, portMAX_DELAY); /* * There's no "nop" in btsnoop protocol so we just send empty system note @@ -345,7 +358,7 @@ ble_monitor_init(void) int ble_monitor_send(uint16_t opcode, const void *data, size_t len) { - ble_npl_mutex_pend(&lock, OS_TIMEOUT_NEVER); + ble_npl_mutex_pend(&lock, portMAX_DELAY); monitor_write_header(opcode, len); monitor_write(data, len); @@ -367,7 +380,7 @@ ble_monitor_send_om(uint16_t opcode, const struct os_mbuf *om) om_tmp = SLIST_NEXT(om_tmp, om_next); } - ble_npl_mutex_pend(&lock, OS_TIMEOUT_NEVER); + ble_npl_mutex_pend(&lock, portMAX_DELAY); monitor_write_header(opcode, length); @@ -429,7 +442,7 @@ ble_monitor_log(int level, const char *fmt, ...) ulog.ident_len = sizeof(id); - ble_npl_mutex_pend(&lock, OS_TIMEOUT_NEVER); + ble_npl_mutex_pend(&lock, portMAX_DELAY); monitor_write_header(BLE_MONITOR_OPCODE_USER_LOGGING, sizeof(ulog) + sizeof(id) + len + 1); diff --git a/src/libs/mynewt-nimble/nimble/host/src/ble_sm_alg.c b/src/libs/mynewt-nimble/nimble/host/src/ble_sm_alg.c index 148995c8..50fd77ec 100644 --- a/src/libs/mynewt-nimble/nimble/host/src/ble_sm_alg.c +++ b/src/libs/mynewt-nimble/nimble/host/src/ble_sm_alg.c @@ -68,6 +68,8 @@ ble_sm_alg_encrypt(const uint8_t *key, const uint8_t *plaintext, swap_buf(tmp, plaintext, 16); + + if (tc_aes_encrypt(enc_data, tmp, &s) == TC_CRYPTO_FAIL) { return BLE_HS_EUNKNOWN; } diff --git a/src/libs/mynewt-nimble/nimble/host/src/ble_store_util.c b/src/libs/mynewt-nimble/nimble/host/src/ble_store_util.c index 7de48272..444cc55d 100644 --- a/src/libs/mynewt-nimble/nimble/host/src/ble_store_util.c +++ b/src/libs/mynewt-nimble/nimble/host/src/ble_store_util.c @@ -233,15 +233,13 @@ ble_store_util_status_rr(struct ble_store_status_event *event, void *arg) switch (event->event_code) { case BLE_STORE_EVENT_OVERFLOW: switch (event->overflow.obj_type) { - case BLE_STORE_OBJ_TYPE_OUR_SEC: - case BLE_STORE_OBJ_TYPE_PEER_SEC: - return ble_gap_unpair_oldest_peer(); - case BLE_STORE_OBJ_TYPE_CCCD: - /* Try unpairing oldest peer except current peer */ - return ble_gap_unpair_oldest_except(&event->overflow.value->cccd.peer_addr); - - default: - return BLE_HS_EUNKNOWN; + case BLE_STORE_OBJ_TYPE_OUR_SEC: + case BLE_STORE_OBJ_TYPE_PEER_SEC: + case BLE_STORE_OBJ_TYPE_CCCD: + return ble_gap_unpair_oldest_peer(); + + default: + return BLE_HS_EUNKNOWN; } case BLE_STORE_EVENT_FULL: diff --git a/src/libs/mynewt-nimble/nimble/host/test/src/ble_gap_test.c b/src/libs/mynewt-nimble/nimble/host/test/src/ble_gap_test.c index 7496e316..3962bacc 100644 --- a/src/libs/mynewt-nimble/nimble/host/test/src/ble_gap_test.c +++ b/src/libs/mynewt-nimble/nimble/host/test/src/ble_gap_test.c @@ -2049,6 +2049,70 @@ ble_gap_test_util_update_l2cap(struct ble_gap_upd_params *params, peer_addr, 6) == 0); } +static void +ble_gap_test_util_update_no_l2cap_tmo(struct ble_gap_upd_params *params, + int master) +{ + struct ble_hs_conn *conn; + int rc; + + uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 }; + + ble_gap_test_util_init(); + + ble_hs_test_util_create_conn(2, peer_addr, ble_gap_test_util_connect_cb, + NULL); + + if (!master) { + ble_hs_lock(); + conn = ble_hs_conn_find(2); + TEST_ASSERT_FATAL(conn != NULL); + conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER; + ble_hs_unlock(); + } + + /* Erase callback info reported during connection establishment; we only + * care about updates. + */ + ble_gap_test_util_reset_cb_info(); + + TEST_ASSERT(!ble_gap_master_in_progress()); + + rc = ble_hs_test_util_conn_update(2, params, 0); + TEST_ASSERT(rc == 0); + TEST_ASSERT(!ble_gap_master_in_progress()); + + /* Verify tx of connection update command. */ + ble_gap_test_util_verify_tx_update_conn(params); + + /* Ensure no update event reported. */ + TEST_ASSERT(ble_gap_test_event.type == 0xff); + + /* Advance 39 seconds; ensure no timeout reported. */ + os_time_advance(39 * OS_TICKS_PER_SEC); + ble_gap_timer(); + TEST_ASSERT(ble_gap_test_event.type == 0xff); + + /* Advance 40th second; ensure timeout reported. */ + os_time_advance(1 * OS_TICKS_PER_SEC); + + /* Timeout will result in a terminate HCI command being sent; schedule ack + * from controller. + */ + ble_hs_test_util_hci_ack_set_disconnect(0); + + ble_gap_timer(); + + /* Verify terminate was sent. */ + ble_gap_test_util_verify_tx_disconnect(); + + TEST_ASSERT(ble_gap_test_event.type == BLE_GAP_EVENT_CONN_UPDATE); + TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_ETIMEOUT); + TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2); + TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr.val, + peer_addr, 6) == 0); +} + static void ble_gap_test_util_update_l2cap_tmo(struct ble_gap_upd_params *params, uint8_t hci_status, uint8_t event_status, @@ -2918,6 +2982,9 @@ TEST_CASE_SELF(ble_gap_test_case_update_timeout) .max_ce_len = 456, }; + /* No L2CAP. */ + ble_gap_test_util_update_no_l2cap_tmo(¶ms, 1); + /* L2CAP - Local unsupported; L2CAP timeout. */ ble_gap_test_util_update_l2cap_tmo(¶ms, BLE_ERR_UNKNOWN_HCI_CMD, 0, 0); -- cgit v1.2.3