Skip to content

Commit

Permalink
Merge branch 'bugfix/spp_notify_v4.4' into 'release/v4.4'
Browse files Browse the repository at this point in the history
NimBLE: Removed indicate flag from gatt db and added subscription case in spp_server example (v4.4)

See merge request espressif/esp-idf!23536
  • Loading branch information
jack0c committed Jun 13, 2023
2 parents abb204f + a2eba93 commit 4c17059
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ struct ble_hs_cfg;
union ble_store_value;
union ble_store_key;

#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
/* 16 Bit SPP Service UUID */
#define GATT_SPP_SVC_UUID 0xABF0

/* 16 Bit SPP Service Characteristic UUID */
#define GATT_SPP_CHR_UUID 0xABF1

/** Misc. */
void print_bytes(const uint8_t *bytes, int len);
Expand Down
10 changes: 1 addition & 9 deletions examples/bluetooth/nimble/ble_spp/spp_client/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ void ble_store_config_init(void);
static bool is_connect = false;
uint16_t connection_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
ble_spp_client_set_handles(const struct peer *peer){
Expand Down Expand Up @@ -138,7 +130,7 @@ ble_spp_client_should_connect(const struct ble_gap_disc_desc *disc)
* service (0x1811).
*/
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;
}
}
Expand Down
18 changes: 6 additions & 12 deletions examples/bluetooth/nimble/ble_spp/spp_server/main/ble_spp_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,18 @@
extern "C" {
#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_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);
int new_gatt_svr_init(void);

/* Console */
int scli_init(void);
int scli_receive_key(int *key);

/** Misc. */
void print_bytes(const uint8_t *bytes, int len);
void print_addr(const void *addr);
Expand Down
196 changes: 96 additions & 100 deletions examples/bluetooth/nimble/ble_spp/spp_server/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,12 @@
#include "ble_spp_server.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 uint8_t own_addr_type;
int gatt_svr_register(void);
QueueHandle_t spp_common_uart_queue = NULL;
static bool is_connect = false;
uint16_t connection_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
static bool conn_handle_subs[CONFIG_BT_NIMBLE_MAX_CONNECTIONS + 1];
static uint16_t ble_spp_svc_gatt_read_val_handle;

void ble_store_config_init(void);

Expand Down Expand Up @@ -110,7 +96,7 @@ ble_spp_server_advertise(void)
fields.name_is_complete = 1;

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.uuids16_is_complete = 1;
Expand Down Expand Up @@ -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);
assert(rc == 0);
ble_spp_server_print_conn_desc(&desc);
is_connect=true;
connection_handle = event->connect.conn_handle;
}
MODLOG_DFLT(INFO, "\n");
if (event->connect.status != 0) {
Expand All @@ -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);
MODLOG_DFLT(INFO, "\n");

conn_handle_subs[event->disconnect.conn.conn_handle] = false;

/* Connection terminated; resume advertising. */
ble_spp_server_advertise();
return 0;
Expand Down Expand Up @@ -206,6 +192,19 @@ ble_spp_server_gap_event(struct ble_gap_event *event, void *arg)
event->mtu.value);
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:
return 0;
}
Expand Down Expand Up @@ -245,7 +244,7 @@ ble_spp_server_on_sync(void)

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 */
nimble_port_run();

Expand All @@ -255,59 +254,43 @@ void ble_spp_server_host_task(void *param)
/* Callback function for custom service */
static int ble_svc_gatt_handler(uint16_t conn_handle, uint16_t attr_handle,struct ble_gatt_access_ctxt *ctxt, void *arg)
{
switch(ctxt->op){
case BLE_GATT_ACCESS_OP_READ_CHR:
ESP_LOGI(tag, "Callback for read");
break;

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);
break;

default:
ESP_LOGI(tag, "\nDefault Callback");
break;
}
return 0;
switch (ctxt->op) {
case BLE_GATT_ACCESS_OP_READ_CHR:
MODLOG_DFLT(INFO, "Callback for read");
break;

case BLE_GATT_ACCESS_OP_WRITE_CHR:
MODLOG_DFLT(INFO, "Data received in write event,conn_handle = %x,attr_handle = %x", conn_handle, attr_handle);
break;

default:
MODLOG_DFLT(INFO, "\nDefault Callback");
break;
}
return 0;
}

/* Define new custom service */
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 */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID16_DECLARE(BLE_SVC_SPP_UUID16),
.characteristics = (struct ble_gatt_chr_def[]) { {
/* Support SPP service */
.uuid = BLE_UUID16_DECLARE(BLE_SVC_SPP_CHR_UUID16),
.access_cb = ble_svc_gatt_handler,
.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,
},
{
0, /* No more characteristics */
}
},
},
{
0, /* No more services. */
},
{
/*** Service: SPP */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID16_DECLARE(BLE_SVC_SPP_UUID16),
.characteristics = (struct ble_gatt_chr_def[])
{ {
/* Support SPP service */
.uuid = BLE_UUID16_DECLARE(BLE_SVC_SPP_CHR_UUID16),
.access_cb = ble_svc_gatt_handler,
.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,
}, {
0, /* No more characteristics */
}
},
},
{
0, /* No more services. */
},
};

int gatt_svr_register(void)
Expand All @@ -328,37 +311,45 @@ int gatt_svr_register(void)
return 0;
}


void ble_server_uart_task(void *pvParameters){
ESP_LOGI(tag,"BLE server UART_task started\n");
uart_event_t event;
int rc=0;
for (;;) {
//Waiting for UART event.
if (xQueueReceive(spp_common_uart_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
switch (event.type) {
//Event of UART receving data
case UART_DATA:
if (event.size && (is_connect == true)) {
static uint8_t ntf[1];
ntf[0] = 90;
struct os_mbuf *txom;
txom = ble_hs_mbuf_from_flat(ntf, sizeof(ntf));
rc = ble_gattc_notify_custom(connection_handle,ble_spp_svc_gatt_read_val_handle,txom);
if( rc == 0){
ESP_LOGI(tag,"Notification sent successfully");
}
else {
ESP_LOGI(tag,"Error in sending notification");
}
}
break;
default:
break;
}
}
}
vTaskDelete(NULL);
void ble_server_uart_task(void *pvParameters)
{
MODLOG_DFLT(INFO, "BLE server UART_task started\n");
uart_event_t event;
int rc = 0;
for (;;) {
//Waiting for UART event.
if (xQueueReceive(spp_common_uart_queue, (void * )&event, (TickType_t)portMAX_DELAY)) {
switch (event.type) {
//Event of UART receving data
case UART_DATA:
if (event.size) {
uint8_t *ntf;
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;
txom = ble_hs_mbuf_from_flat(ntf, sizeof(ntf));
rc = ble_gattc_notify_custom(i, ble_spp_svc_gatt_read_val_handle,
txom);
if (rc == 0) {
MODLOG_DFLT(INFO, "Notification sent successfully");
} else {
MODLOG_DFLT(INFO, "Error in sending notification rc = %d", rc);
}
}
}
}
break;
default:
break;
}
}
}
vTaskDelete(NULL);
}
static void ble_spp_uart_init(void)
{
Expand Down Expand Up @@ -398,6 +389,11 @@ app_main(void)

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 */
ble_spp_uart_init();

Expand Down

0 comments on commit 4c17059

Please sign in to comment.