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