1
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
28
29
33
34
35
36
37
38
39
40
41
42
43
44
45
49
50
62
63
64
65
70
71
77
78
79
80
81
82
83
84
85
89
90
91
96
97
98
99
100
109
110
111
112
113
114
115
116
117
118
119
123
124
125
129
130
131
134
135
136
148
149
157
158
159
160
161
162
163
164
165
168
171
172
173
174
177
178
179
180
181
182
183
184
187
188
189
192
193
194
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
217
218
230
231
232
233
247
248
254
255
256
257
258
259
260
261
262
266
267
268
273
274
275
276
277
286
287
288
289
290
291
292
293
294
295
296
297
301
302
303
304
305
308
309
310
322
323
331
332
333
334
335
336
337
338
339
343
346
349
350
351
352
355
356
357
358
359
360
361
362
365
366
367
370
371
372
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
396
397
398
399
400
403
404
405
417
418
419
420
421
422
423
424
425
426
427
428
429
445
446
447
448
449
450
451
452
457
458
459
463
466
469
470
471
472
475
476
477
478
479
480
481
482
485
486
487
490
491
492
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
517
518
519
520
521
524
525
526
538
539
547
548
549
550
551
552
553
554
555
556
561
562
563
567
570
573
574
575
576
579
580
581
582
583
584
585
586
587
590
591
595
596
597
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
623
624
625
626
627
628
629
630
631
632
633
634
635
636
640
641
653
654
655
656
657
663
664
665
666
667
668
669
670
671
675
676
677
682
683
684
685
686
687
688
689
690
691
692
693
697
698
699
703
704
705
713
714
717
718
721
722
723
724
727
728
729
730
731
732
733
734
735
736
737
738
742
743
755
756
757
775
776
782
783
784
785
786
787
788
789
790
794
795
796
801
802
803
804
805
806
807
808
812
813
834
835
836
837
838
839
840
841
842
843
844
845
849
850
851
855
856
857
865
866
869
870
873
874
875
876
879
880
881
882
883
884
885
886
887
888
889
890
894
895
907
908
909
954
955
961
962
963
964
965
966
967
968
969
970
974
975
976
981
982
999
1000
1001
1002
1003
1004
1005
1009
1010
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1049
1050
1051
1052
1053
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1079
1080
1081
1082
1083
1084
1085
1092
1093
1094
1097
1100
1103
1104
1105
1106
1109
1110
1111
1112
1113
1114
1115
1116
1119
1120
1121
1132
1133
1134
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1156
1157
1158
1159
1160
1168
1169
1173
1176
1177
1180
1181
1184
1185
1186
1187
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1204
1205
1206
1207
1208
1213
1214
1215
1224
1225
1232
1233
1236
1237
1240
1241
1245
1246
1247
1248
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1266
1267
1268
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1293
1294
1306
1307
1308
1309
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1345
1346
1347
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1366
1367
1368
1373
1374
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1421
1422
1425
1428
1431
1432
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1467
1470
1473
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1492
1493
1494
1495
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1529
1530
1531
1537
1538
1550
1551
1552
1555
1556
1557
1558
1559
1560
1561
1562
1563
1566
1567
1568
1574
1575
1576
1577
1578
1579
1580
1581
1582
1586
1587
1588
1589
1593
1594
1595
1596
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1615
1616
1622
1623
1624
1625
1626
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1650
1651
1652
1653
1654
1663
1664
1665
1666
1673
1674
1679
1680
1685
1686
1687
1688
1689
1693
1694
1695
1696
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1730
1731
1732
1738
1739
1751
1752
1753
1763
1764
1770
1771
1772
1773
1774
1775
1776
1777
1778
1782
1783
1784
1785
1786
1787
1788
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1807
1808
1811
1812
1813
1814
1815
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1839
1840
1841
1842
1846
1847
1848
1858
1859
1860
1867
1868
1869
1870
1871
1872
1873
1878
1879
1880
1881
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1915
1916
1917
1923
1924
1936
1937
1938
1945
1946
1952
1953
1954
1955
1956
1957
1958
1959
1960
1964
1965
1966
1967
1968
1969
1970
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1989
1990
1993
1994
1995
1996
1997
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2021
2022
2023
2024
2028
2029
2030
2039
2040
2041
2048
2049
2050
2051
2055
2056
2057
2058
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2074
2075
2082
2083
2084
2085
2086
2089
2090
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2111
2112
2113
2121
2122
2127
2128
2129
2130
2136
2137
2138
2142
2143
2144
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2188
2189
2190
2194
2195
2196
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2227
2228
2229
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2276
2277
2278
2279
2282
2283
2286
2287
2288
2289
2290
2291
2292
2296
2297
2298
2299
2300
2304
2305
2306
2307
2308
2309
2313
2314
2315
2316
2322
2323
2327
2328
2329
2330
2331
2332
2336
2337
2338
2339
2340
2341
2345
2349
2350
2351
2352
2353
2354
2358
2359
2360
2361
2367
2368
2372
2373
2374
2375
2376
2377
2381
2382
2383
2384
2385
2386
2390
2391
2392
2393
2394
2395
2399
2400
2401
2402
2403
2404
2408
2409
2410
2411
2412
2413
2417
2418
2419
2420
2421
2422
2425
2426
2427
2428
2429
2430
2431
2432
2441
2442
2451
2452
2461
2462
2463
2464
2468
2469
2472
2473
2477
2478
2479
2480
2484
2485
2486
2487
2491
2492
2495
2496
2500
2501
2502
2503
2507
2508
2517
2518
2527
2528
2529
2530
2533
2534
2538
2539
2540
2541
2550
2551
2560
2561
2570
2571
2580
2581
2582
2583
2584
2588
2589
2590
2607
2608
2612
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2630
2633
2634
2635
2636
2637
2638
2639
2640
2649
2650
2659
2660
2669
2670
2679
2680
2684
2685
2694
2695
2699
2700
2709
2710
2719
2720
2724
2725
2734
2735
2744
2745
2754
2755
2764
2765
2766
2772
2773
2779
2780
2786
2787
2793
2794
2800
2801
2807
2808
2814
2815
2821
2822
2828
2829
2835
2836
2842
2843
2849
2850
2856
2857
2863
2864
/* ... */
#include <errno.h>
#include "btc_ble_mesh_generic_model.h"
#include "mesh/config.h"
#include "access.h"
#include "transport.h"
#include "mesh/model_opcode.h"
#include "mesh/state_transition.h"
#include "mesh/device_property.h"8 includes
#if CONFIG_BLE_MESH_GENERIC_SERVER
static bt_mesh_mutex_t generic_server_lock;
void bt_mesh_generic_server_lock(void)
{
bt_mesh_mutex_lock(&generic_server_lock);
}{ ... }
void bt_mesh_generic_server_unlock(void)
{
bt_mesh_mutex_unlock(&generic_server_lock);
}{ ... }
static void send_gen_onoff_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
bool publish)
{
struct bt_mesh_gen_onoff_srv *srv = model->user_data;
struct net_buf_simple *msg = NULL;
uint8_t length = 2 + 3;
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, BLE_MESH_MODEL_OP_GEN_ONOFF_STATUS);
net_buf_simple_add_u8(msg, srv->state.onoff);
if (srv->transition.counter) {
bt_mesh_server_calc_remain_time(&srv->transition);
net_buf_simple_add_u8(msg, srv->state.target_onoff);
net_buf_simple_add_u8(msg, srv->transition.remain_time);
}{...}
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));
}{...}
}{ ... }
static void gen_onoff_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_onoff_srv *srv = model->user_data;
if (srv == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG,
model, ctx, NULL, 0);
return;
}{...}
send_gen_onoff_status(model, ctx, false);
}{ ... }
void gen_onoff_publish(struct bt_mesh_model *model)
{
if (model->user_data == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
send_gen_onoff_status(model, NULL, true);
}{ ... }
static void gen_onoff_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_onoff_srv *srv = model->user_data;
uint8_t tid = 0U, onoff = 0U, trans_time = 0U, delay = 0U;
bool optional = false;
int64_t now = 0;
if (srv == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
onoff = net_buf_simple_pull_u8(buf);
if (onoff > BLE_MESH_STATE_ON) {
BT_ERR("Invalid OnOff value 0x%02x", onoff);
return;
}{...}
tid = net_buf_simple_pull_u8(buf);
if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
return;
}{...}
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_gen_server_recv_set_msg_t set = {
.onoff_set.op_en = optional,
.onoff_set.onoff = onoff,
.onoff_set.tid = tid,
.onoff_set.trans_time = trans_time,
.onoff_set.delay = delay,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_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_GEN_ONOFF_SET) {
send_gen_onoff_status(model, ctx, false);
}{...}
send_gen_onoff_status(model, NULL, true);
return;
}{...}
bt_mesh_generic_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_onoff = onoff;
if (srv->state.target_onoff != srv->state.onoff) {
generic_onoff_tt_values(srv, trans_time, delay);
}{...} else {
bt_mesh_gen_server_state_change_t change = {
.gen_onoff_set.onoff = srv->state.onoff,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ONOFF_SET) {
send_gen_onoff_status(model, ctx, false);
}{...}
send_gen_onoff_status(model, NULL, true);
bt_mesh_generic_server_unlock();
return;
}{...}
if (srv->transition.timer.work.user_data) {
memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx));
}{...}
if (srv->transition.counter == 0U) {
srv->state.onoff = srv->state.target_onoff;
}{...}
srv->transition.just_started = true;
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ONOFF_SET) {
send_gen_onoff_status(model, ctx, false);
}{...}
send_gen_onoff_status(model, NULL, true);
bt_mesh_generic_server_unlock();
bt_mesh_server_start_transition(&srv->transition);
}{ ... }
static void send_gen_level_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
bool publish)
{
struct bt_mesh_gen_level_srv *srv = model->user_data;
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, BLE_MESH_MODEL_OP_GEN_LEVEL_STATUS);
net_buf_simple_add_le16(msg, srv->state.level);
if (srv->transition.counter) {
if (srv->state.move_start) {
if (srv->state.positive) {
net_buf_simple_add_le16(msg, INT16_MAX);
}{...} else {
net_buf_simple_add_le16(msg, INT16_MIN);
}{...}
net_buf_simple_add_u8(msg, BLE_MESH_UNKNOWN_REMAIN_TIME);
}{...} else {
bt_mesh_server_calc_remain_time(&srv->transition);
net_buf_simple_add_le16(msg, srv->state.target_level);
net_buf_simple_add_u8(msg, srv->transition.remain_time);
}{...}
}{...}
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));
}{...}
}{ ... }
static void gen_level_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_level_srv *srv = model->user_data;
if (srv == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG,
model, ctx, NULL, 0);
return;
}{...}
send_gen_level_status(model, ctx, false);
}{ ... }
void gen_level_publish(struct bt_mesh_model *model)
{
if (model->user_data == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
send_gen_level_status(model, NULL, true);
}{ ... }
static void gen_level_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_level_srv *srv = model->user_data;
uint8_t tid = 0U, trans_time = 0U, delay = 0U;
bool optional = false;
int16_t level = 0;
int64_t now = 0;
if (srv == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
level = (int16_t) 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;
}{...}
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_gen_server_recv_set_msg_t set = {
.level_set.op_en = optional,
.level_set.level = level,
.level_set.tid = tid,
.level_set.trans_time = trans_time,
.level_set.delay = delay,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_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_GEN_LEVEL_SET) {
send_gen_level_status(model, ctx, false);
}{...}
send_gen_level_status(model, NULL, true);
return;
}{...}
bt_mesh_generic_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_level = level;
/* ... */
if (srv->state.target_level != srv->state.level) {
generic_level_tt_values(srv, trans_time, delay);
}{...} else {
bt_mesh_gen_server_state_change_t change = {
.gen_level_set.level = srv->state.level,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_LEVEL_SET) {
send_gen_level_status(model, ctx, false);
}{...}
send_gen_level_status(model, NULL, true);
bt_mesh_generic_server_unlock();
return;
}{...}
if (srv->transition.timer.work.user_data) {
memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx));
}{...}
if (srv->transition.counter == 0U) {
srv->state.level = srv->state.target_level;
}{...}
srv->transition.just_started = true;
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_LEVEL_SET) {
send_gen_level_status(model, ctx, false);
}{...}
send_gen_level_status(model, NULL, true);
bt_mesh_generic_server_unlock();
bt_mesh_server_start_transition(&srv->transition);
}{ ... }
static void gen_delta_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_level_srv *srv = model->user_data;
uint8_t tid = 0U, trans_time = 0U, delay = 0U;
int32_t tmp32 = 0, delta = 0;
bool optional = false;
int64_t now = 0;
if (srv == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
delta = (int32_t)net_buf_simple_pull_le32(buf);
tid = net_buf_simple_pull_u8(buf);
if (bt_mesh_server_get_optional(model, ctx, buf, &trans_time, &delay, &optional)) {
return;
}{...}
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_gen_server_recv_set_msg_t set = {
.delta_set.op_en = optional,
.delta_set.delta_level = delta,
.delta_set.tid = tid,
.delta_set.trans_time = trans_time,
.delta_set.delay = delay,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_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 (srv->state.last_delta == delta) {
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_DELTA_SET) {
send_gen_level_status(model, ctx, false);
}{...}
send_gen_level_status(model, NULL, true);
return;
}{...}
tmp32 = srv->state.last_level + delta;
}{...} else {
srv->state.last_level = srv->state.level;
tmp32 = srv->state.level + delta;
}{...}
bt_mesh_generic_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.last_delta = delta;
if (tmp32 < INT16_MIN) {
tmp32 = INT16_MIN;
}{...} else if (tmp32 > INT16_MAX) {
tmp32 = INT16_MAX;
}{...}
srv->state.target_level = tmp32;
/* ... */
if (srv->state.target_level != srv->state.level) {
generic_level_tt_values(srv, trans_time, delay);
}{...} else {
bt_mesh_gen_server_state_change_t change = {
.gen_delta_set.level = srv->state.level,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_DELTA_SET) {
send_gen_level_status(model, ctx, false);
}{...}
send_gen_level_status(model, NULL, true);
bt_mesh_generic_server_unlock();
return;
}{...}
if (srv->transition.timer.work.user_data) {
memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx));
}{...}
if (srv->transition.counter == 0U) {
srv->state.level = srv->state.target_level;
}{...}
srv->transition.just_started = true;
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_DELTA_SET) {
send_gen_level_status(model, ctx, false);
}{...}
send_gen_level_status(model, NULL, true);
bt_mesh_generic_server_unlock();
bt_mesh_server_start_transition(&srv->transition);
}{ ... }
static void gen_move_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_level_srv *srv = model->user_data;
uint8_t tid = 0U, trans_time = 0U, delay = 0U;
bool optional = false;
int16_t delta = 0;
int32_t tmp32 = 0;
int64_t now = 0;
if (srv == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
delta = (int16_t) 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;
}{...}
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_gen_server_recv_set_msg_t set = {
.move_set.op_en = optional,
.move_set.delta_level = delta,
.move_set.tid = tid,
.move_set.trans_time = trans_time,
.move_set.delay = delay,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_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_GEN_MOVE_SET) {
send_gen_level_status(model, ctx, false);
}{...}
send_gen_level_status(model, NULL, true);
return;
}{...}
bt_mesh_generic_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.last_delta = delta;
tmp32 = srv->state.level + delta;
if (tmp32 < INT16_MIN) {
tmp32 = INT16_MIN;
}{...} else if (tmp32 > INT16_MAX) {
tmp32 = INT16_MAX;
}{...}
srv->state.target_level = tmp32;
/* ... */
if (srv->state.target_level != srv->state.level) {
generic_level_tt_values(srv, trans_time, delay);
}{...} else {
bt_mesh_gen_server_state_change_t change = {
.gen_move_set.level = srv->state.level,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MOVE_SET) {
send_gen_level_status(model, ctx, false);
}{...}
send_gen_level_status(model, NULL, true);
srv->state.move_start = false;
bt_mesh_generic_server_unlock();
return;
}{...}
if (srv->transition.timer.work.user_data) {
memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx));
}{...}
if (delta) {
srv->state.move_start = true;
srv->state.positive = (delta > 0) ? true : false;
}{...}
srv->transition.just_started = true;
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MOVE_SET) {
send_gen_level_status(model, ctx, false);
}{...}
send_gen_level_status(model, NULL, true);
bt_mesh_generic_server_unlock();
/* ... */
if (srv->transition.counter == 0U) {
srv->state.move_start = false;
bt_mesh_gen_server_state_change_t change = {
.gen_move_set.level = srv->state.level,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
return;
}{...}
bt_mesh_server_start_transition(&srv->transition);
}{ ... }
static void send_gen_def_trans_time_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
bool publish)
{
struct bt_mesh_gen_def_trans_time_srv *srv = model->user_data;
struct net_buf_simple *msg = NULL;
uint8_t length = 2 + 1;
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, BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_STATUS);
net_buf_simple_add_u8(msg, srv->state.trans_time);
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));
}{...}
}{ ... }
static void gen_def_trans_time_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_def_trans_time_srv *srv = model->user_data;
if (srv == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG,
model, ctx, NULL, 0);
return;
}{...}
send_gen_def_trans_time_status(model, ctx, false);
}{ ... }
static void gen_def_trans_time_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_def_trans_time_srv *srv = model->user_data;
uint8_t trans_time = 0U;
if (srv == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
trans_time = net_buf_simple_pull_u8(buf);
if ((trans_time & 0x3F) == 0x3F) {
BT_WARN("Invalid Transaction Number of Steps 0x3f");
return;
}{...}
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_gen_server_recv_set_msg_t set = {
.def_trans_time_set.trans_time = trans_time,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG,
model, ctx, (const uint8_t *)&set, sizeof(set));
return;
}{...}
if (srv->state.trans_time != trans_time) {
srv->state.trans_time = trans_time;
}{...}
bt_mesh_gen_server_state_change_t change = {
.gen_def_trans_time_set.trans_time = trans_time,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET) {
send_gen_def_trans_time_status(model, ctx, false);
}{...}
send_gen_def_trans_time_status(model, NULL, true);
}{ ... }
static void send_gen_onpowerup_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
bool publish)
{
struct net_buf_simple *msg = NULL;
uint8_t length = 2 + 1;
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, BLE_MESH_MODEL_OP_GEN_ONPOWERUP_STATUS);
switch (model->id) {
case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV: {
struct bt_mesh_gen_power_onoff_srv *srv = model->user_data;
net_buf_simple_add_u8(msg, srv->state->onpowerup);
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV: {
struct bt_mesh_gen_power_onoff_setup_srv *srv = model->user_data;
net_buf_simple_add_u8(msg, srv->state->onpowerup);
break;
}{...}
... default:
BT_ERR("Invalid Generic Power OnOff Server 0x%04x", model->id);
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));
}{...}
}{ ... }
static void gen_onpowerup_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_power_onoff_srv *srv = model->user_data;
if (srv == NULL || srv->state == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG,
model, ctx, NULL, 0);
return;
}{...}
send_gen_onpowerup_status(model, ctx, false);
}{ ... }
void gen_onpowerup_publish(struct bt_mesh_model *model)
{
if (model->user_data == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
switch (model->id) {
case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV: {
struct bt_mesh_gen_power_onoff_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Generic Power OnOff Server state");
return;
}{...}
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV: {
struct bt_mesh_gen_power_onoff_setup_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Generic Power OnOff Setup Server state");
return;
}{...}
break;
}{...}
... default:
BT_ERR("Invalid Generic Power OnOff Server 0x%04x", model->id);
return;...
}{...}
send_gen_onpowerup_status(model, NULL, true);
}{ ... }
static void gen_onpowerup_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_power_onoff_setup_srv *srv = model->user_data;
uint8_t onpowerup = 0U;
if (srv == NULL || srv->state == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
onpowerup = net_buf_simple_pull_u8(buf);
if (onpowerup > BLE_MESH_STATE_RESTORE) {
BT_WARN("Invalid OnPowerUp value 0x%02x", onpowerup);
return;
}{...}
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_gen_server_recv_set_msg_t set = {
.onpowerup_set.onpowerup = onpowerup,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG,
model, ctx, (const uint8_t *)&set, sizeof(set));
return;
}{...}
if (srv->state->onpowerup != onpowerup) {
srv->state->onpowerup = onpowerup;
}{...}
bt_mesh_gen_server_state_change_t change = {
.gen_onpowerup_set.onpowerup = onpowerup,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET) {
send_gen_onpowerup_status(model, ctx, false);
}{...}
send_gen_onpowerup_status(model, NULL, true);
}{ ... }
static void send_gen_power_level_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_GEN_POWER_LEVEL_STATUS:
case BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS: {
struct bt_mesh_gen_power_level_srv *srv = model->user_data;
if (opcode == BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS) {
net_buf_simple_add_le16(msg, srv->state->power_actual);
if (srv->transition.counter) {
bt_mesh_server_calc_remain_time(&srv->transition);
net_buf_simple_add_le16(msg, srv->state->target_power_actual);
net_buf_simple_add_u8(msg, srv->transition.remain_time);
}{...}
}{...} else if (opcode == BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS) {
net_buf_simple_add_le16(msg, srv->state->power_last);
}{...}
break;
}{...}
... case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS:
if (model->id == BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV) {
struct bt_mesh_gen_power_level_srv *srv = model->user_data;
net_buf_simple_add_le16(msg, srv->state->power_default);
}{...} else if (model->id == BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV) {
struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data;
net_buf_simple_add_le16(msg, srv->state->power_default);
}{...}
break;...
case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS:
if (model->id == BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV) {
struct bt_mesh_gen_power_level_srv *srv = model->user_data;
net_buf_simple_add_u8(msg, srv->state->status_code);
net_buf_simple_add_le16(msg, srv->state->power_range_min);
net_buf_simple_add_le16(msg, srv->state->power_range_max);
}{...} else if (model->id == BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV) {
struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data;
net_buf_simple_add_u8(msg, srv->state->status_code);
net_buf_simple_add_le16(msg, srv->state->power_range_min);
net_buf_simple_add_le16(msg, srv->state->power_range_max);
}{...}
break;...
default:
BT_WARN("Unknown Generic Power 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));
}{...}
}{ ... }
static void gen_power_level_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_power_level_srv *srv = model->user_data;
uint16_t opcode = 0U;
if (srv == NULL || srv->state == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG,
model, ctx, NULL, 0);
return;
}{...}
switch (ctx->recv_op) {
case BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET:
opcode = BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS;
break;...
case BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET:
opcode = BLE_MESH_MODEL_OP_GEN_POWER_LAST_STATUS;
break;...
case BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET:
opcode = BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS;
break;...
case BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET:
opcode = BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS;
break;...
default:
BT_WARN("Unknown Generic Power Get opcode 0x%04x", ctx->recv_op);
return;...
}{...}
send_gen_power_level_status(model, ctx, false, opcode);
}{ ... }
void gen_power_level_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_GEN_POWER_LEVEL_SRV: {
struct bt_mesh_gen_power_level_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Generic Power Level Server state");
return;
}{...}
break;
}{...}
... case ESP_BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV: {
struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Generic Power Level Setup Server state");
return;
}{...}
break;
}{...}
... default:
BT_ERR("Invalid Generic Power Level Server 0x%04x", model->id);
return;...
}{...}
send_gen_power_level_status(model, NULL, true, opcode);
}{ ... }
static void gen_power_level_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_power_level_srv *srv = model->user_data;
uint8_t tid = 0U, trans_time = 0U, delay = 0U;
bool optional = false;
uint16_t power = 0U;
int64_t now = 0;
if (srv == NULL || srv->state == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
power = 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;
}{...}
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_gen_server_recv_set_msg_t set = {
.power_level_set.op_en = optional,
.power_level_set.power = power,
.power_level_set.tid = tid,
.power_level_set.trans_time = trans_time,
.power_level_set.delay = delay,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_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_GEN_POWER_LEVEL_SET) {
send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS);
}{...}
send_gen_power_level_status(model, NULL, true, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS);
return;
}{...}
bt_mesh_generic_server_lock();
bt_mesh_server_stop_transition(&srv->transition);
bt_mesh_server_update_last_msg(&srv->last, tid, ctx->addr, ctx->recv_dst, &now);
if (power) {
if (srv->state->power_range_min && power < srv->state->power_range_min) {
power = srv->state->power_range_min;
}{...} else if (srv->state->power_range_max && power > srv->state->power_range_max) {
power = srv->state->power_range_max;
}{...}
}{...}
srv->state->target_power_actual = power;
/* ... */
if (srv->state->target_power_actual != srv->state->power_actual) {
generic_power_level_tt_values(srv, trans_time, delay);
}{...} else {
bt_mesh_gen_server_state_change_t change = {
.gen_power_level_set.power = srv->state->power_actual,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET) {
send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS);
}{...}
send_gen_power_level_status(model, NULL, true, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS);
bt_mesh_generic_server_unlock();
return;
}{...}
if (srv->transition.timer.work.user_data) {
memcpy(srv->transition.timer.work.user_data, ctx, sizeof(struct bt_mesh_msg_ctx));
}{...}
if (srv->transition.counter == 0U) {
srv->state->power_actual = srv->state->target_power_actual;
/* ... */
if (srv->state->power_actual) {
srv->state->power_last = srv->state->power_actual;
}{...}
}{...}
srv->transition.just_started = true;
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET) {
send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS);
}{...}
send_gen_power_level_status(model, NULL, true, BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_STATUS);
bt_mesh_generic_server_unlock();
bt_mesh_server_start_transition(&srv->transition);
}{ ... }
static void gen_power_default_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data;
uint16_t power = 0U;
if (srv == NULL || srv->state == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
power = net_buf_simple_pull_le16(buf);
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_gen_server_recv_set_msg_t set = {
.power_default_set.power = power,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG,
model, ctx, (const uint8_t *)&set, sizeof(set));
return;
}{...}
/* ... */
if (power == 0x0000) {
power = srv->state->power_last;
}{...}
if (srv->state->power_default != power) {
srv->state->power_default = power;
}{...}
bt_mesh_gen_server_state_change_t change = {
.gen_power_default_set.power = power,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET) {
send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS);
}{...}
send_gen_power_level_status(model, NULL, true, BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_STATUS);
}{ ... }
static void gen_power_range_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_power_level_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;
}{...}
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_gen_server_recv_set_msg_t set = {
.power_range_set.range_min = range_min,
.power_range_set.range_max = range_max,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG,
model, ctx, (const uint8_t *)&set, sizeof(set));
return;
}{...}
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;
}{...}
if (range_min && srv->state->power_range_min != range_min) {
srv->state->power_range_min = range_min;
}{...}
if (range_max && srv->state->power_range_max != range_max) {
srv->state->power_range_max = range_max;
}{...}
bt_mesh_gen_server_state_change_t change = {
.gen_power_range_set.range_min = srv->state->power_range_min,
.gen_power_range_set.range_max = srv->state->power_range_max,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET) {
send_gen_power_level_status(model, ctx, false, BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS);
}{...}
send_gen_power_level_status(model, NULL, true, BLE_MESH_MODEL_OP_GEN_POWER_RANGE_STATUS);
}{ ... }
static void gen_battery_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_battery_srv *srv = model->user_data;
NET_BUF_SIMPLE_DEFINE(msg, 2 + 8 + BLE_MESH_SERVER_TRANS_MIC_SIZE);
if (srv == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG,
model, ctx, NULL, 0);
return;
}{...}
bt_mesh_model_msg_init(&msg, BLE_MESH_MODEL_OP_GEN_BATTERY_STATUS);
net_buf_simple_add_le32(&msg, srv->state.time_to_discharge << 8 | srv->state.battery_level);
net_buf_simple_add_le32(&msg, srv->state.battery_flags << 24 | srv->state.time_to_charge);
BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, &msg, NULL, NULL));
}{ ... }
static void send_gen_location_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 = 1 + 10;
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_GEN_LOC_GLOBAL_STATUS:
if (model->id == BLE_MESH_MODEL_ID_GEN_LOCATION_SRV) {
struct bt_mesh_gen_location_srv *srv = model->user_data;
net_buf_simple_add_le32(msg, srv->state->global_latitude);
net_buf_simple_add_le32(msg, srv->state->global_longitude);
net_buf_simple_add_le16(msg, srv->state->global_altitude);
}{...} else if (model->id == BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV) {
struct bt_mesh_gen_location_setup_srv *srv = model->user_data;
net_buf_simple_add_le32(msg, srv->state->global_latitude);
net_buf_simple_add_le32(msg, srv->state->global_longitude);
net_buf_simple_add_le16(msg, srv->state->global_altitude);
}{...}
break;...
case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS:
if (model->id == BLE_MESH_MODEL_ID_GEN_LOCATION_SRV) {
struct bt_mesh_gen_location_srv *srv = model->user_data;
net_buf_simple_add_le16(msg, srv->state->local_north);
net_buf_simple_add_le16(msg, srv->state->local_east);
net_buf_simple_add_le16(msg, srv->state->local_altitude);
net_buf_simple_add_u8(msg, srv->state->floor_number);
net_buf_simple_add_le16(msg, srv->state->uncertainty);
}{...} else if (model->id == BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV) {
struct bt_mesh_gen_location_setup_srv *srv = model->user_data;
net_buf_simple_add_le16(msg, srv->state->local_north);
net_buf_simple_add_le16(msg, srv->state->local_east);
net_buf_simple_add_le16(msg, srv->state->local_altitude);
net_buf_simple_add_u8(msg, srv->state->floor_number);
net_buf_simple_add_le16(msg, srv->state->uncertainty);
}{...}
break;...
default:
BT_WARN("Unknown Generic Location 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));
}{...}
}{ ... }
static void gen_location_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_location_srv *srv = model->user_data;
uint16_t opcode = 0U;
if (srv == NULL || srv->state == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG,
model, ctx, NULL, 0);
return;
}{...}
switch (ctx->recv_op) {
case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET:
opcode = BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS;
break;...
case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET:
opcode = BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS;
break;...
default:
BT_WARN("Unknown Generic Location Get opcode 0x%04x", ctx->recv_op);
return;...
}{...}
send_gen_location_status(model, ctx, false, opcode);
}{ ... }
static void gen_location_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_location_setup_srv *srv = model->user_data;
uint16_t opcode = 0U;
if (srv == NULL || srv->state == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
switch (ctx->recv_op) {
case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET:
case BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK: {
opcode = BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_STATUS;
int32_t latitude = net_buf_simple_pull_le32(buf);
int32_t longitude = net_buf_simple_pull_le32(buf);
int16_t altitude = net_buf_simple_pull_le16(buf);
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_gen_server_recv_set_msg_t set = {
.loc_global_set.latitude = latitude,
.loc_global_set.longitude = longitude,
.loc_global_set.altitude = altitude,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG,
model, ctx, (const uint8_t *)&set, sizeof(set));
return;
}{...}
if (latitude != 0x80000000) {
srv->state->global_latitude = latitude;
}{...}
if (longitude != 0x80000000) {
srv->state->global_longitude = longitude;
}{...}
if (altitude != 0x7FFF) {
srv->state->global_altitude = altitude;
}{...}
bt_mesh_gen_server_state_change_t change = {
.gen_loc_global_set.latitude = srv->state->global_latitude,
.gen_loc_global_set.longitude = srv->state->global_longitude,
.gen_loc_global_set.altitude = srv->state->global_altitude,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
break;
}{...}
... case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET:
case BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK: {
opcode = BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_STATUS;
uint16_t north = net_buf_simple_pull_le16(buf);
uint16_t east = net_buf_simple_pull_le16(buf);
uint16_t altitude = net_buf_simple_pull_le16(buf);
uint8_t floor = net_buf_simple_pull_u8(buf);
uint16_t uncertainty = net_buf_simple_pull_le16(buf);
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_gen_server_recv_set_msg_t set = {
.loc_local_set.north = north,
.loc_local_set.east = east,
.loc_local_set.altitude = altitude,
.loc_local_set.floor_number = floor,
.loc_local_set.uncertainty = uncertainty,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG,
model, ctx, (const uint8_t *)&set, sizeof(set));
return;
}{...}
if (north != 0x8000) {
srv->state->local_north = north;
}{...}
if (east != 0x8000) {
srv->state->local_east = east;
}{...}
if (altitude != 0x7FFF) {
srv->state->local_altitude = altitude;
}{...}
if (floor != 0xFF) {
srv->state->floor_number = floor;
}{...}
srv->state->uncertainty = uncertainty;
bt_mesh_gen_server_state_change_t change = {
.gen_loc_local_set.north = srv->state->local_north,
.gen_loc_local_set.east = srv->state->local_east,
.gen_loc_local_set.altitude = srv->state->local_altitude,
.gen_loc_local_set.floor_number = srv->state->floor_number,
.gen_loc_local_set.uncertainty = srv->state->uncertainty,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
break;
}{...}
... default:
BT_WARN("Unknown Generic Location Set opcode 0x%04x", ctx->recv_op);
return;...
}{...}
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET ||
ctx->recv_op == BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET) {
send_gen_location_status(model, ctx, false, opcode);
}{...}
send_gen_location_status(model, NULL, true, opcode);
}{ ... }
static struct bt_mesh_generic_property *gen_get_user_property(struct bt_mesh_model *model,
uint16_t property_id)
{
struct bt_mesh_gen_user_prop_srv *srv = model->user_data;
int i;
for (i = 0; i < srv->property_count; i++) {
if (srv->properties[i].id == property_id) {
return &srv->properties[i];
}{...}
}{...}
return NULL;
}{ ... }
static void send_gen_user_prop_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
uint16_t property_id, bool publish)
{
struct bt_mesh_generic_property *property = NULL;
struct net_buf_simple *msg = NULL;
uint16_t length = 0U;
if (property_id == BLE_MESH_INVALID_DEVICE_PROPERTY_ID) {
BT_ERR("Invalid User Property ID 0x%04x", property_id);
return;
}{...}
property = gen_get_user_property(model, property_id);
if (property == NULL) {
BT_WARN("User property 0x%04x not exists", property_id);
length = sizeof(property_id);
}{...} else {
length = sizeof(property->id) + sizeof(property->user_access) + property->val->len;
}{...}
if (publish == false) {
msg = bt_mesh_alloc_buf(1 + 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, 1 + length);
if (msg == NULL) {
return;
}{...}
}{...}
bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_STATUS);
if (property == NULL) {
net_buf_simple_add_le16(msg, property_id);
}{...} else {
net_buf_simple_add_le16(msg, property->id);
net_buf_simple_add_u8(msg, property->user_access);
if ((ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET &&
property->user_access != USER_ACCESS_PROHIBIT &&
property->user_access != USER_ACCESS_WRITE) ||
((ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET ||
ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK) &&
property->user_access != USER_ACCESS_PROHIBIT &&
property->user_access != USER_ACCESS_READ)) {
net_buf_simple_add_mem(msg, property->val->data, property->val->len);
}{...}
}{...}
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));
}{...}
}{ ... }
static void gen_user_prop_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_user_prop_srv *srv = model->user_data;
if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
/* ... */
bt_mesh_gen_server_recv_get_msg_t get = {0};
const uint8_t *param = NULL;
size_t len = 0;
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET) {
get.user_property_get.id = net_buf_simple_pull_le16(buf);
param = (const uint8_t *)&get;
len = sizeof(get);
}{...}
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG,
model, ctx, param, len);
return;
}{...}
switch (ctx->recv_op) {
case BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET: {
struct net_buf_simple *msg = NULL;
uint8_t i;
msg = bt_mesh_alloc_buf(1 + srv->property_count * 2 + BLE_MESH_SERVER_TRANS_MIC_SIZE);
if (msg == NULL) {
BT_ERR("%s, Out of memory", __func__);
return;
}{...}
bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_STATUS);
for (i = 0U; i < srv->property_count; i++) {
if (srv->properties[i].admin_access != ADMIN_NOT_USER_PROP &&
srv->properties[i].manu_access != MANU_NOT_USER_PROP) {
net_buf_simple_add_le16(msg, srv->properties[i].id);
}{...}
}{...}
BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
bt_mesh_free_buf(msg);
return;
}{...}
... case BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET: {
uint16_t property_id = net_buf_simple_pull_le16(buf);
send_gen_user_prop_status(model, ctx, property_id, false);
return;
}{...}
... default:
BT_WARN("Unknown Generic User Property Get opcode 0x%04x", ctx->recv_op);
return;...
}{...}
}{ ... }
static void gen_user_prop_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_user_prop_srv *srv = model->user_data;
struct bt_mesh_generic_property *property = NULL;
uint16_t property_id = 0U;
uint8_t expect_len = 0U;
if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
property_id = net_buf_simple_pull_le16(buf);
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_gen_server_recv_set_msg_t set = {
.user_property_set.id = property_id,
.user_property_set.value = buf,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG,
model, ctx, (const uint8_t *)&set, sizeof(set));
return;
}{...}
property = gen_get_user_property(model, property_id);
if (property == NULL || property->user_access == USER_ACCESS_PROHIBIT ||
property->user_access == USER_ACCESS_READ) {
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET) {
send_gen_user_prop_status(model, ctx, property_id, false);
}{...}
send_gen_user_prop_status(model, ctx, property_id, true);
return;
}{...}
/* ... */
expect_len = bt_mesh_get_dev_prop_len(property_id);
if (buf->len != expect_len) {
BT_ERR("Invalid User Property 0x%04x length, expect %d, actual %d",
property_id, expect_len, buf->len);
return;
}{...}
net_buf_simple_reset(property->val);
net_buf_simple_add_mem(property->val, buf->data, MIN(buf->len, property->val->size));
bt_mesh_gen_server_state_change_t change = {
.gen_user_prop_set.id = property_id,
.gen_user_prop_set.value = property->val,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET) {
send_gen_user_prop_status(model, ctx, property_id, false);
}{...}
send_gen_user_prop_status(model, ctx, property_id, true);
}{ ... }
static struct bt_mesh_generic_property *gen_get_admin_property(struct bt_mesh_model *model,
uint16_t property_id)
{
struct bt_mesh_gen_admin_prop_srv *srv = model->user_data;
int i;
for (i = 0; i < srv->property_count; i++) {
if (srv->properties[i].id == property_id) {
return &srv->properties[i];
}{...}
}{...}
return NULL;
}{ ... }
static void send_gen_admin_prop_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
uint16_t property_id, bool publish)
{
struct bt_mesh_generic_property *property = NULL;
struct net_buf_simple *msg = NULL;
uint16_t length = 0U;
if (property_id == BLE_MESH_INVALID_DEVICE_PROPERTY_ID) {
BT_ERR("Invalid Admin Property ID 0x%04x", property_id);
return;
}{...}
property = gen_get_admin_property(model, property_id);
if (property == NULL) {
BT_WARN("Admin property 0x%04x not exists", property_id);
length = sizeof(property_id);
}{...} else {
length = sizeof(property->id) + sizeof(property->admin_access) + property->val->len;
}{...}
if (publish == false) {
msg = bt_mesh_alloc_buf(1 + 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, 1 + length);
if (msg == NULL) {
return;
}{...}
}{...}
bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_STATUS);
if (property == NULL) {
net_buf_simple_add_le16(msg, property_id);
}{...} else {
net_buf_simple_add_le16(msg, property->id);
net_buf_simple_add_u8(msg, property->admin_access);
if (ctx->recv_op != BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET ||
property->admin_access != ADMIN_ACCESS_WRITE) {
net_buf_simple_add_mem(msg, property->val->data, property->val->len);
}{...}
}{...}
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));
}{...}
}{ ... }
static void gen_admin_prop_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_admin_prop_srv *srv = model->user_data;
if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_gen_server_recv_get_msg_t get = {0};
const uint8_t *param = NULL;
size_t len = 0U;
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET) {
get.admin_property_get.id = net_buf_simple_pull_le16(buf);
param = (const uint8_t *)&get;
len = sizeof(get);
}{...}
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG,
model, ctx, param, len);
return;
}{...}
switch (ctx->recv_op) {
case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET: {
struct net_buf_simple *msg = NULL;
uint8_t i = 0U;
msg = bt_mesh_alloc_buf(1 + srv->property_count * 2 + BLE_MESH_SERVER_TRANS_MIC_SIZE);
if (msg == NULL) {
BT_ERR("%s, Out of memory", __func__);
return;
}{...}
bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_STATUS);
for (i = 0U; i < srv->property_count; i++) {
net_buf_simple_add_le16(msg, srv->properties[i].id);
}{...}
BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
bt_mesh_free_buf(msg);
return;
}{...}
... case BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET: {
uint16_t property_id = net_buf_simple_pull_le16(buf);
send_gen_admin_prop_status(model, ctx, property_id, false);
return;
}{...}
... default:
BT_WARN("Unknown Generic Admin Property Get opcode 0x%04x", ctx->recv_op);
return;...
}{...}
}{ ... }
static void gen_admin_prop_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_admin_prop_srv *srv = model->user_data;
struct bt_mesh_generic_property *property = NULL;
uint16_t property_id = 0U;
uint8_t access = 0U;
if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
property_id = net_buf_simple_pull_le16(buf);
access = net_buf_simple_pull_u8(buf);
if (access > ADMIN_ACCESS_READ_WRITE) {
BT_ERR("Invalid Admin Access 0x%02x", access);
return;
}{...}
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_gen_server_recv_set_msg_t set = {
.admin_property_set.id = property_id,
.admin_property_set.access = access,
.admin_property_set.value = buf,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG,
model, ctx, (const uint8_t *)&set, sizeof(set));
return;
}{...}
property = gen_get_admin_property(model, property_id);
if (property == NULL) {
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET) {
send_gen_admin_prop_status(model, ctx, property_id, false);
}{...}
send_gen_admin_prop_status(model, ctx, property_id, true);
return;
}{...}
property->admin_access = access;
net_buf_simple_reset(property->val);
net_buf_simple_add_mem(property->val, buf->data, MIN(buf->len, property->val->size));
bt_mesh_gen_server_state_change_t change = {
.gen_admin_prop_set.id = property_id,
.gen_admin_prop_set.access = property->admin_access,
.gen_admin_prop_set.value = property->val,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET) {
send_gen_admin_prop_status(model, ctx, property_id, false);
}{...}
send_gen_admin_prop_status(model, ctx, property_id, true);
}{ ... }
static struct bt_mesh_generic_property *gen_get_manu_property(struct bt_mesh_model *model,
uint16_t property_id)
{
struct bt_mesh_gen_manu_prop_srv *srv = model->user_data;
int i;
for (i = 0; i < srv->property_count; i++) {
if (srv->properties[i].id == property_id) {
return &srv->properties[i];
}{...}
}{...}
return NULL;
}{ ... }
static void send_gen_manu_prop_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
uint16_t property_id, bool publish)
{
struct bt_mesh_generic_property *property = NULL;
struct net_buf_simple *msg = NULL;
uint16_t length = 0U;
if (property_id == BLE_MESH_INVALID_DEVICE_PROPERTY_ID) {
BT_ERR("Invalid Manu Property ID 0x%04x", property_id);
return;
}{...}
property = gen_get_manu_property(model, property_id);
if (property == NULL) {
BT_WARN("Manu property 0x%04x not exists", property_id);
length = sizeof(property_id);
}{...} else {
length = sizeof(property->id) + sizeof(property->manu_access) + property->val->len;
}{...}
if (publish == false) {
msg = bt_mesh_alloc_buf(1 + 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, 1 + length);
if (msg == NULL) {
return;
}{...}
}{...}
bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_STATUS);
if (property == NULL) {
net_buf_simple_add_le16(msg, property_id);
}{...} else {
net_buf_simple_add_le16(msg, property->id);
net_buf_simple_add_u8(msg, property->manu_access);
net_buf_simple_add_mem(msg, property->val->data, property->val->len);
}{...}
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));
}{...}
}{ ... }
static void gen_manu_prop_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_manu_prop_srv *srv = model->user_data;
if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_gen_server_recv_get_msg_t get = {0};
const uint8_t *param = NULL;
size_t len = 0U;
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET) {
get.manu_property_get.id = net_buf_simple_pull_le16(buf);
param = (const uint8_t *)&get;
len = sizeof(get);
}{...}
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG,
model, ctx, param, len);
return;
}{...}
switch (ctx->recv_op) {
case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET: {
struct net_buf_simple *msg = NULL;
uint8_t i = 0U;
msg = bt_mesh_alloc_buf(1 + srv->property_count * 2 + BLE_MESH_SERVER_TRANS_MIC_SIZE);
if (msg == NULL) {
BT_ERR("%s, Out of memory", __func__);
return;
}{...}
bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_STATUS);
for (i = 0U; i < srv->property_count; i++) {
net_buf_simple_add_le16(msg, srv->properties[i].id);
}{...}
BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
bt_mesh_free_buf(msg);
return;
}{...}
... case BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET: {
uint16_t property_id = net_buf_simple_pull_le16(buf);
send_gen_manu_prop_status(model, ctx, property_id, false);
return;
}{...}
... default:
BT_WARN("Unknown Generic Manu Property Get opcode 0x%04x", ctx->recv_op);
return;...
}{...}
}{ ... }
static void gen_manu_prop_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_manu_prop_srv *srv = model->user_data;
struct bt_mesh_generic_property *property = NULL;
uint16_t property_id = 0U;
uint8_t access = 0U;
if (srv == NULL || srv->property_count == 0U || srv->properties == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
property_id = net_buf_simple_pull_le16(buf);
access = net_buf_simple_pull_u8(buf);
if (access > MANU_ACCESS_READ) {
BT_ERR("Invalid Manu Access 0x%02x", access);
return;
}{...}
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_gen_server_recv_set_msg_t set = {
.manu_property_set.id = property_id,
.manu_property_set.access = access,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_SET_MSG,
model, ctx, (const uint8_t *)&set, sizeof(set));
return;
}{...}
property = gen_get_manu_property(model, property_id);
if (property == NULL) {
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET) {
send_gen_manu_prop_status(model, ctx, property_id, false);
}{...}
send_gen_manu_prop_status(model, ctx, property_id, true);
return;
}{...}
property->manu_access = access;
bt_mesh_gen_server_state_change_t change = {
.gen_manu_prop_set.id = property_id,
.gen_manu_prop_set.access = property->manu_access,
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
if (ctx->recv_op == BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET) {
send_gen_manu_prop_status(model, ctx, property_id, false);
}{...}
send_gen_manu_prop_status(model, ctx, property_id, true);
}{ ... }
static int search_prop_id_index(const uint16_t *array, uint8_t array_idx, uint16_t id)
{
static const uint16_t *start = NULL;
uint8_t index = 0U;
uint16_t temp = 0U;
if (start == NULL) {
start = array;
}{...}
if (array_idx == 0U) {
if (*array >= id) {
return array - start;
}{...}
return -1;
}{...}
index = array_idx / 2;
temp = array[index];
if (temp == id) {
return array + index - start;
}{...}
if (temp > id) {
return search_prop_id_index(array, index, id);
}{...}
return search_prop_id_index(array + index + 1, array_idx - 1 - index, id);
}{ ... }
static void gen_client_prop_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_gen_client_prop_srv *srv = model->user_data;
struct net_buf_simple *sdu = NULL;
uint16_t total_len = 5U;
uint16_t property_id = 0U;
int i, index = 0;
if (srv == NULL || srv->id_count == 0U || srv->property_ids == NULL) {
BT_ERR("%s, Invalid model user data", __func__);
return;
}{...}
if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
bt_mesh_gen_server_recv_get_msg_t get = {
.client_properties_get.id = net_buf_simple_pull_le16(buf),
}{...};
bt_mesh_generic_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_GENERIC_SERVER_RECV_GET_MSG,
model, ctx, (const uint8_t *)&get, sizeof(get));
return;
}{...}
/* ... */
property_id = net_buf_simple_pull_le16(buf);
index = search_prop_id_index(srv->property_ids, srv->id_count - 1, property_id);
if (index < 0) {
NET_BUF_SIMPLE_DEFINE(msg, 1 + BLE_MESH_SERVER_TRANS_MIC_SIZE);
bt_mesh_model_msg_init(&msg, BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS);
BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, &msg, NULL, NULL));
return;
}{...}
sdu = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN));
if (sdu == NULL) {
BT_ERR("%s, Out of memory", __func__);
return;
}{...}
bt_mesh_model_msg_init(sdu, BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_STATUS);
for (i = index; i < srv->id_count; i++) {
total_len += sizeof(uint16_t);
if (total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) {
BT_WARN("Too large generic client properties status");
break;
}{...}
net_buf_simple_add_le16(sdu, srv->property_ids[i]);
}{...}
BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, sdu, NULL, NULL));
bt_mesh_free_buf(sdu);
}{ ... }
const struct bt_mesh_model_op bt_mesh_gen_onoff_srv_op[] = {
{ BLE_MESH_MODEL_OP_GEN_ONOFF_GET, 0, gen_onoff_get },
{ BLE_MESH_MODEL_OP_GEN_ONOFF_SET, 2, gen_onoff_set },
{ BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK, 2, gen_onoff_set },
BLE_MESH_MODEL_OP_END,
}{...};
const struct bt_mesh_model_op bt_mesh_gen_level_srv_op[] = {
{ BLE_MESH_MODEL_OP_GEN_LEVEL_GET, 0, gen_level_get },
{ BLE_MESH_MODEL_OP_GEN_LEVEL_SET, 3, gen_level_set },
{ BLE_MESH_MODEL_OP_GEN_LEVEL_SET_UNACK, 3, gen_level_set },
{ BLE_MESH_MODEL_OP_GEN_DELTA_SET, 5, gen_delta_set },
{ BLE_MESH_MODEL_OP_GEN_DELTA_SET_UNACK, 5, gen_delta_set },
{ BLE_MESH_MODEL_OP_GEN_MOVE_SET, 3, gen_move_set },
{ BLE_MESH_MODEL_OP_GEN_MOVE_SET_UNACK, 3, gen_move_set },
BLE_MESH_MODEL_OP_END,
}{...};
const struct bt_mesh_model_op bt_mesh_gen_def_trans_time_srv_op[] = {
{ BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_GET, 0, gen_def_trans_time_get },
{ BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET, 1, gen_def_trans_time_set },
{ BLE_MESH_MODEL_OP_GEN_DEF_TRANS_TIME_SET_UNACK, 1, gen_def_trans_time_set },
BLE_MESH_MODEL_OP_END,
}{...};
const struct bt_mesh_model_op bt_mesh_gen_power_onoff_srv_op[] = {
{ BLE_MESH_MODEL_OP_GEN_ONPOWERUP_GET, 0, gen_onpowerup_get },
BLE_MESH_MODEL_OP_END,
}{...};
const struct bt_mesh_model_op bt_mesh_gen_power_onoff_setup_srv_op[] = {
{ BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET, 1, gen_onpowerup_set },
{ BLE_MESH_MODEL_OP_GEN_ONPOWERUP_SET_UNACK, 1, gen_onpowerup_set },
BLE_MESH_MODEL_OP_END,
}{...};
const struct bt_mesh_model_op bt_mesh_gen_power_level_srv_op[] = {
{ BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_GET, 0, gen_power_level_get },
{ BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET, 3, gen_power_level_set },
{ BLE_MESH_MODEL_OP_GEN_POWER_LEVEL_SET_UNACK, 3, gen_power_level_set },
{ BLE_MESH_MODEL_OP_GEN_POWER_LAST_GET, 0, gen_power_level_get },
{ BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_GET, 0, gen_power_level_get },
{ BLE_MESH_MODEL_OP_GEN_POWER_RANGE_GET, 0, gen_power_level_get },
BLE_MESH_MODEL_OP_END,
}{...};
const struct bt_mesh_model_op bt_mesh_gen_power_level_setup_srv_op[] = {
{ BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET, 2, gen_power_default_set },
{ BLE_MESH_MODEL_OP_GEN_POWER_DEFAULT_SET_UNACK, 2, gen_power_default_set },
{ BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET, 4, gen_power_range_set },
{ BLE_MESH_MODEL_OP_GEN_POWER_RANGE_SET_UNACK, 4, gen_power_range_set },
BLE_MESH_MODEL_OP_END,
}{...};
const struct bt_mesh_model_op bt_mesh_gen_battery_srv_op[] = {
{ BLE_MESH_MODEL_OP_GEN_BATTERY_GET, 0, gen_battery_get },
BLE_MESH_MODEL_OP_END,
}{...};
const struct bt_mesh_model_op bt_mesh_gen_location_srv_op[] = {
{ BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_GET, 0, gen_location_get },
{ BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_GET, 0, gen_location_get },
BLE_MESH_MODEL_OP_END,
}{...};
const struct bt_mesh_model_op bt_mesh_gen_location_setup_srv_op[] = {
{ BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET, 10, gen_location_set },
{ BLE_MESH_MODEL_OP_GEN_LOC_GLOBAL_SET_UNACK, 10, gen_location_set },
{ BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET, 9, gen_location_set },
{ BLE_MESH_MODEL_OP_GEN_LOC_LOCAL_SET_UNACK, 9, gen_location_set },
BLE_MESH_MODEL_OP_END,
}{...};
const struct bt_mesh_model_op bt_mesh_gen_user_prop_srv_op[] = {
{ BLE_MESH_MODEL_OP_GEN_USER_PROPERTIES_GET, 0, gen_user_prop_get },
{ BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_GET, 2, gen_user_prop_get },
{ BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET, 3, gen_user_prop_set },
{ BLE_MESH_MODEL_OP_GEN_USER_PROPERTY_SET_UNACK, 3, gen_user_prop_set },
BLE_MESH_MODEL_OP_END,
}{...};
const struct bt_mesh_model_op bt_mesh_gen_admin_prop_srv_op[] = {
{ BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTIES_GET, 0, gen_admin_prop_get },
{ BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_GET, 2, gen_admin_prop_get },
{ BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET, 4, gen_admin_prop_set },
{ BLE_MESH_MODEL_OP_GEN_ADMIN_PROPERTY_SET_UNACK, 4, gen_admin_prop_set },
BLE_MESH_MODEL_OP_END,
}{...};
const struct bt_mesh_model_op bt_mesh_gen_manu_prop_srv_op[] = {
{ BLE_MESH_MODEL_OP_GEN_MANU_PROPERTIES_GET, 0, gen_manu_prop_get },
{ BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_GET, 2, gen_manu_prop_get },
{ BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET, 3, gen_manu_prop_set },
{ BLE_MESH_MODEL_OP_GEN_MANU_PROPERTY_SET_UNACK, 3, gen_manu_prop_set },
BLE_MESH_MODEL_OP_END,
}{...};
const struct bt_mesh_model_op bt_mesh_gen_client_prop_srv_op[] = {
{ BLE_MESH_MODEL_OP_GEN_CLIENT_PROPERTIES_GET, 2, gen_client_prop_get },
BLE_MESH_MODEL_OP_END,
}{...};
static inline int property_id_compare(const void *p1, const void *p2)
{
if (*(uint16_t *)p1 < * (uint16_t *)p2) {
return -1;
}{...}
if (*(uint16_t *)p1 > *(uint16_t *)p2) {
return 1;
}{...}
return 0;
}{ ... }
static int generic_server_init(struct bt_mesh_model *model)
{
if (model->user_data == NULL) {
BT_ERR("Invalid Generic Server user data, model id 0x%04x", model->id);
return -EINVAL;
}{...}
switch (model->id) {
case BLE_MESH_MODEL_ID_GEN_ONOFF_SRV: {
struct bt_mesh_gen_onoff_srv *srv = model->user_data;
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
k_delayed_work_init(&srv->transition.timer, generic_onoff_work_handler);
}{...}
srv->model = model;
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_LEVEL_SRV: {
struct bt_mesh_gen_level_srv *srv = model->user_data;
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
k_delayed_work_init(&srv->transition.timer, generic_level_work_handler);
}{...}
srv->model = model;
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV: {
struct bt_mesh_gen_def_trans_time_srv *srv = model->user_data;
srv->model = model;
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV: {
struct bt_mesh_gen_power_onoff_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Generic OnPowerUp State");
return -EINVAL;
}{...}
srv->model = model;
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV: {
struct bt_mesh_gen_power_onoff_setup_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Generic OnPowerUp State");
return -EINVAL;
}{...}
srv->model = model;
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV: {
struct bt_mesh_gen_power_level_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Generic Power Level State");
return -EINVAL;
}{...}
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
bt_mesh_server_alloc_ctx(&srv->transition.timer.work);
k_delayed_work_init(&srv->transition.timer, generic_power_level_work_handler);
}{...}
srv->model = model;
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV: {
struct bt_mesh_gen_power_level_setup_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Generic Power Level State");
return -EINVAL;
}{...}
srv->model = model;
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_BATTERY_SRV: {
struct bt_mesh_gen_battery_srv *srv = model->user_data;
srv->model = model;
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_LOCATION_SRV: {
struct bt_mesh_gen_location_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Generic Location State");
return -EINVAL;
}{...}
srv->model = model;
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV: {
struct bt_mesh_gen_location_setup_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Generic Location State");
return -EINVAL;
}{...}
srv->model = model;
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_USER_PROP_SRV: {
struct bt_mesh_gen_user_prop_srv *srv = model->user_data;
if (srv->property_count == 0U || srv->properties == NULL) {
BT_ERR("Invalid Generic User Property State");
return -EINVAL;
}{...}
srv->model = model;
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV: {
struct bt_mesh_gen_admin_prop_srv *srv = model->user_data;
if (srv->property_count == 0U || srv->properties == NULL) {
BT_ERR("Invalid Generic Admin Property State");
return -EINVAL;
}{...}
srv->model = model;
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV: {
struct bt_mesh_gen_manu_prop_srv *srv = model->user_data;
if (srv->property_count == 0U || srv->properties == NULL) {
BT_ERR("Invalid Generic Manufacturer Property State");
return -EINVAL;
}{...}
srv->model = model;
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV: {
struct bt_mesh_gen_client_prop_srv *srv = model->user_data;
if (srv->id_count == 0U || srv->property_ids == NULL) {
BT_ERR("Invalid Generic Client Property State");
return -EINVAL;
}{...}
qsort(srv->property_ids, srv->id_count, sizeof(uint16_t), property_id_compare);
srv->model = model;
break;
}{...}
... default:
BT_WARN("Unknown Generic Server, model id 0x%04x", model->id);
return -EINVAL;...
}{...}
bt_mesh_mutex_create(&generic_server_lock);
return 0;
}{ ... }
static int gen_onoff_srv_init(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic OnOff Server has no publication support");
return -EINVAL;
}{...}
return generic_server_init(model);
}{ ... }
static int gen_level_srv_init(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Level Server has no publication support");
return -EINVAL;
}{...}
return generic_server_init(model);
}{ ... }
static int gen_def_trans_time_srv_init(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Default Trans Time Server has no publication support");
return -EINVAL;
}{...}
return generic_server_init(model);
}{ ... }
static int gen_power_onoff_srv_init(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Power OnOff Server has no publication support");
return -EINVAL;
}{...}
/* ... */
struct bt_mesh_elem *element = bt_mesh_model_elem(model);
if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV) == NULL) {
BT_WARN("Generic Power OnOff Setup Server not present");
}{...}
return generic_server_init(model);
}{ ... }
static int gen_power_onoff_setup_srv_init(struct bt_mesh_model *model)
{
return generic_server_init(model);
}{ ... }
static int gen_power_level_srv_init(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Power Level Server has no publication support");
return -EINVAL;
}{...}
/* ... */
struct bt_mesh_elem *element = bt_mesh_model_elem(model);
if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV) == NULL) {
BT_WARN("Generic Power Level Setup Server not present");
}{...}
return generic_server_init(model);
}{ ... }
static int gen_power_level_setup_srv_init(struct bt_mesh_model *model)
{
return generic_server_init(model);
}{ ... }
static int gen_battery_srv_init(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Battery Server has no publication support");
return -EINVAL;
}{...}
return generic_server_init(model);
}{ ... }
static int gen_location_srv_init(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Location Server has no publication support");
return -EINVAL;
}{...}
return generic_server_init(model);
}{ ... }
static int gen_location_setup_srv_init(struct bt_mesh_model *model)
{
/* ... */
struct bt_mesh_elem *element = bt_mesh_model_elem(model);
if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV) == NULL) {
BT_WARN("Generic Location Setup Server not present");
}{...}
return generic_server_init(model);
}{ ... }
static int gen_user_prop_srv_init(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic User Property Server has no publication support");
return -EINVAL;
}{...}
return generic_server_init(model);
}{ ... }
static int gen_admin_prop_srv_init(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Admin Property Server has no publication support");
return -EINVAL;
}{...}
return generic_server_init(model);
}{ ... }
static int gen_manu_prop_srv_init(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Manufacturer Property Server has no publication support");
return -EINVAL;
}{...}
return generic_server_init(model);
}{ ... }
static int gen_client_prop_srv_init(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Client Property Server has no publication support");
return -EINVAL;
}{...}
return generic_server_init(model);
}{ ... }
#if CONFIG_BLE_MESH_DEINIT
static int generic_server_deinit(struct bt_mesh_model *model)
{
if (model->user_data == NULL) {
BT_ERR("Invalid Generic Server user data, model id 0x%04x", model->id);
return -EINVAL;
}{...}
switch (model->id) {
case BLE_MESH_MODEL_ID_GEN_ONOFF_SRV: {
struct bt_mesh_gen_onoff_srv *srv = model->user_data;
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
bt_mesh_server_free_ctx(&srv->transition.timer.work);
k_delayed_work_free(&srv->transition.timer);
}{...}
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_LEVEL_SRV: {
struct bt_mesh_gen_level_srv *srv = model->user_data;
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
bt_mesh_server_free_ctx(&srv->transition.timer.work);
k_delayed_work_free(&srv->transition.timer);
}{...}
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV: {
struct bt_mesh_gen_power_level_srv *srv = model->user_data;
if (srv->state == NULL) {
BT_ERR("Invalid Generic Power Level State");
return -EINVAL;
}{...}
if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_AUTO_RSP) {
bt_mesh_server_free_ctx(&srv->transition.timer.work);
k_delayed_work_free(&srv->transition.timer);
}{...}
break;
}{...}
... case BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV:
case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV:
case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV:
case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV:
case BLE_MESH_MODEL_ID_GEN_BATTERY_SRV:
case BLE_MESH_MODEL_ID_GEN_LOCATION_SRV:
case BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV:
case BLE_MESH_MODEL_ID_GEN_USER_PROP_SRV:
case BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV:
case BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV:
case BLE_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV:
break;...
default:
BT_WARN("Unknown Generic Server, model id 0x%04x", model->id);
return -EINVAL;...
}{...}
bt_mesh_mutex_free(&generic_server_lock);
return 0;
}{ ... }
static int gen_onoff_srv_deinit(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic OnOff Server has no publication support");
return -EINVAL;
}{...}
return generic_server_deinit(model);
}{ ... }
static int gen_level_srv_deinit(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Level Server has no publication support");
return -EINVAL;
}{...}
return generic_server_deinit(model);
}{ ... }
static int gen_def_trans_time_srv_deinit(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Default Trans Time Server has no publication support");
return -EINVAL;
}{...}
return generic_server_deinit(model);
}{ ... }
static int gen_power_onoff_srv_deinit(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Power OnOff Server has no publication support");
return -EINVAL;
}{...}
return generic_server_deinit(model);
}{ ... }
static int gen_power_onoff_setup_srv_deinit(struct bt_mesh_model *model)
{
return generic_server_deinit(model);
}{ ... }
static int gen_power_level_srv_deinit(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Power Level Server has no publication support");
return -EINVAL;
}{...}
return generic_server_deinit(model);
}{ ... }
static int gen_power_level_setup_srv_deinit(struct bt_mesh_model *model)
{
return generic_server_deinit(model);
}{ ... }
static int gen_battery_srv_deinit(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Battery Server has no publication support");
return -EINVAL;
}{...}
return generic_server_deinit(model);
}{ ... }
static int gen_location_srv_deinit(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Location Server has no publication support");
return -EINVAL;
}{...}
return generic_server_deinit(model);
}{ ... }
static int gen_location_setup_srv_deinit(struct bt_mesh_model *model)
{
return generic_server_deinit(model);
}{ ... }
static int gen_user_prop_srv_deinit(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic User Property Server has no publication support");
return -EINVAL;
}{...}
return generic_server_deinit(model);
}{ ... }
static int gen_admin_prop_srv_deinit(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Admin Property Server has no publication support");
return -EINVAL;
}{...}
return generic_server_deinit(model);
}{ ... }
static int gen_manu_prop_srv_deinit(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Manufacturer Property Server has no publication support");
return -EINVAL;
}{...}
return generic_server_deinit(model);
}{ ... }
static int gen_client_prop_srv_deinit(struct bt_mesh_model *model)
{
if (model->pub == NULL) {
BT_ERR("Generic Client Property Server has no publication support");
return -EINVAL;
}{...}
return generic_server_deinit(model);
}{ ... }
#endif/* ... */
const struct bt_mesh_model_cb bt_mesh_gen_onoff_srv_cb = {
.init = gen_onoff_srv_init,
#if CONFIG_BLE_MESH_DEINIT
.deinit = gen_onoff_srv_deinit,
#endif
}{...};
const struct bt_mesh_model_cb bt_mesh_gen_level_srv_cb = {
.init = gen_level_srv_init,
#if CONFIG_BLE_MESH_DEINIT
.deinit = gen_level_srv_deinit,
#endif
}{...};
const struct bt_mesh_model_cb bt_mesh_gen_def_trans_time_srv_cb = {
.init = gen_def_trans_time_srv_init,
#if CONFIG_BLE_MESH_DEINIT
.deinit = gen_def_trans_time_srv_deinit,
#endif
}{...};
const struct bt_mesh_model_cb bt_mesh_gen_power_onoff_srv_cb = {
.init = gen_power_onoff_srv_init,
#if CONFIG_BLE_MESH_DEINIT
.deinit = gen_power_onoff_srv_deinit,
#endif
}{...};
const struct bt_mesh_model_cb bt_mesh_gen_power_onoff_setup_srv_cb = {
.init = gen_power_onoff_setup_srv_init,
#if CONFIG_BLE_MESH_DEINIT
.deinit = gen_power_onoff_setup_srv_deinit,
#endif
}{...};
const struct bt_mesh_model_cb bt_mesh_gen_power_level_srv_cb = {
.init = gen_power_level_srv_init,
#if CONFIG_BLE_MESH_DEINIT
.deinit = gen_power_level_srv_deinit,
#endif
}{...};
const struct bt_mesh_model_cb bt_mesh_gen_power_level_setup_srv_cb = {
.init = gen_power_level_setup_srv_init,
#if CONFIG_BLE_MESH_DEINIT
.deinit = gen_power_level_setup_srv_deinit,
#endif
}{...};
const struct bt_mesh_model_cb bt_mesh_gen_battery_srv_cb = {
.init = gen_battery_srv_init,
#if CONFIG_BLE_MESH_DEINIT
.deinit = gen_battery_srv_deinit,
#endif
}{...};
const struct bt_mesh_model_cb bt_mesh_gen_location_srv_cb = {
.init = gen_location_srv_init,
#if CONFIG_BLE_MESH_DEINIT
.deinit = gen_location_srv_deinit,
#endif
}{...};
const struct bt_mesh_model_cb bt_mesh_gen_location_setup_srv_cb = {
.init = gen_location_setup_srv_init,
#if CONFIG_BLE_MESH_DEINIT
.deinit = gen_location_setup_srv_deinit,
#endif
}{...};
const struct bt_mesh_model_cb bt_mesh_gen_user_prop_srv_cb = {
.init = gen_user_prop_srv_init,
#if CONFIG_BLE_MESH_DEINIT
.deinit = gen_user_prop_srv_deinit,
#endif
}{...};
const struct bt_mesh_model_cb bt_mesh_gen_admin_prop_srv_cb = {
.init = gen_admin_prop_srv_init,
#if CONFIG_BLE_MESH_DEINIT
.deinit = gen_admin_prop_srv_deinit,
#endif
}{...};
const struct bt_mesh_model_cb bt_mesh_gen_manu_prop_srv_cb = {
.init = gen_manu_prop_srv_init,
#if CONFIG_BLE_MESH_DEINIT
.deinit = gen_manu_prop_srv_deinit,
#endif
}{...};
const struct bt_mesh_model_cb bt_mesh_gen_client_prop_srv_cb = {
.init = gen_client_prop_srv_init,
#if CONFIG_BLE_MESH_DEINIT
.deinit = gen_client_prop_srv_deinit,
#endif
}{...};
/* ... */
#endif