Merge "Implement TDS functionalities" 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_connect(BT_AUDIO_A2DP, &address);
1625
1626                 if (result != BLUETOOTH_ERROR_NONE) {
1627                         char addr[BT_ADDRESS_STRING_SIZE];
1628                         _bt_convert_addr_type_to_string(addr, address.addr);
1629                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1630                 } else {
1631                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1632                         _bt_convert_addr_type_to_string(addr, address.addr);
1633                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1634                         _bt_save_invocation_context(context, result, sender,
1635                                         function_name, (gpointer)addr);
1636                 }
1637                 break;
1638         }
1639         case BT_AUDIO_CONNECT: {
1640                 bluetooth_device_address_t address = { {0} };
1641                 __bt_service_get_parameters(in_param1,
1642                                 &address, sizeof(bluetooth_device_address_t));
1643
1644                 result = _bt_audio_connect(BT_AUDIO_ALL, &address);
1645
1646                 if (result != BLUETOOTH_ERROR_NONE) {
1647                         char addr[BT_ADDRESS_STRING_SIZE];
1648                         _bt_convert_addr_type_to_string(addr, address.addr);
1649                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1650                 } else {
1651                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1652                         _bt_convert_addr_type_to_string(addr, address.addr);
1653                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1654                         _bt_save_invocation_context(context, result, sender,
1655                                         function_name, (gpointer)addr);
1656                 }
1657                 break;
1658         }
1659         case BT_AUDIO_DISCONNECT: {
1660                 bluetooth_device_address_t address = { {0} };
1661                 __bt_service_get_parameters(in_param1,
1662                                 &address, sizeof(bluetooth_device_address_t));
1663
1664                 result = _bt_audio_disconnect(BT_AUDIO_ALL, &address);
1665
1666                 if (result != BLUETOOTH_ERROR_NONE) {
1667                         char addr[BT_ADDRESS_STRING_SIZE];
1668                         _bt_convert_addr_type_to_string(addr, address.addr);
1669                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1670                 } else {
1671                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1672                         _bt_convert_addr_type_to_string(addr, address.addr);
1673                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1674                         _bt_save_invocation_context(context, result, sender,
1675                                         function_name, (gpointer)addr);
1676                 }
1677                 break;
1678         }
1679         case BT_AV_DISCONNECT: {
1680                 bluetooth_device_address_t address = { {0} };
1681                 __bt_service_get_parameters(in_param1,
1682                                 &address, sizeof(bluetooth_device_address_t));
1683
1684                 result = _bt_audio_disconnect(BT_AUDIO_A2DP, &address);
1685
1686                 if (result != BLUETOOTH_ERROR_NONE) {
1687                         char addr[BT_ADDRESS_STRING_SIZE];
1688                         _bt_convert_addr_type_to_string(addr, address.addr);
1689                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1690                 } else {
1691                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1692                         _bt_convert_addr_type_to_string(addr, address.addr);
1693                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1694                         _bt_save_invocation_context(context, result, sender,
1695                                         function_name, (gpointer)addr);
1696                 }
1697                 break;
1698         }
1699         case BT_AG_CONNECT: {
1700                 bluetooth_device_address_t address = { {0} };
1701                 __bt_service_get_parameters(in_param1,
1702                                 &address, sizeof(bluetooth_device_address_t));
1703
1704                 result = _bt_audio_connect(BT_AUDIO_HSP, &address);
1705
1706                 if (result != BLUETOOTH_ERROR_NONE) {
1707                         char addr[BT_ADDRESS_STRING_SIZE];
1708                         _bt_convert_addr_type_to_string(addr, address.addr);
1709                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1710                 } else {
1711                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1712                         _bt_convert_addr_type_to_string(addr, address.addr);
1713                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1714                         _bt_save_invocation_context(context, result, sender,
1715                                         function_name, (gpointer)addr);
1716                 }
1717                 break;
1718         }
1719         case BT_AG_DISCONNECT: {
1720                 bluetooth_device_address_t address = { {0} };
1721
1722                 __bt_service_get_parameters(in_param1,
1723                                 &address, sizeof(bluetooth_device_address_t));
1724
1725                 result = _bt_audio_disconnect(BT_AUDIO_HSP, &address);
1726
1727                 if (result != BLUETOOTH_ERROR_NONE) {
1728                         char addr[BT_ADDRESS_STRING_SIZE];
1729                         _bt_convert_addr_type_to_string(addr, address.addr);
1730                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1731                 } else {
1732                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1733                         _bt_convert_addr_type_to_string(addr, address.addr);
1734                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1735                         _bt_save_invocation_context(context, result, sender,
1736                                         function_name, (gpointer)addr);
1737                 }
1738                 break;
1739         }
1740         case BT_AV_SOURCE_CONNECT: {
1741                 bluetooth_device_address_t address = { {0} };
1742
1743                 __bt_service_get_parameters(in_param1,
1744                                 &address, sizeof(bluetooth_device_address_t));
1745
1746                 result = _bt_audio_connect(BT_AUDIO_A2DP_SOURCE, &address);
1747                 if (result != BLUETOOTH_ERROR_NONE) {
1748                         char addr[BT_ADDRESS_STRING_SIZE];
1749                         _bt_convert_addr_type_to_string(addr, address.addr);
1750                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1751                 } else {
1752                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1753                         _bt_convert_addr_type_to_string(addr, address.addr);
1754                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1755                         _bt_save_invocation_context(context, result, sender,
1756                                         function_name, (gpointer)addr);
1757                 }
1758                 break;
1759         }
1760         case BT_AV_SOURCE_DISCONNECT: {
1761                 bluetooth_device_address_t address = { {0} };
1762
1763                 __bt_service_get_parameters(in_param1,
1764                                 &address, sizeof(bluetooth_device_address_t));
1765
1766                 result = _bt_audio_disconnect(BT_AUDIO_A2DP_SOURCE, &address);
1767                 if (result != BLUETOOTH_ERROR_NONE) {
1768                         char addr[BT_ADDRESS_STRING_SIZE];
1769                         _bt_convert_addr_type_to_string(addr, address.addr);
1770                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1771                 } else {
1772                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1773                         _bt_convert_addr_type_to_string(addr, address.addr);
1774                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1775                         _bt_save_invocation_context(context, result, sender,
1776                                         function_name, (gpointer)addr);
1777                 }
1778                 break;
1779         }
1780         case BT_HF_CONNECT: {
1781                 bluetooth_device_address_t address = { {0} };
1782
1783                 __bt_service_get_parameters(in_param1,
1784                                 &address, sizeof(bluetooth_device_address_t));
1785
1786                 result = _bt_hf_connect(&address);
1787                 if (result != BLUETOOTH_ERROR_NONE) {
1788                         char addr[BT_ADDRESS_STRING_SIZE];
1789                         _bt_convert_addr_type_to_string(addr, address.addr);
1790                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1791                 } else {
1792                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1793                         _bt_convert_addr_type_to_string(addr, address.addr);
1794                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1795                         _bt_save_invocation_context(context, result, sender,
1796                                         function_name, (gpointer)addr);
1797                 }
1798                 break;
1799         }
1800         case BT_HF_DISCONNECT: {
1801                 bluetooth_device_address_t address = { {0} };
1802
1803                 __bt_service_get_parameters(in_param1,
1804                                 &address, sizeof(bluetooth_device_address_t));
1805
1806                 result = _bt_hf_disconnect(&address);
1807                 if (result != BLUETOOTH_ERROR_NONE) {
1808                         char addr[BT_ADDRESS_STRING_SIZE];
1809                         _bt_convert_addr_type_to_string(addr, address.addr);
1810                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1811                 } else {
1812                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1813                         _bt_convert_addr_type_to_string(addr, address.addr);
1814                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1815                         _bt_save_invocation_context(context, result, sender,
1816                                         function_name, (gpointer)addr);
1817                 }
1818                 break;
1819         }
1820         case BT_AVRCP_TARGET_CONNECT: {
1821                 bluetooth_device_address_t address = { {0} };
1822
1823                 __bt_service_get_parameters(in_param1,
1824                                 &address, sizeof(bluetooth_device_address_t));
1825
1826                 result = _bt_audio_connect(BT_AVRCP_TARGET, &address);
1827                 if (result != BLUETOOTH_ERROR_NONE) {
1828                         char addr[BT_ADDRESS_STRING_SIZE];
1829                         _bt_convert_addr_type_to_string(addr, address.addr);
1830                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1831                 } else {
1832                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1833                         _bt_convert_addr_type_to_string(addr, address.addr);
1834                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1835                         _bt_save_invocation_context(context, result, sender,
1836                                         function_name, (gpointer)addr);
1837                 }
1838                 break;
1839         }
1840         case BT_AVRCP_TARGET_DISCONNECT: {
1841                 bluetooth_device_address_t address = { {0} };
1842
1843                 __bt_service_get_parameters(in_param1,
1844                                 &address, sizeof(bluetooth_device_address_t));
1845
1846                 result = _bt_audio_disconnect(BT_AVRCP_TARGET, &address);
1847                 if (result != BLUETOOTH_ERROR_NONE) {
1848                         char addr[BT_ADDRESS_STRING_SIZE];
1849                         _bt_convert_addr_type_to_string(addr, address.addr);
1850                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1851                 } else {
1852                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1853                         _bt_convert_addr_type_to_string(addr, address.addr);
1854                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1855                         _bt_save_invocation_context(context, result, sender,
1856                                         function_name, (gpointer)addr);
1857                 }
1858                 break;
1859         }
1860         case BT_AVRCP_CONTROL_CONNECT: {
1861                 bluetooth_device_address_t address = { {0} };
1862
1863                 __bt_service_get_parameters(in_param1,
1864                                 &address, sizeof(bluetooth_device_address_t));
1865
1866                 result = _bt_audio_connect(BT_AVRCP, &address);
1867                 if (result != BLUETOOTH_ERROR_NONE) {
1868                         char addr[BT_ADDRESS_STRING_SIZE];
1869                         _bt_convert_addr_type_to_string(addr, address.addr);
1870                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1871                 } else {
1872                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1873                         _bt_convert_addr_type_to_string(addr, address.addr);
1874                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1875                         _bt_save_invocation_context(context, result, sender,
1876                                         function_name, (gpointer)addr);
1877                 }
1878                 break;
1879         }
1880         case BT_AVRCP_CONTROL_DISCONNECT: {
1881                 bluetooth_device_address_t address = { {0} };
1882
1883                 __bt_service_get_parameters(in_param1,
1884                                 &address, sizeof(bluetooth_device_address_t));
1885
1886                 result = _bt_audio_disconnect(BT_AVRCP, &address);
1887                 if (result != BLUETOOTH_ERROR_NONE) {
1888                         char addr[BT_ADDRESS_STRING_SIZE];
1889                         _bt_convert_addr_type_to_string(addr, address.addr);
1890                         g_array_append_vals(*out_param1, addr, BT_ADDRESS_STRING_SIZE);
1891                 } else {
1892                         char *addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1893                         _bt_convert_addr_type_to_string(addr, address.addr);
1894                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1895                         _bt_save_invocation_context(context, result, sender,
1896                                         function_name, (gpointer)addr);
1897                 }
1898                 break;
1899         }
1900         case BT_AVRCP_HANDLE_CONTROL: {
1901                 int key_code;
1902                 __bt_service_get_parameters(in_param1, &key_code, sizeof(int));
1903                 result = _bt_avrcp_control_cmd(key_code);
1904                 break;
1905         }
1906         case BT_AVRCP_HANDLE_CONTROL_TO_DEST: {
1907                 int key_code;
1908                 bluetooth_device_address_t address = { { 0 } };
1909
1910                 __bt_service_get_parameters(in_param1, &key_code, sizeof(int));
1911                 __bt_service_get_parameters(in_param2,
1912                                                 &address, sizeof(bluetooth_device_address_t));
1913
1914                 result = _bt_avrcp_control_cmd_to_dest(key_code, &address);
1915                 break;
1916         }
1917         case BT_AVRCP_CONTROL_SET_PROPERTY: {
1918                 int type;
1919                 unsigned int value;
1920
1921                 __bt_service_get_parameters(in_param1,
1922                                 &type, sizeof(int));
1923                 __bt_service_get_parameters(in_param2,
1924                                 &value, sizeof(unsigned int));
1925
1926                 result = _bt_avrcp_control_set_property(type, value);
1927                 break;
1928         }
1929         case BT_AVRCP_TRANSPORT_SET_PROPERTY: {
1930                 int type;
1931                 unsigned int value;
1932                 BT_DBG("+");
1933
1934                 __bt_service_get_parameters(in_param1,
1935                                 &type, sizeof(int));
1936                 __bt_service_get_parameters(in_param2,
1937                                 &value, sizeof(unsigned int));
1938
1939                 result = _bt_avrcp_transport_set_property(type, value);
1940                 BT_DBG("-");
1941                 break;
1942         }
1943         case BT_AVRCP_CONTROL_GET_PROPERTY: {
1944                 int type;
1945
1946                 __bt_service_get_parameters(in_param1, &type, sizeof(int));
1947
1948                 result = _bt_avrcp_control_get_property(type);
1949                 /* Save invocation */
1950                 if (result == BLUETOOTH_ERROR_NONE) {
1951                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1952                         _bt_save_invocation_context(context, result, sender,
1953                                         function_name, g_memdup(&type, sizeof(int)));
1954                 }
1955                 break;
1956         }
1957         case BT_AVRCP_GET_TRACK_INFO: {
1958                 result = _bt_avrcp_control_get_track_info();
1959                 /* Save invocation */
1960                 if (result == BLUETOOTH_ERROR_NONE) {
1961                         sender = (char*)g_dbus_method_invocation_get_sender(context);
1962                         _bt_save_invocation_context(context, result, sender,
1963                                         function_name, NULL);
1964                 }
1965                 break;
1966         }
1967         case BT_HDP_REGISTER_SINK_APP: {
1968                 unsigned short data_type;
1969                 bt_hdp_role_type_t role;
1970                 bt_hdp_qos_type_t channel_type;
1971                 int *app_id = g_new0(int, 1);
1972
1973                 __bt_service_get_parameters(in_param1,
1974                                 &data_type, sizeof(short));
1975                 __bt_service_get_parameters(in_param2,
1976                                 &role, sizeof(bt_hdp_role_type_t));
1977                 __bt_service_get_parameters(in_param3,
1978                                 &channel_type, sizeof(bt_hdp_qos_type_t));
1979                 sender = (char*)g_dbus_method_invocation_get_sender(context);
1980
1981                 result = _bt_hdp_app_register(role,
1982                                 channel_type, data_type, sender, app_id);
1983                 if (result != BLUETOOTH_ERROR_NONE) {
1984                         g_array_append_vals(*out_param1, &app_id, sizeof(app_id));
1985                         g_free(app_id);
1986                 } else {
1987                         _bt_save_invocation_context(context, result, sender,
1988                                         function_name, (gpointer)app_id);
1989                 }
1990                 break;
1991         }
1992         case BT_HDP_UNREGISTER_SINK_APP: {
1993                 char *app_handle;
1994                 int *app_id = g_new0(int, 1);
1995
1996                 app_handle = (char *)g_variant_get_data(in_param1);
1997                 sscanf(app_handle, "health_app_%d", app_id);
1998                 result = _bt_hdp_app_unregister(*app_id);
1999                 if (result != BLUETOOTH_ERROR_NONE) {
2000                         g_free(app_id);
2001                 } else {
2002                         sender = (char*)g_dbus_method_invocation_get_sender(context);
2003                         _bt_save_invocation_context(context, result, sender,
2004                                         function_name, (gpointer)app_id);
2005                 }
2006                 break;
2007         }
2008         case BT_HDP_CONNECT: {
2009                 int app_id = -1;
2010                 char *app_handle;
2011
2012                 bt_hdp_connected_t *conn_info = NULL;
2013
2014                 conn_info = g_malloc0(sizeof(bt_hdp_connected_t));
2015
2016                 app_handle = (char *)g_variant_get_data(in_param1);
2017                 conn_info->app_handle = app_handle;
2018                 sscanf(app_handle, "health_app_%d", &app_id);
2019
2020                 __bt_service_get_parameters(in_param2,
2021                                 &(conn_info->type), sizeof(bt_hdp_qos_type_t));
2022                 __bt_service_get_parameters(in_param3,
2023                                 &(conn_info->device_address),
2024                                 sizeof(bluetooth_device_address_t));
2025
2026                 result = _bt_hdp_connect(app_id, &(conn_info->device_address),
2027                                 conn_info->type, (int *)(&(conn_info->channel_id)));
2028                 if (result != BLUETOOTH_ERROR_NONE) {
2029                         g_array_append_vals(*out_param1, conn_info,
2030                                         sizeof(bt_hdp_connected_t));
2031                         g_free(conn_info);
2032                 } else {
2033                         sender = (char*)g_dbus_method_invocation_get_sender(context);
2034                         _bt_save_invocation_context(context, result, sender,
2035                                         function_name, (gpointer)conn_info);
2036                 }
2037                 break;
2038         }
2039         case BT_HDP_DISCONNECT: {
2040                 bt_hdp_disconnected_t *hdp_disconn_info = NULL;
2041
2042                 hdp_disconn_info = g_malloc0(sizeof(bt_hdp_disconnected_t));
2043
2044                 __bt_service_get_parameters(in_param1,
2045                                 &(hdp_disconn_info->channel_id), sizeof(int));
2046                 __bt_service_get_parameters(in_param2,
2047                                 &(hdp_disconn_info->device_address),
2048                                 sizeof(bluetooth_device_address_t));
2049
2050                 result = _bt_hdp_disconnect(hdp_disconn_info->channel_id);
2051                 if (result != BLUETOOTH_ERROR_NONE) {
2052                         g_array_append_vals(*out_param1, hdp_disconn_info,
2053                                         sizeof(bt_hdp_disconnected_t));
2054                         g_free(hdp_disconn_info);
2055                 } else {
2056                         sender = (char*)g_dbus_method_invocation_get_sender(context);
2057                         _bt_save_invocation_context(context, result, sender,
2058                                         function_name, (gpointer)hdp_disconn_info);
2059                 }
2060                 break;
2061         }
2062         case BT_HDP_GET_FD: {
2063                 int *channel_id = g_new0(int, 1);
2064
2065                 __bt_service_get_parameters(in_param1, channel_id, sizeof(int));
2066
2067                 result = _bt_hdp_get_fd(*channel_id);
2068                 if (result != BLUETOOTH_ERROR_NONE) {
2069                         g_free(channel_id);
2070                 } else {
2071                         sender = (char*)g_dbus_method_invocation_get_sender(context);
2072                         _bt_save_invocation_context(context, result, sender,
2073                                         function_name, (gpointer)channel_id);
2074                 }
2075                 break;
2076         }
2077         case BT_HDP_SEND_DATA: {
2078                 /*
2079                  * This call to bt-service is only used for privilege check, so return
2080                  * BLUETOOTH_ERROR_NONE from here.
2081                  */
2082                 result = BLUETOOTH_ERROR_NONE;
2083                 break;
2084         }
2085         case BT_OOB_READ_LOCAL_DATA: {  //208
2086                 bt_oob_data_t local_oob_data;
2087
2088                 memset(&local_oob_data, 0x00, sizeof(bt_oob_data_t));
2089                 result = _bt_oob_read_local_data(&local_oob_data);
2090
2091                 g_array_append_vals(*out_param1, &local_oob_data,
2092                                 sizeof(bt_oob_data_t));
2093
2094                 break;
2095         }
2096         case BT_OOB_ADD_REMOTE_DATA: {   //209
2097                 bluetooth_device_address_t address = { {0} };
2098                 unsigned short address_type;
2099                 bt_oob_data_t remote_oob_data;
2100
2101                 __bt_service_get_parameters(in_param1,
2102                                 &address, sizeof(bluetooth_device_address_t));
2103                 __bt_service_get_parameters(in_param2,
2104                                 &address_type, sizeof(unsigned short));
2105                 __bt_service_get_parameters(in_param3,
2106                                 &remote_oob_data, sizeof(bt_oob_data_t));
2107
2108                 result = _bt_oob_add_remote_data(&address, address_type, &remote_oob_data);
2109
2110                 break;
2111         }
2112         case BT_OOB_REMOVE_REMOTE_DATA: {       //210
2113                 bluetooth_device_address_t address = { {0} };
2114
2115                 __bt_service_get_parameters(in_param1,
2116                                 &address, sizeof(bluetooth_device_address_t));
2117
2118                 result = _bt_oob_remove_remote_data(&address);
2119
2120                 break;
2121         }
2122         case BT_LE_OOB_READ_LOCAL_DATA: {
2123                 bt_oob_data_t local_oob_data;
2124                 bt_oob_data_t *local_oob_cache_data;
2125
2126                 local_oob_cache_data = _bt_le_oob_get_local_cache_data();
2127                 if (local_oob_cache_data && local_oob_cache_data->eir_len) {
2128                         g_array_append_vals(*out_param1, local_oob_cache_data,
2129                                 sizeof(bt_oob_data_t));
2130                         return BLUETOOTH_ERROR_NONE;
2131                 }
2132
2133                 /* We should modify this logic in later */
2134                 if (_bt_is_le_static_random_address_enabled() == false) {
2135                         /* Seperate BT & LE address */
2136                         _bt_set_le_static_random_address(TRUE);
2137                 }
2138
2139                 result = _bt_le_oob_read_local_data(&local_oob_data);
2140                 _bt_le_oob_set_local_cache_data(&local_oob_data);
2141
2142                 g_array_append_vals(*out_param1, &local_oob_data,
2143                                 sizeof(bt_oob_data_t));
2144
2145                 break;
2146         }
2147         case BT_LE_OOB_ADD_REMOTE_DATA: {
2148                 bluetooth_device_address_t address = { {0} };
2149                 unsigned short address_type;
2150                 bt_oob_data_t remote_oob_data;
2151
2152                 __bt_service_get_parameters(in_param1,
2153                                 &address, sizeof(bluetooth_device_address_t));
2154                 __bt_service_get_parameters(in_param2,
2155                                 &address_type, sizeof(unsigned short));
2156                 __bt_service_get_parameters(in_param3,
2157                                 &remote_oob_data, sizeof(bt_oob_data_t));
2158
2159                 result = _bt_oob_add_remote_data(&address, address_type, &remote_oob_data);
2160
2161                 break;
2162         }
2163         case BT_AVRCP_SET_TRACK_INFO: {
2164                 media_metadata_t data;
2165                 media_metadata_attributes_t meta_data;
2166
2167                 memset(&data, 0x00, sizeof(media_metadata_t));
2168                 memset(&meta_data, 0x00, sizeof(media_metadata_attributes_t));
2169
2170                 __bt_service_get_parameters(in_param1,
2171                                 &data, sizeof(media_metadata_t));
2172
2173                 meta_data.title = g_strdup(data.title);
2174                 meta_data.artist = g_strdup(data.artist);
2175                 meta_data.album = g_strdup(data.album);
2176                 meta_data.genre = g_strdup(data.genre);
2177                 meta_data.total_tracks = data.total_tracks;
2178                 meta_data.number = data.number;
2179                 meta_data.duration = (int64_t) data.duration;
2180
2181                 result = _bt_avrcp_set_track_info(&meta_data);
2182
2183                 g_free((gpointer)meta_data.title);
2184                 g_free((gpointer)meta_data.artist);
2185                 g_free((gpointer)meta_data.album);
2186                 g_free((gpointer)meta_data.genre);
2187
2188                 break;
2189         }
2190         case BT_AVRCP_SET_PROPERTY: {
2191                 int type;
2192                 unsigned int value;
2193
2194                 sender = (char *)g_dbus_method_invocation_get_sender(context);
2195
2196                 __bt_service_get_parameters(in_param1,
2197                                 &type, sizeof(int));
2198                 __bt_service_get_parameters(in_param2,
2199                                 &value, sizeof(unsigned int));
2200
2201                 if (value == STATUS_PLAYING) {
2202                         if (current_sender_playing)
2203                                 g_free(current_sender_playing);
2204                         current_sender_playing = g_strdup(sender);
2205                 }
2206                 if (g_strcmp0(sender, current_sender_playing) == 0 ||
2207                         current_sender_playing == NULL) {
2208                         BT_INFO("Current Player Status %d type %d Sender %s", value, type, sender);
2209                 } else {
2210                         BT_INFO("Current Player and this sender (%s) are different", sender);
2211                         result = BLUETOOTH_ERROR_NONE;
2212                         break;
2213                 }
2214                 result = _bt_avrcp_set_property(type, value);
2215
2216                 break;
2217         }
2218         case BT_AVRCP_SET_PROPERTIES: {
2219                 media_player_settings_t properties;
2220
2221                 memset(&properties, 0x00, sizeof(media_player_settings_t));
2222                 __bt_service_get_parameters(in_param1,
2223                                 &properties, sizeof(media_player_settings_t));
2224
2225                 result = _bt_avrcp_set_properties(&properties);
2226
2227                 break;
2228         }
2229         case BT_SET_ADVERTISING_DATA: {
2230                 char *app = NULL;
2231                 int *adv_handle;
2232                 bluetooth_advertising_data_t adv = { {0} };
2233                 int length;
2234                 gboolean use_reserved_slot = FALSE;
2235
2236                 app = (char *)g_dbus_method_invocation_get_sender(context);
2237                 adv_handle = g_malloc0(sizeof(int));
2238
2239                 __bt_service_get_parameters(in_param1,
2240                                 adv_handle, sizeof(int));
2241                 __bt_service_get_parameters(in_param3,
2242                                 &length, sizeof(int));
2243                 __bt_service_get_parameters(in_param2,
2244                                 &adv, length);
2245                 __bt_service_get_parameters(in_param4,
2246                                 &use_reserved_slot, sizeof(gboolean));
2247                 result = _bt_set_advertising_data(app, *adv_handle,
2248                                 &adv, length, use_reserved_slot);
2249                 if (result != BLUETOOTH_ERROR_NONE) {
2250                         BT_ERR("Set Advertising data failed!!");
2251                         g_free(adv_handle);
2252                 } else {
2253                         _bt_save_invocation_context(context, result, app,
2254                                         function_name, (gpointer)adv_handle);
2255                 }
2256                 break;
2257         }
2258         case BT_SET_SCAN_RESPONSE_DATA: {
2259                 char *app = NULL;
2260                 int *adv_handle;
2261                 bluetooth_scan_resp_data_t rsp = { {0} };
2262                 int length;
2263                 gboolean use_reserved_slot = FALSE;
2264
2265                 app = (char *)g_dbus_method_invocation_get_sender(context);
2266                 adv_handle = g_malloc0(sizeof(int));
2267
2268                 __bt_service_get_parameters(in_param1,
2269                                 adv_handle, sizeof(int));
2270                 __bt_service_get_parameters(in_param3,
2271                                 &length, sizeof(int));
2272                 __bt_service_get_parameters(in_param2,
2273                                 &rsp, length);
2274                 __bt_service_get_parameters(in_param4,
2275                                 &use_reserved_slot, sizeof(gboolean));
2276
2277                 result = _bt_set_scan_response_data(app, *adv_handle,
2278                                 &rsp, length, use_reserved_slot);
2279
2280                 if (result != BLUETOOTH_ERROR_NONE) {
2281                         BT_ERR("Set Scan Response Data failed!!");
2282                         g_free(adv_handle);
2283                 } else {
2284                         _bt_save_invocation_context(context, result, app,
2285                                         function_name, (gpointer)adv_handle);
2286                 }
2287                 break;
2288         }
2289         case BT_SET_ADVERTISING: {
2290                 char *app = NULL;
2291                 int *adv_handle;
2292                 gboolean enable = FALSE;
2293                 gboolean use_reserved_slot = FALSE;
2294
2295                 adv_handle = g_malloc0(sizeof(int));
2296                 __bt_service_get_parameters(in_param1,
2297                                 adv_handle, sizeof(int));
2298                 __bt_service_get_parameters(in_param2,
2299                                 &enable, sizeof(gboolean));
2300                 __bt_service_get_parameters(in_param3,
2301                                 &use_reserved_slot, sizeof(gboolean));
2302
2303                 app = (char *)g_dbus_method_invocation_get_sender(context);
2304
2305                 result = _bt_set_advertising(app, *adv_handle,
2306                                 enable, use_reserved_slot);
2307                 if (result != BLUETOOTH_ERROR_NONE) {
2308                         BT_ERR("Start Advertising failed!!");
2309                         g_free(adv_handle);
2310                 } else {
2311                         _bt_save_invocation_context(context, result, app,
2312                                         function_name, (gpointer)adv_handle);
2313                 }
2314                 break;
2315         }
2316         case BT_SET_CUSTOM_ADVERTISING: {
2317                 char *app = NULL;
2318                 int *adv_handle;
2319                 gboolean enable = FALSE;
2320                 bluetooth_advertising_params_t adv_params;
2321                 gboolean use_reserved_slot = FALSE;
2322
2323                 app = (char *)g_dbus_method_invocation_get_sender(context);
2324                 adv_handle = g_malloc0(sizeof(int));
2325
2326                 __bt_service_get_parameters(in_param1, adv_handle,
2327                                 sizeof(int));
2328                 __bt_service_get_parameters(in_param2, &enable,
2329                                 sizeof(gboolean));
2330                 __bt_service_get_parameters(in_param3, &adv_params,
2331                                 sizeof(bluetooth_advertising_params_t));
2332                 __bt_service_get_parameters(in_param4, &use_reserved_slot,
2333                                 sizeof(gboolean));
2334
2335                 BT_DBG("bluetooth_advertising_params_t [%d %d %d %d %d]",
2336                                 adv_params.interval_min, adv_params.interval_max,
2337                                 adv_params.filter_policy, adv_params.type, adv_params.tx_power_level);
2338                 result = _bt_set_custom_advertising(app, *adv_handle,
2339                                 enable, &adv_params, use_reserved_slot);
2340                 if (result != BLUETOOTH_ERROR_NONE) {
2341                         BT_ERR("Start Custom Advertising failed!!");
2342                         g_free(adv_handle);
2343                 } else {
2344                         _bt_save_invocation_context(context, result, app,
2345                                         function_name, (gpointer)adv_handle);
2346                 }
2347                 break;
2348         }
2349         case BT_GET_SCAN_RESPONSE_DATA: {
2350                 bluetooth_scan_resp_data_t rsp = { {0} };
2351                 char *app = NULL;
2352                 int length = 0;
2353                 int adv_handle;
2354                 app = (char *)g_dbus_method_invocation_get_sender(context);
2355
2356                 __bt_service_get_parameters(in_param1, &adv_handle,
2357                                 sizeof(int));
2358
2359                 result = _bt_get_scan_response_data(app, adv_handle, &rsp, &length);
2360                 if (result == BLUETOOTH_ERROR_NONE)
2361                         g_array_append_vals(*out_param1, rsp.data, length);
2362
2363                 break;
2364         }
2365         case BT_GET_ADVERTISING_DATA: {
2366                 bluetooth_advertising_data_t adv = { {0} };
2367                 char *app = NULL;
2368                 int length = 0;
2369                 int adv_handle;
2370                 app = (char *)g_dbus_method_invocation_get_sender(context);
2371
2372                 __bt_service_get_parameters(in_param1, &adv_handle,
2373                                 sizeof(int));
2374
2375                 result = _bt_get_advertising_data(app, adv_handle, &adv, &length);
2376                 if (result == BLUETOOTH_ERROR_NONE)
2377                         g_array_append_vals(*out_param1, adv.data, length);
2378
2379                 break;
2380         }
2381         case BT_GATT_REGISTER_APPLICATION: {
2382                 BT_DBG("Register GATT application:Unhandled!!");
2383                 break;
2384         }
2385         case BT_GATT_SERVER_REGISTER: {
2386                 BT_DBG("GATT Server instance initialization");
2387                 char *app;
2388
2389                 app = (char *)g_dbus_method_invocation_get_sender(context);
2390                 BT_DBG("GATT Server Unique Name [%s]", app);
2391
2392                 /* No ADV handle: Set 0 */
2393                 result = _bt_register_server_instance(app, 0);
2394
2395                 if (result != BLUETOOTH_ERROR_NONE) {
2396                         BT_ERR("GATT Server registration failed. result %d", result);
2397                 } else {
2398                         _bt_save_invocation_context(context, result, app,
2399                                         function_name, NULL);
2400                 }
2401                 break;
2402         }
2403         case BT_GATT_SERVER_DEREGISTER: {
2404                 char *app;
2405                 app = (char*)g_dbus_method_invocation_get_sender(context);
2406
2407                 result = _bt_unregister_server_instance(app, 0/* Adv Handle*/);
2408                 break;
2409         }
2410         case BT_GATT_SERVER_ADD_SERVICE: {
2411                 BT_DBG("GATT Server Add Service");
2412                 int service_type;
2413                 int num_handles;
2414                 char *svc_uuid;
2415                 int instance_id;
2416                 char *app;
2417                 int *tmp_inst_id = NULL;
2418
2419                 app = (char *)g_dbus_method_invocation_get_sender(context);
2420
2421                 __bt_service_get_parameters(in_param1, &service_type,
2422                                 sizeof(int));
2423                 __bt_service_get_parameters(in_param2, &num_handles,
2424                                 sizeof(int));
2425                 svc_uuid = (char *)g_variant_get_data(in_param3);
2426                 __bt_service_get_parameters(in_param4, &instance_id,
2427                                 sizeof(int));
2428
2429                 result = _bt_gatt_server_add_service(app, service_type, num_handles, svc_uuid, instance_id);
2430                 if (result != BLUETOOTH_ERROR_NONE) {
2431                         BT_ERR("GATT Server Add Service failed!!");
2432                 } else {
2433                         tmp_inst_id = g_malloc0(sizeof(int));
2434                         *tmp_inst_id = instance_id;
2435                         _bt_save_invocation_context(context, result, app,
2436                                         function_name, (gpointer)tmp_inst_id);
2437                 }
2438                 break;
2439         }
2440         case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
2441                 BT_DBG("GATT Server Add Characteristic");
2442                 char *char_uuid;
2443                 char *app;
2444                 int *tmp_inst_id = NULL;
2445                 bluetooth_gatt_server_attribute_params_t param;
2446                 memset(&param, 0, sizeof(bluetooth_gatt_server_attribute_params_t));
2447
2448                 app = (char*)g_dbus_method_invocation_get_sender(context);
2449
2450                 __bt_service_get_parameters(in_param1, &param,
2451                                 sizeof(bluetooth_gatt_server_attribute_params_t));
2452                 char_uuid = (char *)g_variant_get_data(in_param2);
2453
2454                 result = _bt_gatt_server_add_characteristic(app, char_uuid, &param);
2455                 if (result != BLUETOOTH_ERROR_NONE) {
2456                         BT_ERR("GATT Server Add Service failed!!");
2457                 } else {
2458                         tmp_inst_id = g_malloc0(sizeof(int));
2459                         *tmp_inst_id = param.instance_id;
2460
2461                         _bt_save_invocation_context(context, result, app,
2462                                         function_name, (gpointer)tmp_inst_id);
2463                 }
2464                 break;
2465         }
2466         case BT_GATT_SERVER_ADD_DESCRIPTOR: {
2467                 BT_DBG("GATT Server Add Descriptor");
2468                 char *desc_uuid;
2469                 int instance_id;
2470                 int service_handle;
2471                 bt_gatt_permission_t perm;
2472                 char *app;
2473                 int *tmp_inst_id = NULL;
2474                 memset(&perm, 0, sizeof(bt_gatt_permission_t));
2475
2476                 app = (char *)g_dbus_method_invocation_get_sender(context);
2477
2478                 __bt_service_get_parameters(in_param1, &service_handle,
2479                                 sizeof(int));
2480                 __bt_service_get_parameters(in_param2, &instance_id,
2481                                 sizeof(int));
2482                 desc_uuid = (char *)g_variant_get_data(in_param4);
2483                 __bt_service_get_parameters(in_param3, &perm,
2484                                 sizeof(bt_gatt_permission_t));
2485
2486                 result = _bt_gatt_server_add_descriptor(app, desc_uuid, &perm, service_handle, instance_id);
2487                 if (result != BLUETOOTH_ERROR_NONE) {
2488                         BT_ERR("GATT Server Add Service failed!!");
2489                 } else {
2490                         tmp_inst_id = g_malloc0(sizeof(int));
2491                         *tmp_inst_id = instance_id;
2492                         _bt_save_invocation_context(context, result, app,
2493                                         function_name, (gpointer)tmp_inst_id);
2494                 }
2495                 break;
2496         }
2497         case BT_GATT_SERVER_START_SERVICE: {
2498                 int service_handle;
2499                 int instance_id;
2500
2501                 char *app;
2502                 int *tmp_inst_id = NULL;
2503                 app = (char*)g_dbus_method_invocation_get_sender(context);
2504
2505                 __bt_service_get_parameters(in_param1, &service_handle,
2506                                 sizeof(int));
2507                 __bt_service_get_parameters(in_param2, &instance_id,
2508                                 sizeof(int));
2509
2510                 result = _bt_gatt_server_start_service(app, service_handle, instance_id);
2511
2512                 if (BLUETOOTH_ERROR_NONE == result) {
2513                         tmp_inst_id = g_malloc0(sizeof(int));
2514                         *tmp_inst_id = instance_id;
2515                         _bt_save_invocation_context(context, result, app,
2516                                         function_name, (gpointer)tmp_inst_id);
2517                 }
2518                 break;
2519         }
2520         case BT_GATT_SERVER_STOP_SERVICE: {
2521                 int service_handle;
2522                 int instance_id;
2523                 char *app;
2524                 int *tmp_inst_id = NULL;
2525                 app = (char*)g_dbus_method_invocation_get_sender(context);
2526
2527                 __bt_service_get_parameters(in_param1, &service_handle,
2528                                 sizeof(int));
2529                 __bt_service_get_parameters(in_param2, &instance_id,
2530                                 sizeof(int));
2531
2532                 result = _bt_gatt_server_stop_service(app, service_handle, instance_id);
2533
2534                 if (BLUETOOTH_ERROR_NONE == result) {
2535                         tmp_inst_id = g_malloc0(sizeof(int));
2536                         *tmp_inst_id = instance_id;
2537                         _bt_save_invocation_context(context, result, app,
2538                                         function_name, (gpointer)tmp_inst_id);
2539                 }
2540                 break;
2541         }
2542         case BT_GATT_SERVER_DELETE_SERVICE: {
2543                 int service_handle;
2544                 int instance_id;
2545                 int *tmp_inst_id = NULL;
2546                 char *app;
2547                 app = (char*)g_dbus_method_invocation_get_sender(context);
2548
2549                 __bt_service_get_parameters(in_param1, &service_handle,
2550                                 sizeof(int));
2551                 __bt_service_get_parameters(in_param2, &instance_id,
2552                                 sizeof(int));
2553
2554                 result = _bt_gatt_server_delete_service(app, service_handle, instance_id);
2555
2556                 if (BLUETOOTH_ERROR_NONE == result) {
2557                         tmp_inst_id = g_malloc0(sizeof(int));
2558                         *tmp_inst_id = instance_id;
2559                         _bt_save_invocation_context(context, result, app,
2560                                         function_name, (gpointer)tmp_inst_id);
2561                 }
2562                 break;
2563         }
2564         case BT_GATT_SERVER_SEND_RESPONSE: {
2565                 bluetooth_gatt_server_response_params_t param;
2566                 bluetooth_gatt_att_data_t data;
2567                 char *app;
2568
2569                 memset(&param, 0x00, sizeof(bluetooth_gatt_server_response_params_t));
2570                 memset(&data, 0x00, sizeof(bluetooth_gatt_att_data_t));
2571
2572                 app = (char*)g_dbus_method_invocation_get_sender(context);
2573
2574                 __bt_service_get_parameters(in_param1, &data,
2575                                 sizeof(bluetooth_gatt_att_data_t));
2576                 __bt_service_get_parameters(in_param2, &param,
2577                                 sizeof(bluetooth_gatt_server_response_params_t));
2578
2579                 result = _bt_gatt_server_send_response(app, &data, &param);
2580
2581                 break;
2582         }
2583         case BT_GATT_SERVER_ACQURE_WRITE_RESPONSE: {
2584
2585                 bluetooth_gatt_server_acquire_response_params_t param;
2586                 char *app;
2587                 GDBusMessage *msg;
2588                 msg = g_dbus_method_invocation_get_message(context);
2589                 GUnixFDList *fd_list;
2590                 int fd  = -1;;
2591                 int *fd_list_array;
2592                 int len;
2593
2594                 fd_list = g_dbus_message_get_unix_fd_list(msg);
2595
2596                 memset(&param, 0x00, sizeof(bluetooth_gatt_server_acquire_response_params_t));
2597
2598                 app = (char*)g_dbus_method_invocation_get_sender(context);
2599
2600                 __bt_service_get_parameters(in_param1, &param,
2601                                 sizeof(bluetooth_gatt_server_acquire_response_params_t));
2602
2603                 fd_list_array = (int *)g_unix_fd_list_peek_fds(fd_list, &len);
2604                 BT_DBG("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
2605                 fd = fd_list_array[0];
2606
2607                 param.fd = fd;
2608                 result = _bt_gatt_server_acquire_send_response(app, &param, fd_list);
2609
2610                 break;
2611         }
2612         case BT_GATT_SERVER_ACQUIRE_NOTIFY_RESPONSE: {
2613
2614                 bluetooth_gatt_server_acquire_response_params_t param;
2615                 char *app;
2616                 GDBusMessage *msg;
2617                 msg = g_dbus_method_invocation_get_message(context);
2618                 GUnixFDList *fd_list;
2619                 int fd  = -1;
2620                 int *fd_list_array;
2621                 int len;
2622
2623                 fd_list = g_dbus_message_get_unix_fd_list(msg);
2624
2625                 memset(&param, 0x00, sizeof(bluetooth_gatt_server_acquire_response_params_t));
2626
2627                 app = (char*)g_dbus_method_invocation_get_sender(context);
2628
2629                 __bt_service_get_parameters(in_param1, &param,
2630                                 sizeof(bluetooth_gatt_server_acquire_response_params_t));
2631
2632                 fd_list_array = (int *)g_unix_fd_list_peek_fds(fd_list, &len);
2633                 BT_DBG("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
2634                 fd = fd_list_array[0];
2635
2636                 param.fd = fd;
2637                 result = _bt_gatt_server_acquire_send_response(app, &param, fd_list);
2638
2639                 break;
2640         }
2641         case BT_GATT_SERVER_SEND_INDICATION: {
2642                 bluetooth_gatt_server_indication_params_t param;
2643                 bluetooth_gatt_att_data_t data;
2644                 bluetooth_device_address_t address;
2645
2646                 char *app;
2647                 app = (char*)g_dbus_method_invocation_get_sender(context);
2648
2649                 memset(&param, 0x00, sizeof(bluetooth_gatt_server_indication_params_t));
2650                 memset(&data, 0x00, sizeof(bluetooth_gatt_att_data_t));
2651                 memset(&address, 0x00, sizeof(bluetooth_device_address_t));
2652
2653                 __bt_service_get_parameters(in_param1, &data,
2654                                 sizeof(bluetooth_gatt_att_data_t));
2655                 __bt_service_get_parameters(in_param2, &param,
2656                                 sizeof(bluetooth_gatt_server_indication_params_t));
2657                 __bt_service_get_parameters(in_param3, &address,
2658                                 sizeof(bluetooth_device_address_t));
2659
2660                 result = _bt_gatt_server_send_indication(app, &address, &data, &param);
2661
2662                 break;
2663         }
2664         case BT_GATT_SERVER_UPDATE_VALUE: {
2665                 bluetooth_gatt_server_update_value_t param;
2666                 int instance_id;
2667                 char *app;
2668
2669                 memset(&param, 0x00, sizeof(bluetooth_gatt_server_update_value_t));
2670
2671                 app = (char*)g_dbus_method_invocation_get_sender(context);
2672
2673                 __bt_service_get_parameters(in_param1, &instance_id,
2674                                 sizeof(int));
2675                 __bt_service_get_parameters(in_param2, &param,
2676                                 sizeof(bluetooth_gatt_server_update_value_t));
2677
2678                 result = _bt_gatt_server_update_attribute_value(app, instance_id, &param);
2679                 break;
2680         }
2681         case BT_GET_ATT_MTU: {
2682                 bluetooth_device_address_t address = { {0} };
2683                 unsigned int mtu = 0;
2684
2685                 __bt_service_get_parameters(in_param1,
2686                                 &address, sizeof(bluetooth_device_address_t));
2687                 result = _bt_get_att_mtu(&address, &mtu);
2688                 BT_DBG("MTU: %d", mtu);
2689
2690                 if (result == BLUETOOTH_ERROR_NONE) {
2691                         g_array_append_vals(*out_param1, &mtu,
2692                                         sizeof(unsigned int));
2693                 }
2694                 break;
2695         }
2696         case BT_REQ_ATT_MTU: {
2697                 bluetooth_device_address_t address = { {0} };
2698                 unsigned int mtu;
2699                 char *addr;
2700
2701                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2702
2703                 __bt_service_get_parameters(in_param1,
2704                                 &address, sizeof(bluetooth_device_address_t));
2705                 __bt_service_get_parameters(in_param2,
2706                                 &mtu, sizeof(unsigned int));
2707                 BT_DBG("BT_REQ_ATT_MTU: %d", mtu);
2708                 result = _bt_request_att_mtu(&address, mtu);
2709                 if (BLUETOOTH_ERROR_NONE == result) {
2710                         addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
2711
2712                         _bt_convert_addr_type_to_string(addr, address.addr);
2713
2714                         /* Save the informations to invocation */
2715                         _bt_save_invocation_context(context, result, sender,
2716                                         function_name, (gpointer)addr);
2717                 } else {
2718                         g_array_append_vals(*out_param1, &address,
2719                                         sizeof(bluetooth_device_address_t));
2720                 }
2721                 break;
2722         }
2723         case BT_GET_DEVICE_IDA: {
2724                 bluetooth_device_address_t address = { {0} };
2725                 bluetooth_device_address_t id_addr = { {0} };
2726
2727                 __bt_service_get_parameters(in_param1,
2728                                 &address, sizeof(bluetooth_device_address_t));
2729
2730                 result = _bt_device_get_ida(&address, &id_addr);
2731
2732                 if (result == BLUETOOTH_ERROR_NONE) {
2733                         g_array_append_vals(*out_param1, &id_addr,
2734                                         sizeof(bluetooth_device_address_t));
2735                 }
2736                 break;
2737         }
2738         case BT_SET_LE_STATIC_RANDOM_ADDRESS: {
2739                 gboolean is_enable;
2740
2741                 __bt_service_get_parameters(in_param1, &is_enable,
2742                                 sizeof(gboolean));
2743
2744                 result = _bt_set_le_static_random_address(is_enable);
2745
2746                 break;
2747         }
2748         /* Local Async */
2749         case BT_GATT_CLIENT_REGISTER: {
2750                 bluetooth_device_address_t address = { {0} };
2751                 char *addr;
2752
2753                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2754
2755                 __bt_service_get_parameters(in_param1,
2756                                 &address, sizeof(bluetooth_device_address_t));
2757
2758                 result = _bt_register_gatt_client_instance(sender, &address);
2759                 if (BLUETOOTH_ERROR_NONE == result) {
2760                         addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
2761
2762                         _bt_convert_addr_type_to_string(addr, address.addr);
2763
2764                         /* Save the informations to invocation */
2765                         _bt_save_invocation_context(context, result, sender,
2766                                         function_name, (gpointer)addr);
2767                 }
2768                 break;
2769         }
2770         /* Sync */
2771         case BT_GATT_CLIENT_UNREGISTER: {
2772                 int client_id;
2773
2774                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2775                 __bt_service_get_parameters(in_param1,
2776                                 &client_id, sizeof(int));
2777
2778                 result = _bt_unregister_gatt_client_instance(sender, client_id);
2779                 break;
2780         }
2781
2782         /* Async: Remote Interaction  */
2783         case BT_GATT_ACQUIRE_WRITE: {
2784
2785                 int fd = -1;
2786                 int mtu = -1;
2787                 bluetooth_gatt_client_char_prop_info_t param;
2788
2789                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2790
2791                 __bt_service_get_parameters(in_param1, &param,
2792                                         sizeof(bluetooth_gatt_client_char_prop_info_t));
2793
2794
2795                 result   = _bt_gatt_acquire_write(&param , &fd, &mtu);
2796
2797                 g_array_append_vals(*out_param1, &mtu, sizeof(int));
2798
2799                 if (BLUETOOTH_ERROR_NONE == result)
2800                         BT_DBG("GATT Client: Save Invocation data for characteristic props app[%s] fd[ %d]", sender, fd);
2801                 else {
2802                         BT_ERR("GATT Client: gatt acquire write failed");
2803                         break;
2804                 }
2805
2806                 GUnixFDList *fd_list = NULL;
2807                 GError *error = NULL;
2808
2809                 /* Add socket fd to unix_fd_list */
2810                 fd_list = g_unix_fd_list_new();
2811                 g_unix_fd_list_append(fd_list, fd, &error);
2812                 g_assert_no_error(error);
2813
2814                 _bt_service_method_return_with_unix_fd_list(
2815                                 context, *out_param1, result, fd_list);
2816
2817                 close(fd);
2818                 g_object_unref(fd_list);
2819
2820                 break;
2821         }
2822         /* Async: Remote Interaction  */
2823         case BT_CONNECT_LE: {
2824                 bluetooth_device_address_t address = { {0} };
2825                 gboolean auto_connect;
2826                 char *addr;
2827                 int client_id;
2828
2829                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2830
2831                 __bt_service_get_parameters(in_param1,
2832                                 &address, sizeof(bluetooth_device_address_t));
2833                 __bt_service_get_parameters(in_param2,
2834                                 &auto_connect, sizeof(gboolean));
2835                 __bt_service_get_parameters(in_param3,
2836                                 &client_id, sizeof(int));
2837
2838                 BT_DBG("GATT Client: client_id[%d]", client_id);
2839                 result = _bt_connect_le_device(&address, auto_connect, client_id);
2840                 if (BLUETOOTH_ERROR_NONE == result) {
2841                         addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
2842                         _bt_convert_addr_type_to_string(addr, address.addr);
2843
2844                         /* Save the informations to invocation */
2845                         _bt_save_invocation_context(context, result, sender,
2846                                         function_name, (gpointer)addr);
2847
2848                         if (auto_connect)
2849                                 _bt_handle_invocation_context(function_name, (void *)addr);
2850                 } else {
2851                         BT_ERR("GATT Client: gatt connect failed. client_id[%d]", client_id);
2852                         g_array_append_vals(*out_param1, &address,
2853                                         sizeof(bluetooth_device_address_t));
2854                 }
2855                 break;
2856         }
2857         /* Async: Remote Interaction  */
2858         case BT_DISCONNECT_LE: {
2859                 bluetooth_device_address_t address = { {0} };
2860                 char *addr;
2861                 int client_id;
2862
2863                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2864
2865                 __bt_service_get_parameters(in_param1,
2866                                 &address, sizeof(bluetooth_device_address_t));
2867                 __bt_service_get_parameters(in_param2,
2868                                 &client_id, sizeof(int));
2869
2870                 BT_INFO("GATT Disconnect using client Interface [%d]", client_id);
2871                 result = _bt_disconnect_le_device(&address, client_id);
2872                 if (BLUETOOTH_ERROR_NONE == result) {
2873                         addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
2874
2875                         _bt_convert_addr_type_to_string(addr, address.addr);
2876
2877                         /* Save the informations to invocation */
2878                         _bt_save_invocation_context(context, result, sender,
2879                                         function_name, (gpointer)addr);
2880                 } else {
2881                         g_array_append_vals(*out_param1, &address,
2882                                         sizeof(bluetooth_device_address_t));
2883                 }
2884                 break;
2885         }
2886         /* Sync */
2887         case BT_GET_GATT_DATA_BATCHING_AVAILABLE_PACKETS: {
2888                 guint available_packets = 0;
2889
2890                 result = _bt_gatt_get_data_batching_available_packets(&available_packets);
2891                 BT_DBG("LE batching available packets %u", available_packets);
2892                 if (result == BLUETOOTH_ERROR_NONE) {
2893                         g_array_append_vals(*out_param1, &available_packets,
2894                                         sizeof(guint));
2895                 }
2896
2897                 break;
2898         }
2899         /* Sync */
2900         case BT_ENABLE_GATT_DATA_BATCHING: {
2901                 bluetooth_device_address_t address = { {0} };
2902                 int packet_threshold;
2903                 int timeout;
2904
2905                 __bt_service_get_parameters(in_param1,
2906                                 &address, sizeof(bluetooth_device_address_t));
2907                 __bt_service_get_parameters(in_param2,
2908                                 &packet_threshold, sizeof(int));
2909                 __bt_service_get_parameters(in_param3,
2910                                 &timeout, sizeof(int));
2911                 result = _bt_gatt_enable_data_batching(&address, packet_threshold, timeout);
2912
2913                 break;
2914         }
2915         /* Sync */
2916         case BT_DISABLE_GATT_DATA_BATCHING: {
2917                 bluetooth_device_address_t address = { {0} };
2918
2919                 __bt_service_get_parameters(in_param1,
2920                                 &address, sizeof(bluetooth_device_address_t));
2921                 result = _bt_gatt_disable_data_batching(&address);
2922
2923                 break;
2924         }
2925
2926         case BT_GATT_GET_PRIMARY_SERVICES: {
2927                 char *addr;
2928
2929                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2930                 bluetooth_device_address_t address = { {0} };
2931
2932                 __bt_service_get_parameters(in_param1, &address,
2933                                 sizeof(bluetooth_device_address_t));
2934
2935                 addr = g_malloc0(sizeof(char) * BT_ADDRESS_STRING_SIZE);
2936                 _bt_convert_addr_type_to_string(addr, address.addr);
2937
2938                 result = _bt_gatt_get_primary_services(addr);
2939                 if (BLUETOOTH_ERROR_NONE == result) {
2940                         _bt_save_invocation_context(context, result, sender,
2941                                         function_name, (gpointer)addr);
2942                 } else
2943                         g_free(addr);
2944                 break;
2945         }
2946         /* Local Async */
2947         case BT_GATT_GET_SERVICE_PROPERTIES: {
2948                 bluetooth_gatt_client_svc_prop_info_t param;
2949
2950                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2951                 memset(&param, 0x00, sizeof(bluetooth_gatt_client_svc_prop_info_t));
2952
2953                 __bt_service_get_parameters(in_param1, &param,
2954                                 sizeof(bluetooth_gatt_client_svc_prop_info_t));
2955
2956                 result = _bt_gatt_get_all_characteristic(&param);
2957                 if (BLUETOOTH_ERROR_NONE == result) {
2958                         /* Save the informations to invocation */
2959                         _bt_save_invocation_context(context, result, sender,
2960                                         function_name,
2961                                         (gpointer)g_memdup(&param, sizeof(bluetooth_gatt_client_svc_prop_info_t)));
2962                 }
2963                 break;
2964         }
2965         case BT_GATT_GET_CHARACTERISTIC_PROPERTIES: {
2966                 bluetooth_gatt_client_char_prop_info_t param;
2967
2968                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2969                 memset(&param, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
2970
2971                 __bt_service_get_parameters(in_param1, &param,
2972                                 sizeof(bluetooth_gatt_client_char_prop_info_t));
2973
2974                 result = _bt_gatt_get_all_characteristic_properties(&param);
2975                 if (BLUETOOTH_ERROR_NONE == result) {
2976                         /* Save the informations to invocation */
2977                         _bt_save_invocation_context(context, result, sender,
2978                                         function_name,
2979                                         (gpointer)g_memdup(&param, sizeof(bluetooth_gatt_client_char_prop_info_t)));
2980                 }
2981                 break;
2982         }
2983         /* Async: Remote Interaction  */
2984         case BT_GATT_READ_CHARACTERISTIC: {
2985                 bluetooth_gatt_client_char_prop_info_t param;
2986
2987                 sender = (char*)g_dbus_method_invocation_get_sender(context);
2988                 memset(&param, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
2989
2990                 __bt_service_get_parameters(in_param1, &param,
2991                                 sizeof(bluetooth_gatt_client_char_prop_info_t));
2992
2993                 result = _bt_gatt_read_characteristic_value(&param);
2994                 if (BLUETOOTH_ERROR_NONE == result) {
2995                         /* Save the informations to invocation */
2996                         _bt_save_invocation_context(context, result, sender,
2997                                         function_name,
2998                                         (gpointer)g_memdup(&param, sizeof(bluetooth_gatt_client_char_prop_info_t)));
2999                 }
3000                 break;
3001         }
3002         /* Async: Remote Interaction  */
3003         case BT_GATT_READ_DESCRIPTOR_VALUE: {
3004                 bluetooth_gatt_client_desc_prop_info_t param;
3005
3006                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3007                 memset(&param, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3008
3009                 __bt_service_get_parameters(in_param1, &param,
3010                                 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3011
3012                 result = _bt_gatt_read_descriptor_value(&param);
3013                 if (BLUETOOTH_ERROR_NONE == result) {
3014                         /* Save the informations to invocation */
3015                         _bt_save_invocation_context(context, result, sender,
3016                                         function_name,
3017                                         (gpointer)g_memdup(&param, sizeof(bluetooth_gatt_client_desc_prop_info_t)));
3018                 }
3019                 break;
3020         }
3021         /* Async: Remote Interaction  */
3022         case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: {
3023                 bluetooth_gatt_client_char_prop_info_t param;
3024                 bluetooth_gatt_att_data_t data;
3025                 bluetooth_gatt_write_type_e write_type;
3026
3027                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3028
3029                 memset(&data, 0x00, sizeof(bluetooth_gatt_att_data_t));
3030                 memset(&param, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3031
3032                 __bt_service_get_parameters(in_param1, &param,
3033                                 sizeof(bluetooth_gatt_client_char_prop_info_t));
3034                 __bt_service_get_parameters(in_param2, &data,
3035                                 sizeof(bluetooth_gatt_att_data_t));
3036                 __bt_service_get_parameters(in_param3, &write_type,
3037                                 sizeof(bluetooth_gatt_write_type_e));
3038
3039                 result = _bt_gatt_write_characteristic_value_by_type(&param , &data, write_type);
3040                 if (BLUETOOTH_ERROR_NONE == result) {
3041                         /* Save the informations to invocation */
3042                         _bt_save_invocation_context(context, result, sender,
3043                                         function_name,
3044                                         (gpointer)g_memdup(&param, sizeof(bluetooth_gatt_client_char_prop_info_t)));
3045                 }
3046                 break;
3047         }
3048         /* Async: Remote Interaction  */
3049         case BT_GATT_WRITE_DESCRIPTOR_VALUE: {
3050                 bluetooth_gatt_client_desc_prop_info_t param;
3051                 bluetooth_gatt_att_data_t data;
3052                 bluetooth_gatt_write_type_e write_type;
3053
3054                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3055                 memset(&data, 0x00, sizeof(bluetooth_gatt_att_data_t));
3056                 memset(&param, 0x00, sizeof(bluetooth_gatt_client_desc_prop_info_t));
3057
3058                 __bt_service_get_parameters(in_param1, &param,
3059                                 sizeof(bluetooth_gatt_client_desc_prop_info_t));
3060                 __bt_service_get_parameters(in_param2, &data,
3061                                 sizeof(bluetooth_gatt_att_data_t));
3062                 __bt_service_get_parameters(in_param3, &write_type,
3063                                 sizeof(bluetooth_gatt_write_type_e));
3064
3065                 result = _bt_gatt_write_descriptor_value_by_type(&param, &data, write_type);
3066                 if (BLUETOOTH_ERROR_NONE == result) {
3067                         /* Save the informations to invocation */
3068                         _bt_save_invocation_context(context, result, sender,
3069                                         function_name,
3070                                         (gpointer)g_memdup(&param, sizeof(bluetooth_gatt_client_desc_prop_info_t)));
3071                 }
3072                 break;
3073         }
3074         /* Local Async */
3075         case BT_GATT_WATCH_CHARACTERISTIC: {
3076                 bluetooth_gatt_client_char_prop_info_t param;
3077                 int client_id;
3078                 gboolean is_notify;
3079                 int fd = -1;
3080                 int mtu = 0;
3081                 gboolean is_indicate;
3082
3083
3084                 memset(&param, 0x00, sizeof(bluetooth_gatt_client_char_prop_info_t));
3085
3086                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3087
3088                 __bt_service_get_parameters(in_param1, &param,
3089                                 sizeof(bluetooth_gatt_client_char_prop_info_t));
3090                 __bt_service_get_parameters(in_param2, &client_id,
3091                                 sizeof(int));
3092                 __bt_service_get_parameters(in_param3, &is_notify,
3093                                 sizeof(gboolean));
3094                 __bt_service_get_parameters(in_param4, &is_indicate,
3095                                 sizeof(gboolean));
3096
3097 #ifdef TIZEN_FEATURE_BT_GATT_CLIENT_FD_DISABLE
3098                 goto normal;
3099 #endif
3100
3101                 if (is_indicate == false) {
3102                         result   = _bt_gatt_acquire_notify(&param , &fd, &mtu);
3103                         if (BLUETOOTH_ERROR_NONE == result && fd > -1) {
3104                                 BT_DBG("GATT Client: Save Invocation data for characteristic props app[%s] fd[ %d]", sender, fd);
3105
3106                                 GUnixFDList *fd_list = NULL;
3107                                 GError *error = NULL;
3108
3109                                 g_array_append_vals(*out_param1, &mtu, sizeof(int));
3110                                 /* Add socket fd to unix_fd_list */
3111                                 fd_list = g_unix_fd_list_new();
3112                                 g_unix_fd_list_append(fd_list, fd, &error);
3113                                 g_assert_no_error(error);
3114                                 close(fd);
3115                                 _bt_service_method_return_with_unix_fd_list(
3116                                 context, *out_param1, result, fd_list);
3117
3118                                 g_object_unref(fd_list);
3119                         } else
3120                                 goto normal;
3121                 } else {
3122
3123 normal:
3124                         result = _bt_gatt_watch_characteristic(&param , client_id, is_notify);
3125                         if (BLUETOOTH_ERROR_NONE == result) {
3126                                 /* Save the informations to invocation */
3127                                 _bt_save_invocation_context(context, result, sender, function_name,
3128                                          (gpointer)g_memdup(&param, sizeof(bluetooth_gatt_client_char_prop_info_t)));
3129                         }
3130                 }
3131                 break;
3132         }
3133         case BT_GATT_WATCH_SERVICE_CHANGED_INDICATION: {
3134                 gboolean is_enabled;
3135                 bluetooth_device_address_t address;
3136
3137                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3138
3139                 __bt_service_get_parameters(in_param1,
3140                                 &address, sizeof(bluetooth_device_address_t));
3141                 __bt_service_get_parameters(in_param2, &is_enabled,
3142                                 sizeof(gboolean));
3143                 result = _bt_gatt_watch_service_changed_indication(sender, &address, is_enabled);
3144                 BT_INFO("Service changed indication from app [%s]", sender);
3145
3146                 break;
3147         }
3148         case BT_LE_CONN_UPDATE: {
3149                 char *sender = NULL;
3150                 bluetooth_device_address_t local_address = { {0} };
3151                 bluetooth_le_connection_param_t parameters = {0};
3152
3153                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3154
3155                 __bt_service_get_parameters(in_param1, &local_address,
3156                                 sizeof(bluetooth_device_address_t));
3157                 __bt_service_get_parameters(in_param2, &parameters,
3158                                 sizeof(bluetooth_le_connection_param_t));
3159
3160                 result = _bt_le_connection_update(sender,
3161                                         local_address.addr,
3162                                         parameters.interval_min,
3163                                         parameters.interval_max,
3164                                         parameters.latency,
3165                                         parameters.timeout);
3166                 break;
3167         }
3168         case BT_START_LE_DISCOVERY: {
3169                 uid_t uid = 0;
3170                 pid_t pid = 0;
3171
3172                 __bt_service_get_parameters(in_param1, &uid, sizeof(uid_t));
3173
3174                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3175
3176                 if (__bt_service_get_sender_pid(sender, &pid) != BLUETOOTH_ERROR_NONE)
3177                         BT_ERR("Fail to get the sender pid");
3178
3179                 result = _bt_start_le_scan(sender, uid, pid);
3180                 if (result == BLUETOOTH_ERROR_NONE) {
3181                         _bt_save_invocation_context(context, result, sender,
3182                                         function_name, NULL);
3183
3184                         BT_DBG("Remeber pid / uid for the scan operation");
3185                         _bt_bm_add_scan_app(SCAN_LE, uid, pid);
3186                 }
3187                 break;
3188         }
3189         case BT_STOP_LE_DISCOVERY: {
3190                 uid_t uid = 0;
3191                 pid_t pid = 0;
3192
3193                 __bt_service_get_parameters(in_param1, &uid, sizeof(uid_t));
3194
3195                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3196
3197                 if (__bt_service_get_sender_pid(sender, &pid) != BLUETOOTH_ERROR_NONE)
3198                         BT_ERR("Fail to get the sender pid");
3199
3200                 result = _bt_stop_le_scan(sender);
3201                 if (result == BLUETOOTH_ERROR_NONE) {
3202                         _bt_save_invocation_context(context, result, sender,
3203                                         function_name, NULL);
3204                 }
3205
3206                 BT_DBG("Remove pid / uid for the scan operation");
3207                 _bt_bm_remove_scan_app(SCAN_LE, uid, pid);
3208
3209                 break;
3210         }
3211         case BT_IS_LE_DISCOVERYING: {
3212                 gboolean le_discovering = FALSE;
3213
3214                 le_discovering = _bt_is_le_scanning();
3215                 g_array_append_vals(*out_param1,
3216                                 &le_discovering, sizeof(gboolean));
3217                 break;
3218         }
3219         case BT_SET_SCAN_PARAMETERS: {
3220                 bluetooth_le_scan_params_t scan_params;
3221
3222                 __bt_service_get_parameters(in_param1, &scan_params,
3223                                 sizeof(bluetooth_le_scan_params_t));
3224
3225                 BT_DBG("bluetooth_le_scan_params_t [%f %f %d]",
3226                                 scan_params.interval, scan_params.window,
3227                                 scan_params.type);
3228
3229                 result = _bt_prepare_scan_parameters(&scan_params, 0);
3230                 break;
3231         }
3232         case BT_SET_SCAN_TYPE: {
3233                 int scan_type = 0;
3234                 __bt_service_get_parameters(in_param1, &scan_type, sizeof(int));
3235
3236                 BT_DBG("bluetooth_le_scan_type [%d]", scan_type);
3237                 result = _bt_prepare_scan_parameters(NULL, scan_type);
3238                 break;
3239         }
3240 #ifdef TIZEN_FEATURE_BT_PAN_NAP
3241         case BT_NETWORK_ACTIVATE:
3242                 BT_DBG("BT_NETWORK_ACTIVATE Request recieved Request Handler ");
3243                 result = _bt_network_activate();
3244                 break;
3245         case BT_NETWORK_DEACTIVATE:
3246                 result = _bt_network_deactivate();
3247                 break;
3248         case BT_NETWORK_CONNECT: {
3249                 bluetooth_device_address_t address = { {0} };
3250                 int role;
3251
3252                 __bt_service_get_parameters(in_param1,
3253                                 &address, sizeof(bluetooth_device_address_t));
3254                 __bt_service_get_parameters(in_param2,
3255                                 &role, sizeof(int));
3256
3257                 result = _bt_network_connect(request_id, role, &address);
3258                 if (result != BLUETOOTH_ERROR_NONE) {
3259                         g_array_append_vals(*out_param1, &address,
3260                                         sizeof(bluetooth_device_address_t));
3261                 }
3262                 break;
3263         }
3264         case BT_NETWORK_DISCONNECT: {
3265                 bluetooth_device_address_t address = { {0} };
3266
3267                 __bt_service_get_parameters(in_param1,
3268                                 &address, sizeof(bluetooth_device_address_t));
3269
3270                 result = _bt_network_disconnect(request_id, &address);
3271                 if (result != BLUETOOTH_ERROR_NONE) {
3272                         g_array_append_vals(*out_param1, &address,
3273                                         sizeof(bluetooth_device_address_t));
3274                 }
3275                 break;
3276         }
3277         case BT_NETWORK_SERVER_DISCONNECT: {
3278                 bluetooth_device_address_t address = { {0} };
3279
3280                 __bt_service_get_parameters(in_param1,
3281                                 &address, sizeof(bluetooth_device_address_t));
3282
3283                 result = _bt_network_server_disconnect(request_id, &address);
3284                 if (result != BLUETOOTH_ERROR_NONE) {
3285                         g_array_append_vals(*out_param1, &address,
3286                                         sizeof(bluetooth_device_address_t));
3287                 }
3288                 break;
3289         }
3290
3291 #endif
3292 #ifdef TIZEN_FEATURE_BT_IPSP
3293         case BT_LE_IPSP_INIT:
3294                 result = _bt_initialize_ipsp();
3295                 break;
3296         case BT_LE_IPSP_DEINIT:
3297                 result = _bt_deinitialize_ipsp();
3298                 break;
3299         case BT_LE_IPSP_CONNECT: {
3300                 bluetooth_device_address_t address = { {0} };
3301                 __bt_service_get_parameters(in_param1, &address,
3302                                 sizeof(bluetooth_device_address_t));
3303                 result = _bt_connect_le_ipsp_device(&address);
3304                 break;
3305         }
3306         case BT_LE_IPSP_DISCONNECT: {
3307                 bluetooth_device_address_t address = { {0} };
3308                 __bt_service_get_parameters(in_param1, &address,
3309                                 sizeof(bluetooth_device_address_t));
3310                 result = _bt_disconnect_le_ipsp_device(&address);
3311                 break;
3312         }
3313 #endif
3314         case BT_IS_ADVERTISING: {
3315                 gboolean advertising = FALSE;
3316                 advertising = _bt_is_advertising();
3317                 g_array_append_vals(*out_param1, &advertising,
3318                                 sizeof(gboolean));
3319                 break;
3320         }
3321         case BT_IS_LE_2M_PHY_SUPPORTED: {
3322                 gboolean is_2m_phy_supported = FALSE;
3323
3324                 is_2m_phy_supported = _bt_is_le_2m_phy_supported();
3325                 g_array_append_vals(*out_param1, &is_2m_phy_supported, sizeof(gboolean));
3326                 break;
3327         }
3328         case BT_IS_LE_CODED_PHY_SUPPORTED: {
3329                 gboolean is_coded_phy_supported = FALSE;
3330
3331                 is_coded_phy_supported = _bt_is_le_coded_phy_supported();
3332                 g_array_append_vals(*out_param1, &is_coded_phy_supported, sizeof(gboolean));
3333                 break;
3334         }
3335         case BT_DISCONNECT_DEVICE: {
3336                 bluetooth_device_address_t address = { {0} };
3337
3338                 __bt_service_get_parameters(in_param1,
3339                                 &address, sizeof(bluetooth_device_address_t));
3340
3341                 result = _bt_disconnect_device(&address);
3342                 break;
3343         }
3344         case BT_REGISTER_SCAN_FILTER: {
3345                 bluetooth_le_scan_filter_t scan_filter;
3346
3347                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3348                 __bt_service_get_parameters(in_param1, &scan_filter,
3349                                 sizeof(bluetooth_le_scan_filter_t));
3350
3351                 BT_DBG("bluetooth_le_scan_filter_t [features : 0x%.2x]",
3352                                 scan_filter.added_features);
3353
3354                 result = _bt_register_scan_filter(sender, &scan_filter);
3355
3356                 break;
3357         }
3358
3359         case BT_TDS_PROVIDER_REGISTER: {
3360                 char *sender = NULL;
3361
3362                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3363                 result = _bt_tds_provider_register(sender);
3364
3365                 break;
3366         }
3367
3368         case BT_TDS_PROVIDER_UNREGISTER: {
3369                 char *sender = NULL;
3370
3371                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3372                 result = _bt_tds_provider_unregister(sender);
3373
3374                 break;
3375         }
3376
3377         case BT_TDS_PROVIDER_SET_MANUF_DATA: {
3378                 char *sender = NULL;
3379                 unsigned int length = 0;
3380                 bluetooth_advertising_data_t manuf_data;
3381
3382                 __bt_service_get_parameters(in_param1,
3383                                 &length, sizeof(unsigned int));
3384                 __bt_service_get_parameters(in_param2,
3385                                 &manuf_data, sizeof(bluetooth_advertising_data_t));
3386                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3387
3388                 result = _bt_tds_provider_set_manuf_data(sender, manuf_data.data, length);
3389                 break;
3390         }
3391
3392         case BT_TDS_PROVIDER_CREATE: {
3393                 char *sender = NULL;
3394                 unsigned int tds_handle = 0;
3395                 int transport;
3396
3397                 __bt_service_get_parameters(in_param1,
3398                                 &tds_handle, sizeof(unsigned int));
3399                 __bt_service_get_parameters(in_param2,
3400                                 &transport, sizeof(int));
3401                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3402                 result = _bt_tds_provider_transport_create(sender, transport, tds_handle);
3403
3404                 break;
3405         }
3406
3407         case BT_TDS_PROVIDER_DESTROY: {
3408                 char *sender = NULL;
3409                 unsigned int tds_handle = 0;
3410
3411                 __bt_service_get_parameters(in_param1,
3412                                 &tds_handle, sizeof(unsigned int));
3413                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3414                 result = _bt_tds_provider_transport_remove(sender, tds_handle);
3415
3416                 break;
3417         }
3418
3419         case BT_TDS_PROVIDER_SET_TRANSPORT_DATA: {
3420                 char *sender = NULL;
3421                 unsigned int tds_handle = 0;
3422                 int transport_state = 0;
3423                 bluetooth_tds_data_t tds_data;
3424
3425                 __bt_service_get_parameters(in_param1,
3426                                 &tds_handle, sizeof(unsigned int));
3427                 __bt_service_get_parameters(in_param2,
3428                                 &transport_state, sizeof(int));
3429                 __bt_service_get_parameters(in_param3,
3430                                 &tds_data, sizeof(bluetooth_tds_data_t));
3431                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3432
3433                 result = _bt_tds_provider_set_transport_data(sender, tds_handle,
3434                                 transport_state, tds_data.data, tds_data.length);
3435                 break;
3436         }
3437
3438         case BT_TDS_SEND_ACTIVATION_RESPONSE: {
3439                 bluetooth_device_address_t address = { {0} };
3440                 bluetooth_tds_data_t tds_data;
3441                 char *sender = NULL;
3442                 unsigned int tds_handle = 0;
3443                 int response;
3444
3445                 __bt_service_get_parameters(in_param1,
3446                                 &tds_handle, sizeof(unsigned int));
3447                 __bt_service_get_parameters(in_param2,
3448                                 &response, sizeof(int));
3449                 __bt_service_get_parameters(in_param3, &address,
3450                         sizeof(bluetooth_device_address_t));
3451                 __bt_service_get_parameters(in_param4,
3452                                 &tds_data, sizeof(bluetooth_tds_data_t));
3453                 sender = (char *)g_dbus_method_invocation_get_sender(context);
3454
3455                 result = _bt_tds_provider_send_activation_response(sender, tds_handle,
3456                                 &address, response, tds_data.data, tds_data.length);
3457                 break;
3458         }
3459
3460         case BT_MESH_INIT:
3461                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3462                 BT_INFO("Mesh: Init by [%s]", sender);
3463                 result = _bt_mesh_init(sender);
3464                 /* Save invocation */
3465                 if (result == BLUETOOTH_ERROR_NONE) {
3466                         BT_INFO("Mesh: Save Invoation");
3467                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3468                         _bt_save_invocation_context(context,
3469                                         result, sender,
3470                                         function_name, NULL);
3471                 }
3472                 break;
3473
3474         case BT_MESH_DEINIT:
3475                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3476                 BT_INFO("Mesh: De-Init by [%s]", sender);
3477                 result = _bt_mesh_deinit(sender);
3478                 break;
3479
3480         case BT_MESH_NETWORK_CREATE: {
3481                 bluetooth_mesh_node_t node;
3482                 GSList *model_list = NULL;
3483                 int total_models = 0;
3484                 GArray *param3;
3485                 int i = 0;
3486                 const char *network_name = NULL;
3487                 bluetooth_mesh_network_t *network;
3488
3489                 memset(&node, 0x00, sizeof(bluetooth_mesh_node_t));
3490                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3491
3492                 __bt_service_get_parameters(in_param1,
3493                                 &node, sizeof(bluetooth_mesh_node_t));
3494                 network_name = (const char *)g_variant_get_data(in_param2);
3495
3496                 BT_INFO("MESH: Network name [%s]", network_name);
3497
3498                 param3 = g_array_new(TRUE, TRUE, sizeof(gchar));
3499                 __bt_fill_garray_from_variant(in_param3, param3);
3500
3501                 total_models = (param3->len) / sizeof(bluetooth_mesh_model_t);
3502                 for (i = 0; i < total_models; i++) {
3503                         bluetooth_mesh_model_t *info = NULL;
3504                         bluetooth_mesh_model_t *mod = NULL;
3505
3506                         info = &g_array_index(param3,
3507                                         bluetooth_mesh_model_t, i);
3508
3509                         mod = g_memdup(info, sizeof(bluetooth_mesh_model_t));
3510
3511                         if (mod)
3512                                 model_list = g_slist_append(model_list,
3513                                                         (gpointer)mod);
3514                 }
3515                 result = _bt_mesh_network_create(requester_unique_creds,
3516                                 sender, network_name,  &node, model_list);
3517
3518                  /* Save invocation */
3519                 if (result == BLUETOOTH_ERROR_NONE) {
3520                         BT_INFO("Mesh: Save Invoation");
3521                         network =  g_malloc0(sizeof(bluetooth_mesh_network_t));
3522
3523                         /* Save Network name & UUID, Token will be received in event */
3524                         _bt_mesh_util_convert_hex_to_string(
3525                                         (uint8_t *) node.uuid, 16,
3526                                         network->uuid,
3527                                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
3528                         BT_INFO("Mesh: Mesh Netwok UUID [%s]", network->uuid);
3529                         g_strlcpy(network->name.name, network_name,
3530                                 sizeof(network->name.name));
3531                         g_strlcpy(network->app_cred,
3532                                 requester_unique_creds,
3533                                 sizeof(network->app_cred));
3534                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3535                         BT_INFO("Mesh: Network Create by [%s]", sender);
3536                         _bt_save_invocation_context(context,
3537                                         result, sender,
3538                                         function_name, (gpointer)network);
3539                 } else {
3540                         g_array_append_vals(*out_param1, &node, sizeof(bluetooth_mesh_node_t));
3541                         BT_ERR("Mesh: Mesh Network creation schedule failed");
3542                         g_slist_free_full(model_list, g_free);
3543                 }
3544
3545                 /* Cleanup */
3546                 BT_INFO("Mesh: Cleanup");
3547                 g_free((gpointer)requester_unique_creds);
3548                 g_array_free(param3, TRUE);
3549                 BT_INFO("Mesh: Cleanup Done");
3550                 break;
3551         }
3552         case BT_MESH_NETWORK_DESTROY: {
3553                 bluetooth_mesh_network_t network;
3554                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3555
3556                 __bt_service_get_parameters(in_param1,
3557                                 &network, sizeof(bluetooth_mesh_network_t));
3558
3559                 BT_INFO("Mesh: Destroy Network");
3560                 result = _bt_mesh_network_destroy(requester_unique_creds,
3561                                 sender, &network);
3562
3563                 if (result != BLUETOOTH_ERROR_NONE) {
3564                         g_array_append_vals(*out_param1,
3565                                 &network, sizeof(bluetooth_mesh_network_t));
3566                 } else {
3567                         BT_INFO("Mesh: Destroy Network scheduled: add invocation");
3568                         bluetooth_mesh_network_t *net =  \
3569                                         g_memdup(&network, sizeof(bluetooth_mesh_network_t));
3570
3571                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3572                         _bt_save_invocation_context(context, result, sender,
3573                                         function_name, (gpointer)net);
3574                 }
3575                 break;
3576         }
3577         case BT_MESH_NETWORK_LOAD: {
3578                 char *token = NULL;
3579                 bluetooth_mesh_network_t *network;
3580                 token = (char *)g_variant_get_data(in_param1);
3581                 BT_INFO("MESH: Network Load: Token string [%s]", token);
3582                 result = _bt_mesh_network_load(requester_unique_creds, sender, token);
3583                 /* Save invocation */
3584                 if (result == BLUETOOTH_ERROR_NONE) {
3585                         network =  g_malloc0(sizeof(bluetooth_mesh_network_t));
3586                         g_strlcpy(network->token.token, token,
3587                                         BLUETOOTH_MESH_NETWORK_TOKEN_STRING_LENGTH + 1);
3588                         g_strlcpy(network->app_cred, requester_unique_creds,
3589                                         sizeof(network->app_cred));
3590
3591                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3592                         _bt_save_invocation_context(context, result, sender,
3593                                         function_name, (gpointer)network);
3594                 }
3595                 g_free((gpointer)requester_unique_creds);
3596                 break;
3597         }
3598         case BT_MESH_NETWORK_UNLOAD: {
3599                 bluetooth_mesh_network_t network;
3600                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3601
3602                 __bt_service_get_parameters(in_param1,
3603                                 &network, sizeof(bluetooth_mesh_network_t));
3604
3605                 BT_INFO("MESH: Network Unload: Network UUID [%s]", network.uuid);
3606                 result = _bt_mesh_network_unload(requester_unique_creds,
3607                                 sender, &network);
3608                 break;
3609         }
3610         case BT_MESH_NETWORK_SCAN: {
3611                 bluetooth_mesh_network_t network;
3612                 bluetooth_mesh_scan_param_t param;
3613                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3614                 memset(&param, 0x00, sizeof(bluetooth_mesh_scan_param_t));
3615
3616                 __bt_service_get_parameters(in_param1,
3617                                 &network, sizeof(bluetooth_mesh_network_t));
3618
3619                 __bt_service_get_parameters(in_param2,
3620                                 &param, sizeof(bluetooth_mesh_scan_param_t));
3621
3622                 result = _bt_mesh_network_scan(requester_unique_creds,
3623                                 sender, &network, &param);
3624
3625                 if (result != BLUETOOTH_ERROR_NONE) {
3626                         g_array_append_vals(*out_param1,
3627                                 &network, sizeof(bluetooth_mesh_network_t));
3628                 } else {
3629                         bluetooth_mesh_network_t *net =  \
3630                                         g_memdup(&network, sizeof(bluetooth_mesh_network_t));
3631
3632                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3633                         _bt_save_invocation_context(context, result, sender,
3634                                         function_name, (gpointer)net);
3635                 }
3636                 break;
3637         }
3638         case BT_MESH_NETWORK_CANCEL_SCAN: {
3639                 bluetooth_mesh_network_t network;
3640                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3641
3642                 __bt_service_get_parameters(in_param1,
3643                                 &network, sizeof(bluetooth_mesh_network_t));
3644
3645                 result = _bt_mesh_network_scan_cancel(requester_unique_creds,
3646                         sender, &network);
3647
3648                 if (result != BLUETOOTH_ERROR_NONE)
3649                         g_array_append_vals(*out_param1,
3650                                 &network, sizeof(bluetooth_mesh_network_t));
3651                 break;
3652         }
3653         case BT_MESH_NETWORK_SET_CAPABILITIES: {
3654                 bluetooth_mesh_network_t network;
3655                 bluetooth_mesh_provisioner_caps_t caps;
3656                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3657                 memset(&caps, 0x00, sizeof(bluetooth_mesh_provisioner_caps_t));
3658
3659                 __bt_service_get_parameters(in_param1,
3660                                 &network, sizeof(bluetooth_mesh_network_t));
3661
3662                 __bt_service_get_parameters(in_param2,
3663                                 &caps, sizeof(bluetooth_mesh_provisioner_caps_t));
3664
3665                 result = _bt_mesh_network_set_provisioner_caps(
3666                                 requester_unique_creds, sender, &network, &caps);
3667
3668                 break;
3669         }
3670         case BT_MESH_NETWORK_PROVISION_DEVICE: {
3671                 bluetooth_mesh_provisioning_request_t req;
3672                 memset(&req, 0x00, sizeof(bluetooth_mesh_provisioning_request_t));
3673
3674                 __bt_service_get_parameters(in_param1,
3675                                 &req, sizeof(bluetooth_mesh_provisioning_request_t));
3676
3677                 BT_INFO("Mesh: Provision Device UUID [%s]", req.dev_uuid);
3678                 BT_INFO("Mesh: Provision Device in network[%s]", req.net_uuid);
3679
3680                 result = _bt_mesh_network_provision_device(
3681                                 requester_unique_creds, sender, &req);
3682
3683                 /* Save invocation */
3684                 if (result != BLUETOOTH_ERROR_NONE) {
3685                         g_array_append_vals(*out_param1, &req,
3686                                 sizeof(bluetooth_mesh_provisioning_request_t));
3687                 } else {
3688                         bluetooth_mesh_provisioning_request_t *param =  \
3689                                 g_memdup(&req, sizeof(bluetooth_mesh_provisioning_request_t));
3690
3691                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3692                         _bt_save_invocation_context(context, result, sender,
3693                                         function_name, (gpointer)param);
3694                 }
3695                 break;
3696         }
3697         case BT_MESH_AUTHENTICATION_REPLY: {
3698                 BT_INFO("Mesh: BT_MESH_AUTHENTICATION_REPLY");
3699                 const char *auth_value = NULL;
3700                 gboolean authentication_reply = FALSE;
3701                 int auth_type;
3702                 auth_value = g_variant_get_data(in_param2);
3703                 __bt_service_get_parameters(in_param3,
3704                                 &authentication_reply, sizeof(gboolean));
3705                 __bt_service_get_parameters(in_param1,
3706                                 &auth_type, sizeof(int));
3707                 BT_INFO("Mesh: Auth Reply Type [%d]", auth_type);
3708                 BT_INFO("Mesh: Auth Value [%s]", auth_value);
3709                 BT_INFO("Mesh: Auth Accept [%d]", authentication_reply);
3710                 result = _bt_mesh_authentication_reply(auth_type,
3711                                 auth_value, authentication_reply);
3712                 break;
3713         }
3714         case BT_MESH_NETWORK_SET_NAME: {
3715                 bluetooth_mesh_network_t network;
3716                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3717
3718                 __bt_service_get_parameters(in_param1,
3719                                 &network, sizeof(bluetooth_mesh_network_t));
3720
3721                 result = _bt_mesh_network_set_name(requester_unique_creds, sender, &network);
3722
3723                 break;
3724         }
3725         case BT_MESH_NETWORK_ADD_NETKEY: {
3726                 bluetooth_mesh_network_t network;
3727                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3728
3729                 __bt_service_get_parameters(in_param1,
3730                                 &network, sizeof(bluetooth_mesh_network_t));
3731
3732                 result = _bt_mesh_network_add_netkey(requester_unique_creds,
3733                         sender, &network);
3734
3735                 if (result != BLUETOOTH_ERROR_NONE) {
3736                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
3737                 } else {
3738                         bluetooth_mesh_network_t *net =  g_memdup(&network, sizeof(bluetooth_mesh_network_t));
3739
3740                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3741                         _bt_save_invocation_context(context, result, sender,
3742                                         function_name, (gpointer)net);
3743                 }
3744                 break;
3745         }
3746         case BT_MESH_NETWORK_DELETE_NETKEY: {
3747                 bluetooth_mesh_network_t network;
3748                 uint16_t index;
3749
3750                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3751
3752                 __bt_service_get_parameters(in_param1,
3753                                 &network, sizeof(bluetooth_mesh_network_t));
3754                 __bt_service_get_parameters(in_param2,
3755                                 &index, sizeof(guint16));
3756
3757                 result = _bt_mesh_network_delete_netkey(requester_unique_creds,
3758                                 sender, &network, index);
3759
3760                 if (result != BLUETOOTH_ERROR_NONE) {
3761                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
3762                 } else {
3763                         bluetooth_mesh_network_t *net =  g_memdup(&network, sizeof(bluetooth_mesh_network_t));
3764
3765                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3766                         _bt_save_invocation_context(context, result, sender,
3767                                         function_name, (gpointer)net);
3768                 }
3769                 break;
3770         }
3771         case BT_MESH_NETWORK_UPDATE_NETKEY: {
3772                 bluetooth_mesh_network_t network;
3773                 uint16_t index;
3774
3775                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3776
3777                 __bt_service_get_parameters(in_param1,
3778                                 &network, sizeof(bluetooth_mesh_network_t));
3779                 __bt_service_get_parameters(in_param2,
3780                                 &index, sizeof(guint16));
3781
3782                 result = _bt_mesh_network_update_netkey(requester_unique_creds,
3783                                 sender, &network, index);
3784
3785                 if (result != BLUETOOTH_ERROR_NONE) {
3786                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
3787                 } else {
3788                         bluetooth_mesh_network_t *net =  g_memdup(&network, sizeof(bluetooth_mesh_network_t));
3789
3790                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3791                         _bt_save_invocation_context(context, result, sender,
3792                                         function_name, (gpointer)net);
3793                 }
3794                 break;
3795         }
3796         case BT_MESH_NETWORK_ADD_APPKEY: {
3797                 bluetooth_mesh_network_t network;
3798                 uint16_t net_idx;
3799
3800                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3801
3802                 __bt_service_get_parameters(in_param1,
3803                                 &network, sizeof(bluetooth_mesh_network_t));
3804                 __bt_service_get_parameters(in_param2,
3805                                 &net_idx, sizeof(guint16));
3806
3807                 result = _bt_mesh_network_add_appkey(requester_unique_creds,
3808                                 sender, &network, net_idx);
3809
3810                 if (result != BLUETOOTH_ERROR_NONE) {
3811                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
3812                 } else {
3813                         bluetooth_mesh_network_t *net =  g_memdup(&network, sizeof(bluetooth_mesh_network_t));
3814
3815                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3816                         _bt_save_invocation_context(context, result, sender,
3817                                         function_name, (gpointer)net);
3818                 }
3819                 break;
3820         }
3821         case BT_MESH_NETWORK_UPDATE_APPKEY: {
3822                 bluetooth_mesh_network_t network;
3823                 uint16_t net_idx;
3824                 uint16_t app_idx;
3825
3826                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3827
3828                 __bt_service_get_parameters(in_param1,
3829                                 &network, sizeof(bluetooth_mesh_network_t));
3830                 __bt_service_get_parameters(in_param2,
3831                                 &net_idx, sizeof(guint16));
3832                 __bt_service_get_parameters(in_param3,
3833                                 &app_idx, sizeof(guint16));
3834
3835                 result = _bt_mesh_network_update_appkey(requester_unique_creds,
3836                                 sender, &network, net_idx, app_idx);
3837
3838                 if (result != BLUETOOTH_ERROR_NONE) {
3839                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
3840                 } else {
3841                         bluetooth_mesh_network_t *net =  g_memdup(&network, sizeof(bluetooth_mesh_network_t));
3842
3843                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3844                         _bt_save_invocation_context(context, result, sender,
3845                                         function_name, (gpointer)net);
3846                 }
3847                 break;
3848         }
3849         case BT_MESH_NETWORK_DELETE_APPKEY: {
3850                 bluetooth_mesh_network_t network;
3851                 uint16_t net_idx;
3852                 uint16_t app_idx;
3853
3854                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3855
3856                 __bt_service_get_parameters(in_param1,
3857                                 &network, sizeof(bluetooth_mesh_network_t));
3858                 __bt_service_get_parameters(in_param2,
3859                                 &net_idx, sizeof(guint16));
3860                 __bt_service_get_parameters(in_param3,
3861                                 &app_idx, sizeof(guint16));
3862
3863                 result = _bt_mesh_network_delete_appkey(requester_unique_creds,
3864                                 sender, &network, net_idx, app_idx);
3865
3866                 if (result != BLUETOOTH_ERROR_NONE) {
3867                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
3868                 } else {
3869                         bluetooth_mesh_network_t *net =  g_memdup(&network, sizeof(bluetooth_mesh_network_t));
3870
3871                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3872                         _bt_save_invocation_context(context, result, sender,
3873                                         function_name, (gpointer)net);
3874                 }
3875                 break;
3876         }
3877         case BT_MESH_NETWORK_GET_NETKEYS: {
3878                 bluetooth_mesh_network_t network;
3879                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3880                 __bt_service_get_parameters(in_param1,
3881                                 &network, sizeof(bluetooth_mesh_network_t));
3882                 result = _bt_mesh_network_get_netkeys(requester_unique_creds,
3883                                 sender, &network, out_param1);
3884                 break;
3885         }
3886         case BT_MESH_NETWORK_GET_APPKEYS: {
3887                 bluetooth_mesh_network_t network;
3888                 uint16_t net_idx;
3889
3890                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3891                 __bt_service_get_parameters(in_param1,
3892                                 &network, sizeof(bluetooth_mesh_network_t));
3893                 __bt_service_get_parameters(in_param2,
3894                                 &net_idx, sizeof(guint16));
3895                 result = _bt_mesh_network_get_appkeys(
3896                                 requester_unique_creds, sender,
3897                                         &network, net_idx, out_param1);
3898                 break;
3899         }
3900         case BT_MESH_NETWORK_GET_NODES: {
3901                 bluetooth_mesh_network_t network;
3902                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3903                 __bt_service_get_parameters(in_param1,
3904                                 &network, sizeof(bluetooth_mesh_network_t));
3905                 result = _bt_mesh_network_get_nodes(
3906                                 requester_unique_creds, sender,
3907                                         &network, out_param1);
3908                 break;
3909         }
3910         case BT_MESH_ELEMENT_GET_MODELS: {
3911                 bluetooth_mesh_network_t network;
3912                 uint16_t node_addr;
3913                 int elem_idx;
3914
3915                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3916                 __bt_service_get_parameters(in_param1,
3917                                 &network, sizeof(bluetooth_mesh_network_t));
3918                 __bt_service_get_parameters(in_param2,
3919                                 &node_addr, sizeof(guint16));
3920                 __bt_service_get_parameters(in_param3,
3921                                 &elem_idx, sizeof(int));
3922                 result = _bt_mesh_element_get_models(requester_unique_creds, sender,
3923                                 &network, node_addr, elem_idx, out_param1);
3924                 break;
3925         }
3926         case BT_MESH_NODE_BROWSE: {
3927                 bluetooth_mesh_node_discover_t req;
3928                 memset(&req, 0x00, sizeof(bluetooth_mesh_node_discover_t));
3929
3930                 __bt_service_get_parameters(in_param1,
3931                                 &req, sizeof(bluetooth_mesh_node_discover_t));
3932
3933                 result = _bt_mesh_browse_remote_node(
3934                                 requester_unique_creds, sender, &req);
3935
3936                 /* Save invocation */
3937                 if (result != BLUETOOTH_ERROR_NONE) {
3938                         g_array_append_vals(*out_param1, &req,
3939                                 sizeof(bluetooth_mesh_node_discover_t));
3940                 } else {
3941                         bluetooth_mesh_node_discover_t *param = \
3942                                 g_memdup(&req, sizeof(bluetooth_mesh_node_discover_t));
3943
3944                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3945                         _bt_save_invocation_context(context, result, sender,
3946                                         function_name, (gpointer)param);
3947                 }
3948                 break;
3949         }
3950         case BT_MESH_NODE_GET_VENDOR_FEATURES: {
3951                 bluetooth_mesh_node_features_t req;
3952                 memset(&req, 0x00, sizeof(bluetooth_mesh_node_features_t));
3953
3954                 __bt_service_get_parameters(in_param1,
3955                                 &req, sizeof(bluetooth_mesh_node_features_t));
3956
3957                 result = _bt_mesh_node_discover_vendor_features(
3958                                 requester_unique_creds, sender, &req);
3959
3960                 /* Save invocation */
3961                 if (result != BLUETOOTH_ERROR_NONE) {
3962                         g_array_append_vals(*out_param1,
3963                                 &req, sizeof(bluetooth_mesh_node_features_t));
3964                 } else {
3965                         bluetooth_mesh_node_features_t *param =  \
3966                                 g_memdup(&req, sizeof(bluetooth_mesh_node_features_t));
3967
3968                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3969                         _bt_save_invocation_context(context, result, sender,
3970                                         function_name, (gpointer)param);
3971                 }
3972                 break;
3973         }
3974         case BT_MESH_NODE_CONFIGURE_KEY: {
3975                 bluetooth_mesh_key_configure_t req;
3976
3977                 memset(&req, 0x00, sizeof(bluetooth_mesh_key_configure_t));
3978
3979                 __bt_service_get_parameters(in_param1,
3980                                 &req, sizeof(bluetooth_mesh_key_configure_t));
3981
3982                 result = _bt_mesh_node_configure_key(
3983                                 requester_unique_creds, sender, &req);
3984
3985                 /* Save invocation */
3986                 if (result != BLUETOOTH_ERROR_NONE) {
3987                         g_array_append_vals(*out_param1,
3988                                 &req, sizeof(bluetooth_mesh_key_configure_t));
3989                 } else {
3990                         bluetooth_mesh_key_configure_t *param = \
3991                                 g_memdup(&req, sizeof(bluetooth_mesh_key_configure_t));
3992
3993                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3994                         _bt_save_invocation_context(context, result, sender,
3995                                         function_name, (gpointer)param);
3996                 }
3997                 break;
3998         }
3999         case BT_MESH_NODE_TTL_EXECUTE: {
4000                 bluetooth_mesh_node_ttl_info_t req;
4001
4002                 memset(&req, 0x00, sizeof(bluetooth_mesh_node_ttl_info_t));
4003
4004                 __bt_service_get_parameters(in_param1,
4005                                 &req, sizeof(bluetooth_mesh_node_ttl_info_t));
4006
4007                 result = _bt_mesh_ttl_execute_remote_node(
4008                                 requester_unique_creds, sender, &req);
4009
4010                 /* Save invocation */
4011                 if (result != BLUETOOTH_ERROR_NONE) {
4012                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_node_ttl_info_t));
4013                 } else {
4014                         bluetooth_mesh_node_ttl_info_t *param =  \
4015                                 g_memdup(&req, sizeof(bluetooth_mesh_node_ttl_info_t));
4016
4017                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4018                         _bt_save_invocation_context(context, result, sender,
4019                                         function_name, (gpointer)param);
4020                 }
4021                 break;
4022         }
4023         case BT_MESH_MODEL_CONFIGURE_APPKEY: {
4024                 bluetooth_mesh_model_configure_t req;
4025
4026                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
4027
4028                 __bt_service_get_parameters(in_param1,
4029                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4030
4031                 result = _bt_mesh_node_model_appkey_execute(
4032                                 requester_unique_creds, sender, &req);
4033
4034                 /* Save invocation */
4035                 if (result != BLUETOOTH_ERROR_NONE) {
4036                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_configure_t));
4037                 } else {
4038                         bluetooth_mesh_model_configure_t *param =  \
4039                                 g_memdup(&req, sizeof(bluetooth_mesh_model_configure_t));
4040
4041                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4042                         _bt_save_invocation_context(context, result, sender,
4043                                         function_name, (gpointer)param);
4044                 }
4045                 break;
4046         }
4047         case BT_MESH_MODEL_EXECUTE_MSG: {
4048                 bluetooth_mesh_model_msg_t req;
4049
4050                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_msg_t));
4051
4052                 __bt_service_get_parameters(in_param1,
4053                                 &req, sizeof(bluetooth_mesh_model_msg_t));
4054
4055                 result = _bt_mesh_model_execute_msg(
4056                                 requester_unique_creds, sender, &req);
4057
4058                 /* Save invocation */
4059                 if (result != BLUETOOTH_ERROR_NONE) {
4060                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_msg_t));
4061                 } else {
4062                         bluetooth_mesh_model_msg_t *param =  \
4063                                 g_memdup(&req, sizeof(bluetooth_mesh_model_msg_t));
4064
4065                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4066                         _bt_save_invocation_context(context, result, sender,
4067                                         function_name, (gpointer)param);
4068                 }
4069                 break;
4070         }
4071         case BT_MESH_NODE_GET_NETKEYS: {
4072                 bluetooth_mesh_node_discover_t node;
4073                 memset(&node, 0x00, sizeof(bluetooth_mesh_node_discover_t));
4074                 __bt_service_get_parameters(in_param1,
4075                                 &node, sizeof(bluetooth_mesh_node_discover_t));
4076                 result = _bt_mesh_network_node_get_netkeys(
4077                                 requester_unique_creds, &node, out_param1);
4078                 break;
4079         }
4080         case BT_MESH_NODE_GET_APPKEYS: {
4081                 bluetooth_mesh_node_discover_t node;
4082                 memset(&node, 0x00, sizeof(bluetooth_mesh_node_discover_t));
4083                 __bt_service_get_parameters(in_param1,
4084                                 &node, sizeof(bluetooth_mesh_node_discover_t));
4085                 result = _bt_mesh_network_node_get_appkeys(
4086                                 requester_unique_creds, sender, &node, out_param1);
4087                 break;
4088         }
4089         case BT_MESH_NODE_RESET: {
4090                 bluetooth_mesh_node_info_t node;
4091                 memset(&node, 0x00, sizeof(bluetooth_mesh_node_info_t));
4092
4093                 __bt_service_get_parameters(in_param1,
4094                                 &node, sizeof(bluetooth_mesh_node_info_t));
4095
4096                 BT_INFO("Mesh: Reset the Node");
4097                 result = _bt_mesh_network_reset_node(requester_unique_creds,
4098                                 sender, &node);
4099
4100                 break;
4101         }
4102         case BT_MESH_MODEL_GET_APPKEY_LIST: {
4103                 bluetooth_mesh_model_configure_t req;
4104
4105                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
4106
4107                 __bt_service_get_parameters(in_param1,
4108                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4109
4110                 result = _bt_mesh_node_model_get_appkey_list(
4111                                         requester_unique_creds, sender, &req);
4112
4113                 /* Save invocation */
4114                 if (result != BLUETOOTH_ERROR_NONE) {
4115                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_configure_t));
4116                 } else {
4117                         bluetooth_mesh_model_configure_t *param = \
4118                                 g_memdup(&req, sizeof(bluetooth_mesh_model_configure_t));
4119
4120                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4121                         _bt_save_invocation_context(context, result, sender,
4122                                         function_name, (gpointer)param);
4123                 }
4124                 break;
4125         }
4126         case BT_MESH_MODEL_GET_SUBSCRIPTION_LIST: {
4127                 bluetooth_mesh_model_configure_t req;
4128
4129                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
4130
4131                 __bt_service_get_parameters(in_param1,
4132                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4133
4134                 result = _bt_mesh_node_model_get_subscription_list(
4135                                 requester_unique_creds, sender, &req);
4136
4137                 /* Save invocation */
4138                 if (result != BLUETOOTH_ERROR_NONE) {
4139                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_configure_t));
4140                 } else {
4141                         bluetooth_mesh_model_configure_t *param =  \
4142                                 g_memdup(&req, sizeof(bluetooth_mesh_model_configure_t));
4143
4144                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4145                         _bt_save_invocation_context(context, result, sender,
4146                                         function_name, (gpointer)param);
4147                 }
4148
4149                 break;
4150         }
4151         case BT_MESH_NETWORK_CREATE_GROUP: {
4152                 bluetooth_mesh_network_t net;
4153                 bluetooth_mesh_network_group_info_t res;
4154                 gboolean is_virtual;
4155                 uint16_t grp_addr;
4156
4157                 memset(&net, 0x00, sizeof(bluetooth_mesh_network_t));
4158                 memset(&res, 0x00, sizeof(bluetooth_mesh_network_group_info_t));
4159
4160                 __bt_service_get_parameters(in_param1,
4161                                 &net, sizeof(bluetooth_mesh_network_t));
4162                 __bt_service_get_parameters(in_param2,
4163                                 &is_virtual, sizeof(gboolean));
4164                 __bt_service_get_parameters(in_param3,
4165                                 &grp_addr, sizeof(guint16));
4166
4167                 result = _bt_mesh_network_create_group(
4168                                 requester_unique_creds, sender, &net,
4169                                         is_virtual, grp_addr, &res);
4170
4171                 if (result == BLUETOOTH_ERROR_NONE)
4172                         g_array_append_vals(*out_param1, &res,
4173                                 sizeof(bluetooth_mesh_network_group_info_t));
4174                 break;
4175         }
4176         case BT_MESH_NETWORK_REMOVE_GROUP: {
4177                 bluetooth_mesh_network_t net;
4178                 bluetooth_mesh_network_group_info_t req;
4179
4180                 memset(&net, 0x00, sizeof(bluetooth_mesh_network_t));
4181                 memset(&req, 0x00, sizeof(bluetooth_mesh_network_group_info_t));
4182
4183                 __bt_service_get_parameters(in_param1,
4184                                 &net, sizeof(bluetooth_mesh_network_t));
4185                 __bt_service_get_parameters(in_param2,
4186                                 &req, sizeof(bluetooth_mesh_network_group_info_t));
4187
4188                 result = _bt_mesh_network_remove_group(
4189                                 requester_unique_creds, sender, &net, &req);
4190
4191                 break;
4192         }
4193         case BT_MESH_MODEL_CONFIG_GROUP_SUB: {
4194                 bluetooth_mesh_model_configure_t req;
4195
4196                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
4197
4198                 __bt_service_get_parameters(in_param1,
4199                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4200
4201                 result = _bt_mesh_model_configure_group_subscription(
4202                                 requester_unique_creds, sender, &req);
4203
4204                 /* Save invocation */
4205                 if (result != BLUETOOTH_ERROR_NONE) {
4206                         g_array_append_vals(*out_param1,
4207                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4208                 } else {
4209                         bluetooth_mesh_model_configure_t *param =  \
4210                                 g_memdup(&req, sizeof(bluetooth_mesh_model_configure_t));
4211
4212                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4213                         _bt_save_invocation_context(context, result, sender,
4214                                         function_name, (gpointer)param);
4215                 }
4216                 break;
4217         }
4218         case BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB: {
4219                 bluetooth_mesh_model_configure_t req;
4220
4221                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
4222
4223                 __bt_service_get_parameters(in_param1,
4224                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4225
4226                 result = _bt_mesh_model_configure_virtual_group_subscription(
4227                                 requester_unique_creds, sender, &req);
4228
4229                 /* Save invocation */
4230                 if (result != BLUETOOTH_ERROR_NONE) {
4231                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_configure_t));
4232                 } else {
4233                         bluetooth_mesh_model_configure_t *param =  \
4234                                 g_memdup(&req, sizeof(bluetooth_mesh_model_configure_t));
4235
4236                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4237                         _bt_save_invocation_context(context, result, sender,
4238                                         function_name, (gpointer)param);
4239                 }
4240                 break;
4241         }
4242         case BT_MESH_MODEL_SET_PUBLICATION: {
4243                 bluetooth_mesh_model_configure_t req;
4244
4245                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
4246
4247                 __bt_service_get_parameters(in_param1,
4248                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4249
4250                 result = _bt_mesh_model_set_publication(
4251                                 requester_unique_creds, sender, &req);
4252
4253                 /* Save invocation */
4254                 if (result != BLUETOOTH_ERROR_NONE) {
4255                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_configure_t));
4256                 } else {
4257                         bluetooth_mesh_model_configure_t *param = g_memdup(&req, sizeof(bluetooth_mesh_model_configure_t));
4258
4259                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4260                         _bt_save_invocation_context(context, result, sender,
4261                                         function_name, (gpointer)param);
4262                 }
4263                 break;
4264         }
4265         case BT_MESH_MODEL_GET_PUBLICATION: {
4266                 bluetooth_mesh_model_configure_t req;
4267
4268                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
4269
4270                 __bt_service_get_parameters(in_param1,
4271                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4272
4273                 result = _bt_mesh_model_get_publication(
4274                                         requester_unique_creds, sender, &req);
4275
4276                 /* Save invocation */
4277                 if (result != BLUETOOTH_ERROR_NONE) {
4278                         g_array_append_vals(*out_param1,
4279                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4280                 } else {
4281                         bluetooth_mesh_model_configure_t *param =  \
4282                                 g_memdup(&req, sizeof(bluetooth_mesh_model_configure_t));
4283
4284                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4285                         _bt_save_invocation_context(context, result, sender,
4286                                         function_name, (gpointer)param);
4287                 }
4288                 break;
4289         }
4290         case BT_MESH_NETWORK_GET_GROUPS: {
4291                 bluetooth_mesh_network_t network;
4292                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
4293                 __bt_service_get_parameters(in_param1,
4294                                 &network, sizeof(bluetooth_mesh_network_t));
4295                 BT_INFO("Mesh: Request:: Get Groups");
4296                 result = _bt_mesh_network_get_groups(
4297                                 requester_unique_creds, sender, &network, out_param1);
4298                 break;
4299         }
4300         default:
4301                 BT_INFO("UnSupported function [%d]", function_name);
4302                 result = BLUETOOTH_ERROR_NOT_SUPPORT;
4303                 break;
4304         }
4305
4306         return result;
4307 }
4308 #ifdef TIZEN_FEATURE_BT_OBEX
4309 /* Function definitions*/
4310 static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
4311 {
4312         char *data;
4313         int size;
4314
4315         size = g_variant_get_size(var);
4316         if (size > 0) {
4317                 data = (char *)g_variant_get_data(var);
4318                 if (data)
4319                         param = g_array_append_vals(param, data, size);
4320
4321         }
4322 }
4323 #endif
4324
4325 int __bt_obexd_request(int function_name,
4326                 int request_type,
4327                 int request_id,
4328                 GDBusMethodInvocation *context,
4329                 GVariant *in_param1,
4330                 GVariant *in_param2,
4331                 GVariant *in_param3,
4332                 GVariant *in_param4,
4333                 GArray **out_param1)
4334 {
4335         BT_DBG("+");
4336
4337         int result = BLUETOOTH_ERROR_NONE;
4338
4339         BT_DBG("function_name : %x", function_name);
4340
4341         switch (function_name) {
4342         case BT_OPP_PUSH_FILES: {
4343                 BT_DBG("BT_OPP_PUSH_FILES");
4344                 int i;
4345                 bluetooth_device_address_t address = { {0} };
4346                 bt_file_path_t *path;
4347                 char **file_path;
4348                 int file_count;
4349                 GArray *param2;
4350
4351                 __bt_service_get_parameters(in_param1, &address,
4352                                 sizeof(bluetooth_device_address_t));
4353                 __bt_service_get_parameters(in_param3, &file_count,
4354                                 sizeof(int));
4355
4356                 file_path = g_new0(char *, file_count + 1);
4357
4358                 param2 = g_array_new(TRUE, TRUE, sizeof(gchar));
4359                 __bt_fill_garray_from_variant(in_param2, param2);
4360
4361                 for (i = 0; i < file_count; i++) {
4362                         path = &g_array_index(param2, bt_file_path_t, i);
4363                         file_path[i] = g_strdup(path->path);
4364                 }
4365                 BT_DBG("_bt_opp_client_push_files");
4366                 result = _bt_opp_client_push_files(request_id, context,
4367                                                 &address, file_path,
4368                                                 file_count);
4369
4370                 for (i = 0; i < file_count; i++)
4371                         g_free(file_path[i]);
4372
4373                 g_free(file_path);
4374                 g_array_free(param2, TRUE);
4375
4376                 break;
4377         }
4378         case BT_OPP_CANCEL_PUSH: {
4379                 result = _bt_opp_client_cancel_push();
4380
4381                 break;
4382         }
4383         case BT_OPP_IS_PUSHING_FILES: {
4384                 gboolean is_sending = FALSE;
4385
4386                 result = _bt_opp_client_is_sending(&is_sending);
4387
4388                 g_array_append_vals(*out_param1, &is_sending,
4389                                 sizeof(gboolean));
4390                 break;
4391         }
4392         case BT_OPP_GET_TRANSFER_PROGRESS: {
4393                 int direction;
4394                 int transfer_id;
4395                 guint8 progress = 0;
4396
4397                 __bt_service_get_parameters(in_param1, &direction,
4398                                 sizeof(int));
4399                 __bt_service_get_parameters(in_param2, &transfer_id,
4400                                 sizeof(int));
4401                 if (direction)
4402                         result = _bt_opp_get_client_progress(&progress);
4403                 else
4404                         result = _bt_opp_get_server_progress(transfer_id, &progress);
4405
4406                 g_array_append_vals(*out_param1, &progress,
4407                                 sizeof(guint8));
4408
4409                 break;
4410         }
4411
4412         case BT_MAP_CREATE_SESSION: {
4413                 BT_DBG("BT_MAP_CREATE_SESSION");
4414                 char *address = (char *)g_variant_get_data(in_param1);
4415                 char *session_id = NULL;
4416                 result = _bt_create_session_sync(address, &session_id);
4417                 if (result == BLUETOOTH_ERROR_NONE)
4418                         g_array_append_vals(*out_param1, session_id, strlen(session_id)+1);
4419                 break;
4420         }
4421
4422         case BT_MAP_DESTROY_SESSION: {
4423                 BT_DBG("BT_MAP_DESTROY_SESSION");
4424                 char* session_id = (char *)g_variant_get_data(in_param1);
4425                 result = _bt_destroy_session_sync(session_id);
4426                 if (result == BLUETOOTH_ERROR_NONE)
4427                         BT_DBG("successfully destroyed session");
4428                 break;
4429         }
4430
4431         case BT_MAP_SET_FOLDER: {
4432                 BT_DBG("BT_MAP_SET_FOLDER");
4433                 char *session_id = (char *)g_variant_get_data(in_param1);
4434                 char *name = (char *)g_variant_get_data(in_param2);
4435                 result = _bt_map_client_set_folder(session_id, name);
4436                 break;
4437         }
4438
4439         case BT_MAP_LIST_FOLDERS: {
4440                 BT_DBG("BT_MAP_LIST_FOLDERS");
4441
4442                 char* session_id = (char *)g_variant_get_data(in_param1);
4443                 char* filter_serialized = (char*)g_variant_get_data(in_param2);
4444
4445                 result = _bt_map_client_list_folders(request_id, context, session_id, filter_serialized);
4446                 if (result == BLUETOOTH_ERROR_NONE)
4447                         BT_DBG("_bt_map_client_list_folders succeed");
4448
4449                 break;
4450         }
4451
4452         case BT_MAP_LIST_FILTER_FIELDS: {
4453                 BT_DBG("BT_MAP_LIST_FILTER_FIELDS");
4454
4455                 char* session_id = (char *)g_variant_get_data(in_param1);
4456
4457                 result = _bt_map_client_list_filter_fields(request_id, context, session_id);
4458                 if (result == BLUETOOTH_ERROR_NONE)
4459                         BT_DBG("_bt_map_client_list_filter_fields succeed");
4460
4461                 break;
4462         }
4463
4464         case BT_MAP_LIST_MESSAGES: {
4465                 BT_DBG("BT_MAP_LIST_MESSAGES");
4466
4467                 char* session_id = (char*)g_variant_get_data(in_param1);
4468                 char* folder = (char*)g_variant_get_data(in_param2);
4469                 char* filter_serialized = (char*)g_variant_get_data(in_param3);
4470
4471                 result = _bt_map_client_list_messages(request_id, context, session_id, folder, filter_serialized);
4472                 if (result == BLUETOOTH_ERROR_NONE)
4473                         BT_DBG("_bt_map_client_list_messages succeed");
4474                 else
4475                         BT_DBG("_bt_map_client_list_messages failed");
4476
4477                 break;
4478         }
4479
4480         case BT_MAP_UPDATE_INBOX: {
4481                 BT_DBG("BT_MAP_UPDATE_INBOX");
4482                 char* session_id = (char *)g_variant_get_data(in_param1);
4483                 result = _bt_map_client_update_inbox(session_id);
4484                 break;
4485         }
4486
4487         case BT_MAP_PUSH_MESSAGE: {
4488                 BT_DBG("BT_MAP_PUSH_MESSAGE");
4489
4490                 char* session_id = (char *)g_variant_get_data(in_param1);
4491                 char* source_file = (char *)g_variant_get_data(in_param2);
4492                 char* folder = (char *)g_variant_get_data(in_param3);
4493                 char* args_serialized = (char *)g_variant_get_data(in_param4);
4494
4495                 result = _bt_map_client_push_message(
4496                                 request_id, context, session_id, source_file, folder, args_serialized);
4497                 if (result == BLUETOOTH_ERROR_NONE)
4498                         BT_DBG("_bt_map_client_push_message succeed");
4499                 else
4500                         BT_ERR("_bt_map_client_push_message failed");
4501
4502                 break;
4503         }
4504
4505         case BT_MAP_GET_MESSAGE: {
4506                 BT_DBG("BT_MAP_GET_MESSAGE");
4507                 // TODO session currently is not used, but should be valid
4508                 //char* session_id = (char *)g_variant_get_data(in_param1);
4509                 char* message_object = (char *)g_variant_get_data(in_param2);
4510                 char* target_file = (char *)g_variant_get_data(in_param3);
4511                 bool attachment = false;
4512                 __bt_service_get_parameters(in_param4, &attachment, sizeof(bool));
4513
4514                 result = _bt_map_client_get_message(request_id, context, message_object,
4515                                 target_file, attachment);
4516                 if (result == BLUETOOTH_ERROR_NONE)
4517                         BT_DBG("_bt_map_client_get_message succeed");
4518
4519                 break;
4520         }
4521
4522         case BT_OBEX_SERVER_ALLOCATE: {
4523                 int app_pid;
4524                 gboolean is_native;
4525                 char *path;
4526                 char *sender;
4527
4528                 sender = (char *)g_dbus_method_invocation_get_sender(context);
4529
4530                 path = (char *)g_variant_get_data(in_param1);
4531                 __bt_service_get_parameters(in_param2, &is_native,
4532                                 sizeof(gboolean));
4533                 __bt_service_get_parameters(in_param3, &app_pid,
4534                                 sizeof(int));
4535
4536                 result = _bt_obex_server_allocate(sender,
4537                                 path, app_pid, is_native);
4538
4539                 break;
4540         }
4541         case BT_OBEX_SERVER_DEALLOCATE: {
4542                 int app_pid;
4543                 gboolean is_native;
4544
4545                 __bt_service_get_parameters(in_param1, &is_native,
4546                                 sizeof(gboolean));
4547                 __bt_service_get_parameters(in_param2, &app_pid,
4548                                 sizeof(int));
4549
4550                 result = _bt_obex_server_deallocate(app_pid, is_native);
4551                 break;
4552         }
4553         case BT_OBEX_SERVER_IS_ACTIVATED: {
4554                 gboolean is_activated = FALSE;
4555
4556                 result = _bt_obex_server_is_activated(&is_activated);
4557
4558                 g_array_append_vals(*out_param1, &is_activated,
4559                                 sizeof(gboolean));
4560
4561                 break;
4562         }
4563         case BT_OBEX_SERVER_ACCEPT_CONNECTION: {
4564                 result = _bt_obex_server_accept_connection(request_id);
4565
4566                 break;
4567         }
4568         case BT_OBEX_SERVER_REJECT_CONNECTION: {
4569                 result = _bt_obex_server_reject_connection();
4570
4571                 break;
4572         }
4573         case BT_OBEX_SERVER_ACCEPT_FILE: {
4574                 char *file_name;
4575
4576                 file_name = (char *)g_variant_get_data(in_param1);
4577
4578                 result = _bt_obex_server_accept_authorize(file_name, TRUE);
4579
4580                 break;
4581         }
4582         case BT_OBEX_SERVER_REJECT_FILE: {
4583                 result = _bt_obex_server_reject_authorize();
4584
4585                 break;
4586         }
4587         case BT_OBEX_SERVER_SET_PATH: {
4588                 gboolean is_native;
4589                 char *destination_path;
4590
4591                 destination_path = (char *)g_variant_get_data(in_param1);
4592                 __bt_service_get_parameters(in_param2, &is_native,
4593                                 sizeof(gboolean));
4594
4595                 result = _bt_obex_server_set_destination_path(destination_path,
4596                                                         is_native);
4597
4598                 break;
4599         }
4600         case BT_OBEX_SERVER_SET_ROOT: {
4601                 char *root;
4602
4603                 root = (char *)g_variant_get_data(in_param1);
4604
4605                 result = _bt_obex_server_set_root(root);
4606
4607                 break;
4608         }
4609         case BT_OBEX_SERVER_CANCEL_TRANSFER: {
4610                 int transfer_id;
4611
4612                 __bt_service_get_parameters(in_param1, &transfer_id,
4613                                 sizeof(int));
4614
4615                 result = _bt_obex_server_cancel_transfer(transfer_id);
4616
4617                 break;
4618         }
4619         case BT_OBEX_SERVER_CANCEL_ALL_TRANSFERS: {
4620                 result = _bt_obex_server_cancel_all_transfers();
4621
4622                 break;
4623         }
4624         case BT_OBEX_SERVER_IS_RECEIVING: {
4625                 gboolean is_receiving = FALSE;
4626
4627                 result = _bt_obex_server_is_receiving(&is_receiving);
4628
4629                 g_array_append_vals(*out_param1, &is_receiving,
4630                                 sizeof(gboolean));
4631                 break;
4632         }
4633         case BT_PBAP_CONNECT: {
4634                 bluetooth_device_address_t address = { {0} };
4635
4636                 __bt_service_get_parameters(in_param1, &address,
4637                                 sizeof(bluetooth_device_address_t));
4638
4639                 result = _bt_pbap_connect(&address);
4640                 break;
4641         }
4642         case BT_PBAP_DISCONNECT: {
4643                 bluetooth_device_address_t address = { {0} };
4644
4645                 __bt_service_get_parameters(in_param1, &address,
4646                                 sizeof(bluetooth_device_address_t));
4647
4648                 result = _bt_pbap_disconnect(&address);
4649                 break;
4650         }
4651         case BT_PBAP_GET_PHONEBOOK_SIZE: {
4652                 bluetooth_device_address_t address = { {0} };
4653                 bt_pbap_folder_t folder = { 0, };
4654
4655                 __bt_service_get_parameters(in_param1, &address,
4656                                 sizeof(bluetooth_device_address_t));
4657                 __bt_service_get_parameters(in_param2, &folder,
4658                                 sizeof(bt_pbap_folder_t));
4659
4660                 result = _bt_pbap_get_phonebook_size(&address,
4661                                 folder.addressbook, folder.folder_type);
4662                 break;
4663         }
4664         case BT_PBAP_GET_PHONEBOOK: {
4665                 bluetooth_device_address_t address = { {0} };
4666                 bt_pbap_folder_t folder = { 0, };
4667                 bt_pbap_pull_parameters_t app_param = { 0, };
4668
4669                 __bt_service_get_parameters(in_param1, &address,
4670                                 sizeof(bluetooth_device_address_t));
4671                 __bt_service_get_parameters(in_param2, &folder,
4672                                 sizeof(bt_pbap_folder_t));
4673                 __bt_service_get_parameters(in_param3, &app_param,
4674                                 sizeof(bt_pbap_pull_parameters_t));
4675
4676                 result = _bt_pbap_get_phonebook(&address, folder.addressbook,
4677                                 folder.folder_type, &app_param);
4678                 break;
4679         }
4680         case BT_PBAP_GET_LIST: {
4681                 bluetooth_device_address_t address = { {0} };
4682                 bt_pbap_folder_t folder = { 0, };
4683                 bt_pbap_list_parameters_t app_param = { 0, };
4684
4685                 __bt_service_get_parameters(in_param1, &address,
4686                                 sizeof(bluetooth_device_address_t));
4687                 __bt_service_get_parameters(in_param2, &folder,
4688                                 sizeof(bt_pbap_folder_t));
4689                 __bt_service_get_parameters(in_param3, &app_param,
4690                                 sizeof(bt_pbap_list_parameters_t));
4691
4692                 result = _bt_pbap_get_list(&address, folder.addressbook,
4693                                 folder.folder_type, &app_param);
4694                 break;
4695         }
4696         case BT_PBAP_PULL_VCARD: {
4697                 bluetooth_device_address_t address = { {0} };
4698                 bt_pbap_folder_t folder = { 0, };
4699                 bt_pbap_pull_vcard_parameters_t app_param = { 0, };
4700
4701                 __bt_service_get_parameters(in_param1, &address,
4702                                 sizeof(bluetooth_device_address_t));
4703                 __bt_service_get_parameters(in_param2, &folder,
4704                                 sizeof(bt_pbap_folder_t));
4705                 __bt_service_get_parameters(in_param3, &app_param,
4706                                 sizeof(bt_pbap_pull_vcard_parameters_t));
4707
4708                 result = _bt_pbap_pull_vcard(&address, folder.addressbook,
4709                                 folder.folder_type, &app_param);
4710                 break;
4711         }
4712         case BT_PBAP_PHONEBOOK_SEARCH: {
4713                 bluetooth_device_address_t address = { {0} };
4714                 bt_pbap_folder_t folder = { 0, };
4715                 bt_pbap_search_parameters_t app_param = { 0, };
4716
4717                 __bt_service_get_parameters(in_param1, &address,
4718                                 sizeof(bluetooth_device_address_t));
4719                 __bt_service_get_parameters(in_param2, &folder,
4720                                 sizeof(bt_pbap_folder_t));
4721                 __bt_service_get_parameters(in_param3, &app_param,
4722                                 sizeof(bt_pbap_search_parameters_t));
4723
4724                 result = _bt_pbap_phonebook_search(&address, folder.addressbook,
4725                                 folder.folder_type, &app_param);
4726                 break;
4727         }
4728
4729         default:
4730                 BT_ERR("Unknown function!");
4731                 result = BLUETOOTH_ERROR_INTERNAL;
4732                 break;
4733         }
4734
4735         FN_END;
4736
4737         return result;
4738 }
4739
4740 int __bt_agent_request(int function_name,
4741                 int request_type,
4742                 int request_id,
4743                 GDBusMethodInvocation *context,
4744                 GVariant *in_param1,
4745                 GVariant *in_param2,
4746                 GVariant *in_param3,
4747                 GVariant *in_param4,
4748                 GArray **out_param1)
4749 {
4750         int result;
4751         switch (function_name) {
4752         case BT_SET_AUTHORIZATION: {
4753                 int type;
4754                 char *uuid;
4755                 char *path;
4756                 int fd;
4757                 char *sender;
4758
4759                 sender = (char *)g_dbus_method_invocation_get_sender(context);
4760                 __bt_service_get_parameters(in_param1, &type, sizeof(int));
4761                 uuid = (char *)g_variant_get_data(in_param2);
4762                 path = (char *)g_variant_get_data(in_param3);
4763                 __bt_service_get_parameters(in_param4, &fd, sizeof(int));
4764
4765                 result = _bt_register_osp_server_in_agent(sender, type, uuid, path, fd);
4766                 break;
4767         }
4768         case BT_UNSET_AUTHORIZATION: {
4769                 int type;
4770                 char *uuid;
4771
4772                 __bt_service_get_parameters(in_param1, &type, sizeof(int));
4773                 uuid = (char *)g_variant_get_data(in_param2);
4774
4775                 result = _bt_unregister_osp_server_in_agent(type, uuid);
4776                 break;
4777         }
4778         default:
4779                 BT_ERR("Unknown function!");
4780                 result = BLUETOOTH_ERROR_INTERNAL;
4781                 break;
4782         }
4783
4784         return result;
4785 }
4786
4787 int __bt_core_request(int function_name,
4788                 int request_type,
4789                 int request_id,
4790                 GDBusMethodInvocation *context,
4791                 GVariant *in_param1)
4792 {
4793         int result;
4794
4795         switch (function_name) {
4796         case BT_ENABLE_ADAPTER:
4797         {
4798                 bt_status_t status;
4799                 _bt_set_adapter_request_state(TRUE);
4800                 status = _bt_adapter_get_status();
4801
4802                 if (status == BT_ACTIVATING) {
4803                         BT_DBG("Enabling in progress");
4804                         result = BLUETOOTH_ERROR_IN_PROGRESS;
4805                 } else if (status == BT_ACTIVATED) {
4806                         BT_DBG("Already enabled");
4807                         result = BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
4808                 } else {
4809                         _bt_adapter_set_status(BT_ACTIVATING);
4810                         result = BLUETOOTH_ERROR_NONE;
4811                 }
4812
4813                 break;
4814         }
4815         case BT_DISABLE_ADAPTER:
4816         {
4817                 bt_status_t status;
4818
4819                 status = _bt_adapter_get_status();
4820                 if (status == BT_DEACTIVATING) {
4821                                 BT_DBG("Disabling in progress");
4822                                 result = BLUETOOTH_ERROR_IN_PROGRESS;
4823                 } else if (status == BT_DEACTIVATED) {
4824                                 BT_DBG("Already disabled");
4825                                 result = BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
4826                 } else {
4827                         _bt_adapter_set_status(BT_DEACTIVATING);
4828                         result = BLUETOOTH_ERROR_NONE;
4829                 }
4830
4831                 break;
4832         }
4833         case BT_ENABLE_ADAPTER_LE:
4834         {
4835                 bt_le_status_t le_status;
4836                 _bt_set_le_request_state(TRUE);
4837                 le_status = _bt_adapter_get_le_status();
4838                 if (le_status == BT_LE_ACTIVATING) {
4839                         BT_DBG("Enabling in progress");
4840                         result = BLUETOOTH_ERROR_IN_PROGRESS;
4841                 } else if (le_status == BT_LE_ACTIVATED) {
4842                         BT_DBG("Already enabled");
4843                         result = BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
4844                 } else {
4845                         _bt_adapter_set_le_status(BT_LE_ACTIVATING);
4846                         result = BLUETOOTH_ERROR_NONE;
4847                 }
4848
4849                 break;
4850         }
4851         case BT_DISABLE_ADAPTER_LE:
4852         {
4853                 bt_le_status_t le_status;
4854
4855                 le_status = _bt_adapter_get_le_status();
4856                 if (le_status == BT_LE_DEACTIVATING) {
4857                                 BT_DBG("Disabling in progress");
4858                                 result = BLUETOOTH_ERROR_IN_PROGRESS;
4859                 } else if (le_status == BT_LE_DEACTIVATED) {
4860                                 BT_DBG("Already disabled");
4861                                 result = BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
4862                 } else {
4863                         _bt_adapter_set_le_status(BT_LE_DEACTIVATING);
4864                         result = BLUETOOTH_ERROR_NONE;
4865                 }
4866
4867                 break;
4868         }
4869         default:
4870                 BT_ERR("Unknown function!");
4871                 result = BLUETOOTH_ERROR_INTERNAL;
4872                 break;
4873         }
4874
4875         return result;
4876 }
4877
4878 static int __bt_service_get_requester_app_id(const char *unique_name, char *app_id, int len)
4879 {
4880         int ret;
4881         pid_t pid = 0;
4882
4883         retv_if(bt_service_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
4884         retv_if(unique_name == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
4885
4886         ret = __bt_service_get_sender_pid(unique_name, &pid);
4887         if (ret != BLUETOOTH_ERROR_NONE) {
4888                 BT_ERR("Fail to get the sender pid");
4889                 return ret;
4890         }
4891
4892         ret = aul_app_get_appid_bypid(pid, app_id, len);
4893         if (ret != AUL_R_OK) {
4894                 BT_ERR("app_id not found");
4895                 return BLUETOOTH_ERROR_NOT_SUPPORT;
4896         }
4897         BT_DBG("Sender app_id: %s", app_id);
4898
4899         return BLUETOOTH_ERROR_NONE;
4900 }
4901
4902 gboolean __bt_service_check_privilege(int function_name,
4903                                         int service_type,
4904                                         const char *unique_name)
4905 {
4906         int ret_val;
4907         gboolean result = TRUE;
4908         char *client_creds = NULL;
4909         char *user_creds = NULL;
4910         char *client_session = "";
4911         enum cynara_client_creds client_creds_method = CLIENT_METHOD_SMACK;
4912         enum cynara_user_creds user_creds_method = USER_METHOD_UID;
4913         char err_msg[256] = {0, };
4914
4915         retv_if(unique_name == NULL, FALSE);
4916         retv_if(bt_service_conn == NULL, FALSE);
4917
4918         ret_val = cynara_creds_get_default_client_method(&client_creds_method);
4919         if (ret_val != CYNARA_API_SUCCESS) {
4920                 cynara_strerror(ret_val, err_msg, sizeof(err_msg));
4921                 BT_ERR("Fail to get default client method: %s", err_msg);
4922                 return FALSE;
4923         }
4924
4925         ret_val = cynara_creds_get_default_user_method(&user_creds_method);
4926         if (ret_val != CYNARA_API_SUCCESS) {
4927                 cynara_strerror(ret_val, err_msg, sizeof(err_msg));
4928                 BT_ERR("Fail to get default user method: %s", err_msg);
4929                 return FALSE;
4930         }
4931
4932         ret_val = cynara_creds_gdbus_get_client(bt_service_conn, unique_name, client_creds_method, &client_creds);
4933         if (ret_val != CYNARA_API_SUCCESS) {
4934                 cynara_strerror(ret_val, err_msg, sizeof(err_msg));
4935                 BT_ERR("Fail to get client credential: %s", err_msg);
4936                 return FALSE;
4937         }
4938
4939         ret_val = cynara_creds_gdbus_get_user(bt_service_conn, unique_name, user_creds_method, &user_creds);
4940         if (ret_val != CYNARA_API_SUCCESS) {
4941                 cynara_strerror(ret_val, err_msg, sizeof(err_msg));
4942                 BT_ERR("Fail to get user credential: %s", err_msg);
4943                 if (client_creds)
4944                         free(client_creds);
4945                 return FALSE;
4946         }
4947
4948         BT_DBG("%s, %s, %s", unique_name, client_creds, user_creds);
4949
4950         switch (function_name) {
4951         case BT_SET_LOCAL_NAME:
4952         case BT_START_DISCOVERY:
4953         case BT_START_CUSTOM_DISCOVERY:
4954         case BT_CANCEL_DISCOVERY:
4955         case BT_OOB_ADD_REMOTE_DATA:
4956         case BT_OOB_REMOVE_REMOTE_DATA:
4957         case BT_OOB_READ_LOCAL_DATA:
4958         case BT_SET_ADVERTISING:
4959         case BT_SET_CUSTOM_ADVERTISING:
4960         case BT_SET_ADVERTISING_PARAMETERS:
4961         case BT_START_LE_DISCOVERY:
4962         case BT_STOP_LE_DISCOVERY:
4963         case BT_SET_SCAN_PARAMETERS:
4964         case BT_SET_SCAN_TYPE:
4965
4966         case BT_BOND_DEVICE:
4967         case BT_CANCEL_BONDING:
4968         case BT_UNBOND_DEVICE:
4969         case BT_SET_ALIAS:
4970         case BT_SET_AUTHORIZATION:
4971         case BT_UNSET_AUTHORIZATION:
4972         case BT_SEARCH_SERVICE:
4973
4974         case BT_RFCOMM_CLIENT_CONNECT:
4975                 if (function_name == BT_RFCOMM_CLIENT_CONNECT)
4976                         BT_PERMANENT_LOG("Connect socket");
4977         case BT_RFCOMM_CLIENT_CANCEL_CONNECT:
4978         case BT_RFCOMM_SOCKET_DISCONNECT:
4979                 if (function_name == BT_RFCOMM_SOCKET_DISCONNECT)
4980                         BT_PERMANENT_LOG("Disconnect socket");
4981         case BT_RFCOMM_SOCKET_WRITE:
4982         case BT_RFCOMM_CREATE_SOCKET:
4983         case BT_RFCOMM_REMOVE_SOCKET:
4984         case BT_RFCOMM_SEND_RX_DETAILS:
4985         case BT_RFCOMM_SEND_TX_DETAILS:
4986         case BT_RFCOMM_UPDATE_CONNECTION_INFO:
4987
4988         case BT_OPP_PUSH_FILES:
4989         case BT_OPP_CANCEL_PUSH:
4990         /* TODO: MAP? MAP functions, see above */
4991
4992         case BT_OBEX_SERVER_ACCEPT_CONNECTION:
4993         case BT_OBEX_SERVER_REJECT_CONNECTION:
4994         case BT_OBEX_SERVER_ACCEPT_FILE:
4995         case BT_OBEX_SERVER_REJECT_FILE:
4996         case BT_OBEX_SERVER_SET_PATH:
4997         case BT_OBEX_SERVER_SET_ROOT:
4998         case BT_OBEX_SERVER_CANCEL_TRANSFER:
4999         case BT_OBEX_SERVER_CANCEL_ALL_TRANSFERS:
5000
5001         case BT_AUDIO_SELECT_ROLE:
5002         case BT_AUDIO_CONNECT:
5003         case BT_AUDIO_DISCONNECT:
5004         case BT_AG_CONNECT:
5005         case BT_AG_DISCONNECT:
5006         case BT_AV_CONNECT:
5007         case BT_AV_DISCONNECT:
5008         case BT_AV_SOURCE_CONNECT:
5009         case BT_AV_SOURCE_DISCONNECT:
5010         case BT_AVRCP_TARGET_CONNECT:
5011         case BT_AVRCP_TARGET_DISCONNECT:
5012         case BT_AVRCP_CONTROL_CONNECT:
5013         case BT_AVRCP_CONTROL_DISCONNECT:
5014         case BT_AVRCP_HANDLE_CONTROL:
5015         case BT_AVRCP_SET_TRACK_INFO:
5016         case BT_AVRCP_SET_PROPERTY:
5017         case BT_AVRCP_SET_PROPERTIES:
5018         case BT_AVRCP_CONTROL_SET_PROPERTY:
5019
5020         case BT_AUDIO_SET_ABSOLUTE_VOLUME:
5021         case BT_AUDIO_GET_ABSOLUTE_VOLUME:
5022         case BT_AUDIO_IS_AVC_ACTIVATED:
5023
5024         case BT_HF_CONNECT:
5025         case BT_HF_DISCONNECT:
5026
5027         case BT_HID_CONNECT:
5028         case BT_HID_DISCONNECT:
5029
5030         case BT_HID_DEVICE_ACTIVATE:
5031         case BT_HID_DEVICE_DEACTIVATE:
5032         case BT_HID_DEVICE_CONNECT:
5033         case BT_HID_DEVICE_DISCONNECT:
5034         case BT_HID_DEVICE_SEND_MOUSE_EVENT:
5035         case BT_HID_DEVICE_SEND_KEY_EVENT:
5036         case BT_HID_DEVICE_SEND_REPLY_TO_REPORT:
5037         case BT_HID_DEVICE_SEND_CUSTOM_EVENT:
5038
5039         case BT_CONNECT_LE:
5040         case BT_DISCONNECT_LE:
5041
5042         case BT_SET_ADVERTISING_DATA:
5043         case BT_SET_SCAN_RESPONSE_DATA:
5044
5045         case BT_HDP_CONNECT:
5046         case BT_HDP_DISCONNECT:
5047         case BT_HDP_SEND_DATA:
5048         case BT_HDP_REGISTER_SINK_APP:
5049         case BT_HDP_UNREGISTER_SINK_APP:
5050         case BT_HDP_GET_FD:
5051
5052         case BT_DPM_SET_ALLOW_BT_MODE:
5053         case BT_DPM_GET_ALLOW_BT_MODE:
5054         case BT_DPM_SET_DEVICE_RESTRITION:
5055         case BT_DPM_GET_DEVICE_RESTRITION:
5056         case BT_DPM_SET_UUID_RESTRITION:
5057         case BT_DPM_GET_UUID_RESTRITION:
5058         case BT_DPM_ADD_DEVICES_BLACKLIST:
5059         case BT_DPM_ADD_DEVICES_WHITELIST:
5060         case BT_DPM_ADD_UUIDS_BLACKLIST:
5061         case BT_DPM_ADD_UUIDS_WHITELIST:
5062         case BT_DPM_CLEAR_DEVICES_BLACKLIST:
5063         case BT_DPM_CLEAR_DEVICES_WHITELIST:
5064         case BT_DPM_CLEAR_UUIDS_BLACKLIST:
5065         case BT_DPM_CLEAR_UUIDS_WHITELIST:
5066         case BT_DPM_REMOVE_DEVICE_BLACKLIST:
5067         case BT_DPM_REMOVE_DEVICE_WHITELIST:
5068         case BT_DPM_REMOVE_UUID_BLACKLIST:
5069         case BT_DPM_REMOVE_UUID_WHITELIST:
5070         case BT_DPM_GET_DEVICES_BLACKLIST:
5071         case BT_DPM_GET_DEVICES_WHITELIST:
5072         case BT_DPM_GET_UUIDS_BLACKLIST:
5073         case BT_DPM_GET_UUIDS_WHITELIST:
5074         case BT_DPM_SET_ALLOW_OUTGOING_CALL:
5075         case BT_DPM_GET_ALLOW_OUTGOING_CALL:
5076         case BT_DPM_SET_PAIRING_STATE:
5077         case BT_DPM_GET_PAIRING_STATE:
5078         case BT_DPM_SET_PROFILE_STATE:
5079         case BT_DPM_GET_PROFILE_STATE:
5080         case BT_DPM_SET_DESKROP_CONNECTIVITY_STATE:
5081         case BT_DPM_GET_DESKROP_CONNECTIVITY_STATE:
5082         case BT_DPM_SET_DISCOVERABLE_STATE:
5083         case BT_DPM_GET_DISCOVERABLE_STATE:
5084         case BT_DPM_SET_LIMITED_DISCOVERABLE_STATE:
5085         case BT_DPM_GET_LIMITED_DISCOVERABLE_STATE:
5086         case BT_DPM_SET_DATA_TRANSFER_STATE:
5087         case BT_DPM_GET_DATA_TRANSFER_STATE:
5088
5089         case BT_NETWORK_ACTIVATE:
5090         case BT_NETWORK_DEACTIVATE:
5091         case BT_NETWORK_CONNECT:
5092         case BT_NETWORK_DISCONNECT:
5093         case BT_NETWORK_SERVER_DISCONNECT:
5094
5095         case BT_GATT_GET_PRIMARY_SERVICES:
5096         case BT_GATT_GET_SERVICE_PROPERTIES:             /* GATT Client */
5097         case BT_GATT_GET_CHARACTERISTIC_PROPERTIES:      /* GATT Client */
5098         case BT_GATT_WATCH_SERVICE_CHANGED_INDICATION:   /* GATT Client */
5099         case BT_GATT_DISCOVER_CHARACTERISTICS:
5100         case BT_GATT_SET_PROPERTY_REQUEST:
5101         case BT_GATT_READ_CHARACTERISTIC:
5102         case BT_GATT_READ_DESCRIPTOR_VALUE:              /* GATT Client */
5103         case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: /* GATT Client */
5104         case BT_GATT_ACQUIRE_WRITE:
5105         case BT_GATT_WRITE_DESCRIPTOR_VALUE:             /* GATT Client */
5106         case BT_GATT_WATCH_CHARACTERISTIC:               /* GATT Client */
5107         case BT_GATT_CLIENT_REGISTER:                    /* GATT Client */
5108         case BT_GATT_CLIENT_UNREGISTER:                   /* GATT Client */
5109         case BT_GATT_DISCOVER_CHARACTERISTICS_DESCRIPTOR:
5110         case BT_GATT_REGISTER_APPLICATION:
5111         case BT_GATT_REGISTER_SERVICE:
5112         case BT_GATT_SEND_RESPONSE:
5113 #ifndef GATT_DIRECT
5114         case BT_GATT_SERVER_REGISTER:
5115         case BT_GATT_SERVER_ADD_SERVICE:
5116         case BT_GATT_SERVER_ADD_CHARACTERISTIC:
5117         case BT_GATT_SERVER_ADD_DESCRIPTOR:
5118         case BT_GATT_SERVER_START_SERVICE:
5119         case BT_GATT_SERVER_STOP_SERVICE:
5120         case BT_GATT_SERVER_DELETE_SERVICE:
5121         case BT_GATT_SERVER_SEND_RESPONSE:
5122         case BT_GATT_SERVER_ACQURE_WRITE_RESPONSE:
5123         case BT_GATT_SERVER_ACQUIRE_NOTIFY_RESPONSE:
5124         case BT_GATT_SERVER_SEND_INDICATION:
5125         case BT_GATT_SERVER_UPDATE_VALUE:
5126         case BT_GATT_SERVER_DEREGISTER:
5127 #endif
5128         case BT_REQ_ATT_MTU:
5129         case BT_PBAP_CONNECT:
5130         case BT_PBAP_DISCONNECT:
5131         case BT_PBAP_GET_PHONEBOOK_SIZE:
5132         case BT_PBAP_GET_PHONEBOOK:
5133         case BT_PBAP_GET_LIST:
5134         case BT_PBAP_PULL_VCARD:
5135         case BT_PBAP_PHONEBOOK_SEARCH:
5136
5137         case BT_RFCOMM_LISTEN_AND_ACCEPT:
5138
5139         ret_val = cynara_check(p_cynara, client_creds, client_session, user_creds,
5140                                                 BT_PRIVILEGE_PUBLIC);
5141
5142         BT_INFO("Client Credentials [%s]", client_creds);
5143         if (ret_val != CYNARA_API_ACCESS_ALLOWED) {
5144                 BT_ERR("Fail to access: %s", BT_PRIVILEGE_PUBLIC);
5145                 result = FALSE;
5146         }
5147
5148         /* Need to check mediastorage privilege */
5149         if (function_name == BT_PBAP_GET_PHONEBOOK ||
5150                 function_name == BT_PBAP_PULL_VCARD) {
5151                 ret_val = cynara_check(p_cynara, client_creds, client_session, user_creds,
5152                                                         MEDIASTORAGE_PRIVILEGE);
5153
5154                 if (ret_val != CYNARA_API_ACCESS_ALLOWED) {
5155                 BT_ERR("Fail to access: %s", MEDIASTORAGE_PRIVILEGE);
5156                 result = FALSE;
5157                 }
5158         }
5159         break;
5160
5161         case BT_ENABLE_ADAPTER:
5162         case BT_DISABLE_ADAPTER:
5163         case BT_RESET_ADAPTER:
5164         case BT_RECOVER_ADAPTER:
5165         case BT_BATTERY_READ_DATA:
5166         case BT_ENABLE_ADAPTER_LE:
5167         case BT_DISABLE_ADAPTER_LE:
5168         case BT_SET_CONNECTABLE:
5169         case BT_SET_DISCOVERABLE_MODE:
5170         case BT_ADD_WHITE_LIST:
5171         case BT_REMOVE_WHITE_LIST:
5172         case BT_CLEAR_WHITE_LIST:
5173         case BT_SET_MANUFACTURER_DATA:
5174
5175         case BT_CANCEL_SEARCH_SERVICE:
5176         case BT_ENABLE_RSSI:
5177         case BT_DISCONNECT_DEVICE:
5178
5179         case BT_RFCOMM_ACCEPT_CONNECTION:
5180         case BT_RFCOMM_REJECT_CONNECTION:
5181         case BT_RFCOMM_LISTEN:
5182
5183         case BT_HID_ENABLE_BARCODE_FEATURE:
5184
5185         case BT_AVRCP_TRANSPORT_SET_PROPERTY:
5186         case BT_AVRCP_HANDLE_CONTROL_TO_DEST:
5187
5188         case BT_SET_CONTENT_PROTECT:
5189         case BT_BOND_DEVICE_BY_TYPE:
5190         case BT_SET_LE_PRIVACY:
5191         case BT_SET_LE_STATIC_RANDOM_ADDRESS:
5192         case BT_LE_CONN_UPDATE:
5193         case BT_UPDATE_LE_CONNECTION_MODE:
5194         case BT_GET_DEVICE_IDA:
5195         case BT_LE_READ_MAXIMUM_DATA_LENGTH:
5196         case BT_LE_WRITE_HOST_SUGGESTED_DATA_LENGTH:
5197         case BT_LE_READ_HOST_SUGGESTED_DATA_LENGTH:
5198         case BT_LE_SET_DATA_LENGTH:
5199         case BT_LE_OOB_READ_LOCAL_DATA:
5200         case BT_LE_OOB_ADD_REMOTE_DATA:
5201
5202         case BT_GET_GATT_DATA_BATCHING_AVAILABLE_PACKETS:
5203         case BT_ENABLE_GATT_DATA_BATCHING:
5204         case BT_DISABLE_GATT_DATA_BATCHING:
5205
5206         case BT_LE_IPSP_INIT:
5207         case BT_LE_IPSP_DEINIT:
5208         case BT_LE_IPSP_CONNECT:
5209         case BT_LE_IPSP_DISCONNECT:
5210         case BT_PXP_MONITOR_SET_PROPERTY:
5211         case BT_PXP_MONITOR_GET_PROPERTY:
5212         case BT_PXP_MONITOR_GET_SUPPORTED_SERIVCES:
5213         case BT_PXP_REPORTER_REGISTER:
5214         case BT_PXP_REPORTER_UNREGISTER:
5215         case BT_PXP_REPORTER_GET_PROPERTY:
5216
5217         /* TDS */
5218         case BT_TDS_PROVIDER_REGISTER:
5219         case BT_TDS_PROVIDER_UNREGISTER:
5220         case BT_TDS_PROVIDER_SET_MANUF_DATA:
5221         case BT_TDS_PROVIDER_CREATE:
5222         case BT_TDS_PROVIDER_DESTROY:
5223         case BT_TDS_PROVIDER_SET_TRANSPORT_DATA:
5224         case BT_TDS_SEND_ACTIVATION_RESPONSE:
5225         case BT_TDS_READ_TRANSPORT_DATA:
5226         case BT_TDS_ENABLE_CONTROL_POINT:
5227         case BT_TDS_ACTIVATE_CONTROL_POINT:
5228
5229         /* OTP Server */
5230         case BT_OTP_SERVER_INIT:
5231         case BT_OTP_SERVER_DEINIT:
5232         case BT_OTP_READ_VALUE:
5233         case BT_OTP_ENABLE_NOTIFICATION:
5234         case BT_OTP_WRITE_VALUE:
5235         case BT_LE_OTC_CONNECT:
5236         case BT_LE_OTC_DISCONNECT:
5237
5238         case BT_MAP_CREATE_SESSION:
5239         case BT_MAP_DESTROY_SESSION:
5240         case BT_MAP_SET_FOLDER:
5241         case BT_MAP_LIST_FOLDERS:
5242         case BT_MAP_LIST_FILTER_FIELDS:
5243         case BT_MAP_LIST_MESSAGES:
5244         case BT_MAP_UPDATE_INBOX:
5245         case BT_MAP_PUSH_MESSAGE:
5246         case BT_MAP_GET_MESSAGE:
5247
5248         case BT_MESH_INIT:
5249         case BT_MESH_DEINIT:
5250         case BT_MESH_NETWORK_CREATE:
5251         case BT_MESH_NETWORK_DESTROY:
5252         case BT_MESH_NETWORK_LOAD:
5253         case BT_MESH_NETWORK_UNLOAD:
5254         case BT_MESH_NETWORK_SCAN:
5255         case BT_MESH_NETWORK_CANCEL_SCAN:
5256         case BT_MESH_NETWORK_SET_CAPABILITIES:
5257         case BT_MESH_NETWORK_PROVISION_DEVICE:
5258         case BT_MESH_AUTHENTICATION_REPLY:
5259         case BT_MESH_NETWORK_SET_NAME:
5260         case BT_MESH_NETWORK_ADD_NETKEY:
5261         case BT_MESH_NETWORK_DELETE_NETKEY:
5262         case BT_MESH_NETWORK_UPDATE_NETKEY:
5263         case BT_MESH_NETWORK_ADD_APPKEY:
5264         case BT_MESH_NETWORK_DELETE_APPKEY:
5265         case BT_MESH_NETWORK_UPDATE_APPKEY:
5266         case BT_MESH_NETWORK_GET_NETKEYS:
5267         case BT_MESH_NETWORK_GET_APPKEYS:
5268         case BT_MESH_NETWORK_GET_NODES:
5269         case BT_MESH_ELEMENT_GET_MODELS:
5270         case BT_MESH_NODE_BROWSE:
5271         case BT_MESH_NODE_GET_VENDOR_FEATURES:
5272         case BT_MESH_NODE_CONFIGURE_KEY:
5273         case BT_MESH_NODE_TTL_EXECUTE:
5274         case BT_MESH_NODE_GET_NETKEYS:
5275         case BT_MESH_NODE_GET_APPKEYS:
5276         case BT_MESH_NODE_RESET:
5277         case BT_MESH_MODEL_CONFIGURE_APPKEY:
5278         case BT_MESH_MODEL_GET_APPKEY_LIST:
5279         case BT_MESH_MODEL_EXECUTE_MSG:
5280         case BT_MESH_NETWORK_CREATE_GROUP:
5281         case BT_MESH_NETWORK_REMOVE_GROUP:
5282         case BT_MESH_MODEL_CONFIG_GROUP_SUB:
5283         case BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB:
5284         case BT_MESH_MODEL_GET_SUBSCRIPTION_LIST:
5285         case BT_MESH_MODEL_SET_PUBLICATION:
5286         case BT_MESH_MODEL_GET_PUBLICATION:
5287         case BT_MESH_NETWORK_GET_GROUPS:
5288
5289         ret_val = cynara_check(p_cynara, client_creds, client_session, user_creds,
5290                                 BT_PRIVILEGE_PLATFORM);
5291
5292         if (ret_val != CYNARA_API_ACCESS_ALLOWED) {
5293                 BT_ERR("Fail to access: %s", BT_PRIVILEGE_PLATFORM);
5294                 result = FALSE;
5295         }
5296         break;
5297
5298         case BT_CHECK_ADAPTER:
5299         case BT_GET_RSSI:
5300
5301         case BT_GET_LOCAL_NAME:
5302         case BT_GET_LOCAL_ADDRESS:
5303         case BT_GET_LOCAL_VERSION:
5304         case BT_IS_SERVICE_USED:
5305         case BT_GET_DISCOVERABLE_MODE:
5306         case BT_GET_DISCOVERABLE_TIME:
5307         case BT_IS_DISCOVERYING:
5308         case BT_IS_LE_DISCOVERYING:
5309         case BT_IS_CONNECTABLE:
5310         case BT_GET_CONNECTABLE:
5311         case BT_GET_BONDED_DEVICES:
5312         case BT_GET_PROFILE_CONNECTED_DEVICES:
5313         case BT_GET_BONDED_DEVICE:
5314         case BT_PASSKEY_REPLY:
5315         case BT_PASSKEY_CONFIRMATION_REPLY:
5316         case BT_GET_IS_ALIAS_SET:
5317         case BT_IS_DEVICE_CONNECTED:
5318         case BT_GET_CONNECTED_LINK_TYPE:
5319         case BT_SET_PROFILE_TRUSTED:
5320         case BT_GET_PROFILE_TRUSTED:
5321         case BT_GET_SPEAKER_GAIN:
5322         case BT_SET_SPEAKER_GAIN:
5323         case BT_RFCOMM_CLIENT_IS_CONNECTED:
5324         case BT_RFCOMM_IS_UUID_AVAILABLE:
5325         case BT_GET_ADVERTISING_DATA:
5326         case BT_GET_SCAN_RESPONSE_DATA:
5327         case BT_IS_ADVERTISING:
5328         case BT_IS_LE_2M_PHY_SUPPORTED:
5329         case BT_IS_LE_CODED_PHY_SUPPORTED:
5330         case BT_REGISTER_SCAN_FILTER:
5331         case BT_IS_SCAN_FILTER_SUPPORTED:
5332         case BT_GET_ATT_MTU:
5333
5334         case BT_OBEX_SERVER_ALLOCATE:
5335         case BT_OBEX_SERVER_DEALLOCATE:
5336         case BT_OBEX_SERVER_IS_ACTIVATED:
5337         case BT_OPP_GET_TRANSFER_PROGRESS:
5338         case BT_AVRCP_CONTROL_GET_PROPERTY:
5339         case BT_AVRCP_GET_TRACK_INFO:
5340                 /* Non-privilege control */
5341                 break;
5342         default:
5343                 BT_ERR("Unknown function!");
5344                 result = FALSE;
5345                 break;
5346         }
5347
5348         if (function_name >= BT_FUNC_MESH_BASE) {
5349                 BT_INFO("MESH Function called");
5350                 char app_id[256] = { 0, };
5351
5352                 if (BLUETOOTH_ERROR_NONE == __bt_service_get_requester_app_id(unique_name,
5353                                         app_id, sizeof(app_id))) {
5354                         requester_unique_creds = g_strdup(app_id);
5355                 } else {
5356                         BT_DBG("Requester app_id not found, use client creds");
5357                         requester_unique_creds = g_strdup(client_creds);
5358                 }
5359                 BT_DBG("Requester unique_creds: %s", requester_unique_creds);
5360         } else {
5361                 BT_INFO("Non MESH Function called client creds [%s]", client_creds);
5362         }
5363
5364         if (client_creds)
5365                 free(client_creds);
5366
5367         if (user_creds)
5368                 free(user_creds);
5369
5370         return result;
5371 }
5372
5373 GDBusNodeInfo *__bt_service_create_method_node_info
5374                                         (const gchar *introspection_data)
5375 {
5376         GError *err = NULL;
5377         GDBusNodeInfo *node_info = NULL;
5378
5379         if (introspection_data == NULL) {
5380                 BT_ERR("Introspection XML not present");
5381                 return NULL;
5382         }
5383
5384         node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
5385
5386         if (err) {
5387                 BT_ERR("Unable to create node: %s", err->message);
5388                 g_clear_error(&err);
5389         }
5390         return node_info;
5391 }
5392
5393 int __bt_service_register_object(GDBusConnection *conn,
5394                 GDBusNodeInfo *node_info, gboolean reg)
5395 {
5396         static guint service_id = 0;
5397         GError *error = NULL;
5398
5399         if (reg) {
5400                 if (node_info == NULL)
5401                         return -1;
5402
5403                 service_id = g_dbus_connection_register_object(conn,
5404                                 BT_SERVICE_PATH,
5405                                 node_info->interfaces[0],
5406                                 &method_table,
5407                                 NULL, NULL, &error);
5408                 if (service_id == 0)
5409                         return -1;
5410         } else {
5411                 if (service_id > 0) {
5412                         g_dbus_connection_unregister_object(conn,
5413                                         service_id);
5414                         service_id = 0;
5415                 }
5416         }
5417
5418         return 0;
5419 }
5420
5421 static void __name_owner_changed(GDBusConnection *connection,
5422                 const gchar *sender_name,
5423                 const gchar *object_path,
5424                 const gchar *interface_name,
5425                 const gchar *signal_name,
5426                 GVariant *parameters,
5427                 gpointer user_data)
5428 {
5429         const char *name = NULL;
5430         const char *old_owner = NULL;
5431         const char *new_owner = NULL;
5432
5433         g_variant_get(parameters, "(&s&s&s)", &name, &old_owner, &new_owner);
5434         if (*new_owner != '\0')
5435                 return;
5436
5437         _bt_check_hdp_app_termination(name);
5438
5439         /* Advertising App Termination */
5440         _bt_check_adv_app_termination(name);
5441
5442         /* Check if le_scanning app is terminated */
5443         _bt_check_le_scanner_app_termination(name);
5444
5445         /* Check if RFCOMM app is terminated */
5446         _bt_rfcomm_check_termination(name);
5447
5448         /* Mesh App Termination */
5449         _bt_check_mesh_app_termination(name);
5450
5451         /* Stop the Transport Discovery service */
5452         _bt_tds_stop_by_terminated_process(name);
5453 }
5454
5455 static void __bt_service_bus_acquired_handler(GDBusConnection *connection,
5456                 const gchar *name, gpointer user_data)
5457 {
5458         GDBusNodeInfo *node_info = NULL;
5459
5460         BT_INFO("bus acquired");
5461
5462         ret_if(connection == NULL);
5463
5464         node_info = __bt_service_create_method_node_info(
5465                         bt_service_introspection_xml);
5466         ret_if(node_info == NULL);
5467
5468         __bt_service_register_object(connection, node_info, TRUE);
5469         g_dbus_node_info_unref(node_info);
5470
5471         bt_service_conn = connection;
5472 }
5473
5474 static void __bt_service_name_acquired_handler(GDBusConnection *connection,
5475                 const gchar *name, gpointer user_data)
5476 {
5477         BT_INFO("name acquired");
5478         name_acquired = TRUE;
5479 }
5480
5481 static void __bt_service_name_lost_handler(GDBusConnection *connection,
5482                 const gchar *name, gpointer user_data)
5483 {
5484         BT_INFO("name lost");
5485         name_acquired = FALSE;
5486 }
5487
5488 gboolean _is_name_acquired(void)
5489 {
5490         return name_acquired;
5491 }
5492
5493 void _bt_service_unref_connection(void)
5494 {
5495         BT_INFO("+");
5496
5497         if (bt_service_conn) {
5498                 g_dbus_connection_flush_sync(bt_service_conn, NULL, NULL);
5499                 g_object_unref(bt_service_conn);
5500                 bt_service_conn = NULL;
5501         }
5502
5503         BT_INFO("-");
5504 }
5505
5506 int _bt_service_register(void)
5507 {
5508         GDBusConnection *conn;
5509         GError *err = NULL;
5510
5511         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
5512         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
5513         bt_service_conn = conn;
5514
5515         owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
5516                                 BT_SERVICE_NAME,
5517                                 G_BUS_NAME_OWNER_FLAGS_NONE,
5518                                 __bt_service_bus_acquired_handler,
5519                                 __bt_service_name_acquired_handler,
5520                                 __bt_service_name_lost_handler,
5521                                 NULL, NULL);
5522         BT_DBG("owner_id is [%d]", owner_id);
5523         if (owner_id == 0)
5524                 goto fail;
5525
5526         /* Subscribe for name owner changed signal */
5527         owner_sig_id = g_dbus_connection_signal_subscribe(conn,
5528                         BT_FREEDESKTOP_SERVICE, BT_FREEDESKTOP_INTERFACE,
5529                         BT_NAME_OWNER_CHANGED, NULL, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
5530                         __name_owner_changed, NULL, NULL);
5531         BT_INFO("owner_sig_id: %d", owner_sig_id);
5532
5533         return BLUETOOTH_ERROR_NONE;
5534
5535 fail:
5536         if (bt_service_conn) {
5537                 g_object_unref(bt_service_conn);
5538                 bt_service_conn = NULL;
5539         }
5540
5541         return BLUETOOTH_ERROR_INTERNAL;
5542 }
5543
5544 void _bt_service_unregister(void)
5545 {
5546         if (bt_service_conn) {
5547                 sd_notify(0, "STOPPING=1");
5548
5549                 if (owner_sig_id > 0) {
5550                         g_dbus_connection_signal_unsubscribe(
5551                                         bt_service_conn, owner_sig_id);
5552                         owner_sig_id = 0;
5553                 }
5554
5555                 if (owner_id > 0) {
5556                         g_bus_unown_name(owner_id);
5557                         owner_id = 0;
5558                 }
5559
5560                 __bt_service_register_object(bt_service_conn, NULL, FALSE);
5561         }
5562 }
5563
5564 int _bt_service_cynara_init(void)
5565 {
5566         int result;
5567         char err_msg[256] = {0, };
5568
5569         retv_if(p_cynara != NULL, BLUETOOTH_ERROR_ALREADY_INITIALIZED);
5570
5571         result = cynara_initialize(&p_cynara, conf);
5572
5573         if (result != CYNARA_API_SUCCESS) {
5574                 cynara_strerror(result, err_msg, sizeof(err_msg));
5575                 BT_ERR("Fail to initialize cynara: [%s]", err_msg);
5576                 return BLUETOOTH_ERROR_INTERNAL;
5577         }
5578
5579         return BLUETOOTH_ERROR_NONE;
5580 }
5581
5582 void _bt_service_cynara_deinit(void)
5583 {
5584         int result;
5585         char err_msg[256] = {0, };
5586
5587         ret_if(p_cynara == NULL);
5588
5589         result = cynara_finish(p_cynara);
5590
5591         if (result != CYNARA_API_SUCCESS) {
5592                 cynara_strerror(result, err_msg, sizeof(err_msg));
5593                 BT_ERR("Fail to finish cynara: [%s]", err_msg);
5594                 return;
5595         }
5596
5597         p_cynara = NULL;
5598         conf = NULL;
5599 }
5600
5601 void _bt_service_method_return(GDBusMethodInvocation *invocation,
5602                 GArray *out_param, int result)
5603 {
5604         GVariant *out_var;
5605         BT_DBG("");
5606         out_var = g_variant_new_from_data((const GVariantType *)"ay",
5607                         out_param->data, out_param->len, TRUE, NULL, NULL);
5608
5609         g_dbus_method_invocation_return_value(invocation,
5610                         g_variant_new("(iv)", result, out_var));
5611 }
5612
5613 void _bt_service_method_return_with_unix_fd_list(GDBusMethodInvocation *invocation,
5614                 GArray *out_param, int result, GUnixFDList *fd_list)
5615 {
5616         GVariant *out_var;
5617         BT_DBG("");
5618         out_var = g_variant_new_from_data((const GVariantType *)"ay",
5619                         out_param->data, out_param->len, TRUE, NULL, NULL);
5620
5621         g_dbus_method_invocation_return_value_with_unix_fd_list(invocation,
5622                         g_variant_new("(iv)", result, out_var), fd_list);
5623 }