Skip to content

Commit 24f5fbd

Browse files
committed
Add ImmediateAlertClient to support FindMyPhone functionality.
1 parent 832d38f commit 24f5fbd

File tree

5 files changed

+184
-0
lines changed

5 files changed

+184
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ target_sources(infinisim PUBLIC
131131
sim/components/battery/BatteryController.cpp
132132
sim/components/ble/MusicService.h
133133
sim/components/ble/MusicService.cpp
134+
sim/components/ble/ImmediateAlertClient.h
135+
sim/components/ble/ImmediateAlertClient.cpp
134136
sim/components/ble/NimbleController.h
135137
sim/components/ble/NimbleController.cpp
136138
sim/components/brightness/BrightnessController.h
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include "components/ble/ImmediateAlertClient.h"
2+
#include <cstring>
3+
#include <nrf_log.h>
4+
#include "systemtask/SystemTask.h"
5+
6+
using namespace Pinetime::Controllers;
7+
8+
constexpr ble_uuid16_t ImmediateAlertClient::immediateAlertClientUuid;
9+
constexpr ble_uuid16_t ImmediateAlertClient::alertLevelCharacteristicUuid;
10+
11+
// namespace {
12+
// int OnDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error* error, const struct ble_gatt_svc* service, void* arg) {
13+
// auto client = static_cast<ImmediateAlertClient*>(arg);
14+
// return client->OnDiscoveryEvent(conn_handle, error, service);
15+
// }
16+
17+
// int OnImmediateAlertCharacteristicDiscoveredCallback(uint16_t conn_handle,
18+
// const struct ble_gatt_error* error,
19+
// const struct ble_gatt_chr* chr,
20+
// void* arg) {
21+
// auto client = static_cast<ImmediateAlertClient*>(arg);
22+
// return client->OnCharacteristicDiscoveryEvent(conn_handle, error, chr);
23+
// }
24+
// }
25+
26+
ImmediateAlertClient::ImmediateAlertClient(Pinetime::System::SystemTask& systemTask)
27+
: systemTask {systemTask},
28+
characteristicDefinition {{
29+
.uuid = &alertLevelCharacteristicUuid.u,
30+
.arg = this,
31+
.flags = BLE_GATT_CHR_F_WRITE_NO_RSP,
32+
},
33+
{0}},
34+
serviceDefinition {
35+
{/* Device Information Service */
36+
.type = BLE_GATT_SVC_TYPE_PRIMARY,
37+
.uuid = &immediateAlertClientUuid.u,
38+
.characteristics = characteristicDefinition},
39+
{0},
40+
} {
41+
}
42+
43+
void ImmediateAlertClient::Init() {
44+
}
45+
46+
bool ImmediateAlertClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_svc* service) {
47+
// if (service == nullptr && error->status == BLE_HS_EDONE) {
48+
// if (isDiscovered) {
49+
// NRF_LOG_INFO("IAS found, starting characteristics discovery");
50+
51+
// ble_gattc_disc_all_chrs(connectionHandle, iasStartHandle, iasEndHandle, OnImmediateAlertCharacteristicDiscoveredCallback, this);
52+
// } else {
53+
// NRF_LOG_INFO("IAS not found");
54+
// onServiceDiscovered(connectionHandle);
55+
// }
56+
// return true;
57+
// }
58+
59+
// if (service != nullptr && ble_uuid_cmp(&immediateAlertClientUuid.u, &service->uuid.u) == 0) {
60+
// NRF_LOG_INFO("IAS discovered : 0x%x - 0x%x", service->start_handle, service->end_handle);
61+
// isDiscovered = true;
62+
// iasStartHandle = service->start_handle;
63+
// iasEndHandle = service->end_handle;
64+
// }
65+
return false;
66+
}
67+
68+
int ImmediateAlertClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle,
69+
const ble_gatt_error* error,
70+
const ble_gatt_chr* characteristic) {
71+
72+
// if (error->status != 0 && error->status != BLE_HS_EDONE) {
73+
// NRF_LOG_INFO("IAS Characteristic discovery ERROR");
74+
// onServiceDiscovered(conn_handle);
75+
// return 0;
76+
// }
77+
78+
// if (characteristic == nullptr && error->status == BLE_HS_EDONE) {
79+
// if (!isCharacteristicDiscovered) {
80+
// NRF_LOG_INFO("IAS Characteristic discovery unsuccessful");
81+
// onServiceDiscovered(conn_handle);
82+
// }
83+
84+
// return 0;
85+
// }
86+
87+
// if (characteristic != nullptr && ble_uuid_cmp(&alertLevelCharacteristicUuid.u, &characteristic->uuid.u) == 0) {
88+
// NRF_LOG_INFO("AIS Characteristic discovered : 0x%x", characteristic->val_handle);
89+
// isCharacteristicDiscovered = true;
90+
// alertLevelHandle = characteristic->val_handle;
91+
// }
92+
return 0;
93+
}
94+
95+
void ImmediateAlertClient::Discover(uint16_t connectionHandle, std::function<void(uint16_t)> onServiceDiscovered) {
96+
NRF_LOG_INFO("[IAS] Starting discovery");
97+
this->onServiceDiscovered = onServiceDiscovered;
98+
// ble_gattc_disc_svc_by_uuid(connectionHandle, &immediateAlertClientUuid.u, OnDiscoveryEventCallback, this);
99+
}
100+
101+
bool ImmediateAlertClient::sendImmediateAlert(ImmediateAlertClient::Levels level) {
102+
103+
// auto* om = ble_hs_mbuf_from_flat(&level, 1);
104+
105+
// uint16_t connectionHandle = systemTask.nimble().connHandle();
106+
107+
// if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
108+
// return false;
109+
// }
110+
111+
// ble_gattc_write_no_rsp(connectionHandle, alertLevelHandle, om);
112+
// return true;
113+
return false;
114+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
#undef max
6+
#undef min
7+
#include <cstdint>
8+
#include "components/ble/BleClient.h"
9+
10+
namespace Pinetime {
11+
namespace System {
12+
class SystemTask;
13+
}
14+
15+
namespace Controllers {
16+
class NotificationManager;
17+
18+
class ImmediateAlertClient : public BleClient {
19+
public:
20+
enum class Levels : uint8_t { NoAlert = 0, MildAlert = 1, HighAlert = 2 };
21+
22+
ImmediateAlertClient(Pinetime::System::SystemTask& systemTask);
23+
void Init();
24+
25+
bool OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_svc* service);
26+
int OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error* error, const ble_gatt_chr* characteristic);
27+
28+
bool sendImmediateAlert(Levels level);
29+
30+
static constexpr const ble_uuid16_t* Uuid() {
31+
return &ImmediateAlertClient::immediateAlertClientUuid;
32+
}
33+
34+
static constexpr const ble_uuid16_t* AlertLevelCharacteristicUuid() {
35+
return &ImmediateAlertClient::alertLevelCharacteristicUuid;
36+
}
37+
38+
void Discover(uint16_t connectionHandle, std::function<void(uint16_t)> lambda) override;
39+
40+
private:
41+
Pinetime::System::SystemTask& systemTask;
42+
43+
static constexpr uint16_t immediateAlertClientId {0x1802};
44+
static constexpr uint16_t alertLevelId {0x2A06};
45+
46+
static constexpr ble_uuid16_t immediateAlertClientUuid {.u {.type = BLE_UUID_TYPE_16}, .value = immediateAlertClientId};
47+
static constexpr ble_uuid16_t alertLevelCharacteristicUuid {.u {.type = BLE_UUID_TYPE_16}, .value = alertLevelId};
48+
49+
bool isDiscovered = false;
50+
uint16_t iasStartHandle;
51+
uint16_t iasEndHandle;
52+
bool isCharacteristicDiscovered = false;
53+
54+
struct ble_gatt_chr_def characteristicDefinition[3];
55+
struct ble_gatt_svc_def serviceDefinition[2];
56+
57+
uint16_t alertLevelHandle;
58+
std::function<void(uint16_t)> onServiceDiscovered;
59+
};
60+
}
61+
}

sim/components/ble/NimbleController.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
4545
// currentTimeService {dateTimeController},
4646
musicService {systemTask},
4747
weatherService {dateTimeController},
48+
iaClient {systemTask},
4849
// batteryInformationService {batteryController},
4950
// immediateAlertService {systemTask, notificationManager},
5051
// heartRateService {systemTask, heartRateController},
@@ -91,6 +92,7 @@ void NimbleController::Init() {
9192
musicService.Init();
9293
weatherService.Init();
9394
navService.Init();
95+
iaClient.Init();
9496
// anService.Init();
9597
// dfuService.Init();
9698
// batteryInformationService.Init();

sim/components/ble/NimbleController.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
//#include "components/ble/DfuService.h"
1717
//#include "components/ble/HeartRateService.h"
1818
//#include "components/ble/ImmediateAlertService.h"
19+
#include "components/ble/ImmediateAlertClient.h"
1920
#include "components/ble/MusicService.h"
2021
#include "components/ble/NavigationService.h"
2122
//#include "components/ble/ServiceDiscovery.h"
@@ -81,6 +82,9 @@ namespace Pinetime {
8182
Pinetime::Controllers::SimpleWeatherService& weather() {
8283
return weatherService;
8384
};
85+
Pinetime::Controllers::ImmediateAlertClient& immediateAlertClient() {
86+
return iaClient;
87+
};
8488

8589
uint16_t connHandle();
8690
void NotifyBatteryLevel(uint8_t level);
@@ -115,6 +119,7 @@ namespace Pinetime {
115119
NavigationService navService;
116120
// BatteryInformationService batteryInformationService;
117121
// ImmediateAlertService immediateAlertService;
122+
ImmediateAlertClient iaClient;
118123
// HeartRateService heartRateService;
119124
MotionService motionService;
120125
// FSService fsService;

0 commit comments

Comments
 (0)