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
3438static 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
126130static void
127131blehr_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)
251267static void
252268blehr_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
257273void blehr_host_task (void * param )
0 commit comments