-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathHomeService.cpp
More file actions
144 lines (111 loc) · 4.49 KB
/
HomeService.cpp
File metadata and controls
144 lines (111 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "HomeService.h"
#include <libraries/log/nrf_log.h>
#include "components/ble/NimbleController.h"
namespace {
// 0006yyxx-78fc-48fe-8e23-433b3a1942d0
constexpr ble_uuid128_t CharUuid(uint8_t x, uint8_t y) {
return ble_uuid128_t {.u = {.type = BLE_UUID_TYPE_128},
.value = {0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, x, y, 0x06, 0x00}};
}
// 00060000-78fc-48fe-8e23-433b3a1942d0
constexpr ble_uuid128_t BaseUuid() {
return CharUuid(0x00, 0x00);
}
constexpr ble_uuid128_t homeUuid {BaseUuid()};
constexpr ble_uuid128_t homeOpenUuid {CharUuid(0x01, 0x00)};
constexpr ble_uuid128_t homeLayoutUuid {CharUuid(0x02, 0x00)};
constexpr ble_uuid128_t homePressUuid {CharUuid(0x03, 0x00)};
int HomeCallback(uint16_t /*conn_handle*/, uint16_t /*attr_handle*/, struct ble_gatt_access_ctxt* ctxt, void* arg) {
return static_cast<Pinetime::Controllers::HomeService*>(arg)->OnCommand(ctxt);
}
} // namespace
Pinetime::Controllers::HomeService::HomeService(NimbleController& nimble) : nimble(nimble) {
characteristicDefinition[0] = {.uuid = &homeLayoutUuid.u, .access_cb = HomeCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE};
characteristicDefinition[1] = {.uuid = &homeOpenUuid.u,
.access_cb = HomeCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_NOTIFY,
.val_handle = &eventOpenedHandle};
characteristicDefinition[2] = {.uuid = &homePressUuid.u,
.access_cb = HomeCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_NOTIFY,
.val_handle = &eventPressedHandle};
characteristicDefinition[3] = {0};
serviceDefinition[0] = {.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &homeUuid.u, .characteristics = characteristicDefinition};
serviceDefinition[1] = {0};
}
void Pinetime::Controllers::HomeService::Init() {
uint8_t res = 0;
res = ble_gatts_count_cfg(serviceDefinition);
ASSERT(res == 0);
res = ble_gatts_add_svcs(serviceDefinition);
ASSERT(res == 0);
}
int Pinetime::Controllers::HomeService::OnCommand(ble_gatt_access_ctxt* ctxt) {
if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
size_t bufferSize = OS_MBUF_PKTLEN(ctxt->om);
uint8_t data[bufferSize];
os_mbuf_copydata(ctxt->om, 0, bufferSize, data);
if (ble_uuid_cmp(ctxt->chr->uuid, &homeLayoutUuid.u) == 0) {
auto screen = std::make_unique<Screen>();
uint8_t* ptr = &data[0];
numScreens = *ptr++;
screen->index = *ptr++;
screen->cols = *ptr >> 4;
screen->rows = *ptr & 0x0F;
ptr++;
uint8_t num_comps = *(ptr++);
for (size_t j = 0; j < num_comps; j++) {
Component comp;
comp.type = (ComponentType) (*(ptr++));
comp.x = *ptr >> 4;
comp.y = *ptr & 0x0F;
ptr++;
comp.w = *ptr >> 4;
comp.h = *ptr & 0x0F;
ptr++;
uint8_t label_len = *(ptr++);
auto label = std::make_unique<char[]>(label_len + 1);
memcpy(label.get(), ptr, label_len);
label.get()[label_len] = 0;
ptr += label_len;
comp.label = std::move(label);
screen->components.emplace_back(std::move(comp));
}
currentScreen = std::move(screen);
dataUpdateTime = xTaskGetTickCount();
}
}
return 0;
}
bool Pinetime::Controllers::HomeService::NotifyOpened(int8_t screenIndex) {
uint16_t connectionHandle = nimble.connHandle();
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
return false;
}
auto* om = ble_hs_mbuf_from_flat(&screenIndex, sizeof(screenIndex));
ble_gattc_notify_custom(connectionHandle, eventOpenedHandle, om);
return true;
}
bool Pinetime::Controllers::HomeService::OnOpened() {
return NotifyOpened(0);
}
void Pinetime::Controllers::HomeService::OnViewScreen(uint8_t n) {
NotifyOpened(n);
}
void Pinetime::Controllers::HomeService::OnClosed() {
dataUpdateTime = 0;
numScreens = 0;
currentScreen.reset();
NotifyOpened(-1);
}
void Pinetime::Controllers::HomeService::OnPressed(uint8_t screen, uint8_t componentId) {
uint16_t connectionHandle = nimble.connHandle();
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
return;
}
uint8_t v[] = {screen, componentId};
auto* om = ble_hs_mbuf_from_flat(v, sizeof(v));
ble_gattc_notify_custom(connectionHandle, eventPressedHandle, om);
}