Skip to content

Commit b9a9093

Browse files
jmlichvchigrin
authored andcommitted
Make it separate as Immediate Alert Client
1 parent cf468e3 commit b9a9093

File tree

9 files changed

+202
-36
lines changed

9 files changed

+202
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ cmake-build-*
77
cmake-*/
88
CMakeFiles
99
**/CMakeCache.txt
10+
CMakeLists.txt.user*
1011
cmake_install.cmake
1112
Makefile
1213
build
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 {{.uuid = &alertLevelCharacteristicUuid.u,
29+
.arg = this,
30+
.flags = BLE_GATT_CHR_F_WRITE_NO_RSP,
31+
},
32+
{0}},
33+
serviceDefinition {
34+
{/* Device Information Service */
35+
.type = BLE_GATT_SVC_TYPE_PRIMARY,
36+
.uuid = &immediateAlertClientUuid.u,
37+
.characteristics = characteristicDefinition},
38+
{0},
39+
} {
40+
}
41+
42+
void ImmediateAlertClient::Init() {
43+
}
44+
45+
bool ImmediateAlertClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_svc* service) {
46+
if (service == nullptr && error->status == BLE_HS_EDONE) {
47+
if (isDiscovered) {
48+
NRF_LOG_INFO("IAS found, starting characteristics discovery");
49+
50+
ble_gattc_disc_all_chrs(connectionHandle, iasStartHandle, iasEndHandle, OnImmediateAlertCharacteristicDiscoveredCallback, this);
51+
} else {
52+
NRF_LOG_INFO("IAS not found");
53+
onServiceDiscovered(connectionHandle);
54+
}
55+
return true;
56+
}
57+
58+
if (service != nullptr && ble_uuid_cmp(&immediateAlertClientUuid.u, &service->uuid.u) == 0) {
59+
NRF_LOG_INFO("IAS discovered : 0x%x - 0x%x", service->start_handle, service->end_handle);
60+
isDiscovered = true;
61+
iasStartHandle = service->start_handle;
62+
iasEndHandle = service->end_handle;
63+
}
64+
return false;
65+
}
66+
67+
int ImmediateAlertClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error* error, const ble_gatt_chr* characteristic) {
68+
69+
if (error->status != 0 && error->status != BLE_HS_EDONE) {
70+
NRF_LOG_INFO("IAS Characteristic discovery ERROR");
71+
onServiceDiscovered(conn_handle);
72+
return 0;
73+
}
74+
75+
if (characteristic == nullptr && error->status == BLE_HS_EDONE) {
76+
if (isCharacteristicDiscovered) {
77+
// NRF_LOG_INFO("IAS Characteristic discovery complete, fetching alert level?");
78+
// ble_gattc_read(conn_handle, alertLevelHandle, AlertLevelReadCallback, this); // FIXME
79+
} else {
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+
96+
void ImmediateAlertClient::Discover(uint16_t connectionHandle, std::function<void(uint16_t)> onServiceDiscovered) {
97+
NRF_LOG_INFO("[IAS] Starting discovery");
98+
this->onServiceDiscovered = onServiceDiscovered;
99+
ble_gattc_disc_svc_by_uuid(connectionHandle, &immediateAlertClientUuid.u, OnDiscoveryEventCallback, this);
100+
}
101+
102+
void ImmediateAlertClient::sendImmediateAlert(ImmediateAlertClient::Levels level) {
103+
104+
auto* om = ble_hs_mbuf_from_flat(&level, 1);
105+
106+
uint16_t connectionHandle = systemTask.nimble().connHandle();
107+
108+
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
109+
return;
110+
}
111+
112+
ble_gattc_notify_custom(connectionHandle, alertLevelHandle, om);
113+
114+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
11+
namespace Pinetime {
12+
namespace System {
13+
class SystemTask;
14+
}
15+
16+
namespace Controllers {
17+
class NotificationManager;
18+
19+
class ImmediateAlertClient : public BleClient {
20+
public:
21+
enum class Levels : uint8_t { NoAlert = 0, MildAlert = 1, HighAlert = 2 };
22+
23+
ImmediateAlertClient(Pinetime::System::SystemTask& systemTask);
24+
void Init();
25+
26+
bool OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_svc* service);
27+
int OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error* error, const ble_gatt_chr* characteristic);
28+
29+
void sendImmediateAlert(Levels level);
30+
31+
static constexpr const ble_uuid16_t* Uuid() {
32+
return &ImmediateAlertClient::immediateAlertClientUuid;
33+
}
34+
35+
static constexpr const ble_uuid16_t* AlertLevelCharacteristicUuid() {
36+
return &ImmediateAlertClient::alertLevelCharacteristicUuid;
37+
}
38+
39+
void Discover(uint16_t connectionHandle, std::function<void(uint16_t)> lambda) override;
40+
41+
private:
42+
Pinetime::System::SystemTask& systemTask;
43+
44+
static constexpr uint16_t immediateAlertClientId {0x1802};
45+
static constexpr uint16_t alertLevelId {0x2A06};
46+
47+
static constexpr ble_uuid16_t immediateAlertClientUuid {.u {.type = BLE_UUID_TYPE_16}, .value = immediateAlertClientId};
48+
static constexpr ble_uuid16_t alertLevelCharacteristicUuid {.u {.type = BLE_UUID_TYPE_16}, .value = alertLevelId};
49+
50+
bool isDiscovered = false;
51+
uint16_t iasStartHandle;
52+
uint16_t iasEndHandle;
53+
bool isCharacteristicDiscovered = false;
54+
55+
struct ble_gatt_chr_def characteristicDefinition[3];
56+
struct ble_gatt_svc_def serviceDefinition[2];
57+
58+
uint16_t alertLevelHandle;
59+
std::function<void(uint16_t)> onServiceDiscovered;
60+
61+
};
62+
}
63+
}

src/components/ble/ImmediateAlertService.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,3 @@ int ImmediateAlertService::OnAlertLevelChanged(uint16_t attributeHandle, ble_gat
7373

7474
return 0;
7575
}
76-
77-
void ImmediateAlertService::sendImmediateAlert(ImmediateAlertService::Levels level) {
78-
79-
auto* om = ble_hs_mbuf_from_flat(&level, 1);
80-
81-
uint16_t connectionHandle = systemTask.nimble().connHandle();
82-
83-
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
84-
return;
85-
}
86-
87-
ble_gattc_notify_custom(connectionHandle, alertLevelHandle, om);
88-
89-
}

src/components/ble/ImmediateAlertService.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ namespace Pinetime {
2121
void Init();
2222
int OnAlertLevelChanged(uint16_t attributeHandle, ble_gatt_access_ctxt* context);
2323

24-
void sendImmediateAlert(Levels level);
25-
2624
private:
2725
Pinetime::System::SystemTask& systemTask;
2826
NotificationManager& notificationManager;

src/components/ble/NimbleController.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
4545
musicService {*this},
4646
weatherService {dateTimeController},
4747
batteryInformationService {batteryController},
48-
iaService {systemTask, notificationManager},
48+
immediateAlertService {systemTask, notificationManager},
49+
iaClient {systemTask},
4950
heartRateService {*this, heartRateController},
5051
motionService {*this, motionController},
5152
fsService {systemTask, fs},
@@ -94,7 +95,8 @@ void NimbleController::Init() {
9495
anService.Init();
9596
dfuService.Init();
9697
batteryInformationService.Init();
97-
iaService.Init();
98+
immediateAlertService.Init();
99+
iaClient.Init();
98100
heartRateService.Init();
99101
motionService.Init();
100102
fsService.Init();

src/components/ble/NimbleController.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "components/ble/FSService.h"
1818
#include "components/ble/HeartRateService.h"
1919
#include "components/ble/ImmediateAlertService.h"
20+
#include "components/ble/ImmediateAlertClient.h"
2021
#include "components/ble/MusicService.h"
2122
#include "components/ble/NavigationService.h"
2223
#include "components/ble/ServiceDiscovery.h"
@@ -71,8 +72,8 @@ namespace Pinetime {
7172
return weatherService;
7273
};
7374

74-
Pinetime::Controllers::ImmediateAlertService& immediateAlertService() {
75-
return iaService;
75+
Pinetime::Controllers::ImmediateAlertClient& immediateAlertClient() {
76+
return iaClient;
7677
}
7778

7879
uint16_t connHandle();
@@ -106,7 +107,8 @@ namespace Pinetime {
106107
SimpleWeatherService weatherService;
107108
NavigationService navService;
108109
BatteryInformationService batteryInformationService;
109-
ImmediateAlertService iaService;
110+
ImmediateAlertService immediateAlertService;
111+
ImmediateAlertClient iaClient;
110112
HeartRateService heartRateService;
111113
MotionService motionService;
112114
FSService fsService;

src/displayapp/screens/FindMyPhone.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ namespace {
1313
}
1414
}
1515

16-
FindMyPhone::FindMyPhone(Pinetime::Controllers::ImmediateAlertService& immediateAlertService)
17-
: immediateAlertService {immediateAlertService} {
18-
last_level = Pinetime::Controllers::ImmediateAlertService::Levels::NoAlert;
16+
FindMyPhone::FindMyPhone(Pinetime::Controllers::ImmediateAlertClient& immediateAlertClient)
17+
: immediateAlertClient {immediateAlertClient} {
18+
last_level = Pinetime::Controllers::ImmediateAlertClient::Levels::NoAlert;
1919

2020
container = lv_cont_create(lv_scr_act(), nullptr);
2121

@@ -67,29 +67,29 @@ FindMyPhone::~FindMyPhone() {
6767
void FindMyPhone::OnImmediateAlertEvent(lv_obj_t* obj, lv_event_t event) {
6868
if (event == LV_EVENT_CLICKED) {
6969
if (obj == bt_none) {
70-
last_level = Pinetime::Controllers::ImmediateAlertService::Levels::NoAlert;
70+
last_level = Pinetime::Controllers::ImmediateAlertClient::Levels::NoAlert;
7171
} else if (obj == bt_mild) {
72-
last_level = Pinetime::Controllers::ImmediateAlertService::Levels::MildAlert;
72+
last_level = Pinetime::Controllers::ImmediateAlertClient::Levels::MildAlert;
7373
} else if (obj == bt_high) {
74-
last_level = Pinetime::Controllers::ImmediateAlertService::Levels::HighAlert;
74+
last_level = Pinetime::Controllers::ImmediateAlertClient::Levels::HighAlert;
7575
}
7676
UpdateImmediateAlerts();
7777
}
7878
}
7979

8080
void FindMyPhone::UpdateImmediateAlerts() {
8181
switch (last_level) {
82-
case Pinetime::Controllers::ImmediateAlertService::Levels::NoAlert:
82+
case Pinetime::Controllers::ImmediateAlertClient::Levels::NoAlert:
8383
lv_obj_set_style_local_text_color(label_title, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::lightGray);
8484
break;
85-
case Pinetime::Controllers::ImmediateAlertService::Levels::MildAlert:
85+
case Pinetime::Controllers::ImmediateAlertClient::Levels::MildAlert:
8686
lv_obj_set_style_local_text_color(label_title, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::highlight);
8787
break;
88-
case Pinetime::Controllers::ImmediateAlertService::Levels::HighAlert:
88+
case Pinetime::Controllers::ImmediateAlertClient::Levels::HighAlert:
8989
lv_obj_set_style_local_text_color(label_title, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
9090
break;
9191
}
92-
immediateAlertService.sendImmediateAlert(last_level);
92+
immediateAlertClient.sendImmediateAlert(last_level);
9393

9494
}
9595

src/displayapp/screens/FindMyPhone.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,29 @@
55
#include "displayapp/screens/Screen.h"
66
#include "Symbols.h"
77
#include "systemtask/SystemTask.h"
8-
#include "components/ble/ImmediateAlertService.h"
8+
#include "components/ble/ImmediateAlertClient.h"
99

1010
#include <lvgl/src/lv_core/lv_style.h>
1111
#include <lvgl/src/lv_core/lv_obj.h>
1212

1313
namespace Pinetime {
1414

1515
namespace Controllers {
16-
class ImmediateAlertService;
16+
class ImmediateAlertClient;
1717
}
1818

1919
namespace Applications {
2020
namespace Screens {
2121

2222
class FindMyPhone : public Screen {
2323
public:
24-
FindMyPhone(Pinetime::Controllers::ImmediateAlertService& immediateAlertService);
24+
FindMyPhone(Pinetime::Controllers::ImmediateAlertClient& immediateAlertClient);
2525
~FindMyPhone() override;
2626

2727
void OnImmediateAlertEvent(lv_obj_t* obj, lv_event_t event);
2828

2929
private:
30-
Pinetime::Controllers::ImmediateAlertService& immediateAlertService;
30+
Pinetime::Controllers::ImmediateAlertClient& immediateAlertClient;
3131

3232
void UpdateImmediateAlerts();
3333

@@ -41,7 +41,7 @@ namespace Pinetime {
4141
lv_obj_t* label_mild;
4242

4343

44-
Pinetime::Controllers::ImmediateAlertService::Levels last_level;
44+
Pinetime::Controllers::ImmediateAlertClient::Levels last_level;
4545
};
4646
}
4747

0 commit comments

Comments
 (0)