summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Collins <ccollins@apache.org>2016-05-09 21:00:56 -0700
committerChristopher Collins <ccollins@apache.org>2016-05-09 21:01:39 -0700
commitfc5d42d50c883d338a89e9982a11fb9fc0ab1c9a (patch)
tree31cd60179f5b3e9f9d09e015d493e0434cd98098
parentf4c734e3924ef439a8554ff8d6f2bc0d5f10a2e3 (diff)
BLE host - fix check for sufficient conn resources
-rwxr-xr-xapps/bleprph/src/main.c10
-rwxr-xr-xapps/bletiny/src/main.c4
-rw-r--r--net/nimble/host/src/ble_gatt_priv.h2
-rw-r--r--net/nimble/host/src/ble_gatts.c24
-rw-r--r--net/nimble/host/src/ble_hs.c2
-rw-r--r--net/nimble/host/src/ble_hs_conn.c10
-rw-r--r--net/nimble/host/src/ble_l2cap.c2
-rw-r--r--net/nimble/host/src/ble_l2cap_priv.h2
-rw-r--r--net/nimble/host/src/test/ble_gatts_notify_test.c2
9 files changed, 34 insertions, 24 deletions
diff --git a/apps/bleprph/src/main.c b/apps/bleprph/src/main.c
index ae59c335..3394c671 100755
--- a/apps/bleprph/src/main.c
+++ b/apps/bleprph/src/main.c
@@ -186,11 +186,6 @@ bleprph_task_handler(void *unused)
struct os_event *ev;
struct os_callout_func *cf;
- /* Register GATT attributes (services, characteristics, and
- * descriptors).
- */
- gatt_svr_init();
-
/* Begin advertising. */
bleprph_advertise();
@@ -290,6 +285,11 @@ main(void)
rc = console_init(NULL);
assert(rc == 0);
+ /* Register GATT attributes (services, characteristics, and
+ * descriptors).
+ */
+ gatt_svr_init();
+
/* Start the OS */
os_start();
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index a2a557e1..7f76cc7f 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -1380,8 +1380,6 @@ bletiny_task_handler(void *arg)
rc = ble_hs_start();
assert(rc == 0);
- periph_init();
-
ble_att_set_notify_cb(bletiny_on_notify, NULL);
bletiny_start_auto_advertise();
@@ -1524,6 +1522,8 @@ main(void)
htole16(bletiny_pref_conn_params + 4, 0);
htole16(bletiny_pref_conn_params + 6, BSWAP16(0x100));
+ periph_init();
+
/* Start the OS */
os_start();
diff --git a/net/nimble/host/src/ble_gatt_priv.h b/net/nimble/host/src/ble_gatt_priv.h
index 704d4412..44f79ec3 100644
--- a/net/nimble/host/src/ble_gatt_priv.h
+++ b/net/nimble/host/src/ble_gatt_priv.h
@@ -149,7 +149,9 @@ void ble_gatts_send_notifications(struct ble_hs_conn *conn);
/*** @misc. */
void ble_gatts_conn_deinit(struct ble_gatts_conn *gatts_conn);
+int ble_gatts_conn_can_alloc(void);
int ble_gatts_conn_init(struct ble_gatts_conn *gatts_conn);
+int ble_gatts_start(void);
int ble_gatts_init(void);
#endif
diff --git a/net/nimble/host/src/ble_gatts.c b/net/nimble/host/src/ble_gatts.c
index cc252ca5..b0d5c7c4 100644
--- a/net/nimble/host/src/ble_gatts.c
+++ b/net/nimble/host/src/ble_gatts.c
@@ -38,7 +38,6 @@ static int ble_gatts_num_svc_entries;
static os_membuf_t *ble_gatts_clt_cfg_mem;
static struct os_mempool ble_gatts_clt_cfg_pool;
-static uint8_t ble_gatts_clt_cfg_inited;
struct ble_gatts_clt_cfg {
uint16_t chr_def_handle;
@@ -889,8 +888,8 @@ ble_gatts_clt_cfg_size(void)
return ble_gatts_num_cfgable_chrs * sizeof (struct ble_gatts_clt_cfg);
}
-static int
-ble_gatts_clt_cfg_init(void)
+int
+ble_gatts_start(void)
{
struct ble_att_svr_entry *ha;
struct ble_gatt_chr_def *chr;
@@ -957,19 +956,15 @@ ble_gatts_clt_cfg_init(void)
}
int
-ble_gatts_conn_init(struct ble_gatts_conn *gatts_conn)
+ble_gatts_conn_can_alloc(void)
{
- int rc;
-
- /* Initialize the client configuration memory pool if necessary. */
- if (!ble_gatts_clt_cfg_inited) {
- rc = ble_gatts_clt_cfg_init();
- if (rc != 0) {
- return rc;
- }
- ble_gatts_clt_cfg_inited = 1;
- }
+ return ble_gatts_num_cfgable_chrs == 0 ||
+ ble_gatts_clt_cfg_pool.mp_num_free > 0;
+}
+int
+ble_gatts_conn_init(struct ble_gatts_conn *gatts_conn)
+{
if (ble_gatts_num_cfgable_chrs > 0) {
ble_gatts_conn_deinit(gatts_conn);
gatts_conn->clt_cfgs = os_memblock_get(&ble_gatts_clt_cfg_pool);
@@ -1079,7 +1074,6 @@ ble_gatts_init(void)
ble_gatts_free_mem();
ble_gatts_num_cfgable_chrs = 0;
ble_gatts_clt_cfgs = NULL;
- ble_gatts_clt_cfg_inited = 0;
if (ble_hs_cfg.max_client_configs > 0) {
ble_gatts_clt_cfg_mem = malloc(
diff --git a/net/nimble/host/src/ble_hs.c b/net/nimble/host/src/ble_hs.c
index c3f1f65b..fbbc14de 100644
--- a/net/nimble/host/src/ble_hs.c
+++ b/net/nimble/host/src/ble_hs.c
@@ -242,6 +242,8 @@ ble_hs_start(void)
ble_hs_heartbeat_timer_reset();
+ ble_gatts_start();
+
rc = ble_hs_startup_go();
return rc;
}
diff --git a/net/nimble/host/src/ble_hs_conn.c b/net/nimble/host/src/ble_hs_conn.c
index 8d22739f..4071f457 100644
--- a/net/nimble/host/src/ble_hs_conn.c
+++ b/net/nimble/host/src/ble_hs_conn.c
@@ -23,6 +23,9 @@
#include "host/host_hci.h"
#include "ble_hs_priv.h"
+/** At least three channels required per connection (sig, att, sm). */
+#define BLE_HS_CONN_MIN_CHANS 3
+
static SLIST_HEAD(, ble_hs_conn) ble_hs_conns;
static struct os_mempool ble_hs_conn_pool;
@@ -35,7 +38,9 @@ ble_hs_conn_can_alloc(void)
return 0;
#endif
- return ble_hs_conn_pool.mp_num_free >= 1;
+ return ble_hs_conn_pool.mp_num_free >= 1 &&
+ ble_l2cap_chan_pool.mp_num_free >= BLE_HS_CONN_MIN_CHANS &&
+ ble_gatts_conn_can_alloc();
}
struct ble_l2cap_chan *
@@ -127,6 +132,9 @@ ble_hs_conn_alloc(void)
goto err;
}
+ /* XXX: We should create the SM channel even if not configured. We need it
+ * to reject SM messages.
+ */
#if NIMBLE_OPT_SM
chan = ble_l2cap_sm_create_chan();
if (chan == NULL) {
diff --git a/net/nimble/host/src/ble_l2cap.c b/net/nimble/host/src/ble_l2cap.c
index f1921f41..6dcc34a4 100644
--- a/net/nimble/host/src/ble_l2cap.c
+++ b/net/nimble/host/src/ble_l2cap.c
@@ -28,7 +28,7 @@
_Static_assert(sizeof (struct ble_l2cap_hdr) == BLE_L2CAP_HDR_SZ,
"struct ble_l2cap_hdr must be 4 bytes");
-static struct os_mempool ble_l2cap_chan_pool;
+struct os_mempool ble_l2cap_chan_pool;
static void *ble_l2cap_chan_mem;
diff --git a/net/nimble/host/src/ble_l2cap_priv.h b/net/nimble/host/src/ble_l2cap_priv.h
index ecf7d179..1a0e458c 100644
--- a/net/nimble/host/src/ble_l2cap_priv.h
+++ b/net/nimble/host/src/ble_l2cap_priv.h
@@ -42,6 +42,8 @@ STATS_SECT_START(ble_l2cap_stats)
STATS_SECT_END
extern STATS_SECT_DECL(ble_l2cap_stats) ble_l2cap_stats;
+extern struct os_mempool ble_l2cap_chan_pool;
+
#define BLE_L2CAP_CID_ATT 4
#define BLE_L2CAP_CID_SIG 5
#define BLE_L2CAP_CID_SM 6
diff --git a/net/nimble/host/src/test/ble_gatts_notify_test.c b/net/nimble/host/src/test/ble_gatts_notify_test.c
index 26fc55c7..a1b9e572 100644
--- a/net/nimble/host/src/test/ble_gatts_notify_test.c
+++ b/net/nimble/host/src/test/ble_gatts_notify_test.c
@@ -80,6 +80,8 @@ ble_gatts_notify_test_misc_init(struct ble_hs_conn **conn,
TEST_ASSERT_FATAL(ble_gatts_notify_test_chr_1_def_handle != 0);
TEST_ASSERT_FATAL(ble_gatts_notify_test_chr_2_def_handle != 0);
+ ble_gatts_start();
+
ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
NULL, NULL);
*conn = ble_hs_conn_find(2);