-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathBLE_Server.cpp
878 lines (759 loc) · 35.6 KB
/
BLE_Server.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
/*
* Copyright (C) 2020 Anthony Doud & Joel Baranick
* All rights reserved
*
* SPDX-License-Identifier: GPL-2.0-only
*/
#include "Main.h"
#include "SS2KLog.h"
#include "BLE_Common.h"
#include <ArduinoJson.h>
#include <Constants.h>
#include <NimBLEDevice.h>
// BLE Server Settings
SpinBLEServer spinBLEServer;
NimBLEServer *pServer = nullptr;
BLEService *pHeartService;
BLECharacteristic *heartRateMeasurementCharacteristic;
BLEService *pPowerMonitor;
BLECharacteristic *cyclingPowerMeasurementCharacteristic;
BLECharacteristic *cyclingPowerFeatureCharacteristic;
BLECharacteristic *sensorLocationCharacteristic;
BLEService *pFitnessMachineService;
BLECharacteristic *fitnessMachineFeature;
BLECharacteristic *fitnessMachineIndoorBikeData;
BLECharacteristic *fitnessMachineStatusCharacteristic;
BLECharacteristic *fitnessMachineControlPoint;
BLECharacteristic *fitnessMachineResistanceLevelRange;
BLECharacteristic *fitnessMachinePowerRange;
BLECharacteristic *fitnessMachineInclinationRange;
BLECharacteristic *fitnessMachineTrainingStatus;
BLEService *pSmartSpin2kService;
BLECharacteristic *smartSpin2kCharacteristic;
std::string FTMSWrite = "";
/******** Bit field Flag Example ********/
// 00000000000000000001 - 1 - 0x001 - Pedal Power Balance Present
// 00000000000000000010 - 2 - 0x002 - Pedal Power Balance Reference
// 00000000000000000100 - 4 - 0x004 - Accumulated Torque Present
// 00000000000000001000 - 8 - 0x008 - Accumulated Torque Source
// 00000000000000010000 - 16 - 0x010 - Wheel Revolution Data Present
// 00000000000000100000 - 32 - 0x020 - Crank Revolution Data Present
// 00000000000001000000 - 64 - 0x040 - Extreme Force Magnitudes Present
// 00000000000010000000 - 128 - 0x080 - Extreme Torque Magnitudes Present
// 00000000000100000000 - Extreme Angles Present (bit8)
// 00000000001000000000 - Top Dead Spot Angle Present (bit 9)
// 00000000010000000000 - Bottom Dead Spot Angle Present (bit 10)
// 00000000100000000000 - Accumulated Energy Present (bit 11)
// 00000001000000000000 - Offset Compensation Indicator (bit 12)
// 98765432109876543210 - bit placement helper :)
// 00000000001001000000
// 00000101000010000110
// 00000000100001010100
// 100000
byte heartRateMeasurement[2] = {0x00, 0x00};
byte cyclingPowerMeasurement[9] = {0b0000000100011, 0, 200, 0, 0, 0, 0, 0, 0};
byte cpsLocation[1] = {0b000}; // sensor location 5 == left crank
byte cpFeature[1] = {0b00100000}; // crank information present // 3rd & 2nd
// byte is reported power
// byte ftmsService[6] = {0x00, 0x00, 0x00, 0b01, 0b0100000, 0x00};
byte ftmsControlPoint[3] = {0x00, 0x00, 0x00}; // 0x08 we need to return a value of 1 for any successful change
byte ftmsMachineStatus[7] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // 0x04 means started by the user
struct FitnessMachineFeature ftmsFeature = {FitnessMachineFeatureFlags::Types::CadenceSupported | FitnessMachineFeatureFlags::Types::HeartRateMeasurementSupported |
FitnessMachineFeatureFlags::Types::PowerMeasurementSupported | FitnessMachineFeatureFlags::Types::InclinationSupported |
FitnessMachineFeatureFlags::Types::ResistanceLevelSupported,
FitnessMachineTargetFlags::PowerTargetSettingSupported | FitnessMachineTargetFlags::Types::InclinationTargetSettingSupported |
FitnessMachineTargetFlags::Types::ResistanceTargetSettingSupported |
FitnessMachineTargetFlags::Types::IndoorBikeSimulationParametersSupported |
FitnessMachineTargetFlags::Types::SpinDownControlSupported};
uint8_t ftmsIndoorBikeData[14] = {0x44, 0x02, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; // 00000000100001010100 ISpeed, ICAD,
// TDistance, IPower, ETime
uint8_t ftmsResistanceLevelRange[6] = {0x00, 0x01, 0x25, 0x00, 0x01, 0x00}; // 1:37 increment 1
uint8_t ftmsPowerRange[6] = {0x00, 0x01, 0xA0, 0x0F, 0x01, 0x00}; // 1:4000 watts increment 1
uint8_t ftmsInclinationRange[6] = {0x38, 0xff, 0xc8, 0x00, 0x01, 0x00}; // -20.0:20.0 increment .1
uint8_t ftmsTrainingStatus[2] = {0x08, 0x00};
uint8_t ss2kCustomCharacteristicValue[3] = {0x00, 0x00, 0x00};
void logCharacteristic(char *buffer, const size_t bufferCapacity, const byte *data, const size_t dataLength, const NimBLEUUID serviceUUID, const NimBLEUUID charUUID,
const char *format, ...) {
int bufferLength = ss2k_log_hex_to_buffer(data, dataLength, buffer, 0, bufferCapacity);
bufferLength += snprintf(buffer + bufferLength, bufferCapacity - bufferLength, "-> %s | %s | ", serviceUUID.toString().c_str(), charUUID.toString().c_str());
va_list args;
va_start(args, format);
bufferLength += vsnprintf(buffer + bufferLength, bufferCapacity - bufferLength, format, args);
va_end(args);
SS2K_LOG(BLE_SERVER_LOG_TAG, "%s", buffer);
SEND_TO_TELEGRAM(String(buffer));
}
void startBLEServer() {
// Server Setup
SS2K_LOG(BLE_SERVER_LOG_TAG, "Starting BLE Server");
pServer = BLEDevice::createServer();
// HEART RATE MONITOR SERVICE SETUP
pHeartService = pServer->createService(HEARTSERVICE_UUID);
heartRateMeasurementCharacteristic = pHeartService->createCharacteristic(HEARTCHARACTERISTIC_UUID, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
// Power Meter MONITOR SERVICE SETUP
pPowerMonitor = pServer->createService(CYCLINGPOWERSERVICE_UUID);
cyclingPowerMeasurementCharacteristic = pPowerMonitor->createCharacteristic(CYCLINGPOWERMEASUREMENT_UUID, NIMBLE_PROPERTY::NOTIFY);
cyclingPowerFeatureCharacteristic = pPowerMonitor->createCharacteristic(CYCLINGPOWERFEATURE_UUID, NIMBLE_PROPERTY::READ);
sensorLocationCharacteristic = pPowerMonitor->createCharacteristic(SENSORLOCATION_UUID, NIMBLE_PROPERTY::READ);
// Fitness Machine service setup
pFitnessMachineService = pServer->createService(FITNESSMACHINESERVICE_UUID);
fitnessMachineFeature = pFitnessMachineService->createCharacteristic(FITNESSMACHINEFEATURE_UUID, NIMBLE_PROPERTY::READ);
fitnessMachineControlPoint = pFitnessMachineService->createCharacteristic(FITNESSMACHINECONTROLPOINT_UUID, NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::INDICATE);
fitnessMachineStatusCharacteristic = pFitnessMachineService->createCharacteristic(FITNESSMACHINESTATUS_UUID, NIMBLE_PROPERTY::NOTIFY);
fitnessMachineIndoorBikeData = pFitnessMachineService->createCharacteristic(FITNESSMACHINEINDOORBIKEDATA_UUID, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
fitnessMachineResistanceLevelRange = pFitnessMachineService->createCharacteristic(FITNESSMACHINERESISTANCELEVELRANGE_UUID, NIMBLE_PROPERTY::READ);
fitnessMachinePowerRange = pFitnessMachineService->createCharacteristic(FITNESSMACHINEPOWERRANGE_UUID, NIMBLE_PROPERTY::READ);
fitnessMachineInclinationRange = pFitnessMachineService->createCharacteristic(FITNESSMACHINEINCLINATIONRANGE_UUID, NIMBLE_PROPERTY::READ);
fitnessMachineTrainingStatus = pFitnessMachineService->createCharacteristic(FITNESSMACHINETRAININGSTATUS_UUID, NIMBLE_PROPERTY::NOTIFY);
pSmartSpin2kService = pServer->createService(SMARTSPIN2K_SERVICE_UUID);
smartSpin2kCharacteristic =
pSmartSpin2kService->createCharacteristic(SMARTSPIN2K_CHARACTERISTIC_UUID, NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::INDICATE | NIMBLE_PROPERTY::NOTIFY);
pServer->setCallbacks(new MyServerCallbacks());
// Creating Characteristics
heartRateMeasurementCharacteristic->setValue(heartRateMeasurement, 2);
cyclingPowerMeasurementCharacteristic->setValue(cyclingPowerMeasurement, 9);
cyclingPowerFeatureCharacteristic->setValue(cpFeature, 1);
sensorLocationCharacteristic->setValue(cpsLocation, 1);
fitnessMachineFeature->setValue(ftmsFeature.bytes, sizeof(ftmsFeature));
fitnessMachineControlPoint->setValue(ftmsControlPoint, 3);
fitnessMachineIndoorBikeData->setValue(ftmsIndoorBikeData, 14);
fitnessMachineStatusCharacteristic->setValue(ftmsMachineStatus, 7);
fitnessMachineResistanceLevelRange->setValue(ftmsResistanceLevelRange, 6);
fitnessMachinePowerRange->setValue(ftmsPowerRange, 6);
fitnessMachineInclinationRange->setValue(ftmsInclinationRange, 6);
smartSpin2kCharacteristic->setValue(ss2kCustomCharacteristicValue, 3);
fitnessMachineControlPoint->setCallbacks(new MyCallbacks());
smartSpin2kCharacteristic->setCallbacks(new ss2kCustomCharacteristicCallbacks());
pHeartService->start();
pPowerMonitor->start();
pFitnessMachineService->start();
pSmartSpin2kService->start();
// const std::string fitnessData = {0b00000001, 0b00100000, 0b00000000};
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
// pAdvertising->setServiceData(FITNESSMACHINESERVICE_UUID, fitnessData);
pAdvertising->addServiceUUID(FITNESSMACHINESERVICE_UUID);
pAdvertising->addServiceUUID(CYCLINGPOWERSERVICE_UUID);
pAdvertising->addServiceUUID(HEARTSERVICE_UUID);
pAdvertising->addServiceUUID(SMARTSPIN2K_SERVICE_UUID);
pAdvertising->setMaxInterval(250);
pAdvertising->setMinInterval(160);
pAdvertising->setScanResponse(true);
BLEDevice::startAdvertising();
SS2K_LOG(BLE_SERVER_LOG_TAG, "Bluetooth Characteristic defined!");
}
bool spinDown() {
std::string rxValue = fitnessMachineStatusCharacteristic->getValue();
if (rxValue[0] != 0x14) {
return false;
}
uint8_t spinStatus[2] = {0x14, 0x01};
if (rxValue[1] == 0x01) {
// debugDirector("Spin Down Initiated", true);
vTaskDelay(1000 / portTICK_RATE_MS);
spinStatus[1] = 0x04; // send Stop Pedaling
fitnessMachineStatusCharacteristic->setValue(spinStatus, 2);
}
if (rxValue[1] == 0x04) {
// debugDirector("Stop Pedaling", true);
vTaskDelay(1000 / portTICK_RATE_MS);
spinStatus[1] = 0x02; // Success
fitnessMachineStatusCharacteristic->setValue(spinStatus, 2);
}
if (rxValue[1] == 0x02) {
// debugDirector("Success", true);
spinStatus[0] = 0x00;
spinStatus[1] = 0x00; // Success
fitnessMachineStatusCharacteristic->setValue(spinStatus, 2);
uint8_t returnValue[3] = {0x00, 0x00, 0x00};
fitnessMachineControlPoint->setValue(returnValue, 3);
fitnessMachineControlPoint->indicate();
}
fitnessMachineStatusCharacteristic->notify();
return true;
}
// as a note, Trainer Road sends 50w target whenever the app is connected.
void computeERG(int newSetPoint) {
// SS2K_LOG(BLE_SERVER_LOG_TAG, "ComputeERG. Setpoint: %d", newSetPoint);
if (userConfig.getERGMode() && spinBLEClient.connectedPM) {
// continue
} else {
// SS2K_LOG(BLE_SERVER_LOG_TAG, "ERG request but ERG off or no connected PM");
return;
}
static bool userIsPedaling = true;
static int setPoint = 0;
float incline = userConfig.getIncline();
float newIncline = incline;
int amountToChangeIncline = 0;
int wattChange = userConfig.getSimulatedWatts() - setPoint;
if (newSetPoint > 0) { // only update the value if new value is sent
setPoint = newSetPoint;
}
if (setPoint < 50) { // minumum setPoint
setPoint = 50;
}
if (userConfig.getSimulatedCad() <= 20) {
if (!userIsPedaling) { // Test so motor stop command only happens once.
motorStop(); // release tension
return;
}
userIsPedaling = false;
userConfig.setIncline(incline-userConfig.getShiftStep()*2);
// Cadence too low, nothing to do here
return;
}
userIsPedaling = true;
amountToChangeIncline = wattChange * userConfig.getERGSensitivity();
if (abs(wattChange) < WATTS_PER_SHIFT) {
// As the desired value gets closer, make smaller changes for a smoother experience
amountToChangeIncline *= SUB_SHIFT_SCALE;
}
// limit to 10 shifts at a time
if (abs(amountToChangeIncline) > userConfig.getShiftStep() * 2) {
if (amountToChangeIncline > 5) {
amountToChangeIncline = userConfig.getShiftStep() * 2;
}
if (amountToChangeIncline < -5) {
amountToChangeIncline = -(userConfig.getShiftStep() * 2);
}
}
// Reduce the amount per loop (don't try to oneshot it) and scale the movement the higher the watt target is as higher wattages require less knob movement.
// amountToChangeIncline = amountToChangeIncline / ((userConfig.getSimulatedWatts() / 100) + .1); // +.1 to eliminate possible divide by zero.
newIncline = incline - amountToChangeIncline;
userConfig.setIncline(newIncline);
// SS2K_LOG(BLE_SERVER_LOG_TAG, "newincline: %f", newIncline);
}
void updateIndoorBikeDataChar() {
float cadRaw = userConfig.getSimulatedCad();
int cad = static_cast<int>(cadRaw * 2);
int watts = userConfig.getSimulatedWatts();
int hr = userConfig.getSimulatedHr();
int speed = 0;
float speedRaw = userConfig.getSimulatedSpeed();
if (speedRaw <= 0) {
float gearRatio = 1;
speed = ((cad * 2.75 * 2.08 * 60 * gearRatio) / 10);
} else {
speed = static_cast<int>(speedRaw * 100);
}
ftmsIndoorBikeData[2] = (uint8_t)(speed & 0xff);
ftmsIndoorBikeData[3] = (uint8_t)(speed >> 8);
ftmsIndoorBikeData[4] = (uint8_t)(cad & 0xff);
ftmsIndoorBikeData[5] = (uint8_t)(cad >> 8); // cadence value
ftmsIndoorBikeData[6] = (uint8_t)(watts & 0xff);
ftmsIndoorBikeData[7] = (uint8_t)(watts >> 8); // power value, constrained to avoid negative values,
// although the specification allows for a sint16
ftmsIndoorBikeData[8] = (uint8_t)hr;
fitnessMachineIndoorBikeData->setValue(ftmsIndoorBikeData, 9);
fitnessMachineIndoorBikeData->notify();
const int kLogBufCapacity = 200; // Data(30), Sep(data/2), Arrow(3), CharId(37), Sep(3), CharId(37), Sep(3), Name(10), Prefix(2), HR(7), SEP(1), CD(10), SEP(1), PW(8), SEP(1),
// SD(7), Suffix(2), Nul(1), rounded up
char logBuf[kLogBufCapacity];
const size_t ftmsIndoorBikeDataLength = sizeof(ftmsIndoorBikeData) / sizeof(ftmsIndoorBikeData[0]);
logCharacteristic(logBuf, kLogBufCapacity, ftmsIndoorBikeData, ftmsIndoorBikeDataLength, FITNESSMACHINESERVICE_UUID, fitnessMachineIndoorBikeData->getUUID(),
"FTMS(IBD)[ HR(%d) CD(%.2f) PW(%d) SD(%.2f) ]", hr % 1000, fmodf(cadRaw, 1000.0), watts % 10000, fmodf(speed, 1000.0));
} // ^^Using the New Way of setting Bytes.
void updateCyclingPowerMeasurementChar() {
int power = userConfig.getSimulatedWatts();
int remainder, quotient;
quotient = power / 256;
remainder = power % 256;
cyclingPowerMeasurement[2] = remainder;
cyclingPowerMeasurement[3] = quotient;
cyclingPowerMeasurementCharacteristic->setValue(cyclingPowerMeasurement, 9);
float cadence = userConfig.getSimulatedCad();
if (cadence > 0) {
float crankRevPeriod = (60 * 1024) / cadence;
spinBLEClient.cscCumulativeCrankRev++;
spinBLEClient.cscLastCrankEvtTime += crankRevPeriod;
int remainder, quotient;
quotient = spinBLEClient.cscCumulativeCrankRev / 256;
remainder = spinBLEClient.cscCumulativeCrankRev % 256;
cyclingPowerMeasurement[5] = remainder;
cyclingPowerMeasurement[6] = quotient;
quotient = spinBLEClient.cscLastCrankEvtTime / 256;
remainder = spinBLEClient.cscLastCrankEvtTime % 256;
cyclingPowerMeasurement[7] = remainder;
cyclingPowerMeasurement[8] = quotient;
}
cyclingPowerMeasurementCharacteristic->notify();
const int kLogBufCapacity =
150; // Data(18), Sep(data/2), Arrow(3), CharId(37), Sep(3), CharId(37), Sep(3),Name(8), Prefix(2), CD(10), SEP(1), PW(8), Suffix(2), Nul(1), rounded up
char logBuf[kLogBufCapacity];
const size_t cyclingPowerMeasurementLength = sizeof(cyclingPowerMeasurement) / sizeof(cyclingPowerMeasurement[0]);
logCharacteristic(logBuf, kLogBufCapacity, cyclingPowerMeasurement, cyclingPowerMeasurementLength, FITNESSMACHINESERVICE_UUID, fitnessMachineIndoorBikeData->getUUID(),
"CPS(CPM)[ CD(%.2f) PW(%d) ]", cadence > 0 ? fmodf(cadence, 1000.0) : 0, power % 10000);
}
void updateHeartRateMeasurementChar() {
int hr = userConfig.getSimulatedHr();
heartRateMeasurement[1] = hr;
heartRateMeasurementCharacteristic->setValue(heartRateMeasurement, 2);
heartRateMeasurementCharacteristic->notify();
const int kLogBufCapacity = 125; // Data(10), Sep(data/2), Arrow(3), CharId(37), Sep(3), CharId(37), Sep(3), Name(8), Prefix(2), HR(7), Suffix(2), Nul(1), rounded up
char logBuf[kLogBufCapacity];
const size_t heartRateMeasurementLength = sizeof(heartRateMeasurement) / sizeof(heartRateMeasurement[0]);
logCharacteristic(logBuf, kLogBufCapacity, heartRateMeasurement, heartRateMeasurementLength, HEARTSERVICE_UUID, heartRateMeasurementCharacteristic->getUUID(),
"HRS(HRM)[ HR(%d) ]", hr % 1000);
}
// Creating Server Connection Callbacks
void MyServerCallbacks::onConnect(BLEServer *pServer, ble_gap_conn_desc *desc) {
SS2K_LOG(BLE_SERVER_LOG_TAG, "Bluetooth Remote Client Connected: %s Connected Clients: %d", NimBLEAddress(desc->peer_ota_addr).toString().c_str(), pServer->getConnectedCount());
updateConnParametersFlag = true;
bleConnDesc = desc->conn_handle;
if (pServer->getConnectedCount() < CONFIG_BT_NIMBLE_MAX_CONNECTIONS - NUM_BLE_DEVICES) {
BLEDevice::startAdvertising();
} else {
SS2K_LOG(BLE_SERVER_LOG_TAG, "Max Remote Client Connections Reached");
BLEDevice::stopAdvertising();
}
}
void MyServerCallbacks::onDisconnect(BLEServer *pServer) {
SS2K_LOG(BLE_SERVER_LOG_TAG, "Bluetooth Remote Client Disconnected. Remaining Clients: %d", pServer->getConnectedCount());
BLEDevice::startAdvertising();
}
void MyCallbacks::onWrite(BLECharacteristic *pCharacteristic) {
FTMSWrite = pCharacteristic->getValue();
}
void processFTMSWrite() {
if (FTMSWrite == "") {
return;
}
BLECharacteristic *pCharacteristic = NimBLEDevice::getServer()->getServiceByUUID(FITNESSMACHINESERVICE_UUID)->getCharacteristic(FITNESSMACHINECONTROLPOINT_UUID);
std::string rxValue = FTMSWrite;
if (rxValue.length() > 1) {
uint8_t *pData = reinterpret_cast<uint8_t *>(&rxValue[0]);
int length = rxValue.length();
const int kLogBufCapacity = (rxValue.length() * 2) + 60; // largest comment is 48 VV
char logBuf[kLogBufCapacity];
int logBufLength = ss2k_log_hex_to_buffer(pData, length, logBuf, 0, kLogBufCapacity);
int port = 0;
uint8_t returnValue[3] = {0x80, (uint8_t)rxValue[0], 0x02};
switch ((uint8_t)rxValue[0]) {
case 0x00: // request control
logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "-> Control Request");
returnValue[2] = 0x01;
//userConfig.setERGMode(false);
pCharacteristic->setValue(returnValue, 3);
ftmsTrainingStatus[1] = 0x01;
fitnessMachineTrainingStatus->setValue(ftmsTrainingStatus, 2);
fitnessMachineTrainingStatus->notify();
pCharacteristic->setValue(returnValue, 3);
break;
case 0x03: { // inclination level setting - differs from sim mode as no negative numbers
port = (rxValue[2] << 8) + rxValue[1];
port *= 10;
userConfig.setIncline(port);
userConfig.setERGMode(false);
logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "-> Incline Mode: %2f", userConfig.getIncline() / 100);
returnValue[2] = 0x01;
uint8_t inclineStatus[3] = {0x06, (uint8_t)rxValue[1], (uint8_t)rxValue[2]};
fitnessMachineStatusCharacteristic->setValue(inclineStatus, 3);
pCharacteristic->setValue(returnValue, 3);
ftmsTrainingStatus[1] = 0x00;
fitnessMachineTrainingStatus->setValue(ftmsTrainingStatus, 2);
fitnessMachineTrainingStatus->notify();
} break;
case 0x04: { // Resistance level setting
int targetResistance = rxValue[1];
userConfig.setShifterPosition(targetResistance);
userConfig.setERGMode(false);
logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "-> Resistance Mode: %d", userConfig.getShifterPosition());
returnValue[2] = 0x01;
uint8_t resistanceStatus[2] = {0x07, rxValue[1]};
fitnessMachineStatusCharacteristic->setValue(resistanceStatus, 3);
pCharacteristic->setValue(returnValue, 3);
ftmsTrainingStatus[1] = 0x00;
fitnessMachineTrainingStatus->setValue(ftmsTrainingStatus, 2);
fitnessMachineTrainingStatus->notify();
} break;
case 0x05: { // Power Level Mode
if (spinBLEClient.connectedPM || userConfig.getSimulateWatts()) {
int targetWatts = bytes_to_u16(rxValue[2], rxValue[1]);
userConfig.setERGMode(true);
computeERG(targetWatts);
logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "-> ERG Mode Target: %d Current: %d Incline: %2f", targetWatts,
userConfig.getSimulatedWatts(), userConfig.getIncline() / 100);
returnValue[2] = 0x01;
uint8_t ERGStatus[3] = {0x08, (uint8_t)rxValue[1], 0x01};
fitnessMachineStatusCharacteristic->setValue(ERGStatus, 3);
ftmsTrainingStatus[1] = 0x0C;
fitnessMachineTrainingStatus->setValue(ftmsTrainingStatus, 2);
fitnessMachineTrainingStatus->notify();
} else {
returnValue[2] = 0x02; // no power meter connected, so no ERG
logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "-> ERG Mode: No Power Meter Connected");
}
pCharacteristic->setValue(returnValue, 3);
} break;
case 0x07: // Start training
returnValue[2] = 0x01;
logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "-> Start Training");
pCharacteristic->setValue(returnValue, 3);
ftmsTrainingStatus[1] = 0x00;
fitnessMachineTrainingStatus->setValue(ftmsTrainingStatus, 2);
fitnessMachineTrainingStatus->notify();
break;
case 0x11: { // sim mode
signed char buf[2];
// int16_t windSpeed = (rxValue[2] << 8) + rxValue[1];
buf[0] = rxValue[3]; // (Least significant byte)
buf[1] = rxValue[4]; // (Most significant byte)
// int8_t rollingResistance = rxValue[5];
// int8_t windResistance = rxValue[6];
port = bytes_to_u16(buf[1], buf[0]);
userConfig.setIncline(port);
logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "-> Sim Mode Incline %2f", userConfig.getIncline() / 100);
userConfig.setERGMode(false);
returnValue[2] = 0x01;
uint8_t simStatus[7] = {0x12, (uint8_t)rxValue[1], (uint8_t)rxValue[2], (uint8_t)rxValue[3], (uint8_t)rxValue[4], (uint8_t)rxValue[5], (uint8_t)rxValue[6]};
fitnessMachineStatusCharacteristic->setValue(simStatus, 7);
pCharacteristic->setValue(returnValue, 3);
ftmsTrainingStatus[1] = 0x00;
fitnessMachineTrainingStatus->setValue(ftmsTrainingStatus, 2);
fitnessMachineTrainingStatus->notify();
} break;
case 0x13: { // Spin Down
logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "-> Spin Down Requested");
uint8_t spinStatus[2] = {0x14, 0x01}; // send low and high speed targets
fitnessMachineStatusCharacteristic->setValue(spinStatus, 2);
uint8_t controlPoint[6] = {0x80, 0x01, 0x24, 0x03, 0x96, 0x0e}; // send low and high speed targets
pCharacteristic->setValue(controlPoint, 6);
ftmsTrainingStatus[1] = 0x00;
fitnessMachineTrainingStatus->setValue(ftmsTrainingStatus, 2);
fitnessMachineTrainingStatus->notify();
} break;
default:
logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "-> Unsupported FTMS Request");
pCharacteristic->setValue(returnValue, 3);
}
SS2K_LOG(BLE_SERVER_LOG_TAG, "%s", logBuf);
fitnessMachineStatusCharacteristic->notify();
} else {
SS2K_LOG(BLE_SERVER_LOG_TAG, "App wrote nothing ");
SS2K_LOG(BLE_SERVER_LOG_TAG, "assuming it's a Control request");
uint8_t controlPoint[3] = {0x80, 0x00, 0x01};
//userConfig.setERGMode(true);
pCharacteristic->setValue(controlPoint, 3);
ftmsTrainingStatus[1] = 0x01;
fitnessMachineTrainingStatus->setValue(ftmsTrainingStatus, 2);
fitnessMachineTrainingStatus->notify();
}
FTMSWrite = "";
}
void controlPointIndicate() { fitnessMachineControlPoint->indicate(); }
// Return number of clients connected to our server.
int connectedClientCount() {
if (BLEDevice::getServer()) {
return BLEDevice::getServer()->getConnectedCount();
} else {
return 0;
}
}
void calculateInstPwrFromHR() {
static int oldHR = userConfig.getSimulatedHr();
static int newHR = userConfig.getSimulatedHr();
static double delta = 0;
oldHR = newHR; // Copying HR from Last loop
newHR = userConfig.getSimulatedHr();
delta = (newHR - oldHR) / (BLE_CLIENT_DELAY / 1000);
// userConfig.setSimulatedWatts((s1Pwr*s2HR)-(s2Pwr*S1HR))/(S2HR-s1HR)+(userConfig.getSimulatedHr(*((s1Pwr-s2Pwr)/(s1HR-s2HR)));
int avgP = ((userPWC.session1Pwr * userPWC.session2HR) - (userPWC.session2Pwr * userPWC.session1HR)) / (userPWC.session2HR - userPWC.session1HR) +
(newHR * ((userPWC.session1Pwr - userPWC.session2Pwr) / (userPWC.session1HR - userPWC.session2HR)));
if (avgP < 50) {
avgP = 50;
}
if (delta < 0) {
// magic math here for inst power
}
if (delta > 0) {
// magic math here for inst power
}
#ifndef DEBUG_HR_TO_PWR
userConfig.setSimulatedWatts(avgP);
userConfig.setSimulatedCad(90);
#endif // DEBUG_HR_TO_PWR
SS2K_LOG(BLE_SERVER_LOG_TAG, "Power From HR: %d", avgP);
}
/*
Custom Characteristic for userConfig Variable manipulation via BLE
An example follows to read/write 26.3kph to simulatedSpeed:
simulatedSpeed is a float and first needs to be converted to int by *10 for transmission, so convert 26.3kph to 263 (multiply by 10)
Decimal 263 == hexidecimal 0107 but the data needs to be converted to LSO, MSO to match the rest of the BLE spec so 263 == 0x07, 0x01 (LSO,MSO)
So,
If client wants to write (0x02) int value 263 (0x07 0x01) to simulatedSpeed(0x06):
Client Writes:
0x02, 0x06, 0x07, 0x01
(operator, variable, LSO, MSO)
Server will then indicate:
0x80, 0x06, 0x07, 0x01
(status, variable, LSO, MSO)
Example to read (0x01) from simulatedSpeed (0x06)
Client Writes:
0x01, 0x06
Server will then indicate:
0x80, 0x06, 0x07, 0x01
success, simulatedSpeed,0x07,0x01
Pay special attention to the float values below. Since they have to be transmitted as an int, some are converted *100, others are converted *10.
True values are >00. False are 00.
*/
void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
uint8_t read = 0x01; // value to request read operation
uint8_t write = 0x02; // Value to request write operation
uint8_t error = 0xff; // value server error/unable
uint8_t success = 0x80; // value for success
size_t returnLength = rxValue.length();
uint8_t returnValue[returnLength];
returnValue[0] = error;
for (size_t i = 1; i < returnLength; i++) {
returnValue[i] = rxValue[i];
}
SS2K_LOG(BLE_SERVER_LOG_TAG, "Custom Request Received");
switch (rxValue[1]) {
case BLE_firmwareUpdateURL: // 0x01
returnValue[0] = error;
break;
case BLE_incline: { // 0x02
returnValue[0] = success;
if (rxValue[0] == read) {
int inc = userConfig.getIncline() * 100;
returnValue[2] = (uint8_t)(inc & 0xff);
returnValue[3] = (uint8_t)(inc >> 8);
returnLength += 2;
}
if (rxValue[0] == write) {
userConfig.setIncline(bytes_to_u16(rxValue[3], rxValue[2]) / 100);
}
} break;
case BLE_simulatedWatts: // 0x03
returnValue[0] = success;
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(userConfig.getSimulatedWatts() & 0xff);
returnValue[3] = (uint8_t)(userConfig.getSimulatedWatts() >> 8);
returnLength += 2;
}
if (rxValue[0] == write) {
userConfig.setSimulatedWatts(bytes_to_u16(rxValue[3], rxValue[2]));
}
break;
case BLE_simulatedHr: // 0x04
returnValue[0] = success;
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(userConfig.getSimulatedHr() & 0xff);
returnValue[3] = (uint8_t)(userConfig.getSimulatedHr() >> 8);
returnLength += 2;
}
if (rxValue[0] == write) {
userConfig.setSimulatedHr(bytes_to_u16(rxValue[3], rxValue[2]));
}
break;
case BLE_simulatedCad: // 0x05
returnValue[0] = success;
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(userConfig.getSimulatedCad() & 0xff);
returnValue[3] = (uint8_t)(userConfig.getSimulatedCad() >> 8);
returnLength += 2;
}
if (rxValue[0] == write) {
userConfig.setSimulatedCad(bytes_to_u16(rxValue[3], rxValue[2]));
}
break;
case BLE_simulatedSpeed: { // 0x06
returnValue[0] = success;
int spd = userConfig.getSimulatedSpeed() * 10;
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(spd & 0xff);
returnValue[3] = (uint8_t)(spd >> 8);
returnLength += 2;
}
if (rxValue[0] == write) {
userConfig.setSimulatedSpeed(bytes_to_u16(rxValue[3], rxValue[2]) / 10);
}
} break;
case BLE_deviceName: // 0x07
returnValue[0] = error;
break;
case BLE_shiftStep: // 0x08
returnValue[0] = success;
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(userConfig.getShiftStep() & 0xff);
returnValue[3] = (uint8_t)(userConfig.getShiftStep() >> 8);
returnLength += 2;
}
if (rxValue[0] == write) {
userConfig.setShiftStep(bytes_to_u16(rxValue[3], rxValue[2]));
}
break;
case BLE_stepperPower: // 0x09
returnValue[0] = success;
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(userConfig.getStepperPower() & 0xff);
returnValue[3] = (uint8_t)(userConfig.getStepperPower() >> 8);
returnLength += 2;
}
if (rxValue[0] == write) {
userConfig.setStepperPower(bytes_to_u16(rxValue[3], rxValue[2]));
updateStepperPower();
}
break;
case BLE_stealthchop: // 0x0A
returnValue[0] = success;
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(userConfig.getStealthchop());
returnLength += 1;
}
if (rxValue[0] == write) {
userConfig.setStealthChop(rxValue[2]);
updateStealthchop();
}
break;
case BLE_inclineMultiplier: { // 0x0B
returnValue[0] = success;
int inc = userConfig.getInclineMultiplier();
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(inc & 0xff);
returnValue[3] = (uint8_t)(inc >> 8);
returnLength += 2;
}
if (rxValue[0] == write) {
userConfig.setInclineMultiplier(bytes_to_u16(rxValue[3], rxValue[2]));
}
} break;
case BLE_powerCorrectionFactor: { // 0x0C
returnValue[0] = success;
int pcf = userConfig.getPowerCorrectionFactor() * 10;
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(pcf & 0xff);
returnValue[3] = (uint8_t)(pcf >> 8);
returnLength += 2;
}
if (rxValue[0] == write) {
userConfig.setPowerCorrectionFactor(bytes_to_u16(rxValue[3], rxValue[2]) / 10);
}
} break;
case BLE_simulateHr: // 0x0D
returnValue[0] = success;
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(userConfig.getSimulateHr());
returnLength += 1;
}
if (rxValue[0] == write) {
userConfig.setSimulateHr(rxValue[2]);
}
break;
case BLE_simulateWatts: // 0x0E
returnValue[0] = success;
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(userConfig.getSimulateWatts());
returnLength += 1;
}
if (rxValue[0] == write) {
userConfig.setSimulateWatts(rxValue[2]);
}
break;
case BLE_simulateCad: // 0x0F
returnValue[0] = success;
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(userConfig.getSimulateCad());
returnLength += 1;
}
if (rxValue[0] == write) {
userConfig.setSimulateCad(rxValue[2]);
}
break;
case BLE_ERGMode: // 0x10
returnValue[0] = success;
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(userConfig.getERGMode());
returnLength += 1;
}
if (rxValue[0] == write) {
userConfig.setERGMode(rxValue[2]);
}
break;
case BLE_autoUpdate: // 0x11
returnValue[0] = success;
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(userConfig.getautoUpdate());
returnLength += 1;
}
if (rxValue[0] == write) {
userConfig.setAutoUpdate(rxValue[2]);
}
break;
case BLE_ssid: // 0x12
returnValue[0] = error;
break;
case BLE_password: // 0x13
returnValue[0] = error;
break;
case BLE_foundDevices: // 0x14
returnValue[0] = error;
break;
case BLE_connectedPowerMeter: // 0x15
returnValue[0] = error;
break;
case BLE_connectedHeartMonitor: // 0x16
returnValue[0] = error;
break;
case BLE_shifterPosition: // 0x17
returnValue[0] = success;
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(userConfig.getShifterPosition() & 0xff);
returnValue[3] = (uint8_t)(userConfig.getShifterPosition() >> 8);
returnLength += 2;
}
if (rxValue[0] == write) {
userConfig.setShifterPosition(bytes_to_u16(rxValue[3], rxValue[2]));
}
break;
case BLE_saveToSpiffs: // 0x18
userConfig.saveToSPIFFS();
returnValue[0] = success;
break;
case BLE_targetPosition: // 0x19
returnValue[0] = success;
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(targetPosition & 0xff);
returnValue[3] = (uint8_t)(targetPosition >> 8);
returnValue[4] = (uint8_t)(targetPosition >> 16);
returnValue[5] = (uint8_t)(targetPosition >> 24);
returnLength += 4;
}
if (rxValue[0] == write) {
targetPosition = (int32_t((uint8_t)(rxValue[2]) << 0 | (uint8_t)(rxValue[3]) << 8 | (uint8_t)(rxValue[4]) << 16 | (uint8_t)(rxValue[5]) << 24));
}
break;
case BLE_externalControl: // 0x1A
returnValue[0] = success;
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(externalControl);
returnLength += 1;
}
if (rxValue[0] == write) {
externalControl = static_cast<bool>(rxValue[2]);
}
break;
case BLE_syncMode: // 0x1B
returnValue[0] = success;
if (rxValue[0] == read) {
returnValue[2] = (uint8_t)(syncMode);
returnLength += 1;
}
if (rxValue[0] == write) {
syncMode = static_cast<bool>(rxValue[2]);
}
break;
}
pCharacteristic->setValue(returnValue, returnLength);
pCharacteristic->indicate();
}
void SpinBLEServer::notifyShift(bool upDown) {
uint8_t returnValue[4];
returnValue[0] = 0x80;
returnValue[1] = BLE_shifterPosition;
returnValue[2] = (uint8_t)(userConfig.getShifterPosition() & 0xff);
returnValue[3] = (uint8_t)(userConfig.getShifterPosition() >> 8);
smartSpin2kCharacteristic->setValue(returnValue, 4);
smartSpin2kCharacteristic->notify(true);
}