Fix coverty issues
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-mesh-dbus-handler.c
1 /*
2  * Bluetooth HAL
3  *
4  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
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
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <string.h>
26
27 #include <glib.h>
28 #include <gio/gio.h>
29 #include <dlog.h>
30 #include <vconf.h>
31 #include <ell/ell.h>
32
33 #include "bt-hal-mesh-dbus-handler.h"
34 #include "bt-hal-dbus-common-utils.h"
35 #include "bt-hal-internal.h"
36
37 #include <hardware/bt_mesh.h>
38
39 #define BT_HAL_MESH_DBUS_NAME "org.projectx.bt.mesh"
40
41 #define BT_HAL_UUID_LEN 16
42 #define BT_HAL_BLUEZ_MESH_NAME "org.bluez.mesh"
43
44 #define BT_HAL_MESH_NETWORK_INTERFACE "org.bluez.mesh.Network1"
45 #define BT_HAL_MESH_NODE_INTERFACE "org.bluez.mesh.Node1"
46 #define BT_HAL_MESH_MANAGEMENT_INTERFACE "org.bluez.mesh.Management1"
47 #define BT_HAL_MESH_ELEMENT_INTERFACE "org.bluez.mesh.Element1"
48 #define BT_HAL_MESH_APPLICATION_INTERFACE "org.bluez.mesh.Application1"
49 #define BT_HAL_MESH_PROVISION_AGENT_INTERFACE "org.bluez.mesh.ProvisionAgent1"
50 #define BT_HAL_MESH_PROVISIONER_INTERFACE "org.bluez.mesh.Provisioner1"
51 #define BT_HAL_MESH_ERROR_INTERFACE "org.bluez.mesh.Error"
52
53 #define BT_HAL_MESH_MAX_DEVKEY_BUF_SIZE 2048
54 #define BT_HAL_MESH_MAX_MSG_BUF_SIZE 2048
55
56 static const char *dbus_err_args = "org.freedesktop.DBus.Error.InvalidArgs";
57 static const char *dbus_err_fail = "org.freedesktop.DBus.Error.Failed";
58
59 static struct l_dbus *dbus = NULL;
60 static struct l_dbus_client *client = NULL;
61
62 static struct l_dbus_proxy *net_proxy = NULL;
63 static struct l_dbus_message *agent_msg;
64
65 static handle_stack_msg mesh_event_cb = NULL;
66 static uint32_t iv_index;
67
68 struct subnet_key_request {
69         uint16_t idx;
70         const char *str;
71 };
72
73 struct app_key_request {
74         uint16_t net_idx;
75         uint16_t app_idx;
76         const char *str;
77 };
78
79 struct configuration_request {
80         const char *ele_path;
81         bool rmt;
82         bool is_dev_key;
83         uint16_t dst;
84         uint16_t idx;
85         uint8_t *data;
86         uint16_t len;
87 };
88
89 struct key_config_request {
90         const char *ele_path;
91         uint16_t dst;
92         uint16_t key_req_idx;
93         uint16_t idx;
94         bool update_req;
95 };
96
97 struct mesh_provision_auth_action {
98         const char *action;
99         bt_hal_mesh_auth_variant_e auth_type;
100 };
101
102 struct mesh_remote_node_info {
103         uint16_t unicast;
104         uint8_t num_elements;
105 };
106
107 static struct mesh_provision_auth_action auth_table[] = {
108         { "blink", BT_HAL_MESH_AUTH_REQ_BLINK_COUNT_INPUT},
109         { "beep", BT_HAL_MESH_AUTH_REQ_BEEP_COUNT_INPUT},
110         { "vibrate", BT_HAL_MESH_AUTH_REQ_VIBRATE_COUNT_INPUT},
111         { "in-numeric", BT_HAL_MESH_AUTH_REQ_NUMERIC_INPUT},
112         { "in-alpha", BT_HAL_MESH_AUTH_REQ_ALPHANUMERIC_INPUT},
113         { "twist", BT_HAL_MESH_AUTH_TWIST_COUNT_DISPLAY},
114         { "push", BT_HAL_MESH_AUTH_PUSH_COUNT_DISPLAY},
115         { "out-numeric", BT_HAL_MESH_AUTH_NUMERIC_DISPLAY},
116         { "out-alpha", BT_HAL_MESH_AUTH_ALPHANUMERIC_DISPLAY},
117         { "static-oob", BT_HAL_MESH_AUTH_REQ_OOB_STATIC_KEY_INPUT},
118         { "unknown", BT_HAL_MESH_UNKNOWN_AUTH_METHOD},
119 };
120
121
122 static uint8_t __bt_mesh_util_get_prov_error_code(char *prov_err_str)
123 {
124         uint8_t ret = 0;
125
126         if (!g_strcmp0(prov_err_str, "success"))
127                 ret = BT_HAL_MESH_PROV_ERR_SUCCESS;
128         else if (!g_strcmp0(prov_err_str, "bad-pduread"))
129                 ret = BT_HAL_MESH_PROV_ERR_INVALID_PDU;
130         else if (!g_strcmp0(prov_err_str, "confirmation-failed"))
131                 ret = BT_HAL_MESH_PROV_ERR_CONFIRM_FAILED;
132         else if (!g_strcmp0(prov_err_str, "out-of-resources"))
133                 ret = BT_HAL_MESH_PROV_ERR_INSUF_RESOURCE;
134         else if (!g_strcmp0(prov_err_str, "decryption-error"))
135                 ret = BT_HAL_MESH_PROV_ERR_DECRYPT_FAILED;
136         else if (!g_strcmp0(prov_err_str, "cannot-assign-addresses"))
137                 ret = BT_HAL_MESH_PROV_ERR_CANT_ASSIGN_ADDR;
138         else if (!g_strcmp0(prov_err_str, "timeout"))
139                 ret = BT_HAL_MESH_PROV_ERR_TIMEOUT;
140         else if (!g_strcmp0(prov_err_str, "unexpected-error"))
141                 ret = BT_HAL_MESH_PROV_ERR_UNEXPECTED_ERR;
142
143         return ret;
144 }
145
146 static bt_hal_mesh_auth_variant_e  __mesh_get_authentication_type(char *str)
147 {
148         int len = strlen(str);
149         int sz = L_ARRAY_SIZE(auth_table);
150         int i;
151
152         for (i = 0; i < sz; ++i)
153                 if (len == strlen(auth_table[i].action) &&
154                                 !strcmp(str, auth_table[i].action))
155                         break;
156         if (i < sz) {
157                 return auth_table[i].auth_type;
158         } else {
159                 ERR("Mesh : Not A Proper Authentication Type!");
160                 return BT_HAL_MESH_UNKNOWN_AUTH_METHOD;
161         }
162 }
163
164 enum mesh_dbus_interface_e {
165         MESH_APP_IFACE,
166         MESH_PROV_IFACE,
167         MESH_AGENT_IFACE,
168         MESH_ELEMENT_IFACE,
169 };
170
171 typedef enum mesh_dbus_interface_e mesh_dbus_interface_e;
172
173 struct meshcfg_model {
174         uint16_t elem_index;
175         uint32_t model;
176 };
177
178 typedef struct meshcfg_model meshcfg_model;
179
180 struct meshcfg_el {
181         char *path;
182         uint16_t index;
183         GSList *models;
184 };
185
186 typedef struct meshcfg_el meshcfg_el;
187
188 struct meshcfg_app {
189         /* Remember Proxies & dbus paths */
190         char *path;
191         char *agent_path;
192         struct l_dbus_proxy *proxy;
193         struct l_dbus_proxy *mgmt_proxy;
194
195         /* Local node Info */
196         GSList *elements;
197         uint16_t cid;
198         uint16_t pid;
199         uint16_t vid;
200         uint16_t crpl;
201         uint8_t uuid[16];
202         union {
203                 uint64_t u64;
204                 uint8_t u8[8];
205         } token;
206         bool is_prov;
207         /* Caps */
208         bool static_oob;
209         bool public_oob;
210         uint16_t out_oob;
211         uint16_t in_oob;
212         struct l_dbus_message *msg;
213         guint scan_timer_id;
214 };
215
216 typedef struct meshcfg_app meshcfg_app;
217
218 /* Will contain critical data related to local Mesh Network */
219 static GSList *mesh_apps = NULL;
220
221
222 struct meshcfg_node {
223         const char *path;
224         struct l_dbus_proxy *proxy;
225         struct l_dbus_proxy *mgmt_proxy;
226         union {
227                 uint64_t u64;
228                 uint8_t u8[8];
229         } token;
230 };
231
232 typedef struct meshcfg_node meshcfg_node;
233
234 static void __mesh_append_byte_array(struct l_dbus_message_builder *builder,
235                 unsigned char *data, unsigned int len)
236 {
237         unsigned int i;
238
239         l_dbus_message_builder_enter_array(builder, "y");
240
241         for (i = 0; i < len; i++)
242                 l_dbus_message_builder_append_basic(builder, 'y', &(data[i]));
243
244         l_dbus_message_builder_leave_array(builder);
245 }
246
247 static gint __mesh_compare_network_uuid(gconstpointer data, gconstpointer user_data)
248 {
249         const meshcfg_app *app = (meshcfg_app*) data;
250         uint8_t uuid[16];
251         memcpy(uuid, (uint8_t*) user_data, 16);
252
253         return memcmp(uuid, app->uuid, sizeof(bt_uuid_t));
254 }
255
256 static unsigned char* __mesh_get_net_uuid_from_dbus_proxy_path(
257                 const char *dbus_path)
258 {
259         char uuid[33];
260         size_t sz;
261
262         memcpy(uuid, (void*)&dbus_path[20], sizeof(uuid));
263         uuid[32] = '\0';
264         INFO("Mesh: Net UUID string [%s]", uuid);
265         return l_util_from_hexstring(uuid, &sz);
266 }
267
268 static unsigned char* __mesh_get_net_uuid_from_path(
269                 const char *dbus_path, bool is_prov,
270                         mesh_dbus_interface_e iface)
271 {
272         char uuid[33];
273         size_t sz;
274
275         switch (iface) {
276         case MESH_APP_IFACE:
277         case MESH_PROV_IFACE:  {
278                 memcpy(uuid, is_prov ? (void*) &dbus_path[16] : (void*) &dbus_path[17], 32);
279                 uuid[32] = '\0';
280                 return l_util_from_hexstring(uuid, &sz);
281         }
282         case MESH_AGENT_IFACE: {
283                 memcpy(uuid, is_prov ? (void*) &dbus_path[16] : (void*) &dbus_path[23], 32);
284                 uuid[32] = '\0';
285                 return l_util_from_hexstring(uuid, &sz);
286         }
287         case MESH_ELEMENT_IFACE: {
288                 memcpy(uuid, is_prov ? (void*) &dbus_path[16] : (void*) &dbus_path[17], 32);
289                 uuid[32] = '\0';
290                 return l_util_from_hexstring(uuid, &sz);
291         }
292         default:
293                 return NULL;
294         }
295 }
296
297 static void __mesh_hal_free_elements(gpointer data)
298 {
299         meshcfg_el *element = (meshcfg_el*) data;
300         if (!element)
301                 return;
302         g_free(element->path);
303         if (element->models)
304                 g_slist_free_full(element->models, g_free);
305
306         element->models = NULL;
307         g_free(element);
308 }
309
310 static bool __bt_mesh_proxy_check(meshcfg_app *app)
311 {
312         /* Check meshd stack Vis up or not */
313         if (!dbus) {
314                 ERR("Mesh: DBUS is not UP!, possibly stack not Up!");
315                 return false;
316         }
317         /* Check Network Proxy is added or not */
318         if (!net_proxy) {
319                 ERR("Mesh: Network proxy is not attached yet!");
320                 return false;
321         }
322
323         if (app) {
324                 /* Check App management proxyis added or not */
325                 if (!app->mgmt_proxy) {
326                         ERR("Mesh: Network proxy is not attached yet!");
327                         return false;
328                 }
329                 /* Check App  Node Proxy is added or not */
330                 if (!app->proxy) {
331                         ERR("Mesh: Node proxy is not attached yet!");
332                         return false;
333                 }
334         }
335         return true;
336 }
337
338 static void __bt_hal_mesh_destroy_app_object(gpointer data)
339 {
340
341         meshcfg_app *app = (meshcfg_app*) data;
342         if (!app)
343                 return;
344
345         mesh_apps = g_slist_remove(mesh_apps, app);
346
347         if (app->path) {
348                 INFO("Mesh: App path [%s] ", app->path);
349                 g_free(app->path);
350         }
351         if (app->agent_path) {
352                 INFO("Mesh: Agent Path [%s]", app->agent_path);
353                 g_free(app->agent_path);
354         }
355
356         if (app->elements) {
357                 INFO("Mesh: Total elements present in app [%d]",
358                                 g_slist_length(app->elements));
359                 g_slist_free_full(app->elements, __mesh_hal_free_elements);
360         }
361
362         if (app->scan_timer_id > 0) {
363                 INFO("Mesh: Remove timer if already running");
364                 g_source_remove(app->scan_timer_id);
365                 app->scan_timer_id = 0;
366         }
367         g_free(app);
368 }
369
370 static void __mesh_client_connected(struct l_dbus *dbus_obj, void *user_data)
371 {
372         ERR("MESH: D-Bus client connected\n");
373         dbus = dbus_obj;
374         if (dbus)
375                 INFO("Mesh: MeshD connected: dbus [%p]", dbus);
376 }
377
378 static void __mesh_client_disconnected(struct l_dbus *dbus_obj, void *user_data)
379 {
380         ERR("MESH: D-Bus client disconnected, possibly meshd exited \n");
381         /* TODO: Send event to app about meshd termination  & then remove all mesh apps */
382         INFO("Mesh: Total number of networks present [%d]",
383                         g_slist_length(mesh_apps));
384
385         if (mesh_apps) {
386                 g_slist_free_full(mesh_apps, __bt_hal_mesh_destroy_app_object);
387                 mesh_apps = NULL;
388         }
389         /* Set DBUS to NULL */
390         if (dbus)
391                 INFO("Mesh: dbus [%p]", dbus);
392         if (net_proxy)
393                 INFO("Mesh: net proxy [%p]", net_proxy);
394
395         dbus = NULL;
396         net_proxy = NULL;
397         INFO("Mesh: All apps cleaned up after meshd exited");
398 }
399
400 static gint __compare_proxy_path(gconstpointer data, gconstpointer user_data)
401 {
402         int ret = 0;
403         char *app_uuid_path;
404         const meshcfg_app *app = (meshcfg_app*) data;
405         char *path = (char *) user_data;
406         if (!path)
407                 return -1;
408
409         app_uuid_path = l_util_hexstring(app->uuid, 16);
410         char **strings =  g_strsplit(path, "node", 2);
411
412         ret = g_strcmp0(strings[1], app_uuid_path);
413
414         g_strfreev(strings);
415         l_free(app_uuid_path);
416
417         return ret;
418 }
419
420 static gint __compare_dbus_path(gconstpointer data, gconstpointer user_data)
421 {
422         int ret = 0;
423         const meshcfg_app *app = (meshcfg_app*) data;
424         char *path = (char *) user_data;
425         if (!path) {
426                 INFO("Mesh: NULL dbus path");
427                 return -1;
428         }
429         char **strings =  g_strsplit(path, "/elem", 2);
430         ret = g_strcmp0(strings[0], app->path);
431         g_strfreev(strings);
432         return ret;
433 }
434
435 static gint __compare_element_index(gconstpointer data, gconstpointer user_data)
436 {
437         const meshcfg_el *elem = data;
438         uint16_t elem_index = GPOINTER_TO_UINT(user_data);
439         if (!elem)
440                 return 1;
441
442         return (elem->index == elem_index ? 0 : -1);
443 }
444
445 static void __send_network_destroy_event(void *param, uint8_t status)
446 {
447         struct hal_ev_mesh_network_destroyed ev;
448         meshcfg_app *app = (meshcfg_app*)param;
449
450         memset(&ev, 0, sizeof(ev));
451         memcpy(ev.uuid, app->uuid, sizeof(app->uuid));
452         memcpy(ev.token, app->token.u8, 8);
453
454         ev.status = status;
455         if (mesh_event_cb)
456                 mesh_event_cb(HAL_EV_MESH_NETWORK_DESTROYED,
457                         (void*)&ev, sizeof(ev));
458 }
459
460 static void __mesh_proxy_added(struct l_dbus_proxy *proxy, void *user_data)
461 {
462         const char *interface = l_dbus_proxy_get_interface(proxy);
463         const char *path = l_dbus_proxy_get_path(proxy);
464
465         INFO("MESH: Proxy added: %s (%s)\n", interface, path);
466
467         if (!strcmp(interface, BT_HAL_MESH_NETWORK_INTERFACE)) {
468                 struct hal_ev_mesh_network_proxy_added ev;
469                 INFO("Mesh: Network Proxy added");
470
471                 /* Save Global proxy */
472                 net_proxy = proxy;
473                 if (net_proxy)
474                         INFO("Mesh: Net Proxy [%p]", net_proxy);
475
476                 memset(&ev, 0, sizeof(ev));
477                 ev.status = BT_STATUS_SUCCESS;
478
479                 /* Send event cb */
480                 if (mesh_event_cb)
481                         mesh_event_cb(HAL_EV_MESH_NETWORK_PROXY_ADDED,
482                                 (void*)&ev, sizeof(ev));
483
484                 return;
485         }
486
487         if (!strcmp(interface, BT_HAL_MESH_MANAGEMENT_INTERFACE)) {
488                 GSList *l;
489                 meshcfg_app *app;
490                 INFO("Mesh: Mgmt Proxy added");
491                 INFO("Mesh: Number of mesh app present in list [%d]",
492                         g_slist_length(mesh_apps));
493                 l = g_slist_find_custom(mesh_apps, path, __compare_proxy_path);
494                 if (l) {
495                         app = l->data;
496                         app->mgmt_proxy = proxy;
497                 } else {
498                         ERR("Mesh: app not found for Mgmt proxy");
499                 }
500                 return;
501         }
502
503         if (!strcmp(interface, BT_HAL_MESH_NODE_INTERFACE)) {
504                 INFO("Mesh: Node Proxy added");
505                 GSList *l;
506                 meshcfg_app *app;
507                 l = g_slist_find_custom(mesh_apps, path, __compare_proxy_path);
508                 if (l) {
509                         app = l->data;
510                         app->proxy = proxy;
511                 } else {
512                         ERR("Mesh: app not found for Node proxy");
513                 }
514                 return;
515         }
516 }
517
518 static void __mesh_proxy_removed(struct l_dbus_proxy *proxy, void *user_data)
519 {
520         const char *interface = l_dbus_proxy_get_interface(proxy);
521         const char *path = l_dbus_proxy_get_path(proxy);
522
523         INFO("Proxy removed: %s (%s)\n", interface, path);
524
525         if (!strcmp(interface, BT_HAL_MESH_NETWORK_INTERFACE)) {
526                 INFO("Mesh: Network Interface removed,, possibly meshd terminated.\n");
527                 /*TODO: Send event to app about stop of Mesh service  & then remove all apps */
528                 if (mesh_apps) {
529                         g_slist_free_full(mesh_apps, __bt_hal_mesh_destroy_app_object);
530                         mesh_apps = NULL;
531                         return;
532                 }
533         } else if (!strcmp(interface, BT_HAL_MESH_NODE_INTERFACE)) {
534                 INFO("Mesh: Node Interface removed,, possibly node has left network.\n");
535                 GSList *l;
536                 meshcfg_app *app;
537                 l = g_slist_find_custom(mesh_apps, path, __compare_proxy_path);
538                 if (l) {
539                         app = l->data;
540                         /* Send event to app about removal of a mesh local node */
541                         __send_network_destroy_event(app, BT_STATUS_SUCCESS);
542                         __bt_hal_mesh_destroy_app_object(app);
543                 } else {
544                         ERR("Mesh: app not found for Mgmt proxy");
545                 }
546                 return;
547         } else if (!strcmp(interface, BT_HAL_MESH_MANAGEMENT_INTERFACE)) {
548                 INFO("Mesh: Managament Interface removed,, possibly node has left network.\n");
549                 GSList *l;
550                 meshcfg_app *app;
551                 l = g_slist_find_custom(mesh_apps, path, __compare_proxy_path);
552                 if (l) {
553                         app = l->data;
554                         /* Send event to app about removal of a mesh local node */
555                         __send_network_destroy_event(app, BT_STATUS_SUCCESS);
556                         __bt_hal_mesh_destroy_app_object(app);
557                 } else {
558                         ERR("Mesh: app not found for Mgmt proxy");
559                 }
560                 return;
561         }
562 }
563
564 static void __mesh_property_changed(struct l_dbus_proxy *proxy, const char *name,
565                                 struct l_dbus_message *msg, void *user_data)
566 {
567         const char *interface = l_dbus_proxy_get_interface(proxy);
568         const char *path = l_dbus_proxy_get_path(proxy);
569         GSList *l;
570         meshcfg_app *app = NULL;
571
572         l = g_slist_find_custom(mesh_apps, path, __compare_proxy_path);
573         if (l) {
574                 app = l->data;
575         } else {
576                 ERR("Mesh: app not found for Mgmt proxy");
577         }
578
579         INFO("Property changed: %s %s %s\n", name, path, interface);
580
581         if (!app)
582                 return;
583
584         if (!strcmp(interface, BT_HAL_MESH_NODE_INTERFACE)) {
585
586                 if (!strcmp(name, "IvIndex")) {
587                         uint32_t ivi;
588
589                         if (!l_dbus_message_get_arguments(msg, "u", &ivi))
590                                 return;
591
592                         INFO("New IV Index: %u\n", ivi);
593
594                         iv_index = ivi;
595                         /*TODO: save iv index */
596                 }
597         }
598 }
599
600
601 static void __mesh_dbus_client_ready(struct l_dbus_client *client_obj,
602                 void *user_data)
603 {
604         INFO("Mesh: D-Bus client ready: bluetooth-meshd connected \n");
605         client = client_obj;
606 }
607
608 static void __mesh_acquire_name_callback(struct l_dbus *dbus_obj, bool success,
609                                         bool queued, void *user_data)
610 {
611         if (success == false)
612                 ERR("Mesh: Fail to acquire dbus name\n");
613
614         if (!l_dbus_object_manager_enable(dbus_obj, "/"))
615                 ERR("Mesh: Failed to register the ObjectManager\n");
616 }
617
618 static void __mesh_ready_callback(void *user_data)
619 {
620         INFO("Mesh: Connected to D-Bus\n");
621
622         if (!l_dbus_name_acquire(dbus, BT_HAL_MESH_DBUS_NAME, false, false, false,
623                                 __mesh_acquire_name_callback, NULL))
624                 ERR("Mesh: Failed to own well-known name\n");
625 }
626
627 bool _bt_hal_mesh_stack_init(void)
628 {
629         INFO("Mesh: Connect with meshd");
630         /* Connect with meshd */
631         dbus = l_dbus_new_default(L_DBUS_SYSTEM_BUS);
632         if (!dbus)
633                 return false;
634
635         INFO("Mesh: Got dbus [%p]", dbus);
636
637         if (!l_dbus_set_ready_handler(dbus, __mesh_ready_callback, NULL, NULL))
638                 return false;
639
640         client = l_dbus_client_new(dbus,
641                         BT_HAL_BLUEZ_MESH_NAME, "/org/bluez/mesh");
642         if (!client)
643                 return false;
644
645         if (!l_dbus_client_set_connect_handler(client,
646                                 __mesh_client_connected, NULL, NULL))
647                 return false;
648
649         if (!l_dbus_client_set_disconnect_handler(client,
650                                 __mesh_client_disconnected, NULL,
651                                 NULL))
652                 return false;
653         if (!l_dbus_client_set_proxy_handlers(client,
654                                 __mesh_proxy_added, __mesh_proxy_removed,
655                                 __mesh_property_changed, NULL, NULL))
656                 return false;
657         if (!l_dbus_client_set_ready_handler(client,
658                                 __mesh_dbus_client_ready, NULL, NULL))
659                 return false;
660
661         INFO("Mesh: Stack Init watchers registered with  meshd");
662         return true;
663 }
664
665 void _bt_hal_mesh_stack_deinit(void)
666 {
667         INFO("Mesh: Stack Deinit");
668
669         if (client) {
670                 l_dbus_client_destroy(client);
671                 client = NULL;
672         }
673
674         if (dbus) {
675                 l_dbus_destroy(dbus);
676                 dbus = NULL;
677         }
678
679         /* Reset Globals */
680         net_proxy = NULL;
681         agent_msg = NULL;
682         INFO("Mesh: Number of meshapps present in memory [%d]",
683                         g_slist_length(mesh_apps));
684 }
685
686 /* To send stack event to hal-mesh handler */
687 void _bt_hal_mesh_register_dbus_handler_cb(handle_stack_msg cb)
688 {
689         mesh_event_cb = cb;
690 }
691
692 /* To send stack event to hal-mesh handler */
693 void _bt_hal_mesh_unregister_dbus_handler_cb()
694 {
695         mesh_event_cb = NULL;
696 }
697
698 static bool __mesh_get_companyid(struct l_dbus *dbus,
699                 struct l_dbus_message *message,
700                         struct l_dbus_message_builder *builder,
701                                 void *user_data)
702 {
703         meshcfg_app *app = (meshcfg_app*) user_data;
704         if (!app)
705                 return false;
706
707         l_dbus_message_builder_append_basic(builder, 'q', &app->cid);
708
709         return true;
710 }
711
712 static bool __mesh_get_productid(struct l_dbus *dbus,
713                 struct l_dbus_message *message,
714                         struct l_dbus_message_builder *builder,
715                                 void *user_data)
716 {
717         meshcfg_app *app = (meshcfg_app*) user_data;
718         if (!app)
719                 return false;
720         l_dbus_message_builder_append_basic(builder, 'q', &app->pid);
721
722         return true;
723 }
724
725 static bool __mesh_get_versionid(struct l_dbus *dbus,
726                 struct l_dbus_message *message,
727                         struct l_dbus_message_builder *builder,
728                                 void *user_data)
729 {
730         meshcfg_app *app = (meshcfg_app*) user_data;
731         if (!app)
732                 return false;
733         l_dbus_message_builder_append_basic(builder, 'q', &app->vid);
734
735         return true;
736 }
737
738 static bool __mesh_get_crpl(struct l_dbus *dbus,
739                 struct l_dbus_message *message,
740                         struct l_dbus_message_builder *builder,
741                                 void *user_data)
742 {
743         meshcfg_app *app = (meshcfg_app*) user_data;
744         if (!app)
745                 return false;
746         l_dbus_message_builder_append_basic(builder, 'q', &app->crpl);
747
748         return true;
749 }
750
751 static void __send_network_attach_event(void *param, uint8_t status)
752 {
753         struct hal_ev_mesh_network_attached ev;
754         meshcfg_app *app = (meshcfg_app*)param;
755
756         memset(&ev, 0, sizeof(ev));
757         memcpy(ev.uuid, app->uuid, sizeof(app->uuid));
758         memcpy(ev.token, app->token.u8, 8);
759         ev.is_prov = app->is_prov;
760
761         ev.status = status;
762         if (mesh_event_cb)
763                 mesh_event_cb(HAL_EV_MESH_NETWORK_ATTACHED,
764                         (void*)&ev, sizeof(ev));
765 }
766
767 static void __bt_hal_mesh_attach_node_reply(struct l_dbus_proxy *proxy,
768                 struct l_dbus_message *msg, void *user_data)
769 {
770         struct l_dbus_message_iter iter_cfg;
771         char *path;
772         meshcfg_app *app = (meshcfg_app*) user_data;
773         uint32_t ivi;
774
775         INFO("Mesh: Attach Node Reply: App path [%s] Agent Path [%s]",
776                 app->path, app->agent_path);
777
778         if (l_dbus_message_is_error(msg)) {
779                 const char *name;
780                 l_dbus_message_get_error(msg, &name, NULL);
781                 ERR("Mesh: Failed to attach node: %s", name);
782                 goto failed;
783
784         }
785
786         if (!l_dbus_message_get_arguments(msg, "oa(ya(qa{sv}))",
787                                 &path, &iter_cfg))
788                 goto failed;
789
790         INFO("Mesh: Attached with path %s\n", app->path);
791         __send_network_attach_event(app, BT_STATUS_SUCCESS);
792
793         if (l_dbus_proxy_get_property(app->proxy, "IvIndex", "u", &ivi) &&
794                                                         ivi != iv_index) {
795                 iv_index = ivi;
796                 /* TODO: Save IV index */
797         }
798
799         return;
800 failed:
801         __send_network_attach_event(app, BT_STATUS_FAIL);
802         /* Destroy mesh app object */
803         __bt_hal_mesh_destroy_app_object(app);
804 }
805
806 static void __bt_hal_mesh_attach_node_setup(struct l_dbus_message *msg,
807                 void *user_data)
808 {
809         meshcfg_app *app = (meshcfg_app*) user_data;
810
811         l_dbus_message_set_arguments(msg, "ot", app->path,
812                         l_get_be64(app->token.u8));
813 }
814
815
816 static void __bt_hal_mesh_attach_node(void *user_data)
817 {
818         if (!l_dbus_proxy_method_call(net_proxy, "Attach",
819                                 __bt_hal_mesh_attach_node_setup,
820                                 __bt_hal_mesh_attach_node_reply,
821                                 user_data,
822                                 NULL)) {
823                 ERR("Mesh: Node attach failed!!");
824                 /* Node could not be attached */
825                 __send_network_attach_event(user_data, BT_STATUS_FAIL);
826                 /* Destroy mesh app object */
827                 __bt_hal_mesh_destroy_app_object((meshcfg_app*)user_data);
828         }
829 }
830
831 static struct l_dbus_message *__mesh_node_join_complete(struct l_dbus *dbus,
832                 struct l_dbus_message *message,
833                         void *user_data)
834 {
835         char *str;
836         uint64_t tmp;
837
838         meshcfg_app *app = (meshcfg_app*) user_data;
839         INFO("Mesh: Join Complete");
840
841         /* Return error */
842         if (!l_dbus_message_get_arguments(message, "t", &tmp)) {
843
844                 /* Send Network creation fail event */
845                 __send_network_attach_event(app, BT_STATUS_FAIL);
846
847                 /* Destroy mesh app object */
848                 __bt_hal_mesh_destroy_app_object(app);
849
850                 return l_dbus_message_new_error(message, dbus_err_args, NULL);
851         }
852
853         /* Save token */
854         app->token.u64 = l_get_be64(&tmp);
855         str = l_util_hexstring(&app->token.u8[0], 8);
856         INFO("Mesh: Created new node with token %s\n", str);
857         l_free(str);
858
859         /* Authenticate the node */
860         l_idle_oneshot(__bt_hal_mesh_attach_node, app, NULL);
861         return l_dbus_message_new_method_return(message);
862 }
863
864 static void append_dict_entry_basic(struct l_dbus_message_builder *builder,
865                 const char *key, const char *signature,
866                 const void *data)
867 {
868         if (!builder)
869                 return;
870
871         l_dbus_message_builder_enter_dict(builder, "sv");
872         l_dbus_message_builder_append_basic(builder, 's', key);
873         l_dbus_message_builder_enter_variant(builder, signature);
874         l_dbus_message_builder_append_basic(builder, signature[0], data);
875         l_dbus_message_builder_leave_variant(builder);
876         l_dbus_message_builder_leave_dict(builder);
877 }
878
879 static void __bt_hal_mesh_foreach_model_getter(gpointer data,
880                 gpointer user_data)
881 {
882         struct l_dbus_message_builder *builder = (struct l_dbus_message_builder *) user_data;
883         meshcfg_model *model_info = (meshcfg_model*) data;
884         bool pub_enable = true;
885         bool sub_enable = true;
886
887         l_dbus_message_builder_enter_struct(builder, "qa{sv}");
888         l_dbus_message_builder_append_basic(builder, 'q', &model_info->model);
889         l_dbus_message_builder_enter_array(builder, "{sv}");
890         append_dict_entry_basic(builder, "Subscribe", "b", &sub_enable);
891         append_dict_entry_basic(builder, "Publish", "b", &pub_enable);
892         l_dbus_message_builder_leave_array(builder);
893         l_dbus_message_builder_leave_struct(builder);
894 }
895
896 static bool __mesh_model_getter(struct l_dbus *dbus,
897                 struct l_dbus_message *message,
898                         struct l_dbus_message_builder *builder,
899                                 void *user_data)
900 {
901         meshcfg_el *element = (meshcfg_el*) user_data;
902
903         l_dbus_message_builder_enter_array(builder, "(qa{sv})");
904         g_slist_foreach(element->models,
905                 __bt_hal_mesh_foreach_model_getter, builder);
906
907         l_dbus_message_builder_leave_array(builder);
908
909         return true;
910 }
911
912 /*TODO: Vendor Model handling is currently not Handled */
913 static bool __mesh_vendor_model_getter(struct l_dbus *dbus,
914                 struct l_dbus_message *message,
915                         struct l_dbus_message_builder *builder,
916                                 void *user_data)
917 {
918         l_dbus_message_builder_enter_array(builder, "(qqa{sv})");
919         l_dbus_message_builder_leave_array(builder);
920
921         return true;
922 }
923
924 static bool __mesh_element_index_getter(struct l_dbus *dbus,
925                 struct l_dbus_message *message,
926                         struct l_dbus_message_builder *builder,
927                                 void *user_data)
928 {
929         meshcfg_el *element = (meshcfg_el*) user_data;
930         l_dbus_message_builder_append_basic(builder, 'y', &element->index);
931
932         return true;
933 }
934
935
936 static struct l_dbus_message *__mesh_device_message_received(struct l_dbus *dbus,
937                 struct l_dbus_message *msg, void *user_data)
938 {
939         struct l_dbus_message_iter iter;
940         uint16_t src, idx;
941         uint8_t *data;
942         uint32_t n;
943         bool rmt;
944         const char *dbus_path;
945         uint16_t size;
946         uint8_t *net_uuid;
947         dbus_path =  l_dbus_message_get_path(msg);
948         net_uuid = __mesh_get_net_uuid_from_path(dbus_path, true,  MESH_ELEMENT_IFACE);
949         uint8_t buf[BT_HAL_MESH_MAX_DEVKEY_BUF_SIZE];
950         struct hal_ev_mesh_devkey_message_event *ev = (void *)buf;
951
952         INFO("Mesh: app path [%s]", dbus_path);
953
954         memset(buf, 0, sizeof(buf));
955         size = (uint16_t) sizeof(*ev);
956         memcpy(ev->net_uuid, net_uuid, 16);
957         g_free(net_uuid);
958
959         if (!l_dbus_message_get_arguments(msg, "qbqay", &src, &rmt, &idx,
960                                 &iter)) {
961                 ERR("Mesh: Cannot parse received message");
962                 return l_dbus_message_new_error(msg, dbus_err_args, NULL);
963         }
964
965         if (!l_dbus_message_iter_get_fixed_array(&iter, &data, &n)) {
966                 ERR("Mesh: Cannot parse received message: data");
967                 return l_dbus_message_new_error(msg, dbus_err_args, NULL);
968         }
969
970         INFO("Mesh: Received dev key message (len %u):", n);
971         ev->source_addr = src;
972         ev->is_remote_devkey = rmt;
973         ev->netkey_idx = idx;
974         ev->data_len = n;
975         memcpy(ev->data, data, n);
976         size += n;
977
978         INFO("Mesh: Src [0x%2.2x]", src);
979         INFO("Mesh: Is Remote [%s]", rmt ? "YES" : "NO");
980         INFO("Mesh: NetKey Idx [0x%2.2x]", idx);
981         /* Send DevKeyMessage Received event */
982         if (mesh_event_cb)
983                 mesh_event_cb(HAL_EV_MESH_DEVKEY_MESSAGE_EVENT, (void*)buf, size);
984         return l_dbus_message_new_method_return(msg);
985 }
986
987 static struct l_dbus_message *__mesh_message_received(struct l_dbus *dbus,
988                 struct l_dbus_message *msg, void *user_data)
989 {
990         struct l_dbus_message_iter iter;
991         uint16_t src, idx, dst;
992         uint8_t *data;
993         uint32_t n;
994         const char *dbus_path;
995         uint16_t size;
996         bool is_provisioner = true;
997         uint8_t *net_uuid;
998         dbus_path =  l_dbus_message_get_path(msg);
999         GSList *l = g_slist_find_custom(mesh_apps, dbus_path, __compare_dbus_path);
1000         if (l) {
1001                 meshcfg_app *app = NULL;
1002                 app = l->data;
1003                 INFO("Mesh: is Provisioner [%d]", app->is_prov);
1004                 is_provisioner = app->is_prov;
1005         } else {
1006                 ERR("Mesh: app is NULL");
1007         }
1008         net_uuid = __mesh_get_net_uuid_from_path(dbus_path, is_provisioner,  MESH_ELEMENT_IFACE);
1009         uint8_t buf[BT_HAL_MESH_MAX_MSG_BUF_SIZE];
1010         struct hal_ev_mesh_message_event *ev = (void *)buf;
1011
1012         INFO("Mesh: app path [%s]", dbus_path);
1013
1014         memset(buf, 0, sizeof(buf));
1015         size = (uint16_t) sizeof(*ev);
1016         memcpy(ev->net_uuid, net_uuid, 16);
1017         g_free(net_uuid);
1018         ev->is_prov = is_provisioner;
1019
1020         if (!l_dbus_message_get_arguments(msg, "qqvay", &src, &idx, &dst,
1021                                 &iter)) {
1022                 ERR("Mesh: Cannot parse received message");
1023                 return l_dbus_message_new_error(msg, dbus_err_args, NULL);
1024         }
1025
1026         if (!l_dbus_message_iter_get_fixed_array(&iter, &data, &n)) {
1027                 ERR("Mesh: Cannot parse received message: data");
1028                 return l_dbus_message_new_error(msg, dbus_err_args, NULL);
1029         }
1030
1031         INFO("Mesh: Received mesh message (len %u):", n);
1032         ev->source_addr = src;
1033         ev->dest_addr = dst;
1034         ev->key_idx = idx;
1035         ev->data_len = n;
1036         memcpy(ev->data, data, n);
1037         size += n;
1038
1039         INFO("Mesh: Src [0x%2.2x]", src);
1040         INFO("Mesh: Dst [0x%2.2x]", dst);
1041         INFO("Mesh: NetKey Idx [0x%2.2x]", idx);
1042         /* Send Message Received event */
1043         if (mesh_event_cb) {
1044                 INFO("Mesh: Send message event");
1045                 mesh_event_cb(HAL_EV_MESH_MESSAGE_EVENT, (void*)buf, size);
1046         }
1047         return l_dbus_message_new_method_return(msg);
1048 }
1049
1050 static void __bt_hal_mesh_setup_ele_iface(struct l_dbus_interface *iface)
1051 {
1052         INFO("Mesh: Setup element interface properties & methods");
1053         /* Properties */
1054         l_dbus_interface_property(iface, "Index", 0, "y", __mesh_element_index_getter,
1055                         NULL);
1056         l_dbus_interface_property(iface, "VendorModels", 0, "a(qqa{sv})",
1057                         __mesh_vendor_model_getter, NULL);
1058         l_dbus_interface_property(iface, "Models", 0, "a(qa{sv})", __mesh_model_getter,
1059                                                                         NULL);
1060
1061         /* Methods */
1062         l_dbus_interface_method(iface, "DevKeyMessageReceived", 0,
1063                         __mesh_device_message_received, "", "qbqay", "source",
1064                         "remote", "net_index", "data");
1065         l_dbus_interface_method(iface, "MessageReceived", 0,
1066                         __mesh_message_received, "", "qqvay", "source",
1067                         "key_index", "destination", "data");
1068         /* TODO: Other methods */
1069 }
1070
1071 static struct l_dbus_message *__mesh_scan_result_received(struct l_dbus *dbus,
1072                 struct l_dbus_message *msg,
1073                         void *user_data)
1074 {
1075         struct l_dbus_message_iter iter, opts;
1076         meshcfg_app *app = (meshcfg_app*) user_data;
1077         int16_t rssi;
1078         uint32_t n;
1079         uint8_t *prov_data;
1080         char *str;
1081         const char *sig = "naya{sv}";
1082
1083         /* Find network uuid from dbus path */
1084         struct hal_ev_mesh_scan_result ev;
1085
1086         if (!app->scan_timer_id) {
1087                 /* Scan is not running */
1088                 INFO("Got scan result, but scan is already stopped");
1089                 return l_dbus_message_new_method_return(msg);
1090         }
1091         memset(&ev, 0, sizeof(ev));
1092         memcpy(ev.net_uuid, app->uuid, 16);
1093
1094         if (!l_dbus_message_get_arguments(msg, sig, &rssi, &iter, &opts)) {
1095                 ERR("Mesh: Cannot parse scan results");
1096                 ev.status = BT_STATUS_FAIL;
1097                 if (mesh_event_cb)
1098                         mesh_event_cb(HAL_EV_MESH_SCAN_RESULT, (void*)&ev, sizeof(ev));
1099                 return l_dbus_message_new_error(msg, dbus_err_args, NULL);
1100         }
1101
1102         if (!l_dbus_message_iter_get_fixed_array(&iter, &prov_data, &n) ||
1103                         n < 16) {
1104                 ERR("Mesh: Cannot parse scan result: data");
1105                 ev.status = BT_STATUS_FAIL;
1106                 if (mesh_event_cb)
1107                         mesh_event_cb(HAL_EV_MESH_SCAN_RESULT, (void*)&ev, sizeof(ev));
1108                 return l_dbus_message_new_error(msg, dbus_err_args, NULL);
1109         }
1110
1111         INFO("Mesh: Scan result:\n");
1112         INFO("Mesh: Scan rssi = [%d]\n", rssi);
1113         str = l_util_hexstring_upper(prov_data, 16);
1114         INFO("Mesh: Scan UUID = [%s]\n",  str);
1115         l_free(str);
1116
1117         if (n >= 18) {
1118                 str = l_util_hexstring_upper(prov_data + 16, 2);
1119                 INFO("Mesh: Scan OOB = [%s]\n", str);
1120                 l_free(str);
1121         }
1122
1123         if (n >= 22) {
1124                 str = l_util_hexstring_upper(prov_data + 18, 4);
1125                 INFO("Mesh: Scan URI hash = [%s]\n", str);
1126                 l_free(str);
1127         }
1128
1129         /* 16 octet Dev UUID */
1130         memcpy(ev.dev_uuid, prov_data, 16);
1131
1132         /* 2 octet Dev OOB Info */
1133         memcpy(ev.oob_info, prov_data + 16, 2);
1134
1135         /* 4 octet URI Hash */
1136         memcpy(ev.uri_hash, prov_data + 18, 4);
1137
1138         ev.rssi = rssi;
1139
1140         ev.status = BT_STATUS_SUCCESS;
1141         if (mesh_event_cb)
1142                 mesh_event_cb(HAL_EV_MESH_SCAN_RESULT,
1143                         (void*)&ev, sizeof(ev));
1144
1145         return l_dbus_message_new_method_return(msg);
1146 }
1147
1148 static struct l_dbus_message *__mesh_request_provisioner_call(
1149                 struct l_dbus *dbus,
1150                         struct l_dbus_message *msg,
1151                                 void *user_data)
1152 {
1153         uint8_t cnt;
1154         struct hal_ev_mesh_provision_finished ev;
1155         struct hal_ev_mesh_provision_data_request req;
1156         char *uuid_string;
1157         meshcfg_app *app = user_data;
1158
1159         INFO("Mesh: provisioning data requested app path [%s]",
1160                 app->path);
1161         uuid_string = l_util_hexstring(app->uuid, 16);
1162         INFO("Mesh: Network UUID [%s]", uuid_string);
1163
1164         memset(&ev, 0, sizeof(ev));
1165         memset(&req, 0, sizeof(req));
1166         memcpy(ev.net_uuid, app->uuid, 16);
1167         memcpy(req.net_uuid, app->uuid, 16);
1168         l_free(uuid_string);
1169
1170         if (!l_dbus_message_get_arguments(msg, "y", &cnt)) {
1171                 ERR("Mesh: Cannot parse request for prov data");
1172                 /* Send Event */
1173                 ev.status = BT_STATUS_FAIL;
1174                 ev.reason = BT_HAL_MESH_PROV_ERR_INTERNAL;
1175                 if (mesh_event_cb)
1176                         mesh_event_cb(HAL_EV_MESH_PROVISIONING_FINISHED,
1177                                 (void*)&ev, sizeof(ev));
1178                 return l_dbus_message_new_error(msg, dbus_err_args, NULL);
1179         }
1180
1181         app->msg = msg;
1182         req.count = cnt;
1183         if (mesh_event_cb)
1184                 mesh_event_cb(HAL_EV_MESH_PROVISIONING_DATA_REQUEST,
1185                         (void*)&req, sizeof(req));
1186
1187         l_dbus_message_ref(msg);
1188         return NULL;
1189 }
1190
1191 static struct l_dbus_message *__mesh_node_add_completed(
1192                 struct l_dbus *dbus,
1193                         struct l_dbus_message *msg,
1194                                 void *user_data)
1195 {
1196         struct l_dbus_message_iter iter;
1197         int16_t unicast;
1198         uint8_t cnt;
1199         uint32_t n;
1200         uint8_t *uuid;
1201         struct hal_ev_mesh_provision_finished ev;
1202         const char *dbus_path;
1203         uint8_t *net_uuid;
1204         dbus_path =  l_dbus_message_get_path(msg);
1205         net_uuid = __mesh_get_net_uuid_from_path(dbus_path,
1206                         true,  MESH_PROV_IFACE);
1207
1208         INFO("Mesh: app path [%s]", dbus_path);
1209
1210         memset(&ev, 0, sizeof(ev));
1211         memcpy(ev.net_uuid, net_uuid, 16);
1212
1213         g_free(net_uuid);
1214
1215         if (!l_dbus_message_get_arguments(msg, "ayqy", &iter, &unicast, &cnt)) {
1216                 ERR("Mesh: Cannot parse add node complete message");
1217                 /* Send Event */
1218                 ev.status = BT_STATUS_FAIL;
1219                 ev.reason = BT_HAL_MESH_PROV_ERR_INTERNAL;
1220                 if (mesh_event_cb)
1221                         mesh_event_cb(HAL_EV_MESH_PROVISIONING_FINISHED,
1222                                 (void*)&ev, sizeof(ev));
1223                 return l_dbus_message_new_error(msg, dbus_err_args, NULL);
1224         }
1225
1226         if (!l_dbus_message_iter_get_fixed_array(&iter, &uuid, &n) ||
1227                         n != 16) {
1228                 ERR("Mesh: Cannot parse add node complete message: uuid");
1229                 /* Send Event */
1230                 ev.status = BT_STATUS_FAIL;
1231                 ev.reason = BT_HAL_MESH_PROV_ERR_INTERNAL;
1232                 if (mesh_event_cb)
1233                         mesh_event_cb(HAL_EV_MESH_PROVISIONING_FINISHED,
1234                                 (void*)&ev, sizeof(ev));
1235                 return l_dbus_message_new_error(msg, dbus_err_args, NULL);
1236         }
1237
1238         /* Send Event */
1239         ev.status = BT_STATUS_SUCCESS;
1240         ev.reason = BT_HAL_MESH_PROV_ERR_SUCCESS;
1241         memcpy(ev.dev_uuid, uuid, 16);
1242         ev.unicast = unicast;
1243         ev.count = cnt;
1244
1245         if (mesh_event_cb)
1246                 mesh_event_cb(HAL_EV_MESH_PROVISIONING_FINISHED,
1247                         (void*)&ev, sizeof(ev));
1248
1249         return l_dbus_message_new_method_return(msg);
1250 }
1251
1252 static struct l_dbus_message *__mesh_node_add_failed(
1253                 struct l_dbus *dbus,
1254                         struct l_dbus_message *msg,
1255                                 void *user_data)
1256 {
1257         struct l_dbus_message_iter iter;
1258         uint32_t n;
1259         uint8_t *uuid;
1260         char *str, *reason;
1261         struct hal_ev_mesh_provision_finished ev;
1262         const char *dbus_path;
1263
1264         uint8_t *net_uuid;
1265         dbus_path =  l_dbus_message_get_path(msg);
1266         net_uuid = __mesh_get_net_uuid_from_path(dbus_path,
1267                         true,  MESH_PROV_IFACE);
1268
1269         memset(&ev, 0, sizeof(ev));
1270         memcpy(ev.net_uuid, net_uuid, 16);
1271
1272         g_free(net_uuid);
1273         if (!l_dbus_message_get_arguments(msg, "ays", &iter, &reason)) {
1274                 ERR("Mesh: Cannot parse add node failed message");
1275                 /* Send Event */
1276                 ev.status = BT_STATUS_FAIL;
1277                 ev.reason = BT_HAL_MESH_PROV_ERR_INTERNAL;
1278                 if (mesh_event_cb)
1279                         mesh_event_cb(HAL_EV_MESH_PROVISIONING_FINISHED,
1280                                 (void*)&ev, sizeof(ev));
1281                 return l_dbus_message_new_error(msg, dbus_err_args, NULL);
1282         }
1283
1284         if (!l_dbus_message_iter_get_fixed_array(&iter, &uuid, &n) ||
1285                         n != 16) {
1286                 ERR("Mesh:Cannot parse add node failed message: uuid");
1287                 ev.status = BT_STATUS_FAIL;
1288                 ev.reason = BT_HAL_MESH_PROV_ERR_INTERNAL;
1289                 if (mesh_event_cb)
1290                         mesh_event_cb(HAL_EV_MESH_PROVISIONING_FINISHED,
1291                                 (void*)&ev, sizeof(ev));
1292                 return l_dbus_message_new_error(msg, dbus_err_args, NULL);
1293         }
1294
1295         INFO("Mesh: Provisioning failed:\n");
1296         str = l_util_hexstring_upper(uuid, 16);
1297         INFO("Mesh: UUID = [%s] Reason [%s]", str, reason);
1298
1299         ev.status = BT_STATUS_FAIL;
1300         ev.reason = __bt_mesh_util_get_prov_error_code(str);
1301         memcpy(ev.dev_uuid, uuid, 16);
1302
1303         l_free(str);
1304
1305         if (mesh_event_cb)
1306                 mesh_event_cb(HAL_EV_MESH_PROVISIONING_FINISHED,
1307                         (void*)&ev, sizeof(ev));
1308
1309         return l_dbus_message_new_method_return(msg);
1310 }
1311
1312 static void __bt_hal_mesh_setup_prov_iface(struct l_dbus_interface *interface)
1313 {
1314         INFO("Mesh: Setup provisioner interface properties & methods");
1315         l_dbus_interface_method(interface, "ScanResult", 0,
1316                         __mesh_scan_result_received, "",
1317                         "naya{sv}", "rssi", "data", "options");
1318
1319         l_dbus_interface_method(interface, "RequestProvData", 0,
1320                         __mesh_request_provisioner_call,
1321                         "qq", "y", "net_index", "unicast", "count");
1322
1323         l_dbus_interface_method(interface, "AddNodeComplete", 0,
1324                         __mesh_node_add_completed, "", "ayqy",
1325                         "uuid", "unicast", "count");
1326
1327         l_dbus_interface_method(interface, "AddNodeFailed", 0,
1328                         __mesh_node_add_failed,
1329                         "", "ays", "uuid", "reason");
1330 }
1331
1332 static void __bt_hal_mesh_setup_app_iface(struct l_dbus_interface *iface)
1333 {
1334         INFO("Mesh: Setup application interface properties & methods");
1335
1336         l_dbus_interface_property(iface, "CompanyID", 0, "q",
1337                         __mesh_get_companyid,
1338                         NULL);
1339         l_dbus_interface_property(iface, "VersionID", 0, "q",
1340                         __mesh_get_versionid,
1341                         NULL);
1342         l_dbus_interface_property(iface, "ProductID", 0, "q",
1343                         __mesh_get_productid,
1344                         NULL);
1345         l_dbus_interface_property(iface, "CRPL", 0, "q",
1346                         __mesh_get_crpl, NULL);
1347         l_dbus_interface_method(iface, "JoinComplete", 0,
1348                         __mesh_node_join_complete,
1349                         "", "t", "token");
1350
1351         /* TODO: Methods */
1352 }
1353
1354 static void __mesh_fill_in_capabilities(meshcfg_app *app,
1355                  struct l_dbus_message_builder *builder)
1356 {
1357         if (app->in_oob & 0x08)
1358                 l_dbus_message_builder_append_basic(builder, 's', "in-alpha");
1359         if (app->in_oob & 0x04)
1360                 l_dbus_message_builder_append_basic(builder, 's', "in-numeric");
1361         if (app->in_oob & 0x02)
1362                 l_dbus_message_builder_append_basic(builder, 's', "twist");
1363         if (app->in_oob & 0x01)
1364                 l_dbus_message_builder_append_basic(builder, 's', "push");
1365 }
1366
1367 static void __mesh_fill_out_capabilities(meshcfg_app *app,
1368                  struct l_dbus_message_builder *builder)
1369 {
1370         if (app->out_oob & 0x10)
1371                 l_dbus_message_builder_append_basic(builder, 's', "out-alpha");
1372         if (app->out_oob & 0x08)
1373                 l_dbus_message_builder_append_basic(builder, 's', "out-numeric");
1374         if (app->out_oob & 0x04)
1375                 l_dbus_message_builder_append_basic(builder, 's', "vibrate");
1376         if (app->out_oob & 0x02)
1377                 l_dbus_message_builder_append_basic(builder, 's', "beep");
1378         if (app->out_oob & 0x01)
1379                 l_dbus_message_builder_append_basic(builder, 's', "blink");
1380 }
1381
1382 static bool __mesh_agent_capability_getter(
1383                 struct l_dbus *dbus, struct l_dbus_message *message,
1384                         struct l_dbus_message_builder *builder,
1385                                 void *user_data)
1386 {
1387         meshcfg_app *app;
1388         app = user_data;
1389         INFO("Mesh: app path [%s]", app->path);
1390         INFO("Mesh: Agent path [%s]", app->agent_path);
1391
1392         if (!l_dbus_message_builder_enter_array(builder, "s"))
1393                 return false;
1394
1395         __mesh_fill_out_capabilities(app, builder);
1396         __mesh_fill_in_capabilities(app, builder);
1397
1398         if (app->static_oob)
1399                 l_dbus_message_builder_append_basic(builder,
1400                         's', "static-oob");
1401
1402
1403         l_dbus_message_builder_leave_array(builder);
1404         INFO("Mesh: __agent_capability_getter: Success");
1405         return true;
1406 }
1407
1408 static struct l_dbus_message *__mesh_agent_display_string_request(
1409                 struct l_dbus *dbus,
1410                         struct l_dbus_message *msg,
1411                                 void *user_data)
1412 {
1413         struct hal_ev_mesh_authentication_request ev;
1414         char *str;
1415         uint8_t *net_uuid;
1416         const char *dbus_path;
1417         meshcfg_app *app;
1418         app = user_data;
1419         INFO("Mesh: app path [%s]", app->path);
1420         INFO("Mesh: Agent path [%s]", app->agent_path);
1421
1422         dbus_path =  l_dbus_message_get_path(msg);
1423         net_uuid = __mesh_get_net_uuid_from_path(app->agent_path,
1424                         true,  MESH_AGENT_IFACE);
1425
1426         INFO("Mesh: app path [%s]", dbus_path);
1427
1428         memset(&ev, 0, sizeof(ev));
1429         memcpy(ev.net_uuid, net_uuid, 16);
1430
1431
1432         if (!l_dbus_message_get_arguments(msg, "s", &str)) {
1433                 ERR("Mesh: Cannot parse \"DisplayString\" arguments");
1434                 struct hal_ev_mesh_provision_finished ev;
1435                 memset(&ev, 0, sizeof(ev));
1436                 memcpy(ev.net_uuid, net_uuid, 16);
1437                 ev.status = BT_STATUS_FAIL;
1438                 ev.reason = BT_HAL_MESH_PROV_ERR_INTERNAL;
1439                 if (mesh_event_cb)
1440                         mesh_event_cb(HAL_EV_MESH_PROVISIONING_FINISHED,
1441                                 (void*)&ev, sizeof(ev));
1442
1443                 g_free(net_uuid);
1444
1445                 return l_dbus_message_new_error(msg, dbus_err_fail, NULL);
1446         }
1447
1448         INFO("Mesh:[OUT] AlphaNumeric Authentication: Value [%s]", str);
1449         ev.auth_type = __mesh_get_authentication_type(str);
1450         if (ev.auth_type == BT_HAL_MESH_UNKNOWN_AUTH_METHOD)
1451                 return l_dbus_message_new_error(msg, dbus_err_fail, NULL);
1452         g_strlcpy(ev.auth_value, str, sizeof(ev.auth_value));
1453
1454         if (mesh_event_cb)
1455                 mesh_event_cb(HAL_EV_MESH_AUTHENTICATION_REQUEST,
1456                         (void*)&ev, sizeof(ev));
1457
1458         g_free(net_uuid);
1459         return l_dbus_message_new_method_return(msg);
1460 }
1461
1462 static struct l_dbus_message *__mesh_agent_display_numeric_request(
1463                 struct l_dbus *dbus,
1464                         struct l_dbus_message *msg,
1465                                 void *user_data)
1466 {
1467         uint32_t n;
1468         struct hal_ev_mesh_authentication_request ev;
1469         char *str;
1470         char *auth_value;
1471         uint8_t *net_uuid;
1472         const char *dbus_path;
1473         meshcfg_app *app;
1474         app = user_data;
1475         INFO("Mesh: app path [%s]", app->path);
1476         INFO("Mesh: Agent path [%s]", app->agent_path);
1477
1478         dbus_path =  l_dbus_message_get_path(msg);
1479         net_uuid = __mesh_get_net_uuid_from_path(app->agent_path,
1480                                 true,  MESH_AGENT_IFACE);
1481
1482         INFO("Mesh: app path [%s]", dbus_path);
1483
1484         memset(&ev, 0, sizeof(ev));
1485         memcpy(ev.net_uuid, net_uuid, 16);
1486
1487
1488         if (!l_dbus_message_get_arguments(msg, "su", &str, &n)) {
1489                 ERR("Mesh: Cannot parse \"DisplayNumeric\" arguments");
1490                 struct hal_ev_mesh_provision_finished ev;
1491                 memset(&ev, 0, sizeof(ev));
1492                 memcpy(ev.net_uuid, net_uuid, 16);
1493                 ev.status = BT_STATUS_FAIL;
1494                 ev.reason = BT_HAL_MESH_PROV_ERR_INTERNAL;
1495                 if (mesh_event_cb) {
1496                         mesh_event_cb(HAL_EV_MESH_PROVISIONING_FINISHED,
1497                                 (void*)&ev, sizeof(ev));
1498                 }
1499                 g_free(net_uuid);
1500                 return l_dbus_message_new_error(msg, dbus_err_fail, NULL);
1501         }
1502
1503         INFO("Mesh:[OUT] Numeric Authentication type [%s] value [%u]", str, n);
1504         auth_value = l_strdup_printf("%u", n);
1505         ev.auth_type = __mesh_get_authentication_type(str);
1506         if (ev.auth_type == BT_HAL_MESH_UNKNOWN_AUTH_METHOD)
1507                 return l_dbus_message_new_error(msg, dbus_err_fail, NULL);
1508         g_strlcpy(ev.auth_value, auth_value, sizeof(ev.auth_value));
1509
1510         if (mesh_event_cb)
1511                 mesh_event_cb(HAL_EV_MESH_AUTHENTICATION_REQUEST,
1512                         (void*)&ev, sizeof(ev));
1513
1514         g_free(net_uuid);
1515
1516         return l_dbus_message_new_method_return(msg);
1517 }
1518
1519 static struct l_dbus_message *__mesh_agent_prompt_numeric_request(
1520                 struct l_dbus *dbus,
1521                         struct l_dbus_message *msg,
1522                                 void *user_data)
1523 {
1524         struct hal_ev_mesh_authentication_request ev;
1525         char *str;
1526         uint8_t *net_uuid;
1527         const char *dbus_path;
1528         GSList *l;
1529         meshcfg_app *app;
1530
1531         dbus_path =  l_dbus_message_get_path(msg);
1532         net_uuid = __mesh_get_net_uuid_from_path(dbus_path,
1533                         true,  MESH_AGENT_IFACE);
1534
1535         INFO("Mesh: app path [%s]", dbus_path);
1536
1537         l = g_slist_find_custom(mesh_apps, net_uuid,
1538                         __mesh_compare_network_uuid);
1539         if (!l) {
1540                 g_free(net_uuid);
1541                 return NULL;
1542         }
1543         app = l->data;
1544
1545         memset(&ev, 0, sizeof(ev));
1546         memcpy(ev.net_uuid, net_uuid, 16);
1547
1548         g_free(net_uuid);
1549         if (!l_dbus_message_get_arguments(msg, "s", &str)) {
1550                 ERR("Mesh: Cannot parse \"PromptNumeric\" arguments");
1551
1552                 struct hal_ev_mesh_provision_finished ev;
1553                 memset(&ev, 0, sizeof(ev));
1554                 memcpy(ev.net_uuid, app->uuid, 16);
1555                 ev.status = BT_STATUS_FAIL;
1556                 ev.reason = BT_HAL_MESH_PROV_ERR_INTERNAL;
1557                 if (mesh_event_cb) {
1558                         mesh_event_cb(HAL_EV_MESH_PROVISIONING_FINISHED,
1559                                 (void*)&ev, sizeof(ev));
1560                 }
1561                 return l_dbus_message_new_error(msg, dbus_err_fail, NULL);
1562         }
1563
1564         INFO("Mesh:[IN] Numeric Authentication type [%s]", str);
1565
1566         ev.auth_type = __mesh_get_authentication_type(str);
1567         if (ev.auth_type == BT_HAL_MESH_UNKNOWN_AUTH_METHOD)
1568                 return l_dbus_message_new_error(msg, dbus_err_fail, NULL);
1569         agent_msg = msg;
1570         l_dbus_message_ref(msg);
1571         if (mesh_event_cb)
1572                 mesh_event_cb(HAL_EV_MESH_AUTHENTICATION_REQUEST,
1573                         (void*)&ev, sizeof(ev));
1574
1575         return NULL;
1576 }
1577
1578 static struct l_dbus_message *__mesh_agent_prompt_static_request(
1579                 struct l_dbus *dbus,
1580                         struct l_dbus_message *msg,
1581                                 void *user_data)
1582 {
1583         struct hal_ev_mesh_authentication_request ev;
1584         char *str;
1585         uint8_t *net_uuid;
1586         const char *dbus_path;
1587         GSList *l;
1588         meshcfg_app *app = NULL;
1589
1590         dbus_path =  l_dbus_message_get_path(msg);
1591         net_uuid = __mesh_get_net_uuid_from_path(dbus_path, true,
1592                         MESH_AGENT_IFACE);
1593
1594         INFO("Mesh: app path [%s]", dbus_path);
1595
1596         l = g_slist_find_custom(mesh_apps, net_uuid,
1597                         __mesh_compare_network_uuid);
1598
1599         if (l)
1600                 app = l->data;
1601         else
1602                 ERR("Mesh: app not found");
1603
1604
1605         memset(&ev, 0, sizeof(ev));
1606         memcpy(ev.net_uuid, net_uuid, 16);
1607
1608         g_free(net_uuid);
1609         if (!l_dbus_message_get_arguments(msg, "s", &str)) {
1610                 ERR("Mesh: Cannot parse \"PromptNumeric\" arguments");
1611
1612                 struct hal_ev_mesh_provision_finished ev;
1613                 memset(&ev, 0, sizeof(ev));
1614
1615                 if (app)
1616                         memcpy(ev.net_uuid, app->uuid, 16);
1617
1618                 ev.status = BT_STATUS_FAIL;
1619                 ev.reason = BT_HAL_MESH_PROV_ERR_INTERNAL;
1620                 if (mesh_event_cb)
1621                         mesh_event_cb(HAL_EV_MESH_PROVISIONING_FINISHED,
1622                                         (void*)&ev, sizeof(ev));
1623                 return l_dbus_message_new_error(msg, dbus_err_fail, NULL);
1624         }
1625
1626         INFO("Mesh: [IN] AlphaNumeric Authentication type [%s]", str);
1627
1628         ev.auth_type = __mesh_get_authentication_type(str);
1629         if (ev.auth_type == BT_HAL_MESH_UNKNOWN_AUTH_METHOD)
1630                 return l_dbus_message_new_error(msg, dbus_err_fail, NULL);
1631         agent_msg = msg;
1632         l_dbus_message_ref(msg);
1633         if (mesh_event_cb)
1634                 mesh_event_cb(HAL_EV_MESH_AUTHENTICATION_REQUEST,
1635                                 (void*)&ev, sizeof(ev));
1636
1637         return NULL;
1638 }
1639
1640 static void __bt_hal_mesh_setup_agent_iface(struct l_dbus_interface *interface)
1641 {
1642         INFO("Mesh: Setup Agent interface properties & methods");
1643         l_dbus_interface_property(interface, "Capabilities", 0, "as",
1644                         __mesh_agent_capability_getter,
1645                         NULL);
1646         /* TODO: Other properties */
1647         l_dbus_interface_method(interface, "DisplayString", 0,
1648                         __mesh_agent_display_string_request,
1649                         "", "s", "value");
1650         l_dbus_interface_method(interface, "DisplayNumeric", 0,
1651                         __mesh_agent_display_numeric_request,
1652                         "", "su", "type", "number");
1653         l_dbus_interface_method(interface, "PromptNumeric", 0,
1654                         __mesh_agent_prompt_numeric_request,
1655                         "u", "s", "number", "type");
1656         l_dbus_interface_method(interface, "PromptStatic", 0,
1657                         __mesh_agent_prompt_static_request,
1658                         "ay", "s", "data", "type");
1659 }
1660
1661 static void __bt_hal_mesh_register_element_obj(gpointer data, gpointer user_data)
1662 {
1663         meshcfg_el *elem = (meshcfg_el*) data;
1664         INFO("Mesh: Register Element: Index [%d] elem path [%s]",
1665                 elem->index, elem->path);
1666         if (!l_dbus_register_object(dbus, elem->path, NULL, NULL,
1667                                 BT_HAL_MESH_ELEMENT_INTERFACE, elem, NULL)) {
1668                 ERR("Mesh: Failed to register object %s", elem->path);
1669         }
1670 }
1671
1672 bool __bt_hal_mesh_register_agent(meshcfg_app *ptr)
1673 {
1674         if (!ptr)
1675                 return false;
1676
1677         if (!l_dbus_register_interface(dbus, BT_HAL_MESH_PROVISION_AGENT_INTERFACE,
1678                                 __bt_hal_mesh_setup_agent_iface, NULL, false)) {
1679                 ERR("Mesh: Unable to register agent interface");
1680                 //return false;
1681         }
1682
1683         INFO("Mesh: Register Agent path [%s]",  ptr->agent_path);
1684         if (!l_dbus_register_object(dbus, ptr->agent_path, NULL, NULL,
1685                                 BT_HAL_MESH_PROVISION_AGENT_INTERFACE, ptr, NULL)) {
1686                 ERR("Mesh: Failed to register object %s", ptr->agent_path);
1687                 return false;
1688         }
1689
1690         if (!l_dbus_object_add_interface(dbus, ptr->agent_path,
1691                                 L_DBUS_INTERFACE_PROPERTIES, NULL)) {
1692                 ERR("Mesh: Failed to add interface %s",
1693                                 L_DBUS_INTERFACE_PROPERTIES);
1694                 return false;
1695         }
1696
1697         return true;
1698 }
1699
1700 bool __bt_hal_mesh_register_application(meshcfg_app *ptr)
1701 {
1702
1703         if (!ptr)
1704                 return false;
1705         if (!dbus)
1706                 return false;
1707
1708         if (!l_dbus_register_interface(dbus, BT_HAL_MESH_APPLICATION_INTERFACE,
1709                                 __bt_hal_mesh_setup_app_iface, NULL, false)) {
1710                 ERR("Mesh: Failed to register interface %s",
1711                                 BT_HAL_MESH_APPLICATION_INTERFACE);
1712                 return false;
1713         }
1714
1715         if (!l_dbus_register_interface(dbus, BT_HAL_MESH_PROVISIONER_INTERFACE,
1716                                 __bt_hal_mesh_setup_prov_iface, NULL, false)) {
1717                 ERR("Mesh: Failed to register interface %s",
1718                                 BT_HAL_MESH_PROVISIONER_INTERFACE);
1719                 return false;
1720         }
1721
1722         if (!l_dbus_register_object(dbus, ptr->path, NULL, NULL,
1723                                 BT_HAL_MESH_APPLICATION_INTERFACE, ptr,
1724                                 BT_HAL_MESH_PROVISIONER_INTERFACE, ptr,
1725                                 NULL)) {
1726                 ERR("Mesh: Failed to register object %s", ptr->path);
1727                 return false;
1728         }
1729
1730         if (!__bt_hal_mesh_register_agent(ptr))
1731                 return false;
1732
1733         if (!l_dbus_register_interface(dbus, BT_HAL_MESH_ELEMENT_INTERFACE,
1734                                 __bt_hal_mesh_setup_ele_iface, NULL, false)) {
1735                 ERR("Mesh: Failed to register interface %s",
1736                                 BT_HAL_MESH_ELEMENT_INTERFACE);
1737                 return false;
1738         }
1739
1740         INFO("Mesh: Number of elements to be registsred [%d]",
1741                         g_slist_length(ptr->elements));
1742
1743         g_slist_foreach(ptr->elements, __bt_hal_mesh_register_element_obj, ptr);
1744
1745         INFO("Mesh: Add Object manager Interface: app path [%s]", ptr->path);
1746         if (!l_dbus_object_add_interface(dbus, ptr->path,
1747                                 L_DBUS_INTERFACE_OBJECT_MANAGER, NULL)) {
1748                 ERR("Mesh: Failed to add interface %s",
1749                                 L_DBUS_INTERFACE_OBJECT_MANAGER);
1750                 return false;
1751         }
1752         INFO("Mesh: Application Register completed");
1753
1754         return true;
1755 }
1756
1757 static void __bt_mesh_hal_create_element_object(gpointer data, gpointer user_data)
1758 {
1759         GSList *l;
1760         meshcfg_el *elem;
1761         meshcfg_model *model_info = (meshcfg_model*) data;
1762         meshcfg_app *app = (meshcfg_app*) user_data;
1763
1764         l = g_slist_find_custom(app->elements,
1765                 GUINT_TO_POINTER(model_info->elem_index), __compare_element_index);
1766         if (l) {
1767                 elem = l->data;
1768         } else {
1769                 elem = g_malloc0(sizeof(meshcfg_el));
1770                 elem->index = model_info->elem_index;
1771                 elem->path = g_strdup_printf("%s/elem%u", app->path, elem->index);
1772                 app->elements = g_slist_append(app->elements, elem);
1773                 INFO("Mesh: Created element index [%d] path [%s]",
1774                         elem->index, elem->path);
1775         }
1776         INFO("Mesh: This is model [0x%4.4x] for Element [0x%2.2x]",
1777                 model_info->model, elem->index);
1778         /* Add Model in the element */
1779         elem->models = g_slist_append(elem->models, model_info);
1780         INFO("Mesh: Model added in element: total Model count in elem [%d] is [%d]",
1781                         elem->index, g_slist_length(elem->models));
1782 }
1783
1784 meshcfg_app *__bt_hal_mesh_create_app(bt_hal_mesh_node_t *node,
1785                 GSList *models, bool is_prov)
1786 {
1787         uint8_t uuid[16];
1788         meshcfg_app *app = NULL;
1789         char *uuid_str = NULL;
1790
1791         uuid_str = l_util_hexstring(node->uuid.uu, sizeof(uuid));
1792         INFO("Mesh: Network UUID [%s]", uuid_str);
1793
1794         app = g_malloc0(sizeof(meshcfg_app));
1795         memcpy(app->uuid, node->uuid.uu, sizeof(uuid));
1796
1797         app->cid = node->vendor_info.companyid;
1798         app->pid = node->vendor_info.vendorid;
1799         app->vid =  node->vendor_info.versionid;
1800         app->crpl = node->vendor_info.crpl;
1801         INFO("Mesh: is_prov [%d]", is_prov);
1802
1803         if (is_prov) {
1804                 app->path = g_strdup_printf("/tizen/mesh/cfg/%s", uuid_str);
1805                 app->agent_path = g_strdup_printf("%s/agent", app->path);
1806
1807         } else {
1808                 app->path = g_strdup_printf("/tizen/mesh/node/%s", uuid_str);
1809                 app->agent_path = g_strdup_printf("%s/agent", app->path);
1810         }
1811         g_slist_foreach(models, __bt_mesh_hal_create_element_object, app);
1812
1813         g_free(uuid_str);
1814         app->is_prov = is_prov;
1815         app->token.u64 = node->token.u64;
1816         INFO("Mesh: Token [%llu]", (unsigned long long int)app->token.u64);
1817         INFO("Mesh: app created");
1818         return app;
1819 }
1820
1821 static void __bt_hal_mesh_leave_net_reply(
1822                 struct l_dbus_proxy *proxy,
1823                         struct l_dbus_message *msg, void *user_data)
1824 {
1825         meshcfg_app *app;
1826         app = (meshcfg_app*) user_data;
1827
1828         INFO("Mesh: Leave Network Reply from Meshd: app path [%s]", app->path);
1829         if (l_dbus_message_is_error(msg)) {
1830                 const char *name;
1831
1832                 l_dbus_message_get_error(msg, &name, NULL);
1833                 ERR("Mesh: Failed to leave network: %s", name);
1834
1835                 /* Send Network Destroy fail event */
1836                 __send_network_destroy_event(app, BT_STATUS_FAIL);
1837         } else {
1838                 INFO("Mesh: Leave Network: Success, cleanup app after proxy removed");
1839         }
1840 }
1841
1842 static void __bt_hal_mesh_leave_net_setup(struct l_dbus_message *msg,
1843                 void *user_data)
1844 {
1845         meshcfg_app *app = (meshcfg_app*) user_data;
1846
1847         l_dbus_message_set_arguments(msg, "t", l_get_be64(app->token.u8));
1848         INFO("Mesh: Leave Network Setup app path [%s]", app->path);
1849 }
1850
1851 static void __bt_hal_mesh_release_net_reply(
1852                 struct l_dbus_proxy *proxy,
1853                         struct l_dbus_message *msg, void *user_data)
1854 {
1855         meshcfg_app *app;
1856         app = (meshcfg_app*) user_data;
1857
1858         INFO("Mesh:Release Network Reply from Meshd: app path [%s]", app->path);
1859         if (l_dbus_message_is_error(msg)) {
1860                 const char *name;
1861
1862                 l_dbus_message_get_error(msg, &name, NULL);
1863                 ERR("Mesh: Failed to Release network: %s", name);
1864
1865         } else {
1866                 INFO("Mesh: Release Network: Success, cleanup app after proxy removed");
1867         }
1868 }
1869
1870 static void __bt_hal_mesh_release_net_setup(struct l_dbus_message *msg,
1871                 void *user_data)
1872 {
1873         meshcfg_app *app = (meshcfg_app*) user_data;
1874
1875         l_dbus_message_set_arguments(msg, "t", l_get_be64(app->token.u8));
1876         INFO("Mesh: Release Network Setup app path [%s]", app->path);
1877 }
1878
1879 static void __bt_hal_mesh_create_net_reply(
1880                 struct l_dbus_proxy *proxy,
1881                         struct l_dbus_message *msg, void *user_data)
1882 {
1883         meshcfg_app *app;
1884         app = (meshcfg_app*) user_data;
1885
1886         INFO("Mesh: Create Network Reply from Meshd: app path [%s]", app->path);
1887         if (l_dbus_message_is_error(msg)) {
1888                 const char *name;
1889
1890                 l_dbus_message_get_error(msg, &name, NULL);
1891                 ERR("Mesh: Failed to create network: %s", name);
1892
1893                 /* Send Network creation fail event */
1894                 __send_network_attach_event(app, BT_STATUS_FAIL);
1895
1896                 /* Destroy mesh app object */
1897                 __bt_hal_mesh_destroy_app_object(app);
1898                 return;
1899         }
1900 }
1901
1902 static void __bt_hal_mesh_create_net_setup(struct l_dbus_message *msg,
1903                 void *user_data)
1904 {
1905         meshcfg_app *app;
1906         struct l_dbus_message_builder *builder;
1907         app = (meshcfg_app*) user_data;
1908
1909         builder = l_dbus_message_builder_new(msg);
1910
1911         INFO("Mesh: Create Network Setup app path [%s]", app->path);
1912         l_dbus_message_builder_append_basic(builder, 'o', app->path);
1913         __mesh_append_byte_array(builder, app->uuid, 16);
1914         l_dbus_message_builder_finalize(builder);
1915         l_dbus_message_builder_destroy(builder);
1916 }
1917
1918 static void __mesh_trigger_scan_finished_event(meshcfg_app *app)
1919 {
1920         struct hal_ev_mesh_scan_state_changed ev;
1921
1922         memset(&ev, 0, sizeof(ev));
1923         memcpy(ev.net_uuid, app->uuid, 16);
1924
1925         ev.status = BT_STATUS_SUCCESS;
1926
1927         ev.state = HAL_MESH_SCAN_STATE_STOPPED;
1928         if (mesh_event_cb)
1929                 mesh_event_cb(HAL_EV_MESH_SCAN_STATE_CHANGED,
1930                         (void*)&ev, sizeof(ev));
1931 }
1932
1933 static gboolean __bt_mesh_scan_timer_cb(gpointer user_data)
1934 {
1935         meshcfg_app *app = (meshcfg_app*) user_data;
1936         __mesh_trigger_scan_finished_event(app);
1937         return false;
1938 }
1939
1940 void __bt_mesh_enable_scanning_timer(uint8_t *net_uuid, uint16_t secs)
1941 {
1942         GSList *l;
1943         meshcfg_app *app;
1944         l = g_slist_find_custom(mesh_apps, net_uuid,
1945                         __mesh_compare_network_uuid);
1946         if (!l)
1947                 return;
1948         app = l->data;
1949
1950         if (app->scan_timer_id > 0) {
1951                 g_source_remove(app->scan_timer_id);
1952                 app->scan_timer_id = 0;
1953         }
1954
1955         app->scan_timer_id = g_timeout_add_seconds(secs,
1956                         __bt_mesh_scan_timer_cb, app);
1957
1958         return;
1959 }
1960
1961 static void __mesh_scan_reply(struct l_dbus_proxy *proxy,
1962                 struct l_dbus_message *msg, void *user_data)
1963 {
1964         struct hal_ev_mesh_scan_state_changed ev;
1965         const char *dbus_path;
1966         uint8_t *net_uuid;
1967         dbus_path =  l_dbus_proxy_get_path(proxy);
1968         INFO("Mesh: DBUS path [%s]", dbus_path);
1969         net_uuid = __mesh_get_net_uuid_from_dbus_proxy_path(dbus_path);
1970
1971         uint16_t secs = (uint16_t) L_PTR_TO_UINT(user_data);
1972         INFO("Mesh: Scan duration [%u]", secs);
1973
1974         memset(&ev, 0, sizeof(ev));
1975         memcpy(ev.net_uuid, net_uuid, 16);
1976
1977         if (l_dbus_message_is_error(msg)) {
1978                 const char *name;
1979                 l_dbus_message_get_error(msg, &name, NULL);
1980                 ERR("Mesh: Failed to start unprovisioned scan: [%s]", name);
1981                 ev.status = BT_STATUS_FAIL;
1982         } else {
1983                 INFO("Mesh: Unprovisioned scan started\n");
1984                 ev.status = BT_STATUS_SUCCESS;
1985                 __bt_mesh_enable_scanning_timer(net_uuid, secs);
1986         }
1987
1988         ev.state = HAL_MESH_SCAN_STATE_STARTED;
1989         if (mesh_event_cb)
1990                 mesh_event_cb(HAL_EV_MESH_SCAN_STATE_CHANGED,
1991                         (void*)&ev, sizeof(ev));
1992         l_free(net_uuid);
1993 }
1994
1995 static void __mesh_scan_setup(struct l_dbus_message *msg, void *user_data)
1996 {
1997         struct l_dbus_message_builder *builder;
1998         uint16_t secs = (uint16_t) L_PTR_TO_UINT(user_data);
1999         INFO("Mesh: Scan duration [%u]", secs);
2000
2001         builder = l_dbus_message_builder_new(msg);
2002         l_dbus_message_builder_enter_array(builder, "{sv}");
2003         append_dict_entry_basic(builder, "Seconds", "q", &secs);
2004         l_dbus_message_builder_leave_array(builder);
2005         l_dbus_message_builder_finalize(builder);
2006         l_dbus_message_builder_destroy(builder);
2007 }
2008
2009
2010 bt_status_t _bt_hal_mesh_network_set_caps(
2011                 bt_uuid_t *net_uuid, bt_hal_mesh_prov_caps_t *caps)
2012 {
2013         GSList *l;
2014         meshcfg_app *app;
2015         l = g_slist_find_custom(mesh_apps, net_uuid->uu, __mesh_compare_network_uuid);
2016         if (l) {
2017                 app = l->data;
2018                 /* Fill OOB data */
2019                 app->public_oob = caps->public_oob;
2020                 app->static_oob = caps->static_oob;
2021                 app->out_oob = caps->out_oob;
2022                 app->in_oob = caps->in_oob;
2023         } else {
2024                 ERR("Mesh: app not found!!");
2025                 return BT_STATUS_PARM_INVALID;
2026
2027         }
2028         return BT_STATUS_SUCCESS;
2029 }
2030
2031 static void __bt_hal_mesh_add_node_reply(
2032                 struct l_dbus_proxy *proxy,
2033                         struct l_dbus_message *msg,
2034                                 void *user_data)
2035 {
2036         struct hal_ev_mesh_provision_status ev;
2037         const char *dbus_path;
2038         uint8_t *net_uuid;
2039         dbus_path =  l_dbus_proxy_get_path(proxy);
2040         INFO("Mesh: DBUS path [%s]", dbus_path);
2041         net_uuid = __mesh_get_net_uuid_from_dbus_proxy_path(dbus_path);
2042
2043         bt_uuid_t *dev_uuid = (bt_uuid_t*) user_data;
2044
2045         INFO("Mesh: app path [%s]", dbus_path);
2046
2047         memset(&ev, 0, sizeof(ev));
2048         memcpy(ev.net_uuid, net_uuid, 16);
2049         memcpy(ev.dev_uuid, dev_uuid->uu, 16);
2050
2051         /* Free User data */
2052         g_free((void*)dev_uuid);
2053         l_free(net_uuid);
2054
2055         if (l_dbus_message_is_error(msg)) {
2056                 const char *name;
2057
2058                 l_dbus_message_get_error(msg, &name, NULL);
2059                 ERR("Mesh: Failed to start provisioning: %s", name);
2060                 ev.status = BT_STATUS_FAIL;
2061         } else {
2062                 INFO("Mesh: Provisioning started\n");
2063                 ev.status = BT_STATUS_SUCCESS;
2064         }
2065         if (mesh_event_cb)
2066                 mesh_event_cb(HAL_EV_MESH_PROVISIONING_STATUS,
2067                         (void*)&ev, sizeof(ev));
2068         INFO("Mesh: Provisioning status sent");
2069 }
2070
2071 static void __bt_hal_mesh_add_node_setup(struct l_dbus_message *msg,
2072                 void *user_data)
2073 {
2074         char *uuid = NULL;
2075         bt_uuid_t *dev = user_data;
2076         struct l_dbus_message_builder *builder;
2077
2078         uuid =  l_util_hexstring(dev->uu, 16);
2079         if (uuid == NULL)
2080                 return;
2081
2082         INFO("Mesh: Add Node Setup UUID [%s]", uuid);
2083
2084         builder = l_dbus_message_builder_new(msg);
2085         __mesh_append_byte_array(builder, dev->uu, 16);
2086         l_dbus_message_builder_enter_array(builder, "{sv}");
2087         l_dbus_message_builder_leave_array(builder);
2088         l_dbus_message_builder_finalize(builder);
2089         l_dbus_message_builder_destroy(builder);
2090
2091         l_free(uuid);
2092 }
2093
2094 bt_status_t _bt_hal_mesh_provision_device(
2095                 bt_uuid_t *net_uuid, bt_uuid_t *dev_uuid)
2096 {
2097         GSList *l;
2098         meshcfg_app *app;
2099         bt_uuid_t *dev;
2100         l = g_slist_find_custom(mesh_apps, net_uuid->uu, __mesh_compare_network_uuid);
2101         if (l) {
2102                 app = l->data;
2103                 dev = g_memdup((gpointer)dev_uuid, 16);
2104                 INFO("Mesh: Schedule Add Node request to meshd");
2105                 if (!l_dbus_proxy_method_call(app->mgmt_proxy, "AddNode",
2106                                         __bt_hal_mesh_add_node_setup,
2107                                         __bt_hal_mesh_add_node_reply,
2108                                         (void*)dev, NULL))
2109                         return BT_STATUS_FAIL;
2110         } else {
2111                 ERR("Mesh: app not found!!");
2112                 return BT_STATUS_PARM_INVALID;
2113
2114         }
2115         return BT_STATUS_SUCCESS;
2116 }
2117
2118 static void __bt_hal_mesh_subnet_key_setup(
2119                 struct l_dbus_message *msg, void *user_data)
2120 {
2121         struct subnet_key_request *req = user_data;
2122         uint16_t idx = (uint16_t) req->idx;
2123
2124         l_dbus_message_set_arguments(msg, "q", idx);
2125 }
2126
2127 static void __bt_hal_mesh_subnet_key_reply(struct l_dbus_proxy *proxy,
2128                 struct l_dbus_message *msg, void *user_data)
2129 {
2130         struct hal_ev_mesh_netkey_execute_event ev;
2131         const char *dbus_path;
2132         uint8_t *net_uuid;
2133         struct subnet_key_request *req = user_data;
2134         const char *method = req->str;
2135
2136         dbus_path =  l_dbus_proxy_get_path(proxy);
2137         INFO("Mesh: DBUS path [%s]", dbus_path);
2138         net_uuid = __mesh_get_net_uuid_from_dbus_proxy_path(dbus_path);
2139
2140         memset(&ev, 0, sizeof(ev));
2141         memcpy(ev.net_uuid, net_uuid, 16);
2142         ev.key_idx = req->idx;
2143
2144         g_free(net_uuid);
2145
2146         if (l_dbus_message_is_error(msg)) {
2147                 const char *name;
2148
2149                 l_dbus_message_get_error(msg, &name, NULL);
2150                 ERR("Mesh: Subnet [%s] failed: error: [%s]", method, name);
2151                 ev.status = BT_STATUS_FAIL;
2152         }
2153
2154         ev.status = BT_STATUS_SUCCESS;
2155
2156         if (!strcmp("CreateSubnet", method)) {
2157                 INFO("Mesh: Reply for CreateSubnet");
2158                 ev.key_event = HAL_MESH_KEY_ADD;
2159         } else if (!strcmp("DeleteSubnet", method)) {
2160                 INFO("Mesh: Reply for DeleteSubnet");
2161                 ev.key_event = HAL_MESH_KEY_DELETE;
2162         } else if (!strcmp("UpdateSubnet", method)) {
2163                 INFO("Mesh: Reply for UpdateSubnet");
2164                 ev.key_event = HAL_MESH_KEY_UPDATE;
2165         }
2166
2167         if (mesh_event_cb)
2168                 mesh_event_cb(HAL_EV_MESH_NETKEY_EXECUTE_EVENT,
2169                         (void*)&ev, sizeof(ev));
2170 }
2171
2172 static bool  __mesh_subnet_netkey_command_execute(meshcfg_app *app,
2173                         uint16_t index, const char *key_execute_method)
2174 {
2175         struct subnet_key_request *req;
2176
2177         req = l_new(struct subnet_key_request, 1);
2178         req->str = key_execute_method;
2179         req->idx = index;
2180
2181         if (!l_dbus_proxy_method_call(app->mgmt_proxy, key_execute_method,
2182                                 __bt_hal_mesh_subnet_key_setup,
2183                                 __bt_hal_mesh_subnet_key_reply,
2184                                 req, l_free))
2185                 return false;
2186
2187         return true;
2188 }
2189
2190 static void __bt_hal_mesh_app_key_setup(struct l_dbus_message *msg,
2191         void *user_data)
2192 {
2193         struct app_key_request *req = user_data;
2194         uint16_t net_idx = (uint16_t) req->net_idx;
2195         uint16_t app_idx = (uint16_t) req->app_idx;
2196
2197         if (g_strcmp0(req->str, "CreateAppKey") == 0)
2198                 l_dbus_message_set_arguments(msg, "qq", net_idx, app_idx);
2199         else
2200                 l_dbus_message_set_arguments(msg, "q", app_idx);
2201 }
2202
2203 static void __bt_hal_mesh_app_key_reply(struct l_dbus_proxy *proxy,
2204                 struct l_dbus_message *msg, void *user_data)
2205 {
2206         struct hal_ev_mesh_appkey_execute_event ev;
2207         const char *dbus_path;
2208         uint8_t *net_uuid;
2209         struct app_key_request *req = user_data;
2210         const char *method = req->str;
2211
2212         dbus_path =  l_dbus_proxy_get_path(proxy);
2213         INFO("Mesh: DBUS path [%s]", dbus_path);
2214         net_uuid = __mesh_get_net_uuid_from_dbus_proxy_path(dbus_path);
2215
2216         memset(&ev, 0, sizeof(ev));
2217         memcpy(ev.net_uuid, net_uuid, 16);
2218         ev.net_idx = req->net_idx;
2219         ev.app_idx = req->app_idx;
2220
2221         g_free(net_uuid);
2222
2223         if (l_dbus_message_is_error(msg)) {
2224                 const char *name;
2225
2226                 l_dbus_message_get_error(msg, &name, NULL);
2227                 ERR("Mesh: AppKey execute [%s] failed: error: [%s]", method, name);
2228                 ev.status = BT_STATUS_FAIL;
2229         } else
2230                 ev.status = BT_STATUS_SUCCESS;
2231
2232         if (!strcmp("CreateAppKey", method)) {
2233                 INFO("Mesh: AppKey Create Reply");
2234                 ev.key_event = HAL_MESH_KEY_ADD;
2235         } else if (!strcmp("DeleteAppKey", method)) {
2236                 INFO("Mesh: AppKey Delete Reply");
2237                 ev.key_event = HAL_MESH_KEY_DELETE;
2238         } else if (!strcmp("UpdateAppKey", method)) {
2239                 INFO("Mesh: AppKey Update Reply");
2240                 ev.key_event = HAL_MESH_KEY_UPDATE;
2241         }
2242
2243         if (mesh_event_cb)
2244                 mesh_event_cb(HAL_EV_MESH_APPKEY_EXECUTE_EVENT, (void*)&ev, sizeof(ev));
2245 }
2246
2247 static bool  __mesh_subnet_appkey_command_execute(meshcfg_app *app,
2248                         uint16_t net_idx, uint16_t app_idx,
2249                                 const char *key_execute_method)
2250 {
2251         struct app_key_request *req;
2252
2253         req = l_new(struct app_key_request, 1);
2254         req->str = key_execute_method;
2255         req->net_idx = net_idx;
2256         req->app_idx = app_idx;
2257
2258         if (!l_dbus_proxy_method_call(app->mgmt_proxy, key_execute_method,
2259                                 __bt_hal_mesh_app_key_setup,
2260                                 __bt_hal_mesh_app_key_reply,
2261                                 req, l_free))
2262                 return false;
2263
2264         return true;
2265 }
2266
2267 bt_status_t _bt_hal_mesh_network_subnet_execute(bt_uuid_t *net_uuid,
2268                         bt_mesh_key_op_e op, uint16_t netkey_idx)
2269 {
2270         GSList *l;
2271         meshcfg_app *app;
2272         bool status = true;
2273         l = g_slist_find_custom(mesh_apps, net_uuid->uu, __mesh_compare_network_uuid);
2274         if (l) {
2275                 app = l->data;
2276
2277                 if (op == BT_MESH_KEY_CREATE)
2278                         status = __mesh_subnet_netkey_command_execute(app,
2279                                         netkey_idx, "CreateSubnet");
2280                 else if (op == BT_MESH_KEY_DELETE)
2281                         status = __mesh_subnet_netkey_command_execute(app,
2282                                         netkey_idx, "DeleteSubnet");
2283                 else if (op == BT_MESH_KEY_UPDATE)
2284                         status = __mesh_subnet_netkey_command_execute(app,
2285                                         netkey_idx, "UpdateSubnet");
2286                 if (!status)
2287                         return BT_STATUS_FAIL;
2288
2289         } else {
2290                 ERR("Mesh: app not found!!");
2291                 return BT_STATUS_PARM_INVALID;
2292
2293         }
2294         return BT_STATUS_SUCCESS;
2295 }
2296
2297 bt_status_t _bt_hal_mesh_network_appkey_execute(bt_uuid_t *net_uuid,
2298                         bt_mesh_key_op_e op, uint16_t netkey_idx, uint16_t appkey_idx)
2299 {
2300         GSList *l;
2301         meshcfg_app *app;
2302         bool status = true;
2303         l = g_slist_find_custom(mesh_apps, net_uuid->uu, __mesh_compare_network_uuid);
2304         if (l) {
2305                 app = l->data;
2306
2307                 if (op == BT_MESH_KEY_CREATE)
2308                         status = __mesh_subnet_appkey_command_execute(app,
2309                                 netkey_idx, appkey_idx, "CreateAppKey");
2310                 else if (op == BT_MESH_KEY_DELETE)
2311                         status = __mesh_subnet_appkey_command_execute(app,
2312                                 netkey_idx, appkey_idx, "DeleteAppKey");
2313                 else if (op == BT_MESH_KEY_UPDATE) {
2314                         INFO("Mesh: Update ApKey command NK Idx [0x%2.2x] AK Idx [0x%2.2x]",
2315                                         netkey_idx, appkey_idx);
2316                         status = __mesh_subnet_appkey_command_execute(app,
2317                                 netkey_idx, appkey_idx, "UpdateAppKey");
2318                 }
2319                 if (!status)
2320                         return BT_STATUS_FAIL;
2321
2322         } else {
2323                 ERR("Mesh: app not found!!");
2324                 return BT_STATUS_PARM_INVALID;
2325
2326         }
2327         return BT_STATUS_SUCCESS;
2328 }
2329
2330 bt_status_t _bt_hal_mesh_send_provision_data(
2331                 bt_uuid_t *net_uuid, uint16_t netkey_idx, uint16_t unicast)
2332 {
2333         GSList *l;
2334         meshcfg_app *app;
2335         struct l_dbus_message *msg;
2336         struct l_dbus_message *reply;
2337
2338         l = g_slist_find_custom(mesh_apps, net_uuid->uu, __mesh_compare_network_uuid);
2339         if (l) {
2340                 app = l->data;
2341                 msg = app->msg;
2342
2343                 reply = l_dbus_message_new_method_return(msg);
2344                 l_dbus_message_set_arguments(reply, "qq", netkey_idx,  unicast);
2345                 l_dbus_send(dbus, reply);
2346         } else {
2347                 ERR("Mesh: app not found!!");
2348                 return BT_STATUS_PARM_INVALID;
2349         }
2350         return BT_STATUS_SUCCESS;
2351 }
2352
2353 bt_status_t _bt_hal_mesh_network_scan_cancel(bt_uuid_t *net_uuid)
2354 {
2355         GSList *l;
2356         meshcfg_app *app;
2357         l = g_slist_find_custom(mesh_apps,
2358                 net_uuid->uu, __mesh_compare_network_uuid);
2359         if (l) {
2360                 app = l->data;
2361                 if (!__bt_mesh_proxy_check(app)) {
2362                         ERR("Mesh: Proxy check failed!!");
2363                         return BT_STATUS_FAIL;
2364                 }
2365                 if (!l_dbus_proxy_method_call(app->mgmt_proxy,
2366                                 "UnprovisionedScanCancel",
2367                                         NULL, NULL, NULL, NULL))
2368                         return BT_STATUS_FAIL;
2369         } else {
2370                 ERR("Mesh: app not found!!");
2371                 return BT_STATUS_PARM_INVALID;
2372         }
2373
2374         /* Stop Scan timer */
2375         if (app->scan_timer_id > 0) {
2376                 g_source_remove(app->scan_timer_id);
2377                 app->scan_timer_id = 0;
2378         }
2379
2380         /* Trigger Scan finished event */
2381         __mesh_trigger_scan_finished_event(app);
2382
2383         return BT_STATUS_SUCCESS;
2384 }
2385
2386 bt_status_t _bt_hal_mesh_auth_reply(bt_hal_mesh_auth_variant_e auth_type,
2387                         const char *auth_value)
2388 {
2389         uint32_t val_u32;
2390         struct l_dbus_message *reply = NULL;
2391         struct l_dbus_message_builder *builder;
2392         uint8_t *alpha;
2393         bt_status_t ret = BT_STATUS_SUCCESS;
2394         size_t sz = 0;
2395
2396         /* Proxy Check */
2397         if (!__bt_mesh_proxy_check(0)) {
2398                 ERR("Mesh: Proxy check failed!!");
2399                 return BT_STATUS_FAIL;
2400         }
2401         INFO("Mesh: Authentication Reply: auth type [%d]", auth_type);
2402         INFO("Mesh: Authentication Reply: auth value [%s]", auth_value);
2403         /* For Numeric Type Inputs: Numeric, Blink, Beep & Vibrate */
2404         if (auth_type >= BT_HAL_MESH_AUTH_REQ_NUMERIC_INPUT &&
2405                         auth_type <= BT_HAL_MESH_AUTH_REQ_VIBRATE_COUNT_INPUT) {
2406                 INFO("Mesh: Authentication reply: Numeric Type");
2407                 val_u32 = atoi(auth_value);
2408                 reply = l_dbus_message_new_method_return(agent_msg);
2409                 l_dbus_message_set_arguments(reply, "u", val_u32);
2410                 if (!reply)
2411                         reply = l_dbus_message_new_error(agent_msg, dbus_err_fail, NULL);
2412                 l_dbus_send(dbus, reply);
2413                 ret = BT_STATUS_SUCCESS;
2414         /* For Alpha-Numeric */
2415         } else if (auth_type == BT_HAL_MESH_AUTH_REQ_ALPHANUMERIC_INPUT ||
2416                         auth_type == BT_HAL_MESH_AUTH_REQ_OOB_STATIC_KEY_INPUT ||
2417                         auth_type == BT_HAL_MESH_AUTH_REQ_OOB_PUBLIC_KEY_INPUT) {
2418                 INFO("Mesh: Authentication reply: Alpha-Numeric Type");
2419                 alpha = l_util_from_hexstring(auth_value, &sz);
2420                 reply = l_dbus_message_new_method_return(agent_msg);
2421                 builder = l_dbus_message_builder_new(reply);
2422                 __mesh_append_byte_array(builder, alpha, 16);
2423                 l_dbus_message_builder_finalize(builder);
2424                 l_dbus_message_builder_destroy(builder);
2425                 l_free(alpha);
2426                 if (!reply)
2427                         reply = l_dbus_message_new_error(agent_msg, dbus_err_fail, NULL);
2428                 l_dbus_send(dbus, reply);
2429                 ret = BT_STATUS_SUCCESS;
2430         }
2431         return ret;
2432 }
2433
2434 bt_status_t _bt_hal_mesh_network_scan(bt_uuid_t *net_uuid,
2435                 bt_hal_mesh_scan_param_t *param)
2436 {
2437         GSList *l;
2438         meshcfg_app *app;
2439         l = g_slist_find_custom(mesh_apps, net_uuid->uu, __mesh_compare_network_uuid);
2440         if (l) {
2441                 app = l->data;
2442                 if (!__bt_mesh_proxy_check(app)) {
2443                         ERR("Mesh: Proxy check failed!!");
2444                         return BT_STATUS_FAIL;
2445                 }
2446                 if (!l_dbus_proxy_method_call(app->mgmt_proxy, "UnprovisionedScan",
2447                                         __mesh_scan_setup, __mesh_scan_reply,
2448                                         L_UINT_TO_PTR(param->scan_time), NULL))
2449                         return BT_STATUS_FAIL;
2450         } else {
2451                 ERR("Mesh: app not found!!");
2452                 return BT_STATUS_PARM_INVALID;
2453         }
2454         return BT_STATUS_SUCCESS;
2455 }
2456
2457 static void __bt_hal_mesh_delete_node_setup(struct l_dbus_message *msg,
2458                 void *user_data)
2459 {
2460         struct mesh_remote_node_info *node_info = \
2461                 (struct mesh_remote_node_info*) user_data;
2462
2463         l_dbus_message_set_arguments(msg, "qy",
2464                 node_info->unicast, node_info->num_elements);
2465         INFO("Mesh: Delete Remote Node Setup params passed");
2466 }
2467
2468 static void __bt_hal_mesh_delete_node_reply(
2469                 struct l_dbus_proxy *proxy,
2470                         struct l_dbus_message *msg, void *user_data)
2471 {
2472         INFO("Mesh: Delete Remote Node Reply from DBUS");
2473 }
2474
2475 bt_status_t _bt_hal_mesh_node_delete(bt_uuid_t *network,
2476                 uint16_t unicast, uint16_t num_elements)
2477 {
2478         GSList *l;
2479         meshcfg_app *app;
2480         struct mesh_remote_node_info *node_info;
2481         INFO("Mesh: Delete Remote Node");
2482         l = g_slist_find_custom(mesh_apps, network->uu, __mesh_compare_network_uuid);
2483         if (l) {
2484                 app = l->data;
2485                 if (!__bt_mesh_proxy_check(app)) {
2486                         ERR("Mesh: Proxy check failed!!");
2487                         return BT_STATUS_FAIL;
2488                 }
2489                 INFO("Mesh: Delete Remote Node Unicast [0x%2.2x] Num els [%u]",
2490                         unicast, num_elements);
2491
2492                 /* Delete Remote Node Request */
2493                 node_info = g_malloc0(sizeof(struct mesh_remote_node_info));
2494                 node_info->unicast = unicast;
2495                 node_info->num_elements = num_elements;
2496
2497                 if (!l_dbus_proxy_method_call(app->mgmt_proxy, "DeleteRemoteNode",
2498                                         __bt_hal_mesh_delete_node_setup,
2499                                         __bt_hal_mesh_delete_node_reply, node_info,
2500                                         l_free)) {
2501                         ERR("Mesh: Delete Remote Node Request failed!!");
2502                         g_free(node_info);
2503                         return BT_STATUS_FAIL;
2504                 }
2505         } else {
2506                 ERR("Mesh: App not found!!");
2507                 return BT_STATUS_PARM_INVALID;
2508         }
2509         INFO("Mesh: Delete Remote Node Call issued successfully!!");
2510         return BT_STATUS_SUCCESS;
2511 }
2512
2513 bt_status_t _bt_hal_mesh_network_release(bt_uuid_t *net_uuid)
2514 {
2515         GSList *l;
2516         meshcfg_app *app;
2517         INFO("Mesh: Release Network");
2518         l = g_slist_find_custom(mesh_apps, net_uuid->uu, __mesh_compare_network_uuid);
2519         if (l) {
2520                 app = l->data;
2521                 if (!__bt_mesh_proxy_check(app)) {
2522                         ERR("Mesh: Proxy check failed!!");
2523                         return BT_STATUS_FAIL;
2524                 }
2525                 INFO("Mesh: Release Network");
2526                 /* Create CFG Network */
2527                 if (!l_dbus_proxy_method_call(net_proxy, "Release",
2528                                         __bt_hal_mesh_release_net_setup,
2529                                         __bt_hal_mesh_release_net_reply, app,
2530                                         NULL)) {
2531                         ERR("Mesh: Network Release failed!!");
2532                         return BT_STATUS_FAIL;
2533                 }
2534         } else {
2535                 ERR("Mesh: App not found!!");
2536                 return BT_STATUS_PARM_INVALID;
2537         }
2538         INFO("Mesh: Network Release Call issued successfully!!");
2539         return BT_STATUS_SUCCESS;
2540 }
2541
2542 bt_status_t _bt_hal_mesh_network_destroy(bt_uuid_t *net_uuid)
2543 {
2544         GSList *l;
2545         meshcfg_app *app;
2546         INFO("Mesh: Destroy network");
2547         l = g_slist_find_custom(mesh_apps, net_uuid->uu, __mesh_compare_network_uuid);
2548         if (l) {
2549                 app = l->data;
2550                 if (!__bt_mesh_proxy_check(app)) {
2551                         ERR("Mesh: Proxy check failed!!");
2552                         return BT_STATUS_FAIL;
2553                 }
2554                 INFO("Mesh: Destroy Network");
2555                 /* Create CFG Network */
2556                 if (!l_dbus_proxy_method_call(net_proxy, "Leave",
2557                                         __bt_hal_mesh_leave_net_setup,
2558                                         __bt_hal_mesh_leave_net_reply, app,
2559                                         NULL)) {
2560                         ERR("Mesh: Network Leave failed!!");
2561                         return BT_STATUS_FAIL;
2562                 }
2563         } else {
2564                 ERR("Mesh: App not found!!");
2565                 return BT_STATUS_PARM_INVALID;
2566         }
2567         INFO("Mesh: Network Leave Call issued successfully!!");
2568         return BT_STATUS_SUCCESS;
2569 }
2570
2571 bt_status_t _bt_hal_mesh_create_network(
2572                 bt_hal_mesh_node_t *node, GSList *models, bool is_prov)
2573 {
2574         meshcfg_app *app;
2575
2576         INFO("Mesh: Create Network Request");
2577
2578         if (!__bt_mesh_proxy_check(0)) {
2579                 ERR("Mesh: Proxy check failed!!");
2580                 return BT_STATUS_FAIL;
2581         }
2582
2583         INFO("Mesh: Node Element count [%d]", node->num_elements);
2584         INFO("Mesh: Node Primary Unicast[0x%2.2x]", node->primary_unicast);
2585         INFO("Mesh: Node Vendor Info: CID[0x%2.2x]", node->vendor_info.companyid);
2586         INFO("Mesh: Node Vendor Info: VID[0x%2.2x]", node->vendor_info.vendorid);
2587         INFO("Mesh: Node Vendor Info: VSID[0x%2.2x]", node->vendor_info.versionid);
2588         INFO("Mesh: Node Vendor Info: CRPL[0x%2.2x]", node->vendor_info.crpl);
2589         INFO("Mesh: Node Total Number of Models in the node[%d]", g_slist_length(models));
2590         INFO("Mesh: Token [%llu]", (unsigned long long int)node->token.u64);
2591         /* Create DBUS APP */
2592         app = __bt_hal_mesh_create_app(node, models, is_prov);
2593         if (!app)
2594                 return BT_STATUS_FAIL;
2595
2596         /* Register DBUS APP */
2597         if (!__bt_hal_mesh_register_application(app))
2598                 goto failed;
2599
2600         if (app->token.u64 == 0) {
2601                 INFO("Mesh: Create New Network");
2602                 /* Create CFG Network */
2603                 if (!l_dbus_proxy_method_call(net_proxy, "CreateNetwork",
2604                                         __bt_hal_mesh_create_net_setup,
2605                                         __bt_hal_mesh_create_net_reply, app,
2606                                         NULL)) {
2607                         ERR("Mesh: Network Create failed!!");
2608                         goto failed;
2609                 }
2610         } else {
2611                 INFO("Mesh: Attach Node to Network: Token [%llu]", (unsigned long long int)app->token.u64);
2612                 /* Attach to Network */
2613                 if (!l_dbus_proxy_method_call(net_proxy, "Attach",
2614                                         __bt_hal_mesh_attach_node_setup,
2615                                         __bt_hal_mesh_attach_node_reply,
2616                                         app,
2617                                 NULL)) {
2618                         ERR("Mesh: Node attach failed!!");
2619                         goto failed;
2620                 }
2621         }
2622
2623         INFO("Mesh: Node registration request scheudled");
2624         mesh_apps = g_slist_append(mesh_apps, app);
2625         INFO("Mesh: Total number of apps in list  [%d]",
2626                 g_slist_length(mesh_apps));
2627         return BT_STATUS_SUCCESS;
2628 failed:
2629         ERR("Mesh: network can not be created!!");
2630         __bt_hal_mesh_destroy_app_object(app);
2631         return BT_STATUS_FAIL;
2632 }
2633
2634 bt_status_t _bt_hal_mesh_join_network(
2635                 bt_hal_mesh_node_t *node, GSList *models)
2636 {
2637         meshcfg_app *app;
2638
2639         INFO("Mesh: Join Network Request");
2640
2641         if (!__bt_mesh_proxy_check(0)) {
2642                 ERR("Mesh: Proxy check failed!!");
2643                 return BT_STATUS_FAIL;
2644         }
2645
2646         INFO("Mesh: Node Element count [%d]", node->num_elements);
2647         INFO("Mesh: Node Primary Unicast[0x%2.2x]", node->primary_unicast);
2648         INFO("Mesh: Node Vendor Info: CID[0x%2.2x]", node->vendor_info.companyid);
2649         INFO("Mesh: Node Vendor Info: VID[0x%2.2x]", node->vendor_info.vendorid);
2650         INFO("Mesh: Node Vendor Info: VSID[0x%2.2x]", node->vendor_info.versionid);
2651         INFO("Mesh: Node Vendor Info: CRPL[0x%2.2x]", node->vendor_info.crpl);
2652         INFO("Mesh: Node Total Number of Models in the node[%d]", g_slist_length(models));
2653         INFO("Mesh: Token [%llu]", (unsigned long long int)node->token.u64);
2654         /* Create DBUS APP */
2655         app = __bt_hal_mesh_create_app(node, models, false);
2656         if (!app)
2657                 return BT_STATUS_FAIL;
2658         /* Register DBUS APP */
2659         if (!__bt_hal_mesh_register_application(app))
2660                 goto failed;
2661
2662         INFO("Mesh: Create New Network");
2663         INFO("Mesh: app is_prov [%d]", app->is_prov);
2664         if (!l_dbus_proxy_method_call(net_proxy, "Join",
2665                                 __bt_hal_mesh_create_net_setup,
2666                                 __bt_hal_mesh_create_net_reply, app,
2667                                 NULL)) {
2668                  ERR("Mesh: Join Network failed!!");
2669                  goto failed;
2670         }
2671
2672         INFO("Mesh: Node registration request scheudled");
2673         INFO("Mesh: Total number of apps in list Before [%d]",
2674                         g_slist_length(mesh_apps));
2675         mesh_apps = g_slist_append(mesh_apps, app);
2676         INFO("Mesh: Total number of apps in list  [%d]",
2677                         g_slist_length(mesh_apps));
2678         return BT_STATUS_SUCCESS;
2679 failed:
2680         ERR("Mesh: network can not be joined!!");
2681         __bt_hal_mesh_destroy_app_object(app);
2682         return BT_STATUS_FAIL;
2683 }
2684
2685 bt_status_t _bt_hal_mesh_join_cancel(bt_uuid_t *node_uuid)
2686 {
2687         GSList *l;
2688         meshcfg_app *app;
2689         INFO("Mesh: Call cancel join");
2690         l = g_slist_last(mesh_apps);
2691         if (l) {
2692                 app = l->data;
2693                 if (!l_dbus_proxy_method_call(net_proxy, "Cancel",
2694                                         NULL,
2695                                         NULL, NULL,
2696                                         NULL)) {
2697                         ERR("Mesh: Cancel Join failed!!");
2698                         return BT_STATUS_FAIL;
2699                 }
2700         } else {
2701                 ERR("Mesh: App not found!!");
2702                 return BT_STATUS_PARM_INVALID;
2703         }
2704         INFO("Mesh: Cancel Join request Call issued successfully!!");
2705         __bt_hal_mesh_destroy_app_object(app);
2706         return BT_STATUS_SUCCESS;
2707 }
2708
2709 static void __bt_hal_mesh_config_send(
2710                 struct l_dbus_message *msg, void *user_data)
2711 {
2712         struct configuration_request *req = user_data;
2713         struct l_dbus_message_builder *builder;
2714
2715         builder = l_dbus_message_builder_new(msg);
2716
2717         l_dbus_message_builder_append_basic(builder, 'o', req->ele_path);
2718         l_dbus_message_builder_append_basic(builder, 'q', &req->dst);
2719         if (req->is_dev_key)
2720                 l_dbus_message_builder_append_basic(builder, 'b', &req->rmt);
2721
2722         l_dbus_message_builder_append_basic(builder, 'q', &req->idx);
2723
2724         /* Options */
2725         l_dbus_message_builder_enter_array(builder, "{sv}");
2726         l_dbus_message_builder_enter_dict(builder, "sv");
2727         l_dbus_message_builder_leave_dict(builder);
2728         l_dbus_message_builder_leave_array(builder);
2729
2730         __mesh_append_byte_array(builder, req->data, req->len);
2731         l_dbus_message_builder_finalize(builder);
2732         l_dbus_message_builder_destroy(builder);
2733 }
2734
2735 static void __bt_hal_mesh_key_config_send(
2736                 struct l_dbus_message *msg, void *user_data)
2737 {
2738         struct key_config_request *req = user_data;
2739         struct l_dbus_message_builder *builder;
2740
2741         builder = l_dbus_message_builder_new(msg);
2742
2743         l_dbus_message_builder_append_basic(builder, 'o', req->ele_path);
2744         l_dbus_message_builder_append_basic(builder, 'q', &req->dst);
2745         l_dbus_message_builder_append_basic(builder, 'q', &req->key_req_idx);
2746         l_dbus_message_builder_append_basic(builder, 'q', &req->idx);
2747         l_dbus_message_builder_append_basic(builder, 'b', &req->update_req);
2748         l_dbus_message_builder_finalize(builder);
2749         l_dbus_message_builder_destroy(builder);
2750 }
2751
2752 static void __bt_hal_mesh_model_execute_message(
2753                 struct l_dbus_message *msg, void *user_data)
2754 {
2755         struct configuration_request *req = user_data;
2756         struct l_dbus_message_builder *builder;
2757
2758         builder = l_dbus_message_builder_new(msg);
2759
2760         l_dbus_message_builder_append_basic(builder, 'o', req->ele_path);
2761         l_dbus_message_builder_append_basic(builder, 'q', &req->dst);
2762         l_dbus_message_builder_append_basic(builder, 'q', &req->idx);
2763
2764         /* Options */
2765         l_dbus_message_builder_enter_array(builder, "{sv}");
2766         l_dbus_message_builder_enter_dict(builder, "sv");
2767         l_dbus_message_builder_leave_dict(builder);
2768         l_dbus_message_builder_leave_array(builder);
2769
2770         __mesh_append_byte_array(builder, req->data, req->len);
2771         l_dbus_message_builder_finalize(builder);
2772         l_dbus_message_builder_destroy(builder);
2773 }
2774
2775 bt_status_t _bt_hal_mesh_send_key_config_message(
2776                 bt_uuid_t *network, uint16_t dest,
2777                         bool is_netkey, bool is_update,
2778                                 uint16_t key_idx, uint16_t netkey_idx)
2779 {
2780         GSList *l;
2781         GSList *l1;
2782         struct key_config_request *req;
2783         meshcfg_app *app;
2784         meshcfg_el *elem;
2785         const char *key_method = (!is_netkey) ? "AddAppKey" : "AddNetKey";
2786         /* Source is Config Client Local Node */
2787         int src_elem_idx = 0;
2788         l = g_slist_find_custom(mesh_apps, network->uu, __mesh_compare_network_uuid);
2789         if (l) {
2790                 app = l->data;
2791                 if (!__bt_mesh_proxy_check(app)) {
2792                         ERR("Mesh: Proxy check failed!!");
2793                         return BT_STATUS_FAIL;
2794                 }
2795                 l1 = g_slist_find_custom(app->elements,
2796                         GUINT_TO_POINTER(src_elem_idx), __compare_element_index);
2797                 if (!l1)
2798                         return BT_STATUS_FAIL;
2799                 elem = l1->data;
2800
2801                 req = l_new(struct key_config_request, 1);
2802                 req->ele_path = elem->path;
2803                 req->dst = dest;
2804                 req->key_req_idx = key_idx;
2805                 req->idx = netkey_idx; /* Encryption Key index */
2806                 req->update_req = is_update;
2807
2808                 if (!l_dbus_proxy_method_call(app->proxy, key_method,
2809                                         __bt_hal_mesh_key_config_send, NULL,
2810                                         (void*)req, l_free))
2811                         return BT_STATUS_FAIL;
2812         } else {
2813                 ERR("Mesh: app not found!!");
2814                 return BT_STATUS_PARM_INVALID;
2815
2816         }
2817         return BT_STATUS_SUCCESS;
2818 }
2819
2820 bt_status_t _bt_hal_mesh_send_configuration_message(
2821         bt_uuid_t *network, uint16_t dest,
2822                 bool is_dev_key, uint16_t netkey_idx,
2823                         uint8_t *buf, int len)
2824 {
2825         GSList *l;
2826         GSList *l1;
2827         struct configuration_request *req;
2828         meshcfg_app *app;
2829         meshcfg_el *elem;
2830         int src_elem_idx = 0;
2831         l = g_slist_find_custom(mesh_apps, network->uu,
2832                         __mesh_compare_network_uuid);
2833         if (l) {
2834                 app = l->data;
2835                 if (!__bt_mesh_proxy_check(app)) {
2836                         ERR("Mesh: Proxy check failed!!");
2837                         return BT_STATUS_FAIL;
2838                 }
2839                 l1 = g_slist_find_custom(app->elements,
2840                                 GUINT_TO_POINTER(src_elem_idx),
2841                                 __compare_element_index);
2842                 if (!l1)
2843                         return BT_STATUS_FAIL;
2844                 elem = l1->data;
2845
2846                 req = l_new(struct configuration_request, 1);
2847                 req->ele_path = elem->path;
2848                 req->dst = dest;
2849                 req->idx = netkey_idx;
2850                 req->data = buf;
2851                 req->len = len;
2852                 req->rmt = true;
2853                 req->is_dev_key = is_dev_key;
2854
2855                 if (!l_dbus_proxy_method_call(app->proxy, "DevKeySend",
2856                                         __bt_hal_mesh_config_send, NULL,
2857                                         (void*)req, l_free))
2858                         return BT_STATUS_FAIL;
2859         } else {
2860                 ERR("Mesh: app not found!!");
2861                 return BT_STATUS_PARM_INVALID;
2862
2863         }
2864         return BT_STATUS_SUCCESS;
2865 }
2866
2867 bt_status_t _bt_hal_mesh_model_execute_message(
2868         bt_uuid_t *network, uint16_t dest,
2869         uint16_t appkey_idx, uint8_t *buf, int len)
2870 {
2871         GSList *l;
2872         GSList *l1;
2873         struct configuration_request *req;
2874         meshcfg_app *app;
2875         meshcfg_el *elem;
2876         int src_elem_idx = 0;
2877         l = g_slist_find_custom(mesh_apps, network->uu,
2878                         __mesh_compare_network_uuid);
2879         if (l) {
2880                 app = l->data;
2881                 if (!__bt_mesh_proxy_check(app)) {
2882                         ERR("Mesh: Proxy check failed!!");
2883                         return BT_STATUS_FAIL;
2884                 }
2885                 l1 = g_slist_find_custom(app->elements,
2886                                 GUINT_TO_POINTER(src_elem_idx),
2887                                 __compare_element_index);
2888                 if (!l1)
2889                         return BT_STATUS_FAIL;
2890                 elem = l1->data;
2891
2892                 req = l_new(struct configuration_request, 1);
2893                 req->ele_path = elem->path;
2894                 req->dst = dest;
2895                 req->idx = appkey_idx;
2896                 req->data = buf;
2897                 req->len = len;
2898                 req->rmt = false;
2899                 req->is_dev_key = false;
2900
2901                 if (!l_dbus_proxy_method_call(app->proxy, "Send",
2902                                         __bt_hal_mesh_model_execute_message,
2903                                         NULL, (void*)req, l_free))
2904                         return BT_STATUS_FAIL;
2905         } else {
2906                 ERR("Mesh: app not found!!");
2907                 return BT_STATUS_PARM_INVALID;
2908
2909         }
2910         return BT_STATUS_SUCCESS;
2911 }