mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
Nimble: Updated ble_l2cap_coc example
1. Corrected format 2. Fixed double free of memory issue in case of disconnection 3. Updated sdkconfig.default file to have correct value for COC_MAX_NUM configuration setting
This commit is contained in:
parent
c41bb46d92
commit
4da5d3e7a5
@ -21,10 +21,10 @@ static uint8_t peer_addr[6];
|
||||
|
||||
void ble_store_config_init(void);
|
||||
|
||||
#if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM > 1
|
||||
#if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM >= 1
|
||||
|
||||
#define COC_BUF_COUNT (3 * MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM))
|
||||
#define L2CAP_COC_UUID 0x1812
|
||||
#define L2CAP_COC_UUID 0x1812
|
||||
|
||||
static uint16_t conn_handle_coc ;
|
||||
static uint16_t mtu = 512;
|
||||
@ -55,9 +55,9 @@ blecent_l2cap_coc_send_data(struct ble_l2cap_chan *chan)
|
||||
|
||||
rc = ble_l2cap_send(chan, sdu_rx_data);
|
||||
if (rc == 0) {
|
||||
MODLOG_DFLT(INFO,"Data sent successfully");
|
||||
MODLOG_DFLT(INFO, "Data sent successfully");
|
||||
} else {
|
||||
MODLOG_DFLT(INFO,"Data sending failed, rc = %d",rc);
|
||||
MODLOG_DFLT(INFO, "Data sending failed, rc = %d", rc);
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,9 +73,7 @@ blecent_l2cap_coc_on_disc_complete(const struct peer *peer, int status, void *ar
|
||||
|
||||
sdu_rx = os_mbuf_get_pkthdr(&sdu_os_mbuf_pool, 0);
|
||||
ble_l2cap_connect(conn_handle_coc, psm, mtu, sdu_rx, blecent_l2cap_coc_event_cb,
|
||||
NULL);
|
||||
|
||||
os_mbuf_free(sdu_rx);
|
||||
NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -97,35 +95,35 @@ blecent_l2cap_coc_event_cb(struct ble_l2cap_event *event, void *arg)
|
||||
{
|
||||
struct ble_l2cap_chan_info chan_info;
|
||||
|
||||
switch(event->type) {
|
||||
case BLE_L2CAP_EVENT_COC_CONNECTED:
|
||||
if (event->connect.status) {
|
||||
console_printf("LE COC error: %d\n", event->connect.status);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ble_l2cap_get_chan_info(event->connect.chan, &chan_info)) {
|
||||
assert(0);
|
||||
}
|
||||
|
||||
console_printf("LE COC connected, conn: %d, chan: %p, psm: 0x%02x,"
|
||||
" scid: 0x%04x, ""dcid: 0x%04x, our_mps: %d, our_mtu: %d,"
|
||||
" peer_mps: %d, peer_mtu: %d\n",
|
||||
event->connect.conn_handle, event->connect.chan,
|
||||
chan_info.psm, chan_info.scid, chan_info.dcid,
|
||||
chan_info.our_l2cap_mtu, chan_info.our_coc_mtu,
|
||||
chan_info.peer_l2cap_mtu, chan_info.peer_coc_mtu);
|
||||
blecent_l2cap_coc_send_data(event->connect.chan);
|
||||
switch (event->type) {
|
||||
case BLE_L2CAP_EVENT_COC_CONNECTED:
|
||||
if (event->connect.status) {
|
||||
console_printf("LE COC error: %d\n", event->connect.status);
|
||||
return 0;
|
||||
}
|
||||
|
||||
case BLE_L2CAP_EVENT_COC_DISCONNECTED:
|
||||
console_printf("LE CoC disconnected, chan: %p\n",
|
||||
event->disconnect.chan);
|
||||
return 0;
|
||||
if (ble_l2cap_get_chan_info(event->connect.chan, &chan_info)) {
|
||||
assert(0);
|
||||
}
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
console_printf("LE COC connected, conn: %d, chan: %p, psm: 0x%02x,"
|
||||
" scid: 0x%04x, ""dcid: 0x%04x, our_mps: %d, our_mtu: %d,"
|
||||
" peer_mps: %d, peer_mtu: %d\n",
|
||||
event->connect.conn_handle, event->connect.chan,
|
||||
chan_info.psm, chan_info.scid, chan_info.dcid,
|
||||
chan_info.our_l2cap_mtu, chan_info.our_coc_mtu,
|
||||
chan_info.peer_l2cap_mtu, chan_info.peer_coc_mtu);
|
||||
blecent_l2cap_coc_send_data(event->connect.chan);
|
||||
return 0;
|
||||
|
||||
case BLE_L2CAP_EVENT_COC_DISCONNECTED:
|
||||
console_printf("LE CoC disconnected, chan: %p\n",
|
||||
event->disconnect.chan);
|
||||
return 0;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
@ -133,17 +131,18 @@ blecent_l2cap_coc_mem_init(void)
|
||||
{
|
||||
int rc;
|
||||
rc = os_mempool_init(&sdu_coc_mbuf_mempool, COC_BUF_COUNT, mtu, sdu_coc_mem,
|
||||
"coc_sdu_pool");
|
||||
"coc_sdu_pool");
|
||||
assert(rc == 0);
|
||||
rc = os_mbuf_pool_init(&sdu_os_mbuf_pool, &sdu_coc_mbuf_mempool, mtu,
|
||||
COC_BUF_COUNT);
|
||||
COC_BUF_COUNT);
|
||||
assert(rc == 0);
|
||||
}
|
||||
#endif // #if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM > 1
|
||||
#endif // #if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM >= 1
|
||||
|
||||
/**
|
||||
* Called when service discovery of the specified peer has completed.
|
||||
*/
|
||||
#if !CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM
|
||||
static void
|
||||
blecent_on_disc_complete(const struct peer *peer, int status, void *arg)
|
||||
{
|
||||
@ -167,6 +166,7 @@ blecent_on_disc_complete(const struct peer *peer, int status, void *arg)
|
||||
* write, and subscribe to notifications.
|
||||
*/
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Initiates the GAP general discovery procedure.
|
||||
@ -227,7 +227,7 @@ ext_blecent_should_connect(const struct ble_gap_ext_disc_desc *disc)
|
||||
return 0;
|
||||
}
|
||||
if (strlen(CONFIG_EXAMPLE_PEER_ADDR) &&
|
||||
(strncmp(CONFIG_EXAMPLE_PEER_ADDR, "ADDR_ANY", strlen("ADDR_ANY")) != 0)) {
|
||||
(strncmp(CONFIG_EXAMPLE_PEER_ADDR, "ADDR_ANY", strlen("ADDR_ANY")) != 0)) {
|
||||
ESP_LOGI(tag, "Peer address from menuconfig: %s", CONFIG_EXAMPLE_PEER_ADDR);
|
||||
/* Convert string to address */
|
||||
sscanf(CONFIG_EXAMPLE_PEER_ADDR, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
|
||||
@ -246,7 +246,7 @@ ext_blecent_should_connect(const struct ble_gap_ext_disc_desc *disc)
|
||||
break;
|
||||
}
|
||||
|
||||
/* Search if ANS UUID is advertised */
|
||||
/* Search if ANS UUID is advertised */
|
||||
if (disc->data[offset] == 0x03 && disc->data[offset + 1] == 0x03) {
|
||||
if ( disc->data[offset + 2] == 0x18 && disc->data[offset + 3] == 0x12 ) {
|
||||
return 1;
|
||||
@ -255,7 +255,7 @@ ext_blecent_should_connect(const struct ble_gap_ext_disc_desc *disc)
|
||||
|
||||
offset += ad_struct_len + 1;
|
||||
|
||||
} while ( offset < disc->length_data );
|
||||
} while ( offset < disc->length_data );
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
@ -279,7 +279,7 @@ blecent_should_connect(const struct ble_gap_disc_desc *disc)
|
||||
}
|
||||
|
||||
if (strlen(CONFIG_EXAMPLE_PEER_ADDR) &&
|
||||
(strncmp(CONFIG_EXAMPLE_PEER_ADDR, "ADDR_ANY", strlen("ADDR_ANY")) != 0)) {
|
||||
(strncmp(CONFIG_EXAMPLE_PEER_ADDR, "ADDR_ANY", strlen("ADDR_ANY")) != 0)) {
|
||||
MODLOG_DFLT(INFO, "Peer address from menuconfig:%s", CONFIG_EXAMPLE_PEER_ADDR);
|
||||
/* Convert string to address */
|
||||
sscanf(CONFIG_EXAMPLE_PEER_ADDR, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
|
||||
@ -405,9 +405,9 @@ blecent_gap_event(struct ble_gap_event *event, void *arg)
|
||||
print_conn_desc(&desc);
|
||||
MODLOG_DFLT(INFO, "\n");
|
||||
|
||||
#if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM > 1
|
||||
#if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM >= 1
|
||||
conn_handle_coc = event->connect.conn_handle;
|
||||
disc_cb = blecent_l2cap_coc_on_disc_complete;
|
||||
disc_cb = blecent_l2cap_coc_on_disc_complete;
|
||||
#else
|
||||
disc_cb = blecent_on_disc_complete;
|
||||
#endif
|
||||
@ -512,7 +512,7 @@ app_main(void)
|
||||
ble_hs_cfg.sync_cb = blecent_on_sync;
|
||||
ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
|
||||
|
||||
#if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM > 1
|
||||
#if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM >= 1
|
||||
blecent_l2cap_coc_mem_init();
|
||||
#endif
|
||||
|
||||
|
@ -10,3 +10,4 @@ CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n
|
||||
CONFIG_BTDM_CTRL_MODE_BTDM=n
|
||||
CONFIG_BT_BLUEDROID_ENABLED=n
|
||||
CONFIG_BT_NIMBLE_ENABLED=y
|
||||
CONFIG_NIMBLE_L2CAP_COC_MAX_NUM=1
|
||||
|
@ -30,7 +30,7 @@ static uint8_t own_addr_type;
|
||||
|
||||
void ble_store_config_init(void);
|
||||
|
||||
#if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM > 1
|
||||
#if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM >= 1
|
||||
|
||||
#define COC_BUF_COUNT (3 * MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM))
|
||||
|
||||
@ -191,10 +191,10 @@ bleprph_advertise(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM > 1
|
||||
#if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM >= 1
|
||||
static int
|
||||
bleprph_l2cap_coc_accept(uint16_t conn_handle, uint16_t peer_mtu,
|
||||
struct ble_l2cap_chan *chan)
|
||||
struct ble_l2cap_chan *chan)
|
||||
{
|
||||
struct os_mbuf *sdu_rx;
|
||||
|
||||
@ -226,54 +226,54 @@ bleprph_l2cap_coc_accept(uint16_t conn_handle, uint16_t peer_mtu,
|
||||
static int
|
||||
bleprph_l2cap_coc_event_cb(struct ble_l2cap_event *event, void *arg)
|
||||
{
|
||||
struct ble_l2cap_chan_info chan_info;
|
||||
struct ble_l2cap_chan_info chan_info;
|
||||
|
||||
switch(event->type) {
|
||||
case BLE_L2CAP_EVENT_COC_CONNECTED:
|
||||
if (event->connect.status) {
|
||||
console_printf("LE COC error: %d\n", event->connect.status);
|
||||
return 0;
|
||||
}
|
||||
switch (event->type) {
|
||||
case BLE_L2CAP_EVENT_COC_CONNECTED:
|
||||
if (event->connect.status) {
|
||||
console_printf("LE COC error: %d\n", event->connect.status);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ble_l2cap_get_chan_info(event->connect.chan, &chan_info)) {
|
||||
assert(0);
|
||||
}
|
||||
if (ble_l2cap_get_chan_info(event->connect.chan, &chan_info)) {
|
||||
assert(0);
|
||||
}
|
||||
|
||||
console_printf("LE COC connected, conn: %d, chan: %p, psm: 0x%02x,"
|
||||
" scid: 0x%04x, ""dcid: 0x%04x, our_mps: %d, our_mtu: %d,"
|
||||
" peer_mps: %d, peer_mtu: %d\n",
|
||||
event->connect.conn_handle, event->connect.chan,
|
||||
chan_info.psm, chan_info.scid, chan_info.dcid,
|
||||
chan_info.our_l2cap_mtu, chan_info.our_coc_mtu,
|
||||
chan_info.peer_l2cap_mtu, chan_info.peer_coc_mtu);
|
||||
console_printf("LE COC connected, conn: %d, chan: %p, psm: 0x%02x,"
|
||||
" scid: 0x%04x, ""dcid: 0x%04x, our_mps: %d, our_mtu: %d,"
|
||||
" peer_mps: %d, peer_mtu: %d\n",
|
||||
event->connect.conn_handle, event->connect.chan,
|
||||
chan_info.psm, chan_info.scid, chan_info.dcid,
|
||||
chan_info.our_l2cap_mtu, chan_info.our_coc_mtu,
|
||||
chan_info.peer_l2cap_mtu, chan_info.peer_coc_mtu);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
|
||||
case BLE_L2CAP_EVENT_COC_DISCONNECTED:
|
||||
console_printf("LE CoC disconnected, chan: %p\n",
|
||||
event->disconnect.chan);
|
||||
case BLE_L2CAP_EVENT_COC_DISCONNECTED:
|
||||
console_printf("LE CoC disconnected, chan: %p\n",
|
||||
event->disconnect.chan);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
|
||||
case BLE_L2CAP_EVENT_COC_DATA_RECEIVED:
|
||||
if (event->receive.sdu_rx != NULL) {
|
||||
MODLOG_DFLT(INFO, "Data received : ");
|
||||
for (int i = 0; i < event->receive.sdu_rx->om_len; i++) {
|
||||
console_printf("%d ", event->receive.sdu_rx->om_data[i]);
|
||||
}
|
||||
}
|
||||
fflush(stdout);
|
||||
return 0;
|
||||
case BLE_L2CAP_EVENT_COC_DATA_RECEIVED:
|
||||
if (event->receive.sdu_rx != NULL) {
|
||||
MODLOG_DFLT(INFO, "Data received : ");
|
||||
for (int i = 0; i < event->receive.sdu_rx->om_len; i++) {
|
||||
console_printf("%d ", event->receive.sdu_rx->om_data[i]);
|
||||
}
|
||||
}
|
||||
fflush(stdout);
|
||||
return 0;
|
||||
|
||||
case BLE_L2CAP_EVENT_COC_ACCEPT:
|
||||
bleprph_l2cap_coc_accept(event->accept.conn_handle,
|
||||
event->accept.peer_sdu_size,
|
||||
event->accept.chan);
|
||||
return 0;
|
||||
case BLE_L2CAP_EVENT_COC_ACCEPT:
|
||||
bleprph_l2cap_coc_accept(event->accept.conn_handle,
|
||||
event->accept.peer_sdu_size,
|
||||
event->accept.chan);
|
||||
return 0;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
static void
|
||||
bleprph_l2cap_coc_mem_init(void)
|
||||
@ -326,18 +326,18 @@ bleprph_gap_event(struct ble_gap_event *event, void *arg)
|
||||
if (event->connect.status != 0) {
|
||||
/* Connection failed; resume advertising. */
|
||||
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
||||
ext_bleprph_advertise();
|
||||
ext_bleprph_advertise();
|
||||
#else
|
||||
bleprph_advertise();
|
||||
#endif
|
||||
} else {
|
||||
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
|
||||
assert(rc == 0);
|
||||
bleprph_print_conn_desc(&desc);
|
||||
#if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM > 1
|
||||
rc = ble_l2cap_create_server(psm, mtu, bleprph_l2cap_coc_event_cb, NULL);
|
||||
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
|
||||
assert(rc == 0);
|
||||
bleprph_print_conn_desc(&desc);
|
||||
#if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM >= 1
|
||||
rc = ble_l2cap_create_server(psm, mtu, bleprph_l2cap_coc_event_cb, NULL);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
case BLE_GAP_EVENT_DISCONNECT:
|
||||
@ -347,7 +347,7 @@ bleprph_gap_event(struct ble_gap_event *event, void *arg)
|
||||
|
||||
/* Connection terminated; resume advertising. */
|
||||
#if CONFIG_EXAMPLE_EXTENDED_ADV
|
||||
ext_bleprph_advertise();
|
||||
ext_bleprph_advertise();
|
||||
#else
|
||||
bleprph_advertise();
|
||||
#endif
|
||||
@ -371,7 +371,7 @@ bleprph_gap_event(struct ble_gap_event *event, void *arg)
|
||||
#endif
|
||||
return 0;
|
||||
|
||||
default:
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -460,7 +460,7 @@ app_main(void)
|
||||
ble_hs_cfg.sm_their_key_dist = 1;
|
||||
#endif
|
||||
|
||||
#if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM > 1
|
||||
#if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM >= 1
|
||||
bleprph_l2cap_coc_mem_init();
|
||||
#endif
|
||||
|
||||
|
@ -10,3 +10,4 @@ CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n
|
||||
CONFIG_BTDM_CTRL_MODE_BTDM=n
|
||||
CONFIG_BT_BLUEDROID_ENABLED=n
|
||||
CONFIG_BT_NIMBLE_ENABLED=y
|
||||
CONFIG_NIMBLE_L2CAP_COC_MAX_NUM=1
|
||||
|
Loading…
x
Reference in New Issue
Block a user