Mesh: Add Stack event handlers
[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
126                                 BT_INFO("Mesh: BT_MESH_NETWORK_LOAD Request found Token [%s]",
127                                         token_str);
128
129                                 /* TODO: Handle Meh Network Load CDB */
130                                 /* Send request to mesh-network to load keys and Nodes for the network */
131                         }
132                         break;
133                 }
134                 case BT_MESH_NETWORK_SCAN: {
135                         bluetooth_mesh_network_t *network;
136                         event_mesh_scan_status_t *event;
137                         char net_uuid[BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1];
138
139                         event = (event_mesh_scan_status_t*) param;
140                         network = (bluetooth_mesh_network_t*)req_info->user_data;
141                         _bt_mesh_util_convert_hex_to_string(
142                                 (uint8_t *) event->net_uuid.uuid, 16, net_uuid,
143                                 BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
144
145                         BT_DBG("Request Sender: [%s]", req_info->sender);
146                         if (!g_strcmp0(network->uuid, net_uuid)) {
147                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
148                                 g_array_append_vals(out_param,
149                                         network, sizeof(bluetooth_mesh_network_t));
150                                 _bt_service_method_return(req_info->context,
151                                         out_param, result);
152                                 _bt_free_info_from_invocation_list(req_info);
153                                 g_free(req_info->user_data);
154                                 g_array_free(out_param, TRUE);
155                         }
156                         break;
157                 }
158                 case BT_MESH_NETWORK_PROVISION_DEVICE: {
159                         bluetooth_mesh_provisioning_request_t *req_data;
160                         bluetooth_mesh_provisioning_request_t status_data;
161                         event_mesh_provisioning_status_t *event;
162                         memset(&status_data,
163                                 0x00, sizeof(bluetooth_mesh_provisioning_request_t));
164
165                         event = (event_mesh_provisioning_status_t*) param;
166                         req_data = (bluetooth_mesh_provisioning_request_t*) req_info->user_data;
167
168                         _bt_mesh_util_convert_hex_to_string(
169                                 (uint8_t *) event->net_uuid.uuid, 16, status_data.net_uuid,
170                                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
171
172                         _bt_mesh_util_convert_hex_to_string(
173                                 (uint8_t *) event->dev_uuid.uuid, 16, status_data.dev_uuid,
174                                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
175
176                         BT_DBG("Request Sender: [%s]", req_info->sender);
177                         if (!g_strcmp0(req_data->net_uuid, status_data.net_uuid)) {
178                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
179                                 g_array_append_vals(out_param, &status_data,
180                                         sizeof(bluetooth_mesh_provisioning_request_t));
181                                 _bt_service_method_return(req_info->context, out_param, result);
182                                 _bt_free_info_from_invocation_list(req_info);
183                                 g_free(req_info->user_data);
184                                 g_array_free(out_param, TRUE);
185                         }
186                         break;
187                 }
188                 /* Fall through */
189                 case BT_MESH_NETWORK_DELETE_NETKEY:
190                 case BT_MESH_NETWORK_UPDATE_NETKEY:
191                 case BT_MESH_NETWORK_ADD_NETKEY: {
192                         bluetooth_mesh_network_t *network;
193                         event_mesh_netkey_operation_t *event;
194                         char net_uuid[BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1];
195
196                         event = (event_mesh_netkey_operation_t*) param;
197                         network = (bluetooth_mesh_network_t*)req_info->user_data;
198                         _bt_mesh_util_convert_hex_to_string(
199                                 (uint8_t *) event->net_uuid.uuid, 16, net_uuid,
200                                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
201
202                         BT_DBG("Request Sender: [%s]", req_info->sender);
203                         if (!g_strcmp0(network->uuid, net_uuid)) {
204                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
205                                 g_array_append_vals(out_param, &event->key_idx, sizeof(guint16));
206                                 _bt_service_method_return(req_info->context, out_param, result);
207                                 _bt_free_info_from_invocation_list(req_info);
208                                 g_free(req_info->user_data);
209                                 g_array_free(out_param, TRUE);
210                         }
211                         break;
212                 }
213                 /* Fall through */
214                 case BT_MESH_NETWORK_DELETE_APPKEY:
215                 case BT_MESH_NETWORK_UPDATE_APPKEY:
216                 case BT_MESH_NETWORK_ADD_APPKEY: {
217                         bluetooth_mesh_network_t *network;
218                         event_mesh_appkey_operation_t *event;
219                         char net_uuid[BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1];
220
221                         event = (event_mesh_appkey_operation_t*) param;
222                         network = (bluetooth_mesh_network_t*)req_info->user_data;
223                         _bt_mesh_util_convert_hex_to_string(
224                                 (uint8_t *) event->net_uuid.uuid, 16, net_uuid,
225                                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
226
227                         BT_DBG("Request Sender: [%s]", req_info->sender);
228                         if (!g_strcmp0(network->uuid, net_uuid)) {
229                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
230                                 g_array_append_vals(out_param,
231                                         &event->app_idx, sizeof(guint16));
232                                 _bt_service_method_return(req_info->context, out_param, result);
233                                 _bt_free_info_from_invocation_list(req_info);
234                                 g_free(req_info->user_data);
235                                 g_array_free(out_param, TRUE);
236                         }
237                         break;
238                 }
239                 default:
240                         BT_DBG("Unknown function(%d)", service_function);
241                         break;
242                 }
243         }
244 }
245
246 static void  __handle_mesh_devkey_message_received_event(
247                 event_mesh_devkey_message_t *event)
248 {
249         /* TODO Handle Devkey message Handler */
250 }
251
252 static void __handle_mesh_network_attached_event(
253                 event_mesh_network_attached_t *event)
254 {
255         int result = BLUETOOTH_ERROR_NONE;
256         bluetooth_mesh_node_t node;
257
258         if (event->status != OAL_STATUS_SUCCESS)
259                 result = BLUETOOTH_ERROR_INTERNAL;
260
261         memset(&node, 0x00, sizeof(bluetooth_mesh_node_t));
262
263         memcpy(node.uuid, event->uuid.uuid, 16);
264         memcpy(node.token.u8, event->token, 8);
265
266         __bt_mesh_handle_pending_request_info(result,
267                         BT_MESH_NETWORK_CREATE,
268                         &node, sizeof(bluetooth_mesh_node_t));
269         __bt_mesh_handle_pending_request_info(result,
270                         BT_MESH_NETWORK_LOAD,
271                         &node, sizeof(bluetooth_mesh_node_t));
272 }
273
274 static void __handle_mesh_network_scan_started_event(
275         event_mesh_scan_status_t *event)
276 {
277         GVariant *out_var = NULL, *param = NULL;
278         GArray *info = NULL;
279         bluetooth_mesh_network_t network;
280
281         int result = BLUETOOTH_ERROR_NONE;
282         if (event->status != OAL_STATUS_SUCCESS) {
283                 result = BLUETOOTH_ERROR_INTERNAL;
284                 _bt_mesh_set_scanning_state(false);
285         } else
286                 _bt_mesh_set_scanning_state(true);
287
288         /* Handle DBUS Context return */
289         __bt_mesh_handle_pending_request_info(result,
290                         BT_MESH_NETWORK_SCAN,
291                         event, sizeof(event_mesh_scan_status_t));
292
293         /* Handle Scan started event */
294         if (result == BLUETOOTH_ERROR_NONE) {
295                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
296                 _bt_mesh_util_convert_hex_to_string(
297                         (uint8_t *) event->net_uuid.uuid, 16, network.uuid,
298                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
299
300                 info = g_array_new(FALSE, FALSE, sizeof(gchar));
301                 g_array_append_vals(info, &network,
302                         sizeof(bluetooth_mesh_network_t));
303
304                 out_var = g_variant_new_from_data((const GVariantType *)"ay",
305                         info->data, info->len,
306                         TRUE, NULL, NULL);
307
308                 param = g_variant_new("(iv)", result, out_var);
309                 _bt_send_event(BT_MESH_EVENT,
310                         BLUETOOTH_EVENT_MESH_SCAN_STARTED,
311                         param);
312         }
313 }
314
315 static void __handle_mesh_network_scan_finished_event(
316         event_mesh_scan_status_t *event)
317 {
318         GVariant *out_var = NULL, *param = NULL;
319         GArray *info = NULL;
320         bluetooth_mesh_network_t network;
321
322         int result = BLUETOOTH_ERROR_NONE;
323         if (event->status != OAL_STATUS_SUCCESS)
324                 result = BLUETOOTH_ERROR_INTERNAL;
325
326
327         /* Handle Scan finsihed event */
328         if (result == BLUETOOTH_ERROR_NONE) {
329                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
330                 _bt_mesh_util_convert_hex_to_string(
331                         (uint8_t *) event->net_uuid.uuid, 16, network.uuid,
332                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
333
334                 info = g_array_new(FALSE, FALSE, sizeof(gchar));
335                 g_array_append_vals(info, &network,
336                         sizeof(bluetooth_mesh_network_t));
337
338                 out_var = g_variant_new_from_data((const GVariantType *)"ay",
339                         info->data, info->len,
340                         TRUE, NULL, NULL);
341
342                 param = g_variant_new("(iv)", result, out_var);
343                 _bt_send_event(BT_MESH_EVENT,
344                         BLUETOOTH_EVENT_MESH_SCAN_FINISHED,
345                         param);
346                 _bt_mesh_set_scanning_state(false);
347         }
348 }
349
350 static void __handle_mesh_network_provisioning_started_event(
351                 event_mesh_provisioning_status_t *status)
352 {
353         int result = BLUETOOTH_ERROR_NONE;
354
355         __bt_mesh_handle_pending_request_info(result,
356                         BT_MESH_NETWORK_PROVISION_DEVICE,
357                         status, sizeof(event_mesh_scan_status_t));
358
359         _bt_mesh_set_provisioning_state(true);
360 }
361
362 static void __handle_mesh_network_provisioning_failed_event(
363                 event_mesh_provisioning_status_t *status)
364 {
365         int result = BLUETOOTH_ERROR_INTERNAL;
366
367         __bt_mesh_handle_pending_request_info(result,
368                         BT_MESH_NETWORK_PROVISION_DEVICE,
369                         status, sizeof(event_mesh_scan_status_t));
370
371         _bt_mesh_set_provisioning_state(false);
372 }
373
374 static void __handle_mesh_network_provisioning_finished_event(
375                 event_mesh_provisioning_finished_t *event)
376 {
377         GVariant *out_var = NULL, *param = NULL;
378         GArray *info = NULL;
379         bluetooth_mesh_provisioning_result_t prov_result;
380
381         memset(&prov_result, 0x00,
382                         sizeof(bluetooth_mesh_provisioning_result_t));
383
384         if (event->status != OAL_STATUS_SUCCESS)
385                 prov_result.result = BLUETOOTH_ERROR_INTERNAL;
386         prov_result.result = BLUETOOTH_ERROR_NONE;
387
388         prov_result.reason = event->reason;
389         prov_result.unicast = event->unicast;
390         prov_result.count = event->count;
391
392         _bt_mesh_util_convert_hex_to_string(
393                         (uint8_t *) event->net_uuid.uuid, 16, prov_result.net_uuid,
394                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
395
396         _bt_mesh_util_convert_hex_to_string(
397                         (uint8_t *) event->dev_uuid.uuid, 16, prov_result.dev_uuid,
398                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
399
400         info = g_array_new(FALSE, FALSE, sizeof(gchar));
401         g_array_append_vals(info, &prov_result,
402                         sizeof(bluetooth_mesh_provisioning_result_t));
403
404         out_var = g_variant_new_from_data((const GVariantType *)"ay",
405                         info->data, info->len,
406                         TRUE, NULL, NULL);
407
408         param = g_variant_new("(iv)", prov_result.result, out_var);
409         _bt_send_event(BT_MESH_EVENT,
410                         BLUETOOTH_EVENT_MESH_PROVISIONING_FINISHED,
411                         param);
412
413         /* TODO Add newly provisioned Node info in Node Manager */
414         /* Add Remote Node entry in Local CDB */
415 }
416
417 static void __handle_mesh_network_provisioning_data_requested_event(
418         event_mesh_provisioning_data_requested_t *event)
419 {
420         _bt_mesh_network_request_provisioning_data_request(
421                         event->net_uuid.uuid, event->count);
422 }
423
424 static void __handle_mesh_network_authentication_requested_event(
425                 event_mesh_authentication_requested_t *event)
426 {
427         GVariant *out_var = NULL, *param = NULL;
428         GArray *info = NULL;
429         bluetooth_mesh_authentication_request_t auth_req;
430
431         memset(&auth_req, 0x00,
432                         sizeof(bluetooth_mesh_authentication_request_t));
433
434         auth_req.auth_type = event->auth_type;
435         g_strlcpy(auth_req.auth_value, event->auth_value,
436                         sizeof(auth_req.auth_value));
437
438         _bt_mesh_util_convert_hex_to_string(
439                         (uint8_t *) event->net_uuid.uuid, 16, auth_req.net_uuid,
440                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
441
442         info = g_array_new(FALSE, FALSE, sizeof(gchar));
443         g_array_append_vals(info, &auth_req,
444                         sizeof(bluetooth_mesh_authentication_request_t));
445
446         out_var = g_variant_new_from_data((const GVariantType *)"ay",
447                         info->data, info->len,
448                         TRUE, NULL, NULL);
449
450         param = g_variant_new("(iv)", BLUETOOTH_ERROR_NONE, out_var);
451         _bt_send_event(BT_MESH_EVENT,
452                         BLUETOOTH_EVENT_MESH_AUTHENTICATION_REQUEST,
453                         param);
454 }
455
456 static void __handle_mesh_network_scan_result_event(
457                 event_mesh_scan_result_t *event)
458 {
459         GVariant *out_var = NULL, *param = NULL;
460         GArray *info = NULL;
461         bluetooth_mesh_scan_result_t data;
462         int result = BLUETOOTH_ERROR_NONE;
463
464         memset(&data, 0x00, sizeof(bluetooth_mesh_scan_result_t));
465
466         /* Fill Network UUID */
467         _bt_mesh_util_convert_hex_to_string(
468                 (uint8_t *) event->net_uuid.uuid, 16, data.net_uuid,
469                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
470
471         /* Fill Device UUID */
472         _bt_mesh_util_convert_hex_to_string(
473                 (uint8_t *) event->result.dev_uuid.uuid, 16, data.dev_uuid,
474                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
475
476         /* Fill RSSI */
477         data.rssi = event->result.rssi;
478
479         /* Fill OOB Info */
480         memcpy(&data.oob_info, event->result.oob_info, 2);
481
482         /* Fill URI Hash Info */
483         memcpy(&data.uri_hash, event->result.uri_hash, 4);
484
485         if (event->status != OAL_STATUS_SUCCESS)
486                 result = BLUETOOTH_ERROR_INTERNAL;
487
488         /* Fill Data */
489         info = g_array_new(FALSE, FALSE, sizeof(gchar));
490         g_array_append_vals(info, &data,
491                 sizeof(bluetooth_mesh_scan_result_t));
492
493         out_var = g_variant_new_from_data((const GVariantType *)"ay",
494                         info->data, info->len,
495                         TRUE, NULL, NULL);
496
497         param = g_variant_new("(iv)", result, out_var);
498         _bt_send_event(BT_MESH_EVENT,
499                         BLUETOOTH_EVENT_MESH_SCAN_RESULT,
500                         param);
501 }
502
503 static void __handle_mesh_events(int event_type,
504                 gpointer event_data)
505 {
506         switch (event_type) {
507         case OAL_EVENT_MESH_NETWORK_ATTACHED:
508                 __handle_mesh_network_attached_event(
509                         (event_mesh_network_attached_t*)event_data);
510                 BT_PERMANENT_LOG("Mesh: Network attached!!");
511                 break;
512         case OAL_EVENT_MESH_SCAN_STARTED:
513                 __handle_mesh_network_scan_started_event(
514                         (event_mesh_scan_status_t*)event_data);
515                 BT_PERMANENT_LOG("Mesh: Network Scan Stated!!");
516                 break;
517         case OAL_EVENT_MESH_SCAN_FINISHED:
518                 __handle_mesh_network_scan_finished_event(
519                         (event_mesh_scan_status_t*)event_data);
520                 BT_PERMANENT_LOG("Mesh: Network Scan Finished!!");
521                 break;
522         case OAL_EVENT_MESH_SCAN_RESULT:
523                 __handle_mesh_network_scan_result_event(
524                         (event_mesh_scan_result_t*)event_data);
525                 BT_PERMANENT_LOG("Mesh: Network Scan Result!!");
526                 break;
527         case OAL_EVENT_MESH_PROVISIONING_STARTED:
528                 __handle_mesh_network_provisioning_started_event(
529                         (event_mesh_provisioning_status_t*)event_data);
530                 BT_PERMANENT_LOG("Mesh: Network Provisioning Started");
531         break;
532         case OAL_EVENT_MESH_PROVISIONING_FAILED:
533                 __handle_mesh_network_provisioning_failed_event(
534                         (event_mesh_provisioning_status_t*)event_data);
535                 BT_PERMANENT_LOG("Mesh: Network Provisioning Failed");
536                 break;
537         case OAL_EVENT_MESH_PROVISIONING_FINISHED:
538                 __handle_mesh_network_provisioning_finished_event(
539                         (event_mesh_provisioning_finished_t*)event_data);
540                 BT_PERMANENT_LOG("Mesh: Network Provisioning Finished");
541                 break;
542         case OAL_EVENT_MESH_PROVISIONING_DATA_REQUESTED:
543                 __handle_mesh_network_provisioning_data_requested_event(
544                         (event_mesh_provisioning_data_requested_t*)event_data);
545                 BT_PERMANENT_LOG("Mesh: Network Provisioning Data Requested");
546                 break;
547         case OAL_EVENT_MESH_AUTHENTICATION_REQUESTED:
548                 __handle_mesh_network_authentication_requested_event(
549                         (event_mesh_authentication_requested_t*)event_data);
550                 BT_PERMANENT_LOG("Mesh: Network Authentication Requested");
551                 break;
552         case OAL_EVENT_MESH_NETKEY_EXECUTE_EVENT:
553                 /* TODO: Handle Netkey key DBUS Handler & event */
554                 BT_PERMANENT_LOG("Mesh: Network Subnet operation event");
555                 break;
556         case OAL_EVENT_MESH_APPKEY_EXECUTE_EVENT:
557                 /* TODO: Handle Netkey key DBUS Handler & event */
558                 BT_PERMANENT_LOG("Mesh: AppKey operation event");
559                 break;
560         case OAL_EVENT_MESH_DEVKEY_MESSAGE_RECEIVED:
561                 __handle_mesh_devkey_message_received_event(
562                         (event_mesh_devkey_message_t*)event_data);
563                 BT_PERMANENT_LOG("Mesh: DevKey Message Received event");
564                 break;
565         default:
566         break;
567         }
568 }
569
570 int _bt_mesh_init(void)
571 {
572         oal_status_t status = OAL_STATUS_SUCCESS;
573
574         status = mesh_enable();
575         if (OAL_STATUS_SUCCESS != status) {
576                 BT_ERR("Mesh: Failed to initialize Mesh profile, status: %d",
577                                 status);
578                 return BLUETOOTH_ERROR_INTERNAL;
579         }
580         BT_INFO("Mesh: Stack Initialization Done successfully");
581
582         /* Register MESH event handler */
583         _bt_service_register_event_handler_callback(BT_MESH_MODULE,
584                         __handle_mesh_events);
585
586         return BLUETOOTH_ERROR_NONE;
587 }
588
589 int _bt_mesh_deinit(void)
590 {
591         oal_status_t status = OAL_STATUS_SUCCESS;
592
593         status = mesh_disable();
594         if (OAL_STATUS_SUCCESS != status) {
595                 BT_ERR("Mesh: Failed to de-initialize Mesh profile, status: %d",
596                                 status);
597                 return BLUETOOTH_ERROR_INTERNAL;
598         }
599
600         /* Register AVRCP target event handler */
601         _bt_service_unregister_event_handler_callback(BT_MESH_MODULE);
602
603         return BLUETOOTH_ERROR_NONE;
604 }