Skip to content

Commit 37c8920

Browse files
committed
ble battery service now with notify option
1 parent 2be4ab5 commit 37c8920

File tree

11 files changed

+102
-13
lines changed

11 files changed

+102
-13
lines changed

components/battery/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set(srcs "battery.c")
2+
3+
idf_component_register(SRCS "${srcs}"
4+
INCLUDE_DIRS "include"
5+
REQUIRES bt ble)

components/battery/battery.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "battery.h"
2+
3+
static const char *tag_BAT = "SimLinkModule_BAT";
4+
5+
uint8_t batteryPercentage = 0;
6+
7+
void initBatteryRead(){
8+
9+
}
10+
11+
void battery_Timer_Event(TimerHandle_t ev){
12+
//https://esp32.com/viewtopic.php?t=1459
13+
14+
struct os_mbuf *om;
15+
int rc;
16+
17+
batteryPercentage = (batteryPercentage < 100) ? batteryPercentage+10 : 0;
18+
19+
if(notify_state_battery_status){
20+
om = ble_hs_mbuf_from_flat(&batteryPercentage, sizeof(batteryPercentage));
21+
//Deprecated. Should not be used. Use ble_gatts_notify_custom instead.
22+
rc = ble_gattc_notify_custom(conn_handle, battery_status_handle, om);
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef BATTERY_H
2+
#define BATTERY_H
3+
4+
#include "esp_log.h"
5+
6+
#include <stdint.h>
7+
#include "freertos/FreeRTOS.h"
8+
#include "freertos/timers.h"
9+
#include "host/ble_gatt.h"
10+
11+
#include "gatt.h"
12+
#include "gap.h"
13+
14+
extern uint8_t batteryPercentage;
15+
16+
void initBatteryRead();
17+
void battery_Timer_Event(TimerHandle_t ev);
18+
19+
#endif

components/ble/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ set(srcs "gap.c" "gatt.c" "ble.c")
22

33
idf_component_register(SRCS "${srcs}"
44
INCLUDE_DIRS "include"
5-
REQUIRES bt nimble_peripheral_utils ssd1306 crsf nvs_flash
5+
REQUIRES bt nimble_peripheral_utils ssd1306 crsf nvs_flash battery
66
PRIV_REQUIRES )

components/ble/gap.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "gap.h"
22

33
uint16_t conn_handle = 0;
4-
bool notify_state = false;
4+
bool notify_state_report_data = false;
5+
bool notify_state_battery_status = false;
56

67
/*
78
* Enables advertising with parameters:
@@ -119,6 +120,9 @@ int bleGAPEevent(struct ble_gap_event *event, void *arg) {
119120
//517 = Authentication Failure
120121
//573 = Connection Terminated due to MIC Failure
121122

123+
notify_state_report_data = false;
124+
notify_state_battery_status = false;
125+
122126
/* Connection terminated; resume advertising */
123127
bleAdvertise();
124128
break;
@@ -144,10 +148,14 @@ int bleGAPEevent(struct ble_gap_event *event, void *arg) {
144148
}
145149

146150
if (event->subscribe.attr_handle == report_data_handle) {
147-
notify_state = event->subscribe.cur_notify;
148-
} else if (event->subscribe.attr_handle != report_data_handle) {
149-
notify_state = event->subscribe.cur_notify;
151+
//check if the device subscribed to the report data notifies
152+
notify_state_report_data = event->subscribe.cur_notify;
153+
} else if (event->subscribe.attr_handle == battery_status_handle) {
154+
//check if the device subscribed to the battery status notifies
155+
notify_state_battery_status = event->subscribe.cur_notify;
150156
}
157+
158+
151159
ESP_LOGI(tag_GAP, "conn_handle from subscribe=%d", conn_handle);
152160
break;
153161

@@ -226,7 +234,6 @@ int bleGAPEevent(struct ble_gap_event *event, void *arg) {
226234
case BLE_GAP_EVENT_NOTIFY_TX:
227235
//Represents a transmitted ATT notification or indication, or a
228236
//completed indication transaction.
229-
ESP_LOGI(tag_GAP, "notify tx event occured");
230237
return 0;
231238
default:
232239
ESP_LOGI(tag_GAP, "GAP EVENT ID: %d\n",event->type);

components/ble/gatt.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "gatt.h"
22

33
uint16_t report_data_handle;
4+
uint16_t battery_status_handle;
45

56
static const char *manuf_name = "SimLinkModule";
67
static const char *model_num = "P-1.0";
@@ -95,7 +96,8 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
9596
/* Characteristic: Battery Level */
9697
.uuid = BLE_UUID16_DECLARE(GATT_BATTERY_LEVEL_UUID),
9798
.access_cb = gatt_svr_chr_access_device_info,
98-
.flags = BLE_GATT_CHR_F_READ,
99+
.val_handle = &battery_status_handle,
100+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
99101
}, {
100102
0, /* No more characteristics in this service */
101103
},
@@ -176,7 +178,7 @@ int gatt_svr_chr_access_device_info(uint16_t conn_handle, uint16_t attr_handle,
176178
}
177179

178180
if (uuid == GATT_BATTERY_LEVEL_UUID) {
179-
int percentage = 99;
181+
int percentage = batteryPercentage;
180182
rc = os_mbuf_append(ctxt->om, &percentage, sizeof(percentage));
181183
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
182184
}
@@ -268,6 +270,8 @@ int gatt_svr_chr_hid(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt
268270
//nur das erste bit betrachten
269271
//unter ios wird der suspend state schon geändert wenn man das gerät nur umdreht und das display noch nicht eingeschalten hat :)
270272
int wakeupInfo = *test & 0b11;
273+
notify_state_report_data = wakeupInfo;
274+
notify_state_battery_status = wakeupInfo;
271275
ESP_LOGW(tag_GATT, "WRITE TO CONTROL POINT %d",wakeupInfo);
272276
return 0;
273277
}

components/ble/include/gap.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ static const ble_uuid16_t hid_service_uuid = BLE_UUID16_INIT(0x1812);
1717

1818
//eindeutiges handle was bei einem verbindungsaufbau einer verbindung zugeordnet wird
1919
extern uint16_t conn_handle;
20-
extern bool notify_state;
20+
extern bool notify_state_report_data;
21+
extern bool notify_state_battery_status;
2122

2223
void bleAdvertise(void);
2324
int bleGAPEevent(struct ble_gap_event *event, void *arg);

components/ble/include/gatt.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
#include "services/gap/ble_svc_gap.h"
77
#include "services/gatt/ble_svc_gatt.h"
88
#include "host/ble_hs.h"
9+
910
#include "crsf.h"
11+
#include "gap.h"
12+
#include "battery.h"
1013

1114
/* device info configuration */
1215
#define GATT_DEVICE_INFO_UUID 0x180A
@@ -39,7 +42,9 @@
3942
static const char *tag_GATT = "SimLinkModule_GATT";
4043

4144
//The value handle of the relevant characteristic for notification
42-
extern uint16_t report_data_handle;
45+
extern uint16_t report_data_handle; //report daten
46+
extern uint16_t battery_status_handle; //battery status
47+
4348

4449
//zu verschickender report muss immer so groß sein wie auch der report groß ist sonst geht es nicht
4550
static uint8_t reportData[1] = {

components/crsf/crsf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ void crsf_get_ChannelData_task(void *arg)
225225
int rc;
226226
struct os_mbuf *om;
227227

228-
if(notify_state){
228+
if(notify_state_report_data){
229229
om = ble_hs_mbuf_from_flat(&channelData, sizeof(channelData));
230230
//Deprecated. Should not be used. Use ble_gatts_notify_custom instead.
231231
rc = ble_gattc_notify_custom(conn_handle, report_data_handle, om);

docs/todo

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
- ADC zur spannungsanzeige implemtentieren
2+
- ReadMe anpassen mit dem Pinout
3+
- sdkconfig vergleichen, ob bei v5.0 neue möglichkeiten hinzugekommen sind
4+
- kommentieren und in .md-Datein dokumentieren, wo was zu finden ist
5+
16
- Bei einem erneuten verbindungsaufbau wird das gestoppte updaten der daten nicht wieder gestartet
2-
- ios funktioniert nicht --> schauen ob man uqora team anschreiben kann für beta invite
3-
--> dev@orqafpv.com anschreiben für IOS testflight invite code
7+
- ios funktioniert nicht / kann nicht getestet werden
8+
9+
- buttons hinzufügen
10+
- LEDs hinzufügen
11+
- Display output hinzufügen

0 commit comments

Comments
 (0)