-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightbulb.c
2484 lines (2187 loc) · 109 KB
/
lightbulb.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
* @brief Lightbulb module
* This file implements a lightbulb with associated mesh models
*******************************************************************************
* # License
* <b>Copyright 2018 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/
#include "lightbulb.h"
/* C Standard Library headers */
#include <stdlib.h>
#include <stdio.h>
/* Bluetooth stack headers */
#include "mesh_generic_model_capi_types.h"
#include "mesh_lighting_model_capi_types.h"
#include "mesh_lib.h"
/* LED driver with support for PWM dimming */
#include "led_driver.h"
/***********************************************************************************************//**
* @addtogroup Lightbulb
* @{
**************************************************************************************************/
/** 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)
/*******************************************************************************
* Timer handles defines.
******************************************************************************/
#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_PRI_LEVEL 49
#define TIMER_ID_PRI_LEVEL_TRANSITION 48
#define TIMER_ID_DELAYED_CTL 47
#define TIMER_ID_CTL_TRANSITION 46
#define TIMER_ID_DELAYED_CTL_TEMPERATURE 45
#define TIMER_ID_CTL_TEMP_TRANSITION 44
#define TIMER_ID_DELAYED_SEC_LEVEL 43
#define TIMER_ID_SEC_LEVEL_TRANSITION 42
static uint16 _primary_elem_index = 0xffff; /* For indexing elements of the node */
static uint16 _secondary_elem_index = 0xffff; /* For indexing elements of the node */
/***********************************************************************************************//**
* \defgroup lightbulb_state Lightbulb State
* \brief Manage lightbulb state.
**************************************************************************************************/
/***********************************************************************************************//**
* @addtogroup lightbulb_state
* @{
**************************************************************************************************/
/// Lightbulb state
static PACKSTRUCT(struct lightbulb_state {
// On/Off Server state
uint8_t onoff_current; /**< Current generic on/off value */
uint8_t onoff_target; /**< Target generic on/off value */
// Transition Time Server state
uint8_t transtime; /**< Transition time */
// On Power Up Server state
uint8_t onpowerup; /**< On Power Up value */
// Lightness server
uint16_t lightness_current; /**< Current lightness value */
uint16_t lightness_target; /**< Target lightness value */
uint16_t lightness_last; /**< Last lightness value */
uint16_t lightness_default; /**< Default lightness value */
// Primary Generic Level
int16_t pri_level_current; /**< Current primary generic level value */
int16_t pri_level_target; /**< Target primary generic level value */
// Temperature server
uint16_t temperature_current; /**< Current temperature value */
uint16_t temperature_target; /**< Target temperature value */
uint16_t temperature_default; /**< Default temperature value */
uint16_t temperature_min; /**< Minimum temperature value */
uint16_t temperature_max; /**< Maximum temperature value */
// Delta UV
int16_t deltauv_current; /**< Current delta UV value */
int16_t deltauv_target; /**< Target delta UV value */
int16_t deltauv_default; /**< Default delta UV value */
// Secondary Generic Level
int16_t sec_level_current; /**< Current secondary generic level value */
int16_t sec_level_target; /**< Target secondary generic level value */
}) lightbulb_state;
/** @} (end addtogroup lightbulb_state) */
/// copy of transition delay parameter, needed for delayed on/off request
static uint32_t delayed_onoff_trans = 0;
/// copy of transition delay parameter, needed for delayed lightness request
static uint32_t delayed_lightness_trans = 0;
/// copy of transition delay parameter, needed for delayed primary generic level request
static uint32_t delayed_pri_level_trans = 0;
/// copy of transition delay parameter, needed for delayed ctl request
static uint32_t delayed_ctl_trans = 0;
/// copy of transition delay parameter, needed for delayed temperature request
static uint32_t delayed_ctl_temperature_trans = 0;
/// copy of transition delay parameter, needed for delayed secondary generic level request
static 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);
/***********************************************************************************************//**
* \defgroup mesh_models Mesh Models
* \brief Mesh models associated with lightbulb.
**************************************************************************************************/
/***********************************************************************************************//**
* @addtogroup mesh_models
* @{
**************************************************************************************************/
/***************************************************************************//**
* This function convert mesh format of default transition time to milliseconds.
*
* @return Default transition time in milliseconds.
******************************************************************************/
static uint32_t default_transition_time(void)
{
return mesh_lib_transition_time_to_ms(lightbulb_state.transtime);
}
/***********************************************************************************************//**
* \defgroup GenericOnOff
* \brief Generic OnOff Server model.
**************************************************************************************************/
/***********************************************************************************************//**
* @addtogroup GenericOnOff
* @{
**************************************************************************************************/
/***************************************************************************//**
* Response to generic on/off request.
*
* @param[in] element_index Server model element index.
* @param[in] client_addr Address of the client model which sent the message.
* @param[in] appkey_index The application key index used in encrypting.
*
* @return Status of the response operation.
* Returns bg_err_success (0) if succeed, non-zero otherwise.
******************************************************************************/
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);
}
/***************************************************************************//**
* Update generic on/off state.
*
* @param[in] element_index Server model element index.
*
* @return Status of the update operation.
* Returns bg_err_success (0) if succeed, non-zero otherwise.
******************************************************************************/
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);
}
/***************************************************************************//**
* Update generic on/off state and publish model state to the network.
*
* @param[in] element_index Server model element index.
*
* @return Status of the update and publish operation.
* Returns bg_err_success (0) if succeed, non-zero otherwise.
******************************************************************************/
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;
}
/***************************************************************************//**
* This function process the requests for the generic on/off model.
*
* @param[in] model_id Server model ID.
* @param[in] element_index Server model element index.
* @param[in] client_addr Address of the client model which sent the message.
* @param[in] server_addr Address the message was sent to.
* @param[in] appkey_index The application key index used in encrypting the request.
* @param[in] request Pointer to the request structure.
* @param[in] transition_ms Requested transition time (in milliseconds).
* @param[in] delay_ms Delay time (in milliseconds).
* @param[in] request_flags Message flags. Bitmask of the following:
* - Bit 0: Nonrelayed. If nonzero indicates
* a response to a nonrelayed request.
* - Bit 1: Response required. If nonzero client
* expects a response from the server.
******************************************************************************/
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);
}
}
/***************************************************************************//**
* This function is a handler for generic on/off change event.
*
* @param[in] model_id Server model ID.
* @param[in] element_index Server model element index.
* @param[in] current Pointer to current state structure.
* @param[in] target Pointer to target state structure.
* @param[in] remaining_ms Time (in milliseconds) remaining before transition
* from current state to target state is complete.
******************************************************************************/
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");
}
}
/***************************************************************************//**
* This function is called when a light on/off request
* with non-zero transition time has completed.
******************************************************************************/
static void onoff_transition_complete(void)
{
// transition done -> set state, update and publish
lightbulb_state.onoff_current = lightbulb_state.onoff_target;
printf("transition complete. New state is %s\r\n", lightbulb_state.onoff_current ? "ON" : "OFF");
lightbulb_state_changed();
onoff_update_and_publish(_primary_elem_index);
}
/***************************************************************************//**
* This function is called when delay for light on/off request has completed.
******************************************************************************/
static void delayed_onoff_request(void)
{
printf("starting delayed on/off request: %u -> %u, %lu ms\r\n",
lightbulb_state.onoff_current,
lightbulb_state.onoff_target,
delayed_onoff_trans
);
if (delayed_onoff_trans == 0) {
// no transition delay, update state immediately
lightbulb_state.onoff_current = lightbulb_state.onoff_target;
if (lightbulb_state.onoff_current == MESH_GENERIC_ON_OFF_STATE_OFF) {
LEDS_SetState(LED_STATE_OFF);
} else {
// restore last brightness level
LEDS_SetLevel(lightbulb_state.lightness_last, 0);
lightbulb_state.lightness_current = lightbulb_state.lightness_last;
}
lightbulb_state_changed();
onoff_update_and_publish(_primary_elem_index);
} else {
if (lightbulb_state.onoff_target == MESH_GENERIC_ON_OFF_STATE_OFF) {
LEDS_SetLevel(0, delayed_onoff_trans);
} else {
// restore last brightness level, with transition delay
LEDS_SetLevel(lightbulb_state.lightness_last, delayed_onoff_trans);
lightbulb_state.lightness_target = lightbulb_state.lightness_last;
}
// state is updated when transition is complete
gecko_cmd_hardware_set_soft_timer(TIMER_MS_2_TIMERTICK(delayed_onoff_trans), TIMER_ID_ONOFF_TRANSITION, 1);
}
}
/** @} (end addtogroup GenericOnOff) */
/***********************************************************************************************//**
* \defgroup GenericPowerOnOff
* \brief Generic Power OnOff Server model.
**************************************************************************************************/
/***********************************************************************************************//**
* @addtogroup GenericPowerOnOff
* @{
**************************************************************************************************/
/***************************************************************************//**
* Response to generic power on/off request.
*
* @param[in] element_index Server model element index.
* @param[in] client_addr Address of the client model which sent the message.
* @param[in] appkey_index The application key index used in encrypting.
*
* @return Status of the response operation.
* Returns bg_err_success (0) if succeed, non-zero otherwise.
******************************************************************************/
static errorcode_t power_onoff_response(uint16_t element_index,
uint16_t client_addr,
uint16_t appkey_index)
{
struct mesh_generic_state current;
current.kind = mesh_generic_state_on_power_up;
current.on_power_up.on_power_up = lightbulb_state.onpowerup;
return mesh_lib_generic_server_response(MESH_GENERIC_POWER_ON_OFF_SETUP_SERVER_MODEL_ID,
element_index,
client_addr,
appkey_index,
¤t,
NULL,
0,
0x00);
}
/***************************************************************************//**
* Update generic power on/off state.
*
* @param[in] element_index Server model element index.
*
* @return Status of the update operation.
* Returns bg_err_success (0) if succeed, non-zero otherwise.
******************************************************************************/
static errorcode_t power_onoff_update(uint16_t element_index)
{
struct mesh_generic_state current;
current.kind = mesh_generic_state_on_power_up;
current.on_power_up.on_power_up = lightbulb_state.onpowerup;
return mesh_lib_generic_server_update(MESH_GENERIC_POWER_ON_OFF_SERVER_MODEL_ID,
element_index,
¤t,
NULL,
0);
}
/***************************************************************************//**
* Update generic power on/off state and publish model state to the network.
*
* @param[in] element_index Server model element index.
*
* @return Status of the update and publish operation.
* Returns bg_err_success (0) if succeed, non-zero otherwise.
******************************************************************************/
static errorcode_t power_onoff_update_and_publish(uint16_t element_index)
{
errorcode_t e;
e = power_onoff_update(element_index);
if (e == bg_err_success) {
e = mesh_lib_generic_server_publish(MESH_GENERIC_POWER_ON_OFF_SERVER_MODEL_ID,
element_index,
mesh_generic_state_on_power_up);
}
return e;
}
/***************************************************************************//**
* This function process the requests for the generic power on/off model.
*
* @param[in] model_id Server model ID.
* @param[in] element_index Server model element index.
* @param[in] client_addr Address of the client model which sent the message.
* @param[in] server_addr Address the message was sent to.
* @param[in] appkey_index The application key index used in encrypting the request.
* @param[in] request Pointer to the request structure.
* @param[in] transition_ms Requested transition time (in milliseconds).
* @param[in] delay_ms Delay time (in milliseconds).
* @param[in] request_flags Message flags. Bitmask of the following:
* - Bit 0: Nonrelayed. If nonzero indicates
* a response to a nonrelayed request.
* - Bit 1: Response required. If nonzero client
* expects a response from the server.
******************************************************************************/
static void power_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 POWER UP request received; state=<%s>\n",
lightbulb_state.onpowerup == 0 ? "OFF"
: lightbulb_state.onpowerup == 1 ? "ON"
: "RESTORE");
if (lightbulb_state.onpowerup == request->on_power_up) {
printf("Request for current state received; no op\n");
} else {
printf("Setting onpowerup to <%s>\n",
request->on_power_up == 0 ? "OFF"
: request->on_power_up == 1 ? "ON"
: "RESTORE");
lightbulb_state.onpowerup = request->on_power_up;
lightbulb_state_changed();
}
if (request_flags & MESH_REQUEST_FLAG_RESPONSE_REQUIRED) {
power_onoff_response(element_index, client_addr, appkey_index);
} else {
power_onoff_update(element_index);
}
}
/***************************************************************************//**
* This function is a handler for generic power on/off change event.
*
* @param[in] model_id Server model ID.
* @param[in] element_index Server model element index.
* @param[in] current Pointer to current state structure.
* @param[in] target Pointer to target state structure.
* @param[in] remaining_ms Time (in milliseconds) remaining before transition
* from current state to target state is complete.
******************************************************************************/
static void power_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)
{
// TODO
}
/** @} (end addtogroup GenericPowerOnOff) */
/***********************************************************************************************//**
* \defgroup GenericTransitionTime
* \brief Generic Default Transition Time Server model.
**************************************************************************************************/
/***********************************************************************************************//**
* @addtogroup GenericTransitionTime
* @{
**************************************************************************************************/
/***************************************************************************//**
* Response to generic default transition time request.
*
* @param[in] element_index Server model element index.
* @param[in] client_addr Address of the client model which sent the message.
* @param[in] appkey_index The application key index used in encrypting.
*
* @return Status of the response operation.
* Returns bg_err_success (0) if succeed, non-zero otherwise.
******************************************************************************/
static errorcode_t transtime_response(uint16_t element_index,
uint16_t client_addr,
uint16_t appkey_index)
{
struct mesh_generic_state current;
current.kind = mesh_generic_state_transition_time;
current.transition_time.time = lightbulb_state.transtime;
return mesh_lib_generic_server_response(MESH_GENERIC_TRANSITION_TIME_SERVER_MODEL_ID,
element_index,
client_addr,
appkey_index,
¤t,
NULL,
0,
0x00);
}
/***************************************************************************//**
* Update generic default transition time state.
*
* @param[in] element_index Server model element index.
*
* @return Status of the update operation.
* Returns bg_err_success (0) if succeed, non-zero otherwise.
******************************************************************************/
static errorcode_t transtime_update(uint16_t element_index)
{
struct mesh_generic_state current;
current.kind = mesh_generic_state_transition_time;
current.transition_time.time = lightbulb_state.transtime;
return mesh_lib_generic_server_update(MESH_GENERIC_TRANSITION_TIME_SERVER_MODEL_ID,
element_index,
¤t,
NULL,
0);
}
/***************************************************************************//**
* This function process the requests for the generic default transition time
* model.
*
* @param[in] model_id Server model ID.
* @param[in] element_index Server model element index.
* @param[in] client_addr Address of the client model which sent the message.
* @param[in] server_addr Address the message was sent to.
* @param[in] appkey_index The application key index used in encrypting the request.
* @param[in] request Pointer to the request structure.
* @param[in] transition_ms Requested transition time (in milliseconds).
* @param[in] delay_ms Delay time (in milliseconds).
* @param[in] request_flags Message flags. Bitmask of the following:
* - Bit 0: Nonrelayed. If nonzero indicates
* a response to a nonrelayed request.
* - Bit 1: Response required. If nonzero client
* expects a response from the server.
******************************************************************************/
static void transtime_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("TRANSTIME request received; state=<0x%x>\n",
lightbulb_state.transtime);
if (lightbulb_state.transtime == request->transition_time) {
printf("Request for current state received; no op\n");
} else {
printf("Setting transtime to <0x%x>\n", request->transition_time);
lightbulb_state.transtime = request->transition_time;
lightbulb_state_changed();
}
if (request_flags & MESH_REQUEST_FLAG_RESPONSE_REQUIRED) {
transtime_response(element_index, client_addr, appkey_index);
} else {
transtime_update(element_index);
}
}
/***************************************************************************//**
* This function is a handler for generic default transition time change event.
*
* @param[in] model_id Server model ID.
* @param[in] element_index Server model element index.
* @param[in] current Pointer to current state structure.
* @param[in] target Pointer to target state structure.
* @param[in] remaining_ms Time (in milliseconds) remaining before transition
* from current state to target state is complete.
******************************************************************************/
static void transtime_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)
{
// TODO
}
/** @} (end addtogroup GenericTransitionTime) */
/***********************************************************************************************//**
* \defgroup LightLightness
* \brief Light Lightness Server model.
**************************************************************************************************/
/***********************************************************************************************//**
* @addtogroup LightLightness
* @{
**************************************************************************************************/
/***************************************************************************//**
* Response to light lightness request.
*
* @param[in] element_index Server model element index.
* @param[in] client_addr Address of the client model which sent the message.
* @param[in] appkey_index The application key index used in encrypting.
*
* @return Status of the response operation.
* Returns bg_err_success (0) if succeed, non-zero otherwise.
******************************************************************************/
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);
}
/***************************************************************************//**
* Update light lightness state.
*
* @param[in] element_index Server model element index.
*
* @return Status of the update operation.
* Returns bg_err_success (0) if succeed, non-zero otherwise.
******************************************************************************/
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);
}
/***************************************************************************//**
* Update light lightness state and publish model state to the network.
*
* @param[in] element_index Server model element index.
*
* @return Status of the update and publish operation.
* Returns bg_err_success (0) if succeed, non-zero otherwise.
******************************************************************************/
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;
}
/***************************************************************************//**
* This function process the requests for the light lightness model.
*
* @param[in] model_id Server model ID.
* @param[in] element_index Server model element index.
* @param[in] client_addr Address of the client model which sent the message.
* @param[in] server_addr Address the message was sent to.
* @param[in] appkey_index The application key index used in encrypting the request.
* @param[in] request Pointer to the request structure.
* @param[in] transition_ms Requested transition time (in milliseconds).
* @param[in] delay_ms Delay time (in milliseconds).
* @param[in] request_flags Message flags. Bitmask of the following:
* - Bit 0: Nonrelayed. If nonzero indicates
* a response to a nonrelayed request.
* - Bit 1: Response required. If nonzero client
* expects a response from the server.
******************************************************************************/
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);
}
}
/***************************************************************************//**
* This function is a handler for light lightness change event.
*
* @param[in] model_id Server model ID.
* @param[in] element_index Server model element index.
* @param[in] current Pointer to current state structure.
* @param[in] target Pointer to target state structure.
* @param[in] remaining_ms Time (in milliseconds) remaining before transition
* from current state to target state is complete.
******************************************************************************/
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);
}
}
/***************************************************************************//**
* This function is called when a light lightness request
* with non-zero transition time has completed.
******************************************************************************/
static void lightness_transition_complete(void)
{
// transition done -> set state, update and publish
lightbulb_state.lightness_current = lightbulb_state.lightness_target;
if (lightbulb_state.lightness_target != 0) {
lightbulb_state.lightness_last = lightbulb_state.lightness_target;
}
printf("transition complete. New level is %u\r\n", lightbulb_state.lightness_current);
lightbulb_state_changed();
lightness_update_and_publish(_primary_elem_index);
}
/***************************************************************************//**
* This function is called when delay for light lightness request has completed.
******************************************************************************/
static void delayed_lightness_request(void)
{
printf("starting delayed lightness request: level %u -> %u, %lu ms\r\n",
lightbulb_state.lightness_current,
lightbulb_state.lightness_target,
delayed_lightness_trans
);
LEDS_SetLevel(lightbulb_state.lightness_target, delayed_lightness_trans);
if (delayed_lightness_trans == 0) {
// no transition delay, update state immediately
lightbulb_state.lightness_current = lightbulb_state.lightness_target;
if (lightbulb_state.lightness_target != 0) {
lightbulb_state.lightness_last = lightbulb_state.lightness_target;
}
lightbulb_state_changed();
lightness_update_and_publish(_primary_elem_index);
} else {
// state is updated when transition is complete
gecko_cmd_hardware_set_soft_timer(TIMER_MS_2_TIMERTICK(delayed_lightness_trans), TIMER_ID_LIGHTNESS_TRANSITION, 1);
}
}
/** @} (end addtogroup LightLightness) */
/***********************************************************************************************//**
* \defgroup PriGenericLevel
* \brief Generic Level Server model on primary element.
**************************************************************************************************/
/***********************************************************************************************//**
* @addtogroup PriGenericLevel
* @{
**************************************************************************************************/
/***************************************************************************//**
* Response to generic level request on primary element.
*
* @param[in] element_index Server model element index.
* @param[in] client_addr Address of the client model which sent the message.
* @param[in] appkey_index The application key index used in encrypting.
*
* @return Status of the response operation.
* Returns bg_err_success (0) if succeed, non-zero otherwise.
******************************************************************************/
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);
}
/***************************************************************************//**
* Update generic level state on primary element.
*
* @param[in] element_index Server model element index.
*
* @return Status of the update operation.
* Returns bg_err_success (0) if succeed, non-zero otherwise.
******************************************************************************/
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);
}
/***************************************************************************//**
* Update generic level state on primary element
* and publish model state to the network.
*
* @param[in] element_index Server model element index.
*
* @return Status of the update and publish operation.
* Returns bg_err_success (0) if succeed, non-zero otherwise.
******************************************************************************/
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,