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));
326 BT_INFO("Mesh: Number of pending requests [%u] Remove the req",
327 l_queue_length(pending_requests));
328 l_queue_remove(pending_requests, req);
329 __mesh_request_remove(req);
332 static void __bt_mesh_add_request(uint32_t opcode, uint16_t dest,
333 uint8_t net_uuid[], void *data)
335 struct mesh_pending_request *req;
336 const struct mesh_config_cmd *cmd;
339 cmd = __mesh_get_command(opcode);
342 _bt_mesh_util_convert_hex_to_string((uint8_t *) net_uuid, 16, uuid_str,
343 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
344 BT_INFO("Mesh: Net UUID[%s]", uuid_str);
346 BT_INFO("Mesh: Adding command opcode [0x%2.2x] response [0x%2.2x]",
347 cmd->opcode, cmd->response);
348 req = l_new(struct mesh_pending_request, 1);
352 memcpy(req->net_uuid, net_uuid, 16);
353 req->timer = l_timeout_create(MESH_DEFAULT_RESPONSE_TIMEOUT,
354 __bt_mesh_wait_response_timeout, req, NULL);
356 if (!pending_requests)
357 pending_requests = l_queue_new();
358 l_queue_push_tail(pending_requests, req);
359 BT_INFO("Mesh: Number of pending requests [%u]", l_queue_length(pending_requests));
362 static struct mesh_pending_request *__bt_mesh_get_request_by_response(
363 uint16_t addr, uint8_t net_uuid[],
366 const struct l_queue_entry *entry;
370 BT_INFO("Mesh: Number of pending requests [%u]", l_queue_length(pending_requests));
371 entry = l_queue_get_entries(pending_requests);
373 for (; entry; entry = entry->next) {
374 struct mesh_pending_request *req = entry->data;
377 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);
378 BT_INFO("Mesh: Current req addr [0x%2.2x] res [0x%2.2x]", addr, response);
379 _bt_mesh_util_convert_hex_to_string((uint8_t *) net_uuid, 16, uuid_str,
380 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
381 BT_INFO("Mesh: Net UUID[%s]", uuid_str);
383 _bt_mesh_util_convert_hex_to_string((uint8_t *) req->net_uuid, 16, uuid_str1,
384 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
385 BT_INFO("Mesh: Net UUID1[%s]", uuid_str1);
386 if (!memcmp(net_uuid, req->net_uuid, 16) &&
388 req->cmd->response == response)
395 bool _bt_mesh_check_pending_request(uint32_t opcode,
396 uint16_t dest, uint8_t net_uuid[])
398 const struct mesh_config_cmd *cmd;
399 cmd = __mesh_get_command(opcode);
404 if (__bt_mesh_get_request_by_response(dest,
405 net_uuid, cmd->response)) {
406 BT_ERR("Mesh:Another command is pending\n");
412 static uint32_t __bt_mesh_print_model_identifier(uint8_t *data,
413 bool vendor, const char *offset)
418 mod_id = l_get_le16(data);
419 BT_INFO("%sModel ID\t%4.4x\n", offset, mod_id);
420 mod_id = MESH_VENDOR_ID_MASK | mod_id;
422 mod_id = l_get_le16(data + 2);
423 BT_INFO("%sModel ID\t%4.4x %4.4x\n", offset,
424 l_get_le16(data), mod_id);
425 mod_id = l_get_le16(data) << 16 | mod_id;
431 static void __bt_mesh_print_device_composition_data(
432 uint8_t *data, uint16_t len)
437 BT_INFO("Mesh: Received composion:\n");
439 /* skip page -- We only support Page Zero */
443 BT_INFO("\tCID: %4.4x", l_get_le16(&data[0]));
444 BT_INFO("\tPID: %4.4x", l_get_le16(&data[2]));
445 BT_INFO("\tVID: %4.4x", l_get_le16(&data[4]));
446 BT_INFO("\tCRPL: %4.4x", l_get_le16(&data[6]));
448 features = l_get_le16(&data[8]);
452 BT_INFO("\tFeature support:\n");
453 BT_INFO("\t\trelay: %s\n", (features & MESH_FEATURE_RELAY) ?
455 BT_INFO("\t\tproxy: %s\n", (features & MESH_FEATURE_PROXY) ?
457 BT_INFO("\t\tfriend: %s\n", (features & MESH_FEATURE_FRIEND) ?
459 BT_INFO("\t\tlpn: %s\n", (features & MESH_FEATURE_LPN) ?
464 BT_INFO("\t Element %d:\n", i);
465 BT_INFO("\t\tlocation: %4.4x\n", l_get_le16(data));
474 BT_INFO("\t\tSIG defined models:\n");
476 while (len >= 2 && m--) {
477 __bt_mesh_print_model_identifier(data, false, "\t\t ");
483 BT_INFO("\t\t Vendor defined models:\n");
485 while (len >= 4 && v--) {
486 __bt_mesh_print_model_identifier(data, true, "\t\t ");
495 static void __bt_mesh_send_model_publication_status_event(
496 int event, int result,
497 bluetooth_mesh_model_configure_t *evt)
499 GVariant *out_var = NULL, *param = NULL;
502 if (BLUETOOTH_ERROR_NONE == result) {
503 BT_INFO("Mesh: Model ID [0x%4.4x]", evt->model);
504 BT_INFO("Mesh: Model Publication Address [0x%2.2x]", evt->pub_addr);
506 info = g_array_new(FALSE, FALSE, sizeof(gchar));
507 g_array_append_vals(info, evt,
508 sizeof(bluetooth_mesh_model_configure_t));
510 out_var = g_variant_new_from_data((const GVariantType *)"ay",
511 info->data, info->len,
514 param = g_variant_new("(iv)", result, out_var);
515 _bt_send_event(BT_MESH_EVENT, event,
520 static void __bt_mesh_send_model_subscription_configure_event(
521 int event, int result,
522 bluetooth_mesh_model_configure_t *evt)
524 GVariant *out_var = NULL, *param = NULL;
527 if (BLUETOOTH_ERROR_NONE == result) {
529 info = g_array_new(FALSE, FALSE, sizeof(gchar));
530 g_array_append_vals(info, evt,
531 sizeof(bluetooth_mesh_model_configure_t));
533 out_var = g_variant_new_from_data((const GVariantType *)"ay",
534 info->data, info->len,
537 param = g_variant_new("(iv)", result, out_var);
538 _bt_send_event(BT_MESH_EVENT, event,
543 static void __bt_mesh_send_model_get_subscription_list_event(
544 int result, bluetooth_mesh_model_configure_t *evt)
546 GVariant *param = NULL;
547 GVariantBuilder *builder = NULL;
551 if (BLUETOOTH_ERROR_NONE == result) {
552 BT_INFO("Mesh: Total Subscriptions bound to model [%d]",
554 BT_INFO("Mesh: Network [%s]", evt->net_uuid);
555 builder = g_variant_builder_new(G_VARIANT_TYPE("aq"));
556 for (i = 0; i < evt->sublist_count; i++) {
557 BT_INFO("Mesh: Subscription Address [0x%2.2x]",
559 g_variant_builder_add(builder, "q", *evt->sub_list[i]);
562 net_uuid = g_strdup(evt->net_uuid);
563 param = g_variant_new("(isqiui(aq))", result, net_uuid,
564 evt->primary_unicast, evt->elem_index, evt->model,
565 evt->sublist_count, builder);
567 g_variant_builder_unref(builder);
570 _bt_send_event(BT_MESH_EVENT,
571 BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_LIST,
574 if (evt->sublist_count) {
576 for (int i = 0; i < evt->sublist_count; i++)
577 g_free(evt->sub_list[i]);
578 g_free(evt->sub_list);
581 BT_INFO("freed net uuid");
585 static void __bt_mesh_send_model_get_appkey_list_event(int result,
586 bluetooth_mesh_model_configure_t *evt)
588 GVariant *param = NULL;
589 GVariantBuilder *builder = NULL;
593 if (BLUETOOTH_ERROR_NONE == result) {
594 builder = g_variant_builder_new(G_VARIANT_TYPE("aq"));
595 BT_INFO("Mesh: Total AppKeys bound to model [%d]",
596 evt->appkeylist_count);
597 BT_INFO("Mesh: Network [%s]", evt->net_uuid);
598 for (i = 0; i < evt->appkeylist_count; i++)
599 g_variant_builder_add(builder, "q", *evt->appkey_list[i]);
600 net_uuid = g_strdup(evt->net_uuid);
601 param = g_variant_new("(isqiui(aq))", result, net_uuid,
602 evt->primary_unicast, evt->elem_index, evt->model,
603 evt->appkeylist_count, builder);
605 g_variant_builder_unref(builder);
608 _bt_send_event(BT_MESH_EVENT,
609 BLUETOOTH_EVENT_MESH_MODEL_APPKEY_LIST,
612 if (evt->appkeylist_count) {
613 for (int i = 0; i < evt->appkeylist_count; i++)
614 g_free(evt->appkey_list[i]);
616 g_free(evt->appkey_list);
623 static void __bt_mesh_send_model_configure_appkey_event(int result,
624 bluetooth_mesh_model_configure_t *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, evt, sizeof(bluetooth_mesh_model_configure_t));
634 out_var = g_variant_new_from_data((const GVariantType *)"ay",
635 info->data, info->len,
638 param = g_variant_new("(iv)", result, out_var);
639 _bt_send_event(BT_MESH_EVENT,
640 BLUETOOTH_EVENT_MESH_MODEL_APPKEY_BIND,
645 static void __bt_mesh_send_node_ttl_configuration_event(int result,
646 bluetooth_mesh_node_ttl_info_t *ttl_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, ttl_evt,
655 sizeof(bluetooth_mesh_node_ttl_info_t));
657 out_var = g_variant_new_from_data((const GVariantType *)"ay",
658 info->data, info->len,
661 param = g_variant_new("(iv)", result, out_var);
662 _bt_send_event(BT_MESH_EVENT,
663 BLUETOOTH_EVENT_MESH_NODE_TTL_CONFIGURED,
668 static void __bt_mesh_send_node_key_configuration_event(int result,
669 bluetooth_mesh_key_configure_t *key_evt)
671 GVariant *out_var = NULL, *param = NULL;
674 if (BLUETOOTH_ERROR_NONE == result) {
676 info = g_array_new(FALSE, FALSE, sizeof(gchar));
677 g_array_append_vals(info, key_evt,
678 sizeof(bluetooth_mesh_key_configure_t));
680 out_var = g_variant_new_from_data((const GVariantType *)"ay",
681 info->data, info->len,
684 param = g_variant_new("(iv)", result, out_var);
685 _bt_send_event(BT_MESH_EVENT,
686 BLUETOOTH_EVENT_MESH_NODE_KEY_CONFIGURED,
691 static void __bt_mesh_send_node_get_vendor_features_event(int result,
692 bluetooth_mesh_node_features_t *features_evt)
694 GVariant *out_var = NULL, *param = NULL;
697 if (BLUETOOTH_ERROR_NONE == result) {
699 info = g_array_new(FALSE, FALSE, sizeof(gchar));
700 g_array_append_vals(info, features_evt,
701 sizeof(bluetooth_mesh_node_features_t));
703 out_var = g_variant_new_from_data((const GVariantType *)"ay",
704 info->data, info->len,
707 param = g_variant_new("(iv)", result, out_var);
708 _bt_send_event(BT_MESH_EVENT,
709 BLUETOOTH_EVENT_MESH_NODE_VENDOR_FEATURES,
714 static void __bt_mesh_send_node_browsed_event(int result,
715 bluetooth_mesh_node_discover_t *browse_evt)
717 GVariant *out_var = NULL, *param = NULL;
720 if (BLUETOOTH_ERROR_NONE == result) {
722 info = g_array_new(FALSE, FALSE, sizeof(gchar));
723 g_array_append_vals(info, browse_evt,
724 sizeof(bluetooth_mesh_node_discover_t));
726 out_var = g_variant_new_from_data((const GVariantType *)"ay",
727 info->data, info->len,
730 param = g_variant_new("(iv)", result, out_var);
731 _bt_send_event(BT_MESH_EVENT,
732 BLUETOOTH_EVENT_MESH_NODE_BROWSED,
737 static void __bt_mesh_handle_pending_dev_config_request_info(int result,
738 int service_function, void *param, unsigned int size)
742 invocation_info_t *req_info = NULL;
744 for (l = _bt_get_invocation_list(); l != NULL; ) {
747 if (req_info == NULL ||
748 req_info->service_function != service_function)
751 switch (service_function) {
752 case BT_MESH_NODE_GET_VENDOR_FEATURES: {
753 bluetooth_mesh_node_features_t *event;
754 bluetooth_mesh_node_features_t *req;
756 event = (bluetooth_mesh_node_features_t*) param;
757 req = (bluetooth_mesh_node_features_t*)req_info->user_data;
759 BT_DBG("Request Sender: [%s]", req_info->sender);
760 /* Match Network and Remote Node unicast*/
761 if (!g_strcmp0(event->net_uuid, req->net_uuid) && event->unicast == req->unicast) {
762 event->unicast = req->unicast;
763 event->elem_count = req->elem_count;
766 __bt_mesh_send_node_get_vendor_features_event(result, event);
768 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
769 g_array_append_vals(out_param, event, sizeof(bluetooth_mesh_node_features_t));
771 /* Return DBUS Invocation*/
772 _bt_service_method_return(req_info->context, out_param, result);
773 _bt_free_info_from_invocation_list(req_info);
774 g_array_free(out_param, TRUE);
778 case BT_MESH_NODE_BROWSE: {
779 bluetooth_mesh_node_discover_t *node;
780 event_mesh_devkey_message_t *event;
781 uint16_t remote_addr;
783 uint8_t dev_uuid[16];
784 char net_uuid[BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1];
786 event = (event_mesh_devkey_message_t*) param;
787 node = (bluetooth_mesh_node_discover_t*)req_info->user_data;
788 BT_INFO("Mesh: Request Node UUID [%s]", node->dev_uuid);
789 BT_INFO("Mesh: Request Network UUID [%s]", node->net_uuid);
791 _bt_mesh_util_convert_hex_to_string((uint8_t *) event->net_uuid.uuid, 16, net_uuid,
792 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
794 _bt_mesh_util_convert_string_to_hex(node->dev_uuid, strlen(node->dev_uuid), dev_uuid, 16);
796 /* Get Unicast from pending request's Dev UUID and match with event */
797 if (_bt_mesh_node_get_unicast_from_dev_uuid(event->net_uuid.uuid, dev_uuid, &remote_addr)) {
799 BT_DBG("Request Sender: [%s]", req_info->sender);
800 /* Match Network and Remote Node unicast*/
801 if (!g_strcmp0(node->net_uuid, net_uuid) && remote_addr == event->source) {
802 _bt_mesh_node_get_element_count(event->net_uuid.uuid, remote_addr, &elem_count);
803 node->unicast = event->source;
804 node->count = elem_count;
805 BT_INFO("Mesh: Browse event for Node: Unicast [0x%2.2x] Element Count [%d]",
806 node->unicast, elem_count);
808 __bt_mesh_send_node_browsed_event(result, node);
810 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
811 g_array_append_vals(out_param, node, sizeof(bluetooth_mesh_node_discover_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);
822 case BT_MESH_NODE_CONFIGURE_KEY: {
823 bluetooth_mesh_key_configure_t *event;
824 bluetooth_mesh_key_configure_t *req;
826 event = (bluetooth_mesh_key_configure_t*) param;
827 req = (bluetooth_mesh_key_configure_t*)req_info->user_data;
829 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
830 req->primary_unicast == event->primary_unicast &&
831 req->is_netkey == event->is_netkey) {
833 __bt_mesh_send_node_key_configuration_event(result, event);
835 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
836 g_array_append_vals(out_param, event, sizeof(bluetooth_mesh_key_configure_t));
838 /* Return DBUS Invocation*/
839 _bt_service_method_return(req_info->context, out_param, result);
840 _bt_free_info_from_invocation_list(req_info);
841 g_array_free(out_param, TRUE);
845 case BT_MESH_NODE_TTL_EXECUTE: {
846 bluetooth_mesh_node_ttl_info_t *event;
847 bluetooth_mesh_node_ttl_info_t *req;
849 event = (bluetooth_mesh_node_ttl_info_t*) param;
850 req = (bluetooth_mesh_node_ttl_info_t*)req_info->user_data;
851 req->ttl = event->ttl;
853 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
854 req->unicast == event->unicast) {
856 __bt_mesh_send_node_ttl_configuration_event(result, req);
858 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
859 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_node_ttl_info_t));
861 /* Return DBUS Invocation*/
862 _bt_service_method_return(req_info->context, out_param, result);
863 _bt_free_info_from_invocation_list(req_info);
864 g_array_free(out_param, TRUE);
868 case BT_MESH_MODEL_CONFIGURE_APPKEY: {
869 bluetooth_mesh_model_configure_t *event;
870 bluetooth_mesh_model_configure_t *req;
872 event = (bluetooth_mesh_model_configure_t*) param;
873 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
875 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
876 req->primary_unicast == event->primary_unicast) {
878 __bt_mesh_send_model_configure_appkey_event(result, req);
880 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
881 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_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);
891 case BT_MESH_MODEL_GET_APPKEY_LIST: {
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_get_appkey_list_event(result, event);
903 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
904 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
906 /* Return DBUS Invocation*/
907 _bt_service_method_return(req_info->context, out_param, result);
908 _bt_free_info_from_invocation_list(req_info);
909 g_array_free(out_param, TRUE);
913 case BT_MESH_MODEL_GET_SUBSCRIPTION_LIST: {
914 bluetooth_mesh_model_configure_t *event;
915 bluetooth_mesh_model_configure_t *req;
917 BT_INFO("Mesh: Handle event for BT_MESH_MODEL_GET_SUBSCRIPTION_LIST");
918 event = (bluetooth_mesh_model_configure_t*) param;
919 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
921 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
922 req->primary_unicast == event->primary_unicast) {
924 __bt_mesh_send_model_get_subscription_list_event(result, event);
926 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
927 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
929 /* Return DBUS Invocation*/
930 _bt_service_method_return(req_info->context, out_param, result);
931 _bt_free_info_from_invocation_list(req_info);
932 g_array_free(out_param, TRUE);
936 case BT_MESH_MODEL_CONFIG_GROUP_SUB: {
937 bluetooth_mesh_model_configure_t *event;
938 bluetooth_mesh_model_configure_t *req;
940 event = (bluetooth_mesh_model_configure_t*) param;
941 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
942 BT_INFO("Mesh: Event Network [%s] Req Network [%s]",
943 event->net_uuid, req->net_uuid);
945 BT_INFO("Mesh: Event Unicast [0x%4.4x] Req Unicast [0x%4.4x]",
946 event->primary_unicast, req->primary_unicast);
948 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
949 req->primary_unicast == event->primary_unicast) {
951 req->sub_addr = event->sub_addr;
952 BT_INFO("Mesh: Send event for Model Subscription");
954 __bt_mesh_send_model_subscription_configure_event(
955 BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_CONFGURED,
958 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
959 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
961 /* Return DBUS Invocation*/
962 _bt_service_method_return(req_info->context, out_param, result);
963 _bt_free_info_from_invocation_list(req_info);
964 g_array_free(out_param, TRUE);
968 case BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB: {
969 bluetooth_mesh_model_configure_t *event;
970 bluetooth_mesh_model_configure_t *req;
972 event = (bluetooth_mesh_model_configure_t*) param;
973 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
974 BT_INFO("Mesh: Event Network [%s] Req Network [%s]",
975 event->net_uuid, req->net_uuid);
977 BT_INFO("Mesh: Event Unicast [0x%4.4x] Req Unicast [0x%4.4x]",
978 event->primary_unicast, req->primary_unicast);
979 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
980 req->primary_unicast == event->primary_unicast) {
982 __bt_mesh_send_model_subscription_configure_event( \
983 BLUETOOTH_EVENT_MESH_MODEL_VIRTUAL_SUBSCRIPTION_CONFGURED, \
986 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
987 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
989 /* Return DBUS Invocation*/
990 _bt_service_method_return(req_info->context, out_param, result);
991 _bt_free_info_from_invocation_list(req_info);
992 g_array_free(out_param, TRUE);
997 case BT_MESH_MODEL_GET_PUBLICATION: {
998 BT_INFO("Mesh: Event for Model Get Publication Request");
999 bluetooth_mesh_model_configure_t *event;
1000 bluetooth_mesh_model_configure_t *req;
1002 event = (bluetooth_mesh_model_configure_t*) param;
1003 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
1005 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
1006 req->primary_unicast == event->primary_unicast) {
1008 BT_INFO("Mesh: Send Model Publication status for Get Request");
1009 __bt_mesh_send_model_publication_status_event( \
1010 BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS, \
1013 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1014 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
1016 /* Return DBUS Invocation*/
1017 _bt_service_method_return(req_info->context, out_param, result);
1018 _bt_free_info_from_invocation_list(req_info);
1019 g_array_free(out_param, TRUE);
1023 case BT_MESH_MODEL_SET_PUBLICATION: {
1024 BT_INFO("Mesh: Event for Model Set Publication Request");
1025 bluetooth_mesh_model_configure_t *event;
1026 bluetooth_mesh_model_configure_t *req;
1028 event = (bluetooth_mesh_model_configure_t*) param;
1029 req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
1031 if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
1032 req->primary_unicast == event->primary_unicast) {
1034 BT_INFO("Mesh: Send Model Publication status for Set Request");
1035 __bt_mesh_send_model_publication_status_event( \
1036 BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS, \
1039 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1040 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
1042 /* Return DBUS Invocation*/
1043 _bt_service_method_return(req_info->context, out_param, result);
1044 _bt_free_info_from_invocation_list(req_info);
1045 g_array_free(out_param, TRUE);
1050 BT_DBG("Unknown function(%d)", service_function);
1056 const char *__mesh_status_to_string(uint8_t err)
1059 case MESH_STATUS_SUCCESS: return "Success";
1060 case MESH_STATUS_INVALID_ADDRESS: return "Invalid Address";
1061 case MESH_STATUS_INVALID_MODEL: return "Invalid Model";
1062 case MESH_STATUS_INVALID_APPKEY: return "Invalid AppKey";
1063 case MESH_STATUS_INVALID_NETKEY: return "Invalid NetKey";
1064 case MESH_STATUS_INSUFF_RESOURCES: return "Insufficient Resources";
1065 case MESH_STATUS_IDX_ALREADY_STORED: return "Key Idx Already Stored";
1066 case MESH_STATUS_INVALID_PUB_PARAM: return "Invalid Publish Parameters";
1067 case MESH_STATUS_NOT_SUB_MOD: return "Not a Subscribe Model";
1068 case MESH_STATUS_STORAGE_FAIL: return "Storage Failure";
1069 case MESH_STATUS_FEATURE_NO_SUPPORT: return "Feature Not Supported";
1070 case MESH_STATUS_CANNOT_UPDATE: return "Cannot Update";
1071 case MESH_STATUS_CANNOT_REMOVE: return "Cannot Remove";
1072 case MESH_STATUS_CANNOT_BIND: return "Cannot bind";
1073 case MESH_STATUS_UNABLE_CHANGE_STATE: return "Unable to change state";
1074 case MESH_STATUS_CANNOT_SET: return "Cannot set";
1075 case MESH_STATUS_UNSPECIFIED_ERROR: return "Unspecified error";
1076 case MESH_STATUS_INVALID_BINDING: return "Invalid Binding";
1078 default: return "Unknown";
1082 static void __mesh_handle_model_subscription_event(int result,
1083 bluetooth_mesh_model_configure_t *param,
1084 const struct mesh_config_cmd *cmd)
1086 BT_INFO("Mesh: Model Subscription Event: Request [%d]", cmd->opcode);
1087 switch (cmd->opcode) {
1089 case MESH_OPCODE_CONFIG_MODEL_SUB_ADD:
1090 case MESH_OPCODE_CONFIG_MODEL_SUB_DELETE:
1091 case MESH_OPCODE_CONFIG_MODEL_SUB_OVERWRITE:
1092 /* Model Bind/UnBind Event */
1093 BT_INFO("Mesh: Handle Event for Request: BT_MESH_MODEL_CONFIG_GROUP_SUB");
1094 __bt_mesh_handle_pending_dev_config_request_info(result,
1095 BT_MESH_MODEL_CONFIG_GROUP_SUB, param,
1096 sizeof(bluetooth_mesh_model_configure_t));
1098 case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_ADD:
1099 case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_DELETE:
1100 case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_OVERWRITE:
1101 BT_INFO("Mesh: Handle Event for Request: BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB");
1102 __bt_mesh_handle_pending_dev_config_request_info(result,
1103 BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB, param,
1104 sizeof(bluetooth_mesh_model_configure_t));
1106 case MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL:
1107 BT_INFO("Mesh: Handle Event for Request: MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL");
1108 __bt_mesh_handle_pending_dev_config_request_info(result,
1109 BT_MESH_MODEL_CONFIG_GROUP_SUB, param,
1110 sizeof(bluetooth_mesh_model_configure_t));
1111 __bt_mesh_handle_pending_dev_config_request_info(result,
1112 BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB, param,
1113 sizeof(bluetooth_mesh_model_configure_t));
1120 void _bt_mesh_config_client_devkey_msg_handler(
1121 event_mesh_devkey_message_t *event)
1124 const struct mesh_config_cmd *cmd;
1125 uint16_t data_len = event->data_len;
1126 uint8_t *data = event->data;
1127 int result = BLUETOOTH_ERROR_NONE;
1135 struct mesh_pending_request *req;
1137 if (_bt_mesh_util_opcode_get(data, data_len, &opcode, &n)) {
1138 BT_INFO("Mesh: Opcode of response data [0x%2.2x], actual data len [%d]", opcode, n);
1144 BT_INFO("Mesh: Received %s (len %u) opcode [0x%2.2x]",
1145 __mesh_get_opcode_string(opcode), data_len, opcode);
1147 req = __bt_mesh_get_request_by_response(event->source,
1148 event->net_uuid.uuid, (opcode & ~MESH_OPCODE_UNRELIABLE));
1150 BT_INFO("Mesh: Got Config Request");
1152 __mesh_request_remove(req);
1153 l_queue_remove(pending_requests, req);
1158 switch (opcode & ~MESH_OPCODE_UNRELIABLE) {
1161 case MESH_OPCODE_CONFIG_MODEL_PUB_STATUS: {
1162 if (data_len != 12 && data_len != 14)
1164 bluetooth_mesh_model_configure_t param;
1165 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1167 _bt_mesh_util_convert_hex_to_string((uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1168 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1170 BT_INFO("\nNode %4.4x Model Publication status %s\n",
1171 event->source, __mesh_status_to_string(data[0]));
1173 if (data[0] != MESH_STATUS_SUCCESS)
1174 result = BLUETOOTH_ERROR_INTERNAL;
1176 /* Extract Element Address */
1177 ele_addr = l_get_le16(data + 1);
1179 /* Extract Model ID */
1180 if (data_len == 14) {
1182 mod_id = l_get_le16(data + 10 + 2);
1183 mod_id = l_get_le16(data) << 16 | mod_id;
1186 mod_id = l_get_le16(data + 10);
1187 mod_id = MESH_VENDOR_ID_MASK | mod_id;
1190 param.model = mod_id;
1191 BT_INFO("Model ID\t%4.4x\n", mod_id);
1193 param.primary_unicast = event->source;
1194 param.elem_index = ele_addr - event->source;
1195 /* Extract Publish Address */
1196 param.pub_addr = l_get_le16(data + 3);
1197 BT_INFO("Mesh: Pub Address [0x%2.2x]", param.pub_addr);
1199 /* Extract Appkey Index */
1200 param.appkey_idx = l_get_le16(data + 5);
1201 BT_INFO("Mesh: AppKey Idx [0x%2.2x]", param.appkey_idx);
1204 param.ttl = data[6];
1205 BT_INFO("Mesh: TTL [0x%x]", param.ttl);
1207 /* Extract Period */
1208 param.period = data[7];
1212 if (cmd->opcode == MESH_OPCODE_CONFIG_MODEL_PUB_GET)
1213 __bt_mesh_handle_pending_dev_config_request_info(result,
1214 BT_MESH_MODEL_GET_PUBLICATION, ¶m,
1215 sizeof(bluetooth_mesh_model_configure_t));
1217 __bt_mesh_handle_pending_dev_config_request_info(result,
1218 BT_MESH_MODEL_SET_PUBLICATION, ¶m,
1219 sizeof(bluetooth_mesh_model_configure_t));
1222 case MESH_OPCODE_CONFIG_MODEL_SUB_STATUS: {
1223 if (data_len != 7 && data_len != 9)
1225 bluetooth_mesh_model_configure_t param;
1226 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1228 _bt_mesh_util_convert_hex_to_string(
1229 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1230 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1232 BT_INFO("\nNode %4.4x Subscription status %s\n",
1233 event->source, __mesh_status_to_string(data[0]));
1235 if (data[0] != MESH_STATUS_SUCCESS)
1236 result = BLUETOOTH_ERROR_INTERNAL;
1239 BT_INFO("Mesh: Command not found!!");
1243 ele_addr = l_get_le16(data + 1);
1244 addr = l_get_le16(data + 3);
1245 BT_INFO("Element Addr\t%4.4x\n", ele_addr);
1246 BT_INFO("Message Source Addr\t%4.4x\n", event->source);
1248 if (data_len == 9) {
1250 mod_id = l_get_le16(data + 5 + 2);
1251 mod_id = l_get_le16(data) << 16 | mod_id;
1254 mod_id = l_get_le16(data + 5);
1255 mod_id = MESH_VENDOR_ID_MASK | mod_id;
1258 param.primary_unicast = event->source;
1259 param.elem_index = ele_addr - event->source;
1260 /* Subscription address, unassigned address in case of Delete All command */
1261 param.sub_addr = addr;
1262 param.model = mod_id;
1263 BT_INFO("Subscr Addr\t%4.4x\n", addr);
1264 BT_INFO("Model ID\t%4.4x\n", mod_id);
1267 __mesh_handle_model_subscription_event(result, ¶m, cmd);
1271 case MESH_OPCODE_CONFIG_DEFAULT_TTL_STATUS: {
1274 bluetooth_mesh_node_ttl_info_t param;
1275 memset(¶m, 0x00, sizeof(bluetooth_mesh_node_ttl_info_t));
1277 BT_INFO("Node %4.4x Default TTL %d", event->source, data[0]);
1278 _bt_mesh_util_convert_hex_to_string(
1279 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1280 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1281 param.unicast = event->source;
1282 param.ttl = data[0];
1284 if (!_bt_mesh_network_save_remote_node_ttl(event->net_uuid.uuid,
1285 event->source, data[0])) {
1286 result = BLUETOOTH_ERROR_INTERNAL;
1287 BT_INFO("Failed to save node TTL");
1290 /* Remote Node TTL event */
1291 __bt_mesh_handle_pending_dev_config_request_info(result,
1292 BT_MESH_NODE_TTL_EXECUTE, ¶m,
1293 sizeof(bluetooth_mesh_node_ttl_info_t));
1296 case MESH_OPCODE_MODEL_APP_STATUS: {
1297 if (data_len != 7 && data_len != 9)
1299 bluetooth_mesh_model_configure_t param;
1300 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1302 _bt_mesh_util_convert_hex_to_string(
1303 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1304 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1305 BT_INFO("Node %4.4x: Model App status %s\n", event->source,
1306 __mesh_status_to_string(data[0]));
1308 if (data[0] != MESH_STATUS_SUCCESS)
1309 result = BLUETOOTH_ERROR_INTERNAL;
1311 elem_addr = l_get_le16(data + 1);
1312 app_idx = l_get_le16(data + 3);
1314 BT_INFO("Element Addr\t%4.4x", elem_addr);
1315 if (data_len == 9) {
1317 mod_id = l_get_le16(data + 5 + 2);
1318 mod_id = l_get_le16(data) << 16 | mod_id;
1321 mod_id = l_get_le16(data + 5);
1322 mod_id = MESH_VENDOR_ID_MASK | mod_id;
1325 param.primary_unicast = event->source;
1326 param.elem_index = elem_addr - event->source;
1327 param.appkey_idx = app_idx;
1328 param.model = mod_id;
1329 BT_INFO("AppIdx\t\t%u (0x%3.3x)\n ", app_idx, app_idx);
1331 /* Model Bind/UnBind Event */
1332 __bt_mesh_handle_pending_dev_config_request_info(result,
1333 BT_MESH_MODEL_CONFIGURE_APPKEY, ¶m,
1334 sizeof(bluetooth_mesh_model_configure_t));
1337 case MESH_OPCODE_MODEL_APP_LIST: {
1342 bluetooth_mesh_model_configure_t param;
1343 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1345 _bt_mesh_util_convert_hex_to_string(
1346 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1347 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1349 garray = g_array_new(FALSE, FALSE, sizeof(gchar));
1351 BT_INFO("\nNode %4.4x Model AppIdx status %s\n",
1352 event->source, __mesh_status_to_string(data[0]));
1354 BT_INFO("Element Addr\t%4.4x\n", l_get_le16(data + 1));
1355 BT_INFO("Model ID\t%4.4x\n", l_get_le16(data + 3));
1357 elem_addr = l_get_le16(data + 1);
1359 mod_id = l_get_le16(data + 3);
1360 mod_id = MESH_VENDOR_ID_MASK | mod_id;
1362 param.primary_unicast = event->source;
1363 param.elem_index = elem_addr - event->source;
1364 param.model = mod_id;
1369 while (data_len >= 3) {
1370 app_idx = l_get_le16(data) & 0xfff;
1371 g_array_append_vals(garray, &app_idx, sizeof(uint16_t));
1372 BT_INFO("\t%u (0x%3.3x)\n", app_idx, app_idx);
1374 app_idx = l_get_le16(data + 1) >> 4;
1375 g_array_append_vals(garray, &app_idx, sizeof(uint16_t));
1376 BT_INFO("\t%u (0x%3.3x)\n", app_idx, app_idx);
1381 if (data_len == 2) {
1382 app_idx = l_get_le16(data) & 0xfff;
1383 g_array_append_vals(garray, &app_idx, sizeof(uint16_t));
1384 BT_INFO("\t %u (0x%3.3x)\n", app_idx, app_idx);
1386 total = garray->len / sizeof(uint16_t);
1387 param.appkey_list = (uint16_t **)g_malloc0(sizeof(uint16_t*) * garray->len);
1388 param.appkeylist_count = total;
1389 for (int i = 0; i < total; i++) {
1390 param.appkey_list[i] = g_malloc(sizeof(uint16_t));
1391 *param.appkey_list[i] = g_array_index(garray, uint16_t, i);
1393 g_array_free(garray, TRUE);
1395 __bt_mesh_handle_pending_dev_config_request_info(result,
1396 BT_MESH_MODEL_GET_APPKEY_LIST, ¶m,
1397 sizeof(bluetooth_mesh_model_configure_t));
1400 case MESH_OPCODE_CONFIG_MODEL_SUB_LIST: {
1405 bluetooth_mesh_model_configure_t param;
1406 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1408 _bt_mesh_util_convert_hex_to_string((uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1409 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1411 BT_INFO("\nNode %4.4x BT SIG Model Subscription List status %s\n",
1412 event->source, __mesh_status_to_string(data[0]));
1414 BT_INFO("Element Addr\t%4.4x\n", l_get_le16(data + 1));
1415 elem_addr = l_get_le16(data + 1);
1418 mod_id = l_get_le16(data + 3);
1419 mod_id = MESH_VENDOR_ID_MASK | mod_id;
1420 BT_INFO("Model ID\t%4.4x\n", mod_id);
1422 param.primary_unicast = event->source;
1423 param.elem_index = elem_addr - event->source;
1424 param.model = mod_id;
1425 BT_INFO("Mesh: Data length of All Models Together [%d]", data_len - 5);
1426 total = (data_len - 5)/2;
1427 param.sublist_count = total;
1428 BT_INFO("Mesh: Total Number of subscriptions [%d]", total);
1430 param.sub_list = (uint16_t **)g_malloc0(sizeof(uint16_t*) * total);
1433 for (int k = 0; i < data_len; k++, i += 2) {
1434 BT_INFO("Mesh: Subscription Addr \t\t%4.4x\n ", l_get_le16(data + i));
1435 param.sub_list[k] = g_malloc(sizeof(uint16_t));
1436 *param.sub_list[k] = l_get_le16(data + i);
1439 __bt_mesh_handle_pending_dev_config_request_info(result,
1440 BT_MESH_MODEL_GET_SUBSCRIPTION_LIST, ¶m,
1441 sizeof(bluetooth_mesh_model_configure_t));
1444 case MESH_OPCODE_CONFIG_VEND_MODEL_SUB_LIST: {
1449 bluetooth_mesh_model_configure_t param;
1450 memset(¶m, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1452 _bt_mesh_util_convert_hex_to_string(
1453 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1454 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1456 BT_INFO("\nNode %4.4x Vendor Model Subscription List status %s\n",
1457 event->source, __mesh_status_to_string(data[0]));
1459 BT_INFO("Element Addr\t%4.4x\n", l_get_le16(data + 1));
1460 elem_addr = l_get_le16(data + 1);
1463 mod_id = l_get_le16(data + 5);
1464 mod_id = l_get_le16(data + 3) << 16 | mod_id;
1466 BT_INFO("Model ID\t%4.4x\n", mod_id);
1468 param.primary_unicast = event->source;
1469 param.elem_index = elem_addr - event->source;
1470 param.model = mod_id;
1472 BT_INFO("Mesh: Data length of All Models Together [%d]", data_len - 7);
1473 total = (data_len - 7)/2;
1474 param.sublist_count = total;
1476 BT_INFO("Mesh: Total Number of subscriptions [%d]", total);
1478 param.sub_list = (uint16_t **)g_malloc0(sizeof(uint16_t*) * total);
1481 for (int k = 0; i < data_len; k++, i += 2) {
1482 BT_INFO("Mesh: Subscription Addr \t\t%4.4x\n ", l_get_le16(data + i));
1483 param.sub_list[k] = g_malloc(sizeof(uint16_t));
1484 *param.sub_list[k] = l_get_le16(data + i);
1487 __bt_mesh_handle_pending_dev_config_request_info(result,
1488 BT_MESH_MODEL_GET_SUBSCRIPTION_LIST, ¶m,
1489 sizeof(bluetooth_mesh_model_configure_t));
1492 case MESH_OPCODE_DEV_COMP_STATUS: {
1493 if (data_len < MESH_MINIMUM_COMPOSITION_LEN)
1495 bluetooth_mesh_node_features_t features;
1496 memset(&features, 0x00, sizeof(bluetooth_mesh_node_features_t));
1497 BT_INFO("Mesh: Got Response for Device Composition Data");
1498 __bt_mesh_print_device_composition_data(data, data_len);
1500 if (!_bt_mesh_network_save_remote_node_composition(
1501 event->net_uuid.uuid, event->source, data, data_len)) {
1502 result = BLUETOOTH_ERROR_INTERNAL;
1503 BT_INFO("Failed to save node composition!");
1505 /* Browse Remote Node event */
1506 __bt_mesh_handle_pending_dev_config_request_info(result,
1507 BT_MESH_NODE_BROWSE, event, sizeof(event_mesh_devkey_message_t));
1509 /* Vendor Features Discover event */
1510 _bt_mesh_util_convert_hex_to_string(
1511 (uint8_t *) event->net_uuid.uuid, 16, features.net_uuid,
1512 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1513 features.unicast = event->source;
1514 BT_INFO("Mesh: Net UUID [%s]", features.net_uuid);
1516 BT_INFO("Mesh: Composition status for node unicast [0x%2.2x]",
1518 if (!_bt_mesh_node_get_vendor_features(event->net_uuid.uuid,
1519 event->source, &features)) {
1520 BT_ERR("Mesh: Failed to get Vendor Specific Infor for [0x%2.2x]",
1522 result = BLUETOOTH_ERROR_INTERNAL;
1524 BT_INFO("Mesh: Got Feature informations for [0x%2.2x]",
1527 __bt_mesh_handle_pending_dev_config_request_info(result,
1528 BT_MESH_NODE_GET_VENDOR_FEATURES, &features,
1529 sizeof(bluetooth_mesh_node_features_t));
1532 case MESH_OPCODE_NETKEY_STATUS: {
1536 bluetooth_mesh_key_configure_t param;
1537 memset(¶m, 0x00, sizeof(bluetooth_mesh_key_configure_t));
1539 BT_INFO("Mesh: Node %4.4x NetKey status %s",
1540 event->source, __mesh_status_to_string(data[0]));
1541 net_idx = l_get_le16(data + 1) & 0xfff;
1543 BT_INFO("\tNetKey %u (0x%3.3x)", net_idx, net_idx);
1546 result = BLUETOOTH_ERROR_INTERNAL;
1551 _bt_mesh_util_convert_hex_to_string(
1552 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1553 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1554 param.primary_unicast = event->source;
1555 param.netkey_idx = net_idx;
1556 param.is_netkey = true;
1558 BT_INFO("Mesh: Resp recvd from node unicast [0x%4.4x]", event->source);
1559 if (cmd->opcode == MESH_OPCODE_NETKEY_ADD) {
1560 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_NETKEY_ADD");
1561 if (result == BLUETOOTH_ERROR_NONE) {
1562 if (!_bt_mesh_network_save_remote_node_netkey(
1563 event->net_uuid.uuid, event->source, net_idx)) {
1564 result = BLUETOOTH_ERROR_INTERNAL;
1565 BT_INFO("Failed to save node Netkey!");
1568 param.op = BLUETOOTH_MESH_NODE_KEY_ADD;
1569 } else if (cmd->opcode == MESH_OPCODE_NETKEY_DELETE) {
1570 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_NETKEY_DELETE");
1571 if (result == BLUETOOTH_ERROR_NONE) {
1572 if (!_bt_mesh_network_delete_remote_node_netkey(
1573 event->net_uuid.uuid, event->source, net_idx)) {
1574 result = BLUETOOTH_ERROR_INTERNAL;
1575 BT_INFO("Failed to delete node Netkey!");
1578 param.op = BLUETOOTH_MESH_NODE_KEY_DELETE;
1579 } else if (cmd->opcode == MESH_OPCODE_NETKEY_UPDATE) {
1580 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_NETKEY_UPDATE");
1581 param.op = BLUETOOTH_MESH_NODE_KEY_UPDATE;
1583 /* Node Net Key Configure event */
1584 __bt_mesh_handle_pending_dev_config_request_info(result,
1585 BT_MESH_NODE_CONFIGURE_KEY,
1586 ¶m, sizeof(bluetooth_mesh_key_configure_t));
1589 case MESH_OPCODE_APPKEY_STATUS: {
1593 bluetooth_mesh_key_configure_t param;
1594 memset(¶m, 0x00, sizeof(bluetooth_mesh_key_configure_t));
1596 BT_INFO("Mesh: Node %4.4x AppKey status %s\n", event->source,
1597 __mesh_status_to_string(data[0]));
1598 net_idx = l_get_le16(data + 1) & 0xfff;
1599 app_idx = l_get_le16(data + 2) >> 4;
1601 BT_INFO("NetKey\t%u (0x%3.3x)\n", net_idx, net_idx);
1602 BT_INFO("AppKey\t%u (0x%3.3x)\n", app_idx, app_idx);
1604 if (data[0] != MESH_STATUS_SUCCESS)
1605 result = BLUETOOTH_ERROR_INTERNAL;
1610 _bt_mesh_util_convert_hex_to_string(
1611 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1612 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1614 param.primary_unicast = event->source;
1615 param.netkey_idx = net_idx;
1616 param.appkey_idx = app_idx;
1617 param.is_netkey = false;
1619 if (cmd->opcode == MESH_OPCODE_APPKEY_ADD) {
1620 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_APPKEY_ADD");
1621 if (result == BLUETOOTH_ERROR_NONE) {
1622 if (!_bt_mesh_network_save_remote_node_appkey(
1623 event->net_uuid.uuid, event->source,
1624 net_idx, app_idx)) {
1625 result = BLUETOOTH_ERROR_INTERNAL;
1626 BT_INFO("Failed to save node Appkey!");
1629 param.op = BLUETOOTH_MESH_NODE_KEY_ADD;
1630 } else if (cmd->opcode == MESH_OPCODE_APPKEY_DELETE) {
1631 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_APPKEY_DELETE");
1632 if (result == BLUETOOTH_ERROR_NONE) {
1633 if (!_bt_mesh_network_delete_remote_node_appkey(
1634 event->net_uuid.uuid, event->source,
1635 net_idx, app_idx)) {
1636 result = BLUETOOTH_ERROR_INTERNAL;
1637 BT_INFO("Failed to delete node Appkey!");
1640 param.op = BLUETOOTH_MESH_NODE_KEY_DELETE;
1641 } else if (cmd->opcode == MESH_OPCODE_APPKEY_UPDATE) {
1642 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_APPKEY_UPDATE");
1643 param.op = BLUETOOTH_MESH_NODE_KEY_UPDATE;
1646 /* Node App Key Configure event */
1647 __bt_mesh_handle_pending_dev_config_request_info(result,
1648 BT_MESH_NODE_CONFIGURE_KEY,
1649 ¶m, sizeof(bluetooth_mesh_key_configure_t));
1655 static gboolean __bt_mesh_vendor_feature_event_handler(gpointer data)
1657 bluetooth_mesh_node_features_t *result = (bluetooth_mesh_node_features_t*) data;
1658 __bt_mesh_handle_pending_dev_config_request_info(
1659 BLUETOOTH_ERROR_NONE,
1660 BT_MESH_NODE_GET_VENDOR_FEATURES,
1661 result, sizeof(bluetooth_mesh_node_features_t));
1666 int _bt_mesh_node_discover_vendor_features(const char *app_cred, const char *sender,
1667 bluetooth_mesh_node_features_t *req)
1669 int ret = OAL_STATUS_SUCCESS;
1671 uint16_t netkey_idx;
1673 oal_uuid_t net_uuid;
1674 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1676 BT_INFO("Mesh: Vendor features for Network UUID [%s]", req->net_uuid);
1677 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1678 strlen(req->net_uuid), net_uuid.uuid, 16);
1679 /* Check if Node's vendor features are already svaed or not */
1680 if (_bt_mesh_node_get_vendor_features(net_uuid.uuid, req->unicast, req)) {
1681 BT_INFO("Mesh: Vendor Features already available for Node: Unicast [0x%.2x]",
1683 /* Schedule event ot Application */
1684 bluetooth_mesh_node_features_t *event = \
1685 g_memdup(req, sizeof(bluetooth_mesh_node_features_t));
1686 g_idle_add(__bt_mesh_vendor_feature_event_handler, (gpointer) event);
1687 return BLUETOOTH_ERROR_NONE;
1690 /* If Scanning is going on */
1691 if (_bt_mesh_is_provisioning() ||
1692 _bt_mesh_is_scanning()) {
1693 BT_ERR("Device is buzy..");
1694 return BLUETOOTH_ERROR_DEVICE_BUSY;
1697 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1698 strlen(req->net_uuid), net_uuid.uuid, 16);
1700 dest = req->unicast;
1701 BT_INFO("Mesh: Get Vendor Features for Remote Node Unicast [0x%2.2x]", dest);
1703 /* Check pending request */
1704 if (_bt_mesh_check_pending_request(MESH_OPCODE_DEV_COMP_GET,
1705 dest, net_uuid.uuid)) {
1706 BT_ERR("Device is buzy..");
1707 return BLUETOOTH_ERROR_DEVICE_BUSY;
1710 BT_INFO("Mesh: Browse Remote Node: Unicast [0x%2.2x]", dest);
1711 /* Get Subnet index of the rmeote node for TX encryption */
1712 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, dest);
1713 if (netkey_idx == MESH_NET_IDX_INVALID)
1714 return BLUETOOTH_ERROR_INTERNAL;
1716 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_DEV_COMP_GET,
1719 /* By default, use page 0 */
1720 buffer[data_len++] = 0;
1721 ret = mesh_conf_send_message(&net_uuid, dest, true,
1722 netkey_idx, buffer, data_len);
1724 if (ret != OAL_STATUS_SUCCESS) {
1725 BT_ERR("ret: %d", ret);
1726 return BLUETOOTH_ERROR_INTERNAL;
1729 /* Queue the request with timeout */
1730 __bt_mesh_add_request(MESH_OPCODE_DEV_COMP_GET,
1731 dest, net_uuid.uuid, NULL);
1732 return BLUETOOTH_ERROR_NONE;
1735 int _bt_mesh_browse_remote_node(const char *app_cred,
1737 bluetooth_mesh_node_discover_t *req)
1739 int ret = OAL_STATUS_SUCCESS;
1741 uint16_t netkey_idx;
1743 oal_uuid_t net_uuid;
1744 oal_uuid_t dev_uuid;
1745 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1747 /* If Scanning is going on */
1748 if (_bt_mesh_is_provisioning() ||
1749 _bt_mesh_is_scanning()) {
1750 BT_ERR("Device is buzy..");
1751 return BLUETOOTH_ERROR_DEVICE_BUSY;
1754 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1755 strlen(req->net_uuid), net_uuid.uuid, 16);
1756 _bt_mesh_util_convert_string_to_hex(req->dev_uuid,
1757 strlen(req->dev_uuid), dev_uuid.uuid, 16);
1759 BT_INFO("Mesh: Browse Node UUID [%s]", req->dev_uuid);
1761 /* Get Remote Node unicast address from Dev UUID */
1762 if (!_bt_mesh_node_get_unicast_from_dev_uuid(net_uuid.uuid,
1763 dev_uuid.uuid, &dest))
1764 return BLUETOOTH_ERROR_INTERNAL;
1766 BT_INFO("Mesh: Browse Remote Node: Unicast [0x%2.2x]", dest);
1767 /* Check pending request */
1768 if (_bt_mesh_check_pending_request(MESH_OPCODE_DEV_COMP_GET,
1769 dest, net_uuid.uuid)) {
1770 BT_ERR("Device is buzy..");
1771 return BLUETOOTH_ERROR_DEVICE_BUSY;
1774 /* Get Subnet index of the rmeote node for TX encryption */
1775 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, dest);
1776 if (netkey_idx == MESH_NET_IDX_INVALID)
1777 return BLUETOOTH_ERROR_INTERNAL;
1779 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_DEV_COMP_GET, buffer);
1781 /* By default, use page 0 */
1782 buffer[data_len++] = 0;
1783 ret = mesh_conf_send_message(&net_uuid, dest, true,
1784 netkey_idx, buffer, data_len);
1786 if (ret != OAL_STATUS_SUCCESS) {
1787 BT_ERR("ret: %d", ret);
1788 return BLUETOOTH_ERROR_INTERNAL;
1791 /* Queue the request with timeout */
1792 __bt_mesh_add_request(MESH_OPCODE_DEV_COMP_GET,
1793 dest, net_uuid.uuid, NULL);
1794 return BLUETOOTH_ERROR_NONE;
1797 int _bt_mesh_model_configure_group_subscription(const char *app_cred,
1798 const char *sender, bluetooth_mesh_model_configure_t *req)
1800 int ret = OAL_STATUS_SUCCESS;
1801 uint16_t netkey_idx;
1802 oal_uuid_t net_uuid;
1804 uint32_t opcode = 0;
1805 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1807 /* If Scanning is going on */
1808 if (_bt_mesh_is_provisioning() ||
1809 _bt_mesh_is_scanning()) {
1810 BT_ERR("Device is buzy..");
1811 return BLUETOOTH_ERROR_DEVICE_BUSY;
1814 BT_INFO("Mesh: group Subscription Req: [%d]", req->op);
1815 if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1816 BT_INFO("Mesh: group Subscription Addr [0x%2.2x]", req->sub_addr);
1818 /* Subscriptio address sanity check */
1819 if (!MESH_IS_GROUP(req->sub_addr) ||
1820 MESH_IS_VIRTUAL(req->sub_addr)) {
1821 BT_ERR("Mesh: Bad subscription address [0x%2.2x\n]",
1823 return BLUETOOTH_ERROR_INVALID_PARAM;
1826 BT_INFO("Mesh: group Subscription Req: Delete All");
1828 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1829 strlen(req->net_uuid), net_uuid.uuid, 16);
1830 /* Get Subnet index of the remote node for TX encryption */
1831 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid,
1832 req->primary_unicast);
1833 if (netkey_idx == MESH_NET_IDX_INVALID)
1834 return BLUETOOTH_ERROR_INTERNAL;
1837 if (req->op == BLUETOOTH_MESH_MODEL_SUB_ADD)
1838 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_ADD;
1839 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE)
1840 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE;
1841 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL)
1842 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL;
1843 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_OVERWRITE)
1844 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_OVERWRITE;
1846 /* Check pending request */
1847 if (_bt_mesh_check_pending_request(opcode,
1848 req->primary_unicast, net_uuid.uuid)) {
1849 BT_ERR("Device is buzy..");
1850 return BLUETOOTH_ERROR_DEVICE_BUSY;
1853 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
1855 BT_INFO("Mesh: Group Subscription Primary unicast [0x%2.2x]",
1856 req->primary_unicast);
1857 /* Element Address */
1858 l_put_le16((req->primary_unicast + req->elem_index),
1862 /* Subscription address */
1863 if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1864 l_put_le16(req->sub_addr, buffer + data_len);
1868 BT_INFO("Mesh: Group Subscription Model ID [0x%4.4x]", req->model);
1869 /* Insert Model ID */
1870 if (req->model >= 0xFFFF0000) {
1871 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
1872 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1875 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
1876 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
1878 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1882 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true,
1883 netkey_idx, buffer, data_len);
1885 if (ret != OAL_STATUS_SUCCESS) {
1886 BT_ERR("ret: %d", ret);
1887 return BLUETOOTH_ERROR_INTERNAL;
1890 BT_INFO("Mesh: Group Subscription Command sent successfully");
1891 /* Queue the request with timeout */
1892 __bt_mesh_add_request(opcode, req->primary_unicast,
1894 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
1896 return BLUETOOTH_ERROR_NONE;
1899 int _bt_mesh_model_configure_virtual_group_subscription(
1900 const char *app_cred, const char *sender,
1901 bluetooth_mesh_model_configure_t *req)
1903 int ret = OAL_STATUS_SUCCESS;
1904 uint16_t netkey_idx;
1905 oal_uuid_t net_uuid;
1907 uint32_t opcode = 0;
1908 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1910 /* If Scanning is going on */
1911 if (_bt_mesh_is_provisioning() ||
1912 _bt_mesh_is_scanning()) {
1913 BT_ERR("Device is buzy..");
1914 return BLUETOOTH_ERROR_DEVICE_BUSY;
1917 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1918 strlen(req->net_uuid), net_uuid.uuid, 16);
1920 BT_INFO("Mesh: Virtual Group Subscription Req: [%d] Group Addr [0x%2.2x]",
1921 req->op, req->sub_addr);
1923 if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1924 /* Subscription address sanity check */
1925 if (MESH_IS_GROUP(req->sub_addr) ||
1926 !(MESH_IS_VIRTUAL(req->sub_addr))) {
1927 BT_ERR("Mesh: Bad Virtual Group subscription address [0x%2.2x\n]",
1929 return BLUETOOTH_ERROR_INVALID_PARAM;
1932 BT_INFO("Mesh: Virtual Group Subscription Req: Delete All");
1934 /* Get Subnet index of the remote node for TX encryption */
1935 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid,
1936 req->primary_unicast);
1937 if (netkey_idx == MESH_NET_IDX_INVALID)
1938 return BLUETOOTH_ERROR_INTERNAL;
1940 if (req->op == BLUETOOTH_MESH_MODEL_SUB_ADD)
1941 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_ADD;
1942 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE)
1943 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_DELETE;
1944 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_OVERWRITE)
1945 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_OVERWRITE;
1946 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL)
1947 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL;
1949 /* Check pending request */
1950 if (_bt_mesh_check_pending_request(opcode,
1951 req->primary_unicast, net_uuid.uuid)) {
1952 BT_ERR("Device is buzy..");
1953 return BLUETOOTH_ERROR_DEVICE_BUSY;
1956 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
1958 /* Element Address */
1959 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
1962 /* Subscription address */
1963 if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1964 uint8_t label_uuid[16];
1965 if (!_bt_mesh_network_get_label_uuid_from_sub_addr(
1966 net_uuid.uuid, req->sub_addr, label_uuid)) {
1967 BT_ERR("Mesh: Virtual Group Label UUID Not found");
1968 return BLUETOOTH_ERROR_INVALID_PARAM;
1970 memcpy(buffer + data_len, label_uuid, 16);
1974 BT_INFO("Mesh: Virtual Group Subscription Model ID [0x%4.4x]", req->model);
1975 /* Insert Model ID */
1976 if (req->model >= 0xFFFF0000) {
1977 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
1978 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1981 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
1982 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
1984 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1988 ret = mesh_conf_send_message(&net_uuid,
1989 req->primary_unicast, true,
1990 netkey_idx, buffer, data_len);
1992 if (ret != OAL_STATUS_SUCCESS) {
1993 BT_ERR("ret: %d", ret);
1994 return BLUETOOTH_ERROR_INTERNAL;
1997 BT_INFO("Mesh: Virtual Group Subscription Command sent successfully");
1998 /* Queue the request with timeout */
1999 __bt_mesh_add_request(opcode, req->primary_unicast,
2001 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2003 return BLUETOOTH_ERROR_NONE;
2006 int _bt_mesh_model_get_publication(const char *app_cred, const char *sender,
2007 bluetooth_mesh_model_configure_t *req)
2009 int ret = OAL_STATUS_SUCCESS;
2010 uint16_t netkey_idx;
2011 oal_uuid_t net_uuid;
2013 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2015 /* If Scanning is going on */
2016 if (_bt_mesh_is_provisioning() ||
2017 _bt_mesh_is_scanning()) {
2018 BT_ERR("Device is buzy..");
2019 return BLUETOOTH_ERROR_DEVICE_BUSY;
2022 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
2023 strlen(req->net_uuid), net_uuid.uuid, 16);
2025 /* Check pending request */
2026 if (_bt_mesh_check_pending_request(MESH_OPCODE_CONFIG_MODEL_PUB_GET,
2027 req->primary_unicast, net_uuid.uuid)) {
2028 BT_ERR("Device is buzy..");
2029 return BLUETOOTH_ERROR_DEVICE_BUSY;
2032 /* Get Subnet index of the remote node for TX encryption */
2033 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2034 if (netkey_idx == MESH_NET_IDX_INVALID)
2035 return BLUETOOTH_ERROR_INTERNAL;
2038 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_CONFIG_MODEL_PUB_GET, buffer);
2040 /* Element Address */
2041 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2044 /* Insert Model ID */
2045 if (req->model >= 0xFFFF0000) {
2046 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2047 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2050 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2051 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2053 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2057 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2059 if (ret != OAL_STATUS_SUCCESS) {
2060 BT_ERR("ret: %d", ret);
2061 return BLUETOOTH_ERROR_INTERNAL;
2064 /* Queue the request with timeout */
2065 __bt_mesh_add_request(MESH_OPCODE_CONFIG_MODEL_PUB_GET, req->primary_unicast, net_uuid.uuid,
2066 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2068 return BLUETOOTH_ERROR_NONE;
2071 int _bt_mesh_model_set_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 uint32_t opcode = 0;
2079 uint8_t label_uuid[16];
2080 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2082 /* If Scanning is going on */
2083 if (_bt_mesh_is_provisioning() ||
2084 _bt_mesh_is_scanning()) {
2085 BT_ERR("Device is buzy..");
2086 return BLUETOOTH_ERROR_DEVICE_BUSY;
2089 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2091 if (req->pub_addr == MESH_ALL_NODES_ADDRESS)
2092 BT_INFO("Mesh: Setting Publication to ALL Node Address");
2094 if (!MESH_IS_GROUP(req->pub_addr) && !MESH_IS_VIRTUAL(req->pub_addr) &&
2095 req->pub_addr != MESH_UNASSIGNED_ADDRESS) {
2096 BT_ERR("Mesh: Bad Publication address %x\n", req->pub_addr);
2097 return BLUETOOTH_ERROR_INVALID_PARAM;
2100 if (req->pub_addr != MESH_UNASSIGNED_ADDRESS) {
2101 if (MESH_IS_VIRTUAL(req->pub_addr)) {
2102 if (!_bt_mesh_network_get_label_uuid_from_sub_addr(
2103 net_uuid.uuid, req->pub_addr, label_uuid))
2104 return BLUETOOTH_ERROR_INVALID_PARAM;
2105 opcode = MESH_OPCODE_CONFIG_MODEL_PUB_VIRT_SET;
2107 } else if (MESH_IS_GROUP(req->pub_addr))
2108 opcode = MESH_OPCODE_CONFIG_MODEL_PUB_SET;
2111 /* Check pending request */
2112 if (_bt_mesh_check_pending_request(opcode,
2113 req->primary_unicast, net_uuid.uuid)) {
2114 BT_ERR("Device is buzy..");
2115 return BLUETOOTH_ERROR_DEVICE_BUSY;
2118 /* Get Subnet index of the remote node for TX encryption */
2119 netkey_idx = _bt_mesh_node_get_subnet_idx(
2120 net_uuid.uuid, req->primary_unicast);
2121 if (netkey_idx == MESH_NET_IDX_INVALID)
2122 return BLUETOOTH_ERROR_INTERNAL;
2125 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2127 /* Element Address */
2128 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2131 /* Fill Publication Address */
2132 if (MESH_IS_VIRTUAL(req->pub_addr)) {
2133 memcpy(buffer + data_len, label_uuid, 16);
2136 l_put_le16(req->pub_addr, buffer + data_len);
2140 /* AppKey index + credential (set to 0) */
2141 l_put_le16(req->appkey_idx, buffer + data_len);
2145 buffer[data_len++] = req->ttl;
2146 /* Publish period step count and step resolution */
2147 buffer[data_len++] = req->period;
2148 /* Publish retransmit count & interval steps */
2149 buffer[data_len++] = req->retransmit;
2151 /* Insert Model ID */
2152 if (req->model >= 0xFFFF0000) {
2153 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2154 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2157 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2158 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2160 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2164 ret = mesh_conf_send_message(&net_uuid,
2165 req->primary_unicast, true,
2166 netkey_idx, buffer, data_len);
2168 if (ret != OAL_STATUS_SUCCESS) {
2169 BT_ERR("ret: %d", ret);
2170 return BLUETOOTH_ERROR_INTERNAL;
2173 /* Queue the request with timeout */
2174 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2175 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2177 return BLUETOOTH_ERROR_NONE;
2180 int _bt_mesh_node_model_get_subscription_list(const char *app_cred, const char *sender,
2181 bluetooth_mesh_model_configure_t *req)
2183 int ret = OAL_STATUS_SUCCESS;
2184 uint16_t netkey_idx;
2185 oal_uuid_t net_uuid;
2187 uint32_t opcode = 0;
2188 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2190 /* If Scanning is going on */
2191 if (_bt_mesh_is_provisioning() ||
2192 _bt_mesh_is_scanning()) {
2193 BT_ERR("Device is buzy..");
2194 return BLUETOOTH_ERROR_DEVICE_BUSY;
2197 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2199 /* Insert Model ID */
2200 if (req->model >= 0xFFFF0000)
2201 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2202 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_GET;
2204 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2205 opcode = MESH_OPCODE_CONFIG_VEND_MODEL_SUB_GET;
2207 /* Check pending request */
2208 if (_bt_mesh_check_pending_request(opcode,
2209 req->primary_unicast, net_uuid.uuid)) {
2210 BT_ERR("Device is buzy..");
2211 return BLUETOOTH_ERROR_DEVICE_BUSY;
2213 /* Get Subnet index of the remote node for TX encryption */
2214 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2215 if (netkey_idx == MESH_NET_IDX_INVALID)
2216 return BLUETOOTH_ERROR_INTERNAL;
2218 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2219 /* Element Address */
2220 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2223 /* Insert Model ID */
2224 if (opcode == MESH_OPCODE_CONFIG_MODEL_SUB_GET) {
2225 BT_INFO("Mesh: Get Subscription List for BT SIG Model");
2226 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2227 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2230 BT_INFO("Mesh: Get Subscription List for Vendor Model");
2231 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2232 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2234 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2238 ret = mesh_conf_send_message(&net_uuid,
2239 req->primary_unicast, true, netkey_idx, buffer, data_len);
2241 if (ret != OAL_STATUS_SUCCESS) {
2242 BT_ERR("ret: %d", ret);
2243 return BLUETOOTH_ERROR_INTERNAL;
2246 /* Queue the request with timeout */
2247 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2248 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2250 return BLUETOOTH_ERROR_NONE;
2253 int _bt_mesh_node_model_get_appkey_list(const char *app_cred, const char *sender,
2254 bluetooth_mesh_model_configure_t *req)
2256 int ret = OAL_STATUS_SUCCESS;
2257 uint16_t netkey_idx;
2258 oal_uuid_t net_uuid;
2260 uint32_t opcode = 0;
2261 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2263 /* If Scanning is going on */
2264 if (_bt_mesh_is_provisioning() ||
2265 _bt_mesh_is_scanning()) {
2266 BT_ERR("Device is buzy..");
2267 return BLUETOOTH_ERROR_DEVICE_BUSY;
2270 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
2271 strlen(req->net_uuid), net_uuid.uuid, 16);
2273 /* Insert Model ID */
2274 if (req->model >= 0xFFFF0000)
2275 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2276 opcode = MESH_OPCODE_MODEL_APP_GET;
2278 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2279 opcode = MESH_OPCODE_VENDOR_MODEL_APP_GET;
2281 /* Check pending request */
2282 if (_bt_mesh_check_pending_request(opcode,
2283 req->primary_unicast, net_uuid.uuid)) {
2284 BT_ERR("Device is buzy..");
2285 return BLUETOOTH_ERROR_DEVICE_BUSY;
2287 /* Get Subnet index of the remote node for TX encryption */
2288 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2289 if (netkey_idx == MESH_NET_IDX_INVALID)
2290 return BLUETOOTH_ERROR_INTERNAL;
2292 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2293 /* Element Address */
2294 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2298 /* Insert Model ID */
2299 if (opcode == MESH_OPCODE_MODEL_APP_GET) {
2300 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2301 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2304 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2305 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2307 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2311 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2313 if (ret != OAL_STATUS_SUCCESS) {
2314 BT_ERR("ret: %d", ret);
2315 return BLUETOOTH_ERROR_INTERNAL;
2318 /* Queue the request with timeout */
2319 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2320 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2322 return BLUETOOTH_ERROR_NONE;
2325 int _bt_mesh_network_reset_node(const char *app_cred, const char *sender,
2326 bluetooth_mesh_node_info_t *node)
2328 int ret = OAL_STATUS_SUCCESS;
2329 uint16_t netkey_idx;
2330 oal_uuid_t net_uuid;
2332 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2334 /* If Scanning is going on */
2335 if (_bt_mesh_is_provisioning() ||
2336 _bt_mesh_is_scanning()) {
2337 BT_ERR("Device is buzy..");
2338 return BLUETOOTH_ERROR_DEVICE_BUSY;
2341 _bt_mesh_util_convert_string_to_hex(node->net_uuid,
2342 strlen(node->net_uuid), net_uuid.uuid, 16);
2344 /* Check pending request */
2345 if (_bt_mesh_check_pending_request(MESH_OPCODE_NODE_RESET,
2346 node->primary_unicast, net_uuid.uuid)) {
2347 BT_ERR("Device is buzy..");
2348 return BLUETOOTH_ERROR_DEVICE_BUSY;
2350 BT_INFO("Mesh: Reset a remote Node [0x%2.2x] Elem Cnt [0x%2.2x]",
2351 node->primary_unicast, node->num_elements);
2352 /* Get Subnet index of the remote node for TX encryption */
2353 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, node->primary_unicast);
2354 if (netkey_idx == MESH_NET_IDX_INVALID)
2355 return BLUETOOTH_ERROR_INTERNAL;
2357 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_NODE_RESET, buffer);
2359 /* Send Node Reset command 5 times to ensure reliability */
2360 for (int i = 0; i < 5; i++)
2361 ret = mesh_conf_send_message(&net_uuid, node->primary_unicast,
2362 true, netkey_idx, buffer, data_len);
2364 if (ret != OAL_STATUS_SUCCESS) {
2365 BT_ERR("Mesh: Node Reset Failed!: %d", ret);
2366 return BLUETOOTH_ERROR_INTERNAL;
2369 BT_INFO("Mesh: Node reset Done, Send Delete req: Unicast[0x%2.2x]",
2370 node->primary_unicast);
2372 ret = mesh_delete_remote_node(&net_uuid, node->primary_unicast,
2373 node->num_elements);
2374 if (ret != OAL_STATUS_SUCCESS) {
2375 BT_ERR("Mesh: Remote Node Deletion Failed!: %d", ret);
2376 return BLUETOOTH_ERROR_INTERNAL;
2379 return _bt_mesh_network_remove_node_configuration(node);
2382 int _bt_mesh_node_model_appkey_execute(const char *app_cred, const char *sender,
2383 bluetooth_mesh_model_configure_t *req)
2385 int ret = OAL_STATUS_SUCCESS;
2386 uint16_t netkey_idx;
2387 oal_uuid_t net_uuid;
2389 uint32_t opcode = 0;
2390 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2392 /* If Scanning is going on */
2393 if (_bt_mesh_is_provisioning() ||
2394 _bt_mesh_is_scanning()) {
2395 BT_ERR("Device is buzy..");
2396 return BLUETOOTH_ERROR_DEVICE_BUSY;
2399 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2401 /* Check pending request */
2402 if (_bt_mesh_check_pending_request(req->is_bind ? MESH_OPCODE_MODEL_APP_BIND : MESH_OPCODE_MODEL_APP_UNBIND,
2403 req->primary_unicast, net_uuid.uuid)) {
2404 BT_ERR("Device is buzy..");
2405 return BLUETOOTH_ERROR_DEVICE_BUSY;
2407 /* Get Subnet index of the remote node for TX encryption */
2408 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2409 if (netkey_idx == MESH_NET_IDX_INVALID)
2410 return BLUETOOTH_ERROR_INTERNAL;
2413 opcode = MESH_OPCODE_MODEL_APP_BIND;
2415 opcode = MESH_OPCODE_MODEL_APP_UNBIND;
2417 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2418 /* Element Address */
2419 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2423 l_put_le16(req->appkey_idx, buffer + data_len);
2426 /* Insert Model ID */
2427 if (req->model >= 0xFFFF0000) {
2428 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2429 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2432 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2433 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2435 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2439 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2441 if (ret != OAL_STATUS_SUCCESS) {
2442 BT_ERR("ret: %d", ret);
2443 return BLUETOOTH_ERROR_INTERNAL;
2446 /* Queue the request with timeout */
2447 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2448 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2450 return BLUETOOTH_ERROR_NONE;
2453 int _bt_mesh_ttl_execute_remote_node(const char *app_cred, const char *sender,
2454 bluetooth_mesh_node_ttl_info_t *req)
2456 int ret = OAL_STATUS_SUCCESS;
2457 uint16_t netkey_idx;
2459 oal_uuid_t net_uuid;
2460 uint32_t opcode = 0;
2461 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2463 /* If Scanning is going on */
2464 if (_bt_mesh_is_provisioning() ||
2465 _bt_mesh_is_scanning()) {
2466 BT_ERR("Device is buzy..");
2467 return BLUETOOTH_ERROR_DEVICE_BUSY;
2470 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2472 /* Check pending request */
2473 if (_bt_mesh_check_pending_request(req->is_set ? MESH_OPCODE_CONFIG_DEFAULT_TTL_SET : MESH_OPCODE_CONFIG_DEFAULT_TTL_GET,
2474 req->unicast, net_uuid.uuid)) {
2475 BT_ERR("Device is buzy..");
2476 return BLUETOOTH_ERROR_DEVICE_BUSY;
2479 /* Get Subnet index of the rmeote node for TX encryption */
2480 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->unicast);
2481 if (netkey_idx == MESH_NET_IDX_INVALID)
2482 return BLUETOOTH_ERROR_INTERNAL;
2485 opcode = MESH_OPCODE_CONFIG_DEFAULT_TTL_SET;
2486 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_CONFIG_DEFAULT_TTL_SET, buffer);
2487 buffer[data_len++] = req->ttl;
2489 opcode = MESH_OPCODE_CONFIG_DEFAULT_TTL_GET;
2490 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_CONFIG_DEFAULT_TTL_GET, buffer);
2493 ret = mesh_conf_send_message(&net_uuid, req->unicast, true, netkey_idx, buffer, data_len);
2495 if (ret != OAL_STATUS_SUCCESS) {
2496 BT_ERR("ret: %d", ret);
2497 return BLUETOOTH_ERROR_INTERNAL;
2500 /* Queue the request with timeout */
2501 __bt_mesh_add_request(opcode, req->unicast, net_uuid.uuid,
2502 g_memdup(req, sizeof(bluetooth_mesh_node_ttl_info_t)));
2503 return BLUETOOTH_ERROR_NONE;
2506 static bool __bt_mesh_check_pending_key_cmd(uint16_t dest, bool is_netkey,
2507 bluetooth_mesh_node_key_conf_e op, uint8_t net_uuid[], uint32_t *opcode)
2509 const struct mesh_config_cmd *cmd;
2513 case BLUETOOTH_MESH_NODE_KEY_ADD:
2514 cmd = __mesh_get_command(MESH_OPCODE_NETKEY_ADD);
2517 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2518 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2521 *opcode = MESH_OPCODE_NETKEY_ADD;
2523 case BLUETOOTH_MESH_NODE_KEY_DELETE:
2524 cmd = __mesh_get_command(MESH_OPCODE_NETKEY_DELETE);
2527 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2528 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2531 *opcode = MESH_OPCODE_NETKEY_DELETE;
2533 case BLUETOOTH_MESH_NODE_KEY_UPDATE:
2534 cmd = __mesh_get_command(MESH_OPCODE_NETKEY_UPDATE);
2537 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2538 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2541 *opcode = MESH_OPCODE_NETKEY_UPDATE;
2546 case BLUETOOTH_MESH_NODE_KEY_ADD:
2547 cmd = __mesh_get_command(MESH_OPCODE_APPKEY_ADD);
2550 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2551 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2554 *opcode = MESH_OPCODE_APPKEY_ADD;
2556 case BLUETOOTH_MESH_NODE_KEY_DELETE:
2557 cmd = __mesh_get_command(MESH_OPCODE_APPKEY_DELETE);
2560 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2561 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2564 *opcode = MESH_OPCODE_APPKEY_DELETE;
2566 case BLUETOOTH_MESH_NODE_KEY_UPDATE:
2567 cmd = __mesh_get_command(MESH_OPCODE_APPKEY_UPDATE);
2570 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2571 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2574 *opcode = MESH_OPCODE_APPKEY_UPDATE;
2581 int _bt_mesh_node_configure_key(const char *app_cred, const char *sender,
2582 bluetooth_mesh_key_configure_t *req)
2584 int ret = OAL_STATUS_SUCCESS;
2585 uint16_t netkey_idx;
2586 uint16_t bound_netkey_idx = 0x0000;
2587 oal_uuid_t net_uuid;
2588 uint32_t opcode = 0;
2591 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2593 /* If Scanning is going on */
2594 if (_bt_mesh_is_provisioning() ||
2595 _bt_mesh_is_scanning()) {
2596 BT_ERR("Device is buzy..");
2597 return BLUETOOTH_ERROR_DEVICE_BUSY;
2600 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2602 if (req->is_netkey && !_bt_mesh_keys_subnet_exists(net_uuid.uuid, req->netkey_idx)) {
2603 BT_ERR("Local Subnet not found..");
2604 return BLUETOOTH_ERROR_INVALID_PARAM;
2607 /* For Appkey Configuration, check for available bound netkey */
2608 if (!req->is_netkey) {
2609 bound_netkey_idx = _bt_mesh_keys_get_bound_key(net_uuid.uuid, req->appkey_idx);
2610 if (bound_netkey_idx == MESH_NET_IDX_INVALID) {
2611 BT_ERR("Local AppKey not found..");
2612 return BLUETOOTH_ERROR_INVALID_PARAM;
2616 /* Check pending request */
2617 if (__bt_mesh_check_pending_key_cmd(req->primary_unicast,
2618 req->is_netkey, req->op, net_uuid.uuid, &opcode)) {
2619 BT_ERR("Device is buzy..");
2620 return BLUETOOTH_ERROR_DEVICE_BUSY;
2623 /* Get Subnet index of the rmeote node for TX encryption */
2624 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2625 if (netkey_idx == MESH_NET_IDX_INVALID)
2626 return BLUETOOTH_ERROR_INTERNAL;
2628 /* Handle Key (App/Net) Delete Commands: Configuration Message */
2629 if (opcode == MESH_OPCODE_NETKEY_DELETE || opcode == MESH_OPCODE_APPKEY_DELETE) {
2630 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2631 if (req->is_netkey) {
2632 l_put_le16(req->netkey_idx, buffer + data_len);
2635 buffer[data_len] = bound_netkey_idx;
2636 buffer[data_len + 1] = ((bound_netkey_idx >> 8) & 0xf) | ((req->appkey_idx << 4) & 0xf0);
2637 buffer[data_len + 2] = req->appkey_idx >> 4;
2641 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2643 /* Handle Key (App/Net) Update & Add Commands: Key Config message */
2644 update = (opcode == MESH_OPCODE_NETKEY_UPDATE || opcode == MESH_OPCODE_APPKEY_UPDATE);
2645 ret = mesh_conf_send_key_message(&net_uuid, req->primary_unicast, req->is_netkey,
2646 update, req->is_netkey ? req->netkey_idx : req->appkey_idx, netkey_idx);
2648 if (ret != OAL_STATUS_SUCCESS) {
2649 BT_ERR("ret: %d", ret);
2650 return BLUETOOTH_ERROR_INTERNAL;
2653 /* Queue the request with timeout */
2654 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2655 g_memdup(req, sizeof(bluetooth_mesh_key_configure_t)));
2656 return BLUETOOTH_ERROR_NONE;