Skip to content

Commit de054a0

Browse files
committed
code refactoring
1 parent aae6d6c commit de054a0

File tree

16 files changed

+273
-257
lines changed

16 files changed

+273
-257
lines changed

.vscode/settings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
"stdio.h": "c",
1515
"freertosconfig.h": "c",
1616
"inttypes.h": "c",
17-
"glue.h": "c"
17+
"glue.h": "c",
18+
"adc_cali_scheme.h": "c",
19+
"timers.h": "c",
20+
"adc_oneshot.h": "c",
21+
"ble_svc_gatt.h": "c"
1822
}
1923
}

components/battery/battery.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
#include "battery.h"
22

3+
#include "esp_adc/adc_oneshot.h"
4+
#include "esp_adc/adc_cali.h"
5+
#include "esp_adc/adc_cali_scheme.h"
6+
7+
#include "host/ble_gatt.h"
8+
#include "esp_log.h"
9+
10+
#include "gatt.h"
11+
#include "gap.h"
12+
313
static const char *tag_BAT = "SimLinkModule_BAT";
414

515
uint8_t batteryPercentage = 0;
@@ -10,9 +20,12 @@ static bool adc_calibrated = false;
1020
static int measurementCount = 0;
1121
static int sumVoltage = 0; //Voltage in mV
1222

23+
static bool adc_calibration(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *out_handle);
24+
static int32_t getVoltage();
25+
1326

1427
//determine the esp ADC reference voltage which is around 1100 mV
15-
bool adc_calibration(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *return_handle){
28+
static bool adc_calibration(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *return_handle){
1629
adc_cali_handle_t handle = NULL;
1730
esp_err_t ret = ESP_FAIL;
1831
bool returnValue = false;
@@ -58,7 +71,7 @@ void initBatteryRead(){
5871
}
5972

6073
//Voltage in mV
61-
int32_t getVoltage(){
74+
static int32_t getVoltage(){
6275
int raw_val, voltage;
6376
adc_oneshot_read(adc_handle, ADC_CHANNEL_4, &raw_val);
6477
if(adc_calibrated){
@@ -74,9 +87,6 @@ int32_t getVoltage(){
7487

7588
void battery_Timer_Event(TimerHandle_t ev){
7689
//https://esp32.com/viewtopic.php?t=1459
77-
78-
struct os_mbuf *om;
79-
int rc;
8090

8191
getVoltage();
8292
if(measurementCount < 10){
@@ -101,9 +111,11 @@ void battery_Timer_Event(TimerHandle_t ev){
101111

102112
if(newPercentage != batteryPercentage){
103113
if(notify_state_battery_status){
114+
struct os_mbuf *om;
115+
104116
om = ble_hs_mbuf_from_flat(&batteryPercentage, sizeof(batteryPercentage));
105117
//Deprecated. Should not be used. Use ble_gatts_notify_custom instead.
106-
rc = ble_gattc_notify_custom(conn_handle, battery_status_handle, om);
118+
ble_gattc_notify_custom(conn_handle, battery_status_handle, om);
107119
}
108120
newPercentage = batteryPercentage;
109121
}

components/battery/include/battery.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
11
#ifndef BATTERY_H
22
#define BATTERY_H
33

4-
#include "esp_log.h"
5-
64
#include <stdint.h>
5+
76
#include "freertos/FreeRTOS.h"
87
#include "freertos/timers.h"
98

10-
#include "esp_adc/adc_oneshot.h"
11-
#include "esp_adc/adc_cali.h"
12-
#include "esp_adc/adc_cali_scheme.h"
13-
14-
#include "host/ble_gatt.h"
15-
16-
#include "gatt.h"
17-
#include "gap.h"
18-
199
extern uint8_t batteryPercentage;
2010

2111
void initBatteryRead();
2212
void battery_Timer_Event(TimerHandle_t ev);
23-
bool adc_calibration(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *out_handle);
2413

2514
#endif

components/ble/ble.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
#include "ble.h"
22

3+
#include "esp_log.h"
4+
#include "nvs_flash.h"
5+
6+
#include "gap.h"
7+
#include "gatt.h"
8+
9+
#include "nimble/nimble_port.h"
10+
#include "esp_nimble_hci.h"
11+
#include "host/ble_hs.h"
12+
#include "services/gap/ble_svc_gap.h"
13+
#include "host/ble_hs_pvcy.h"
14+
#include "host/util/util.h"
15+
#include "nimble/nimble_port_freertos.h"
16+
17+
static void bleOnSync(void);
18+
static void bleOnReset(int reason);
19+
20+
/* Define template prototype for store*/
21+
//located in nimble/host/store/config/src/ble_store_config.c
22+
//never definded in a .h file
23+
void ble_store_config_init(void);
24+
25+
static const char *tag_BLE = "SimLinkModule_BLE";
26+
327
uint8_t bleAddressType = BLE_ADDR_RANDOM;
428

529
void initBLE(){
@@ -84,8 +108,8 @@ void initBLE(){
84108
}
85109

86110
//This callback is executed when the host and controller become synced. This happens at startup and after a reset
87-
void bleOnSync(void){
88-
int rc;
111+
static void bleOnSync(void){
112+
//int rc;
89113

90114
//rpa = resolvable private address; Address randomly generated from an identity address and an identity resolving key (IRK).
91115
ble_hs_pvcy_rpa_config(1);
@@ -117,7 +141,7 @@ void bleOnSync(void){
117141
}
118142

119143
//This callback is executed when the host resets itself and the controller
120-
void bleOnReset(int reason){
144+
static void bleOnReset(int reason){
121145
ESP_LOGI(tag_BLE, "Resetting state; reason=%d\n", reason);
122146
}
123147

components/ble/gap.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
#include "gap.h"
22

3+
#include <string.h>
4+
5+
#include "host/ble_hs.h"
6+
#include "services/gap/ble_svc_gap.h"
7+
8+
#include "esp_log.h"
9+
#include "esp_random.h"
10+
11+
#include "gatt.h"
12+
#include "ble.h"
13+
#include "ssd1306.h"
14+
#include "button.h"
15+
16+
static int bleGAPEevent(struct ble_gap_event *event, void *arg);
17+
18+
static const char *tag_GAP = "SimLinkModule_GAP";
19+
static const ble_uuid16_t hid_service_uuid = BLE_UUID16_INIT(0x1812);
20+
321
uint16_t conn_handle = 0;
422
bool notify_state_report_data = false;
523
bool notify_state_battery_status = false;
@@ -77,7 +95,7 @@ void bleAdvertise(void){
7795
}
7896

7997
//The callback to associate with this advertising procedure. If advertising ends, the event is reported through this callback. If advertising results in a connection, the connection inherits this callback as its event-reporting mechanism.
80-
int bleGAPEevent(struct ble_gap_event *event, void *arg) {
98+
static int bleGAPEevent(struct ble_gap_event *event, void *arg) {
8199
struct ble_gap_conn_desc desc;
82100
int rc;
83101

components/ble/gatt.c

Lines changed: 99 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,48 @@
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+
346
uint16_t report_data_handle;
447
uint16_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+
190288
void 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

Comments
 (0)