mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
Merge branch 'bugfix/spp_clear_conn_array_v5.0' into 'release/v5.0'
Bluetooth/NimBLE : Fixed clearing connection_handle[] after disconnect event (v5.0) See merge request espressif/esp-idf!22272
This commit is contained in:
commit
8a83241c49
@ -19,12 +19,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
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
@ -14,24 +14,17 @@
|
|||||||
#include "console/console.h"
|
#include "console/console.h"
|
||||||
#include "services/gap/ble_svc_gap.h"
|
#include "services/gap/ble_svc_gap.h"
|
||||||
#include "ble_spp_client.h"
|
#include "ble_spp_client.h"
|
||||||
|
|
||||||
#include "driver/uart.h"
|
#include "driver/uart.h"
|
||||||
|
|
||||||
|
#define PEER_ADDR_VAL_SIZE 6
|
||||||
|
|
||||||
static const char *tag = "NimBLE_SPP_BLE_CENT";
|
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);
|
||||||
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];
|
static ble_addr_t connected_addr[CONFIG_BT_NIMBLE_MAX_CONNECTIONS + 1];
|
||||||
|
|
||||||
/* 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_handle(const struct peer *peer)
|
ble_spp_client_set_handle(const struct peer *peer)
|
||||||
@ -40,7 +33,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 - 1] = chr->chr.val_handle;
|
attribute_handle[peer->conn_handle] = chr->chr.val_handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -125,8 +118,8 @@ ble_spp_client_should_connect(const struct ble_gap_disc_desc *disc)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* Check if device is already connected or not */
|
/* Check if device is already connected or not */
|
||||||
for ( i = 0; i < CONFIG_BT_NIMBLE_MAX_CONNECTIONS; i++) {
|
for ( i = 0; i <= CONFIG_BT_NIMBLE_MAX_CONNECTIONS; i++) {
|
||||||
if (memcmp(&connected_addr[i].val,disc->addr.val, sizeof(disc->addr.val)) == 0) {
|
if (memcmp(&connected_addr[i].val, disc->addr.val, PEER_ADDR_VAL_SIZE) == 0) {
|
||||||
MODLOG_DFLT(DEBUG, "Device already connected");
|
MODLOG_DFLT(DEBUG, "Device already connected");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -144,15 +137,14 @@ ble_spp_client_should_connect(const struct ble_gap_disc_desc *disc)
|
|||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The device has to advertise support for the Alert Notification
|
/* The device has to advertise support for the SPP
|
||||||
* service (0x1811).
|
* service (0xABF0).
|
||||||
*/
|
*/
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,8 +235,8 @@ ble_spp_client_gap_event(struct ble_gap_event *event, void *arg)
|
|||||||
MODLOG_DFLT(INFO, "Connection established ");
|
MODLOG_DFLT(INFO, "Connection established ");
|
||||||
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,
|
memcpy(&connected_addr[event->connect.conn_handle].val, desc.peer_id_addr.val,
|
||||||
sizeof(desc.peer_id_addr.val));
|
PEER_ADDR_VAL_SIZE);
|
||||||
print_conn_desc(&desc);
|
print_conn_desc(&desc);
|
||||||
MODLOG_DFLT(INFO, "\n");
|
MODLOG_DFLT(INFO, "\n");
|
||||||
|
|
||||||
@ -278,6 +270,7 @@ ble_spp_client_gap_event(struct ble_gap_event *event, void *arg)
|
|||||||
MODLOG_DFLT(INFO, "\n");
|
MODLOG_DFLT(INFO, "\n");
|
||||||
|
|
||||||
/* Forget about peer. */
|
/* Forget about peer. */
|
||||||
|
memset(&connected_addr[event->disconnect.conn.conn_handle].val, 0, PEER_ADDR_VAL_SIZE);
|
||||||
attribute_handle[event->disconnect.conn.conn_handle] = 0;
|
attribute_handle[event->disconnect.conn.conn_handle] = 0;
|
||||||
peer_delete(event->disconnect.conn.conn_handle);
|
peer_delete(event->disconnect.conn.conn_handle);
|
||||||
|
|
||||||
@ -367,13 +360,12 @@ 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 = 0; 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+1, attribute_handle[i],temp, event.size,NULL,NULL);
|
rc = ble_gattc_write_flat(i, 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!");
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
ESP_LOGI(tag, "Error in writing characteristic rc=%d", rc);
|
ESP_LOGI(tag, "Error in writing characteristic rc=%d", rc);
|
||||||
}
|
}
|
||||||
vTaskDelay(10);
|
vTaskDelay(10);
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
set(srcs "main.c"
|
set(srcs "main.c")
|
||||||
"gatt_svr.c")
|
|
||||||
|
|
||||||
idf_component_register(SRCS "${srcs}"
|
idf_component_register(SRCS "${srcs}"
|
||||||
INCLUDE_DIRS ".")
|
INCLUDE_DIRS ".")
|
||||||
|
@ -15,20 +15,15 @@
|
|||||||
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);
|
|
||||||
int new_gatt_svr_init(void);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,207 +0,0 @@
|
|||||||
/*
|
|
||||||
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "host/ble_hs.h"
|
|
||||||
#include "host/ble_uuid.h"
|
|
||||||
#include "services/gap/ble_svc_gap.h"
|
|
||||||
#include "services/gatt/ble_svc_gatt.h"
|
|
||||||
#include "ble_spp_server.h"
|
|
||||||
|
|
||||||
/* 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
|
|
||||||
#define BLE_SVC_ANS_CHR_UUID16_NEW_ALERT 0x2a46
|
|
||||||
#define BLE_SVC_ANS_CHR_UUID16_SUP_UNR_ALERT_CAT 0x2a48
|
|
||||||
#define BLE_SVC_ANS_CHR_UUID16_UNR_ALERT_STAT 0x2a45
|
|
||||||
#define BLE_SVC_ANS_CHR_UUID16_ALERT_NOT_CTRL_PT 0x2a44
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The vendor specific security test service consists of two characteristics:
|
|
||||||
* o random-number-generator: generates a random 32-bit number each time
|
|
||||||
* it is read. This characteristic can only be read over an encrypted
|
|
||||||
* connection.
|
|
||||||
* o static-value: a single-byte characteristic that can always be read,
|
|
||||||
* but can only be written over an encrypted connection.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* 59462f12-9543-9999-12c8-58b459a2712d */
|
|
||||||
static const ble_uuid128_t gatt_svr_svc_sec_test_uuid =
|
|
||||||
BLE_UUID128_INIT(0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12,
|
|
||||||
0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59);
|
|
||||||
|
|
||||||
/* 5c3a659e-897e-45e1-b016-007107c96df6 */
|
|
||||||
static const ble_uuid128_t gatt_svr_chr_sec_test_rand_uuid =
|
|
||||||
BLE_UUID128_INIT(0xf6, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
|
|
||||||
0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c);
|
|
||||||
|
|
||||||
/* 5c3a659e-897e-45e1-b016-007107c96df7 */
|
|
||||||
static const ble_uuid128_t gatt_svr_chr_sec_test_static_uuid =
|
|
||||||
BLE_UUID128_INIT(0xf7, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
|
|
||||||
0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c);
|
|
||||||
|
|
||||||
static uint8_t gatt_svr_sec_test_static_val;
|
|
||||||
|
|
||||||
static int
|
|
||||||
gatt_svr_chr_access_sec_test(uint16_t conn_handle, uint16_t attr_handle,
|
|
||||||
struct ble_gatt_access_ctxt *ctxt,
|
|
||||||
void *arg);
|
|
||||||
|
|
||||||
struct ble_gatt_svc_def gatt_svr_svcs[] = {
|
|
||||||
{
|
|
||||||
/*** Service: Security test. */
|
|
||||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
|
||||||
.uuid = &gatt_svr_svc_sec_test_uuid.u,
|
|
||||||
.characteristics = (struct ble_gatt_chr_def[])
|
|
||||||
{ {
|
|
||||||
/*** Characteristic: Random number generator. */
|
|
||||||
.uuid = &gatt_svr_chr_sec_test_rand_uuid.u,
|
|
||||||
.access_cb = gatt_svr_chr_access_sec_test,
|
|
||||||
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC,
|
|
||||||
}, {
|
|
||||||
/*** Characteristic: Static value. */
|
|
||||||
.uuid = &gatt_svr_chr_sec_test_static_uuid.u,
|
|
||||||
.access_cb = gatt_svr_chr_access_sec_test,
|
|
||||||
.flags = BLE_GATT_CHR_F_READ |
|
|
||||||
BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_ENC,
|
|
||||||
}, {
|
|
||||||
0, /* No more characteristics in this service. */
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
0, /* No more services. */
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
static int
|
|
||||||
gatt_svr_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len,
|
|
||||||
void *dst, uint16_t *len)
|
|
||||||
{
|
|
||||||
uint16_t om_len;
|
|
||||||
int rc;
|
|
||||||
|
|
||||||
om_len = OS_MBUF_PKTLEN(om);
|
|
||||||
if (om_len < min_len || om_len > max_len) {
|
|
||||||
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = ble_hs_mbuf_to_flat(om, dst, max_len, len);
|
|
||||||
if (rc != 0) {
|
|
||||||
return BLE_ATT_ERR_UNLIKELY;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
gatt_svr_chr_access_sec_test(uint16_t conn_handle, uint16_t attr_handle,
|
|
||||||
struct ble_gatt_access_ctxt *ctxt,
|
|
||||||
void *arg)
|
|
||||||
{
|
|
||||||
const ble_uuid_t *uuid;
|
|
||||||
int rand_num;
|
|
||||||
int rc;
|
|
||||||
|
|
||||||
uuid = ctxt->chr->uuid;
|
|
||||||
|
|
||||||
/* Determine which characteristic is being accessed by examining its
|
|
||||||
* 128-bit UUID.
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (ble_uuid_cmp(uuid, &gatt_svr_chr_sec_test_rand_uuid.u) == 0) {
|
|
||||||
assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
|
|
||||||
|
|
||||||
/* Respond with a 32-bit random number. */
|
|
||||||
rand_num = rand();
|
|
||||||
rc = os_mbuf_append(ctxt->om, &rand_num, sizeof rand_num);
|
|
||||||
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ble_uuid_cmp(uuid, &gatt_svr_chr_sec_test_static_uuid.u) == 0) {
|
|
||||||
switch (ctxt->op) {
|
|
||||||
case BLE_GATT_ACCESS_OP_READ_CHR:
|
|
||||||
rc = os_mbuf_append(ctxt->om, &gatt_svr_sec_test_static_val,
|
|
||||||
sizeof gatt_svr_sec_test_static_val);
|
|
||||||
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
|
||||||
|
|
||||||
case BLE_GATT_ACCESS_OP_WRITE_CHR:
|
|
||||||
rc = gatt_svr_chr_write(ctxt->om,
|
|
||||||
sizeof gatt_svr_sec_test_static_val,
|
|
||||||
sizeof gatt_svr_sec_test_static_val,
|
|
||||||
&gatt_svr_sec_test_static_val, NULL);
|
|
||||||
return rc;
|
|
||||||
|
|
||||||
default:
|
|
||||||
assert(0);
|
|
||||||
return BLE_ATT_ERR_UNLIKELY;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Unknown characteristic; the nimble stack should not have called this
|
|
||||||
* function.
|
|
||||||
*/
|
|
||||||
assert(0);
|
|
||||||
return BLE_ATT_ERR_UNLIKELY;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
|
|
||||||
{
|
|
||||||
char buf[BLE_UUID_STR_LEN];
|
|
||||||
|
|
||||||
switch (ctxt->op) {
|
|
||||||
case BLE_GATT_REGISTER_OP_SVC:
|
|
||||||
MODLOG_DFLT(DEBUG, "registered service %s with handle=%d\n",
|
|
||||||
ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf),
|
|
||||||
ctxt->svc.handle);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case BLE_GATT_REGISTER_OP_CHR:
|
|
||||||
MODLOG_DFLT(DEBUG, "registering characteristic %s with "
|
|
||||||
"def_handle=%d val_handle=%d\n",
|
|
||||||
ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf),
|
|
||||||
ctxt->chr.def_handle,
|
|
||||||
ctxt->chr.val_handle);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case BLE_GATT_REGISTER_OP_DSC:
|
|
||||||
MODLOG_DFLT(DEBUG, "registering descriptor %s with handle=%d\n",
|
|
||||||
ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf),
|
|
||||||
ctxt->dsc.handle);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
assert(0);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
new_gatt_svr_init(void)
|
|
||||||
{
|
|
||||||
int rc;
|
|
||||||
|
|
||||||
ble_svc_gap_init();
|
|
||||||
ble_svc_gatt_init();
|
|
||||||
|
|
||||||
rc = ble_gatts_count_cfg(gatt_svr_svcs);
|
|
||||||
if (rc != 0) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = ble_gatts_add_svcs(gatt_svr_svcs);
|
|
||||||
if (rc != 0) {
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
@ -13,6 +13,7 @@
|
|||||||
#include "host/util/util.h"
|
#include "host/util/util.h"
|
||||||
#include "console/console.h"
|
#include "console/console.h"
|
||||||
#include "services/gap/ble_svc_gap.h"
|
#include "services/gap/ble_svc_gap.h"
|
||||||
|
#include "services/gatt/ble_svc_gatt.h"
|
||||||
#include "ble_spp_server.h"
|
#include "ble_spp_server.h"
|
||||||
#include "driver/uart.h"
|
#include "driver/uart.h"
|
||||||
|
|
||||||
@ -21,20 +22,8 @@ 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;
|
||||||
uint16_t connection_handle[CONFIG_BT_NIMBLE_MAX_CONNECTIONS];
|
int connection_handle[CONFIG_BT_NIMBLE_MAX_CONNECTIONS + 1];
|
||||||
static uint16_t ble_svc_gatt_read_val_handle,ble_spp_svc_gatt_read_val_handle;
|
static uint16_t 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);
|
||||||
|
|
||||||
@ -108,7 +97,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;
|
||||||
@ -162,7 +151,7 @@ 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);
|
||||||
connection_handle[event->connect.conn_handle - 1] = event->connect.conn_handle;
|
connection_handle[event->connect.conn_handle] = event->connect.conn_handle;
|
||||||
}
|
}
|
||||||
MODLOG_DFLT(INFO, "\n");
|
MODLOG_DFLT(INFO, "\n");
|
||||||
if (event->connect.status != 0 || CONFIG_BT_NIMBLE_MAX_CONNECTIONS > 1) {
|
if (event->connect.status != 0 || CONFIG_BT_NIMBLE_MAX_CONNECTIONS > 1) {
|
||||||
@ -176,6 +165,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");
|
||||||
|
|
||||||
|
connection_handle[event->disconnect.conn.conn_handle] = -1;
|
||||||
|
|
||||||
/* Connection terminated; resume advertising. */
|
/* Connection terminated; resume advertising. */
|
||||||
ble_spp_server_advertise();
|
ble_spp_server_advertise();
|
||||||
return 0;
|
return 0;
|
||||||
@ -270,34 +261,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 | BLE_GATT_CHR_F_INDICATE,
|
||||||
},
|
}, {
|
||||||
{
|
|
||||||
0, /* No more characteristics */
|
0, /* No more characteristics */
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -307,9 +282,43 @@ static const struct ble_gatt_svc_def new_ble_svc_gatt_defs[] = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
int gatt_svr_register(void)
|
static void
|
||||||
|
gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
|
||||||
|
{
|
||||||
|
char buf[BLE_UUID_STR_LEN];
|
||||||
|
|
||||||
|
switch (ctxt->op) {
|
||||||
|
case BLE_GATT_REGISTER_OP_SVC:
|
||||||
|
MODLOG_DFLT(DEBUG, "registered service %s with handle=%d\n",
|
||||||
|
ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf),
|
||||||
|
ctxt->svc.handle);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BLE_GATT_REGISTER_OP_CHR:
|
||||||
|
MODLOG_DFLT(DEBUG, "registering characteristic %s with "
|
||||||
|
"def_handle=%d val_handle=%d\n",
|
||||||
|
ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf),
|
||||||
|
ctxt->chr.def_handle,
|
||||||
|
ctxt->chr.val_handle);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BLE_GATT_REGISTER_OP_DSC:
|
||||||
|
MODLOG_DFLT(DEBUG, "registering descriptor %s with handle=%d\n",
|
||||||
|
ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf),
|
||||||
|
ctxt->dsc.handle);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
assert(0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int gatt_svr_init(void)
|
||||||
{
|
{
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
|
ble_svc_gap_init();
|
||||||
|
ble_svc_gatt_init();
|
||||||
|
|
||||||
rc = ble_gatts_count_cfg(new_ble_svc_gatt_defs);
|
rc = ble_gatts_count_cfg(new_ble_svc_gatt_defs);
|
||||||
|
|
||||||
@ -326,7 +335,8 @@ int gatt_svr_register(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ble_server_uart_task(void *pvParameters){
|
void ble_server_uart_task(void *pvParameters)
|
||||||
|
{
|
||||||
ESP_LOGI(tag, "BLE server UART_task started\n");
|
ESP_LOGI(tag, "BLE server UART_task started\n");
|
||||||
uart_event_t event;
|
uart_event_t event;
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
@ -339,8 +349,8 @@ void ble_server_uart_task(void *pvParameters){
|
|||||||
if (event.size) {
|
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++) {
|
for ( int i = 0; i <= CONFIG_BT_NIMBLE_MAX_CONNECTIONS; i++) {
|
||||||
if (connection_handle[i] != 0) {
|
if (connection_handle[i] != -1) {
|
||||||
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_gatts_notify_custom(connection_handle[i],
|
rc = ble_gatts_notify_custom(connection_handle[i],
|
||||||
@ -348,8 +358,7 @@ void ble_server_uart_task(void *pvParameters){
|
|||||||
txom);
|
txom);
|
||||||
if ( rc == 0 ) {
|
if ( rc == 0 ) {
|
||||||
ESP_LOGI(tag, "Notification sent successfully");
|
ESP_LOGI(tag, "Notification sent successfully");
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
ESP_LOGI(tag, "Error in sending notification rc = %d", rc);
|
ESP_LOGI(tag, "Error in sending notification rc = %d", rc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -399,6 +408,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++) {
|
||||||
|
connection_handle[i] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
/* Initialize uart driver and start uart task */
|
/* Initialize uart driver and start uart task */
|
||||||
ble_spp_uart_init();
|
ble_spp_uart_init();
|
||||||
|
|
||||||
@ -425,12 +439,8 @@ app_main(void)
|
|||||||
ble_hs_cfg.sm_their_key_dist = 1;
|
ble_hs_cfg.sm_their_key_dist = 1;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
rc = new_gatt_svr_init();
|
|
||||||
assert(rc == 0);
|
|
||||||
|
|
||||||
/* Register custom service */
|
/* Register custom service */
|
||||||
rc = gatt_svr_register();
|
rc = gatt_svr_init();
|
||||||
assert(rc == 0);
|
assert(rc == 0);
|
||||||
|
|
||||||
/* Set the default device name. */
|
/* Set the default device name. */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user