-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathapp.c
More file actions
668 lines (590 loc) · 23.7 KB
/
app.c
File metadata and controls
668 lines (590 loc) · 23.7 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
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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/***************************************************************************//**
* @file
* @brief Core application logic.
*******************************************************************************
* # License
* <b>Copyright 2025 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* SPDX-License-Identifier: Zlib
*
* The licensor of this software is Silicon Laboratories Inc.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
*******************************************************************************
* # Experimental Quality
* This code has not been formally tested and is provided as-is. It is not
* suitable for production environments. In addition, this code will not be
* maintained and there may be no bug maintenance planned for these resources.
* Silicon Labs may update projects from time to time.
******************************************************************************/
#include <stdio.h>
#include "sl_bluetooth.h"
#include "sl_sleeptimer.h"
#include "gatt_db.h"
#include "sl_common.h"
#include "app_assert.h"
#include "app.h"
#include "app_log.h"
#include "app_assert.h"
#include "sl_i2cspm_instances.h"
#include "glib.h"
#include "glib_font.h"
#include "container_nvm.h"
#include "container_rgb.h"
#include "sl_simple_button_instances.h"
// structure for container information
typedef struct container_info_t
{
container_nvm_config_t config[4];
uint32_t sample_counter[4];
uint32_t container_value[4];
uint8_t is_updated[4];
}container_info_t;
/**************************************************************************//**
* application definitions.
*****************************************************************************/
// operational mode
#define NORMAL_MODE 0xff
#define CONFIG_MODE 0xfe
// external signal flag
#define C_LVL_CLIENT_TIMER_EVENT 1 << 0
// user request time out
#define TIMER_TIMEOUT 25000
// maximum container slot
#define MAX_CONTAINER_SLOT 4
/**************************************************************************//**
* static variables.
*****************************************************************************/
// timer for main periodic callback
static sl_sleeptimer_timer_handle_t app_sleep_timer_main;
// timer for connection and user request time out
static sl_sleeptimer_timer_handle_t app_advertising_timer;
// The advertising set handle allocated from Bluetooth stack.
static uint8_t advertising_set_handle = 0xff;
// container object hold information of all container
static container_info_t container;
// glib object show status of application
static glib_context_t status_context;
// application selected mode
static uint8_t app_mode = NORMAL_MODE;
// part name of sensor devices
static char target_scanned_name[] = "CON_LEV_S_";
// connection handle
static uint8_t app_connection_handle;
// slot selected
static uint8_t slot_selected = 0;
/**************************************************************************//**
* static functions.
*****************************************************************************/
// main periodic callback to update sensor device's info
static void app_sleep_timer_main_callback(sl_sleeptimer_timer_handle_t *handle,
void *data);
// connection timeout callback
static void app_advertising_timer_callback(sl_sleeptimer_timer_handle_t *handle,
void *data);
// Scanner legacy advertisement report handler.
static void scanner_legacy_advertisement_report_handle(sl_bt_msg_t content);
// update sensor devices's level
static void container_level_device_check_event_handler(void);
// connect to phone
static void connection_open_event_handler(sl_bt_msg_t *evt);
// system boot
static void app_system_boot_event_handler(void);
// update application's status
static void container_display_status(char *status);
// find sensor device's name in advertisement packet
static uint16_t find_name_in_advertisement(uint8_t *data, uint8_t len);
// user write request handler
static void container_app_user_write_request_handler(sl_bt_msg_t *evt);
// user read request handler
static void container_app_user_read_request_handler(sl_bt_msg_t *evt);
/**************************************************************************//**
* Application Init.
*****************************************************************************/
void app_init(void)
{
/////////////////////////////////////////////////////////////////////////////
// Put your additional application init code here! //
// This is called once during start-up. //
/////////////////////////////////////////////////////////////////////////////
sl_status_t sc;
container_config_nvm3_init();
container_nvm3_get_config(container.config);
// show all container in nvm
for (uint8_t slot = 0; slot < MAX_CONTAINER_SLOT; slot++)
{
app_log("container id: %x, low: %d, high: %d\n",
container.config[slot].id_configuration,
container.config[slot].lowest_level,
container.config[slot].highest_level);
}
// initialize the led matrix
sc = container_rgb_init(sl_i2cspm_qwiic);
app_assert(sc == SL_STATUS_OK,
"\rAdafruit RGB LED Matrix initialization fail\n");
// show start up
container_rgb_context_init(&status_context);
container_display_status("<>");
app_log("\rAdafruit RGB LED initialization done\n");
app_log("\rStart displaying text\n");
sl_sleeptimer_delay_millisecond(1000);
// check user selected mode
sl_simple_button_enable(&sl_button_btn0);
if (sl_simple_button_get_state(&sl_button_btn0) == SL_SIMPLE_BUTTON_PRESSED) {
app_mode = CONFIG_MODE;
container_display_status("CFG");
app_log("configuration mode.\n");
} else {
app_mode = NORMAL_MODE;
container_display_status("Nom");
app_log("Normal mode.\n");
glib_clear(&status_context);
}
}
/**************************************************************************//**
* Application Process Action.
*****************************************************************************/
void app_process_action(void)
{
/////////////////////////////////////////////////////////////////////////////
// Put your additional application code here! //
// This is called infinitely. //
// Do not call blocking functions from here! //
/////////////////////////////////////////////////////////////////////////////
}
/**************************************************************************//**
* Bluetooth stack event handler.
* This overrides the dummy weak implementation.
*
* @param[in] evt Event coming from the Bluetooth stack.
*****************************************************************************/
void sl_bt_on_event(sl_bt_msg_t *evt)
{
sl_status_t sc;
switch (SL_BT_MSG_ID(evt->header)) {
// -------------------------------
// This event indicates the device has started and the radio is ready.
// Do not call any stack command before receiving this boot event!
case sl_bt_evt_system_boot_id:
app_system_boot_event_handler();
break;
case sl_bt_evt_scanner_legacy_advertisement_report_id:
scanner_legacy_advertisement_report_handle(*evt);
break;
// -------------------------------
// This event indicates that a new connection was opened.
case sl_bt_evt_connection_opened_id:
connection_open_event_handler(evt);
break;
case sl_bt_evt_gatt_server_user_write_request_id:
container_app_user_write_request_handler(evt);
break;
case sl_bt_evt_gatt_server_user_read_request_id:
container_app_user_read_request_handler(evt);
break;
// -------------------------------
// This event indicates that a connection was closed.
case sl_bt_evt_connection_closed_id:
// Generate data for advertising
sc = sl_bt_legacy_advertiser_generate_data(advertising_set_handle,
sl_bt_advertiser_general_discoverable);
app_assert_status(sc);
// Restart advertising after client has disconnected.
sc = sl_bt_legacy_advertiser_start(advertising_set_handle,
sl_bt_advertiser_connectable_scannable);
app_assert_status(sc);
break;
case sl_bt_evt_system_external_signal_id:
container_level_device_check_event_handler();
break;
///////////////////////////////////////////////////////////////////////////
// Add additional event handlers here as your application requires! //
///////////////////////////////////////////////////////////////////////////
// -------------------------------
// Default event handler.
default:
break;
}
}
void app_system_boot_event_handler()
{
sl_status_t sc;
switch (app_mode)
{
case NORMAL_MODE:
{
// start scanning devices
sc = sl_bt_scanner_start(sl_bt_scanner_scan_phy_1m,
sl_bt_scanner_discover_observation);
app_assert_status(sc);
app_log("Start scanning\r\n");
// start main periodic callback to update status;
sl_sleeptimer_start_periodic_timer_ms(&app_sleep_timer_main,
1500,
app_sleep_timer_main_callback,
NULL,
0,
0);
break;
}
case CONFIG_MODE:
{
// Create an advertising set.
sc = sl_bt_advertiser_create_set(&advertising_set_handle);
app_assert_status(sc);
// Generate data for advertising
sc = sl_bt_legacy_advertiser_generate_data(advertising_set_handle,
sl_bt_advertiser_general_discoverable);
app_assert_status(sc);
// Set advertising interval to 100ms.
sc = sl_bt_advertiser_set_timing(
advertising_set_handle,
160, // min. adv. interval (milliseconds * 1.6)
160, // max. adv. interval (milliseconds * 1.6)
0, // adv. duration
0); // max. num. adv. events
app_assert_status(sc);
// Start advertising and enable connections.
sc = sl_bt_legacy_advertiser_start(advertising_set_handle,
sl_bt_advertiser_connectable_scannable);
app_assert_status(sc);
app_log("Start advertising.\r\n");
// start timer for checking timeout request
sc = sl_sleeptimer_start_periodic_timer_ms(&app_advertising_timer,
TIMER_TIMEOUT,
app_advertising_timer_callback,
NULL,
1,
0);
app_assert_status(sc);
break;
}
default:
{
app_log("invalid mode. Please reset device.\r\n");
container_display_status("ER");
break;
}
}
}
static void scanner_legacy_advertisement_report_handle(sl_bt_msg_t content)
{
sl_status_t sc;
uint8_t data_advertisement[100] = { 0 };
uint32_t sample_counter = 0;
uint8_t slot = 0;
uint8_t *raw_data = 0;
uint8_t raw_data_len = 0;
uint16_t scanned_name = 0;
// get advertisement data
raw_data = content.data.evt_scanner_legacy_advertisement_report.data.data;
raw_data_len = content.data.evt_scanner_legacy_advertisement_report.data.len;
scanned_name = find_name_in_advertisement(raw_data, raw_data_len);
// If a sensor advertisement is found...
if (scanned_name != 0) {
for (slot = 0; slot < MAX_CONTAINER_SLOT; slot++)
{
// update information if container is already included
if (scanned_name == container.config[slot].id_configuration) {
// app_log("matched.\n");
memcpy(data_advertisement, raw_data, raw_data_len);
sample_counter = (data_advertisement[7]
| (data_advertisement[8] << 8)
| (data_advertisement[9] << 16)
| (data_advertisement[10] << 24));
// update if sample counter changed
if (sample_counter != container.sample_counter[slot]) {
container.container_value[slot] = data_advertisement[11]
| (data_advertisement[12] << 8)
| (data_advertisement[13] << 16)
| (data_advertisement[14] << 24);
container.sample_counter[slot] = sample_counter;
container.is_updated[slot] = true;
}
return;
}
}
// found new device
for (slot = 0; slot < MAX_CONTAINER_SLOT; slot++) {
// save to slot have id 0x00.
if (container.config[slot].id_configuration == 0) {
app_log("Found device: %x, slot: %d\n",
scanned_name,
slot);
// get information of new device
container.config[slot].id_configuration = scanned_name;
container.container_value[slot] = data_advertisement[11]
| (data_advertisement[12] << 8)
| (data_advertisement[13] << 16)
| (data_advertisement[14] << 24);
container.sample_counter[slot] = sample_counter;
container.is_updated[slot] = true;
// update in nvm
sc = container_nvm3_write(NVM3_KEY_MIN + slot, container.config[slot]);
app_assert_status(sc);
return;
}
}
}
}
// main timer tick
static void app_sleep_timer_main_callback(sl_sleeptimer_timer_handle_t *handle,
void *data)
{
(void)data;
(void)handle;
sl_bt_external_signal(C_LVL_CLIENT_TIMER_EVENT); // trigger main timer tick
}
// connection and request timeout callback
static void app_advertising_timer_callback(sl_sleeptimer_timer_handle_t *handle,
void *data)
{
(void) handle;
(void) data;
app_log("time out. reseting.\r\n");
sl_bt_system_reboot();
}
void container_level_device_check_event_handler()
{
int distance = 0;
for (int slot = 0; slot < MAX_CONTAINER_SLOT; slot++)
{
if (container.config[slot].id_configuration != 0x00) {
// if sensor device is updated
if ((container.is_updated[slot] == true)) {
// get sensor value
if (container.container_value[slot]
< container.config[slot].lowest_level) {
distance = 9;
} else if (container.container_value[slot]
>= container.config[slot].highest_level) {
distance = 1;
} else {
distance = (container.config[slot].highest_level
- container.container_value[slot]) * 9
/ (container.config[slot].highest_level
- container.config[slot].lowest_level);
if (distance == 0) {
distance = 1;
}
}
} else {
distance = 0;
}
// update in the led matrix
container_rgb_update_level(slot, distance);
container.is_updated[slot] = false;
app_log("updating device: %x, distance: %d, distan: %d\n\n",
(int)container.config[slot].id_configuration,
(int)container.container_value[slot],
distance);
}
}
}
void container_display_status(char *status)
{
glib_clear(&status_context);
glib_draw_string(&status_context, status, 0, 5);
glib_update_display();
}
static void connection_open_event_handler(sl_bt_msg_t *evt)
{
app_log("connection opened. \r\n");
container_display_status("->");
app_connection_handle = evt->data.evt_connection_opened.connection;
}
static uint16_t find_name_in_advertisement(uint8_t *data, uint8_t len)
{
uint8_t ad_field_length;
uint8_t ad_field_type;
uint8_t i = 0;
// Parse advertisement packet
while (i < len) {
ad_field_length = data[i];
ad_field_type = data[i + 1];
// Shortened Local Name ($08) or Complete Local Name($09)
if ((ad_field_type == 0x08) || (ad_field_type == 0x09)) {
// compare name
if (memcmp(&data[i + 2], target_scanned_name, 5) == 0) {
return (data[i + 12] << 8 | data[i + 13]);
}
}
// advance to the next AD struct
i = i + ad_field_length + 1;
}
return 0;
}
static void container_app_user_write_request_handler(sl_bt_msg_t *evt)
{
sl_status_t sc = 0;
uint16_t data = 1;
uint8_t *raw_data = 0;
raw_data = evt->data.evt_gatt_server_user_write_request.value.data;
app_log("writing\n");
data = (uint16_t)atoi((char *)raw_data);
switch (evt->data.evt_gatt_server_user_write_request.characteristic)
{
case gattdb_select_container_to_configure:
{
if (data >= MAX_CONTAINER_SLOT) {
app_log("invalid selected slot\r\n");
container_display_status("ER");
sc = SL_STATUS_BT_ATT_OUT_OF_RANGE;
} else {
app_log("Writing selected slot\r\n");
container_display_status("WT");
slot_selected = data;
}
break;
}
case gattdb_container_lowest_level:
{
if ((data > 4000) || (data < 40)
|| (data > container.config[slot_selected].highest_level)) {
app_log("invalid lowest value\r\n");
container_display_status("ER");
sc = SL_STATUS_BT_ATT_OUT_OF_RANGE;
} else {
app_log("Writing lowest value\r\n");
container_display_status("LO");
container.config[slot_selected].lowest_level = data;
sc = container_nvm3_write(NVM3_KEY_MIN + slot_selected,
container.config[slot_selected]);
}
break;
}
case gattdb_container_highest_level:
{
if ((data > 4000) || (data < 40)
|| (data < container.config[slot_selected].lowest_level)) {
app_log("invalid highest value\r\n");
container_display_status("ER");
sc = SL_STATUS_BT_ATT_OUT_OF_RANGE;
} else {
app_log("Writing highest value\r\n");
container_display_status("HI");
container.config[slot_selected].highest_level = data;
sc = container_nvm3_write(NVM3_KEY_MIN + slot_selected,
container.config[slot_selected]);
}
break;
}
default: break;
}
sc = sl_bt_gatt_server_send_user_write_response(
app_connection_handle,
evt->data.evt_gatt_server_user_write_request.characteristic,
sc);
app_assert_status(sc);
// time out last request.
sc = sl_sleeptimer_restart_periodic_timer_ms(&app_advertising_timer,
TIMER_TIMEOUT,
app_advertising_timer_callback,
NULL,
1,
0);
app_assert_status(sc);
}
static void container_app_user_read_request_handler(sl_bt_msg_t *evt)
{
sl_status_t sc = 0;
char respond_data[10];
uint8_t respond_data_length = 0;
memset(respond_data, 0, sizeof(respond_data));
switch (evt->data.evt_gatt_server_user_write_request.characteristic)
{
case gattdb_select_container_to_configure:
{
app_log("reading selected slot\r\n");
container_display_status("RD");
respond_data_length = snprintf(respond_data, 10, "%d", slot_selected);
sc = sl_bt_gatt_server_send_user_read_response(app_connection_handle,
gattdb_select_container_to_configure,
0,
(size_t)respond_data_length,
(uint8_t *)respond_data,
NULL);
app_assert_status(sc);
break;
}
case gattdb_container_slot_configuration:
{
app_log("reading id slot\r\n");
container_display_status("id");
respond_data_length = snprintf(respond_data,
10,
"%d",
container.config[slot_selected].id_configuration);
sc = sl_bt_gatt_server_send_user_read_response(app_connection_handle,
gattdb_select_container_to_configure,
0,
(size_t)respond_data_length,
(uint8_t *)respond_data,
NULL);
app_assert_status(sc);
break;
}
case gattdb_container_lowest_level:
{
app_log("reading lowest value\r\n");
container_display_status("LO");
container_nvm3_get_config(container.config);
respond_data_length = snprintf(respond_data,
10,
"%d",
container.config[slot_selected].lowest_level);
sc = sl_bt_gatt_server_send_user_read_response(app_connection_handle,
gattdb_container_lowest_level,
0,
(size_t)respond_data_length,
(uint8_t *)respond_data,
NULL);
app_assert_status(sc);
break;
}
case gattdb_container_highest_level:
{
app_log("reading highest value\r\n");
container_display_status("HI");
container_nvm3_get_config(container.config);
respond_data_length = snprintf(respond_data,
10,
"%d",
container.config[slot_selected].highest_level);
sc = sl_bt_gatt_server_send_user_read_response(app_connection_handle,
gattdb_container_highest_level,
0,
(size_t)respond_data_length,
(uint8_t *)respond_data,
NULL);
app_assert_status(sc);
break;
}
default:
break;
}
// time out last request.
sc = sl_sleeptimer_restart_periodic_timer_ms(&app_advertising_timer,
TIMER_TIMEOUT,
app_advertising_timer_callback,
NULL,
1,
0);
app_assert_status(sc);
}