Handle bluetooth-meshd life cycle
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / services / mesh / bt-service-mesh-main.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
5  *
6  * @author: Anupam Roy <anupam.r@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *              http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include <dirent.h>
22 #include <ftw.h>
23 #include <unistd.h>
24 #include <stdio.h>
25
26 #include <glib.h>
27 #include <dlog.h>
28 #include <limits.h>
29 #include <time.h>
30 #include <sys/time.h>
31 #include <ell/ell.h>
32 #include <actd/unit_control.h>
33
34 #include "bluetooth-api.h"
35 #include "bt-internal-types.h"
36 #include "bt-service-common.h"
37 #include "bt-service-event.h"
38 #include "bt-service-event-receiver.h"
39 #include "bt-service-util.h"
40
41 #include "bt-service-mesh-util.h"
42 #include "bt-service-mesh-network.h"
43 #include "bt-service-mesh-nodes.h"
44 #include "bt-service-mesh-config-client.h"
45
46 #include <oal-mesh.h>
47
48 #define MESH_SYSTEMD_SERVICE_NAME "bluetooth-mesh.service"
49
50 /* Event handlers */
51 static void __bt_mesh_handle_pending_request_info(int result,
52                 int service_function, void *param,
53                         unsigned int size)
54 {
55         GSList *l;
56         GArray *out_param;
57         invocation_info_t *req_info = NULL;
58
59         for (l = _bt_get_invocation_list(); l != NULL; ) {
60                 req_info = l->data;
61                 l = g_slist_next(l);
62                 if (req_info == NULL ||
63                         req_info->service_function != service_function)
64                         continue;
65
66                 switch (service_function) {
67                 case BT_MESH_NETWORK_CREATE: {
68                         char uuid_str[BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1];
69                         bluetooth_mesh_node_t *node;
70                         bluetooth_mesh_network_t *network;
71                         ret_if(param == NULL);
72                         BT_INFO("Mesh: Request: BT_MESH_NETWORK_CREATE Sender: [%s] result[%d]",
73                                         req_info->sender, result);
74
75                         node = (bluetooth_mesh_node_t*) param;
76                         network = (bluetooth_mesh_network_t*)req_info->user_data;
77
78                         _bt_mesh_util_convert_hex_to_string(
79                                 (uint8_t *) node->uuid, 16, uuid_str,
80                                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
81
82                         BT_INFO("Mesh: Network UUID from event [%s] Netw UUID from req [%s]",
83                                         uuid_str, network->uuid);
84
85                         if (!g_strcmp0(network->uuid, uuid_str)) {
86                                 BT_INFO("Mesh: BT_MESH_NETWORK_CREATE Request found uuid [%s]",
87                                         network->uuid);
88                                 if (BLUETOOTH_ERROR_NONE != _bt_mesh_network_create_cdb(
89                                         result, req_info->sender,
90                                         network->app_cred, node->uuid,
91                                                 node->token.u8, network->name.name)) {
92                                         result = BLUETOOTH_ERROR_INTERNAL;
93                                         BT_ERR("!!Mesh: BT_MESH_NETWORK_CREATE Failed!!");
94                                 }
95
96                                 _bt_mesh_util_convert_hex_to_string(
97                                         (uint8_t *) node->token.u8, 8,
98                                                 network->token.token,
99                                                 BLUETOOTH_MESH_NETWORK_TOKEN_STRING_LENGTH + 1);
100
101                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
102                                 g_array_append_vals(out_param, network,
103                                         sizeof(bluetooth_mesh_network_t));
104                                 _bt_service_method_return(req_info->context,
105                                         out_param, result);
106                                 _bt_free_info_from_invocation_list(req_info);
107                                 g_array_free(out_param, TRUE);
108                         }
109                         break;
110                 }
111                 case BT_MESH_NETWORK_LOAD: {
112                         char token_str[BLUETOOTH_MESH_NETWORK_TOKEN_STRING_LENGTH + 1];
113                         bluetooth_mesh_node_t *node;
114                         bluetooth_mesh_network_t *network;
115                         ret_if(param == NULL);
116                         BT_INFO("Mesh: Request: BT_MESH_NETWORK_LOAD Sender: [%s]",
117                                 req_info->sender);
118
119                         node = (bluetooth_mesh_node_t*) param;
120                         network = (bluetooth_mesh_network_t*)req_info->user_data;
121
122                         _bt_mesh_util_convert_hex_to_string(
123                                 (uint8_t *) node->token.u8, 8, token_str,
124                                         BLUETOOTH_MESH_NETWORK_TOKEN_STRING_LENGTH + 1);
125
126                         if (!g_strcmp0(network->token.token, token_str)) {
127                                 char *network_name = NULL;
128                                 BT_INFO("Mesh: BT_MESH_NETWORK_LOAD Request found Token [%s]",
129                                         token_str);
130
131                                 /* Send request to mesh-network to load keys and Nodes for the network */
132                                 if (BLUETOOTH_ERROR_NONE == _bt_mesh_network_load_cdb(
133                                                 result, req_info->sender, network->app_cred,
134                                                         node->uuid, node->token.u8, &network_name)) {
135                                         g_strlcpy(network->name.name, network_name, strlen(network_name));
136                                 } else
137                                         BT_ERR("!!Mesh: BT_MESH_NETWORK_LOAD Failed!!");
138
139                                 _bt_mesh_util_convert_hex_to_string((uint8_t *) node->uuid, 16, network->uuid,
140                                                 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
141
142                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
143                                 g_array_append_vals(out_param, network, sizeof(bluetooth_mesh_network_t));
144                                 _bt_service_method_return(req_info->context, out_param, result);
145                                 _bt_free_info_from_invocation_list(req_info);
146                                 g_array_free(out_param, TRUE);
147                         }
148                         break;
149                 }
150                 case BT_MESH_NETWORK_SCAN: {
151                         bluetooth_mesh_network_t *network;
152                         event_mesh_scan_status_t *event;
153                         char net_uuid[BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1];
154
155                         event = (event_mesh_scan_status_t*) param;
156                         network = (bluetooth_mesh_network_t*)req_info->user_data;
157                         _bt_mesh_util_convert_hex_to_string(
158                                 (uint8_t *) event->net_uuid.uuid, 16, net_uuid,
159                                 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
160
161                         BT_DBG("Request Sender: [%s]", req_info->sender);
162                         if (!g_strcmp0(network->uuid, net_uuid)) {
163                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
164                                 g_array_append_vals(out_param,
165                                         network, sizeof(bluetooth_mesh_network_t));
166                                 _bt_service_method_return(req_info->context,
167                                         out_param, result);
168                                 _bt_free_info_from_invocation_list(req_info);
169                                 g_array_free(out_param, TRUE);
170                         }
171                         break;
172                 }
173                 case BT_MESH_NETWORK_PROVISION_DEVICE: {
174                         bluetooth_mesh_provisioning_request_t *req_data;
175                         bluetooth_mesh_provisioning_request_t status_data;
176                         event_mesh_provisioning_status_t *event;
177                         memset(&status_data,
178                                 0x00, sizeof(bluetooth_mesh_provisioning_request_t));
179
180                         event = (event_mesh_provisioning_status_t*) param;
181                         req_data = (bluetooth_mesh_provisioning_request_t*) req_info->user_data;
182
183                         _bt_mesh_util_convert_hex_to_string(
184                                 (uint8_t *) event->net_uuid.uuid, 16, status_data.net_uuid,
185                                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
186
187                         _bt_mesh_util_convert_hex_to_string(
188                                 (uint8_t *) event->dev_uuid.uuid, 16, status_data.dev_uuid,
189                                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
190                         BT_INFO("Mesh: Provision Status: Device UUID [%s]", status_data.dev_uuid);
191                         BT_INFO("Mesh: Provision Status: Net UUID [%s]", status_data.net_uuid);
192                         BT_INFO("Mesh: Provision Status: Result [%d]", event->status);
193                         if (event->status == OAL_STATUS_SUCCESS)
194                                 BT_INFO("Mesh: Provisioning status : SUCCESS");
195                         else
196                                 BT_INFO("Mesh: Provisioning status : FAIL");
197
198                         BT_DBG("Request Sender: [%s]", req_info->sender);
199                         if (!g_strcmp0(req_data->net_uuid, status_data.net_uuid)) {
200                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
201                                 g_array_append_vals(out_param, &status_data,
202                                         sizeof(bluetooth_mesh_provisioning_request_t));
203                                 _bt_service_method_return(req_info->context, out_param, result);
204                                 _bt_free_info_from_invocation_list(req_info);
205                                 g_array_free(out_param, TRUE);
206                         }
207                         break;
208                 }
209                 /* Fall through */
210                 case BT_MESH_NETWORK_DELETE_NETKEY:
211                 case BT_MESH_NETWORK_UPDATE_NETKEY:
212                 case BT_MESH_NETWORK_ADD_NETKEY: {
213                         bluetooth_mesh_network_t *network;
214                         event_mesh_netkey_operation_t *event;
215                         char net_uuid[BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1];
216
217                         event = (event_mesh_netkey_operation_t*) param;
218                         network = (bluetooth_mesh_network_t*)req_info->user_data;
219                         _bt_mesh_util_convert_hex_to_string(
220                                 (uint8_t *) event->net_uuid.uuid, 16, net_uuid,
221                                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
222
223                         BT_DBG("Request Sender: [%s]", req_info->sender);
224                         if (!g_strcmp0(network->uuid, net_uuid)) {
225                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
226                                 g_array_append_vals(out_param, &event->key_idx, sizeof(guint16));
227                                 _bt_service_method_return(req_info->context, out_param, result);
228                                 _bt_free_info_from_invocation_list(req_info);
229                                 g_array_free(out_param, TRUE);
230                         }
231                         break;
232                 }
233                 /* Fall through */
234                 case BT_MESH_NETWORK_DELETE_APPKEY:
235                 case BT_MESH_NETWORK_UPDATE_APPKEY:
236                 case BT_MESH_NETWORK_ADD_APPKEY: {
237                         bluetooth_mesh_network_t *network;
238                         event_mesh_appkey_operation_t *event;
239                         char net_uuid[BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1];
240
241                         event = (event_mesh_appkey_operation_t*) param;
242                         network = (bluetooth_mesh_network_t*)req_info->user_data;
243                         _bt_mesh_util_convert_hex_to_string((uint8_t *) event->net_uuid.uuid, 16, net_uuid,
244                                 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
245
246                         BT_DBG("Request Sender: [%s]", req_info->sender);
247                         if (!g_strcmp0(network->uuid, net_uuid)) {
248                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
249                                 g_array_append_vals(out_param, &event->app_idx, sizeof(guint16));
250                                 _bt_service_method_return(req_info->context, out_param, result);
251                                 _bt_free_info_from_invocation_list(req_info);
252                                 g_array_free(out_param, TRUE);
253                         }
254                         break;
255                 }
256                 default:
257                         BT_DBG("Unknown function(%d)", service_function);
258                         break;
259                 }
260         }
261 }
262
263 static void __handle_mesh_network_subnet_operation_event(
264                 event_mesh_netkey_operation_t *event)
265 {
266         int result = BLUETOOTH_ERROR_NONE;
267
268         if (event->status != OAL_STATUS_SUCCESS)
269                 result = BLUETOOTH_ERROR_INTERNAL;
270
271         /* Handle DBUS Context return */
272         if (event->op == OAL_MESH_KEY_ADD) {
273                 if (result == BLUETOOTH_ERROR_NONE)
274                         _bt_mesh_network_handle_netkey_added(event->net_uuid.uuid, event->key_idx);
275
276                 __bt_mesh_handle_pending_request_info(result, BT_MESH_NETWORK_ADD_NETKEY,
277                                 event, sizeof(event_mesh_netkey_operation_t));
278         } else if (event->op == OAL_MESH_KEY_DELETE) {
279                 if (result == BLUETOOTH_ERROR_NONE)
280                         _bt_mesh_network_handle_netkey_deleted(event->net_uuid.uuid, event->key_idx);
281
282                 __bt_mesh_handle_pending_request_info(result, BT_MESH_NETWORK_DELETE_NETKEY,
283                                 event, sizeof(event_mesh_netkey_operation_t));
284         } else if (event->op == OAL_MESH_KEY_UPDATE) {
285                 _bt_mesh_network_handle_netkey_updated(event->net_uuid.uuid, event->key_idx);
286
287                 __bt_mesh_handle_pending_request_info(result, BT_MESH_NETWORK_UPDATE_NETKEY,
288                                 event, sizeof(event_mesh_netkey_operation_t));
289         }
290 }
291
292 static void __handle_mesh_network_appkey_operation_event(
293                         event_mesh_appkey_operation_t *event)
294 {
295         int result = BLUETOOTH_ERROR_NONE;
296
297         if (event->status != OAL_STATUS_SUCCESS)
298                 result = BLUETOOTH_ERROR_INTERNAL;
299         /* Handle DBUS Context return */
300         if (event->op == OAL_MESH_KEY_ADD) {
301                 BT_INFO("Mesh: Appkey Add event");
302                 if (result == BLUETOOTH_ERROR_NONE)
303                         _bt_mesh_network_handle_appkey_added(
304                                         event->net_uuid.uuid, event->net_idx, event->app_idx);
305
306                 __bt_mesh_handle_pending_request_info(result, BT_MESH_NETWORK_ADD_APPKEY,
307                                 event, sizeof(event_mesh_netkey_operation_t));
308         } else if (event->op == OAL_MESH_KEY_DELETE) {
309                 BT_INFO("Mesh: Appkey Delete event");
310                 if (result == BLUETOOTH_ERROR_NONE)
311                         _bt_mesh_network_handle_appkey_deleted(
312                                         event->net_uuid.uuid, event->net_idx, event->app_idx);
313
314                 __bt_mesh_handle_pending_request_info(result, BT_MESH_NETWORK_DELETE_APPKEY,
315                                 event, sizeof(event_mesh_netkey_operation_t));
316         } else if (event->op == OAL_MESH_KEY_UPDATE) {
317                 BT_INFO("Mesh: Appkey Update event");
318                 __bt_mesh_handle_pending_request_info(result, BT_MESH_NETWORK_UPDATE_APPKEY,
319                                 event, sizeof(event_mesh_netkey_operation_t));
320         }
321 }
322
323 static void  __handle_mesh_devkey_message_received_event(
324                 event_mesh_devkey_message_t *event)
325 {
326         _bt_mesh_config_client_devkey_msg_handler(event);
327 }
328
329 static void __handle_mesh_network_attached_event(
330                 event_mesh_network_attached_t *event)
331 {
332         int result = BLUETOOTH_ERROR_NONE;
333         bluetooth_mesh_node_t node;
334
335         if (event->status != OAL_STATUS_SUCCESS)
336                 result = BLUETOOTH_ERROR_INTERNAL;
337
338         memset(&node, 0x00, sizeof(bluetooth_mesh_node_t));
339
340         memcpy(node.uuid, event->uuid.uuid, 16);
341         memcpy(node.token.u8, event->token, 8);
342
343         __bt_mesh_handle_pending_request_info(result,
344                         BT_MESH_NETWORK_CREATE,
345                         &node, sizeof(bluetooth_mesh_node_t));
346         __bt_mesh_handle_pending_request_info(result,
347                         BT_MESH_NETWORK_LOAD,
348                         &node, sizeof(bluetooth_mesh_node_t));
349 }
350
351 static void __handle_mesh_network_scan_started_event(
352         event_mesh_scan_status_t *event)
353 {
354         GVariant *out_var = NULL, *param = NULL;
355         GArray *info = NULL;
356         bluetooth_mesh_network_t network;
357
358         int result = BLUETOOTH_ERROR_NONE;
359         if (event->status != OAL_STATUS_SUCCESS) {
360                 result = BLUETOOTH_ERROR_INTERNAL;
361                 _bt_mesh_set_scanning_state(false);
362         } else
363                 _bt_mesh_set_scanning_state(true);
364
365         /* Handle DBUS Context return */
366         __bt_mesh_handle_pending_request_info(result,
367                         BT_MESH_NETWORK_SCAN,
368                         event, sizeof(event_mesh_scan_status_t));
369
370         /* Handle Scan started event */
371         if (result == BLUETOOTH_ERROR_NONE) {
372                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
373                 _bt_mesh_util_convert_hex_to_string(
374                         (uint8_t *) event->net_uuid.uuid, 16, network.uuid,
375                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
376
377                 info = g_array_new(FALSE, FALSE, sizeof(gchar));
378                 g_array_append_vals(info, &network,
379                         sizeof(bluetooth_mesh_network_t));
380
381                 out_var = g_variant_new_from_data((const GVariantType *)"ay",
382                         info->data, info->len,
383                         TRUE, NULL, NULL);
384
385                 param = g_variant_new("(iv)", result, out_var);
386                 _bt_send_event(BT_MESH_EVENT,
387                         BLUETOOTH_EVENT_MESH_SCAN_STARTED,
388                         param);
389         }
390 }
391
392 static void __handle_mesh_network_scan_finished_event(
393         event_mesh_scan_status_t *event)
394 {
395         GVariant *out_var = NULL, *param = NULL;
396         GArray *info = NULL;
397         bluetooth_mesh_network_t network;
398         int result = BLUETOOTH_ERROR_NONE;
399         if (event->status != OAL_STATUS_SUCCESS) {
400                 BT_INFO("Mesh: Scan Finished: status:: FAILED!");
401                 result = BLUETOOTH_ERROR_INTERNAL;
402         } else
403                 BT_INFO("Mesh: Scan Finished: status:: SUCCESS!");
404
405         /* Handle Scan finsihed event */
406         if (result == BLUETOOTH_ERROR_NONE) {
407                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
408                 _bt_mesh_util_convert_hex_to_string(
409                         (uint8_t *) event->net_uuid.uuid, 16, network.uuid,
410                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
411
412                 info = g_array_new(FALSE, FALSE, sizeof(gchar));
413                 g_array_append_vals(info, &network,
414                         sizeof(bluetooth_mesh_network_t));
415
416                 out_var = g_variant_new_from_data((const GVariantType *)"ay",
417                         info->data, info->len,
418                         TRUE, NULL, NULL);
419
420                 param = g_variant_new("(iv)", result, out_var);
421                 _bt_send_event(BT_MESH_EVENT,
422                         BLUETOOTH_EVENT_MESH_SCAN_FINISHED,
423                         param);
424                 _bt_mesh_set_scanning_state(false);
425         }
426 }
427
428 static void __handle_mesh_network_provisioning_started_event(
429                 event_mesh_provisioning_status_t *status)
430 {
431         int result = BLUETOOTH_ERROR_NONE;
432         BT_INFO("Mesh: Provisioning started");
433
434         __bt_mesh_handle_pending_request_info(result,
435                         BT_MESH_NETWORK_PROVISION_DEVICE,
436                         status, sizeof(event_mesh_provisioning_status_t));
437
438         _bt_mesh_set_provisioning_state(true);
439 }
440
441 static void __handle_mesh_network_provisioning_failed_event(
442                 event_mesh_provisioning_status_t *status)
443 {
444         int result = BLUETOOTH_ERROR_INTERNAL;
445         BT_INFO("Mesh: Provisioning failed!!");
446         __bt_mesh_handle_pending_request_info(result,
447                         BT_MESH_NETWORK_PROVISION_DEVICE,
448                         status, sizeof(event_mesh_provisioning_status_t));
449
450         _bt_mesh_set_provisioning_state(false);
451 }
452
453 static void __handle_mesh_network_provisioning_finished_event(
454                 event_mesh_provisioning_finished_t *event)
455 {
456         GVariant *out_var = NULL, *param = NULL;
457         GArray *info = NULL;
458         bluetooth_mesh_provisioning_result_t prov_result;
459         BT_INFO("Mesh: Provisioning Finished!");
460
461         memset(&prov_result, 0x00,
462                         sizeof(bluetooth_mesh_provisioning_result_t));
463
464         if (event->status != OAL_STATUS_SUCCESS)
465                 prov_result.result = BLUETOOTH_ERROR_INTERNAL;
466         else
467                 prov_result.result = BLUETOOTH_ERROR_NONE;
468
469         prov_result.reason = event->reason;
470         prov_result.unicast = event->unicast;
471         prov_result.count = event->count;
472
473         _bt_mesh_util_convert_hex_to_string(
474                         (uint8_t *) event->net_uuid.uuid, 16, prov_result.net_uuid,
475                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
476
477         _bt_mesh_util_convert_hex_to_string(
478                         (uint8_t *) event->dev_uuid.uuid, 16, prov_result.dev_uuid,
479                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
480
481         info = g_array_new(FALSE, FALSE, sizeof(gchar));
482         g_array_append_vals(info, &prov_result,
483                         sizeof(bluetooth_mesh_provisioning_result_t));
484
485         out_var = g_variant_new_from_data((const GVariantType *)"ay",
486                         info->data, info->len,
487                         TRUE, NULL, NULL);
488
489         param = g_variant_new("(iv)", prov_result.result, out_var);
490         _bt_send_event(BT_MESH_EVENT,
491                         BLUETOOTH_EVENT_MESH_PROVISIONING_FINISHED,
492                         param);
493
494         /* Add Remote Node entry in Local CDB */
495         if (event->status == OAL_STATUS_SUCCESS) {
496                 BT_INFO("Mesh: Provisioning done, add node to Network");
497                 BT_INFO("Mesh: Node UUID [%s]", prov_result.dev_uuid);
498                 BT_INFO("Mesh: Node Unicast[0x%2.2x] Element Count [%d]",
499                         event->unicast, event->count);
500
501                 _bt_mesh_network_add_remote_node(
502                         event->net_uuid.uuid, event->dev_uuid.uuid,
503                                 event->unicast, event->count);
504         }
505         /* Unset provisioning state */
506         _bt_mesh_set_provisioning_state(false);
507 }
508
509 static void __handle_mesh_network_provisioning_data_requested_event(
510         event_mesh_provisioning_data_requested_t *event)
511 {
512         _bt_mesh_network_request_provisioning_data_request(
513                         event->net_uuid.uuid, event->count);
514 }
515
516 static void __handle_mesh_network_authentication_requested_event(
517                 event_mesh_authentication_requested_t *event)
518 {
519         GVariant *out_var = NULL, *param = NULL;
520         GArray *info = NULL;
521         bluetooth_mesh_authentication_request_t auth_req;
522
523         memset(&auth_req, 0x00,
524                         sizeof(bluetooth_mesh_authentication_request_t));
525
526         auth_req.auth_type = event->auth_type;
527         g_strlcpy(auth_req.auth_value, event->auth_value,
528                         sizeof(auth_req.auth_value));
529
530         _bt_mesh_util_convert_hex_to_string(
531                         (uint8_t *) event->net_uuid.uuid, 16, auth_req.net_uuid,
532                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
533
534         BT_INFO("Mesh: Authentication Requested by Device: Network [%s]",
535                 auth_req.net_uuid);
536         info = g_array_new(FALSE, FALSE, sizeof(gchar));
537         g_array_append_vals(info, &auth_req,
538                         sizeof(bluetooth_mesh_authentication_request_t));
539
540         out_var = g_variant_new_from_data((const GVariantType *)"ay",
541                         info->data, info->len,
542                         TRUE, NULL, NULL);
543
544         param = g_variant_new("(iv)", BLUETOOTH_ERROR_NONE, out_var);
545         _bt_send_event(BT_MESH_EVENT,
546                         BLUETOOTH_EVENT_MESH_AUTHENTICATION_REQUEST,
547                         param);
548 }
549
550 static void __handle_mesh_network_scan_result_event(
551                 event_mesh_scan_result_t *event)
552 {
553         GVariant *out_var = NULL, *param = NULL;
554         GArray *info = NULL;
555         bluetooth_mesh_scan_result_t data;
556         int result = BLUETOOTH_ERROR_NONE;
557
558         memset(&data, 0x00, sizeof(bluetooth_mesh_scan_result_t));
559
560         /* Fill Network UUID */
561         _bt_mesh_util_convert_hex_to_string(
562                 (uint8_t *) event->net_uuid.uuid, 16, data.net_uuid,
563                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
564
565         /* Fill Device UUID */
566         _bt_mesh_util_convert_hex_to_string(
567                 (uint8_t *) event->result.dev_uuid.uuid, 16, data.dev_uuid,
568                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
569
570         /* Fill RSSI */
571         data.rssi = event->result.rssi;
572
573         /* Fill OOB Info */
574         memcpy(&data.oob_info, event->result.oob_info, 2);
575
576         /* Fill URI Hash Info */
577         memcpy(&data.uri_hash, event->result.uri_hash, 4);
578
579         if (event->status != OAL_STATUS_SUCCESS)
580                 result = BLUETOOTH_ERROR_INTERNAL;
581
582         /* Fill Data */
583         info = g_array_new(FALSE, FALSE, sizeof(gchar));
584         g_array_append_vals(info, &data,
585                 sizeof(bluetooth_mesh_scan_result_t));
586
587         out_var = g_variant_new_from_data((const GVariantType *)"ay",
588                         info->data, info->len,
589                         TRUE, NULL, NULL);
590
591         param = g_variant_new("(iv)", result, out_var);
592         _bt_send_event(BT_MESH_EVENT,
593                         BLUETOOTH_EVENT_MESH_SCAN_RESULT,
594                         param);
595 }
596
597 static void __handle_mesh_events(int event_type,
598                 gpointer event_data)
599 {
600         BT_INFO("Mesh: Got Mesh event!!! event type [%d]", event_type);
601         if (event_type == OAL_EVENT_MESH_PROVISIONING_STARTED)
602                 BT_INFO("Mesh: Provisioning started event");
603
604         switch (event_type) {
605         case OAL_EVENT_MESH_NETWORK_ATTACHED:
606                 __handle_mesh_network_attached_event(
607                         (event_mesh_network_attached_t*)event_data);
608                 BT_PERMANENT_LOG("Mesh: Network attached!!");
609                 break;
610         case OAL_EVENT_MESH_SCAN_STARTED:
611                 __handle_mesh_network_scan_started_event(
612                         (event_mesh_scan_status_t*)event_data);
613                 BT_PERMANENT_LOG("Mesh: Network Scan Stated!!");
614                 break;
615         case OAL_EVENT_MESH_SCAN_FINISHED:
616                 __handle_mesh_network_scan_finished_event(
617                         (event_mesh_scan_status_t*)event_data);
618                 BT_PERMANENT_LOG("Mesh: Network Scan Finished!!");
619                 break;
620         case OAL_EVENT_MESH_SCAN_RESULT:
621                 __handle_mesh_network_scan_result_event(
622                         (event_mesh_scan_result_t*)event_data);
623                 BT_PERMANENT_LOG("Mesh: Network Scan Result!!");
624                 break;
625         case OAL_EVENT_MESH_PROVISIONING_STARTED:
626                 BT_INFO("Mesh: Network Provisioning Started");
627                 __handle_mesh_network_provisioning_started_event(
628                         (event_mesh_provisioning_status_t*)event_data);
629                 BT_PERMANENT_LOG("Mesh: Network Provisioning Started");
630                 break;
631         case OAL_EVENT_MESH_PROVISIONING_FAILED:
632                 BT_INFO("Mesh: Network Provisioning Failed!!!!");
633                 __handle_mesh_network_provisioning_failed_event(
634                         (event_mesh_provisioning_status_t*)event_data);
635                 BT_PERMANENT_LOG("Mesh: Network Provisioning Failed");
636                 break;
637         case OAL_EVENT_MESH_PROVISIONING_FINISHED:
638                 __handle_mesh_network_provisioning_finished_event(
639                         (event_mesh_provisioning_finished_t*)event_data);
640                 BT_PERMANENT_LOG("Mesh: Network Provisioning Finished");
641                 break;
642         case OAL_EVENT_MESH_PROVISIONING_DATA_REQUESTED:
643                 __handle_mesh_network_provisioning_data_requested_event(
644                         (event_mesh_provisioning_data_requested_t*)event_data);
645                 BT_PERMANENT_LOG("Mesh: Network Provisioning Data Requested");
646                 break;
647         case OAL_EVENT_MESH_AUTHENTICATION_REQUESTED:
648                 __handle_mesh_network_authentication_requested_event(
649                         (event_mesh_authentication_requested_t*)event_data);
650                 BT_PERMANENT_LOG("Mesh: Network Authentication Requested");
651                 break;
652         case OAL_EVENT_MESH_NETKEY_EXECUTE_EVENT:
653                 __handle_mesh_network_subnet_operation_event(
654                         (event_mesh_netkey_operation_t*)event_data);
655                 BT_PERMANENT_LOG("Mesh: Network Subnet operation event");
656                 break;
657         case OAL_EVENT_MESH_APPKEY_EXECUTE_EVENT:
658                  __handle_mesh_network_appkey_operation_event(
659                         (event_mesh_appkey_operation_t*)event_data);
660                 BT_PERMANENT_LOG("Mesh: AppKey operation event");
661                 break;
662         case OAL_EVENT_MESH_DEVKEY_MESSAGE_RECEIVED:
663                 __handle_mesh_devkey_message_received_event(
664                         (event_mesh_devkey_message_t*)event_data);
665                 BT_PERMANENT_LOG("Mesh: DevKey Message Received event");
666                 break;
667         default:
668         break;
669         }
670 }
671
672 int _bt_mesh_init(void)
673 {
674         int ret = UNIT_CONTROL_OK;
675         oal_status_t status = OAL_STATUS_SUCCESS;
676
677         /* Launch bluetooth-meshd */
678         ret = actd_start_unit(UNIT_CONTROL_BUS_TYPE_SYSTEM,
679                                 MESH_SYSTEMD_SERVICE_NAME, 5000);
680
681         if (ret != UNIT_CONTROL_OK) {
682                 BT_ERR("Failed to call systemact service: %d", ret);
683                 return BLUETOOTH_ERROR_INTERNAL;
684         }
685
686         status = mesh_enable();
687         if (OAL_STATUS_SUCCESS != status) {
688                 BT_ERR("Mesh: Failed to initialize Mesh profile, status: %d",
689                                 status);
690                 return BLUETOOTH_ERROR_INTERNAL;
691         }
692         BT_INFO("Mesh: Stack Initialization Done successfully");
693
694         /* Register MESH event handler */
695         _bt_service_register_event_handler_callback(BT_MESH_MODULE,
696                         __handle_mesh_events);
697
698         return BLUETOOTH_ERROR_NONE;
699 }
700
701 int _bt_mesh_deinit(void)
702 {
703         oal_status_t status = OAL_STATUS_SUCCESS;
704         int ret = UNIT_CONTROL_OK;
705
706         /* Terminate bluetooth-meshd */
707         ret = actd_stop_unit(UNIT_CONTROL_BUS_TYPE_SYSTEM,
708                         MESH_SYSTEMD_SERVICE_NAME, 5000);
709
710         if (ret != UNIT_CONTROL_OK)
711                 BT_ERR("Failed to call systemact service: %d", ret);
712
713         status = mesh_disable();
714         if (OAL_STATUS_SUCCESS != status) {
715                 BT_ERR("Mesh: Failed to de-initialize Mesh profile, status: %d",
716                                 status);
717                 return BLUETOOTH_ERROR_INTERNAL;
718         }
719
720         /* Register AVRCP target event handler */
721         _bt_service_unregister_event_handler_callback(BT_MESH_MODULE);
722
723         return BLUETOOTH_ERROR_NONE;
724 }