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