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 for Vendor Features */
239 bluetooth_mesh_node_features_t *vendor_event = \
240 g_malloc0(sizeof(bluetooth_mesh_node_features_t));
242 _bt_mesh_util_convert_hex_to_string((uint8_t *) req->net_uuid, 16,
243 vendor_event->net_uuid,
244 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
245 vendor_event->unicast = req->addr;
246 __bt_mesh_handle_pending_dev_config_request_info(
247 BLUETOOTH_ERROR_TIMEOUT, BT_MESH_NODE_GET_VENDOR_FEATURES,
248 vendor_event, sizeof(bluetooth_mesh_node_features_t));
249 g_free(vendor_event);
251 /* Send event with timeout for Node Browse */
252 event_mesh_devkey_message_t *event = \
253 g_malloc0(sizeof(event_mesh_devkey_message_t));
254 memcpy(event->net_uuid.uuid, req->net_uuid, 16);
255 event->source = req->addr;
257 __bt_mesh_handle_pending_dev_config_request_info(
258 BLUETOOTH_ERROR_TIMEOUT, BT_MESH_NODE_BROWSE,
259 event, sizeof(event_mesh_devkey_message_t));
264 case MESH_OPCODE_CONFIG_MODEL_SUB_GET:
265 case MESH_OPCODE_CONFIG_VEND_MODEL_SUB_GET:
266 BT_INFO("Mesh: Command TimedOut: Model Subscription list get");
267 /* Send event with timeout */
268 __bt_mesh_handle_pending_dev_config_request_info(
269 BLUETOOTH_ERROR_TIMEOUT,
270 BT_MESH_MODEL_GET_SUBSCRIPTION_LIST, req->data,
271 sizeof(bluetooth_mesh_model_configure_t));
273 case MESH_OPCODE_NETKEY_ADD:
274 case MESH_OPCODE_NETKEY_UPDATE:
275 case MESH_OPCODE_NETKEY_DELETE:
276 case MESH_OPCODE_APPKEY_ADD:
277 case MESH_OPCODE_APPKEY_UPDATE:
278 case MESH_OPCODE_APPKEY_DELETE:
279 /* Send event with timeout */
280 __bt_mesh_handle_pending_dev_config_request_info(
281 BLUETOOTH_ERROR_TIMEOUT,
282 BT_MESH_NODE_CONFIGURE_KEY, req->data,
283 sizeof(bluetooth_mesh_key_configure_t));
285 case MESH_OPCODE_CONFIG_DEFAULT_TTL_GET:
286 case MESH_OPCODE_CONFIG_DEFAULT_TTL_SET:
287 /* Send event with timeout */
288 __bt_mesh_handle_pending_dev_config_request_info(
289 BLUETOOTH_ERROR_TIMEOUT,
290 BT_MESH_NODE_TTL_EXECUTE, req->data,
291 sizeof(bluetooth_mesh_node_ttl_info_t));
293 case MESH_OPCODE_CONFIG_MODEL_SUB_ADD:
294 case MESH_OPCODE_CONFIG_MODEL_SUB_DELETE:
295 case MESH_OPCODE_CONFIG_MODEL_SUB_OVERWRITE:
296 /* Send event with timeout */
297 __bt_mesh_handle_pending_dev_config_request_info(
298 BLUETOOTH_ERROR_TIMEOUT,
299 BT_MESH_MODEL_CONFIG_GROUP_SUB, req->data,
300 sizeof(bluetooth_mesh_model_configure_t));
302 case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_ADD:
303 case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_DELETE:
304 case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_OVERWRITE:
305 /* Send event with timeout */
306 __bt_mesh_handle_pending_dev_config_request_info(
307 BLUETOOTH_ERROR_TIMEOUT,
308 BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB, req->data,
309 sizeof(bluetooth_mesh_model_configure_t));
311 case MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL:
312 /* Send event with timeout */
313 __bt_mesh_handle_pending_dev_config_request_info(
314 BLUETOOTH_ERROR_TIMEOUT,
315 BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB, req->data,
316 sizeof(bluetooth_mesh_model_configure_t));
317 /* Send event with timeout */
318 __bt_mesh_handle_pending_dev_config_request_info(
319 BLUETOOTH_ERROR_TIMEOUT,
320 BT_MESH_MODEL_CONFIG_GROUP_SUB, req->data,
321 sizeof(bluetooth_mesh_model_configure_t));
323 case MESH_OPCODE_CONFIG_MODEL_PUB_GET:
324 /* Send event with timeout */
325 __bt_mesh_handle_pending_dev_config_request_info(
326 BLUETOOTH_ERROR_TIMEOUT,
327 BT_MESH_MODEL_GET_PUBLICATION, req->data,
328 sizeof(bluetooth_mesh_model_configure_t));
330 case MESH_OPCODE_CONFIG_MODEL_PUB_SET:
331 /* Send event with timeout */
332 __bt_mesh_handle_pending_dev_config_request_info(
333 BLUETOOTH_ERROR_TIMEOUT,
334 BT_MESH_MODEL_SET_PUBLICATION, req->data,
335 sizeof(bluetooth_mesh_model_configure_t));
337 case MESH_OPCODE_MODEL_APP_BIND:
338 case MESH_OPCODE_MODEL_APP_UNBIND:
339 /* Send event with timeout */
340 __bt_mesh_handle_pending_dev_config_request_info(
341 BLUETOOTH_ERROR_TIMEOUT,
342 BT_MESH_MODEL_CONFIGURE_APPKEY, req->data,
343 sizeof(bluetooth_mesh_model_configure_t));
348 BT_INFO("Mesh: Number of pending requests [%u] Remove the req",
349 l_queue_length(pending_requests));
350 l_queue_remove(pending_requests, req);
351 __mesh_request_remove(req);
354 static void __bt_mesh_add_request(uint32_t opcode, uint16_t dest,
355 uint8_t net_uuid[], void *data)
357 struct mesh_pending_request *req;
358 const struct mesh_config_cmd *cmd;
361 cmd = __mesh_get_command(opcode);
364 _bt_mesh_util_convert_hex_to_string((uint8_t *) net_uuid, 16, uuid_str,
365 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
366 BT_INFO("Mesh: Net UUID[%s]", uuid_str);
368 BT_INFO("Mesh: Adding command opcode [0x%2.2x] response [0x%2.2x]",
369 cmd->opcode, cmd->response);
370 req = l_new(struct mesh_pending_request, 1);
374 memcpy(req->net_uuid, net_uuid, 16);
375 req->timer = l_timeout_create(MESH_DEFAULT_RESPONSE_TIMEOUT,
376 __bt_mesh_wait_response_timeout, req, NULL);
378 if (!pending_requests)
379 pending_requests = l_queue_new();
380 l_queue_push_tail(pending_requests, req);
381 BT_INFO("Mesh: Number of pending requests [%u]", l_queue_length(pending_requests));
384 static struct mesh_pending_request *__bt_mesh_get_request_by_response(
385 uint16_t addr, uint8_t net_uuid[],
388 const struct l_queue_entry *entry;
392 BT_INFO("Mesh: Number of pending requests [%u]", l_queue_length(pending_requests));
393 entry = l_queue_get_entries(pending_requests);
395 for (; entry; entry = entry->next) {
396 struct mesh_pending_request *req = entry->data;
399 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);
400 BT_INFO("Mesh: Current req addr [0x%2.2x] res [0x%2.2x]", addr, response);
401 _bt_mesh_util_convert_hex_to_string((uint8_t *) net_uuid, 16, uuid_str,
402 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
403 BT_INFO("Mesh: Net UUID[%s]", uuid_str);
405 _bt_mesh_util_convert_hex_to_string((uint8_t *) req->net_uuid, 16, uuid_str1,
406 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
407 BT_INFO("Mesh: Net UUID1[%s]", uuid_str1);
408 if (!memcmp(net_uuid, req->net_uuid, 16) &&
410 req->cmd->response == response)
417 bool _bt_mesh_check_pending_request(uint32_t opcode,
418 uint16_t dest, uint8_t net_uuid[])
420 const struct mesh_config_cmd *cmd;
421 cmd = __mesh_get_command(opcode);
426 if (__bt_mesh_get_request_by_response(dest,
427 net_uuid, cmd->response)) {
428 BT_ERR("Mesh:Another command is pending\n");
434 static uint32_t __bt_mesh_print_model_identifier(uint8_t *data,
435 bool vendor, const char *offset)
440 mod_id = l_get_le16(data);
441 BT_INFO("%sModel ID\t%4.4x\n", offset, mod_id);
442 mod_id = MESH_VENDOR_ID_MASK | mod_id;
444 mod_id = l_get_le16(data + 2);
445 BT_INFO("%sModel ID\t%4.4x %4.4x\n", offset,
446 l_get_le16(data), mod_id);
447 mod_id = l_get_le16(data) << 16 | mod_id;
453 static void __bt_mesh_print_device_composition_data(
454 uint8_t *data, uint16_t len)
459 BT_INFO("Mesh: Received composion:\n");
461 /* skip page -- We only support Page Zero */
465 BT_INFO("\tCID: %4.4x", l_get_le16(&data[0]));
466 BT_INFO("\tPID: %4.4x", l_get_le16(&data[2]));
467 BT_INFO("\tVID: %4.4x", l_get_le16(&data[4]));
468 BT_INFO("\tCRPL: %4.4x", l_get_le16(&data[6]));
470 features = l_get_le16(&data[8]);
474 BT_INFO("\tFeature support:\n");
475 BT_INFO("\t\trelay: %s\n", (features & MESH_FEATURE_RELAY) ?
477 BT_INFO("\t\tproxy: %s\n", (features & MESH_FEATURE_PROXY) ?
479 BT_INFO("\t\tfriend: %s\n", (features & MESH_FEATURE_FRIEND) ?
481 BT_INFO("\t\tlpn: %s\n", (features & MESH_FEATURE_LPN) ?
486 BT_INFO("\t Element %d:\n", i);
487 BT_INFO("\t\tlocation: %4.4x\n", l_get_le16(data));
496 BT_INFO("\t\tSIG defined models:\n");
498 while (len >= 2 && m--) {
499 __bt_mesh_print_model_identifier(data, false, "\t\t ");
505 BT_INFO("\t\t Vendor defined models:\n");
507 while (len >= 4 && v--) {
508 __bt_mesh_print_model_identifier(data, true, "\t\t ");
517 static void __bt_mesh_send_model_publication_status_event(
518 int event, int result,
519 bluetooth_mesh_model_configure_t *evt)
521 GVariant *out_var = NULL, *param = NULL;
524 if (BLUETOOTH_ERROR_NONE == result) {
525 BT_INFO("Mesh: Model ID [0x%4.4x]", evt->model);
526 BT_INFO("Mesh: Model Publication Address [0x%2.2x]", evt->pub_addr);
528 info = g_array_new(FALSE, FALSE, sizeof(gchar));
529 g_array_append_vals(info, evt,
530 sizeof(bluetooth_mesh_model_configure_t));
532 out_var = g_variant_new_from_data((const GVariantType *)"ay",
533 info->data, info->len,
536 param = g_variant_new("(iv)", result, out_var);
537 _bt_send_event(BT_MESH_EVENT, event,
542 static void __bt_mesh_send_model_subscription_configure_event(
543 int event, int result,
544 bluetooth_mesh_model_configure_t *evt)
546 GVariant *out_var = NULL, *param = NULL;
549 if (BLUETOOTH_ERROR_NONE == result) {
551 info = g_array_new(FALSE, FALSE, sizeof(gchar));
552 g_array_append_vals(info, evt,
553 sizeof(bluetooth_mesh_model_configure_t));
555 out_var = g_variant_new_from_data((const GVariantType *)"ay",
556 info->data, info->len,
559 param = g_variant_new("(iv)", result, out_var);
560 _bt_send_event(BT_MESH_EVENT, event,
565 static void __bt_mesh_send_model_get_subscription_list_event(
566 int result, bluetooth_mesh_model_configure_t *evt)
568 GVariant *param = NULL;
569 GVariantBuilder *builder = NULL;
573 if (BLUETOOTH_ERROR_NONE == result) {
574 BT_INFO("Mesh: Total Subscriptions bound to model [%d]",
576 BT_INFO("Mesh: Network [%s]", evt->net_uuid);
577 builder = g_variant_builder_new(G_VARIANT_TYPE("aq"));
578 for (i = 0; i < evt->sublist_count; i++) {
579 BT_INFO("Mesh: Subscription Address [0x%2.2x]",
581 g_variant_builder_add(builder, "q", *evt->sub_list[i]);
584 net_uuid = g_strdup(evt->net_uuid);
585 param = g_variant_new("(isqiui(aq))", result, net_uuid,
586 evt->primary_unicast, evt->elem_index, evt->model,
587 evt->sublist_count, builder);
589 g_variant_builder_unref(builder);
592 _bt_send_event(BT_MESH_EVENT,
593 BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_LIST,
596 if (evt->sublist_count) {
598 for (int i = 0; i < evt->sublist_count; i++)
599 g_free(evt->sub_list[i]);
600 g_free(evt->sub_list);
603 BT_INFO("freed net uuid");
607 static void __bt_mesh_send_model_get_appkey_list_event(int result,
608 bluetooth_mesh_model_configure_t *evt)
610 GVariant *param = NULL;
611 GVariantBuilder *builder = NULL;
615 if (BLUETOOTH_ERROR_NONE == result) {
616 builder = g_variant_builder_new(G_VARIANT_TYPE("aq"));
617 BT_INFO("Mesh: Total AppKeys bound to model [%d]",
618 evt->appkeylist_count);
619 BT_INFO("Mesh: Network [%s]", evt->net_uuid);
620 for (i = 0; i < evt->appkeylist_count; i++)
621 g_variant_builder_add(builder, "q", *evt->appkey_list[i]);
622 net_uuid = g_strdup(evt->net_uuid);
623 param = g_variant_new("(isqiui(aq))", result, net_uuid,
624 evt->primary_unicast, evt->elem_index, evt->model,
625 evt->appkeylist_count, builder);
627 g_variant_builder_unref(builder);
630 _bt_send_event(BT_MESH_EVENT,
631 BLUETOOTH_EVENT_MESH_MODEL_APPKEY_LIST,
634 if (evt->appkeylist_count) {
635 for (int i = 0; i < evt->appkeylist_count; i++)
636 g_free(evt->appkey_list[i]);
638 g_free(evt->appkey_list);
645 static void __bt_mesh_send_model_configure_appkey_event(int result,
646 bluetooth_mesh_model_configure_t *evt)
648 GVariant *out_var = NULL, *param = NULL;
651 if (BLUETOOTH_ERROR_NONE == result) {
653 info = g_array_new(FALSE, FALSE, sizeof(gchar));
654 g_array_append_vals(info, evt, sizeof(bluetooth_mesh_model_configure_t));
656 out_var = g_variant_new_from_data((const GVariantType *)"ay",
657 info->data, info->len,
660 param = g_variant_new("(iv)", result, out_var);
661 _bt_send_event(BT_MESH_EVENT,
662 BLUETOOTH_EVENT_MESH_MODEL_APPKEY_BIND,
667 static void __bt_mesh_send_node_ttl_configuration_event(int result,
668 bluetooth_mesh_node_ttl_info_t *ttl_evt)
670 GVariant *out_var = NULL, *param = NULL;
673 if (BLUETOOTH_ERROR_NONE == result) {
675 info = g_array_new(FALSE, FALSE, sizeof(gchar));
676 g_array_append_vals(info, ttl_evt,
677 sizeof(bluetooth_mesh_node_ttl_info_t));
679 out_var = g_variant_new_from_data((const GVariantType *)"ay",
680 info->data, info->len,
683 param = g_variant_new("(iv)", result, out_var);
684 _bt_send_event(BT_MESH_EVENT,
685 BLUETOOTH_EVENT_MESH_NODE_TTL_CONFIGURED,
690 static void __bt_mesh_send_node_key_configuration_event(int result,
691 bluetooth_mesh_key_configure_t *key_evt)
693 GVariant *out_var = NULL, *param = NULL;
696 if (BLUETOOTH_ERROR_NONE == result) {
698 info = g_array_new(FALSE, FALSE, sizeof(gchar));
699 g_array_append_vals(info, key_evt,
700 sizeof(bluetooth_mesh_key_configure_t));
702 out_var = g_variant_new_from_data((const GVariantType *)"ay",
703 info->data, info->len,
706 param = g_variant_new("(iv)", result, out_var);
707 _bt_send_event(BT_MESH_EVENT,
708 BLUETOOTH_EVENT_MESH_NODE_KEY_CONFIGURED,
713 static void __bt_mesh_send_node_get_vendor_features_event(int result,
714 bluetooth_mesh_node_features_t *features_evt)
716 GVariant *out_var = NULL, *param = NULL;
719 if (BLUETOOTH_ERROR_NONE == result) {
721 info = g_array_new(FALSE, FALSE, sizeof(gchar));
722 g_array_append_vals(info, features_evt,
723 sizeof(bluetooth_mesh_node_features_t));
725 out_var = g_variant_new_from_data((const GVariantType *)"ay",
726 info->data, info->len,
729 param = g_variant_new("(iv)", result, out_var);
730 _bt_send_event(BT_MESH_EVENT,
731 BLUETOOTH_EVENT_MESH_NODE_VENDOR_FEATURES,
736 static void __bt_mesh_send_node_browsed_event(int result,
737 bluetooth_mesh_node_discover_t *browse_evt)
739 GVariant *out_var = NULL, *param = NULL;
742 if (BLUETOOTH_ERROR_NONE == result) {
744 info = g_array_new(FALSE, FALSE, sizeof(gchar));
745 g_array_append_vals(info, browse_evt,
746 sizeof(bluetooth_mesh_node_discover_t));
748 out_var = g_variant_new_from_data((const GVariantType *)"ay",
749 info->data, info->len,
752 param = g_variant_new("(iv)", result, out_var);
753 _bt_send_event(BT_MESH_EVENT,
754 BLUETOOTH_EVENT_MESH_NODE_BROWSED,
759 static void __bt_mesh_handle_pending_dev_config_request_info(int result,
760 int service_function, void *param, unsigned int size)
764 invocation_info_t *req_info = NULL;
766 for (l = _bt_get_invocation_list(); l != NULL; ) {
769 if (req_info == NULL ||
770 req_info->service_function != service_function)
773 switch (service_function) {
774 case BT_MESH_NODE_GET_VENDOR_FEATURES: {
775 bluetooth_mesh_node_features_t *event;
776 bluetooth_mesh_node_features_t *req;
778 event = (bluetooth_mesh_node_features_t*) param;
779 req = (bluetooth_mesh_node_features_t*)req_info->user_data;
781 BT_DBG("Request Sender: [%s]", req_info->sender);
782 /* Match Network and Remote Node unicast*/
783 if (!g_strcmp0(event->net_uuid, req->net_uuid) && event->unicast == req->unicast) {
784 event->unicast = req->unicast;
785 event->elem_count = req->elem_count;
788 __bt_mesh_send_node_get_vendor_features_event(result, event);
790 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
791 g_array_append_vals(out_param, event, sizeof(bluetooth_mesh_node_features_t));
793 /* Return DBUS Invocation*/
794 _bt_service_method_return(req_info->context, out_param, result);
795 _bt_free_info_from_invocation_list(req_info);
796 g_array_free(out_param, TRUE);
800 case BT_MESH_NODE_BROWSE: {
801 bluetooth_mesh_node_discover_t *node;
802 event_mesh_devkey_message_t *event;
803 uint16_t remote_addr;
805 uint8_t dev_uuid[16];
806 char net_uuid[BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1];
808 event = (event_mesh_devkey_message_t*) param;
809 node = (bluetooth_mesh_node_discover_t*)req_info->user_data;
810 BT_INFO("Mesh: Request Node UUID [%s]", node->dev_uuid);
811 BT_INFO("Mesh: Request Network UUID [%s]", node->net_uuid);
813 _bt_mesh_util_convert_hex_to_string((uint8_t *) event->net_uuid.uuid, 16, net_uuid,
814 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
816 _bt_mesh_util_convert_string_to_hex(node->dev_uuid, strlen(node->dev_uuid), dev_uuid, 16);
818 /* Get Unicast from pending request's Dev UUID and match with event */
819 if (_bt_mesh_node_get_unicast_from_dev_uuid(event->net_uuid.uuid, dev_uuid, &remote_addr)) {
821 BT_DBG("Request Sender: [%s]", req_info->sender);
822 /* Match Network and Remote Node unicast*/
823 if (!g_strcmp0(node->net_uuid, net_uuid) && remote_addr == event->source) {
824 _bt_mesh_node_get_element_count(event->net_uuid.uuid, remote_addr, &elem_count);
825 node->unicast = event->source;
826 node->count = elem_count;
827 BT_INFO("Mesh: Browse event for Node: Unicast [0x%2.2x] Element Count [%d]",
828 node->unicast, elem_count);
830 __bt_mesh_send_node_browsed_event(result, node);
832 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
833 g_array_append_vals(out_param, node, sizeof(bluetooth_mesh_node_discover_t));
835 /* Return DBUS Invocation*/
836 _bt_service_method_return(req_info->context, out_param, result);
837 _bt_free_info_from_invocation_list(req_info);
838 g_array_free(out_param, TRUE);
844 case BT_MESH_NODE_CONFIGURE_KEY: {
845 bluetooth_mesh_key_configure_t *event;
846 bluetooth_mesh_key_configure_t *req;
848 event = (bluetooth_mesh_key_configure_t*) param;
849 req = (bluetooth_mesh_key_configure_t*)req_info->user_data;
851 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
852 req->primary_unicast == event->primary_unicast &&
853 req->is_netkey == event->is_netkey) {
855 __bt_mesh_send_node_key_configuration_event(result, event);
857 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
858 g_array_append_vals(out_param, event, sizeof(bluetooth_mesh_key_configure_t));
860 /* Return DBUS Invocation*/
861 _bt_service_method_return(req_info->context, out_param, result);
862 _bt_free_info_from_invocation_list(req_info);
863 g_array_free(out_param, TRUE);
867 case BT_MESH_NODE_TTL_EXECUTE: {
868 bluetooth_mesh_node_ttl_info_t *event;
869 bluetooth_mesh_node_ttl_info_t *req;
871 event = (bluetooth_mesh_node_ttl_info_t*) param;
872 req = (bluetooth_mesh_node_ttl_info_t*)req_info->user_data;
873 req->ttl = event->ttl;
875 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
876 req->unicast == event->unicast) {
878 __bt_mesh_send_node_ttl_configuration_event(result, req);
880 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
881 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_node_ttl_info_t));
883 /* Return DBUS Invocation*/
884 _bt_service_method_return(req_info->context, out_param, result);
885 _bt_free_info_from_invocation_list(req_info);
886 g_array_free(out_param, TRUE);
890 case BT_MESH_MODEL_CONFIGURE_APPKEY: {
891 bluetooth_mesh_model_configure_t *event;
892 bluetooth_mesh_model_configure_t *req;
894 event = (bluetooth_mesh_model_configure_t*) param;
895 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
897 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
898 req->primary_unicast == event->primary_unicast) {
900 __bt_mesh_send_model_configure_appkey_event(result, req);
902 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
903 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
905 /* Return DBUS Invocation*/
906 _bt_service_method_return(req_info->context, out_param, result);
907 _bt_free_info_from_invocation_list(req_info);
908 g_array_free(out_param, TRUE);
913 case BT_MESH_MODEL_GET_APPKEY_LIST: {
914 bluetooth_mesh_model_configure_t *event;
915 bluetooth_mesh_model_configure_t *req;
917 event = (bluetooth_mesh_model_configure_t*) param;
918 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
920 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
921 req->primary_unicast == event->primary_unicast) {
923 __bt_mesh_send_model_get_appkey_list_event(result, event);
925 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
926 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
928 /* Return DBUS Invocation*/
929 _bt_service_method_return(req_info->context, out_param, result);
930 _bt_free_info_from_invocation_list(req_info);
931 g_array_free(out_param, TRUE);
935 case BT_MESH_MODEL_GET_SUBSCRIPTION_LIST: {
936 bluetooth_mesh_model_configure_t *event;
937 bluetooth_mesh_model_configure_t *req;
939 BT_INFO("Mesh: Handle event for BT_MESH_MODEL_GET_SUBSCRIPTION_LIST");
940 event = (bluetooth_mesh_model_configure_t*) param;
941 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
943 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
944 req->primary_unicast == event->primary_unicast) {
946 __bt_mesh_send_model_get_subscription_list_event(result, event);
948 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
949 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
951 /* Return DBUS Invocation*/
952 _bt_service_method_return(req_info->context, out_param, result);
953 _bt_free_info_from_invocation_list(req_info);
954 g_array_free(out_param, TRUE);
958 case BT_MESH_MODEL_CONFIG_GROUP_SUB: {
959 bluetooth_mesh_model_configure_t *event;
960 bluetooth_mesh_model_configure_t *req;
962 event = (bluetooth_mesh_model_configure_t*) param;
963 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
964 BT_INFO("Mesh: Event Network [%s] Req Network [%s]",
965 event->net_uuid, req->net_uuid);
967 BT_INFO("Mesh: Event Unicast [0x%4.4x] Req Unicast [0x%4.4x]",
968 event->primary_unicast, req->primary_unicast);
970 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
971 req->primary_unicast == event->primary_unicast) {
973 req->sub_addr = event->sub_addr;
974 BT_INFO("Mesh: Send event for Model Subscription");
976 __bt_mesh_send_model_subscription_configure_event(
977 BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_CONFGURED,
980 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
981 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
983 /* Return DBUS Invocation*/
984 _bt_service_method_return(req_info->context, out_param, result);
985 _bt_free_info_from_invocation_list(req_info);
986 g_array_free(out_param, TRUE);
990 case BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB: {
991 bluetooth_mesh_model_configure_t *event;
992 bluetooth_mesh_model_configure_t *req;
994 event = (bluetooth_mesh_model_configure_t*) param;
995 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
996 BT_INFO("Mesh: Event Network [%s] Req Network [%s]",
997 event->net_uuid, req->net_uuid);
999 BT_INFO("Mesh: Event Unicast [0x%4.4x] Req Unicast [0x%4.4x]",
1000 event->primary_unicast, req->primary_unicast);
1001 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
1002 req->primary_unicast == event->primary_unicast) {
1004 __bt_mesh_send_model_subscription_configure_event( \
1005 BLUETOOTH_EVENT_MESH_MODEL_VIRTUAL_SUBSCRIPTION_CONFGURED, \
1008 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1009 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
1011 /* Return DBUS Invocation*/
1012 _bt_service_method_return(req_info->context, out_param, result);
1013 _bt_free_info_from_invocation_list(req_info);
1014 g_array_free(out_param, TRUE);
1019 case BT_MESH_MODEL_GET_PUBLICATION: {
1020 BT_INFO("Mesh: Event for Model Get Publication Request");
1021 bluetooth_mesh_model_configure_t *event;
1022 bluetooth_mesh_model_configure_t *req;
1024 event = (bluetooth_mesh_model_configure_t*) param;
1025 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
1027 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
1028 req->primary_unicast == event->primary_unicast) {
1030 BT_INFO("Mesh: Send Model Publication status for Get Request");
1031 __bt_mesh_send_model_publication_status_event( \
1032 BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS, \
1035 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1036 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
1038 /* Return DBUS Invocation*/
1039 _bt_service_method_return(req_info->context, out_param, result);
1040 _bt_free_info_from_invocation_list(req_info);
1041 g_array_free(out_param, TRUE);
1045 case BT_MESH_MODEL_SET_PUBLICATION: {
1046 BT_INFO("Mesh: Event for Model Set Publication Request");
1047 bluetooth_mesh_model_configure_t *event;
1048 bluetooth_mesh_model_configure_t *req;
1050 event = (bluetooth_mesh_model_configure_t*) param;
1051 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
1053 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
1054 req->primary_unicast == event->primary_unicast) {
1056 BT_INFO("Mesh: Send Model Publication status for Set Request");
1057 __bt_mesh_send_model_publication_status_event( \
1058 BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS, \
1061 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1062 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
1064 /* Return DBUS Invocation*/
1065 _bt_service_method_return(req_info->context, out_param, result);
1066 _bt_free_info_from_invocation_list(req_info);
1067 g_array_free(out_param, TRUE);
1072 BT_DBG("Unknown function(%d)", service_function);
1078 const char *__mesh_status_to_string(uint8_t err)
1081 case MESH_STATUS_SUCCESS: return "Success";
1082 case MESH_STATUS_INVALID_ADDRESS: return "Invalid Address";
1083 case MESH_STATUS_INVALID_MODEL: return "Invalid Model";
1084 case MESH_STATUS_INVALID_APPKEY: return "Invalid AppKey";
1085 case MESH_STATUS_INVALID_NETKEY: return "Invalid NetKey";
1086 case MESH_STATUS_INSUFF_RESOURCES: return "Insufficient Resources";
1087 case MESH_STATUS_IDX_ALREADY_STORED: return "Key Idx Already Stored";
1088 case MESH_STATUS_INVALID_PUB_PARAM: return "Invalid Publish Parameters";
1089 case MESH_STATUS_NOT_SUB_MOD: return "Not a Subscribe Model";
1090 case MESH_STATUS_STORAGE_FAIL: return "Storage Failure";
1091 case MESH_STATUS_FEATURE_NO_SUPPORT: return "Feature Not Supported";
1092 case MESH_STATUS_CANNOT_UPDATE: return "Cannot Update";
1093 case MESH_STATUS_CANNOT_REMOVE: return "Cannot Remove";
1094 case MESH_STATUS_CANNOT_BIND: return "Cannot bind";
1095 case MESH_STATUS_UNABLE_CHANGE_STATE: return "Unable to change state";
1096 case MESH_STATUS_CANNOT_SET: return "Cannot set";
1097 case MESH_STATUS_UNSPECIFIED_ERROR: return "Unspecified error";
1098 case MESH_STATUS_INVALID_BINDING: return "Invalid Binding";
1100 default: return "Unknown";
1104 static void __mesh_handle_model_subscription_event(int result,
1105 bluetooth_mesh_model_configure_t *param,
1106 const struct mesh_config_cmd *cmd)
1108 BT_INFO("Mesh: Model Subscription Event: Request [%d]", cmd->opcode);
1109 switch (cmd->opcode) {
1111 case MESH_OPCODE_CONFIG_MODEL_SUB_ADD:
1112 case MESH_OPCODE_CONFIG_MODEL_SUB_DELETE:
1113 case MESH_OPCODE_CONFIG_MODEL_SUB_OVERWRITE:
1114 /* Model Bind/UnBind Event */
1115 BT_INFO("Mesh: Handle Event for Request: BT_MESH_MODEL_CONFIG_GROUP_SUB");
1116 __bt_mesh_handle_pending_dev_config_request_info(result,
1117 BT_MESH_MODEL_CONFIG_GROUP_SUB, param,
1118 sizeof(bluetooth_mesh_model_configure_t));
1120 case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_ADD:
1121 case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_DELETE:
1122 case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_OVERWRITE:
1123 BT_INFO("Mesh: Handle Event for Request: BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB");
1124 __bt_mesh_handle_pending_dev_config_request_info(result,
1125 BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB, param,
1126 sizeof(bluetooth_mesh_model_configure_t));
1128 case MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL:
1129 BT_INFO("Mesh: Handle Event for Request: MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL");
1130 __bt_mesh_handle_pending_dev_config_request_info(result,
1131 BT_MESH_MODEL_CONFIG_GROUP_SUB, param,
1132 sizeof(bluetooth_mesh_model_configure_t));
1133 __bt_mesh_handle_pending_dev_config_request_info(result,
1134 BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB, param,
1135 sizeof(bluetooth_mesh_model_configure_t));
1142 void _bt_mesh_config_client_devkey_msg_handler(
1143 event_mesh_devkey_message_t *event)
1146 const struct mesh_config_cmd *cmd;
1147 uint16_t data_len = event->data_len;
1148 uint8_t *data = event->data;
1149 int result = BLUETOOTH_ERROR_NONE;
1157 struct mesh_pending_request *req;
1159 if (_bt_mesh_util_opcode_get(data, data_len, &opcode, &n) == false)
1162 BT_INFO("Mesh: Opcode of response data [0x%2.2x], actual data len [%d]", opcode, n);
1166 BT_INFO("Mesh: Received %s (len %u) opcode [0x%2.2x]",
1167 __mesh_get_opcode_string(opcode), data_len, opcode);
1169 req = __bt_mesh_get_request_by_response(event->source,
1170 event->net_uuid.uuid, (opcode & ~MESH_OPCODE_UNRELIABLE));
1172 BT_INFO("Mesh: Got Config Request");
1174 l_queue_remove(pending_requests, req);
1175 __mesh_request_remove(req);
1181 switch (opcode & ~MESH_OPCODE_UNRELIABLE) {
1184 case MESH_OPCODE_CONFIG_MODEL_PUB_STATUS: {
1185 if (data_len != 12 && data_len != 14)
1187 bluetooth_mesh_model_configure_t param;
1188 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1190 _bt_mesh_util_convert_hex_to_string((uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1191 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1193 BT_INFO("\nNode %4.4x Model Publication status %s\n",
1194 event->source, __mesh_status_to_string(data[0]));
1196 if (data[0] != MESH_STATUS_SUCCESS)
1197 result = BLUETOOTH_ERROR_INTERNAL;
1199 /* Extract Element Address */
1200 ele_addr = l_get_le16(data + 1);
1202 /* Extract Model ID */
1203 if (data_len == 14) {
1205 mod_id = l_get_le16(data + 10 + 2);
1206 mod_id = l_get_le16(data) << 16 | mod_id;
1209 mod_id = l_get_le16(data + 10);
1210 mod_id = MESH_VENDOR_ID_MASK | mod_id;
1213 param.model = mod_id;
1214 BT_INFO("Model ID\t%4.4x\n", mod_id);
1216 param.primary_unicast = event->source;
1217 param.elem_index = ele_addr - event->source;
1218 /* Extract Publish Address */
1219 param.pub_addr = l_get_le16(data + 3);
1220 BT_INFO("Mesh: Pub Address [0x%2.2x]", param.pub_addr);
1222 /* Extract Appkey Index */
1223 param.appkey_idx = l_get_le16(data + 5);
1224 BT_INFO("Mesh: AppKey Idx [0x%2.2x]", param.appkey_idx);
1227 param.ttl = data[6];
1228 BT_INFO("Mesh: TTL [0x%x]", param.ttl);
1230 /* Extract Period */
1231 param.period = data[7];
1235 if (cmd->opcode == MESH_OPCODE_CONFIG_MODEL_PUB_GET)
1236 __bt_mesh_handle_pending_dev_config_request_info(result,
1237 BT_MESH_MODEL_GET_PUBLICATION, ¶m,
1238 sizeof(bluetooth_mesh_model_configure_t));
1240 __bt_mesh_handle_pending_dev_config_request_info(result,
1241 BT_MESH_MODEL_SET_PUBLICATION, ¶m,
1242 sizeof(bluetooth_mesh_model_configure_t));
1245 case MESH_OPCODE_CONFIG_MODEL_SUB_STATUS: {
1246 if (data_len != 7 && data_len != 9)
1248 bluetooth_mesh_model_configure_t param;
1249 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1251 _bt_mesh_util_convert_hex_to_string(
1252 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1253 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1255 BT_INFO("\nNode %4.4x Subscription status %s\n",
1256 event->source, __mesh_status_to_string(data[0]));
1258 if (data[0] != MESH_STATUS_SUCCESS)
1259 result = BLUETOOTH_ERROR_INTERNAL;
1262 BT_INFO("Mesh: Command not found!!");
1266 ele_addr = l_get_le16(data + 1);
1267 addr = l_get_le16(data + 3);
1268 BT_INFO("Element Addr\t%4.4x\n", ele_addr);
1269 BT_INFO("Message Source Addr\t%4.4x\n", event->source);
1271 if (data_len == 9) {
1273 mod_id = l_get_le16(data + 5 + 2);
1274 mod_id = l_get_le16(data) << 16 | mod_id;
1277 mod_id = l_get_le16(data + 5);
1278 mod_id = MESH_VENDOR_ID_MASK | mod_id;
1281 param.primary_unicast = event->source;
1282 param.elem_index = ele_addr - event->source;
1283 /* Subscription address, unassigned address in case of Delete All command */
1284 param.sub_addr = addr;
1285 param.model = mod_id;
1286 BT_INFO("Subscr Addr\t%4.4x\n", addr);
1287 BT_INFO("Model ID\t%4.4x\n", mod_id);
1289 if (data[0] == MESH_STATUS_SUCCESS) {
1290 BT_INFO("_bt_mesh_network_add_model_subscription called or not");
1292 switch (cmd->opcode) {
1293 case MESH_OPCODE_CONFIG_MODEL_SUB_ADD:
1294 case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_ADD:
1295 if (!_bt_mesh_network_add_model_subscription(
1296 event->net_uuid.uuid, event->source,
1297 ele_addr - event->source, mod_id, addr)) {
1298 BT_INFO("Failed to add model subscription!");
1300 BT_INFO("_bt_mesh_network_add_model_subscription called or not");
1302 case MESH_OPCODE_CONFIG_MODEL_SUB_DELETE:
1303 case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_DELETE:
1304 if (!_bt_mesh_network_delete_model_subscription(
1305 event->net_uuid.uuid, event->source,
1306 ele_addr - event->source, mod_id, addr)) {
1307 BT_INFO("Failed to delete model subscription!");
1310 case MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL:
1311 if (!_bt_mesh_network_delete_all_model_subscription(
1312 event->net_uuid.uuid, event->source,
1313 ele_addr - event->source, mod_id)) {
1314 BT_INFO("Failed to delete all model subscription!");
1317 case MESH_OPCODE_CONFIG_MODEL_SUB_OVERWRITE:
1318 case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_OVERWRITE:
1319 if (!_bt_mesh_network_overwrite_model_subscription(
1320 event->net_uuid.uuid, event->source,
1321 ele_addr - event->source, mod_id, addr)) {
1322 BT_INFO("Failed to overwrite model subscription!");
1329 __mesh_handle_model_subscription_event(result, ¶m, cmd);
1333 case MESH_OPCODE_CONFIG_DEFAULT_TTL_STATUS: {
1336 bluetooth_mesh_node_ttl_info_t param;
1337 memset(¶m, 0x00, sizeof(bluetooth_mesh_node_ttl_info_t));
1339 BT_INFO("Node %4.4x Default TTL %d", event->source, data[0]);
1340 _bt_mesh_util_convert_hex_to_string(
1341 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1342 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1343 param.unicast = event->source;
1344 param.ttl = data[0];
1346 if (!_bt_mesh_network_save_remote_node_ttl(event->net_uuid.uuid,
1347 event->source, data[0])) {
1348 result = BLUETOOTH_ERROR_INTERNAL;
1349 BT_INFO("Failed to save node TTL");
1352 /* Remote Node TTL event */
1353 __bt_mesh_handle_pending_dev_config_request_info(result,
1354 BT_MESH_NODE_TTL_EXECUTE, ¶m,
1355 sizeof(bluetooth_mesh_node_ttl_info_t));
1358 case MESH_OPCODE_MODEL_APP_STATUS: {
1359 if (data_len != 7 && data_len != 9)
1361 bluetooth_mesh_model_configure_t param;
1362 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1364 _bt_mesh_util_convert_hex_to_string(
1365 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1366 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1367 BT_INFO("Node %4.4x: Model App status %s\n", event->source,
1368 __mesh_status_to_string(data[0]));
1370 if (data[0] != MESH_STATUS_SUCCESS)
1371 result = BLUETOOTH_ERROR_INTERNAL;
1373 elem_addr = l_get_le16(data + 1);
1374 app_idx = l_get_le16(data + 3);
1376 BT_INFO("Element Addr\t%4.4x", elem_addr);
1377 if (data_len == 9) {
1379 mod_id = l_get_le16(data + 5 + 2);
1380 mod_id = l_get_le16(data) << 16 | mod_id;
1383 mod_id = l_get_le16(data + 5);
1384 mod_id = MESH_VENDOR_ID_MASK | mod_id;
1387 param.primary_unicast = event->source;
1388 param.elem_index = elem_addr - event->source;
1389 param.appkey_idx = app_idx;
1390 param.model = mod_id;
1391 BT_INFO("AppIdx\t\t%u (0x%3.3x)\n ", app_idx, app_idx);
1393 /* Model Bind/UnBind Event */
1394 __bt_mesh_handle_pending_dev_config_request_info(result,
1395 BT_MESH_MODEL_CONFIGURE_APPKEY, ¶m,
1396 sizeof(bluetooth_mesh_model_configure_t));
1399 case MESH_OPCODE_MODEL_APP_LIST: {
1404 bluetooth_mesh_model_configure_t param;
1405 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1407 _bt_mesh_util_convert_hex_to_string(
1408 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1409 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1411 garray = g_array_new(FALSE, FALSE, sizeof(gchar));
1413 BT_INFO("\nNode %4.4x Model AppIdx status %s\n",
1414 event->source, __mesh_status_to_string(data[0]));
1416 BT_INFO("Element Addr\t%4.4x\n", l_get_le16(data + 1));
1417 BT_INFO("Model ID\t%4.4x\n", l_get_le16(data + 3));
1419 elem_addr = l_get_le16(data + 1);
1421 mod_id = l_get_le16(data + 3);
1422 mod_id = MESH_VENDOR_ID_MASK | mod_id;
1424 param.primary_unicast = event->source;
1425 param.elem_index = elem_addr - event->source;
1426 param.model = mod_id;
1431 while (data_len >= 3) {
1432 app_idx = l_get_le16(data) & 0xfff;
1433 g_array_append_vals(garray, &app_idx, sizeof(uint16_t));
1434 BT_INFO("\t%u (0x%3.3x)\n", app_idx, app_idx);
1436 app_idx = l_get_le16(data + 1) >> 4;
1437 g_array_append_vals(garray, &app_idx, sizeof(uint16_t));
1438 BT_INFO("\t%u (0x%3.3x)\n", app_idx, app_idx);
1443 if (data_len == 2) {
1444 app_idx = l_get_le16(data) & 0xfff;
1445 g_array_append_vals(garray, &app_idx, sizeof(uint16_t));
1446 BT_INFO("\t %u (0x%3.3x)\n", app_idx, app_idx);
1448 total = garray->len / sizeof(uint16_t);
1449 param.appkey_list = (uint16_t **)g_malloc0(sizeof(uint16_t*) * garray->len);
1450 param.appkeylist_count = total;
1451 for (int i = 0; i < total; i++) {
1452 param.appkey_list[i] = g_malloc(sizeof(uint16_t));
1453 *param.appkey_list[i] = g_array_index(garray, uint16_t, i);
1455 g_array_free(garray, TRUE);
1457 __bt_mesh_handle_pending_dev_config_request_info(result,
1458 BT_MESH_MODEL_GET_APPKEY_LIST, ¶m,
1459 sizeof(bluetooth_mesh_model_configure_t));
1462 case MESH_OPCODE_CONFIG_MODEL_SUB_LIST: {
1467 bluetooth_mesh_model_configure_t param;
1468 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1470 _bt_mesh_util_convert_hex_to_string((uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1471 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1473 BT_INFO("\nNode %4.4x BT SIG Model Subscription List status %s\n",
1474 event->source, __mesh_status_to_string(data[0]));
1476 BT_INFO("Element Addr\t%4.4x\n", l_get_le16(data + 1));
1477 elem_addr = l_get_le16(data + 1);
1480 mod_id = l_get_le16(data + 3);
1481 mod_id = MESH_VENDOR_ID_MASK | mod_id;
1482 BT_INFO("Model ID\t%4.4x\n", mod_id);
1484 param.primary_unicast = event->source;
1485 param.elem_index = elem_addr - event->source;
1486 param.model = mod_id;
1487 BT_INFO("Mesh: Data length of All Models Together [%d]", data_len - 5);
1488 total = (data_len - 5)/2;
1489 param.sublist_count = total;
1490 BT_INFO("Mesh: Total Number of subscriptions [%d]", total);
1492 param.sub_list = (uint16_t **)g_malloc0(sizeof(uint16_t*) * total);
1495 for (int k = 0; i < data_len; k++, i += 2) {
1496 BT_INFO("Mesh: Subscription Addr \t\t%4.4x\n ", l_get_le16(data + i));
1497 param.sub_list[k] = g_malloc(sizeof(uint16_t));
1498 *param.sub_list[k] = l_get_le16(data + i);
1502 /* <TO-DO> Update model sub in cdb */
1504 __bt_mesh_handle_pending_dev_config_request_info(result,
1505 BT_MESH_MODEL_GET_SUBSCRIPTION_LIST, ¶m,
1506 sizeof(bluetooth_mesh_model_configure_t));
1509 case MESH_OPCODE_CONFIG_VEND_MODEL_SUB_LIST: {
1514 bluetooth_mesh_model_configure_t param;
1515 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1517 _bt_mesh_util_convert_hex_to_string(
1518 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1519 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1521 BT_INFO("\nNode %4.4x Vendor Model Subscription List status %s\n",
1522 event->source, __mesh_status_to_string(data[0]));
1524 BT_INFO("Element Addr\t%4.4x\n", l_get_le16(data + 1));
1525 elem_addr = l_get_le16(data + 1);
1528 mod_id = l_get_le16(data + 5);
1529 mod_id = l_get_le16(data + 3) << 16 | mod_id;
1531 BT_INFO("Model ID\t%4.4x\n", mod_id);
1533 param.primary_unicast = event->source;
1534 param.elem_index = elem_addr - event->source;
1535 param.model = mod_id;
1537 BT_INFO("Mesh: Data length of All Models Together [%d]", data_len - 7);
1538 total = (data_len - 7)/2;
1539 param.sublist_count = total;
1541 BT_INFO("Mesh: Total Number of subscriptions [%d]", total);
1543 param.sub_list = (uint16_t **)g_malloc0(sizeof(uint16_t*) * total);
1546 for (int k = 0; i < data_len; k++, i += 2) {
1547 BT_INFO("Mesh: Subscription Addr \t\t%4.4x\n ", l_get_le16(data + i));
1548 param.sub_list[k] = g_malloc(sizeof(uint16_t));
1549 *param.sub_list[k] = l_get_le16(data + i);
1552 __bt_mesh_handle_pending_dev_config_request_info(result,
1553 BT_MESH_MODEL_GET_SUBSCRIPTION_LIST, ¶m,
1554 sizeof(bluetooth_mesh_model_configure_t));
1557 case MESH_OPCODE_DEV_COMP_STATUS: {
1558 if (data_len < MESH_MINIMUM_COMPOSITION_LEN)
1560 bluetooth_mesh_node_features_t features;
1561 memset(&features, 0x00, sizeof(bluetooth_mesh_node_features_t));
1562 BT_INFO("Mesh: Got Response for Device Composition Data");
1563 __bt_mesh_print_device_composition_data(data, data_len);
1565 if (!_bt_mesh_network_save_remote_node_composition(
1566 event->net_uuid.uuid, event->source, data, data_len)) {
1567 result = BLUETOOTH_ERROR_INTERNAL;
1568 BT_INFO("Failed to save node composition!");
1570 /* Browse Remote Node event */
1571 __bt_mesh_handle_pending_dev_config_request_info(result,
1572 BT_MESH_NODE_BROWSE, event, sizeof(event_mesh_devkey_message_t));
1574 /* Vendor Features Discover event */
1575 _bt_mesh_util_convert_hex_to_string(
1576 (uint8_t *) event->net_uuid.uuid, 16, features.net_uuid,
1577 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1578 features.unicast = event->source;
1579 BT_INFO("Mesh: Net UUID [%s]", features.net_uuid);
1581 BT_INFO("Mesh: Composition status for node unicast [0x%2.2x]",
1583 if (!_bt_mesh_node_get_vendor_features(event->net_uuid.uuid,
1584 event->source, &features)) {
1585 BT_ERR("Mesh: Failed to get Vendor Specific Infor for [0x%2.2x]",
1587 result = BLUETOOTH_ERROR_INTERNAL;
1589 BT_INFO("Mesh: Got Feature informations for [0x%2.2x]",
1592 __bt_mesh_handle_pending_dev_config_request_info(result,
1593 BT_MESH_NODE_GET_VENDOR_FEATURES, &features,
1594 sizeof(bluetooth_mesh_node_features_t));
1597 case MESH_OPCODE_NETKEY_STATUS: {
1601 bluetooth_mesh_key_configure_t param;
1602 memset(¶m, 0x00, sizeof(bluetooth_mesh_key_configure_t));
1604 BT_INFO("Mesh: Node %4.4x NetKey status %s",
1605 event->source, __mesh_status_to_string(data[0]));
1606 net_idx = l_get_le16(data + 1) & 0xfff;
1608 BT_INFO("\tNetKey %u (0x%3.3x)", net_idx, net_idx);
1611 result = BLUETOOTH_ERROR_INTERNAL;
1616 _bt_mesh_util_convert_hex_to_string(
1617 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1618 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1619 param.primary_unicast = event->source;
1620 param.netkey_idx = net_idx;
1621 param.is_netkey = true;
1623 BT_INFO("Mesh: Resp recvd from node unicast [0x%4.4x]", event->source);
1624 if (cmd->opcode == MESH_OPCODE_NETKEY_ADD) {
1625 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_NETKEY_ADD");
1626 if (result == BLUETOOTH_ERROR_NONE) {
1627 if (!_bt_mesh_network_save_remote_node_netkey(
1628 event->net_uuid.uuid, event->source, net_idx)) {
1629 result = BLUETOOTH_ERROR_INTERNAL;
1630 BT_INFO("Failed to save node Netkey!");
1633 param.op = BLUETOOTH_MESH_NODE_KEY_ADD;
1634 } else if (cmd->opcode == MESH_OPCODE_NETKEY_DELETE) {
1635 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_NETKEY_DELETE");
1636 if (result == BLUETOOTH_ERROR_NONE) {
1637 if (!_bt_mesh_network_delete_remote_node_netkey(
1638 event->net_uuid.uuid, event->source, net_idx)) {
1639 result = BLUETOOTH_ERROR_INTERNAL;
1640 BT_INFO("Failed to delete node Netkey!");
1643 param.op = BLUETOOTH_MESH_NODE_KEY_DELETE;
1644 } else if (cmd->opcode == MESH_OPCODE_NETKEY_UPDATE) {
1645 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_NETKEY_UPDATE");
1646 param.op = BLUETOOTH_MESH_NODE_KEY_UPDATE;
1648 /* Node Net Key Configure event */
1649 __bt_mesh_handle_pending_dev_config_request_info(result,
1650 BT_MESH_NODE_CONFIGURE_KEY,
1651 ¶m, sizeof(bluetooth_mesh_key_configure_t));
1654 case MESH_OPCODE_APPKEY_STATUS: {
1658 bluetooth_mesh_key_configure_t param;
1659 memset(¶m, 0x00, sizeof(bluetooth_mesh_key_configure_t));
1661 BT_INFO("Mesh: Node %4.4x AppKey status %s\n", event->source,
1662 __mesh_status_to_string(data[0]));
1663 net_idx = l_get_le16(data + 1) & 0xfff;
1664 app_idx = l_get_le16(data + 2) >> 4;
1666 BT_INFO("NetKey\t%u (0x%3.3x)\n", net_idx, net_idx);
1667 BT_INFO("AppKey\t%u (0x%3.3x)\n", app_idx, app_idx);
1669 if (data[0] != MESH_STATUS_SUCCESS)
1670 result = BLUETOOTH_ERROR_INTERNAL;
1675 _bt_mesh_util_convert_hex_to_string(
1676 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1677 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1679 param.primary_unicast = event->source;
1680 param.netkey_idx = net_idx;
1681 param.appkey_idx = app_idx;
1682 param.is_netkey = false;
1684 if (cmd->opcode == MESH_OPCODE_APPKEY_ADD) {
1685 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_APPKEY_ADD");
1686 if (result == BLUETOOTH_ERROR_NONE) {
1687 if (!_bt_mesh_network_save_remote_node_appkey(
1688 event->net_uuid.uuid, event->source,
1689 net_idx, app_idx)) {
1690 result = BLUETOOTH_ERROR_INTERNAL;
1691 BT_INFO("Failed to save node Appkey!");
1694 param.op = BLUETOOTH_MESH_NODE_KEY_ADD;
1695 } else if (cmd->opcode == MESH_OPCODE_APPKEY_DELETE) {
1696 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_APPKEY_DELETE");
1697 if (result == BLUETOOTH_ERROR_NONE) {
1698 if (!_bt_mesh_network_delete_remote_node_appkey(
1699 event->net_uuid.uuid, event->source,
1700 net_idx, app_idx)) {
1701 result = BLUETOOTH_ERROR_INTERNAL;
1702 BT_INFO("Failed to delete node Appkey!");
1705 param.op = BLUETOOTH_MESH_NODE_KEY_DELETE;
1706 } else if (cmd->opcode == MESH_OPCODE_APPKEY_UPDATE) {
1707 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_APPKEY_UPDATE");
1708 param.op = BLUETOOTH_MESH_NODE_KEY_UPDATE;
1711 /* Node App Key Configure event */
1712 __bt_mesh_handle_pending_dev_config_request_info(result,
1713 BT_MESH_NODE_CONFIGURE_KEY,
1714 ¶m, sizeof(bluetooth_mesh_key_configure_t));
1720 static gboolean __bt_mesh_vendor_feature_event_handler(gpointer data)
1722 bluetooth_mesh_node_features_t *result = (bluetooth_mesh_node_features_t*) data;
1723 __bt_mesh_handle_pending_dev_config_request_info(
1724 BLUETOOTH_ERROR_NONE,
1725 BT_MESH_NODE_GET_VENDOR_FEATURES,
1726 result, sizeof(bluetooth_mesh_node_features_t));
1731 int _bt_mesh_node_discover_vendor_features(const char *app_cred, const char *sender,
1732 bluetooth_mesh_node_features_t *req)
1734 int ret = OAL_STATUS_SUCCESS;
1736 uint16_t netkey_idx;
1738 oal_uuid_t net_uuid;
1739 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1741 BT_INFO("Mesh: Vendor features for Network UUID [%s]", req->net_uuid);
1742 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1743 strlen(req->net_uuid), net_uuid.uuid, 16);
1744 /* Check if Node's vendor features are already svaed or not */
1745 if (_bt_mesh_node_get_vendor_features(net_uuid.uuid, req->unicast, req)) {
1746 BT_INFO("Mesh: Vendor Features already available for Node: Unicast [0x%.2x]",
1748 /* Schedule event ot Application */
1749 bluetooth_mesh_node_features_t *event = \
1750 g_memdup2(req, sizeof(bluetooth_mesh_node_features_t));
1751 g_idle_add(__bt_mesh_vendor_feature_event_handler, (gpointer) event);
1752 return BLUETOOTH_ERROR_NONE;
1755 /* If Scanning is going on */
1756 if (_bt_mesh_is_provisioning() ||
1757 _bt_mesh_is_scanning()) {
1758 BT_ERR("Device is buzy..");
1759 return BLUETOOTH_ERROR_DEVICE_BUSY;
1762 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1763 strlen(req->net_uuid), net_uuid.uuid, 16);
1765 dest = req->unicast;
1766 BT_INFO("Mesh: Get Vendor Features for Remote Node Unicast [0x%2.2x]", dest);
1768 /* Check pending request */
1769 if (_bt_mesh_check_pending_request(MESH_OPCODE_DEV_COMP_GET,
1770 dest, net_uuid.uuid)) {
1771 BT_ERR("Device is buzy..");
1772 return BLUETOOTH_ERROR_DEVICE_BUSY;
1775 BT_INFO("Mesh: Browse Remote Node: Unicast [0x%2.2x]", dest);
1776 /* Get Subnet index of the rmeote node for TX encryption */
1777 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, dest);
1778 if (netkey_idx == MESH_NET_IDX_INVALID)
1779 return BLUETOOTH_ERROR_INTERNAL;
1781 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_DEV_COMP_GET,
1784 /* By default, use page 0 */
1785 buffer[data_len++] = 0;
1786 ret = mesh_conf_send_message(&net_uuid, dest, true,
1787 netkey_idx, buffer, data_len);
1789 if (ret != OAL_STATUS_SUCCESS) {
1790 BT_ERR("ret: %d", ret);
1791 return BLUETOOTH_ERROR_INTERNAL;
1794 /* Queue the request with timeout */
1795 __bt_mesh_add_request(MESH_OPCODE_DEV_COMP_GET,
1796 dest, net_uuid.uuid, NULL);
1797 return BLUETOOTH_ERROR_NONE;
1800 int _bt_mesh_browse_remote_node(const char *app_cred,
1802 bluetooth_mesh_node_discover_t *req)
1804 int ret = OAL_STATUS_SUCCESS;
1806 uint16_t netkey_idx;
1808 oal_uuid_t net_uuid;
1809 oal_uuid_t dev_uuid;
1810 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1812 /* If Scanning is going on */
1813 if (_bt_mesh_is_provisioning() ||
1814 _bt_mesh_is_scanning()) {
1815 BT_ERR("Device is buzy..");
1816 return BLUETOOTH_ERROR_DEVICE_BUSY;
1819 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1820 strlen(req->net_uuid), net_uuid.uuid, 16);
1821 _bt_mesh_util_convert_string_to_hex(req->dev_uuid,
1822 strlen(req->dev_uuid), dev_uuid.uuid, 16);
1824 BT_INFO("Mesh: Browse Node UUID [%s]", req->dev_uuid);
1826 /* Get Remote Node unicast address from Dev UUID */
1827 if (!_bt_mesh_node_get_unicast_from_dev_uuid(net_uuid.uuid,
1828 dev_uuid.uuid, &dest))
1829 return BLUETOOTH_ERROR_INTERNAL;
1831 BT_INFO("Mesh: Browse Remote Node: Unicast [0x%2.2x]", dest);
1832 /* Check pending request */
1833 if (_bt_mesh_check_pending_request(MESH_OPCODE_DEV_COMP_GET,
1834 dest, net_uuid.uuid)) {
1835 BT_ERR("Device is buzy..");
1836 return BLUETOOTH_ERROR_DEVICE_BUSY;
1839 /* Get Subnet index of the rmeote node for TX encryption */
1840 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, dest);
1841 if (netkey_idx == MESH_NET_IDX_INVALID)
1842 return BLUETOOTH_ERROR_INTERNAL;
1844 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_DEV_COMP_GET, buffer);
1846 /* By default, use page 0 */
1847 buffer[data_len++] = 0;
1848 ret = mesh_conf_send_message(&net_uuid, dest, true,
1849 netkey_idx, buffer, data_len);
1851 if (ret != OAL_STATUS_SUCCESS) {
1852 BT_ERR("ret: %d", ret);
1853 return BLUETOOTH_ERROR_INTERNAL;
1856 /* Queue the request with timeout */
1857 __bt_mesh_add_request(MESH_OPCODE_DEV_COMP_GET,
1858 dest, net_uuid.uuid, NULL);
1859 return BLUETOOTH_ERROR_NONE;
1862 int _bt_mesh_model_configure_group_subscription(const char *app_cred,
1863 const char *sender, bluetooth_mesh_model_configure_t *req)
1865 int ret = OAL_STATUS_SUCCESS;
1866 uint16_t netkey_idx;
1867 oal_uuid_t net_uuid;
1869 uint32_t opcode = 0;
1870 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1872 /* If Scanning is going on */
1873 if (_bt_mesh_is_provisioning() ||
1874 _bt_mesh_is_scanning()) {
1875 BT_ERR("Device is buzy..");
1876 return BLUETOOTH_ERROR_DEVICE_BUSY;
1879 BT_INFO("Mesh: group Subscription Req: [%d]", req->op);
1880 if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1881 BT_INFO("Mesh: group Subscription Addr [0x%2.2x]", req->sub_addr);
1883 /* Subscriptio address sanity check */
1884 if (!MESH_IS_GROUP(req->sub_addr) ||
1885 MESH_IS_VIRTUAL(req->sub_addr)) {
1886 BT_ERR("Mesh: Bad subscription address [0x%2.2x\n]",
1888 return BLUETOOTH_ERROR_INVALID_PARAM;
1891 BT_INFO("Mesh: group Subscription Req: Delete All");
1893 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1894 strlen(req->net_uuid), net_uuid.uuid, 16);
1895 /* Get Subnet index of the remote node for TX encryption */
1896 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid,
1897 req->primary_unicast);
1898 if (netkey_idx == MESH_NET_IDX_INVALID)
1899 return BLUETOOTH_ERROR_INTERNAL;
1902 if (req->op == BLUETOOTH_MESH_MODEL_SUB_ADD)
1903 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_ADD;
1904 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE)
1905 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE;
1906 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL)
1907 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL;
1908 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_OVERWRITE)
1909 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_OVERWRITE;
1911 /* Check pending request */
1912 if (_bt_mesh_check_pending_request(opcode,
1913 req->primary_unicast, net_uuid.uuid)) {
1914 BT_ERR("Device is buzy..");
1915 return BLUETOOTH_ERROR_DEVICE_BUSY;
1918 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
1920 BT_INFO("Mesh: Group Subscription Primary unicast [0x%2.2x]",
1921 req->primary_unicast);
1922 /* Element Address */
1923 l_put_le16((req->primary_unicast + req->elem_index),
1927 /* Subscription address */
1928 if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1929 l_put_le16(req->sub_addr, buffer + data_len);
1933 BT_INFO("Mesh: Group Subscription Model ID [0x%4.4x]", req->model);
1934 /* Insert Model ID */
1935 if (req->model >= 0xFFFF0000) {
1936 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
1937 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1940 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
1941 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
1943 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1947 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true,
1948 netkey_idx, buffer, data_len);
1950 if (ret != OAL_STATUS_SUCCESS) {
1951 BT_ERR("ret: %d", ret);
1952 return BLUETOOTH_ERROR_INTERNAL;
1955 BT_INFO("Mesh: Group Subscription Command sent successfully");
1956 /* Queue the request with timeout */
1957 __bt_mesh_add_request(opcode, req->primary_unicast,
1959 g_memdup2(req, sizeof(bluetooth_mesh_model_configure_t)));
1961 return BLUETOOTH_ERROR_NONE;
1964 int _bt_mesh_model_configure_virtual_group_subscription(
1965 const char *app_cred, const char *sender,
1966 bluetooth_mesh_model_configure_t *req)
1968 int ret = OAL_STATUS_SUCCESS;
1969 uint16_t netkey_idx;
1970 oal_uuid_t net_uuid;
1972 uint32_t opcode = 0;
1973 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1975 /* If Scanning is going on */
1976 if (_bt_mesh_is_provisioning() ||
1977 _bt_mesh_is_scanning()) {
1978 BT_ERR("Device is buzy..");
1979 return BLUETOOTH_ERROR_DEVICE_BUSY;
1982 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1983 strlen(req->net_uuid), net_uuid.uuid, 16);
1985 BT_INFO("Mesh: Virtual Group Subscription Req: [%d] Group Addr [0x%2.2x]",
1986 req->op, req->sub_addr);
1988 if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1989 /* Subscription address sanity check */
1990 if (MESH_IS_GROUP(req->sub_addr) ||
1991 !(MESH_IS_VIRTUAL(req->sub_addr))) {
1992 BT_ERR("Mesh: Bad Virtual Group subscription address [0x%2.2x\n]",
1994 return BLUETOOTH_ERROR_INVALID_PARAM;
1997 BT_INFO("Mesh: Virtual Group Subscription Req: Delete All");
1999 /* Get Subnet index of the remote node for TX encryption */
2000 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid,
2001 req->primary_unicast);
2002 if (netkey_idx == MESH_NET_IDX_INVALID)
2003 return BLUETOOTH_ERROR_INTERNAL;
2005 if (req->op == BLUETOOTH_MESH_MODEL_SUB_ADD)
2006 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_ADD;
2007 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE)
2008 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_DELETE;
2009 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_OVERWRITE)
2010 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_OVERWRITE;
2011 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL)
2012 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL;
2014 /* Check pending request */
2015 if (_bt_mesh_check_pending_request(opcode,
2016 req->primary_unicast, net_uuid.uuid)) {
2017 BT_ERR("Device is buzy..");
2018 return BLUETOOTH_ERROR_DEVICE_BUSY;
2021 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2023 /* Element Address */
2024 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2027 /* Subscription address */
2028 if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
2029 uint8_t label_uuid[16];
2030 if (!_bt_mesh_network_get_label_uuid_from_sub_addr(
2031 net_uuid.uuid, req->sub_addr, label_uuid)) {
2032 BT_ERR("Mesh: Virtual Group Label UUID Not found");
2033 return BLUETOOTH_ERROR_INVALID_PARAM;
2035 memcpy(buffer + data_len, label_uuid, 16);
2039 BT_INFO("Mesh: Virtual Group Subscription Model ID [0x%4.4x]", req->model);
2040 /* Insert Model ID */
2041 if (req->model >= 0xFFFF0000) {
2042 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2043 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2046 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2047 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2049 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2053 ret = mesh_conf_send_message(&net_uuid,
2054 req->primary_unicast, true,
2055 netkey_idx, buffer, data_len);
2057 if (ret != OAL_STATUS_SUCCESS) {
2058 BT_ERR("ret: %d", ret);
2059 return BLUETOOTH_ERROR_INTERNAL;
2062 BT_INFO("Mesh: Virtual Group Subscription Command sent successfully");
2063 /* Queue the request with timeout */
2064 __bt_mesh_add_request(opcode, req->primary_unicast,
2066 g_memdup2(req, sizeof(bluetooth_mesh_model_configure_t)));
2068 return BLUETOOTH_ERROR_NONE;
2071 int _bt_mesh_model_get_publication(const char *app_cred, const char *sender,
2072 bluetooth_mesh_model_configure_t *req)
2074 int ret = OAL_STATUS_SUCCESS;
2075 uint16_t netkey_idx;
2076 oal_uuid_t net_uuid;
2078 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2080 /* If Scanning is going on */
2081 if (_bt_mesh_is_provisioning() ||
2082 _bt_mesh_is_scanning()) {
2083 BT_ERR("Device is buzy..");
2084 return BLUETOOTH_ERROR_DEVICE_BUSY;
2087 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
2088 strlen(req->net_uuid), net_uuid.uuid, 16);
2090 /* Check pending request */
2091 if (_bt_mesh_check_pending_request(MESH_OPCODE_CONFIG_MODEL_PUB_GET,
2092 req->primary_unicast, net_uuid.uuid)) {
2093 BT_ERR("Device is buzy..");
2094 return BLUETOOTH_ERROR_DEVICE_BUSY;
2097 /* Get Subnet index of the remote node for TX encryption */
2098 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2099 if (netkey_idx == MESH_NET_IDX_INVALID)
2100 return BLUETOOTH_ERROR_INTERNAL;
2103 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_CONFIG_MODEL_PUB_GET, buffer);
2105 /* Element Address */
2106 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2109 /* Insert Model ID */
2110 if (req->model >= 0xFFFF0000) {
2111 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2112 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2115 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2116 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2118 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2122 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2124 if (ret != OAL_STATUS_SUCCESS) {
2125 BT_ERR("ret: %d", ret);
2126 return BLUETOOTH_ERROR_INTERNAL;
2129 /* Queue the request with timeout */
2130 __bt_mesh_add_request(MESH_OPCODE_CONFIG_MODEL_PUB_GET, req->primary_unicast, net_uuid.uuid,
2131 g_memdup2(req, sizeof(bluetooth_mesh_model_configure_t)));
2133 return BLUETOOTH_ERROR_NONE;
2136 int _bt_mesh_model_set_publication(const char *app_cred, const char *sender,
2137 bluetooth_mesh_model_configure_t *req)
2139 int ret = OAL_STATUS_SUCCESS;
2140 uint16_t netkey_idx;
2141 oal_uuid_t net_uuid;
2143 uint32_t opcode = 0;
2144 uint8_t label_uuid[16];
2145 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2147 /* If Scanning is going on */
2148 if (_bt_mesh_is_provisioning() ||
2149 _bt_mesh_is_scanning()) {
2150 BT_ERR("Device is buzy..");
2151 return BLUETOOTH_ERROR_DEVICE_BUSY;
2154 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2156 if (req->pub_addr == MESH_ALL_NODES_ADDRESS)
2157 BT_INFO("Mesh: Setting Publication to ALL Node Address");
2159 if (!MESH_IS_GROUP(req->pub_addr) && !MESH_IS_VIRTUAL(req->pub_addr) &&
2160 req->pub_addr != MESH_UNASSIGNED_ADDRESS) {
2161 BT_ERR("Mesh: Bad Publication address %x\n", req->pub_addr);
2162 return BLUETOOTH_ERROR_INVALID_PARAM;
2165 if (req->pub_addr != MESH_UNASSIGNED_ADDRESS) {
2166 if (MESH_IS_VIRTUAL(req->pub_addr)) {
2167 if (!_bt_mesh_network_get_label_uuid_from_sub_addr(
2168 net_uuid.uuid, req->pub_addr, label_uuid))
2169 return BLUETOOTH_ERROR_INVALID_PARAM;
2170 opcode = MESH_OPCODE_CONFIG_MODEL_PUB_VIRT_SET;
2172 } else if (MESH_IS_GROUP(req->pub_addr))
2173 opcode = MESH_OPCODE_CONFIG_MODEL_PUB_SET;
2176 /* Check pending request */
2177 if (_bt_mesh_check_pending_request(opcode,
2178 req->primary_unicast, net_uuid.uuid)) {
2179 BT_ERR("Device is buzy..");
2180 return BLUETOOTH_ERROR_DEVICE_BUSY;
2183 /* Get Subnet index of the remote node for TX encryption */
2184 netkey_idx = _bt_mesh_node_get_subnet_idx(
2185 net_uuid.uuid, req->primary_unicast);
2186 if (netkey_idx == MESH_NET_IDX_INVALID)
2187 return BLUETOOTH_ERROR_INTERNAL;
2190 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2192 /* Element Address */
2193 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2196 /* Fill Publication Address */
2197 if (MESH_IS_VIRTUAL(req->pub_addr)) {
2198 memcpy(buffer + data_len, label_uuid, 16);
2201 l_put_le16(req->pub_addr, buffer + data_len);
2205 /* AppKey index + credential (set to 0) */
2206 l_put_le16(req->appkey_idx, buffer + data_len);
2210 buffer[data_len++] = req->ttl;
2211 /* Publish period step count and step resolution */
2212 buffer[data_len++] = req->period;
2213 /* Publish retransmit count & interval steps */
2214 buffer[data_len++] = req->retransmit;
2216 /* Insert Model ID */
2217 if (req->model >= 0xFFFF0000) {
2218 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2219 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2222 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2223 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2225 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2229 ret = mesh_conf_send_message(&net_uuid,
2230 req->primary_unicast, true,
2231 netkey_idx, buffer, data_len);
2233 if (ret != OAL_STATUS_SUCCESS) {
2234 BT_ERR("ret: %d", ret);
2235 return BLUETOOTH_ERROR_INTERNAL;
2238 /* Queue the request with timeout */
2239 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2240 g_memdup2(req, sizeof(bluetooth_mesh_model_configure_t)));
2242 return BLUETOOTH_ERROR_NONE;
2245 int _bt_mesh_node_model_get_subscription_list(const char *app_cred, const char *sender,
2246 bluetooth_mesh_model_configure_t *req)
2248 int ret = OAL_STATUS_SUCCESS;
2249 uint16_t netkey_idx;
2250 oal_uuid_t net_uuid;
2252 uint32_t opcode = 0;
2253 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2255 /* If Scanning is going on */
2256 if (_bt_mesh_is_provisioning() ||
2257 _bt_mesh_is_scanning()) {
2258 BT_ERR("Device is buzy..");
2259 return BLUETOOTH_ERROR_DEVICE_BUSY;
2262 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2264 /* Insert Model ID */
2265 if (req->model >= 0xFFFF0000)
2266 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2267 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_GET;
2269 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2270 opcode = MESH_OPCODE_CONFIG_VEND_MODEL_SUB_GET;
2272 /* Check pending request */
2273 if (_bt_mesh_check_pending_request(opcode,
2274 req->primary_unicast, net_uuid.uuid)) {
2275 BT_ERR("Device is buzy..");
2276 return BLUETOOTH_ERROR_DEVICE_BUSY;
2278 /* Get Subnet index of the remote node for TX encryption */
2279 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2280 if (netkey_idx == MESH_NET_IDX_INVALID)
2281 return BLUETOOTH_ERROR_INTERNAL;
2283 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2284 /* Element Address */
2285 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2288 /* Insert Model ID */
2289 if (opcode == MESH_OPCODE_CONFIG_MODEL_SUB_GET) {
2290 BT_INFO("Mesh: Get Subscription List for BT SIG Model");
2291 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2292 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2295 BT_INFO("Mesh: Get Subscription List for Vendor Model");
2296 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2297 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2299 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2303 ret = mesh_conf_send_message(&net_uuid,
2304 req->primary_unicast, true, netkey_idx, buffer, data_len);
2306 if (ret != OAL_STATUS_SUCCESS) {
2307 BT_ERR("ret: %d", ret);
2308 return BLUETOOTH_ERROR_INTERNAL;
2311 /* Queue the request with timeout */
2312 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2313 g_memdup2(req, sizeof(bluetooth_mesh_model_configure_t)));
2315 return BLUETOOTH_ERROR_NONE;
2318 int _bt_mesh_node_model_get_appkey_list(const char *app_cred, const char *sender,
2319 bluetooth_mesh_model_configure_t *req)
2321 int ret = OAL_STATUS_SUCCESS;
2322 uint16_t netkey_idx;
2323 oal_uuid_t net_uuid;
2325 uint32_t opcode = 0;
2326 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2328 /* If Scanning is going on */
2329 if (_bt_mesh_is_provisioning() ||
2330 _bt_mesh_is_scanning()) {
2331 BT_ERR("Device is buzy..");
2332 return BLUETOOTH_ERROR_DEVICE_BUSY;
2335 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
2336 strlen(req->net_uuid), net_uuid.uuid, 16);
2338 /* Insert Model ID */
2339 if (req->model >= 0xFFFF0000)
2340 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2341 opcode = MESH_OPCODE_MODEL_APP_GET;
2343 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2344 opcode = MESH_OPCODE_VENDOR_MODEL_APP_GET;
2346 /* Check pending request */
2347 if (_bt_mesh_check_pending_request(opcode,
2348 req->primary_unicast, net_uuid.uuid)) {
2349 BT_ERR("Device is buzy..");
2350 return BLUETOOTH_ERROR_DEVICE_BUSY;
2352 /* Get Subnet index of the remote node for TX encryption */
2353 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2354 if (netkey_idx == MESH_NET_IDX_INVALID)
2355 return BLUETOOTH_ERROR_INTERNAL;
2357 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2358 /* Element Address */
2359 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2363 /* Insert Model ID */
2364 if (opcode == MESH_OPCODE_MODEL_APP_GET) {
2365 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2366 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2369 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2370 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2372 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2376 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2378 if (ret != OAL_STATUS_SUCCESS) {
2379 BT_ERR("ret: %d", ret);
2380 return BLUETOOTH_ERROR_INTERNAL;
2383 /* Queue the request with timeout */
2384 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2385 g_memdup2(req, sizeof(bluetooth_mesh_model_configure_t)));
2387 return BLUETOOTH_ERROR_NONE;
2390 int _bt_mesh_network_reset_node(const char *app_cred, const char *sender,
2391 bluetooth_mesh_node_info_t *node)
2393 int ret = OAL_STATUS_SUCCESS;
2394 uint16_t netkey_idx;
2395 oal_uuid_t net_uuid;
2397 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2399 /* If Scanning is going on */
2400 if (_bt_mesh_is_provisioning() ||
2401 _bt_mesh_is_scanning()) {
2402 BT_ERR("Device is buzy..");
2403 return BLUETOOTH_ERROR_DEVICE_BUSY;
2406 _bt_mesh_util_convert_string_to_hex(node->net_uuid,
2407 strlen(node->net_uuid), net_uuid.uuid, 16);
2409 /* Check pending request */
2410 if (_bt_mesh_check_pending_request(MESH_OPCODE_NODE_RESET,
2411 node->primary_unicast, net_uuid.uuid)) {
2412 BT_ERR("Device is buzy..");
2413 return BLUETOOTH_ERROR_DEVICE_BUSY;
2415 BT_INFO("Mesh: Reset a remote Node [0x%2.2x] Elem Cnt [0x%2.2x]",
2416 node->primary_unicast, node->num_elements);
2417 /* Get Subnet index of the remote node for TX encryption */
2418 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, node->primary_unicast);
2419 if (netkey_idx == MESH_NET_IDX_INVALID)
2420 return BLUETOOTH_ERROR_INTERNAL;
2422 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_NODE_RESET, buffer);
2424 /* Send Node Reset command 5 times to ensure reliability */
2425 for (int i = 0; i < 5; i++)
2426 ret = mesh_conf_send_message(&net_uuid, node->primary_unicast,
2427 true, netkey_idx, buffer, data_len);
2429 if (ret != OAL_STATUS_SUCCESS) {
2430 BT_ERR("Mesh: Node Reset Failed!: %d", ret);
2431 return BLUETOOTH_ERROR_INTERNAL;
2434 BT_INFO("Mesh: Node reset Done, Send Delete req: Unicast[0x%2.2x]",
2435 node->primary_unicast);
2437 ret = mesh_delete_remote_node(&net_uuid, node->primary_unicast,
2438 node->num_elements);
2439 if (ret != OAL_STATUS_SUCCESS) {
2440 BT_ERR("Mesh: Remote Node Deletion Failed!: %d", ret);
2441 return BLUETOOTH_ERROR_INTERNAL;
2444 return _bt_mesh_network_remove_node_configuration(node);
2447 int _bt_mesh_node_model_appkey_execute(const char *app_cred, const char *sender,
2448 bluetooth_mesh_model_configure_t *req)
2450 int ret = OAL_STATUS_SUCCESS;
2451 uint16_t netkey_idx;
2452 oal_uuid_t net_uuid;
2454 uint32_t opcode = 0;
2455 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2457 /* If Scanning is going on */
2458 if (_bt_mesh_is_provisioning() ||
2459 _bt_mesh_is_scanning()) {
2460 BT_ERR("Device is buzy..");
2461 return BLUETOOTH_ERROR_DEVICE_BUSY;
2464 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2466 /* Check pending request */
2467 if (_bt_mesh_check_pending_request(req->is_bind ? MESH_OPCODE_MODEL_APP_BIND : MESH_OPCODE_MODEL_APP_UNBIND,
2468 req->primary_unicast, net_uuid.uuid)) {
2469 BT_ERR("Device is buzy..");
2470 return BLUETOOTH_ERROR_DEVICE_BUSY;
2472 /* Get Subnet index of the remote node for TX encryption */
2473 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2474 if (netkey_idx == MESH_NET_IDX_INVALID)
2475 return BLUETOOTH_ERROR_INTERNAL;
2478 opcode = MESH_OPCODE_MODEL_APP_BIND;
2480 opcode = MESH_OPCODE_MODEL_APP_UNBIND;
2482 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2483 /* Element Address */
2484 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2488 l_put_le16(req->appkey_idx, buffer + data_len);
2491 /* Insert Model ID */
2492 if (req->model >= 0xFFFF0000) {
2493 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2494 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2497 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2498 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2500 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2504 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2506 if (ret != OAL_STATUS_SUCCESS) {
2507 BT_ERR("ret: %d", ret);
2508 return BLUETOOTH_ERROR_INTERNAL;
2511 /* Queue the request with timeout */
2512 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2513 g_memdup2(req, sizeof(bluetooth_mesh_model_configure_t)));
2515 return BLUETOOTH_ERROR_NONE;
2518 int _bt_mesh_ttl_execute_remote_node(const char *app_cred, const char *sender,
2519 bluetooth_mesh_node_ttl_info_t *req)
2521 int ret = OAL_STATUS_SUCCESS;
2522 uint16_t netkey_idx;
2524 oal_uuid_t net_uuid;
2525 uint32_t opcode = 0;
2526 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2528 /* If Scanning is going on */
2529 if (_bt_mesh_is_provisioning() ||
2530 _bt_mesh_is_scanning()) {
2531 BT_ERR("Device is buzy..");
2532 return BLUETOOTH_ERROR_DEVICE_BUSY;
2535 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2537 /* Check pending request */
2538 if (_bt_mesh_check_pending_request(req->is_set ? MESH_OPCODE_CONFIG_DEFAULT_TTL_SET : MESH_OPCODE_CONFIG_DEFAULT_TTL_GET,
2539 req->unicast, net_uuid.uuid)) {
2540 BT_ERR("Device is buzy..");
2541 return BLUETOOTH_ERROR_DEVICE_BUSY;
2544 /* Get Subnet index of the rmeote node for TX encryption */
2545 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->unicast);
2546 if (netkey_idx == MESH_NET_IDX_INVALID)
2547 return BLUETOOTH_ERROR_INTERNAL;
2550 opcode = MESH_OPCODE_CONFIG_DEFAULT_TTL_SET;
2551 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_CONFIG_DEFAULT_TTL_SET, buffer);
2552 buffer[data_len++] = req->ttl;
2554 opcode = MESH_OPCODE_CONFIG_DEFAULT_TTL_GET;
2555 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_CONFIG_DEFAULT_TTL_GET, buffer);
2558 ret = mesh_conf_send_message(&net_uuid, req->unicast, true, netkey_idx, buffer, data_len);
2560 if (ret != OAL_STATUS_SUCCESS) {
2561 BT_ERR("ret: %d", ret);
2562 return BLUETOOTH_ERROR_INTERNAL;
2565 /* Queue the request with timeout */
2566 __bt_mesh_add_request(opcode, req->unicast, net_uuid.uuid,
2567 g_memdup2(req, sizeof(bluetooth_mesh_node_ttl_info_t)));
2568 return BLUETOOTH_ERROR_NONE;
2571 static bool __bt_mesh_check_pending_key_cmd(uint16_t dest, bool is_netkey,
2572 bluetooth_mesh_node_key_conf_e op, uint8_t net_uuid[], uint32_t *opcode)
2574 const struct mesh_config_cmd *cmd;
2578 case BLUETOOTH_MESH_NODE_KEY_ADD:
2579 cmd = __mesh_get_command(MESH_OPCODE_NETKEY_ADD);
2582 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2583 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2586 *opcode = MESH_OPCODE_NETKEY_ADD;
2588 case BLUETOOTH_MESH_NODE_KEY_DELETE:
2589 cmd = __mesh_get_command(MESH_OPCODE_NETKEY_DELETE);
2592 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2593 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2596 *opcode = MESH_OPCODE_NETKEY_DELETE;
2598 case BLUETOOTH_MESH_NODE_KEY_UPDATE:
2599 cmd = __mesh_get_command(MESH_OPCODE_NETKEY_UPDATE);
2602 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2603 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2606 *opcode = MESH_OPCODE_NETKEY_UPDATE;
2611 case BLUETOOTH_MESH_NODE_KEY_ADD:
2612 cmd = __mesh_get_command(MESH_OPCODE_APPKEY_ADD);
2615 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2616 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2619 *opcode = MESH_OPCODE_APPKEY_ADD;
2621 case BLUETOOTH_MESH_NODE_KEY_DELETE:
2622 cmd = __mesh_get_command(MESH_OPCODE_APPKEY_DELETE);
2625 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2626 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2629 *opcode = MESH_OPCODE_APPKEY_DELETE;
2631 case BLUETOOTH_MESH_NODE_KEY_UPDATE:
2632 cmd = __mesh_get_command(MESH_OPCODE_APPKEY_UPDATE);
2635 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2636 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2639 *opcode = MESH_OPCODE_APPKEY_UPDATE;
2646 int _bt_mesh_node_configure_key(const char *app_cred, const char *sender,
2647 bluetooth_mesh_key_configure_t *req)
2649 int ret = OAL_STATUS_SUCCESS;
2650 uint16_t netkey_idx;
2651 uint16_t bound_netkey_idx = 0x0000;
2652 oal_uuid_t net_uuid;
2653 uint32_t opcode = 0;
2656 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2658 /* If Scanning is going on */
2659 if (_bt_mesh_is_provisioning() ||
2660 _bt_mesh_is_scanning()) {
2661 BT_ERR("Device is buzy..");
2662 return BLUETOOTH_ERROR_DEVICE_BUSY;
2665 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2667 if (req->is_netkey && !_bt_mesh_keys_subnet_exists(net_uuid.uuid, req->netkey_idx)) {
2668 BT_ERR("Local Subnet not found..");
2669 return BLUETOOTH_ERROR_INVALID_PARAM;
2672 /* For Appkey Configuration, check for available bound netkey */
2673 if (!req->is_netkey) {
2674 bound_netkey_idx = _bt_mesh_keys_get_bound_key(net_uuid.uuid, req->appkey_idx);
2675 if (bound_netkey_idx == MESH_NET_IDX_INVALID) {
2676 BT_ERR("Local AppKey not found..");
2677 return BLUETOOTH_ERROR_INVALID_PARAM;
2681 /* Check pending request */
2682 if (__bt_mesh_check_pending_key_cmd(req->primary_unicast,
2683 req->is_netkey, req->op, net_uuid.uuid, &opcode)) {
2684 BT_ERR("Device is buzy..");
2685 return BLUETOOTH_ERROR_DEVICE_BUSY;
2688 /* Get Subnet index of the rmeote node for TX encryption */
2689 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2690 if (netkey_idx == MESH_NET_IDX_INVALID)
2691 return BLUETOOTH_ERROR_INTERNAL;
2693 /* Handle Key (App/Net) Delete Commands: Configuration Message */
2694 if (opcode == MESH_OPCODE_NETKEY_DELETE || opcode == MESH_OPCODE_APPKEY_DELETE) {
2695 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2696 if (req->is_netkey) {
2697 l_put_le16(req->netkey_idx, buffer + data_len);
2700 buffer[data_len] = bound_netkey_idx;
2701 buffer[data_len + 1] = ((bound_netkey_idx >> 8) & 0xf) | ((req->appkey_idx << 4) & 0xf0);
2702 buffer[data_len + 2] = req->appkey_idx >> 4;
2706 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2708 /* Handle Key (App/Net) Update & Add Commands: Key Config message */
2709 update = (opcode == MESH_OPCODE_NETKEY_UPDATE || opcode == MESH_OPCODE_APPKEY_UPDATE);
2710 ret = mesh_conf_send_key_message(&net_uuid, req->primary_unicast, req->is_netkey,
2711 update, req->is_netkey ? req->netkey_idx : req->appkey_idx, netkey_idx);
2713 if (ret != OAL_STATUS_SUCCESS) {
2714 BT_ERR("ret: %d", ret);
2715 return BLUETOOTH_ERROR_INTERNAL;
2718 /* Queue the request with timeout */
2719 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2720 g_memdup2(req, sizeof(bluetooth_mesh_key_configure_t)));
2721 return BLUETOOTH_ERROR_NONE;