Skip to content

Commit edbe9c8

Browse files
committed
works with android, linux and windows
NO IOS
1 parent 93ae955 commit edbe9c8

File tree

3 files changed

+58
-43
lines changed

3 files changed

+58
-43
lines changed

main/blehr_sens.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern "C" {
1919
#define GATT_MODEL_NUMBER_UUID 0x2A24
2020
#define GATT_FIRMWARE_REVISION_UUID 0x2A26
2121
#define GATT_SOFTWARE_REVISION_UUID 0x2A28
22+
#define GATT_PNP_ID_UUID 0x2A50
2223

2324
/* battery configuration */
2425
#define GATT_BATTERYS_UUID 0x180f
@@ -40,10 +41,11 @@ extern "C" {
4041
#define HID_KBD_FLAGS HID_FLAGS_REMOTE_WAKE
4142
#define HID_INFORMATION_LEN 4 // HID Information
4243

44+
//The value handle of the relevant characteristic for notification
4345
extern uint16_t report_data_handle;
4446

45-
static uint8_t reportData[2] = {
46-
0x00,
47+
//zu verschickender report muss immer so groß sein wie auch der report groß ist sonst geht es nicht
48+
static uint8_t reportData[1] = {
4749
0x00
4850
};
4951

main/gatt_svr.c

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,23 @@ static const uint8_t hidReportMap[] = {
1616
0x05, 0x0C, // Usage Page (Consumer)
1717
0x09, 0x01, // Usage (Consumer Control)
1818
0xA1, 0x01, // Collection (Application)
19-
0x15, 0x00, // Logical Minimum (0)
19+
0x85, 0x01, // Report Id (1)
20+
0x05, 0x0C, // Usage Page (Consumer)
21+
0x09, 0x86, // Usage (Channel)
22+
0x15, 0xFF, // Logical Minimum (-1)
2023
0x25, 0x01, // Logical Maximum (1)
24+
0x75, 0x02, // Report Size (2)
25+
0x95, 0x01, // Report Count (1)
26+
0x81, 0x46, // Input (Data,Var,Rel,No Wrap,Linear,Preferred State,Null State)
2127
0x09, 0xE9, // Usage (Volume Increment)
2228
0x09, 0xEA, // Usage (Volume Decrement)
29+
0x15, 0x00, // Logical Minimum (0)
2330
0x75, 0x01, // Report Size (1)
2431
0x95, 0x02, // Report Count (2)
2532
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
26-
0x09, 0xE2, // Usage (Mute)
27-
0x09, 0x00, // Usage (Unassigned)
28-
0x81, 0x06, // Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position)
29-
0x09, 0x00, // Usage (Unassigned)
30-
0x95, 0x04, // Report Count (4)
31-
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
32-
0x26, 0xFF, 0x00, // Logical Maximum (255)
33-
0x09, 0x00, // Usage (Unassigned)
34-
0x75, 0x08, // Report Size (8)
35-
0x95, 0x03, // Report Count (3)
36-
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
37-
0x09, 0x00, // Usage (Unassigned)
38-
0x95, 0x04, // Report Count (4)
39-
0x91, 0x02, // Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
33+
0x95, 0x01, // Report Count (1)
34+
0x75, 0x04, // Report Size (4)
35+
0x81, 0x03, // Input (Const,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
4036
0xC0, // End Collection
4137
};
4238

@@ -47,7 +43,7 @@ static const uint8_t hidInfo[HID_INFORMATION_LEN] = {
4743
};
4844

4945
static const uint8_t reportReferenceChar[2] = {
50-
0x00, //report-id des reports vom report deskriptor
46+
0x01, //report-id des reports vom report deskriptor
5147
0x01 //input: 0x01, output: 0x02, feature: 0x03
5248
};
5349

@@ -145,7 +141,8 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
145141
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
146142
.descriptors = (struct ble_gatt_dsc_def[]){
147143
//client configuration descriptor soll nicht manuell hinzugefügt werden, da dieser mittels dem flag notify automatisch hinzugefügt wird
148-
{
144+
{
145+
//TODO: | BLE_ATT_F_WRITE_ENC vielleicht bei ios benötigt
149146
.att_flags = BLE_ATT_F_READ,
150147
.access_cb = report_descriptor_callback,
151148
.uuid = BLE_UUID16_DECLARE(GATT_REPORT_REFERENCE_CHAR_UUID) //damit wird angegeben welche report id und report type abgedeckt werden
@@ -264,21 +261,21 @@ gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
264261

265262
switch (ctxt->op) {
266263
case BLE_GATT_REGISTER_OP_SVC:
267-
MODLOG_DFLT(DEBUG, "registered service %s with handle=%d\n",
264+
ESP_LOGI("ASDF", "registered service %s with handle=%d\n",
268265
ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf),
269266
ctxt->svc.handle);
270267
break;
271268

272269
case BLE_GATT_REGISTER_OP_CHR:
273-
MODLOG_DFLT(DEBUG, "registering characteristic %s with "
270+
ESP_LOGI("ASDF", "registering characteristic %s with "
274271
"def_handle=%d val_handle=%d\n",
275272
ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf),
276273
ctxt->chr.def_handle,
277274
ctxt->chr.val_handle);
278275
break;
279276

280277
case BLE_GATT_REGISTER_OP_DSC:
281-
MODLOG_DFLT(DEBUG, "registering descriptor %s with handle=%d\n",
278+
ESP_LOGI("ASDF", "registering descriptor %s with handle=%d\n",
282279
ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf),
283280
ctxt->dsc.handle);
284281
break;

main/main.c

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
//https://macchina.io/blog/internet-of-things/communication-with-low-energy-bluetooth-devices-on-linux/
33

44

5-
//TODO: ADD notify option; works only with windows
5+
//TODO: alle geräte funktionieren außer ios; hier reinschauen wie notify gemacht wird; muss wahrscheinlich secure verbindung sein
6+
//https://github.com/Xi-MingYu/Central/tree/394ccfb02f2c58d8ea7eb0e72b0abe927602f5ea/lib/NimBLE/src
7+
//vielleicht eine secure verbindung aufbauen https://github.com/h2zero/NimBLE-Arduino/issues/222
8+
//https://github.com/wakwak-koba/ESP32-NimBLE-Keyboard/blob/ec4ce707f00d260f30286191fe63dc23d65a5934/BleKeyboard.cpp#L141
9+
// Diese verschlüsselung mal hinzufügen
610

711
#include "esp_log.h"
812
#include "nvs_flash.h"
@@ -33,7 +37,7 @@ static int blehr_gap_event(struct ble_gap_event *event, void *arg);
3337

3438
static uint8_t blehr_addr_type;
3539

36-
static int tmpVol = 0;
40+
static bool volumeUp = true;
3741

3842
/**
3943
* Utility function to log an array of bytes.
@@ -43,7 +47,7 @@ print_bytes(const uint8_t *bytes, int len)
4347
{
4448
int i;
4549
for (i = 0; i < len; i++) {
46-
MODLOG_DFLT(INFO, "%s0x%02x", i != 0 ? ":" : "", bytes[i]);
50+
ESP_LOGI("ASDF", "%s0x%02x", i != 0 ? ":" : "", bytes[i]);
4751
}
4852
}
4953

@@ -53,7 +57,7 @@ print_addr(const void *addr)
5357
const uint8_t *u8p;
5458

5559
u8p = addr;
56-
MODLOG_DFLT(INFO, "%02x:%02x:%02x:%02x:%02x:%02x",
60+
ESP_LOGI("ASDF", "%02x:%02x:%02x:%02x:%02x:%02x",
5761
u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
5862
}
5963

@@ -107,7 +111,7 @@ blehr_advertise(void)
107111

108112
rc = ble_gap_adv_set_fields(&fields);
109113
if (rc != 0) {
110-
MODLOG_DFLT(ERROR, "error setting advertisement data; rc=%d\n", rc);
114+
ESP_LOGI("ASDF", "error setting advertisement data; rc=%d\n", rc);
111115
return;
112116
}
113117

@@ -118,15 +122,15 @@ blehr_advertise(void)
118122
rc = ble_gap_adv_start(blehr_addr_type, NULL, BLE_HS_FOREVER,
119123
&adv_params, blehr_gap_event, NULL);
120124
if (rc != 0) {
121-
MODLOG_DFLT(ERROR, "error enabling advertisement; rc=%d\n", rc);
125+
ESP_LOGI("ASDF", "error enabling advertisement; rc=%d\n", rc);
122126
return;
123127
}
124128
}
125129

126130
static void
127131
blehr_tx_hrate_stop(void)
128132
{
129-
tmpVol = 0;
133+
volumeUp = true;
130134
xTimerStop( blehr_tx_timer, 1000 / portTICK_PERIOD_MS );
131135
}
132136

@@ -160,14 +164,13 @@ blehr_tx_hrate(xTimerHandle ev)
160164
}
161165

162166
/* Simulation of volume */
163-
ESP_LOGW("ASDF", "%d",tmpVol);
164-
if(tmpVol < 10){
165-
tmpVol++;
166-
reportData[0] = 0b00000001;
167+
ESP_LOGW("ASDF", "%d",volumeUp);
168+
if(volumeUp){
169+
reportData[0] = 0b00000100;
167170
} else {
168-
tmpVol--;
169-
reportData[0] = 0b00000010;
171+
reportData[0] = 0b00001000;
170172
}
173+
volumeUp = !volumeUp;
171174

172175
om = ble_hs_mbuf_from_flat(reportData, sizeof(reportData));
173176
rc = ble_gattc_notify_custom(conn_handle, report_data_handle, om);
@@ -183,31 +186,44 @@ blehr_gap_event(struct ble_gap_event *event, void *arg)
183186
switch (event->type) {
184187
case BLE_GAP_EVENT_CONNECT:
185188
/* A new connection was established or a connection attempt failed */
186-
MODLOG_DFLT(INFO, "connection %s; status=%d\n",
189+
ESP_LOGI("ASDF", "connection %s; status=%d\n",
187190
event->connect.status == 0 ? "established" : "failed",
188191
event->connect.status);
189192

190193
if (event->connect.status != 0) {
191194
/* Connection failed; resume advertising */
192195
blehr_advertise();
193196
}
197+
198+
struct ble_gap_upd_params connectionParameters = {
199+
//itvl: These determine how often the devices will "ping-pong" each other and also when they will send any data required. So if you set the value to something like 20, that would mean packets are sent every 25ms, which will obviously consume more power than say a value of 80 (100ms). The reason for the min max values is so the devices can negotiate a compromise for the best possible communication, you can set these to the same value if you prefer.
200+
.itvl_min = (int)(11.25/1.25), //1.25ms units; laut apple 11.25 minimum fuer hid
201+
.itvl_max = (int)(20/1.25), //minimum ist laut apple eigentlich 15ms deswegen etwas höher setzen
202+
//latency: This is how many "ping-pong" (connection interval) events the slave(server) device is allowed to skip without the client device considering the connection terminated. So if you had a 25ms connection interval and you wanted to sleep for 1 second you could set this value to 40 and the client would consider the connection active for up to 40 skipped intervals.
203+
.latency = 30, //up to 30 connection intervals
204+
//timeout: This is the absolute (disconnection) timeout, if no packets are received by either device within this time the connection is considered terminated.
205+
.supervision_timeout = 1860/10 //10ms units, laut apple größer als itvl_max * (latency + 1) * 3
206+
};
207+
208+
ESP_ERROR_CHECK(ble_gap_update_params(event->connect.conn_handle, &connectionParameters));
209+
194210
conn_handle = event->connect.conn_handle;
195211
break;
196212

197213
case BLE_GAP_EVENT_DISCONNECT:
198-
MODLOG_DFLT(INFO, "disconnect; reason=%d\n", event->disconnect.reason);
214+
ESP_LOGI("ASDF", "disconnect; reason=%d\n", event->disconnect.reason);
199215

200216
/* Connection terminated; resume advertising */
201217
blehr_advertise();
202218
break;
203219

204220
case BLE_GAP_EVENT_ADV_COMPLETE:
205-
MODLOG_DFLT(INFO, "adv complete\n");
221+
ESP_LOGI("ASDF", "adv complete; reason = %d\n", event->adv_complete.reason);
206222
blehr_advertise();
207223
break;
208224

209225
case BLE_GAP_EVENT_SUBSCRIBE:
210-
MODLOG_DFLT(INFO, "subscribe event; cur_notify=%d\n value handle; val_handle=%d\n", event->subscribe.cur_notify, report_data_handle);
226+
ESP_LOGI("ASDF", "subscribe event; cur_notify=%d\n value handle; val_handle=%d\n", event->subscribe.cur_notify, report_data_handle);
211227
if (event->subscribe.attr_handle == report_data_handle) {
212228
notify_state = event->subscribe.cur_notify;
213229
blehr_tx_hrate_reset();
@@ -219,7 +235,7 @@ blehr_gap_event(struct ble_gap_event *event, void *arg)
219235
break;
220236

221237
case BLE_GAP_EVENT_MTU:
222-
MODLOG_DFLT(INFO, "mtu update event; conn_handle=%d mtu=%d\n",
238+
ESP_LOGI("ASDF", "mtu update event; mtu already updated; nothing todo; conn_handle=%d mtu=%d\n",
223239
event->mtu.conn_handle,
224240
event->mtu.value);
225241
break;
@@ -240,9 +256,9 @@ blehr_on_sync(void)
240256
uint8_t addr_val[6] = {0};
241257
rc = ble_hs_id_copy_addr(blehr_addr_type, addr_val, NULL);
242258

243-
MODLOG_DFLT(INFO, "Device Address: ");
259+
ESP_LOGI("ASDF", "Device Address: ");
244260
print_addr(addr_val);
245-
MODLOG_DFLT(INFO, "\n");
261+
ESP_LOGI("ASDF", "\n");
246262

247263
/* Begin advertising */
248264
blehr_advertise();
@@ -251,7 +267,7 @@ blehr_on_sync(void)
251267
static void
252268
blehr_on_reset(int reason)
253269
{
254-
MODLOG_DFLT(ERROR, "Resetting state; reason=%d\n", reason);
270+
ESP_LOGI("ASDF", "Resetting state; reason=%d\n", reason);
255271
}
256272

257273
void blehr_host_task(void *param)

0 commit comments

Comments
 (0)