Select one of the symbols to view example projects that use it.
 
Outline
#include <stdio.h>
#include <ctype.h>
#include "esp_log.h"
#include <string.h>
#include <esp_log.h>
#include <esp_console.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include <driver/uart.h>
#include "esp_peripheral.h"
#define BLE_RX_TIMEOUT
cli_task
cli_handle
stop
enter_passkey_handler(int, char **)
scli_receive_key(int *)
cmds
ble_register_cli()
scli_task(void *)
scli_init()
Files
loading...
SourceVuESP-IDF Framework and ExamplesESP-IDFexamples/bluetooth/nimble/common/nimble_peripheral_utils/scli.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 *//* ... */ #include <stdio.h> #include <ctype.h> #include "esp_log.h" #include <string.h> #include <esp_log.h> #include <esp_console.h> #include <freertos/FreeRTOS.h> #include <freertos/task.h> #include <freertos/queue.h> #include <driver/uart.h> #include "esp_peripheral.h"11 includes #define BLE_RX_TIMEOUT (30000 / portTICK_PERIOD_MS) static TaskHandle_t cli_task; static QueueHandle_t cli_handle; static int stop; static int enter_passkey_handler(int argc, char *argv[]) { int key; char pkey[8]; int num; if (argc != 2) { return -1; }{...} sscanf(argv[1], "%s", pkey); ESP_LOGI("You entered", "%s %s", argv[0], argv[1]); num = pkey[0]; if (isalpha(num)) { if ((strcasecmp(pkey, "Y") == 0) || (strcasecmp(pkey, "Yes") == 0)) { key = 1; xQueueSend(cli_handle, &key, 0); }{...} else { key = 0; xQueueSend(cli_handle, &key, 0); }{...} }{...} else { sscanf(pkey, "%d", &key); xQueueSend(cli_handle, &key, 0); }{...} return 0; }{ ... } int scli_receive_key(int *console_key) { return xQueueReceive(cli_handle, console_key, BLE_RX_TIMEOUT); }{ ... } static esp_console_cmd_t cmds[] = { { .command = "key", .help = "", .func = enter_passkey_handler, }{...}, }{...}; static int ble_register_cli(void) { int cmds_num = sizeof(cmds) / sizeof(esp_console_cmd_t); int i; for (i = 0; i < cmds_num; i++) { esp_console_cmd_register(&cmds[i]); }{...} return 0; }{ ... } static void scli_task(void *arg) { int uart_num = (int) arg; uint8_t linebuf[256]; int i, cmd_ret; esp_err_t ret; QueueHandle_t uart_queue; uart_event_t event; uart_driver_install(uart_num, 256, 0, 8, &uart_queue, 0); /* Initialize the console */ esp_console_config_t console_config = { .max_cmdline_args = 8, .max_cmdline_length = 256, }{...}; esp_console_init(&console_config); while (!stop) { i = 0; memset(linebuf, 0, sizeof(linebuf)); do { ret = xQueueReceive(uart_queue, (void * )&event, (TickType_t)portMAX_DELAY); if (ret != pdPASS) { if (stop == 1) { break; }{...} else { continue; }{...} }{...} if (event.type == UART_DATA) { while (uart_read_bytes(uart_num, (uint8_t *) &linebuf[i], 1, 0)) { if (linebuf[i] == '\r') { uart_write_bytes(uart_num, "\r\n", 2); }{...} else { uart_write_bytes(uart_num, (char *) &linebuf[i], 1); }{...} i++; }{...} }{...} }{...} while ((i < 255) && linebuf[i - 1] != '\r'); if (stop) { break; }{...} /* Remove the truncating \r\n */ linebuf[strlen((char *)linebuf) - 1] = '\0'; ret = esp_console_run((char *) linebuf, &cmd_ret); if (ret < 0) { break; }{...} }{...} vTaskDelete(NULL); }{ ... } 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) { return ESP_FAIL; }{...} cli_handle = xQueueCreate( 1, sizeof(int) ); if (cli_handle == NULL) { return ESP_FAIL; }{...} return ESP_OK; }{ ... }
Details