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