bbbf3daf4c4e01c0e1fbf4f61caed295ef4d8871
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / services / bt-request-handler.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <string.h>
19 #include <stdio.h>
20 #include <glib.h>
21 #include <dlog.h>
22 #include <gio/gio.h>
23 #include <gio/gunixfdlist.h>
24 #include <cynara-client.h>
25 #include <cynara-creds-gdbus.h>
26 #include <systemd/sd-daemon.h>
27 #include <aul.h>
28
29 #include "bluetooth-api.h"
30 #include "bluetooth-audio-api.h"
31 #include "bluetooth-gatt-server-api.h"
32 #include "bluetooth-mesh-api.h"
33 #include "bt-request-handler.h"
34 #include "bt-service-common.h"
35 #include "bt-service-util.h"
36
37 #include "bt-service-core-adapter.h"
38 #include "bt-service-core-adapter-le.h"
39 #include "bt-service-core-device.h"
40
41 #include "bt-service-audio-common.h"
42 #include "bt-service-avrcp-tg.h"
43 #include "bt-service-avrcp-ctrl.h"
44 #include "bt-service-gatt.h"
45 #include "bt-service-mesh-main.h"
46 #include "bt-service-mesh-util.h"
47 #include "bt-service-mesh-network.h"
48 #include "bt-service-mesh-config-client.h"
49 #include "bt-service-mesh-model.h"
50 #include "bt-service-dpm.h"
51 #include "bt-service-hidhost.h"
52 #include "bt-service-rfcomm.h"
53 #include "bt-service-hdp.h"
54 #include "bt-service-tds.h"
55
56 /*Obex*/
57 #include "bt-service-obex-server.h"
58 #include "bt-service-opp-client.h"
59 #include "bt-service-map-client.h"
60 #include "bt-service-pbap.h"
61 #include "bt-service-oob.h"
62
63 #include "bt-service-battery-monitor.h"
64
65 #include "bt-service-l2cap-le.h"
66
67 #ifdef TIZEN_FEATURE_BT_PAN_NAP
68 #include "bt-service-network.h"
69 #endif
70
71 #ifdef TIZEN_FEATURE_BT_IPSP
72 #include "bt-service-ipsp.h"
73 #endif
74 /* For maintaining Application Sync API call requests */
75 static GSList *invocation_list = NULL;
76
77 #ifdef TIZEN_FEATURE_BT_CONTAINER
78 struct nspid_t {
79         char *unique_name;
80         gboolean is_container;
81 };
82 static GSList *nspid_list = NULL;
83 #endif
84
85 static GDBusConnection *bt_service_conn;
86 static guint owner_id = 0;
87 static guint owner_sig_id = 0;
88 static gboolean is_le_intended = FALSE;
89
90 static cynara *p_cynara;
91 static cynara_configuration *conf;
92 static const char *requester_unique_creds = NULL;
93
94 static const gchar bt_service_introspection_xml[] =
95 "<node name='/org/projectx/bt_service'>"
96 "       <interface name='org.projectx.bt'>"
97 "               <method name='service_request'>"
98                         /* Input Parameters */
99 "                       <arg type='i' name='service_type' direction='in' />"
100 "                       <arg type='i' name='service_function' direction='in' />"
101 "                       <arg type='i' name='request_type' direction='in' />"
102 "                       <arg type='ay' name='input_param1' direction='in' />"
103 "                       <arg type='ay' name='input_param2' direction='in' />"
104 "                       <arg type='ay' name='input_param3' direction='in' />"
105 "                       <arg type='ay' name='input_param4' direction='in' />"
106 "                       <arg type='ay' name='input_param5' direction='in' />"
107                         /* Return Parameters */
108 "                       <arg type='i' name='output_param1' direction='out' />"
109 "                       <arg type='v' name='output_param2' direction='out' />"
110 "               </method>"
111 "               <method name='get_avc_mode'>"
112                         /* Out Parameters */
113 "                       <arg type='u' name='avc_mode' direction='out' />"
114 "               </method>"
115 "       </interface>"
116 "</node>";
117
118 static gboolean name_acquired = FALSE;
119
120 static char *current_sender_playing = NULL;
121
122 GDBusNodeInfo *node_info_g = NULL;
123
124 static void __bt_fill_garray_from_variant(GVariant *var, GArray *param);
125
126 static void __bt_service_method(GDBusConnection *connection,
127                 const gchar *sender,
128                 const gchar *object_path,
129                 const gchar *interface_name,
130                 const gchar *method_name,
131                 GVariant *parameters,
132                 GDBusMethodInvocation *invocation,
133                 gpointer user_data);
134
135 int __bt_bluez_request(int function_name,
136                 int request_type,
137                 int request_id,
138                 GDBusMethodInvocation *context,
139                 GVariant *in_param1,
140                 GVariant *in_param2,
141                 GVariant *in_param3,
142                 GVariant *in_param4,
143                 GArray **out_param1);
144 int __bt_obexd_request(int function_name,
145                 int request_type,
146                 int request_id,
147                 GDBusMethodInvocation *context,
148                 GVariant *in_param1,
149                 GVariant *in_param2,
150                 GVariant *in_param3,
151                 GVariant *in_param4,
152                 GArray **out_param1);
153 int __bt_agent_request(int function_name,
154                 int request_type,
155                 int request_id,
156                 GDBusMethodInvocation *context,
157                 GVariant *in_param1,
158                 GVariant *in_param2,
159                 GVariant *in_param3,
160                 GVariant *in_param4,
161                 GArray **out_param1);
162 int __bt_core_request(int function_name,
163                 int request_type,
164                 int request_id,
165                 GDBusMethodInvocation *context,
166                 GVariant *in_param1);
167
168 gboolean __bt_service_check_privilege(int function_name,
169                                         int service_type,
170                                         const char *unique_name);
171
172 /* Function definitions*/
173 GSList *_bt_get_invocation_list(void)
174 {
175         return invocation_list;
176 }
177
178 void _bt_free_info_from_invocation_list(invocation_info_t *req_info)
179 {
180         ret_if(NULL == req_info);
181         invocation_list = g_slist_remove(invocation_list, req_info);
182         g_free(req_info->sender);
183         g_free(req_info->user_data);
184         g_free(req_info);
185 }
186
187 void _bt_set_le_intended_status(gboolean value)
188 {
189         is_le_intended = value;
190 }
191
192 static void __bt_service_get_parameters(GVariant *in_param,
193                 void *value, int size)
194 {
195         void *buf = NULL;
196         buf = (void *)g_variant_get_data(in_param);
197         memcpy(value, buf, size);
198 }
199
200 static gboolean __bt_is_sync_function(int service_function)
201 {
202         /*TODO: Keep adding sync methods with expect replies from bluetooth service */
203         if (service_function == BT_GET_LOCAL_ADDRESS
204                         || service_function == BT_GET_LOCAL_NAME
205                         || service_function == BT_GET_LOCAL_VERSION
206                         || service_function == BT_GET_BONDED_DEVICES
207                         || service_function == BT_GET_BONDED_DEVICE
208                         || service_function == BT_GET_PROFILE_CONNECTED_DEVICES
209                         || service_function == BT_GET_IS_ALIAS_SET
210                         || service_function == BT_GET_CONNECTED_LINK_TYPE
211                         || service_function == BT_IS_SERVICE_USED
212                         || service_function == BT_RFCOMM_LISTEN_AND_ACCEPT
213                         || service_function == BT_RFCOMM_LISTEN
214                         || service_function == BT_HDP_REGISTER_SINK_APP
215                         || service_function == BT_HDP_UNREGISTER_SINK_APP
216                         || service_function == BT_HDP_GET_FD
217                         || service_function == BT_AVRCP_GET_TRACK_INFO
218                         || service_function == BT_AVRCP_CONTROL_GET_PROPERTY
219                         || service_function == BT_SET_ADVERTISING_DATA
220                         || service_function == BT_SET_SCAN_RESPONSE_DATA
221                         || service_function == BT_SET_ADVERTISING
222                         || service_function == BT_SET_CUSTOM_ADVERTISING
223                         || service_function == BT_GATT_SERVER_REGISTER
224                         || service_function == BT_GATT_SERVER_ADD_SERVICE
225                         || service_function == BT_GATT_SERVER_ADD_CHARACTERISTIC
226                         || service_function == BT_GATT_SERVER_ADD_DESCRIPTOR
227                         || service_function == BT_GATT_SERVER_START_SERVICE
228                         || service_function == BT_GATT_SERVER_STOP_SERVICE
229                         || service_function == BT_GATT_SERVER_DELETE_SERVICE
230                         || service_function == BT_START_LE_DISCOVERY
231                         || service_function == BT_STOP_LE_DISCOVERY
232                         || service_function == BT_GET_CONNECTABLE
233                         || service_function == BT_GATT_CLIENT_REGISTER
234                         || service_function == BT_GATT_GET_PRIMARY_SERVICES
235                         || service_function == BT_GATT_GET_SERVICE_PROPERTIES
236                         || service_function == BT_GATT_GET_INCLUDED_SERVICES
237                         || service_function == BT_GATT_GET_CHARACTERISTIC_PROPERTIES
238                         || service_function == BT_GATT_WATCH_CHARACTERISTIC
239                         || service_function == BT_GATT_ACQUIRE_WRITE
240                         || service_function == BT_AUDIO_SELECT_ROLE
241                         /* Mesh API's */
242                         || service_function == BT_MESH_INIT
243                         || service_function == BT_MESH_NETWORK_CREATE
244                         || service_function == BT_MESH_NETWORK_DESTROY
245                         || service_function == BT_MESH_NETWORK_LOAD
246                         || service_function == BT_MESH_NETWORK_SCAN
247                         || service_function == BT_MESH_NETWORK_ADD_NETKEY
248                         || service_function == BT_MESH_NETWORK_DELETE_NETKEY
249                         || service_function == BT_MESH_NETWORK_UPDATE_NETKEY
250                         || service_function == BT_MESH_NETWORK_ADD_APPKEY
251                         || service_function == BT_MESH_NETWORK_DELETE_APPKEY
252                         || service_function == BT_MESH_NETWORK_UPDATE_APPKEY
253                         || service_function == BT_L2CAP_LE_LISTEN_AND_ACCEPT
254                         || service_function == BT_L2CAP_LE_LISTEN)
255                 return TRUE;
256         else
257                 return FALSE;
258 }
259
260 void _bt_save_invocation_context(GDBusMethodInvocation *invocation, int result,
261                 char *sender, int service_function,
262                 gpointer invocation_data)
263 {
264         invocation_info_t *info;
265         info = g_malloc0(sizeof(invocation_info_t));
266         info->context = invocation;
267         info->result = result;
268         info->sender = g_strdup(sender);
269         info->service_function = service_function;
270         info->user_data = invocation_data;
271         invocation_list = g_slist_append(invocation_list, info);
272
273 }
274
275 static int __bt_service_get_sender_pid(const char *unique_name, pid_t *pid)
276 {
277         int ret;
278         char err_msg[256] = {0, };
279
280         retv_if(bt_service_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
281
282         ret = cynara_creds_gdbus_get_pid(bt_service_conn, unique_name, pid);
283         if (ret != CYNARA_API_SUCCESS) {
284                 cynara_strerror(ret, err_msg, sizeof(err_msg));
285                 BT_ERR("Fail to get user credential: %s", err_msg);
286
287                 return BLUETOOTH_ERROR_INTERNAL;
288         }
289
290         return BLUETOOTH_ERROR_NONE;
291 }
292
293 static int __bt_bm_request_data(_bt_battery_data_t *latest)
294 {
295         int ret = _bt_bm_read_data(latest);
296         if (ret == BLUETOOTH_ERROR_NONE) {
297                 BT_DBG("Received data from bluetooth battery monitor: %ld %ld %d %d",
298                         latest->session_start_time, latest->session_end_time,
299                                 latest->session_scan_time, latest->session_connected_time);
300
301                 if (latest->atm_list == NULL) {
302                         BT_DBG("No app data transaction for this session");
303                         return ret;
304                 }
305
306                 for (GSList *l = latest->atm_list; l != NULL; l = g_slist_next(l)) {
307                         _bt_battery_app_data_t *t = (_bt_battery_app_data_t *)(l->data);
308                         BT_DBG("%ld %ld %d %d", (long int)(t->uid), (long int)(t->pid), t->rx_bytes, t->tx_bytes);
309                 }
310         }
311         else
312                 BT_ERR("Error encountered");
313         return ret;
314 }
315
316 static void __bt_service_method(GDBusConnection *connection,
317                 const gchar *sender,
318                 const gchar *object_path,
319                 const gchar *interface_name,
320                 const gchar *method_name,
321                 GVariant *parameters,
322                 GDBusMethodInvocation *invocation,
323                 gpointer user_data)
324 {
325         FN_START;
326
327         BT_DBG("Method[%s] Object Path[%s] Interface Name[%s]",
328                         method_name, object_path, interface_name);
329
330         if (g_strcmp0(method_name, "service_request") == 0) {
331                 int service_type;
332                 int service_function;
333                 int request_type;
334                 GVariant *param1 = NULL;
335                 GVariant *param2 = NULL;
336                 GVariant *param3 = NULL;
337                 GVariant *param4 = NULL;
338                 GVariant *param5 = NULL;
339                 GArray *out_param1 = NULL;
340                 GVariant *out_var = NULL;
341                 GVariant *temp = NULL;
342                 int result = 0;
343                 int request_id = -1;
344                 const char *unique_name = NULL;
345
346                 g_variant_get(parameters, "(iii@ay@ay@ay@ay@ay)", &service_type,
347                                 &service_function, &request_type,
348                                 &param1, &param2, &param3, &param4, &param5);
349
350                 out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
351
352 #ifdef TIZEN_FEATURE_BT_CONTAINER
353                 /* In case of the bt-service in container, only privilege check request is coming
354                  * from the bt-service in host. At this time, the original sender's privilege
355                  * should be checked not bt-service's privilege */
356                 if (_bt_service_is_container() == TRUE)
357                         unique_name = (const char *)g_variant_get_data(param1);
358                 else
359                         unique_name = sender;
360 #else
361                 unique_name = sender;
362 #endif
363
364                 if (service_type == BT_CORE_SERVICE) {
365                         BT_DBG("No need to check privilege from bt-core");
366                 } else if (__bt_service_check_privilege(service_function,
367                                         service_type, unique_name) == FALSE) {
368                         BT_ERR("Client don't have the privilege to excute this function");
369                         result = BLUETOOTH_ERROR_PERMISSION_DEINED;
370                         goto fail;
371                 }
372
373                 BT_DBG("SERVICE TYPE [%d] SERVICE FUNC [%d]",
374                                 service_type, service_function);
375                 switch (service_type) {
376                 case BT_BLUEZ_SERVICE:
377                         result = __bt_bluez_request(service_function,
378                                         request_type, request_id,
379                                         invocation, param1, param2,
380                                         param3, param4, &out_param1);
381                         break;
382                 case BT_OBEX_SERVICE:
383                         result = __bt_obexd_request(service_function,
384                                         request_type, request_id,
385                                         invocation, param1,
386                                         param2, param3,
387                                         param4, &out_param1);
388                         break;
389                 case BT_AGENT_SERVICE:
390                         result = __bt_agent_request(service_function,
391                                         request_type, request_id,
392                                         invocation, param1,
393                                         param2, param3,
394                                         param4, &out_param1);
395                         break;
396                 case BT_CORE_SERVICE:
397                         result = __bt_core_request(service_function,
398                                         request_type, request_id,
399                                         invocation, param1);
400                         break;
401                 case BT_CHECK_PRIVILEGE:
402                         result = BLUETOOTH_ERROR_NONE;
403                         break;
404                 default:
405                         BT_ERR("Unknown service type");
406                         result = BLUETOOTH_ERROR_INTERNAL;
407                         goto fail;
408                 }
409
410                 if (result != BLUETOOTH_ERROR_NONE)
411                         goto fail;
412
413                 if (service_type == BT_CHECK_PRIVILEGE)
414                         goto send_reply;
415
416                 if (service_function == BT_OPP_PUSH_FILES)
417                         goto done;
418
419                 if (request_type == BT_ASYNC_REQ ||
420                                 __bt_is_sync_function(service_function)) {
421                         /* Do not send reply to bt-api over dbus,
422                                 Invocation context is already saved in service_function */
423                         goto done;
424                 }
425
426 send_reply:
427                 out_var = g_variant_new_from_data((const GVariantType *)"ay",
428                                 out_param1->data, out_param1->len,
429                                 TRUE, NULL, NULL);
430
431                 temp = g_variant_new("(iv)", result, out_var);
432                 g_dbus_method_invocation_return_value(invocation, temp);
433                 g_array_free(out_param1, TRUE);
434                 out_param1 = NULL;
435
436 done:
437                 g_variant_unref(param1);
438                 g_variant_unref(param2);
439                 g_variant_unref(param3);
440                 g_variant_unref(param4);
441                 g_variant_unref(param5);
442                 FN_END;
443                 return;
444 fail:
445                 BT_ERR_C("Request is failed [%s] [0x%x]",
446                                 _bt_convert_error_to_string(result), result);
447
448                 out_var = g_variant_new_from_data((const GVariantType *)"ay",
449                                 out_param1->data, out_param1->len,
450                                 TRUE, NULL, NULL);
451
452                 temp = g_variant_new("(iv)", result, out_var);
453                 g_dbus_method_invocation_return_value(invocation, temp);
454
455                 g_array_free(out_param1, TRUE);
456                 out_param1 = NULL;
457
458                 g_variant_unref(param1);
459                 g_variant_unref(param2);
460                 g_variant_unref(param3);
461                 g_variant_unref(param4);
462                 g_variant_unref(param5);
463         }
464         else if (g_strcmp0(method_name, "get_avc_mode") == 0) {
465                 unsigned int mode = 0;
466
467                 if (_bt_audio_get_avc_mode(&mode) != BLUETOOTH_ERROR_NONE)
468                         BT_ERR("Fail to get the avc mode");
469
470                 BT_DBG("Absolute Volume Control mode: %d", mode);
471
472                 g_dbus_method_invocation_return_value(invocation,
473                         g_variant_new("(u)", mode));
474         }
475
476         FN_END;
477         return;
478 }
479
480
481 static const GDBusInterfaceVTable method_table = {
482         __bt_service_method,
483         NULL,
484         NULL,
485         {0}
486 };
487
488 int __bt_bluez_request(int function_name,
489                 int request_type,
490                 int request_id,
491                 GDBusMethodInvocation *context,
492                 GVariant *in_param1,
493                 GVariant *in_param2,
494                 GVariant *in_param3,
495                 GVariant *in_param4,
496                 GArray **out_param1)
497 {
498         int result = BLUETOOTH_ERROR_NONE;
499         char *sender = NULL;
500
501         switch (function_name) {
502         case BT_ENABLE_ADAPTER: {
503                 result = _bt_enable_adapter();
504                 _bt_adapter_start_enable_timer();
505                 break;
506         }
507         case BT_DISABLE_ADAPTER: {
508                 result = _bt_disable_adapter();
509                 break;
510         }
511         case BT_RECOVER_ADAPTER:
512                  result = _bt_recover_adapter();
513                  break;
514         case BT_RESET_ADAPTER:
515                 result = _bt_reset_adapter();
516                 break;
517         case BT_BATTERY_READ_DATA: {
518                 _bt_battery_data_t *data = NULL;
519                 data = g_malloc0(sizeof(_bt_battery_data_t));
520                 result = __bt_bm_request_data(data);
521
522                 bt_battery_dbus_data_t dbus_data;
523                 memset(&dbus_data, 0, sizeof(bt_battery_dbus_data_t));
524                 dbus_data.session_start_time = data->session_start_time;
525                 dbus_data.session_end_time = data->session_end_time;
526                 dbus_data.session_scan_time = data->session_scan_time;
527                 dbus_data.session_connected_time = data->session_connected_time;
528                 dbus_data.tx_time = data->tx_time;
529                 dbus_data.rx_time = data->rx_time;
530                 dbus_data.idle_time = data->idle_time;
531
532                 /*Populating app data*/
533                 int n = 0;
534                 for (GSList *l = data->atm_list; l != NULL; l = g_slist_next(l)) {
535                         bt_battery_app_data *t = (bt_battery_app_data *)(l->data);
536                         memcpy(&dbus_data.app_data[n], t, sizeof(bt_battery_app_data));
537                         n++;
538                 }
539                 dbus_data.num_app = n;
540                 g_array_append_vals(*out_param1, &dbus_data, sizeof(bt_battery_dbus_data_t));
541                 g_slist_free_full(data->atm_list, g_free);
542                 g_free(data);
543                 data = NULL;
544                 break;
545         }
546         case BT_CHECK_ADAPTER: {
547                 int enabled = BT_ADAPTER_DISABLED;
548                 result = _bt_check_adapter(&enabled);
549
550                 g_array_append_vals(*out_param1, &enabled,
551                                 sizeof(int));
552                 break;
553         }
554         case BT_ENABLE_ADAPTER_LE:
555                 _bt_set_le_intended_status(TRUE);
556                 result = _bt_enable_adapter_le();
557                 break;
558         case BT_DISABLE_ADAPTER_LE:
559                 _bt_set_le_intended_status(FALSE);
560                 result = _bt_disable_adapter_le();
561                 break;
562         case BT_START_DISCOVERY: {
563                 unsigned short max_response;
564                 unsigned short discovery_duration;
565                 unsigned int classOfDeviceMask;
566                 uid_t uid;
567
568                 __bt_service_get_parameters(in_param1,
569                                 &max_response, sizeof(unsigned short));
570                 __bt_service_get_parameters(in_param2,
571                                 &discovery_duration, sizeof(unsigned short));
572                 __bt_service_get_parameters(in_param3,
573                                 &classOfDeviceMask, sizeof(unsigned int));
574                 __bt_service_get_parameters(in_param4, &uid, sizeof(uid_t));
575
576                 result = _bt_start_discovery(max_response, discovery_duration, classOfDeviceMask);
577
578                 if (result == BLUETOOTH_ERROR_NONE) {
579                         pid_t pid;
580
581                         sender = (char*)g_dbus_method_invocation_get_sender(context);
582
583                         if (__bt_service_get_sender_pid(sender, &pid) != BLUETOOTH_ERROR_NONE)
584                                 BT_ERR("Fail to get the sender pid");
585
586                         BT_DBG("Remeber pid / uid for the scan operation");
587                         _bt_bm_add_scan_app(SCAN_REGACY, uid, pid);
588                 }
589
590                 break;
591         }
592         case BT_START_CUSTOM_DISCOVERY: {
593                 bt_discovery_role_type_t role;
594
595                 __bt_service_get_parameters(in_param1,
596                                 &role, sizeof(bt_discovery_role_type_t));
597                 result = _bt_start_custom_discovery(role);
598
599                 break;
600         }
601         case BT_CANCEL_DISCOVERY:
602                 result = _bt_cancel_discovery();
603                 break;
604         case BT_IS_DISCOVERYING: {
605                 gboolean discovering = FALSE;
606                 discovering = _bt_is_discovering();
607                 g_array_append_vals(*out_param1,
608                                 &discovering, sizeof(gboolean));
609                 break;
610         }
611         case BT_GET_LOCAL_ADDRESS: {
612                 result = _bt_get_local_address();
613
614                 /* Save invocation */
615                 if (result == BLUETOOTH_ERROR_NONE) {
616                         sender = (char*)g_dbus_method_invocation_get_sender(context);
617                         _bt_save_invocation_context(context, result, sender,
618                                         function_name, NULL);
619                 }
620                 break;
621         }
622         case BT_GET_LOCAL_VERSION: {
623                 result = _bt_get_local_version();
624
625                 /* Save invocation */
626                 if (result == BLUETOOTH_ERROR_NONE) {
627                         sender = (char*)g_dbus_method_invocation_get_sender(context);
628                         _bt_save_invocation_context(context, result, sender,
629                                         function_name, NULL);
630                 }
631                 break;
632         }
633         case BT_GET_LOCAL_NAME: {
634                 result = _bt_get_local_name();
635
636                 /* Save invocation */
637                 if (result == BLUETOOTH_ERROR_NONE) {
638                         sender = (char*)g_dbus_method_invocation_get_sender(context);
639                         _bt_save_invocation_context(context, result, sender,
640                                         function_name, NULL);
641                 }
642                 break;
643         }
644         case BT_SET_LOCAL_NAME: {
645                 bluetooth_device_name_t local_name = { {0} };
646                 __bt_service_get_parameters(in_param1,
647                                 &local_name, sizeof(bluetooth_device_name_t));
648                 result = _bt_set_local_name(local_name.name);
649                 break;
650         }
651         case BT_GET_DISCOVERABLE_MODE: {
652                 int discoverable_mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
653                 result = _bt_get_discoverable_mode(&discoverable_mode);
654                 g_array_append_vals(*out_param1, &discoverable_mode, sizeof(int));
655                 break;
656         }
657         case BT_GET_DISCOVERABLE_TIME: {
658                 int timeout = 0;
659
660                 result = _bt_get_timeout_value(&timeout);
661                 g_array_append_vals(*out_param1, &timeout, sizeof(int));
662                 break;
663         }
664         case BT_SET_DISCOVERABLE_MODE: {
665                 int mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
666                 int time = 0;
667
668                 __bt_service_get_parameters(in_param1, &mode, sizeof(int));
669                 __bt_service_get_parameters(in_param2, &time, sizeof(int));
670
671                 result = _bt_set_discoverable_mode(mode, time);
672                 break;
673         }
674         case BT_IS_CONNECTABLE: {
675                 gboolean is_connectable = FALSE;
676
677                 is_connectable = _bt_is_connectable();
678                 g_array_append_vals(*out_param1, &is_connectable, sizeof(gboolean));
679                 break;
680         }
681         case BT_GET_CONNECTABLE: {
682                 result  = _bt_get_connectable();
683
684                 /* Save invocation */
685                 if (result == BLUETOOTH_ERROR_NONE) {
686                          sender = (char*)g_dbus_method_invocation_get_sender(context);
687                          _bt_save_invocation_context(context, result, sender,
688                                          function_name, NULL);
689                 }
690                 break;
691         }
692         case BT_SET_CONNECTABLE: {
693                 gboolean is_connectable;
694
695                 __bt_service_get_parameters(in_param1,
696                                 &is_connectable, sizeof(gboolean));
697                 result = _bt_set_connectable(is_connectable);
698                 break;
699         }
700         case BT_IS_SERVICE_USED: {
701                 char *uuid = NULL;
702
703                 uuid = (char *)g_variant_get_data(in_param1);
704                 BT_INFO("UUID to be searched [%s]", uuid);
705                 result = _bt_is_service_used();
706
707                 /* Save invocation */
708                 if (result == BLUETOOTH_ERROR_NONE) {
709                         sender = (char*)g_dbus_method_invocation_get_sender(context);
710                         _bt_save_invocation_context(context, result, sender,
711                                         function_name, g_strdup(uuid));
712                 }
713                 break;
714         }
715         case BT_GET_BONDED_DEVICES: {
716                 result = _bt_get_bonded_devices();
717                 /* Save invocation */
718                 if (result == BLUETOOTH_ERROR_NONE) {
719                         sender = (char*)g_dbus_method_invocation_get_sender(context);
720                         _bt_save_invocation_context(context, result, sender,
721                                         function_name, NULL);
722                 }
723                 break;
724         }
725         case BT_GET_BONDED_DEVICE: {
726                 bluetooth_device_address_t address = { {0} };
727
728                 __bt_service_get_parameters(in_param1,
729                                 &address, sizeof(bluetooth_device_address_t));
730
731                 result = _bt_get_bonded_device_info(&address);
732                 /* Save invocation */
733                 if (result == BLUETOOTH_ERROR_NONE) {
734                         char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
735                         if (!addr) {
736                                 result = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
737                                 break;
738                         }
739
740                         _bt_convert_addr_type_to_string(addr, address.addr);
741                         sender = (char*)g_dbus_method_invocation_get_sender(context);
742                         _bt_save_invocation_context(context, result, sender,
743                                         function_name, addr);
744                 }
745                 break;
746         }
747         case BT_GET_PROFILE_CONNECTED_DEVICES: {
748                 char *uuid;
749                 uuid = (char *)g_variant_get_data(in_param1);
750                 result = _bt_get_profile_connected_devices(uuid);
751
752                 /* Save invocation */
753                 if (result == BLUETOOTH_ERROR_NONE) {
754                         sender = (char *)g_dbus_method_invocation_get_sender(context);
755                         _bt_save_invocation_context(context, result, sender,
756                                         function_name, NULL);
757                 }
758                 break;
759         }
760         case BT_SET_ALIAS: {
761                 bluetooth_device_address_t address = { {0} };
762                 const char *local_name;
763
764                 __bt_service_get_parameters(in_param1,
765                                 &address, sizeof(bluetooth_device_address_t));
766                 local_name = (const char *)g_variant_get_data(in_param2);
767
768                 result = _bt_set_alias(&address, local_name);
769                 break;
770         }
771         case BT_BOND_DEVICE: {
772                 bluetooth_device_address_t address = { {0} };
773
774                 __bt_service_get_parameters(in_param1,
775                                 &address, sizeof(bluetooth_device_address_t));
776                 result = _bt_bond_device(&address, BLUETOOTH_DEV_CONN_DEFAULT, out_param1);
777
778                 /* Save invocation */
779                 if (result == BLUETOOTH_ERROR_NONE) {
780                         char * addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
781                         _bt_convert_addr_type_to_string(addr, address.addr);
782                         sender = (char*)g_dbus_method_invocation_get_sender(context);
783                         _bt_save_invocation_context(context, result, sender,
784                                         function_name, (gpointer)addr);
785                 } else {
786                         bluetooth_device_info_t dev_info;
787
788                         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
789                         memcpy(&dev_info.device_address, &address,
790                                         sizeof(bluetooth_device_address_t));
791                         g_array_append_vals(*out_param1, &dev_info,
792                                         sizeof(bluetooth_device_info_t));
793                 }
794                 break;
795         }
796         case BT_BOND_DEVICE_BY_TYPE: {
797                 bluetooth_device_address_t address = { {0} };
798                 unsigned short conn_type = 0;
799
800                 __bt_service_get_parameters(in_param1,
801                                 &address, sizeof(bluetooth_device_address_t));
802                 __bt_service_get_parameters(in_param2,
803                                 &conn_type, sizeof(unsigned short));
804
805                 result = _bt_bond_device(&address, conn_type, out_param1);
806                 /* Save invocation */
807                 if (result == BLUETOOTH_ERROR_NONE) {
808                         char * addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
809                         _bt_convert_addr_type_to_string(addr, address.addr);
810                         sender = (char*)g_dbus_method_invocation_get_sender(context);
811                         _bt_save_invocation_context(context, result, sender,
812                                         function_name, (gpointer)addr);
813                 } else {
814                         bluetooth_device_info_t dev_info;
815
816                         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
817                         memcpy(&dev_info.device_address, &address,
818                                         sizeof(bluetooth_device_address_t));
819                         g_array_append_vals(*out_param1, &dev_info,
820                                         sizeof(bluetooth_device_info_t));
821                 }
822                 break;
823         }
824         case BT_UNBOND_DEVICE: {
825                 bluetooth_device_address_t address = { {0} };
826
827                 __bt_service_get_parameters(in_param1,
828                                 &address, sizeof(bluetooth_device_address_t));
829                 result = _bt_unbond_device(&address, out_param1);
830
831                 /* Save invocation */
832                 if (result == BLUETOOTH_ERROR_NONE) {
833                         char * addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
834                         _bt_convert_addr_type_to_string(addr, address.addr);
835                         sender = (char*)g_dbus_method_invocation_get_sender(context);
836                         _bt_save_invocation_context(context, result, sender,
837                                         function_name, (gpointer)addr);
838                 } else {
839                         g_array_append_vals(*out_param1, &address,
840                                         sizeof(bluetooth_device_address_t));
841                 }
842                 break;
843         }
844         case BT_CANCEL_BONDING:{
845                 result = _bt_cancel_bonding();
846                 break;
847         }
848         case BT_SEARCH_SERVICE: {
849                 bluetooth_device_address_t address = { {0} };
850                 __bt_service_get_parameters(in_param1,
851                                 &address, sizeof(bluetooth_device_address_t));
852                 result = _bt_search_device(&address);
853                 /* Save invocation */
854                 if (result == BLUETOOTH_ERROR_NONE) {
855                         char * addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
856                         _bt_convert_addr_type_to_string(addr, address.addr);
857                         sender = (char*)g_dbus_method_invocation_get_sender(context);
858                         _bt_save_invocation_context(context, result, sender,
859                                         function_name, (gpointer)addr);
860                 } else {
861                         bt_sdp_info_t sdp_info;
862
863                         memset(&sdp_info, 0x00, sizeof(bt_sdp_info_t));
864                         memcpy(&sdp_info.device_addr, &address,
865                                         sizeof(bluetooth_device_address_t));
866                         g_array_append_vals(*out_param1, &sdp_info, sizeof(bt_sdp_info_t));
867                 }
868                 break;
869         }
870         case BT_CANCEL_SEARCH_SERVICE: {
871                 result = _bt_cancel_search_device();
872                 break;
873         }
874         case BT_SET_PASSKEY_NOTIFICATION: {
875                 char *sender = NULL;
876                 gboolean enable;
877
878                 sender = (char *)g_dbus_method_invocation_get_sender(context);
879                 __bt_service_get_parameters(in_param1,
880                                 &enable, sizeof(gboolean));
881
882                 result = _bt_set_passkey_notification(sender, enable);
883                 break;
884         }
885         case BT_SET_PIN_CODE: {
886                 bluetooth_device_address_t address = { {0} };
887                 bluetooth_device_pin_code_t pin_code = { {0} };
888
889                 __bt_service_get_parameters(in_param1,
890                                 &address, sizeof(bluetooth_device_address_t));
891                 __bt_service_get_parameters(in_param2,
892                                 &pin_code, sizeof(bluetooth_device_pin_code_t));
893
894                 result = _bt_set_pin_code(&address, &pin_code);
895                 break;
896         }
897         case BT_UNSET_PIN_CODE: {
898                 bluetooth_device_address_t address = { {0} };
899
900                 __bt_service_get_parameters(in_param1,
901                                 &address, sizeof(bluetooth_device_address_t));
902
903                 result = _bt_unset_pin_code(&address);
904                 break;
905         }
906         case BT_PASSKEY_REPLY: {
907                 const char *passkey = NULL;
908                 gboolean authentication_reply = FALSE;
909                 passkey = g_variant_get_data(in_param1);
910                 __bt_service_get_parameters(in_param2,
911                                 &authentication_reply, sizeof(gboolean));
912                 result = _bt_passkey_reply(passkey, authentication_reply);
913                 break;
914         }
915         case BT_PASSKEY_CONFIRMATION_REPLY: {
916                 gboolean confirmation_reply = FALSE;
917                 __bt_service_get_parameters(in_param1,
918                                 &confirmation_reply, sizeof(gboolean));
919                 result = _bt_passkey_confirmation_reply(confirmation_reply);
920                 break;
921         }
922         case BT_GET_IS_ALIAS_SET: {
923                 bluetooth_device_address_t address = { {0} };
924
925                 __bt_service_get_parameters(in_param1,
926                                 &address, sizeof(bluetooth_device_address_t));
927
928                 result = _bt_is_alias_set(&address);
929                 if (result == BLUETOOTH_ERROR_NONE) {
930                         char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
931                         if (!addr) {
932                                 result = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
933                                 break;
934                         }
935
936                         _bt_convert_addr_type_to_string(addr, address.addr);
937                         sender = (char*)g_dbus_method_invocation_get_sender(context);
938                         _bt_save_invocation_context(context, result, sender,
939                                         function_name, addr);
940                 }
941                 break;
942         }
943         case BT_SET_AUTHORIZATION: {
944                 bluetooth_device_address_t address = { {0} };
945                 gboolean authorize;
946                 __bt_service_get_parameters(in_param1,
947                                 &address, sizeof(bluetooth_device_address_t));
948                 __bt_service_get_parameters(in_param2,
949                                 &authorize, sizeof(gboolean));
950                 result = _bt_set_authorization(&address, authorize);
951                 break;
952         }
953         case BT_SET_LE_PRIVACY: {
954                gboolean set_privacy;
955                __bt_service_get_parameters(in_param1, &set_privacy,
956                                 sizeof(gboolean));
957                result = _bt_set_le_privacy(set_privacy);
958                break;
959         }
960         case BT_ADD_ALLOW_LIST: {
961                 bluetooth_device_address_t address = { {0} };
962                 int address_type = 0;
963                 bool is_add = true;
964                 __bt_service_get_parameters(in_param1,
965                                 &address, sizeof(bluetooth_device_address_t));
966                 __bt_service_get_parameters(in_param2,
967                                 &address_type, sizeof(int));
968                 result = _bt_set_allow_list(&address, address_type, is_add);
969                 break;
970         }
971         case BT_REMOVE_ALLOW_LIST: {
972                 bluetooth_device_address_t address = { {0} };
973                 int address_type = 0;
974                 bool is_add = false;
975                 __bt_service_get_parameters(in_param1,
976                                 &address, sizeof(bluetooth_device_address_t));
977                 __bt_service_get_parameters(in_param2,
978                                 &address_type, sizeof(int));
979                 result = _bt_set_allow_list(&address, address_type, is_add);
980                 break;
981         }
982         case BT_UPDATE_LE_CONNECTION_MODE: {
983                 char *sender = NULL;
984                 bluetooth_device_address_t remote_address = { { 0 } };
985                 bluetooth_le_connection_param_t parameters = { 0 };
986                 bluetooth_le_connection_mode_t mode = BLUETOOTH_LE_CONNECTION_MODE_BALANCED;
987
988                 __bt_service_get_parameters(in_param1, &remote_address,
989                                 sizeof(bluetooth_device_address_t));
990                 __bt_service_get_parameters(in_param2, &mode,
991                                 sizeof(bluetooth_le_connection_mode_t));
992
993                 result = _bt_get_le_connection_parameter(mode, &parameters);
994                 if (result != BLUETOOTH_ERROR_NONE)
995                         break;
996
997                 sender = (char *)g_dbus_method_invocation_get_sender(context);
998
999                 result = _bt_le_connection_update(sender,
1000                                 remote_address.addr,
1001                                 parameters.interval_min,
1002                                 parameters.interval_max,
1003                                 parameters.latency,
1004                                 parameters.timeout);
1005                 break;
1006         }
1007         case BT_SET_MANUFACTURER_DATA: {
1008                 bluetooth_manufacturer_data_t m_data = { 0 };
1009                 __bt_service_get_parameters(in_param1,
1010                                 &m_data, sizeof(bluetooth_manufacturer_data_t));
1011                 result = _bt_set_manufacturer_data(&m_data);
1012                 break;
1013         }
1014
1015         case BT_IS_DEVICE_CONNECTED: {
1016                 bluetooth_device_address_t address = { {0} };
1017                 gboolean connected = FALSE;
1018                 int service_type;
1019
1020                 __bt_service_get_parameters(in_param1,
1021                                 &address, sizeof(bluetooth_device_address_t));
1022                 __bt_service_get_parameters(in_param2,
1023                                 &service_type, sizeof(int));
1024
1025 #if defined(TIZEN_FEATURE_BT_PAN_NAP)
1026                 if ((service_type == BLUETOOTH_NAP_SERVICE) || (service_type == BLUETOOTH_NAP_SERVER_SERVICE))
1027                         connected = _bt_is_nap_panu_device_connected(&address, service_type);
1028                 else
1029                         connected = _bt_is_device_connected(&address, service_type);
1030 #else
1031                 connected = _bt_is_device_connected(&address, service_type);
1032 #endif
1033                 BT_INFO("is_connected: %d", connected);
1034                 g_array_append_vals(*out_param1, &connected, sizeof(gboolean));
1035                 break;
1036         }
1037         case BT_GET_CONNECTED_LINK_TYPE: {
1038                 bluetooth_device_address_t address = { {0} };
1039
1040                 __bt_service_get_parameters(in_param1,
1041                                 &address, sizeof(bluetooth_device_address_t));
1042
1043                 result = _bt_get_connected_link(&address);
1044                 if (result == BLUETOOTH_ERROR_NONE) {
1045                         char *addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
1046                         if (!addr) {
1047                                 result = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1048                                 break;
1049                         }
1050
1051                         _bt_convert_addr_type_to_string(addr, address.addr);
1052                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1053                         _bt_save_invocation_context(context, result, sender,
1054                                         function_name, addr);
1055                 }
1056                 break;
1057         }
1058         case BT_IS_SCAN_FILTER_SUPPORTED:{
1059                 int is_supported = 0;
1060
1061                 if (_bt_is_scan_filter_supported() == TRUE)
1062                         is_supported = 1;
1063
1064                 g_array_append_vals(*out_param1,
1065                                 &is_supported, sizeof(int));
1066
1067                 break;
1068         }
1069         case BT_ENABLE_RSSI: {
1070                 bluetooth_device_address_t bd_addr;
1071                 int link_type;
1072                 bt_rssi_threshold_t rssi_threshold;
1073                 int low_threshold;
1074                 int in_range_threshold;
1075                 int high_threshold;
1076
1077                 BT_DBG("Enable RSSI");
1078
1079                 __bt_service_get_parameters(in_param1,
1080                                 &bd_addr, sizeof(bluetooth_device_address_t));
1081                 __bt_service_get_parameters(in_param2,
1082                                 &link_type, sizeof(int));
1083                 __bt_service_get_parameters(in_param3,
1084                                 &rssi_threshold, sizeof(bt_rssi_threshold_t));
1085
1086                 low_threshold = rssi_threshold.low_threshold;
1087                 in_range_threshold = rssi_threshold.in_range_threshold;
1088                 high_threshold = rssi_threshold.high_threshold;
1089
1090                 result = _bt_enable_rssi(&bd_addr, link_type, low_threshold,
1091                                 in_range_threshold, high_threshold);
1092                 break;
1093         }
1094         case BT_GET_RSSI: {
1095                 int link_type;
1096                 bluetooth_device_address_t bd_addr;
1097
1098                 BT_DBG("Get RSSI Strength");
1099
1100                 __bt_service_get_parameters(in_param1,
1101                                 &bd_addr, sizeof(bluetooth_device_address_t));
1102                 __bt_service_get_parameters(in_param2,
1103                                 &link_type, sizeof(int));
1104
1105                 result = _bt_get_rssi_strength(&bd_addr, link_type);
1106                 break;
1107         }
1108         case BT_SET_PROFILE_TRUSTED: {
1109                 bluetooth_device_address_t bd_addr = { {0} };
1110                 int profile;
1111                 int trust;
1112
1113                 __bt_service_get_parameters(in_param1, &bd_addr,
1114                                 sizeof(bluetooth_device_address_t));
1115                 __bt_service_get_parameters(in_param2, &profile, sizeof(int));
1116                 __bt_service_get_parameters(in_param3, &trust, sizeof(int));
1117
1118                 result = _bt_set_trust_profile(&bd_addr, profile, trust);
1119                 break;
1120         }
1121         case BT_GET_PROFILE_TRUSTED: {
1122                 bluetooth_device_address_t bd_addr = { {0} };
1123                 int profile;
1124                 guint trusted_profile = 0;
1125
1126                 __bt_service_get_parameters(in_param1, &bd_addr,
1127                                 sizeof(bluetooth_device_address_t));
1128                 __bt_service_get_parameters(in_param2, &profile, sizeof(int));
1129
1130                 result = _bt_get_trust_profile(&bd_addr, profile, &trusted_profile);
1131                 BT_DBG("TRUST %d", trusted_profile);
1132                 if (result == BLUETOOTH_ERROR_NONE) {
1133                         g_array_append_vals(*out_param1, &trusted_profile,
1134                                         sizeof(guint));
1135                 }
1136
1137                 break;
1138         }
1139         case BT_HID_CONNECT: {
1140                 bluetooth_device_address_t address = { {0} };
1141
1142                 __bt_service_get_parameters(in_param1,
1143                                 &address, sizeof(bluetooth_device_address_t));
1144
1145                 result = _bt_hid_connect(&address);
1146                 if (result != BLUETOOTH_ERROR_NONE) {
1147                         g_array_append_vals(*out_param1, &address,
1148                                         sizeof(bluetooth_device_address_t));
1149                 } else {
1150                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1151                         _bt_convert_addr_type_to_string(addr, address.addr);
1152                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1153                         _bt_save_invocation_context(context, result, sender,
1154                                         function_name, (gpointer)addr);
1155                 }
1156                 break;
1157         }
1158         case BT_HID_DISCONNECT: {
1159                 bluetooth_device_address_t address = { {0} };
1160
1161                 __bt_service_get_parameters(in_param1,
1162                                 &address, sizeof(bluetooth_device_address_t));
1163
1164                 result = _bt_hid_disconnect(&address);
1165                 if (result != BLUETOOTH_ERROR_NONE) {
1166                         g_array_append_vals(*out_param1, &address,
1167                                         sizeof(bluetooth_device_address_t));
1168                 } else {
1169                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1170                         _bt_convert_addr_type_to_string(addr, address.addr);
1171                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1172                         _bt_save_invocation_context(context, result, sender,
1173                                         function_name, (gpointer)addr);
1174                 }
1175                 break;
1176         }
1177         case BT_DPM_SET_ALLOW_BT_MODE: {
1178                 dpm_bt_allow_t value = DPM_BT_ERROR;
1179
1180                 __bt_service_get_parameters(in_param1, &value,
1181                                 sizeof(int));
1182
1183                 result = _bt_dpm_set_allow_bluetooth_mode(value);
1184                 break;
1185         }
1186         case BT_DPM_GET_ALLOW_BT_MODE: {
1187                 int value = DPM_BT_ERROR;
1188
1189                 result = _bt_dpm_get_allow_bluetooth_mode(&value);
1190                 g_array_append_vals(*out_param1, &value, sizeof(int));
1191                 break;
1192         }
1193         case BT_DPM_SET_DEVICE_RESTRITION: {
1194                 dpm_status_t value = DPM_STATUS_ERROR;
1195
1196                 __bt_service_get_parameters(in_param1, &value,
1197                                 sizeof(int));
1198
1199                 result = _bt_dpm_activate_bluetooth_device_restriction(value);
1200                 break;
1201         }
1202         case BT_DPM_GET_DEVICE_RESTRITION: {
1203                 int value = DPM_STATUS_ERROR;
1204
1205                 result = _bt_dpm_is_bluetooth_device_restriction_active(&value);
1206                 g_array_append_vals(*out_param1, &value, sizeof(int));
1207                 break;
1208         }
1209         case BT_DPM_SET_UUID_RESTRITION: {
1210                 dpm_status_t value = DPM_STATUS_ERROR;
1211
1212                 __bt_service_get_parameters(in_param1, &value,
1213                                 sizeof(int));
1214
1215                 result = _bt_dpm_activate_bluetoooth_uuid_restriction(value);
1216                 break;
1217         }
1218         case BT_DPM_GET_UUID_RESTRITION: {
1219                 int value = DPM_STATUS_ERROR;
1220
1221                 result = _bt_dpm_is_bluetooth_uuid_restriction_active(&value);
1222                 g_array_append_vals(*out_param1, &value, sizeof(int));
1223                 break;
1224         }
1225         case BT_DPM_ADD_DEVICES_BLOCKLIST: {
1226                 bluetooth_device_address_t address = { {0} };
1227
1228                 __bt_service_get_parameters(in_param1, &address,
1229                         sizeof(bluetooth_device_address_t));
1230
1231                 result = _bt_dpm_add_bluetooth_devices_to_blocklist(&address);
1232                 break;
1233         }
1234         case BT_DPM_ADD_DEVICES_ALLOWLIST: {
1235                 bluetooth_device_address_t address = { {0} };
1236
1237                 __bt_service_get_parameters(in_param1, &address,
1238                         sizeof(bluetooth_device_address_t));
1239
1240                 result = _bt_dpm_add_bluetooth_devices_to_allowlist(&address);
1241                 break;
1242         }
1243         case BT_DPM_ADD_UUIDS_BLOCKLIST: {
1244                 const char *uuid = NULL;
1245
1246                 uuid = g_variant_get_data(in_param1);
1247
1248                 result = _bt_dpm_add_bluetooth_uuids_to_blocklist(uuid);
1249                 break;
1250         }
1251         case BT_DPM_ADD_UUIDS_ALLOWLIST: {
1252                 const char *uuid = NULL;
1253
1254                 uuid = g_variant_get_data(in_param1);
1255
1256                 result = _bt_dpm_add_bluetooth_uuids_to_allowlist(uuid);
1257                 break;
1258         }
1259         case BT_DPM_CLEAR_DEVICES_BLOCKLIST: {
1260                 result = _bt_dpm_clear_bluetooth_devices_from_blocklist();
1261                 break;
1262         }
1263         case BT_DPM_CLEAR_DEVICES_ALLOWLIST: {
1264                 result = _bt_dpm_clear_bluetooth_devices_from_allowlist();
1265                 break;
1266         }
1267         case BT_DPM_CLEAR_UUIDS_BLOCKLIST: {
1268                 result = _bt_dpm_clear_bluetooth_uuids_from_blocklist();
1269                 break;
1270         }
1271         case BT_DPM_CLEAR_UUIDS_ALLOWLIST: {
1272                 result = _bt_dpm_clear_bluetooth_uuids_from_allowlist();
1273                 break;
1274         }
1275         case BT_DPM_REMOVE_DEVICE_BLOCKLIST: {
1276                 bluetooth_device_address_t address = { {0} };
1277
1278                 __bt_service_get_parameters(in_param1, &address,
1279                         sizeof(bluetooth_device_address_t));
1280
1281                 result = _bt_dpm_remove_bluetooth_devices_from_blocklist(&address);
1282                 break;
1283         }
1284         case BT_DPM_REMOVE_DEVICE_ALLOWLIST: {
1285                 bluetooth_device_address_t address = { {0} };
1286
1287                 __bt_service_get_parameters(in_param1, &address,
1288                         sizeof(bluetooth_device_address_t));
1289
1290                 result = _bt_dpm_remove_bluetooth_devices_from_allowlist(&address);
1291                 break;
1292         }
1293         case BT_DPM_REMOVE_UUID_BLOCKLIST: {
1294                 const char *uuid = NULL;
1295
1296                 uuid = g_variant_get_data(in_param1);
1297
1298                 result = _bt_dpm_remove_bluetooth_uuids_from_blocklist(uuid);
1299                 break;
1300         }
1301         case BT_DPM_REMOVE_UUID_ALLOWLIST: {
1302                 const char *uuid = NULL;
1303
1304                 uuid = g_variant_get_data(in_param1);
1305
1306                 result = _bt_dpm_remove_bluetooth_uuids_from_allowlist(uuid);
1307
1308                 break;
1309         }
1310         case BT_DPM_GET_DEVICES_BLOCKLIST: {
1311                 result = _bt_dpm_get_bluetooth_devices_from_blocklist(out_param1);
1312                 break;
1313         }
1314         case BT_DPM_GET_DEVICES_ALLOWLIST: {
1315                 result = _bt_dpm_get_bluetooth_devices_from_allowlist(out_param1);
1316
1317                 break;
1318         }
1319         case BT_DPM_GET_UUIDS_BLOCKLIST: {
1320                 result = _bt_dpm_get_bluetooth_uuids_from_blocklist(out_param1);
1321                 break;
1322         }
1323         case BT_DPM_GET_UUIDS_ALLOWLIST: {
1324                 result = _bt_dpm_get_bluetooth_uuids_from_allowlist(out_param1);
1325                 break;
1326         }
1327         case BT_DPM_SET_ALLOW_OUTGOING_CALL: {
1328                 dpm_status_t value = DPM_STATUS_ERROR;
1329
1330                 __bt_service_get_parameters(in_param1, &value,
1331                                 sizeof(int));
1332
1333                 result = _bt_dpm_set_allow_bluetooth_outgoing_call(value);
1334
1335                 break;
1336         }
1337         case BT_DPM_GET_ALLOW_OUTGOING_CALL: {
1338                 int value = DPM_STATUS_ERROR;
1339
1340                 result = _bt_dpm_get_allow_bluetooth_outgoing_call(&value);
1341                 g_array_append_vals(*out_param1, &value, sizeof(int));
1342                 break;
1343         }
1344         case BT_DPM_SET_PAIRING_STATE: {
1345                 dpm_status_t value = DPM_STATUS_ERROR;
1346
1347                 __bt_service_get_parameters(in_param1, &value,
1348                                 sizeof(int));
1349
1350                 result = _bt_dpm_set_bluetooth_pairing_state(value);
1351
1352                 break;
1353         }
1354         case BT_DPM_GET_PAIRING_STATE: {
1355                 int value = DPM_STATUS_ERROR;
1356
1357                 result = _bt_dpm_get_bluetooth_pairing_state(&value);
1358                 g_array_append_vals(*out_param1, &value, sizeof(int));
1359                 break;
1360         }
1361         case BT_DPM_SET_PROFILE_STATE: {
1362                 int value = DPM_STATUS_ERROR;
1363                 int profile = DPM_PROFILE_NONE;
1364
1365                 __bt_service_get_parameters(in_param1, &profile,
1366                                 sizeof(int));
1367                 __bt_service_get_parameters(in_param2, &value,
1368                                 sizeof(int));
1369
1370                 result = _bt_dpm_set_bluetooth_profile_state(profile, value);
1371
1372                 break;
1373         }
1374         case BT_DPM_GET_PROFILE_STATE: {
1375                 int value = DPM_STATUS_ERROR;
1376                 int profile = DPM_PROFILE_NONE;
1377
1378                 __bt_service_get_parameters(in_param1, &profile,
1379                                 sizeof(int));
1380
1381                 result = _bt_dpm_get_bluetooth_profile_state(profile, &value);
1382                 g_array_append_vals(*out_param1, &value, sizeof(int));
1383                 break;
1384         }
1385         case BT_DPM_SET_DESKROP_CONNECTIVITY_STATE: {
1386                 int value = DPM_BT_ERROR;
1387
1388                 __bt_service_get_parameters(in_param1, &value,
1389                                 sizeof(int));
1390
1391                 result = _bt_dpm_set_bluetooth_desktop_connectivity_state(value);
1392
1393                 break;
1394         }
1395         case BT_DPM_GET_DESKROP_CONNECTIVITY_STATE: {
1396                 int value = DPM_STATUS_ERROR;
1397
1398                 result = _bt_dpm_get_bluetooth_desktop_connectivity_state(&value);
1399                 g_array_append_vals(*out_param1, &value, sizeof(int));
1400                 break;
1401         }
1402         case BT_DPM_SET_DISCOVERABLE_STATE: {
1403                 int value = DPM_STATUS_ERROR;
1404
1405                 __bt_service_get_parameters(in_param1, &value,
1406                                 sizeof(int));
1407
1408                 result = _bt_dpm_set_bluetooth_discoverable_state(value);
1409
1410                 break;
1411         }
1412         case BT_DPM_GET_DISCOVERABLE_STATE: {
1413                 int value = DPM_STATUS_ERROR;
1414
1415                 result = _bt_dpm_get_bluetooth_discoverable_state(&value);
1416                 g_array_append_vals(*out_param1, &value, sizeof(int));
1417                 break;
1418         }
1419         case BT_DPM_SET_LIMITED_DISCOVERABLE_STATE: {
1420                 int value = DPM_STATUS_ERROR;
1421
1422                 __bt_service_get_parameters(in_param1, &value,
1423                                 sizeof(int));
1424
1425                 result = _bt_dpm_set_bluetooth_limited_discoverable_state(value);
1426
1427                 break;
1428         }
1429         case BT_DPM_GET_LIMITED_DISCOVERABLE_STATE: {
1430                 int value = DPM_STATUS_ERROR;
1431
1432                 result = _bt_dpm_get_bluetooth_limited_discoverable_state(&value);
1433                 g_array_append_vals(*out_param1, &value, sizeof(int));
1434                 break;
1435         }
1436         case BT_DPM_SET_DATA_TRANSFER_STATE: {
1437                 int value = DPM_STATUS_ERROR;
1438
1439                 __bt_service_get_parameters(in_param1, &value,
1440                                 sizeof(int));
1441
1442                 result = _bt_dpm_set_bluetooth_data_transfer_state(value);
1443
1444                 break;
1445         }
1446         case BT_DPM_GET_DATA_TRANSFER_STATE: {
1447                 int value = DPM_STATUS_ERROR;
1448
1449                 result = _bt_dpm_get_allow_bluetooth_data_transfer_state(&value);
1450                 g_array_append_vals(*out_param1, &value, sizeof(int));
1451                 break;
1452         }
1453         case BT_RFCOMM_CLIENT_CONNECT: {
1454                 bluetooth_device_address_t address = { {0} };
1455                 char *input_string;
1456                 int connect_type;
1457
1458                 __bt_service_get_parameters(in_param1,
1459                                 &address, sizeof(bluetooth_device_address_t));
1460                 input_string = (char *)g_variant_get_data(in_param2);
1461                 __bt_service_get_parameters(in_param3, &connect_type, sizeof(int));
1462
1463                 if (connect_type == BT_RFCOMM_UUID)
1464                         result = _bt_rfcomm_connect_using_uuid(&address, input_string);
1465                 else
1466                         result = _bt_rfcomm_connect_using_channel(&address, input_string);
1467
1468                 if (result != BLUETOOTH_ERROR_NONE) {
1469                         bluetooth_rfcomm_connection_t conn_info;
1470
1471                         BT_ERR("BT_RFCOMM_CLIENT_CONNECT failed, send error");
1472                         memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
1473                         if (connect_type == BT_RFCOMM_UUID)
1474                                 g_strlcpy(conn_info.uuid, input_string, BLUETOOTH_UUID_STRING_MAX);
1475                         else
1476                                 g_strlcpy(conn_info.uuid, "not_used", BLUETOOTH_UUID_STRING_MAX);
1477
1478                         conn_info.device_role = RFCOMM_ROLE_CLIENT;
1479                         conn_info.socket_fd = -1;
1480                         memcpy(&conn_info.device_addr, &address, sizeof(bluetooth_device_address_t));
1481                         g_array_append_vals(*out_param1, &conn_info,
1482                                         sizeof(bluetooth_rfcomm_connection_t));
1483                 } else {
1484                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1485                         _bt_convert_addr_type_to_string(addr, address.addr);
1486                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1487                         _bt_save_invocation_context(context, result, sender, function_name, addr);
1488                 }
1489                 break;
1490         }
1491         case BT_RFCOMM_SOCKET_DISCONNECT: {
1492                 /*
1493                  * Bluetooth RFCOMM socket disconnection will be done from bt-api, call to bt-service
1494                  * is only used for privilege check, so return BLUETOOTH_ERROR_NONE from here.
1495                  */
1496                 result = BLUETOOTH_ERROR_NONE;
1497                 break;
1498         }
1499         case BT_RFCOMM_SOCKET_WRITE: {
1500                 /*
1501                  * This call to bt-service is only used for privilege check, so return
1502                  * BLUETOOTH_ERROR_NONE from here.
1503                  */
1504                 result = BLUETOOTH_ERROR_NONE;
1505                 break;
1506         }
1507         case BT_RFCOMM_CREATE_SOCKET: {
1508                 /*
1509                  * This call to bt-service is only used for privilege check, so return
1510                  * BLUETOOTH_ERROR_NONE from here.
1511                  */
1512                 result = BLUETOOTH_ERROR_NONE;
1513                 break;
1514         }
1515         case BT_RFCOMM_LISTEN_AND_ACCEPT: {
1516                 char *uuid;
1517                 int socket_fd = -1;
1518
1519                 sender = (char *)g_dbus_method_invocation_get_sender(context);
1520                 uuid = (char *)g_variant_get_data(in_param1);
1521
1522                 result = _bt_rfcomm_socket_listen(sender, uuid, true);
1523                 if (result > 0) {
1524                         result = BLUETOOTH_ERROR_NONE;
1525                         _bt_save_invocation_context(context,
1526                                         result, sender, function_name, NULL);
1527                 } else {
1528                         g_array_append_vals(*out_param1, &socket_fd, sizeof(int));
1529                 }
1530                 break;
1531         }
1532         case BT_RFCOMM_LISTEN: {
1533                 char *uuid;
1534                 int socket_fd = -1;
1535
1536                 sender = (char *)g_dbus_method_invocation_get_sender(context);
1537                 uuid = (char *)g_variant_get_data(in_param1);
1538
1539                 result = _bt_rfcomm_socket_listen(sender, uuid, false);
1540                 if (result > 0) {
1541                         result = BLUETOOTH_ERROR_NONE;
1542                         _bt_save_invocation_context(context,
1543                                         result, sender, function_name, NULL);
1544                 } else {
1545                         g_array_append_vals(*out_param1, &socket_fd, sizeof(int));
1546                 }
1547                 break;
1548         }
1549         case BT_RFCOMM_ACCEPT_CONNECTION: {
1550                 char *address;
1551
1552                 address = (char *)g_variant_get_data(in_param1);
1553                 result = _bt_socket_reply_conn_authorization(address, TRUE);
1554                 break;
1555         }
1556         case BT_RFCOMM_REJECT_CONNECTION: {
1557                 char *address;
1558
1559                 address = (char *)g_variant_get_data(in_param1);
1560                 result = _bt_socket_reply_conn_authorization(address, FALSE);
1561                 break;
1562         }
1563         case BT_RFCOMM_REMOVE_SOCKET: {
1564                 /*
1565                  * This call to bt-service is only used for privilege check, so return
1566                  * BLUETOOTH_ERROR_NONE from here.
1567                  */
1568                 result = BLUETOOTH_ERROR_NONE;
1569                 break;
1570         }
1571         case BT_RFCOMM_SEND_RX_DETAILS: {
1572                 uid_t uid;
1573                 pid_t pid;
1574                 int size;
1575                 __bt_service_get_parameters(in_param1, &uid, sizeof(uid_t));
1576                 __bt_service_get_parameters(in_param2, &pid, sizeof(pid_t));
1577                 __bt_service_get_parameters(in_param3, &size, sizeof(unsigned int));
1578                 _bt_bm_add_transaction_details(uid, pid, size, RX_DATA);
1579                 break;
1580         }
1581         case BT_RFCOMM_SEND_TX_DETAILS: {
1582                 uid_t uid;
1583                 pid_t pid;
1584                 int size;
1585                 __bt_service_get_parameters(in_param1, &uid, sizeof(uid_t));
1586                 __bt_service_get_parameters(in_param2, &pid, sizeof(pid_t));
1587                 __bt_service_get_parameters(in_param3, &size, sizeof(unsigned int));
1588                 _bt_bm_add_transaction_details(uid, pid, size, TX_DATA);
1589                 break;
1590         }
1591         case BT_RFCOMM_UPDATE_CONNECTION_INFO: {
1592                 gboolean connected = FALSE;
1593                 int socket_fd = -1;
1594                 int role = RFCOMM_ROLE_SERVER;
1595
1596                 sender = (char *)g_dbus_method_invocation_get_sender(context);
1597
1598                 __bt_service_get_parameters(in_param1, &role, sizeof(int));
1599                 __bt_service_get_parameters(in_param2, &connected, sizeof(gboolean));
1600                 __bt_service_get_parameters(in_param3, &socket_fd, sizeof(int));
1601
1602                 if (connected == TRUE)
1603                         result = _bt_rfcomm_conn_added(sender, role, socket_fd);
1604                 else
1605                         result = _bt_rfcomm_conn_removed(sender, role, socket_fd);
1606
1607                 break;
1608         }
1609         case BT_AUDIO_SELECT_ROLE: {
1610                 bluetooth_audio_role_t role;
1611
1612                 __bt_service_get_parameters(in_param1,
1613                                 &role, sizeof(bluetooth_audio_role_t));
1614
1615                 result = _bt_audio_select_role(role);
1616                 if (result == BLUETOOTH_ERROR_NONE) {
1617                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1618                         _bt_save_invocation_context(context, result, sender,
1619                                         function_name, NULL);
1620                 }
1621                 break;
1622         }
1623         case BT_AUDIO_SET_ABSOLUTE_VOLUME: {
1624                 unsigned int volume = 0;
1625
1626                 __bt_service_get_parameters(in_param1,
1627                                 &volume, sizeof(unsigned int));
1628
1629                 result = _bt_audio_set_absolute_volume(volume);
1630                 break;
1631         }
1632         case BT_AUDIO_GET_ABSOLUTE_VOLUME: {
1633                 unsigned int volume = 0;
1634
1635                 result = _bt_audio_get_absolute_volume(&volume);
1636                 g_array_append_vals(*out_param1, &volume, sizeof(unsigned int));
1637                 break;
1638         }
1639         case BT_AUDIO_IS_AVC_ACTIVATED: {
1640                 bool activated = 0;
1641
1642                 result = _bt_audio_is_avc_activated(&activated);
1643                 g_array_append_vals(*out_param1, &activated, sizeof(bool));
1644                 break;
1645         }
1646         case BT_AV_CONNECT: {
1647                 bluetooth_device_address_t address = { {0} };
1648                 __bt_service_get_parameters(in_param1,
1649                                 &address, sizeof(bluetooth_device_address_t));
1650
1651                 result = _bt_audio_is_profile_connection_in_progress(BT_AV_CONNECT, &address);
1652
1653                 if (result == BLUETOOTH_ERROR_NONE)
1654                         result = _bt_audio_connect(BT_AUDIO_A2DP, &address);
1655
1656                 if (result != BLUETOOTH_ERROR_NONE) {
1657                         BT_ERR("AV Connect Request has failed to execute");
1658                         char addr[BT_ADDRESS_STRING_SIZE];
1659                         _bt_convert_addr_type_to_string(addr, address.addr);
1660                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1661                 } else {
1662                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1663                         _bt_convert_addr_type_to_string(addr, address.addr);
1664                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1665                         _bt_save_invocation_context(context, result, sender,
1666                                         function_name, (gpointer)addr);
1667                 }
1668                 break;
1669         }
1670         case BT_AUDIO_CONNECT: {
1671                 bluetooth_device_address_t address = { {0} };
1672                 __bt_service_get_parameters(in_param1,
1673                                 &address, sizeof(bluetooth_device_address_t));
1674
1675                 result = _bt_audio_is_profile_connection_in_progress(BT_AUDIO_CONNECT, &address);
1676
1677                 if (result == BLUETOOTH_ERROR_NONE)
1678                         result = _bt_audio_connect(BT_AUDIO_ALL, &address);
1679
1680                 if (result != BLUETOOTH_ERROR_NONE) {
1681                         BT_ERR("Audio All Connect Request has failed to execute");
1682                         char addr[BT_ADDRESS_STRING_SIZE];
1683                         _bt_convert_addr_type_to_string(addr, address.addr);
1684                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1685                 } else {
1686                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1687                         _bt_convert_addr_type_to_string(addr, address.addr);
1688                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1689                         _bt_save_invocation_context(context, result, sender,
1690                                         function_name, (gpointer)addr);
1691                 }
1692                 break;
1693         }
1694         case BT_AUDIO_DISCONNECT: {
1695                 bluetooth_device_address_t address = { {0} };
1696                 __bt_service_get_parameters(in_param1,
1697                                 &address, sizeof(bluetooth_device_address_t));
1698
1699                 result = _bt_audio_disconnect(BT_AUDIO_ALL, &address);
1700
1701                 if (result != BLUETOOTH_ERROR_NONE) {
1702                         char addr[BT_ADDRESS_STRING_SIZE];
1703                         _bt_convert_addr_type_to_string(addr, address.addr);
1704                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1705                 } else {
1706                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1707                         _bt_convert_addr_type_to_string(addr, address.addr);
1708                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1709                         _bt_save_invocation_context(context, result, sender,
1710                                         function_name, (gpointer)addr);
1711                 }
1712                 break;
1713         }
1714         case BT_AV_DISCONNECT: {
1715                 bluetooth_device_address_t address = { {0} };
1716                 __bt_service_get_parameters(in_param1,
1717                                 &address, sizeof(bluetooth_device_address_t));
1718
1719                 result = _bt_audio_disconnect(BT_AUDIO_A2DP, &address);
1720
1721                 if (result != BLUETOOTH_ERROR_NONE) {
1722                         char addr[BT_ADDRESS_STRING_SIZE];
1723                         _bt_convert_addr_type_to_string(addr, address.addr);
1724                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1725                 } else {
1726                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1727                         _bt_convert_addr_type_to_string(addr, address.addr);
1728                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1729                         _bt_save_invocation_context(context, result, sender,
1730                                         function_name, (gpointer)addr);
1731                 }
1732                 break;
1733         }
1734         case BT_AG_CONNECT: {
1735                 bluetooth_device_address_t address = { {0} };
1736                 __bt_service_get_parameters(in_param1,
1737                                 &address, sizeof(bluetooth_device_address_t));
1738
1739                 result = _bt_audio_is_profile_connection_in_progress(BT_AG_CONNECT, &address);
1740
1741                 if (result == BLUETOOTH_ERROR_NONE)
1742                         result = _bt_audio_connect(BT_AUDIO_HSP, &address);
1743
1744                 if (result != BLUETOOTH_ERROR_NONE) {
1745                         BT_ERR("AG Connect Request has failed to execute");
1746                         char addr[BT_ADDRESS_STRING_SIZE];
1747                         _bt_convert_addr_type_to_string(addr, address.addr);
1748                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1749                 } else {
1750                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1751                         _bt_convert_addr_type_to_string(addr, address.addr);
1752                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1753                         _bt_save_invocation_context(context, result, sender,
1754                                         function_name, (gpointer)addr);
1755                 }
1756                 break;
1757         }
1758         case BT_AG_DISCONNECT: {
1759                 bluetooth_device_address_t address = { {0} };
1760
1761                 __bt_service_get_parameters(in_param1,
1762                                 &address, sizeof(bluetooth_device_address_t));
1763
1764                 result = _bt_audio_disconnect(BT_AUDIO_HSP, &address);
1765
1766                 if (result != BLUETOOTH_ERROR_NONE) {
1767                         char addr[BT_ADDRESS_STRING_SIZE];
1768                         _bt_convert_addr_type_to_string(addr, address.addr);
1769                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1770                 } else {
1771                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1772                         _bt_convert_addr_type_to_string(addr, address.addr);
1773                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1774                         _bt_save_invocation_context(context, result, sender,
1775                                         function_name, (gpointer)addr);
1776                 }
1777                 break;
1778         }
1779         case BT_AV_SOURCE_CONNECT: {
1780                 bluetooth_device_address_t address = { {0} };
1781
1782                 __bt_service_get_parameters(in_param1,
1783                                 &address, sizeof(bluetooth_device_address_t));
1784
1785                 result = _bt_audio_is_profile_connection_in_progress(BT_AV_SOURCE_CONNECT,
1786                                 &address);
1787
1788                 if (result == BLUETOOTH_ERROR_NONE)
1789                         result = _bt_audio_connect(BT_AUDIO_A2DP_SOURCE, &address);
1790
1791                 if (result != BLUETOOTH_ERROR_NONE) {
1792                         BT_ERR("A2DP Source Connect Request has failed to execute");
1793                         char addr[BT_ADDRESS_STRING_SIZE];
1794                         _bt_convert_addr_type_to_string(addr, address.addr);
1795                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1796                 } else {
1797                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1798                         _bt_convert_addr_type_to_string(addr, address.addr);
1799                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1800                         _bt_save_invocation_context(context, result, sender,
1801                                         function_name, (gpointer)addr);
1802                 }
1803                 break;
1804         }
1805         case BT_AV_SOURCE_DISCONNECT: {
1806                 bluetooth_device_address_t address = { {0} };
1807
1808                 __bt_service_get_parameters(in_param1,
1809                                 &address, sizeof(bluetooth_device_address_t));
1810
1811                 result = _bt_audio_disconnect(BT_AUDIO_A2DP_SOURCE, &address);
1812                 if (result != BLUETOOTH_ERROR_NONE) {
1813                         char addr[BT_ADDRESS_STRING_SIZE];
1814                         _bt_convert_addr_type_to_string(addr, address.addr);
1815                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1816                 } else {
1817                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1818                         _bt_convert_addr_type_to_string(addr, address.addr);
1819                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1820                         _bt_save_invocation_context(context, result, sender,
1821                                         function_name, (gpointer)addr);
1822                 }
1823                 break;
1824         }
1825         case BT_HF_CONNECT: {
1826                 bluetooth_device_address_t address = { {0} };
1827
1828                 __bt_service_get_parameters(in_param1,
1829                                 &address, sizeof(bluetooth_device_address_t));
1830
1831                 result = _bt_audio_is_profile_connection_in_progress(BT_HF_CONNECT,
1832                                 &address);
1833
1834                 if (result == BLUETOOTH_ERROR_NONE)
1835                         result = _bt_hf_connect(&address);
1836
1837                 if (result != BLUETOOTH_ERROR_NONE) {
1838                         BT_ERR("HF Connect Request has failed to execute");
1839                         char addr[BT_ADDRESS_STRING_SIZE];
1840                         _bt_convert_addr_type_to_string(addr, address.addr);
1841                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1842                 } else {
1843                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1844                         _bt_convert_addr_type_to_string(addr, address.addr);
1845                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1846                         _bt_save_invocation_context(context, result, sender,
1847                                         function_name, (gpointer)addr);
1848                 }
1849                 break;
1850         }
1851         case BT_HF_DISCONNECT: {
1852                 bluetooth_device_address_t address = { {0} };
1853
1854                 __bt_service_get_parameters(in_param1,
1855                                 &address, sizeof(bluetooth_device_address_t));
1856
1857                 result = _bt_hf_disconnect(&address);
1858                 if (result != BLUETOOTH_ERROR_NONE) {
1859                         char addr[BT_ADDRESS_STRING_SIZE];
1860                         _bt_convert_addr_type_to_string(addr, address.addr);
1861                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1862                 } else {
1863                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1864                         _bt_convert_addr_type_to_string(addr, address.addr);
1865                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1866                         _bt_save_invocation_context(context, result, sender,
1867                                         function_name, (gpointer)addr);
1868                 }
1869                 break;
1870         }
1871         case BT_AVRCP_TARGET_CONNECT: {
1872                 bluetooth_device_address_t address = { {0} };
1873
1874                 __bt_service_get_parameters(in_param1,
1875                                 &address, sizeof(bluetooth_device_address_t));
1876
1877                 result = _bt_audio_is_profile_connection_in_progress(BT_AVRCP_TARGET_CONNECT,
1878                                 &address);
1879
1880                 if (result == BLUETOOTH_ERROR_NONE)
1881                         result = _bt_audio_connect(BT_AVRCP_TARGET, &address);
1882
1883                 if (result != BLUETOOTH_ERROR_NONE) {
1884                         BT_ERR("AVRCP Target Connect Request has failed to execute");
1885                         char addr[BT_ADDRESS_STRING_SIZE];
1886                         _bt_convert_addr_type_to_string(addr, address.addr);
1887                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1888                 } else {
1889                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1890                         _bt_convert_addr_type_to_string(addr, address.addr);
1891                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1892                         _bt_save_invocation_context(context, result, sender,
1893                                         function_name, (gpointer)addr);
1894                 }
1895                 break;
1896         }
1897         case BT_AVRCP_TARGET_DISCONNECT: {
1898                 bluetooth_device_address_t address = { {0} };
1899
1900                 __bt_service_get_parameters(in_param1,
1901                                 &address, sizeof(bluetooth_device_address_t));
1902
1903                 result = _bt_audio_disconnect(BT_AVRCP_TARGET, &address);
1904                 if (result != BLUETOOTH_ERROR_NONE) {
1905                         char addr[BT_ADDRESS_STRING_SIZE];
1906                         _bt_convert_addr_type_to_string(addr, address.addr);
1907                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1908                 } else {
1909                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1910                         _bt_convert_addr_type_to_string(addr, address.addr);
1911                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1912                         _bt_save_invocation_context(context, result, sender,
1913                                         function_name, (gpointer)addr);
1914                 }
1915                 break;
1916         }
1917         case BT_AVRCP_CONTROL_CONNECT: {
1918                 bluetooth_device_address_t address = { {0} };
1919
1920                 __bt_service_get_parameters(in_param1,
1921                                 &address, sizeof(bluetooth_device_address_t));
1922
1923                 result = _bt_audio_is_profile_connection_in_progress(BT_AVRCP_CONTROL_CONNECT,
1924                                 &address);
1925
1926                 if (result == BLUETOOTH_ERROR_NONE)
1927                         result = _bt_audio_connect(BT_AVRCP, &address);
1928
1929                 if (result != BLUETOOTH_ERROR_NONE) {
1930                         BT_ERR("AVRCP Control Connect Request has failed to execute");
1931                         char addr[BT_ADDRESS_STRING_SIZE];
1932                         _bt_convert_addr_type_to_string(addr, address.addr);
1933                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1934                 } else {
1935                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1936                         _bt_convert_addr_type_to_string(addr, address.addr);
1937                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1938                         _bt_save_invocation_context(context, result, sender,
1939                                         function_name, (gpointer)addr);
1940                 }
1941                 break;
1942         }
1943         case BT_AVRCP_CONTROL_DISCONNECT: {
1944                 bluetooth_device_address_t address = { {0} };
1945
1946                 __bt_service_get_parameters(in_param1,
1947                                 &address, sizeof(bluetooth_device_address_t));
1948
1949                 result = _bt_audio_disconnect(BT_AVRCP, &address);
1950                 if (result != BLUETOOTH_ERROR_NONE) {
1951                         char addr[BT_ADDRESS_STRING_SIZE];
1952                         _bt_convert_addr_type_to_string(addr, address.addr);
1953                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1954                 } else {
1955                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1956                         _bt_convert_addr_type_to_string(addr, address.addr);
1957                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1958                         _bt_save_invocation_context(context, result, sender,
1959                                         function_name, (gpointer)addr);
1960                 }
1961                 break;
1962         }
1963         case BT_AVRCP_HANDLE_CONTROL: {
1964                 int key_code;
1965                 __bt_service_get_parameters(in_param1, &key_code, sizeof(int));
1966                 result = _bt_avrcp_control_cmd(key_code);
1967                 break;
1968         }
1969         case BT_AVRCP_HANDLE_CONTROL_TO_DEST: {
1970                 int key_code;
1971                 bluetooth_device_address_t address = { { 0 } };
1972
1973                 __bt_service_get_parameters(in_param1, &key_code, sizeof(int));
1974                 __bt_service_get_parameters(in_param2,
1975                                                 &address, sizeof(bluetooth_device_address_t));
1976
1977                 result = _bt_avrcp_control_cmd_to_dest(key_code, &address);
1978                 break;
1979         }
1980         case BT_AVRCP_CONTROL_SET_PROPERTY: {
1981                 int type;
1982                 unsigned int value;
1983
1984                 __bt_service_get_parameters(in_param1,
1985                                 &type, sizeof(int));
1986                 __bt_service_get_parameters(in_param2,
1987                                 &value, sizeof(unsigned int));
1988
1989                 result = _bt_avrcp_control_set_property(type, value);
1990                 break;
1991         }
1992         case BT_AVRCP_TRANSPORT_SET_PROPERTY: {
1993                 int type;
1994                 unsigned int value;
1995                 BT_DBG("+");
1996
1997                 __bt_service_get_parameters(in_param1,
1998                                 &type, sizeof(int));
1999                 __bt_service_get_parameters(in_param2,
2000                                 &value, sizeof(unsigned int));
2001
2002                 result = _bt_avrcp_transport_set_property(type, value);
2003                 BT_DBG("-");
2004                 break;
2005         }
2006         case BT_AVRCP_CONTROL_GET_PROPERTY: {
2007                 int type;
2008
2009                 __bt_service_get_parameters(in_param1, &type, sizeof(int));
2010
2011                 result = _bt_avrcp_control_get_property(type);
2012                 /* Save invocation */
2013                 if (result == BLUETOOTH_ERROR_NONE) {
2014                         sender = (char*)g_dbus_method_invocation_get_sender(context);
2015                         _bt_save_invocation_context(context, result, sender,
2016                                         function_name, g_memdup2(&type, sizeof(int)));
2017                 }
2018                 break;
2019         }
2020         case BT_AVRCP_GET_TRACK_INFO: {
2021                 result = _bt_avrcp_control_get_track_info();
2022                 /* Save invocation */
2023                 if (result == BLUETOOTH_ERROR_NONE) {
2024                         sender = (char*)g_dbus_method_invocation_get_sender(context);
2025                         _bt_save_invocation_context(context, result, sender,
2026                                         function_name, NULL);
2027                 }
2028                 break;
2029         }
2030         case BT_HDP_REGISTER_SINK_APP: {
2031                 unsigned short data_type;
2032                 bt_hdp_role_type_t role;
2033                 bt_hdp_qos_type_t channel_type;
2034                 int *app_id = g_new0(int, 1);
2035
2036                 __bt_service_get_parameters(in_param1,
2037                                 &data_type, sizeof(short));
2038                 __bt_service_get_parameters(in_param2,
2039                                 &role, sizeof(bt_hdp_role_type_t));
2040                 __bt_service_get_parameters(in_param3,
2041                                 &channel_type, sizeof(bt_hdp_qos_type_t));
2042                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2043
2044                 result = _bt_hdp_app_register(role,
2045                                 channel_type, data_type, sender, app_id);
2046                 if (result != BLUETOOTH_ERROR_NONE) {
2047                         g_array_append_vals(*out_param1, &app_id, sizeof(app_id));
2048                         g_free(app_id);
2049                 } else {
2050                         _bt_save_invocation_context(context, result, sender,
2051                                         function_name, (gpointer)app_id);
2052                 }
2053                 break;
2054         }
2055         case BT_HDP_UNREGISTER_SINK_APP: {
2056                 char *app_handle;
2057                 int *app_id = g_new0(int, 1);
2058
2059                 app_handle = (char *)g_variant_get_data(in_param1);
2060                 sscanf(app_handle, "health_app_%d", app_id);
2061                 result = _bt_hdp_app_unregister(*app_id);
2062                 if (result != BLUETOOTH_ERROR_NONE) {
2063                         g_free(app_id);
2064                 } else {
2065                         sender = (char*)g_dbus_method_invocation_get_sender(context);
2066                         _bt_save_invocation_context(context, result, sender,
2067                                         function_name, (gpointer)app_id);
2068                 }
2069                 break;
2070         }
2071         case BT_HDP_CONNECT: {
2072                 int app_id = -1;
2073                 char *app_handle;
2074
2075                 bt_hdp_connected_t *conn_info = NULL;
2076
2077                 conn_info = g_malloc0(sizeof(bt_hdp_connected_t));
2078
2079                 app_handle = (char *)g_variant_get_data(in_param1);
2080                 conn_info->app_handle = app_handle;
2081                 sscanf(app_handle, "health_app_%d", &app_id);
2082
2083                 __bt_service_get_parameters(in_param2,
2084                                 &(conn_info->type), sizeof(bt_hdp_qos_type_t));
2085                 __bt_service_get_parameters(in_param3,
2086                                 &(conn_info->device_address),
2087                                 sizeof(bluetooth_device_address_t));
2088
2089                 result = _bt_hdp_connect(app_id, &(conn_info->device_address),
2090                                 conn_info->type, (int *)(&(conn_info->channel_id)));
2091                 if (result != BLUETOOTH_ERROR_NONE) {
2092                         g_array_append_vals(*out_param1, conn_info,
2093                                         sizeof(bt_hdp_connected_t));
2094                         g_free(conn_info);
2095                 } else {
2096                         sender = (char*)g_dbus_method_invocation_get_sender(context);
2097                         _bt_save_invocation_context(context, result, sender,
2098                                         function_name, (gpointer)conn_info);
2099                 }
2100                 break;
2101         }
2102         case BT_HDP_DISCONNECT: {
2103                 bt_hdp_disconnected_t *hdp_disconn_info = NULL;
2104
2105                 hdp_disconn_info = g_malloc0(sizeof(bt_hdp_disconnected_t));
2106
2107                 __bt_service_get_parameters(in_param1,
2108                                 &(hdp_disconn_info->channel_id), sizeof(int));
2109                 __bt_service_get_parameters(in_param2,
2110                                 &(hdp_disconn_info->device_address),
2111                                 sizeof(bluetooth_device_address_t));
2112
2113                 result = _bt_hdp_disconnect(hdp_disconn_info->channel_id);
2114                 if (result != BLUETOOTH_ERROR_NONE) {
2115                         g_array_append_vals(*out_param1, hdp_disconn_info,
2116                                         sizeof(bt_hdp_disconnected_t));
2117                         g_free(hdp_disconn_info);
2118                 } else {
2119                         sender = (char*)g_dbus_method_invocation_get_sender(context);
2120                         _bt_save_invocation_context(context, result, sender,
2121                                         function_name, (gpointer)hdp_disconn_info);
2122                 }
2123                 break;
2124         }
2125         case BT_HDP_GET_FD: {
2126                 int *channel_id = g_new0(int, 1);
2127
2128                 __bt_service_get_parameters(in_param1, channel_id, sizeof(int));
2129
2130                 result = _bt_hdp_get_fd(*channel_id);
2131                 if (result != BLUETOOTH_ERROR_NONE) {
2132                         g_free(channel_id);
2133                 } else {
2134                         sender = (char*)g_dbus_method_invocation_get_sender(context);
2135                         _bt_save_invocation_context(context, result, sender,
2136                                         function_name, (gpointer)channel_id);
2137                 }
2138                 break;
2139         }
2140         case BT_HDP_SEND_DATA: {
2141                 /*
2142                  * This call to bt-service is only used for privilege check, so return
2143                  * BLUETOOTH_ERROR_NONE from here.
2144                  */
2145                 result = BLUETOOTH_ERROR_NONE;
2146                 break;
2147         }
2148         case BT_OOB_READ_LOCAL_DATA: {  //208
2149                 bt_oob_data_t local_oob_data;
2150
2151                 memset(&local_oob_data, 0x00, sizeof(bt_oob_data_t));
2152                 result = _bt_oob_read_local_data(&local_oob_data);
2153
2154                 g_array_append_vals(*out_param1, &local_oob_data,
2155                                 sizeof(bt_oob_data_t));
2156
2157                 break;
2158         }
2159         case BT_OOB_ADD_REMOTE_DATA: {   //209
2160                 bluetooth_device_address_t address = { {0} };
2161                 unsigned short address_type;
2162                 bt_oob_data_t remote_oob_data;
2163
2164                 __bt_service_get_parameters(in_param1,
2165                                 &address, sizeof(bluetooth_device_address_t));
2166                 __bt_service_get_parameters(in_param2,
2167                                 &address_type, sizeof(unsigned short));
2168                 __bt_service_get_parameters(in_param3,
2169                                 &remote_oob_data, sizeof(bt_oob_data_t));
2170
2171                 result = _bt_oob_add_remote_data(&address, address_type, &remote_oob_data);
2172
2173                 break;
2174         }
2175         case BT_OOB_REMOVE_REMOTE_DATA: {       //210
2176                 bluetooth_device_address_t address = { {0} };
2177
2178                 __bt_service_get_parameters(in_param1,
2179                                 &address, sizeof(bluetooth_device_address_t));
2180
2181                 result = _bt_oob_remove_remote_data(&address);
2182
2183                 break;
2184         }
2185         case BT_LE_OOB_READ_LOCAL_DATA: {
2186                 bt_oob_data_t local_oob_data;
2187                 bt_oob_data_t *local_oob_cache_data;
2188
2189                 local_oob_cache_data = _bt_le_oob_get_local_cache_data();
2190                 if (local_oob_cache_data && local_oob_cache_data->eir_len) {
2191                         g_array_append_vals(*out_param1, local_oob_cache_data,
2192                                 sizeof(bt_oob_data_t));
2193                         return BLUETOOTH_ERROR_NONE;
2194                 }
2195
2196                 /* We should modify this logic in later */
2197                 if (_bt_is_le_static_random_address_enabled() == false) {
2198                         /* Seperate BT & LE address */
2199                         _bt_set_le_static_random_address(TRUE);
2200                 }
2201
2202                 result = _bt_le_oob_read_local_data(&local_oob_data);
2203                 _bt_le_oob_set_local_cache_data(&local_oob_data);
2204
2205                 g_array_append_vals(*out_param1, &local_oob_data,
2206                                 sizeof(bt_oob_data_t));
2207
2208                 break;
2209         }
2210         case BT_LE_OOB_ADD_REMOTE_DATA: {
2211                 bluetooth_device_address_t address = { {0} };
2212                 unsigned short address_type;
2213                 bt_oob_data_t remote_oob_data;
2214
2215                 __bt_service_get_parameters(in_param1,
2216                                 &address, sizeof(bluetooth_device_address_t));
2217                 __bt_service_get_parameters(in_param2,
2218                                 &address_type, sizeof(unsigned short));
2219                 __bt_service_get_parameters(in_param3,
2220                                 &remote_oob_data, sizeof(bt_oob_data_t));
2221
2222                 result = _bt_oob_add_remote_data(&address, address_type, &remote_oob_data);
2223
2224                 break;
2225         }
2226         case BT_AVRCP_SET_TRACK_INFO: {
2227                 media_metadata_t data;
2228                 media_metadata_attributes_t meta_data;
2229
2230                 memset(&data, 0x00, sizeof(media_metadata_t));
2231                 memset(&meta_data, 0x00, sizeof(media_metadata_attributes_t));
2232
2233                 __bt_service_get_parameters(in_param1,
2234                                 &data, sizeof(media_metadata_t));
2235
2236                 meta_data.title = g_strdup(data.title);
2237                 meta_data.artist = g_strdup(data.artist);
2238                 meta_data.album = g_strdup(data.album);
2239                 meta_data.genre = g_strdup(data.genre);
2240                 meta_data.total_tracks = data.total_tracks;
2241                 meta_data.number = data.number;
2242                 meta_data.duration = (int64_t) data.duration;
2243
2244                 result = _bt_avrcp_set_track_info(&meta_data);
2245
2246                 g_free((gpointer)meta_data.title);
2247                 g_free((gpointer)meta_data.artist);
2248                 g_free((gpointer)meta_data.album);
2249                 g_free((gpointer)meta_data.genre);
2250
2251                 break;
2252         }
2253         case BT_AVRCP_SET_PROPERTY: {
2254                 int type;
2255                 unsigned int value;
2256
2257                 sender = (char *)g_dbus_method_invocation_get_sender(context);
2258
2259                 __bt_service_get_parameters(in_param1,
2260                                 &type, sizeof(int));
2261                 __bt_service_get_parameters(in_param2,
2262                                 &value, sizeof(unsigned int));
2263
2264                 if (value == STATUS_PLAYING) {
2265                         if (current_sender_playing)
2266                                 g_free(current_sender_playing);
2267                         current_sender_playing = g_strdup(sender);
2268                 }
2269                 if (g_strcmp0(sender, current_sender_playing) == 0 ||
2270                         current_sender_playing == NULL) {
2271                         BT_INFO("Current Player Status %d type %d Sender %s", value, type, sender);
2272                 } else {
2273                         BT_INFO("Current Player and this sender (%s) are different", sender);
2274                         result = BLUETOOTH_ERROR_NONE;
2275                         break;
2276                 }
2277                 result = _bt_avrcp_set_property(type, value);
2278
2279                 break;
2280         }
2281         case BT_AVRCP_SET_PROPERTIES: {
2282                 media_player_settings_t properties;
2283
2284                 memset(&properties, 0x00, sizeof(media_player_settings_t));
2285                 __bt_service_get_parameters(in_param1,
2286                                 &properties, sizeof(media_player_settings_t));
2287
2288                 result = _bt_avrcp_set_properties(&properties);
2289
2290                 break;
2291         }
2292         case BT_SET_ADVERTISING_DATA: {
2293                 char *app = NULL;
2294                 int *adv_handle;
2295                 bluetooth_advertising_data_t adv = { {0} };
2296                 int length;
2297                 bluetooth_le_slot_and_adv_type_t rs_at;
2298                 rs_at.is_legacy = TRUE;
2299                 rs_at.use_reserved_slot = FALSE;
2300
2301                 app = (char *)g_dbus_method_invocation_get_sender(context);
2302                 adv_handle = g_malloc0(sizeof(int));
2303
2304                 __bt_service_get_parameters(in_param1,
2305                                 adv_handle, sizeof(int));
2306                 __bt_service_get_parameters(in_param3,
2307                                 &length, sizeof(int));
2308                 __bt_service_get_parameters(in_param2,
2309                                 &adv, length);
2310                 __bt_service_get_parameters(in_param4,
2311                                 &rs_at, sizeof(bluetooth_le_slot_and_adv_type_t));
2312                 result = _bt_set_advertising_data(app, *adv_handle,
2313                                 &adv, length, &rs_at);
2314                 if (result != BLUETOOTH_ERROR_NONE) {
2315                         BT_ERR("Set Advertising data failed!!");
2316                         g_free(adv_handle);
2317                 } else {
2318                         _bt_save_invocation_context(context, result, app,
2319                                         function_name, (gpointer)adv_handle);
2320                 }
2321                 break;
2322         }
2323         case BT_SET_SCAN_RESPONSE_DATA: {
2324                 char *app = NULL;
2325                 int *adv_handle;
2326                 bluetooth_scan_resp_data_t rsp = { {0} };
2327                 int length;
2328                 gboolean use_reserved_slot = FALSE;
2329
2330                 app = (char *)g_dbus_method_invocation_get_sender(context);
2331                 adv_handle = g_malloc0(sizeof(int));
2332
2333                 __bt_service_get_parameters(in_param1,
2334                                 adv_handle, sizeof(int));
2335                 __bt_service_get_parameters(in_param3,
2336                                 &length, sizeof(int));
2337                 __bt_service_get_parameters(in_param2,
2338                                 &rsp, length);
2339                 __bt_service_get_parameters(in_param4,
2340                                 &use_reserved_slot, sizeof(gboolean));
2341
2342                 result = _bt_set_scan_response_data(app, *adv_handle,
2343                                 &rsp, length, use_reserved_slot);
2344
2345                 if (result != BLUETOOTH_ERROR_NONE) {
2346                         BT_ERR("Set Scan Response Data failed!!");
2347                         g_free(adv_handle);
2348                 } else {
2349                         _bt_save_invocation_context(context, result, app,
2350                                         function_name, (gpointer)adv_handle);
2351                 }
2352                 break;
2353         }
2354         case BT_SET_ADVERTISING: {
2355                 char *app = NULL;
2356                 int *adv_handle;
2357                 gboolean enable = FALSE;
2358                 gboolean use_reserved_slot = FALSE;
2359
2360                 adv_handle = g_malloc0(sizeof(int));
2361                 __bt_service_get_parameters(in_param1,
2362                                 adv_handle, sizeof(int));
2363                 __bt_service_get_parameters(in_param2,
2364                                 &enable, sizeof(gboolean));
2365                 __bt_service_get_parameters(in_param3,
2366                                 &use_reserved_slot, sizeof(gboolean));
2367
2368                 app = (char *)g_dbus_method_invocation_get_sender(context);
2369
2370                 result = _bt_set_advertising(app, *adv_handle,
2371                                 enable, use_reserved_slot);
2372                 if (result != BLUETOOTH_ERROR_NONE) {
2373                         BT_ERR("Start Advertising failed!!");
2374                         g_free(adv_handle);
2375                 } else {
2376                         _bt_save_invocation_context(context, result, app,
2377                                         function_name, (gpointer)adv_handle);
2378                 }
2379                 break;
2380         }
2381         case BT_SET_CUSTOM_ADVERTISING: {
2382                 char *app = NULL;
2383                 int *adv_handle;
2384                 gboolean enable = FALSE;
2385                 bluetooth_advertising_params_t adv_params;
2386                 gboolean use_reserved_slot = FALSE;
2387
2388                 app = (char *)g_dbus_method_invocation_get_sender(context);
2389                 adv_handle = g_malloc0(sizeof(int));
2390
2391                 __bt_service_get_parameters(in_param1, adv_handle,
2392                                 sizeof(int));
2393                 __bt_service_get_parameters(in_param2, &enable,
2394                                 sizeof(gboolean));
2395                 __bt_service_get_parameters(in_param3, &adv_params,
2396                                 sizeof(bluetooth_advertising_params_t));
2397                 __bt_service_get_parameters(in_param4, &use_reserved_slot,
2398                                 sizeof(gboolean));
2399
2400                 BT_DBG("bluetooth_advertising_params_t [%d %d %d %d %d]",
2401                                 adv_params.interval_min, adv_params.interval_max,
2402                                 adv_params.filter_policy, adv_params.type, adv_params.tx_power_level);
2403                 result = _bt_set_custom_advertising(app, *adv_handle,
2404                                 enable, &adv_params, use_reserved_slot);
2405                 if (result != BLUETOOTH_ERROR_NONE) {
2406                         BT_ERR("Start Custom Advertising failed!!");
2407                         g_free(adv_handle);
2408                 } else {
2409                         _bt_save_invocation_context(context, result, app,
2410                                         function_name, (gpointer)adv_handle);
2411                 }
2412                 break;
2413         }
2414         case BT_GET_SCAN_RESPONSE_DATA: {
2415                 bluetooth_scan_resp_data_t rsp = { {0} };
2416                 char *app = NULL;
2417                 int length = 0;
2418                 int adv_handle;
2419                 app = (char *)g_dbus_method_invocation_get_sender(context);
2420
2421                 __bt_service_get_parameters(in_param1, &adv_handle,
2422                                 sizeof(int));
2423
2424                 result = _bt_get_scan_response_data(app, adv_handle, &rsp, &length);
2425                 if (result == BLUETOOTH_ERROR_NONE)
2426                         g_array_append_vals(*out_param1, rsp.data, length);
2427
2428                 break;
2429         }
2430         case BT_GET_ADVERTISING_DATA: {
2431                 bluetooth_advertising_data_t adv = { {0} };
2432                 char *app = NULL;
2433                 int length = 0;
2434                 int adv_handle;
2435                 app = (char *)g_dbus_method_invocation_get_sender(context);
2436
2437                 __bt_service_get_parameters(in_param1, &adv_handle,
2438                                 sizeof(int));
2439
2440                 result = _bt_get_advertising_data(app, adv_handle, &adv, &length);
2441                 if (result == BLUETOOTH_ERROR_NONE)
2442                         g_array_append_vals(*out_param1, adv.data, length);
2443
2444                 break;
2445         }
2446         case BT_GATT_REGISTER_APPLICATION: {
2447                 BT_DBG("Register GATT application:Unhandled!!");
2448                 break;
2449         }
2450         case BT_GATT_SERVER_REGISTER: {
2451                 BT_DBG("GATT Server instance initialization");
2452                 char *app;
2453
2454                 app = (char *)g_dbus_method_invocation_get_sender(context);
2455                 BT_DBG("GATT Server Unique Name [%s]", app);
2456
2457                 /* No ADV handle: Set 0 */
2458                 result = _bt_register_server_instance(app, 0);
2459
2460                 if (result != BLUETOOTH_ERROR_NONE) {
2461                         BT_ERR("GATT Server registration failed. result %d", result);
2462                 } else {
2463                         _bt_save_invocation_context(context, result, app,
2464                                         function_name, NULL);
2465                 }
2466                 break;
2467         }
2468         case BT_GATT_SERVER_DEREGISTER: {
2469                 char *app;
2470                 app = (char*)g_dbus_method_invocation_get_sender(context);
2471
2472                 result = _bt_unregister_server_instance(app, 0/* Adv Handle*/);
2473                 break;
2474         }
2475         case BT_GATT_SERVER_ADD_SERVICE: {
2476                 BT_DBG("GATT Server Add Service");
2477                 int service_type;
2478                 int num_handles;
2479                 char *svc_uuid;
2480                 int instance_id;
2481                 char *app;
2482                 int *tmp_inst_id = NULL;
2483
2484                 app = (char *)g_dbus_method_invocation_get_sender(context);
2485
2486                 __bt_service_get_parameters(in_param1, &service_type,
2487                                 sizeof(int));
2488                 __bt_service_get_parameters(in_param2, &num_handles,
2489                                 sizeof(int));
2490                 svc_uuid = (char *)g_variant_get_data(in_param3);
2491                 __bt_service_get_parameters(in_param4, &instance_id,
2492                                 sizeof(int));
2493
2494                 result = _bt_gatt_server_add_service(app, service_type, num_handles, svc_uuid, instance_id);
2495                 if (result != BLUETOOTH_ERROR_NONE) {
2496                         BT_ERR("GATT Server Add Service failed!!");
2497                 } else {
2498                         tmp_inst_id = g_malloc0(sizeof(int));
2499                         *tmp_inst_id = instance_id;
2500                         _bt_save_invocation_context(context, result, app,
2501                                         function_name, (gpointer)tmp_inst_id);
2502                 }
2503                 break;
2504         }
2505         case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
2506                 BT_DBG("GATT Server Add Characteristic");
2507                 char *char_uuid;
2508                 char *app;
2509                 int *tmp_inst_id = NULL;
2510                 bluetooth_gatt_server_attribute_params_t param;
2511                 memset(&param, 0, sizeof(bluetooth_gatt_server_attribute_params_t));
2512
2513                 app = (char*)g_dbus_method_invocation_get_sender(context);
2514
2515                 __bt_service_get_parameters(in_param1, &param,
2516                                 sizeof(bluetooth_gatt_server_attribute_params_t));
2517                 char_uuid = (char *)g_variant_get_data(in_param2);
2518
2519                 result = _bt_gatt_server_add_characteristic(app, char_uuid, &param);
2520                 if (result != BLUETOOTH_ERROR_NONE) {
2521                         BT_ERR("GATT Server Add Service failed!!");
2522                 } else {
2523                         tmp_inst_id = g_malloc0(sizeof(int));
2524                         *tmp_inst_id = param.instance_id;
2525
2526                         _bt_save_invocation_context(context, result, app,
2527                                         function_name, (gpointer)tmp_inst_id);
2528                 }
2529                 break;
2530         }
2531         case BT_GATT_SERVER_ADD_DESCRIPTOR: {
2532                 BT_DBG("GATT Server Add Descriptor");
2533                 char *desc_uuid;
2534                 int instance_id;
2535                 int service_handle;
2536                 bt_gatt_permission_t perm;
2537                 char *app;
2538                 int *tmp_inst_id = NULL;
2539                 memset(&perm, 0, sizeof(bt_gatt_permission_t));
2540
2541                 app = (char *)g_dbus_method_invocation_get_sender(context);
2542
2543                 __bt_service_get_parameters(in_param1, &service_handle,
2544                                 sizeof(int));
2545                 __bt_service_get_parameters(in_param2, &instance_id,
2546                                 sizeof(int));
2547                 desc_uuid = (char *)g_variant_get_data(in_param4);
2548                 __bt_service_get_parameters(in_param3, &perm,
2549                                 sizeof(bt_gatt_permission_t));
2550
2551                 result = _bt_gatt_server_add_descriptor(app, desc_uuid, &perm, service_handle, instance_id);
2552                 if (result != BLUETOOTH_ERROR_NONE) {
2553                         BT_ERR("GATT Server Add Service failed!!");
2554                 } else {
2555                         tmp_inst_id = g_malloc0(sizeof(int));
2556                         *tmp_inst_id = instance_id;
2557                         _bt_save_invocation_context(context, result, app,
2558                                         function_name, (gpointer)tmp_inst_id);
2559                 }
2560                 break;
2561         }
2562         case BT_GATT_SERVER_START_SERVICE: {
2563                 int service_handle;
2564                 int instance_id;
2565
2566                 char *app;
2567                 int *tmp_inst_id = NULL;
2568                 app = (char*)g_dbus_method_invocation_get_sender(context);
2569
2570                 __bt_service_get_parameters(in_param1, &service_handle,
2571                                 sizeof(int));
2572                 __bt_service_get_parameters(in_param2, &instance_id,
2573                                 sizeof(int));
2574
2575                 result = _bt_gatt_server_start_service(app, service_handle, instance_id);
2576
2577                 if (BLUETOOTH_ERROR_NONE == result) {
2578                         tmp_inst_id = g_malloc0(sizeof(int));
2579                         *tmp_inst_id = instance_id;
2580                         _bt_save_invocation_context(context, result, app,
2581                                         function_name, (gpointer)tmp_inst_id);
2582                 }
2583                 break;
2584         }
2585         case BT_GATT_SERVER_STOP_SERVICE: {
2586                 int service_handle;
2587                 int instance_id;
2588                 char *app;
2589                 int *tmp_inst_id = NULL;
2590                 app = (char*)g_dbus_method_invocation_get_sender(context);
2591
2592                 __bt_service_get_parameters(in_param1, &service_handle,
2593                                 sizeof(int));
2594                 __bt_service_get_parameters(in_param2, &instance_id,
2595                                 sizeof(int));
2596
2597                 result = _bt_gatt_server_stop_service(app, service_handle, instance_id);
2598
2599                 if (BLUETOOTH_ERROR_NONE == result) {
2600                         tmp_inst_id = g_malloc0(sizeof(int));
2601                         *tmp_inst_id = instance_id;
2602                         _bt_save_invocation_context(context, result, app,
2603                                         function_name, (gpointer)tmp_inst_id);
2604                 }
2605                 break;
2606         }
2607         case BT_GATT_SERVER_DELETE_SERVICE: {
2608                 int service_handle;
2609                 int instance_id;
2610                 int *tmp_inst_id = NULL;
2611                 char *app;
2612                 app = (char*)g_dbus_method_invocation_get_sender(context);
2613
2614                 __bt_service_get_parameters(in_param1, &service_handle,
2615                                 sizeof(int));
2616                 __bt_service_get_parameters(in_param2, &instance_id,
2617                                 sizeof(int));
2618
2619                 result = _bt_gatt_server_delete_service(app, service_handle, instance_id);
2620
2621                 if (BLUETOOTH_ERROR_NONE == result) {
2622                         tmp_inst_id = g_malloc0(sizeof(int));
2623                         *tmp_inst_id = instance_id;
2624                         _bt_save_invocation_context(context, result, app,
2625                                         function_name, (gpointer)tmp_inst_id);
2626                 }
2627                 break;
2628         }
2629         case BT_GATT_SERVER_SET_SERVICE_PERSISTENCE: {
2630                 char *uuid;
2631                 uuid = (char *)g_variant_get_data(in_param1);
2632                 result = _bt_gatt_server_set_service_persistence(uuid);
2633                 break;
2634         }
2635         case BT_GATT_SERVER_UNSET_SERVICE_PERSISTENCE: {
2636                 char *uuid;
2637                 uuid = (char *)g_variant_get_data(in_param1);
2638                 result = _bt_gatt_server_unset_service_persistence(uuid);
2639                 break;
2640         }
2641         case BT_GATT_SERVER_SEND_RESPONSE: {
2642                 bluetooth_gatt_server_response_params_t param;
2643                 bluetooth_gatt_att_data_t data;
2644                 char *app;
2645
2646                 memset(&param, 0x00, sizeof(bluetooth_gatt_server_response_params_t));
2647                 memset(&data, 0x00, sizeof(bluetooth_gatt_att_data_t));
2648
2649                 app = (char*)g_dbus_method_invocation_get_sender(context);
2650
2651                 __bt_service_get_parameters(in_param1, &data,
2652                                 sizeof(bluetooth_gatt_att_data_t));
2653                 __bt_service_get_parameters(in_param2, &param,
2654                                 sizeof(bluetooth_gatt_server_response_params_t));
2655
2656                 result = _bt_gatt_server_send_response(app, &data, &param);
2657
2658                 break;
2659         }
2660         case BT_GATT_SERVER_ACQURE_WRITE_RESPONSE: {
2661
2662                 bluetooth_gatt_server_acquire_response_params_t param;
2663                 char *app;
2664                 GDBusMessage *msg;
2665                 msg = g_dbus_method_invocation_get_message(context);
2666                 GUnixFDList *fd_list;
2667                 int fd  = -1;;
2668                 int *fd_list_array;
2669                 int len;
2670
2671                 fd_list = g_dbus_message_get_unix_fd_list(msg);
2672
2673                 memset(&param, 0x00, sizeof(bluetooth_gatt_server_acquire_response_params_t));
2674
2675                 app = (char*)g_dbus_method_invocation_get_sender(context);
2676
2677                 __bt_service_get_parameters(in_param1, &param,
2678                                 sizeof(bluetooth_gatt_server_acquire_response_params_t));
2679
2680                 fd_list_array = (int *)g_unix_fd_list_peek_fds(fd_list, &len);
2681                 BT_DBG("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
2682                 fd = fd_list_array[0];
2683
2684                 param.fd = fd;
2685                 result = _bt_gatt_server_acquire_send_response(app, &param, fd_list);
2686
2687                 break;
2688         }
2689         case BT_GATT_SERVER_ACQUIRE_NOTIFY_RESPONSE: {
2690
2691                 bluetooth_gatt_server_acquire_response_params_t param;
2692                 char *app;
2693                 GDBusMessage *msg;
2694                 msg = g_dbus_method_invocation_get_message(context);
2695                 GUnixFDList *fd_list;
2696                 int fd  = -1;
2697                 int *fd_list_array;
2698                 int len;
2699
2700                 fd_list = g_dbus_message_get_unix_fd_list(msg);
2701
2702                 memset(&param, 0x00, sizeof(bluetooth_gatt_server_acquire_response_params_t));
2703
2704                 app = (char*)g_dbus_method_invocation_get_sender(context);
2705
2706                 __bt_service_get_parameters(in_param1, &param,
2707                                 sizeof(bluetooth_gatt_server_acquire_response_params_t));
2708
2709                 fd_list_array = (int *)g_unix_fd_list_peek_fds(fd_list, &len);
2710                 BT_DBG("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
2711                 fd = fd_list_array[0];
2712
2713                 param.fd = fd;
2714                 result = _bt_gatt_server_acquire_send_response(app, &param, fd_list);
2715
2716                 break;
2717         }
2718         case BT_GATT_SERVER_SEND_INDICATION: {
2719                 bluetooth_gatt_server_indication_params_t param;
2720                 bluetooth_gatt_att_data_t data;
2721                 bluetooth_device_address_t address;
2722
2723                 char *app;
2724                 app = (char*)g_dbus_method_invocation_get_sender(context);
2725
2726                 memset(&param, 0x00, sizeof(bluetooth_gatt_server_indication_params_t));
2727                 memset(&data, 0x00, sizeof(bluetooth_gatt_att_data_t));
2728                 memset(&address, 0x00, sizeof(bluetooth_device_address_t));
2729
2730                 __bt_service_get_parameters(in_param1, &data,
2731                                 sizeof(bluetooth_gatt_att_data_t));
2732                 __bt_service_get_parameters(in_param2, &param,
2733                                 sizeof(bluetooth_gatt_server_indication_params_t));
2734                 __bt_service_get_parameters(in_param3, &address,
2735                                 sizeof(bluetooth_device_address_t));
2736
2737                 result = _bt_gatt_server_send_indication(app, &address, &data, &param);
2738
2739                 break;
2740         }
2741         case BT_GATT_SERVER_UPDATE_VALUE: {
2742                 bluetooth_gatt_server_update_value_t param;
2743                 int instance_id;
2744                 char *app;
2745
2746                 memset(&param, 0x00, sizeof(bluetooth_gatt_server_update_value_t));
2747
2748                 app = (char*)g_dbus_method_invocation_get_sender(context);
2749
2750                 __bt_service_get_parameters(in_param1, &instance_id,
2751                                 sizeof(int));
2752                 __bt_service_get_parameters(in_param2, &param,
2753                                 sizeof(bluetooth_gatt_server_update_value_t));
2754
2755                 result = _bt_gatt_server_update_attribute_value(app, instance_id, &param);
2756                 break;
2757         }
2758         case BT_GATT_SERVER_READ_PHY: {
2759                 bluetooth_device_address_t address = { {0} };
2760
2761                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2762
2763                 __bt_service_get_parameters(in_param1,
2764                                 &address, sizeof(bluetooth_device_address_t));
2765
2766                 result = _bt_gatt_server_read_phy(&address);
2767                 break;
2768         }
2769         case BT_GATT_SERVER_SET_PHY: {
2770                 bluetooth_device_address_t address = { {0} };
2771                 int tx_phy;
2772                 int rx_phy;
2773                 int phy_options;
2774
2775                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2776
2777                 __bt_service_get_parameters(in_param1,
2778                                 &address, sizeof(bluetooth_device_address_t));
2779                 __bt_service_get_parameters(in_param2,
2780                                 &tx_phy, sizeof(int));
2781                 __bt_service_get_parameters(in_param3,
2782                                 &rx_phy, sizeof(int));
2783                 __bt_service_get_parameters(in_param4,
2784                                 &phy_options, sizeof(int));
2785
2786                 result = _bt_gatt_server_set_phy(&address, tx_phy, rx_phy, phy_options);
2787                 break;
2788         }
2789         case BT_GATT_CLIENT_READ_PHY: {
2790                 bluetooth_device_address_t address = { {0} };
2791
2792                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2793
2794                 __bt_service_get_parameters(in_param1,
2795                                 &address, sizeof(bluetooth_device_address_t));
2796
2797                 result = _bt_gatt_client_read_phy(&address);
2798                 break;
2799         }
2800         case BT_GATT_CLIENT_SET_PHY: {
2801                 bluetooth_device_address_t address = { {0} };
2802                 int tx_phy;
2803                 int rx_phy;
2804                 int phy_options;
2805
2806                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2807
2808                 __bt_service_get_parameters(in_param1,
2809                                 &address, sizeof(bluetooth_device_address_t));
2810                 __bt_service_get_parameters(in_param2,
2811                                 &tx_phy, sizeof(int));
2812                 __bt_service_get_parameters(in_param3,
2813                                 &rx_phy, sizeof(int));
2814                 __bt_service_get_parameters(in_param4,
2815                                 &phy_options, sizeof(int));
2816
2817                 result = _bt_gatt_client_set_phy(&address, tx_phy, rx_phy, phy_options);
2818                 break;
2819         }
2820         case BT_GET_ATT_MTU: {
2821                 bluetooth_device_address_t address = { {0} };
2822                 unsigned int mtu = 0;
2823
2824                 __bt_service_get_parameters(in_param1,
2825                                 &address, sizeof(bluetooth_device_address_t));
2826                 result = _bt_get_att_mtu(&address, &mtu);
2827                 BT_DBG("MTU: %d", mtu);
2828
2829                 if (result == BLUETOOTH_ERROR_NONE) {
2830                         g_array_append_vals(*out_param1, &mtu,
2831                                         sizeof(unsigned int));
2832                 }
2833                 break;
2834         }
2835         case BT_REQ_ATT_MTU: {
2836                 bluetooth_device_address_t address = { {0} };
2837                 unsigned int mtu;
2838                 char *addr;
2839
2840                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2841
2842                 __bt_service_get_parameters(in_param1,
2843                                 &address, sizeof(bluetooth_device_address_t));
2844                 __bt_service_get_parameters(in_param2,
2845                                 &mtu, sizeof(unsigned int));
2846                 BT_DBG("BT_REQ_ATT_MTU: %d", mtu);
2847                 result = _bt_request_att_mtu(&address, mtu);
2848                 if (BLUETOOTH_ERROR_NONE == result) {
2849                         addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
2850
2851                         _bt_convert_addr_type_to_string(addr, address.addr);
2852
2853                         /* Save the informations to invocation */
2854                         _bt_save_invocation_context(context, result, sender,
2855                                         function_name, (gpointer)addr);
2856                 } else {
2857                         g_array_append_vals(*out_param1, &address,
2858                                         sizeof(bluetooth_device_address_t));
2859                 }
2860                 break;
2861         }
2862         case BT_GET_DEVICE_IDA: {
2863                 bluetooth_device_address_t address = { {0} };
2864                 bluetooth_device_address_t id_addr = { {0} };
2865
2866                 __bt_service_get_parameters(in_param1,
2867                                 &address, sizeof(bluetooth_device_address_t));
2868
2869                 result = _bt_device_get_ida(&address, &id_addr);
2870
2871                 if (result == BLUETOOTH_ERROR_NONE) {
2872                         g_array_append_vals(*out_param1, &id_addr,
2873                                         sizeof(bluetooth_device_address_t));
2874                 }
2875                 break;
2876         }
2877         case BT_SET_LE_STATIC_RANDOM_ADDRESS: {
2878                 gboolean is_enable;
2879
2880                 __bt_service_get_parameters(in_param1, &is_enable,
2881                                 sizeof(gboolean));
2882
2883                 result = _bt_set_le_static_random_address(is_enable);
2884
2885                 break;
2886         }
2887         /* Local Async */
2888         case BT_GATT_CLIENT_REGISTER: {
2889                 bluetooth_device_address_t address = { {0} };
2890                 char *addr;
2891
2892                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2893
2894                 __bt_service_get_parameters(in_param1,
2895                                 &address, sizeof(bluetooth_device_address_t));
2896
2897                 result = _bt_register_gatt_client_instance(sender, &address);
2898                 if (BLUETOOTH_ERROR_NONE == result) {
2899                         addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
2900
2901                         _bt_convert_addr_type_to_string(addr, address.addr);
2902
2903                         /* Save the informations to invocation */
2904                         _bt_save_invocation_context(context, result, sender,
2905                                         function_name, (gpointer)addr);
2906                 }
2907                 break;
2908         }
2909         /* Sync */
2910         case BT_GATT_CLIENT_UNREGISTER: {
2911                 int client_id;
2912
2913                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2914                 __bt_service_get_parameters(in_param1,
2915                                 &client_id, sizeof(int));
2916
2917                 result = _bt_unregister_gatt_client_instance(sender, client_id);
2918                 break;
2919         }
2920
2921         /* Async: Remote Interaction  */
2922         case BT_GATT_ACQUIRE_WRITE: {
2923
2924                 int fd = -1;
2925                 int mtu = -1;
2926                 bluetooth_gatt_client_char_prop_info_t param;
2927
2928                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2929
2930                 __bt_service_get_parameters(in_param1, &param,
2931                                         sizeof(bluetooth_gatt_client_char_prop_info_t));
2932
2933
2934                 result   = _bt_gatt_acquire_write(&param , &fd, &mtu);
2935
2936                 g_array_append_vals(*out_param1, &mtu, sizeof(int));
2937
2938                 if (BLUETOOTH_ERROR_NONE == result)
2939                         BT_DBG("GATT Client: Save Invocation data for characteristic props app[%s] fd[ %d]", sender, fd);
2940                 else {
2941                         BT_ERR("GATT Client: gatt acquire write failed");
2942                         break;
2943                 }
2944
2945                 GUnixFDList *fd_list = NULL;
2946                 GError *error = NULL;
2947
2948                 /* Add socket fd to unix_fd_list */
2949                 fd_list = g_unix_fd_list_new();
2950                 g_unix_fd_list_append(fd_list, fd, &error);
2951                 g_assert_no_error(error);
2952
2953                 _bt_service_method_return_with_unix_fd_list(
2954                                 context, *out_param1, result, fd_list);
2955
2956                 close(fd);
2957                 g_object_unref(fd_list);
2958
2959                 break;
2960         }
2961         /* Async: Remote Interaction  */
2962         case BT_CONNECT_LE: {
2963                 bluetooth_device_address_t address = { {0} };
2964                 gboolean auto_connect;
2965                 char *addr;
2966                 int client_id;
2967
2968                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2969
2970                 __bt_service_get_parameters(in_param1,
2971                                 &address, sizeof(bluetooth_device_address_t));
2972                 __bt_service_get_parameters(in_param2,
2973                                 &auto_connect, sizeof(gboolean));
2974                 __bt_service_get_parameters(in_param3,
2975                                 &client_id, sizeof(int));
2976
2977                 BT_DBG("GATT Client: client_id[%d]", client_id);
2978                 result = _bt_connect_le_device(&address, auto_connect, client_id);
2979                 if (BLUETOOTH_ERROR_NONE == result) {
2980                         addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
2981                         _bt_convert_addr_type_to_string(addr, address.addr);
2982
2983                         /* Save the informations to invocation */
2984                         _bt_save_invocation_context(context, result, sender,
2985                                         function_name, (gpointer)addr);
2986
2987                         if (auto_connect)
2988                                 _bt_handle_invocation_context(function_name, (void *)addr);
2989                 } else {
2990                         BT_ERR("GATT Client: gatt connect failed. client_id[%d]", client_id);
2991                         g_array_append_vals(*out_param1, &address,
2992                                         sizeof(bluetooth_device_address_t));
2993                 }
2994                 break;
2995         }
2996         /* Async: Remote Interaction  */
2997         case BT_DISCONNECT_LE: {
2998                 bluetooth_device_address_t address = { {0} };
2999                 char *addr;
3000                 int client_id;
3001
3002                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3003
3004                 __bt_service_get_parameters(in_param1,
3005                                 &address, sizeof(bluetooth_device_address_t));
3006                 __bt_service_get_parameters(in_param2,
3007                                 &client_id, sizeof(int));
3008
3009                 BT_INFO("GATT Disconnect using client Interface [%d]", client_id);
3010                 result = _bt_disconnect_le_device(&address, client_id);
3011                 if (BLUETOOTH_ERROR_NONE == result) {
3012                         addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
3013
3014                         _bt_convert_addr_type_to_string(addr, address.addr);
3015
3016                         /* Save the informations to invocation */
3017                         _bt_save_invocation_context(context, result, sender,
3018                                         function_name, (gpointer)addr);
3019                 } else {
3020                         g_array_append_vals(*out_param1, &address,
3021                                         sizeof(bluetooth_device_address_t));
3022                 }
3023                 break;
3024         }
3025         /* Sync */
3026         case BT_GET_GATT_DATA_BATCHING_AVAILABLE_PACKETS: {
3027                 guint available_packets = 0;
3028
3029                 result = _bt_gatt_get_data_batching_available_packets(&available_packets);
3030                 BT_DBG("LE batching available packets %u", available_packets);
3031                 if (result == BLUETOOTH_ERROR_NONE) {
3032                         g_array_append_vals(*out_param1, &available_packets,
3033                                         sizeof(guint));
3034                 }
3035
3036                 break;
3037         }
3038         /* Sync */
3039         case BT_ENABLE_GATT_DATA_BATCHING: {
3040                 bluetooth_device_address_t address = { {0} };
3041                 int packet_threshold;
3042                 int timeout;
3043
3044                 __bt_service_get_parameters(in_param1,
3045                                 &address, sizeof(bluetooth_device_address_t));
3046                 __bt_service_get_parameters(in_param2,
3047                                 &packet_threshold, sizeof(int));
3048                 __bt_service_get_parameters(in_param3,
3049                                 &timeout, sizeof(int));
3050                 result = _bt_gatt_enable_data_batching(&address, packet_threshold, timeout);
3051
3052                 break;
3053         }
3054         /* Sync */
3055         case BT_DISABLE_GATT_DATA_BATCHING: {
3056                 bluetooth_device_address_t address = { {0} };
3057
3058                 __bt_service_get_parameters(in_param1,
3059                                 &address, sizeof(bluetooth_device_address_t));
3060                 result = _bt_gatt_disable_data_batching(&address);
3061
3062                 break;
3063         }
3064
3065         case BT_GATT_GET_PRIMARY_SERVICES: {
3066                 char *addr;
3067
3068                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3069                 bluetooth_device_address_t address = { {0} };
3070
3071                 __bt_service_get_parameters(in_param1, &address,
3072                                 sizeof(bluetooth_device_address_t));
3073
3074                 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
3075                 _bt_convert_addr_type_to_string(addr, address.addr);
3076
3077                 result = _bt_gatt_get_primary_services(addr);
3078                 if (BLUETOOTH_ERROR_NONE == result) {
3079                         _bt_save_invocation_context(context, result, sender,
3080                                         function_name, (gpointer)addr);
3081                 } else
3082                         g_free(addr);
3083                 break;
3084         }
3085         /* Local Async */
3086         case BT_GATT_GET_SERVICE_PROPERTIES: {
3087                 bluetooth_gatt_client_svc_prop_info_t param;
3088
3089                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3090                 memset(&param, 0x00, sizeof(bluetooth_gatt_client_svc_prop_info_t));
3091
3092                 __bt_service_get_parameters(in_param1, &param,
3093                                 sizeof(bluetooth_gatt_client_svc_prop_info_t));
3094
3095                 result = _bt_gatt_get_all_characteristic(&param);
3096                 if (BLUETOOTH_ERROR_NONE == result) {
3097                         /* Save the informations to invocation */
3098                         _bt_save_invocation_context(context, result, sender,
3099                                         function_name,
3100                                         (gpointer)g_memdup2(&param, sizeof(bluetooth_gatt_client_svc_prop_info_t)));
3101                 }
3102                 break;
3103         }
3104         case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
3105                 bluetooth_gatt_client_char_prop_info_t param;
3106
3107                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3108                 memset(&param, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3109
3110                 __bt_service_get_parameters(in_param1, &param,
3111                                 sizeof(bluetooth_gatt_client_char_prop_info_t));
3112
3113                 result = _bt_gatt_get_all_characteristic_properties(&param);
3114                 if (BLUETOOTH_ERROR_NONE == result) {
3115                         /* Save the informations to invocation */
3116                         _bt_save_invocation_context(context, result, sender,
3117                                         function_name,
3118                                         (gpointer)g_memdup2(&param, sizeof(bluetooth_gatt_client_char_prop_info_t)));
3119                 }
3120                 break;
3121         }
3122         /* Async: Remote Interaction  */
3123         case BT_GATT_READ_CHARACTERISTIC: {
3124                 bluetooth_gatt_client_char_prop_info_t param;
3125
3126                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3127                 memset(&param, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3128
3129                 __bt_service_get_parameters(in_param1, &param,
3130                                 sizeof(bluetooth_gatt_client_char_prop_info_t));
3131
3132                 result = _bt_gatt_read_characteristic_value(&param);
3133                 if (BLUETOOTH_ERROR_NONE == result) {
3134                         /* Save the informations to invocation */
3135                         _bt_save_invocation_context(context, result, sender,
3136                                         function_name,
3137                                         (gpointer)g_memdup2(&param, sizeof(bluetooth_gatt_client_char_prop_info_t)));
3138                 }
3139                 break;
3140         }
3141         /* Async: Remote Interaction  */
3142         case BT_GATT_READ_DESCRIPTOR_VALUE: {
3143                 bluetooth_gatt_client_desc_prop_info_t param;
3144
3145                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3146                 memset(&param, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3147
3148                 __bt_service_get_parameters(in_param1, &param,
3149                                 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3150
3151                 result = _bt_gatt_read_descriptor_value(&param);
3152                 if (BLUETOOTH_ERROR_NONE == result) {
3153                         /* Save the informations to invocation */
3154                         _bt_save_invocation_context(context, result, sender,
3155                                         function_name,
3156                                         (gpointer)g_memdup2(&param, sizeof(bluetooth_gatt_client_desc_prop_info_t)));
3157                 }
3158                 break;
3159         }
3160         /* Async: Remote Interaction  */
3161         case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
3162                 bluetooth_gatt_client_char_prop_info_t param;
3163                 bluetooth_gatt_att_data_t data;
3164                 bluetooth_gatt_write_type_e write_type;
3165
3166                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3167
3168                 memset(&data, 0x00, sizeof(bluetooth_gatt_att_data_t));
3169                 memset(&param, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3170
3171                 __bt_service_get_parameters(in_param1, &param,
3172                                 sizeof(bluetooth_gatt_client_char_prop_info_t));
3173                 __bt_service_get_parameters(in_param2, &data,
3174                                 sizeof(bluetooth_gatt_att_data_t));
3175                 __bt_service_get_parameters(in_param3, &write_type,
3176                                 sizeof(bluetooth_gatt_write_type_e));
3177
3178                 result = _bt_gatt_write_characteristic_value_by_type(&param , &data, write_type);
3179                 if (BLUETOOTH_ERROR_NONE == result) {
3180                         /* Save the informations to invocation */
3181                         _bt_save_invocation_context(context, result, sender,
3182                                         function_name,
3183                                         (gpointer)g_memdup2(&param, sizeof(bluetooth_gatt_client_char_prop_info_t)));
3184                 }
3185                 break;
3186         }
3187         /* Async: Remote Interaction  */
3188         case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
3189                 bluetooth_gatt_client_desc_prop_info_t param;
3190                 bluetooth_gatt_att_data_t data;
3191                 bluetooth_gatt_write_type_e write_type;
3192
3193                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3194                 memset(&data, 0x00, sizeof(bluetooth_gatt_att_data_t));
3195                 memset(&param, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3196
3197                 __bt_service_get_parameters(in_param1, &param,
3198                                 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3199                 __bt_service_get_parameters(in_param2, &data,
3200                                 sizeof(bluetooth_gatt_att_data_t));
3201                 __bt_service_get_parameters(in_param3, &write_type,
3202                                 sizeof(bluetooth_gatt_write_type_e));
3203
3204                 result = _bt_gatt_write_descriptor_value_by_type(&param, &data, write_type);
3205                 if (BLUETOOTH_ERROR_NONE == result) {
3206                         /* Save the informations to invocation */
3207                         _bt_save_invocation_context(context, result, sender,
3208                                         function_name,
3209                                         (gpointer)g_memdup2(&param, sizeof(bluetooth_gatt_client_desc_prop_info_t)));
3210                 }
3211                 break;
3212         }
3213         /* Local Async */
3214         case BT_GATT_WATCH_CHARACTERISTIC: {
3215                 bluetooth_gatt_client_char_prop_info_t param;
3216                 int client_id;
3217                 gboolean is_notify;
3218                 int fd = -1;
3219                 int mtu = 0;
3220                 gboolean is_indicate;
3221
3222
3223                 memset(&param, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3224
3225                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3226
3227                 __bt_service_get_parameters(in_param1, &param,
3228                                 sizeof(bluetooth_gatt_client_char_prop_info_t));
3229                 __bt_service_get_parameters(in_param2, &client_id,
3230                                 sizeof(int));
3231                 __bt_service_get_parameters(in_param3, &is_notify,
3232                                 sizeof(gboolean));
3233                 __bt_service_get_parameters(in_param4, &is_indicate,
3234                                 sizeof(gboolean));
3235
3236 #ifdef TIZEN_FEATURE_BT_GATT_CLIENT_FD_DISABLE
3237                 goto normal;
3238 #endif
3239
3240                 if (is_indicate == false) {
3241                         result   = _bt_gatt_acquire_notify(&param , &fd, &mtu);
3242                         if (BLUETOOTH_ERROR_NONE == result && fd > -1) {
3243                                 BT_DBG("GATT Client: Save Invocation data for characteristic props app[%s] fd[ %d]", sender, fd);
3244
3245                                 GUnixFDList *fd_list = NULL;
3246                                 GError *error = NULL;
3247
3248                                 g_array_append_vals(*out_param1, &mtu, sizeof(int));
3249                                 /* Add socket fd to unix_fd_list */
3250                                 fd_list = g_unix_fd_list_new();
3251                                 g_unix_fd_list_append(fd_list, fd, &error);
3252                                 g_assert_no_error(error);
3253                                 close(fd);
3254                                 _bt_service_method_return_with_unix_fd_list(
3255                                 context, *out_param1, result, fd_list);
3256
3257                                 g_object_unref(fd_list);
3258                         } else
3259                                 goto normal;
3260                 } else {
3261
3262 normal:
3263                         result = _bt_gatt_watch_characteristic(&param , client_id, is_notify);
3264                         if (BLUETOOTH_ERROR_NONE == result) {
3265                                 /* Save the informations to invocation */
3266                                 _bt_save_invocation_context(context, result, sender, function_name,
3267                                          (gpointer)g_memdup2(&param, sizeof(bluetooth_gatt_client_char_prop_info_t)));
3268                         }
3269                 }
3270                 break;
3271         }
3272         case BT_GATT_WATCH_SERVICE_CHANGED_INDICATION: {
3273                 gboolean is_enabled;
3274                 bluetooth_device_address_t address;
3275
3276                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3277
3278                 __bt_service_get_parameters(in_param1,
3279                                 &address, sizeof(bluetooth_device_address_t));
3280                 __bt_service_get_parameters(in_param2, &is_enabled,
3281                                 sizeof(gboolean));
3282                 result = _bt_gatt_watch_service_changed_indication(sender, &address, is_enabled);
3283                 BT_INFO("Service changed indication from app [%s]", sender);
3284
3285                 break;
3286         }
3287         case BT_LE_CONN_UPDATE: {
3288                 char *sender = NULL;
3289                 bluetooth_device_address_t local_address = { {0} };
3290                 bluetooth_le_connection_param_t parameters = {0};
3291
3292                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3293
3294                 __bt_service_get_parameters(in_param1, &local_address,
3295                                 sizeof(bluetooth_device_address_t));
3296                 __bt_service_get_parameters(in_param2, &parameters,
3297                                 sizeof(bluetooth_le_connection_param_t));
3298
3299                 result = _bt_le_connection_update(sender,
3300                                         local_address.addr,
3301                                         parameters.interval_min,
3302                                         parameters.interval_max,
3303                                         parameters.latency,
3304                                         parameters.timeout);
3305                 break;
3306         }
3307         case BT_START_LE_DISCOVERY: {
3308                 uid_t uid = 0;
3309                 pid_t pid = 0;
3310
3311                 __bt_service_get_parameters(in_param1, &uid, sizeof(uid_t));
3312
3313                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3314
3315                 if (__bt_service_get_sender_pid(sender, &pid) != BLUETOOTH_ERROR_NONE)
3316                         BT_ERR("Fail to get the sender pid");
3317
3318                 result = _bt_start_le_scan(sender, uid, pid);
3319                 if (result == BLUETOOTH_ERROR_NONE) {
3320                         _bt_save_invocation_context(context, result, sender,
3321                                         function_name, NULL);
3322
3323                         BT_DBG("Remeber pid / uid for the scan operation");
3324                         _bt_bm_add_scan_app(SCAN_LE, uid, pid);
3325                 }
3326                 break;
3327         }
3328         case BT_STOP_LE_DISCOVERY: {
3329                 uid_t uid = 0;
3330                 pid_t pid = 0;
3331
3332                 __bt_service_get_parameters(in_param1, &uid, sizeof(uid_t));
3333
3334                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3335
3336                 if (__bt_service_get_sender_pid(sender, &pid) != BLUETOOTH_ERROR_NONE)
3337                         BT_ERR("Fail to get the sender pid");
3338
3339                 result = _bt_stop_le_scan(sender);
3340                 if (result == BLUETOOTH_ERROR_NONE) {
3341                         _bt_save_invocation_context(context, result, sender,
3342                                         function_name, NULL);
3343                 }
3344
3345                 BT_DBG("Remove pid / uid for the scan operation");
3346                 _bt_bm_remove_scan_app(SCAN_LE, uid, pid);
3347
3348                 break;
3349         }
3350         case BT_IS_LE_DISCOVERYING: {
3351                 gboolean le_discovering = FALSE;
3352
3353                 le_discovering = _bt_is_le_scanning();
3354                 g_array_append_vals(*out_param1,
3355                                 &le_discovering, sizeof(gboolean));
3356                 break;
3357         }
3358         case BT_SET_SCAN_PARAMETERS: {
3359                 bluetooth_le_scan_params_t scan_params;
3360
3361                 __bt_service_get_parameters(in_param1, &scan_params,
3362                                 sizeof(bluetooth_le_scan_params_t));
3363
3364                 BT_DBG("bluetooth_le_scan_params_t [%f %f %d]",
3365                                 scan_params.interval, scan_params.window,
3366                                 scan_params.type);
3367
3368                 result = _bt_prepare_scan_parameters(&scan_params, 0);
3369                 break;
3370         }
3371         case BT_SET_SCAN_TYPE: {
3372                 int scan_type = 0;
3373                 __bt_service_get_parameters(in_param1, &scan_type, sizeof(int));
3374
3375                 BT_DBG("bluetooth_le_scan_type [%d]", scan_type);
3376                 result = _bt_prepare_scan_parameters(NULL, scan_type);
3377                 break;
3378         }
3379 #ifdef TIZEN_FEATURE_BT_PAN_NAP
3380         case BT_NETWORK_ACTIVATE:
3381                 BT_DBG("BT_NETWORK_ACTIVATE Request recieved Request Handler ");
3382                 result = _bt_network_activate();
3383                 break;
3384         case BT_NETWORK_DEACTIVATE:
3385                 result = _bt_network_deactivate();
3386                 break;
3387         case BT_NETWORK_CONNECT: {
3388                 bluetooth_device_address_t address = { {0} };
3389                 int role;
3390
3391                 __bt_service_get_parameters(in_param1,
3392                                 &address, sizeof(bluetooth_device_address_t));
3393                 __bt_service_get_parameters(in_param2,
3394                                 &role, sizeof(int));
3395
3396                 result = _bt_network_connect(request_id, role, &address);
3397                 if (result != BLUETOOTH_ERROR_NONE) {
3398                         g_array_append_vals(*out_param1, &address,
3399                                         sizeof(bluetooth_device_address_t));
3400                 }
3401                 break;
3402         }
3403         case BT_NETWORK_DISCONNECT: {
3404                 bluetooth_device_address_t address = { {0} };
3405
3406                 __bt_service_get_parameters(in_param1,
3407                                 &address, sizeof(bluetooth_device_address_t));
3408
3409                 result = _bt_network_disconnect(request_id, &address);
3410                 if (result != BLUETOOTH_ERROR_NONE) {
3411                         g_array_append_vals(*out_param1, &address,
3412                                         sizeof(bluetooth_device_address_t));
3413                 }
3414                 break;
3415         }
3416         case BT_NETWORK_SERVER_DISCONNECT: {
3417                 bluetooth_device_address_t address = { {0} };
3418
3419                 __bt_service_get_parameters(in_param1,
3420                                 &address, sizeof(bluetooth_device_address_t));
3421
3422                 result = _bt_network_server_disconnect(request_id, &address);
3423                 if (result != BLUETOOTH_ERROR_NONE) {
3424                         g_array_append_vals(*out_param1, &address,
3425                                         sizeof(bluetooth_device_address_t));
3426                 }
3427                 break;
3428         }
3429
3430 #endif
3431 #ifdef TIZEN_FEATURE_BT_IPSP
3432         case BT_LE_IPSP_INIT:
3433                 result = _bt_initialize_ipsp();
3434                 break;
3435         case BT_LE_IPSP_DEINIT:
3436                 result = _bt_deinitialize_ipsp();
3437                 break;
3438         case BT_LE_IPSP_CONNECT: {
3439                 bluetooth_device_address_t address = { {0} };
3440                 __bt_service_get_parameters(in_param1, &address,
3441                                 sizeof(bluetooth_device_address_t));
3442                 result = _bt_connect_le_ipsp_device(&address);
3443                 break;
3444         }
3445         case BT_LE_IPSP_DISCONNECT: {
3446                 bluetooth_device_address_t address = { {0} };
3447                 __bt_service_get_parameters(in_param1, &address,
3448                                 sizeof(bluetooth_device_address_t));
3449                 result = _bt_disconnect_le_ipsp_device(&address);
3450                 break;
3451         }
3452 #endif
3453         case BT_IS_ADVERTISING: {
3454                 gboolean advertising = FALSE;
3455                 advertising = _bt_is_advertising();
3456                 g_array_append_vals(*out_param1, &advertising,
3457                                 sizeof(gboolean));
3458                 break;
3459         }
3460         case BT_IS_LE_2M_PHY_SUPPORTED: {
3461                 gboolean is_2m_phy_supported = FALSE;
3462
3463                 is_2m_phy_supported = _bt_is_le_2m_phy_supported();
3464                 g_array_append_vals(*out_param1, &is_2m_phy_supported, sizeof(gboolean));
3465                 break;
3466         }
3467         case BT_IS_LE_CODED_PHY_SUPPORTED: {
3468                 gboolean is_coded_phy_supported = FALSE;
3469
3470                 is_coded_phy_supported = _bt_is_le_coded_phy_supported();
3471                 g_array_append_vals(*out_param1, &is_coded_phy_supported, sizeof(gboolean));
3472                 break;
3473         }
3474         case BT_IS_LE_EXTENDED_ADVERTISING_SUPPORTED: {
3475                 gboolean is_extended_advertising_supported = FALSE;
3476
3477                 is_extended_advertising_supported = _bt_is_le_extended_advertising_supported();
3478                 g_array_append_vals(*out_param1, &is_extended_advertising_supported,
3479                         sizeof(gboolean));
3480                 break;
3481         }
3482         case BT_IS_LE_EXTENDED_SCAN_SUPPORTED: {
3483                 gboolean is_extended_scan_supported = FALSE;
3484
3485                 is_extended_scan_supported = _bt_is_le_extended_scan_supported();
3486                 g_array_append_vals(*out_param1, &is_extended_scan_supported, sizeof(gboolean));
3487                 break;
3488         }
3489         case BT_LE_MAX_ADVERTISING_DATA_LEN: {
3490                 gint max_advertising_len = 0;
3491
3492                 max_advertising_len = _bt_le_get_maximum_advertising_len();
3493                 g_array_append_vals(*out_param1, &max_advertising_len, sizeof(gint));
3494                 break;
3495         }
3496         case BT_DISCONNECT_DEVICE: {
3497                 bluetooth_device_address_t address = { {0} };
3498
3499                 __bt_service_get_parameters(in_param1,
3500                                 &address, sizeof(bluetooth_device_address_t));
3501
3502                 result = _bt_disconnect_device(&address);
3503                 break;
3504         }
3505         case BT_SET_SCANNING_PHY: {
3506                 int phy = 0;
3507                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3508                 __bt_service_get_parameters(in_param1, &phy, sizeof(int));
3509
3510                 result = _bt_set_scan_phy(sender, phy);
3511
3512                 break;
3513         }
3514         case BT_SET_SCANNING_ROLE: {
3515                 int role = 0;
3516                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3517                 __bt_service_get_parameters(in_param1, &role, sizeof(int));
3518
3519                 result = _bt_set_scan_role(sender, role);
3520
3521                 break;
3522         }
3523         case BT_REGISTER_SCAN_FILTER: {
3524                 bluetooth_le_scan_filter_t scan_filter;
3525
3526                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3527                 __bt_service_get_parameters(in_param1, &scan_filter,
3528                                 sizeof(bluetooth_le_scan_filter_t));
3529
3530                 BT_DBG("bluetooth_le_scan_filter_t [features : 0x%.2x]",
3531                                 scan_filter.added_features);
3532
3533                 result = _bt_register_scan_filter(sender, &scan_filter);
3534
3535                 break;
3536         }
3537
3538         case BT_TDS_PROVIDER_REGISTER: {
3539                 char *sender = NULL;
3540
3541                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3542                 result = _bt_tds_provider_register(sender);
3543
3544                 break;
3545         }
3546
3547         case BT_TDS_PROVIDER_UNREGISTER: {
3548                 char *sender = NULL;
3549
3550                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3551                 result = _bt_tds_provider_unregister(sender);
3552
3553                 break;
3554         }
3555
3556         case BT_TDS_PROVIDER_SET_MANUF_DATA: {
3557                 char *sender = NULL;
3558                 unsigned int length = 0;
3559                 bluetooth_advertising_data_t manuf_data;
3560
3561                 __bt_service_get_parameters(in_param1,
3562                                 &length, sizeof(unsigned int));
3563                 __bt_service_get_parameters(in_param2,
3564                                 &manuf_data, sizeof(bluetooth_advertising_data_t));
3565                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3566
3567                 result = _bt_tds_provider_set_manuf_data(sender, manuf_data.data, length);
3568                 break;
3569         }
3570
3571         case BT_TDS_PROVIDER_CREATE: {
3572                 char *sender = NULL;
3573                 unsigned int tds_handle = 0;
3574                 int transport;
3575
3576                 __bt_service_get_parameters(in_param1,
3577                                 &tds_handle, sizeof(unsigned int));
3578                 __bt_service_get_parameters(in_param2,
3579                                 &transport, sizeof(int));
3580                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3581                 result = _bt_tds_provider_transport_create(sender, transport, tds_handle);
3582
3583                 break;
3584         }
3585
3586         case BT_TDS_PROVIDER_DESTROY: {
3587                 char *sender = NULL;
3588                 unsigned int tds_handle = 0;
3589
3590                 __bt_service_get_parameters(in_param1,
3591                                 &tds_handle, sizeof(unsigned int));
3592                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3593                 result = _bt_tds_provider_transport_remove(sender, tds_handle);
3594
3595                 break;
3596         }
3597
3598         case BT_TDS_PROVIDER_SET_TRANSPORT_DATA: {
3599                 char *sender = NULL;
3600                 unsigned int tds_handle = 0;
3601                 int transport_state = 0;
3602                 bluetooth_tds_data_t tds_data;
3603
3604                 __bt_service_get_parameters(in_param1,
3605                                 &tds_handle, sizeof(unsigned int));
3606                 __bt_service_get_parameters(in_param2,
3607                                 &transport_state, sizeof(int));
3608                 __bt_service_get_parameters(in_param3,
3609                                 &tds_data, sizeof(bluetooth_tds_data_t));
3610                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3611
3612                 result = _bt_tds_provider_set_transport_data(sender, tds_handle,
3613                                 transport_state, tds_data.data, tds_data.length);
3614                 break;
3615         }
3616
3617         case BT_TDS_SEND_ACTIVATION_RESPONSE: {
3618                 bluetooth_device_address_t address = { {0} };
3619                 bluetooth_tds_data_t tds_data;
3620                 char *sender = NULL;
3621                 unsigned int tds_handle = 0;
3622                 int response;
3623
3624                 __bt_service_get_parameters(in_param1,
3625                                 &tds_handle, sizeof(unsigned int));
3626                 __bt_service_get_parameters(in_param2,
3627                                 &response, sizeof(int));
3628                 __bt_service_get_parameters(in_param3, &address,
3629                         sizeof(bluetooth_device_address_t));
3630                 __bt_service_get_parameters(in_param4,
3631                                 &tds_data, sizeof(bluetooth_tds_data_t));
3632                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3633
3634                 result = _bt_tds_provider_send_activation_response(sender, tds_handle,
3635                                 &address, response, tds_data.data, tds_data.length);
3636                 break;
3637         }
3638
3639         case BT_MESH_INIT:
3640                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3641                 BT_INFO("Mesh: Init by [%s]", sender);
3642                 result = _bt_mesh_init(sender);
3643                 /* Save invocation */
3644                 if (result == BLUETOOTH_ERROR_NONE) {
3645                         BT_INFO("Mesh: Save Invoation");
3646                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3647                         _bt_save_invocation_context(context,
3648                                         result, sender,
3649                                         function_name, NULL);
3650                 }
3651                 break;
3652
3653         case BT_MESH_DEINIT:
3654                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3655                 BT_INFO("Mesh: De-Init by [%s]", sender);
3656                 result = _bt_mesh_deinit(sender);
3657                 break;
3658
3659         case BT_MESH_NETWORK_CREATE: {
3660                 bluetooth_mesh_node_t node;
3661                 GSList *model_list = NULL;
3662                 int total_models = 0;
3663                 GArray *param3;
3664                 int i = 0;
3665                 const char *network_name = NULL;
3666                 bluetooth_mesh_network_t *network;
3667
3668                 memset(&node, 0x00, sizeof(bluetooth_mesh_node_t));
3669                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3670
3671                 __bt_service_get_parameters(in_param1,
3672                                 &node, sizeof(bluetooth_mesh_node_t));
3673                 network_name = (const char *)g_variant_get_data(in_param2);
3674
3675                 BT_INFO("MESH: Network name [%s]", network_name);
3676
3677                 param3 = g_array_new(TRUE, TRUE, sizeof(gchar));
3678                 __bt_fill_garray_from_variant(in_param3, param3);
3679
3680                 total_models = (param3->len) / sizeof(bluetooth_mesh_model_t);
3681                 for (i = 0; i < total_models; i++) {
3682                         bluetooth_mesh_model_t *info = NULL;
3683                         bluetooth_mesh_model_t *mod = NULL;
3684
3685                         info = &g_array_index(param3,
3686                                         bluetooth_mesh_model_t, i);
3687
3688                         mod = g_memdup2(info, sizeof(bluetooth_mesh_model_t));
3689
3690                         if (mod)
3691                                 model_list = g_slist_append(model_list,
3692                                                         (gpointer)mod);
3693                 }
3694                 result = _bt_mesh_network_create(requester_unique_creds,
3695                                 sender, network_name,  &node, model_list);
3696
3697                  /* Save invocation */
3698                 if (result == BLUETOOTH_ERROR_NONE) {
3699                         BT_INFO("Mesh: Save Invoation");
3700                         network =  g_malloc0(sizeof(bluetooth_mesh_network_t));
3701
3702                         /* Save Network name & UUID, Token will be received in event */
3703                         _bt_mesh_util_convert_hex_to_string(
3704                                         (uint8_t *) node.uuid, 16,
3705                                         network->uuid,
3706                                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
3707                         BT_INFO("Mesh: Mesh Netwok UUID [%s]", network->uuid);
3708                         g_strlcpy(network->name.name, network_name,
3709                                 sizeof(network->name.name));
3710                         g_strlcpy(network->app_cred,
3711                                 requester_unique_creds,
3712                                 sizeof(network->app_cred));
3713                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3714                         BT_INFO("Mesh: Network Create by [%s]", sender);
3715                         _bt_save_invocation_context(context,
3716                                         result, sender,
3717                                         function_name, (gpointer)network);
3718                 } else {
3719                         g_array_append_vals(*out_param1, &node, sizeof(bluetooth_mesh_node_t));
3720                         BT_ERR("Mesh: Mesh Network creation schedule failed");
3721                         g_slist_free_full(model_list, g_free);
3722                 }
3723
3724                 /* Cleanup */
3725                 BT_INFO("Mesh: Cleanup");
3726                 g_free((gpointer)requester_unique_creds);
3727                 g_array_free(param3, TRUE);
3728                 BT_INFO("Mesh: Cleanup Done");
3729                 break;
3730         }
3731         case BT_MESH_NETWORK_DESTROY: {
3732                 bluetooth_mesh_network_t network;
3733                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3734
3735                 __bt_service_get_parameters(in_param1,
3736                                 &network, sizeof(bluetooth_mesh_network_t));
3737
3738                 BT_INFO("Mesh: Destroy Network");
3739                 result = _bt_mesh_network_destroy(requester_unique_creds,
3740                                 sender, &network);
3741
3742                 if (result != BLUETOOTH_ERROR_NONE) {
3743                         g_array_append_vals(*out_param1,
3744                                 &network, sizeof(bluetooth_mesh_network_t));
3745                 } else {
3746                         BT_INFO("Mesh: Destroy Network scheduled: add invocation");
3747                         bluetooth_mesh_network_t *net =  \
3748                                         g_memdup2(&network, sizeof(bluetooth_mesh_network_t));
3749
3750                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3751                         _bt_save_invocation_context(context, result, sender,
3752                                         function_name, (gpointer)net);
3753                 }
3754                 break;
3755         }
3756         case BT_MESH_NETWORK_LOAD: {
3757                 char *token = NULL;
3758                 bluetooth_mesh_network_t *network;
3759                 token = (char *)g_variant_get_data(in_param1);
3760                 BT_INFO("MESH: Network Load: Token string [%s]", token);
3761                 result = _bt_mesh_network_load(requester_unique_creds, sender, token);
3762                 /* Save invocation */
3763                 if (result == BLUETOOTH_ERROR_NONE) {
3764                         network =  g_malloc0(sizeof(bluetooth_mesh_network_t));
3765                         g_strlcpy(network->token.token, token,
3766                                         BLUETOOTH_MESH_NETWORK_TOKEN_STRING_LENGTH + 1);
3767                         g_strlcpy(network->app_cred, requester_unique_creds,
3768                                         sizeof(network->app_cred));
3769
3770                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3771                         _bt_save_invocation_context(context, result, sender,
3772                                         function_name, (gpointer)network);
3773                 }
3774                 g_free((gpointer)requester_unique_creds);
3775                 break;
3776         }
3777         case BT_MESH_NETWORK_UNLOAD: {
3778                 bluetooth_mesh_network_t network;
3779                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3780
3781                 __bt_service_get_parameters(in_param1,
3782                                 &network, sizeof(bluetooth_mesh_network_t));
3783
3784                 BT_INFO("MESH: Network Unload: Network UUID [%s]", network.uuid);
3785                 result = _bt_mesh_network_unload(requester_unique_creds,
3786                                 sender, &network);
3787                 break;
3788         }
3789         case BT_MESH_NETWORK_JOIN: {
3790                 bluetooth_mesh_node_t node;
3791                 GSList *model_list = NULL;
3792                 int total_models = 0;
3793                 GArray *param2;
3794                 int i = 0;
3795
3796                 memset(&node, 0x00, sizeof(bluetooth_mesh_node_t));
3797                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3798
3799                 __bt_service_get_parameters(in_param1,
3800                                 &node, sizeof(bluetooth_mesh_node_t));
3801
3802                 param2 = g_array_new(TRUE, TRUE, sizeof(gchar));
3803                 __bt_fill_garray_from_variant(in_param2, param2);
3804
3805                 total_models = (param2->len) / sizeof(bluetooth_mesh_model_t);
3806                 for (i = 0; i < total_models; i++) {
3807                         bluetooth_mesh_model_t *info = NULL;
3808                         bluetooth_mesh_model_t *mod = NULL;
3809                         info = &g_array_index(param2,
3810                                 bluetooth_mesh_model_t, i);
3811                         mod = g_memdup2(info, sizeof(bluetooth_mesh_model_t));
3812
3813                         if (mod)
3814                                 model_list = g_slist_append(model_list,
3815                                         (gpointer)mod);
3816                 }
3817
3818                 BT_INFO("MESH: Network Join bt-service");
3819                 //result = BLUETOOTH_ERROR_NONE;
3820                 result = _bt_mesh_network_join(requester_unique_creds,
3821                                 sender, &node, model_list);
3822                 if (result != BLUETOOTH_ERROR_NONE) {
3823                         g_array_append_vals(*out_param1, &node, sizeof(bluetooth_mesh_node_t));
3824                         BT_ERR("Mesh: Mesh Join Network schedule failed");
3825                         g_slist_free_full(model_list, g_free);
3826                 }
3827
3828                 BT_INFO("return of _bt_mesh_network_join : [%d]", result);
3829                 break;
3830         }
3831         case BT_MESH_CANCEL_JOIN: {
3832                 bluetooth_mesh_node_info_t node;
3833                 memset(&node, 0x00, sizeof(bluetooth_mesh_node_info_t));
3834
3835                 __bt_service_get_parameters(in_param1,
3836                                 &node, sizeof(bluetooth_mesh_node_info_t));
3837
3838                 BT_INFO("Mesh: Cancel Join request");
3839                 result = _bt_mesh_cancel_join(requester_unique_creds,
3840                                 sender, &node);
3841                 if (result != BLUETOOTH_ERROR_NONE) {
3842                         g_array_append_vals(*out_param1,
3843                                         &node, sizeof(bluetooth_mesh_node_t));
3844                 }
3845                 break;
3846         }
3847         case BT_MESH_NETWORK_SCAN: {
3848                 bluetooth_mesh_network_t network;
3849                 bluetooth_mesh_scan_param_t param;
3850                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3851                 memset(&param, 0x00, sizeof(bluetooth_mesh_scan_param_t));
3852
3853                 __bt_service_get_parameters(in_param1,
3854                                 &network, sizeof(bluetooth_mesh_network_t));
3855
3856                 __bt_service_get_parameters(in_param2,
3857                                 &param, sizeof(bluetooth_mesh_scan_param_t));
3858
3859                 result = _bt_mesh_network_scan(requester_unique_creds,
3860                                 sender, &network, &param);
3861
3862                 if (result != BLUETOOTH_ERROR_NONE) {
3863                         g_array_append_vals(*out_param1,
3864                                 &network, sizeof(bluetooth_mesh_network_t));
3865                 } else {
3866                         bluetooth_mesh_network_t *net =  \
3867                                         g_memdup2(&network, sizeof(bluetooth_mesh_network_t));
3868
3869                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3870                         _bt_save_invocation_context(context, result, sender,
3871                                         function_name, (gpointer)net);
3872                 }
3873                 break;
3874         }
3875         case BT_MESH_NETWORK_CANCEL_SCAN: {
3876                 bluetooth_mesh_network_t network;
3877                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3878
3879                 __bt_service_get_parameters(in_param1,
3880                                 &network, sizeof(bluetooth_mesh_network_t));
3881
3882                 result = _bt_mesh_network_scan_cancel(requester_unique_creds,
3883                         sender, &network);
3884
3885                 if (result != BLUETOOTH_ERROR_NONE)
3886                         g_array_append_vals(*out_param1,
3887                                 &network, sizeof(bluetooth_mesh_network_t));
3888                 break;
3889         }
3890         case BT_MESH_NETWORK_SET_CAPABILITIES: {
3891                 bluetooth_mesh_network_t network;
3892                 bluetooth_mesh_provisioner_caps_t caps;
3893                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3894                 memset(&caps, 0x00, sizeof(bluetooth_mesh_provisioner_caps_t));
3895
3896                 __bt_service_get_parameters(in_param1,
3897                                 &network, sizeof(bluetooth_mesh_network_t));
3898
3899                 __bt_service_get_parameters(in_param2,
3900                                 &caps, sizeof(bluetooth_mesh_provisioner_caps_t));
3901
3902                 result = _bt_mesh_network_set_provisioner_caps(
3903                                 requester_unique_creds, sender, &network, &caps);
3904
3905                 break;
3906         }
3907         case BT_MESH_NETWORK_PROVISION_DEVICE: {
3908                 bluetooth_mesh_provisioning_request_t req;
3909                 memset(&req, 0x00, sizeof(bluetooth_mesh_provisioning_request_t));
3910
3911                 __bt_service_get_parameters(in_param1,
3912                                 &req, sizeof(bluetooth_mesh_provisioning_request_t));
3913
3914                 BT_INFO("Mesh: Provision Device UUID [%s]", req.dev_uuid);
3915                 BT_INFO("Mesh: Provision Device in network[%s]", req.net_uuid);
3916
3917                 result = _bt_mesh_network_provision_device(
3918                                 requester_unique_creds, sender, &req);
3919
3920                 /* Save invocation */
3921                 if (result != BLUETOOTH_ERROR_NONE) {
3922                         g_array_append_vals(*out_param1, &req,
3923                                 sizeof(bluetooth_mesh_provisioning_request_t));
3924                 } else {
3925                         bluetooth_mesh_provisioning_request_t *param =  \
3926                                 g_memdup2(&req, sizeof(bluetooth_mesh_provisioning_request_t));
3927
3928                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3929                         _bt_save_invocation_context(context, result, sender,
3930                                         function_name, (gpointer)param);
3931                 }
3932                 break;
3933         }
3934         case BT_MESH_AUTHENTICATION_REPLY: {
3935                 BT_INFO("Mesh: BT_MESH_AUTHENTICATION_REPLY");
3936                 const char *auth_value = NULL;
3937                 gboolean authentication_reply = FALSE;
3938                 int auth_type;
3939                 auth_value = g_variant_get_data(in_param2);
3940                 __bt_service_get_parameters(in_param3,
3941                                 &authentication_reply, sizeof(gboolean));
3942                 __bt_service_get_parameters(in_param1,
3943                                 &auth_type, sizeof(int));
3944                 BT_INFO("Mesh: Auth Reply Type [%d]", auth_type);
3945                 BT_INFO("Mesh: Auth Value [%s]", auth_value);
3946                 BT_INFO("Mesh: Auth Accept [%d]", authentication_reply);
3947                 result = _bt_mesh_authentication_reply(auth_type,
3948                                 auth_value, authentication_reply);
3949                 break;
3950         }
3951         case BT_MESH_NETWORK_SET_NAME: {
3952                 bluetooth_mesh_network_t network;
3953                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3954
3955                 __bt_service_get_parameters(in_param1,
3956                                 &network, sizeof(bluetooth_mesh_network_t));
3957
3958                 result = _bt_mesh_network_set_name(requester_unique_creds, sender, &network);
3959
3960                 break;
3961         }
3962         case BT_MESH_NETWORK_ADD_NETKEY: {
3963                 bluetooth_mesh_network_t network;
3964                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3965
3966                 __bt_service_get_parameters(in_param1,
3967                                 &network, sizeof(bluetooth_mesh_network_t));
3968
3969                 result = _bt_mesh_network_add_netkey(requester_unique_creds,
3970                         sender, &network);
3971
3972                 if (result != BLUETOOTH_ERROR_NONE) {
3973                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
3974                 } else {
3975                         bluetooth_mesh_network_t *net =  g_memdup2(&network, sizeof(bluetooth_mesh_network_t));
3976
3977                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3978                         _bt_save_invocation_context(context, result, sender,
3979                                         function_name, (gpointer)net);
3980                 }
3981                 break;
3982         }
3983         case BT_MESH_NETWORK_DELETE_NETKEY: {
3984                 bluetooth_mesh_network_t network;
3985                 uint16_t index;
3986
3987                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3988
3989                 __bt_service_get_parameters(in_param1,
3990                                 &network, sizeof(bluetooth_mesh_network_t));
3991                 __bt_service_get_parameters(in_param2,
3992                                 &index, sizeof(guint16));
3993
3994                 result = _bt_mesh_network_delete_netkey(requester_unique_creds,
3995                                 sender, &network, index);
3996
3997                 if (result != BLUETOOTH_ERROR_NONE) {
3998                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
3999                 } else {
4000                         bluetooth_mesh_network_t *net =  g_memdup2(&network, sizeof(bluetooth_mesh_network_t));
4001
4002                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4003                         _bt_save_invocation_context(context, result, sender,
4004                                         function_name, (gpointer)net);
4005                 }
4006                 break;
4007         }
4008         case BT_MESH_NETWORK_UPDATE_NETKEY: {
4009                 bluetooth_mesh_network_t network;
4010                 uint16_t index;
4011
4012                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
4013
4014                 __bt_service_get_parameters(in_param1,
4015                                 &network, sizeof(bluetooth_mesh_network_t));
4016                 __bt_service_get_parameters(in_param2,
4017                                 &index, sizeof(guint16));
4018
4019                 result = _bt_mesh_network_update_netkey(requester_unique_creds,
4020                                 sender, &network, index);
4021
4022                 if (result != BLUETOOTH_ERROR_NONE) {
4023                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
4024                 } else {
4025                         bluetooth_mesh_network_t *net =  g_memdup2(&network, sizeof(bluetooth_mesh_network_t));
4026
4027                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4028                         _bt_save_invocation_context(context, result, sender,
4029                                         function_name, (gpointer)net);
4030                 }
4031                 break;
4032         }
4033         case BT_MESH_NETWORK_ADD_APPKEY: {
4034                 bluetooth_mesh_network_t network;
4035                 uint16_t net_idx;
4036
4037                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
4038
4039                 __bt_service_get_parameters(in_param1,
4040                                 &network, sizeof(bluetooth_mesh_network_t));
4041                 __bt_service_get_parameters(in_param2,
4042                                 &net_idx, sizeof(guint16));
4043
4044                 result = _bt_mesh_network_add_appkey(requester_unique_creds,
4045                                 sender, &network, net_idx);
4046
4047                 if (result != BLUETOOTH_ERROR_NONE) {
4048                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
4049                 } else {
4050                         bluetooth_mesh_network_t *net =  g_memdup2(&network, sizeof(bluetooth_mesh_network_t));
4051
4052                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4053                         _bt_save_invocation_context(context, result, sender,
4054                                         function_name, (gpointer)net);
4055                 }
4056                 break;
4057         }
4058         case BT_MESH_NETWORK_UPDATE_APPKEY: {
4059                 bluetooth_mesh_network_t network;
4060                 uint16_t net_idx;
4061                 uint16_t app_idx;
4062
4063                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
4064
4065                 __bt_service_get_parameters(in_param1,
4066                                 &network, sizeof(bluetooth_mesh_network_t));
4067                 __bt_service_get_parameters(in_param2,
4068                                 &net_idx, sizeof(guint16));
4069                 __bt_service_get_parameters(in_param3,
4070                                 &app_idx, sizeof(guint16));
4071
4072                 result = _bt_mesh_network_update_appkey(requester_unique_creds,
4073                                 sender, &network, net_idx, app_idx);
4074
4075                 if (result != BLUETOOTH_ERROR_NONE) {
4076                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
4077                 } else {
4078                         bluetooth_mesh_network_t *net =  g_memdup2(&network, sizeof(bluetooth_mesh_network_t));
4079
4080                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4081                         _bt_save_invocation_context(context, result, sender,
4082                                         function_name, (gpointer)net);
4083                 }
4084                 break;
4085         }
4086         case BT_MESH_NETWORK_DELETE_APPKEY: {
4087                 bluetooth_mesh_network_t network;
4088                 uint16_t net_idx;
4089                 uint16_t app_idx;
4090
4091                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
4092
4093                 __bt_service_get_parameters(in_param1,
4094                                 &network, sizeof(bluetooth_mesh_network_t));
4095                 __bt_service_get_parameters(in_param2,
4096                                 &net_idx, sizeof(guint16));
4097                 __bt_service_get_parameters(in_param3,
4098                                 &app_idx, sizeof(guint16));
4099
4100                 result = _bt_mesh_network_delete_appkey(requester_unique_creds,
4101                                 sender, &network, net_idx, app_idx);
4102
4103                 if (result != BLUETOOTH_ERROR_NONE) {
4104                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
4105                 } else {
4106                         bluetooth_mesh_network_t *net =  g_memdup2(&network, sizeof(bluetooth_mesh_network_t));
4107
4108                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4109                         _bt_save_invocation_context(context, result, sender,
4110                                         function_name, (gpointer)net);
4111                 }
4112                 break;
4113         }
4114         case BT_MESH_NETWORK_GET_NETKEYS: {
4115                 bluetooth_mesh_network_t network;
4116                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
4117                 __bt_service_get_parameters(in_param1,
4118                                 &network, sizeof(bluetooth_mesh_network_t));
4119                 result = _bt_mesh_network_get_netkeys(requester_unique_creds,
4120                                 sender, &network, out_param1);
4121                 break;
4122         }
4123         case BT_MESH_NETWORK_GET_APPKEYS: {
4124                 bluetooth_mesh_network_t network;
4125                 uint16_t net_idx;
4126
4127                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
4128                 __bt_service_get_parameters(in_param1,
4129                                 &network, sizeof(bluetooth_mesh_network_t));
4130                 __bt_service_get_parameters(in_param2,
4131                                 &net_idx, sizeof(guint16));
4132                 result = _bt_mesh_network_get_appkeys(
4133                                 requester_unique_creds, sender,
4134                                         &network, net_idx, out_param1);
4135                 break;
4136         }
4137         case BT_MESH_NETWORK_GET_NODES: {
4138                 bluetooth_mesh_network_t network;
4139                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
4140                 __bt_service_get_parameters(in_param1,
4141                                 &network, sizeof(bluetooth_mesh_network_t));
4142                 result = _bt_mesh_network_get_nodes(
4143                                 requester_unique_creds, sender,
4144                                         &network, out_param1);
4145                 break;
4146         }
4147         case BT_MESH_ELEMENT_GET_MODELS: {
4148                 bluetooth_mesh_network_t network;
4149                 uint16_t node_addr;
4150                 int elem_idx;
4151
4152                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
4153                 __bt_service_get_parameters(in_param1,
4154                                 &network, sizeof(bluetooth_mesh_network_t));
4155                 __bt_service_get_parameters(in_param2,
4156                                 &node_addr, sizeof(guint16));
4157                 __bt_service_get_parameters(in_param3,
4158                                 &elem_idx, sizeof(int));
4159                 result = _bt_mesh_element_get_models(requester_unique_creds, sender,
4160                                 &network, node_addr, elem_idx, out_param1);
4161                 break;
4162         }
4163         case BT_MESH_NODE_BROWSE: {
4164                 bluetooth_mesh_node_discover_t req;
4165                 memset(&req, 0x00, sizeof(bluetooth_mesh_node_discover_t));
4166
4167                 __bt_service_get_parameters(in_param1,
4168                                 &req, sizeof(bluetooth_mesh_node_discover_t));
4169
4170                 result = _bt_mesh_browse_remote_node(
4171                                 requester_unique_creds, sender, &req);
4172
4173                 /* Save invocation */
4174                 if (result != BLUETOOTH_ERROR_NONE) {
4175                         g_array_append_vals(*out_param1, &req,
4176                                 sizeof(bluetooth_mesh_node_discover_t));
4177                 } else {
4178                         bluetooth_mesh_node_discover_t *param = \
4179                                 g_memdup2(&req, sizeof(bluetooth_mesh_node_discover_t));
4180
4181                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4182                         _bt_save_invocation_context(context, result, sender,
4183                                         function_name, (gpointer)param);
4184                 }
4185                 break;
4186         }
4187         case BT_MESH_NODE_GET_VENDOR_FEATURES: {
4188                 bluetooth_mesh_node_features_t req;
4189                 memset(&req, 0x00, sizeof(bluetooth_mesh_node_features_t));
4190
4191                 __bt_service_get_parameters(in_param1,
4192                                 &req, sizeof(bluetooth_mesh_node_features_t));
4193
4194                 result = _bt_mesh_node_discover_vendor_features(
4195                                 requester_unique_creds, sender, &req);
4196
4197                 /* Save invocation */
4198                 if (result != BLUETOOTH_ERROR_NONE) {
4199                         g_array_append_vals(*out_param1,
4200                                 &req, sizeof(bluetooth_mesh_node_features_t));
4201                 } else {
4202                         bluetooth_mesh_node_features_t *param =  \
4203                                 g_memdup2(&req, sizeof(bluetooth_mesh_node_features_t));
4204
4205                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4206                         _bt_save_invocation_context(context, result, sender,
4207                                         function_name, (gpointer)param);
4208                 }
4209                 break;
4210         }
4211         case BT_MESH_NODE_CONFIGURE_KEY: {
4212                 bluetooth_mesh_key_configure_t req;
4213
4214                 memset(&req, 0x00, sizeof(bluetooth_mesh_key_configure_t));
4215
4216                 __bt_service_get_parameters(in_param1,
4217                                 &req, sizeof(bluetooth_mesh_key_configure_t));
4218
4219                 result = _bt_mesh_node_configure_key(
4220                                 requester_unique_creds, sender, &req);
4221
4222                 /* Save invocation */
4223                 if (result != BLUETOOTH_ERROR_NONE) {
4224                         g_array_append_vals(*out_param1,
4225                                 &req, sizeof(bluetooth_mesh_key_configure_t));
4226                 } else {
4227                         bluetooth_mesh_key_configure_t *param = \
4228                                 g_memdup2(&req, sizeof(bluetooth_mesh_key_configure_t));
4229
4230                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4231                         _bt_save_invocation_context(context, result, sender,
4232                                         function_name, (gpointer)param);
4233                 }
4234                 break;
4235         }
4236         case BT_MESH_NODE_TTL_EXECUTE: {
4237                 bluetooth_mesh_node_ttl_info_t req;
4238
4239                 memset(&req, 0x00, sizeof(bluetooth_mesh_node_ttl_info_t));
4240
4241                 __bt_service_get_parameters(in_param1,
4242                                 &req, sizeof(bluetooth_mesh_node_ttl_info_t));
4243
4244                 result = _bt_mesh_ttl_execute_remote_node(
4245                                 requester_unique_creds, sender, &req);
4246
4247                 /* Save invocation */
4248                 if (result != BLUETOOTH_ERROR_NONE) {
4249                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_node_ttl_info_t));
4250                 } else {
4251                         bluetooth_mesh_node_ttl_info_t *param =  \
4252                                 g_memdup2(&req, sizeof(bluetooth_mesh_node_ttl_info_t));
4253
4254                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4255                         _bt_save_invocation_context(context, result, sender,
4256                                         function_name, (gpointer)param);
4257                 }
4258                 break;
4259         }
4260         case BT_MESH_MODEL_CONFIGURE_APPKEY: {
4261                 bluetooth_mesh_model_configure_t req;
4262
4263                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
4264
4265                 __bt_service_get_parameters(in_param1,
4266                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4267
4268                 result = _bt_mesh_node_model_appkey_execute(
4269                                 requester_unique_creds, sender, &req);
4270
4271                 /* Save invocation */
4272                 if (result != BLUETOOTH_ERROR_NONE) {
4273                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_configure_t));
4274                 } else {
4275                         bluetooth_mesh_model_configure_t *param =  \
4276                                 g_memdup2(&req, sizeof(bluetooth_mesh_model_configure_t));
4277
4278                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4279                         _bt_save_invocation_context(context, result, sender,
4280                                         function_name, (gpointer)param);
4281                 }
4282                 break;
4283         }
4284         case BT_MESH_MODEL_EXECUTE_MSG: {
4285                 bluetooth_mesh_model_msg_t req;
4286
4287                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_msg_t));
4288
4289                 __bt_service_get_parameters(in_param1,
4290                                 &req, sizeof(bluetooth_mesh_model_msg_t));
4291
4292                 result = _bt_mesh_model_execute_msg(
4293                                 requester_unique_creds, sender, &req);
4294
4295                 /* Save invocation */
4296                 if (result != BLUETOOTH_ERROR_NONE) {
4297                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_msg_t));
4298                 } else {
4299                         bluetooth_mesh_model_msg_t *param =  \
4300                                 g_memdup2(&req, sizeof(bluetooth_mesh_model_msg_t));
4301
4302                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4303                         _bt_save_invocation_context(context, result, sender,
4304                                         function_name, (gpointer)param);
4305                 }
4306                 break;
4307         }
4308         case BT_MESH_MODEL_RESPONSE_MSG: {
4309                 bluetooth_mesh_model_msg_t req;
4310                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_msg_t));
4311                 __bt_service_get_parameters(in_param1,
4312                                 &req, sizeof(bluetooth_mesh_model_msg_t));
4313
4314                 result = _bt_mesh_model_send_response(
4315                                 requester_unique_creds, sender, &req);
4316                 if (result != BLUETOOTH_ERROR_NONE)
4317                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_msg_t));
4318                 break;
4319         }
4320         case BT_MESH_NODE_GET_NETKEYS: {
4321                 bluetooth_mesh_node_discover_t node;
4322                 memset(&node, 0x00, sizeof(bluetooth_mesh_node_discover_t));
4323                 __bt_service_get_parameters(in_param1,
4324                                 &node, sizeof(bluetooth_mesh_node_discover_t));
4325                 result = _bt_mesh_network_node_get_netkeys(
4326                                 requester_unique_creds, &node, out_param1);
4327                 break;
4328         }
4329         case BT_MESH_NODE_GET_APPKEYS: {
4330                 bluetooth_mesh_node_discover_t node;
4331                 memset(&node, 0x00, sizeof(bluetooth_mesh_node_discover_t));
4332                 __bt_service_get_parameters(in_param1,
4333                                 &node, sizeof(bluetooth_mesh_node_discover_t));
4334                 result = _bt_mesh_network_node_get_appkeys(
4335                                 requester_unique_creds, sender, &node, out_param1);
4336                 break;
4337         }
4338         case BT_MESH_NODE_RESET: {
4339                 bluetooth_mesh_node_info_t node;
4340                 memset(&node, 0x00, sizeof(bluetooth_mesh_node_info_t));
4341
4342                 __bt_service_get_parameters(in_param1,
4343                                 &node, sizeof(bluetooth_mesh_node_info_t));
4344
4345                 BT_INFO("Mesh: Reset the Node");
4346                 result = _bt_mesh_network_reset_node(requester_unique_creds,
4347                                 sender, &node);
4348
4349                 break;
4350         }
4351         case BT_MESH_MODEL_GET_APPKEY_LIST: {
4352                 bluetooth_mesh_model_configure_t req;
4353
4354                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
4355
4356                 __bt_service_get_parameters(in_param1,
4357                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4358
4359                 result = _bt_mesh_node_model_get_appkey_list(
4360                                         requester_unique_creds, sender, &req);
4361
4362                 /* Save invocation */
4363                 if (result != BLUETOOTH_ERROR_NONE) {
4364                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_configure_t));
4365                 } else {
4366                         bluetooth_mesh_model_configure_t *param = \
4367                                 g_memdup2(&req, sizeof(bluetooth_mesh_model_configure_t));
4368
4369                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4370                         _bt_save_invocation_context(context, result, sender,
4371                                         function_name, (gpointer)param);
4372                 }
4373                 break;
4374         }
4375         case BT_MESH_MODEL_GET_SUBSCRIPTION_LIST: {
4376                 bluetooth_mesh_model_configure_t req;
4377
4378                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
4379
4380                 __bt_service_get_parameters(in_param1,
4381                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4382
4383                 result = _bt_mesh_node_model_get_subscription_list(
4384                                 requester_unique_creds, sender, &req);
4385
4386                 /* Save invocation */
4387                 if (result != BLUETOOTH_ERROR_NONE) {
4388                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_configure_t));
4389                 } else {
4390                         bluetooth_mesh_model_configure_t *param =  \
4391                                 g_memdup2(&req, sizeof(bluetooth_mesh_model_configure_t));
4392
4393                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4394                         _bt_save_invocation_context(context, result, sender,
4395                                         function_name, (gpointer)param);
4396                 }
4397
4398                 break;
4399         }
4400         case BT_MESH_NETWORK_CREATE_GROUP: {
4401                 bluetooth_mesh_network_t net;
4402                 bluetooth_mesh_network_group_info_t res;
4403                 gboolean is_virtual;
4404                 uint16_t grp_addr;
4405
4406                 memset(&net, 0x00, sizeof(bluetooth_mesh_network_t));
4407                 memset(&res, 0x00, sizeof(bluetooth_mesh_network_group_info_t));
4408
4409                 __bt_service_get_parameters(in_param1,
4410                                 &net, sizeof(bluetooth_mesh_network_t));
4411                 __bt_service_get_parameters(in_param2,
4412                                 &is_virtual, sizeof(gboolean));
4413                 __bt_service_get_parameters(in_param3,
4414                                 &grp_addr, sizeof(guint16));
4415
4416                 result = _bt_mesh_network_create_group(
4417                                 requester_unique_creds, sender, &net,
4418                                         is_virtual, grp_addr, &res);
4419
4420                 if (result == BLUETOOTH_ERROR_NONE)
4421                         g_array_append_vals(*out_param1, &res,
4422                                 sizeof(bluetooth_mesh_network_group_info_t));
4423                 break;
4424         }
4425         case BT_MESH_NETWORK_REMOVE_GROUP: {
4426                 bluetooth_mesh_network_t net;
4427                 bluetooth_mesh_network_group_info_t req;
4428
4429                 memset(&net, 0x00, sizeof(bluetooth_mesh_network_t));
4430                 memset(&req, 0x00, sizeof(bluetooth_mesh_network_group_info_t));
4431
4432                 __bt_service_get_parameters(in_param1,
4433                                 &net, sizeof(bluetooth_mesh_network_t));
4434                 __bt_service_get_parameters(in_param2,
4435                                 &req, sizeof(bluetooth_mesh_network_group_info_t));
4436
4437                 result = _bt_mesh_network_remove_group(
4438                                 requester_unique_creds, sender, &net, &req);
4439
4440                 break;
4441         }
4442         case BT_MESH_MODEL_CONFIG_GROUP_SUB: {
4443                 bluetooth_mesh_model_configure_t req;
4444
4445                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
4446
4447                 __bt_service_get_parameters(in_param1,
4448                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4449
4450                 result = _bt_mesh_model_configure_group_subscription(
4451                                 requester_unique_creds, sender, &req);
4452
4453                 /* Save invocation */
4454                 if (result != BLUETOOTH_ERROR_NONE) {
4455                         g_array_append_vals(*out_param1,
4456                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4457                 } else {
4458                         bluetooth_mesh_model_configure_t *param =  \
4459                                 g_memdup2(&req, sizeof(bluetooth_mesh_model_configure_t));
4460
4461                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4462                         _bt_save_invocation_context(context, result, sender,
4463                                         function_name, (gpointer)param);
4464                 }
4465                 break;
4466         }
4467         case BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB: {
4468                 bluetooth_mesh_model_configure_t req;
4469
4470                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
4471
4472                 __bt_service_get_parameters(in_param1,
4473                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4474
4475                 result = _bt_mesh_model_configure_virtual_group_subscription(
4476                                 requester_unique_creds, sender, &req);
4477
4478                 /* Save invocation */
4479                 if (result != BLUETOOTH_ERROR_NONE) {
4480                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_configure_t));
4481                 } else {
4482                         bluetooth_mesh_model_configure_t *param =  \
4483                                 g_memdup2(&req, sizeof(bluetooth_mesh_model_configure_t));
4484
4485                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4486                         _bt_save_invocation_context(context, result, sender,
4487                                         function_name, (gpointer)param);
4488                 }
4489                 break;
4490         }
4491         case BT_MESH_MODEL_SET_PUBLICATION: {
4492                 bluetooth_mesh_model_configure_t req;
4493
4494                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
4495
4496                 __bt_service_get_parameters(in_param1,
4497                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4498
4499                 result = _bt_mesh_model_set_publication(
4500                                 requester_unique_creds, sender, &req);
4501
4502                 /* Save invocation */
4503                 if (result != BLUETOOTH_ERROR_NONE) {
4504                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_configure_t));
4505                 } else {
4506                         bluetooth_mesh_model_configure_t *param = g_memdup2(&req, sizeof(bluetooth_mesh_model_configure_t));
4507
4508                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4509                         _bt_save_invocation_context(context, result, sender,
4510                                         function_name, (gpointer)param);
4511                 }
4512                 break;
4513         }
4514         case BT_MESH_MODEL_GET_PUBLICATION: {
4515                 bluetooth_mesh_model_configure_t req;
4516
4517                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
4518
4519                 __bt_service_get_parameters(in_param1,
4520                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4521
4522                 result = _bt_mesh_model_get_publication(
4523                                         requester_unique_creds, sender, &req);
4524
4525                 /* Save invocation */
4526                 if (result != BLUETOOTH_ERROR_NONE) {
4527                         g_array_append_vals(*out_param1,
4528                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4529                 } else {
4530                         bluetooth_mesh_model_configure_t *param =  \
4531                                 g_memdup2(&req, sizeof(bluetooth_mesh_model_configure_t));
4532
4533                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4534                         _bt_save_invocation_context(context, result, sender,
4535                                         function_name, (gpointer)param);
4536                 }
4537                 break;
4538         }
4539         case BT_MESH_NETWORK_GET_GROUPS: {
4540                 bluetooth_mesh_network_t network;
4541                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
4542                 __bt_service_get_parameters(in_param1,
4543                                 &network, sizeof(bluetooth_mesh_network_t));
4544                 BT_INFO("Mesh: Request:: Get Groups");
4545                 result = _bt_mesh_network_get_groups(
4546                                 requester_unique_creds, sender, &network, out_param1);
4547                 break;
4548         }
4549         case BT_L2CAP_LE_CLIENT_CONNECT: {
4550                 bluetooth_device_address_t address = { {0} };
4551                 int psm;
4552
4553                 __bt_service_get_parameters(in_param1,
4554                                 &address, sizeof(bluetooth_device_address_t));
4555
4556                 __bt_service_get_parameters(in_param2, &psm, sizeof(int));
4557
4558                 result = _bt_l2cap_le_connect_using_psm(&address, psm);
4559
4560                 if (result != BLUETOOTH_ERROR_NONE) {
4561                         bluetooth_l2cap_le_connection_t conn_info;
4562
4563                         BT_ERR("BT_L2CAP_LE_CLIENT_CONNECT failed, send error errcode %d", result);
4564                         memset(&conn_info, 0x00, sizeof(bluetooth_l2cap_le_connection_t));
4565                         conn_info.device_role = L2CAP_LE_ROLE_CLIENT;
4566                         conn_info.socket_fd = -1;
4567                         conn_info.psm = psm;
4568                         memcpy(&conn_info.device_addr, &address, sizeof(bluetooth_device_address_t));
4569                         g_array_append_vals(*out_param1, &conn_info,
4570                                         sizeof(bluetooth_l2cap_le_connection_t));
4571                 } else {
4572                         bluetooth_l2cap_le_connection_t *conn_info;
4573
4574                         BT_INFO("BT_L2CAP_LE_CLIENT_CONNECT success, save context");
4575                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4576                         conn_info = g_malloc0(sizeof(bluetooth_l2cap_le_connection_t));
4577                         conn_info->socket_fd = -1;
4578                         conn_info->device_role = L2CAP_LE_ROLE_CLIENT;
4579                         conn_info->psm = psm;
4580
4581                         memcpy(&conn_info->device_addr, &address, sizeof(bluetooth_device_address_t));
4582
4583                         _bt_save_invocation_context(context, result, sender, function_name, (gpointer)conn_info);
4584                 }
4585                 break;
4586         }
4587         case BT_L2CAP_LE_SOCKET_DISCONNECT: {
4588                 /*
4589                  * Bluetooth L2CAP_LE socket disconnection will be done from bt-api, call to bt-service
4590                  * is only used for privilege check, so return BLUETOOTH_ERROR_NONE from here.
4591                  */
4592                 result = BLUETOOTH_ERROR_NONE;
4593                 break;
4594         }
4595         case BT_L2CAP_LE_SOCKET_WRITE: {
4596                 /*
4597                  * This call to bt-service is only used for privilege check, so return
4598                  * BLUETOOTH_ERROR_NONE from here.
4599                  */
4600                 result = BLUETOOTH_ERROR_NONE;
4601                 break;
4602         }
4603         case BT_L2CAP_LE_CREATE_SOCKET: {
4604                 /*
4605                  * This call to bt-service is only used for privilege check, so return
4606                  * BLUETOOTH_ERROR_NONE from here.
4607                  */
4608                 result = BLUETOOTH_ERROR_NONE;
4609                 break;
4610         }
4611         case BT_L2CAP_LE_LISTEN_AND_ACCEPT: {
4612                 int socket_fd = -1;
4613                 int psm;
4614
4615                 __bt_service_get_parameters(in_param1, &psm, sizeof(int));
4616
4617                 socket_fd = _bt_l2cap_le_socket_listen(psm, true);
4618                 if (socket_fd > 0) {
4619                         result = BLUETOOTH_ERROR_NONE;
4620                         sender = (char *)g_dbus_method_invocation_get_sender(context);
4621                         _bt_save_invocation_context(context,
4622                                         result, sender, function_name, NULL);
4623                 } else {
4624                         g_array_append_vals(*out_param1, &socket_fd, sizeof(int));
4625                 }
4626                 break;
4627         }
4628         case BT_L2CAP_LE_LISTEN: {
4629                 int socket_fd = -1;
4630                 int psm;
4631
4632                 __bt_service_get_parameters(in_param1, &psm, sizeof(int));
4633
4634                 socket_fd = _bt_l2cap_le_socket_listen(psm, false);
4635                 if (socket_fd > 0) {
4636                         result = BLUETOOTH_ERROR_NONE;
4637                         sender = (char *)g_dbus_method_invocation_get_sender(context);
4638                         _bt_save_invocation_context(context, result,
4639                                         sender, function_name, NULL);
4640                 } else {
4641                         g_array_append_vals(*out_param1, &socket_fd, sizeof(int));
4642                 }
4643                 break;
4644         }
4645         case BT_L2CAP_LE_ACCEPT_CONNECTION: {
4646                 char *address;
4647
4648                 address = (char *)g_variant_get_data(in_param1);
4649                 result = _bt_socket_reply_conn_authorization(address, TRUE);
4650                 break;
4651         }
4652         case BT_L2CAP_LE_REJECT_CONNECTION: {
4653                 char *address;
4654
4655                 address = (char *)g_variant_get_data(in_param1);
4656                 result = _bt_socket_reply_conn_authorization(address, FALSE);
4657                 break;
4658         }
4659         case BT_L2CAP_LE_REMOVE_SOCKET: {
4660                 /*
4661                  * This call to bt-service is only used for privilege check, so return
4662                  * BLUETOOTH_ERROR_NONE from here.
4663                  */
4664                 result = BLUETOOTH_ERROR_NONE;
4665                 break;
4666         }
4667         default:
4668                 BT_INFO("UnSupported function [%d]", function_name);
4669                 result = BLUETOOTH_ERROR_NOT_SUPPORT;
4670                 break;
4671         }
4672
4673         return result;
4674 }
4675 #ifdef TIZEN_FEATURE_BT_OBEX
4676 /* Function definitions*/
4677 static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
4678 {
4679         char *data;
4680         int size;
4681
4682         size = g_variant_get_size(var);
4683         if (size > 0) {
4684                 data = (char *)g_variant_get_data(var);
4685                 if (data)
4686                         param = g_array_append_vals(param, data, size);
4687
4688         }
4689 }
4690 #endif
4691
4692 int __bt_obexd_request(int function_name,
4693                 int request_type,
4694                 int request_id,
4695                 GDBusMethodInvocation *context,
4696                 GVariant *in_param1,
4697                 GVariant *in_param2,
4698                 GVariant *in_param3,
4699                 GVariant *in_param4,
4700                 GArray **out_param1)
4701 {
4702         BT_DBG("+");
4703
4704         int result = BLUETOOTH_ERROR_NONE;
4705
4706         BT_DBG("function_name : %x", function_name);
4707
4708         switch (function_name) {
4709         case BT_OPP_PUSH_FILES: {
4710                 BT_DBG("BT_OPP_PUSH_FILES");
4711                 int i;
4712                 bluetooth_device_address_t address = { {0} };
4713                 bt_file_path_t *path;
4714                 char **file_path;
4715                 int file_count;
4716                 GArray *param2;
4717
4718                 __bt_service_get_parameters(in_param1, &address,
4719                                 sizeof(bluetooth_device_address_t));
4720                 __bt_service_get_parameters(in_param3, &file_count,
4721                                 sizeof(int));
4722
4723                 file_path = g_new0(char *, file_count + 1);
4724
4725                 param2 = g_array_new(TRUE, TRUE, sizeof(gchar));
4726                 __bt_fill_garray_from_variant(in_param2, param2);
4727
4728                 for (i = 0; i < file_count; i++) {
4729                         path = &g_array_index(param2, bt_file_path_t, i);
4730                         file_path[i] = g_strdup(path->path);
4731                 }
4732                 BT_DBG("_bt_opp_client_push_files");
4733                 result = _bt_opp_client_push_files(request_id, context,
4734                                                 &address, file_path,
4735                                                 file_count);
4736
4737                 for (i = 0; i < file_count; i++)
4738                         g_free(file_path[i]);
4739
4740                 g_free(file_path);
4741                 g_array_free(param2, TRUE);
4742
4743                 break;
4744         }
4745         case BT_OPP_CANCEL_PUSH: {
4746                 result = _bt_opp_client_cancel_push();
4747
4748                 break;
4749         }
4750         case BT_OPP_IS_PUSHING_FILES: {
4751                 gboolean is_sending = FALSE;
4752
4753                 result = _bt_opp_client_is_sending(&is_sending);
4754
4755                 g_array_append_vals(*out_param1, &is_sending,
4756                                 sizeof(gboolean));
4757                 break;
4758         }
4759         case BT_OPP_GET_TRANSFER_PROGRESS: {
4760                 int direction;
4761                 int transfer_id;
4762                 guint8 progress = 0;
4763
4764                 __bt_service_get_parameters(in_param1, &direction,
4765                                 sizeof(int));
4766                 __bt_service_get_parameters(in_param2, &transfer_id,
4767                                 sizeof(int));
4768                 if (direction)
4769                         result = _bt_opp_get_client_progress(&progress);
4770                 else
4771                         result = _bt_opp_get_server_progress(transfer_id, &progress);
4772
4773                 g_array_append_vals(*out_param1, &progress,
4774                                 sizeof(guint8));
4775
4776                 break;
4777         }
4778
4779         case BT_MAP_CREATE_SESSION: {
4780                 BT_DBG("BT_MAP_CREATE_SESSION");
4781                 char *address = (char *)g_variant_get_data(in_param1);
4782                 char *session_id = NULL;
4783                 result = _bt_create_session_sync(address, &session_id);
4784                 if (result == BLUETOOTH_ERROR_NONE)
4785                         g_array_append_vals(*out_param1, session_id, strlen(session_id)+1);
4786                 break;
4787         }
4788
4789         case BT_MAP_DESTROY_SESSION: {
4790                 BT_DBG("BT_MAP_DESTROY_SESSION");
4791                 char* session_id = (char *)g_variant_get_data(in_param1);
4792                 result = _bt_destroy_session_sync(session_id);
4793                 if (result == BLUETOOTH_ERROR_NONE)
4794                         BT_DBG("successfully destroyed session");
4795                 break;
4796         }
4797
4798         case BT_MAP_SET_FOLDER: {
4799                 BT_DBG("BT_MAP_SET_FOLDER");
4800                 char *session_id = (char *)g_variant_get_data(in_param1);
4801                 char *name = (char *)g_variant_get_data(in_param2);
4802                 result = _bt_map_client_set_folder(session_id, name);
4803                 break;
4804         }
4805
4806         case BT_MAP_LIST_FOLDERS: {
4807                 BT_DBG("BT_MAP_LIST_FOLDERS");
4808
4809                 char* session_id = (char *)g_variant_get_data(in_param1);
4810                 char* filter_serialized = (char*)g_variant_get_data(in_param2);
4811
4812                 result = _bt_map_client_list_folders(request_id, context, session_id, filter_serialized);
4813                 if (result == BLUETOOTH_ERROR_NONE)
4814                         BT_DBG("_bt_map_client_list_folders succeed");
4815
4816                 break;
4817         }
4818
4819         case BT_MAP_LIST_FILTER_FIELDS: {
4820                 BT_DBG("BT_MAP_LIST_FILTER_FIELDS");
4821
4822                 char* session_id = (char *)g_variant_get_data(in_param1);
4823
4824                 result = _bt_map_client_list_filter_fields(request_id, context, session_id);
4825                 if (result == BLUETOOTH_ERROR_NONE)
4826                         BT_DBG("_bt_map_client_list_filter_fields succeed");
4827
4828                 break;
4829         }
4830
4831         case BT_MAP_LIST_MESSAGES: {
4832                 BT_DBG("BT_MAP_LIST_MESSAGES");
4833
4834                 char* session_id = (char*)g_variant_get_data(in_param1);
4835                 char* folder = (char*)g_variant_get_data(in_param2);
4836                 char* filter_serialized = (char*)g_variant_get_data(in_param3);
4837
4838                 result = _bt_map_client_list_messages(request_id, context, session_id, folder, filter_serialized);
4839                 if (result == BLUETOOTH_ERROR_NONE)
4840                         BT_DBG("_bt_map_client_list_messages succeed");
4841                 else
4842                         BT_DBG("_bt_map_client_list_messages failed");
4843
4844                 break;
4845         }
4846
4847         case BT_MAP_UPDATE_INBOX: {
4848                 BT_DBG("BT_MAP_UPDATE_INBOX");
4849                 char* session_id = (char *)g_variant_get_data(in_param1);
4850                 result = _bt_map_client_update_inbox(session_id);
4851                 break;
4852         }
4853
4854         case BT_MAP_PUSH_MESSAGE: {
4855                 BT_DBG("BT_MAP_PUSH_MESSAGE");
4856
4857                 char* session_id = (char *)g_variant_get_data(in_param1);
4858                 char* source_file = (char *)g_variant_get_data(in_param2);
4859                 char* folder = (char *)g_variant_get_data(in_param3);
4860                 char* args_serialized = (char *)g_variant_get_data(in_param4);
4861
4862                 result = _bt_map_client_push_message(
4863                                 request_id, context, session_id, source_file, folder, args_serialized);
4864                 if (result == BLUETOOTH_ERROR_NONE)
4865                         BT_DBG("_bt_map_client_push_message succeed");
4866                 else
4867                         BT_ERR("_bt_map_client_push_message failed");
4868
4869                 break;
4870         }
4871
4872         case BT_MAP_GET_MESSAGE: {
4873                 BT_DBG("BT_MAP_GET_MESSAGE");
4874                 // TODO session currently is not used, but should be valid
4875                 //char* session_id = (char *)g_variant_get_data(in_param1);
4876                 char* message_object = (char *)g_variant_get_data(in_param2);
4877                 char* target_file = (char *)g_variant_get_data(in_param3);
4878                 bool attachment = false;
4879                 __bt_service_get_parameters(in_param4, &attachment, sizeof(bool));
4880
4881                 result = _bt_map_client_get_message(request_id, context, message_object,
4882                                 target_file, attachment);
4883                 if (result == BLUETOOTH_ERROR_NONE)
4884                         BT_DBG("_bt_map_client_get_message succeed");
4885
4886                 break;
4887         }
4888
4889         case BT_OBEX_SERVER_ALLOCATE: {
4890                 int app_pid;
4891                 gboolean is_native;
4892                 char *path;
4893                 char *sender;
4894
4895                 sender = (char *)g_dbus_method_invocation_get_sender(context);
4896
4897                 path = (char *)g_variant_get_data(in_param1);
4898                 __bt_service_get_parameters(in_param2, &is_native,
4899                                 sizeof(gboolean));
4900                 __bt_service_get_parameters(in_param3, &app_pid,
4901                                 sizeof(int));
4902
4903                 result = _bt_obex_server_allocate(sender,
4904                                 path, app_pid, is_native);
4905
4906                 break;
4907         }
4908         case BT_OBEX_SERVER_DEALLOCATE: {
4909                 int app_pid;
4910                 gboolean is_native;
4911
4912                 __bt_service_get_parameters(in_param1, &is_native,
4913                                 sizeof(gboolean));
4914                 __bt_service_get_parameters(in_param2, &app_pid,
4915                                 sizeof(int));
4916
4917                 result = _bt_obex_server_deallocate(app_pid, is_native);
4918                 break;
4919         }
4920         case BT_OBEX_SERVER_IS_ACTIVATED: {
4921                 gboolean is_activated = FALSE;
4922
4923                 result = _bt_obex_server_is_activated(&is_activated);
4924
4925                 g_array_append_vals(*out_param1, &is_activated,
4926                                 sizeof(gboolean));
4927
4928                 break;
4929         }
4930         case BT_OBEX_SERVER_ACCEPT_CONNECTION: {
4931                 result = _bt_obex_server_accept_connection(request_id);
4932
4933                 break;
4934         }
4935         case BT_OBEX_SERVER_REJECT_CONNECTION: {
4936                 result = _bt_obex_server_reject_connection();
4937
4938                 break;
4939         }
4940         case BT_OBEX_SERVER_ACCEPT_FILE: {
4941                 char *file_name;
4942
4943                 file_name = (char *)g_variant_get_data(in_param1);
4944
4945                 result = _bt_obex_server_accept_authorize(file_name, TRUE);
4946
4947                 break;
4948         }
4949         case BT_OBEX_SERVER_REJECT_FILE: {
4950                 result = _bt_obex_server_reject_authorize();
4951
4952                 break;
4953         }
4954         case BT_OBEX_SERVER_SET_PATH: {
4955                 gboolean is_native;
4956                 char *destination_path;
4957
4958                 destination_path = (char *)g_variant_get_data(in_param1);
4959                 __bt_service_get_parameters(in_param2, &is_native,
4960                                 sizeof(gboolean));
4961
4962                 result = _bt_obex_server_set_destination_path(destination_path,
4963                                                         is_native);
4964
4965                 break;
4966         }
4967         case BT_OBEX_SERVER_SET_ROOT: {
4968                 char *root;
4969
4970                 root = (char *)g_variant_get_data(in_param1);
4971
4972                 result = _bt_obex_server_set_root(root);
4973
4974                 break;
4975         }
4976         case BT_OBEX_SERVER_CANCEL_TRANSFER: {
4977                 int transfer_id;
4978
4979                 __bt_service_get_parameters(in_param1, &transfer_id,
4980                                 sizeof(int));
4981
4982                 result = _bt_obex_server_cancel_transfer(transfer_id);
4983
4984                 break;
4985         }
4986         case BT_OBEX_SERVER_CANCEL_ALL_TRANSFERS: {
4987                 result = _bt_obex_server_cancel_all_transfers();
4988
4989                 break;
4990         }
4991         case BT_OBEX_SERVER_IS_RECEIVING: {
4992                 gboolean is_receiving = FALSE;
4993
4994                 result = _bt_obex_server_is_receiving(&is_receiving);
4995
4996                 g_array_append_vals(*out_param1, &is_receiving,
4997                                 sizeof(gboolean));
4998                 break;
4999         }
5000         case BT_PBAP_CONNECT: {
5001                 bluetooth_device_address_t address = { {0} };
5002
5003                 __bt_service_get_parameters(in_param1, &address,
5004                                 sizeof(bluetooth_device_address_t));
5005
5006                 result = _bt_pbap_connect(&address);
5007                 break;
5008         }
5009         case BT_PBAP_DISCONNECT: {
5010                 bluetooth_device_address_t address = { {0} };
5011
5012                 __bt_service_get_parameters(in_param1, &address,
5013                                 sizeof(bluetooth_device_address_t));
5014
5015                 result = _bt_pbap_disconnect(&address);
5016                 break;
5017         }
5018         case BT_PBAP_GET_PHONEBOOK_SIZE: {
5019                 bluetooth_device_address_t address = { {0} };
5020                 bt_pbap_folder_t folder = { 0, };
5021
5022                 __bt_service_get_parameters(in_param1, &address,
5023                                 sizeof(bluetooth_device_address_t));
5024                 __bt_service_get_parameters(in_param2, &folder,
5025                                 sizeof(bt_pbap_folder_t));
5026
5027                 result = _bt_pbap_get_phonebook_size(&address,
5028                                 folder.addressbook, folder.folder_type);
5029                 break;
5030         }
5031         case BT_PBAP_GET_PHONEBOOK: {
5032                 bluetooth_device_address_t address = { {0} };
5033                 bt_pbap_folder_t folder = { 0, };
5034                 bt_pbap_pull_parameters_t app_param = { 0, };
5035
5036                 __bt_service_get_parameters(in_param1, &address,
5037                                 sizeof(bluetooth_device_address_t));
5038                 __bt_service_get_parameters(in_param2, &folder,
5039                                 sizeof(bt_pbap_folder_t));
5040                 __bt_service_get_parameters(in_param3, &app_param,
5041                                 sizeof(bt_pbap_pull_parameters_t));
5042
5043                 result = _bt_pbap_get_phonebook(&address, folder.addressbook,
5044                                 folder.folder_type, &app_param);
5045                 break;
5046         }
5047         case BT_PBAP_GET_LIST: {
5048                 bluetooth_device_address_t address = { {0} };
5049                 bt_pbap_folder_t folder = { 0, };
5050                 bt_pbap_list_parameters_t app_param = { 0, };
5051
5052                 __bt_service_get_parameters(in_param1, &address,
5053                                 sizeof(bluetooth_device_address_t));
5054                 __bt_service_get_parameters(in_param2, &folder,
5055                                 sizeof(bt_pbap_folder_t));
5056                 __bt_service_get_parameters(in_param3, &app_param,
5057                                 sizeof(bt_pbap_list_parameters_t));
5058
5059                 result = _bt_pbap_get_list(&address, folder.addressbook,
5060                                 folder.folder_type, &app_param);
5061                 break;
5062         }
5063         case BT_PBAP_PULL_VCARD: {
5064                 bluetooth_device_address_t address = { {0} };
5065                 bt_pbap_folder_t folder = { 0, };
5066                 bt_pbap_pull_vcard_parameters_t app_param = { 0, };
5067
5068                 __bt_service_get_parameters(in_param1, &address,
5069                                 sizeof(bluetooth_device_address_t));
5070                 __bt_service_get_parameters(in_param2, &folder,
5071                                 sizeof(bt_pbap_folder_t));
5072                 __bt_service_get_parameters(in_param3, &app_param,
5073                                 sizeof(bt_pbap_pull_vcard_parameters_t));
5074
5075                 result = _bt_pbap_pull_vcard(&address, folder.addressbook,
5076                                 folder.folder_type, &app_param);
5077                 break;
5078         }
5079         case BT_PBAP_PHONEBOOK_SEARCH: {
5080                 bluetooth_device_address_t address = { {0} };
5081                 bt_pbap_folder_t folder = { 0, };
5082                 bt_pbap_search_parameters_t app_param = { 0, };
5083
5084                 __bt_service_get_parameters(in_param1, &address,
5085                                 sizeof(bluetooth_device_address_t));
5086                 __bt_service_get_parameters(in_param2, &folder,
5087                                 sizeof(bt_pbap_folder_t));
5088                 __bt_service_get_parameters(in_param3, &app_param,
5089                                 sizeof(bt_pbap_search_parameters_t));
5090
5091                 result = _bt_pbap_phonebook_search(&address, folder.addressbook,
5092                                 folder.folder_type, &app_param);
5093                 break;
5094         }
5095
5096         default:
5097                 BT_ERR("Unknown function!");
5098                 result = BLUETOOTH_ERROR_INTERNAL;
5099                 break;
5100         }
5101
5102         FN_END;
5103
5104         return result;
5105 }
5106
5107 int __bt_agent_request(int function_name,
5108                 int request_type,
5109                 int request_id,
5110                 GDBusMethodInvocation *context,
5111                 GVariant *in_param1,
5112                 GVariant *in_param2,
5113                 GVariant *in_param3,
5114                 GVariant *in_param4,
5115                 GArray **out_param1)
5116 {
5117         int result;
5118         switch (function_name) {
5119         case BT_SET_AUTHORIZATION: {
5120                 int type;
5121                 char *uuid;
5122                 char *path;
5123                 int fd;
5124                 char *sender;
5125
5126                 sender = (char *)g_dbus_method_invocation_get_sender(context);
5127                 __bt_service_get_parameters(in_param1, &type, sizeof(int));
5128                 uuid = (char *)g_variant_get_data(in_param2);
5129                 path = (char *)g_variant_get_data(in_param3);
5130                 __bt_service_get_parameters(in_param4, &fd, sizeof(int));
5131
5132                 result = _bt_register_osp_server_in_agent(sender, type, uuid, path, fd);
5133                 break;
5134         }
5135         case BT_UNSET_AUTHORIZATION: {
5136                 int type;
5137                 char *uuid;
5138
5139                 __bt_service_get_parameters(in_param1, &type, sizeof(int));
5140                 uuid = (char *)g_variant_get_data(in_param2);
5141
5142                 result = _bt_unregister_osp_server_in_agent(type, uuid);
5143                 break;
5144         }
5145         default:
5146                 BT_ERR("Unknown function!");
5147                 result = BLUETOOTH_ERROR_INTERNAL;
5148                 break;
5149         }
5150
5151         return result;
5152 }
5153
5154 int __bt_core_request(int function_name,
5155                 int request_type,
5156                 int request_id,
5157                 GDBusMethodInvocation *context,
5158                 GVariant *in_param1)
5159 {
5160         int result;
5161
5162         switch (function_name) {
5163         case BT_ENABLE_ADAPTER:
5164         {
5165                 bt_status_t status;
5166                 _bt_set_adapter_request_state(TRUE);
5167                 status = _bt_adapter_get_status();
5168
5169                 if (status == BT_ACTIVATING) {
5170                         BT_DBG("Enabling in progress");
5171                         result = BLUETOOTH_ERROR_IN_PROGRESS;
5172                 } else if (status == BT_ACTIVATED) {
5173                         BT_DBG("Already enabled");
5174                         result = BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
5175                 } else {
5176                         _bt_adapter_set_status(BT_ACTIVATING);
5177                         result = BLUETOOTH_ERROR_NONE;
5178                 }
5179
5180                 break;
5181         }
5182         case BT_DISABLE_ADAPTER:
5183         {
5184                 bt_status_t status;
5185
5186                 status = _bt_adapter_get_status();
5187                 if (status == BT_DEACTIVATING) {
5188                                 BT_DBG("Disabling in progress");
5189                                 result = BLUETOOTH_ERROR_IN_PROGRESS;
5190                 } else if (status == BT_DEACTIVATED) {
5191                                 BT_DBG("Already disabled");
5192                                 result = BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
5193                 } else {
5194                         _bt_adapter_set_status(BT_DEACTIVATING);
5195                         result = BLUETOOTH_ERROR_NONE;
5196                 }
5197
5198                 break;
5199         }
5200         case BT_ENABLE_ADAPTER_LE:
5201         {
5202                 bt_le_status_t le_status;
5203                 _bt_set_le_request_state(TRUE);
5204                 le_status = _bt_adapter_get_le_status();
5205                 if (le_status == BT_LE_ACTIVATING) {
5206                         BT_DBG("Enabling in progress");
5207                         result = BLUETOOTH_ERROR_IN_PROGRESS;
5208                 } else if (le_status == BT_LE_ACTIVATED) {
5209                         BT_DBG("Already enabled");
5210                         result = BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
5211                 } else {
5212                         _bt_adapter_set_le_status(BT_LE_ACTIVATING);
5213                         result = BLUETOOTH_ERROR_NONE;
5214                 }
5215
5216                 break;
5217         }
5218         case BT_DISABLE_ADAPTER_LE:
5219         {
5220                 bt_le_status_t le_status;
5221
5222                 le_status = _bt_adapter_get_le_status();
5223                 if (le_status == BT_LE_DEACTIVATING) {
5224                                 BT_DBG("Disabling in progress");
5225                                 result = BLUETOOTH_ERROR_IN_PROGRESS;
5226                 } else if (le_status == BT_LE_DEACTIVATED) {
5227                                 BT_DBG("Already disabled");
5228                                 result = BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
5229                 } else {
5230                         _bt_adapter_set_le_status(BT_LE_DEACTIVATING);
5231                         result = BLUETOOTH_ERROR_NONE;
5232                 }
5233
5234                 break;
5235         }
5236         default:
5237                 BT_ERR("Unknown function!");
5238                 result = BLUETOOTH_ERROR_INTERNAL;
5239                 break;
5240         }
5241
5242         return result;
5243 }
5244
5245 static int __bt_service_get_requester_app_id(const char *unique_name, char *app_id, int len)
5246 {
5247         int ret;
5248         pid_t pid = 0;
5249
5250         retv_if(bt_service_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
5251         retv_if(unique_name == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
5252
5253         ret = __bt_service_get_sender_pid(unique_name, &pid);
5254         if (ret != BLUETOOTH_ERROR_NONE) {
5255                 BT_ERR("Fail to get the sender pid");
5256                 return ret;
5257         }
5258
5259         ret = aul_app_get_appid_bypid(pid, app_id, len);
5260         if (ret != AUL_R_OK) {
5261                 BT_ERR("app_id not found");
5262                 return BLUETOOTH_ERROR_NOT_SUPPORT;
5263         }
5264         BT_DBG("Sender app_id: %s", app_id);
5265
5266         return BLUETOOTH_ERROR_NONE;
5267 }
5268
5269 #ifdef TIZEN_FEATURE_BT_CONTAINER
5270 static void __bt_service_free_nspid(struct nspid_t *nspid)
5271 {
5272         g_free(nspid->unique_name);
5273         g_free(nspid);
5274 }
5275
5276 static void __bt_service_cleanup_nspid_list(const char *unique_name)
5277 {
5278         GSList *l;
5279         struct nspid_t *nspid;
5280
5281         ret_if(unique_name == NULL);
5282
5283         for (l = nspid_list; l; l = g_slist_next(l)) {
5284                 nspid = l->data;
5285                 if (!nspid)
5286                         continue;
5287                 if (!g_strcmp0(nspid->unique_name, unique_name)) {
5288                         nspid_list = g_slist_remove(nspid_list, nspid);
5289                         __bt_service_free_nspid(nspid);
5290                         return;
5291                 }
5292         }
5293 }
5294
5295 static gboolean __bt_service_is_container_request(const char *unique_name)
5296 {
5297         int ret;
5298         pid_t pid;
5299         char path[1024], buf[1024];
5300         FILE *fp;
5301         gboolean is_container = FALSE;
5302         GSList *l;
5303         struct nspid_t *nspid;
5304
5305         for (l = nspid_list; l; l = g_slist_next(l)) {
5306                 nspid = l->data;
5307                 if (!nspid)
5308                         continue;
5309                 if (!g_strcmp0(nspid->unique_name, unique_name))
5310                         return nspid->is_container;
5311         }
5312
5313         ret = __bt_service_get_sender_pid(unique_name, &pid);
5314         if (ret != BLUETOOTH_ERROR_NONE)
5315                 return FALSE;
5316
5317         sprintf(path, "/proc/%d/status", pid);
5318         fp = fopen(path, "r");
5319         if (fp == NULL) {
5320                 BT_ERR("fopen() failed. %d(%s)", errno, strerror(errno));
5321                 return FALSE;
5322         }
5323
5324         while (fgets(buf, sizeof(buf), fp)) {
5325                 if (strstr(buf, "NSpid:")) {
5326                         char *p = strchr(buf + 7, '\t'); /* find 2nd '\t' */
5327                         if (p) {
5328                                 BT_DBG("caller is container process");
5329                                 is_container = TRUE;
5330                                 goto out;
5331                         }
5332                         break;
5333                 }
5334         }
5335
5336 out:
5337         nspid = g_malloc0(sizeof(struct nspid_t));
5338         nspid->unique_name = g_strdup(unique_name);
5339         nspid->is_container = is_container;
5340         nspid_list = g_slist_append(nspid_list, nspid);
5341         fclose(fp);
5342         return is_container;
5343 }
5344 #endif
5345
5346 #ifdef TIZEN_FEATURE_BT_CONTAINER
5347 static gboolean __bt_service_check_privilege_in_container(int function_name,
5348                 const char *unique_name)
5349 {
5350         int result = BLUETOOTH_ERROR_NONE;
5351         GError *error = NULL;
5352         GDBusProxy *proxy;
5353         GArray *in_param1, *in_param2, *in_param3, *in_param4, *in_param5;
5354         GVariant *param1, *param2, *param3, *param4, *param5, *out_param;
5355
5356         proxy = _bt_get_container_proxy();
5357         if (proxy == NULL) {
5358                 BT_ERR("container proxy is NULL");
5359                 return FALSE;
5360         }
5361
5362         in_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
5363         in_param2 = g_array_new(TRUE, TRUE, sizeof(gchar));
5364         in_param3 = g_array_new(TRUE, TRUE, sizeof(gchar));
5365         in_param4 = g_array_new(TRUE, TRUE, sizeof(gchar));
5366         in_param5 = g_array_new(TRUE, TRUE, sizeof(gchar));
5367
5368         /* The sender's privilege should be checked not bt-service in container */
5369         g_array_append_vals(in_param1, unique_name, strlen(unique_name));
5370
5371         param1 = g_variant_new_from_data((const GVariantType *)"ay",
5372                         in_param1->data, in_param1->len, TRUE, NULL, NULL);
5373         param2 = g_variant_new_from_data((const GVariantType *)"ay",
5374                         in_param2->data, in_param2->len, TRUE, NULL, NULL);
5375         param3 = g_variant_new_from_data((const GVariantType *)"ay",
5376                         in_param3->data, in_param3->len, TRUE, NULL, NULL);
5377         param4 = g_variant_new_from_data((const GVariantType *)"ay",
5378                         in_param4->data, in_param4->len, TRUE, NULL, NULL);
5379         param5 = g_variant_new_from_data((const GVariantType *)"ay",
5380                         in_param5->data, in_param5->len, TRUE, NULL, NULL);
5381
5382         BT_DBG("This request is from container. Privilege will be checked in container. unique_name: %s", unique_name);
5383
5384         GVariant *ret = g_dbus_proxy_call_sync(proxy, "service_request",
5385                         g_variant_new("(iii@ay@ay@ay@ay@ay)",
5386                                         BT_CHECK_PRIVILEGE, function_name,
5387                                         BT_SYNC_REQ, param1, param2,
5388                                         param3, param4, param5),
5389                         G_DBUS_CALL_FLAGS_NONE, -1,
5390                         NULL, &error);
5391         if (ret == NULL) {
5392                 BT_ERR("dBUS-RPC is failed");
5393
5394                 if (error != NULL) {
5395                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
5396                                         error->code, error->message);
5397                         g_clear_error(&error);
5398                 } else {
5399                         BT_ERR("error returned was NULL");
5400                 }
5401
5402                 result = BLUETOOTH_ERROR_INTERNAL;
5403         } else {
5404                 g_variant_get(ret, "(iv)", &result, &out_param);
5405                 g_variant_unref(out_param);
5406                 g_variant_unref(ret);
5407         }
5408
5409         g_array_free(in_param1, TRUE);
5410         g_array_free(in_param2, TRUE);
5411         g_array_free(in_param3, TRUE);
5412         g_array_free(in_param4, TRUE);
5413         g_array_free(in_param5, TRUE);
5414
5415         return (result == BLUETOOTH_ERROR_NONE) ? TRUE : FALSE;
5416 }
5417 #endif
5418
5419 gboolean __bt_service_check_privilege(int function_name,
5420                                         int service_type,
5421                                         const char *unique_name)
5422 {
5423         int ret_val;
5424         gboolean result = TRUE;
5425         char *client_creds = NULL;
5426         char *user_creds = NULL;
5427         char *client_session = "";
5428         enum cynara_client_creds client_creds_method = CLIENT_METHOD_SMACK;
5429         enum cynara_user_creds user_creds_method = USER_METHOD_UID;
5430         char err_msg[256] = {0, };
5431
5432         retv_if(unique_name == NULL, FALSE);
5433         retv_if(bt_service_conn == NULL, FALSE);
5434
5435 #ifdef TIZEN_FEATURE_BT_CONTAINER
5436         if (_bt_service_is_container() == FALSE)
5437                 if (__bt_service_is_container_request(unique_name) == TRUE)
5438                         return __bt_service_check_privilege_in_container(function_name, unique_name);
5439 #endif
5440
5441         ret_val = cynara_creds_get_default_client_method(&client_creds_method);
5442         if (ret_val != CYNARA_API_SUCCESS) {
5443                 cynara_strerror(ret_val, err_msg, sizeof(err_msg));
5444                 BT_ERR("Fail to get default client method: %s", err_msg);
5445                 return FALSE;
5446         }
5447
5448         ret_val = cynara_creds_get_default_user_method(&user_creds_method);
5449         if (ret_val != CYNARA_API_SUCCESS) {
5450                 cynara_strerror(ret_val, err_msg, sizeof(err_msg));
5451                 BT_ERR("Fail to get default user method: %s", err_msg);
5452                 return FALSE;
5453         }
5454
5455         ret_val = cynara_creds_gdbus_get_client(bt_service_conn, unique_name, client_creds_method, &client_creds);
5456         if (ret_val != CYNARA_API_SUCCESS) {
5457                 cynara_strerror(ret_val, err_msg, sizeof(err_msg));
5458                 BT_ERR("Fail to get client credential: %s", err_msg);
5459                 return FALSE;
5460         }
5461
5462         ret_val = cynara_creds_gdbus_get_user(bt_service_conn, unique_name, user_creds_method, &user_creds);
5463         if (ret_val != CYNARA_API_SUCCESS) {
5464                 cynara_strerror(ret_val, err_msg, sizeof(err_msg));
5465                 BT_ERR("Fail to get user credential: %s", err_msg);
5466                 if (client_creds)
5467                         free(client_creds);
5468                 return FALSE;
5469         }
5470
5471         BT_DBG("%s, %s, %s", unique_name, client_creds, user_creds);
5472
5473         switch (function_name) {
5474         case BT_SET_LOCAL_NAME:
5475         case BT_START_DISCOVERY:
5476         case BT_START_CUSTOM_DISCOVERY:
5477         case BT_CANCEL_DISCOVERY:
5478         case BT_OOB_ADD_REMOTE_DATA:
5479         case BT_OOB_REMOVE_REMOTE_DATA:
5480         case BT_OOB_READ_LOCAL_DATA:
5481         case BT_SET_ADVERTISING:
5482         case BT_SET_CUSTOM_ADVERTISING:
5483         case BT_SET_ADVERTISING_PARAMETERS:
5484         case BT_START_LE_DISCOVERY:
5485         case BT_STOP_LE_DISCOVERY:
5486         case BT_SET_SCAN_PARAMETERS:
5487         case BT_SET_SCAN_TYPE:
5488         case BT_UPDATE_LE_CONNECTION_MODE:
5489
5490         case BT_BOND_DEVICE:
5491         case BT_CANCEL_BONDING:
5492         case BT_UNBOND_DEVICE:
5493         case BT_SET_ALIAS:
5494         case BT_SET_AUTHORIZATION:
5495         case BT_UNSET_AUTHORIZATION:
5496         case BT_SEARCH_SERVICE:
5497
5498         case BT_RFCOMM_CLIENT_CONNECT:
5499                 if (function_name == BT_RFCOMM_CLIENT_CONNECT)
5500                         BT_PERMANENT_LOG("Connect socket");
5501         case BT_RFCOMM_CLIENT_CANCEL_CONNECT:
5502         case BT_RFCOMM_SOCKET_DISCONNECT:
5503                 if (function_name == BT_RFCOMM_SOCKET_DISCONNECT)
5504                         BT_PERMANENT_LOG("Disconnect socket");
5505         case BT_RFCOMM_SOCKET_WRITE:
5506         case BT_RFCOMM_CREATE_SOCKET:
5507         case BT_RFCOMM_REMOVE_SOCKET:
5508         case BT_RFCOMM_SEND_RX_DETAILS:
5509         case BT_RFCOMM_SEND_TX_DETAILS:
5510         case BT_RFCOMM_UPDATE_CONNECTION_INFO:
5511
5512         case BT_OPP_PUSH_FILES:
5513         case BT_OPP_CANCEL_PUSH:
5514         /* TODO: MAP? MAP functions, see above */
5515
5516         case BT_OBEX_SERVER_ACCEPT_CONNECTION:
5517         case BT_OBEX_SERVER_REJECT_CONNECTION:
5518         case BT_OBEX_SERVER_ACCEPT_FILE:
5519         case BT_OBEX_SERVER_REJECT_FILE:
5520         case BT_OBEX_SERVER_SET_PATH:
5521         case BT_OBEX_SERVER_SET_ROOT:
5522         case BT_OBEX_SERVER_CANCEL_TRANSFER:
5523         case BT_OBEX_SERVER_CANCEL_ALL_TRANSFERS:
5524
5525         case BT_AUDIO_SELECT_ROLE:
5526         case BT_AUDIO_CONNECT:
5527         case BT_AUDIO_DISCONNECT:
5528         case BT_AG_CONNECT:
5529         case BT_AG_DISCONNECT:
5530         case BT_AV_CONNECT:
5531         case BT_AV_DISCONNECT:
5532         case BT_AV_SOURCE_CONNECT:
5533         case BT_AV_SOURCE_DISCONNECT:
5534         case BT_AVRCP_TARGET_CONNECT:
5535         case BT_AVRCP_TARGET_DISCONNECT:
5536         case BT_AVRCP_CONTROL_CONNECT:
5537         case BT_AVRCP_CONTROL_DISCONNECT:
5538         case BT_AVRCP_HANDLE_CONTROL:
5539         case BT_AVRCP_SET_TRACK_INFO:
5540         case BT_AVRCP_SET_PROPERTY:
5541         case BT_AVRCP_SET_PROPERTIES:
5542         case BT_AVRCP_CONTROL_SET_PROPERTY:
5543
5544         case BT_AUDIO_SET_ABSOLUTE_VOLUME:
5545         case BT_AUDIO_GET_ABSOLUTE_VOLUME:
5546         case BT_AUDIO_IS_AVC_ACTIVATED:
5547
5548         case BT_HF_CONNECT:
5549         case BT_HF_DISCONNECT:
5550
5551         case BT_HID_CONNECT:
5552         case BT_HID_DISCONNECT:
5553
5554         case BT_HID_DEVICE_ACTIVATE:
5555         case BT_HID_DEVICE_DEACTIVATE:
5556         case BT_HID_DEVICE_CONNECT:
5557         case BT_HID_DEVICE_DISCONNECT:
5558         case BT_HID_DEVICE_SEND_MOUSE_EVENT:
5559         case BT_HID_DEVICE_SEND_KEY_EVENT:
5560         case BT_HID_DEVICE_SEND_REPLY_TO_REPORT:
5561         case BT_HID_DEVICE_SEND_CUSTOM_EVENT:
5562
5563         case BT_CONNECT_LE:
5564         case BT_DISCONNECT_LE:
5565
5566         case BT_SET_ADVERTISING_DATA:
5567         case BT_SET_SCAN_RESPONSE_DATA:
5568
5569         case BT_HDP_CONNECT:
5570         case BT_HDP_DISCONNECT:
5571         case BT_HDP_SEND_DATA:
5572         case BT_HDP_REGISTER_SINK_APP:
5573         case BT_HDP_UNREGISTER_SINK_APP:
5574         case BT_HDP_GET_FD:
5575
5576         case BT_DPM_SET_ALLOW_BT_MODE:
5577         case BT_DPM_GET_ALLOW_BT_MODE:
5578         case BT_DPM_SET_DEVICE_RESTRITION:
5579         case BT_DPM_GET_DEVICE_RESTRITION:
5580         case BT_DPM_SET_UUID_RESTRITION:
5581         case BT_DPM_GET_UUID_RESTRITION:
5582         case BT_DPM_ADD_DEVICES_BLOCKLIST:
5583         case BT_DPM_ADD_DEVICES_ALLOWLIST:
5584         case BT_DPM_ADD_UUIDS_BLOCKLIST:
5585         case BT_DPM_ADD_UUIDS_ALLOWLIST:
5586         case BT_DPM_CLEAR_DEVICES_BLOCKLIST:
5587         case BT_DPM_CLEAR_DEVICES_ALLOWLIST:
5588         case BT_DPM_CLEAR_UUIDS_BLOCKLIST:
5589         case BT_DPM_CLEAR_UUIDS_ALLOWLIST:
5590         case BT_DPM_REMOVE_DEVICE_BLOCKLIST:
5591         case BT_DPM_REMOVE_DEVICE_ALLOWLIST:
5592         case BT_DPM_REMOVE_UUID_BLOCKLIST:
5593         case BT_DPM_REMOVE_UUID_ALLOWLIST:
5594         case BT_DPM_GET_DEVICES_BLOCKLIST:
5595         case BT_DPM_GET_DEVICES_ALLOWLIST:
5596         case BT_DPM_GET_UUIDS_BLOCKLIST:
5597         case BT_DPM_GET_UUIDS_ALLOWLIST:
5598         case BT_DPM_SET_ALLOW_OUTGOING_CALL:
5599         case BT_DPM_GET_ALLOW_OUTGOING_CALL:
5600         case BT_DPM_SET_PAIRING_STATE:
5601         case BT_DPM_GET_PAIRING_STATE:
5602         case BT_DPM_SET_PROFILE_STATE:
5603         case BT_DPM_GET_PROFILE_STATE:
5604         case BT_DPM_SET_DESKROP_CONNECTIVITY_STATE:
5605         case BT_DPM_GET_DESKROP_CONNECTIVITY_STATE:
5606         case BT_DPM_SET_DISCOVERABLE_STATE:
5607         case BT_DPM_GET_DISCOVERABLE_STATE:
5608         case BT_DPM_SET_LIMITED_DISCOVERABLE_STATE:
5609         case BT_DPM_GET_LIMITED_DISCOVERABLE_STATE:
5610         case BT_DPM_SET_DATA_TRANSFER_STATE:
5611         case BT_DPM_GET_DATA_TRANSFER_STATE:
5612
5613         case BT_NETWORK_ACTIVATE:
5614         case BT_NETWORK_DEACTIVATE:
5615         case BT_NETWORK_CONNECT:
5616         case BT_NETWORK_DISCONNECT:
5617         case BT_NETWORK_SERVER_DISCONNECT:
5618
5619         case BT_GATT_GET_PRIMARY_SERVICES:
5620         case BT_GATT_GET_SERVICE_PROPERTIES:             /* GATT Client */
5621         case BT_GATT_GET_CHARACTERISTIC_PROPERTIES:      /* GATT Client */
5622         case BT_GATT_WATCH_SERVICE_CHANGED_INDICATION:   /* GATT Client */
5623         case BT_GATT_DISCOVER_CHARACTERISTICS:
5624         case BT_GATT_SET_PROPERTY_REQUEST:
5625         case BT_GATT_READ_CHARACTERISTIC:
5626         case BT_GATT_READ_DESCRIPTOR_VALUE:              /* GATT Client */
5627         case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: /* GATT Client */
5628         case BT_GATT_ACQUIRE_WRITE:
5629         case BT_GATT_WRITE_DESCRIPTOR_VALUE:             /* GATT Client */
5630         case BT_GATT_WATCH_CHARACTERISTIC:               /* GATT Client */
5631         case BT_GATT_CLIENT_REGISTER:                    /* GATT Client */
5632         case BT_GATT_CLIENT_UNREGISTER:                   /* GATT Client */
5633         case BT_GATT_DISCOVER_CHARACTERISTICS_DESCRIPTOR:
5634         case BT_GATT_REGISTER_APPLICATION:
5635         case BT_GATT_REGISTER_SERVICE:
5636         case BT_GATT_SEND_RESPONSE:
5637 #ifndef GATT_DIRECT
5638         case BT_GATT_SERVER_REGISTER:
5639         case BT_GATT_SERVER_ADD_SERVICE:
5640         case BT_GATT_SERVER_ADD_CHARACTERISTIC:
5641         case BT_GATT_SERVER_ADD_DESCRIPTOR:
5642         case BT_GATT_SERVER_START_SERVICE:
5643         case BT_GATT_SERVER_STOP_SERVICE:
5644         case BT_GATT_SERVER_DELETE_SERVICE:
5645         case BT_GATT_SERVER_SET_SERVICE_PERSISTENCE:
5646         case BT_GATT_SERVER_UNSET_SERVICE_PERSISTENCE:
5647         case BT_GATT_SERVER_SEND_RESPONSE:
5648         case BT_GATT_SERVER_ACQURE_WRITE_RESPONSE:
5649         case BT_GATT_SERVER_ACQUIRE_NOTIFY_RESPONSE:
5650         case BT_GATT_SERVER_SEND_INDICATION:
5651         case BT_GATT_SERVER_UPDATE_VALUE:
5652         case BT_GATT_SERVER_DEREGISTER:
5653 #endif
5654         case BT_GATT_SERVER_READ_PHY:
5655         case BT_GATT_SERVER_SET_PHY:
5656         case BT_GATT_CLIENT_READ_PHY:
5657         case BT_GATT_CLIENT_SET_PHY:
5658         case BT_REQ_ATT_MTU:
5659         case BT_PBAP_CONNECT:
5660         case BT_PBAP_DISCONNECT:
5661         case BT_PBAP_GET_PHONEBOOK_SIZE:
5662         case BT_PBAP_GET_PHONEBOOK:
5663         case BT_PBAP_GET_LIST:
5664         case BT_PBAP_PULL_VCARD:
5665         case BT_PBAP_PHONEBOOK_SEARCH:
5666
5667         case BT_RFCOMM_LISTEN_AND_ACCEPT:
5668
5669         case BT_L2CAP_LE_CLIENT_CONNECT:
5670         case BT_L2CAP_LE_CLIENT_CANCEL_CONNECT:
5671         case BT_L2CAP_LE_SOCKET_DISCONNECT:
5672         case BT_L2CAP_LE_SOCKET_WRITE:
5673         case BT_L2CAP_LE_CREATE_SOCKET:
5674         case BT_L2CAP_LE_REMOVE_SOCKET:
5675         case BT_L2CAP_LE_LISTEN_AND_ACCEPT:
5676         case BT_L2CAP_LE_ACCEPT_CONNECTION:
5677         case BT_L2CAP_LE_REJECT_CONNECTION:
5678         case BT_L2CAP_LE_LISTEN:
5679
5680         ret_val = cynara_check(p_cynara, client_creds, client_session, user_creds,
5681                                                 BT_PRIVILEGE_PUBLIC);
5682
5683         BT_INFO("Client Credentials [%s]", client_creds);
5684         if (ret_val != CYNARA_API_ACCESS_ALLOWED) {
5685                 BT_ERR("Fail to access: %s", BT_PRIVILEGE_PUBLIC);
5686                 result = FALSE;
5687         }
5688
5689         /* Need to check mediastorage privilege */
5690         if (function_name == BT_PBAP_GET_PHONEBOOK ||
5691                 function_name == BT_PBAP_PULL_VCARD) {
5692                 ret_val = cynara_check(p_cynara, client_creds, client_session, user_creds,
5693                                                         MEDIASTORAGE_PRIVILEGE);
5694
5695                 if (ret_val != CYNARA_API_ACCESS_ALLOWED) {
5696                 BT_ERR("Fail to access: %s", MEDIASTORAGE_PRIVILEGE);
5697                 result = FALSE;
5698                 }
5699         }
5700         break;
5701
5702         case BT_ENABLE_ADAPTER:
5703         case BT_DISABLE_ADAPTER:
5704         case BT_RESET_ADAPTER:
5705         case BT_RECOVER_ADAPTER:
5706         case BT_BATTERY_READ_DATA:
5707         case BT_ENABLE_ADAPTER_LE:
5708         case BT_DISABLE_ADAPTER_LE:
5709         case BT_SET_CONNECTABLE:
5710         case BT_SET_DISCOVERABLE_MODE:
5711         case BT_ADD_ALLOW_LIST:
5712         case BT_REMOVE_ALLOW_LIST:
5713         case BT_CLEAR_ALLOW_LIST:
5714         case BT_SET_MANUFACTURER_DATA:
5715
5716         case BT_CANCEL_SEARCH_SERVICE:
5717         case BT_ENABLE_RSSI:
5718         case BT_DISCONNECT_DEVICE:
5719
5720         case BT_RFCOMM_ACCEPT_CONNECTION:
5721         case BT_RFCOMM_REJECT_CONNECTION:
5722         case BT_RFCOMM_LISTEN:
5723
5724         case BT_HID_ENABLE_BARCODE_FEATURE:
5725
5726         case BT_AVRCP_TRANSPORT_SET_PROPERTY:
5727         case BT_AVRCP_HANDLE_CONTROL_TO_DEST:
5728
5729         case BT_SET_CONTENT_PROTECT:
5730         case BT_BOND_DEVICE_BY_TYPE:
5731         case BT_SET_LE_PRIVACY:
5732         case BT_SET_LE_STATIC_RANDOM_ADDRESS:
5733         case BT_LE_CONN_UPDATE:
5734         case BT_GET_DEVICE_IDA:
5735         case BT_LE_READ_MAXIMUM_DATA_LENGTH:
5736         case BT_LE_WRITE_HOST_SUGGESTED_DATA_LENGTH:
5737         case BT_LE_READ_HOST_SUGGESTED_DATA_LENGTH:
5738         case BT_LE_SET_DATA_LENGTH:
5739         case BT_LE_OOB_READ_LOCAL_DATA:
5740         case BT_LE_OOB_ADD_REMOTE_DATA:
5741
5742         case BT_GET_GATT_DATA_BATCHING_AVAILABLE_PACKETS:
5743         case BT_ENABLE_GATT_DATA_BATCHING:
5744         case BT_DISABLE_GATT_DATA_BATCHING:
5745
5746         case BT_LE_IPSP_INIT:
5747         case BT_LE_IPSP_DEINIT:
5748         case BT_LE_IPSP_CONNECT:
5749         case BT_LE_IPSP_DISCONNECT:
5750         case BT_PXP_MONITOR_SET_PROPERTY:
5751         case BT_PXP_MONITOR_GET_PROPERTY:
5752         case BT_PXP_MONITOR_GET_SUPPORTED_SERIVCES:
5753         case BT_PXP_REPORTER_REGISTER:
5754         case BT_PXP_REPORTER_UNREGISTER:
5755         case BT_PXP_REPORTER_GET_PROPERTY:
5756
5757         /* TDS */
5758         case BT_TDS_PROVIDER_REGISTER:
5759         case BT_TDS_PROVIDER_UNREGISTER:
5760         case BT_TDS_PROVIDER_SET_MANUF_DATA:
5761         case BT_TDS_PROVIDER_CREATE:
5762         case BT_TDS_PROVIDER_DESTROY:
5763         case BT_TDS_PROVIDER_SET_TRANSPORT_DATA:
5764         case BT_TDS_SEND_ACTIVATION_RESPONSE:
5765         case BT_TDS_READ_TRANSPORT_DATA:
5766         case BT_TDS_ENABLE_CONTROL_POINT:
5767         case BT_TDS_ACTIVATE_CONTROL_POINT:
5768
5769         /* OTP Server */
5770         case BT_OTP_SERVER_INIT:
5771         case BT_OTP_SERVER_DEINIT:
5772         case BT_OTP_READ_VALUE:
5773         case BT_OTP_ENABLE_NOTIFICATION:
5774         case BT_OTP_WRITE_VALUE:
5775         case BT_LE_OTC_CONNECT:
5776         case BT_LE_OTC_DISCONNECT:
5777
5778         case BT_MAP_CREATE_SESSION:
5779         case BT_MAP_DESTROY_SESSION:
5780         case BT_MAP_SET_FOLDER:
5781         case BT_MAP_LIST_FOLDERS:
5782         case BT_MAP_LIST_FILTER_FIELDS:
5783         case BT_MAP_LIST_MESSAGES:
5784         case BT_MAP_UPDATE_INBOX:
5785         case BT_MAP_PUSH_MESSAGE:
5786         case BT_MAP_GET_MESSAGE:
5787
5788         case BT_MESH_INIT:
5789         case BT_MESH_DEINIT:
5790         case BT_MESH_NETWORK_CREATE:
5791         case BT_MESH_NETWORK_DESTROY:
5792         case BT_MESH_NETWORK_LOAD:
5793         case BT_MESH_NETWORK_UNLOAD:
5794         case BT_MESH_NETWORK_JOIN:
5795         case BT_MESH_CANCEL_JOIN:
5796         case BT_MESH_NETWORK_SCAN:
5797         case BT_MESH_NETWORK_CANCEL_SCAN:
5798         case BT_MESH_NETWORK_SET_CAPABILITIES:
5799         case BT_MESH_NETWORK_PROVISION_DEVICE:
5800         case BT_MESH_AUTHENTICATION_REPLY:
5801         case BT_MESH_NETWORK_SET_NAME:
5802         case BT_MESH_NETWORK_ADD_NETKEY:
5803         case BT_MESH_NETWORK_DELETE_NETKEY:
5804         case BT_MESH_NETWORK_UPDATE_NETKEY:
5805         case BT_MESH_NETWORK_ADD_APPKEY:
5806         case BT_MESH_NETWORK_DELETE_APPKEY:
5807         case BT_MESH_NETWORK_UPDATE_APPKEY:
5808         case BT_MESH_NETWORK_GET_NETKEYS:
5809         case BT_MESH_NETWORK_GET_APPKEYS:
5810         case BT_MESH_NETWORK_GET_NODES:
5811         case BT_MESH_ELEMENT_GET_MODELS:
5812         case BT_MESH_NODE_BROWSE:
5813         case BT_MESH_NODE_GET_VENDOR_FEATURES:
5814         case BT_MESH_NODE_CONFIGURE_KEY:
5815         case BT_MESH_NODE_TTL_EXECUTE:
5816         case BT_MESH_NODE_GET_NETKEYS:
5817         case BT_MESH_NODE_GET_APPKEYS:
5818         case BT_MESH_NODE_RESET:
5819         case BT_MESH_MODEL_CONFIGURE_APPKEY:
5820         case BT_MESH_MODEL_GET_APPKEY_LIST:
5821         case BT_MESH_MODEL_EXECUTE_MSG:
5822         case BT_MESH_MODEL_RESPONSE_MSG:
5823         case BT_MESH_NETWORK_CREATE_GROUP:
5824         case BT_MESH_NETWORK_REMOVE_GROUP:
5825         case BT_MESH_MODEL_CONFIG_GROUP_SUB:
5826         case BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB:
5827         case BT_MESH_MODEL_GET_SUBSCRIPTION_LIST:
5828         case BT_MESH_MODEL_SET_PUBLICATION:
5829         case BT_MESH_MODEL_GET_PUBLICATION:
5830         case BT_MESH_NETWORK_GET_GROUPS:
5831
5832         ret_val = cynara_check(p_cynara, client_creds, client_session, user_creds,
5833                                 BT_PRIVILEGE_PLATFORM);
5834
5835         if (ret_val != CYNARA_API_ACCESS_ALLOWED) {
5836                 BT_ERR("Fail to access: %s", BT_PRIVILEGE_PLATFORM);
5837                 result = FALSE;
5838         }
5839         break;
5840
5841         case BT_CHECK_ADAPTER:
5842         case BT_GET_RSSI:
5843
5844         case BT_GET_LOCAL_NAME:
5845         case BT_GET_LOCAL_ADDRESS:
5846         case BT_GET_LOCAL_VERSION:
5847         case BT_IS_SERVICE_USED:
5848         case BT_GET_DISCOVERABLE_MODE:
5849         case BT_GET_DISCOVERABLE_TIME:
5850         case BT_IS_DISCOVERYING:
5851         case BT_IS_LE_DISCOVERYING:
5852         case BT_IS_CONNECTABLE:
5853         case BT_GET_CONNECTABLE:
5854         case BT_GET_BONDED_DEVICES:
5855         case BT_GET_PROFILE_CONNECTED_DEVICES:
5856         case BT_GET_BONDED_DEVICE:
5857         case BT_PASSKEY_REPLY:
5858         case BT_PASSKEY_CONFIRMATION_REPLY:
5859         case BT_GET_IS_ALIAS_SET:
5860         case BT_IS_DEVICE_CONNECTED:
5861         case BT_GET_CONNECTED_LINK_TYPE:
5862         case BT_SET_PROFILE_TRUSTED:
5863         case BT_GET_PROFILE_TRUSTED:
5864         case BT_GET_SPEAKER_GAIN:
5865         case BT_SET_SPEAKER_GAIN:
5866         case BT_RFCOMM_CLIENT_IS_CONNECTED:
5867         case BT_RFCOMM_IS_UUID_AVAILABLE:
5868         case BT_GET_ADVERTISING_DATA:
5869         case BT_GET_SCAN_RESPONSE_DATA:
5870         case BT_IS_ADVERTISING:
5871         case BT_IS_LE_2M_PHY_SUPPORTED:
5872         case BT_IS_LE_CODED_PHY_SUPPORTED:
5873         case BT_SET_SCANNING_ROLE:
5874         case BT_SET_SCANNING_PHY:
5875         case BT_IS_LE_EXTENDED_ADVERTISING_SUPPORTED:
5876         case BT_IS_LE_EXTENDED_SCAN_SUPPORTED:
5877         case BT_LE_MAX_ADVERTISING_DATA_LEN:
5878         case BT_REGISTER_SCAN_FILTER:
5879         case BT_IS_SCAN_FILTER_SUPPORTED:
5880         case BT_GET_ATT_MTU:
5881
5882         case BT_OBEX_SERVER_ALLOCATE:
5883         case BT_OBEX_SERVER_DEALLOCATE:
5884         case BT_OBEX_SERVER_IS_ACTIVATED:
5885         case BT_OPP_GET_TRANSFER_PROGRESS:
5886         case BT_AVRCP_CONTROL_GET_PROPERTY:
5887         case BT_AVRCP_GET_TRACK_INFO:
5888
5889         case BT_L2CAP_LE_IS_PSM_AVAILABLE:
5890                 /* Non-privilege control */
5891                 break;
5892         default:
5893                 BT_ERR("Unknown function!");
5894                 result = FALSE;
5895                 break;
5896         }
5897
5898         if (function_name >= BT_FUNC_MESH_BASE) {
5899                 BT_INFO("MESH Function called");
5900                 char app_id[256] = { 0, };
5901
5902                 if (BLUETOOTH_ERROR_NONE == __bt_service_get_requester_app_id(unique_name,
5903                                         app_id, sizeof(app_id))) {
5904                         requester_unique_creds = g_strdup(app_id);
5905                 } else {
5906                         BT_DBG("Requester app_id not found, use client creds");
5907                         requester_unique_creds = g_strdup(client_creds);
5908                 }
5909                 BT_DBG("Requester unique_creds: %s", requester_unique_creds);
5910         } else {
5911                 BT_INFO("Non MESH Function called client creds [%s]", client_creds);
5912         }
5913
5914         if (client_creds)
5915                 free(client_creds);
5916
5917         if (user_creds)
5918                 free(user_creds);
5919
5920         return result;
5921 }
5922
5923 GDBusNodeInfo *__bt_service_create_method_node_info
5924                                         (const gchar *introspection_data)
5925 {
5926         GError *err = NULL;
5927         GDBusNodeInfo *node_info = NULL;
5928
5929         if (introspection_data == NULL) {
5930                 BT_ERR("Introspection XML not present");
5931                 return NULL;
5932         }
5933
5934         node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
5935
5936         if (err) {
5937                 BT_ERR("Unable to create node: %s", err->message);
5938                 g_clear_error(&err);
5939         }
5940         return node_info;
5941 }
5942
5943 int __bt_service_register_object(GDBusConnection *conn,
5944                 GDBusNodeInfo *node_info, gboolean reg)
5945 {
5946         static guint service_id = 0;
5947         GError *error = NULL;
5948
5949         if (reg) {
5950                 if (node_info == NULL)
5951                         return -1;
5952
5953                 service_id = g_dbus_connection_register_object(conn,
5954                                 BT_SERVICE_PATH,
5955                                 node_info->interfaces[0],
5956                                 &method_table,
5957                                 NULL, NULL, &error);
5958                 if (service_id == 0)
5959                         return -1;
5960         } else {
5961                 if (service_id > 0) {
5962                         g_dbus_connection_unregister_object(conn,
5963                                         service_id);
5964                         service_id = 0;
5965                 }
5966         }
5967
5968         return 0;
5969 }
5970
5971 static void __name_owner_changed(GDBusConnection *connection,
5972                 const gchar *sender_name,
5973                 const gchar *object_path,
5974                 const gchar *interface_name,
5975                 const gchar *signal_name,
5976                 GVariant *parameters,
5977                 gpointer user_data)
5978 {
5979         const char *name = NULL;
5980         const char *old_owner = NULL;
5981         const char *new_owner = NULL;
5982
5983         g_variant_get(parameters, "(&s&s&s)", &name, &old_owner, &new_owner);
5984         if (new_owner && *new_owner != '\0')
5985                 return;
5986
5987         _bt_check_hdp_app_termination(name);
5988
5989         /* Advertising App Termination */
5990         _bt_check_adv_app_termination(name);
5991
5992         /* Check if le_scanning app is terminated */
5993         _bt_check_le_scanner_app_termination(name);
5994
5995         /* Check if RFCOMM app is terminated */
5996         _bt_rfcomm_check_termination(name);
5997
5998         /* Mesh App Termination */
5999         _bt_check_mesh_app_termination(name);
6000
6001         /* Stop the Transport Discovery service */
6002         _bt_tds_stop_by_terminated_process(name);
6003
6004 #ifdef TIZEN_FEATURE_BT_CONTAINER
6005         /* Cleanup the NSpid list */
6006         __bt_service_cleanup_nspid_list(name);
6007 #endif
6008 }
6009
6010 static void __bt_service_bus_acquired_handler(GDBusConnection *connection,
6011                 const gchar *name, gpointer user_data)
6012 {
6013         GDBusNodeInfo *node_info = NULL;
6014
6015         BT_INFO("bus acquired");
6016
6017         ret_if(connection == NULL);
6018
6019         node_info = __bt_service_create_method_node_info(
6020                         bt_service_introspection_xml);
6021         ret_if(node_info == NULL);
6022
6023         __bt_service_register_object(connection, node_info, TRUE);
6024         g_dbus_node_info_unref(node_info);
6025
6026         bt_service_conn = connection;
6027 }
6028
6029 static void __bt_service_name_acquired_handler(GDBusConnection *connection,
6030                 const gchar *name, gpointer user_data)
6031 {
6032         BT_INFO("name acquired");
6033         name_acquired = TRUE;
6034
6035 #ifdef TIZEN_FEATURE_BT_CONTAINER
6036         if (_bt_service_is_container() == TRUE)
6037                 __bt_service_bus_acquired_handler(connection, name, user_data);
6038 #endif
6039 }
6040
6041 static void __bt_service_name_lost_handler(GDBusConnection *connection,
6042                 const gchar *name, gpointer user_data)
6043 {
6044         BT_INFO("name lost");
6045         name_acquired = FALSE;
6046 }
6047
6048 gboolean _is_name_acquired(void)
6049 {
6050         return name_acquired;
6051 }
6052
6053 void _bt_service_unref_connection(void)
6054 {
6055         BT_INFO("+");
6056
6057         if (bt_service_conn) {
6058                 g_dbus_connection_flush_sync(bt_service_conn, NULL, NULL);
6059                 g_object_unref(bt_service_conn);
6060                 bt_service_conn = NULL;
6061         }
6062
6063         BT_INFO("-");
6064 }
6065
6066 int _bt_service_register(void)
6067 {
6068         GDBusConnection *conn;
6069         GError *err = NULL;
6070
6071         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
6072         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
6073         bt_service_conn = conn;
6074
6075         owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
6076                                 BT_SERVICE_NAME,
6077                                 G_BUS_NAME_OWNER_FLAGS_NONE,
6078                                 __bt_service_bus_acquired_handler,
6079                                 __bt_service_name_acquired_handler,
6080                                 __bt_service_name_lost_handler,
6081                                 NULL, NULL);
6082         BT_DBG("owner_id is [%d]", owner_id);
6083         if (owner_id == 0)
6084                 goto fail;
6085
6086         /* Subscribe for name owner changed signal */
6087         owner_sig_id = g_dbus_connection_signal_subscribe(conn,
6088                         BT_FREEDESKTOP_SERVICE, BT_FREEDESKTOP_INTERFACE,
6089                         BT_NAME_OWNER_CHANGED, NULL, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
6090                         __name_owner_changed, NULL, NULL);
6091         BT_INFO("owner_sig_id: %d", owner_sig_id);
6092
6093         return BLUETOOTH_ERROR_NONE;
6094
6095 fail:
6096         if (bt_service_conn) {
6097                 g_object_unref(bt_service_conn);
6098                 bt_service_conn = NULL;
6099         }
6100
6101         return BLUETOOTH_ERROR_INTERNAL;
6102 }
6103
6104 #ifdef TIZEN_FEATURE_BT_CONTAINER
6105 int _bt_service_register_in_container(void)
6106 {
6107         GDBusConnection *conn;
6108         GError *err = NULL;
6109
6110         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
6111         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
6112
6113         /* Register dbus name because systemd will down bt-service
6114          * if we don't reigster the dbus name in container */
6115         owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
6116                                 BT_SERVICE_NAME,
6117                                 G_BUS_NAME_OWNER_FLAGS_NONE,
6118                                 NULL, NULL, NULL, NULL, NULL);
6119         BT_DBG("owner_id is [%d]", owner_id);
6120         if (owner_id == 0)
6121                 goto fail;
6122
6123         conn = g_dbus_connection_new_for_address_sync(
6124                         DBUS_HOST_SYSTEM_BUS_ADDRESS,
6125                         G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
6126                         NULL, NULL, &err);
6127         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
6128         bt_service_conn = conn;
6129
6130         /* Register to host dbus connection to call
6131          * the method toward container in host bt-service */
6132         owner_id = g_bus_own_name_on_connection(conn,
6133                                 BT_SERVICE_CONTAINER_NAME,
6134                                 G_BUS_NAME_OWNER_FLAGS_NONE,
6135                                 __bt_service_name_acquired_handler,
6136                                 __bt_service_name_lost_handler,
6137                                 NULL, NULL);
6138         BT_DBG("owner_id is [%d]", owner_id);
6139         if (owner_id == 0)
6140                 goto fail;
6141
6142         return BLUETOOTH_ERROR_NONE;
6143
6144 fail:
6145         if (bt_service_conn) {
6146                 g_object_unref(bt_service_conn);
6147                 bt_service_conn = NULL;
6148         }
6149
6150         return BLUETOOTH_ERROR_INTERNAL;
6151 }
6152 #endif
6153
6154 void _bt_service_unregister(void)
6155 {
6156         if (bt_service_conn) {
6157                 sd_notify(0, "STOPPING=1");
6158
6159                 if (owner_sig_id > 0) {
6160                         g_dbus_connection_signal_unsubscribe(
6161                                         bt_service_conn, owner_sig_id);
6162                         owner_sig_id = 0;
6163                 }
6164
6165                 if (owner_id > 0) {
6166                         g_bus_unown_name(owner_id);
6167                         owner_id = 0;
6168                 }
6169
6170                 __bt_service_register_object(bt_service_conn, NULL, FALSE);
6171         }
6172
6173 #ifdef TIZEN_FEATURE_BT_CONTAINER
6174         if (nspid_list) {
6175                 g_slist_free_full(nspid_list, (GDestroyNotify)__bt_service_free_nspid);
6176                 nspid_list = NULL;
6177         }
6178 #endif
6179 }
6180
6181 int _bt_service_cynara_init(void)
6182 {
6183         int result;
6184         char err_msg[256] = {0, };
6185
6186         retv_if(p_cynara != NULL, BLUETOOTH_ERROR_ALREADY_INITIALIZED);
6187
6188         result = cynara_initialize(&p_cynara, conf);
6189
6190         if (result != CYNARA_API_SUCCESS) {
6191                 cynara_strerror(result, err_msg, sizeof(err_msg));
6192                 BT_ERR("Fail to initialize cynara: [%s]", err_msg);
6193                 return BLUETOOTH_ERROR_INTERNAL;
6194         }
6195
6196         return BLUETOOTH_ERROR_NONE;
6197 }
6198
6199 void _bt_service_cynara_deinit(void)
6200 {
6201         int result;
6202         char err_msg[256] = {0, };
6203
6204         ret_if(p_cynara == NULL);
6205
6206         result = cynara_finish(p_cynara);
6207
6208         if (result != CYNARA_API_SUCCESS) {
6209                 cynara_strerror(result, err_msg, sizeof(err_msg));
6210                 BT_ERR("Fail to finish cynara: [%s]", err_msg);
6211                 return;
6212         }
6213
6214         p_cynara = NULL;
6215         conf = NULL;
6216 }
6217
6218 void _bt_service_method_return(GDBusMethodInvocation *invocation,
6219                 GArray *out_param, int result)
6220 {
6221         GVariant *out_var;
6222         BT_DBG("");
6223         out_var = g_variant_new_from_data((const GVariantType *)"ay",
6224                         out_param->data, out_param->len, TRUE, NULL, NULL);
6225
6226         g_dbus_method_invocation_return_value(invocation,
6227                         g_variant_new("(iv)", result, out_var));
6228 }
6229
6230 void _bt_service_method_return_with_unix_fd_list(GDBusMethodInvocation *invocation,
6231                 GArray *out_param, int result, GUnixFDList *fd_list)
6232 {
6233         GVariant *out_var;
6234         BT_DBG("");
6235         out_var = g_variant_new_from_data((const GVariantType *)"ay",
6236                         out_param->data, out_param->len, TRUE, NULL, NULL);
6237
6238         g_dbus_method_invocation_return_value_with_unix_fd_list(invocation,
6239                         g_variant_new("(iv)", result, out_var), fd_list);
6240 }