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