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