11#include "gatt.h"
22
3+ #include "services/gatt/ble_svc_gatt.h"
4+ #include "services/gap/ble_svc_gap.h"
5+
6+ #include "esp_log.h"
7+
8+ #include "crsf.h"
9+ #include "gap.h"
10+ #include "battery.h"
11+
12+ /* device info configuration */
13+ #define GATT_DEVICE_INFO_UUID 0x180A
14+ #define GATT_MANUFACTURER_NAME_UUID 0x2A29
15+ #define GATT_MODEL_NUMBER_UUID 0x2A24
16+ #define GATT_FIRMWARE_REVISION_UUID 0x2A26
17+ #define GATT_SOFTWARE_REVISION_UUID 0x2A28
18+ #define GATT_PNP_ID_UUID 0x2A50
19+
20+ /* battery configuration */
21+ #define GATT_BATTERYS_UUID 0x180f
22+ #define GATT_BATTERY_LEVEL_UUID 0x2a19
23+
24+ /* hid configuration */
25+ #define GATT_HIDS_UUID 0x1812
26+ #define GATT_HID_REPORT_MAP_UUID 0x2A4B
27+ #define GATT_HID_INFORMATION_UUID 0x2A4A
28+ #define GATT_HID_CONTROL_POINT_UUID 0x2A4C
29+ #define GATT_HID_REPORT_UUID 0x2A4D
30+
31+ /*hid report configuration*/
32+ #define GATT_REPORT_REFERENCE_CHAR_UUID 0x2908
33+
34+ /*hid information infos*/
35+ #define HID_FLAGS_REMOTE_WAKE 0x01 // RemoteWake
36+ #define HID_FLAGS_NORMALLY_CONNECTABLE 0x02 // NormallyConnectable
37+ #define HID_KBD_FLAGS HID_FLAGS_REMOTE_WAKE
38+ #define HID_INFORMATION_LEN 4 // HID Information
39+
40+ static int gatt_svr_chr_access_device_info (uint16_t conn_handle , uint16_t attr_handle , struct ble_gatt_access_ctxt * ctxt , void * arg );
41+ static int gatt_svr_chr_hid (uint16_t conn_handle , uint16_t attr_handle , struct ble_gatt_access_ctxt * ctxt , void * arg );
42+ static int report_descriptor_callback (uint16_t conn_handle , uint16_t attr_handle , struct ble_gatt_access_ctxt * ctxt , void * arg );
43+
44+ static const char * tag_GATT = "SimLinkModule_GATT" ;
45+
346uint16_t report_data_handle ;
447uint16_t battery_status_handle ;
548
@@ -151,7 +194,7 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
151194 },
152195};
153196
154- int gatt_svr_chr_access_device_info (uint16_t conn_handle , uint16_t attr_handle , struct ble_gatt_access_ctxt * ctxt , void * arg ) {
197+ static int gatt_svr_chr_access_device_info (uint16_t conn_handle , uint16_t attr_handle , struct ble_gatt_access_ctxt * ctxt , void * arg ) {
155198 uint16_t uuid ;
156199 int rc ;
157200
@@ -187,6 +230,61 @@ int gatt_svr_chr_access_device_info(uint16_t conn_handle, uint16_t attr_handle,
187230 return BLE_ATT_ERR_UNLIKELY ;
188231}
189232
233+ static int gatt_svr_chr_hid (uint16_t conn_handle , uint16_t attr_handle , struct ble_gatt_access_ctxt * ctxt , void * arg ) {
234+ uint16_t uuid ;
235+ int rc ;
236+
237+ uuid = ble_uuid_u16 (ctxt -> chr -> uuid );
238+
239+ if (uuid == GATT_HID_REPORT_MAP_UUID ) {
240+ rc = os_mbuf_append (ctxt -> om , hidReportMap , sizeof (hidReportMap )/sizeof (hidReportMap [0 ]));
241+ return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES ;
242+ }
243+
244+ if (uuid == GATT_HID_INFORMATION_UUID ) {
245+ rc = os_mbuf_append (ctxt -> om , hidInfo , sizeof (hidInfo )/sizeof (hidInfo [0 ]));
246+ return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES ;
247+ }
248+
249+ if (uuid == GATT_HID_CONTROL_POINT_UUID ) {
250+ int * test = OS_MBUF_DATA (ctxt -> om ,int * );
251+ //00 == hid host is entering the suspend state
252+ //01 == hid host is exiting the suspend state
253+ //nur das erste bit betrachten
254+ //unter ios wird der suspend state schon geändert wenn man das gerät nur umdreht und das display noch nicht eingeschalten hat :)
255+ int wakeupInfo = * test & 0b11 ;
256+ notify_state_report_data = wakeupInfo ;
257+ notify_state_battery_status = wakeupInfo ;
258+ ESP_LOGW (tag_GATT , "WRITE TO CONTROL POINT %d" ,wakeupInfo );
259+ return 0 ;
260+ }
261+
262+ //Daten des reports übermitteln
263+ if (uuid == GATT_HID_REPORT_UUID ) {
264+ rc = os_mbuf_append (ctxt -> om , & channelData , sizeof (channelData ));
265+ return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES ;
266+ }
267+
268+ assert (0 );
269+ return BLE_ATT_ERR_UNLIKELY ;
270+ }
271+
272+ static int report_descriptor_callback (uint16_t conn_handle , uint16_t attr_handle , struct ble_gatt_access_ctxt * ctxt , void * arg ) {
273+ uint16_t uuid ;
274+ int rc ;
275+
276+ uuid = ble_uuid_u16 (ctxt -> chr -> uuid );
277+
278+ if (uuid == GATT_REPORT_REFERENCE_CHAR_UUID ) {
279+ //report id soll ungleich 0 sein, wenn es mehr als einen reportmerkmal gibt für einen bestimmten typen
280+ rc = os_mbuf_append (ctxt -> om , reportReferenceChar , sizeof (reportReferenceChar )/sizeof (reportReferenceChar [0 ]));
281+ return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES ;
282+ }
283+
284+ assert (0 );
285+ return BLE_ATT_ERR_UNLIKELY ;
286+ }
287+
190288void gattSvrRegisterCb (struct ble_gatt_register_ctxt * ctxt , void * arg )
191289{
192290 char buf [BLE_UUID_STR_LEN ];
@@ -245,59 +343,4 @@ int gattSvrInit(void) {
245343 }
246344
247345 return 0 ;
248- }
249-
250- int gatt_svr_chr_hid (uint16_t conn_handle , uint16_t attr_handle , struct ble_gatt_access_ctxt * ctxt , void * arg ) {
251- uint16_t uuid ;
252- int rc ;
253-
254- uuid = ble_uuid_u16 (ctxt -> chr -> uuid );
255-
256- if (uuid == GATT_HID_REPORT_MAP_UUID ) {
257- rc = os_mbuf_append (ctxt -> om , hidReportMap , sizeof (hidReportMap )/sizeof (hidReportMap [0 ]));
258- return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES ;
259- }
260-
261- if (uuid == GATT_HID_INFORMATION_UUID ) {
262- rc = os_mbuf_append (ctxt -> om , hidInfo , sizeof (hidInfo )/sizeof (hidInfo [0 ]));
263- return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES ;
264- }
265-
266- if (uuid == GATT_HID_CONTROL_POINT_UUID ) {
267- int * test = OS_MBUF_DATA (ctxt -> om ,int * );
268- //00 == hid host is entering the suspend state
269- //01 == hid host is exiting the suspend state
270- //nur das erste bit betrachten
271- //unter ios wird der suspend state schon geändert wenn man das gerät nur umdreht und das display noch nicht eingeschalten hat :)
272- int wakeupInfo = * test & 0b11 ;
273- notify_state_report_data = wakeupInfo ;
274- notify_state_battery_status = wakeupInfo ;
275- ESP_LOGW (tag_GATT , "WRITE TO CONTROL POINT %d" ,wakeupInfo );
276- return 0 ;
277- }
278-
279- //Daten des reports übermitteln
280- if (uuid == GATT_HID_REPORT_UUID ) {
281- rc = os_mbuf_append (ctxt -> om , & channelData , sizeof (channelData ));
282- return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES ;
283- }
284-
285- assert (0 );
286- return BLE_ATT_ERR_UNLIKELY ;
287- }
288-
289- int report_descriptor_callback (uint16_t conn_handle , uint16_t attr_handle , struct ble_gatt_access_ctxt * ctxt , void * arg ) {
290- uint16_t uuid ;
291- int rc ;
292-
293- uuid = ble_uuid_u16 (ctxt -> chr -> uuid );
294-
295- if (uuid == GATT_REPORT_REFERENCE_CHAR_UUID ) {
296- //report id soll ungleich 0 sein, wenn es mehr als einen reportmerkmal gibt für einen bestimmten typen
297- rc = os_mbuf_append (ctxt -> om , reportReferenceChar , sizeof (reportReferenceChar )/sizeof (reportReferenceChar [0 ]));
298- return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES ;
299- }
300-
301- assert (0 );
302- return BLE_ATT_ERR_UNLIKELY ;
303346}
0 commit comments