forked from espressif/esp-idf
Merge branch 'bugfix/spp_notify_v4.4' into 'release/v4.4'
NimBLE: Removed indicate flag from gatt db and added subscription case in spp_server example (v4.4) See merge request espressif/esp-idf!23536
This commit is contained in:
@@ -18,12 +18,11 @@ struct ble_hs_cfg;
|
|||||||
union ble_store_value;
|
union ble_store_value;
|
||||||
union ble_store_key;
|
union ble_store_key;
|
||||||
|
|
||||||
#define GATT_SVR_SVC_ALERT_UUID 0x1811
|
/* 16 Bit SPP Service UUID */
|
||||||
#define GATT_SVR_CHR_SUP_NEW_ALERT_CAT_UUID 0x2A47
|
#define GATT_SPP_SVC_UUID 0xABF0
|
||||||
#define GATT_SVR_CHR_NEW_ALERT 0x2A46
|
|
||||||
#define GATT_SVR_CHR_SUP_UNR_ALERT_CAT_UUID 0x2A48
|
/* 16 Bit SPP Service Characteristic UUID */
|
||||||
#define GATT_SVR_CHR_UNR_ALERT_STAT_UUID 0x2A45
|
#define GATT_SPP_CHR_UUID 0xABF1
|
||||||
#define GATT_SVR_CHR_ALERT_NOT_CTRL_PT 0x2A44
|
|
||||||
|
|
||||||
/** Misc. */
|
/** Misc. */
|
||||||
void print_bytes(const uint8_t *bytes, int len);
|
void print_bytes(const uint8_t *bytes, int len);
|
||||||
|
@@ -24,14 +24,6 @@ void ble_store_config_init(void);
|
|||||||
static bool is_connect = false;
|
static bool is_connect = false;
|
||||||
uint16_t connection_handle;
|
uint16_t connection_handle;
|
||||||
uint16_t attribute_handle;
|
uint16_t attribute_handle;
|
||||||
/* 16 Bit Alert Notification Service UUID */
|
|
||||||
#define GATT_SVR_SVC_ALERT_UUID 0x1811
|
|
||||||
|
|
||||||
/* 16 Bit SPP Service UUID */
|
|
||||||
#define GATT_SPP_SVC_UUID 0xABF0
|
|
||||||
|
|
||||||
/* 16 Bit SPP Service Characteristic UUID */
|
|
||||||
#define GATT_SPP_CHR_UUID 0xABF1
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
ble_spp_client_set_handles(const struct peer *peer){
|
ble_spp_client_set_handles(const struct peer *peer){
|
||||||
@@ -138,7 +130,7 @@ ble_spp_client_should_connect(const struct ble_gap_disc_desc *disc)
|
|||||||
* service (0x1811).
|
* service (0x1811).
|
||||||
*/
|
*/
|
||||||
for (i = 0; i < fields.num_uuids16; i++) {
|
for (i = 0; i < fields.num_uuids16; i++) {
|
||||||
if (ble_uuid_u16(&fields.uuids16[i].u) == GATT_SVR_SVC_ALERT_UUID) {
|
if (ble_uuid_u16(&fields.uuids16[i].u) == GATT_SPP_SVC_UUID) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -14,24 +14,18 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* 16 Bit SPP Service UUID */
|
||||||
|
#define BLE_SVC_SPP_UUID16 0xABF0
|
||||||
|
|
||||||
|
/* 16 Bit SPP Service Characteristic UUID */
|
||||||
|
#define BLE_SVC_SPP_CHR_UUID16 0xABF1
|
||||||
|
|
||||||
struct ble_hs_cfg;
|
struct ble_hs_cfg;
|
||||||
struct ble_gatt_register_ctxt;
|
struct ble_gatt_register_ctxt;
|
||||||
|
|
||||||
/** GATT server. */
|
|
||||||
#define GATT_SVR_SVC_ALERT_UUID 0x1811
|
|
||||||
#define GATT_SVR_CHR_SUP_NEW_ALERT_CAT_UUID 0x2A47
|
|
||||||
#define GATT_SVR_CHR_NEW_ALERT 0x2A46
|
|
||||||
#define GATT_SVR_CHR_SUP_UNR_ALERT_CAT_UUID 0x2A48
|
|
||||||
#define GATT_SVR_CHR_UNR_ALERT_STAT_UUID 0x2A45
|
|
||||||
#define GATT_SVR_CHR_ALERT_NOT_CTRL_PT 0x2A44
|
|
||||||
|
|
||||||
void gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg);
|
void gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg);
|
||||||
int new_gatt_svr_init(void);
|
int new_gatt_svr_init(void);
|
||||||
|
|
||||||
/* Console */
|
|
||||||
int scli_init(void);
|
|
||||||
int scli_receive_key(int *key);
|
|
||||||
|
|
||||||
/** Misc. */
|
/** Misc. */
|
||||||
void print_bytes(const uint8_t *bytes, int len);
|
void print_bytes(const uint8_t *bytes, int len);
|
||||||
void print_addr(const void *addr);
|
void print_addr(const void *addr);
|
||||||
|
@@ -17,26 +17,12 @@
|
|||||||
#include "ble_spp_server.h"
|
#include "ble_spp_server.h"
|
||||||
#include "driver/uart.h"
|
#include "driver/uart.h"
|
||||||
|
|
||||||
static const char *tag = "NimBLE_SPP_BLE_PRPH";
|
|
||||||
static int ble_spp_server_gap_event(struct ble_gap_event *event, void *arg);
|
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;
|
static bool conn_handle_subs[CONFIG_BT_NIMBLE_MAX_CONNECTIONS + 1];
|
||||||
uint16_t connection_handle;
|
static uint16_t 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 */
|
|
||||||
#define BLE_SVC_ANS_UUID16 0x1811
|
|
||||||
|
|
||||||
/* 16 Bit Alert Notification Service Characteristic UUIDs */
|
|
||||||
#define BLE_SVC_ANS_CHR_UUID16_SUP_NEW_ALERT_CAT 0x2a47
|
|
||||||
|
|
||||||
/* 16 Bit SPP Service UUID */
|
|
||||||
#define BLE_SVC_SPP_UUID16 0xABF0
|
|
||||||
|
|
||||||
/* 16 Bit SPP Service Characteristic UUID */
|
|
||||||
#define BLE_SVC_SPP_CHR_UUID16 0xABF1
|
|
||||||
|
|
||||||
void ble_store_config_init(void);
|
void ble_store_config_init(void);
|
||||||
|
|
||||||
@@ -110,7 +96,7 @@ ble_spp_server_advertise(void)
|
|||||||
fields.name_is_complete = 1;
|
fields.name_is_complete = 1;
|
||||||
|
|
||||||
fields.uuids16 = (ble_uuid16_t[]) {
|
fields.uuids16 = (ble_uuid16_t[]) {
|
||||||
BLE_UUID16_INIT(GATT_SVR_SVC_ALERT_UUID)
|
BLE_UUID16_INIT(BLE_SVC_SPP_UUID16)
|
||||||
};
|
};
|
||||||
fields.num_uuids16 = 1;
|
fields.num_uuids16 = 1;
|
||||||
fields.uuids16_is_complete = 1;
|
fields.uuids16_is_complete = 1;
|
||||||
@@ -164,8 +150,6 @@ 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;
|
|
||||||
}
|
}
|
||||||
MODLOG_DFLT(INFO, "\n");
|
MODLOG_DFLT(INFO, "\n");
|
||||||
if (event->connect.status != 0) {
|
if (event->connect.status != 0) {
|
||||||
@@ -179,6 +163,8 @@ ble_spp_server_gap_event(struct ble_gap_event *event, void *arg)
|
|||||||
ble_spp_server_print_conn_desc(&event->disconnect.conn);
|
ble_spp_server_print_conn_desc(&event->disconnect.conn);
|
||||||
MODLOG_DFLT(INFO, "\n");
|
MODLOG_DFLT(INFO, "\n");
|
||||||
|
|
||||||
|
conn_handle_subs[event->disconnect.conn.conn_handle] = false;
|
||||||
|
|
||||||
/* Connection terminated; resume advertising. */
|
/* Connection terminated; resume advertising. */
|
||||||
ble_spp_server_advertise();
|
ble_spp_server_advertise();
|
||||||
return 0;
|
return 0;
|
||||||
@@ -206,6 +192,19 @@ ble_spp_server_gap_event(struct ble_gap_event *event, void *arg)
|
|||||||
event->mtu.value);
|
event->mtu.value);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
case BLE_GAP_EVENT_SUBSCRIBE:
|
||||||
|
MODLOG_DFLT(INFO, "subscribe event; conn_handle=%d attr_handle=%d "
|
||||||
|
"reason=%d prevn=%d curn=%d previ=%d curi=%d\n",
|
||||||
|
event->subscribe.conn_handle,
|
||||||
|
event->subscribe.attr_handle,
|
||||||
|
event->subscribe.reason,
|
||||||
|
event->subscribe.prev_notify,
|
||||||
|
event->subscribe.cur_notify,
|
||||||
|
event->subscribe.prev_indicate,
|
||||||
|
event->subscribe.cur_indicate);
|
||||||
|
conn_handle_subs[event->subscribe.conn_handle] = true;
|
||||||
|
return 0;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -245,7 +244,7 @@ ble_spp_server_on_sync(void)
|
|||||||
|
|
||||||
void ble_spp_server_host_task(void *param)
|
void ble_spp_server_host_task(void *param)
|
||||||
{
|
{
|
||||||
ESP_LOGI(tag, "BLE Host Task Started");
|
MODLOG_DFLT(INFO, "BLE Host Task Started");
|
||||||
/* This function will return only when nimble_port_stop() is executed */
|
/* This function will return only when nimble_port_stop() is executed */
|
||||||
nimble_port_run();
|
nimble_port_run();
|
||||||
|
|
||||||
@@ -257,15 +256,15 @@ static int ble_svc_gatt_handler(uint16_t conn_handle, uint16_t attr_handle,stru
|
|||||||
{
|
{
|
||||||
switch (ctxt->op) {
|
switch (ctxt->op) {
|
||||||
case BLE_GATT_ACCESS_OP_READ_CHR:
|
case BLE_GATT_ACCESS_OP_READ_CHR:
|
||||||
ESP_LOGI(tag, "Callback for read");
|
MODLOG_DFLT(INFO, "Callback for read");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case BLE_GATT_ACCESS_OP_WRITE_CHR:
|
case BLE_GATT_ACCESS_OP_WRITE_CHR:
|
||||||
ESP_LOGI(tag,"Data received in write event,conn_handle = %x,attr_handle = %x",conn_handle,attr_handle);
|
MODLOG_DFLT(INFO, "Data received in write event,conn_handle = %x,attr_handle = %x", conn_handle, attr_handle);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
ESP_LOGI(tag, "\nDefault Callback");
|
MODLOG_DFLT(INFO, "\nDefault Callback");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -273,34 +272,18 @@ static int ble_svc_gatt_handler(uint16_t conn_handle, uint16_t attr_handle,stru
|
|||||||
|
|
||||||
/* Define new custom service */
|
/* Define new custom service */
|
||||||
static const struct ble_gatt_svc_def new_ble_svc_gatt_defs[] = {
|
static const struct ble_gatt_svc_def new_ble_svc_gatt_defs[] = {
|
||||||
{
|
|
||||||
/*** Service: GATT */
|
|
||||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
|
||||||
.uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_UUID16),
|
|
||||||
.characteristics = (struct ble_gatt_chr_def[]) { {
|
|
||||||
/* Support new alert category */
|
|
||||||
.uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_SUP_NEW_ALERT_CAT),
|
|
||||||
.access_cb = ble_svc_gatt_handler,
|
|
||||||
.val_handle = &ble_svc_gatt_read_val_handle,
|
|
||||||
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_INDICATE,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
0, /* No more characteristics */
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
/*** Service: SPP */
|
/*** Service: SPP */
|
||||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||||
.uuid = BLE_UUID16_DECLARE(BLE_SVC_SPP_UUID16),
|
.uuid = BLE_UUID16_DECLARE(BLE_SVC_SPP_UUID16),
|
||||||
.characteristics = (struct ble_gatt_chr_def[]) { {
|
.characteristics = (struct ble_gatt_chr_def[])
|
||||||
|
{ {
|
||||||
/* Support SPP service */
|
/* Support SPP service */
|
||||||
.uuid = BLE_UUID16_DECLARE(BLE_SVC_SPP_CHR_UUID16),
|
.uuid = BLE_UUID16_DECLARE(BLE_SVC_SPP_CHR_UUID16),
|
||||||
.access_cb = ble_svc_gatt_handler,
|
.access_cb = ble_svc_gatt_handler,
|
||||||
.val_handle = &ble_spp_svc_gatt_read_val_handle,
|
.val_handle = &ble_spp_svc_gatt_read_val_handle,
|
||||||
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_INDICATE,
|
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY,
|
||||||
},
|
}, {
|
||||||
{
|
|
||||||
0, /* No more characteristics */
|
0, /* No more characteristics */
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -328,28 +311,36 @@ int gatt_svr_register(void)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ble_server_uart_task(void *pvParameters)
|
||||||
void ble_server_uart_task(void *pvParameters){
|
{
|
||||||
ESP_LOGI(tag,"BLE server UART_task started\n");
|
MODLOG_DFLT(INFO, "BLE server UART_task started\n");
|
||||||
uart_event_t event;
|
uart_event_t event;
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
//Waiting for UART event.
|
//Waiting for UART event.
|
||||||
if (xQueueReceive(spp_common_uart_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
|
if (xQueueReceive(spp_common_uart_queue, (void * )&event, (TickType_t)portMAX_DELAY)) {
|
||||||
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];
|
uint8_t *ntf;
|
||||||
ntf[0] = 90;
|
ntf = (uint8_t *)malloc(sizeof(uint8_t) * event.size);
|
||||||
|
memset(ntf, 0x00, event.size);
|
||||||
|
uart_read_bytes(UART_NUM_0, ntf, event.size, portMAX_DELAY);
|
||||||
|
|
||||||
|
for (int i = 0; i <= CONFIG_BT_NIMBLE_MAX_CONNECTIONS; i++) {
|
||||||
|
/* Check if client has subscribed to notifications */
|
||||||
|
if (conn_handle_subs[i]) {
|
||||||
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(i, ble_spp_svc_gatt_read_val_handle,
|
||||||
|
txom);
|
||||||
if (rc == 0) {
|
if (rc == 0) {
|
||||||
ESP_LOGI(tag,"Notification sent successfully");
|
MODLOG_DFLT(INFO, "Notification sent successfully");
|
||||||
|
} else {
|
||||||
|
MODLOG_DFLT(INFO, "Error in sending notification rc = %d", rc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
ESP_LOGI(tag,"Error in sending notification");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -398,6 +389,11 @@ app_main(void)
|
|||||||
|
|
||||||
nimble_port_init();
|
nimble_port_init();
|
||||||
|
|
||||||
|
/* Initialize connection_handle array */
|
||||||
|
for (int i = 0; i <= CONFIG_BT_NIMBLE_MAX_CONNECTIONS; i++) {
|
||||||
|
conn_handle_subs[i] = false;
|
||||||
|
}
|
||||||
|
|
||||||
/* Initialize uart driver and start uart task */
|
/* Initialize uart driver and start uart task */
|
||||||
ble_spp_uart_init();
|
ble_spp_uart_init();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user