-
Notifications
You must be signed in to change notification settings - Fork 7.2k
/
lighting_server.c
3659 lines (3211 loc) · 134 KB
/
lighting_server.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
/* Bluetooth: Mesh Lighting Server Models
*
* SPDX-FileCopyrightText: 2018 Vikrant More
* SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include "btc_ble_mesh_lighting_model.h"
#include "mesh_config.h"
#include "access.h"
#include "transport.h"
#include "model_opcode.h"
#include "state_transition.h"
#include "device_property.h"
#if CONFIG_BLE_MESH_LIGHTING_SERVER
static bt_mesh_mutex_t light_server_lock;
static inline void bt_mesh_light_server_mutex_new(void)
{
if (!light_server_lock.mutex) {
bt_mesh_mutex_create(&light_server_lock);
}
}
#if CONFIG_BLE_MESH_DEINIT
static inline void bt_mesh_light_server_mutex_free(void)
{
bt_mesh_mutex_free(&light_server_lock);
}
#endif /* CONFIG_BLE_MESH_DEINIT */
void bt_mesh_light_server_lock(void)
{
bt_mesh_mutex_lock(&light_server_lock);
}
void bt_mesh_light_server_unlock(void)
{
bt_mesh_mutex_unlock(&light_server_lock);
}
/* message handlers (Start) */
/* Light Lightness Server/Setup Server message handlers */
static void send_light_lightness_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
bool publish, uint16_t opcode)
{
struct net_buf_simple *msg = NULL;
uint8_t length = 2 + 5;
if (ctx == NULL && publish == false) {
BT_ERR("%s, Invalid parameter", __func__);
return;
}
if (publish == false) {
msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
if (msg == NULL) {
BT_ERR("%s, Out of memory", __func__);
return;
}
} else {
msg = bt_mesh_server_get_pub_msg(model, length);
if (msg == NULL) {
return;
}
}
bt_mesh_model_msg_init(msg, opcode);
switch (opcode) {
case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS: {
struct bt_mesh_light_lightness_srv *srv = model->user_data;
net_buf_simple_add_le16(msg, srv->state->lightness_actual);
if (srv->actual_transition.counter) {
bt_mesh_server_calc_remain_time(&srv->actual_transition);
net_buf_simple_add_le16(msg, srv->state->target_lightness_actual);
net_buf_simple_add_u8(msg, srv->actual_transition.remain_time);
}
break;
}
case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS: {
struct bt_mesh_light_lightness_srv *srv = model->user_data;
net_buf_simple_add_le16(msg, srv->state->lightness_linear);
if (srv->linear_transition.counter) {
bt_mesh_server_calc_remain_time(&srv->linear_transition);
net_buf_simple_add_le16(msg, srv->state->target_lightness_linear);
net_buf_simple_add_u8(msg, srv->linear_transition.remain_time);
}
break;
}
case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS: {
struct bt_mesh_light_lightness_srv *srv = model->user_data;
net_buf_simple_add_le16(msg, srv->state->lightness_last);
break;
}
case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS:
if (model->id == BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV) {
struct bt_mesh_light_lightness_srv *srv = model->user_data;
net_buf_simple_add_le16(msg, srv->state->lightness_default);
} else if (model->id == BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV) {
struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
net_buf_simple_add_le16(msg, srv->state->lightness_default);
}
break;
case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS:
if (model->id == BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV) {
struct bt_mesh_light_lightness_srv *srv = model->user_data;
net_buf_simple_add_u8(msg, srv->state->status_code);
net_buf_simple_add_le16(msg, srv->state->lightness_range_min);
net_buf_simple_add_le16(msg, srv->state->lightness_range_max);
} else if (model->id == BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV) {
struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
net_buf_simple_add_u8(msg, srv->state->status_code);
net_buf_simple_add_le16(msg, srv->state->lightness_range_min);
net_buf_simple_add_le16(msg, srv->state->lightness_range_max);
}
break;
default:
BT_WARN("Unknown Light Lightness status opcode 0x%04x", opcode);
if (publish == false) {
bt_mesh_free_buf(msg);
}
return;
}
if (publish == false) {
BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
bt_mesh_free_buf(msg);
} else {
BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
}
return;
}
static void light_lightness_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_light_lightness_srv *srv = model->user_data;
uint16_t opcode = 0U;
if (srv == NULL || srv->state == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}
/* Callback the received message to the application layer */
if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_lighting_server_cb_evt_to_btc(
BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
return;
}
switch (ctx->recv_op) {
case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_GET:
opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS;
break;
case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_GET:
opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS;
break;
case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_GET:
opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LAST_STATUS;
break;
case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_GET:
opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS;
break;
case BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_GET:
opcode = BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS;
break;
default:
BT_WARN("Unknown Light Lightness Get opcode 0x%04x", ctx->recv_op);
return;
}
send_light_lightness_status(model, ctx, false, opcode);
return;
}
void light_lightness_publish(struct bt_mesh_model *model, uint16_t opcode)
{
if (model->user_data == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}
switch (model->id) {
case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV: {
struct bt_mesh_light_lightness_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Light Lightness Server state");
return;
}
break;
}
case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV: {
struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Light Lightness Setup Server state");
return;
}
break;
}
default:
BT_ERR("Invalid Light Lightness Server model 0x%04x", model->id);
return;
}
send_light_lightness_status(model, NULL, true, opcode);
return;
}
static void light_lightness_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_light_lightness_srv *srv = model->user_data;
uint8_t tid = 0U, trans_time = 0U, delay = 0U;
bool optional = false;
uint16_t actual = 0U;
int64_t now = 0;
if (srv == NULL || srv->state == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}
actual = net_buf_simple_pull_le16(buf);
tid = net_buf_simple_pull_u8(buf);
if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
return;
}
/* Callback the received message to the application layer */
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_light_server_recv_set_msg_t set = {
.lightness_set.op_en = optional,
.lightness_set.lightness = actual,
.lightness_set.tid = tid,
.lightness_set.trans_time = trans_time,
.lightness_set.delay = delay,
};
bt_mesh_lighting_server_cb_evt_to_btc(
BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
return;
}
if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET) {
send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
}
send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
/* In this condition, no event will be callback to application layer */
return;
}
bt_mesh_light_server_lock();
bt_mesh_server_stop_transition(&srv->actual_transition);
bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
if (actual) {
if (srv->state->lightness_range_min && actual < srv->state->lightness_range_min) {
actual = srv->state->lightness_range_min;
} else if (srv->state->lightness_range_max && actual > srv->state->lightness_range_max) {
actual = srv->state->lightness_range_max;
}
}
srv->state->target_lightness_actual = actual;
/**
* If the target state is equal to the current state, the transition shall not be
* started and is considered complete.
*/
if (srv->state->target_lightness_actual != srv->state->lightness_actual) {
light_lightness_actual_tt_values(srv, trans_time, delay);
} else {
bt_mesh_light_server_state_change_t change = {
.lightness_set.lightness = srv->state->lightness_actual,
};
bt_mesh_lighting_server_cb_evt_to_btc(
BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET) {
send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
}
send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
bt_mesh_light_server_unlock();
return;
}
/* Copy the ctx of the received message */
if (srv->actual_transition.timer.work._reserved) {
memcpy(srv->actual_transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
}
/* For Instantaneous Transition */
if (srv->actual_transition.counter == 0U) {
srv->state->lightness_actual = srv->state->target_lightness_actual;
/**
* Whenever the Light Lightness Actual state is changed with a non-transactional
* message or a completed sequence of transactional messages to a non-zero value,
* the value of the Light Lightness Last shall be set to the value of the Light
* Lightness Actual.
*/
if (srv->state->lightness_actual) {
srv->state->lightness_last = srv->state->lightness_actual;
}
}
srv->actual_transition.just_started = true;
if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_SET) {
send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
}
send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_STATUS);
bt_mesh_light_server_unlock();
bt_mesh_server_start_transition(&srv->actual_transition);
return;
}
static void light_lightness_linear_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_light_lightness_srv *srv = model->user_data;
uint8_t tid = 0U, trans_time = 0U, delay = 0U;
bool optional = false;
uint16_t linear = 0U;
int64_t now = 0;
if (srv == NULL || srv->state == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}
linear = net_buf_simple_pull_le16(buf);
tid = net_buf_simple_pull_u8(buf);
if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
return;
}
/* Callback the received message to the application layer */
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_light_server_recv_set_msg_t set = {
.lightness_linear_set.op_en = optional,
.lightness_linear_set.lightness = linear,
.lightness_linear_set.tid = tid,
.lightness_linear_set.trans_time = trans_time,
.lightness_linear_set.delay = delay,
};
bt_mesh_lighting_server_cb_evt_to_btc(
BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
return;
}
if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET) {
send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
}
send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
/* In this condition, no event will be callback to application layer */
return;
}
bt_mesh_light_server_lock();
bt_mesh_server_stop_transition(&srv->linear_transition);
bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
srv->state->target_lightness_linear = linear;
/**
* If the target state is equal to the current state, the transition shall not
* be started and is considered complete.
*/
if (srv->state->target_lightness_linear != srv->state->lightness_linear) {
light_lightness_linear_tt_values(srv, trans_time, delay);
} else {
bt_mesh_light_server_state_change_t change = {
.lightness_linear_set.lightness = srv->state->lightness_actual,
};
bt_mesh_lighting_server_cb_evt_to_btc(
BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET) {
send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
}
send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
bt_mesh_light_server_unlock();
return;
}
/* Copy the ctx of the received message */
if (srv->linear_transition.timer.work._reserved) {
memcpy(srv->linear_transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
}
/* For Instantaneous Transition */
if (srv->linear_transition.counter == 0U) {
srv->state->lightness_linear = srv->state->target_lightness_linear;
}
srv->linear_transition.just_started = true;
if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_SET) {
send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
}
send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_LINEAR_STATUS);
bt_mesh_light_server_unlock();
bt_mesh_server_start_transition(&srv->linear_transition);
return;
}
static void light_lightness_default_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
uint16_t lightness = 0U;
if (srv == NULL || srv->state == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}
lightness = net_buf_simple_pull_le16(buf);
/* Callback the received message to the application layer */
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_light_server_recv_set_msg_t set = {
.lightness_default_set.lightness = lightness,
};
bt_mesh_lighting_server_cb_evt_to_btc(
BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
return;
}
if (srv->state->lightness_default != lightness) {
srv->state->lightness_default = lightness;
bt_mesh_light_server_state_change_t change = {
.lightness_default_set.lightness = lightness,
};
bt_mesh_lighting_server_cb_evt_to_btc(
BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
}
if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_SET) {
send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS);
}
send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_DEFAULT_STATUS);
return;
}
static void light_lightness_range_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_light_lightness_setup_srv *srv = model->user_data;
uint16_t range_min = 0U, range_max = 0U;
if (srv == NULL || srv->state == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}
range_min = net_buf_simple_pull_le16(buf);
range_max = net_buf_simple_pull_le16(buf);
if (range_min > range_max) {
BT_ERR("Range min 0x%04x is greater than range max 0x%04x",
range_min, range_max);
return;
}
/* Callback the received message to the application layer */
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_light_server_recv_set_msg_t set = {
.lightness_range_set.range_min = range_min,
.lightness_range_set.range_max = range_max,
};
bt_mesh_lighting_server_cb_evt_to_btc(
BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
return;
}
/**
* When a Light Lightness Setup Server receives a Light Lightness Range Set
* message or a Light Lightness Range Set Unacknowledged message with values
* that cannot be accepted, it shall set the status of the operation to a
* value representing the reason why the values cannot be accepted.
*
* TODO: 0x0000 for Light Range Min/Max is prohibited, but BQB test case
* MMDL/SR/LLNS/BI-01-C requires 'SUCCESS' when it sends a set message with
* Light Range Min set to 0x0000.
*/
#if 0
srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS;
#else
if (range_min == 0x0000) {
srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MIN;
} else if (range_max == 0x0000) {
srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MAX;
} else {
srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS;
}
#endif
if (range_min && srv->state->lightness_range_min != range_min) {
srv->state->lightness_range_min = range_min;
}
if (range_max && srv->state->lightness_range_max != range_max) {
srv->state->lightness_range_max = range_max;
}
bt_mesh_light_server_state_change_t change = {
.lightness_range_set.range_min = srv->state->lightness_range_min,
.lightness_range_set.range_max = srv->state->lightness_range_max,
};
bt_mesh_lighting_server_cb_evt_to_btc(
BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_SET) {
send_light_lightness_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS);
}
send_light_lightness_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_LIGHTNESS_RANGE_STATUS);
return;
}
/* Light CTL Server/Temperature Server/Setup Server message handlers */
static void send_light_ctl_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
bool publish, uint16_t opcode)
{
struct net_buf_simple *msg = NULL;
uint8_t length = 2 + 9;
if (ctx == NULL && publish == false) {
BT_ERR("%s, Invalid parameter", __func__);
return;
}
if (publish == false) {
msg = bt_mesh_alloc_buf(length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
if (msg == NULL) {
BT_ERR("%s, Out of memory", __func__);
return;
}
} else {
msg = bt_mesh_server_get_pub_msg(model, length);
if (msg == NULL) {
return;
}
}
bt_mesh_model_msg_init(msg, opcode);
switch (opcode) {
case BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS: {
struct bt_mesh_light_ctl_srv *srv = model->user_data;
net_buf_simple_add_le16(msg, srv->state->lightness);
net_buf_simple_add_le16(msg, srv->state->temperature);
if (srv->transition.counter) {
bt_mesh_server_calc_remain_time(&srv->transition);
net_buf_simple_add_le16(msg, srv->state->target_lightness);
net_buf_simple_add_le16(msg, srv->state->target_temperature);
net_buf_simple_add_u8(msg, srv->transition.remain_time);
}
break;
}
case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS:
if (model->id == BLE_MESH_MODEL_ID_LIGHT_CTL_SRV) {
struct bt_mesh_light_ctl_srv *srv = model->user_data;
net_buf_simple_add_u8(msg, srv->state->status_code);
net_buf_simple_add_le16(msg, srv->state->temperature_range_min);
net_buf_simple_add_le16(msg, srv->state->temperature_range_max);
} else if (model->id == BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV) {
struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
net_buf_simple_add_u8(msg, srv->state->status_code);
net_buf_simple_add_le16(msg, srv->state->temperature_range_min);
net_buf_simple_add_le16(msg, srv->state->temperature_range_max);
}
break;
case BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS: {
if (model->id == BLE_MESH_MODEL_ID_LIGHT_CTL_SRV) {
struct bt_mesh_light_ctl_srv *srv = model->user_data;
net_buf_simple_add_le16(msg, srv->state->lightness_default);
net_buf_simple_add_le16(msg, srv->state->temperature_default);
net_buf_simple_add_le16(msg, srv->state->delta_uv_default);
} else if (model->id == BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV) {
struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
net_buf_simple_add_le16(msg, srv->state->lightness_default);
net_buf_simple_add_le16(msg, srv->state->temperature_default);
net_buf_simple_add_le16(msg, srv->state->delta_uv_default);
}
break;
}
case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS: {
struct bt_mesh_light_ctl_temp_srv *srv = model->user_data;
net_buf_simple_add_le16(msg, srv->state->temperature);
net_buf_simple_add_le16(msg, srv->state->delta_uv);
if (srv->transition.counter) {
bt_mesh_server_calc_remain_time(&srv->transition);
net_buf_simple_add_le16(msg, srv->state->target_temperature);
net_buf_simple_add_le16(msg, srv->state->target_delta_uv);
net_buf_simple_add_u8(msg, srv->transition.remain_time);
}
break;
}
default:
BT_WARN("Unknown Light CTL status opcode 0x%04x", opcode);
if (publish == false) {
bt_mesh_free_buf(msg);
}
return;
}
if (publish == false) {
BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
bt_mesh_free_buf(msg);
} else {
BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
}
return;
}
static void light_ctl_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_server_rsp_ctrl *rsp_ctrl = NULL;
uint16_t opcode = 0U;
if (model->user_data == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}
switch (model->id) {
case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV: {
struct bt_mesh_light_ctl_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Light CTL Server state");
return;
}
rsp_ctrl = &srv->rsp_ctrl;
break;
}
case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV: {
struct bt_mesh_light_ctl_temp_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Light CTL Temperature Server state");
return;
}
rsp_ctrl = &srv->rsp_ctrl;
break;
}
default:
BT_ERR("Invalid Light CTL Server model 0x%04x", model->id);
return;
}
/* Callback the received message to the application layer */
if (rsp_ctrl->get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_lighting_server_cb_evt_to_btc(
BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_GET_MSG, model, ctx, NULL, 0);
return;
}
switch (ctx->recv_op) {
case BLE_MESH_MODEL_OP_LIGHT_CTL_GET:
opcode = BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS;
break;
case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_GET:
opcode = BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS;
break;
case BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_GET:
opcode = BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS;
break;
case BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_GET:
opcode = BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_STATUS;
break;
default:
BT_WARN("Unknown Light CTL Get opcode 0x%04x", ctx->recv_op);
return;
}
send_light_ctl_status(model, ctx, false, opcode);
return;
}
void light_ctl_publish(struct bt_mesh_model *model, uint16_t opcode)
{
if (model->user_data == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}
switch (model->id) {
case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV: {
struct bt_mesh_light_ctl_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Light CTL Server state");
return;
}
break;
}
case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV: {
struct bt_mesh_light_ctl_temp_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Light CTL Temperature Server state");
return;
}
break;
}
case BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV: {
struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Light CTL Setup Server state");
return;
}
break;
}
default:
BT_ERR("Invalid Light CTL Server model 0x%04x", model->id);
return;
}
send_light_ctl_status(model, NULL, true, opcode);
return;
}
static void light_ctl_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_light_ctl_srv *srv = model->user_data;
uint16_t lightness = 0U, temperature = 0U;
uint8_t tid = 0U, trans_time = 0U, delay = 0U;
int16_t delta_uv = 0;
bool optional = false;
int64_t now = 0;
if (srv == NULL || srv->state == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}
lightness = net_buf_simple_pull_le16(buf);
temperature = net_buf_simple_pull_le16(buf);
delta_uv = (int16_t) net_buf_simple_pull_le16(buf);
tid = net_buf_simple_pull_u8(buf);
if (temperature < BLE_MESH_TEMPERATURE_MIN || temperature > BLE_MESH_TEMPERATURE_MAX) {
BT_ERR("Invalid temperature 0x%04x", temperature);
return;
}
if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
return;
}
/* Callback the received message to the application layer */
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_light_server_recv_set_msg_t set = {
.ctl_set.op_en = optional,
.ctl_set.lightness = lightness,
.ctl_set.temperature = temperature,
.ctl_set.delta_uv = delta_uv,
.ctl_set.tid = tid,
.ctl_set.trans_time = trans_time,
.ctl_set.delay = delay,
};
bt_mesh_lighting_server_cb_evt_to_btc(
BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
return;
}
if (bt_mesh_is_server_recv_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now)) {
if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_SET) {
send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
}
send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
/* In this condition, no event will be callback to application layer */
return;
}
bt_mesh_light_server_lock();
bt_mesh_server_stop_transition(&srv->transition);
bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
srv->state->target_lightness = lightness;
if (srv->state->temperature_range_min &&
srv->state->temperature_range_min != BLE_MESH_TEMPERATURE_UNKNOWN &&
temperature < srv->state->temperature_range_min) {
temperature = srv->state->temperature_range_min;
} else if (srv->state->temperature_range_max &&
srv->state->temperature_range_max != BLE_MESH_TEMPERATURE_UNKNOWN &&
temperature > srv->state->temperature_range_max) {
temperature = srv->state->temperature_range_max;
}
srv->state->target_temperature = temperature;
srv->state->target_delta_uv = delta_uv;
if (srv->state->target_lightness != srv->state->lightness ||
srv->state->target_temperature != srv->state->temperature ||
srv->state->target_delta_uv != srv->state->delta_uv) {
light_ctl_tt_values(srv, trans_time, delay);
} else {
bt_mesh_light_server_state_change_t change = {
.ctl_set.lightness = srv->state->lightness,
.ctl_set.temperature = srv->state->temperature,
.ctl_set.delta_uv = srv->state->delta_uv,
};
bt_mesh_lighting_server_cb_evt_to_btc(
BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_SET) {
send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
}
send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
bt_mesh_light_server_unlock();
return;
}
/* Copy the ctx of the received message */
if (srv->transition.timer.work._reserved) {
memcpy(srv->transition.timer.work._reserved, ctx, sizeof(struct bt_mesh_msg_ctx));
}
/* For Instantaneous Transition */
if (srv->transition.counter == 0U) {
srv->state->lightness = srv->state->target_lightness;
srv->state->temperature = srv->state->target_temperature;
srv->state->delta_uv = srv->state->target_delta_uv;
}
srv->transition.just_started = true;
if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_SET) {
send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
}
send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_STATUS);
bt_mesh_light_server_unlock();
bt_mesh_server_start_transition(&srv->transition);
return;
}
static void light_ctl_default_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
uint16_t lightness = 0U, temperature = 0U;
int16_t delta_uv = 0;
if (srv == NULL || srv->state == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}
lightness = net_buf_simple_pull_le16(buf);
temperature = net_buf_simple_pull_le16(buf);
delta_uv = (int16_t) net_buf_simple_pull_le16(buf);
if (temperature < BLE_MESH_TEMPERATURE_MIN || temperature > BLE_MESH_TEMPERATURE_MAX) {
BT_ERR("Invalid temperature 0x%04x", temperature);
return;
}
/* Callback the received message to the application layer */
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_light_server_recv_set_msg_t set = {
.ctl_default_set.lightness = lightness,
.ctl_default_set.temperature = temperature,
.ctl_default_set.delta_uv = delta_uv,
};
bt_mesh_lighting_server_cb_evt_to_btc(
BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
return;
}
if (srv->state->temperature_range_min &&
srv->state->temperature_range_min != BLE_MESH_TEMPERATURE_UNKNOWN &&
temperature < srv->state->temperature_range_min) {
temperature = srv->state->temperature_range_min;
} else if (srv->state->temperature_range_max &&
srv->state->temperature_range_max != BLE_MESH_TEMPERATURE_UNKNOWN &&
temperature > srv->state->temperature_range_max) {
temperature = srv->state->temperature_range_max;
}
srv->state->lightness_default = lightness;
srv->state->temperature_default = temperature;
srv->state->delta_uv_default = delta_uv;
bt_mesh_light_server_state_change_t change = {
.ctl_default_set.lightness = srv->state->lightness_default,
.ctl_default_set.temperature = srv->state->temperature_default,
.ctl_default_set.delta_uv = srv->state->delta_uv_default,
};
bt_mesh_lighting_server_cb_evt_to_btc(
BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_SET) {
send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS);
}
send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_DEFAULT_STATUS);
return;
}
static void light_ctl_temp_range_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_light_ctl_setup_srv *srv = model->user_data;
uint16_t min = 0U, max = 0U;
if (srv == NULL || srv->state == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}
min = net_buf_simple_pull_le16(buf);
max = net_buf_simple_pull_le16(buf);
/* This is as per 6.1.3.1 in Mesh Model Specification */
if (min > max ||
min < BLE_MESH_TEMPERATURE_MIN || (min != BLE_MESH_TEMPERATURE_UNKNOWN && min > BLE_MESH_TEMPERATURE_MAX) ||
max < BLE_MESH_TEMPERATURE_MIN || (max != BLE_MESH_TEMPERATURE_UNKNOWN && max > BLE_MESH_TEMPERATURE_MAX)) {
BT_ERR("Invalid parameter, range min 0x%04x, range max 0x%04x",
min, max);
return;
}
/* Callback the received message to the application layer */
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_light_server_recv_set_msg_t set = {
.ctl_temp_range_set.range_min = min,
.ctl_temp_range_set.range_max = max,
};
bt_mesh_lighting_server_cb_evt_to_btc(
BTC_BLE_MESH_EVT_LIGHTING_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
return;
}
if (min == BLE_MESH_TEMPERATURE_UNKNOWN) {
srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MIN;
} else if (max == BLE_MESH_TEMPERATURE_UNKNOWN ) {
srv->state->status_code = BLE_MESH_CANNOT_SET_RANGE_MAX;
} else {
srv->state->status_code = BLE_MESH_RANGE_UPDATE_SUCCESS;
}
if (min != BLE_MESH_TEMPERATURE_UNKNOWN && srv->state->temperature_range_min != min) {
srv->state->temperature_range_min = min;
}
if (max != BLE_MESH_TEMPERATURE_UNKNOWN && srv->state->temperature_range_max != max) {
srv->state->temperature_range_max = max;
}
bt_mesh_light_server_state_change_t change = {
.ctl_temp_range_set.range_min = srv->state->temperature_range_min,
.ctl_temp_range_set.range_max = srv->state->temperature_range_max,
};
bt_mesh_lighting_server_cb_evt_to_btc(
BTC_BLE_MESH_EVT_LIGHTING_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_SET) {
send_light_ctl_status(model, ctx, false, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS);
}
send_light_ctl_status(model, NULL, true, BLE_MESH_MODEL_OP_LIGHT_CTL_TEMPERATURE_RANGE_STATUS);
return;
}
static void light_ctl_temp_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,