-
-
Notifications
You must be signed in to change notification settings - Fork 747
/
Copy pathble_handlers.cpp
295 lines (280 loc) · 8.81 KB
/
ble_handlers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <FreeRTOS.h>
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <task.h>
#include "types.h"
#include "BSP.h"
#include "TipThermoModel.h"
#include "ble_peripheral.h"
#include "bluetooth.h"
#include "configuration.h"
#include "conn.h"
#include "gatt.h"
#include "hal_clock.h"
#include "hci_core.h"
#include "log.h"
#include "uuid.h"
#include "OperatingModes.h"
#include "OLED.hpp"
#include "USBPD.h"
#include "ble_characteristics.h"
#include "ble_handlers.h"
#include "pd.h"
#include "power.hpp"
#if POW_PD
#include "USBPD.h"
#include "pd.h"
#endif
extern TickType_t lastMovementTime;
extern OperatingMode currentMode;
int ble_char_read_status_callback(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) {
if (attr == NULL || attr->uuid == NULL) {
return 0;
}
uint16_t uuid_value = ((struct bt_uuid_16 *)attr->uuid)->val;
uint32_t temp = 0;
switch (uuid_value) {
case 1: // Live temp
{
temp = TipThermoModel::getTipInC();
memcpy(buf, &temp, sizeof(temp));
return sizeof(temp);
} break;
case 2: // Setpoint temp
temp = getSettingValue(SettingsOptions::SolderingTemp);
memcpy(buf, &temp, sizeof(temp));
return sizeof(temp);
break;
case 3: // DC Input
temp = getInputVoltageX10(getSettingValue(SettingsOptions::VoltageDiv), 0);
memcpy(buf, &temp, sizeof(temp));
return sizeof(temp);
break;
case 4: // Handle temp
temp = getHandleTemperature(0);
memcpy(buf, &temp, sizeof(temp));
return sizeof(temp);
break;
case 5: // power level
// return current PWM level
temp = X10WattsToPWM(x10WattHistory.average());
memcpy(buf, &temp, sizeof(temp));
return sizeof(temp);
break;
case 6: // power src
// Todo return enum for current power source
temp = getPowerSrc();
memcpy(buf, &temp, sizeof(temp));
return sizeof(temp);
break;
case 7:
// Tip resistance
temp = getTipResistanceX10();
memcpy(buf, &temp, sizeof(temp));
return sizeof(temp);
break;
case 8:
// uptime
temp = xTaskGetTickCount() / TICKS_100MS;
memcpy(buf, &temp, sizeof(temp));
return sizeof(temp);
break;
case 9:
// movement
temp = lastMovementTime / TICKS_100MS;
memcpy(buf, &temp, sizeof(temp));
return sizeof(temp);
break;
case 10:
// max temp
temp = TipThermoModel::getTipMaxInC();
memcpy(buf, &temp, sizeof(temp));
return sizeof(temp);
break;
case 11:
// raw tip
temp = TipThermoModel::convertTipRawADCTouV(getTipRawTemp(0), true);
memcpy(buf, &temp, sizeof(temp));
return sizeof(temp);
break;
case 12:
// hall sensor
{
int16_t hallEffectStrength = getRawHallEffect();
if (hallEffectStrength < 0)
hallEffectStrength = -hallEffectStrength;
temp = hallEffectStrength;
memcpy(buf, &temp, sizeof(temp));
return sizeof(temp);
}
break;
case 13:
// Operating mode
temp = currentMode;
memcpy(buf, &temp, sizeof(temp));
return sizeof(temp);
break;
case 14:
// Estimated watts
temp = x10WattHistory.average();
memcpy(buf, &temp, sizeof(temp));
return sizeof(temp);
break;
}
MSG((char *)"Unhandled attr read %d | %d\n", (uint32_t)attr->uuid, uuid_value);
return 0;
}
int ble_char_read_bulk_value_callback(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) {
if (attr == NULL || attr->uuid == NULL) {
return 0;
}
uint16_t uuid_value = ((struct bt_uuid_16 *)attr->uuid)->val;
// Bulk is the non-const size service
switch (uuid_value) {
case 1:
// Bulk data
{
uint32_t bulkData[] = {
TipThermoModel::getTipInC(), // 0 - Current temp
getSettingValue(SettingsOptions::SolderingTemp), // 1 - Setpoint
getInputVoltageX10(getSettingValue(SettingsOptions::VoltageDiv), 0), // 2 - Input voltage
getHandleTemperature(0), // 3 - Handle X10 Temp in C
X10WattsToPWM(x10WattHistory.average()), // 4 - Power as PWM level
getPowerSrc(), // 5 - power src
getTipResistanceX10(), // 6 - Tip resistance
xTaskGetTickCount() / TICKS_100MS, // 7 - uptime in deciseconds
lastMovementTime / TICKS_100MS, // 8 - last movement time (deciseconds)
TipThermoModel::getTipMaxInC(), // 9 - max temp
TipThermoModel::convertTipRawADCTouV(getTipRawTemp(0), true), // 10 - Raw tip in μV
abs(getRawHallEffect()), // 11 - hall sensor
currentMode, // 12 - Operating mode
x10WattHistory.average(), // 13 - Estimated Wattage *10
};
int lenToCopy = sizeof(bulkData) - offset;
if (lenToCopy > len) {
lenToCopy = len;
}
if (lenToCopy < 0) {
lenToCopy = 0;
}
memcpy(buf, ((uint8_t *)bulkData) + offset, lenToCopy);
return lenToCopy;
}
break;
case 2:
// Accelerometer name
// TODO: Need to store non-encoded version
break;
case 3:
// Build
// TODO: Need to store non-encoded version
break;
case 4:
// Device unique id
{
uint64_t id = getDeviceID();
memcpy(buf, &id, sizeof(id));
return sizeof(id);
}
break;
}
return 0;
}
int ble_char_read_setting_value_callback(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) {
if (attr == NULL || attr->uuid == NULL) {
return 0;
}
uint16_t uuid_value = ((struct bt_uuid_16 *)attr->uuid)->val;
uint16_t temp = 0xFFFF;
if (uuid_value <= SettingsOptions::SettingsOptionsLength) {
temp = getSettingValue((SettingsOptions)(uuid_value));
memcpy(buf, &temp, sizeof(temp));
return sizeof(temp);
} else {
memcpy(buf, &temp, sizeof(temp));
return sizeof(temp);
}
MSG((char *)"Unhandled attr read %d | %d\n", (uint32_t)attr->uuid, uuid_value);
return 0;
}
int ble_char_write_setting_value_callback(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, u16_t len, u16_t offset, u8_t flags) {
if (flags & BT_GATT_WRITE_FLAG_PREPARE) {
// Don't use prepare write data, execute write will upload data again.
BT_WARN((char *)"recv prepare write request\n");
return 0;
}
if (attr == NULL || attr->uuid == NULL) {
return 0;
}
if (flags & BT_GATT_WRITE_FLAG_CMD) {
// Use write command data.
BT_WARN((char *)"recv write command\n");
} else {
// Use write request / execute write data.
BT_WARN((char *)"recv write request / exce write\n");
}
uint16_t uuid_value = ((struct bt_uuid_16 *)attr->uuid)->val;
if (len == 2) {
uint16_t new_value = 0;
memcpy(&new_value, buf, sizeof(new_value));
if (uuid_value == 0xFFFF) {
if (new_value == 1) {
saveSettings();
return len;
}
} else if (uuid_value == 0xFFFE) {
if (new_value == 1) {
resetSettings();
return len;
}
} else if (uuid_value < SettingsOptions::SettingsOptionsLength) {
setSettingValue((SettingsOptions)(uuid_value), new_value);
// @TODO refactor to make this more usable
if (uuid_value == SettingsOptions::OLEDInversion) {
OLED::setInverseDisplay(getSettingValue(SettingsOptions::OLEDInversion));
}
if (uuid_value == SettingsOptions::OLEDBrightness){
OLED::setBrightness(getSettingValue(SettingsOptions::OLEDBrightness));
}
if (uuid_value == SettingsOptions::OrientationMode){
OLED::setRotation(getSettingValue(SettingsOptions::OrientationMode) & 1);
}
return len;
}
}
MSG((char *)"Unhandled attr write %d | %d\n", (uint32_t)attr->uuid, uuid_value);
return 0;
}
uint32_t getPowerSrc() {
int sourceNumber = 0;
if (getIsPoweredByDCIN()) {
sourceNumber = 0;
} else {
// We are not powered via DC, so want to display the appropriate state for PD or QC
bool poweredbyPD = false;
bool pdHasVBUSConnected = false;
#if POW_PD
if (USBPowerDelivery::fusbPresent()) {
// We are PD capable
if (USBPowerDelivery::negotiationComplete()) {
// We are powered via PD
poweredbyPD = true;
#ifdef VBUS_MOD_TEST
pdHasVBUSConnected = USBPowerDelivery::isVBUSConnected();
#endif
}
}
#endif
if (poweredbyPD) {
if (pdHasVBUSConnected) {
sourceNumber = 2;
} else {
sourceNumber = 3;
}
} else {
sourceNumber = 1;
}
}
return sourceNumber;
}