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_mesh_node_get_vendor_features(event->net_uuid.uuid,
1515 event->source, &features);
1517 __bt_mesh_handle_pending_dev_config_request_info(result,
1518 BT_MESH_NODE_GET_VENDOR_FEATURES, &features,
1519 sizeof(bluetooth_mesh_node_features_t));
1522 case MESH_OPCODE_NETKEY_STATUS: {
1526 bluetooth_mesh_key_configure_t param;
1527 memset(¶m, 0x00, sizeof(bluetooth_mesh_key_configure_t));
1529 BT_INFO("Mesh: Node %4.4x NetKey status %s",
1530 event->source, __mesh_status_to_string(data[0]));
1531 net_idx = l_get_le16(data + 1) & 0xfff;
1533 BT_INFO("\tNetKey %u (0x%3.3x)", net_idx, net_idx);
1536 result = BLUETOOTH_ERROR_INTERNAL;
1541 _bt_mesh_util_convert_hex_to_string(
1542 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1543 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1544 param.primary_unicast = event->source;
1545 param.netkey_idx = net_idx;
1546 param.is_netkey = true;
1548 BT_INFO("Mesh: Resp recvd from node unicast [0x%4.4x]", event->source);
1549 if (cmd->opcode == MESH_OPCODE_NETKEY_ADD) {
1550 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_NETKEY_ADD");
1551 if (result == BLUETOOTH_ERROR_NONE) {
1552 if (!_bt_mesh_network_save_remote_node_netkey(
1553 event->net_uuid.uuid, event->source, net_idx)) {
1554 result = BLUETOOTH_ERROR_INTERNAL;
1555 BT_INFO("Failed to save node Netkey!");
1558 param.op = BLUETOOTH_MESH_NODE_KEY_ADD;
1559 } else if (cmd->opcode == MESH_OPCODE_NETKEY_DELETE) {
1560 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_NETKEY_DELETE");
1561 if (result == BLUETOOTH_ERROR_NONE) {
1562 if (!_bt_mesh_network_delete_remote_node_netkey(
1563 event->net_uuid.uuid, event->source, net_idx)) {
1564 result = BLUETOOTH_ERROR_INTERNAL;
1565 BT_INFO("Failed to delete node Netkey!");
1568 param.op = BLUETOOTH_MESH_NODE_KEY_DELETE;
1569 } else if (cmd->opcode == MESH_OPCODE_NETKEY_UPDATE) {
1570 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_NETKEY_UPDATE");
1571 param.op = BLUETOOTH_MESH_NODE_KEY_UPDATE;
1573 /* Node Net Key Configure event */
1574 __bt_mesh_handle_pending_dev_config_request_info(result,
1575 BT_MESH_NODE_CONFIGURE_KEY,
1576 ¶m, sizeof(bluetooth_mesh_key_configure_t));
1579 case MESH_OPCODE_APPKEY_STATUS: {
1583 bluetooth_mesh_key_configure_t param;
1584 memset(¶m, 0x00, sizeof(bluetooth_mesh_key_configure_t));
1586 BT_INFO("Mesh: Node %4.4x AppKey status %s\n", event->source,
1587 __mesh_status_to_string(data[0]));
1588 net_idx = l_get_le16(data + 1) & 0xfff;
1589 app_idx = l_get_le16(data + 2) >> 4;
1591 BT_INFO("NetKey\t%u (0x%3.3x)\n", net_idx, net_idx);
1592 BT_INFO("AppKey\t%u (0x%3.3x)\n", app_idx, app_idx);
1594 if (data[0] != MESH_STATUS_SUCCESS)
1595 result = BLUETOOTH_ERROR_INTERNAL;
1600 _bt_mesh_util_convert_hex_to_string(
1601 (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1602 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1604 param.primary_unicast = event->source;
1605 param.netkey_idx = net_idx;
1606 param.appkey_idx = app_idx;
1607 param.is_netkey = false;
1609 if (cmd->opcode == MESH_OPCODE_APPKEY_ADD) {
1610 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_APPKEY_ADD");
1611 if (result == BLUETOOTH_ERROR_NONE) {
1612 if (!_bt_mesh_network_save_remote_node_appkey(
1613 event->net_uuid.uuid, event->source,
1614 net_idx, app_idx)) {
1615 result = BLUETOOTH_ERROR_INTERNAL;
1616 BT_INFO("Failed to save node Appkey!");
1619 param.op = BLUETOOTH_MESH_NODE_KEY_ADD;
1620 } else if (cmd->opcode == MESH_OPCODE_APPKEY_DELETE) {
1621 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_APPKEY_DELETE");
1622 if (result == BLUETOOTH_ERROR_NONE) {
1623 if (!_bt_mesh_network_delete_remote_node_appkey(
1624 event->net_uuid.uuid, event->source,
1625 net_idx, app_idx)) {
1626 result = BLUETOOTH_ERROR_INTERNAL;
1627 BT_INFO("Failed to delete node Appkey!");
1630 param.op = BLUETOOTH_MESH_NODE_KEY_DELETE;
1631 } else if (cmd->opcode == MESH_OPCODE_APPKEY_UPDATE) {
1632 BT_INFO("Mesh: Resp recvd: MESH_OPCODE_APPKEY_UPDATE");
1633 param.op = BLUETOOTH_MESH_NODE_KEY_UPDATE;
1636 /* Node App Key Configure event */
1637 __bt_mesh_handle_pending_dev_config_request_info(result,
1638 BT_MESH_NODE_CONFIGURE_KEY,
1639 ¶m, sizeof(bluetooth_mesh_key_configure_t));
1645 static gboolean __bt_mesh_vendor_feature_event_handler(gpointer data)
1647 bluetooth_mesh_node_features_t *result = (bluetooth_mesh_node_features_t*) data;
1648 __bt_mesh_handle_pending_dev_config_request_info(
1649 BLUETOOTH_ERROR_NONE,
1650 BT_MESH_NODE_GET_VENDOR_FEATURES,
1651 result, sizeof(bluetooth_mesh_node_features_t));
1656 int _bt_mesh_node_discover_vendor_features(const char *app_cred, const char *sender,
1657 bluetooth_mesh_node_features_t *req)
1659 int ret = OAL_STATUS_SUCCESS;
1661 uint16_t netkey_idx;
1663 oal_uuid_t net_uuid;
1664 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1666 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1667 strlen(req->net_uuid), net_uuid.uuid, 16);
1668 /* Check if Node's vendor features are already svaed or not */
1669 if (_bt_mesh_node_get_vendor_features(net_uuid.uuid, req->unicast, req)) {
1670 BT_INFO("Mesh: Vendor Features already available for Node: Unicast [0x%.2x]",
1672 /* Schedule event ot Application */
1673 bluetooth_mesh_node_features_t *event = \
1674 g_memdup(req, sizeof(bluetooth_mesh_node_features_t));
1675 g_idle_add(__bt_mesh_vendor_feature_event_handler, (gpointer) event);
1676 return BLUETOOTH_ERROR_NONE;
1679 /* If Scanning is going on */
1680 if (_bt_mesh_is_provisioning() ||
1681 _bt_mesh_is_scanning()) {
1682 BT_ERR("Device is buzy..");
1683 return BLUETOOTH_ERROR_DEVICE_BUSY;
1686 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1687 strlen(req->net_uuid), net_uuid.uuid, 16);
1689 dest = req->unicast;
1690 BT_INFO("Mesh: Get Vendor Features for Remote Node Unicast [0x%2.2x]", dest);
1692 /* Check pending request */
1693 if (_bt_mesh_check_pending_request(MESH_OPCODE_DEV_COMP_GET,
1694 dest, net_uuid.uuid)) {
1695 BT_ERR("Device is buzy..");
1696 return BLUETOOTH_ERROR_DEVICE_BUSY;
1699 BT_INFO("Mesh: Browse Remote Node: Unicast [0x%2.2x]", dest);
1700 /* Get Subnet index of the rmeote node for TX encryption */
1701 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, dest);
1702 if (netkey_idx == MESH_NET_IDX_INVALID)
1703 return BLUETOOTH_ERROR_INTERNAL;
1705 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_DEV_COMP_GET,
1708 /* By default, use page 0 */
1709 buffer[data_len++] = 0;
1710 ret = mesh_conf_send_message(&net_uuid, dest, true,
1711 netkey_idx, buffer, data_len);
1713 if (ret != OAL_STATUS_SUCCESS) {
1714 BT_ERR("ret: %d", ret);
1715 return BLUETOOTH_ERROR_INTERNAL;
1718 /* Queue the request with timeout */
1719 __bt_mesh_add_request(MESH_OPCODE_DEV_COMP_GET,
1720 dest, net_uuid.uuid, NULL);
1721 return BLUETOOTH_ERROR_NONE;
1724 int _bt_mesh_browse_remote_node(const char *app_cred,
1726 bluetooth_mesh_node_discover_t *req)
1728 int ret = OAL_STATUS_SUCCESS;
1730 uint16_t netkey_idx;
1732 oal_uuid_t net_uuid;
1733 oal_uuid_t dev_uuid;
1734 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1736 /* If Scanning is going on */
1737 if (_bt_mesh_is_provisioning() ||
1738 _bt_mesh_is_scanning()) {
1739 BT_ERR("Device is buzy..");
1740 return BLUETOOTH_ERROR_DEVICE_BUSY;
1743 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1744 strlen(req->net_uuid), net_uuid.uuid, 16);
1745 _bt_mesh_util_convert_string_to_hex(req->dev_uuid,
1746 strlen(req->dev_uuid), dev_uuid.uuid, 16);
1748 BT_INFO("Mesh: Browse Node UUID [%s]", req->dev_uuid);
1750 /* Get Remote Node unicast address from Dev UUID */
1751 if (!_bt_mesh_node_get_unicast_from_dev_uuid(net_uuid.uuid,
1752 dev_uuid.uuid, &dest))
1753 return BLUETOOTH_ERROR_INTERNAL;
1755 BT_INFO("Mesh: Browse Remote Node: Unicast [0x%2.2x]", dest);
1756 /* Check pending request */
1757 if (_bt_mesh_check_pending_request(MESH_OPCODE_DEV_COMP_GET,
1758 dest, net_uuid.uuid)) {
1759 BT_ERR("Device is buzy..");
1760 return BLUETOOTH_ERROR_DEVICE_BUSY;
1763 /* Get Subnet index of the rmeote node for TX encryption */
1764 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, dest);
1765 if (netkey_idx == MESH_NET_IDX_INVALID)
1766 return BLUETOOTH_ERROR_INTERNAL;
1768 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_DEV_COMP_GET, buffer);
1770 /* By default, use page 0 */
1771 buffer[data_len++] = 0;
1772 ret = mesh_conf_send_message(&net_uuid, dest, true,
1773 netkey_idx, buffer, data_len);
1775 if (ret != OAL_STATUS_SUCCESS) {
1776 BT_ERR("ret: %d", ret);
1777 return BLUETOOTH_ERROR_INTERNAL;
1780 /* Queue the request with timeout */
1781 __bt_mesh_add_request(MESH_OPCODE_DEV_COMP_GET,
1782 dest, net_uuid.uuid, NULL);
1783 return BLUETOOTH_ERROR_NONE;
1786 int _bt_mesh_model_configure_group_subscription(const char *app_cred,
1787 const char *sender, bluetooth_mesh_model_configure_t *req)
1789 int ret = OAL_STATUS_SUCCESS;
1790 uint16_t netkey_idx;
1791 oal_uuid_t net_uuid;
1793 uint32_t opcode = 0;
1794 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1796 /* If Scanning is going on */
1797 if (_bt_mesh_is_provisioning() ||
1798 _bt_mesh_is_scanning()) {
1799 BT_ERR("Device is buzy..");
1800 return BLUETOOTH_ERROR_DEVICE_BUSY;
1803 BT_INFO("Mesh: group Subscription Req: [%d]", req->op);
1804 if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1805 BT_INFO("Mesh: group Subscription Addr [0x%2.2x]", req->sub_addr);
1807 /* Subscriptio address sanity check */
1808 if (!MESH_IS_GROUP(req->sub_addr) ||
1809 MESH_IS_VIRTUAL(req->sub_addr)) {
1810 BT_ERR("Mesh: Bad subscription address [0x%2.2x\n]",
1812 return BLUETOOTH_ERROR_INVALID_PARAM;
1815 BT_INFO("Mesh: group Subscription Req: Delete All");
1817 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1818 strlen(req->net_uuid), net_uuid.uuid, 16);
1819 /* Get Subnet index of the remote node for TX encryption */
1820 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid,
1821 req->primary_unicast);
1822 if (netkey_idx == MESH_NET_IDX_INVALID)
1823 return BLUETOOTH_ERROR_INTERNAL;
1826 if (req->op == BLUETOOTH_MESH_MODEL_SUB_ADD)
1827 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_ADD;
1828 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE)
1829 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE;
1830 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL)
1831 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL;
1832 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_OVERWRITE)
1833 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_OVERWRITE;
1835 /* Check pending request */
1836 if (_bt_mesh_check_pending_request(opcode,
1837 req->primary_unicast, net_uuid.uuid)) {
1838 BT_ERR("Device is buzy..");
1839 return BLUETOOTH_ERROR_DEVICE_BUSY;
1842 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
1844 BT_INFO("Mesh: Group Subscription Primary unicast [0x%2.2x]",
1845 req->primary_unicast);
1846 /* Element Address */
1847 l_put_le16((req->primary_unicast + req->elem_index),
1851 /* Subscription address */
1852 if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1853 l_put_le16(req->sub_addr, buffer + data_len);
1857 BT_INFO("Mesh: Group Subscription Model ID [0x%4.4x]", req->model);
1858 /* Insert Model ID */
1859 if (req->model >= 0xFFFF0000) {
1860 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
1861 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1864 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
1865 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
1867 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1871 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true,
1872 netkey_idx, buffer, data_len);
1874 if (ret != OAL_STATUS_SUCCESS) {
1875 BT_ERR("ret: %d", ret);
1876 return BLUETOOTH_ERROR_INTERNAL;
1879 BT_INFO("Mesh: Group Subscription Command sent successfully");
1880 /* Queue the request with timeout */
1881 __bt_mesh_add_request(opcode, req->primary_unicast,
1883 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
1885 return BLUETOOTH_ERROR_NONE;
1888 int _bt_mesh_model_configure_virtual_group_subscription(
1889 const char *app_cred, const char *sender,
1890 bluetooth_mesh_model_configure_t *req)
1892 int ret = OAL_STATUS_SUCCESS;
1893 uint16_t netkey_idx;
1894 oal_uuid_t net_uuid;
1896 uint32_t opcode = 0;
1897 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1899 /* If Scanning is going on */
1900 if (_bt_mesh_is_provisioning() ||
1901 _bt_mesh_is_scanning()) {
1902 BT_ERR("Device is buzy..");
1903 return BLUETOOTH_ERROR_DEVICE_BUSY;
1906 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1907 strlen(req->net_uuid), net_uuid.uuid, 16);
1909 BT_INFO("Mesh: Virtual Group Subscription Req: [%d] Group Addr [0x%2.2x]",
1910 req->op, req->sub_addr);
1912 if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1913 /* Subscription address sanity check */
1914 if (MESH_IS_GROUP(req->sub_addr) ||
1915 !(MESH_IS_VIRTUAL(req->sub_addr))) {
1916 BT_ERR("Mesh: Bad Virtual Group subscription address [0x%2.2x\n]",
1918 return BLUETOOTH_ERROR_INVALID_PARAM;
1921 BT_INFO("Mesh: Virtual Group Subscription Req: Delete All");
1923 /* Get Subnet index of the remote node for TX encryption */
1924 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid,
1925 req->primary_unicast);
1926 if (netkey_idx == MESH_NET_IDX_INVALID)
1927 return BLUETOOTH_ERROR_INTERNAL;
1929 if (req->op == BLUETOOTH_MESH_MODEL_SUB_ADD)
1930 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_ADD;
1931 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE)
1932 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_DELETE;
1933 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_OVERWRITE)
1934 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_OVERWRITE;
1935 else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL)
1936 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL;
1938 /* Check pending request */
1939 if (_bt_mesh_check_pending_request(opcode,
1940 req->primary_unicast, net_uuid.uuid)) {
1941 BT_ERR("Device is buzy..");
1942 return BLUETOOTH_ERROR_DEVICE_BUSY;
1945 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
1947 /* Element Address */
1948 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
1951 /* Subscription address */
1952 if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1953 uint8_t label_uuid[16];
1954 if (!_bt_mesh_network_get_label_uuid_from_sub_addr(
1955 net_uuid.uuid, req->sub_addr, label_uuid)) {
1956 BT_ERR("Mesh: Virtual Group Label UUID Not found");
1957 return BLUETOOTH_ERROR_INVALID_PARAM;
1959 memcpy(buffer + data_len, label_uuid, 16);
1963 BT_INFO("Mesh: Virtual Group Subscription Model ID [0x%4.4x]", req->model);
1964 /* Insert Model ID */
1965 if (req->model >= 0xFFFF0000) {
1966 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
1967 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1970 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
1971 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
1973 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1977 ret = mesh_conf_send_message(&net_uuid,
1978 req->primary_unicast, true,
1979 netkey_idx, buffer, data_len);
1981 if (ret != OAL_STATUS_SUCCESS) {
1982 BT_ERR("ret: %d", ret);
1983 return BLUETOOTH_ERROR_INTERNAL;
1986 BT_INFO("Mesh: Virtual Group Subscription Command sent successfully");
1987 /* Queue the request with timeout */
1988 __bt_mesh_add_request(opcode, req->primary_unicast,
1990 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
1992 return BLUETOOTH_ERROR_NONE;
1995 int _bt_mesh_model_get_publication(const char *app_cred, const char *sender,
1996 bluetooth_mesh_model_configure_t *req)
1998 int ret = OAL_STATUS_SUCCESS;
1999 uint16_t netkey_idx;
2000 oal_uuid_t net_uuid;
2002 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2004 /* If Scanning is going on */
2005 if (_bt_mesh_is_provisioning() ||
2006 _bt_mesh_is_scanning()) {
2007 BT_ERR("Device is buzy..");
2008 return BLUETOOTH_ERROR_DEVICE_BUSY;
2011 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
2012 strlen(req->net_uuid), net_uuid.uuid, 16);
2014 /* Check pending request */
2015 if (_bt_mesh_check_pending_request(MESH_OPCODE_CONFIG_MODEL_PUB_GET,
2016 req->primary_unicast, net_uuid.uuid)) {
2017 BT_ERR("Device is buzy..");
2018 return BLUETOOTH_ERROR_DEVICE_BUSY;
2021 /* Get Subnet index of the remote node for TX encryption */
2022 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2023 if (netkey_idx == MESH_NET_IDX_INVALID)
2024 return BLUETOOTH_ERROR_INTERNAL;
2027 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_CONFIG_MODEL_PUB_GET, buffer);
2029 /* Element Address */
2030 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2033 /* Insert Model ID */
2034 if (req->model >= 0xFFFF0000) {
2035 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2036 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2039 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2040 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2042 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2046 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2048 if (ret != OAL_STATUS_SUCCESS) {
2049 BT_ERR("ret: %d", ret);
2050 return BLUETOOTH_ERROR_INTERNAL;
2053 /* Queue the request with timeout */
2054 __bt_mesh_add_request(MESH_OPCODE_CONFIG_MODEL_PUB_GET, req->primary_unicast, net_uuid.uuid,
2055 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2057 return BLUETOOTH_ERROR_NONE;
2060 int _bt_mesh_model_set_publication(const char *app_cred, const char *sender,
2061 bluetooth_mesh_model_configure_t *req)
2063 int ret = OAL_STATUS_SUCCESS;
2064 uint16_t netkey_idx;
2065 oal_uuid_t net_uuid;
2067 uint32_t opcode = 0;
2068 uint8_t label_uuid[16];
2069 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2071 /* If Scanning is going on */
2072 if (_bt_mesh_is_provisioning() ||
2073 _bt_mesh_is_scanning()) {
2074 BT_ERR("Device is buzy..");
2075 return BLUETOOTH_ERROR_DEVICE_BUSY;
2078 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2080 if (req->pub_addr == MESH_ALL_NODES_ADDRESS)
2081 BT_INFO("Mesh: Setting Publication to ALL Node Address");
2083 if (!MESH_IS_GROUP(req->pub_addr) && !MESH_IS_VIRTUAL(req->pub_addr) &&
2084 req->pub_addr != MESH_UNASSIGNED_ADDRESS) {
2085 BT_ERR("Mesh: Bad Publication address %x\n", req->pub_addr);
2086 return BLUETOOTH_ERROR_INVALID_PARAM;
2089 if (req->pub_addr != MESH_UNASSIGNED_ADDRESS) {
2090 if (MESH_IS_VIRTUAL(req->pub_addr)) {
2091 if (!_bt_mesh_network_get_label_uuid_from_sub_addr(
2092 net_uuid.uuid, req->pub_addr, label_uuid))
2093 return BLUETOOTH_ERROR_INVALID_PARAM;
2094 opcode = MESH_OPCODE_CONFIG_MODEL_PUB_VIRT_SET;
2096 } else if (MESH_IS_GROUP(req->pub_addr))
2097 opcode = MESH_OPCODE_CONFIG_MODEL_PUB_SET;
2100 /* Check pending request */
2101 if (_bt_mesh_check_pending_request(opcode,
2102 req->primary_unicast, net_uuid.uuid)) {
2103 BT_ERR("Device is buzy..");
2104 return BLUETOOTH_ERROR_DEVICE_BUSY;
2107 /* Get Subnet index of the remote node for TX encryption */
2108 netkey_idx = _bt_mesh_node_get_subnet_idx(
2109 net_uuid.uuid, req->primary_unicast);
2110 if (netkey_idx == MESH_NET_IDX_INVALID)
2111 return BLUETOOTH_ERROR_INTERNAL;
2114 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2116 /* Element Address */
2117 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2120 /* Fill Publication Address */
2121 if (MESH_IS_VIRTUAL(req->pub_addr)) {
2122 memcpy(buffer + data_len, label_uuid, 16);
2125 l_put_le16(req->pub_addr, buffer + data_len);
2129 /* AppKey index + credential (set to 0) */
2130 l_put_le16(req->appkey_idx, buffer + data_len);
2134 buffer[data_len++] = req->ttl;
2135 /* Publish period step count and step resolution */
2136 buffer[data_len++] = req->period;
2137 /* Publish retransmit count & interval steps */
2138 buffer[data_len++] = req->retransmit;
2140 /* Insert Model ID */
2141 if (req->model >= 0xFFFF0000) {
2142 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2143 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2146 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2147 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2149 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2153 ret = mesh_conf_send_message(&net_uuid,
2154 req->primary_unicast, true,
2155 netkey_idx, buffer, data_len);
2157 if (ret != OAL_STATUS_SUCCESS) {
2158 BT_ERR("ret: %d", ret);
2159 return BLUETOOTH_ERROR_INTERNAL;
2162 /* Queue the request with timeout */
2163 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2164 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2166 return BLUETOOTH_ERROR_NONE;
2169 int _bt_mesh_node_model_get_subscription_list(const char *app_cred, const char *sender,
2170 bluetooth_mesh_model_configure_t *req)
2172 int ret = OAL_STATUS_SUCCESS;
2173 uint16_t netkey_idx;
2174 oal_uuid_t net_uuid;
2176 uint32_t opcode = 0;
2177 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2179 /* If Scanning is going on */
2180 if (_bt_mesh_is_provisioning() ||
2181 _bt_mesh_is_scanning()) {
2182 BT_ERR("Device is buzy..");
2183 return BLUETOOTH_ERROR_DEVICE_BUSY;
2186 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2188 /* Insert Model ID */
2189 if (req->model >= 0xFFFF0000)
2190 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2191 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_GET;
2193 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2194 opcode = MESH_OPCODE_CONFIG_VEND_MODEL_SUB_GET;
2196 /* Check pending request */
2197 if (_bt_mesh_check_pending_request(opcode,
2198 req->primary_unicast, net_uuid.uuid)) {
2199 BT_ERR("Device is buzy..");
2200 return BLUETOOTH_ERROR_DEVICE_BUSY;
2202 /* Get Subnet index of the remote node for TX encryption */
2203 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2204 if (netkey_idx == MESH_NET_IDX_INVALID)
2205 return BLUETOOTH_ERROR_INTERNAL;
2207 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2208 /* Element Address */
2209 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2212 /* Insert Model ID */
2213 if (opcode == MESH_OPCODE_CONFIG_MODEL_SUB_GET) {
2214 BT_INFO("Mesh: Get Subscription List for BT SIG Model");
2215 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2216 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2219 BT_INFO("Mesh: Get Subscription List for Vendor Model");
2220 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2221 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2223 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2227 ret = mesh_conf_send_message(&net_uuid,
2228 req->primary_unicast, true, netkey_idx, buffer, data_len);
2230 if (ret != OAL_STATUS_SUCCESS) {
2231 BT_ERR("ret: %d", ret);
2232 return BLUETOOTH_ERROR_INTERNAL;
2235 /* Queue the request with timeout */
2236 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2237 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2239 return BLUETOOTH_ERROR_NONE;
2242 int _bt_mesh_node_model_get_appkey_list(const char *app_cred, const char *sender,
2243 bluetooth_mesh_model_configure_t *req)
2245 int ret = OAL_STATUS_SUCCESS;
2246 uint16_t netkey_idx;
2247 oal_uuid_t net_uuid;
2249 uint32_t opcode = 0;
2250 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2252 /* If Scanning is going on */
2253 if (_bt_mesh_is_provisioning() ||
2254 _bt_mesh_is_scanning()) {
2255 BT_ERR("Device is buzy..");
2256 return BLUETOOTH_ERROR_DEVICE_BUSY;
2259 _bt_mesh_util_convert_string_to_hex(req->net_uuid,
2260 strlen(req->net_uuid), net_uuid.uuid, 16);
2262 /* Insert Model ID */
2263 if (req->model >= 0xFFFF0000)
2264 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2265 opcode = MESH_OPCODE_MODEL_APP_GET;
2267 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2268 opcode = MESH_OPCODE_VENDOR_MODEL_APP_GET;
2270 /* Check pending request */
2271 if (_bt_mesh_check_pending_request(opcode,
2272 req->primary_unicast, net_uuid.uuid)) {
2273 BT_ERR("Device is buzy..");
2274 return BLUETOOTH_ERROR_DEVICE_BUSY;
2276 /* Get Subnet index of the remote node for TX encryption */
2277 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2278 if (netkey_idx == MESH_NET_IDX_INVALID)
2279 return BLUETOOTH_ERROR_INTERNAL;
2281 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2282 /* Element Address */
2283 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2287 /* Insert Model ID */
2288 if (opcode == MESH_OPCODE_MODEL_APP_GET) {
2289 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2290 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2293 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2294 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2296 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2300 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2302 if (ret != OAL_STATUS_SUCCESS) {
2303 BT_ERR("ret: %d", ret);
2304 return BLUETOOTH_ERROR_INTERNAL;
2307 /* Queue the request with timeout */
2308 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2309 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2311 return BLUETOOTH_ERROR_NONE;
2314 int _bt_mesh_node_model_appkey_execute(const char *app_cred, const char *sender,
2315 bluetooth_mesh_model_configure_t *req)
2317 int ret = OAL_STATUS_SUCCESS;
2318 uint16_t netkey_idx;
2319 oal_uuid_t net_uuid;
2321 uint32_t opcode = 0;
2322 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2324 /* If Scanning is going on */
2325 if (_bt_mesh_is_provisioning() ||
2326 _bt_mesh_is_scanning()) {
2327 BT_ERR("Device is buzy..");
2328 return BLUETOOTH_ERROR_DEVICE_BUSY;
2331 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2333 /* Check pending request */
2334 if (_bt_mesh_check_pending_request(req->is_bind ? MESH_OPCODE_MODEL_APP_BIND : MESH_OPCODE_MODEL_APP_UNBIND,
2335 req->primary_unicast, net_uuid.uuid)) {
2336 BT_ERR("Device is buzy..");
2337 return BLUETOOTH_ERROR_DEVICE_BUSY;
2339 /* Get Subnet index of the remote node for TX encryption */
2340 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2341 if (netkey_idx == MESH_NET_IDX_INVALID)
2342 return BLUETOOTH_ERROR_INTERNAL;
2345 opcode = MESH_OPCODE_MODEL_APP_BIND;
2347 opcode = MESH_OPCODE_MODEL_APP_UNBIND;
2349 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2350 /* Element Address */
2351 l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2355 l_put_le16(req->appkey_idx, buffer + data_len);
2358 /* Insert Model ID */
2359 if (req->model >= 0xFFFF0000) {
2360 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2361 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2364 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2365 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2367 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2371 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2373 if (ret != OAL_STATUS_SUCCESS) {
2374 BT_ERR("ret: %d", ret);
2375 return BLUETOOTH_ERROR_INTERNAL;
2378 /* Queue the request with timeout */
2379 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2380 g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2382 return BLUETOOTH_ERROR_NONE;
2385 int _bt_mesh_ttl_execute_remote_node(const char *app_cred, const char *sender,
2386 bluetooth_mesh_node_ttl_info_t *req)
2388 int ret = OAL_STATUS_SUCCESS;
2389 uint16_t netkey_idx;
2391 oal_uuid_t net_uuid;
2392 uint32_t opcode = 0;
2393 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2395 /* If Scanning is going on */
2396 if (_bt_mesh_is_provisioning() ||
2397 _bt_mesh_is_scanning()) {
2398 BT_ERR("Device is buzy..");
2399 return BLUETOOTH_ERROR_DEVICE_BUSY;
2402 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2404 /* Check pending request */
2405 if (_bt_mesh_check_pending_request(req->is_set ? MESH_OPCODE_CONFIG_DEFAULT_TTL_SET : MESH_OPCODE_CONFIG_DEFAULT_TTL_GET,
2406 req->unicast, net_uuid.uuid)) {
2407 BT_ERR("Device is buzy..");
2408 return BLUETOOTH_ERROR_DEVICE_BUSY;
2411 /* Get Subnet index of the rmeote node for TX encryption */
2412 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->unicast);
2413 if (netkey_idx == MESH_NET_IDX_INVALID)
2414 return BLUETOOTH_ERROR_INTERNAL;
2417 opcode = MESH_OPCODE_CONFIG_DEFAULT_TTL_SET;
2418 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_CONFIG_DEFAULT_TTL_SET, buffer);
2419 buffer[data_len++] = req->ttl;
2421 opcode = MESH_OPCODE_CONFIG_DEFAULT_TTL_GET;
2422 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_CONFIG_DEFAULT_TTL_GET, buffer);
2425 ret = mesh_conf_send_message(&net_uuid, req->unicast, true, netkey_idx, buffer, data_len);
2427 if (ret != OAL_STATUS_SUCCESS) {
2428 BT_ERR("ret: %d", ret);
2429 return BLUETOOTH_ERROR_INTERNAL;
2432 /* Queue the request with timeout */
2433 __bt_mesh_add_request(opcode, req->unicast, net_uuid.uuid,
2434 g_memdup(req, sizeof(bluetooth_mesh_node_ttl_info_t)));
2435 return BLUETOOTH_ERROR_NONE;
2438 static bool __bt_mesh_check_pending_key_cmd(uint16_t dest, bool is_netkey,
2439 bluetooth_mesh_node_key_conf_e op, uint8_t net_uuid[], uint32_t *opcode)
2441 const struct mesh_config_cmd *cmd;
2445 case BLUETOOTH_MESH_NODE_KEY_ADD:
2446 cmd = __mesh_get_command(MESH_OPCODE_NETKEY_ADD);
2449 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2450 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2453 *opcode = MESH_OPCODE_NETKEY_ADD;
2455 case BLUETOOTH_MESH_NODE_KEY_DELETE:
2456 cmd = __mesh_get_command(MESH_OPCODE_NETKEY_DELETE);
2459 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2460 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2463 *opcode = MESH_OPCODE_NETKEY_DELETE;
2465 case BLUETOOTH_MESH_NODE_KEY_UPDATE:
2466 cmd = __mesh_get_command(MESH_OPCODE_NETKEY_UPDATE);
2469 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2470 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2473 *opcode = MESH_OPCODE_NETKEY_UPDATE;
2478 case BLUETOOTH_MESH_NODE_KEY_ADD:
2479 cmd = __mesh_get_command(MESH_OPCODE_APPKEY_ADD);
2482 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2483 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2486 *opcode = MESH_OPCODE_APPKEY_ADD;
2488 case BLUETOOTH_MESH_NODE_KEY_DELETE:
2489 cmd = __mesh_get_command(MESH_OPCODE_APPKEY_DELETE);
2492 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2493 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2496 *opcode = MESH_OPCODE_APPKEY_DELETE;
2498 case BLUETOOTH_MESH_NODE_KEY_UPDATE:
2499 cmd = __mesh_get_command(MESH_OPCODE_APPKEY_UPDATE);
2502 if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2503 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2506 *opcode = MESH_OPCODE_APPKEY_UPDATE;
2513 int _bt_mesh_node_configure_key(const char *app_cred, const char *sender,
2514 bluetooth_mesh_key_configure_t *req)
2516 int ret = OAL_STATUS_SUCCESS;
2517 uint16_t netkey_idx;
2518 uint16_t bound_netkey_idx = 0x0000;
2519 oal_uuid_t net_uuid;
2520 uint32_t opcode = 0;
2523 uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2525 /* If Scanning is going on */
2526 if (_bt_mesh_is_provisioning() ||
2527 _bt_mesh_is_scanning()) {
2528 BT_ERR("Device is buzy..");
2529 return BLUETOOTH_ERROR_DEVICE_BUSY;
2532 _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2534 if (req->is_netkey && !_bt_mesh_keys_subnet_exists(net_uuid.uuid, req->netkey_idx)) {
2535 BT_ERR("Local Subnet not found..");
2536 return BLUETOOTH_ERROR_INVALID_PARAM;
2539 /* For Appkey Configuration, check for available bound netkey */
2540 if (!req->is_netkey) {
2541 bound_netkey_idx = _bt_mesh_keys_get_bound_key(net_uuid.uuid, req->appkey_idx);
2542 if (bound_netkey_idx == MESH_NET_IDX_INVALID) {
2543 BT_ERR("Local AppKey not found..");
2544 return BLUETOOTH_ERROR_INVALID_PARAM;
2548 /* Check pending request */
2549 if (__bt_mesh_check_pending_key_cmd(req->primary_unicast,
2550 req->is_netkey, req->op, net_uuid.uuid, &opcode)) {
2551 BT_ERR("Device is buzy..");
2552 return BLUETOOTH_ERROR_DEVICE_BUSY;
2555 /* Get Subnet index of the rmeote node for TX encryption */
2556 netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2557 if (netkey_idx == MESH_NET_IDX_INVALID)
2558 return BLUETOOTH_ERROR_INTERNAL;
2560 /* Handle Key (App/Net) Delete Commands: Configuration Message */
2561 if (opcode == MESH_OPCODE_NETKEY_DELETE || opcode == MESH_OPCODE_APPKEY_DELETE) {
2562 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2563 if (req->is_netkey) {
2564 l_put_le16(req->netkey_idx, buffer + data_len);
2567 buffer[data_len] = bound_netkey_idx;
2568 buffer[data_len + 1] = ((bound_netkey_idx >> 8) & 0xf) | ((req->appkey_idx << 4) & 0xf0);
2569 buffer[data_len + 2] = req->appkey_idx >> 4;
2573 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2575 /* Handle Key (App/Net) Update & Add Commands: Key Config message */
2576 update = (opcode == MESH_OPCODE_NETKEY_UPDATE || opcode == MESH_OPCODE_APPKEY_UPDATE);
2577 ret = mesh_conf_send_key_message(&net_uuid, req->primary_unicast, req->is_netkey,
2578 update, req->is_netkey ? req->netkey_idx : req->appkey_idx, netkey_idx);
2580 if (ret != OAL_STATUS_SUCCESS) {
2581 BT_ERR("ret: %d", ret);
2582 return BLUETOOTH_ERROR_INTERNAL;
2585 /* Queue the request with timeout */
2586 __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2587 g_memdup(req, sizeof(bluetooth_mesh_key_configure_t)));
2588 return BLUETOOTH_ERROR_NONE;