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