Skip to content

Commit 49aa003

Browse files
committed
Fix build for motion service improvement
Fixes the build after InfiniTimeOrg/InfiniTime#1970 was merged.
1 parent 71877a1 commit 49aa003

File tree

7 files changed

+181
-10
lines changed

7 files changed

+181
-10
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ target_sources(infinisim PUBLIC
118118
sim/components/battery/BatteryController.cpp
119119
sim/components/ble/AlertNotificationService.h
120120
sim/components/ble/AlertNotificationService.cpp
121+
sim/components/ble/MotionService.h
122+
sim/components/ble/MotionService.cpp
121123
sim/components/ble/MusicService.h
122124
sim/components/ble/MusicService.cpp
123125
sim/components/ble/NavigationService.h

sim/components/ble/MotionService.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include "components/ble/MotionService.h"
2+
#include "components/motion/MotionController.h"
3+
#include "components/ble/NimbleController.h"
4+
#include <nrf_log.h>
5+
6+
using namespace Pinetime::Controllers;
7+
8+
// namespace {
9+
// // 0003yyxx-78fc-48fe-8e23-433b3a1942d0
10+
// constexpr ble_uuid128_t CharUuid(uint8_t x, uint8_t y) {
11+
// return ble_uuid128_t {.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+
// // 00030000-78fc-48fe-8e23-433b3a1942d0
16+
// constexpr ble_uuid128_t BaseUuid() {
17+
// return CharUuid(0x00, 0x00);
18+
// }
19+
//
20+
// constexpr ble_uuid128_t motionServiceUuid {BaseUuid()};
21+
// constexpr ble_uuid128_t stepCountCharUuid {CharUuid(0x01, 0x00)};
22+
// constexpr ble_uuid128_t motionValuesCharUuid {CharUuid(0x02, 0x00)};
23+
//
24+
// int MotionServiceCallback(uint16_t /*conn_handle*/, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
25+
// auto* motionService = static_cast<MotionService*>(arg);
26+
// return motionService->OnStepCountRequested(attr_handle, ctxt);
27+
// }
28+
// }
29+
//
30+
// TODO Refactoring - remove dependency to SystemTask
31+
MotionService::MotionService(NimbleController& nimble, Controllers::MotionController& motionController)
32+
: nimble {nimble},
33+
motionController {motionController}/*,
34+
characteristicDefinition {{.uuid = &stepCountCharUuid.u,
35+
.access_cb = MotionServiceCallback,
36+
.arg = this,
37+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
38+
.val_handle = &stepCountHandle},
39+
{.uuid = &motionValuesCharUuid.u,
40+
.access_cb = MotionServiceCallback,
41+
.arg = this,
42+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
43+
.val_handle = &motionValuesHandle},
44+
{0}},
45+
serviceDefinition {
46+
{.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &motionServiceUuid.u, .characteristics = characteristicDefinition},
47+
{0},
48+
} */{
49+
// TODO refactor to prevent this loop dependency (service depends on controller and controller depends on service)
50+
motionController.SetService(this);
51+
}
52+
//
53+
// void MotionService::Init() {
54+
// int res = 0;
55+
// res = ble_gatts_count_cfg(serviceDefinition);
56+
// ASSERT(res == 0);
57+
//
58+
// res = ble_gatts_add_svcs(serviceDefinition);
59+
// ASSERT(res == 0);
60+
// }
61+
//
62+
// int MotionService::OnStepCountRequested(uint16_t attributeHandle, ble_gatt_access_ctxt* context) {
63+
// if (attributeHandle == stepCountHandle) {
64+
// NRF_LOG_INFO("Motion-stepcount : handle = %d", stepCountHandle);
65+
// uint32_t buffer = motionController.NbSteps();
66+
//
67+
// int res = os_mbuf_append(context->om, &buffer, 4);
68+
// return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
69+
// } else if (attributeHandle == motionValuesHandle) {
70+
// int16_t buffer[3] = {motionController.X(), motionController.Y(), motionController.Z()};
71+
//
72+
// int res = os_mbuf_append(context->om, buffer, 3 * sizeof(int16_t));
73+
// return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
74+
// }
75+
// return 0;
76+
// }
77+
//
78+
// void MotionService::OnNewStepCountValue(uint32_t stepCount) {
79+
// if (!stepCountNoficationEnabled)
80+
// return;
81+
//
82+
// uint32_t buffer = stepCount;
83+
// auto* om = ble_hs_mbuf_from_flat(&buffer, 4);
84+
//
85+
// uint16_t connectionHandle = nimble.connHandle();
86+
//
87+
// if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
88+
// return;
89+
// }
90+
//
91+
// ble_gattc_notify_custom(connectionHandle, stepCountHandle, om);
92+
// }
93+
//
94+
// void MotionService::OnNewMotionValues(int16_t x, int16_t y, int16_t z) {
95+
// if (!motionValuesNoficationEnabled)
96+
// return;
97+
//
98+
// int16_t buffer[3] = {x, y, z};
99+
// auto* om = ble_hs_mbuf_from_flat(buffer, 3 * sizeof(int16_t));
100+
//
101+
// uint16_t connectionHandle = nimble.connHandle();
102+
//
103+
// if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
104+
// return;
105+
// }
106+
//
107+
// ble_gattc_notify_custom(connectionHandle, motionValuesHandle, om);
108+
// }
109+
110+
void MotionService::SubscribeNotification(uint16_t attributeHandle) {
111+
if (attributeHandle == stepCountHandle)
112+
stepCountNoficationEnabled = true;
113+
else if (attributeHandle == motionValuesHandle)
114+
motionValuesNoficationEnabled = true;
115+
}
116+
117+
void MotionService::UnsubscribeNotification(uint16_t attributeHandle) {
118+
if (attributeHandle == stepCountHandle)
119+
stepCountNoficationEnabled = false;
120+
else if (attributeHandle == motionValuesHandle)
121+
motionValuesNoficationEnabled = false;
122+
}
123+
124+
bool MotionService::IsMotionNotificationSubscribed() const {
125+
return motionValuesNoficationEnabled;
126+
}

sim/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 Controllers {
11+
class NimbleController;
12+
class MotionController;
13+
14+
class MotionService {
15+
public:
16+
MotionService(NimbleController& nimble, Controllers::MotionController& motionController);
17+
// void Init();
18+
// int OnStepCountRequested(uint16_t attributeHandle, ble_gatt_access_ctxt* context);
19+
// void OnNewStepCountValue(uint32_t stepCount);
20+
// void OnNewMotionValues(int16_t x, int16_t y, int16_t z);
21+
22+
void SubscribeNotification(uint16_t attributeHandle);
23+
void UnsubscribeNotification(uint16_t attributeHandle);
24+
bool IsMotionNotificationSubscribed() const;
25+
26+
private:
27+
NimbleController& nimble;
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+
}

sim/components/ble/NimbleController.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
4545
// currentTimeService {dateTimeController},
4646
musicService {systemTask},
4747
weatherService {dateTimeController},
48-
navService {systemTask} {
48+
navService {systemTask},
4949
// batteryInformationService {batteryController},
5050
// immediateAlertService {systemTask, notificationManager},
5151
// heartRateService {systemTask, heartRateController},
52-
// motionService {systemTask, motionController},
52+
motionService {*this, motionController} {
5353
// fsService {systemTask, fs},
5454
// serviceDiscovery({&currentTimeClient, &alertNotificationClient}) {
5555
}

sim/components/ble/NimbleController.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "components/ble/MusicService.h"
2020
#include "components/ble/NavigationService.h"
2121
//#include "components/ble/ServiceDiscovery.h"
22-
//#include "components/ble/MotionService.h"
22+
#include "components/ble/MotionService.h"
2323
#include "components/ble/SimpleWeatherService.h"
2424
#include "components/fs/FS.h"
2525
//#include "components/ble/FSService.h"
@@ -116,7 +116,7 @@ namespace Pinetime {
116116
// BatteryInformationService batteryInformationService;
117117
// ImmediateAlertService immediateAlertService;
118118
// HeartRateService heartRateService;
119-
// MotionService motionService;
119+
MotionService motionService;
120120
// FSService fsService;
121121
// ServiceDiscovery serviceDiscovery;
122122

sim/components/motion/MotionController.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//#include <FreeRTOS.h>
66

77
#include "drivers/Bma421.h"
8-
//#include "components/ble/MotionService.h"
8+
#include "components/ble/MotionService.h"
99
#include "utility/CircularBuffer.h"
1010

1111
namespace Pinetime {
@@ -58,9 +58,13 @@ namespace Pinetime {
5858

5959
void Init(Pinetime::Drivers::Bma421::DeviceTypes types);
6060

61-
// void SetService(Pinetime::Controllers::MotionService* service) {
62-
// this->service = service;
63-
// }
61+
void SetService(Pinetime::Controllers::MotionService* service) {
62+
this->service = service;
63+
}
64+
65+
Pinetime::Controllers::MotionService* GetService() const {
66+
return service;
67+
}
6468

6569
private:
6670
uint32_t nbSteps = 0;
@@ -93,7 +97,7 @@ namespace Pinetime {
9397
int32_t accumulatedSpeed = 0;
9498

9599
DeviceTypes deviceType = DeviceTypes::Unknown;
96-
// Pinetime::Controllers::MotionService* service = nullptr;
100+
Pinetime::Controllers::MotionService* service = nullptr;
97101
};
98102
}
99103
}

0 commit comments

Comments
 (0)