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