forked from bluez/bluez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.c
2562 lines (1970 loc) · 60.1 KB
/
node.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
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2017-2020 Intel Corporation. All rights reserved.
*
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <dirent.h>
#include <limits.h>
#include <stdio.h>
#include <sys/time.h>
#include <ell/ell.h>
#include "mesh/mesh-defs.h"
#include "mesh/mesh.h"
#include "mesh/net.h"
#include "mesh/net-keys.h"
#include "mesh/appkey.h"
#include "mesh/mesh-config.h"
#include "mesh/provision.h"
#include "mesh/prov.h"
#include "mesh/keyring.h"
#include "mesh/model.h"
#include "mesh/cfgmod.h"
#include "mesh/remprv.h"
#include "mesh/prv-beacon.h"
#include "mesh/util.h"
#include "mesh/error.h"
#include "mesh/dbus.h"
#include "mesh/agent.h"
#include "mesh/manager.h"
#include "mesh/rpl.h"
#include "mesh/node.h"
#define MESH_NODE_PATH_PREFIX "/node"
/* Default values for a new locally created node */
#define DEFAULT_NEW_UNICAST 0x0001
#define DEFAULT_IV_INDEX 0x0000
/* Default element location: unknown */
#define DEFAULT_LOCATION 0x0000
enum request_type {
REQUEST_TYPE_JOIN,
REQUEST_TYPE_ATTACH,
REQUEST_TYPE_CREATE,
REQUEST_TYPE_IMPORT,
};
struct node_element {
char *path;
struct l_queue *models;
uint16_t location;
uint8_t idx;
};
struct node_composition {
uint16_t cid;
uint16_t pid;
uint16_t vid;
uint16_t crpl;
};
struct mesh_node {
struct mesh_net *net;
struct l_queue *elements;
struct l_queue *pages;
char *app_path;
char *owner;
char *obj_path;
struct mesh_agent *agent;
struct mesh_config *cfg;
char *storage_dir;
uint32_t disc_watch;
uint32_t seq_number;
bool busy;
bool provisioner;
uint16_t primary;
struct node_composition comp;
struct {
uint16_t interval;
uint8_t cnt;
uint8_t mode;
} relay;
uint8_t uuid[16];
uint8_t dev_key[16];
uint8_t token[8];
uint8_t num_ele;
uint8_t ttl;
uint8_t lpn;
uint8_t proxy;
uint8_t friend;
uint8_t beacon;
uint8_t mpb;
uint8_t mpb_period;
};
struct node_import {
uint8_t dev_key[16];
uint8_t net_key[16];
uint16_t net_idx;
struct {
bool ivu;
bool kr;
} flags;
uint32_t iv_index;
uint16_t unicast;
};
struct managed_obj_request {
struct mesh_node *node;
union {
node_ready_func_t ready_cb;
node_join_ready_func_t join_ready_cb;
};
struct l_dbus_message *pending_msg;
enum request_type type;
union {
struct mesh_node *attach;
struct node_import *import;
};
};
struct send_options {
bool segmented;
uint16_t vendor_id;
};
static struct l_queue *nodes;
static bool match_device_uuid(const void *a, const void *b)
{
const struct mesh_node *node = a;
const uint8_t *uuid = b;
return (memcmp(node->uuid, uuid, 16) == 0);
}
static bool match_token(const void *a, const void *b)
{
const struct mesh_node *node = a;
const uint64_t *token = b;
const uint64_t tmp = l_get_be64(node->token);
return *token == tmp;
}
static bool match_element_idx(const void *a, const void *b)
{
const struct node_element *element = a;
uint32_t index = L_PTR_TO_UINT(b);
return (element->idx == index);
}
static int compare_element_idx(const void *a, const void *b, void *user_data)
{
uint32_t a_idx = ((const struct node_element *)a)->idx;
uint32_t b_idx = ((const struct node_element *)b)->idx;
if (a_idx < b_idx)
return -1;
if (a_idx > b_idx)
return 1;
return 0;
}
static bool match_element_path(const void *a, const void *b)
{
const struct node_element *element = a;
const char *path = b;
if (!element->path)
return false;
return (!strcmp(element->path, path));
}
struct mesh_node *node_find_by_uuid(uint8_t uuid[16])
{
return l_queue_find(nodes, match_device_uuid, uuid);
}
struct mesh_node *node_find_by_token(uint64_t token)
{
return l_queue_find(nodes, match_token, (void *) &token);
}
uint8_t *node_uuid_get(struct mesh_node *node)
{
if (!node)
return NULL;
return node->uuid;
}
static void set_defaults(struct mesh_node *node)
{
node->lpn = MESH_MODE_UNSUPPORTED;
node->proxy = MESH_MODE_UNSUPPORTED;
node->mpb = MESH_MODE_DISABLED;
node->mpb_period = NET_MPB_REFRESH_DEFAULT;
node->friend = (mesh_friendship_supported()) ? MESH_MODE_DISABLED :
MESH_MODE_UNSUPPORTED;
node->beacon = (mesh_beacon_enabled()) ? MESH_MODE_ENABLED :
MESH_MODE_DISABLED;
node->relay.mode = (mesh_relay_supported()) ? MESH_MODE_DISABLED :
MESH_MODE_UNSUPPORTED;
node->ttl = TTL_MASK;
node->seq_number = DEFAULT_SEQUENCE_NUMBER;
}
static struct mesh_node *node_new(const uint8_t uuid[16])
{
struct mesh_node *node;
node = l_new(struct mesh_node, 1);
node->net = mesh_net_new(node);
node->elements = l_queue_new();
node->pages = l_queue_new();
memcpy(node->uuid, uuid, sizeof(node->uuid));
set_defaults(node);
return node;
}
static void free_element_path(void *a, void *b)
{
struct node_element *element = a;
l_free(element->path);
element->path = NULL;
}
static void element_free(void *data)
{
struct node_element *element = data;
l_queue_destroy(element->models, mesh_model_free);
l_free(element->path);
l_free(element);
}
static void free_node_dbus_resources(struct mesh_node *node)
{
if (!node)
return;
if (node->disc_watch) {
l_dbus_remove_watch(dbus_get_bus(), node->disc_watch);
node->disc_watch = 0;
}
l_queue_foreach(node->elements, free_element_path, NULL);
l_free(node->owner);
node->owner = NULL;
l_free(node->app_path);
node->app_path = NULL;
if (node->obj_path) {
l_dbus_object_remove_interface(dbus_get_bus(), node->obj_path,
MESH_NODE_INTERFACE);
l_dbus_object_remove_interface(dbus_get_bus(), node->obj_path,
MESH_MANAGEMENT_INTERFACE);
l_dbus_object_remove_interface(dbus_get_bus(), node->obj_path,
L_DBUS_INTERFACE_PROPERTIES);
l_free(node->obj_path);
node->obj_path = NULL;
}
}
static void free_node_resources(void *data)
{
struct mesh_node *node = data;
/* Unregister io callbacks */
mesh_net_detach(node->net);
/* In case of a provisioner, stop active scanning */
if (node->provisioner)
manager_scan_cancel(node);
/* Free dynamic resources */
free_node_dbus_resources(node);
l_queue_destroy(node->elements, element_free);
l_queue_destroy(node->pages, l_free);
mesh_agent_remove(node->agent);
mesh_config_release(node->cfg);
mesh_net_free(node->net);
l_free(node->storage_dir);
l_free(node);
}
/*
* This function is called to free resources and remove the
* configuration files for the specified node.
*/
void node_remove(struct mesh_node *node)
{
if (!node)
return;
l_queue_remove(nodes, node);
mesh_config_destroy_nvm(node->cfg);
free_node_resources(node);
}
static bool add_element_from_storage(struct mesh_node *node,
struct mesh_config_element *db_ele)
{
struct node_element *ele;
ele = l_new(struct node_element, 1);
if (!ele)
return false;
ele->idx = db_ele->index;
ele->location = db_ele->location;
ele->models = l_queue_new();
l_queue_push_tail(node->elements, ele);
if (!mesh_model_add_from_storage(node, ele->idx, ele->models,
db_ele->models))
return false;
return true;
}
static bool add_elements_from_storage(struct mesh_node *node,
struct mesh_config_node *db_node)
{
const struct l_queue_entry *entry;
struct node_element *ele;
entry = l_queue_get_entries(db_node->elements);
for (; entry; entry = entry->next)
if (!add_element_from_storage(node, entry->data))
return false;
ele = l_queue_find(node->elements, match_element_idx,
L_UINT_TO_PTR(PRIMARY_ELE_IDX));
if (!ele)
return false;
/* Add configuration server model on the primary element */
mesh_model_add(node, ele->models, CONFIG_SRV_MODEL, NULL);
/* Add remote provisioning models on the primary element */
mesh_model_add(node, ele->models, REM_PROV_SRV_MODEL, NULL);
if (node->provisioner)
mesh_model_add(node, ele->models, REM_PROV_CLI_MODEL, NULL);
return true;
}
static void set_net_key(void *a, void *b)
{
struct mesh_config_netkey *netkey = a;
struct mesh_node *node = b;
mesh_net_set_key(node->net, netkey->idx, netkey->key, netkey->new_key,
netkey->phase);
}
static void set_appkey(void *a, void *b)
{
struct mesh_config_appkey *appkey = a;
struct mesh_node *node = b;
appkey_key_init(node->net, appkey->net_idx, appkey->app_idx,
appkey->key, appkey->new_key);
}
static bool init_storage_dir(struct mesh_node *node)
{
char uuid[33];
char dir_name[PATH_MAX];
if (node->storage_dir)
return true;
if (!hex2str(node->uuid, 16, uuid, sizeof(uuid)))
return false;
snprintf(dir_name, PATH_MAX, "%s/%s", mesh_get_storage_dir(), uuid);
if (strlen(dir_name) >= PATH_MAX)
return false;
create_dir(dir_name);
node->storage_dir = l_strdup(dir_name);
/* Initialize directory for storing RPL info */
return rpl_init(node->storage_dir);
}
static void init_net_settings(struct mesh_node *node)
{
struct mesh_net *net = node->net;
mesh_net_set_proxy_mode(net, node->proxy == MESH_MODE_ENABLED);
mesh_net_set_friend_mode(net, node->friend == MESH_MODE_ENABLED);
mesh_net_set_relay_mode(net, node->relay.mode == MESH_MODE_ENABLED,
node->relay.cnt, node->relay.interval);
mesh_net_set_snb_mode(net, node->beacon == MESH_MODE_ENABLED);
mesh_net_set_mpb_mode(net, node->mpb == MESH_MODE_ENABLED,
node->mpb_period, true);
}
static bool init_from_storage(struct mesh_config_node *db_node,
const uint8_t uuid[16], struct mesh_config *cfg,
void *user_data)
{
unsigned int num_ele;
struct mesh_node *node = node_new(uuid);
if (!nodes)
nodes = l_queue_new();
l_queue_push_tail(nodes, node);
node->comp.cid = db_node->cid;
node->comp.pid = db_node->pid;
node->comp.vid = db_node->vid;
node->comp.crpl = db_node->crpl;
node->lpn = db_node->modes.lpn;
node->proxy = db_node->modes.proxy;
node->friend = db_node->modes.friend;
node->relay.mode = db_node->modes.relay.state;
node->relay.cnt = db_node->modes.relay.cnt;
node->relay.interval = db_node->modes.relay.interval;
node->beacon = db_node->modes.beacon;
node->mpb = db_node->modes.mpb;
node->mpb_period = db_node->modes.mpb_period;
l_debug("relay %2.2x, proxy %2.2x, lpn %2.2x, friend %2.2x",
node->relay.mode, node->proxy, node->lpn, node->friend);
node->ttl = db_node->ttl;
node->seq_number = db_node->seq_number;
memcpy(node->dev_key, db_node->dev_key, 16);
memcpy(node->token, db_node->token, 8);
num_ele = l_queue_length(db_node->elements);
if (num_ele > MAX_ELE_COUNT)
goto fail;
node->num_ele = num_ele;
if (num_ele != 0 && !add_elements_from_storage(node, db_node))
goto fail;
node->primary = db_node->unicast;
if (!db_node->netkeys)
goto fail;
if (!IS_UNASSIGNED(node->primary) &&
!mesh_net_register_unicast(node->net, node->primary, num_ele))
goto fail;
mesh_net_set_iv_index(node->net, db_node->iv_index, db_node->iv_update);
/* Initialize directory for storing keyring and RPL info */
if (!init_storage_dir(node) || !mesh_net_load_rpl(node->net))
goto fail;
if (db_node->net_transmit)
mesh_net_transmit_params_set(node->net,
db_node->net_transmit->count,
db_node->net_transmit->interval);
l_queue_foreach(db_node->netkeys, set_net_key, node);
l_queue_foreach(db_node->appkeys, set_appkey, node);
while (l_queue_length(db_node->pages)) {
struct mesh_config_comp_page *page;
/* Move the composition pages to the node struct */
page = l_queue_pop_head(db_node->pages);
l_queue_push_tail(node->pages, page);
}
mesh_net_set_seq_num(node->net, node->seq_number);
mesh_net_set_default_ttl(node->net, node->ttl);
init_net_settings(node);
/* Initialize configuration server model */
cfgmod_server_init(node, PRIMARY_ELE_IDX);
/* Initialize remote provisioning models */
remote_prov_server_init(node, PRIMARY_ELE_IDX);
remote_prov_client_init(node, PRIMARY_ELE_IDX);
/* Initialize Private Beacon server model */
prv_beacon_server_init(node, PRIMARY_ELE_IDX);
node->cfg = cfg;
return true;
fail:
node_remove(node);
return false;
}
static void cleanup_node(void *data)
{
struct mesh_node *node = data;
uint32_t seq_num = mesh_net_get_seq_num(node->net);
/* Preserve the last used sequence number */
mesh_config_write_seq_number(node->cfg, seq_num, false);
free_node_resources(node);
}
/*
* This function is called to free resources and write the current
* sequence numbers to the configuration file for each known node.
*/
void node_cleanup_all(void)
{
l_queue_destroy(nodes, cleanup_node);
l_dbus_unregister_interface(dbus_get_bus(), MESH_NODE_INTERFACE);
l_dbus_unregister_interface(dbus_get_bus(), MESH_MANAGEMENT_INTERFACE);
}
bool node_is_provisioner(struct mesh_node *node)
{
return node->provisioner;
}
bool node_is_busy(struct mesh_node *node)
{
return node->busy;
}
void node_app_key_delete(struct mesh_node *node, uint16_t net_idx,
uint16_t app_idx)
{
const struct l_queue_entry *entry;
entry = l_queue_get_entries(node->elements);
for (; entry; entry = entry->next) {
struct node_element *ele = entry->data;
mesh_model_app_key_delete(node, ele->idx, ele->models, app_idx);
}
}
uint16_t node_get_primary(struct mesh_node *node)
{
if (!node)
return UNASSIGNED_ADDRESS;
else
return node->primary;
}
bool node_refresh(struct mesh_node *node, bool hard, void *prov_info)
{
struct mesh_prov_node_info *info = prov_info;
bool res = true;
if (!node || !info)
return false;
if (!IS_UNICAST(info->unicast))
return false;
/* Changing Unicast addresses requires a hard node reset */
if (!hard && info->unicast != node->primary)
return false;
/*
* Hard refresh results in immediate use of new Device Key.
* Soft refresh saves new device key as Candidate until we
* successfully receive new incoming message on that key.
*/
if (hard) {
if (!mesh_config_write_device_key(node->cfg, info->device_key))
return false;
memcpy(node->dev_key, info->device_key, sizeof(node->dev_key));
} else if (!mesh_config_write_candidate(node->cfg, info->device_key))
return false;
/* Replace Primary Unicast address if it has changed */
if (node->primary != info->unicast) {
res = mesh_config_write_unicast(node->cfg, info->unicast);
if (res) {
node->primary = info->unicast;
node->num_ele = info->num_ele;
mesh_net_register_unicast(node->net, node->primary,
node->num_ele);
}
}
/* Replace Page 0 with Page 128 if it exists */
if (res) {
if (node_replace_comp(node, 0, 128))
return true;
}
return res;
}
const uint8_t *node_get_device_key(struct mesh_node *node)
{
if (!node)
return NULL;
return node->dev_key;
}
bool node_get_device_key_candidate(struct mesh_node *node, uint8_t *key)
{
if (!node)
return false;
return mesh_config_read_candidate(node->cfg, key);
}
void node_finalize_candidate(struct mesh_node *node)
{
if (!node)
return;
if (mesh_config_read_candidate(node->cfg, node->dev_key))
mesh_config_finalize_candidate(node->cfg);
}
void node_set_token(struct mesh_node *node, uint8_t token[8])
{
memcpy(node->token, token, 8);
}
const uint8_t *node_get_token(struct mesh_node *node)
{
if (!node)
return NULL;
else
return node->token;
}
uint8_t node_get_num_elements(struct mesh_node *node)
{
return node->num_ele;
}
struct l_queue *node_get_element_models(struct mesh_node *node, uint8_t ele_idx)
{
struct node_element *ele;
if (!node)
return NULL;
ele = l_queue_find(node->elements, match_element_idx,
L_UINT_TO_PTR(ele_idx));
if (!ele)
return NULL;
return ele->models;
}
uint8_t node_default_ttl_get(struct mesh_node *node)
{
if (!node)
return TTL_MASK;
return node->ttl;
}
bool node_default_ttl_set(struct mesh_node *node, uint8_t ttl)
{
bool res;
if (!node)
return false;
res = mesh_config_write_ttl(node->cfg, ttl);
if (res) {
node->ttl = ttl;
mesh_net_set_default_ttl(node->net, ttl);
}
return res;
}
bool node_set_sequence_number(struct mesh_node *node, uint32_t seq)
{
if (!node)
return false;
node->seq_number = seq;
return mesh_config_write_seq_number(node->cfg, node->seq_number, true);
}
uint32_t node_get_sequence_number(struct mesh_node *node)
{
if (!node)
return 0xffffffff;
return node->seq_number;
}
int node_get_element_idx(struct mesh_node *node, uint16_t ele_addr)
{
uint16_t addr;
uint8_t num_ele;
if (!node)
return -1;
num_ele = node_get_num_elements(node);
if (!num_ele)
return -2;
addr = node_get_primary(node);
if (ele_addr < addr || ele_addr >= addr + num_ele)
return -3;
else
return ele_addr - addr;
}
uint16_t node_get_crpl(struct mesh_node *node)
{
if (!node)
return 0;
return node->comp.crpl;
}
uint8_t node_relay_mode_get(struct mesh_node *node, uint8_t *count,
uint16_t *interval)
{
if (!node) {
*count = 0;
*interval = 0;
return MESH_MODE_DISABLED;
}
*count = node->relay.cnt;
*interval = node->relay.interval;
return node->relay.mode;
}
uint8_t node_lpn_mode_get(struct mesh_node *node)
{
if (!node)
return MESH_MODE_DISABLED;
return node->lpn;
}
bool node_relay_mode_set(struct mesh_node *node, bool enable, uint8_t cnt,
uint16_t interval)
{
bool res;
if (!node || node->relay.mode == MESH_MODE_UNSUPPORTED)
return false;
res = mesh_config_write_relay_mode(node->cfg, enable, cnt, interval);
if (res) {
node->relay.mode = enable ? MESH_MODE_ENABLED :
MESH_MODE_DISABLED;
node->relay.cnt = cnt;
node->relay.interval = interval;
mesh_net_set_relay_mode(node->net, enable, cnt, interval);
}
return res;
}
bool node_proxy_mode_set(struct mesh_node *node, bool enable)
{
bool res;
uint8_t proxy;
if (!node || node->proxy == MESH_MODE_UNSUPPORTED)
return false;
proxy = enable ? MESH_MODE_ENABLED : MESH_MODE_DISABLED;
res = mesh_config_write_mode(node->cfg, "proxy", proxy);
if (res) {
node->proxy = proxy;
mesh_net_set_proxy_mode(node->net, enable);
}
return res;
}
uint8_t node_proxy_mode_get(struct mesh_node *node)
{
if (!node)
return MESH_MODE_DISABLED;
return node->proxy;
}
bool node_beacon_mode_set(struct mesh_node *node, bool enable)
{
bool res;
uint8_t beacon;
if (!node)
return false;
beacon = enable ? MESH_MODE_ENABLED : MESH_MODE_DISABLED;
res = mesh_config_write_mode(node->cfg, "beacon", beacon);
if (res) {
node->beacon = beacon;
mesh_net_set_snb_mode(node->net, enable);
}
return res;
}
uint8_t node_beacon_mode_get(struct mesh_node *node)
{
if (!node)
return MESH_MODE_DISABLED;
return node->beacon;
}
bool node_mpb_mode_set(struct mesh_node *node, bool enable, uint8_t period)
{
bool res;
uint8_t beacon;
if (!node)
return false;
beacon = enable ? MESH_MODE_ENABLED : MESH_MODE_DISABLED;
res = mesh_config_write_mpb(node->cfg, beacon, period);
if (res) {
node->mpb = beacon;
node->mpb_period = period;
mesh_net_set_mpb_mode(node->net, enable, period, false);
}
return res;
}
uint8_t node_mpb_mode_get(struct mesh_node *node, uint8_t *period)
{
if (!node)
return MESH_MODE_DISABLED;
*period = node->mpb_period;
return node->mpb;
}
bool node_friend_mode_set(struct mesh_node *node, bool enable)
{
bool res;
uint8_t friend;
if (!node || node->friend == MESH_MODE_UNSUPPORTED)
return false;
friend = enable ? MESH_MODE_ENABLED : MESH_MODE_DISABLED;
res = mesh_config_write_mode(node->cfg, "friend", friend);
if (res) {
node->friend = friend;
mesh_net_set_friend_mode(node->net, enable);
}
return res;
}
uint8_t node_friend_mode_get(struct mesh_node *node)
{
if (!node)
return MESH_MODE_DISABLED;
return node->friend;
}
static uint16_t node_generate_comp(struct mesh_node *node, uint8_t *buf,
uint16_t sz)
{
uint16_t n, features, num_ele = 0;
const struct l_queue_entry *entry;
n = 0;
l_put_le16(node->comp.cid, buf + n);
n += 2;
l_put_le16(node->comp.pid, buf + n);
n += 2;
l_put_le16(node->comp.vid, buf + n);
n += 2;
l_put_le16(node->comp.crpl, buf + n);
n += 2;
features = 0;
if (node->relay.mode != MESH_MODE_UNSUPPORTED)
features |= FEATURE_RELAY;
if (node->proxy != MESH_MODE_UNSUPPORTED)
features |= FEATURE_PROXY;
if (node->friend != MESH_MODE_UNSUPPORTED)
features |= FEATURE_FRIEND;
if (node->lpn != MESH_MODE_UNSUPPORTED)
features |= FEATURE_LPN;
l_put_le16(features, buf + n);
n += 2;
entry = l_queue_get_entries(node->elements);
for (; entry; entry = entry->next) {
struct node_element *ele = entry->data;
if (ele->idx != num_ele)
return 0;
num_ele++;
/* At least fit location and zeros for number of models */
if ((n + 4) > sz)
return n;
l_put_le16(ele->location, buf + n);
n += 2;
n += mesh_model_generate_composition(ele->models, sz - n,
buf + n);
}
if (!num_ele)
return 0;
return n;
}
static bool match_page(const void *a, const void *b)
{
const struct mesh_config_comp_page *page = a;
uint8_t page_num = L_PTR_TO_UINT(b);
return page->page_num == page_num;
}
static void convert_node_to_storage(struct mesh_node *node,
struct mesh_config_node *db_node)
{
const struct l_queue_entry *entry;
memset(db_node, 0, sizeof(struct mesh_config_node));
db_node->cid = node->comp.cid;
db_node->pid = node->comp.pid;
db_node->vid = node->comp.vid;
db_node->crpl = node->comp.crpl;
db_node->modes.lpn = node->lpn;
db_node->modes.proxy = node->proxy;
db_node->modes.friend = node->friend;
db_node->modes.relay.state = node->relay.mode;
db_node->modes.relay.cnt = node->relay.cnt;
db_node->modes.relay.interval = node->relay.interval;