mirror of
https://github.com/espressif/esp-idf.git
synced 2026-10-01 18:50:34 +03:00
fix(nimble): Fix issues found in review for nimble examples
This commit is contained in:
@@ -12,12 +12,20 @@
|
||||
int
|
||||
peer_addr_parse(const char *addr_str, uint8_t addr[PEER_ADDR_VAL_SIZE])
|
||||
{
|
||||
unsigned int tmp[6] = {0};
|
||||
int rc;
|
||||
int i;
|
||||
|
||||
if (addr_str == NULL) {
|
||||
return 0;
|
||||
}
|
||||
return sscanf(addr_str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
|
||||
&addr[5], &addr[4], &addr[3],
|
||||
&addr[2], &addr[1], &addr[0]);
|
||||
rc = sscanf(addr_str, "%x:%x:%x:%x:%x:%x",
|
||||
&tmp[5], &tmp[4], &tmp[3],
|
||||
&tmp[2], &tmp[1], &tmp[0]);
|
||||
for (i = 0; i < PEER_ADDR_VAL_SIZE; i++) {
|
||||
addr[i] = (uint8_t)tmp[i];
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,7 +106,7 @@ print_conn_desc(const struct ble_gap_conn_desc *desc)
|
||||
}
|
||||
|
||||
#if MYNEWT_VAL(BLE_EXT_ADV)
|
||||
void
|
||||
static void
|
||||
print_addr(const void *addr, const char *name)
|
||||
{
|
||||
const uint8_t *u8p;
|
||||
@@ -254,11 +262,9 @@ print_adv_fields(const struct ble_hs_adv_fields *fields)
|
||||
|
||||
if (fields->device_addr_is_present) {
|
||||
MODLOG_DFLT(DEBUG, " device_addr=");
|
||||
u8p = fields->device_addr;
|
||||
MODLOG_DFLT(DEBUG, "%s ", addr_str(u8p));
|
||||
|
||||
u8p += BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN;
|
||||
MODLOG_DFLT(DEBUG, "addr_type %d ", *u8p);
|
||||
u8p = fields->device_addr;
|
||||
MODLOG_DFLT(DEBUG, "%s addr_type %d ", addr_str(u8p),
|
||||
fields->device_addr_type);
|
||||
}
|
||||
|
||||
if (fields->le_role_is_present) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2021-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
@@ -167,7 +167,7 @@ peer_dsc_add(struct peer *peer, uint16_t chr_val_handle,
|
||||
if (prev == NULL) {
|
||||
SLIST_INSERT_HEAD(&chr->dscs, dsc, next);
|
||||
} else {
|
||||
SLIST_NEXT(prev, next) = dsc;
|
||||
SLIST_INSERT_AFTER(prev, dsc, next);
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -196,6 +196,7 @@ peer_disc_dscs(struct peer *peer)
|
||||
peer_dsc_disced, peer);
|
||||
if (rc != 0) {
|
||||
peer_disc_complete(peer, rc);
|
||||
return;
|
||||
}
|
||||
|
||||
peer->disc_prev_chr_val = chr->chr.val_handle;
|
||||
@@ -357,7 +358,7 @@ peer_chr_add(struct peer *peer, uint16_t svc_start_handle,
|
||||
if (prev == NULL) {
|
||||
SLIST_INSERT_HEAD(&svc->chrs, chr, next);
|
||||
} else {
|
||||
SLIST_NEXT(prev, next) = chr;
|
||||
SLIST_INSERT_AFTER(prev, chr, next);
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -407,12 +408,15 @@ peer_disc_chrs(struct peer *peer)
|
||||
struct peer_svc *svc;
|
||||
int rc;
|
||||
|
||||
/* Search through the list of discovered service for the first service that
|
||||
* contains undiscovered characteristics. Then, discover all
|
||||
* characteristics belonging to that service.
|
||||
*/
|
||||
SLIST_FOREACH(svc, &peer->svcs, next) {
|
||||
if (!peer_svc_is_empty(svc) && SLIST_EMPTY(&svc->chrs)) {
|
||||
/* Advance past the last processed service, or start from the beginning. */
|
||||
if (peer->cur_svc == NULL) {
|
||||
svc = SLIST_FIRST(&peer->svcs);
|
||||
} else {
|
||||
svc = SLIST_NEXT(peer->cur_svc, next);
|
||||
}
|
||||
|
||||
for (; svc != NULL; svc = SLIST_NEXT(svc, next)) {
|
||||
if (!peer_svc_is_empty(svc)) {
|
||||
peer->cur_svc = svc;
|
||||
rc = ble_gattc_disc_all_chrs(peer->conn_handle,
|
||||
svc->svc.start_handle,
|
||||
@@ -596,17 +600,24 @@ peer_disc_incs(struct peer *peer)
|
||||
if (peer->cur_svc == NULL) {
|
||||
if (peer->disc_prev_chr_val > 0) {
|
||||
peer_disc_chrs(peer);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* Skip empty services rather than aborting include discovery early. */
|
||||
while (peer->cur_svc != NULL && peer_svc_is_empty(peer->cur_svc)) {
|
||||
peer->cur_svc = SLIST_NEXT(peer->cur_svc, next);
|
||||
}
|
||||
|
||||
svc = peer->cur_svc;
|
||||
if (svc != NULL && !peer_svc_is_empty(svc)) {
|
||||
if (svc != NULL) {
|
||||
rc = ble_gattc_find_inc_svcs(peer->conn_handle,
|
||||
svc->svc.start_handle,
|
||||
svc->svc.end_handle,
|
||||
peer_inc_disced, peer);
|
||||
if (rc != 0) {
|
||||
peer->cur_svc = NULL;
|
||||
peer_disc_chrs(peer);
|
||||
}
|
||||
} else {
|
||||
@@ -684,6 +695,10 @@ peer_svc_find_uuid(const struct peer *peer, const ble_uuid_t *uuid)
|
||||
{
|
||||
const struct peer_svc *svc;
|
||||
|
||||
if (peer == NULL || uuid == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SLIST_FOREACH(svc, &peer->svcs, next) {
|
||||
if (ble_uuid_cmp(&svc->svc.uuid.u, uuid) == 0) {
|
||||
return svc;
|
||||
@@ -700,6 +715,10 @@ peer_chr_find_uuid(const struct peer *peer, const ble_uuid_t *svc_uuid,
|
||||
const struct peer_svc *svc;
|
||||
const struct peer_chr *chr;
|
||||
|
||||
if (chr_uuid == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
svc = peer_svc_find_uuid(peer, svc_uuid);
|
||||
if (svc == NULL) {
|
||||
return NULL;
|
||||
@@ -721,6 +740,10 @@ peer_dsc_find_uuid(const struct peer *peer, const ble_uuid_t *svc_uuid,
|
||||
const struct peer_chr *chr;
|
||||
const struct peer_dsc *dsc;
|
||||
|
||||
if (dsc_uuid == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
chr = peer_chr_find_uuid(peer, svc_uuid, chr_uuid);
|
||||
if (chr == NULL) {
|
||||
return NULL;
|
||||
@@ -810,6 +833,7 @@ peer_svc_disced(uint16_t conn_handle, const struct ble_gatt_error *error,
|
||||
#else
|
||||
/* All services discovered; start discovering characteristics. */
|
||||
if (peer->disc_prev_chr_val > 0) {
|
||||
peer->cur_svc = NULL;
|
||||
peer_disc_chrs(peer);
|
||||
}
|
||||
#endif
|
||||
@@ -979,6 +1003,7 @@ peer_free_mem(void)
|
||||
{
|
||||
free(peer_mem);
|
||||
peer_mem = NULL;
|
||||
SLIST_INIT(&peers);
|
||||
|
||||
free(peer_svc_mem);
|
||||
peer_svc_mem = NULL;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2021-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
@@ -20,23 +20,27 @@
|
||||
|
||||
static TaskHandle_t cli_task;
|
||||
static QueueHandle_t cli_handle;
|
||||
static int stop;
|
||||
static volatile int stop;
|
||||
|
||||
static int enter_passkey_handler(int argc, char *argv[])
|
||||
{
|
||||
int key;
|
||||
char pkey[8];
|
||||
char pkey[8] = {0};
|
||||
int num;
|
||||
|
||||
if (argc != 2) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (cli_handle == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
sscanf(argv[1], "%7s", pkey);
|
||||
ESP_LOGI("You entered", "%s %s", argv[0], argv[1]);
|
||||
num = pkey[0];
|
||||
|
||||
if (isalpha(num)) {
|
||||
if (isalpha((unsigned char)num)) {
|
||||
if ((strcasecmp(pkey, "Y") == 0) || (strcasecmp(pkey, "Yes") == 0)) {
|
||||
key = 1;
|
||||
xQueueSend(cli_handle, &key, 0);
|
||||
@@ -56,6 +60,9 @@ static int enter_passkey_handler(int argc, char *argv[])
|
||||
|
||||
int scli_receive_key(int *console_key)
|
||||
{
|
||||
if (cli_handle == NULL) {
|
||||
return pdFALSE;
|
||||
}
|
||||
return xQueueReceive(cli_handle, console_key, BLE_RX_TIMEOUT);
|
||||
}
|
||||
|
||||
@@ -108,10 +115,8 @@ static void scli_task(void *arg)
|
||||
}
|
||||
}
|
||||
if (event.type == UART_DATA) {
|
||||
while (uart_read_bytes(uart_num, (uint8_t *) &linebuf[i], 1, 0)) {
|
||||
if (i >= sizeof(linebuf) - 1) {
|
||||
break;
|
||||
}
|
||||
while (i < sizeof(linebuf) - 1 &&
|
||||
uart_read_bytes(uart_num, (uint8_t *) &linebuf[i], 1, 0)) {
|
||||
if (linebuf[i] == '\r') {
|
||||
uart_write_bytes(uart_num, "\r\n", 2);
|
||||
} else {
|
||||
@@ -143,12 +148,14 @@ int scli_init(void)
|
||||
/* Register CLI "key <value>" to accept input from user during pairing */
|
||||
ble_register_cli();
|
||||
|
||||
xTaskCreate(scli_task, "scli_cli", 4096, (void *) 0, 3, &cli_task);
|
||||
if (cli_task == NULL) {
|
||||
cli_handle = xQueueCreate(1, sizeof(int));
|
||||
if (cli_handle == NULL) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
cli_handle = xQueueCreate( 1, sizeof(int) );
|
||||
if (cli_handle == NULL) {
|
||||
xTaskCreate(scli_task, "scli_cli", 4096, (void *) 0, 3, &cli_task);
|
||||
if (cli_task == NULL) {
|
||||
vQueueDelete(cli_handle);
|
||||
cli_handle = NULL;
|
||||
return ESP_FAIL;
|
||||
}
|
||||
return ESP_OK;
|
||||
|
||||
Reference in New Issue
Block a user