Skip to content

Commit 82ca526

Browse files
authored
Merge pull request #754 from InfiniTimeOrg/add-motion-service
Add motion service
2 parents 9538eb9 + b3a8228 commit 82ca526

File tree

12 files changed

+252
-13
lines changed

12 files changed

+252
-13
lines changed

doc/MotionService.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Motion Service
2+
## Introduction
3+
The motion service exposes step count and raw X/Y/Z motion value as READ and NOTIFY characteristics.
4+
5+
## Service
6+
The service UUID is **00020000-78fc-48fe-8e23-433b3a1942d0**
7+
8+
## Characteristics
9+
### Step count (UUID 00020001-78fc-48fe-8e23-433b3a1942d0)
10+
The current number of steps represented as a single `uint32_t` (4 bytes) value.
11+
12+
### Raw motion values (UUID 00020002-78fc-48fe-8e23-433b3a1942d0)
13+
The current raw motion values. This is a 3 `int16_t` array:
14+
15+
- [0] : X
16+
- [1] : Y
17+
- [2] : Z

doc/ble.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,19 @@ When the service does not exist in the BLE specification, InfiniTime implements
6565
The following custom services are implemented in InfiniTime:
6666

6767
- Since InfiniTime 0.8:
68-
```
69-
* Music Service : 00000000-78fc-48fe-8e23-433b3a1942d0
70-
```
71-
68+
* Music Service : 00000000-78fc-48fe-8e23-433b3a1942d0
69+
70+
7271
- Since InfiniTime 0.11:
73-
```
74-
* Navigation Service : 00010000-78fc-48fe-8e23-433b3a1942d0
75-
```
72+
* [Navigation Service](NavigationService.md) : 00010000-78fc-48fe-8e23-433b3a1942d0
73+
74+
75+
- Since InfiniTime 0.13
76+
* Call characteristic (extension to the Alert Notification Service): 00020001-78fc-48fe-8e23-433b3a1942d0
77+
78+
79+
- Since InfiniTime 1.7:
80+
* [Motion Service](MotionService.md) : 00030000-78fc-48fe-8e23-433b3a1942d0
7681

7782
---
7883

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

src/components/ble/HeartRateService.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ constexpr ble_uuid16_t HeartRateService::heartRateServiceUuid;
88
constexpr ble_uuid16_t HeartRateService::heartRateMeasurementUuid;
99

1010
namespace {
11-
int HeartRateServiceServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
11+
int HeartRateServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
1212
auto* heartRateService = static_cast<HeartRateService*>(arg);
1313
return heartRateService->OnHeartRateRequested(conn_handle, attr_handle, ctxt);
1414
}
@@ -19,7 +19,7 @@ HeartRateService::HeartRateService(Pinetime::System::SystemTask& system, Control
1919
: system {system},
2020
heartRateController {heartRateController},
2121
characteristicDefinition {{.uuid = &heartRateMeasurementUuid.u,
22-
.access_cb = HeartRateServiceServiceCallback,
22+
.access_cb = HeartRateServiceCallback,
2323
.arg = this,
2424
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
2525
.val_handle = &heartRateMeasurementHandle},
@@ -56,6 +56,8 @@ int HeartRateService::OnHeartRateRequested(uint16_t connectionHandle, uint16_t a
5656
}
5757

5858
void HeartRateService::OnNewHeartRateValue(uint8_t heartRateValue) {
59+
if(!heartRateMeasurementNotificationEnable) return;
60+
5961
uint8_t buffer[2] = {0, heartRateController.HeartRate()}; // [0] = flags, [1] = hr value
6062
auto* om = ble_hs_mbuf_from_flat(buffer, 2);
6163

@@ -67,3 +69,13 @@ void HeartRateService::OnNewHeartRateValue(uint8_t heartRateValue) {
6769

6870
ble_gattc_notify_custom(connectionHandle, heartRateMeasurementHandle, om);
6971
}
72+
73+
void HeartRateService::SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) {
74+
if(attributeHandle == heartRateMeasurementHandle)
75+
heartRateMeasurementNotificationEnable = true;
76+
}
77+
78+
void HeartRateService::UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle) {
79+
if(attributeHandle == heartRateMeasurementHandle)
80+
heartRateMeasurementNotificationEnable = false;
81+
}

src/components/ble/HeartRateService.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define min // workaround: nimble's min/max macros conflict with libstdc++
33
#define max
44
#include <host/ble_gap.h>
5+
#include <atomic>
56
#undef max
67
#undef min
78

@@ -18,6 +19,9 @@ namespace Pinetime {
1819
int OnHeartRateRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context);
1920
void OnNewHeartRateValue(uint8_t hearRateValue);
2021

22+
void SubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle);
23+
void UnsubscribeNotification(uint16_t connectionHandle, uint16_t attributeHandle);
24+
2125
private:
2226
Pinetime::System::SystemTask& system;
2327
Controllers::HeartRateController& heartRateController;
@@ -28,10 +32,11 @@ namespace Pinetime {
2832

2933
static constexpr ble_uuid16_t heartRateMeasurementUuid {.u {.type = BLE_UUID_TYPE_16}, .value = heartRateMeasurementId};
3034

31-
struct ble_gatt_chr_def characteristicDefinition[3];
35+
struct ble_gatt_chr_def characteristicDefinition[2];
3236
struct ble_gatt_svc_def serviceDefinition[2];
3337

3438
uint16_t heartRateMeasurementHandle;
39+
std::atomic_bool heartRateMeasurementNotificationEnable {false};
3540
};
3641
}
3742
}
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, 0x03, 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: 17 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);
@@ -215,6 +218,19 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) {
215218
event->subscribe.prev_notify,
216219
event->subscribe.cur_notify,
217220
event->subscribe.prev_indicate);
221+
222+
if(event->subscribe.reason == BLE_GAP_SUBSCRIBE_REASON_TERM) {
223+
heartRateService.UnsubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
224+
motionService.UnsubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
225+
}
226+
else if(event->subscribe.prev_notify == 0 && event->subscribe.cur_notify == 1) {
227+
heartRateService.SubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
228+
motionService.SubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
229+
}
230+
else if(event->subscribe.prev_notify == 1 && event->subscribe.cur_notify == 0) {
231+
heartRateService.UnsubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
232+
motionService.UnsubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
233+
}
218234
break;
219235

220236
case BLE_GAP_EVENT_MTU:

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;

0 commit comments

Comments
 (0)