1 // SPDX-License-Identifier: LGPL-2.1-or-later
4 * BlueZ - Bluetooth protocol stack for Linux
6 * Copyright (C) 2017-2020 Intel Corporation. All rights reserved.
15 #ifndef TIZEN_FEATURE_BLUEZ_MODIFY
24 #include "mesh/mesh-defs.h"
25 #include "mesh/mesh.h"
27 #include "mesh/net-keys.h"
28 #include "mesh/appkey.h"
29 #include "mesh/mesh-config.h"
30 #include "mesh/provision.h"
31 #include "mesh/keyring.h"
32 #include "mesh/model.h"
33 #include "mesh/cfgmod.h"
34 #include "mesh/util.h"
35 #include "mesh/error.h"
36 #include "mesh/dbus.h"
37 #include "mesh/agent.h"
38 #include "mesh/manager.h"
40 #include "mesh/node.h"
42 #define MESH_NODE_PATH_PREFIX "/node"
44 /* Default values for a new locally created node */
45 #define DEFAULT_NEW_UNICAST 0x0001
46 #define DEFAULT_IV_INDEX 0x0000
48 /* Default element location: unknown */
49 #define DEFAULT_LOCATION 0x0000
60 struct l_queue *models;
65 struct node_composition {
74 struct l_queue *elements;
75 struct l_queue *pages;
79 struct mesh_agent *agent;
80 struct mesh_config *cfg;
87 struct node_composition comp;
116 struct managed_obj_request {
117 struct mesh_node *node;
119 node_ready_func_t ready_cb;
120 node_join_ready_func_t join_ready_cb;
122 struct l_dbus_message *pending_msg;
123 enum request_type type;
125 struct mesh_node *attach;
126 struct node_import *import;
130 struct send_options {
135 static struct l_queue *nodes;
137 static bool match_device_uuid(const void *a, const void *b)
139 const struct mesh_node *node = a;
140 const uint8_t *uuid = b;
142 return (memcmp(node->uuid, uuid, 16) == 0);
145 static bool match_token(const void *a, const void *b)
147 const struct mesh_node *node = a;
148 const uint64_t *token = b;
149 const uint64_t tmp = l_get_be64(node->token);
151 return *token == tmp;
154 static bool match_element_idx(const void *a, const void *b)
156 const struct node_element *element = a;
157 uint32_t index = L_PTR_TO_UINT(b);
159 return (element->idx == index);
162 static int compare_element_idx(const void *a, const void *b, void *user_data)
164 uint32_t a_idx = ((const struct node_element *)a)->idx;
165 uint32_t b_idx = ((const struct node_element *)b)->idx;
176 static bool match_element_path(const void *a, const void *b)
178 const struct node_element *element = a;
179 const char *path = b;
184 return (!strcmp(element->path, path));
187 struct mesh_node *node_find_by_uuid(uint8_t uuid[16])
189 return l_queue_find(nodes, match_device_uuid, uuid);
192 struct mesh_node *node_find_by_token(uint64_t token)
194 return l_queue_find(nodes, match_token, (void *) &token);
197 uint8_t *node_uuid_get(struct mesh_node *node)
204 static void set_defaults(struct mesh_node *node)
206 node->lpn = MESH_MODE_UNSUPPORTED;
207 node->proxy = MESH_MODE_UNSUPPORTED;
208 node->friend = (mesh_friendship_supported()) ? MESH_MODE_DISABLED :
209 MESH_MODE_UNSUPPORTED;
210 node->beacon = (mesh_beacon_enabled()) ? MESH_MODE_ENABLED :
212 node->relay.mode = (mesh_relay_supported()) ? MESH_MODE_DISABLED :
213 MESH_MODE_UNSUPPORTED;
214 node->ttl = TTL_MASK;
215 node->seq_number = DEFAULT_SEQUENCE_NUMBER;
218 static struct mesh_node *node_new(const uint8_t uuid[16])
220 struct mesh_node *node;
222 node = l_new(struct mesh_node, 1);
223 node->net = mesh_net_new(node);
224 node->elements = l_queue_new();
225 node->pages = l_queue_new();
226 memcpy(node->uuid, uuid, sizeof(node->uuid));
232 static void free_element_path(void *a, void *b)
234 struct node_element *element = a;
236 l_free(element->path);
237 element->path = NULL;
240 static void element_free(void *data)
242 struct node_element *element = data;
244 l_queue_destroy(element->models, mesh_model_free);
245 l_free(element->path);
249 static void free_node_dbus_resources(struct mesh_node *node)
254 if (node->disc_watch) {
255 l_dbus_remove_watch(dbus_get_bus(), node->disc_watch);
256 node->disc_watch = 0;
259 l_queue_foreach(node->elements, free_element_path, NULL);
262 l_free(node->app_path);
263 node->app_path = NULL;
265 if (node->obj_path) {
266 l_dbus_object_remove_interface(dbus_get_bus(), node->obj_path,
267 MESH_NODE_INTERFACE);
269 l_dbus_object_remove_interface(dbus_get_bus(), node->obj_path,
270 MESH_MANAGEMENT_INTERFACE);
272 l_dbus_object_remove_interface(dbus_get_bus(), node->obj_path,
273 L_DBUS_INTERFACE_PROPERTIES);
275 l_free(node->obj_path);
276 node->obj_path = NULL;
280 static void free_node_resources(void *data)
282 struct mesh_node *node = data;
284 /* Unregister io callbacks */
285 mesh_net_detach(node->net);
288 /* In case of a provisioner, stop active scanning */
289 if (node->provisioner)
290 manager_scan_cancel(node);
292 /* Free dynamic resources */
293 free_node_dbus_resources(node);
294 l_queue_destroy(node->elements, element_free);
295 l_queue_destroy(node->pages, l_free);
296 mesh_agent_remove(node->agent);
297 mesh_config_release(node->cfg);
298 mesh_net_free(node->net);
299 l_free(node->storage_dir);
303 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
305 * This function is called to free resources
307 void node_release_resources(struct mesh_node *node)
312 /* Free dynamic resources */
313 free_node_dbus_resources(node);
315 l_debug("Node DBUS Resources are removed");
320 * This function is called to free resources and remove the
321 * configuration files for the specified node.
323 void node_remove(struct mesh_node *node)
328 l_queue_remove(nodes, node);
330 mesh_config_destroy_nvm(node->cfg);
332 free_node_resources(node);
335 static bool add_element_from_storage(struct mesh_node *node,
336 struct mesh_config_element *db_ele)
338 struct node_element *ele;
340 ele = l_new(struct node_element, 1);
344 ele->idx = db_ele->index;
345 ele->location = db_ele->location;
346 ele->models = l_queue_new();
347 l_queue_push_tail(node->elements, ele);
349 if (!mesh_model_add_from_storage(node, ele->idx, ele->models,
356 static bool add_elements_from_storage(struct mesh_node *node,
357 struct mesh_config_node *db_node)
359 const struct l_queue_entry *entry;
361 entry = l_queue_get_entries(db_node->elements);
363 for (; entry; entry = entry->next)
364 if (!add_element_from_storage(node, entry->data))
370 static void set_net_key(void *a, void *b)
372 struct mesh_config_netkey *netkey = a;
373 struct mesh_node *node = b;
375 mesh_net_set_key(node->net, netkey->idx, netkey->key, netkey->new_key,
379 static void set_appkey(void *a, void *b)
381 struct mesh_config_appkey *appkey = a;
382 struct mesh_node *node = b;
384 appkey_key_init(node->net, appkey->net_idx, appkey->app_idx,
385 appkey->key, appkey->new_key);
388 static bool init_storage_dir(struct mesh_node *node)
391 char dir_name[PATH_MAX];
393 if (node->storage_dir)
396 if (!hex2str(node->uuid, 16, uuid, sizeof(uuid)))
399 snprintf(dir_name, PATH_MAX, "%s/%s", mesh_get_storage_dir(), uuid);
401 if (strlen(dir_name) >= PATH_MAX)
404 create_dir(dir_name);
406 node->storage_dir = l_strdup(dir_name);
408 /* Initialize directory for storing RPL info */
409 return rpl_init(node->storage_dir);
412 static void update_net_settings(struct mesh_node *node)
414 struct mesh_net *net = node->net;
416 mesh_net_set_proxy_mode(net, node->proxy == MESH_MODE_ENABLED);
418 mesh_net_set_friend_mode(net, node->friend == MESH_MODE_ENABLED);
420 mesh_net_set_relay_mode(net, node->relay.mode == MESH_MODE_ENABLED,
421 node->relay.cnt, node->relay.interval);
423 mesh_net_set_beacon_mode(net, node->beacon == MESH_MODE_ENABLED);
426 static bool init_from_storage(struct mesh_config_node *db_node,
427 const uint8_t uuid[16], struct mesh_config *cfg,
430 unsigned int num_ele;
432 struct mesh_node *node = node_new(uuid);
435 nodes = l_queue_new();
437 l_queue_push_tail(nodes, node);
439 node->comp.cid = db_node->cid;
440 node->comp.pid = db_node->pid;
441 node->comp.vid = db_node->vid;
442 node->comp.crpl = db_node->crpl;
443 node->lpn = db_node->modes.lpn;
445 node->proxy = db_node->modes.proxy;
446 node->friend = db_node->modes.friend;
447 node->relay.mode = db_node->modes.relay.state;
448 node->relay.cnt = db_node->modes.relay.cnt;
449 node->relay.interval = db_node->modes.relay.interval;
450 node->beacon = db_node->modes.beacon;
452 l_debug("relay %2.2x, proxy %2.2x, lpn %2.2x, friend %2.2x",
453 node->relay.mode, node->proxy, node->lpn, node->friend);
454 node->ttl = db_node->ttl;
455 node->seq_number = db_node->seq_number;
457 memcpy(node->dev_key, db_node->dev_key, 16);
458 memcpy(node->token, db_node->token, 8);
460 num_ele = l_queue_length(db_node->elements);
461 if (num_ele > MAX_ELE_COUNT)
464 node->num_ele = num_ele;
466 if (num_ele != 0 && !add_elements_from_storage(node, db_node))
469 node->primary = db_node->unicast;
471 if (!db_node->netkeys)
474 if (!IS_UNASSIGNED(node->primary) &&
475 !mesh_net_register_unicast(node->net, node->primary, num_ele))
478 mesh_net_set_iv_index(node->net, db_node->iv_index, db_node->iv_update);
480 /* Initialize directory for storing keyring and RPL info */
481 if (!init_storage_dir(node) || !mesh_net_load_rpl(node->net))
484 if (db_node->net_transmit)
485 mesh_net_transmit_params_set(node->net,
486 db_node->net_transmit->count,
487 db_node->net_transmit->interval);
489 l_queue_foreach(db_node->netkeys, set_net_key, node);
491 l_queue_foreach(db_node->appkeys, set_appkey, node);
493 while (l_queue_length(db_node->pages)) {
494 struct mesh_config_comp_page *page;
496 /* Move the composition pages to the node struct */
497 page = l_queue_pop_head(db_node->pages);
498 l_queue_push_tail(node->pages, page);
501 mesh_net_set_seq_num(node->net, node->seq_number);
502 mesh_net_set_default_ttl(node->net, node->ttl);
504 update_net_settings(node);
506 /* Initialize configuration server model */
507 cfgmod_server_init(node, PRIMARY_ELE_IDX);
517 static void cleanup_node(void *data)
519 struct mesh_node *node = data;
520 uint32_t seq_num = mesh_net_get_seq_num(node->net);
522 /* Preserve the last used sequence number */
523 mesh_config_write_seq_number(node->cfg, seq_num, false);
525 free_node_resources(node);
529 * This function is called to free resources and write the current
530 * sequence numbers to the configuration file for each known node.
532 void node_cleanup_all(void)
534 l_queue_destroy(nodes, cleanup_node);
535 l_dbus_unregister_interface(dbus_get_bus(), MESH_NODE_INTERFACE);
536 l_dbus_unregister_interface(dbus_get_bus(), MESH_MANAGEMENT_INTERFACE);
539 bool node_is_provisioner(struct mesh_node *node)
541 return node->provisioner;
544 bool node_is_busy(struct mesh_node *node)
549 void node_app_key_delete(struct mesh_node *node, uint16_t net_idx,
552 const struct l_queue_entry *entry;
554 entry = l_queue_get_entries(node->elements);
555 for (; entry; entry = entry->next) {
556 struct node_element *ele = entry->data;
558 mesh_model_app_key_delete(node, ele->idx, ele->models, app_idx);
562 uint16_t node_get_primary(struct mesh_node *node)
565 return UNASSIGNED_ADDRESS;
567 return node->primary;
570 const uint8_t *node_get_device_key(struct mesh_node *node)
575 return node->dev_key;
578 void node_set_token(struct mesh_node *node, uint8_t token[8])
580 memcpy(node->token, token, 8);
583 const uint8_t *node_get_token(struct mesh_node *node)
591 uint8_t node_get_num_elements(struct mesh_node *node)
593 return node->num_ele;
596 struct l_queue *node_get_element_models(struct mesh_node *node, uint8_t ele_idx)
598 struct node_element *ele;
603 ele = l_queue_find(node->elements, match_element_idx,
604 L_UINT_TO_PTR(ele_idx));
611 uint8_t node_default_ttl_get(struct mesh_node *node)
618 bool node_default_ttl_set(struct mesh_node *node, uint8_t ttl)
625 res = mesh_config_write_ttl(node->cfg, ttl);
629 mesh_net_set_default_ttl(node->net, ttl);
635 bool node_set_sequence_number(struct mesh_node *node, uint32_t seq)
640 node->seq_number = seq;
642 return mesh_config_write_seq_number(node->cfg, node->seq_number, true);
645 uint32_t node_get_sequence_number(struct mesh_node *node)
650 return node->seq_number;
653 int node_get_element_idx(struct mesh_node *node, uint16_t ele_addr)
661 num_ele = node_get_num_elements(node);
665 addr = node_get_primary(node);
667 if (ele_addr < addr || ele_addr >= addr + num_ele)
670 return ele_addr - addr;
673 uint16_t node_get_crpl(struct mesh_node *node)
678 return node->comp.crpl;
681 uint8_t node_relay_mode_get(struct mesh_node *node, uint8_t *count,
687 return MESH_MODE_DISABLED;
690 *count = node->relay.cnt;
691 *interval = node->relay.interval;
692 return node->relay.mode;
695 uint8_t node_lpn_mode_get(struct mesh_node *node)
698 return MESH_MODE_DISABLED;
703 bool node_relay_mode_set(struct mesh_node *node, bool enable, uint8_t cnt,
708 if (!node || node->relay.mode == MESH_MODE_UNSUPPORTED)
711 res = mesh_config_write_relay_mode(node->cfg, enable, cnt, interval);
714 node->relay.mode = enable ? MESH_MODE_ENABLED :
716 node->relay.cnt = cnt;
717 node->relay.interval = interval;
718 mesh_net_set_relay_mode(node->net, enable, cnt, interval);
724 bool node_proxy_mode_set(struct mesh_node *node, bool enable)
729 if (!node || node->proxy == MESH_MODE_UNSUPPORTED)
732 proxy = enable ? MESH_MODE_ENABLED : MESH_MODE_DISABLED;
733 res = mesh_config_write_mode(node->cfg, "proxy", proxy);
737 mesh_net_set_proxy_mode(node->net, enable);
743 uint8_t node_proxy_mode_get(struct mesh_node *node)
746 return MESH_MODE_DISABLED;
751 bool node_beacon_mode_set(struct mesh_node *node, bool enable)
759 beacon = enable ? MESH_MODE_ENABLED : MESH_MODE_DISABLED;
760 res = mesh_config_write_mode(node->cfg, "beacon", beacon);
763 node->beacon = beacon;
764 mesh_net_set_beacon_mode(node->net, enable);
770 uint8_t node_beacon_mode_get(struct mesh_node *node)
773 return MESH_MODE_DISABLED;
778 bool node_friend_mode_set(struct mesh_node *node, bool enable)
783 if (!node || node->friend == MESH_MODE_UNSUPPORTED)
786 friend = enable ? MESH_MODE_ENABLED : MESH_MODE_DISABLED;
787 res = mesh_config_write_mode(node->cfg, "friend", friend);
790 node->friend = friend;
791 mesh_net_set_friend_mode(node->net, enable);
797 uint8_t node_friend_mode_get(struct mesh_node *node)
800 return MESH_MODE_DISABLED;
805 static uint16_t generate_node_comp(struct mesh_node *node, uint8_t *buf,
808 uint16_t n, features, num_ele = 0;
809 const struct l_queue_entry *entry;
813 l_put_le16(node->comp.cid, buf + n);
815 l_put_le16(node->comp.pid, buf + n);
817 l_put_le16(node->comp.vid, buf + n);
819 l_put_le16(node->comp.crpl, buf + n);
824 if (node->relay.mode != MESH_MODE_UNSUPPORTED)
825 features |= FEATURE_RELAY;
826 if (node->proxy != MESH_MODE_UNSUPPORTED)
827 features |= FEATURE_PROXY;
828 if (node->friend != MESH_MODE_UNSUPPORTED)
829 features |= FEATURE_FRIEND;
830 if (node->lpn != MESH_MODE_UNSUPPORTED)
831 features |= FEATURE_LPN;
833 l_put_le16(features, buf + n);
836 entry = l_queue_get_entries(node->elements);
838 for (; entry; entry = entry->next) {
839 struct node_element *ele = entry->data;
841 if (ele->idx != num_ele)
846 /* At least fit location and zeros for number of models */
850 l_put_le16(ele->location, buf + n);
853 n += mesh_model_generate_composition(ele->models, sz - n,
863 static bool match_page(const void *a, const void *b)
865 const struct mesh_config_comp_page *page = a;
866 uint8_t page_num = L_PTR_TO_UINT(b);
868 return page->page_num == page_num;
871 static void convert_node_to_storage(struct mesh_node *node,
872 struct mesh_config_node *db_node)
874 const struct l_queue_entry *entry;
876 memset(db_node, 0, sizeof(struct mesh_config_node));
878 db_node->cid = node->comp.cid;
879 db_node->pid = node->comp.pid;
880 db_node->vid = node->comp.vid;
881 db_node->crpl = node->comp.crpl;
882 db_node->modes.lpn = node->lpn;
883 db_node->modes.proxy = node->proxy;
885 db_node->modes.friend = node->friend;
886 db_node->modes.relay.state = node->relay.mode;
887 db_node->modes.relay.cnt = node->relay.cnt;
888 db_node->modes.relay.interval = node->relay.interval;
889 db_node->modes.beacon = node->beacon;
891 db_node->ttl = node->ttl;
892 db_node->seq_number = node->seq_number;
894 db_node->elements = l_queue_new();
896 entry = l_queue_get_entries(node->elements);
898 for (; entry; entry = entry->next) {
899 struct node_element *ele = entry->data;
900 struct mesh_config_element *db_ele;
902 db_ele = l_new(struct mesh_config_element, 1);
904 db_ele->index = ele->idx;
905 db_ele->location = ele->location;
906 db_ele->models = l_queue_new();
908 mesh_model_convert_to_storage(db_ele->models, ele->models);
910 l_queue_push_tail(db_node->elements, db_ele);
915 static bool create_node_config(struct mesh_node *node, const uint8_t uuid[16])
917 struct mesh_config_node db_node;
918 const struct l_queue_entry *entry;
919 const char *storage_dir;
921 convert_node_to_storage(node, &db_node);
922 storage_dir = mesh_get_storage_dir();
923 node->cfg = mesh_config_create(storage_dir, uuid, &db_node);
926 init_storage_dir(node);
928 /* Free temporarily allocated resources */
929 entry = l_queue_get_entries(db_node.elements);
931 for (; entry; entry = entry->next) {
932 struct mesh_config_element *db_ele = entry->data;
934 l_queue_destroy(db_ele->models, l_free);
937 l_queue_destroy(db_node.elements, l_free);
939 return node->cfg != NULL;
942 static bool set_node_comp(struct mesh_node *node, uint8_t page_num,
943 const uint8_t *data, uint16_t len)
945 struct mesh_config_comp_page *page;
947 if (len < MIN_COMP_SIZE)
950 page = l_queue_remove_if(node->pages, match_page,
951 L_UINT_TO_PTR(page_num));
955 page = l_malloc(sizeof(struct mesh_config_comp_page) + len);
957 page->page_num = page_num;
958 memcpy(page->data, data, len);
959 l_queue_push_tail(node->pages, page);
961 return mesh_config_comp_page_add(node->cfg, page_num, page->data, len);
964 static bool create_node_comp(struct mesh_node *node)
967 uint8_t comp[MAX_MSG_LEN - 2];
969 len = generate_node_comp(node, comp, sizeof(comp));
971 return set_node_comp(node, 0, comp, len);
974 const uint8_t *node_get_comp(struct mesh_node *node, uint8_t page_num,
977 struct mesh_config_comp_page *page = NULL;
980 page = l_queue_find(node->pages, match_page,
981 L_UINT_TO_PTR(page_num));
992 bool node_replace_comp(struct mesh_node *node, uint8_t retire, uint8_t with)
994 struct mesh_config_comp_page *old_page, *keep;
999 keep = l_queue_find(node->pages, match_page, L_UINT_TO_PTR(with));
1004 old_page = l_queue_remove_if(node->pages, match_page,
1005 L_UINT_TO_PTR(retire));
1008 keep->page_num = retire;
1009 mesh_config_comp_page_mv(node->cfg, with, retire);
1014 static void attach_io(void *a, void *b)
1016 struct mesh_node *node = a;
1017 struct mesh_io *io = b;
1020 mesh_net_attach(node->net, io);
1023 /* Register callbacks for all nodes io */
1024 void node_attach_io_all(struct mesh_io *io)
1026 l_queue_foreach(nodes, attach_io, io);
1029 /* Register node object with D-Bus */
1030 static bool register_node_object(struct mesh_node *node)
1034 if (!hex2str(node->uuid, sizeof(node->uuid), uuid, sizeof(uuid)))
1037 node->obj_path = l_strdup_printf(BLUEZ_MESH_PATH MESH_NODE_PATH_PREFIX
1040 if (!l_dbus_object_add_interface(dbus_get_bus(), node->obj_path,
1041 MESH_NODE_INTERFACE, node))
1044 if (!l_dbus_object_add_interface(dbus_get_bus(), node->obj_path,
1045 MESH_MANAGEMENT_INTERFACE, node))
1048 if (!l_dbus_object_add_interface(dbus_get_bus(), node->obj_path,
1049 L_DBUS_INTERFACE_PROPERTIES, NULL))
1055 static void app_disc_cb(struct l_dbus *bus, void *user_data)
1057 struct mesh_node *node = user_data;
1059 l_info("App %s disconnected (%u)", node->owner, node->disc_watch);
1061 node->disc_watch = 0;
1063 /* In case of a provisioner, stop active scanning */
1064 if (node->provisioner)
1065 manager_scan_cancel(node);
1067 free_node_dbus_resources(node);
1070 static bool get_sig_models_from_properties(struct mesh_node *node,
1071 struct node_element *ele,
1072 struct l_dbus_message_iter *property)
1074 struct l_dbus_message_iter mods, var;
1078 ele->models = l_queue_new();
1080 if (!l_dbus_message_iter_get_variant(property, "a(qa{sv})", &mods))
1083 /* Bluetooth SIG defined models */
1084 while (l_dbus_message_iter_next_entry(&mods, &m_id, &var)) {
1085 uint32_t id = SET_ID(SIG_VENDOR, m_id);
1087 /* Allow Config Server Model only on the primary element */
1088 if (ele->idx != PRIMARY_ELE_IDX && id == CONFIG_SRV_MODEL)
1091 if (!mesh_model_add(node, ele->models, id, &var))
1098 static bool get_vendor_models_from_properties(struct mesh_node *node,
1099 struct node_element *ele,
1100 struct l_dbus_message_iter *property)
1102 struct l_dbus_message_iter mods, var;
1103 uint16_t m_id, v_id;
1106 ele->models = l_queue_new();
1108 if (!l_dbus_message_iter_get_variant(property, "a(qqa{sv})", &mods))
1111 /* Vendor defined models */
1112 while (l_dbus_message_iter_next_entry(&mods, &v_id, &m_id, &var)) {
1113 uint32_t id = SET_ID(v_id, m_id);
1115 if (!mesh_model_add(node, ele->models, id, &var))
1122 static bool get_element_properties(struct mesh_node *node, const char *path,
1123 struct l_dbus_message_iter *properties)
1125 struct node_element *ele = l_new(struct node_element, 1);
1127 struct l_dbus_message_iter var;
1130 bool vendor_mods = false;
1132 l_debug("path %s", path);
1134 ele->location = DEFAULT_LOCATION;
1136 while (l_dbus_message_iter_next_entry(properties, &key, &var)) {
1137 if (!strcmp(key, "Index")) {
1139 if (idx || !l_dbus_message_iter_get_variant(&var, "y",
1145 } else if (!strcmp(key, "Models")) {
1150 if (!get_sig_models_from_properties(node, ele, &var))
1154 } else if (!strcmp(key, "VendorModels")) {
1159 if (!get_vendor_models_from_properties(node, ele, &var))
1164 } else if (!strcmp(key, "Location")) {
1165 if (!l_dbus_message_iter_get_variant(&var, "q",
1171 /* Check for the presence of the required properties */
1172 if (!idx || !mods || !vendor_mods)
1175 if (l_queue_find(node->elements, match_element_idx,
1176 L_UINT_TO_PTR(ele->idx)))
1179 l_queue_insert(node->elements, ele, compare_element_idx, NULL);
1181 ele->path = l_strdup(path);
1184 * Add configuration server model on the primary element.
1185 * We allow the application not to specify the presense of
1186 * the Configuration Server model, since it's implemented by the
1187 * daemon. If the model is present in the application properties,
1188 * the operation below will be a "no-op".
1190 if (ele->idx == PRIMARY_ELE_IDX)
1191 mesh_model_add(node, ele->models, CONFIG_SRV_MODEL, NULL);
1200 static bool get_app_properties(struct mesh_node *node, const char *path,
1201 struct l_dbus_message_iter *properties)
1204 struct l_dbus_message_iter variant;
1209 l_debug("path %s", path);
1211 node->comp.crpl = mesh_get_crpl();
1213 while (l_dbus_message_iter_next_entry(properties, &key, &variant)) {
1214 if (!cid && !strcmp(key, "CompanyID")) {
1215 if (!l_dbus_message_iter_get_variant(&variant, "q",
1222 if (!pid && !strcmp(key, "ProductID")) {
1223 if (!l_dbus_message_iter_get_variant(&variant, "q",
1230 if (!vid && !strcmp(key, "VersionID")) {
1231 if (!l_dbus_message_iter_get_variant(&variant, "q",
1238 if (!strcmp(key, "CRPL")) {
1239 if (!l_dbus_message_iter_get_variant(&variant, "q",
1246 if (!cid || !pid || !vid)
1252 static bool add_local_node(struct mesh_node *node, uint16_t unicast, bool kr,
1253 bool ivu, uint32_t iv_idx, uint8_t dev_key[16],
1254 uint16_t net_key_idx, uint8_t net_key[16])
1257 nodes = l_queue_new();
1259 l_queue_push_tail(nodes, node);
1261 if (!mesh_config_write_iv_index(node->cfg, iv_idx, ivu))
1264 mesh_net_set_iv_index(node->net, iv_idx, ivu);
1266 if (!mesh_config_write_unicast(node->cfg, unicast))
1269 l_getrandom(node->token, sizeof(node->token));
1270 if (!mesh_config_write_token(node->cfg, node->token))
1273 memcpy(node->dev_key, dev_key, 16);
1274 if (!mesh_config_write_device_key(node->cfg, dev_key))
1277 node->primary = unicast;
1278 mesh_net_register_unicast(node->net, unicast, node->num_ele);
1280 if (mesh_net_add_key(node->net, net_key_idx, net_key) !=
1281 MESH_STATUS_SUCCESS)
1285 /* Duplicate net key, if the key refresh is on */
1286 if (mesh_net_update_key(node->net, net_key_idx, net_key) !=
1287 MESH_STATUS_SUCCESS)
1290 if (!mesh_config_net_key_set_phase(node->cfg, net_key_idx,
1291 KEY_REFRESH_PHASE_TWO))
1295 update_net_settings(node);
1297 /* Initialize configuration server model */
1298 cfgmod_server_init(node, PRIMARY_ELE_IDX);
1305 static void update_composition(struct mesh_node *node, struct mesh_node *attach)
1307 if (node->comp.cid != attach->comp.cid)
1308 mesh_config_update_company_id(attach->cfg, node->comp.cid);
1310 if (node->comp.pid != attach->comp.pid)
1311 mesh_config_update_product_id(attach->cfg, node->comp.pid);
1313 if (node->comp.vid != attach->comp.vid)
1314 mesh_config_update_version_id(attach->cfg, node->comp.vid);
1316 if (node->comp.crpl != attach->comp.crpl)
1317 mesh_config_update_crpl(attach->cfg, node->comp.crpl);
1319 attach->comp = node->comp;
1322 static void update_model_options(struct mesh_node *node,
1323 struct mesh_node *attach)
1326 struct node_element *ele, *ele_attach;
1328 len = l_queue_length(node->elements);
1330 for (i = 0; i < len; i++) {
1332 ele = l_queue_find(node->elements, match_element_idx,
1334 ele_attach = l_queue_find(attach->elements, match_element_idx,
1336 if (!ele || !ele_attach)
1339 mesh_model_update_opts(node, ele->idx, ele_attach->models,
1344 static bool check_req_node(struct managed_obj_request *req)
1346 const int offset = 8;
1347 uint16_t node_len, len;
1348 uint8_t comp[MAX_MSG_LEN - 2];
1349 const uint8_t *node_comp;
1351 len = generate_node_comp(req->node, comp, sizeof(comp));
1353 if (len < MIN_COMP_SIZE)
1356 node_comp = node_get_comp(req->attach, 0, &node_len);
1358 /* If no page 0 exists, create it and accept */
1359 if (!node_len || !node_comp)
1360 return set_node_comp(req->attach, 0, comp, len);
1362 /* Test Element/Model part of composition and reject if changed */
1363 if (node_len != len || memcmp(&node_comp[offset], &comp[offset],
1367 /* If comp has changed, but not Element/Models, resave and accept */
1368 else if (memcmp(node_comp, comp, node_len))
1369 return set_node_comp(req->attach, 0, comp, len);
1371 /* Nothing has changed */
1375 static bool attach_req_node(struct mesh_node *attach, struct mesh_node *node)
1377 const struct l_queue_entry *attach_entry;
1378 const struct l_queue_entry *node_entry;
1380 attach->obj_path = node->obj_path;
1381 node->obj_path = NULL;
1383 if (!register_node_object(attach)) {
1384 free_node_dbus_resources(attach);
1388 attach_entry = l_queue_get_entries(attach->elements);
1389 node_entry = l_queue_get_entries(node->elements);
1392 * Update existing node with paths collected in temporary node,
1393 * then remove the temporary.
1395 while (attach_entry && node_entry) {
1396 struct node_element *attach_ele = attach_entry->data;
1397 struct node_element *node_ele = node_entry->data;
1399 attach_ele->path = node_ele->path;
1400 node_ele->path = NULL;
1402 attach_entry = attach_entry->next;
1403 node_entry = node_entry->next;
1406 mesh_agent_remove(attach->agent);
1407 attach->agent = node->agent;
1410 attach->provisioner = node->provisioner;
1412 attach->app_path = node->app_path;
1413 node->app_path = NULL;
1415 attach->owner = node->owner;
1418 update_composition(node, attach);
1419 update_model_options(node, attach);
1426 static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data)
1428 struct l_dbus_message_iter objects, interfaces;
1429 struct managed_obj_request *req = user_data;
1431 struct mesh_node *node = req->node;
1432 struct node_import *import;
1433 bool have_app = false;
1434 unsigned int num_ele;
1435 struct keyring_net_key net_key;
1436 uint8_t dev_key[16];
1438 if (req->type == REQUEST_TYPE_ATTACH)
1439 req->attach->busy = false;
1441 if (!msg || l_dbus_message_is_error(msg)) {
1442 l_error("Failed to get app's dbus objects");
1446 if (!l_dbus_message_get_arguments(msg, "a{oa{sa{sv}}}", &objects)) {
1447 l_error("Failed to parse app's dbus objects");
1451 while (l_dbus_message_iter_next_entry(&objects, &path, &interfaces)) {
1452 struct l_dbus_message_iter properties;
1453 const char *interface;
1455 while (l_dbus_message_iter_next_entry(&interfaces, &interface,
1459 if (!strcmp(MESH_ELEMENT_INTERFACE, interface)) {
1460 res = get_element_properties(node, path,
1464 } else if (!strcmp(MESH_APPLICATION_INTERFACE,
1469 req->node->app_path = l_strdup(path);
1471 res = get_app_properties(node, path,
1478 } else if (!strcmp(MESH_PROVISION_AGENT_INTERFACE,
1482 sender = l_dbus_message_get_sender(msg);
1483 node->agent = mesh_agent_create(path, sender,
1488 } else if (!strcmp(MESH_PROVISIONER_INTERFACE,
1490 node->provisioner = true;
1496 l_error("Interface %s not found", MESH_APPLICATION_INTERFACE);
1500 if (l_queue_isempty(node->elements)) {
1501 l_error("Interface %s not found", MESH_ELEMENT_INTERFACE);
1505 if (!l_queue_find(node->elements, match_element_idx,
1506 L_UINT_TO_PTR(PRIMARY_ELE_IDX))) {
1508 l_debug("Primary element not detected");
1512 num_ele = l_queue_length(node->elements);
1514 if (num_ele > MAX_ELE_COUNT)
1517 node->num_ele = num_ele;
1519 if (req->type != REQUEST_TYPE_ATTACH) {
1520 /* Generate node configuration for a brand new node */
1521 if (!create_node_config(node, node->uuid))
1524 /* Create node composition */
1525 if (!create_node_comp(node))
1527 } else if (!check_req_node(req))
1528 /* Check the integrity of the node composition */
1531 switch (req->type) {
1532 case REQUEST_TYPE_ATTACH:
1533 if (!attach_req_node(req->attach, node))
1536 req->attach->disc_watch = l_dbus_add_disconnect_watch(
1537 dbus_get_bus(), req->attach->owner,
1538 app_disc_cb, req->attach, NULL);
1540 req->ready_cb(req->pending_msg, MESH_ERROR_NONE, req->attach);
1543 case REQUEST_TYPE_JOIN:
1545 l_error("Interface %s not found",
1546 MESH_PROVISION_AGENT_INTERFACE);
1550 req->join_ready_cb(node, node->agent);
1554 case REQUEST_TYPE_IMPORT:
1555 import = req->import;
1556 if (!add_local_node(node, import->unicast, import->flags.kr,
1558 import->iv_index, import->dev_key,
1559 import->net_idx, import->net_key))
1562 req->ready_cb(req->pending_msg, MESH_ERROR_NONE, node);
1567 case REQUEST_TYPE_CREATE:
1568 /* Generate device and primary network keys */
1569 l_getrandom(dev_key, sizeof(dev_key));
1570 l_getrandom(net_key.old_key, sizeof(net_key.old_key));
1571 memcpy(net_key.new_key, net_key.old_key,
1572 sizeof(net_key.old_key));
1573 net_key.net_idx = PRIMARY_NET_IDX;
1574 net_key.phase = KEY_REFRESH_PHASE_NONE;
1576 if (!add_local_node(node, DEFAULT_NEW_UNICAST, false, false,
1577 DEFAULT_IV_INDEX, dev_key,
1582 if (!keyring_put_remote_dev_key(node, DEFAULT_NEW_UNICAST,
1583 node->num_ele, dev_key))
1586 if (!keyring_put_net_key(node, PRIMARY_NET_IDX, &net_key))
1589 req->ready_cb(req->pending_msg, MESH_ERROR_NONE, node);
1597 /* Handle failed requests */
1600 if (req->type == REQUEST_TYPE_JOIN)
1601 req->join_ready_cb(NULL, NULL);
1603 req->ready_cb(req->pending_msg, MESH_ERROR_FAILED, NULL);
1605 if (req->type == REQUEST_TYPE_IMPORT)
1606 l_free(req->import);
1609 static void send_managed_objects_request(const char *destination,
1611 struct managed_obj_request *req)
1613 struct l_dbus_message *msg;
1615 msg = l_dbus_message_new_method_call(dbus_get_bus(), destination, path,
1616 L_DBUS_INTERFACE_OBJECT_MANAGER,
1617 "GetManagedObjects");
1618 l_dbus_message_set_arguments(msg, "");
1619 dbus_send_with_timeout(dbus_get_bus(), msg, get_managed_objects_cb,
1620 req, l_free, DEFAULT_DBUS_TIMEOUT);
1623 /* Establish relationship between application and mesh node */
1624 void node_attach(const char *app_root, const char *sender, uint64_t token,
1625 node_ready_func_t cb, void *user_data)
1627 struct managed_obj_request *req;
1628 struct mesh_node *node;
1630 node = l_queue_find(nodes, match_token, (void *) &token);
1632 cb(user_data, MESH_ERROR_NOT_FOUND, NULL);
1636 /* Check if there is a pending request associated with this node */
1638 cb(user_data, MESH_ERROR_BUSY, NULL);
1642 /* Check if the node is already in use */
1644 l_warn("The node is already in use");
1645 cb(user_data, MESH_ERROR_ALREADY_EXISTS, NULL);
1649 req = l_new(struct managed_obj_request, 1);
1652 * Create a temporary node to collect composition data from attaching
1653 * application. Existing node is passed in req->attach.
1655 req->node = node_new(node->uuid);
1656 req->node->owner = l_strdup(sender);
1658 req->pending_msg = user_data;
1660 req->type = REQUEST_TYPE_ATTACH;
1664 l_dbus_method_call(dbus_get_bus(), sender, app_root,
1665 L_DBUS_INTERFACE_OBJECT_MANAGER,
1666 "GetManagedObjects", NULL,
1667 get_managed_objects_cb,
1671 /* Create a temporary pre-provisioned node */
1672 void node_join(const char *app_root, const char *sender, const uint8_t *uuid,
1673 node_join_ready_func_t cb)
1675 struct managed_obj_request *req;
1679 req = l_new(struct managed_obj_request, 1);
1680 req->node = node_new(uuid);
1681 req->join_ready_cb = cb;
1682 req->type = REQUEST_TYPE_JOIN;
1684 send_managed_objects_request(sender, app_root, req);
1687 void node_import(const char *app_root, const char *sender, const uint8_t *uuid,
1688 const uint8_t dev_key[16], const uint8_t net_key[16],
1689 uint16_t net_idx, bool kr, bool ivu,
1690 uint32_t iv_index, uint16_t unicast,
1691 node_ready_func_t cb, void *user_data)
1693 struct managed_obj_request *req;
1697 req = l_new(struct managed_obj_request, 1);
1699 req->node = node_new(uuid);
1701 req->pending_msg = user_data;
1703 req->import = l_new(struct node_import, 1);
1704 memcpy(req->import->dev_key, dev_key, 16);
1705 memcpy(req->import->net_key, net_key, 16);
1706 req->import->net_idx = net_idx;
1707 req->import->flags.kr = kr;
1708 req->import->flags.ivu = ivu;
1709 req->import->iv_index = iv_index;
1710 req->import->unicast = unicast;
1712 req->type = REQUEST_TYPE_IMPORT;
1714 send_managed_objects_request(sender, app_root, req);
1717 void node_create(const char *app_root, const char *sender, const uint8_t *uuid,
1718 node_ready_func_t cb, void *user_data)
1720 struct managed_obj_request *req;
1724 req = l_new(struct managed_obj_request, 1);
1725 req->node = node_new(uuid);
1727 req->pending_msg = user_data;
1728 req->type = REQUEST_TYPE_CREATE;
1730 send_managed_objects_request(sender, app_root, req);
1733 static void build_element_config(void *a, void *b)
1735 struct node_element *ele = a;
1736 struct l_dbus_message_builder *builder = b;
1738 l_debug("Element %u", ele->idx);
1740 l_dbus_message_builder_enter_struct(builder, "ya(qa{sv})");
1743 l_dbus_message_builder_append_basic(builder, 'y', &ele->idx);
1745 l_dbus_message_builder_enter_array(builder, "(qa{sv})");
1747 /* Iterate over models */
1748 l_queue_foreach(ele->models, mesh_model_build_config, builder);
1750 l_dbus_message_builder_leave_array(builder);
1752 l_dbus_message_builder_leave_struct(builder);
1755 void node_build_attach_reply(struct mesh_node *node,
1756 struct l_dbus_message *reply)
1758 struct l_dbus_message_builder *builder;
1760 builder = l_dbus_message_builder_new(reply);
1762 /* Node object path */
1763 l_dbus_message_builder_append_basic(builder, 'o', node->obj_path);
1765 /* Array of element configurations "a*/
1766 l_dbus_message_builder_enter_array(builder, "(ya(qa{sv}))");
1767 l_queue_foreach(node->elements, build_element_config, builder);
1768 l_dbus_message_builder_leave_array(builder);
1769 l_dbus_message_builder_finalize(builder);
1770 l_dbus_message_builder_destroy(builder);
1773 static bool parse_send_options(struct l_dbus_message_iter *itr,
1774 struct send_options *opts)
1777 struct l_dbus_message_iter var;
1779 opts->segmented = false;
1780 opts->vendor_id = SIG_VENDOR;
1782 while (l_dbus_message_iter_next_entry(itr, &key, &var)) {
1783 if (!strcmp(key, "ForceSegmented")) {
1784 if (!l_dbus_message_iter_get_variant(&var, "b",
1789 if (!strcmp(key, "Vendor")) {
1790 if (!l_dbus_message_iter_get_variant(&var, "q",
1799 static struct l_dbus_message *send_call(struct l_dbus *dbus,
1800 struct l_dbus_message *msg,
1803 struct mesh_node *node = user_data;
1804 const char *sender, *ele_path;
1805 struct l_dbus_message_iter dict, iter_data;
1806 struct send_options opts;
1807 struct node_element *ele;
1808 uint16_t dst, app_idx, net_idx, src;
1814 sender = l_dbus_message_get_sender(msg);
1816 if (strcmp(sender, node->owner))
1817 return dbus_error(msg, MESH_ERROR_NOT_AUTHORIZED, NULL);
1819 if (!l_dbus_message_get_arguments(msg, "oqqa{sv}ay", &ele_path, &dst,
1820 &app_idx, &dict, &iter_data))
1821 return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
1823 ele = l_queue_find(node->elements, match_element_path, ele_path);
1825 return dbus_error(msg, MESH_ERROR_NOT_FOUND,
1826 "Element not found");
1828 if (!parse_send_options(&dict, &opts))
1829 return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
1831 src = node_get_primary(node) + ele->idx;
1833 if (!l_dbus_message_iter_get_fixed_array(&iter_data, &data, &len) ||
1834 !len || len > MAX_MSG_LEN)
1835 return dbus_error(msg, MESH_ERROR_INVALID_ARGS,
1838 if (app_idx & ~APP_IDX_MASK)
1839 return dbus_error(msg, MESH_ERROR_INVALID_ARGS,
1840 "Invalid key index");
1842 net_idx = appkey_net_idx(node_get_net(node), app_idx);
1843 if (net_idx == NET_IDX_INVALID)
1844 return dbus_error(msg, MESH_ERROR_INVALID_ARGS,
1847 if (!mesh_model_send(node, src, dst, app_idx, net_idx, DEFAULT_TTL,
1848 opts.segmented, len, data))
1849 return dbus_error(msg, MESH_ERROR_FAILED, NULL);
1851 return l_dbus_message_new_method_return(msg);
1854 static struct l_dbus_message *dev_key_send_call(struct l_dbus *dbus,
1855 struct l_dbus_message *msg,
1858 struct mesh_node *node = user_data;
1859 const char *sender, *ele_path;
1860 struct l_dbus_message_iter iter_data, dict;
1861 struct send_options opts;
1862 struct node_element *ele;
1863 uint16_t dst, app_idx, net_idx, src;
1868 l_debug("DevKeySend");
1870 sender = l_dbus_message_get_sender(msg);
1872 if (strcmp(sender, node->owner))
1873 return dbus_error(msg, MESH_ERROR_NOT_AUTHORIZED, NULL);
1875 if (!l_dbus_message_get_arguments(msg, "oqbqa{sv}ay", &ele_path, &dst,
1876 &remote, &net_idx, &dict, &iter_data))
1877 return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
1879 /* Loopbacks to local servers must use *remote* addressing */
1880 if (!remote && mesh_net_is_local_address(node->net, dst, 1))
1881 return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
1883 ele = l_queue_find(node->elements, match_element_path, ele_path);
1885 return dbus_error(msg, MESH_ERROR_NOT_FOUND,
1886 "Element not found");
1888 if (!parse_send_options(&dict, &opts))
1889 return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
1891 src = node_get_primary(node) + ele->idx;
1893 if (!l_dbus_message_iter_get_fixed_array(&iter_data, &data, &len) ||
1894 !len || len > MAX_MSG_LEN)
1895 return dbus_error(msg, MESH_ERROR_INVALID_ARGS,
1898 app_idx = remote ? APP_IDX_DEV_REMOTE : APP_IDX_DEV_LOCAL;
1899 if (!mesh_model_send(node, src, dst, app_idx, net_idx, DEFAULT_TTL,
1900 opts.segmented, len, data))
1901 return dbus_error(msg, MESH_ERROR_NOT_FOUND, NULL);
1903 return l_dbus_message_new_method_return(msg);
1906 static struct l_dbus_message *add_netkey_call(struct l_dbus *dbus,
1907 struct l_dbus_message *msg,
1910 struct mesh_node *node = user_data;
1911 const char *sender, *ele_path;
1912 struct node_element *ele;
1913 uint16_t dst, sub_idx, net_idx, src;
1915 struct keyring_net_key key;
1918 l_debug("AddNetKey");
1920 sender = l_dbus_message_get_sender(msg);
1922 if (strcmp(sender, node->owner))
1923 return dbus_error(msg, MESH_ERROR_NOT_AUTHORIZED, NULL);
1925 if (!l_dbus_message_get_arguments(msg, "oqqqb", &ele_path, &dst,
1926 &sub_idx, &net_idx, &update))
1927 return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
1929 ele = l_queue_find(node->elements, match_element_path, ele_path);
1931 return dbus_error(msg, MESH_ERROR_NOT_FOUND,
1932 "Element not found");
1934 src = node_get_primary(node) + ele->idx;
1936 if (!keyring_get_net_key(node, sub_idx, &key))
1937 return dbus_error(msg, MESH_ERROR_NOT_FOUND,
1938 "NetKey not found");
1941 l_put_be16(OP_NETKEY_ADD, data);
1943 if (key.phase != KEY_REFRESH_PHASE_TWO)
1944 memcpy(data + 4, key.old_key, 16);
1946 memcpy(data + 4, key.new_key, 16);
1948 if (key.phase != KEY_REFRESH_PHASE_ONE)
1949 return dbus_error(msg, MESH_ERROR_FAILED,
1951 l_put_be16(OP_NETKEY_UPDATE, data);
1952 memcpy(data + 4, key.new_key, 16);
1955 l_put_le16(sub_idx, &data[2]);
1957 if (!mesh_model_send(node, src, dst, APP_IDX_DEV_REMOTE, net_idx,
1958 DEFAULT_TTL, false, 20, data))
1959 return dbus_error(msg, MESH_ERROR_NOT_FOUND, NULL);
1961 return l_dbus_message_new_method_return(msg);
1964 static struct l_dbus_message *add_appkey_call(struct l_dbus *dbus,
1965 struct l_dbus_message *msg,
1968 struct mesh_node *node = user_data;
1969 const char *sender, *ele_path;
1970 struct node_element *ele;
1971 uint16_t dst, app_idx, net_idx, src;
1973 struct keyring_net_key net_key;
1974 struct keyring_app_key app_key;
1977 l_debug("AddAppKey");
1979 sender = l_dbus_message_get_sender(msg);
1981 if (strcmp(sender, node->owner))
1982 return dbus_error(msg, MESH_ERROR_NOT_AUTHORIZED, NULL);
1984 if (!l_dbus_message_get_arguments(msg, "oqqqb", &ele_path, &dst,
1985 &app_idx, &net_idx, &update))
1986 return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
1988 ele = l_queue_find(node->elements, match_element_path, ele_path);
1990 return dbus_error(msg, MESH_ERROR_NOT_FOUND,
1991 "Element not found");
1993 src = node_get_primary(node) + ele->idx;
1995 if (!keyring_get_app_key(node, app_idx, &app_key))
1996 return dbus_error(msg, MESH_ERROR_NOT_FOUND,
1997 "AppKey not found");
1999 if (!keyring_get_net_key(node, app_key.net_idx, &net_key)) {
2000 return dbus_error(msg, MESH_ERROR_NOT_FOUND,
2001 "Bound NetKey not found");
2005 data[0] = OP_APPKEY_ADD;
2006 if (net_key.phase != KEY_REFRESH_PHASE_TWO)
2007 memcpy(data + 4, app_key.old_key, 16);
2009 memcpy(data + 4, app_key.new_key, 16);
2011 if (net_key.phase != KEY_REFRESH_PHASE_ONE)
2012 return dbus_error(msg, MESH_ERROR_FAILED,
2014 data[0] = OP_APPKEY_UPDATE;
2015 memcpy(data + 4, app_key.new_key, 16);
2018 /* Pack bound NetKey and AppKey into 3 octets */
2019 data[1] = app_key.net_idx;
2020 data[2] = ((app_key.net_idx >> 8) & 0xf) | ((app_idx << 4) & 0xf0);
2021 data[3] = app_idx >> 4;
2023 if (!mesh_model_send(node, src, dst, APP_IDX_DEV_REMOTE, net_idx,
2024 DEFAULT_TTL, false, 20, data))
2025 return dbus_error(msg, MESH_ERROR_NOT_FOUND, NULL);
2027 return l_dbus_message_new_method_return(msg);
2030 static struct l_dbus_message *publish_call(struct l_dbus *dbus,
2031 struct l_dbus_message *msg,
2034 struct mesh_node *node = user_data;
2035 const char *sender, *ele_path;
2036 struct l_dbus_message_iter iter_data, dict;
2037 uint16_t mod_id, src;
2038 struct send_options opts;
2039 struct node_element *ele;
2046 sender = l_dbus_message_get_sender(msg);
2048 if (strcmp(sender, node->owner))
2049 return dbus_error(msg, MESH_ERROR_NOT_AUTHORIZED, NULL);
2051 if (!l_dbus_message_get_arguments(msg, "oqa{sv}ay", &ele_path, &mod_id,
2053 return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
2055 ele = l_queue_find(node->elements, match_element_path, ele_path);
2057 return dbus_error(msg, MESH_ERROR_NOT_FOUND,
2058 "Element not found");
2060 if (!parse_send_options(&dict, &opts))
2061 return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
2063 src = node_get_primary(node) + ele->idx;
2065 if (!l_dbus_message_iter_get_fixed_array(&iter_data, &data, &len) ||
2066 !len || len > MAX_MSG_LEN)
2067 return dbus_error(msg, MESH_ERROR_INVALID_ARGS,
2070 id = SET_ID(opts.vendor_id, mod_id);
2072 result = mesh_model_publish(node, id, src, opts.segmented, len, data);
2074 if (result != MESH_ERROR_NONE)
2075 return dbus_error(msg, result, NULL);
2077 return l_dbus_message_new_method_return(msg);
2080 static bool features_getter(struct l_dbus *dbus, struct l_dbus_message *msg,
2081 struct l_dbus_message_builder *builder,
2084 struct mesh_node *node = user_data;
2085 uint8_t friend = node_friend_mode_get(node);
2086 uint8_t lpn = node_lpn_mode_get(node);
2087 uint8_t proxy = node_proxy_mode_get(node);
2090 uint8_t relay = node_relay_mode_get(node, &count, &interval);
2092 l_dbus_message_builder_enter_array(builder, "{sv}");
2094 if (friend != MESH_MODE_UNSUPPORTED)
2095 dbus_append_dict_entry_basic(builder, "Friend", "b", &friend);
2097 if (lpn != MESH_MODE_UNSUPPORTED)
2098 dbus_append_dict_entry_basic(builder, "LowPower", "b", &lpn);
2100 if (proxy != MESH_MODE_UNSUPPORTED)
2101 dbus_append_dict_entry_basic(builder, "Proxy", "b", &proxy);
2103 if (relay != MESH_MODE_UNSUPPORTED)
2104 dbus_append_dict_entry_basic(builder, "Relay", "b", &relay);
2106 l_dbus_message_builder_leave_array(builder);
2111 static bool beacon_getter(struct l_dbus *dbus, struct l_dbus_message *msg,
2112 struct l_dbus_message_builder *builder,
2115 struct mesh_node *node = user_data;
2116 bool beacon_mode = node_beacon_mode_get(node) == MESH_MODE_ENABLED;
2118 l_dbus_message_builder_append_basic(builder, 'b', &beacon_mode);
2123 static bool ivupdate_getter(struct l_dbus *dbus, struct l_dbus_message *msg,
2124 struct l_dbus_message_builder *builder,
2127 struct mesh_node *node = user_data;
2128 struct mesh_net *net = node_get_net(node);
2133 mesh_net_get_snb_state(net, &flags, &iv_index);
2135 ivu = flags & IV_INDEX_UPDATE;
2137 l_dbus_message_builder_append_basic(builder, 'b', &ivu);
2142 static bool ivindex_getter(struct l_dbus *dbus, struct l_dbus_message *msg,
2143 struct l_dbus_message_builder *builder,
2146 struct mesh_node *node = user_data;
2147 struct mesh_net *net = node_get_net(node);
2151 mesh_net_get_snb_state(net, &flags, &iv_index);
2153 l_dbus_message_builder_append_basic(builder, 'u', &iv_index);
2158 static bool seq_num_getter(struct l_dbus *dbus, struct l_dbus_message *msg,
2159 struct l_dbus_message_builder *builder,
2162 struct mesh_node *node = user_data;
2163 struct mesh_net *net = node_get_net(node);
2164 uint32_t seq_nr = mesh_net_get_seq_num(net);
2166 l_dbus_message_builder_append_basic(builder, 'u', &seq_nr);
2171 static bool lastheard_getter(struct l_dbus *dbus, struct l_dbus_message *msg,
2172 struct l_dbus_message_builder *builder,
2175 struct mesh_node *node = user_data;
2176 struct mesh_net *net = node_get_net(node);
2178 uint32_t last_heard;
2180 gettimeofday(&now, NULL);
2182 last_heard = now.tv_sec - mesh_net_get_instant(net);
2184 l_dbus_message_builder_append_basic(builder, 'u', &last_heard);
2190 static bool addresses_getter(struct l_dbus *dbus, struct l_dbus_message *msg,
2191 struct l_dbus_message_builder *builder,
2194 struct mesh_node *node = user_data;
2195 const struct l_queue_entry *entry;
2197 l_dbus_message_builder_enter_array(builder, "q");
2199 entry = l_queue_get_entries(node->elements);
2200 for (; entry; entry = entry->next) {
2201 const struct node_element *ele = entry->data;
2202 uint16_t address = node->primary + ele->idx;
2204 l_dbus_message_builder_append_basic(builder, 'q', &address);
2207 l_dbus_message_builder_leave_array(builder);
2212 static void setup_node_interface(struct l_dbus_interface *iface)
2214 l_dbus_interface_method(iface, "Send", 0, send_call, "", "oqqa{sv}ay",
2215 "element_path", "destination",
2216 "key_index", "options", "data");
2217 l_dbus_interface_method(iface, "DevKeySend", 0, dev_key_send_call, "",
2218 "oqbqa{sv}ay", "element_path",
2219 "destination", "remote",
2220 "net_index", "options", "data");
2221 l_dbus_interface_method(iface, "AddNetKey", 0, add_netkey_call, "",
2222 "oqqqb", "element_path", "destination",
2223 "subnet_index", "net_index", "update");
2224 l_dbus_interface_method(iface, "AddAppKey", 0, add_appkey_call, "",
2225 "oqqqb", "element_path", "destination",
2226 "app_index", "net_index", "update");
2227 l_dbus_interface_method(iface, "Publish", 0, publish_call, "",
2228 "oqa{sv}ay", "element_path", "model_id",
2230 l_dbus_interface_property(iface, "Features", 0, "a{sv}",
2231 features_getter, NULL);
2232 l_dbus_interface_property(iface, "Beacon", 0, "b", beacon_getter, NULL);
2233 l_dbus_interface_property(iface, "IvUpdate", 0, "b", ivupdate_getter,
2235 l_dbus_interface_property(iface, "IvIndex", 0, "u", ivindex_getter,
2237 l_dbus_interface_property(iface, "SequenceNumber", 0, "u",
2238 seq_num_getter, NULL);
2239 l_dbus_interface_property(iface, "SecondsSinceLastHeard", 0, "u",
2240 lastheard_getter, NULL);
2241 l_dbus_interface_property(iface, "Addresses", 0, "aq", addresses_getter,
2245 void node_property_changed(struct mesh_node *node, const char *property)
2247 struct l_dbus *bus = dbus_get_bus();
2249 if (bus && node->obj_path)
2250 l_dbus_property_changed(dbus_get_bus(), node->obj_path,
2251 MESH_NODE_INTERFACE, property);
2254 bool node_dbus_init(struct l_dbus *bus)
2256 if (!l_dbus_register_interface(bus, MESH_NODE_INTERFACE,
2257 setup_node_interface,
2259 l_info("Unable to register %s interface", MESH_NODE_INTERFACE);
2266 const char *node_get_owner(struct mesh_node *node)
2271 const char *node_get_element_path(struct mesh_node *node, uint8_t ele_idx)
2273 struct node_element *ele;
2275 ele = l_queue_find(node->elements, match_element_idx,
2276 L_UINT_TO_PTR(ele_idx));
2284 bool node_add_pending_local(struct mesh_node *node, void *prov_node_info)
2286 struct mesh_prov_node_info *info = prov_node_info;
2287 bool kr = !!(info->flags & PROV_FLAG_KR);
2288 bool ivu = !!(info->flags & PROV_FLAG_IVU);
2290 return add_local_node(node, info->unicast, kr, ivu, info->iv_index,
2291 info->device_key, info->net_index, info->net_key);
2294 struct mesh_config *node_config_get(struct mesh_node *node)
2299 const char *node_get_storage_dir(struct mesh_node *node)
2301 return node->storage_dir;
2304 const char *node_get_app_path(struct mesh_node *node)
2309 return node->app_path;
2312 struct mesh_net *node_get_net(struct mesh_node *node)
2317 struct mesh_agent *node_get_agent(struct mesh_node *node)
2322 bool node_load_from_storage(const char *storage_dir)
2324 return mesh_config_load_nodes(storage_dir, init_from_storage, NULL);
2328 * This is called for a new node that:
2329 * - has been created as a result of successful completion of Join()
2330 * or Create() or Import() methods
2332 * - has been confirmed via successful token delivery to the application
2334 * After a node has been created, the information gathered during initial
2335 * GetManagedObjects() call is cleared. The subsequent call to Attach() would
2336 * verify node's integrity and re-initialize node's D-Bus resources.
2338 void node_finalize_new_node(struct mesh_node *node, struct mesh_io *io)
2343 free_node_dbus_resources(node);
2344 mesh_agent_remove(node->agent);
2349 /* Register callback for the node's io */
2350 attach_io(node, io);