Skip to content

Commit 8a25d02

Browse files
authored
Merge pull request #1 from SimLinkModule/example_blehr
Example blehr
2 parents ad04b6a + 68fbaa2 commit 8a25d02

File tree

25 files changed

+2676
-259
lines changed

25 files changed

+2676
-259
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
build/
2-
sdkconfig
3-
sdkconfig.old
2+
sdkconfig

.vscode/settings.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
"idf.port": "/dev/ttyUSB0",
44
"idf.flashType": "UART",
55
"files.associations": {
6-
"regex": "cpp"
6+
"regex": "cpp",
7+
"random": "c",
8+
"array": "c",
9+
"string": "c",
10+
"string_view": "c",
11+
"ble_svc_gap.h": "c",
12+
"console.h": "c"
713
}
814
}

CMakeLists.txt

100644100755
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
# For more information about build system see
2-
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
3-
# The following five lines of boilerplate have to be in your project's
1+
# The following lines of boilerplate have to be in your project's
42
# CMakeLists in this exact order for cmake to work correctly
53
cmake_minimum_required(VERSION 3.5)
64

75
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
8-
project(SimLinkSoftware)
6+
project(blehr)

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#
2+
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
3+
# project subdirectory.
4+
#
5+
6+
PROJECT_NAME := blehr
7+
8+
include $(IDF_PATH)/make/project.mk
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
idf_component_register(SRCS "misc.c" "scli.c"
2+
INCLUDE_DIRS "."
3+
PRIV_REQUIRES bt console driver)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
6+
7+
#ifndef H_ESP_PERIPHERAL_
8+
#define H_ESP_PERIPHERAL_
9+
10+
#include <stdbool.h>
11+
#include "nimble/ble.h"
12+
#include "modlog/modlog.h"
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
/* Console */
18+
int scli_init(void);
19+
int scli_receive_key(int *key);
20+
21+
/** Misc. */
22+
void print_bytes(const uint8_t *bytes, int len);
23+
void print_addr(const void *addr);
24+
25+
#ifdef __cplusplus
26+
}
27+
#endif
28+
29+
#endif
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
6+
7+
#include "esp_peripheral.h"
8+
9+
/**
10+
* Utility function to log an array of bytes.
11+
*/
12+
void
13+
print_bytes(const uint8_t *bytes, int len)
14+
{
15+
int i;
16+
17+
for (i = 0; i < len; i++) {
18+
MODLOG_DFLT(INFO, "%s0x%02x", i != 0 ? ":" : "", bytes[i]);
19+
}
20+
}
21+
22+
void
23+
print_addr(const void *addr)
24+
{
25+
const uint8_t *u8p;
26+
27+
u8p = addr;
28+
MODLOG_DFLT(INFO, "%02x:%02x:%02x:%02x:%02x:%02x",
29+
u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
30+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
6+
7+
#include <stdio.h>
8+
#include <ctype.h>
9+
#include "esp_log.h"
10+
#include <string.h>
11+
#include <esp_log.h>
12+
#include <esp_console.h>
13+
#include <freertos/FreeRTOS.h>
14+
#include <freertos/task.h>
15+
#include <freertos/queue.h>
16+
#include <driver/uart.h>
17+
#include "esp_peripheral.h"
18+
19+
#define BLE_RX_TIMEOUT (30000 / portTICK_PERIOD_MS)
20+
21+
static TaskHandle_t cli_task;
22+
static QueueHandle_t cli_handle;
23+
static int stop;
24+
25+
static int enter_passkey_handler(int argc, char *argv[])
26+
{
27+
int key;
28+
char pkey[8];
29+
int num;
30+
31+
if (argc != 2) {
32+
return -1;
33+
}
34+
35+
sscanf(argv[1], "%s", pkey);
36+
ESP_LOGI("You entered", "%s %s", argv[0], argv[1]);
37+
num = pkey[0];
38+
39+
if (isalpha(num)) {
40+
if ((strcasecmp(pkey, "Y") == 0) || (strcasecmp(pkey, "Yes") == 0)) {
41+
key = 1;
42+
xQueueSend(cli_handle, &key, 0);
43+
} else {
44+
key = 0;
45+
xQueueSend(cli_handle, &key, 0);
46+
}
47+
} else {
48+
sscanf(pkey, "%d", &key);
49+
xQueueSend(cli_handle, &key, 0);
50+
}
51+
52+
return 0;
53+
}
54+
55+
int scli_receive_key(int *console_key)
56+
{
57+
return xQueueReceive(cli_handle, console_key, BLE_RX_TIMEOUT);
58+
}
59+
60+
static esp_console_cmd_t cmds[] = {
61+
{
62+
.command = "key",
63+
.help = "",
64+
.func = enter_passkey_handler,
65+
},
66+
};
67+
68+
static int ble_register_cli(void)
69+
{
70+
int cmds_num = sizeof(cmds) / sizeof(esp_console_cmd_t);
71+
int i;
72+
for (i = 0; i < cmds_num; i++) {
73+
esp_console_cmd_register(&cmds[i]);
74+
}
75+
return 0;
76+
}
77+
78+
static void scli_task(void *arg)
79+
{
80+
int uart_num = (int) arg;
81+
uint8_t linebuf[256];
82+
int i, cmd_ret;
83+
esp_err_t ret;
84+
QueueHandle_t uart_queue;
85+
uart_event_t event;
86+
87+
uart_driver_install(uart_num, 256, 0, 8, &uart_queue, 0);
88+
/* Initialize the console */
89+
esp_console_config_t console_config = {
90+
.max_cmdline_args = 8,
91+
.max_cmdline_length = 256,
92+
};
93+
94+
esp_console_init(&console_config);
95+
96+
while (!stop) {
97+
i = 0;
98+
memset(linebuf, 0, sizeof(linebuf));
99+
do {
100+
ret = xQueueReceive(uart_queue, (void * )&event, (TickType_t)portMAX_DELAY);
101+
if (ret != pdPASS) {
102+
if (stop == 1) {
103+
break;
104+
} else {
105+
continue;
106+
}
107+
}
108+
if (event.type == UART_DATA) {
109+
while (uart_read_bytes(uart_num, (uint8_t *) &linebuf[i], 1, 0)) {
110+
if (linebuf[i] == '\r') {
111+
uart_write_bytes(uart_num, "\r\n", 2);
112+
} else {
113+
uart_write_bytes(uart_num, (char *) &linebuf[i], 1);
114+
}
115+
i++;
116+
}
117+
}
118+
} while ((i < 255) && linebuf[i - 1] != '\r');
119+
if (stop) {
120+
break;
121+
}
122+
/* Remove the truncating \r\n */
123+
linebuf[strlen((char *)linebuf) - 1] = '\0';
124+
ret = esp_console_run((char *) linebuf, &cmd_ret);
125+
if (ret < 0) {
126+
break;
127+
}
128+
}
129+
vTaskDelete(NULL);
130+
}
131+
132+
int scli_init(void)
133+
{
134+
/* Register CLI "key <value>" to accept input from user during pairing */
135+
ble_register_cli();
136+
137+
xTaskCreate(scli_task, "scli_cli", 4096, (void *) 0, 3, &cli_task);
138+
if (cli_task == NULL) {
139+
return ESP_FAIL;
140+
}
141+
cli_handle = xQueueCreate( 1, sizeof(int) );
142+
if (cli_handle == NULL) {
143+
return ESP_FAIL;
144+
}
145+
return ESP_OK;
146+
}

docs/infos

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Linux reicht es wenn irgendwie viel bytes im report sind, welches zum gerät geschickt wird.
2+
Windows und Android muss die report größe mit der größe des report deskriptors übereinstimmen.
3+
IOS benötigt eine sichere Verbindung und dann noch die erlaubnis auf das report zuzugreifen wenn es eine sichere verbindung ist. Zusätzlich darf während dem aufbau einer sicheren verbindung keine connection updates durchgeführt werden.
4+
5+
Zusätzlich muss für ios die irk key von ios geräten zwischengespeichert werden, da nach einen neustart des ios geräts oder alle x minuten die bluetooth adresse erneurt wird und sonst das gerät sich nicht mehr mit dem esp verbinden kann.
6+
7+
Resolvable private (RPA) --> Address randomly generated from an identity address and an identity resolving key (IRK).

docs/links

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
mynewt.apache.org/latest/network/ble_hs/ble_att.html
2+
docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/bluetooth/nimble.index.html
3+
https://iotespresso.com/esp32-nimble-how-to-send-notifications/
4+
https://embeddedartistry.com/blog/2017/05/01/mixing-c-and-c-extern-c/
5+
6+
7+
// USE LINUX TOOL FOR HID PROFILE --> SONST FUNKTIONIERT ES NICHT
8+
//https://macchina.io/blog/internet-of-things/communication-with-low-energy-bluetooth-devices-on-linux/

0 commit comments

Comments
 (0)