card10.c 12.5 KB
Newer Older
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
#include "wsf_types.h"
#include "wsf_os.h"
#include "wsf_buf.h"
#include "wsf_timer.h"
#include "wsf_trace.h"
#include "app_ui.h"
#include "fit/fit_api.h"
#include "hci_vs.h"

#include "ff.h"

#include "util/bstream.h"
#include "att_api.h"

#include "FreeRTOS.h"
#include "crc32.h"

#include "epicardium.h"

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <machine/endian.h>

#define CARD10_UUID_SUFFIX                                                     \
	0x42, 0x23, 0x42, 0x23, 0x42, 0x23, 0x42, 0x23, 0x42, 0x23, 0x42, 0x23
genofire's avatar
genofire committed
27
#define CARD10_UUID_PREFIX 0x02, 0x23, 0x42
28
29
30
31
32
33
34
35

/*!< \brief Service start handle. */
#define CARD10_START_HDL 0x920
/*!< \brief Service end handle. */
#define CARD10_END_HDL (CARD10_MAX_HDL - 1)

/*
 * This has to match in order and number of members to the functions
genofire's avatar
genofire committed
36
 * called in addCard10GroupDyn() otherwise the stack breaks.
37
38
39
40
 */
enum {
	/*!< \brief card10 service declaration */
	CARD10_SVC_HDL = CARD10_START_HDL,
41
42
43
	/*!< \brief time update characteristic */
	CARD10_TIME_UPDATE_CH_HDL,
	CARD10_TIME_UPDATE_VAL_HDL,
44
45
46
47
48
49
	/*!< \brief vibra characteristic */
	CARD10_VIRBA_CH_HDL,
	CARD10_VIBRA_VAL_HDL,
	/*!< \brief rockets led characteristic */
	CARD10_ROCKETS_CH_HDL,
	CARD10_ROCKETS_VAL_HDL,
genofire's avatar
genofire committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
	/*!< \brief led for background on bottom left characteristic */
	CARD10_LED_BG_BOTTOM_LEFT_CH_HDL,
	CARD10_LED_BG_BOTTOM_LEFT_VAL_HDL,
	/*!< \brief led for background on bottom right characteristic */
	CARD10_LED_BG_BOTTOM_RIGHT_CH_HDL,
	CARD10_LED_BG_BOTTOM_RIGHT_VAL_HDL,
	/*!< \brief led for background on top right characteristic */
	CARD10_LED_BG_TOP_RIGHT_CH_HDL,
	CARD10_LED_BG_TOP_RIGHT_VAL_HDL,
	/*!< \brief led for background on top left characteristic */
	CARD10_LED_BG_TOP_LEFT_CH_HDL,
	CARD10_LED_BG_TOP_LEFT_VAL_HDL,
	/*!< \brief leds above characteristic */
	CARD10_LEDS_ABOVE_CH_HDL,
	CARD10_LEDS_ABOVE_VAL_HDL,
65
66
67
	/*!< \brief single led characteristic */
	CARD10_LED_S_CH_HDL,
	CARD10_LED_S_VAL_HDL,
68
69
70
71
72
73
74
	/*!< \brief light sensor characteristic */
	CARD10_LIGHT_SENSOR_CH_HDL,
	CARD10_LIGHT_SENSOR_VAL_HDL,
	/*!< \brief Maximum handle. */
	CARD10_MAX_HDL
};

genofire's avatar
genofire committed
75
76
/* clang-format off */

77
/* BLE UUID for card10 service*/
genofire's avatar
genofire committed
78
static const uint8_t UUID_svc[] = { CARD10_UUID_SUFFIX, 0x0, CARD10_UUID_PREFIX };
79

80
81
82
83
84
85
86
87
88
89
90
91
92
93

// starting at 0x01 with write (non visual) charateristics

/* BLE UUID for card10 time update */
static const uint8_t UUID_char_time[] = {
	ATT_PROP_WRITE,
	UINT16_TO_BYTES(CARD10_TIME_UPDATE_VAL_HDL),
	CARD10_UUID_SUFFIX, 0x01, CARD10_UUID_PREFIX
};

static const uint8_t UUID_attChar_time[] = {
	CARD10_UUID_SUFFIX, 0x01, CARD10_UUID_PREFIX
};

94
/* BLE UUID for card10 char vibra */
genofire's avatar
genofire committed
95
96
97
static const uint8_t UUID_char_vibra[] = {
	ATT_PROP_WRITE_NO_RSP,
	UINT16_TO_BYTES(CARD10_VIBRA_VAL_HDL),
98
	CARD10_UUID_SUFFIX, 0x0f, CARD10_UUID_PREFIX
genofire's avatar
genofire committed
99
};
100
101

static const uint8_t UUID_attChar_vibra[] = {
102
	CARD10_UUID_SUFFIX, 0x0f, CARD10_UUID_PREFIX
103
104
};

105
106
107

// starting at 0x10 with write of leds (visual output)

108
/* BLE UUID for card10 char rockets */
genofire's avatar
genofire committed
109
110
111
static const uint8_t UUID_char_rockets[] = {
	ATT_PROP_WRITE_NO_RSP,
	UINT16_TO_BYTES(CARD10_ROCKETS_VAL_HDL),
genofire's avatar
genofire committed
112
	CARD10_UUID_SUFFIX, 0x10, CARD10_UUID_PREFIX
genofire's avatar
genofire committed
113
};
114
115

static const uint8_t UUID_attChar_rockets[] = {
genofire's avatar
genofire committed
116
	CARD10_UUID_SUFFIX, 0x10, CARD10_UUID_PREFIX
117
118
};

genofire's avatar
genofire committed
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
/* BLE UUID for card10 led background bottom left */
static const uint8_t UUID_char_led_bg_bottom_left[] = {
	ATT_PROP_WRITE_NO_RSP,
	UINT16_TO_BYTES(CARD10_LED_BG_BOTTOM_LEFT_VAL_HDL),
	CARD10_UUID_SUFFIX, 0x11, CARD10_UUID_PREFIX
};

static const uint8_t UUID_attChar_led_bg_bottom_left[] = {
	CARD10_UUID_SUFFIX, 0x11, CARD10_UUID_PREFIX
};

/* BLE UUID for card10 led background bottom right */
static const uint8_t UUID_char_led_bg_bottom_right[] = {
	ATT_PROP_WRITE_NO_RSP,
	UINT16_TO_BYTES(CARD10_LED_BG_BOTTOM_RIGHT_VAL_HDL),
	CARD10_UUID_SUFFIX, 0x12, CARD10_UUID_PREFIX
};

static const uint8_t UUID_attChar_led_bg_bottom_right[] = {
	CARD10_UUID_SUFFIX, 0x12, CARD10_UUID_PREFIX
};

/* BLE UUID for card10 led background top right */
static const uint8_t UUID_char_led_bg_top_right[] = {
	ATT_PROP_WRITE_NO_RSP,
	UINT16_TO_BYTES(CARD10_LED_BG_TOP_RIGHT_VAL_HDL),
	CARD10_UUID_SUFFIX, 0x13, CARD10_UUID_PREFIX
};

static const uint8_t UUID_attChar_led_bg_top_right[] = {
	CARD10_UUID_SUFFIX, 0x13, CARD10_UUID_PREFIX
};

/* BLE UUID for card10 led background top left */
static const uint8_t UUID_char_led_bg_top_left[] = {
	ATT_PROP_WRITE_NO_RSP,
	UINT16_TO_BYTES(CARD10_LED_BG_TOP_LEFT_VAL_HDL),
	CARD10_UUID_SUFFIX, 0x14, CARD10_UUID_PREFIX
};

static const uint8_t UUID_attChar_led_bg_top_left[] = {
	CARD10_UUID_SUFFIX, 0x14, CARD10_UUID_PREFIX
};

/* BLE UUID for card10 above leds */
static const uint8_t UUID_char_leds_above[] = {
	ATT_PROP_WRITE_NO_RSP,
	UINT16_TO_BYTES(CARD10_LEDS_ABOVE_VAL_HDL),
	CARD10_UUID_SUFFIX, 0x20, CARD10_UUID_PREFIX
};

static const uint8_t UUID_attChar_leds_above[] = {
	CARD10_UUID_SUFFIX, 0x20, CARD10_UUID_PREFIX
};

174
175
176
177
178
179
180
181
182
183
/* BLE UUID for card10 char led single (debugging) */
static const uint8_t UUID_char_led_s[] = {
	ATT_PROP_WRITE_NO_RSP,
	UINT16_TO_BYTES(CARD10_LED_S_VAL_HDL),
	CARD10_UUID_SUFFIX, 0xef, CARD10_UUID_PREFIX
};

static const uint8_t UUID_attChar_led_s[] = {
	CARD10_UUID_SUFFIX, 0xef, CARD10_UUID_PREFIX
};
184
185
// starting at 0xf0 with read only characteristics

186
187
188
189
/* BLE UUID for card10 char light sensor */
static const uint8_t UUID_char_light_sensor[] = {
	ATT_PROP_READ,
	UINT16_TO_BYTES(CARD10_LIGHT_SENSOR_VAL_HDL),
genofire's avatar
genofire committed
190
	CARD10_UUID_SUFFIX, 0xf0, CARD10_UUID_PREFIX
191
192
};
static const uint8_t UUID_attChar_light_sensor[] = {
genofire's avatar
genofire committed
193
	CARD10_UUID_SUFFIX, 0xf0, CARD10_UUID_PREFIX
194
};
genofire's avatar
genofire committed
195
/* clang-format on */
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218

/*
 * Create the BLE service description. 
 */
static void *addCard10GroupDyn(void)
{
	void *pSHdl;

	uint8_t initLightSensorValue[] = { UINT16_TO_BYTES(0) };

	/* Create the service */
	pSHdl = AttsDynCreateGroup(CARD10_START_HDL, CARD10_END_HDL);
	if (pSHdl != NULL) {
		/* Primary service */
		AttsDynAddAttrConst(
			pSHdl,
			attPrimSvcUuid,
			UUID_svc,
			sizeof(UUID_svc),
			0,
			ATTS_PERMIT_READ
		);

219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
		// TIME UPDTAE

		AttsDynAddAttrConst(
			pSHdl,
			attChUuid,
			UUID_char_time,
			sizeof(UUID_char_time),
			0,
			ATTS_PERMIT_READ
		);

		AttsDynAddAttr(
			pSHdl,
			UUID_attChar_time,
			NULL,
			0,
			sizeof(uint64_t),
			ATTS_SET_WRITE_CBACK,
			ATTS_PERMIT_WRITE
		);

240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
		// VIBRA

		AttsDynAddAttrConst(
			pSHdl,
			attChUuid,
			UUID_char_vibra,
			sizeof(UUID_char_vibra),
			0,
			ATTS_PERMIT_READ
		);

		AttsDynAddAttr(
			pSHdl,
			UUID_attChar_vibra,
			NULL,
genofire's avatar
genofire committed
255
			0,
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
			sizeof(uint16_t),
			ATTS_SET_WRITE_CBACK,
			ATTS_PERMIT_WRITE
		);

		// ROCKETS

		AttsDynAddAttrConst(
			pSHdl,
			attChUuid,
			UUID_char_rockets,
			sizeof(UUID_char_rockets),
			0,
			ATTS_PERMIT_READ
		);

		AttsDynAddAttr(
			pSHdl,
			UUID_attChar_rockets,
			NULL,
genofire's avatar
genofire committed
276
			0,
277
278
279
280
281
			3 * sizeof(uint8_t),
			ATTS_SET_WRITE_CBACK,
			ATTS_PERMIT_WRITE
		);

genofire's avatar
genofire committed
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
		// BG LED Bottom left

		AttsDynAddAttrConst(
			pSHdl,
			attChUuid,
			UUID_char_led_bg_bottom_left,
			sizeof(UUID_char_led_bg_bottom_left),
			0,
			ATTS_PERMIT_READ
		);

		AttsDynAddAttr(
			pSHdl,
			UUID_attChar_led_bg_bottom_left,
			NULL,
			0,
			3 * sizeof(uint8_t),
			ATTS_SET_WRITE_CBACK,
			ATTS_PERMIT_WRITE
		);

		// BG LED Bottom right

		AttsDynAddAttrConst(
			pSHdl,
			attChUuid,
			UUID_char_led_bg_bottom_right,
			sizeof(UUID_char_led_bg_bottom_right),
			0,
			ATTS_PERMIT_READ
		);

		AttsDynAddAttr(
			pSHdl,
			UUID_attChar_led_bg_bottom_right,
			NULL,
			0,
			3 * sizeof(uint8_t),
			ATTS_SET_WRITE_CBACK,
			ATTS_PERMIT_WRITE
		);

		// BG LED top right

		AttsDynAddAttrConst(
			pSHdl,
			attChUuid,
			UUID_char_led_bg_top_right,
			sizeof(UUID_char_led_bg_top_right),
			0,
			ATTS_PERMIT_READ
		);

		AttsDynAddAttr(
			pSHdl,
			UUID_attChar_led_bg_top_right,
			NULL,
			0,
			3 * sizeof(uint8_t),
			ATTS_SET_WRITE_CBACK,
			ATTS_PERMIT_WRITE
		);

		// BG LED top left

		AttsDynAddAttrConst(
			pSHdl,
			attChUuid,
			UUID_char_led_bg_top_left,
			sizeof(UUID_char_led_bg_top_left),
			0,
			ATTS_PERMIT_READ
		);

		AttsDynAddAttr(
			pSHdl,
			UUID_attChar_led_bg_top_left,
			NULL,
			0,
			3 * sizeof(uint8_t),
			ATTS_SET_WRITE_CBACK,
			ATTS_PERMIT_WRITE
		);

		// ABOVE LEDS

		AttsDynAddAttrConst(
			pSHdl,
			attChUuid,
			UUID_char_leds_above,
			sizeof(UUID_char_leds_above),
			0,
			ATTS_PERMIT_READ
		);

		AttsDynAddAttr(
			pSHdl,
			UUID_attChar_leds_above,
			NULL,
			0,
			11 * 3 * sizeof(uint8_t),
			ATTS_SET_WRITE_CBACK,
			ATTS_PERMIT_WRITE
		);

387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
		// SINGLE LED

		AttsDynAddAttrConst(
			pSHdl,
			attChUuid,
			UUID_char_led_s,
			sizeof(UUID_char_led_s),
			0,
			ATTS_PERMIT_READ
		);

		AttsDynAddAttr(
			pSHdl,
			UUID_attChar_led_s,
			NULL,
			0,
			sizeof(uint16_t) + 3 * sizeof(uint8_t),
			ATTS_SET_WRITE_CBACK,
			ATTS_PERMIT_WRITE
		);

408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
		// LIGHT_SENSOR

		AttsDynAddAttrConst(
			pSHdl,
			attChUuid,
			UUID_char_light_sensor,
			sizeof(UUID_char_light_sensor),
			0,
			ATTS_PERMIT_READ
		);

		AttsDynAddAttr(
			pSHdl,
			UUID_attChar_light_sensor,
			initLightSensorValue,
			sizeof(uint8_t),
			sizeof(uint8_t),
			ATTS_SET_READ_CBACK,
			ATTS_PERMIT_READ
		);

genofire's avatar
genofire committed
429
		APP_TRACE_INFO0("ble-card10: services bound\n");
430
431
432
433
	}
	return pSHdl;
}

434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
 * Set the time given in milliseconds since 1.1.1970 as 64 bit integer.
 */
static uint8_t setTime(uint8_t *pValue, uint16_t len)
{
	uint64_t timeNet;
	uint64_t time;

	if (len < sizeof(uint64_t)) {
		return ATT_ERR_LENGTH;
	}
	memcpy(&timeNet, pValue, sizeof(timeNet));

	time = __bswap64(timeNet);
	epic_rtc_set_milliseconds(time);

	return ATT_SUCCESS;
}

453
/*
genofire's avatar
genofire committed
454
 * BLE card10 write callback.
455
456
457
458
459
460
461
462
463
464
465
466
467
468
 */

static uint8_t writeCard10CB(
	dmConnId_t connId,
	uint16_t handle,
	uint8_t operation,
	uint16_t offset,
	uint16_t len,
	uint8_t *pValue,
	attsAttr_t *pAttr
) {
	uint16_t ui16;

	switch (handle) {
469
470
471
472
473
474
475
476
	case CARD10_TIME_UPDATE_VAL_HDL:
		if (operation == ATT_PDU_PREP_WRITE_REQ) {
			if (len < sizeof(uint64_t)) {
				return ATT_ERR_LENGTH;
			}
			return ATT_SUCCESS;
		}
		return setTime(pValue, len);
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
	case CARD10_VIBRA_VAL_HDL:
		BYTES_TO_UINT16(ui16, pValue);
		epic_vibra_vibrate(ui16);
		APP_TRACE_INFO1("ble-card10: set vibra for %dms\n", ui16);
		return ATT_SUCCESS;
	case CARD10_ROCKETS_VAL_HDL:
		epic_leds_set_rocket(0, pValue[0]);
		epic_leds_set_rocket(1, pValue[1]);
		epic_leds_set_rocket(2, pValue[2]);
		APP_TRACE_INFO3(
			"ble-card10: set rockets 0:%d, 1:%d, 2:%d\n",
			pValue[0],
			pValue[1],
			pValue[2]
		);
		return ATT_SUCCESS;
genofire's avatar
genofire committed
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
	case CARD10_LED_BG_BOTTOM_LEFT_VAL_HDL:
		epic_leds_set(11, pValue[0], pValue[1], pValue[2]);
		APP_TRACE_INFO3(
			"ble-card10: set bg bottom left: #%02x%02x%02x\n",
			pValue[0],
			pValue[1],
			pValue[2]
		);
		return ATT_SUCCESS;
	case CARD10_LED_BG_BOTTOM_RIGHT_VAL_HDL:
		epic_leds_set(12, pValue[0], pValue[1], pValue[2]);
		APP_TRACE_INFO3(
			"ble-card10: set bg bottom right: #%02x%02x%02x\n",
			pValue[0],
			pValue[1],
			pValue[2]
		);
		return ATT_SUCCESS;
	case CARD10_LED_BG_TOP_RIGHT_VAL_HDL:
		epic_leds_set(13, pValue[0], pValue[1], pValue[2]);
		APP_TRACE_INFO3(
			"ble-card10: set bg top right: #%02x%02x%02x\n",
			pValue[0],
			pValue[1],
			pValue[2]
		);
		return ATT_SUCCESS;
	case CARD10_LED_BG_TOP_LEFT_VAL_HDL:
		epic_leds_set(14, pValue[0], pValue[1], pValue[2]);
		APP_TRACE_INFO3(
			"ble-card10: set bg top left: #%02x%02x%02x\n",
			pValue[0],
			pValue[1],
			pValue[2]
		);
		return ATT_SUCCESS;
	case CARD10_LEDS_ABOVE_VAL_HDL:
		for( ui16 = 0; ui16 < 11; ui16++) {
			epic_leds_set(
				ui16,
				pValue[ui16 * 3],
				pValue[ui16 * 3 + 1],
				pValue[ui16 * 3 + 2]
			);
			APP_TRACE_INFO4(
				"ble-card10: set led %ld above to #%02x%02x%02x\n",
				ui16,
				pValue[ui16 * 3],
				pValue[ui16 * 3 + 1],
				pValue[ui16 * 3 + 2]
			);
		}
545
546
547
548
	case CARD10_LED_S_VAL_HDL:
		BYTES_TO_UINT16(ui16, pValue);
		epic_leds_set(ui16, pValue[2], pValue[3], pValue[4]);
		APP_TRACE_INFO4(
genofire's avatar
genofire committed
549
			"ble-card10: set single led %ld to #%02x%02x%02x\n",
550
551
552
553
554
555
			ui16,
			pValue[2],
			pValue[3],
			pValue[4]
		);
		return ATT_SUCCESS;
556
557
558
559
560
561
562
563
	default:
		APP_TRACE_INFO1(
			"ble-card10: unsupported characteristic: %c\n", handle
		);
		return ATT_ERR_HANDLE;
	}
}

genofire's avatar
genofire committed
564
565
566
567
/*
 * BLE card10 read callback.
 */

568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
static uint8_t readCard10CB(
	dmConnId_t connId,
	uint16_t handle,
	uint8_t operation,
	uint16_t offset,
	attsAttr_t *pAttr
) {
	uint16_t ui16 = 0;

	switch (handle) {
	case CARD10_LIGHT_SENSOR_VAL_HDL:
		epic_light_sensor_get(&ui16);
		*pAttr->pValue = ui16;
		APP_TRACE_INFO1("ble-card10: read lightsensor: %d\n", ui16);
		return ATT_SUCCESS;
	default:
		APP_TRACE_INFO0("ble-card10: read no handler found\n");
		return ATT_ERR_HANDLE;
	}
}

/*
genofire's avatar
genofire committed
590
 * This registers and starts the BLE card10 service.
591
592
593
594
595
596
597
 */

void bleCard10_init(void)
{
	void *pSHdl = addCard10GroupDyn();
	AttsDynRegister(pSHdl, readCard10CB, writeCard10CB);
}