Skip to content

Commit 3f2ef33

Browse files
jmlichvchigrin
authored andcommitted
Make it separate as Immediate Alert Client
1 parent 655d46e commit 3f2ef33

9 files changed

Lines changed: 200 additions & 38 deletions

File tree

.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: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
void 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;
109+
}
110+
111+
ble_gattc_notify_custom(connectionHandle, alertLevelHandle, om);
112+
113+
}
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+
void 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+
}

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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ 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) : immediateAlertClient {immediateAlertClient} {
17+
last_level = Pinetime::Controllers::ImmediateAlertClient::Levels::NoAlert;
1918

2019
container = lv_cont_create(lv_scr_act(), nullptr);
2120

@@ -55,7 +54,7 @@ FindMyPhone::FindMyPhone(Pinetime::Controllers::ImmediateAlertService& immediate
5554
lv_obj_align(bt_high, nullptr, LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0);
5655
label_high = lv_label_create(bt_high, nullptr);
5756
lv_label_set_text_static(label_high, "High");
58-
lv_obj_set_style_local_bg_color(bt_high, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED );
57+
lv_obj_set_style_local_bg_color(bt_high, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
5958

6059
UpdateImmediateAlerts();
6160
}
@@ -67,29 +66,29 @@ FindMyPhone::~FindMyPhone() {
6766
void FindMyPhone::OnImmediateAlertEvent(lv_obj_t* obj, lv_event_t event) {
6867
if (event == LV_EVENT_CLICKED) {
6968
if (obj == bt_none) {
70-
last_level = Pinetime::Controllers::ImmediateAlertService::Levels::NoAlert;
69+
last_level = Pinetime::Controllers::ImmediateAlertClient::Levels::NoAlert;
7170
} else if (obj == bt_mild) {
72-
last_level = Pinetime::Controllers::ImmediateAlertService::Levels::MildAlert;
71+
last_level = Pinetime::Controllers::ImmediateAlertClient::Levels::MildAlert;
7372
} else if (obj == bt_high) {
74-
last_level = Pinetime::Controllers::ImmediateAlertService::Levels::HighAlert;
73+
last_level = Pinetime::Controllers::ImmediateAlertClient::Levels::HighAlert;
7574
}
7675
UpdateImmediateAlerts();
7776
}
7877
}
7978

8079
void FindMyPhone::UpdateImmediateAlerts() {
8180
switch (last_level) {
82-
case Pinetime::Controllers::ImmediateAlertService::Levels::NoAlert:
81+
case Pinetime::Controllers::ImmediateAlertClient::Levels::NoAlert:
8382
lv_obj_set_style_local_text_color(label_title, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::lightGray);
8483
break;
85-
case Pinetime::Controllers::ImmediateAlertService::Levels::MildAlert:
84+
case Pinetime::Controllers::ImmediateAlertClient::Levels::MildAlert:
8685
lv_obj_set_style_local_text_color(label_title, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::highlight);
8786
break;
88-
case Pinetime::Controllers::ImmediateAlertService::Levels::HighAlert:
87+
case Pinetime::Controllers::ImmediateAlertClient::Levels::HighAlert:
8988
lv_obj_set_style_local_text_color(label_title, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
9089
break;
9190
}
92-
immediateAlertService.sendImmediateAlert(last_level);
91+
immediateAlertClient.sendImmediateAlert(last_level);
9392

9493
}
9594

src/displayapp/screens/FindMyPhone.h

Lines changed: 6 additions & 6 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+
explicit 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,14 +41,14 @@ 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

4848
template <>
4949
struct AppTraits<Apps::FindMyPhone> {
5050
static constexpr Apps app = Apps::FindMyPhone;
51-
static constexpr const char* icon = Screens::Symbols::magnifyingGlass ;
51+
static constexpr const char* icon = Screens::Symbols::magnifyingGlass;
5252

5353
static Screens::Screen* Create(AppControllers& controllers) {
5454
return new Screens::FindMyPhone(controllers.systemTask->nimble().immediateAlertClient());

0 commit comments

Comments
 (0)