-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathble_digit.c
334 lines (309 loc) · 11.2 KB
/
ble_digit.c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "sdk_common.h"
#include "ble_bas.h"
#include <string.h>
#include "ble_srv_common.h"
#include "ble_conn_state.h"
#include "nrf_log.h"
#include "./ble_digit.h"
#include "time.h"
#include "app_util.h"
#include <stdint.h>
#include "mem_manager.h"
#define YEAR_LENGTH (2)
#define MONTH_LENGTH (1)
#define DAY_LENGTH (1)
#define HOUR_LENGTH (1)
#define MINUTE_LENGTH (1)
#define SECOND_LENGTH (1)
#define CTS_CHAR_LENGTH (YEAR_LENGTH + \
MONTH_LENGTH + \
DAY_LENGTH + \
HOUR_LENGTH + \
MINUTE_LENGTH + \
SECOND_LENGTH)
#define LEGS_CHAR_LENGTH (CTS_CHAR_LENGTH + 2 * STOP_LENGTH + 1 + LINE_LENGTH + DIRECTION_LENGTH + 4)
static uint8_t min(uint8_t m1, uint8_t m2)
{
return m1 < m2 ? m1 : m2;
}
static void read_cts(const uint8_t src[CTS_CHAR_LENGTH], time_t *dest)
{
struct tm parsed = {
.tm_sec = src[6],
.tm_min = src[5],
.tm_hour = src[4],
.tm_mday = src[3],
.tm_mon = src[2],
.tm_year = uint16_decode(&src[0]) - 1900,
.tm_isdst = 0};
*dest = mktime(&parsed);
}
static void on_write(ble_digit_t *p_digit, ble_evt_t const *p_ble_evt)
{
ble_gatts_evt_write_t const *p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
if ((p_evt_write->handle == p_digit->cts_char_handles.value_handle))
{
if (p_evt_write->len == CTS_CHAR_LENGTH)
{
read_cts(p_evt_write->data, &p_digit->state->current_time);
p_digit->ui_state_changed();
NRF_LOG_INFO("received %d = %d, %d, %d, %d, %d", p_digit->state->current_time,
uint16_decode(&p_evt_write->data[0]),
p_evt_write->data[2],
p_evt_write->data[3],
p_evt_write->data[4],
p_evt_write->data[5]);
}
else
{
NRF_LOG_ERROR("Insufficient data length for cts: %d.", p_evt_write->len);
}
}
if ((p_evt_write->handle == p_digit->event_char_handles.value_handle))
{
if (p_evt_write->len == 1 && p_evt_write->data[0] == 0) {
p_digit->state->display_options.event_active = 0;
p_digit->ui_state_changed();
NRF_LOG_INFO("deleted event");
}
else if (p_evt_write->len >= (CTS_CHAR_LENGTH + 1) || p_evt_write->len <= CTS_CHAR_LENGTH + SUBJECT_LENGTH)
{
read_cts(p_evt_write->data, &p_digit->state->event_start_time);
const char *subjectPtr = (char *)p_evt_write->data + CTS_CHAR_LENGTH;
p_digit->state->event_subject[0] = '\0';
strncat(p_digit->state->event_subject, subjectPtr, min(SUBJECT_LENGTH - 1, p_evt_write->len - CTS_CHAR_LENGTH));
p_digit->state->display_options.event_active = 1;
p_digit->ui_state_changed();
NRF_LOG_INFO("event %s at %d", p_digit->state->event_subject, p_digit->state->event_start_time);
}
else
{
NRF_LOG_ERROR("Insufficient data length for event: %d.", p_evt_write->len);
}
}
if ((p_evt_write->handle == p_digit->directions_char_handles.value_handle))
{
if (p_evt_write->len == 1 && p_evt_write->data[0] == 0) {
p_digit->state->display_options.directions_active = 0;
p_digit->state->directions.legs_count = 0;
p_digit->state->directions.valid_legs = 0;
nrf_free(p_digit->state->directions.legs);
p_digit->ui_state_changed();
NRF_LOG_INFO("deleted directions");
}
else if (p_evt_write->len == (CTS_CHAR_LENGTH * 2 + 1))
{
p_digit->state->display_options.directions_active = 1;
read_cts(p_evt_write->data + 1, &p_digit->state->directions.departure_time);
read_cts(p_evt_write->data + 1 + CTS_CHAR_LENGTH, &p_digit->state->directions.arrival_time);
p_digit->state->directions.legs_count = p_evt_write->data[0];
p_digit->state->directions.valid_legs = 0;
if (NULL != p_digit->state->directions.legs)
{
nrf_free(p_digit->state->directions.legs);
}
if (p_digit->state->directions.legs_count > MAX_LEGS_COUNT)
{
p_digit->state->directions.legs_count = 0;
}
if (p_digit->state->directions.legs_count > 0)
{
p_digit->state->directions.legs = (directions_leg_t *)nrf_malloc(sizeof(directions_leg_t) * p_digit->state->directions.legs_count);
if (NULL == p_digit->state->directions.legs)
{
NRF_LOG_ERROR("Could not allocate legs memory");
p_digit->state->directions.legs_count = 0;
}
}
p_digit->ui_state_changed();
NRF_LOG_INFO("directions %d -> %d, %d legs", p_digit->state->directions.departure_time, p_digit->state->directions.arrival_time,
p_digit->state->directions.legs_count);
}
else
{
NRF_LOG_ERROR("Insufficient data length for directions: %d.", p_evt_write->len);
}
}
if ((p_evt_write->handle == p_digit->directions_leg_char_handles.value_handle))
{
if (p_evt_write->len >= (CTS_CHAR_LENGTH + 5) || p_evt_write->len <= LEGS_CHAR_LENGTH)
{
uint8_t index = p_evt_write->data[0];
if (index > (p_digit->state->directions.legs_count - 1))
{
NRF_LOG_ERROR("received leg %d for max %d legs", index, p_digit->state->directions.legs_count);
}
else
{
directions_leg_t *leg = p_digit->state->directions.legs + index;
read_cts(p_evt_write->data + 1, &leg->departure_time);
uint8_t line_size = p_evt_write->data[CTS_CHAR_LENGTH + 1];
uint8_t direction_size = p_evt_write->data[CTS_CHAR_LENGTH + 2];
uint8_t departure_size = p_evt_write->data[CTS_CHAR_LENGTH + 3];
uint8_t arrival_size = p_evt_write->data[CTS_CHAR_LENGTH + 4];
if (p_evt_write->len != (line_size + direction_size + departure_size + arrival_size + CTS_CHAR_LENGTH + 1 + 4))
{
NRF_LOG_ERROR("Insufficient text length for leg: %d.(%d,%d,%d,%d,%d)", p_evt_write->len,
line_size,
direction_size,
departure_size,
arrival_size, (CTS_CHAR_LENGTH + 1 + 4));
}
else
{
char *textPtr = (char *)p_evt_write->data + CTS_CHAR_LENGTH + 5;
leg->line[0] = '\0';
strncat(leg->line, textPtr, min(LINE_LENGTH - 1, line_size));
textPtr += line_size;
leg->direction[0] = '\0';
strncat(leg->direction, textPtr, min(DIRECTION_LENGTH - 1, direction_size));
textPtr += direction_size;
leg->departure_stop[0] = '\0';
strncat(leg->departure_stop, textPtr, min(STOP_LENGTH - 1, departure_size));
textPtr += departure_size;
leg->arrival_stop[0] = '\0';
strncat(leg->arrival_stop, textPtr, min(STOP_LENGTH - 1, arrival_size));
p_digit->state->directions.valid_legs |= (1 << index);
p_digit->ui_state_changed();
NRF_LOG_INFO("leg %d at %d from %s to %s u %s/%s", index, leg->departure_time, leg->departure_stop, leg->arrival_stop, leg->line, leg->direction);
}
}
}
else
{
NRF_LOG_ERROR("Insufficient data length for leg: %d.", p_evt_write->len);
}
}
}
void ble_digit_on_ble_evt(ble_evt_t const *p_ble_evt, void *p_context)
{
if ((p_context == NULL) || (p_ble_evt == NULL))
{
return;
}
ble_digit_t *p_digit = (ble_digit_t *)p_context;
switch (p_ble_evt->header.evt_id)
{
case BLE_GATTS_EVT_WRITE:
on_write(p_digit, p_ble_evt);
break;
default:
break;
}
}
static ret_code_t cts_char_add(ble_digit_t *p_digit, const ble_digit_init_t *p_digit_init)
{
ret_code_t err_code;
ble_add_char_params_t add_char_params;
memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid_type = p_digit->uuid_type;
add_char_params.uuid = DIGIT_UUID_CTS_CHAR;
add_char_params.max_len = CTS_CHAR_LENGTH;
add_char_params.init_len = 0;
add_char_params.p_init_value = NULL;
add_char_params.char_props.notify = false;
add_char_params.char_props.read = 0;
add_char_params.char_props.write = 1;
add_char_params.cccd_write_access = SEC_NO_ACCESS;
add_char_params.read_access = SEC_NO_ACCESS;
add_char_params.write_access = SEC_JUST_WORKS;
add_char_params.is_value_user = true;
err_code = characteristic_add(p_digit->service_handle,
&add_char_params,
&(p_digit->cts_char_handles));
return err_code;
}
static ret_code_t event_char_add(ble_digit_t *p_digit, const ble_digit_init_t *p_digit_init)
{
ret_code_t err_code;
ble_add_char_params_t add_char_params;
memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid_type = p_digit->uuid_type;
add_char_params.uuid = DIGIT_UUID_EVENT_CHAR;
add_char_params.max_len = CTS_CHAR_LENGTH + SUBJECT_LENGTH;
add_char_params.init_len = 0;
add_char_params.p_init_value = NULL;
add_char_params.char_props.notify = false;
add_char_params.char_props.read = 0;
add_char_params.char_props.write = 1;
add_char_params.cccd_write_access = SEC_NO_ACCESS;
add_char_params.read_access = SEC_NO_ACCESS;
add_char_params.write_access = SEC_JUST_WORKS;
add_char_params.is_value_user = true;
err_code = characteristic_add(p_digit->service_handle,
&add_char_params,
&(p_digit->event_char_handles));
return err_code;
}
static ret_code_t directions_char_add(ble_digit_t *p_digit, const ble_digit_init_t *p_digit_init)
{
ret_code_t err_code;
ble_add_char_params_t add_char_params;
memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid_type = p_digit->uuid_type;
add_char_params.uuid = DIGIT_UUID_DIRECTIONS_CHAR;
add_char_params.max_len = CTS_CHAR_LENGTH * 2 + 1;
add_char_params.init_len = 0;
add_char_params.p_init_value = NULL;
add_char_params.char_props.notify = false;
add_char_params.char_props.read = 0;
add_char_params.char_props.write = 1;
add_char_params.cccd_write_access = SEC_NO_ACCESS;
add_char_params.read_access = SEC_NO_ACCESS;
add_char_params.write_access = SEC_JUST_WORKS;
add_char_params.is_value_user = true;
err_code = characteristic_add(p_digit->service_handle,
&add_char_params,
&(p_digit->directions_char_handles));
return err_code;
}
static ret_code_t directions_leg_char_add(ble_digit_t *p_digit, const ble_digit_init_t *p_digit_init)
{
ret_code_t err_code;
ble_add_char_params_t add_char_params;
memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid_type = p_digit->uuid_type;
add_char_params.uuid = DIGIT_UUID_DIRECTIONS_LEG_CHAR;
add_char_params.max_len = LEGS_CHAR_LENGTH;
add_char_params.init_len = 0;
add_char_params.p_init_value = NULL;
add_char_params.char_props.notify = false;
add_char_params.char_props.read = 0;
add_char_params.char_props.write = 1;
add_char_params.cccd_write_access = SEC_NO_ACCESS;
add_char_params.read_access = SEC_NO_ACCESS;
add_char_params.write_access = SEC_JUST_WORKS;
add_char_params.is_value_user = true;
err_code = characteristic_add(p_digit->service_handle,
&add_char_params,
&(p_digit->directions_leg_char_handles));
return err_code;
}
ret_code_t ble_digit_init(ble_digit_t *p_digit, const ble_digit_init_t *p_digit_init)
{
if (p_digit == NULL || p_digit_init == NULL || p_digit_init->state == NULL)
{
return NRF_ERROR_NULL;
}
p_digit->state = p_digit_init->state;
p_digit->ui_state_changed = p_digit_init->ui_state_changed;
ret_code_t err_code;
ble_uuid_t ble_uuid;
ble_uuid128_t base_uuid = {DIGIT_UUID_BASE};
err_code = sd_ble_uuid_vs_add(&base_uuid, &p_digit->uuid_type);
VERIFY_SUCCESS(err_code);
ble_uuid.type = p_digit->uuid_type;
ble_uuid.uuid = DIGIT_UUID_SERVICE;
err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_digit->service_handle);
VERIFY_SUCCESS(err_code);
err_code = cts_char_add(p_digit, p_digit_init);
VERIFY_SUCCESS(err_code);
err_code = event_char_add(p_digit, p_digit_init);
VERIFY_SUCCESS(err_code);
err_code = directions_char_add(p_digit, p_digit_init);
VERIFY_SUCCESS(err_code);
err_code = directions_leg_char_add(p_digit, p_digit_init);
VERIFY_SUCCESS(err_code);
VERIFY_SUCCESS(nrf_mem_init());
return NRF_SUCCESS;
}