9b57173791a2c01152a151fdb7a06059359e0046
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / services / mesh / bt-service-mesh-config-client.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
5  *
6  * @author: Anupam Roy <anupam.r@samsung.com>
7  *
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
11  *
12  *              http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21 #include <glib.h>
22 #include <dlog.h>
23 #include <fcntl.h>
24 #include <dirent.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <ell/ell.h>
31
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"
37
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"
47
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"
54
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"
60
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>
66 #include <oal-mesh.h>
67
68 #include "bt-internal-types.h"
69
70 #define MESH_CONFIG_BUFFER_MAX_LEN 100
71
72
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,
77                         unsigned int size);
78
79 struct mesh_config_cmd {
80         uint32_t opcode;
81         uint32_t response;
82         const char *descriptor;
83 };
84
85 static struct l_queue *pending_requests;
86
87 struct mesh_pending_request {
88         struct l_timeout *timer;
89         const struct mesh_config_cmd *cmd;
90         uint8_t net_uuid[16];
91         uint16_t addr;
92         void *data;
93 };
94
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,
108                 "DefaultTTLGet" },
109         { MESH_OPCODE_CONFIG_DEFAULT_TTL_SET, MESH_OPCODE_CONFIG_DEFAULT_TTL_STATUS,
110                 "DefaultTTLSet" },
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,
131                 "ModelSubDelete" },
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,
138                 "ModelSubVirtAdd" },
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,
158                 "PollTimeoutList" },
159         { MESH_OPCODE_CONFIG_POLL_TIMEOUT_STATUS, MESH_RESPONSE_NONE, "PollTimeoutStatus" },
160         { MESH_OPCODE_CONFIG_HEARTBEAT_PUB_GET, MESH_OPCODE_CONFIG_HEARTBEAT_PUB_STATUS,
161                 "HeartbeatPubGet" },
162         { MESH_OPCODE_CONFIG_HEARTBEAT_PUB_SET, MESH_OPCODE_CONFIG_HEARTBEAT_PUB_STATUS,
163                 "HeartbeatPubSet" },
164         { MESH_OPCODE_CONFIG_HEARTBEAT_PUB_STATUS, MESH_RESPONSE_NONE, "HeartbeatPubStatus" },
165         { MESH_OPCODE_CONFIG_HEARTBEAT_SUB_GET, MESH_OPCODE_CONFIG_HEARTBEAT_SUB_STATUS,
166                 "HeartbeatSubGet" },
167         { MESH_OPCODE_CONFIG_HEARTBEAT_SUB_SET, MESH_OPCODE_CONFIG_HEARTBEAT_SUB_STATUS,
168                 "HeartbeatSubSet" },
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" }
188 };
189
190
191 static const struct mesh_config_cmd *__mesh_get_command(uint32_t opcode)
192 {
193         uint32_t n;
194
195         for (n = 0; n < L_ARRAY_SIZE(commands); n++) {
196                 if (opcode == commands[n].opcode)
197                         return &commands[n];
198         }
199
200         return NULL;
201 }
202
203 static const char *__mesh_get_opcode_string(uint32_t opcode)
204 {
205         const struct mesh_config_cmd *cmd;
206
207         cmd = __mesh_get_command(opcode);
208         if (!cmd)
209                 return "Unknown Command Received";
210
211         return cmd->descriptor;
212 }
213
214 static void __mesh_request_remove(void *a)
215 {
216         struct mesh_pending_request *req = a;
217
218         if (req->data)
219                 l_free(req->data);
220         l_timeout_remove(req->timer);
221         l_free(req);
222 }
223
224 static void __bt_mesh_wait_response_timeout(
225                 struct l_timeout *timeout, void *user_data)
226 {
227         struct mesh_pending_request *req = user_data;
228
229         BT_INFO("Mesh: No response for \"%s\" from %4.4x\n",
230                         req->cmd->descriptor, req->addr);
231
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 */
234
235
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));
241
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);
250
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;
256
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));
260
261                 g_free(event);
262                 break;
263         }
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));
272         break;
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));
284                 break;
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));
292                 break;
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));
301                 break;
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));
310                 break;
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));
322                 break;
323         case MESH_OPCODE_CONFIG_MODEL_PUB_GET:
324                 /* Send event with timeout */
325                 __bt_mesh_handle_pending_dev_config_request_info(
326                         BLUETOOTH_ERROR_TIMEOUT,
327                                 BT_MESH_MODEL_GET_PUBLICATION, req->data,
328                                 sizeof(bluetooth_mesh_model_configure_t));
329                 break;
330         case MESH_OPCODE_CONFIG_MODEL_PUB_SET:
331                 /* Send event with timeout */
332                 __bt_mesh_handle_pending_dev_config_request_info(
333                         BLUETOOTH_ERROR_TIMEOUT,
334                                 BT_MESH_MODEL_SET_PUBLICATION, req->data,
335                                 sizeof(bluetooth_mesh_model_configure_t));
336                 break;
337         case MESH_OPCODE_MODEL_APP_BIND:
338         case MESH_OPCODE_MODEL_APP_UNBIND:
339                 /* Send event with timeout */
340                 __bt_mesh_handle_pending_dev_config_request_info(
341                         BLUETOOTH_ERROR_TIMEOUT,
342                                 BT_MESH_MODEL_CONFIGURE_APPKEY, req->data,
343                                 sizeof(bluetooth_mesh_model_configure_t));
344                 break;
345         default:
346                 break;
347         }
348         BT_INFO("Mesh: Number of pending requests [%u] Remove the req",
349                 l_queue_length(pending_requests));
350         l_queue_remove(pending_requests, req);
351         __mesh_request_remove(req);
352 }
353
354 static void __bt_mesh_add_request(uint32_t opcode, uint16_t dest,
355                         uint8_t net_uuid[], void *data)
356 {
357         struct mesh_pending_request *req;
358         const struct mesh_config_cmd *cmd;
359         char uuid_str[33];
360
361         cmd = __mesh_get_command(opcode);
362         if (!cmd)
363                 return;
364         _bt_mesh_util_convert_hex_to_string((uint8_t *) net_uuid, 16, uuid_str,
365                 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
366         BT_INFO("Mesh: Net UUID[%s]", uuid_str);
367
368         BT_INFO("Mesh: Adding command opcode [0x%2.2x] response [0x%2.2x]",
369                         cmd->opcode, cmd->response);
370         req = l_new(struct mesh_pending_request, 1);
371         req->cmd = cmd;
372         req->addr = dest;
373         req->data = data;
374         memcpy(req->net_uuid, net_uuid, 16);
375         req->timer = l_timeout_create(MESH_DEFAULT_RESPONSE_TIMEOUT,
376                         __bt_mesh_wait_response_timeout, req, NULL);
377
378         if (!pending_requests)
379                 pending_requests = l_queue_new();
380         l_queue_push_tail(pending_requests, req);
381         BT_INFO("Mesh: Number of pending requests [%u]", l_queue_length(pending_requests));
382 }
383
384 static struct mesh_pending_request *__bt_mesh_get_request_by_response(
385                 uint16_t addr, uint8_t net_uuid[],
386                         uint32_t response)
387 {
388         const struct l_queue_entry *entry;
389         char uuid_str[33];
390         char uuid_str1[33];
391
392         BT_INFO("Mesh: Number of pending requests [%u]", l_queue_length(pending_requests));
393         entry = l_queue_get_entries(pending_requests);
394
395         for (; entry; entry = entry->next) {
396                 struct mesh_pending_request *req = entry->data;
397
398                 /* Test */
399                 BT_INFO("Mesh: Req addr [0x%2.2x] req opcode [0x%2.2x] res [0x%2.2x]", req->addr, req->cmd->opcode, req->cmd->response);
400                 BT_INFO("Mesh: Current req addr [0x%2.2x] res [0x%2.2x]", addr, response);
401                 _bt_mesh_util_convert_hex_to_string((uint8_t *) net_uuid, 16, uuid_str,
402                                 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
403                 BT_INFO("Mesh: Net UUID[%s]", uuid_str);
404
405                 _bt_mesh_util_convert_hex_to_string((uint8_t *) req->net_uuid, 16, uuid_str1,
406                                 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
407                 BT_INFO("Mesh: Net UUID1[%s]", uuid_str1);
408                 if (!memcmp(net_uuid, req->net_uuid, 16) &&
409                                 req->addr == addr &&
410                                 req->cmd->response == response)
411                         return req;
412         }
413
414         return NULL;
415 }
416
417 bool _bt_mesh_check_pending_request(uint32_t opcode,
418                 uint16_t dest, uint8_t net_uuid[])
419 {
420         const struct mesh_config_cmd *cmd;
421         cmd = __mesh_get_command(opcode);
422
423         if (!cmd)
424                 return false;
425
426         if (__bt_mesh_get_request_by_response(dest,
427                                 net_uuid, cmd->response)) {
428                 BT_ERR("Mesh:Another command is pending\n");
429                 return true;
430         }
431         return false;
432 }
433
434 static uint32_t __bt_mesh_print_model_identifier(uint8_t *data,
435                 bool vendor, const char *offset)
436 {
437         uint32_t mod_id;
438
439         if (!vendor) {
440                 mod_id = l_get_le16(data);
441                 BT_INFO("%sModel ID\t%4.4x\n", offset, mod_id);
442                 mod_id = MESH_VENDOR_ID_MASK | mod_id;
443         } else {
444                 mod_id = l_get_le16(data + 2);
445                 BT_INFO("%sModel ID\t%4.4x %4.4x\n", offset,
446                                 l_get_le16(data), mod_id);
447                 mod_id = l_get_le16(data) << 16 | mod_id;
448         }
449
450         return mod_id;
451 }
452
453 static void __bt_mesh_print_device_composition_data(
454                 uint8_t *data, uint16_t len)
455 {
456         uint16_t features;
457         int i = 0;
458
459         BT_INFO("Mesh: Received composion:\n");
460
461         /* skip page -- We only support Page Zero */
462         data++;
463         len--;
464
465         BT_INFO("\tCID: %4.4x", l_get_le16(&data[0]));
466         BT_INFO("\tPID: %4.4x", l_get_le16(&data[2]));
467         BT_INFO("\tVID: %4.4x", l_get_le16(&data[4]));
468         BT_INFO("\tCRPL: %4.4x", l_get_le16(&data[6]));
469
470         features = l_get_le16(&data[8]);
471         data += 10;
472         len -= 10;
473
474         BT_INFO("\tFeature support:\n");
475         BT_INFO("\t\trelay: %s\n", (features & MESH_FEATURE_RELAY) ?
476                         "yes" : "no");
477         BT_INFO("\t\tproxy: %s\n", (features & MESH_FEATURE_PROXY) ?
478                         "yes" : "no");
479         BT_INFO("\t\tfriend: %s\n", (features & MESH_FEATURE_FRIEND) ?
480                         "yes" : "no");
481         BT_INFO("\t\tlpn: %s\n", (features & MESH_FEATURE_LPN) ?
482                         "yes" : "no");
483         while (len) {
484                 uint8_t m, v;
485
486                 BT_INFO("\t Element %d:\n", i);
487                 BT_INFO("\t\tlocation: %4.4x\n", l_get_le16(data));
488                 data += 2;
489                 len -= 2;
490
491                 m = *data++;
492                 v = *data++;
493                 len -= 2;
494
495                 if (m)
496                         BT_INFO("\t\tSIG defined models:\n");
497
498                 while (len >= 2 && m--) {
499                         __bt_mesh_print_model_identifier(data, false, "\t\t  ");
500                         data += 2;
501                         len -= 2;
502                 }
503
504                 if (v)
505                         BT_INFO("\t\t Vendor defined models:\n");
506
507                 while (len >= 4 && v--) {
508                         __bt_mesh_print_model_identifier(data, true, "\t\t  ");
509                         data += 4;
510                         len -= 4;
511                 }
512
513                 i++;
514         }
515 }
516
517 static void __bt_mesh_send_model_publication_status_event(
518                 int event, int result,
519                         bluetooth_mesh_model_configure_t *evt)
520 {
521         GVariant *out_var = NULL, *param = NULL;
522         GArray *info = NULL;
523
524         if (BLUETOOTH_ERROR_NONE == result) {
525                 BT_INFO("Mesh: Model ID [0x%4.4x]", evt->model);
526                 BT_INFO("Mesh: Model Publication Address [0x%2.2x]", evt->pub_addr);
527                 /* Send event */
528                 info = g_array_new(FALSE, FALSE, sizeof(gchar));
529                 g_array_append_vals(info, evt,
530                                 sizeof(bluetooth_mesh_model_configure_t));
531
532                 out_var = g_variant_new_from_data((const GVariantType *)"ay",
533                                 info->data, info->len,
534                                 TRUE, NULL, NULL);
535
536                 param = g_variant_new("(iv)", result, out_var);
537                 _bt_send_event(BT_MESH_EVENT, event,
538                                 param);
539         }
540 }
541
542 static void __bt_mesh_send_model_subscription_configure_event(
543                 int event, int result,
544                         bluetooth_mesh_model_configure_t *evt)
545 {
546         GVariant *out_var = NULL, *param = NULL;
547         GArray *info = NULL;
548
549         if (BLUETOOTH_ERROR_NONE == result) {
550                 /* Send event */
551                 info = g_array_new(FALSE, FALSE, sizeof(gchar));
552                 g_array_append_vals(info, evt,
553                                 sizeof(bluetooth_mesh_model_configure_t));
554
555                 out_var = g_variant_new_from_data((const GVariantType *)"ay",
556                                 info->data, info->len,
557                                 TRUE, NULL, NULL);
558
559                 param = g_variant_new("(iv)", result, out_var);
560                 _bt_send_event(BT_MESH_EVENT, event,
561                                 param);
562         }
563 }
564
565 static void __bt_mesh_send_model_get_subscription_list_event(
566         int result, bluetooth_mesh_model_configure_t *evt)
567 {
568         GVariant  *param = NULL;
569         GVariantBuilder *builder = NULL;
570         int i;
571         char *net_uuid;
572
573         if (BLUETOOTH_ERROR_NONE == result) {
574                 BT_INFO("Mesh: Total Subscriptions bound to model [%d]",
575                                 evt->sublist_count);
576                 BT_INFO("Mesh: Network [%s]", evt->net_uuid);
577                 builder = g_variant_builder_new(G_VARIANT_TYPE("aq"));
578                 for (i = 0; i < evt->sublist_count; i++) {
579                         BT_INFO("Mesh: Subscription Address [0x%2.2x]",
580                                 *evt->sub_list[i]);
581                         g_variant_builder_add(builder, "q", *evt->sub_list[i]);
582                 }
583
584                 net_uuid = g_strdup(evt->net_uuid);
585                 param = g_variant_new("(isqiui(aq))", result, net_uuid,
586                                 evt->primary_unicast, evt->elem_index, evt->model,
587                                 evt->sublist_count, builder);
588
589                 g_variant_builder_unref(builder);
590
591                 /* Send event */
592                 _bt_send_event(BT_MESH_EVENT,
593                                 BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_LIST,
594                                 param);
595
596                 if (evt->sublist_count) {
597                         /* Free List data */
598                         for (int i = 0; i < evt->sublist_count; i++)
599                                 g_free(evt->sub_list[i]);
600                         g_free(evt->sub_list);
601                 }
602                 g_free(net_uuid);
603                 BT_INFO("freed net uuid");
604         }
605 }
606
607 static void __bt_mesh_send_model_get_appkey_list_event(int result,
608                 bluetooth_mesh_model_configure_t *evt)
609 {
610         GVariant  *param = NULL;
611         GVariantBuilder *builder = NULL;
612         char *net_uuid;
613         int i;
614
615         if (BLUETOOTH_ERROR_NONE == result) {
616                 builder = g_variant_builder_new(G_VARIANT_TYPE("aq"));
617                 BT_INFO("Mesh: Total AppKeys bound to model [%d]",
618                                 evt->appkeylist_count);
619                 BT_INFO("Mesh: Network [%s]", evt->net_uuid);
620                 for (i = 0; i < evt->appkeylist_count; i++)
621                         g_variant_builder_add(builder, "q", *evt->appkey_list[i]);
622                 net_uuid = g_strdup(evt->net_uuid);
623                 param = g_variant_new("(isqiui(aq))", result, net_uuid,
624                                 evt->primary_unicast, evt->elem_index, evt->model,
625                                 evt->appkeylist_count, builder);
626
627                 g_variant_builder_unref(builder);
628
629                 /* Send event */
630                 _bt_send_event(BT_MESH_EVENT,
631                                 BLUETOOTH_EVENT_MESH_MODEL_APPKEY_LIST,
632                                 param);
633
634                 if (evt->appkeylist_count) {
635                         for (int i = 0; i < evt->appkeylist_count; i++)
636                                 g_free(evt->appkey_list[i]);
637
638                         g_free(evt->appkey_list);
639                 }
640                 /* Free List data */
641                 g_free(net_uuid);
642         }
643 }
644
645 static void __bt_mesh_send_model_configure_appkey_event(int result,
646                 bluetooth_mesh_model_configure_t *evt)
647 {
648         GVariant *out_var = NULL, *param = NULL;
649         GArray *info = NULL;
650
651         if (BLUETOOTH_ERROR_NONE == result) {
652                 /* Send event */
653                 info = g_array_new(FALSE, FALSE, sizeof(gchar));
654                 g_array_append_vals(info, evt, sizeof(bluetooth_mesh_model_configure_t));
655
656                 out_var = g_variant_new_from_data((const GVariantType *)"ay",
657                                 info->data, info->len,
658                                 TRUE, NULL, NULL);
659
660                 param = g_variant_new("(iv)", result, out_var);
661                 _bt_send_event(BT_MESH_EVENT,
662                                 BLUETOOTH_EVENT_MESH_MODEL_APPKEY_BIND,
663                                 param);
664         }
665 }
666
667 static void __bt_mesh_send_node_ttl_configuration_event(int result,
668                 bluetooth_mesh_node_ttl_info_t *ttl_evt)
669 {
670         GVariant *out_var = NULL, *param = NULL;
671         GArray *info = NULL;
672
673         if (BLUETOOTH_ERROR_NONE == result) {
674                 /* Send event */
675                 info = g_array_new(FALSE, FALSE, sizeof(gchar));
676                 g_array_append_vals(info, ttl_evt,
677                         sizeof(bluetooth_mesh_node_ttl_info_t));
678
679                 out_var = g_variant_new_from_data((const GVariantType *)"ay",
680                                 info->data, info->len,
681                                 TRUE, NULL, NULL);
682
683                 param = g_variant_new("(iv)", result, out_var);
684                 _bt_send_event(BT_MESH_EVENT,
685                                 BLUETOOTH_EVENT_MESH_NODE_TTL_CONFIGURED,
686                                 param);
687         }
688 }
689
690 static void __bt_mesh_send_node_key_configuration_event(int result,
691                 bluetooth_mesh_key_configure_t *key_evt)
692 {
693         GVariant *out_var = NULL, *param = NULL;
694         GArray *info = NULL;
695
696         if (BLUETOOTH_ERROR_NONE == result) {
697                 /* Send event */
698                 info = g_array_new(FALSE, FALSE, sizeof(gchar));
699                 g_array_append_vals(info, key_evt,
700                         sizeof(bluetooth_mesh_key_configure_t));
701
702                 out_var = g_variant_new_from_data((const GVariantType *)"ay",
703                                 info->data, info->len,
704                                 TRUE, NULL, NULL);
705
706                 param = g_variant_new("(iv)", result, out_var);
707                 _bt_send_event(BT_MESH_EVENT,
708                                 BLUETOOTH_EVENT_MESH_NODE_KEY_CONFIGURED,
709                                 param);
710         }
711 }
712
713 static void __bt_mesh_send_node_get_vendor_features_event(int result,
714                 bluetooth_mesh_node_features_t *features_evt)
715 {
716         GVariant *out_var = NULL, *param = NULL;
717         GArray *info = NULL;
718
719         if (BLUETOOTH_ERROR_NONE == result) {
720                 /* Send event */
721                 info = g_array_new(FALSE, FALSE, sizeof(gchar));
722                 g_array_append_vals(info, features_evt,
723                                 sizeof(bluetooth_mesh_node_features_t));
724
725                 out_var = g_variant_new_from_data((const GVariantType *)"ay",
726                                 info->data, info->len,
727                                 TRUE, NULL, NULL);
728
729                 param = g_variant_new("(iv)", result, out_var);
730                 _bt_send_event(BT_MESH_EVENT,
731                                 BLUETOOTH_EVENT_MESH_NODE_VENDOR_FEATURES,
732                                 param);
733         }
734 }
735
736 static void __bt_mesh_send_node_browsed_event(int result,
737                 bluetooth_mesh_node_discover_t *browse_evt)
738 {
739         GVariant *out_var = NULL, *param = NULL;
740         GArray *info = NULL;
741
742         if (BLUETOOTH_ERROR_NONE == result) {
743                 /* Send event */
744                 info = g_array_new(FALSE, FALSE, sizeof(gchar));
745                 g_array_append_vals(info, browse_evt,
746                                 sizeof(bluetooth_mesh_node_discover_t));
747
748                 out_var = g_variant_new_from_data((const GVariantType *)"ay",
749                                 info->data, info->len,
750                                 TRUE, NULL, NULL);
751
752                 param = g_variant_new("(iv)", result, out_var);
753                 _bt_send_event(BT_MESH_EVENT,
754                                 BLUETOOTH_EVENT_MESH_NODE_BROWSED,
755                                 param);
756         }
757 }
758
759 static void __bt_mesh_handle_pending_dev_config_request_info(int result,
760                 int service_function, void *param, unsigned int size)
761 {
762         GSList *l;
763         GArray *out_param;
764         invocation_info_t *req_info = NULL;
765
766         for (l = _bt_get_invocation_list(); l != NULL; ) {
767                 req_info = l->data;
768                 l = g_slist_next(l);
769                 if (req_info == NULL ||
770                                 req_info->service_function != service_function)
771                         continue;
772
773                 switch (service_function) {
774                 case BT_MESH_NODE_GET_VENDOR_FEATURES: {
775                         bluetooth_mesh_node_features_t *event;
776                         bluetooth_mesh_node_features_t *req;
777
778                         event = (bluetooth_mesh_node_features_t*) param;
779                         req = (bluetooth_mesh_node_features_t*)req_info->user_data;
780
781                         BT_DBG("Request Sender: [%s]", req_info->sender);
782                         /* Match Network and Remote Node unicast*/
783                         if (!g_strcmp0(event->net_uuid, req->net_uuid) && event->unicast == req->unicast) {
784                                 event->unicast = req->unicast;
785                                 event->elem_count = req->elem_count;
786
787                                 /* Send Event */
788                                 __bt_mesh_send_node_get_vendor_features_event(result, event);
789
790                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
791                                 g_array_append_vals(out_param, event, sizeof(bluetooth_mesh_node_features_t));
792
793                                 /* Return DBUS Invocation*/
794                                 _bt_service_method_return(req_info->context, out_param, result);
795                                 _bt_free_info_from_invocation_list(req_info);
796                                 g_array_free(out_param, TRUE);
797                         }
798                         break;
799                 }
800                 case BT_MESH_NODE_BROWSE: {
801                         bluetooth_mesh_node_discover_t *node;
802                         event_mesh_devkey_message_t *event;
803                         uint16_t remote_addr;
804                         uint8_t elem_count;
805                         uint8_t dev_uuid[16];
806                         char net_uuid[BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1];
807
808                         event = (event_mesh_devkey_message_t*) param;
809                         node = (bluetooth_mesh_node_discover_t*)req_info->user_data;
810                         BT_INFO("Mesh: Request Node UUID [%s]", node->dev_uuid);
811                         BT_INFO("Mesh: Request Network UUID [%s]", node->net_uuid);
812
813                         _bt_mesh_util_convert_hex_to_string((uint8_t *) event->net_uuid.uuid, 16, net_uuid,
814                                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
815
816                         _bt_mesh_util_convert_string_to_hex(node->dev_uuid, strlen(node->dev_uuid), dev_uuid, 16);
817
818                         /* Get Unicast from pending request's Dev UUID and match with event */
819                         if (_bt_mesh_node_get_unicast_from_dev_uuid(event->net_uuid.uuid, dev_uuid, &remote_addr)) {
820
821                                 BT_DBG("Request Sender: [%s]", req_info->sender);
822                                 /* Match Network and Remote Node unicast*/
823                                 if (!g_strcmp0(node->net_uuid, net_uuid) && remote_addr == event->source) {
824                                         _bt_mesh_node_get_element_count(event->net_uuid.uuid, remote_addr, &elem_count);
825                                         node->unicast = event->source;
826                                         node->count = elem_count;
827                                         BT_INFO("Mesh: Browse event for Node: Unicast [0x%2.2x] Element Count [%d]",
828                                                 node->unicast, elem_count);
829                                         /* Send Event */
830                                         __bt_mesh_send_node_browsed_event(result, node);
831
832                                         out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
833                                         g_array_append_vals(out_param, node, sizeof(bluetooth_mesh_node_discover_t));
834
835                                         /* Return DBUS Invocation*/
836                                         _bt_service_method_return(req_info->context, out_param, result);
837                                         _bt_free_info_from_invocation_list(req_info);
838                                         g_array_free(out_param, TRUE);
839
840                                 }
841                         }
842                         break;
843                 }
844                 case BT_MESH_NODE_CONFIGURE_KEY: {
845                         bluetooth_mesh_key_configure_t *event;
846                         bluetooth_mesh_key_configure_t *req;
847
848                         event = (bluetooth_mesh_key_configure_t*) param;
849                         req = (bluetooth_mesh_key_configure_t*)req_info->user_data;
850
851                         if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
852                                 req->primary_unicast == event->primary_unicast &&
853                                 req->is_netkey == event->is_netkey) {
854                                 /* Send Event */
855                                 __bt_mesh_send_node_key_configuration_event(result, event);
856
857                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
858                                 g_array_append_vals(out_param, event, sizeof(bluetooth_mesh_key_configure_t));
859
860                                 /* Return DBUS Invocation*/
861                                 _bt_service_method_return(req_info->context, out_param, result);
862                                 _bt_free_info_from_invocation_list(req_info);
863                                 g_array_free(out_param, TRUE);
864                         }
865                         break;
866                 }
867                 case BT_MESH_NODE_TTL_EXECUTE: {
868                         bluetooth_mesh_node_ttl_info_t *event;
869                         bluetooth_mesh_node_ttl_info_t *req;
870
871                         event = (bluetooth_mesh_node_ttl_info_t*) param;
872                         req = (bluetooth_mesh_node_ttl_info_t*)req_info->user_data;
873                         req->ttl = event->ttl;
874
875                         if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
876                                 req->unicast == event->unicast) {
877                                 /* Send Event */
878                                 __bt_mesh_send_node_ttl_configuration_event(result, req);
879
880                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
881                                 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_node_ttl_info_t));
882
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);
887                         }
888                         break;
889                 }
890                 case BT_MESH_MODEL_CONFIGURE_APPKEY: {
891                         bluetooth_mesh_model_configure_t *event;
892                         bluetooth_mesh_model_configure_t *req;
893
894                         event = (bluetooth_mesh_model_configure_t*) param;
895                         req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
896
897                         if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
898                                 req->primary_unicast == event->primary_unicast) {
899                                 /* Send Event */
900                                 __bt_mesh_send_model_configure_appkey_event(result, req);
901
902                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
903                                 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
904
905                                 /* Return DBUS Invocation*/
906                                 _bt_service_method_return(req_info->context, out_param, result);
907                                 _bt_free_info_from_invocation_list(req_info);
908                                 g_array_free(out_param, TRUE);
909                         }
910
911                         break;
912                 }
913                 case BT_MESH_MODEL_GET_APPKEY_LIST: {
914                         bluetooth_mesh_model_configure_t *event;
915                         bluetooth_mesh_model_configure_t *req;
916
917                         event = (bluetooth_mesh_model_configure_t*) param;
918                         req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
919
920                         if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
921                                 req->primary_unicast == event->primary_unicast) {
922                                 /* Send Event */
923                                 __bt_mesh_send_model_get_appkey_list_event(result, event);
924
925                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
926                                 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
927
928                                 /* Return DBUS Invocation*/
929                                 _bt_service_method_return(req_info->context, out_param, result);
930                                 _bt_free_info_from_invocation_list(req_info);
931                                 g_array_free(out_param, TRUE);
932                         }
933                         break;
934                 }
935                 case BT_MESH_MODEL_GET_SUBSCRIPTION_LIST: {
936                         bluetooth_mesh_model_configure_t *event;
937                         bluetooth_mesh_model_configure_t *req;
938
939                         BT_INFO("Mesh: Handle event for BT_MESH_MODEL_GET_SUBSCRIPTION_LIST");
940                         event = (bluetooth_mesh_model_configure_t*) param;
941                         req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
942
943                         if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
944                                 req->primary_unicast == event->primary_unicast) {
945                                 /* Send Event */
946                                 __bt_mesh_send_model_get_subscription_list_event(result, event);
947
948                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
949                                 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
950
951                                 /* Return DBUS Invocation*/
952                                 _bt_service_method_return(req_info->context, out_param, result);
953                                 _bt_free_info_from_invocation_list(req_info);
954                                 g_array_free(out_param, TRUE);
955                         }
956                         break;
957                 }
958                 case BT_MESH_MODEL_CONFIG_GROUP_SUB: {
959                         bluetooth_mesh_model_configure_t *event;
960                         bluetooth_mesh_model_configure_t *req;
961
962                         event = (bluetooth_mesh_model_configure_t*) param;
963                         req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
964                         BT_INFO("Mesh: Event Network [%s] Req Network [%s]",
965                                 event->net_uuid, req->net_uuid);
966
967                         BT_INFO("Mesh: Event Unicast [0x%4.4x] Req Unicast [0x%4.4x]",
968                                 event->primary_unicast, req->primary_unicast);
969
970                         if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
971                                 req->primary_unicast == event->primary_unicast) {
972
973                                 req->sub_addr = event->sub_addr;
974                                 BT_INFO("Mesh: Send event for Model Subscription");
975                                 /* Send Event */
976                                 __bt_mesh_send_model_subscription_configure_event(
977                                         BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_CONFGURED,
978                                                 result, req);
979
980                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
981                                 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
982
983                                 /* Return DBUS Invocation*/
984                                 _bt_service_method_return(req_info->context, out_param, result);
985                                 _bt_free_info_from_invocation_list(req_info);
986                                 g_array_free(out_param, TRUE);
987                         }
988                         break;
989                 }
990                 case BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB: {
991                         bluetooth_mesh_model_configure_t *event;
992                         bluetooth_mesh_model_configure_t *req;
993
994                         event = (bluetooth_mesh_model_configure_t*) param;
995                         req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
996                         BT_INFO("Mesh: Event Network [%s] Req Network [%s]",
997                                 event->net_uuid, req->net_uuid);
998
999                         BT_INFO("Mesh: Event Unicast [0x%4.4x] Req Unicast [0x%4.4x]",
1000                                 event->primary_unicast, req->primary_unicast);
1001                         if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
1002                                 req->primary_unicast == event->primary_unicast) {
1003                                 /* Send Event */
1004                                 __bt_mesh_send_model_subscription_configure_event( \
1005                                         BLUETOOTH_EVENT_MESH_MODEL_VIRTUAL_SUBSCRIPTION_CONFGURED, \
1006                                                 result, req);
1007
1008                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1009                                 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
1010
1011                                 /* Return DBUS Invocation*/
1012                                 _bt_service_method_return(req_info->context, out_param, result);
1013                                 _bt_free_info_from_invocation_list(req_info);
1014                                 g_array_free(out_param, TRUE);
1015
1016                         }
1017                         break;
1018                 }
1019                 case BT_MESH_MODEL_GET_PUBLICATION: {
1020                         BT_INFO("Mesh: Event for Model Get Publication Request");
1021                         bluetooth_mesh_model_configure_t *event;
1022                         bluetooth_mesh_model_configure_t *req;
1023
1024                         event = (bluetooth_mesh_model_configure_t*) param;
1025                         req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
1026
1027                         if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
1028                                         req->primary_unicast == event->primary_unicast) {
1029                                 /* Send Event */
1030                                 BT_INFO("Mesh: Send Model Publication status for Get Request");
1031                                 __bt_mesh_send_model_publication_status_event( \
1032                                                 BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS, \
1033                                                 result, event);
1034
1035                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1036                                 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
1037
1038                                 /* Return DBUS Invocation*/
1039                                 _bt_service_method_return(req_info->context, out_param, result);
1040                                 _bt_free_info_from_invocation_list(req_info);
1041                                 g_array_free(out_param, TRUE);
1042                         }
1043                         break;
1044                 }
1045                 case BT_MESH_MODEL_SET_PUBLICATION: {
1046                         BT_INFO("Mesh: Event for Model Set Publication Request");
1047                         bluetooth_mesh_model_configure_t *event;
1048                         bluetooth_mesh_model_configure_t *req;
1049
1050                         event = (bluetooth_mesh_model_configure_t*) param;
1051                         req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
1052
1053                         if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
1054                                 req->primary_unicast == event->primary_unicast) {
1055                                 /* Send Event */
1056                                 BT_INFO("Mesh: Send Model Publication status for Set Request");
1057                                 __bt_mesh_send_model_publication_status_event( \
1058                                         BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS, \
1059                                                 result, req);
1060
1061                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
1062                                 g_array_append_vals(out_param, req, sizeof(bluetooth_mesh_model_configure_t));
1063
1064                                 /* Return DBUS Invocation*/
1065                                 _bt_service_method_return(req_info->context, out_param, result);
1066                                 _bt_free_info_from_invocation_list(req_info);
1067                                 g_array_free(out_param, TRUE);
1068                         }
1069                         break;
1070                 }
1071                 default:
1072                         BT_DBG("Unknown function(%d)", service_function);
1073                         break;
1074                 }
1075         }
1076 }
1077
1078 const char *__mesh_status_to_string(uint8_t err)
1079 {
1080         switch (err) {
1081         case MESH_STATUS_SUCCESS: return "Success";
1082         case MESH_STATUS_INVALID_ADDRESS: return "Invalid Address";
1083         case MESH_STATUS_INVALID_MODEL: return "Invalid Model";
1084         case MESH_STATUS_INVALID_APPKEY: return "Invalid AppKey";
1085         case MESH_STATUS_INVALID_NETKEY: return "Invalid NetKey";
1086         case MESH_STATUS_INSUFF_RESOURCES: return "Insufficient Resources";
1087         case MESH_STATUS_IDX_ALREADY_STORED: return "Key Idx Already Stored";
1088         case MESH_STATUS_INVALID_PUB_PARAM: return "Invalid Publish Parameters";
1089         case MESH_STATUS_NOT_SUB_MOD: return "Not a Subscribe Model";
1090         case MESH_STATUS_STORAGE_FAIL: return "Storage Failure";
1091         case MESH_STATUS_FEATURE_NO_SUPPORT: return "Feature Not Supported";
1092         case MESH_STATUS_CANNOT_UPDATE: return "Cannot Update";
1093         case MESH_STATUS_CANNOT_REMOVE: return "Cannot Remove";
1094         case MESH_STATUS_CANNOT_BIND: return "Cannot bind";
1095         case MESH_STATUS_UNABLE_CHANGE_STATE: return "Unable to change state";
1096         case MESH_STATUS_CANNOT_SET: return "Cannot set";
1097         case MESH_STATUS_UNSPECIFIED_ERROR: return "Unspecified error";
1098         case MESH_STATUS_INVALID_BINDING: return "Invalid Binding";
1099
1100         default: return "Unknown";
1101         }
1102 }
1103
1104 static void __mesh_handle_model_subscription_event(int result,
1105                 bluetooth_mesh_model_configure_t *param,
1106                         const struct mesh_config_cmd *cmd)
1107 {
1108         BT_INFO("Mesh: Model Subscription Event: Request [%d]", cmd->opcode);
1109         switch (cmd->opcode) {
1110         /* Fall through */
1111         case MESH_OPCODE_CONFIG_MODEL_SUB_ADD:
1112         case MESH_OPCODE_CONFIG_MODEL_SUB_DELETE:
1113         case MESH_OPCODE_CONFIG_MODEL_SUB_OVERWRITE:
1114                 /* Model Bind/UnBind Event */
1115         BT_INFO("Mesh: Handle Event for Request: BT_MESH_MODEL_CONFIG_GROUP_SUB");
1116         __bt_mesh_handle_pending_dev_config_request_info(result,
1117                         BT_MESH_MODEL_CONFIG_GROUP_SUB, param,
1118                                 sizeof(bluetooth_mesh_model_configure_t));
1119                 break;
1120         case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_ADD:
1121         case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_DELETE:
1122         case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_OVERWRITE:
1123         BT_INFO("Mesh: Handle Event for Request: BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB");
1124         __bt_mesh_handle_pending_dev_config_request_info(result,
1125                         BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB, param,
1126                                 sizeof(bluetooth_mesh_model_configure_t));
1127                 break;
1128         case MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL:
1129         BT_INFO("Mesh: Handle Event for Request: MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL");
1130         __bt_mesh_handle_pending_dev_config_request_info(result,
1131                         BT_MESH_MODEL_CONFIG_GROUP_SUB, param,
1132                                 sizeof(bluetooth_mesh_model_configure_t));
1133         __bt_mesh_handle_pending_dev_config_request_info(result,
1134                         BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB, param,
1135                                 sizeof(bluetooth_mesh_model_configure_t));
1136                 break;
1137         default:
1138                 break;
1139         }
1140 }
1141
1142 void _bt_mesh_config_client_devkey_msg_handler(
1143                 event_mesh_devkey_message_t *event)
1144 {
1145         uint32_t opcode;
1146         const struct mesh_config_cmd *cmd;
1147         uint16_t data_len = event->data_len;
1148         uint8_t *data = event->data;
1149         int result = BLUETOOTH_ERROR_NONE;
1150         uint16_t app_idx;
1151         uint16_t addr;
1152         uint16_t net_idx;
1153         uint16_t elem_addr;
1154         uint16_t ele_addr;
1155         uint32_t mod_id;
1156         int n;
1157         struct mesh_pending_request *req;
1158
1159         if (_bt_mesh_util_opcode_get(data, data_len, &opcode, &n) == false)
1160                 return;
1161
1162         BT_INFO("Mesh: Opcode of response data [0x%2.2x], actual data len [%d]", opcode, n);
1163         data_len -= n;
1164         data += n;
1165
1166         BT_INFO("Mesh: Received %s (len %u) opcode [0x%2.2x]",
1167                 __mesh_get_opcode_string(opcode), data_len, opcode);
1168
1169         req = __bt_mesh_get_request_by_response(event->source,
1170                 event->net_uuid.uuid, (opcode & ~MESH_OPCODE_UNRELIABLE));
1171         if (req) {
1172                 BT_INFO("Mesh: Got Config Request");
1173                 cmd = req->cmd;
1174                 l_queue_remove(pending_requests, req);
1175                 __mesh_request_remove(req);
1176         } else {
1177                 cmd = NULL;
1178         }
1179
1180
1181         switch (opcode & ~MESH_OPCODE_UNRELIABLE) {
1182         default:
1183                 return;
1184         case MESH_OPCODE_CONFIG_MODEL_PUB_STATUS: {
1185                 if (data_len != 12 && data_len != 14)
1186                         break;
1187                 bluetooth_mesh_model_configure_t param;
1188                 memset(&param, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1189
1190                 _bt_mesh_util_convert_hex_to_string((uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1191                                 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1192
1193                 BT_INFO("\nNode %4.4x Model Publication status %s\n",
1194                                 event->source, __mesh_status_to_string(data[0]));
1195
1196                 if (data[0] != MESH_STATUS_SUCCESS)
1197                         result = BLUETOOTH_ERROR_INTERNAL;
1198
1199                 /* Extract Element Address */
1200                 ele_addr = l_get_le16(data + 1);
1201
1202                 /* Extract Model ID */
1203                 if (data_len == 14) {
1204                         /* vendor Model */
1205                         mod_id = l_get_le16(data + 10 + 2);
1206                         mod_id = l_get_le16(data) << 16 | mod_id;
1207                 } else {
1208                         /* BT SIG Model */
1209                         mod_id = l_get_le16(data + 10);
1210                         mod_id = MESH_VENDOR_ID_MASK | mod_id;
1211                 }
1212
1213                 param.model = mod_id;
1214                 BT_INFO("Model ID\t%4.4x\n", mod_id);
1215
1216                 param.primary_unicast = event->source;
1217                 param.elem_index = ele_addr - event->source;
1218                 /* Extract Publish Address */
1219                 param.pub_addr = l_get_le16(data + 3);
1220                 BT_INFO("Mesh: Pub Address [0x%2.2x]", param.pub_addr);
1221
1222                 /* Extract Appkey Index */
1223                 param.appkey_idx =  l_get_le16(data + 5);
1224                 BT_INFO("Mesh: AppKey Idx [0x%2.2x]", param.appkey_idx);
1225
1226                 /* Extract TTL */
1227                 param.ttl = data[6];
1228                 BT_INFO("Mesh: TTL [0x%x]", param.ttl);
1229
1230                 /* Extract Period */
1231                 param.period = data[7];
1232                 if (!cmd)
1233                         break;
1234
1235                 if (cmd->opcode == MESH_OPCODE_CONFIG_MODEL_PUB_GET)
1236                         __bt_mesh_handle_pending_dev_config_request_info(result,
1237                                 BT_MESH_MODEL_GET_PUBLICATION, &param,
1238                                 sizeof(bluetooth_mesh_model_configure_t));
1239                 else
1240                         __bt_mesh_handle_pending_dev_config_request_info(result,
1241                                 BT_MESH_MODEL_SET_PUBLICATION, &param,
1242                                 sizeof(bluetooth_mesh_model_configure_t));
1243                 break;
1244         }
1245         case MESH_OPCODE_CONFIG_MODEL_SUB_STATUS: {
1246                 if (data_len != 7 && data_len != 9)
1247                         break;
1248                 bluetooth_mesh_model_configure_t param;
1249                 memset(&param, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1250
1251                 _bt_mesh_util_convert_hex_to_string(
1252                         (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1253                                 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1254
1255                 BT_INFO("\nNode %4.4x Subscription status %s\n",
1256                                 event->source, __mesh_status_to_string(data[0]));
1257
1258                 if (data[0] != MESH_STATUS_SUCCESS)
1259                         result = BLUETOOTH_ERROR_INTERNAL;
1260
1261                 if (!cmd) {
1262                         BT_INFO("Mesh: Command not found!!");
1263                         break;
1264                 }
1265
1266                 ele_addr = l_get_le16(data + 1);
1267                 addr = l_get_le16(data + 3);
1268                 BT_INFO("Element Addr\t%4.4x\n", ele_addr);
1269                 BT_INFO("Message Source Addr\t%4.4x\n", event->source);
1270
1271                 if (data_len == 9) {
1272                         /* vendor Model */
1273                         mod_id = l_get_le16(data + 5 + 2);
1274                         mod_id = l_get_le16(data) << 16 | mod_id;
1275                 } else {
1276                         /* BT SIG Model */
1277                         mod_id = l_get_le16(data + 5);
1278                         mod_id = MESH_VENDOR_ID_MASK | mod_id;
1279                 }
1280
1281                 param.primary_unicast = event->source;
1282                 param.elem_index = ele_addr - event->source;
1283                 /* Subscription address, unassigned address in case of Delete All command */
1284                 param.sub_addr = addr;
1285                 param.model = mod_id;
1286                 BT_INFO("Subscr Addr\t%4.4x\n", addr);
1287                 BT_INFO("Model ID\t%4.4x\n", mod_id);
1288
1289                 if (data[0] == MESH_STATUS_SUCCESS) {
1290                         /* Update cdb */
1291                         switch (cmd->opcode) {
1292                         case MESH_OPCODE_CONFIG_MODEL_SUB_ADD:
1293                         case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_ADD:
1294                                 if (!_bt_mesh_network_add_model_subscription(
1295                                         event->net_uuid.uuid, event->source,
1296                                         ele_addr - event->source, mod_id, addr)) {
1297                                         BT_INFO("Failed to add model subscription!");
1298                                 }
1299                         break;
1300                         case MESH_OPCODE_CONFIG_MODEL_SUB_DELETE:
1301                         case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_DELETE:
1302                                 if (!_bt_mesh_network_delete_model_subscription(
1303                                         event->net_uuid.uuid, event->source,
1304                                         ele_addr - event->source, mod_id, addr)) {
1305                                         BT_INFO("Failed to delete model subscription!");
1306                                 }
1307                         break;
1308                         case MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL:
1309                                 if (!_bt_mesh_network_delete_all_model_subscription(
1310                                         event->net_uuid.uuid, event->source,
1311                                         ele_addr - event->source, mod_id)) {
1312                                         BT_INFO("Failed to delete all model subscription!");
1313                                 }
1314                         break;
1315                         case MESH_OPCODE_CONFIG_MODEL_SUB_OVERWRITE:
1316                         case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_OVERWRITE:
1317                                 if (!_bt_mesh_network_overwrite_model_subscription(
1318                                         event->net_uuid.uuid, event->source,
1319                                         ele_addr - event->source, mod_id, addr)) {
1320                                         BT_INFO("Failed to overwrite model subscription!");
1321                                 }
1322                         break;
1323                         }
1324                 }
1325
1326                 if (cmd)
1327                         __mesh_handle_model_subscription_event(result, &param, cmd);
1328
1329                 break;
1330         }
1331         case MESH_OPCODE_CONFIG_DEFAULT_TTL_STATUS: {
1332                 if (data_len != 1)
1333                         break;
1334                 bluetooth_mesh_node_ttl_info_t param;
1335                 memset(&param, 0x00, sizeof(bluetooth_mesh_node_ttl_info_t));
1336
1337                 BT_INFO("Node %4.4x Default TTL %d", event->source, data[0]);
1338                  _bt_mesh_util_convert_hex_to_string(
1339                         (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1340                                 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1341                 param.unicast = event->source;
1342                 param.ttl = data[0];
1343
1344                 if (!_bt_mesh_network_save_remote_node_ttl(event->net_uuid.uuid,
1345                                 event->source,  data[0])) {
1346                         result = BLUETOOTH_ERROR_INTERNAL;
1347                         BT_INFO("Failed to save node TTL");
1348                 }
1349
1350                 /* Remote Node TTL event */
1351                 __bt_mesh_handle_pending_dev_config_request_info(result,
1352                         BT_MESH_NODE_TTL_EXECUTE, &param,
1353                                 sizeof(bluetooth_mesh_node_ttl_info_t));
1354                 break;
1355         }
1356         case MESH_OPCODE_MODEL_APP_STATUS: {
1357                 if (data_len != 7 && data_len != 9)
1358                         break;
1359                 bluetooth_mesh_model_configure_t param;
1360                 memset(&param, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1361
1362                  _bt_mesh_util_convert_hex_to_string(
1363                         (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1364                                 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1365                 BT_INFO("Node %4.4x: Model App status %s\n", event->source,
1366                         __mesh_status_to_string(data[0]));
1367
1368                 if (data[0] != MESH_STATUS_SUCCESS)
1369                         result = BLUETOOTH_ERROR_INTERNAL;
1370
1371                 elem_addr = l_get_le16(data + 1);
1372                 app_idx = l_get_le16(data + 3);
1373
1374                 BT_INFO("Element Addr\t%4.4x", elem_addr);
1375                 if (data_len == 9) {
1376                         /* vendor Model */
1377                         mod_id = l_get_le16(data + 5 + 2);
1378                         mod_id = l_get_le16(data) << 16 | mod_id;
1379                 } else {
1380                         /* BT SIG Model */
1381                         mod_id = l_get_le16(data + 5);
1382                         mod_id = MESH_VENDOR_ID_MASK | mod_id;
1383                 }
1384
1385                 param.primary_unicast = event->source;
1386                 param.elem_index = elem_addr - event->source;
1387                 param.appkey_idx = app_idx;
1388                 param.model = mod_id;
1389                 BT_INFO("AppIdx\t\t%u (0x%3.3x)\n ", app_idx, app_idx);
1390
1391                 /* Model Bind/UnBind Event */
1392                 __bt_mesh_handle_pending_dev_config_request_info(result,
1393                         BT_MESH_MODEL_CONFIGURE_APPKEY, &param,
1394                                 sizeof(bluetooth_mesh_model_configure_t));
1395                 break;
1396         }
1397         case MESH_OPCODE_MODEL_APP_LIST: {
1398                 if (data_len < 5)
1399                         break;
1400                 GArray *garray;
1401                 int total = 0;
1402                 bluetooth_mesh_model_configure_t param;
1403                 memset(&param, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1404
1405                 _bt_mesh_util_convert_hex_to_string(
1406                         (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1407                                 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1408
1409                 garray = g_array_new(FALSE, FALSE, sizeof(gchar));
1410
1411                 BT_INFO("\nNode %4.4x Model AppIdx status %s\n",
1412                                 event->source, __mesh_status_to_string(data[0]));
1413
1414                 BT_INFO("Element Addr\t%4.4x\n", l_get_le16(data + 1));
1415                 BT_INFO("Model ID\t%4.4x\n", l_get_le16(data + 3));
1416
1417                 elem_addr = l_get_le16(data + 1);
1418                 /* BT SIG Model */
1419                 mod_id = l_get_le16(data + 3);
1420                 mod_id = MESH_VENDOR_ID_MASK | mod_id;
1421
1422                 param.primary_unicast = event->source;
1423                 param.elem_index = elem_addr - event->source;
1424                 param.model = mod_id;
1425
1426                 data += 5;
1427                 data_len -= 5;
1428
1429                 while (data_len >= 3) {
1430                         app_idx = l_get_le16(data) & 0xfff;
1431                         g_array_append_vals(garray, &app_idx, sizeof(uint16_t));
1432                         BT_INFO("\t%u (0x%3.3x)\n", app_idx, app_idx);
1433
1434                         app_idx = l_get_le16(data + 1) >> 4;
1435                         g_array_append_vals(garray, &app_idx, sizeof(uint16_t));
1436                         BT_INFO("\t%u (0x%3.3x)\n", app_idx, app_idx);
1437                         data += 3;
1438                         data_len -= 3;
1439                 }
1440
1441                 if (data_len == 2) {
1442                         app_idx = l_get_le16(data) & 0xfff;
1443                         g_array_append_vals(garray, &app_idx, sizeof(uint16_t));
1444                         BT_INFO("\t %u (0x%3.3x)\n", app_idx, app_idx);
1445                 }
1446                 total = garray->len / sizeof(uint16_t);
1447                 param.appkey_list = (uint16_t **)g_malloc0(sizeof(uint16_t*) * garray->len);
1448                 param.appkeylist_count = total;
1449                 for (int i = 0; i < total; i++) {
1450                         param.appkey_list[i] = g_malloc(sizeof(uint16_t));
1451                         *param.appkey_list[i] = g_array_index(garray, uint16_t, i);
1452                 }
1453                 g_array_free(garray, TRUE);
1454
1455                 __bt_mesh_handle_pending_dev_config_request_info(result,
1456                         BT_MESH_MODEL_GET_APPKEY_LIST, &param,
1457                                 sizeof(bluetooth_mesh_model_configure_t));
1458                 break;
1459         }
1460         case MESH_OPCODE_CONFIG_MODEL_SUB_LIST: {
1461                 if (data_len < 5)
1462                         break;
1463                 int total = 0;
1464                 int i = 0;
1465                 bluetooth_mesh_model_configure_t param;
1466                 memset(&param, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1467
1468                 _bt_mesh_util_convert_hex_to_string((uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1469                                 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1470
1471                 BT_INFO("\nNode %4.4x BT SIG Model Subscription List status %s\n",
1472                                 event->source, __mesh_status_to_string(data[0]));
1473
1474                 BT_INFO("Element Addr\t%4.4x\n", l_get_le16(data + 1));
1475                 elem_addr = l_get_le16(data + 1);
1476
1477                 /* BT SIG Model */
1478                 mod_id = l_get_le16(data + 3);
1479                 mod_id = MESH_VENDOR_ID_MASK | mod_id;
1480                 BT_INFO("Model ID\t%4.4x\n", mod_id);
1481
1482                 param.primary_unicast = event->source;
1483                 param.elem_index = elem_addr - event->source;
1484                 param.model = mod_id;
1485                 BT_INFO("Mesh: Data length of All Models Together [%d]", data_len - 5);
1486                 total = (data_len -  5)/2;
1487                 param.sublist_count = total;
1488                 BT_INFO("Mesh: Total Number of subscriptions [%d]", total);
1489                 if (total) {
1490                         param.sub_list = (uint16_t **)g_malloc0(sizeof(uint16_t*) * total);
1491
1492                         i = 5;
1493                         for (int k = 0; i < data_len; k++, i += 2) {
1494                                 BT_INFO("Mesh: Subscription Addr \t\t%4.4x\n ", l_get_le16(data + i));
1495                                 param.sub_list[k] = g_malloc(sizeof(uint16_t));
1496                                 *param.sub_list[k] = l_get_le16(data + i);
1497                         }
1498                 }
1499
1500                 /* <TO-DO> Update model sub in cdb */
1501
1502                 __bt_mesh_handle_pending_dev_config_request_info(result,
1503                         BT_MESH_MODEL_GET_SUBSCRIPTION_LIST, &param,
1504                                 sizeof(bluetooth_mesh_model_configure_t));
1505                 break;
1506         }
1507         case MESH_OPCODE_CONFIG_VEND_MODEL_SUB_LIST: {
1508                 if (data_len < 7)
1509                         break;
1510                 int total = 0;
1511                 int i = 0;
1512                 bluetooth_mesh_model_configure_t param;
1513                 memset(&param, 0x00, sizeof(bluetooth_mesh_model_configure_t));
1514
1515                 _bt_mesh_util_convert_hex_to_string(
1516                         (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1517                                 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1518
1519                 BT_INFO("\nNode %4.4x Vendor Model Subscription List status %s\n",
1520                                 event->source, __mesh_status_to_string(data[0]));
1521
1522                 BT_INFO("Element Addr\t%4.4x\n", l_get_le16(data + 1));
1523                 elem_addr = l_get_le16(data + 1);
1524
1525                 /* VENDOR Model */
1526                 mod_id = l_get_le16(data + 5);
1527                 mod_id = l_get_le16(data + 3) << 16 | mod_id;
1528
1529                 BT_INFO("Model ID\t%4.4x\n", mod_id);
1530
1531                 param.primary_unicast = event->source;
1532                 param.elem_index = elem_addr - event->source;
1533                 param.model = mod_id;
1534
1535                 BT_INFO("Mesh: Data length of All Models Together [%d]", data_len - 7);
1536                 total = (data_len -  7)/2;
1537                 param.sublist_count = total;
1538
1539                 BT_INFO("Mesh: Total Number of subscriptions [%d]", total);
1540                 if (total) {
1541                         param.sub_list = (uint16_t **)g_malloc0(sizeof(uint16_t*) * total);
1542
1543                         i = 7;
1544                         for (int k = 0; i < data_len; k++, i += 2) {
1545                                 BT_INFO("Mesh: Subscription Addr \t\t%4.4x\n ", l_get_le16(data + i));
1546                                 param.sub_list[k] = g_malloc(sizeof(uint16_t));
1547                                 *param.sub_list[k] = l_get_le16(data + i);
1548                         }
1549                 }
1550                 __bt_mesh_handle_pending_dev_config_request_info(result,
1551                         BT_MESH_MODEL_GET_SUBSCRIPTION_LIST, &param,
1552                                 sizeof(bluetooth_mesh_model_configure_t));
1553                 break;
1554         }
1555         case MESH_OPCODE_DEV_COMP_STATUS: {
1556                 if (data_len < MESH_MINIMUM_COMPOSITION_LEN)
1557                         break;
1558                 bluetooth_mesh_node_features_t features;
1559                 memset(&features, 0x00, sizeof(bluetooth_mesh_node_features_t));
1560                 BT_INFO("Mesh: Got Response for Device Composition Data");
1561                 __bt_mesh_print_device_composition_data(data, data_len);
1562
1563                 if (!_bt_mesh_network_save_remote_node_composition(
1564                         event->net_uuid.uuid, event->source, data, data_len)) {
1565                         result = BLUETOOTH_ERROR_INTERNAL;
1566                         BT_INFO("Failed to save node composition!");
1567                 }
1568                 /* Browse Remote Node event */
1569                 __bt_mesh_handle_pending_dev_config_request_info(result,
1570                         BT_MESH_NODE_BROWSE, event, sizeof(event_mesh_devkey_message_t));
1571
1572                 /* Vendor Features Discover event */
1573                 _bt_mesh_util_convert_hex_to_string(
1574                         (uint8_t *) event->net_uuid.uuid, 16, features.net_uuid,
1575                                 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1576                 features.unicast = event->source;
1577                 BT_INFO("Mesh: Net UUID [%s]", features.net_uuid);
1578
1579                 BT_INFO("Mesh: Composition status for node unicast [0x%2.2x]",
1580                         event->source);
1581                 if (!_bt_mesh_node_get_vendor_features(event->net_uuid.uuid,
1582                         event->source, &features)) {
1583                         BT_ERR("Mesh: Failed to get Vendor Specific Infor for [0x%2.2x]",
1584                                 event->source);
1585                         result = BLUETOOTH_ERROR_INTERNAL;
1586                 } else {
1587                         BT_INFO("Mesh: Got Feature informations for [0x%2.2x]",
1588                                 event->source);
1589                 }
1590                 __bt_mesh_handle_pending_dev_config_request_info(result,
1591                         BT_MESH_NODE_GET_VENDOR_FEATURES, &features,
1592                                 sizeof(bluetooth_mesh_node_features_t));
1593                 break;
1594         }
1595         case MESH_OPCODE_NETKEY_STATUS: {
1596                 if (data_len != 3)
1597                         break;
1598
1599                 bluetooth_mesh_key_configure_t param;
1600                 memset(&param, 0x00, sizeof(bluetooth_mesh_key_configure_t));
1601
1602                 BT_INFO("Mesh: Node %4.4x NetKey status %s",
1603                         event->source,  __mesh_status_to_string(data[0]));
1604                 net_idx = l_get_le16(data + 1) & 0xfff;
1605
1606                 BT_INFO("\tNetKey %u (0x%3.3x)", net_idx, net_idx);
1607
1608                 if (data[0] != 0)
1609                         result = BLUETOOTH_ERROR_INTERNAL;
1610
1611                 if (!cmd)
1612                         break;
1613
1614                 _bt_mesh_util_convert_hex_to_string(
1615                         (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1616                                 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1617                 param.primary_unicast = event->source;
1618                 param.netkey_idx = net_idx;
1619                 param.is_netkey = true;
1620
1621                 BT_INFO("Mesh: Resp recvd from node unicast [0x%4.4x]", event->source);
1622                 if (cmd->opcode == MESH_OPCODE_NETKEY_ADD) {
1623                         BT_INFO("Mesh: Resp recvd: MESH_OPCODE_NETKEY_ADD");
1624                         if (result == BLUETOOTH_ERROR_NONE) {
1625                                 if (!_bt_mesh_network_save_remote_node_netkey(
1626                                                         event->net_uuid.uuid, event->source, net_idx)) {
1627                                         result = BLUETOOTH_ERROR_INTERNAL;
1628                                         BT_INFO("Failed to save node Netkey!");
1629                                 }
1630                         }
1631                         param.op = BLUETOOTH_MESH_NODE_KEY_ADD;
1632                 } else if (cmd->opcode == MESH_OPCODE_NETKEY_DELETE) {
1633                         BT_INFO("Mesh: Resp recvd: MESH_OPCODE_NETKEY_DELETE");
1634                         if (result == BLUETOOTH_ERROR_NONE) {
1635                                 if (!_bt_mesh_network_delete_remote_node_netkey(
1636                                                         event->net_uuid.uuid, event->source, net_idx)) {
1637                                         result = BLUETOOTH_ERROR_INTERNAL;
1638                                         BT_INFO("Failed to delete node Netkey!");
1639                                 }
1640                         }
1641                         param.op = BLUETOOTH_MESH_NODE_KEY_DELETE;
1642                 } else if (cmd->opcode == MESH_OPCODE_NETKEY_UPDATE) {
1643                         BT_INFO("Mesh: Resp recvd: MESH_OPCODE_NETKEY_UPDATE");
1644                         param.op = BLUETOOTH_MESH_NODE_KEY_UPDATE;
1645                 }
1646                 /* Node Net Key Configure event */
1647                 __bt_mesh_handle_pending_dev_config_request_info(result,
1648                         BT_MESH_NODE_CONFIGURE_KEY,
1649                                 &param, sizeof(bluetooth_mesh_key_configure_t));
1650                 break;
1651         }
1652         case MESH_OPCODE_APPKEY_STATUS: {
1653                 if (data_len != 4)
1654                         break;
1655
1656                 bluetooth_mesh_key_configure_t param;
1657                 memset(&param, 0x00, sizeof(bluetooth_mesh_key_configure_t));
1658
1659                 BT_INFO("Mesh: Node %4.4x AppKey status %s\n", event->source,
1660                                 __mesh_status_to_string(data[0]));
1661                 net_idx = l_get_le16(data + 1) & 0xfff;
1662                 app_idx = l_get_le16(data + 2) >> 4;
1663
1664                 BT_INFO("NetKey\t%u (0x%3.3x)\n", net_idx, net_idx);
1665                 BT_INFO("AppKey\t%u (0x%3.3x)\n", app_idx, app_idx);
1666
1667                 if (data[0] != MESH_STATUS_SUCCESS)
1668                         result = BLUETOOTH_ERROR_INTERNAL;
1669
1670                 if (!cmd)
1671                         break;
1672
1673                 _bt_mesh_util_convert_hex_to_string(
1674                         (uint8_t *) event->net_uuid.uuid, 16, param.net_uuid,
1675                                 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
1676
1677                 param.primary_unicast = event->source;
1678                 param.netkey_idx = net_idx;
1679                 param.appkey_idx = app_idx;
1680                 param.is_netkey = false;
1681
1682                 if (cmd->opcode == MESH_OPCODE_APPKEY_ADD) {
1683                         BT_INFO("Mesh: Resp recvd: MESH_OPCODE_APPKEY_ADD");
1684                         if (result == BLUETOOTH_ERROR_NONE) {
1685                                 if (!_bt_mesh_network_save_remote_node_appkey(
1686                                                 event->net_uuid.uuid, event->source,
1687                                                 net_idx, app_idx)) {
1688                                         result = BLUETOOTH_ERROR_INTERNAL;
1689                                         BT_INFO("Failed to save node Appkey!");
1690                                 }
1691                         }
1692                         param.op = BLUETOOTH_MESH_NODE_KEY_ADD;
1693                 } else if (cmd->opcode == MESH_OPCODE_APPKEY_DELETE) {
1694                         BT_INFO("Mesh: Resp recvd: MESH_OPCODE_APPKEY_DELETE");
1695                         if (result == BLUETOOTH_ERROR_NONE) {
1696                                 if (!_bt_mesh_network_delete_remote_node_appkey(
1697                                                 event->net_uuid.uuid, event->source,
1698                                                 net_idx, app_idx)) {
1699                                         result = BLUETOOTH_ERROR_INTERNAL;
1700                                         BT_INFO("Failed to delete node Appkey!");
1701                                 }
1702                         }
1703                         param.op = BLUETOOTH_MESH_NODE_KEY_DELETE;
1704                 } else if (cmd->opcode == MESH_OPCODE_APPKEY_UPDATE) {
1705                         BT_INFO("Mesh: Resp recvd: MESH_OPCODE_APPKEY_UPDATE");
1706                         param.op = BLUETOOTH_MESH_NODE_KEY_UPDATE;
1707                 }
1708
1709                 /* Node App Key Configure event */
1710                 __bt_mesh_handle_pending_dev_config_request_info(result,
1711                         BT_MESH_NODE_CONFIGURE_KEY,
1712                                 &param, sizeof(bluetooth_mesh_key_configure_t));
1713                 break;
1714         }
1715         }
1716 }
1717
1718 static gboolean __bt_mesh_vendor_feature_event_handler(gpointer data)
1719 {
1720         bluetooth_mesh_node_features_t *result = (bluetooth_mesh_node_features_t*) data;
1721         __bt_mesh_handle_pending_dev_config_request_info(
1722                 BLUETOOTH_ERROR_NONE,
1723                         BT_MESH_NODE_GET_VENDOR_FEATURES,
1724                                 result, sizeof(bluetooth_mesh_node_features_t));
1725         g_free(result);
1726         return FALSE;
1727 }
1728
1729 int _bt_mesh_node_discover_vendor_features(const char *app_cred, const char *sender,
1730                 bluetooth_mesh_node_features_t *req)
1731 {
1732         int ret = OAL_STATUS_SUCCESS;
1733         uint16_t dest;
1734         uint16_t netkey_idx;
1735         uint16_t data_len;
1736         oal_uuid_t net_uuid;
1737         uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1738
1739         BT_INFO("Mesh: Vendor features for Network UUID [%s]", req->net_uuid);
1740         _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1741                         strlen(req->net_uuid), net_uuid.uuid, 16);
1742         /* Check if Node's vendor features are already svaed or not */
1743         if (_bt_mesh_node_get_vendor_features(net_uuid.uuid, req->unicast, req)) {
1744                 BT_INFO("Mesh: Vendor Features already available for Node: Unicast [0x%.2x]",
1745                                 req->unicast);
1746                 /* Schedule event ot Application */
1747                 bluetooth_mesh_node_features_t *event = \
1748                         g_memdup(req, sizeof(bluetooth_mesh_node_features_t));
1749                 g_idle_add(__bt_mesh_vendor_feature_event_handler, (gpointer) event);
1750                 return BLUETOOTH_ERROR_NONE;
1751         }
1752
1753         /* If Scanning is going on */
1754         if (_bt_mesh_is_provisioning() ||
1755                         _bt_mesh_is_scanning()) {
1756                 BT_ERR("Device is buzy..");
1757                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1758         }
1759
1760         _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1761                         strlen(req->net_uuid), net_uuid.uuid, 16);
1762
1763         dest = req->unicast;
1764         BT_INFO("Mesh: Get Vendor Features for Remote Node Unicast [0x%2.2x]", dest);
1765
1766         /* Check pending request */
1767         if (_bt_mesh_check_pending_request(MESH_OPCODE_DEV_COMP_GET,
1768                                 dest, net_uuid.uuid)) {
1769                 BT_ERR("Device is buzy..");
1770                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1771         }
1772
1773         BT_INFO("Mesh: Browse Remote Node: Unicast [0x%2.2x]", dest);
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;
1778
1779         data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_DEV_COMP_GET,
1780                         buffer);
1781
1782         /* By default, use page 0 */
1783         buffer[data_len++] = 0;
1784         ret = mesh_conf_send_message(&net_uuid, dest, true,
1785                         netkey_idx, buffer, data_len);
1786
1787         if (ret != OAL_STATUS_SUCCESS) {
1788                 BT_ERR("ret: %d", ret);
1789                 return BLUETOOTH_ERROR_INTERNAL;
1790         }
1791
1792         /* Queue the request with timeout */
1793         __bt_mesh_add_request(MESH_OPCODE_DEV_COMP_GET,
1794                         dest, net_uuid.uuid, NULL);
1795         return BLUETOOTH_ERROR_NONE;
1796 }
1797
1798 int _bt_mesh_browse_remote_node(const char *app_cred,
1799         const char *sender,
1800                 bluetooth_mesh_node_discover_t *req)
1801 {
1802         int ret = OAL_STATUS_SUCCESS;
1803         uint16_t dest;
1804         uint16_t netkey_idx;
1805         uint16_t data_len;
1806         oal_uuid_t net_uuid;
1807         oal_uuid_t dev_uuid;
1808         uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1809
1810         /* If Scanning is going on */
1811         if (_bt_mesh_is_provisioning() ||
1812                         _bt_mesh_is_scanning()) {
1813                 BT_ERR("Device is buzy..");
1814                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1815         }
1816
1817         _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1818                 strlen(req->net_uuid), net_uuid.uuid, 16);
1819         _bt_mesh_util_convert_string_to_hex(req->dev_uuid,
1820                 strlen(req->dev_uuid), dev_uuid.uuid, 16);
1821
1822         BT_INFO("Mesh: Browse Node UUID [%s]", req->dev_uuid);
1823
1824         /* Get Remote Node unicast address from Dev UUID */
1825         if (!_bt_mesh_node_get_unicast_from_dev_uuid(net_uuid.uuid,
1826                         dev_uuid.uuid, &dest))
1827                 return BLUETOOTH_ERROR_INTERNAL;
1828
1829         BT_INFO("Mesh: Browse Remote Node: Unicast [0x%2.2x]", dest);
1830         /* Check pending request */
1831         if (_bt_mesh_check_pending_request(MESH_OPCODE_DEV_COMP_GET,
1832                         dest, net_uuid.uuid)) {
1833                 BT_ERR("Device is buzy..");
1834                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1835         }
1836
1837         /* Get Subnet index of the rmeote node for TX encryption */
1838         netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, dest);
1839         if (netkey_idx == MESH_NET_IDX_INVALID)
1840                 return BLUETOOTH_ERROR_INTERNAL;
1841
1842         data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_DEV_COMP_GET, buffer);
1843
1844         /* By default, use page 0 */
1845         buffer[data_len++] = 0;
1846         ret = mesh_conf_send_message(&net_uuid, dest, true,
1847                         netkey_idx, buffer, data_len);
1848
1849         if (ret != OAL_STATUS_SUCCESS) {
1850                 BT_ERR("ret: %d", ret);
1851                 return BLUETOOTH_ERROR_INTERNAL;
1852         }
1853
1854         /* Queue the request with timeout */
1855         __bt_mesh_add_request(MESH_OPCODE_DEV_COMP_GET,
1856                         dest, net_uuid.uuid, NULL);
1857         return BLUETOOTH_ERROR_NONE;
1858 }
1859
1860 int _bt_mesh_model_configure_group_subscription(const char *app_cred,
1861                 const char *sender, bluetooth_mesh_model_configure_t *req)
1862 {
1863         int ret = OAL_STATUS_SUCCESS;
1864         uint16_t netkey_idx;
1865         oal_uuid_t net_uuid;
1866         uint16_t data_len;
1867         uint32_t opcode = 0;
1868         uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1869
1870         /* If Scanning is going on */
1871         if (_bt_mesh_is_provisioning() ||
1872                         _bt_mesh_is_scanning()) {
1873                 BT_ERR("Device is buzy..");
1874                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1875         }
1876
1877         BT_INFO("Mesh: group Subscription Req: [%d]", req->op);
1878         if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1879                 BT_INFO("Mesh: group Subscription Addr [0x%2.2x]", req->sub_addr);
1880
1881                 /* Subscriptio address sanity check */
1882                 if (!MESH_IS_GROUP(req->sub_addr) ||
1883                                 MESH_IS_VIRTUAL(req->sub_addr)) {
1884                         BT_ERR("Mesh: Bad subscription address [0x%2.2x\n]",
1885                                 req->sub_addr);
1886                         return BLUETOOTH_ERROR_INVALID_PARAM;
1887                 }
1888         } else
1889                 BT_INFO("Mesh: group Subscription Req: Delete All");
1890
1891         _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1892                 strlen(req->net_uuid), net_uuid.uuid, 16);
1893         /* Get Subnet index of the remote node for TX encryption */
1894         netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid,
1895                                 req->primary_unicast);
1896         if (netkey_idx == MESH_NET_IDX_INVALID)
1897                 return BLUETOOTH_ERROR_INTERNAL;
1898
1899
1900         if (req->op == BLUETOOTH_MESH_MODEL_SUB_ADD)
1901                 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_ADD;
1902         else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE)
1903                 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE;
1904         else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL)
1905                 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL;
1906         else if (req->op == BLUETOOTH_MESH_MODEL_SUB_OVERWRITE)
1907                 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_OVERWRITE;
1908
1909         /* Check pending request */
1910         if (_bt_mesh_check_pending_request(opcode,
1911                                 req->primary_unicast, net_uuid.uuid)) {
1912                 BT_ERR("Device is buzy..");
1913                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1914         }
1915
1916         data_len = _bt_mesh_util_opcode_set(opcode, buffer);
1917
1918         BT_INFO("Mesh: Group Subscription Primary unicast [0x%2.2x]",
1919                         req->primary_unicast);
1920         /* Element Address */
1921         l_put_le16((req->primary_unicast + req->elem_index),
1922                         buffer + data_len);
1923         data_len += 2;
1924
1925         /* Subscription address */
1926         if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1927                 l_put_le16(req->sub_addr, buffer + data_len);
1928                 data_len += 2;
1929         }
1930
1931         BT_INFO("Mesh: Group Subscription Model ID [0x%4.4x]", req->model);
1932         /* Insert Model ID */
1933          if (req->model >= 0xFFFF0000) {
1934                 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
1935                 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1936                 data_len += 2;
1937         } else {
1938                 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
1939                 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
1940                 data_len += 2;
1941                 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
1942                 data_len += 2;
1943         }
1944
1945         ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true,
1946                         netkey_idx, buffer, data_len);
1947
1948         if (ret != OAL_STATUS_SUCCESS) {
1949                 BT_ERR("ret: %d", ret);
1950                 return BLUETOOTH_ERROR_INTERNAL;
1951         }
1952
1953         BT_INFO("Mesh: Group Subscription Command sent successfully");
1954         /* Queue the request with timeout */
1955         __bt_mesh_add_request(opcode, req->primary_unicast,
1956                 net_uuid.uuid,
1957                         g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
1958
1959         return BLUETOOTH_ERROR_NONE;
1960 }
1961
1962 int _bt_mesh_model_configure_virtual_group_subscription(
1963                 const char *app_cred, const char *sender,
1964                         bluetooth_mesh_model_configure_t *req)
1965 {
1966         int ret = OAL_STATUS_SUCCESS;
1967         uint16_t netkey_idx;
1968         oal_uuid_t net_uuid;
1969         uint16_t data_len;
1970         uint32_t opcode = 0;
1971         uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
1972
1973         /* If Scanning is going on */
1974         if (_bt_mesh_is_provisioning() ||
1975                         _bt_mesh_is_scanning()) {
1976                 BT_ERR("Device is buzy..");
1977                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1978         }
1979
1980         _bt_mesh_util_convert_string_to_hex(req->net_uuid,
1981                 strlen(req->net_uuid), net_uuid.uuid, 16);
1982
1983         BT_INFO("Mesh: Virtual Group Subscription Req: [%d] Group Addr [0x%2.2x]",
1984                         req->op, req->sub_addr);
1985
1986         if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
1987                 /* Subscription address sanity check */
1988                 if (MESH_IS_GROUP(req->sub_addr) ||
1989                                 !(MESH_IS_VIRTUAL(req->sub_addr))) {
1990                         BT_ERR("Mesh: Bad Virtual Group subscription address [0x%2.2x\n]",
1991                                 req->sub_addr);
1992                         return BLUETOOTH_ERROR_INVALID_PARAM;
1993                 }
1994         } else
1995                 BT_INFO("Mesh: Virtual Group Subscription Req: Delete All");
1996
1997         /* Get Subnet index of the remote node for TX encryption */
1998         netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid,
1999                                 req->primary_unicast);
2000         if (netkey_idx == MESH_NET_IDX_INVALID)
2001                 return BLUETOOTH_ERROR_INTERNAL;
2002
2003         if (req->op == BLUETOOTH_MESH_MODEL_SUB_ADD)
2004                 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_ADD;
2005         else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE)
2006                 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_DELETE;
2007         else if (req->op == BLUETOOTH_MESH_MODEL_SUB_OVERWRITE)
2008                 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_OVERWRITE;
2009         else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL)
2010                 opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL;
2011
2012         /* Check pending request */
2013         if (_bt_mesh_check_pending_request(opcode,
2014                                 req->primary_unicast, net_uuid.uuid)) {
2015                 BT_ERR("Device is buzy..");
2016                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2017         }
2018
2019         data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2020
2021         /* Element Address */
2022         l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2023         data_len += 2;
2024
2025         /* Subscription address */
2026         if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
2027                 uint8_t label_uuid[16];
2028                 if (!_bt_mesh_network_get_label_uuid_from_sub_addr(
2029                         net_uuid.uuid, req->sub_addr, label_uuid)) {
2030                         BT_ERR("Mesh: Virtual Group Label UUID Not found");
2031                         return BLUETOOTH_ERROR_INVALID_PARAM;
2032                 }
2033                 memcpy(buffer + data_len, label_uuid, 16);
2034                 data_len += 16;
2035         }
2036
2037         BT_INFO("Mesh: Virtual Group Subscription Model ID [0x%4.4x]", req->model);
2038         /* Insert Model ID */
2039          if (req->model >= 0xFFFF0000) {
2040                 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2041                 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2042                 data_len += 2;
2043         } else {
2044                 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2045                 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2046                 data_len += 2;
2047                 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2048                 data_len += 2;
2049         }
2050
2051         ret = mesh_conf_send_message(&net_uuid,
2052                         req->primary_unicast, true,
2053                                 netkey_idx, buffer, data_len);
2054
2055         if (ret != OAL_STATUS_SUCCESS) {
2056                 BT_ERR("ret: %d", ret);
2057                 return BLUETOOTH_ERROR_INTERNAL;
2058         }
2059
2060         BT_INFO("Mesh: Virtual Group Subscription Command sent successfully");
2061         /* Queue the request with timeout */
2062         __bt_mesh_add_request(opcode, req->primary_unicast,
2063                 net_uuid.uuid,
2064                         g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2065
2066         return BLUETOOTH_ERROR_NONE;
2067 }
2068
2069 int _bt_mesh_model_get_publication(const char *app_cred, const char *sender,
2070                 bluetooth_mesh_model_configure_t *req)
2071 {
2072         int ret = OAL_STATUS_SUCCESS;
2073         uint16_t netkey_idx;
2074         oal_uuid_t net_uuid;
2075         uint16_t data_len;
2076         uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2077
2078         /* If Scanning is going on */
2079         if (_bt_mesh_is_provisioning() ||
2080                         _bt_mesh_is_scanning()) {
2081                 BT_ERR("Device is buzy..");
2082                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2083         }
2084
2085         _bt_mesh_util_convert_string_to_hex(req->net_uuid,
2086                 strlen(req->net_uuid), net_uuid.uuid, 16);
2087
2088         /* Check pending request */
2089         if (_bt_mesh_check_pending_request(MESH_OPCODE_CONFIG_MODEL_PUB_GET,
2090                                 req->primary_unicast, net_uuid.uuid)) {
2091                 BT_ERR("Device is buzy..");
2092                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2093         }
2094
2095         /* Get Subnet index of the remote node for TX encryption */
2096         netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2097         if (netkey_idx == MESH_NET_IDX_INVALID)
2098                 return BLUETOOTH_ERROR_INTERNAL;
2099
2100         /* Set Opcode */
2101         data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_CONFIG_MODEL_PUB_GET, buffer);
2102
2103         /* Element Address */
2104         l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2105         data_len += 2;
2106
2107         /* Insert Model ID */
2108         if (req->model >= 0xFFFF0000) {
2109                 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2110                 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2111                 data_len += 2;
2112         } else {
2113                 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2114                 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2115                 data_len += 2;
2116                 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2117                 data_len += 2;
2118         }
2119
2120         ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2121
2122         if (ret != OAL_STATUS_SUCCESS) {
2123                 BT_ERR("ret: %d", ret);
2124                 return BLUETOOTH_ERROR_INTERNAL;
2125         }
2126
2127         /* Queue the request with timeout */
2128         __bt_mesh_add_request(MESH_OPCODE_CONFIG_MODEL_PUB_GET, req->primary_unicast, net_uuid.uuid,
2129                         g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2130
2131         return BLUETOOTH_ERROR_NONE;
2132 }
2133
2134 int _bt_mesh_model_set_publication(const char *app_cred, const char *sender,
2135                 bluetooth_mesh_model_configure_t *req)
2136 {
2137         int ret = OAL_STATUS_SUCCESS;
2138         uint16_t netkey_idx;
2139         oal_uuid_t net_uuid;
2140         uint16_t data_len;
2141         uint32_t opcode = 0;
2142         uint8_t label_uuid[16];
2143         uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2144
2145         /* If Scanning is going on */
2146         if (_bt_mesh_is_provisioning() ||
2147                         _bt_mesh_is_scanning()) {
2148                 BT_ERR("Device is buzy..");
2149                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2150         }
2151
2152         _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2153
2154         if (req->pub_addr == MESH_ALL_NODES_ADDRESS)
2155                 BT_INFO("Mesh: Setting Publication to ALL Node Address");
2156
2157         if (!MESH_IS_GROUP(req->pub_addr) && !MESH_IS_VIRTUAL(req->pub_addr) &&
2158                         req->pub_addr != MESH_UNASSIGNED_ADDRESS) {
2159                 BT_ERR("Mesh: Bad Publication address %x\n", req->pub_addr);
2160                 return BLUETOOTH_ERROR_INVALID_PARAM;
2161         }
2162
2163         if (req->pub_addr != MESH_UNASSIGNED_ADDRESS) {
2164                 if (MESH_IS_VIRTUAL(req->pub_addr)) {
2165                         if (!_bt_mesh_network_get_label_uuid_from_sub_addr(
2166                                         net_uuid.uuid, req->pub_addr, label_uuid))
2167                                 return BLUETOOTH_ERROR_INVALID_PARAM;
2168                         opcode = MESH_OPCODE_CONFIG_MODEL_PUB_VIRT_SET;
2169
2170                 } else if (MESH_IS_GROUP(req->pub_addr))
2171                         opcode = MESH_OPCODE_CONFIG_MODEL_PUB_SET;
2172         }
2173
2174         /* Check pending request */
2175         if (_bt_mesh_check_pending_request(opcode,
2176                                 req->primary_unicast, net_uuid.uuid)) {
2177                 BT_ERR("Device is buzy..");
2178                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2179         }
2180
2181         /* Get Subnet index of the remote node for TX encryption */
2182         netkey_idx = _bt_mesh_node_get_subnet_idx(
2183                                 net_uuid.uuid, req->primary_unicast);
2184         if (netkey_idx == MESH_NET_IDX_INVALID)
2185                 return BLUETOOTH_ERROR_INTERNAL;
2186
2187         /* Set opcode */
2188         data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2189
2190         /* Element Address */
2191         l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2192         data_len += 2;
2193
2194         /* Fill Publication Address */
2195         if (MESH_IS_VIRTUAL(req->pub_addr)) {
2196                 memcpy(buffer + data_len, label_uuid, 16);
2197                 data_len += 16;
2198         } else {
2199                 l_put_le16(req->pub_addr, buffer + data_len);
2200                 data_len += 2;
2201         }
2202
2203         /* AppKey index + credential (set to 0) */
2204         l_put_le16(req->appkey_idx, buffer + data_len);
2205         data_len += 2;
2206
2207         /* Fill TTL */
2208         buffer[data_len++] = req->ttl;
2209         /* Publish period  step count and step resolution */
2210         buffer[data_len++] = req->period;
2211         /* Publish retransmit count & interval steps */
2212         buffer[data_len++] = req->retransmit;
2213
2214         /* Insert Model ID */
2215         if (req->model >= 0xFFFF0000) {
2216                 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2217                 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2218                 data_len += 2;
2219         } else {
2220                 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2221                 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2222                 data_len += 2;
2223                 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2224                 data_len += 2;
2225         }
2226
2227         ret = mesh_conf_send_message(&net_uuid,
2228                         req->primary_unicast, true,
2229                                 netkey_idx, buffer, data_len);
2230
2231         if (ret != OAL_STATUS_SUCCESS) {
2232                 BT_ERR("ret: %d", ret);
2233                 return BLUETOOTH_ERROR_INTERNAL;
2234         }
2235
2236         /* Queue the request with timeout */
2237         __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2238                         g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2239
2240         return BLUETOOTH_ERROR_NONE;
2241 }
2242
2243 int _bt_mesh_node_model_get_subscription_list(const char *app_cred, const char *sender,
2244                 bluetooth_mesh_model_configure_t *req)
2245 {
2246         int ret = OAL_STATUS_SUCCESS;
2247         uint16_t netkey_idx;
2248         oal_uuid_t net_uuid;
2249         uint16_t data_len;
2250         uint32_t opcode = 0;
2251         uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2252
2253         /* If Scanning is going on */
2254         if (_bt_mesh_is_provisioning() ||
2255                         _bt_mesh_is_scanning()) {
2256                 BT_ERR("Device is buzy..");
2257                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2258         }
2259
2260         _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2261
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_CONFIG_MODEL_SUB_GET;
2266         else
2267                 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2268                 opcode = MESH_OPCODE_CONFIG_VEND_MODEL_SUB_GET;
2269
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;
2275         }
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;
2280
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);
2284         data_len += 2;
2285
2286         /* Insert Model ID */
2287         if (opcode == MESH_OPCODE_CONFIG_MODEL_SUB_GET) {
2288                 BT_INFO("Mesh: Get Subscription List for BT SIG Model");
2289                 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2290                 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2291                 data_len += 2;
2292         } else {
2293                 BT_INFO("Mesh: Get Subscription List for Vendor Model");
2294                 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2295                 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2296                 data_len += 2;
2297                 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2298                 data_len += 2;
2299         }
2300
2301         ret = mesh_conf_send_message(&net_uuid,
2302                 req->primary_unicast, true, netkey_idx, buffer, data_len);
2303
2304         if (ret != OAL_STATUS_SUCCESS) {
2305                 BT_ERR("ret: %d", ret);
2306                 return BLUETOOTH_ERROR_INTERNAL;
2307         }
2308
2309         /* Queue the request with timeout */
2310         __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2311                         g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2312
2313         return BLUETOOTH_ERROR_NONE;
2314 }
2315
2316 int _bt_mesh_node_model_get_appkey_list(const char *app_cred, const char *sender,
2317                 bluetooth_mesh_model_configure_t *req)
2318 {
2319         int ret = OAL_STATUS_SUCCESS;
2320         uint16_t netkey_idx;
2321         oal_uuid_t net_uuid;
2322         uint16_t data_len;
2323         uint32_t opcode = 0;
2324         uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2325
2326         /* If Scanning is going on */
2327         if (_bt_mesh_is_provisioning() ||
2328                         _bt_mesh_is_scanning()) {
2329                 BT_ERR("Device is buzy..");
2330                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2331         }
2332
2333         _bt_mesh_util_convert_string_to_hex(req->net_uuid,
2334                 strlen(req->net_uuid), net_uuid.uuid, 16);
2335
2336         /* Insert Model ID */
2337         if (req->model >= 0xFFFF0000)
2338                 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2339                 opcode = MESH_OPCODE_MODEL_APP_GET;
2340         else
2341                 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2342                 opcode = MESH_OPCODE_VENDOR_MODEL_APP_GET;
2343
2344         /* Check pending request */
2345         if (_bt_mesh_check_pending_request(opcode,
2346                                 req->primary_unicast, net_uuid.uuid)) {
2347                 BT_ERR("Device is buzy..");
2348                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2349         }
2350         /* Get Subnet index of the remote node for TX encryption */
2351         netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2352         if (netkey_idx == MESH_NET_IDX_INVALID)
2353                 return BLUETOOTH_ERROR_INTERNAL;
2354
2355         data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2356         /* Element Address */
2357         l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2358         data_len += 2;
2359
2360
2361         /* Insert Model ID */
2362         if (opcode == MESH_OPCODE_MODEL_APP_GET) {
2363                 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2364                 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2365                 data_len += 2;
2366         } else {
2367                 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2368                 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2369                 data_len += 2;
2370                 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2371                 data_len += 2;
2372         }
2373
2374         ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2375
2376         if (ret != OAL_STATUS_SUCCESS) {
2377                 BT_ERR("ret: %d", ret);
2378                 return BLUETOOTH_ERROR_INTERNAL;
2379         }
2380
2381         /* Queue the request with timeout */
2382         __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2383                         g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2384
2385         return BLUETOOTH_ERROR_NONE;
2386 }
2387
2388 int _bt_mesh_network_reset_node(const char *app_cred, const char *sender,
2389                 bluetooth_mesh_node_info_t *node)
2390 {
2391         int ret = OAL_STATUS_SUCCESS;
2392         uint16_t netkey_idx;
2393         oal_uuid_t net_uuid;
2394         uint16_t data_len;
2395         uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2396
2397         /* If Scanning is going on */
2398         if (_bt_mesh_is_provisioning() ||
2399                         _bt_mesh_is_scanning()) {
2400                 BT_ERR("Device is buzy..");
2401                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2402         }
2403
2404         _bt_mesh_util_convert_string_to_hex(node->net_uuid,
2405                         strlen(node->net_uuid), net_uuid.uuid, 16);
2406
2407         /* Check pending request */
2408         if (_bt_mesh_check_pending_request(MESH_OPCODE_NODE_RESET,
2409                                 node->primary_unicast, net_uuid.uuid)) {
2410                 BT_ERR("Device is buzy..");
2411                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2412         }
2413         BT_INFO("Mesh: Reset a remote Node [0x%2.2x] Elem Cnt [0x%2.2x]",
2414                         node->primary_unicast, node->num_elements);
2415         /* Get Subnet index of the remote node for TX encryption */
2416         netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, node->primary_unicast);
2417         if (netkey_idx == MESH_NET_IDX_INVALID)
2418                 return BLUETOOTH_ERROR_INTERNAL;
2419
2420         data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_NODE_RESET, buffer);
2421
2422         /* Send Node Reset command 5 times to ensure reliability */
2423         for (int i = 0; i < 5; i++)
2424                 ret = mesh_conf_send_message(&net_uuid, node->primary_unicast,
2425                                 true, netkey_idx, buffer, data_len);
2426
2427         if (ret != OAL_STATUS_SUCCESS) {
2428                 BT_ERR("Mesh: Node Reset Failed!: %d", ret);
2429                 return BLUETOOTH_ERROR_INTERNAL;
2430         }
2431
2432         BT_INFO("Mesh: Node reset Done, Send Delete req: Unicast[0x%2.2x]",
2433                         node->primary_unicast);
2434
2435         ret = mesh_delete_remote_node(&net_uuid, node->primary_unicast,
2436                         node->num_elements);
2437         if (ret != OAL_STATUS_SUCCESS) {
2438                 BT_ERR("Mesh: Remote Node Deletion Failed!: %d", ret);
2439                 return BLUETOOTH_ERROR_INTERNAL;
2440         }
2441
2442         return _bt_mesh_network_remove_node_configuration(node);
2443 }
2444
2445 int _bt_mesh_node_model_appkey_execute(const char *app_cred, const char *sender,
2446                 bluetooth_mesh_model_configure_t *req)
2447 {
2448         int ret = OAL_STATUS_SUCCESS;
2449         uint16_t netkey_idx;
2450         oal_uuid_t net_uuid;
2451         uint16_t data_len;
2452         uint32_t opcode = 0;
2453         uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2454
2455         /* If Scanning is going on */
2456         if (_bt_mesh_is_provisioning() ||
2457                         _bt_mesh_is_scanning()) {
2458                 BT_ERR("Device is buzy..");
2459                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2460         }
2461
2462         _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2463
2464         /* Check pending request */
2465         if (_bt_mesh_check_pending_request(req->is_bind ? MESH_OPCODE_MODEL_APP_BIND : MESH_OPCODE_MODEL_APP_UNBIND,
2466                                 req->primary_unicast, net_uuid.uuid)) {
2467                 BT_ERR("Device is buzy..");
2468                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2469         }
2470         /* Get Subnet index of the remote node for TX encryption */
2471         netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2472         if (netkey_idx == MESH_NET_IDX_INVALID)
2473                 return BLUETOOTH_ERROR_INTERNAL;
2474
2475         if (req->is_bind)
2476                 opcode = MESH_OPCODE_MODEL_APP_BIND;
2477         else
2478                 opcode = MESH_OPCODE_MODEL_APP_UNBIND;
2479
2480         data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2481         /* Element Address */
2482         l_put_le16((req->primary_unicast + req->elem_index), buffer + data_len);
2483         data_len += 2;
2484
2485         /* AppKey Index  */
2486         l_put_le16(req->appkey_idx, buffer + data_len);
2487         data_len += 2;
2488
2489         /* Insert Model ID */
2490         if (req->model >= 0xFFFF0000) {
2491                 /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
2492                 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2493                 data_len += 2;
2494         } else {
2495                 /* Vendor Model, 1st 2 octetes: Company ID, next 2 octets: Vendor Model ID */
2496                 l_put_le16(req->model & 0xFFFF0000, buffer + data_len);
2497                 data_len += 2;
2498                 l_put_le16(req->model & 0x0000FFFF, buffer + data_len);
2499                 data_len += 2;
2500         }
2501
2502         ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2503
2504         if (ret != OAL_STATUS_SUCCESS) {
2505                 BT_ERR("ret: %d", ret);
2506                 return BLUETOOTH_ERROR_INTERNAL;
2507         }
2508
2509         /* Queue the request with timeout */
2510         __bt_mesh_add_request(opcode, req->primary_unicast, net_uuid.uuid,
2511                         g_memdup(req, sizeof(bluetooth_mesh_model_configure_t)));
2512
2513         return BLUETOOTH_ERROR_NONE;
2514 }
2515
2516 int _bt_mesh_ttl_execute_remote_node(const char *app_cred, const char *sender,
2517                 bluetooth_mesh_node_ttl_info_t *req)
2518 {
2519         int ret = OAL_STATUS_SUCCESS;
2520         uint16_t netkey_idx;
2521         uint16_t data_len;
2522         oal_uuid_t net_uuid;
2523         uint32_t opcode = 0;
2524         uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2525
2526         /* If Scanning is going on */
2527         if (_bt_mesh_is_provisioning() ||
2528                         _bt_mesh_is_scanning()) {
2529                 BT_ERR("Device is buzy..");
2530                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2531         }
2532
2533         _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2534
2535         /* Check pending request */
2536         if (_bt_mesh_check_pending_request(req->is_set ? MESH_OPCODE_CONFIG_DEFAULT_TTL_SET : MESH_OPCODE_CONFIG_DEFAULT_TTL_GET,
2537                                 req->unicast, net_uuid.uuid)) {
2538                 BT_ERR("Device is buzy..");
2539                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2540         }
2541
2542         /* Get Subnet index of the rmeote node for TX encryption */
2543         netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->unicast);
2544         if (netkey_idx == MESH_NET_IDX_INVALID)
2545                 return BLUETOOTH_ERROR_INTERNAL;
2546
2547         if (req->is_set) {
2548                 opcode = MESH_OPCODE_CONFIG_DEFAULT_TTL_SET;
2549                 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_CONFIG_DEFAULT_TTL_SET, buffer);
2550                 buffer[data_len++] = req->ttl;
2551         } else {
2552                 opcode = MESH_OPCODE_CONFIG_DEFAULT_TTL_GET;
2553                 data_len = _bt_mesh_util_opcode_set(MESH_OPCODE_CONFIG_DEFAULT_TTL_GET, buffer);
2554         }
2555
2556         ret = mesh_conf_send_message(&net_uuid, req->unicast, true, netkey_idx, buffer, data_len);
2557
2558         if (ret != OAL_STATUS_SUCCESS) {
2559                 BT_ERR("ret: %d", ret);
2560                 return BLUETOOTH_ERROR_INTERNAL;
2561         }
2562
2563         /* Queue the request with timeout */
2564         __bt_mesh_add_request(opcode, req->unicast, net_uuid.uuid,
2565                         g_memdup(req, sizeof(bluetooth_mesh_node_ttl_info_t)));
2566         return BLUETOOTH_ERROR_NONE;
2567 }
2568
2569 static bool __bt_mesh_check_pending_key_cmd(uint16_t dest, bool is_netkey,
2570                 bluetooth_mesh_node_key_conf_e op,  uint8_t net_uuid[], uint32_t *opcode)
2571 {
2572         const struct mesh_config_cmd *cmd;
2573
2574         if (is_netkey) {
2575                 switch (op) {
2576                 case BLUETOOTH_MESH_NODE_KEY_ADD:
2577                         cmd = __mesh_get_command(MESH_OPCODE_NETKEY_ADD);
2578                         if (!cmd)
2579                                 return false;
2580                         if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2581                                 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2582                                 return true;
2583                         }
2584                         *opcode = MESH_OPCODE_NETKEY_ADD;
2585                         return false;
2586                 case BLUETOOTH_MESH_NODE_KEY_DELETE:
2587                         cmd = __mesh_get_command(MESH_OPCODE_NETKEY_DELETE);
2588                         if (!cmd)
2589                                 return false;
2590                         if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2591                                 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2592                                 return true;
2593                         }
2594                         *opcode = MESH_OPCODE_NETKEY_DELETE;
2595                         return false;
2596                 case BLUETOOTH_MESH_NODE_KEY_UPDATE:
2597                         cmd = __mesh_get_command(MESH_OPCODE_NETKEY_UPDATE);
2598                         if (!cmd)
2599                                 return false;
2600                         if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2601                                 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2602                                 return true;
2603                         }
2604                         *opcode = MESH_OPCODE_NETKEY_UPDATE;
2605                         return false;
2606                 }
2607         } else {
2608                 switch (op) {
2609                 case BLUETOOTH_MESH_NODE_KEY_ADD:
2610                         cmd = __mesh_get_command(MESH_OPCODE_APPKEY_ADD);
2611                         if (!cmd)
2612                                 return false;
2613                         if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2614                                 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2615                                 return true;
2616                         }
2617                         *opcode = MESH_OPCODE_APPKEY_ADD;
2618                         return false;
2619                 case BLUETOOTH_MESH_NODE_KEY_DELETE:
2620                         cmd = __mesh_get_command(MESH_OPCODE_APPKEY_DELETE);
2621                         if (!cmd)
2622                                 return false;
2623                         if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2624                                 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2625                                 return true;
2626                         }
2627                         *opcode = MESH_OPCODE_APPKEY_DELETE;
2628                         return false;
2629                 case BLUETOOTH_MESH_NODE_KEY_UPDATE:
2630                         cmd = __mesh_get_command(MESH_OPCODE_APPKEY_UPDATE);
2631                         if (!cmd)
2632                                 return false;
2633                         if (__bt_mesh_get_request_by_response(dest, net_uuid, cmd->response)) {
2634                                 BT_ERR("Mesh:Another Key Configuration command is pending\n");
2635                                 return true;
2636                         }
2637                         *opcode = MESH_OPCODE_APPKEY_UPDATE;
2638                         return false;
2639                 }
2640         }
2641         return false;
2642 }
2643
2644 int _bt_mesh_node_configure_key(const char *app_cred, const char *sender,
2645                 bluetooth_mesh_key_configure_t *req)
2646 {
2647         int ret = OAL_STATUS_SUCCESS;
2648         uint16_t netkey_idx;
2649         uint16_t bound_netkey_idx = 0x0000;
2650         oal_uuid_t net_uuid;
2651         uint32_t opcode = 0;
2652         bool update;
2653         uint16_t data_len;
2654         uint8_t buffer[MESH_CONFIG_BUFFER_MAX_LEN];
2655
2656         /* If Scanning is going on */
2657         if (_bt_mesh_is_provisioning() ||
2658                         _bt_mesh_is_scanning()) {
2659                 BT_ERR("Device is buzy..");
2660                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2661         }
2662
2663         _bt_mesh_util_convert_string_to_hex(req->net_uuid, strlen(req->net_uuid), net_uuid.uuid, 16);
2664
2665         if (req->is_netkey && !_bt_mesh_keys_subnet_exists(net_uuid.uuid, req->netkey_idx)) {
2666                 BT_ERR("Local Subnet not found..");
2667                 return BLUETOOTH_ERROR_INVALID_PARAM;
2668         }
2669
2670         /* For Appkey Configuration, check for available bound netkey */
2671         if (!req->is_netkey) {
2672                 bound_netkey_idx = _bt_mesh_keys_get_bound_key(net_uuid.uuid, req->appkey_idx);
2673                 if (bound_netkey_idx == MESH_NET_IDX_INVALID) {
2674                         BT_ERR("Local AppKey not found..");
2675                         return BLUETOOTH_ERROR_INVALID_PARAM;
2676                 }
2677         }
2678
2679         /* Check pending request */
2680         if (__bt_mesh_check_pending_key_cmd(req->primary_unicast,
2681                                 req->is_netkey, req->op, net_uuid.uuid, &opcode)) {
2682                 BT_ERR("Device is buzy..");
2683                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2684         }
2685
2686         /* Get Subnet index of the rmeote node for TX encryption */
2687         netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid, req->primary_unicast);
2688         if (netkey_idx == MESH_NET_IDX_INVALID)
2689                 return BLUETOOTH_ERROR_INTERNAL;
2690
2691         /* Handle Key (App/Net) Delete Commands: Configuration Message  */
2692         if (opcode == MESH_OPCODE_NETKEY_DELETE || opcode == MESH_OPCODE_APPKEY_DELETE) {
2693                 data_len = _bt_mesh_util_opcode_set(opcode, buffer);
2694                 if (req->is_netkey) {
2695                         l_put_le16(req->netkey_idx, buffer + data_len);
2696                         data_len += 2;
2697                 } else {
2698                         buffer[data_len] = bound_netkey_idx;
2699                         buffer[data_len + 1] = ((bound_netkey_idx >> 8) & 0xf) | ((req->appkey_idx << 4) & 0xf0);
2700                         buffer[data_len + 2] = req->appkey_idx >> 4;
2701
2702                         data_len += 3;
2703                 }
2704                 ret = mesh_conf_send_message(&net_uuid, req->primary_unicast, true, netkey_idx, buffer, data_len);
2705         } else {
2706                 /* Handle Key (App/Net) Update & Add Commands: Key Config message  */
2707                 update = (opcode == MESH_OPCODE_NETKEY_UPDATE || opcode == MESH_OPCODE_APPKEY_UPDATE);
2708                 ret = mesh_conf_send_key_message(&net_uuid, req->primary_unicast, req->is_netkey,
2709                                 update, req->is_netkey ? req->netkey_idx : req->appkey_idx, netkey_idx);
2710         }
2711         if (ret != OAL_STATUS_SUCCESS) {
2712                 BT_ERR("ret: %d", ret);
2713                 return BLUETOOTH_ERROR_INTERNAL;
2714         }
2715
2716         /* Queue the request with timeout */
2717         __bt_mesh_add_request(opcode, req->primary_unicast,  net_uuid.uuid,
2718                 g_memdup(req, sizeof(bluetooth_mesh_key_configure_t)));
2719         return BLUETOOTH_ERROR_NONE;
2720 }