4 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6 * @author: Anupam Roy <anupam.r@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
32 #include "bt-service-common.h"
33 #include "bt-service-core-adapter.h"
34 #include "bt-service-event-receiver.h"
35 #include "bt-request-handler.h"
36 #include "bluetooth-api.h"
38 #include "bluetooth-api.h"
39 #include "bluetooth-mesh-api.h"
40 #include "bt-internal-types.h"
41 #include "bt-service-util.h"
42 #include "bt-service-common.h"
43 #include "bt-service-core-adapter.h"
44 #include "bt-service-event-receiver.h"
45 #include "bt-request-handler.h"
46 #include "bluetooth-api.h"
48 #include "bluetooth-api.h"
49 #include "bluetooth-mesh-api.h"
50 #include "bt-internal-types.h"
51 #include "bt-service-util.h"
52 #include "bt-service-common.h"
53 #include "bt-service-event.h"
55 #include "bt-service-mesh-network.h"
56 #include "bt-service-mesh-cdb.h"
57 #include "bt-service-mesh-nodes.h"
58 #include "bt-service-mesh-keys.h"
59 #include "bt-service-mesh-util.h"
61 #include <oal-hardware.h>
62 #include <oal-manager.h>
63 #include <oal-event.h>
64 #include <oal-adapter-mgr.h>
65 #include <oal-device-mgr.h>
68 #include "bt-internal-types.h"
70 #define MESH_CONFIG_BUFFER_MAX_LEN 100
73 static void __bt_mesh_send_node_browsed_event(int result,
74 bluetooth_mesh_node_discover_t *browse_evt);
75 static void __bt_mesh_handle_pending_dev_config_request_info(int result,
76 int service_function, void *param,
79 struct mesh_config_cmd {
82 const char *descriptor;
85 static struct l_queue *pending_requests;
87 struct mesh_pending_request {
88 struct l_timeout *timer;
89 const struct mesh_config_cmd *cmd;
95 static struct mesh_config_cmd commands[] = {
96 { MESH_OPCODE_APPKEY_ADD, MESH_OPCODE_APPKEY_STATUS, "AppKeyAdd" },
97 { MESH_OPCODE_APPKEY_DELETE, MESH_OPCODE_APPKEY_STATUS, "AppKeyDelete" },
98 { MESH_OPCODE_APPKEY_GET, MESH_OPCODE_APPKEY_LIST, "AppKeyGet" },
99 { MESH_OPCODE_APPKEY_LIST, MESH_RESPONSE_NONE, "AppKeyList" },
100 { MESH_OPCODE_APPKEY_STATUS, MESH_RESPONSE_NONE, "AppKeyStatus" },
101 { MESH_OPCODE_APPKEY_UPDATE, MESH_OPCODE_APPKEY_STATUS, "AppKeyUpdate" },
102 { MESH_OPCODE_DEV_COMP_GET, MESH_OPCODE_DEV_COMP_STATUS, "DeviceCompositionGet" },
103 { MESH_OPCODE_DEV_COMP_STATUS, MESH_RESPONSE_NONE, "DeviceCompositionStatus" },
104 { MESH_OPCODE_CONFIG_BEACON_GET, MESH_OPCODE_CONFIG_BEACON_STATUS, "BeaconGet" },
105 { MESH_OPCODE_CONFIG_BEACON_SET, MESH_OPCODE_CONFIG_BEACON_STATUS, "BeaconSet" },
106 { MESH_OPCODE_CONFIG_BEACON_STATUS, MESH_RESPONSE_NONE, "BeaconStatus" },
107 { MESH_OPCODE_CONFIG_DEFAULT_TTL_GET, MESH_OPCODE_CONFIG_DEFAULT_TTL_STATUS,
109 { MESH_OPCODE_CONFIG_DEFAULT_TTL_SET, MESH_OPCODE_CONFIG_DEFAULT_TTL_STATUS,
111 { MESH_OPCODE_CONFIG_DEFAULT_TTL_STATUS, MESH_RESPONSE_NONE, "DefaultTTLStatus" },
112 { MESH_OPCODE_CONFIG_FRIEND_GET, MESH_OPCODE_CONFIG_FRIEND_STATUS, "FriendGet" },
113 { MESH_OPCODE_CONFIG_FRIEND_SET, MESH_OPCODE_CONFIG_FRIEND_STATUS, "FrienSet" },
114 { MESH_OPCODE_CONFIG_FRIEND_STATUS, MESH_RESPONSE_NONE, "FriendStatus" },
115 { MESH_OPCODE_CONFIG_PROXY_GET, MESH_OPCODE_CONFIG_PROXY_STATUS, "ProxyGet" },
116 { MESH_OPCODE_CONFIG_PROXY_SET, MESH_OPCODE_CONFIG_PROXY_STATUS, "ProxySet" },
117 { MESH_OPCODE_CONFIG_PROXY_STATUS, MESH_RESPONSE_NONE, "ProxyStatus" },
118 { MESH_OPCODE_CONFIG_KEY_REFRESH_PHASE_GET, MESH_OPCODE_CONFIG_KEY_REFRESH_PHASE_STATUS,
119 "KeyRefreshPhaseGet" },
120 { MESH_OPCODE_CONFIG_KEY_REFRESH_PHASE_SET, MESH_OPCODE_CONFIG_KEY_REFRESH_PHASE_STATUS,
121 "KeyRefreshPhaseSet" },
122 { MESH_OPCODE_CONFIG_KEY_REFRESH_PHASE_STATUS, MESH_RESPONSE_NONE,
123 "KeyRefreshPhaseStatus" },
124 { MESH_OPCODE_CONFIG_MODEL_PUB_GET, MESH_OPCODE_CONFIG_MODEL_PUB_STATUS, "ModelPubGet" },
125 { MESH_OPCODE_CONFIG_MODEL_PUB_SET, MESH_OPCODE_CONFIG_MODEL_PUB_STATUS, "ModelPubSet" },
126 { MESH_OPCODE_CONFIG_MODEL_PUB_STATUS, MESH_RESPONSE_NONE, "ModelPubStatus" },
127 { MESH_OPCODE_CONFIG_MODEL_PUB_VIRT_SET, MESH_OPCODE_CONFIG_MODEL_PUB_STATUS,
128 "ModelPubVirtualSet" },
129 { MESH_OPCODE_CONFIG_MODEL_SUB_ADD, MESH_OPCODE_CONFIG_MODEL_SUB_STATUS, "ModelSubAdd" },
130 { MESH_OPCODE_CONFIG_MODEL_SUB_DELETE, MESH_OPCODE_CONFIG_MODEL_SUB_STATUS,
132 { MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL, MESH_OPCODE_CONFIG_MODEL_SUB_STATUS,
133 "ModelSubDeleteAll" },
134 { MESH_OPCODE_CONFIG_MODEL_SUB_OVERWRITE, MESH_OPCODE_CONFIG_MODEL_SUB_STATUS,
135 "ModelSubOverwrite" },
136 { MESH_OPCODE_CONFIG_MODEL_SUB_STATUS, MESH_RESPONSE_NONE, "ModelSubStatus" },
137 { MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_ADD, MESH_OPCODE_CONFIG_MODEL_SUB_STATUS,
139 { MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_DELETE, MESH_OPCODE_CONFIG_MODEL_SUB_STATUS,
140 "ModelSubVirtDelete" },
141 { MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_OVERWRITE, MESH_OPCODE_CONFIG_MODEL_SUB_STATUS,
142 "ModelSubVirtOverwrite" },
143 { MESH_OPCODE_CONFIG_NETWORK_TRANSMIT_GET, MESH_OPCODE_CONFIG_NETWORK_TRANSMIT_STATUS,
144 "NetworkTransmitGet" },
145 { MESH_OPCODE_CONFIG_NETWORK_TRANSMIT_SET, MESH_OPCODE_CONFIG_NETWORK_TRANSMIT_STATUS,
146 "NetworkTransmitSet" },
147 { MESH_OPCODE_CONFIG_NETWORK_TRANSMIT_STATUS, MESH_RESPONSE_NONE,
148 "NetworkTransmitStatus" },
149 { MESH_OPCODE_CONFIG_RELAY_GET, MESH_OPCODE_CONFIG_RELAY_STATUS, "RelayGet" },
150 { MESH_OPCODE_CONFIG_RELAY_SET, MESH_OPCODE_CONFIG_RELAY_STATUS, "RelaySet" },
151 { MESH_OPCODE_CONFIG_RELAY_STATUS, MESH_RESPONSE_NONE, "RelayStatus" },
152 { MESH_OPCODE_CONFIG_MODEL_SUB_GET, MESH_OPCODE_CONFIG_MODEL_SUB_LIST, "ModelSubGet" },
153 { MESH_OPCODE_CONFIG_MODEL_SUB_LIST, MESH_RESPONSE_NONE, "ModelSubList" },
154 { MESH_OPCODE_CONFIG_VEND_MODEL_SUB_GET, MESH_OPCODE_CONFIG_VEND_MODEL_SUB_LIST,
155 "VendorModelSubGet" },
156 { MESH_OPCODE_CONFIG_VEND_MODEL_SUB_LIST, MESH_RESPONSE_NONE, "VendorModelSubList" },
157 { MESH_OPCODE_CONFIG_POLL_TIMEOUT_LIST, MESH_OPCODE_CONFIG_POLL_TIMEOUT_STATUS,
159 { MESH_OPCODE_CONFIG_POLL_TIMEOUT_STATUS, MESH_RESPONSE_NONE, "PollTimeoutStatus" },
160 { MESH_OPCODE_CONFIG_HEARTBEAT_PUB_GET, MESH_OPCODE_CONFIG_HEARTBEAT_PUB_STATUS,
162 { MESH_OPCODE_CONFIG_HEARTBEAT_PUB_SET, MESH_OPCODE_CONFIG_HEARTBEAT_PUB_STATUS,
164 { MESH_OPCODE_CONFIG_HEARTBEAT_PUB_STATUS, MESH_RESPONSE_NONE, "HeartbeatPubStatus" },
165 { MESH_OPCODE_CONFIG_HEARTBEAT_SUB_GET, MESH_OPCODE_CONFIG_HEARTBEAT_SUB_STATUS,
167 { MESH_OPCODE_CONFIG_HEARTBEAT_SUB_SET, MESH_OPCODE_CONFIG_HEARTBEAT_SUB_STATUS,
169 { MESH_OPCODE_CONFIG_HEARTBEAT_SUB_STATUS, MESH_RESPONSE_NONE, "HeartbeatSubStatus" },
170 { MESH_OPCODE_MODEL_APP_BIND, MESH_OPCODE_MODEL_APP_STATUS, "ModelAppBind" },
171 { MESH_OPCODE_MODEL_APP_STATUS, MESH_RESPONSE_NONE, "ModelAppStatus" },
172 { MESH_OPCODE_MODEL_APP_UNBIND, MESH_OPCODE_MODEL_APP_STATUS, "ModelAppUnbind" },
173 { MESH_OPCODE_NETKEY_ADD, MESH_OPCODE_NETKEY_STATUS, "NetKeyAdd" },
174 { MESH_OPCODE_NETKEY_DELETE, MESH_OPCODE_NETKEY_STATUS, "NetKeyDelete" },
175 { MESH_OPCODE_NETKEY_GET, MESH_OPCODE_NETKEY_LIST, "NetKeyGet" },
176 { MESH_OPCODE_NETKEY_LIST, MESH_RESPONSE_NONE, "NetKeyList" },
177 { MESH_OPCODE_NETKEY_STATUS, MESH_RESPONSE_NONE, "NetKeyStatus" },
178 { MESH_OPCODE_NETKEY_UPDATE, MESH_OPCODE_NETKEY_STATUS, "NetKeyUpdate" },
179 { MESH_OPCODE_NODE_IDENTITY_GET, MESH_OPCODE_NODE_IDENTITY_STATUS, "NodeIdentityGet" },
180 { MESH_OPCODE_NODE_IDENTITY_SET, MESH_OPCODE_NODE_IDENTITY_STATUS, "NodeIdentitySet" },
181 { MESH_OPCODE_NODE_IDENTITY_STATUS, MESH_RESPONSE_NONE, "NodeIdentityStatus" },
182 { MESH_OPCODE_NODE_RESET, MESH_OPCODE_NODE_RESET_STATUS, "NodeReset" },
183 { MESH_OPCODE_NODE_RESET_STATUS, MESH_RESPONSE_NONE, "NodeResetStatus" },
184 { MESH_OPCODE_MODEL_APP_GET, MESH_OPCODE_MODEL_APP_LIST, "ModelAppGet" },
185 { MESH_OPCODE_MODEL_APP_LIST, MESH_RESPONSE_NONE, "ModelAppList" },
186 { MESH_OPCODE_VENDOR_MODEL_APP_GET, MESH_OPCODE_VENDOR_MODEL_APP_LIST, "VendorModelAppGet" },
187 { MESH_OPCODE_VENDOR_MODEL_APP_LIST, MESH_RESPONSE_NONE, "VendorModelAppList" }
191 static const struct mesh_config_cmd *__mesh_get_command(uint32_t opcode)
195 for (n = 0; n < L_ARRAY_SIZE(commands); n++) {
196 if (opcode == commands[n].opcode)
203 static const char *__mesh_get_opcode_string(uint32_t opcode)
205 const struct mesh_config_cmd *cmd;
207 cmd = __mesh_get_command(opcode);
209 return "Unknown Command Received";
211 return cmd->descriptor;
214 static void __mesh_request_remove(void *a)
216 struct mesh_pending_request *req = a;
220 l_timeout_remove(req->timer);
224 static void __bt_mesh_wait_response_timeout(
225 struct l_timeout *timeout, void *user_data)
227 struct mesh_pending_request *req = user_data;
229 BT_INFO("Mesh: No response for \"%s\" from %4.4x\n",
230 req->cmd->descriptor, req->addr);
232 /* Node reset case: delete the remote even if there is no response */
233 /* TODO Reset the remote node, as no response is expected on reset command */
236 switch(req->cmd->opcode) {
237 case MESH_OPCODE_DEV_COMP_GET: {
238 /* Send event with timeout */
239 event_mesh_devkey_message_t *event = \
240 g_malloc0(sizeof(event_mesh_devkey_message_t));
241 memcpy(event->net_uuid.uuid, req->net_uuid, 16);
242 event->source = req->addr;
243 __bt_mesh_handle_pending_dev_config_request_info(
244 BLUETOOTH_ERROR_TIMEOUT, BT_MESH_NODE_BROWSE,
245 event, sizeof(event_mesh_devkey_message_t));
249 case MESH_OPCODE_NETKEY_ADD:
250 case MESH_OPCODE_NETKEY_UPDATE:
251 case MESH_OPCODE_NETKEY_DELETE:
252 case MESH_OPCODE_APPKEY_ADD:
253 case MESH_OPCODE_APPKEY_UPDATE:
254 case MESH_OPCODE_APPKEY_DELETE:
255 /* Send event with timeout */
256 __bt_mesh_handle_pending_dev_config_request_info(
257 BLUETOOTH_ERROR_TIMEOUT,
258 BT_MESH_NODE_CONFIGURE_KEY, req->data,
259 sizeof(bluetooth_mesh_key_configure_t));
261 case MESH_OPCODE_CONFIG_DEFAULT_TTL_GET:
262 case MESH_OPCODE_CONFIG_DEFAULT_TTL_SET:
263 /* Send event with timeout */
264 __bt_mesh_handle_pending_dev_config_request_info(
265 BLUETOOTH_ERROR_TIMEOUT,
266 BT_MESH_NODE_TTL_EXECUTE, req->data,
267 sizeof(bluetooth_mesh_node_ttl_info_t));
272 BT_INFO("Mesh: Number of pending requests [%u] Remove the req",
273 l_queue_length(pending_requests));
274 l_queue_remove(pending_requests, req);
275 __mesh_request_remove(req);
278 static void __bt_mesh_add_request(uint32_t opcode, uint16_t dest,
279 uint8_t net_uuid[], void *data)
281 struct mesh_pending_request *req;
282 const struct mesh_config_cmd *cmd;
285 cmd = __mesh_get_command(opcode);
288 _bt_mesh_util_convert_hex_to_string((uint8_t *) net_uuid, 16, uuid_str,
289 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
290 BT_INFO("Mesh: Net UUID[%s]",uuid_str);
292 BT_INFO("Mesh: Adding command opcode [0x%2.2x] response [0x%2.2x]",
293 cmd->opcode, cmd->response);
294 req = l_new(struct mesh_pending_request, 1);
298 memcpy(req->net_uuid, net_uuid, 16);
299 req->timer = l_timeout_create(MESH_DEFAULT_RESPONSE_TIMEOUT,
300 __bt_mesh_wait_response_timeout, req, NULL);
302 if (!pending_requests)
303 pending_requests = l_queue_new();
304 l_queue_push_tail(pending_requests, req);
305 BT_INFO("Mesh: Number of pending requests [%u]", l_queue_length(pending_requests));
308 static struct mesh_pending_request *__bt_mesh_get_request_by_response(
309 uint16_t addr, uint8_t net_uuid[],
312 const struct l_queue_entry *entry;
316 BT_INFO("Mesh: Number of pending requests [%u]", l_queue_length(pending_requests));
317 entry = l_queue_get_entries(pending_requests);
319 for (; entry; entry = entry->next) {
320 struct mesh_pending_request *req = entry->data;
323 BT_INFO("Mesh: Req addr [0x%2.2x] req opcode [0x%2.2x] res [0x%2.2x]", req->addr, req->cmd->opcode, req->cmd->response);
324 BT_INFO("Mesh: Current req addr [0x%2.2x] res [0x%2.2x]", addr, response);
325 _bt_mesh_util_convert_hex_to_string((uint8_t *) net_uuid, 16, uuid_str,
326 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
327 BT_INFO("Mesh: Net UUID[%s]",uuid_str);
329 _bt_mesh_util_convert_hex_to_string((uint8_t *) req->net_uuid, 16, uuid_str1,
330 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
331 BT_INFO("Mesh: Net UUID1[%s]",uuid_str1);
332 if (!memcmp(net_uuid, req->net_uuid, 16) &&
334 req->cmd->response == response)
341 bool _bt_mesh_check_pending_request(uint32_t opcode,
342 uint16_t dest, uint8_t net_uuid[])
344 const struct mesh_config_cmd *cmd;
345 cmd = __mesh_get_command(opcode);
350 if (__bt_mesh_get_request_by_response(dest,
351 net_uuid, cmd->response)) {
352 BT_ERR("Mesh:Another command is pending\n");
358 static uint32_t __bt_mesh_print_model_identifier(uint8_t *data,
359 bool vendor, const char *offset)
364 mod_id = l_get_le16(data);
365 BT_INFO("%sModel ID\t%4.4x\n", offset, mod_id);
366 mod_id = MESH_VENDOR_ID_MASK | mod_id;
368 mod_id = l_get_le16(data + 2);
369 BT_INFO("%sModel ID\t%4.4x %4.4x\n", offset,
370 l_get_le16(data), mod_id);
371 mod_id = l_get_le16(data) << 16 | mod_id;
377 static void __bt_mesh_print_device_composition_data(
378 uint8_t *data, uint16_t len)
383 BT_INFO("Mesh: Received composion:\n");
385 /* skip page -- We only support Page Zero */
389 BT_INFO("\tCID: %4.4x", l_get_le16(&data[0]));
390 BT_INFO("\tPID: %4.4x", l_get_le16(&data[2]));
391 BT_INFO("\tVID: %4.4x", l_get_le16(&data[4]));
392 BT_INFO("\tCRPL: %4.4x", l_get_le16(&data[6]));
394 features = l_get_le16(&data[8]);
398 BT_INFO("\tFeature support:\n");
399 BT_INFO("\t\trelay: %s\n", (features & MESH_FEATURE_RELAY) ?
401 BT_INFO("\t\tproxy: %s\n", (features & MESH_FEATURE_PROXY) ?
403 BT_INFO("\t\tfriend: %s\n", (features & MESH_FEATURE_FRIEND) ?
405 BT_INFO("\t\tlpn: %s\n", (features & MESH_FEATURE_LPN) ?
410 BT_INFO("\t Element %d:\n", i);
411 BT_INFO("\t\tlocation: %4.4x\n", l_get_le16(data));
420 BT_INFO("\t\tSIG defined models:\n");
422 while (len >= 2 && m--) {
423 __bt_mesh_print_model_identifier(data, false, "\t\t ");
429 BT_INFO("\t\t Vendor defined models:\n");
431 while (len >= 4 && v--) {
432 __bt_mesh_print_model_identifier(data, true, "\t\t ");
441 static void __bt_mesh_send_model_publication_status_event(
442 int event, int result,
443 bluetooth_mesh_model_configure_t *evt)
445 GVariant *out_var = NULL, *param = NULL;
448 if (BLUETOOTH_ERROR_NONE == result) {
450 info = g_array_new(FALSE, FALSE, sizeof(gchar));
451 g_array_append_vals(info, evt,
452 sizeof(bluetooth_mesh_model_configure_t));
454 out_var = g_variant_new_from_data((const GVariantType *)"ay",
455 info->data, info->len,
458 param = g_variant_new("(iv)", result, out_var);
459 _bt_send_event(BT_MESH_EVENT, event,
464 static void __bt_mesh_send_model_subscription_configure_event(
465 int event, int result,
466 bluetooth_mesh_model_configure_t *evt)
468 GVariant *out_var = NULL, *param = NULL;
471 if (BLUETOOTH_ERROR_NONE == result) {
473 info = g_array_new(FALSE, FALSE, sizeof(gchar));
474 g_array_append_vals(info, evt,
475 sizeof(bluetooth_mesh_model_configure_t));
477 out_var = g_variant_new_from_data((const GVariantType *)"ay",
478 info->data, info->len,
481 param = g_variant_new("(iv)", result, out_var);
482 _bt_send_event(BT_MESH_EVENT, event,
487 static void __bt_mesh_send_model_get_subscription_list_event(
488 int result, bluetooth_mesh_model_configure_t *evt)
490 GVariant *param = NULL;
491 GVariantBuilder *builder = NULL;
494 if (BLUETOOTH_ERROR_NONE == result) {
495 if (evt->sublist_count) {
496 builder = g_variant_builder_new(G_VARIANT_TYPE("aq"));
497 for (i = 0; i < evt->sublist_count; i++)
498 g_variant_builder_add(builder, "q", *evt->sub_list[i]);
500 param = g_variant_new("(isqiui(aq))", result, evt->net_uuid,
501 evt->primary_unicast, evt->elem_index, evt->model,
502 evt->sublist_count, builder);
504 g_variant_builder_unref(builder);
507 _bt_send_event(BT_MESH_EVENT,
508 BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_LIST,
511 if (evt->sublist_count) {
513 for (int i = 0; i < evt->sublist_count; i++)
514 g_free(evt->sub_list[i]);
515 g_free(evt->sub_list);
520 static void __bt_mesh_send_model_get_appkey_list_event(int result,
521 bluetooth_mesh_model_configure_t *evt)
523 GVariant *param = NULL;
524 GVariantBuilder *builder = NULL;
527 if (BLUETOOTH_ERROR_NONE == result) {
529 if (evt->appkeylist_count) {
530 builder = g_variant_builder_new(G_VARIANT_TYPE("aq"));
531 for (i = 0; i < evt->appkeylist_count; i++)
532 g_variant_builder_add(builder, "q", *evt->appkey_list[i]);
534 param = g_variant_new("(isqiui(aq))", result, evt->net_uuid,
535 evt->primary_unicast, evt->elem_index, evt->model,
536 evt->appkeylist_count, builder);
538 g_variant_builder_unref(builder);
542 _bt_send_event(BT_MESH_EVENT,
543 BLUETOOTH_EVENT_MESH_MODEL_APPKEY_LIST,
546 if (evt->appkeylist_count) {
548 for (int i = 0; i < evt->appkeylist_count; i++)
549 g_free(evt->appkey_list[i]);
550 g_free(evt->appkey_list);
555 static void __bt_mesh_send_model_configure_appkey_event(int result,
556 bluetooth_mesh_model_configure_t *evt)
558 GVariant *out_var = NULL, *param = NULL;
561 if (BLUETOOTH_ERROR_NONE == result) {
563 info = g_array_new(FALSE, FALSE, sizeof(gchar));
564 g_array_append_vals(info, evt, sizeof(bluetooth_mesh_model_configure_t));
566 out_var = g_variant_new_from_data((const GVariantType *)"ay",
567 info->data, info->len,
570 param = g_variant_new("(iv)", result, out_var);
571 _bt_send_event(BT_MESH_EVENT,
572 BLUETOOTH_EVENT_MESH_NODE_MODEL_APPKEY_BIND,
577 static void __bt_mesh_send_node_ttl_configuration_event(int result,
578 bluetooth_mesh_node_ttl_info_t *ttl_evt)
580 GVariant *out_var = NULL, *param = NULL;
583 if (BLUETOOTH_ERROR_NONE == result) {
585 info = g_array_new(FALSE, FALSE, sizeof(gchar));
586 g_array_append_vals(info, ttl_evt,
587 sizeof(bluetooth_mesh_node_ttl_info_t));
589 out_var = g_variant_new_from_data((const GVariantType *)"ay",
590 info->data, info->len,
593 param = g_variant_new("(iv)", result, out_var);
594 _bt_send_event(BT_MESH_EVENT,
595 BLUETOOTH_EVENT_MESH_NODE_TTL_CONFIGURED,
600 static void __bt_mesh_send_node_key_configuration_event(int result,
601 bluetooth_mesh_key_configure_t *key_evt)
603 GVariant *out_var = NULL, *param = NULL;
606 if (BLUETOOTH_ERROR_NONE == result) {
608 info = g_array_new(FALSE, FALSE, sizeof(gchar));
609 g_array_append_vals(info, key_evt,
610 sizeof(bluetooth_mesh_key_configure_t));
612 out_var = g_variant_new_from_data((const GVariantType *)"ay",
613 info->data, info->len,
616 param = g_variant_new("(iv)", result, out_var);
617 _bt_send_event(BT_MESH_EVENT,
618 BLUETOOTH_EVENT_MESH_NODE_KEY_CONFIGURED,
623 static void __bt_mesh_send_node_get_vendor_features_event(int result,
624 bluetooth_mesh_node_features_t *features_evt)
626 GVariant *out_var = NULL, *param = NULL;
629 if (BLUETOOTH_ERROR_NONE == result) {
631 info = g_array_new(FALSE, FALSE, sizeof(gchar));
632 g_array_append_vals(info, features_evt,
633 sizeof(bluetooth_mesh_node_features_t));
635 out_var = g_variant_new_from_data((const GVariantType *)"ay",
636 info->data, info->len,
639 param = g_variant_new("(iv)", result, out_var);
640 _bt_send_event(BT_MESH_EVENT,
641 BLUETOOTH_EVENT_MESH_NODE_VENDOR_FEATURES,
646 static void __bt_mesh_send_node_browsed_event(int result,
647 bluetooth_mesh_node_discover_t *browse_evt)
649 GVariant *out_var = NULL, *param = NULL;
652 if (BLUETOOTH_ERROR_NONE == result) {
654 info = g_array_new(FALSE, FALSE, sizeof(gchar));
655 g_array_append_vals(info, browse_evt,
656 sizeof(bluetooth_mesh_node_discover_t));
658 out_var = g_variant_new_from_data((const GVariantType *)"ay",
659 info->data, info->len,
662 param = g_variant_new("(iv)", result, out_var);
663 _bt_send_event(BT_MESH_EVENT,
664 BLUETOOTH_EVENT_MESH_NODE_BROWSED,
669 static void __bt_mesh_handle_pending_dev_config_request_info(int result,
670 int service_function, void *param, unsigned int size)
674 invocation_info_t *req_info = NULL;
676 for (l = _bt_get_invocation_list(); l != NULL; ) {
679 if (req_info == NULL ||
680 req_info->service_function != service_function)
683 switch (service_function) {
684 case BT_MESH_NODE_GET_VENDOR_FEATURES: {
685 bluetooth_mesh_node_features_t *event;
686 bluetooth_mesh_node_features_t *req;
688 event = (bluetooth_mesh_node_features_t*) param;
689 req = (bluetooth_mesh_node_features_t*)req_info->user_data;
691 BT_DBG("Request Sender: [%s]", req_info->sender);
692 /* Match Network and Remote Node unicast*/
693 if (!g_strcmp0(event->net_uuid, req->net_uuid) && event->unicast == req->unicast) {
694 event->unicast = req->unicast;
695 event->elem_count = req->elem_count;
698 __bt_mesh_send_node_get_vendor_features_event(result, event);
700 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
701 g_array_append_vals(out_param, event, sizeof(bluetooth_mesh_node_features_t));
703 /* Return DBUS Invocation*/
704 _bt_service_method_return(req_info->context, out_param, result);
705 _bt_free_info_from_invocation_list(req_info);
706 g_array_free(out_param, TRUE);
710 case BT_MESH_NODE_BROWSE: {
711 bluetooth_mesh_node_discover_t *node;
712 event_mesh_devkey_message_t *event;
713 uint16_t remote_addr;
715 uint8_t dev_uuid[16];
716 char net_uuid[BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1];
718 event = (event_mesh_devkey_message_t*) param;
719 node = (bluetooth_mesh_node_discover_t*)req_info->user_data;
720 BT_INFO("Mesh: Request Node UUID [%s]", node->dev_uuid);
721 BT_INFO("Mesh: Request Network UUID [%s]", node->net_uuid);
723 _bt_mesh_util_convert_hex_to_string((uint8_t *) event->net_uuid.uuid, 16, net_uuid,
724 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
726 _bt_mesh_util_convert_string_to_hex(node->dev_uuid, strlen(node->dev_uuid), dev_uuid, 16);
728 /* Get Unicast from pending request's Dev UUID and match with event */
729 if (_bt_mesh_node_get_unicast_from_dev_uuid(event->net_uuid.uuid, dev_uuid, &remote_addr)) {
731 BT_DBG("Request Sender: [%s]", req_info->sender);
732 /* Match Network and Remote Node unicast*/
733 if (!g_strcmp0(node->net_uuid, net_uuid) && remote_addr == event->source) {
734 _bt_mesh_node_get_element_count(event->net_uuid.uuid, remote_addr, &elem_count);
735 node->unicast = event->source;
736 node->count = elem_count;
738 __bt_mesh_send_node_browsed_event(result, node);
740 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
741 g_array_append_vals(out_param, node, sizeof(bluetooth_mesh_node_discover_t));
743 /* Return DBUS Invocation*/
744 _bt_service_method_return(req_info->context, out_param, result);
745 _bt_free_info_from_invocation_list(req_info);
746 g_array_free(out_param, TRUE);
752 case BT_MESH_NODE_CONFIGURE_KEY: {
753 bluetooth_mesh_key_configure_t *event;
754 bluetooth_mesh_key_configure_t *req;
756 event = (bluetooth_mesh_key_configure_t*) param;
757 req = (bluetooth_mesh_key_configure_t*)req_info->user_data;
759 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
760 req->primary_unicast == event->primary_unicast &&
761 req->is_netkey == event->is_netkey) {
763 __bt_mesh_send_node_key_configuration_event(result, event);
765 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
766 g_array_append_vals(out_param, event, sizeof(bluetooth_mesh_key_configure_t));
768 /* Return DBUS Invocation*/
769 _bt_service_method_return(req_info->context, out_param, result);
770 _bt_free_info_from_invocation_list(req_info);
771 g_array_free(out_param, TRUE);
775 case BT_MESH_NODE_TTL_EXECUTE: {
776 bluetooth_mesh_node_ttl_info_t *event;
777 bluetooth_mesh_node_ttl_info_t *req;
779 event = (bluetooth_mesh_node_ttl_info_t*) param;
780 req = (bluetooth_mesh_node_ttl_info_t*)req_info->user_data;
781 req->ttl = event->ttl;
783 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
784 req->unicast == event->unicast) {
786 __bt_mesh_send_node_ttl_configuration_event(result, req);
788 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
789 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_node_ttl_info_t));
791 /* Return DBUS Invocation*/
792 _bt_service_method_return(req_info->context, out_param, result);
793 _bt_free_info_from_invocation_list(req_info);
794 g_array_free(out_param, TRUE);
798 case BT_MESH_MODEL_CONFIGURE_APPKEY: {
799 bluetooth_mesh_model_configure_t *event;
800 bluetooth_mesh_model_configure_t *req;
802 event = (bluetooth_mesh_model_configure_t*) param;
803 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
805 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
806 req->primary_unicast == event->primary_unicast) {
808 __bt_mesh_send_model_configure_appkey_event(result, req);
810 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
811 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
813 /* Return DBUS Invocation*/
814 _bt_service_method_return(req_info->context, out_param, result);
815 _bt_free_info_from_invocation_list(req_info);
816 g_array_free(out_param, TRUE);
821 case BT_MESH_MODEL_GET_APPKEY_LIST: {
822 bluetooth_mesh_model_configure_t *event;
823 bluetooth_mesh_model_configure_t *req;
825 event = (bluetooth_mesh_model_configure_t*) param;
826 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
828 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
829 req->primary_unicast == event->primary_unicast) {
831 __bt_mesh_send_model_get_appkey_list_event(result, event);
833 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
834 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
836 /* Return DBUS Invocation*/
837 _bt_service_method_return(req_info->context, out_param, result);
838 _bt_free_info_from_invocation_list(req_info);
839 g_array_free(out_param, TRUE);
843 case BT_MESH_MODEL_GET_SUBSCRIPTION_LIST: {
844 bluetooth_mesh_model_configure_t *event;
845 bluetooth_mesh_model_configure_t *req;
847 event = (bluetooth_mesh_model_configure_t*) param;
848 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
850 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
851 req->primary_unicast == event->primary_unicast) {
853 __bt_mesh_send_model_get_subscription_list_event(result, event);
855 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
856 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
858 /* Return DBUS Invocation*/
859 _bt_service_method_return(req_info->context, out_param, result);
860 _bt_free_info_from_invocation_list(req_info);
861 g_array_free(out_param, TRUE);
865 case BT_MESH_MODEL_CONFIG_GROUP_SUB: {
866 bluetooth_mesh_model_configure_t *event;
867 bluetooth_mesh_model_configure_t *req;
869 event = (bluetooth_mesh_model_configure_t*) param;
870 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
872 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
873 req->primary_unicast == event->primary_unicast) {
875 req->sub_addr = event->sub_addr;
877 __bt_mesh_send_model_subscription_configure_event(
878 BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_CONFGURED,
881 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
882 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
884 /* Return DBUS Invocation*/
885 _bt_service_method_return(req_info->context, out_param, result);
886 _bt_free_info_from_invocation_list(req_info);
887 g_array_free(out_param, TRUE);
891 case BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB: {
892 bluetooth_mesh_model_configure_t *event;
893 bluetooth_mesh_model_configure_t *req;
895 event = (bluetooth_mesh_model_configure_t*) param;
896 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
898 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
899 req->primary_unicast == event->primary_unicast) {
901 __bt_mesh_send_model_subscription_configure_event( \
902 BLUETOOTH_EVENT_MESH_MODEL_VIRTUAL_SUBSCRIPTION_CONFGURED, \
905 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
906 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
908 /* Return DBUS Invocation*/
909 _bt_service_method_return(req_info->context, out_param, result);
910 _bt_free_info_from_invocation_list(req_info);
911 g_array_free(out_param, TRUE);
916 case BT_MESH_MODEL_GET_PUBLICATION: {
917 bluetooth_mesh_model_configure_t *event;
918 bluetooth_mesh_model_configure_t *req;
920 event = (bluetooth_mesh_model_configure_t*) param;
921 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
923 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
924 req->primary_unicast == event->primary_unicast) {
926 __bt_mesh_send_model_publication_status_event( \
927 BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS, \
930 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
931 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
933 /* Return DBUS Invocation*/
934 _bt_service_method_return(req_info->context, out_param, result);
935 _bt_free_info_from_invocation_list(req_info);
936 g_array_free(out_param, TRUE);
940 case BT_MESH_MODEL_SET_PUBLICATION: {
941 bluetooth_mesh_model_configure_t *event;
942 bluetooth_mesh_model_configure_t *req;
944 event = (bluetooth_mesh_model_configure_t*) param;
945 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
947 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
948 req->primary_unicast == event->primary_unicast) {
950 __bt_mesh_send_model_publication_status_event( \
951 BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS, \
954 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
955 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
957 /* Return DBUS Invocation*/
958 _bt_service_method_return(req_info->context, out_param, result);
959 _bt_free_info_from_invocation_list(req_info);
960 g_array_free(out_param, TRUE);
965 BT_DBG("Unknown function(%d)", service_function);
971 const char *__mesh_status_to_string(uint8_t err)
974 case MESH_STATUS_SUCCESS: return "Success";
975 case MESH_STATUS_INVALID_ADDRESS: return "Invalid Address";
976 case MESH_STATUS_INVALID_MODEL: return "Invalid Model";
977 case MESH_STATUS_INVALID_APPKEY: return "Invalid AppKey";
978 case MESH_STATUS_INVALID_NETKEY: return "Invalid NetKey";
979 case MESH_STATUS_INSUFF_RESOURCES: return "Insufficient Resources";
980 case MESH_STATUS_IDX_ALREADY_STORED: return "Key Idx Already Stored";
981 case MESH_STATUS_INVALID_PUB_PARAM: return "Invalid Publish Parameters";
982 case MESH_STATUS_NOT_SUB_MOD: return "Not a Subscribe Model";
983 case MESH_STATUS_STORAGE_FAIL: return "Storage Failure";
984 case MESH_STATUS_FEATURE_NO_SUPPORT: return "Feature Not Supported";
985 case MESH_STATUS_CANNOT_UPDATE: return "Cannot Update";
986 case MESH_STATUS_CANNOT_REMOVE: return "Cannot Remove";
987 case MESH_STATUS_CANNOT_BIND: return "Cannot bind";
988 case MESH_STATUS_UNABLE_CHANGE_STATE: return "Unable to change state";
989 case MESH_STATUS_CANNOT_SET: return "Cannot set";
990 case MESH_STATUS_UNSPECIFIED_ERROR: return "Unspecified error";
991 case MESH_STATUS_INVALID_BINDING: return "Invalid Binding";
993 default: return "Unknown";
997 static void __mesh_handle_model_subscription_event(int result,
998 bluetooth_mesh_model_configure_t *param,
999 const struct mesh_config_cmd *cmd)
1001 switch (cmd->opcode) {
1003 case MESH_OPCODE_CONFIG_MODEL_SUB_ADD:
1004 case MESH_OPCODE_CONFIG_MODEL_SUB_DELETE:
1005 case MESH_OPCODE_CONFIG_MODEL_SUB_OVERWRITE:
1006 case MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL:
1007 /* Model Bind/UnBind Event */
1008 __bt_mesh_handle_pending_dev_config_request_info(result,
1009 BT_MESH_MODEL_CONFIG_GROUP_SUB, ¶m,
1010 sizeof(bluetooth_mesh_model_configure_t));
1012 case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_ADD:
1013 case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_DELETE:
1014 case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_OVERWRITE:
1015 __bt_mesh_handle_pending_dev_config_request_info(result,
1016 BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB, ¶m,
1017 sizeof(bluetooth_mesh_model_configure_t));
1024 void _bt_mesh_config_client_devkey_msg_handler(
1025 event_mesh_devkey_message_t *event)
1028 const struct mesh_config_cmd *cmd;
1029 uint16_t data_len = event->data_len;
1030 uint8_t *data = event->data;
1031 int result = BLUETOOTH_ERROR_NONE;
1039 struct mesh_pending_request *req;
1041 if (_bt_mesh_util_opcode_get(data, data_len, &opcode, &n)) {
1042 BT_INFO("Mesh: Opcode of response data [0x%2.2x], actual data len [%d]", opcode, n);
1048 BT_INFO("Mesh: Received %s (len %u) opcode [0x%2.2x]",
1049 __mesh_get_opcode_string(opcode), data_len, opcode);
1051 req = __bt_mesh_get_request_by_response(event->source,
1052 event->net_uuid.uuid, (opcode & ~MESH_OPCODE_UNRELIABLE));
1054 BT_INFO("Mesh: Got Config Request");
1056 __mesh_request_remove(req);
1057 l_queue_remove(pending_requests, req);
1062 switch (opcode & ~MESH_OPCODE_UNRELIABLE) {
1065 case MESH_OPCODE_CONFIG_MODEL_PUB_STATUS: {
1066 if (data_len != 12 && data_len != 14)
1068 bluetooth_mesh_model_configure_t param;
1069 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1071 _bt_mesh_util_convert_hex_to_string((uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1072 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1074 BT_INFO("\nNode %4.4x Model Publication status %s\n",
1075 event->source, __mesh_status_to_string(data[0]));
1077 if (data[0] != MESH_STATUS_SUCCESS)
1080 /* Extract Element Address */
1081 ele_addr = l_get_le16(data + 1);
1083 /* Extract Model ID */
1084 if (data_len == 14) {
1086 mod_id = l_get_le16(data + 10 + 2);
1087 mod_id = l_get_le16(data) << 16 | mod_id;
1090 mod_id = l_get_le16(data + 10);
1091 mod_id = MESH_VENDOR_ID_MASK | mod_id;
1094 param.primary_unicast = event->source;
1095 param.elem_index = ele_addr - event->source;
1096 /* Extract Publish Address */
1097 param.pub_addr = l_get_le16(data + 3);
1099 /* Extract Appkey Index */
1100 param.appkey_idx = l_get_le16(data + 5);
1103 param.ttl = data[6];
1105 /* Extract Period */
1106 param.period = data[7];
1110 if (cmd->opcode == MESH_OPCODE_CONFIG_MODEL_PUB_GET)
1111 __bt_mesh_handle_pending_dev_config_request_info(result,
1112 BT_MESH_MODEL_GET_PUBLICATION, ¶m,
1113 sizeof(bluetooth_mesh_model_configure_t));
1115 __bt_mesh_handle_pending_dev_config_request_info(result,
1116 BT_MESH_MODEL_SET_PUBLICATION, ¶m,
1117 sizeof(bluetooth_mesh_model_configure_t));
1120 case MESH_OPCODE_CONFIG_MODEL_SUB_STATUS: {
1121 if (data_len != 7 && data_len != 9)
1123 bluetooth_mesh_model_configure_t param;
1124 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1126 _bt_mesh_util_convert_hex_to_string(
1127 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1128 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1130 BT_INFO("\nNode %4.4x Subscription status %s\n",
1131 event->source, __mesh_status_to_string(data[0]));
1133 ele_addr = l_get_le16(data + 1);
1134 addr = l_get_le16(data + 3);
1135 BT_INFO("Element Addr\t%4.4x\n", ele_addr);
1137 if (data_len == 9) {
1139 mod_id = l_get_le16(data + 5 + 2);
1140 mod_id = l_get_le16(data) << 16 | mod_id;
1143 mod_id = l_get_le16(data + 5);
1144 mod_id = MESH_VENDOR_ID_MASK | mod_id;
1147 param.primary_unicast = event->source;
1148 param.elem_index = ele_addr - event->source;
1149 /* Subscription address, unassigned address in case of Delete All command */
1150 param.sub_addr = addr;
1151 param.model = mod_id;
1152 BT_INFO("Subscr Addr\t%4.4x\n", addr);
1153 __mesh_handle_model_subscription_event(result, ¶m, cmd);
1156 case MESH_OPCODE_CONFIG_DEFAULT_TTL_STATUS: {
1159 bluetooth_mesh_node_ttl_info_t param;
1160 memset(¶m, 0x00, sizeof(bluetooth_mesh_node_ttl_info_t));
1162 BT_INFO("Node %4.4x Default TTL %d", event->source, data[0]);
1163 _bt_mesh_util_convert_hex_to_string(
1164 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1165 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1166 param.unicast = event->source;
1167 param.ttl = data[0];
1169 if (!_bt_mesh_network_save_remote_node_ttl(event->net_uuid.uuid,
1170 event->source, data[0])) {
1171 result = BLUETOOTH_ERROR_INTERNAL;
1172 BT_INFO("Failed to save node TTL");
1175 /* Remote Node TTL event */
1176 __bt_mesh_handle_pending_dev_config_request_info(result,
1177 BT_MESH_NODE_TTL_EXECUTE, ¶m,
1178 sizeof(bluetooth_mesh_node_ttl_info_t));
1181 case MESH_OPCODE_MODEL_APP_STATUS: {
1182 if (data_len != 7 && data_len != 9)
1184 bluetooth_mesh_model_configure_t param;
1185 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1187 _bt_mesh_util_convert_hex_to_string(
1188 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1189 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1190 BT_INFO("Node %4.4x: Model App status %s\n", event->source,
1191 __mesh_status_to_string(data[0]));
1192 elem_addr = l_get_le16(data + 1);
1193 app_idx = l_get_le16(data + 3);
1195 BT_INFO("Element Addr\t%4.4x", elem_addr);
1196 if (data_len == 9) {
1198 mod_id = l_get_le16(data + 5 + 2);
1199 mod_id = l_get_le16(data) << 16 | mod_id;
1202 mod_id = l_get_le16(data + 5);
1203 mod_id = MESH_VENDOR_ID_MASK | mod_id;
1206 param.primary_unicast = event->source;
1207 param.elem_index = elem_addr - event->source;
1208 param.appkey_idx = app_idx;
1209 param.model = mod_id;
1210 BT_INFO("AppIdx\t\t%u (0x%3.3x)\n ", app_idx, app_idx);
1212 /* Model Bind/UnBind Event */
1213 __bt_mesh_handle_pending_dev_config_request_info(result,
1214 BT_MESH_MODEL_CONFIGURE_APPKEY, ¶m,
1215 sizeof(bluetooth_mesh_model_configure_t));
1218 case MESH_OPCODE_MODEL_APP_LIST: {
1223 bluetooth_mesh_model_configure_t param;
1224 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1226 _bt_mesh_util_convert_hex_to_string(
1227 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1228 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1230 garray = g_array_new(FALSE, FALSE, sizeof(gchar));
1232 BT_INFO("\nNode %4.4x Model AppIdx status %s\n",
1233 event->source, __mesh_status_to_string(data[0]));
1235 BT_INFO("Element Addr\t%4.4x\n", l_get_le16(data + 1));
1236 BT_INFO("Model ID\t%4.4x\n", l_get_le16(data + 3));
1238 elem_addr = l_get_le16(data + 1);
1240 mod_id = l_get_le16(data + 3);
1241 mod_id = MESH_VENDOR_ID_MASK | mod_id;
1243 param.primary_unicast = event->source;
1244 param.elem_index = elem_addr - event->source;
1245 param.model = mod_id;
1250 while (data_len >= 3) {
1251 app_idx = l_get_le16(data) & 0xfff;
1252 g_array_append_vals(garray, &app_idx, sizeof(uint16_t));
1253 BT_INFO("\t%u (0x%3.3x)\n", app_idx, app_idx);
1255 app_idx = l_get_le16(data + 1) >> 4;
1256 g_array_append_vals(garray, &app_idx, sizeof(uint16_t));
1257 BT_INFO("\t%u (0x%3.3x)\n", app_idx, app_idx);
1262 if (data_len == 2) {
1263 app_idx = l_get_le16(data) & 0xfff;
1264 g_array_append_vals(garray, &app_idx, sizeof(uint16_t));
1265 BT_INFO("\t %u (0x%3.3x)\n", app_idx, app_idx);
1267 total = garray->len / sizeof(uint16_t);
1268 param.appkey_list = (uint16_t **)g_malloc0(sizeof(uint16_t*) * garray->len);
1269 param.appkeylist_count = total;
1270 for (int i = 0; i < total; i++) {
1271 param.appkey_list[i] = g_malloc(sizeof(uint16_t));
1272 *param.appkey_list[i] = g_array_index(garray, uint16_t, i);
1274 g_array_free(garray, TRUE);
1276 __bt_mesh_handle_pending_dev_config_request_info(result,
1277 BT_MESH_MODEL_GET_APPKEY_LIST, ¶m,
1278 sizeof(bluetooth_mesh_model_configure_t));
1281 case MESH_OPCODE_CONFIG_MODEL_SUB_LIST: {
1286 bluetooth_mesh_model_configure_t param;
1287 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1289 _bt_mesh_util_convert_hex_to_string((uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1290 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1292 BT_INFO("\nNode %4.4x BT SIG Model Subscription List status %s\n",
1293 event->source, __mesh_status_to_string(data[0]));
1295 BT_INFO("Element Addr\t%4.4x\n", l_get_le16(data + 1));
1296 elem_addr = l_get_le16(data + 1);
1299 mod_id = l_get_le16(data + 3);
1300 mod_id = MESH_VENDOR_ID_MASK | mod_id;
1301 BT_INFO("Model ID\t%4.4x\n", mod_id);
1303 param.primary_unicast = event->source;
1304 param.elem_index = elem_addr - event->source;
1305 param.model = mod_id;
1307 total = data_len - 5;
1308 param.sublist_count = total;
1310 param.sub_list = (uint16_t **)g_malloc0(sizeof(uint16_t*) * total);
1313 for (; i < data_len; i += 2) {
1314 BT_INFO("Subscription Addr \t\t%4.4x\n ", l_get_le16(data + i));
1315 param.sub_list[i] = g_malloc(sizeof(uint16_t));
1316 *param.sub_list[i] = l_get_le16(data + i);
1319 __bt_mesh_handle_pending_dev_config_request_info(result,
1320 BT_MESH_MODEL_GET_SUBSCRIPTION_LIST, ¶m,
1321 sizeof(bluetooth_mesh_model_configure_t));
1324 case MESH_OPCODE_CONFIG_VEND_MODEL_SUB_LIST: {
1329 bluetooth_mesh_model_configure_t param;
1330 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1332 _bt_mesh_util_convert_hex_to_string(
1333 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1334 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1336 BT_INFO("\nNode %4.4x BT SIG Model Subscription List status %s\n",
1337 event->source, __mesh_status_to_string(data[0]));
1339 BT_INFO("Element Addr\t%4.4x\n", l_get_le16(data + 1));
1340 elem_addr = l_get_le16(data + 1);
1343 mod_id = l_get_le16(data + 5);
1344 mod_id = l_get_le16(data + 3) << 16 | mod_id;
1346 BT_INFO("Model ID\t%4.4x\n", mod_id);
1348 param.primary_unicast = event->source;
1349 param.elem_index = elem_addr - event->source;
1350 param.model = mod_id;
1352 total = data_len - 7;
1353 param.sublist_count = total;
1355 param.sub_list = (uint16_t **)g_malloc0(sizeof(uint16_t*) * total);
1358 for (; i < data_len; i += 2) {
1359 BT_INFO("Subscription Addr \t\t%4.4x\n ", l_get_le16(data + i));
1360 param.sub_list[i] = g_malloc(sizeof(uint16_t));
1361 *param.sub_list[i] = l_get_le16(data + i);
1364 __bt_mesh_handle_pending_dev_config_request_info(result,
1365 BT_MESH_MODEL_GET_SUBSCRIPTION_LIST, ¶m,
1366 sizeof(bluetooth_mesh_model_configure_t));
1369 case MESH_OPCODE_DEV_COMP_STATUS: {
1370 if (data_len < MESH_MINIMUM_COMPOSITION_LEN)
1372 bluetooth_mesh_node_features_t features;
1373 memset(&features, 0x00, sizeof(bluetooth_mesh_node_features_t));
1374 BT_INFO("Mesh: Got Response for Device Composition Data");
1375 __bt_mesh_print_device_composition_data(data, data_len);
1377 if (!_bt_mesh_network_save_remote_node_composition(
1378 event->net_uuid.uuid, event->source, data, data_len)) {
1379 result = BLUETOOTH_ERROR_INTERNAL;
1380 BT_INFO("Failed to save node composition!");
1382 /* Browse Remote Node event */
1383 __bt_mesh_handle_pending_dev_config_request_info(result,
1384 BT_MESH_NODE_BROWSE, event, sizeof(event_mesh_devkey_message_t));
1386 /* Vendor Features Discover event */
1387 _bt_mesh_util_convert_hex_to_string(
1388 (uint8_t *) event->net_uuid.uuid, 16, features.net_uuid,
1389 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1390 features.unicast = event->source;
1391 _bt_mesh_node_get_vendor_features(event->net_uuid.uuid,
1392 event->source, &features);
1394 __bt_mesh_handle_pending_dev_config_request_info(result,
1395 BT_MESH_NODE_GET_VENDOR_FEATURES, &features,
1396 sizeof(bluetooth_mesh_node_features_t));
1399 case MESH_OPCODE_NETKEY_STATUS: {
1403 bluetooth_mesh_key_configure_t param;
1404 memset(¶m, 0x00, sizeof(bluetooth_mesh_key_configure_t));
1406 BT_INFO("Mesh: Node %4.4x NetKey status %s",
1407 event->source, __mesh_status_to_string(data[0]));
1408 net_idx = l_get_le16(data + 1) & 0xfff;
1410 BT_INFO("\tNetKey %u (0x%3.3x)", net_idx, net_idx);
1418 _bt_mesh_util_convert_hex_to_string(
1419 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1420 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1421 param.primary_unicast = event->source;
1422 param.netkey_idx = net_idx;
1423 param.is_netkey = true;
1425 if (cmd->opcode == MESH_OPCODE_NETKEY_ADD) {
1426 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_NETKEY_ADD");
1427 if (!_bt_mesh_network_save_remote_node_netkey(
1428 event->net_uuid.uuid, event->source, net_idx)) {
1429 result = BLUETOOTH_ERROR_INTERNAL;
1430 BT_INFO("Failed to save node Netkey!");
1432 param.op = BLUETOOTH_MESH_NODE_KEY_ADD;
1433 } else if (cmd->opcode == MESH_OPCODE_NETKEY_DELETE) {
1434 if (!_bt_mesh_network_delete_remote_node_netkey(
1435 event->net_uuid.uuid, event->source, net_idx)) {
1436 result = BLUETOOTH_ERROR_INTERNAL;
1437 BT_INFO("Failed to delete node Netkey!");
1439 param.op = BLUETOOTH_MESH_NODE_KEY_DELETE;
1440 } else if (cmd->opcode == MESH_OPCODE_NETKEY_UPDATE) {
1441 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_NETKEY_UPDATE");
1442 param.op = BLUETOOTH_MESH_NODE_KEY_UPDATE;
1444 /* Node Net Key Configure event */
1445 __bt_mesh_handle_pending_dev_config_request_info(result,
1446 BT_MESH_NODE_CONFIGURE_KEY,
1447 ¶m, sizeof(bluetooth_mesh_key_configure_t));
1450 case MESH_OPCODE_APPKEY_STATUS: {
1454 bluetooth_mesh_key_configure_t param;
1455 memset(¶m, 0x00, sizeof(bluetooth_mesh_key_configure_t));
1457 BT_INFO("Mesh: Node %4.4x AppKey status %s\n", event->source,
1458 __mesh_status_to_string(data[0]));
1459 net_idx = l_get_le16(data + 1) & 0xfff;
1460 app_idx = l_get_le16(data + 2) >> 4;
1462 BT_INFO("NetKey\t%u (0x%3.3x)\n", net_idx, net_idx);
1463 BT_INFO("AppKey\t%u (0x%3.3x)\n", app_idx, app_idx);
1465 if (data[0] != MESH_STATUS_SUCCESS)
1471 _bt_mesh_util_convert_hex_to_string(
1472 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1473 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1475 param.primary_unicast = event->source;
1476 param.netkey_idx = net_idx;
1477 param.appkey_idx = app_idx;
1478 param.is_netkey = false;
1480 if (cmd->opcode == MESH_OPCODE_APPKEY_ADD) {
1481 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_APPKEY_ADD");
1482 if (!_bt_mesh_network_save_remote_node_appkey(
1483 event->net_uuid.uuid, event->source,
1484 net_idx, app_idx)) {
1485 result = BLUETOOTH_ERROR_INTERNAL;
1486 BT_INFO("Failed to save node Appkey!");
1488 param.op = BLUETOOTH_MESH_NODE_KEY_ADD;
1489 } else if (cmd->opcode == MESH_OPCODE_APPKEY_DELETE) {
1490 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_APPKEY_DELETE");
1491 if (!_bt_mesh_network_delete_remote_node_appkey(
1492 event->net_uuid.uuid, event->source,
1493 net_idx, app_idx)) {
1494 result = BLUETOOTH_ERROR_INTERNAL;
1495 BT_INFO("Failed to delete node Appkey!");
1497 param.op = BLUETOOTH_MESH_NODE_KEY_DELETE;
1498 } else if (cmd->opcode == MESH_OPCODE_APPKEY_UPDATE) {
1499 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_APPKEY_UPDATE");
1500 param.op = BLUETOOTH_MESH_NODE_KEY_UPDATE;
1503 /* Node App Key Configure event */
1504 __bt_mesh_handle_pending_dev_config_request_info(result,
1505 BT_MESH_NODE_CONFIGURE_KEY,
1506 ¶m, sizeof(bluetooth_mesh_key_configure_t));
1512 static gboolean __bt_mesh_vendor_feature_event_handler(gpointer data)
1514 bluetooth_mesh_node_features_t *result = (bluetooth_mesh_node_features_t*) data;
1515 __bt_mesh_handle_pending_dev_config_request_info(
1516 BLUETOOTH_ERROR_NONE,
1517 BT_MESH_NODE_GET_VENDOR_FEATURES,
1518 result, sizeof(bluetooth_mesh_node_features_t));
1523 int _bt_mesh_node_discover_vendor_features(const char *app_cred, const char *sender,
1524 bluetooth_mesh_node_features_t *req)
1526 int ret = OAL_STATUS_SUCCESS;
1528 uint16_t netkey_idx;
1530 oal_uuid_t net_uuid;
1531 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1533 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1534 strlen(req->net_uuid), net_uuid.uuid, 16);
1535 /* Check if Node's vendor features are already svaed or not */
1536 if (_bt_mesh_node_get_vendor_features(net_uuid.uuid, req->unicast, req)) {
1537 /* Schedule event ot Application */
1538 bluetooth_mesh_node_features_t *event = \
1539 g_memdup(req, sizeof(bluetooth_mesh_node_features_t));
1540 g_idle_add(__bt_mesh_vendor_feature_event_handler, (gpointer) event);
1541 return BLUETOOTH_ERROR_NONE;
1544 /* If Scanning is going on */
1545 if (_bt_mesh_is_provisioning() ||
1546 _bt_mesh_is_scanning()) {
1547 BT_ERR("Device is buzy..");
1548 return BLUETOOTH_ERROR_DEVICE_BUSY;
1551 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1552 strlen(req->net_uuid), net_uuid.uuid, 16);
1554 dest = req->unicast;
1556 /* Check pending request */
1557 if (_bt_mesh_check_pending_request(MESH_OPCODE_DEV_COMP_GET,
1558 dest, net_uuid.uuid)) {
1559 BT_ERR("Device is buzy..");
1560 return BLUETOOTH_ERROR_DEVICE_BUSY;
1563 /* Get Subnet index of the rmeote node for TX encryption */
1564 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, dest);
1565 if (netkey_idx == MESH_NET_IDX_INVALID)
1566 return BLUETOOTH_ERROR_INTERNAL;
1568 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_DEV_COMP_GET,
1571 /* By default, use page 0 */
1572 buffer[data_len++] = 0;
1573 ret = mesh_conf_send_message(&net_uuid, dest, true,
1574 netkey_idx, buffer, data_len);
1576 if (ret != OAL_STATUS_SUCCESS) {
1577 BT_ERR("ret: %d", ret);
1578 return BLUETOOTH_ERROR_INTERNAL;
1581 /* Queue the request with timeout */
1582 __bt_mesh_add_request(MESH_OPCODE_DEV_COMP_GET,
1583 dest, net_uuid.uuid, NULL);
1584 return BLUETOOTH_ERROR_NONE;
1587 int _bt_mesh_browse_remote_node(const char *app_cred,
1589 bluetooth_mesh_node_discover_t *req)
1591 int ret = OAL_STATUS_SUCCESS;
1593 uint16_t netkey_idx;
1595 oal_uuid_t net_uuid;
1596 oal_uuid_t dev_uuid;
1597 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1599 /* If Scanning is going on */
1600 if (_bt_mesh_is_provisioning() ||
1601 _bt_mesh_is_scanning()) {
1602 BT_ERR("Device is buzy..");
1603 return BLUETOOTH_ERROR_DEVICE_BUSY;
1606 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1607 strlen(req->net_uuid), net_uuid.uuid, 16);
1608 _bt_mesh_util_convert_string_to_hex(req->dev_uuid,
1609 strlen(req->dev_uuid), dev_uuid.uuid, 16);
1611 BT_INFO("Mesh: Browse Node UUID [%s]", req->dev_uuid);
1613 /* Get Remote Node unicast address from Dev UUID */
1614 if (!_bt_mesh_node_get_unicast_from_dev_uuid(net_uuid.uuid,
1615 dev_uuid.uuid, &dest))
1616 return BLUETOOTH_ERROR_INTERNAL;
1618 /* Check pending request */
1619 if (_bt_mesh_check_pending_request(MESH_OPCODE_DEV_COMP_GET,
1620 dest, net_uuid.uuid)) {
1621 BT_ERR("Device is buzy..");
1622 return BLUETOOTH_ERROR_DEVICE_BUSY;
1625 /* Get Subnet index of the rmeote node for TX encryption */
1626 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, dest);
1627 if (netkey_idx == MESH_NET_IDX_INVALID)
1628 return BLUETOOTH_ERROR_INTERNAL;
1630 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_DEV_COMP_GET, buffer);
1632 /* By default, use page 0 */
1633 buffer[data_len++] = 0;
1634 ret = mesh_conf_send_message(&net_uuid, dest, true,
1635 netkey_idx, buffer, data_len);
1637 if (ret != OAL_STATUS_SUCCESS) {
1638 BT_ERR("ret: %d", ret);
1639 return BLUETOOTH_ERROR_INTERNAL;
1642 /* Queue the request with timeout */
1643 __bt_mesh_add_request(MESH_OPCODE_DEV_COMP_GET,
1644 dest, net_uuid.uuid, NULL);
1645 return BLUETOOTH_ERROR_NONE;
1648 int _bt_mesh_model_configure_group_subscription(const char *app_cred,
1649 const char *sender, bluetooth_mesh_model_configure_t *req)
1651 int ret = OAL_STATUS_SUCCESS;
1652 uint16_t netkey_idx;
1653 oal_uuid_t net_uuid;
1655 uint32_t opcode = 0;
1656 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1658 /* If Scanning is going on */
1659 if (_bt_mesh_is_provisioning() ||
1660 _bt_mesh_is_scanning()) {
1661 BT_ERR("Device is buzy..");
1662 return BLUETOOTH_ERROR_DEVICE_BUSY;
1665 if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1666 /* Subscriptio address sanity check */
1667 if ((!MESH_IS_GROUP(req->sub_addr) ||
1668 MESH_IS_ALL_NODES(req->sub_addr)) ||
1669 MESH_IS_VIRTUAL(req->sub_addr)) {
1670 BT_ERR("Mesh: Bad subscription address %x\n",
1672 return BLUETOOTH_ERROR_INVALID_PARAM;
1676 /* Get Subnet index of the remote node for TX encryption */
1677 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid,
1678 req->primary_unicast);
1679 if (netkey_idx == MESH_NET_IDX_INVALID)
1680 return BLUETOOTH_ERROR_INTERNAL;
1682 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1683 strlen(req->net_uuid), net_uuid.uuid, 16);
1685 if (req->op == BLUETOOTH_MESH_MODEL_SUB_ADD)
1686 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_ADD;
1687 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE)
1688 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE;
1689 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL)
1690 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL;
1691 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_OVERWRITE)
1692 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_OVERWRITE;
1694 /* Check pending request */
1695 if (_bt_mesh_check_pending_request(opcode,
1696 req->primary_unicast, net_uuid.uuid)) {
1697 BT_ERR("Device is buzy..");
1698 return BLUETOOTH_ERROR_DEVICE_BUSY;
1701 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
1703 /* Element Address */
1704 l_put_le16((req->primary_unicast + req->elem_index),
1708 /* Subscription address */
1709 if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1710 l_put_le16(req->sub_addr, buffer + data_len);
1714 /* Insert Model ID */
1715 if (req->model >= 0xFFFF0000) {
1716 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
1717 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1720 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
1721 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
1723 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1727 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true,
1728 netkey_idx, buffer, data_len);
1730 if (ret != OAL_STATUS_SUCCESS) {
1731 BT_ERR("ret: %d", ret);
1732 return BLUETOOTH_ERROR_INTERNAL;
1735 /* Queue the request with timeout */
1736 __bt_mesh_add_request(opcode, req->primary_unicast,
1738 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
1740 return BLUETOOTH_ERROR_NONE;
1743 int _bt_mesh_model_configure_virtual_group_subscription(
1744 const char *app_cred, const char *sender,
1745 bluetooth_mesh_model_configure_t *req)
1747 int ret = OAL_STATUS_SUCCESS;
1748 uint16_t netkey_idx;
1749 oal_uuid_t net_uuid;
1751 uint32_t opcode = 0;
1752 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1754 /* If Scanning is going on */
1755 if (_bt_mesh_is_provisioning() ||
1756 _bt_mesh_is_scanning()) {
1757 BT_ERR("Device is buzy..");
1758 return BLUETOOTH_ERROR_DEVICE_BUSY;
1761 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1762 strlen(req->net_uuid), net_uuid.uuid, 16);
1764 if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1765 /* Subscription address sanity check */
1766 if ((MESH_IS_GROUP(req->sub_addr) ||
1767 MESH_IS_ALL_NODES(req->sub_addr)) ||
1768 !(MESH_IS_VIRTUAL(req->sub_addr))) {
1769 BT_ERR("Mesh: Bad Virtual subscription address %x\n",
1771 return BLUETOOTH_ERROR_INVALID_PARAM;
1775 /* Get Subnet index of the remote node for TX encryption */
1776 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid,
1777 req->primary_unicast);
1778 if (netkey_idx == MESH_NET_IDX_INVALID)
1779 return BLUETOOTH_ERROR_INTERNAL;
1781 if (req->op == BLUETOOTH_MESH_MODEL_SUB_ADD)
1782 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_ADD;
1783 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE)
1784 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE;
1785 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL)
1786 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL;
1787 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_OVERWRITE)
1788 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_OVERWRITE;
1790 /* Check pending request */
1791 if (_bt_mesh_check_pending_request(opcode,
1792 req->primary_unicast, net_uuid.uuid)) {
1793 BT_ERR("Device is buzy..");
1794 return BLUETOOTH_ERROR_DEVICE_BUSY;
1797 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
1799 /* Element Address */
1800 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
1803 /* Subscription address */
1804 if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1805 uint8_t label_uuid[16];
1806 if (!_bt_mesh_network_get_label_uuid_from_sub_addr(
1807 net_uuid.uuid, req->sub_addr, label_uuid)) {
1808 BT_ERR("Mesh: Virtual Group Label UUID Not found");
1809 return BLUETOOTH_ERROR_INVALID_PARAM;
1811 memcpy(buffer + data_len, label_uuid, 16);
1815 /* Insert Model ID */
1816 if (req->model >= 0xFFFF0000) {
1817 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
1818 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1821 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
1822 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
1824 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1828 ret = mesh_conf_send_message(&net_uuid,
1829 req->primary_unicast, true,
1830 netkey_idx, buffer, data_len);
1832 if (ret != OAL_STATUS_SUCCESS) {
1833 BT_ERR("ret: %d", ret);
1834 return BLUETOOTH_ERROR_INTERNAL;
1837 /* Queue the request with timeout */
1838 __bt_mesh_add_request(opcode, req->primary_unicast,
1840 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
1842 return BLUETOOTH_ERROR_NONE;
1845 int _bt_mesh_model_get_publication(const char *app_cred, const char *sender,
1846 bluetooth_mesh_model_configure_t *req)
1848 int ret = OAL_STATUS_SUCCESS;
1849 uint16_t netkey_idx;
1850 oal_uuid_t net_uuid;
1852 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1854 /* If Scanning is going on */
1855 if (_bt_mesh_is_provisioning() ||
1856 _bt_mesh_is_scanning()) {
1857 BT_ERR("Device is buzy..");
1858 return BLUETOOTH_ERROR_DEVICE_BUSY;
1861 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1862 strlen(req->net_uuid), net_uuid.uuid, 16);
1864 /* Check pending request */
1865 if (_bt_mesh_check_pending_request(MESH_OPCODE_CONFIG_MODEL_PUB_GET,
1866 req->primary_unicast, net_uuid.uuid)) {
1867 BT_ERR("Device is buzy..");
1868 return BLUETOOTH_ERROR_DEVICE_BUSY;
1871 /* Get Subnet index of the remote node for TX encryption */
1872 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
1873 if (netkey_idx == MESH_NET_IDX_INVALID)
1874 return BLUETOOTH_ERROR_INTERNAL;
1877 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_CONFIG_MODEL_PUB_GET, buffer);
1879 /* Element Address */
1880 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
1883 /* Insert Model ID */
1884 if (req->model >= 0xFFFF0000) {
1885 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
1886 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1889 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
1890 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
1892 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1896 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
1898 if (ret != OAL_STATUS_SUCCESS) {
1899 BT_ERR("ret: %d", ret);
1900 return BLUETOOTH_ERROR_INTERNAL;
1903 /* Queue the request with timeout */
1904 __bt_mesh_add_request(MESH_OPCODE_CONFIG_MODEL_PUB_GET, req->primary_unicast, net_uuid.uuid,
1905 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
1907 return BLUETOOTH_ERROR_NONE;
1910 int _bt_mesh_model_set_publication(const char *app_cred, const char *sender,
1911 bluetooth_mesh_model_configure_t *req)
1913 int ret = OAL_STATUS_SUCCESS;
1914 uint16_t netkey_idx;
1915 oal_uuid_t net_uuid;
1917 uint32_t opcode = 0;
1918 uint8_t label_uuid[16];
1919 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1921 /* If Scanning is going on */
1922 if (_bt_mesh_is_provisioning() ||
1923 _bt_mesh_is_scanning()) {
1924 BT_ERR("Device is buzy..");
1925 return BLUETOOTH_ERROR_DEVICE_BUSY;
1928 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
1930 if (req->pub_addr == MESH_ALL_NODES_ADDRESS)
1931 BT_INFO("Mesh: Setting Publication to ALL Node Address");
1933 if (!MESH_IS_GROUP(req->pub_addr) && !MESH_IS_VIRTUAL(req->pub_addr) &&
1934 req->pub_addr != MESH_UNASSIGNED_ADDRESS) {
1935 BT_ERR("Mesh: Bad Publication address %x\n", req->pub_addr);
1936 return BLUETOOTH_ERROR_INVALID_PARAM;
1939 if (req->pub_addr != MESH_UNASSIGNED_ADDRESS) {
1940 if (MESH_IS_VIRTUAL(req->pub_addr)) {
1941 if(!_bt_mesh_network_get_label_uuid_from_sub_addr(
1942 net_uuid.uuid, req->pub_addr, label_uuid))
1943 return BLUETOOTH_ERROR_INVALID_PARAM;
1944 opcode = MESH_OPCODE_CONFIG_MODEL_PUB_VIRT_SET;
1946 } else if (MESH_IS_GROUP(req->pub_addr))
1947 opcode = MESH_OPCODE_CONFIG_MODEL_PUB_SET;
1950 /* Check pending request */
1951 if (_bt_mesh_check_pending_request(opcode,
1952 req->primary_unicast, net_uuid.uuid)) {
1953 BT_ERR("Device is buzy..");
1954 return BLUETOOTH_ERROR_DEVICE_BUSY;
1957 /* Get Subnet index of the remote node for TX encryption */
1958 netkey_idx = _bt_mesh_node_get_subnet_idx(
1959 net_uuid.uuid, req->primary_unicast);
1960 if (netkey_idx == MESH_NET_IDX_INVALID)
1961 return BLUETOOTH_ERROR_INTERNAL;
1964 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
1966 /* Element Address */
1967 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
1970 /* Fill Publication Address */
1971 if (MESH_IS_VIRTUAL(req->pub_addr)) {
1972 memcpy(buffer + data_len, label_uuid, 16);
1975 l_put_le16(req->pub_addr, buffer + data_len);
1979 /* AppKey index + credential (set to 0) */
1980 l_put_le16(req->appkey_idx, buffer + data_len);
1984 buffer[data_len++] = req->ttl;
1985 /* Publish period step count and step resolution */
1986 buffer[data_len++] = req->period;
1987 /* Publish retransmit count & interval steps */
1988 buffer[data_len++] = req->retransmit;
1990 /* Insert Model ID */
1991 if (req->model >= 0xFFFF0000) {
1992 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
1993 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1996 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
1997 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
1999 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2003 ret = mesh_conf_send_message(&net_uuid,
2004 req->primary_unicast, true,
2005 netkey_idx, buffer, data_len);
2007 if (ret != OAL_STATUS_SUCCESS) {
2008 BT_ERR("ret: %d", ret);
2009 return BLUETOOTH_ERROR_INTERNAL;
2012 /* Queue the request with timeout */
2013 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2014 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2016 return BLUETOOTH_ERROR_NONE;
2019 int _bt_mesh_node_model_get_subscription_list(const char *app_cred, const char *sender,
2020 bluetooth_mesh_model_configure_t *req)
2022 int ret = OAL_STATUS_SUCCESS;
2023 uint16_t netkey_idx;
2024 oal_uuid_t net_uuid;
2026 uint32_t opcode = 0;
2027 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2029 /* If Scanning is going on */
2030 if (_bt_mesh_is_provisioning() ||
2031 _bt_mesh_is_scanning()) {
2032 BT_ERR("Device is buzy..");
2033 return BLUETOOTH_ERROR_DEVICE_BUSY;
2036 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2038 /* Insert Model ID */
2039 if (req->model >= 0xFFFF0000)
2040 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2041 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_GET;
2043 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2044 opcode = MESH_OPCODE_CONFIG_VEND_MODEL_SUB_GET;
2046 /* Check pending request */
2047 if (_bt_mesh_check_pending_request(opcode,
2048 req->primary_unicast, net_uuid.uuid)) {
2049 BT_ERR("Device is buzy..");
2050 return BLUETOOTH_ERROR_DEVICE_BUSY;
2052 /* Get Subnet index of the remote node for TX encryption */
2053 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2054 if (netkey_idx == MESH_NET_IDX_INVALID)
2055 return BLUETOOTH_ERROR_INTERNAL;
2057 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2058 /* Element Address */
2059 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2063 /* Insert Model ID */
2064 if (opcode == MESH_OPCODE_CONFIG_MODEL_SUB_GET) {
2065 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2066 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2069 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2070 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2072 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2076 ret = mesh_conf_send_message(&net_uuid,
2077 req->primary_unicast, true, netkey_idx, buffer, data_len);
2079 if (ret != OAL_STATUS_SUCCESS) {
2080 BT_ERR("ret: %d", ret);
2081 return BLUETOOTH_ERROR_INTERNAL;
2084 /* Queue the request with timeout */
2085 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2086 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2088 return BLUETOOTH_ERROR_NONE;
2091 int _bt_mesh_node_model_get_appkey_list(const char *app_cred, const char *sender,
2092 bluetooth_mesh_model_configure_t *req)
2094 int ret = OAL_STATUS_SUCCESS;
2095 uint16_t netkey_idx;
2096 oal_uuid_t net_uuid;
2098 uint32_t opcode = 0;
2099 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2101 /* If Scanning is going on */
2102 if (_bt_mesh_is_provisioning() ||
2103 _bt_mesh_is_scanning()) {
2104 BT_ERR("Device is buzy..");
2105 return BLUETOOTH_ERROR_DEVICE_BUSY;
2108 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
2109 strlen(req->net_uuid), net_uuid.uuid, 16);
2111 /* Insert Model ID */
2112 if (req->model >= 0xFFFF0000)
2113 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2114 opcode = MESH_OPCODE_MODEL_APP_GET;
2116 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2117 opcode = MESH_OPCODE_VENDOR_MODEL_APP_GET;
2119 /* Check pending request */
2120 if (_bt_mesh_check_pending_request(opcode,
2121 req->primary_unicast, net_uuid.uuid)) {
2122 BT_ERR("Device is buzy..");
2123 return BLUETOOTH_ERROR_DEVICE_BUSY;
2125 /* Get Subnet index of the remote node for TX encryption */
2126 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2127 if (netkey_idx == MESH_NET_IDX_INVALID)
2128 return BLUETOOTH_ERROR_INTERNAL;
2130 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2131 /* Element Address */
2132 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2136 /* Insert Model ID */
2137 if (opcode == MESH_OPCODE_MODEL_APP_GET) {
2138 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2139 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2142 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2143 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2145 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2149 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2151 if (ret != OAL_STATUS_SUCCESS) {
2152 BT_ERR("ret: %d", ret);
2153 return BLUETOOTH_ERROR_INTERNAL;
2156 /* Queue the request with timeout */
2157 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2158 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2160 return BLUETOOTH_ERROR_NONE;
2163 int _bt_mesh_node_model_appkey_execute(const char *app_cred, const char *sender,
2164 bluetooth_mesh_model_configure_t *req)
2166 int ret = OAL_STATUS_SUCCESS;
2167 uint16_t netkey_idx;
2168 oal_uuid_t net_uuid;
2170 uint32_t opcode = 0;
2171 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2173 /* If Scanning is going on */
2174 if (_bt_mesh_is_provisioning() ||
2175 _bt_mesh_is_scanning()) {
2176 BT_ERR("Device is buzy..");
2177 return BLUETOOTH_ERROR_DEVICE_BUSY;
2180 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2182 /* Check pending request */
2183 if (_bt_mesh_check_pending_request(req->is_bind ? MESH_OPCODE_MODEL_APP_BIND : MESH_OPCODE_MODEL_APP_UNBIND,
2184 req->primary_unicast, net_uuid.uuid)) {
2185 BT_ERR("Device is buzy..");
2186 return BLUETOOTH_ERROR_DEVICE_BUSY;
2188 /* Get Subnet index of the remote node for TX encryption */
2189 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2190 if (netkey_idx == MESH_NET_IDX_INVALID)
2191 return BLUETOOTH_ERROR_INTERNAL;
2194 opcode = MESH_OPCODE_MODEL_APP_BIND;
2196 opcode = MESH_OPCODE_MODEL_APP_UNBIND;
2198 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2199 /* Element Address */
2200 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2204 l_put_le16(req->appkey_idx, buffer + data_len);
2207 /* Insert Model ID */
2208 if (req->model >= 0xFFFF0000) {
2209 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2210 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2213 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2214 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2216 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2220 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2222 if (ret != OAL_STATUS_SUCCESS) {
2223 BT_ERR("ret: %d", ret);
2224 return BLUETOOTH_ERROR_INTERNAL;
2227 /* Queue the request with timeout */
2228 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2229 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2231 return BLUETOOTH_ERROR_NONE;
2234 int _bt_mesh_ttl_execute_remote_node(const char *app_cred, const char *sender,
2235 bluetooth_mesh_node_ttl_info_t *req)
2237 int ret = OAL_STATUS_SUCCESS;
2238 uint16_t netkey_idx;
2240 oal_uuid_t net_uuid;
2241 uint32_t opcode = 0;
2242 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2244 /* If Scanning is going on */
2245 if (_bt_mesh_is_provisioning() ||
2246 _bt_mesh_is_scanning()) {
2247 BT_ERR("Device is buzy..");
2248 return BLUETOOTH_ERROR_DEVICE_BUSY;
2251 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2253 /* Check pending request */
2254 if (_bt_mesh_check_pending_request(req->is_set ? MESH_OPCODE_CONFIG_DEFAULT_TTL_SET : MESH_OPCODE_CONFIG_DEFAULT_TTL_GET,
2255 req->unicast, net_uuid.uuid)) {
2256 BT_ERR("Device is buzy..");
2257 return BLUETOOTH_ERROR_DEVICE_BUSY;
2260 /* Get Subnet index of the rmeote node for TX encryption */
2261 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->unicast);
2262 if (netkey_idx == MESH_NET_IDX_INVALID)
2263 return BLUETOOTH_ERROR_INTERNAL;
2266 opcode = MESH_OPCODE_CONFIG_DEFAULT_TTL_SET;
2267 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_CONFIG_DEFAULT_TTL_SET, buffer);
2268 buffer[data_len++] = req->ttl;
2270 opcode = MESH_OPCODE_CONFIG_DEFAULT_TTL_GET;
2271 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_CONFIG_DEFAULT_TTL_GET, buffer);
2274 ret = mesh_conf_send_message(&net_uuid, req->unicast, true, netkey_idx, buffer, data_len);
2276 if (ret != OAL_STATUS_SUCCESS) {
2277 BT_ERR("ret: %d", ret);
2278 return BLUETOOTH_ERROR_INTERNAL;
2281 /* Queue the request with timeout */
2282 __bt_mesh_add_request(opcode, req->unicast, net_uuid.uuid,
2283 g_memdup(req, sizeof(bluetooth_mesh_node_ttl_info_t)));
2284 return BLUETOOTH_ERROR_NONE;
2287 static bool __bt_mesh_check_pending_key_cmd(uint16_t dest, bool is_netkey,
2288 bluetooth_mesh_node_key_conf_e op, uint8_t net_uuid[], uint32_t *opcode)
2290 const struct mesh_config_cmd *cmd;
2294 case BLUETOOTH_MESH_NODE_KEY_ADD:
2295 cmd = __mesh_get_command(MESH_OPCODE_NETKEY_ADD);
2298 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2299 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2302 *opcode = MESH_OPCODE_NETKEY_ADD;
2304 case BLUETOOTH_MESH_NODE_KEY_DELETE:
2305 cmd = __mesh_get_command(MESH_OPCODE_NETKEY_DELETE);
2308 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2309 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2312 *opcode = MESH_OPCODE_NETKEY_DELETE;
2314 case BLUETOOTH_MESH_NODE_KEY_UPDATE:
2315 cmd = __mesh_get_command(MESH_OPCODE_NETKEY_UPDATE);
2318 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2319 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2322 *opcode = MESH_OPCODE_NETKEY_UPDATE;
2327 case BLUETOOTH_MESH_NODE_KEY_ADD:
2328 cmd = __mesh_get_command(MESH_OPCODE_APPKEY_ADD);
2331 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2332 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2335 *opcode = MESH_OPCODE_APPKEY_ADD;
2337 case BLUETOOTH_MESH_NODE_KEY_DELETE:
2338 cmd = __mesh_get_command(MESH_OPCODE_APPKEY_DELETE);
2341 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2342 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2345 *opcode = MESH_OPCODE_APPKEY_DELETE;
2347 case BLUETOOTH_MESH_NODE_KEY_UPDATE:
2348 cmd = __mesh_get_command(MESH_OPCODE_APPKEY_UPDATE);
2351 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2352 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2355 *opcode = MESH_OPCODE_APPKEY_UPDATE;
2362 int _bt_mesh_node_configure_key(const char *app_cred, const char *sender,
2363 bluetooth_mesh_key_configure_t *req)
2365 int ret = OAL_STATUS_SUCCESS;
2366 uint16_t netkey_idx;
2367 uint16_t bound_netkey_idx = 0x0000;
2368 oal_uuid_t net_uuid;
2369 uint32_t opcode = 0;
2372 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2374 /* If Scanning is going on */
2375 if (_bt_mesh_is_provisioning() ||
2376 _bt_mesh_is_scanning()) {
2377 BT_ERR("Device is buzy..");
2378 return BLUETOOTH_ERROR_DEVICE_BUSY;
2381 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2383 if (req->is_netkey && !_bt_mesh_keys_subnet_exists(net_uuid.uuid, req->netkey_idx)) {
2384 BT_ERR("Local Subnet not found..");
2385 return BLUETOOTH_ERROR_INVALID_PARAM;
2388 /* For Appkey Configuration, check for available bound netkey */
2389 if (!req->is_netkey) {
2390 bound_netkey_idx = _bt_mesh_keys_get_bound_key(net_uuid.uuid, req->appkey_idx);
2391 if (bound_netkey_idx == MESH_NET_IDX_INVALID) {
2392 BT_ERR("Local AppKey not found..");
2393 return BLUETOOTH_ERROR_INVALID_PARAM;
2397 /* Check pending request */
2398 if (__bt_mesh_check_pending_key_cmd(req->primary_unicast,
2399 req->is_netkey, req->op, net_uuid.uuid, &opcode)) {
2400 BT_ERR("Device is buzy..");
2401 return BLUETOOTH_ERROR_DEVICE_BUSY;
2404 /* Get Subnet index of the rmeote node for TX encryption */
2405 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2406 if (netkey_idx == MESH_NET_IDX_INVALID)
2407 return BLUETOOTH_ERROR_INTERNAL;
2409 /* Handle Key (App/Net) Delete Commands: Configuration Message */
2410 if (opcode == MESH_OPCODE_NETKEY_DELETE || opcode == MESH_OPCODE_APPKEY_DELETE) {
2411 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2412 if (req->is_netkey) {
2413 l_put_le16(req->netkey_idx, buffer + data_len);
2416 buffer[data_len] = bound_netkey_idx;
2417 buffer[data_len + 1] = ((bound_netkey_idx >> 8) & 0xf) | ((req->appkey_idx << 4) & 0xf0);
2418 buffer[data_len + 2] = req->appkey_idx >> 4;
2422 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2424 /* Handle Key (App/Net) Update & Add Commands: Key Config message */
2425 update = (opcode == MESH_OPCODE_NETKEY_UPDATE || opcode == MESH_OPCODE_APPKEY_UPDATE);
2426 ret = mesh_conf_send_key_message(&net_uuid, req->primary_unicast, req->is_netkey,
2427 update, req->is_netkey? req->netkey_idx: req->appkey_idx, netkey_idx);
2429 if (ret != OAL_STATUS_SUCCESS) {
2430 BT_ERR("ret: %d", ret);
2431 return BLUETOOTH_ERROR_INTERNAL;
2434 /* Queue the request with timeout */
2435 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2436 g_memdup(req, sizeof(bluetooth_mesh_key_configure_t)));
2437 return BLUETOOTH_ERROR_NONE;