mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-05 13:44:32 +02:00
Merge branch 'feature/nimble_spp_multiple_client' into 'master'
Send notification to multiple clients in BLE SPP example See merge request espressif/esp-idf!18539
This commit is contained in:
@@ -21,9 +21,9 @@ static const char *tag = "NimBLE_SPP_BLE_CENT";
|
|||||||
static int ble_spp_client_gap_event(struct ble_gap_event *event, void *arg);
|
static int ble_spp_client_gap_event(struct ble_gap_event *event, void *arg);
|
||||||
QueueHandle_t spp_common_uart_queue = NULL;
|
QueueHandle_t spp_common_uart_queue = NULL;
|
||||||
void ble_store_config_init(void);
|
void ble_store_config_init(void);
|
||||||
static bool is_connect = false;
|
uint16_t attribute_handle[CONFIG_BT_NIMBLE_MAX_CONNECTIONS];
|
||||||
uint16_t attribute_handle[CONFIG_BT_NIMBLE_MAX_CONNECTIONS + 1];
|
|
||||||
static void ble_spp_client_scan(void);
|
static void ble_spp_client_scan(void);
|
||||||
|
static ble_addr_t connected_addr[CONFIG_BT_NIMBLE_MAX_CONNECTIONS];
|
||||||
|
|
||||||
/* 16 Bit Alert Notification Service UUID */
|
/* 16 Bit Alert Notification Service UUID */
|
||||||
#define GATT_SVR_SVC_ALERT_UUID 0x1811
|
#define GATT_SVR_SVC_ALERT_UUID 0x1811
|
||||||
@@ -41,7 +41,7 @@ ble_spp_client_set_handle(const struct peer *peer)
|
|||||||
chr = peer_chr_find_uuid(peer,
|
chr = peer_chr_find_uuid(peer,
|
||||||
BLE_UUID16_DECLARE(GATT_SPP_SVC_UUID),
|
BLE_UUID16_DECLARE(GATT_SPP_SVC_UUID),
|
||||||
BLE_UUID16_DECLARE(GATT_SPP_CHR_UUID));
|
BLE_UUID16_DECLARE(GATT_SPP_CHR_UUID));
|
||||||
attribute_handle[peer->conn_handle] = chr->chr.val_handle;
|
attribute_handle[peer->conn_handle - 1] = chr->chr.val_handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -66,7 +66,9 @@ ble_spp_client_on_disc_complete(const struct peer *peer, int status, void *arg)
|
|||||||
"conn_handle=%d\n", status, peer->conn_handle);
|
"conn_handle=%d\n", status, peer->conn_handle);
|
||||||
|
|
||||||
ble_spp_client_set_handle(peer);
|
ble_spp_client_set_handle(peer);
|
||||||
|
#if CONFIG_BT_NIMBLE_MAX_CONNECTIONS > 1
|
||||||
ble_spp_client_scan();
|
ble_spp_client_scan();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -123,6 +125,14 @@ ble_spp_client_should_connect(const struct ble_gap_disc_desc *disc)
|
|||||||
int rc;
|
int rc;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
/* Check if device is already connected or not */
|
||||||
|
for ( i = 0; i < CONFIG_BT_NIMBLE_MAX_CONNECTIONS; i++) {
|
||||||
|
if (memcmp(&connected_addr[i].val,disc->addr.val, sizeof(disc->addr.val)) == 0) {
|
||||||
|
MODLOG_DFLT(DEBUG, "Device already connected");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* The device has to be advertising connectability. */
|
/* The device has to be advertising connectability. */
|
||||||
if (disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_ADV_IND &&
|
if (disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_ADV_IND &&
|
||||||
disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_DIR_IND) {
|
disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_DIR_IND) {
|
||||||
@@ -232,9 +242,10 @@ ble_spp_client_gap_event(struct ble_gap_event *event, void *arg)
|
|||||||
if (event->connect.status == 0) {
|
if (event->connect.status == 0) {
|
||||||
/* Connection successfully established. */
|
/* Connection successfully established. */
|
||||||
MODLOG_DFLT(INFO, "Connection established ");
|
MODLOG_DFLT(INFO, "Connection established ");
|
||||||
is_connect = true;
|
|
||||||
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
|
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
|
||||||
assert(rc == 0);
|
assert(rc == 0);
|
||||||
|
memcpy(&connected_addr[event->connect.conn_handle - 1].val, desc.peer_id_addr.val,
|
||||||
|
sizeof(desc.peer_id_addr.val));
|
||||||
print_conn_desc(&desc);
|
print_conn_desc(&desc);
|
||||||
MODLOG_DFLT(INFO, "\n");
|
MODLOG_DFLT(INFO, "\n");
|
||||||
|
|
||||||
@@ -346,7 +357,7 @@ void ble_client_uart_task(void *pvParameters)
|
|||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
//Event of UART receving data
|
//Event of UART receving data
|
||||||
case UART_DATA:
|
case UART_DATA:
|
||||||
if (event.size && (is_connect == true)) {
|
if (event.size) {
|
||||||
|
|
||||||
/* Writing characteristics */
|
/* Writing characteristics */
|
||||||
uint8_t * temp = NULL;
|
uint8_t * temp = NULL;
|
||||||
@@ -357,9 +368,9 @@ void ble_client_uart_task(void *pvParameters)
|
|||||||
}
|
}
|
||||||
memset(temp, 0x0, event.size);
|
memset(temp, 0x0, event.size);
|
||||||
uart_read_bytes(UART_NUM_0,temp,event.size,portMAX_DELAY);
|
uart_read_bytes(UART_NUM_0,temp,event.size,portMAX_DELAY);
|
||||||
for ( i = 1; i <= CONFIG_BT_NIMBLE_MAX_CONNECTIONS; i++) {
|
for ( i = 0; i < CONFIG_BT_NIMBLE_MAX_CONNECTIONS; i++) {
|
||||||
if (attribute_handle[i] != 0) {
|
if (attribute_handle[i] != 0) {
|
||||||
rc = ble_gattc_write_flat(i, attribute_handle[i],temp, event.size,NULL, NULL);
|
rc = ble_gattc_write_flat(i+1, attribute_handle[i],temp, event.size,NULL,NULL);
|
||||||
if (rc == 0) {
|
if (rc == 0) {
|
||||||
ESP_LOGI(tag,"Write in uart task success!");
|
ESP_LOGI(tag,"Write in uart task success!");
|
||||||
}
|
}
|
||||||
|
@@ -22,8 +22,7 @@ static int ble_spp_server_gap_event(struct ble_gap_event *event, void *arg);
|
|||||||
static uint8_t own_addr_type;
|
static uint8_t own_addr_type;
|
||||||
int gatt_svr_register(void);
|
int gatt_svr_register(void);
|
||||||
QueueHandle_t spp_common_uart_queue = NULL;
|
QueueHandle_t spp_common_uart_queue = NULL;
|
||||||
static bool is_connect = false;
|
uint16_t connection_handle[CONFIG_BT_NIMBLE_MAX_CONNECTIONS];
|
||||||
uint16_t connection_handle;
|
|
||||||
static uint16_t ble_svc_gatt_read_val_handle,ble_spp_svc_gatt_read_val_handle;
|
static uint16_t ble_svc_gatt_read_val_handle,ble_spp_svc_gatt_read_val_handle;
|
||||||
|
|
||||||
/* 16 Bit Alert Notification Service UUID */
|
/* 16 Bit Alert Notification Service UUID */
|
||||||
@@ -164,12 +163,11 @@ ble_spp_server_gap_event(struct ble_gap_event *event, void *arg)
|
|||||||
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
|
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
|
||||||
assert(rc == 0);
|
assert(rc == 0);
|
||||||
ble_spp_server_print_conn_desc(&desc);
|
ble_spp_server_print_conn_desc(&desc);
|
||||||
is_connect=true;
|
connection_handle[event->connect.conn_handle - 1] = event->connect.conn_handle;
|
||||||
connection_handle = event->connect.conn_handle;
|
|
||||||
}
|
}
|
||||||
MODLOG_DFLT(INFO, "\n");
|
MODLOG_DFLT(INFO, "\n");
|
||||||
if (event->connect.status != 0) {
|
if (event->connect.status != 0 || CONFIG_BT_NIMBLE_MAX_CONNECTIONS > 1) {
|
||||||
/* Connection failed; resume advertising. */
|
/* Connection failed or if multiple connection allowed; resume advertising. */
|
||||||
ble_spp_server_advertise();
|
ble_spp_server_advertise();
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -339,17 +337,23 @@ void ble_server_uart_task(void *pvParameters){
|
|||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
//Event of UART receving data
|
//Event of UART receving data
|
||||||
case UART_DATA:
|
case UART_DATA:
|
||||||
if (event.size && (is_connect == true)) {
|
if (event.size) {
|
||||||
static uint8_t ntf[1];
|
static uint8_t ntf[1];
|
||||||
ntf[0] = 90;
|
ntf[0] = 90;
|
||||||
|
for ( int i = 0; i < CONFIG_BT_NIMBLE_MAX_CONNECTIONS; i++) {
|
||||||
|
if (connection_handle[i] != 0) {
|
||||||
struct os_mbuf *txom;
|
struct os_mbuf *txom;
|
||||||
txom = ble_hs_mbuf_from_flat(ntf, sizeof(ntf));
|
txom = ble_hs_mbuf_from_flat(ntf, sizeof(ntf));
|
||||||
rc = ble_gattc_notify_custom(connection_handle,ble_spp_svc_gatt_read_val_handle,txom);
|
rc = ble_gattc_notify_custom(connection_handle[i],
|
||||||
if( rc == 0){
|
ble_spp_svc_gatt_read_val_handle,
|
||||||
|
txom);
|
||||||
|
if ( rc == 0 ) {
|
||||||
ESP_LOGI(tag,"Notification sent successfully");
|
ESP_LOGI(tag,"Notification sent successfully");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
ESP_LOGI(tag,"Error in sending notification");
|
ESP_LOGI(tag,"Error in sending notification rc = %d", rc);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
Reference in New Issue
Block a user