Skip to content

Commit 60a49af

Browse files
committed
Add MotionService : expose step count and RAW X/Y/Z values to the host.
1 parent d1f5015 commit 60a49af

8 files changed

Lines changed: 190 additions & 3 deletions

File tree

src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ list(APPEND SOURCE_FILES
477477
components/ble/ImmediateAlertService.cpp
478478
components/ble/ServiceDiscovery.cpp
479479
components/ble/HeartRateService.cpp
480+
components/ble/MotionService.cpp
480481
components/firmwarevalidator/FirmwareValidator.cpp
481482
components/motor/MotorController.cpp
482483
components/settings/Settings.cpp
@@ -545,6 +546,7 @@ list(APPEND RECOVERY_SOURCE_FILES
545546
components/ble/ServiceDiscovery.cpp
546547
components/ble/NavigationService.cpp
547548
components/ble/HeartRateService.cpp
549+
components/ble/MotionService.cpp
548550
components/firmwarevalidator/FirmwareValidator.cpp
549551
components/settings/Settings.cpp
550552
components/timer/TimerController.cpp
@@ -652,6 +654,7 @@ set(INCLUDE_FILES
652654
components/ble/ServiceDiscovery.h
653655
components/ble/BleClient.h
654656
components/ble/HeartRateService.h
657+
components/ble/MotionService.h
655658
components/settings/Settings.h
656659
components/timer/TimerController.h
657660
components/alarm/AlarmController.h
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include "MotionService.h"
2+
#include "components/motion//MotionController.h"
3+
#include "systemtask/SystemTask.h"
4+
5+
using namespace Pinetime::Controllers;
6+
7+
namespace {
8+
// 0002yyxx-78fc-48fe-8e23-433b3a1942d0
9+
constexpr ble_uuid128_t CharUuid(uint8_t x, uint8_t y) {
10+
return ble_uuid128_t{
11+
.u = {.type = BLE_UUID_TYPE_128},
12+
.value = { 0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, x, y, 0x02, 0x00 }
13+
};
14+
}
15+
16+
// 00020000-78fc-48fe-8e23-433b3a1942d0
17+
constexpr ble_uuid128_t BaseUuid() {
18+
return CharUuid(0x00, 0x00);
19+
}
20+
21+
constexpr ble_uuid128_t motionServiceUuid {BaseUuid()};
22+
constexpr ble_uuid128_t stepCountCharUuid {CharUuid(0x01, 0x00)};
23+
constexpr ble_uuid128_t motionValuesCharUuid {CharUuid(0x02, 0x00)};
24+
25+
int MotionServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
26+
auto* motionService = static_cast<MotionService*>(arg);
27+
return motionService->OnStepCountRequested(conn_handle, attr_handle, ctxt);
28+
}
29+
}
30+
31+
// TODO Refactoring - remove dependency to SystemTask
32+
MotionService::MotionService(Pinetime::System::SystemTask& system, Controllers::MotionController& motionController)
33+
: system {system},
34+
motionController {motionController},
35+
characteristicDefinition {{.uuid = &stepCountCharUuid.u,
36+
.access_cb = MotionServiceCallback,
37+
.arg = this,
38+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
39+
.val_handle = &stepCountHandle},
40+
{.uuid = &motionValuesCharUuid.u,
41+
.access_cb = MotionServiceCallback,
42+
.arg = this,
43+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
44+
.val_handle = &motionValuesHandle},
45+
{0}},
46+
serviceDefinition {
47+
{
48+
.type = BLE_GATT_SVC_TYPE_PRIMARY,
49+
.uuid = &motionServiceUuid.u,
50+
.characteristics = characteristicDefinition
51+
},
52+
{0},
53+
} {
54+
// TODO refactor to prevent this loop dependency (service depends on controller and controller depends on service)
55+
motionController.SetService(this);
56+
}
57+
58+
void MotionService::Init() {
59+
int res = 0;
60+
res = ble_gatts_count_cfg(serviceDefinition);
61+
ASSERT(res == 0);
62+
63+
res = ble_gatts_add_svcs(serviceDefinition);
64+
ASSERT(res == 0);
65+
}
66+
67+
int MotionService::OnStepCountRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context) {
68+
if (attributeHandle == stepCountHandle) {
69+
NRF_LOG_INFO("Motion-stepcount : handle = %d", stepCountHandle);
70+
uint32_t buffer = motionController.NbSteps();
71+
72+
int res = os_mbuf_append(context->om, &buffer, 4);
73+
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
74+
} else if(attributeHandle == motionValuesHandle) {
75+
int16_t buffer[3] = { motionController.X(), motionController.Y(), motionController.Z() };
76+
77+
int res = os_mbuf_append(context->om, buffer, 3 * sizeof(int16_t));
78+
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
79+
}
80+
return 0;
81+
}
82+
83+
void MotionService::OnNewStepCountValue(uint8_t stepCount) {
84+
if(!stepCountNoficationEnabled) return;
85+
86+
uint32_t buffer = stepCount;
87+
auto* om = ble_hs_mbuf_from_flat(&buffer, 4);
88+
89+
uint16_t connectionHandle = system.nimble().connHandle();
90+
91+
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
92+
return;
93+
}
94+
95+
ble_gattc_notify_custom(connectionHandle, stepCountHandle, om);
96+
}
97+
void MotionService::OnNewMotionValues(int16_t x, int16_t y, int16_t z) {
98+
if(!motionValuesNoficationEnabled) return;
99+
100+
int16_t buffer[3] = { motionController.X(), motionController.Y(), motionController.Z() };
101+
auto* om = ble_hs_mbuf_from_flat(buffer, 3 * sizeof(int16_t));
102+
103+
uint16_t connectionHandle = system.nimble().connHandle();
104+
105+
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
106+
return;
107+
}
108+
109+
ble_gattc_notify_custom(connectionHandle, motionValuesHandle, om);
110+
}
111+
112+
void MotionService::SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) {
113+
if(attributeHandle == stepCountHandle)
114+
stepCountNoficationEnabled = true;
115+
else if(attributeHandle == motionValuesHandle)
116+
motionValuesNoficationEnabled = true;
117+
}
118+
119+
void MotionService::UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) {
120+
if(attributeHandle == stepCountHandle)
121+
stepCountNoficationEnabled = false;
122+
else if(attributeHandle == motionValuesHandle)
123+
motionValuesNoficationEnabled = false;
124+
}

src/components/ble/MotionService.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#pragma once
2+
#define min // workaround: nimble's min/max macros conflict with libstdc++
3+
#define max
4+
#include <host/ble_gap.h>
5+
#include <atomic>
6+
#undef max
7+
#undef min
8+
9+
namespace Pinetime {
10+
namespace System {
11+
class SystemTask;
12+
}
13+
namespace Controllers {
14+
class MotionController;
15+
class MotionService {
16+
public:
17+
MotionService(Pinetime::System::SystemTask& system, Controllers::MotionController& motionController);
18+
void Init();
19+
int OnStepCountRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context);
20+
void OnNewStepCountValue(uint8_t stepCount);
21+
void OnNewMotionValues(int16_t x, int16_t y, int16_t z);
22+
23+
void SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle);
24+
void UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle);
25+
26+
private:
27+
Pinetime::System::SystemTask& system;
28+
Controllers::MotionController& motionController;
29+
30+
struct ble_gatt_chr_def characteristicDefinition[3];
31+
struct ble_gatt_svc_def serviceDefinition[2];
32+
33+
uint16_t stepCountHandle;
34+
uint16_t motionValuesHandle;
35+
std::atomic_bool stepCountNoficationEnabled {false};
36+
std::atomic_bool motionValuesNoficationEnabled {false};
37+
};
38+
}
39+
}

src/components/ble/NimbleController.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
2323
Pinetime::Controllers::NotificationManager& notificationManager,
2424
Controllers::Battery& batteryController,
2525
Pinetime::Drivers::SpiNorFlash& spiNorFlash,
26-
Controllers::HeartRateController& heartRateController)
26+
Controllers::HeartRateController& heartRateController,
27+
Controllers::MotionController& motionController)
2728
: systemTask {systemTask},
2829
bleController {bleController},
2930
dateTimeController {dateTimeController},
@@ -39,6 +40,7 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
3940
batteryInformationService {batteryController},
4041
immediateAlertService {systemTask, notificationManager},
4142
heartRateService {systemTask, heartRateController},
43+
motionService{systemTask, motionController},
4244
serviceDiscovery({&currentTimeClient, &alertNotificationClient}) {
4345
}
4446

@@ -81,6 +83,7 @@ void NimbleController::Init() {
8183
batteryInformationService.Init();
8284
immediateAlertService.Init();
8385
heartRateService.Init();
86+
motionService.Init();
8487

8588
int rc;
8689
rc = ble_hs_util_ensure_addr(0);

src/components/ble/NimbleController.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "NavigationService.h"
2020
#include "ServiceDiscovery.h"
2121
#include "HeartRateService.h"
22+
#include "MotionService.h"
2223

2324
namespace Pinetime {
2425
namespace Drivers {
@@ -43,7 +44,8 @@ namespace Pinetime {
4344
Pinetime::Controllers::NotificationManager& notificationManager,
4445
Controllers::Battery& batteryController,
4546
Pinetime::Drivers::SpiNorFlash& spiNorFlash,
46-
Controllers::HeartRateController& heartRateController);
47+
Controllers::HeartRateController& heartRateController,
48+
Controllers::MotionController& motionController);
4749
void Init();
4850
void StartAdvertising();
4951
int OnGAPEvent(ble_gap_event* event);
@@ -95,6 +97,7 @@ namespace Pinetime {
9597
BatteryInformationService batteryInformationService;
9698
ImmediateAlertService immediateAlertService;
9799
HeartRateService heartRateService;
100+
MotionService motionService;
98101

99102
uint8_t addrType; // 1 = Random, 0 = PUBLIC
100103
uint16_t connectionHandle = BLE_HS_CONN_HANDLE_NONE;

src/components/motion/MotionController.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
using namespace Pinetime::Controllers;
44

55
void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) {
6+
if (this->nbSteps != nbSteps && service != nullptr) {
7+
service->OnNewStepCountValue(nbSteps);
8+
}
9+
10+
if(service != nullptr && (this->x != x || this->y != y || this->z != z)) {
11+
service->OnNewMotionValues(x, y, z);
12+
}
13+
614
this->x = x;
715
this->y = y;
816
this->z = z;
@@ -41,3 +49,6 @@ void MotionController::Init(Pinetime::Drivers::Bma421::DeviceTypes types) {
4149
default: this->deviceType = DeviceTypes::Unknown; break;
4250
}
4351
}
52+
void MotionController::SetService(Pinetime::Controllers::MotionService* service) {
53+
this->service = service;
54+
}

src/components/motion/MotionController.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <cstdint>
44
#include <drivers/Bma421.h>
5+
#include <components/ble/MotionService.h>
56

67
namespace Pinetime {
78
namespace Controllers {
@@ -39,6 +40,7 @@ namespace Pinetime {
3940
}
4041

4142
void Init(Pinetime::Drivers::Bma421::DeviceTypes types);
43+
void SetService(Pinetime::Controllers::MotionService* service);
4244

4345
private:
4446
uint32_t nbSteps;
@@ -48,6 +50,7 @@ namespace Pinetime {
4850
int16_t lastYForWakeUp = 0;
4951
bool isSensorOk = false;
5052
DeviceTypes deviceType = DeviceTypes::Unknown;
53+
Pinetime::Controllers::MotionService* service = nullptr;
5154
};
5255
}
5356
}

src/systemtask/SystemTask.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi,
101101
heartRateApp(heartRateApp),
102102
fs {fs},
103103
touchHandler {touchHandler},
104-
nimbleController(*this, bleController, dateTimeController, notificationManager, batteryController, spiNorFlash, heartRateController) {
104+
nimbleController(*this, bleController, dateTimeController, notificationManager,
105+
batteryController, spiNorFlash, heartRateController, motionController) {
105106
}
106107

107108
void SystemTask::Start() {

0 commit comments

Comments
 (0)