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