/
silabs_mesh_lpn_main.c
executable file
·2292 lines (1948 loc) · 86.2 KB
/
silabs_mesh_lpn_main.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
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************************************//**
* \file main.c - with LPN - cheng
* \brief Silicon Labs Bluetooth mesh light example
*
* This example implements a Bluetooth mesh light node.
*
***************************************************************************************************
* <b> (C) Copyright 2017 Silicon Labs, http://www.silabs.com</b>
***************************************************************************************************
* This file is licensed under the Silabs License Agreement. See the file
* "Silabs_License_Agreement.txt" for details. Before using this software for
* any purpose, you must agree to the terms of that agreement.
**************************************************************************************************/
/* C Standard Library headers */
#include <stdlib.h>
#include <stdio.h>
/* Board headers */
#include "init_mcu.h"
#include "init_board.h"
#include "init_app.h"
#include "ble-configuration.h"
#include "board_features.h"
#include "retargetserial.h"
/* Bluetooth stack headers */
#include "bg_types.h"
#include "native_gecko.h"
#include "gatt_db.h"
#include <gecko_configuration.h>
#include "mesh_generic_model_capi_types.h"
#include "mesh_lighting_model_capi_types.h"
#include "mesh_lib.h"
#include <mesh_sizes.h>
/* Libraries containing default Gecko configuration values */
#include "em_emu.h"
#include "em_cmu.h"
#include <em_gpio.h>
/* Device initialization header */
#include "hal-config.h"
/* Display Interface header */
#include "display_interface.h"
/* LED driver with support for PWM dimming */
#include "led_driver.h"
#if defined(HAL_CONFIG)
#include "bsphalconfig.h"
#else
#include "bspconfig.h"
#endif
/***********************************************************************************************//**
* @addtogroup Application
* @{
**************************************************************************************************/
/***********************************************************************************************//**
* @addtogroup app
* @{
**************************************************************************************************/
struct mesh_generic_state current, target;
bool mesh_bgapi_listener(struct gecko_cmd_packet *evt);
// Maximum number of simultaneous Bluetooth connections
#define MAX_CONNECTIONS 2
// heap for Bluetooth stack
uint8_t bluetooth_stack_heap[DEFAULT_BLUETOOTH_HEAP(MAX_CONNECTIONS) + BTMESH_HEAP_SIZE + 1760];
// Flag for indicating DFU Reset must be performed
uint8_t boot_to_dfu = 0;
// Bluetooth advertisement set configuration
//
// At minimum the following is required:
// * One advertisement set for Bluetooth LE stack (handle number 0)
// * One advertisement set for Mesh data (handle number 1)
// * One advertisement set for Mesh unprovisioned beacons (handle number 2)
// * One advertisement set for Mesh unprovisioned URI (handle number 3)
// * N advertisement sets for Mesh GATT service advertisements
// (one for each network key, handle numbers 4 .. N+3)
//
#define MAX_ADVERTISERS (4 + MESH_CFG_MAX_NETKEYS)
// Bluetooth stack configuration
const gecko_configuration_t config =
{
.sleep.flags = SLEEP_FLAGS_DEEP_SLEEP_ENABLE,
.bluetooth.max_connections = MAX_CONNECTIONS,
.bluetooth.max_advertisers = MAX_ADVERTISERS,
.bluetooth.heap = bluetooth_stack_heap,
.bluetooth.heap_size = sizeof(bluetooth_stack_heap) - BTMESH_HEAP_SIZE,
.bluetooth.sleep_clock_accuracy = 100,
.gattdb = &bg_gattdb_data,
.btmesh_heap_size = BTMESH_HEAP_SIZE,
#if (HAL_PA_ENABLE) && defined(FEATURE_PA_HIGH_POWER)
.pa.config_enable = 1, // Enable high power PA
.pa.input = GECKO_RADIO_PA_INPUT_VBAT, // Configure PA input to VBAT
#endif // (HAL_PA_ENABLE) && defined(FEATURE_PA_HIGH_POWER)
.max_timers = 16,
};
/** Timer Frequency used. */
#define TIMER_CLK_FREQ ((uint32)32768)
/** Convert msec to timer ticks. */
#define TIMER_MS_2_TIMERTICK(ms) ((TIMER_CLK_FREQ * ms) / 1000)
#define TIMER_ID_RESTART 78
#define TIMER_ID_FACTORY_RESET 77
#define TIMER_ID_PROVISIONING 66
#define TIMER_ID_SAVE_STATE 60
#define TIMER_ID_ONOFF_TRANSITION 53
#define TIMER_ID_LIGHTNESS_TRANSITION 52
#define TIMER_ID_DELAYED_ONOFF 51
#define TIMER_ID_DELAYED_LIGHTNESS 50
#define TIMER_ID_DELAYED_CTL 49
#define TIMER_ID_CTL_TRANSITION 48
#define TIMER_ID_DELAYED_CTL_TEMPERATURE 47
#define TIMER_ID_CTL_TEMP_TRANSITION 46
#define TIMER_ID_DELAYED_PRI_LEVEL 45
#define TIMER_ID_PRI_LEVEL_TRANSITION 44
#define TIMER_ID_DELAYED_SEC_LEVEL 43
#define TIMER_ID_SEC_LEVEL_TRANSITION 42
#define TIMER_ID_FRIEND_FIND 41
/** global variables */
static uint16 _primary_elem_index = 0xffff; /* For indexing elements of the node */
static uint16 _secondary_elem_index = 0xffff; /* For indexing elements of the node */
static uint16 _my_address = 0; /* Address of the Primary Element of the Node */
static uint8 num_connections = 0; /* number of active Bluetooth connections */
static uint8 conn_handle = 0xFF; /* handle of the last opened LE connection */
static uint8 init_done = 0;
static PACKSTRUCT(struct lightbulb_state {
// On/Off Server state
uint8_t onoff_current;
uint8_t onoff_target;
// Transition Time Server state
uint8_t transtime;
// On Power Up Server state
uint8_t onpowerup;
// Lightness server
uint16_t lightness_current;
uint16_t lightness_target;
uint16_t lightness_last;
uint16_t lightness_default;
// Primary Generic Level
int16_t pri_level_current;
int16_t pri_level_target;
// Temperature server
uint16_t temperature_current;
uint16_t temperature_target;
uint16_t temperature_default;
uint16_t temperature_min;
uint16_t temperature_max;
// Delta UV
int16_t deltauv_current;
int16_t deltauv_target;
int16_t deltauv_default;
// Secondary Generic Level
int16_t sec_level_current;
int16_t sec_level_target;
}) lightbulb_state;
/* copy of transition delay parameter, needed for delayed on/off, lightness, temperature, ctl and
* primary and secondary generic level requests */
uint32_t delayed_onoff_trans = 0;
uint32_t delayed_lightness_trans = 0;
uint32_t delayed_ctl_trans = 0;
uint32_t delayed_ctl_temperature_trans = 0;
uint32_t delayed_pri_level_trans = 0;
uint32_t delayed_sec_level_trans = 0;
static int lightbulb_state_load(void);
static int lightbulb_state_store(void);
static void lightbulb_state_changed(void);
static uint32_t default_transition_time(void)
{
return mesh_lib_transition_time_to_ms(lightbulb_state.transtime);
}
static errorcode_t onoff_response(uint16_t element_index,
uint16_t client_addr,
uint16_t appkey_index)
{
struct mesh_generic_state current, target;
current.kind = mesh_generic_state_on_off;
current.on_off.on = lightbulb_state.onoff_current;
target.kind = mesh_generic_state_on_off;
target.on_off.on = lightbulb_state.onoff_target;
return mesh_lib_generic_server_response(MESH_GENERIC_ON_OFF_SERVER_MODEL_ID,
element_index,
client_addr,
appkey_index,
¤t,
&target,
0,
0x00);
}
static errorcode_t onoff_update(uint16_t element_index)
{
struct mesh_generic_state current, target;
current.kind = mesh_generic_state_on_off;
current.on_off.on = lightbulb_state.onoff_current;
target.kind = mesh_generic_state_on_off;
target.on_off.on = lightbulb_state.onoff_target;
return mesh_lib_generic_server_update(MESH_GENERIC_ON_OFF_SERVER_MODEL_ID,
element_index,
¤t,
&target,
0);
}
static errorcode_t onoff_update_and_publish(uint16_t element_index)
{
errorcode_t e;
e = onoff_update(element_index);
if (e == bg_err_success) {
e = mesh_lib_generic_server_publish(MESH_GENERIC_ON_OFF_SERVER_MODEL_ID,
element_index,
mesh_generic_state_on_off);
}
return e;
}
static void onoff_request(uint16_t model_id,
uint16_t element_index,
uint16_t client_addr,
uint16_t server_addr,
uint16_t appkey_index,
const struct mesh_generic_request *request,
uint32_t transition_ms,
uint16_t delay_ms,
uint8_t request_flags)
{
printf("ON/OFF request: requested state=<%s>, transition=%lu, delay=%u\r\n",
request->on_off ? "ON" : "OFF", transition_ms, delay_ms);
if (lightbulb_state.onoff_current == request->on_off) {
printf("Request for current state received; no op\n");
} else {
printf("Turning lightbulb <%s>\r\n", request->on_off ? "ON" : "OFF");
if (transition_ms == 0 && delay_ms == 0) { // Immediate change
lightbulb_state.onoff_current = request->on_off;
lightbulb_state.onoff_target = request->on_off;
if (lightbulb_state.onoff_current == MESH_GENERIC_ON_OFF_STATE_OFF) {
LEDS_SetState(LED_STATE_OFF);
} else {
LEDS_SetState(LED_STATE_ON);
}
} else if (delay_ms > 0) {
// a delay has been specified for the light change. Start a soft timer
// that will trigger the change after the given delay
// Current state remains as is for now
lightbulb_state.onoff_target = request->on_off;
gecko_cmd_hardware_set_soft_timer(TIMER_MS_2_TIMERTICK(delay_ms), TIMER_ID_DELAYED_ONOFF, 1);
// store transition parameter for later use
delayed_onoff_trans = transition_ms;
} else {
// no delay but transition time has been set.
lightbulb_state.onoff_target = request->on_off;
if (request->on_off == MESH_GENERIC_ON_OFF_STATE_OFF) {
LEDS_SetLevel(0, transition_ms);
} else {
// restore last brightness
lightbulb_state.lightness_target = lightbulb_state.lightness_last;
LEDS_SetLevel(lightbulb_state.lightness_target, transition_ms);
}
// lightbulb current state will be updated when transition is complete
gecko_cmd_hardware_set_soft_timer(TIMER_MS_2_TIMERTICK(transition_ms), TIMER_ID_ONOFF_TRANSITION, 1);
}
lightbulb_state_changed();
}
if (request_flags & MESH_REQUEST_FLAG_RESPONSE_REQUIRED) {
onoff_response(element_index, client_addr, appkey_index);
} else {
onoff_update(element_index);
}
}
static void onoff_change(uint16_t model_id,
uint16_t element_index,
const struct mesh_generic_state *current,
const struct mesh_generic_state *target,
uint32_t remaining_ms)
{
if (current->on_off.on != lightbulb_state.onoff_current) {
printf("on-off state changed %u to %u\r\n", lightbulb_state.onoff_current, current->on_off.on);
lightbulb_state.onoff_current = current->on_off.on;
lightbulb_state_changed();
} else {
printf("dummy onoff change - same state as before\r\n");
}
}
static errorcode_t lightness_response(uint16_t element_index,
uint16_t client_addr,
uint16_t appkey_index)
{
struct mesh_generic_state current, target;
current.kind = mesh_lighting_state_lightness_actual;
current.lightness.level = lightbulb_state.lightness_current;
target.kind = mesh_lighting_state_lightness_actual;
target.lightness.level = lightbulb_state.lightness_target;
return mesh_lib_generic_server_response(MESH_LIGHTING_LIGHTNESS_SERVER_MODEL_ID,
element_index,
client_addr,
appkey_index,
¤t,
&target,
0,
0x00);
}
static errorcode_t lightness_update(uint16_t element_index)
{
struct mesh_generic_state current, target;
current.kind = mesh_lighting_state_lightness_actual;
current.lightness.level = lightbulb_state.lightness_current;
target.kind = mesh_lighting_state_lightness_actual;
target.lightness.level = lightbulb_state.lightness_target;
return mesh_lib_generic_server_update(MESH_LIGHTING_LIGHTNESS_SERVER_MODEL_ID,
element_index,
¤t,
&target,
0);
}
static errorcode_t lightness_update_and_publish(uint16_t element_index)
{
errorcode_t e;
e = lightness_update(element_index);
if (e == bg_err_success) {
e = mesh_lib_generic_server_publish(MESH_LIGHTING_LIGHTNESS_SERVER_MODEL_ID,
element_index,
mesh_lighting_state_lightness_actual);
}
return e;
}
static void lightness_request(uint16_t model_id,
uint16_t element_index,
uint16_t client_addr,
uint16_t server_addr,
uint16_t appkey_index,
const struct mesh_generic_request *request,
uint32_t transition_ms,
uint16_t delay_ms,
uint8_t request_flags)
{
// for simplicity, this demo assumes that all lightness requests use the actual scale.
// other type of requests are ignored
if (request->kind != mesh_lighting_request_lightness_actual) {
return;
}
printf("lightness_request: level=%u, transition=%lu, delay=%u\r\n",
request->lightness, transition_ms, delay_ms);
if (lightbulb_state.lightness_current == request->lightness) {
printf("Request for current state received; no op\n");
} else {
printf("Setting lightness to <%u>\r\n", request->lightness);
if (transition_ms == 0 && delay_ms == 0) { // Immediate change
lightbulb_state.lightness_current = request->lightness;
lightbulb_state.lightness_target = request->lightness;
if (request->lightness != 0) {
lightbulb_state.lightness_last = request->lightness;
}
// update LED PWM duty cycle
LEDS_SetLevel(lightbulb_state.lightness_current, 0);
} else if (delay_ms > 0) {
// a delay has been specified for the light change. Start a soft timer
// that will trigger the change after the given delay
// Current state remains as is for now
lightbulb_state.lightness_target = request->lightness;
gecko_cmd_hardware_set_soft_timer(TIMER_MS_2_TIMERTICK(delay_ms), TIMER_ID_DELAYED_LIGHTNESS, 1);
// store transition parameter for later use
delayed_lightness_trans = transition_ms;
} else {
// no delay but transition time has been set.
lightbulb_state.lightness_target = request->lightness;
LEDS_SetLevel(lightbulb_state.lightness_target, transition_ms);
// lightbulb current state will be updated when transition is complete
gecko_cmd_hardware_set_soft_timer(TIMER_MS_2_TIMERTICK(transition_ms), TIMER_ID_LIGHTNESS_TRANSITION, 1);
}
lightbulb_state_changed();
}
if (request_flags & MESH_REQUEST_FLAG_RESPONSE_REQUIRED) {
lightness_response(element_index, client_addr, appkey_index);
} else {
lightness_update(element_index);
}
}
static void lightness_change(uint16_t model_id,
uint16_t element_index,
const struct mesh_generic_state *current,
const struct mesh_generic_state *target,
uint32_t remaining_ms)
{
if (current->kind != mesh_lighting_state_lightness_actual) {
// if kind is not 'actual' then just report the change here, no change to light state
printf("lightness change, kind %u, value %u\r\n", current->kind, current->lightness.level);
return;
}
if (lightbulb_state.lightness_current != current->lightness.level) {
printf("lightness_change: from %u to %u\r\n", lightbulb_state.lightness_current, current->lightness.level);
lightbulb_state.lightness_current = current->lightness.level;
lightbulb_state_changed();
} else {
printf("lightness update -same value (%d)\r\n", lightbulb_state.lightness_current);
}
}
static errorcode_t pri_level_response(uint16_t element_index,
uint16_t client_addr,
uint16_t appkey_index)
{
struct mesh_generic_state current, target;
current.kind = mesh_generic_state_level;
current.level.level = lightbulb_state.pri_level_current;
target.kind = mesh_generic_state_level;
target.level.level = lightbulb_state.pri_level_target;
return mesh_lib_generic_server_response(MESH_GENERIC_LEVEL_SERVER_MODEL_ID,
element_index,
client_addr,
appkey_index,
¤t,
&target,
0,
0x00);
}
static errorcode_t pri_level_update(uint16_t element_index)
{
struct mesh_generic_state current, target;
current.kind = mesh_generic_state_level;
current.level.level = lightbulb_state.pri_level_current;
target.kind = mesh_generic_state_level;
target.level.level = lightbulb_state.pri_level_target;
return mesh_lib_generic_server_update(MESH_GENERIC_LEVEL_SERVER_MODEL_ID,
element_index,
¤t,
&target,
0);
}
static errorcode_t pri_level_update_and_publish(uint16_t element_index)
{
errorcode_t e;
e = pri_level_update(element_index);
if (e == bg_err_success) {
e = mesh_lib_generic_server_publish(MESH_GENERIC_LEVEL_SERVER_MODEL_ID,
element_index,
mesh_generic_state_level);
}
return e;
}
static void pri_level_request(uint16_t model_id,
uint16_t element_index,
uint16_t client_addr,
uint16_t server_addr,
uint16_t appkey_index,
const struct mesh_generic_request *request,
uint32_t transition_ms,
uint16_t delay_ms,
uint8_t request_flags)
{
// for simplicity, this demo assumes that all level requests use set level.
// other type of requests are ignored
uint16_t lightness;
if (request->kind != mesh_generic_request_level) {
return;
}
printf("pri_level_request: level=%d, transition=%lu, delay=%u\r\n",
request->level, transition_ms, delay_ms);
if (lightbulb_state.pri_level_current == request->level) {
printf("Request for current state received; no op\n");
} else {
printf("Setting pri_level to <%d>\r\n", request->level);
lightness = request->level + 32768;
if (transition_ms == 0 && delay_ms == 0) { // Immediate change
lightbulb_state.pri_level_current = request->level;
lightbulb_state.pri_level_target = request->level;
lightbulb_state.lightness_current = lightness;
lightbulb_state.lightness_target = lightness;
// update LED Temperature
LEDS_SetLevel(lightness, 0);
} else if (delay_ms > 0) {
// a delay has been specified for the light change. Start a soft timer
// that will trigger the change after the given delay
// Current state remains as is for now
lightbulb_state.pri_level_target = request->level;
lightbulb_state.lightness_target = lightness;
gecko_cmd_hardware_set_soft_timer(TIMER_MS_2_TIMERTICK(delay_ms), TIMER_ID_DELAYED_PRI_LEVEL, 1);
// store transition parameter for later use
delayed_pri_level_trans = transition_ms;
} else {
// no delay but transition time has been set.
lightbulb_state.pri_level_target = request->level;
lightbulb_state.lightness_target = lightness;
LEDS_SetLevel(lightness, transition_ms);
// lightbulb current state will be updated when transition is complete
gecko_cmd_hardware_set_soft_timer(TIMER_MS_2_TIMERTICK(transition_ms), TIMER_ID_PRI_LEVEL_TRANSITION, 1);
}
lightbulb_state_changed();
}
if (request_flags & MESH_REQUEST_FLAG_RESPONSE_REQUIRED) {
pri_level_response(element_index, client_addr, appkey_index);
} else {
pri_level_update(element_index);
}
}
static void pri_level_change(uint16_t model_id,
uint16_t element_index,
const struct mesh_generic_state *current,
const struct mesh_generic_state *target,
uint32_t remaining_ms)
{
if (lightbulb_state.pri_level_current != current->level.level) {
printf("pri_level_change: from %d to %d\r\n", lightbulb_state.pri_level_current, current->level.level);
lightbulb_state.pri_level_current = current->level.level;
lightbulb_state_changed();
} else {
printf("pri_level update -same value (%d)\r\n", lightbulb_state.pri_level_current);
}
}
static errorcode_t ctl_response(uint16_t element_index,
uint16_t client_addr,
uint16_t appkey_index)
{
struct mesh_generic_state current, target;
current.kind = mesh_lighting_state_ctl;
current.ctl.lightness = lightbulb_state.lightness_current;
current.ctl.temperature = lightbulb_state.temperature_current;
current.ctl.deltauv = lightbulb_state.deltauv_current;
target.kind = mesh_lighting_state_ctl;
target.ctl.lightness = lightbulb_state.lightness_target;
target.ctl.temperature = lightbulb_state.temperature_target;
target.ctl.deltauv = lightbulb_state.deltauv_target;
return mesh_lib_generic_server_response(MESH_LIGHTING_CTL_SERVER_MODEL_ID,
element_index,
client_addr,
appkey_index,
¤t,
&target,
0,
0x00);
}
static errorcode_t ctl_update(uint16_t element_index)
{
struct mesh_generic_state current, target;
current.kind = mesh_lighting_state_ctl;
current.ctl.lightness = lightbulb_state.lightness_current;
current.ctl.temperature = lightbulb_state.temperature_current;
current.ctl.deltauv = lightbulb_state.deltauv_current;
target.kind = mesh_lighting_state_ctl;
target.ctl.lightness = lightbulb_state.lightness_target;
target.ctl.temperature = lightbulb_state.temperature_target;
target.ctl.deltauv = lightbulb_state.deltauv_target;
return mesh_lib_generic_server_update(MESH_LIGHTING_CTL_SERVER_MODEL_ID,
element_index,
¤t,
&target,
0);
}
static errorcode_t ctl_update_and_publish(uint16_t element_index)
{
errorcode_t e;
e = ctl_update(element_index);
if (e == bg_err_success) {
e = mesh_lib_generic_server_publish(MESH_LIGHTING_CTL_SERVER_MODEL_ID,
element_index,
mesh_lighting_state_ctl);
}
return e;
}
static void ctl_request(uint16_t model_id,
uint16_t element_index,
uint16_t client_addr,
uint16_t server_addr,
uint16_t appkey_index,
const struct mesh_generic_request *request,
uint32_t transition_ms,
uint16_t delay_ms,
uint8_t request_flags)
{
printf("ctl_request: lightness=%u, temperature=%u, delta_uv=%d, transition=%lu, delay=%u\r\n",
request->ctl.lightness, request->ctl.temperature, request->ctl.deltauv, transition_ms, delay_ms);
if ((lightbulb_state.lightness_current == request->ctl.lightness)
&& (lightbulb_state.temperature_current == request->ctl.temperature)
&& (lightbulb_state.deltauv_current == request->ctl.deltauv)) {
printf("Request for current state received; no op\n");
} else {
if (lightbulb_state.lightness_current != request->ctl.lightness) {
printf("Setting lightness to <%u>\r\n", request->lightness);
}
if (lightbulb_state.temperature_current != request->ctl.temperature) {
printf("Setting temperature to <%u>\r\n", request->ctl.temperature);
}
if (lightbulb_state.deltauv_current != request->ctl.deltauv) {
printf("Setting delta UV to <%d>\r\n", request->ctl.deltauv);
}
if (transition_ms == 0 && delay_ms == 0) { // Immediate change
lightbulb_state.lightness_current = request->ctl.lightness;
lightbulb_state.lightness_target = request->ctl.lightness;
if (request->lightness != 0) {
lightbulb_state.lightness_last = request->ctl.lightness;
}
// update LED PWM duty cycle
LEDS_SetLevel(lightbulb_state.lightness_current, 0);
lightbulb_state.temperature_current = request->ctl.temperature;
lightbulb_state.temperature_target = request->ctl.temperature;
lightbulb_state.deltauv_current = request->ctl.deltauv;
lightbulb_state.deltauv_target = request->ctl.deltauv;
// update LED color temperature
LEDS_SetTemperature(lightbulb_state.temperature_current, lightbulb_state.deltauv_current, 0);
} else if (delay_ms > 0) {
// a delay has been specified for the light change. Start a soft timer
// that will trigger the change after the given delay
// Current state remains as is for now
lightbulb_state.lightness_target = request->ctl.lightness;
lightbulb_state.temperature_target = request->ctl.temperature;
lightbulb_state.deltauv_target = request->ctl.deltauv;
gecko_cmd_hardware_set_soft_timer(TIMER_MS_2_TIMERTICK(delay_ms), TIMER_ID_DELAYED_CTL, 1);
// store transition parameter for later use
delayed_ctl_trans = transition_ms;
} else {
// no delay but transition time has been set.
lightbulb_state.lightness_target = request->ctl.lightness;
lightbulb_state.temperature_target = request->ctl.temperature;
lightbulb_state.deltauv_target = request->ctl.deltauv;
LEDS_SetLevel(lightbulb_state.lightness_target, transition_ms);
LEDS_SetTemperature(lightbulb_state.temperature_target, lightbulb_state.deltauv_target, transition_ms);
// lightbulb current state will be updated when transition is complete
gecko_cmd_hardware_set_soft_timer(TIMER_MS_2_TIMERTICK(transition_ms), TIMER_ID_CTL_TRANSITION, 1);
}
lightbulb_state_changed();
}
if (request_flags & MESH_REQUEST_FLAG_RESPONSE_REQUIRED) {
ctl_response(element_index, client_addr, appkey_index);
} else {
ctl_update(element_index);
}
}
static void ctl_change(uint16_t model_id,
uint16_t element_index,
const struct mesh_generic_state *current,
const struct mesh_generic_state *target,
uint32_t remaining_ms)
{
if (current->kind != mesh_lighting_state_ctl) {
// if kind is not 'ctl' then just report the change here
printf("ctl change, kind %u\r\n", current->kind);
return;
}
if (lightbulb_state.lightness_current != current->ctl.lightness) {
printf("lightness_change: from %u to %u\r\n", lightbulb_state.lightness_current, current->ctl.lightness);
lightbulb_state.lightness_current = current->ctl.lightness;
lightbulb_state_changed();
} else {
printf("lightness update -same value (%u)\r\n", lightbulb_state.lightness_current);
}
if (lightbulb_state.temperature_current != current->ctl.temperature) {
printf("temperature_change: from %u to %u\r\n", lightbulb_state.temperature_current, current->ctl.temperature);
lightbulb_state.temperature_current = current->ctl.temperature;
lightbulb_state_changed();
} else {
printf("temperature update -same value (%u)\r\n", lightbulb_state.temperature_current);
}
if (lightbulb_state.deltauv_current != current->ctl.deltauv) {
printf("deltauv_change: from %d to %d\r\n", lightbulb_state.deltauv_current, current->ctl.deltauv);
lightbulb_state.deltauv_current = current->ctl.deltauv;
lightbulb_state_changed();
} else {
printf("deltauv update -same value (%d)\r\n", lightbulb_state.deltauv_current);
}
}
static errorcode_t ctl_setup_response(uint16_t element_index,
uint16_t client_addr,
uint16_t appkey_index,
mesh_generic_state_t kind)
{
struct mesh_generic_state current;
current.kind = kind;
switch (kind) {
case mesh_lighting_state_ctl_default:
current.ctl.lightness = lightbulb_state.lightness_default;
current.ctl.temperature = lightbulb_state.temperature_default;
current.ctl.deltauv = lightbulb_state.deltauv_default;
break;
case mesh_lighting_state_ctl_temperature_range:
current.ctl_temperature_range.min = lightbulb_state.temperature_min;
current.ctl_temperature_range.max = lightbulb_state.temperature_max;
break;
default:
break;
}
return mesh_lib_generic_server_response(MESH_LIGHTING_CTL_SETUP_SERVER_MODEL_ID,
element_index,
client_addr,
appkey_index,
¤t,
NULL,
0,
0x00);
}
static errorcode_t ctl_setup_update(uint16_t element_index, mesh_generic_state_t kind)
{
struct mesh_generic_state current;
current.kind = kind;
switch (kind) {
case mesh_lighting_state_ctl_default:
current.ctl.lightness = lightbulb_state.lightness_default;
current.ctl.temperature = lightbulb_state.temperature_default;
current.ctl.deltauv = lightbulb_state.deltauv_default;
break;
case mesh_lighting_state_ctl_temperature_range:
current.ctl_temperature_range.min = lightbulb_state.temperature_min;
current.ctl_temperature_range.max = lightbulb_state.temperature_max;
break;
default:
break;
}
return mesh_lib_generic_server_update(MESH_LIGHTING_CTL_SERVER_MODEL_ID,
element_index,
¤t,
NULL,
0);
}
static void ctl_setup_request(uint16_t model_id,
uint16_t element_index,
uint16_t client_addr,
uint16_t server_addr,
uint16_t appkey_index,
const struct mesh_generic_request *request,
uint32_t transition_ms,
uint16_t delay_ms,
uint8_t request_flags)
{
switch (request->kind) {
case mesh_lighting_request_ctl_default:
printf("ctl_setup_request: state=ctl_default, default_lightness=%u, default_temperature=%u, default_delta_uv=%d",
request->ctl.lightness, request->ctl.temperature, request->ctl.deltauv);
if ((lightbulb_state.lightness_default == request->ctl.lightness)
&& (lightbulb_state.temperature_default == request->ctl.temperature)
&& (lightbulb_state.deltauv_default == request->ctl.deltauv)) {
printf("Request for current state received; no op\n");
} else {
if (lightbulb_state.lightness_default != request->ctl.lightness) {
printf("Setting default lightness to <%u>\r\n", request->ctl.lightness);
lightbulb_state.lightness_default = request->ctl.lightness;
}
if (lightbulb_state.temperature_default != request->ctl.temperature) {
printf("Setting default temperature to <%u>\r\n", request->ctl.temperature);
lightbulb_state.temperature_default = request->ctl.temperature;
}
if (lightbulb_state.deltauv_default != request->ctl.deltauv) {
printf("Setting default delta UV to <%d>\r\n", request->ctl.deltauv);
lightbulb_state.deltauv_default = request->ctl.deltauv;
}
lightbulb_state_changed();
}
break;
case mesh_lighting_request_ctl_temperature_range:
printf("ctl_setup_request: state=ctl_temperature_range, min_temperature=%u, max_temperature=%u",
request->ctl_temperature_range.min, request->ctl_temperature_range.max);
if ((lightbulb_state.temperature_min == request->ctl_temperature_range.min)
&& (lightbulb_state.temperature_max == request->ctl_temperature_range.max)) {
printf("Request for current state received; no op\n");
} else {
if (lightbulb_state.temperature_min != request->ctl_temperature_range.min) {
printf("Setting min temperature to <%u>\r\n", request->ctl_temperature_range.min);
lightbulb_state.temperature_min = request->ctl_temperature_range.min;
}
if (lightbulb_state.temperature_max != request->ctl_temperature_range.max) {
printf("Setting max temperature to <%u>\r\n", request->ctl_temperature_range.max);
lightbulb_state.temperature_max = request->ctl_temperature_range.max;
}
lightbulb_state_changed();
}
break;
default:
break;
}
if (request_flags & MESH_REQUEST_FLAG_RESPONSE_REQUIRED) {
ctl_setup_response(element_index, client_addr, appkey_index, request->kind);
} else {
ctl_setup_update(element_index, request->kind);
}
}
static void ctl_setup_change(uint16_t model_id,
uint16_t element_index,
const struct mesh_generic_state *current,
const struct mesh_generic_state *target,
uint32_t remaining_ms)
{
switch (current->kind) {
case mesh_lighting_state_ctl_default:
if (lightbulb_state.lightness_default != current->ctl.lightness) {
printf("default_lightness_change: from %u to %u\r\n", lightbulb_state.lightness_default, current->ctl.lightness);
lightbulb_state.lightness_default = current->ctl.lightness;
lightbulb_state_changed();
} else {
printf("default lightness update -same value (%u)\r\n", lightbulb_state.lightness_default);
}
if (lightbulb_state.temperature_default != current->ctl.temperature) {
printf("default_temperature_change: from %u to %u\r\n", lightbulb_state.temperature_default, current->ctl.temperature);
lightbulb_state.temperature_default = current->ctl.temperature;
lightbulb_state_changed();
} else {
printf("default temperature update -same value (%u)\r\n", lightbulb_state.temperature_default);
}
if (lightbulb_state.deltauv_current != current->ctl.deltauv) {
printf("default_deltauv_change: from %d to %d\r\n", lightbulb_state.deltauv_default, current->ctl.deltauv);
lightbulb_state.deltauv_default = current->ctl.deltauv;
lightbulb_state_changed();
} else {
printf("default deltauv update -same value (%d)\r\n", lightbulb_state.deltauv_default);
}
break;
case mesh_lighting_state_ctl_temperature_range:
if (lightbulb_state.temperature_min != current->ctl_temperature_range.min) {
printf("min_temperature_change: from %u to %u\r\n", lightbulb_state.temperature_min, current->ctl_temperature_range.min);
lightbulb_state.temperature_min = current->ctl_temperature_range.min;
lightbulb_state_changed();
} else {
printf("min temperature update -same value (%u)\r\n", lightbulb_state.temperature_min);
}
if (lightbulb_state.temperature_max != current->ctl_temperature_range.max) {
printf("max_temperature_change: from %u to %u\r\n", lightbulb_state.temperature_max, current->ctl_temperature_range.max);
lightbulb_state.temperature_max = current->ctl_temperature_range.max;
lightbulb_state_changed();
} else {
printf("max temperature update -same value (%u)\r\n", lightbulb_state.temperature_max);
}
break;
default:
break;
}
}
static errorcode_t ctl_temperature_response(uint16_t element_index,
uint16_t client_addr,
uint16_t appkey_index)
{
struct mesh_generic_state current, target;
current.kind = mesh_lighting_state_ctl_temperature;
current.ctl.temperature = lightbulb_state.temperature_current;
current.ctl.deltauv = lightbulb_state.deltauv_current;
target.kind = mesh_lighting_state_ctl_temperature;
target.ctl.temperature = lightbulb_state.temperature_target;
target.ctl.deltauv = lightbulb_state.deltauv_target;
return mesh_lib_generic_server_response(MESH_LIGHTING_CTL_TEMPERATURE_SERVER_MODEL_ID,
element_index,
client_addr,
appkey_index,
¤t,
&target,
0,
0x00);
}
static errorcode_t ctl_temperature_update(uint16_t element_index)
{
struct mesh_generic_state current, target;
current.kind = mesh_lighting_state_ctl_temperature;
current.ctl.temperature = lightbulb_state.temperature_current;
current.ctl.deltauv = lightbulb_state.deltauv_current;
target.kind = mesh_lighting_state_ctl_temperature;
target.ctl.temperature = lightbulb_state.temperature_target;
target.ctl.deltauv = lightbulb_state.deltauv_target;
return mesh_lib_generic_server_update(MESH_LIGHTING_CTL_TEMPERATURE_SERVER_MODEL_ID,
element_index,
¤t,
&target,
0);
}
static errorcode_t ctl_temperature_update_and_publish(uint16_t element_index)
{
errorcode_t e;
e = ctl_temperature_update(element_index);
if (e == bg_err_success) {
e = mesh_lib_generic_server_publish(MESH_LIGHTING_CTL_TEMPERATURE_SERVER_MODEL_ID,
element_index,
mesh_lighting_state_ctl_temperature);
}