Mesh: Add functionality to find unique app credentials
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / services / bt-request-handler.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <string.h>
19 #include <stdio.h>
20 #include <glib.h>
21 #include <dlog.h>
22 #include <gio/gio.h>
23 #include <gio/gunixfdlist.h>
24 #include <cynara-client.h>
25 #include <cynara-creds-gdbus.h>
26 #include <systemd/sd-daemon.h>
27 #include <aul.h>
28
29 #include "bluetooth-api.h"
30 #include "bluetooth-audio-api.h"
31 #include "bluetooth-gatt-server-api.h"
32 #include "bluetooth-mesh-api.h"
33 #include "bt-request-handler.h"
34 #include "bt-service-common.h"
35 #include "bt-service-util.h"
36
37 #include "bt-service-core-adapter.h"
38 #include "bt-service-core-adapter-le.h"
39 #include "bt-service-core-device.h"
40
41 #include "bt-service-audio-common.h"
42 #include "bt-service-avrcp-tg.h"
43 #include "bt-service-avrcp-ctrl.h"
44 #include "bt-service-gatt.h"
45 #include "bt-service-mesh-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         case BT_MESH_NETWORK_CREATE: {
3369                 bluetooth_mesh_node_t node;
3370                 GSList *model_list = NULL;
3371                 int total_models = 0;
3372                 GArray *param3;
3373                 int i = 0;
3374                 const char *network_name = NULL;
3375                 bluetooth_mesh_network_t *network;
3376
3377                 memset(&node, 0x00, sizeof(bluetooth_mesh_node_t));
3378                 sender = (char*)g_dbus_method_invocation_get_sender(context);
3379
3380                 __bt_service_get_parameters(in_param1,
3381                                 &node, sizeof(bluetooth_mesh_node_t));
3382                 network_name = (const char *)g_variant_get_data(in_param2);
3383                 BT_INFO("MESH: Network name [%s]", network_name);
3384
3385                 param3 = g_array_new(TRUE, TRUE, sizeof(gchar));
3386                 __bt_fill_garray_from_variant(in_param3, param3);
3387
3388                 total_models = (param3->len) / sizeof(bluetooth_mesh_model_t);
3389                 for (i = 0; i < total_models; i++) {
3390                         bluetooth_mesh_model_t *info = NULL;
3391                         bluetooth_mesh_model_t *mod = NULL;
3392
3393                         info = &g_array_index(param3,
3394                                         bluetooth_mesh_model_t, i);
3395
3396                         mod = g_memdup(info, sizeof(bluetooth_mesh_model_t));
3397
3398                         if (mod)
3399                                 model_list = g_slist_append(model_list,
3400                                                         (gpointer)mod);
3401                 }
3402                 result = _bt_mesh_network_create(requester_unique_creds,
3403                                 sender, network_name,  &node, model_list);
3404
3405                  /* Save invocation */
3406                 if (result == BLUETOOTH_ERROR_NONE) {
3407                         BT_INFO("Mesh: Save Invoation");
3408                         network =  g_malloc0(sizeof(bluetooth_mesh_network_t));
3409
3410                         /* Save Network name & UUID, Token will be received in event */
3411                         _bt_mesh_util_convert_hex_to_string(
3412                                         (uint8_t *) node.uuid, 16,
3413                                         network->uuid,
3414                                         BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1);
3415                         BT_INFO("Mesh: Mesh Netwok UUID [%s]", network->uuid);
3416                         g_strlcpy(network->name.name, network_name,
3417                                 sizeof(network->name.name));
3418                         g_strlcpy(network->app_cred,
3419                                 requester_unique_creds,
3420                                 sizeof(network->app_cred));
3421                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3422                         _bt_save_invocation_context(context,
3423                                         result, sender,
3424                                         function_name, (gpointer)network);
3425                 } else {
3426                         g_array_append_vals(*out_param1, &node, sizeof(bluetooth_mesh_node_t));
3427                         BT_ERR("Mesh: Mesh Network creation schedule failed");
3428                         g_slist_free_full(model_list, g_free);
3429                 }
3430
3431                 /* Cleanup */
3432                 BT_INFO("Mesh: Cleanup");
3433                 g_free((gpointer)requester_unique_creds);
3434                 g_array_free(param3, TRUE);
3435                 BT_INFO("Mesh: Cleanup Done");
3436                 break;
3437         }
3438         case BT_MESH_NETWORK_LOAD: {
3439                 char *token = NULL;
3440                 bluetooth_mesh_network_t *network;
3441                 token = (char *)g_variant_get_data(in_param1);
3442                 BT_INFO("MESH: Network Load: Token string [%s]", token);
3443                 result = _bt_mesh_network_load(requester_unique_creds, sender, token);
3444                 /* Save invocation */
3445                 if (result == BLUETOOTH_ERROR_NONE) {
3446                         network =  g_malloc0(sizeof(bluetooth_mesh_network_t));
3447                         g_strlcpy(network->token.token, token, strlen(token));
3448                         g_strlcpy(network->app_cred, requester_unique_creds, strlen(requester_unique_creds));
3449
3450                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3451                         _bt_save_invocation_context(context, result, sender,
3452                                         function_name, (gpointer)network);
3453                 }
3454                 g_free((gpointer)requester_unique_creds);
3455                 break;
3456         }
3457         case BT_MESH_NETWORK_SCAN: {
3458                 bluetooth_mesh_network_t network;
3459                 bluetooth_mesh_scan_param_t param;
3460                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3461                 memset(&param, 0x00, sizeof(bluetooth_mesh_scan_param_t));
3462
3463                 __bt_service_get_parameters(in_param1,
3464                                 &network, sizeof(bluetooth_mesh_network_t));
3465
3466                 __bt_service_get_parameters(in_param2,
3467                                 &param, sizeof(bluetooth_mesh_scan_param_t));
3468
3469                 result = _bt_mesh_network_scan(requester_unique_creds,
3470                                 sender, &network, &param);
3471
3472                 if (result != BLUETOOTH_ERROR_NONE) {
3473                         g_array_append_vals(*out_param1,
3474                                 &network, sizeof(bluetooth_mesh_network_t));
3475                 } else {
3476                         bluetooth_mesh_network_t *net =  \
3477                                         g_memdup(&network, sizeof(bluetooth_mesh_network_t));
3478
3479                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3480                         _bt_save_invocation_context(context, result, sender,
3481                                         function_name, (gpointer)net);
3482                 }
3483                 break;
3484         }
3485         case BT_MESH_NETWORK_CANCEL_SCAN: {
3486                 bluetooth_mesh_network_t network;
3487                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3488
3489                 __bt_service_get_parameters(in_param1,
3490                                 &network, sizeof(bluetooth_mesh_network_t));
3491
3492                 result = _bt_mesh_network_scan_cancel(requester_unique_creds,
3493                         sender, &network);
3494
3495                 if (result != BLUETOOTH_ERROR_NONE)
3496                         g_array_append_vals(*out_param1,
3497                                 &network, sizeof(bluetooth_mesh_network_t));
3498                 break;
3499         }
3500         case BT_MESH_NETWORK_SET_CAPABILITIES: {
3501                 bluetooth_mesh_network_t network;
3502                 bluetooth_mesh_provisioner_caps_t caps;
3503                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3504                 memset(&caps, 0x00, sizeof(bluetooth_mesh_provisioner_caps_t));
3505
3506                 __bt_service_get_parameters(in_param1,
3507                                 &network, sizeof(bluetooth_mesh_network_t));
3508
3509                 __bt_service_get_parameters(in_param2,
3510                                 &caps, sizeof(bluetooth_mesh_provisioner_caps_t));
3511
3512                 result = _bt_mesh_network_set_provisioner_caps(
3513                                 requester_unique_creds, sender, &network, &caps);
3514
3515                 break;
3516         }
3517         case BT_MESH_NETWORK_PROVISION_DEVICE: {
3518                 bluetooth_mesh_provisioning_request_t req;
3519                 memset(&req, 0x00, sizeof(bluetooth_mesh_provisioning_request_t));
3520
3521                 __bt_service_get_parameters(in_param1,
3522                                 &req, sizeof(bluetooth_mesh_provisioning_request_t));
3523
3524                 BT_INFO("Mesh: Provision Device UUID [%s]", req.dev_uuid);
3525                 BT_INFO("Mesh: Provision Device in network[%s]", req.net_uuid);
3526
3527                 result = _bt_mesh_network_provision_device(
3528                                 requester_unique_creds, sender, &req);
3529
3530                 /* Save invocation */
3531                 if (result != BLUETOOTH_ERROR_NONE) {
3532                         g_array_append_vals(*out_param1, &req,
3533                                 sizeof(bluetooth_mesh_provisioning_request_t));
3534                 } else {
3535                         bluetooth_mesh_provisioning_request_t *param =  \
3536                                 g_memdup(&req, sizeof(bluetooth_mesh_provisioning_request_t));
3537
3538                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3539                         _bt_save_invocation_context(context, result, sender,
3540                                         function_name, (gpointer)param);
3541                 }
3542                 break;
3543         }
3544         case BT_MESH_AUTHENTICATION_REPLY: {
3545                 BT_INFO("Mesh: BT_MESH_AUTHENTICATION_REPLY");
3546                 const char *auth_value = NULL;
3547                 gboolean authentication_reply = FALSE;
3548                 int auth_type;
3549                 auth_value = g_variant_get_data(in_param2);
3550                 __bt_service_get_parameters(in_param3,
3551                                 &authentication_reply, sizeof(gboolean));
3552                 __bt_service_get_parameters(in_param1,
3553                                 &auth_type, sizeof(int));
3554                 BT_INFO("Mesh: Auth Reply Type [%d]", auth_type);
3555                 BT_INFO("Mesh: Auth Value [%s]", auth_value);
3556                 BT_INFO("Mesh: Auth Accept [%d]", authentication_reply);
3557                 result = _bt_mesh_authentication_reply(auth_type,
3558                                 auth_value, authentication_reply);
3559                 break;
3560         }
3561         case BT_MESH_NETWORK_SET_NAME: {
3562                 bluetooth_mesh_network_t network;
3563                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3564
3565                 __bt_service_get_parameters(in_param1,
3566                                 &network, sizeof(bluetooth_mesh_network_t));
3567
3568                 result = _bt_mesh_network_set_name(requester_unique_creds, sender, &network);
3569
3570                 break;
3571         }
3572         case BT_MESH_NETWORK_ADD_NETKEY: {
3573                 bluetooth_mesh_network_t network;
3574                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3575
3576                 __bt_service_get_parameters(in_param1,
3577                                 &network, sizeof(bluetooth_mesh_network_t));
3578
3579                 result = _bt_mesh_network_add_netkey(requester_unique_creds,
3580                         sender, &network);
3581
3582                 if (result != BLUETOOTH_ERROR_NONE) {
3583                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
3584                 } else {
3585                         bluetooth_mesh_network_t *net =  g_memdup(&network, sizeof(bluetooth_mesh_network_t));
3586
3587                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3588                         _bt_save_invocation_context(context, result, sender,
3589                                         function_name, (gpointer)net);
3590                 }
3591                 break;
3592         }
3593         case BT_MESH_NETWORK_DELETE_NETKEY: {
3594                 bluetooth_mesh_network_t network;
3595                 uint16_t index;
3596
3597                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3598
3599                 __bt_service_get_parameters(in_param1,
3600                                 &network, sizeof(bluetooth_mesh_network_t));
3601                 __bt_service_get_parameters(in_param2,
3602                                 &index, sizeof(guint16));
3603
3604                 result = _bt_mesh_network_delete_netkey(requester_unique_creds,
3605                                 sender, &network, index);
3606
3607                 if (result != BLUETOOTH_ERROR_NONE) {
3608                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
3609                 } else {
3610                         bluetooth_mesh_network_t *net =  g_memdup(&network, sizeof(bluetooth_mesh_network_t));
3611
3612                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3613                         _bt_save_invocation_context(context, result, sender,
3614                                         function_name, (gpointer)net);
3615                 }
3616                 break;
3617         }
3618         case BT_MESH_NETWORK_UPDATE_NETKEY: {
3619                 bluetooth_mesh_network_t network;
3620                 uint16_t index;
3621
3622                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3623
3624                 __bt_service_get_parameters(in_param1,
3625                                 &network, sizeof(bluetooth_mesh_network_t));
3626                 __bt_service_get_parameters(in_param2,
3627                                 &index, sizeof(guint16));
3628
3629                 result = _bt_mesh_network_update_netkey(requester_unique_creds,
3630                                 sender, &network, index);
3631
3632                 if (result != BLUETOOTH_ERROR_NONE) {
3633                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
3634                 } else {
3635                         bluetooth_mesh_network_t *net =  g_memdup(&network, sizeof(bluetooth_mesh_network_t));
3636
3637                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3638                         _bt_save_invocation_context(context, result, sender,
3639                                         function_name, (gpointer)net);
3640                 }
3641                 break;
3642         }
3643         case BT_MESH_NETWORK_ADD_APPKEY: {
3644                 bluetooth_mesh_network_t network;
3645                 uint16_t net_idx;
3646
3647                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3648
3649                 __bt_service_get_parameters(in_param1,
3650                                 &network, sizeof(bluetooth_mesh_network_t));
3651                 __bt_service_get_parameters(in_param2,
3652                                 &net_idx, sizeof(guint16));
3653
3654                 result = _bt_mesh_network_add_appkey(requester_unique_creds,
3655                                 sender, &network, net_idx);
3656
3657                 if (result != BLUETOOTH_ERROR_NONE) {
3658                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
3659                 } else {
3660                         bluetooth_mesh_network_t *net =  g_memdup(&network, sizeof(bluetooth_mesh_network_t));
3661
3662                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3663                         _bt_save_invocation_context(context, result, sender,
3664                                         function_name, (gpointer)net);
3665                 }
3666                 break;
3667         }
3668         case BT_MESH_NETWORK_UPDATE_APPKEY: {
3669                 bluetooth_mesh_network_t network;
3670                 uint16_t net_idx;
3671                 uint16_t app_idx;
3672
3673                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3674
3675                 __bt_service_get_parameters(in_param1,
3676                                 &network, sizeof(bluetooth_mesh_network_t));
3677                 __bt_service_get_parameters(in_param2,
3678                                 &net_idx, sizeof(guint16));
3679                 __bt_service_get_parameters(in_param3,
3680                                 &app_idx, sizeof(guint16));
3681
3682                 result = _bt_mesh_network_update_appkey(requester_unique_creds,
3683                                 sender, &network, net_idx, app_idx);
3684
3685                 if (result != BLUETOOTH_ERROR_NONE) {
3686                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
3687                 } else {
3688                         bluetooth_mesh_network_t *net =  g_memdup(&network, sizeof(bluetooth_mesh_network_t));
3689
3690                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3691                         _bt_save_invocation_context(context, result, sender,
3692                                         function_name, (gpointer)net);
3693                 }
3694                 break;
3695         }
3696         case BT_MESH_NETWORK_DELETE_APPKEY: {
3697                 bluetooth_mesh_network_t network;
3698                 uint16_t net_idx;
3699                 uint16_t app_idx;
3700
3701                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3702
3703                 __bt_service_get_parameters(in_param1,
3704                                 &network, sizeof(bluetooth_mesh_network_t));
3705                 __bt_service_get_parameters(in_param2,
3706                                 &net_idx, sizeof(guint16));
3707                 __bt_service_get_parameters(in_param3,
3708                                 &app_idx, sizeof(guint16));
3709
3710                 result = _bt_mesh_network_delete_appkey(requester_unique_creds,
3711                                 sender, &network, net_idx, app_idx);
3712
3713                 if (result != BLUETOOTH_ERROR_NONE) {
3714                         g_array_append_vals(*out_param1, &network, sizeof(bluetooth_mesh_network_t));
3715                 } else {
3716                         bluetooth_mesh_network_t *net =  g_memdup(&network, sizeof(bluetooth_mesh_network_t));
3717
3718                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3719                         _bt_save_invocation_context(context, result, sender,
3720                                         function_name, (gpointer)net);
3721                 }
3722                 break;
3723         }
3724         case BT_MESH_NETWORK_GET_NETKEYS: {
3725                 bluetooth_mesh_network_t network;
3726                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3727                 __bt_service_get_parameters(in_param1,
3728                                 &network, sizeof(bluetooth_mesh_network_t));
3729                 result = _bt_mesh_network_get_netkeys(requester_unique_creds,
3730                                 sender, &network, out_param1);
3731                 break;
3732         }
3733         case BT_MESH_NETWORK_GET_APPKEYS: {
3734                 bluetooth_mesh_network_t network;
3735                 uint16_t net_idx;
3736
3737                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3738                 __bt_service_get_parameters(in_param1,
3739                                 &network, sizeof(bluetooth_mesh_network_t));
3740                 __bt_service_get_parameters(in_param2,
3741                                 &net_idx, sizeof(guint16));
3742                 result = _bt_mesh_network_get_appkeys(
3743                                 requester_unique_creds, sender,
3744                                         &network, net_idx, out_param1);
3745                 break;
3746         }
3747         case BT_MESH_NETWORK_GET_NODES: {
3748                 bluetooth_mesh_network_t network;
3749                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3750                 __bt_service_get_parameters(in_param1,
3751                                 &network, sizeof(bluetooth_mesh_network_t));
3752                 result = _bt_mesh_network_get_nodes(
3753                                 requester_unique_creds, sender,
3754                                         &network, out_param1);
3755                 break;
3756         }
3757         case BT_MESH_ELEMENT_GET_MODELS: {
3758                 bluetooth_mesh_network_t network;
3759                 uint16_t node_addr;
3760                 int elem_idx;
3761
3762                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
3763                 __bt_service_get_parameters(in_param1,
3764                                 &network, sizeof(bluetooth_mesh_network_t));
3765                 __bt_service_get_parameters(in_param2,
3766                                 &node_addr, sizeof(guint16));
3767                 __bt_service_get_parameters(in_param3,
3768                                 &elem_idx, sizeof(int));
3769                 result = _bt_mesh_element_get_models(requester_unique_creds, sender,
3770                                 &network, node_addr, elem_idx, out_param1);
3771                 break;
3772         }
3773         case BT_MESH_NODE_BROWSE: {
3774                 bluetooth_mesh_node_discover_t req;
3775                 memset(&req, 0x00, sizeof(bluetooth_mesh_node_discover_t));
3776
3777                 __bt_service_get_parameters(in_param1,
3778                                 &req, sizeof(bluetooth_mesh_node_discover_t));
3779
3780                 result = _bt_mesh_browse_remote_node(
3781                                 requester_unique_creds, sender, &req);
3782
3783                 /* Save invocation */
3784                 if (result != BLUETOOTH_ERROR_NONE) {
3785                         g_array_append_vals(*out_param1, &req,
3786                                 sizeof(bluetooth_mesh_node_discover_t));
3787                 } else {
3788                         bluetooth_mesh_node_discover_t *param = \
3789                                 g_memdup(&req, sizeof(bluetooth_mesh_node_discover_t));
3790
3791                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3792                         _bt_save_invocation_context(context, result, sender,
3793                                         function_name, (gpointer)param);
3794                 }
3795                 break;
3796         }
3797         case BT_MESH_NODE_GET_VENDOR_FEATURES: {
3798                 bluetooth_mesh_node_features_t req;
3799                 memset(&req, 0x00, sizeof(bluetooth_mesh_node_features_t));
3800
3801                 __bt_service_get_parameters(in_param1,
3802                                 &req, sizeof(bluetooth_mesh_node_features_t));
3803
3804                 result = _bt_mesh_node_discover_vendor_features(
3805                                 requester_unique_creds, sender, &req);
3806
3807                 /* Save invocation */
3808                 if (result != BLUETOOTH_ERROR_NONE) {
3809                         g_array_append_vals(*out_param1,
3810                                 &req, sizeof(bluetooth_mesh_node_features_t));
3811                 } else {
3812                         bluetooth_mesh_node_features_t *param =  \
3813                                 g_memdup(&req, sizeof(bluetooth_mesh_node_features_t));
3814
3815                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3816                         _bt_save_invocation_context(context, result, sender,
3817                                         function_name, (gpointer)param);
3818                 }
3819                 break;
3820         }
3821         case BT_MESH_NODE_CONFIGURE_KEY: {
3822                 bluetooth_mesh_key_configure_t req;
3823
3824                 memset(&req, 0x00, sizeof(bluetooth_mesh_key_configure_t));
3825
3826                 __bt_service_get_parameters(in_param1,
3827                                 &req, sizeof(bluetooth_mesh_key_configure_t));
3828
3829                 result = _bt_mesh_node_configure_key(
3830                                 requester_unique_creds, sender, &req);
3831
3832                 /* Save invocation */
3833                 if (result != BLUETOOTH_ERROR_NONE) {
3834                         g_array_append_vals(*out_param1,
3835                                 &req, sizeof(bluetooth_mesh_key_configure_t));
3836                 } else {
3837                         bluetooth_mesh_key_configure_t *param = \
3838                                 g_memdup(&req, sizeof(bluetooth_mesh_key_configure_t));
3839
3840                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3841                         _bt_save_invocation_context(context, result, sender,
3842                                         function_name, (gpointer)param);
3843                 }
3844                 break;
3845         }
3846         case BT_MESH_NODE_TTL_EXECUTE: {
3847                 bluetooth_mesh_node_ttl_info_t req;
3848
3849                 memset(&req, 0x00, sizeof(bluetooth_mesh_node_ttl_info_t));
3850
3851                 __bt_service_get_parameters(in_param1,
3852                                 &req, sizeof(bluetooth_mesh_node_ttl_info_t));
3853
3854                 result = _bt_mesh_ttl_execute_remote_node(
3855                                 requester_unique_creds, sender, &req);
3856
3857                 /* Save invocation */
3858                 if (result != BLUETOOTH_ERROR_NONE) {
3859                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_node_ttl_info_t));
3860                 } else {
3861                         bluetooth_mesh_node_ttl_info_t *param =  \
3862                                 g_memdup(&req, sizeof(bluetooth_mesh_node_ttl_info_t));
3863
3864                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3865                         _bt_save_invocation_context(context, result, sender,
3866                                         function_name, (gpointer)param);
3867                 }
3868                 break;
3869         }
3870         case BT_MESH_MODEL_CONFIGURE_APPKEY: {
3871                 bluetooth_mesh_model_configure_t req;
3872
3873                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
3874
3875                 __bt_service_get_parameters(in_param1,
3876                                 &req, sizeof(bluetooth_mesh_model_configure_t));
3877
3878                 result = _bt_mesh_node_model_appkey_execute(
3879                                 requester_unique_creds, sender, &req);
3880
3881                 /* Save invocation */
3882                 if (result != BLUETOOTH_ERROR_NONE) {
3883                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_configure_t));
3884                 } else {
3885                         bluetooth_mesh_model_configure_t *param =  \
3886                                 g_memdup(&req, sizeof(bluetooth_mesh_model_configure_t));
3887
3888                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3889                         _bt_save_invocation_context(context, result, sender,
3890                                         function_name, (gpointer)param);
3891                 }
3892                 break;
3893         }
3894         case BT_MESH_NODE_GET_NETKEYS: {
3895                 bluetooth_mesh_node_discover_t node;
3896                 memset(&node, 0x00, sizeof(bluetooth_mesh_node_discover_t));
3897                 __bt_service_get_parameters(in_param1,
3898                                 &node, sizeof(bluetooth_mesh_node_discover_t));
3899                 result = _bt_mesh_network_node_get_netkeys(
3900                                 requester_unique_creds, &node, out_param1);
3901                 break;
3902         }
3903         case BT_MESH_NODE_GET_APPKEYS: {
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_appkeys(
3909                                 requester_unique_creds, sender, &node, out_param1);
3910                 break;
3911         }
3912         case BT_MESH_MODEL_GET_APPKEY_LIST: {
3913                 bluetooth_mesh_model_configure_t req;
3914
3915                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
3916
3917                 __bt_service_get_parameters(in_param1,
3918                                 &req, sizeof(bluetooth_mesh_model_configure_t));
3919
3920                 result = _bt_mesh_node_model_get_appkey_list(
3921                                         requester_unique_creds, sender, &req);
3922
3923                 /* Save invocation */
3924                 if (result != BLUETOOTH_ERROR_NONE) {
3925                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_configure_t));
3926                 } else {
3927                         bluetooth_mesh_model_configure_t *param = \
3928                                 g_memdup(&req, sizeof(bluetooth_mesh_model_configure_t));
3929
3930                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3931                         _bt_save_invocation_context(context, result, sender,
3932                                         function_name, (gpointer)param);
3933                 }
3934                 break;
3935         }
3936         case BT_MESH_MODEL_GET_SUBSCRIPTION_LIST: {
3937                 bluetooth_mesh_model_configure_t req;
3938
3939                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
3940
3941                 __bt_service_get_parameters(in_param1,
3942                                 &req, sizeof(bluetooth_mesh_model_configure_t));
3943
3944                 result = _bt_mesh_node_model_get_subscription_list(
3945                                 requester_unique_creds, sender, &req);
3946
3947                 /* Save invocation */
3948                 if (result != BLUETOOTH_ERROR_NONE) {
3949                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_configure_t));
3950                 } else {
3951                         bluetooth_mesh_model_configure_t *param =  \
3952                                 g_memdup(&req, sizeof(bluetooth_mesh_model_configure_t));
3953
3954                         sender = (char*)g_dbus_method_invocation_get_sender(context);
3955                         _bt_save_invocation_context(context, result, sender,
3956                                         function_name, (gpointer)param);
3957                 }
3958
3959                 break;
3960         }
3961         case BT_MESH_NETWORK_CREATE_GROUP: {
3962                 bluetooth_mesh_network_t net;
3963                 bluetooth_mesh_network_group_info_t res;
3964                 gboolean is_virtual;
3965                 uint16_t grp_addr;
3966
3967                 memset(&net, 0x00, sizeof(bluetooth_mesh_network_t));
3968                 memset(&res, 0x00, sizeof(bluetooth_mesh_network_group_info_t));
3969
3970                 __bt_service_get_parameters(in_param1,
3971                                 &net, sizeof(bluetooth_mesh_network_t));
3972                 __bt_service_get_parameters(in_param2,
3973                                 &is_virtual, sizeof(gboolean));
3974                 __bt_service_get_parameters(in_param3,
3975                                 &grp_addr, sizeof(guint16));
3976
3977                 result = _bt_mesh_network_create_group(
3978                                 requester_unique_creds, sender, &net,
3979                                         is_virtual, grp_addr, &res);
3980
3981                 if (result == BLUETOOTH_ERROR_NONE)
3982                         g_array_append_vals(*out_param1, &res,
3983                                 sizeof(bluetooth_mesh_network_group_info_t));
3984                 break;
3985         }
3986         case BT_MESH_MODEL_CONFIG_GROUP_SUB: {
3987                 bluetooth_mesh_model_configure_t req;
3988
3989                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
3990
3991                 __bt_service_get_parameters(in_param1,
3992                                 &req, sizeof(bluetooth_mesh_model_configure_t));
3993
3994                 result = _bt_mesh_model_configure_group_subscription(
3995                                 requester_unique_creds, sender, &req);
3996
3997                 /* Save invocation */
3998                 if (result != BLUETOOTH_ERROR_NONE) {
3999                         g_array_append_vals(*out_param1,
4000                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4001                 } else {
4002                         bluetooth_mesh_model_configure_t *param =  \
4003                                 g_memdup(&req, sizeof(bluetooth_mesh_model_configure_t));
4004
4005                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4006                         _bt_save_invocation_context(context, result, sender,
4007                                         function_name, (gpointer)param);
4008                 }
4009                 break;
4010         }
4011         case BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB: {
4012                 bluetooth_mesh_model_configure_t req;
4013
4014                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
4015
4016                 __bt_service_get_parameters(in_param1,
4017                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4018
4019                 result = _bt_mesh_model_configure_virtual_group_subscription(
4020                                 requester_unique_creds, sender, &req);
4021
4022                 /* Save invocation */
4023                 if (result != BLUETOOTH_ERROR_NONE) {
4024                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_configure_t));
4025                 } else {
4026                         bluetooth_mesh_model_configure_t *param =  \
4027                                 g_memdup(&req, sizeof(bluetooth_mesh_model_configure_t));
4028
4029                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4030                         _bt_save_invocation_context(context, result, sender,
4031                                         function_name, (gpointer)param);
4032                 }
4033                 break;
4034         }
4035         case BT_MESH_MODEL_SET_PUBLICATION: {
4036                 bluetooth_mesh_model_configure_t req;
4037
4038                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
4039
4040                 __bt_service_get_parameters(in_param1,
4041                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4042
4043                 result = _bt_mesh_model_set_publication(
4044                                 requester_unique_creds, sender, &req);
4045
4046                 /* Save invocation */
4047                 if (result != BLUETOOTH_ERROR_NONE) {
4048                         g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_configure_t));
4049                 } else {
4050                         bluetooth_mesh_model_configure_t *param = g_memdup(&req, sizeof(bluetooth_mesh_model_configure_t));
4051
4052                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4053                         _bt_save_invocation_context(context, result, sender,
4054                                         function_name, (gpointer)param);
4055                 }
4056                 break;
4057         }
4058         case BT_MESH_MODEL_GET_PUBLICATION: {
4059                 bluetooth_mesh_model_configure_t req;
4060
4061                 memset(&req, 0x00, sizeof(bluetooth_mesh_model_configure_t));
4062
4063                 __bt_service_get_parameters(in_param1,
4064                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4065
4066                 result = _bt_mesh_model_get_publication(
4067                                         requester_unique_creds, sender, &req);
4068
4069                 /* Save invocation */
4070                 if (result != BLUETOOTH_ERROR_NONE) {
4071                         g_array_append_vals(*out_param1,
4072                                 &req, sizeof(bluetooth_mesh_model_configure_t));
4073                 } else {
4074                         bluetooth_mesh_model_configure_t *param =  \
4075                                 g_memdup(&req, sizeof(bluetooth_mesh_model_configure_t));
4076
4077                         sender = (char*)g_dbus_method_invocation_get_sender(context);
4078                         _bt_save_invocation_context(context, result, sender,
4079                                         function_name, (gpointer)param);
4080                 }
4081                 break;
4082         }
4083         case BT_MESH_NETWORK_GET_GROUPS: {
4084                 bluetooth_mesh_network_t network;
4085                 memset(&network, 0x00, sizeof(bluetooth_mesh_network_t));
4086                 __bt_service_get_parameters(in_param1,
4087                                 &network, sizeof(bluetooth_mesh_network_t));
4088                 result = _bt_mesh_network_get_groups(
4089                                 requester_unique_creds, sender, &network, out_param1);
4090                 break;
4091         }
4092         default:
4093                 BT_INFO("UnSupported function [%d]", function_name);
4094                 result = BLUETOOTH_ERROR_NOT_SUPPORT;
4095                 break;
4096         }
4097
4098         return result;
4099 }
4100 #ifdef TIZEN_FEATURE_BT_OBEX
4101 /* Function definitions*/
4102 static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
4103 {
4104         char *data;
4105         int size;
4106
4107         size = g_variant_get_size(var);
4108         if (size > 0) {
4109                 data = (char *)g_variant_get_data(var);
4110                 if (data)
4111                         param = g_array_append_vals(param, data, size);
4112
4113         }
4114 }
4115 #endif
4116
4117 int __bt_obexd_request(int function_name,
4118                 int request_type,
4119                 int request_id,
4120                 GDBusMethodInvocation *context,
4121                 GVariant *in_param1,
4122                 GVariant *in_param2,
4123                 GVariant *in_param3,
4124                 GVariant *in_param4,
4125                 GArray **out_param1)
4126 {
4127         BT_DBG("+");
4128
4129         int result = BLUETOOTH_ERROR_NONE;
4130
4131         BT_DBG("function_name : %x", function_name);
4132
4133         switch (function_name) {
4134         case BT_OPP_PUSH_FILES: {
4135                 BT_DBG("BT_OPP_PUSH_FILES");
4136                 int i;
4137                 bluetooth_device_address_t address = { {0} };
4138                 bt_file_path_t *path;
4139                 char **file_path;
4140                 int file_count;
4141                 GArray *param2;
4142
4143                 __bt_service_get_parameters(in_param1, &address,
4144                                 sizeof(bluetooth_device_address_t));
4145                 __bt_service_get_parameters(in_param3, &file_count,
4146                                 sizeof(int));
4147
4148                 file_path = g_new0(char *, file_count + 1);
4149
4150                 param2 = g_array_new(TRUE, TRUE, sizeof(gchar));
4151                 __bt_fill_garray_from_variant(in_param2, param2);
4152
4153                 for (i = 0; i < file_count; i++) {
4154                         path = &g_array_index(param2, bt_file_path_t, i);
4155                         file_path[i] = g_strdup(path->path);
4156                 }
4157                 BT_DBG("_bt_opp_client_push_files");
4158                 result = _bt_opp_client_push_files(request_id, context,
4159                                                 &address, file_path,
4160                                                 file_count);
4161
4162                 for (i = 0; i < file_count; i++)
4163                         g_free(file_path[i]);
4164
4165                 g_free(file_path);
4166                 g_array_free(param2, TRUE);
4167
4168                 break;
4169         }
4170         case BT_OPP_CANCEL_PUSH: {
4171                 result = _bt_opp_client_cancel_push();
4172
4173                 break;
4174         }
4175         case BT_OPP_IS_PUSHING_FILES: {
4176                 gboolean is_sending = FALSE;
4177
4178                 result = _bt_opp_client_is_sending(&is_sending);
4179
4180                 g_array_append_vals(*out_param1, &is_sending,
4181                                 sizeof(gboolean));
4182                 break;
4183         }
4184         case BT_OPP_GET_TRANSFER_PROGRESS: {
4185                 int direction;
4186                 int transfer_id;
4187                 guint8 progress = 0;
4188
4189                 __bt_service_get_parameters(in_param1, &direction,
4190                                 sizeof(int));
4191                 __bt_service_get_parameters(in_param2, &transfer_id,
4192                                 sizeof(int));
4193                 if (direction)
4194                         result = _bt_opp_get_client_progress(&progress);
4195                 else
4196                         result = _bt_opp_get_server_progress(transfer_id, &progress);
4197
4198                 g_array_append_vals(*out_param1, &progress,
4199                                 sizeof(guint8));
4200
4201                 break;
4202         }
4203
4204         case BT_MAP_CREATE_SESSION: {
4205                 BT_DBG("BT_MAP_CREATE_SESSION");
4206                 char *address = (char *)g_variant_get_data(in_param1);
4207                 char *session_id = NULL;
4208                 result = _bt_create_session_sync(address, &session_id);
4209                 if (result == BLUETOOTH_ERROR_NONE)
4210                         g_array_append_vals(*out_param1, session_id, strlen(session_id)+1);
4211                 break;
4212         }
4213
4214         case BT_MAP_DESTROY_SESSION: {
4215                 BT_DBG("BT_MAP_DESTROY_SESSION");
4216                 char* session_id = (char *)g_variant_get_data(in_param1);
4217                 result = _bt_destroy_session_sync(session_id);
4218                 if (result == BLUETOOTH_ERROR_NONE)
4219                         BT_DBG("successfully destroyed session");
4220                 break;
4221         }
4222
4223         case BT_MAP_SET_FOLDER: {
4224                 BT_DBG("BT_MAP_SET_FOLDER");
4225                 char *session_id = (char *)g_variant_get_data(in_param1);
4226                 char *name = (char *)g_variant_get_data(in_param2);
4227                 result = _bt_map_client_set_folder(session_id, name);
4228                 break;
4229         }
4230
4231         case BT_MAP_LIST_FOLDERS: {
4232                 BT_DBG("BT_MAP_LIST_FOLDERS");
4233
4234                 char* session_id = (char *)g_variant_get_data(in_param1);
4235                 char* filter_serialized = (char*)g_variant_get_data(in_param2);
4236
4237                 result = _bt_map_client_list_folders(request_id, context, session_id, filter_serialized);
4238                 if (result == BLUETOOTH_ERROR_NONE)
4239                         BT_DBG("_bt_map_client_list_folders succeed");
4240
4241                 break;
4242         }
4243
4244         case BT_MAP_LIST_FILTER_FIELDS: {
4245                 BT_DBG("BT_MAP_LIST_FILTER_FIELDS");
4246
4247                 char* session_id = (char *)g_variant_get_data(in_param1);
4248
4249                 result = _bt_map_client_list_filter_fields(request_id, context, session_id);
4250                 if (result == BLUETOOTH_ERROR_NONE)
4251                         BT_DBG("_bt_map_client_list_filter_fields succeed");
4252
4253                 break;
4254         }
4255
4256         case BT_MAP_LIST_MESSAGES: {
4257                 BT_DBG("BT_MAP_LIST_MESSAGES");
4258
4259                 char* session_id = (char*)g_variant_get_data(in_param1);
4260                 char* folder = (char*)g_variant_get_data(in_param2);
4261                 char* filter_serialized = (char*)g_variant_get_data(in_param3);
4262
4263                 result = _bt_map_client_list_messages(request_id, context, session_id, folder, filter_serialized);
4264                 if (result == BLUETOOTH_ERROR_NONE)
4265                         BT_DBG("_bt_map_client_list_messages succeed");
4266                 else
4267                         BT_DBG("_bt_map_client_list_messages failed");
4268
4269                 break;
4270         }
4271
4272         case BT_MAP_UPDATE_INBOX: {
4273                 BT_DBG("BT_MAP_UPDATE_INBOX");
4274                 char* session_id = (char *)g_variant_get_data(in_param1);
4275                 result = _bt_map_client_update_inbox(session_id);
4276                 break;
4277         }
4278
4279         case BT_MAP_PUSH_MESSAGE: {
4280                 BT_DBG("BT_MAP_PUSH_MESSAGE");
4281
4282                 char* session_id = (char *)g_variant_get_data(in_param1);
4283                 char* source_file = (char *)g_variant_get_data(in_param2);
4284                 char* folder = (char *)g_variant_get_data(in_param3);
4285                 char* args_serialized = (char *)g_variant_get_data(in_param4);
4286
4287                 result = _bt_map_client_push_message(
4288                                 request_id, context, session_id, source_file, folder, args_serialized);
4289                 if (result == BLUETOOTH_ERROR_NONE)
4290                         BT_DBG("_bt_map_client_push_message succeed");
4291                 else
4292                         BT_ERR("_bt_map_client_push_message failed");
4293
4294                 break;
4295         }
4296
4297         case BT_MAP_GET_MESSAGE: {
4298                 BT_DBG("BT_MAP_GET_MESSAGE");
4299                 // TODO session currently is not used, but should be valid
4300                 //char* session_id = (char *)g_variant_get_data(in_param1);
4301                 char* message_object = (char *)g_variant_get_data(in_param2);
4302                 char* target_file = (char *)g_variant_get_data(in_param3);
4303                 bool attachment = false;
4304                 __bt_service_get_parameters(in_param4, &attachment, sizeof(bool));
4305
4306                 result = _bt_map_client_get_message(request_id, context, message_object,
4307                                 target_file, attachment);
4308                 if (result == BLUETOOTH_ERROR_NONE)
4309                         BT_DBG("_bt_map_client_get_message succeed");
4310
4311                 break;
4312         }
4313
4314         case BT_OBEX_SERVER_ALLOCATE: {
4315                 int app_pid;
4316                 gboolean is_native;
4317                 char *path;
4318                 char *sender;
4319
4320                 sender = (char *)g_dbus_method_invocation_get_sender(context);
4321
4322                 path = (char *)g_variant_get_data(in_param1);
4323                 __bt_service_get_parameters(in_param2, &is_native,
4324                                 sizeof(gboolean));
4325                 __bt_service_get_parameters(in_param3, &app_pid,
4326                                 sizeof(int));
4327
4328                 result = _bt_obex_server_allocate(sender,
4329                                 path, app_pid, is_native);
4330
4331                 break;
4332         }
4333         case BT_OBEX_SERVER_DEALLOCATE: {
4334                 int app_pid;
4335                 gboolean is_native;
4336
4337                 __bt_service_get_parameters(in_param1, &is_native,
4338                                 sizeof(gboolean));
4339                 __bt_service_get_parameters(in_param2, &app_pid,
4340                                 sizeof(int));
4341
4342                 result = _bt_obex_server_deallocate(app_pid, is_native);
4343                 break;
4344         }
4345         case BT_OBEX_SERVER_IS_ACTIVATED: {
4346                 gboolean is_activated = FALSE;
4347
4348                 result = _bt_obex_server_is_activated(&is_activated);
4349
4350                 g_array_append_vals(*out_param1, &is_activated,
4351                                 sizeof(gboolean));
4352
4353                 break;
4354         }
4355         case BT_OBEX_SERVER_ACCEPT_CONNECTION: {
4356                 result = _bt_obex_server_accept_connection(request_id);
4357
4358                 break;
4359         }
4360         case BT_OBEX_SERVER_REJECT_CONNECTION: {
4361                 result = _bt_obex_server_reject_connection();
4362
4363                 break;
4364         }
4365         case BT_OBEX_SERVER_ACCEPT_FILE: {
4366                 char *file_name;
4367
4368                 file_name = (char *)g_variant_get_data(in_param1);
4369
4370                 result = _bt_obex_server_accept_authorize(file_name, TRUE);
4371
4372                 break;
4373         }
4374         case BT_OBEX_SERVER_REJECT_FILE: {
4375                 result = _bt_obex_server_reject_authorize();
4376
4377                 break;
4378         }
4379         case BT_OBEX_SERVER_SET_PATH: {
4380                 gboolean is_native;
4381                 char *destination_path;
4382
4383                 destination_path = (char *)g_variant_get_data(in_param1);
4384                 __bt_service_get_parameters(in_param2, &is_native,
4385                                 sizeof(gboolean));
4386
4387                 result = _bt_obex_server_set_destination_path(destination_path,
4388                                                         is_native);
4389
4390                 break;
4391         }
4392         case BT_OBEX_SERVER_SET_ROOT: {
4393                 char *root;
4394
4395                 root = (char *)g_variant_get_data(in_param1);
4396
4397                 result = _bt_obex_server_set_root(root);
4398
4399                 break;
4400         }
4401         case BT_OBEX_SERVER_CANCEL_TRANSFER: {
4402                 int transfer_id;
4403
4404                 __bt_service_get_parameters(in_param1, &transfer_id,
4405                                 sizeof(int));
4406
4407                 result = _bt_obex_server_cancel_transfer(transfer_id);
4408
4409                 break;
4410         }
4411         case BT_OBEX_SERVER_CANCEL_ALL_TRANSFERS: {
4412                 result = _bt_obex_server_cancel_all_transfers();
4413
4414                 break;
4415         }
4416         case BT_OBEX_SERVER_IS_RECEIVING: {
4417                 gboolean is_receiving = FALSE;
4418
4419                 result = _bt_obex_server_is_receiving(&is_receiving);
4420
4421                 g_array_append_vals(*out_param1, &is_receiving,
4422                                 sizeof(gboolean));
4423                 break;
4424         }
4425         case BT_PBAP_CONNECT: {
4426                 bluetooth_device_address_t address = { {0} };
4427
4428                 __bt_service_get_parameters(in_param1, &address,
4429                                 sizeof(bluetooth_device_address_t));
4430
4431                 result = _bt_pbap_connect(&address);
4432                 break;
4433         }
4434         case BT_PBAP_DISCONNECT: {
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_disconnect(&address);
4441                 break;
4442         }
4443         case BT_PBAP_GET_PHONEBOOK_SIZE: {
4444                 bluetooth_device_address_t address = { {0} };
4445                 bt_pbap_folder_t folder = { 0, };
4446
4447                 __bt_service_get_parameters(in_param1, &address,
4448                                 sizeof(bluetooth_device_address_t));
4449                 __bt_service_get_parameters(in_param2, &folder,
4450                                 sizeof(bt_pbap_folder_t));
4451
4452                 result = _bt_pbap_get_phonebook_size(&address,
4453                                 folder.addressbook, folder.folder_type);
4454                 break;
4455         }
4456         case BT_PBAP_GET_PHONEBOOK: {
4457                 bluetooth_device_address_t address = { {0} };
4458                 bt_pbap_folder_t folder = { 0, };
4459                 bt_pbap_pull_parameters_t app_param = { 0, };
4460
4461                 __bt_service_get_parameters(in_param1, &address,
4462                                 sizeof(bluetooth_device_address_t));
4463                 __bt_service_get_parameters(in_param2, &folder,
4464                                 sizeof(bt_pbap_folder_t));
4465                 __bt_service_get_parameters(in_param3, &app_param,
4466                                 sizeof(bt_pbap_pull_parameters_t));
4467
4468                 result = _bt_pbap_get_phonebook(&address, folder.addressbook,
4469                                 folder.folder_type, &app_param);
4470                 break;
4471         }
4472         case BT_PBAP_GET_LIST: {
4473                 bluetooth_device_address_t address = { {0} };
4474                 bt_pbap_folder_t folder = { 0, };
4475                 bt_pbap_list_parameters_t app_param = { 0, };
4476
4477                 __bt_service_get_parameters(in_param1, &address,
4478                                 sizeof(bluetooth_device_address_t));
4479                 __bt_service_get_parameters(in_param2, &folder,
4480                                 sizeof(bt_pbap_folder_t));
4481                 __bt_service_get_parameters(in_param3, &app_param,
4482                                 sizeof(bt_pbap_list_parameters_t));
4483
4484                 result = _bt_pbap_get_list(&address, folder.addressbook,
4485                                 folder.folder_type, &app_param);
4486                 break;
4487         }
4488         case BT_PBAP_PULL_VCARD: {
4489                 bluetooth_device_address_t address = { {0} };
4490                 bt_pbap_folder_t folder = { 0, };
4491                 bt_pbap_pull_vcard_parameters_t app_param = { 0, };
4492
4493                 __bt_service_get_parameters(in_param1, &address,
4494                                 sizeof(bluetooth_device_address_t));
4495                 __bt_service_get_parameters(in_param2, &folder,
4496                                 sizeof(bt_pbap_folder_t));
4497                 __bt_service_get_parameters(in_param3, &app_param,
4498                                 sizeof(bt_pbap_pull_vcard_parameters_t));
4499
4500                 result = _bt_pbap_pull_vcard(&address, folder.addressbook,
4501                                 folder.folder_type, &app_param);
4502                 break;
4503         }
4504         case BT_PBAP_PHONEBOOK_SEARCH: {
4505                 bluetooth_device_address_t address = { {0} };
4506                 bt_pbap_folder_t folder = { 0, };
4507                 bt_pbap_search_parameters_t app_param = { 0, };
4508
4509                 __bt_service_get_parameters(in_param1, &address,
4510                                 sizeof(bluetooth_device_address_t));
4511                 __bt_service_get_parameters(in_param2, &folder,
4512                                 sizeof(bt_pbap_folder_t));
4513                 __bt_service_get_parameters(in_param3, &app_param,
4514                                 sizeof(bt_pbap_search_parameters_t));
4515
4516                 result = _bt_pbap_phonebook_search(&address, folder.addressbook,
4517                                 folder.folder_type, &app_param);
4518                 break;
4519         }
4520
4521         default:
4522                 BT_ERR("Unknown function!");
4523                 result = BLUETOOTH_ERROR_INTERNAL;
4524                 break;
4525         }
4526
4527         FN_END;
4528
4529         return result;
4530 }
4531
4532 int __bt_agent_request(int function_name,
4533                 int request_type,
4534                 int request_id,
4535                 GDBusMethodInvocation *context,
4536                 GVariant *in_param1,
4537                 GVariant *in_param2,
4538                 GVariant *in_param3,
4539                 GVariant *in_param4,
4540                 GArray **out_param1)
4541 {
4542         int result;
4543         switch (function_name) {
4544         case BT_SET_AUTHORIZATION: {
4545                 int type;
4546                 char *uuid;
4547                 char *path;
4548                 int fd;
4549                 char *sender;
4550
4551                 sender = (char *)g_dbus_method_invocation_get_sender(context);
4552                 __bt_service_get_parameters(in_param1, &type, sizeof(int));
4553                 uuid = (char *)g_variant_get_data(in_param2);
4554                 path = (char *)g_variant_get_data(in_param3);
4555                 __bt_service_get_parameters(in_param4, &fd, sizeof(int));
4556
4557                 result = _bt_register_osp_server_in_agent(sender, type, uuid, path, fd);
4558                 break;
4559         }
4560         case BT_UNSET_AUTHORIZATION: {
4561                 int type;
4562                 char *uuid;
4563
4564                 __bt_service_get_parameters(in_param1, &type, sizeof(int));
4565                 uuid = (char *)g_variant_get_data(in_param2);
4566
4567                 result = _bt_unregister_osp_server_in_agent(type, uuid);
4568                 break;
4569         }
4570         default:
4571                 BT_ERR("Unknown function!");
4572                 result = BLUETOOTH_ERROR_INTERNAL;
4573                 break;
4574         }
4575
4576         return result;
4577 }
4578
4579 int __bt_core_request(int function_name,
4580                 int request_type,
4581                 int request_id,
4582                 GDBusMethodInvocation *context,
4583                 GVariant *in_param1)
4584 {
4585         int result;
4586
4587         switch (function_name) {
4588         case BT_ENABLE_ADAPTER:
4589         {
4590                 bt_status_t status;
4591                 _bt_set_adapter_request_state(TRUE);
4592                 status = _bt_adapter_get_status();
4593
4594                 if (status == BT_ACTIVATING) {
4595                         BT_DBG("Enabling in progress");
4596                         result = BLUETOOTH_ERROR_IN_PROGRESS;
4597                 } else if (status == BT_ACTIVATED) {
4598                         BT_DBG("Already enabled");
4599                         result = BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
4600                 } else {
4601                         _bt_adapter_set_status(BT_ACTIVATING);
4602                         result = BLUETOOTH_ERROR_NONE;
4603                 }
4604
4605                 break;
4606         }
4607         case BT_DISABLE_ADAPTER:
4608         {
4609                 bt_status_t status;
4610
4611                 status = _bt_adapter_get_status();
4612                 if (status == BT_DEACTIVATING) {
4613                                 BT_DBG("Disabling in progress");
4614                                 result = BLUETOOTH_ERROR_IN_PROGRESS;
4615                 } else if (status == BT_DEACTIVATED) {
4616                                 BT_DBG("Already disabled");
4617                                 result = BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
4618                 } else {
4619                         _bt_adapter_set_status(BT_DEACTIVATING);
4620                         result = BLUETOOTH_ERROR_NONE;
4621                 }
4622
4623                 break;
4624         }
4625         case BT_ENABLE_ADAPTER_LE:
4626         {
4627                 bt_le_status_t le_status;
4628                 _bt_set_le_request_state(TRUE);
4629                 le_status = _bt_adapter_get_le_status();
4630                 if (le_status == BT_LE_ACTIVATING) {
4631                         BT_DBG("Enabling in progress");
4632                         result = BLUETOOTH_ERROR_IN_PROGRESS;
4633                 } else if (le_status == BT_LE_ACTIVATED) {
4634                         BT_DBG("Already enabled");
4635                         result = BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
4636                 } else {
4637                         _bt_adapter_set_le_status(BT_LE_ACTIVATING);
4638                         result = BLUETOOTH_ERROR_NONE;
4639                 }
4640
4641                 break;
4642         }
4643         case BT_DISABLE_ADAPTER_LE:
4644         {
4645                 bt_le_status_t le_status;
4646
4647                 le_status = _bt_adapter_get_le_status();
4648                 if (le_status == BT_LE_DEACTIVATING) {
4649                                 BT_DBG("Disabling in progress");
4650                                 result = BLUETOOTH_ERROR_IN_PROGRESS;
4651                 } else if (le_status == BT_LE_DEACTIVATED) {
4652                                 BT_DBG("Already disabled");
4653                                 result = BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
4654                 } else {
4655                         _bt_adapter_set_le_status(BT_LE_DEACTIVATING);
4656                         result = BLUETOOTH_ERROR_NONE;
4657                 }
4658
4659                 break;
4660         }
4661         default:
4662                 BT_ERR("Unknown function!");
4663                 result = BLUETOOTH_ERROR_INTERNAL;
4664                 break;
4665         }
4666
4667         return result;
4668 }
4669
4670 static int __bt_service_get_requester_app_id(const char *unique_name, char *app_id, int len)
4671 {
4672         int ret;
4673         pid_t pid = 0;
4674
4675         retv_if(bt_service_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
4676         retv_if(unique_name == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
4677
4678         ret = __bt_service_get_sender_pid(unique_name, &pid);
4679         if (ret != BLUETOOTH_ERROR_NONE) {
4680                 BT_ERR("Fail to get the sender pid");
4681                 return ret;
4682         }
4683
4684         ret = aul_app_get_appid_bypid(pid, app_id, len);
4685         if (ret != AUL_R_OK) {
4686                 BT_ERR("app_id not found");
4687                 return BLUETOOTH_ERROR_NOT_SUPPORT;
4688         }
4689         BT_DBG("Sender app_id: %s", app_id);
4690
4691         return BLUETOOTH_ERROR_NONE;
4692 }
4693
4694 gboolean __bt_service_check_privilege(int function_name,
4695                                         int service_type,
4696                                         const char *unique_name)
4697 {
4698         int ret_val;
4699         gboolean result = TRUE;
4700         char *client_creds = NULL;
4701         char *user_creds = NULL;
4702         char *client_session = "";
4703         enum cynara_client_creds client_creds_method = CLIENT_METHOD_SMACK;
4704         enum cynara_user_creds user_creds_method = USER_METHOD_UID;
4705         char err_msg[256] = {0, };
4706
4707         retv_if(unique_name == NULL, FALSE);
4708         retv_if(bt_service_conn == NULL, FALSE);
4709
4710         ret_val = cynara_creds_get_default_client_method(&client_creds_method);
4711         if (ret_val != CYNARA_API_SUCCESS) {
4712                 cynara_strerror(ret_val, err_msg, sizeof(err_msg));
4713                 BT_ERR("Fail to get default client method: %s", err_msg);
4714                 return FALSE;
4715         }
4716
4717         ret_val = cynara_creds_get_default_user_method(&user_creds_method);
4718         if (ret_val != CYNARA_API_SUCCESS) {
4719                 cynara_strerror(ret_val, err_msg, sizeof(err_msg));
4720                 BT_ERR("Fail to get default user method: %s", err_msg);
4721                 return FALSE;
4722         }
4723
4724         ret_val = cynara_creds_gdbus_get_client(bt_service_conn, unique_name, client_creds_method, &client_creds);
4725         if (ret_val != CYNARA_API_SUCCESS) {
4726                 cynara_strerror(ret_val, err_msg, sizeof(err_msg));
4727                 BT_ERR("Fail to get client credential: %s", err_msg);
4728                 return FALSE;
4729         }
4730
4731         ret_val = cynara_creds_gdbus_get_user(bt_service_conn, unique_name, user_creds_method, &user_creds);
4732         if (ret_val != CYNARA_API_SUCCESS) {
4733                 cynara_strerror(ret_val, err_msg, sizeof(err_msg));
4734                 BT_ERR("Fail to get user credential: %s", err_msg);
4735                 if (client_creds)
4736                         free(client_creds);
4737                 return FALSE;
4738         }
4739
4740         BT_DBG("%s, %s, %s", unique_name, client_creds, user_creds);
4741
4742         switch (function_name) {
4743         case BT_SET_LOCAL_NAME:
4744         case BT_START_DISCOVERY:
4745         case BT_START_CUSTOM_DISCOVERY:
4746         case BT_CANCEL_DISCOVERY:
4747         case BT_OOB_ADD_REMOTE_DATA:
4748         case BT_OOB_REMOVE_REMOTE_DATA:
4749         case BT_OOB_READ_LOCAL_DATA:
4750         case BT_SET_ADVERTISING:
4751         case BT_SET_CUSTOM_ADVERTISING:
4752         case BT_SET_ADVERTISING_PARAMETERS:
4753         case BT_START_LE_DISCOVERY:
4754         case BT_STOP_LE_DISCOVERY:
4755         case BT_SET_SCAN_PARAMETERS:
4756         case BT_SET_SCAN_TYPE:
4757
4758         case BT_BOND_DEVICE:
4759         case BT_CANCEL_BONDING:
4760         case BT_UNBOND_DEVICE:
4761         case BT_SET_ALIAS:
4762         case BT_SET_AUTHORIZATION:
4763         case BT_UNSET_AUTHORIZATION:
4764         case BT_SEARCH_SERVICE:
4765
4766         case BT_RFCOMM_CLIENT_CONNECT:
4767                 if (function_name == BT_RFCOMM_CLIENT_CONNECT)
4768                         BT_PERMANENT_LOG("Connect socket");
4769         case BT_RFCOMM_CLIENT_CANCEL_CONNECT:
4770         case BT_RFCOMM_SOCKET_DISCONNECT:
4771                 if (function_name == BT_RFCOMM_SOCKET_DISCONNECT)
4772                         BT_PERMANENT_LOG("Disconnect socket");
4773         case BT_RFCOMM_SOCKET_WRITE:
4774         case BT_RFCOMM_CREATE_SOCKET:
4775         case BT_RFCOMM_REMOVE_SOCKET:
4776         case BT_RFCOMM_SEND_RX_DETAILS:
4777         case BT_RFCOMM_SEND_TX_DETAILS:
4778         case BT_RFCOMM_UPDATE_CONNECTION_INFO:
4779
4780         case BT_OPP_PUSH_FILES:
4781         case BT_OPP_CANCEL_PUSH:
4782         /* TODO: MAP? MAP functions, see above */
4783
4784         case BT_OBEX_SERVER_ACCEPT_CONNECTION:
4785         case BT_OBEX_SERVER_REJECT_CONNECTION:
4786         case BT_OBEX_SERVER_ACCEPT_FILE:
4787         case BT_OBEX_SERVER_REJECT_FILE:
4788         case BT_OBEX_SERVER_SET_PATH:
4789         case BT_OBEX_SERVER_SET_ROOT:
4790         case BT_OBEX_SERVER_CANCEL_TRANSFER:
4791         case BT_OBEX_SERVER_CANCEL_ALL_TRANSFERS:
4792
4793         case BT_AUDIO_SELECT_ROLE:
4794         case BT_AUDIO_CONNECT:
4795         case BT_AUDIO_DISCONNECT:
4796         case BT_AG_CONNECT:
4797         case BT_AG_DISCONNECT:
4798         case BT_AV_CONNECT:
4799         case BT_AV_DISCONNECT:
4800         case BT_AV_SOURCE_CONNECT:
4801         case BT_AV_SOURCE_DISCONNECT:
4802         case BT_AVRCP_TARGET_CONNECT:
4803         case BT_AVRCP_TARGET_DISCONNECT:
4804         case BT_AVRCP_CONTROL_CONNECT:
4805         case BT_AVRCP_CONTROL_DISCONNECT:
4806         case BT_AVRCP_HANDLE_CONTROL:
4807         case BT_AVRCP_SET_TRACK_INFO:
4808         case BT_AVRCP_SET_PROPERTY:
4809         case BT_AVRCP_SET_PROPERTIES:
4810         case BT_AVRCP_CONTROL_SET_PROPERTY:
4811
4812 #ifdef TIZEN_FEATURE_BT_AVC_TARGET
4813         case BT_AUDIO_SET_ABSOLUTE_VOLUME:
4814         case BT_AUDIO_GET_ABSOLUTE_VOLUME:
4815         case BT_AUDIO_IS_AVC_ACTIVATED:
4816 #endif
4817
4818         case BT_HF_CONNECT:
4819         case BT_HF_DISCONNECT:
4820
4821         case BT_HID_CONNECT:
4822         case BT_HID_DISCONNECT:
4823
4824         case BT_HID_DEVICE_ACTIVATE:
4825         case BT_HID_DEVICE_DEACTIVATE:
4826         case BT_HID_DEVICE_CONNECT:
4827         case BT_HID_DEVICE_DISCONNECT:
4828         case BT_HID_DEVICE_SEND_MOUSE_EVENT:
4829         case BT_HID_DEVICE_SEND_KEY_EVENT:
4830         case BT_HID_DEVICE_SEND_REPLY_TO_REPORT:
4831         case BT_HID_DEVICE_SEND_CUSTOM_EVENT:
4832
4833         case BT_CONNECT_LE:
4834         case BT_DISCONNECT_LE:
4835
4836         case BT_SET_ADVERTISING_DATA:
4837         case BT_SET_SCAN_RESPONSE_DATA:
4838
4839         case BT_HDP_CONNECT:
4840         case BT_HDP_DISCONNECT:
4841         case BT_HDP_SEND_DATA:
4842         case BT_HDP_REGISTER_SINK_APP:
4843         case BT_HDP_UNREGISTER_SINK_APP:
4844         case BT_HDP_GET_FD:
4845
4846         case BT_DPM_SET_ALLOW_BT_MODE:
4847         case BT_DPM_GET_ALLOW_BT_MODE:
4848         case BT_DPM_SET_DEVICE_RESTRITION:
4849         case BT_DPM_GET_DEVICE_RESTRITION:
4850         case BT_DPM_SET_UUID_RESTRITION:
4851         case BT_DPM_GET_UUID_RESTRITION:
4852         case BT_DPM_ADD_DEVICES_BLACKLIST:
4853         case BT_DPM_ADD_DEVICES_WHITELIST:
4854         case BT_DPM_ADD_UUIDS_BLACKLIST:
4855         case BT_DPM_ADD_UUIDS_WHITELIST:
4856         case BT_DPM_CLEAR_DEVICES_BLACKLIST:
4857         case BT_DPM_CLEAR_DEVICES_WHITELIST:
4858         case BT_DPM_CLEAR_UUIDS_BLACKLIST:
4859         case BT_DPM_CLEAR_UUIDS_WHITELIST:
4860         case BT_DPM_REMOVE_DEVICE_BLACKLIST:
4861         case BT_DPM_REMOVE_DEVICE_WHITELIST:
4862         case BT_DPM_REMOVE_UUID_BLACKLIST:
4863         case BT_DPM_REMOVE_UUID_WHITELIST:
4864         case BT_DPM_GET_DEVICES_BLACKLIST:
4865         case BT_DPM_GET_DEVICES_WHITELIST:
4866         case BT_DPM_GET_UUIDS_BLACKLIST:
4867         case BT_DPM_GET_UUIDS_WHITELIST:
4868         case BT_DPM_SET_ALLOW_OUTGOING_CALL:
4869         case BT_DPM_GET_ALLOW_OUTGOING_CALL:
4870         case BT_DPM_SET_PAIRING_STATE:
4871         case BT_DPM_GET_PAIRING_STATE:
4872         case BT_DPM_SET_PROFILE_STATE:
4873         case BT_DPM_GET_PROFILE_STATE:
4874         case BT_DPM_SET_DESKROP_CONNECTIVITY_STATE:
4875         case BT_DPM_GET_DESKROP_CONNECTIVITY_STATE:
4876         case BT_DPM_SET_DISCOVERABLE_STATE:
4877         case BT_DPM_GET_DISCOVERABLE_STATE:
4878         case BT_DPM_SET_LIMITED_DISCOVERABLE_STATE:
4879         case BT_DPM_GET_LIMITED_DISCOVERABLE_STATE:
4880         case BT_DPM_SET_DATA_TRANSFER_STATE:
4881         case BT_DPM_GET_DATA_TRANSFER_STATE:
4882
4883         case BT_NETWORK_ACTIVATE:
4884         case BT_NETWORK_DEACTIVATE:
4885         case BT_NETWORK_CONNECT:
4886         case BT_NETWORK_DISCONNECT:
4887         case BT_NETWORK_SERVER_DISCONNECT:
4888
4889         case BT_GATT_GET_PRIMARY_SERVICES:
4890 #ifdef TIZEN_GATT_CLIENT
4891         case BT_GATT_GET_SERVICE_PROPERTIES:             /* GATT Client */
4892         case BT_GATT_GET_CHARACTERISTIC_PROPERTIES:      /* GATT Client */
4893         case BT_GATT_WATCH_SERVICE_CHANGED_INDICATION:   /* GATT Client */
4894 #endif
4895         case BT_GATT_DISCOVER_CHARACTERISTICS:
4896         case BT_GATT_SET_PROPERTY_REQUEST:
4897         case BT_GATT_READ_CHARACTERISTIC:
4898 #ifdef TIZEN_GATT_CLIENT
4899         case BT_GATT_READ_DESCRIPTOR_VALUE:              /* GATT Client */
4900         case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE: /* GATT Client */
4901         case BT_GATT_ACQUIRE_WRITE:
4902         case BT_GATT_WRITE_DESCRIPTOR_VALUE:             /* GATT Client */
4903         case BT_GATT_WATCH_CHARACTERISTIC:               /* GATT Client */
4904         case BT_GATT_CLIENT_REGISTER:                    /* GATT Client */
4905         case BT_GATT_CLIENT_UNREGISTER:                   /* GATT Client */
4906 #endif
4907         case BT_GATT_DISCOVER_CHARACTERISTICS_DESCRIPTOR:
4908         case BT_GATT_REGISTER_APPLICATION:
4909         case BT_GATT_REGISTER_SERVICE:
4910         case BT_GATT_SEND_RESPONSE:
4911 #ifndef GATT_DIRECT
4912         case BT_GATT_SERVER_REGISTER:
4913         case BT_GATT_SERVER_ADD_SERVICE:
4914         case BT_GATT_SERVER_ADD_CHARACTERISTIC:
4915         case BT_GATT_SERVER_ADD_DESCRIPTOR:
4916         case BT_GATT_SERVER_START_SERVICE:
4917         case BT_GATT_SERVER_STOP_SERVICE:
4918         case BT_GATT_SERVER_DELETE_SERVICE:
4919         case BT_GATT_SERVER_SEND_RESPONSE:
4920         case BT_GATT_SERVER_ACQURE_WRITE_RESPONSE:
4921         case BT_GATT_SERVER_ACQUIRE_NOTIFY_RESPONSE:
4922         case BT_GATT_SERVER_SEND_INDICATION:
4923         case BT_GATT_SERVER_UPDATE_VALUE:
4924         case BT_GATT_SERVER_DEREGISTER:
4925 #endif
4926         case BT_REQ_ATT_MTU:
4927         case BT_PBAP_CONNECT:
4928         case BT_PBAP_DISCONNECT:
4929         case BT_PBAP_GET_PHONEBOOK_SIZE:
4930         case BT_PBAP_GET_PHONEBOOK:
4931         case BT_PBAP_GET_LIST:
4932         case BT_PBAP_PULL_VCARD:
4933         case BT_PBAP_PHONEBOOK_SEARCH:
4934
4935         case BT_RFCOMM_LISTEN_AND_ACCEPT:
4936         case BT_MESH_NETWORK_CREATE:
4937         case BT_MESH_NETWORK_LOAD:
4938         case BT_MESH_NETWORK_SCAN:
4939         case BT_MESH_NETWORK_CANCEL_SCAN:
4940         case BT_MESH_NETWORK_SET_CAPABILITIES:
4941         case BT_MESH_NETWORK_PROVISION_DEVICE:
4942         case BT_MESH_AUTHENTICATION_REPLY:
4943         case BT_MESH_NETWORK_SET_NAME:
4944         case BT_MESH_NETWORK_ADD_NETKEY:
4945         case BT_MESH_NETWORK_DELETE_NETKEY:
4946         case BT_MESH_NETWORK_UPDATE_NETKEY:
4947         case BT_MESH_NETWORK_ADD_APPKEY:
4948         case BT_MESH_NETWORK_DELETE_APPKEY:
4949         case BT_MESH_NETWORK_UPDATE_APPKEY:
4950         case BT_MESH_NETWORK_GET_NETKEYS:
4951         case BT_MESH_NETWORK_GET_APPKEYS:
4952         case BT_MESH_NETWORK_GET_NODES:
4953         case BT_MESH_ELEMENT_GET_MODELS:
4954         case BT_MESH_NODE_BROWSE:
4955         case BT_MESH_NODE_GET_VENDOR_FEATURES:
4956         case BT_MESH_NODE_CONFIGURE_KEY:
4957         case BT_MESH_NODE_TTL_EXECUTE:
4958         case BT_MESH_NODE_GET_NETKEYS:
4959         case BT_MESH_NODE_GET_APPKEYS:
4960         case BT_MESH_MODEL_CONFIGURE_APPKEY:
4961         case BT_MESH_MODEL_GET_APPKEY_LIST:
4962         case BT_MESH_NETWORK_CREATE_GROUP:
4963         case BT_MESH_MODEL_CONFIG_GROUP_SUB:
4964         case BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB:
4965         case BT_MESH_MODEL_SET_PUBLICATION:
4966         case BT_MESH_MODEL_GET_PUBLICATION:
4967         case BT_MESH_NETWORK_GET_GROUPS:
4968
4969         ret_val = cynara_check(p_cynara, client_creds, client_session, user_creds,
4970                                                 BT_PRIVILEGE_PUBLIC);
4971
4972         BT_INFO("Client Credentials [%s]", client_creds);
4973         if (ret_val != CYNARA_API_ACCESS_ALLOWED) {
4974                 BT_ERR("Fail to access: %s", BT_PRIVILEGE_PUBLIC);
4975                 result = FALSE;
4976         }
4977
4978         /* Need to check mediastorage privilege */
4979         if (function_name == BT_PBAP_GET_PHONEBOOK ||
4980                 function_name == BT_PBAP_PULL_VCARD) {
4981                 ret_val = cynara_check(p_cynara, client_creds, client_session, user_creds,
4982                                                         MEDIASTORAGE_PRIVILEGE);
4983
4984                 if (ret_val != CYNARA_API_ACCESS_ALLOWED) {
4985                 BT_ERR("Fail to access: %s", MEDIASTORAGE_PRIVILEGE);
4986                 result = FALSE;
4987                 }
4988         }
4989         break;
4990
4991         case BT_ENABLE_ADAPTER:
4992         case BT_DISABLE_ADAPTER:
4993         case BT_RESET_ADAPTER:
4994         case BT_RECOVER_ADAPTER:
4995         case BT_BATTERY_READ_DATA:
4996         case BT_ENABLE_ADAPTER_LE:
4997         case BT_DISABLE_ADAPTER_LE:
4998         case BT_SET_CONNECTABLE:
4999         case BT_SET_DISCOVERABLE_MODE:
5000         case BT_ADD_WHITE_LIST:
5001         case BT_REMOVE_WHITE_LIST:
5002         case BT_CLEAR_WHITE_LIST:
5003         case BT_SET_MANUFACTURER_DATA:
5004
5005         case BT_CANCEL_SEARCH_SERVICE:
5006         case BT_ENABLE_RSSI:
5007         case BT_DISCONNECT_DEVICE:
5008
5009         case BT_RFCOMM_ACCEPT_CONNECTION:
5010         case BT_RFCOMM_REJECT_CONNECTION:
5011         case BT_RFCOMM_LISTEN:
5012
5013         case BT_HID_ENABLE_BARCODE_FEATURE:
5014
5015         case BT_AVRCP_CONTROL_GET_PROPERTY:
5016         case BT_AVRCP_GET_TRACK_INFO:
5017         case BT_AVRCP_TRANSPORT_SET_PROPERTY:
5018         case BT_AVRCP_HANDLE_CONTROL_TO_DEST:
5019
5020         case BT_SET_CONTENT_PROTECT:
5021         case BT_BOND_DEVICE_BY_TYPE:
5022         case BT_SET_LE_PRIVACY:
5023         case BT_SET_LE_STATIC_RANDOM_ADDRESS:
5024         case BT_LE_CONN_UPDATE:
5025         case BT_UPDATE_LE_CONNECTION_MODE:
5026         case BT_GET_DEVICE_IDA:
5027         case BT_LE_READ_MAXIMUM_DATA_LENGTH:
5028         case BT_LE_WRITE_HOST_SUGGESTED_DATA_LENGTH:
5029         case BT_LE_READ_HOST_SUGGESTED_DATA_LENGTH:
5030         case BT_LE_SET_DATA_LENGTH:
5031         case BT_LE_OOB_READ_LOCAL_DATA:
5032         case BT_LE_OOB_ADD_REMOTE_DATA:
5033
5034         case BT_GET_GATT_DATA_BATCHING_AVAILABLE_PACKETS:
5035         case BT_ENABLE_GATT_DATA_BATCHING:
5036         case BT_DISABLE_GATT_DATA_BATCHING:
5037
5038         case BT_LE_IPSP_INIT:
5039         case BT_LE_IPSP_DEINIT:
5040         case BT_LE_IPSP_CONNECT:
5041         case BT_LE_IPSP_DISCONNECT:
5042         case BT_PXP_MONITOR_SET_PROPERTY:
5043         case BT_PXP_MONITOR_GET_PROPERTY:
5044         case BT_PXP_MONITOR_GET_SUPPORTED_SERIVCES:
5045         case BT_PXP_REPORTER_REGISTER:
5046         case BT_PXP_REPORTER_UNREGISTER:
5047         case BT_PXP_REPORTER_GET_PROPERTY:
5048
5049         /* TDS */
5050         case BT_TDS_PROVIDER_REGISTER:
5051         case BT_TDS_PROVIDER_UNREGISTER:
5052         case BT_TDS_PROVIDER_SET_MANUF_DATA:
5053         case BT_TDS_PROVIDER_CREATE:
5054         case BT_TDS_PROVIDER_DESTROY:
5055         case BT_TDS_PROVIDER_SET_TRANSPORT_DATA:
5056         case BT_TDS_SEND_ACTIVATION_RESPONSE:
5057         case BT_TDS_READ_TRANSPORT_DATA:
5058         case BT_TDS_ENABLE_CONTROL_POINT:
5059         case BT_TDS_ACTIVATE_CONTROL_POINT:
5060
5061         /* OTP Server */
5062         case BT_OTP_SERVER_INIT:
5063         case BT_OTP_SERVER_DEINIT:
5064         case BT_OTP_READ_VALUE:
5065         case BT_OTP_ENABLE_NOTIFICATION:
5066         case BT_OTP_WRITE_VALUE:
5067         case BT_LE_OTC_CONNECT:
5068         case BT_LE_OTC_DISCONNECT:
5069
5070         case BT_MAP_CREATE_SESSION:
5071         case BT_MAP_DESTROY_SESSION:
5072         case BT_MAP_SET_FOLDER:
5073         case BT_MAP_LIST_FOLDERS:
5074         case BT_MAP_LIST_FILTER_FIELDS:
5075         case BT_MAP_LIST_MESSAGES:
5076         case BT_MAP_UPDATE_INBOX:
5077         case BT_MAP_PUSH_MESSAGE:
5078         case BT_MAP_GET_MESSAGE:
5079
5080         ret_val = cynara_check(p_cynara, client_creds, client_session, user_creds,
5081                                 BT_PRIVILEGE_PLATFORM);
5082
5083         if (ret_val != CYNARA_API_ACCESS_ALLOWED) {
5084                 BT_ERR("Fail to access: %s", BT_PRIVILEGE_PLATFORM);
5085                 result = FALSE;
5086         }
5087         break;
5088
5089         case BT_CHECK_ADAPTER:
5090         case BT_GET_RSSI:
5091
5092         case BT_GET_LOCAL_NAME:
5093         case BT_GET_LOCAL_ADDRESS:
5094         case BT_GET_LOCAL_VERSION:
5095         case BT_IS_SERVICE_USED:
5096         case BT_GET_DISCOVERABLE_MODE:
5097         case BT_GET_DISCOVERABLE_TIME:
5098         case BT_IS_DISCOVERYING:
5099         case BT_IS_LE_DISCOVERYING:
5100         case BT_IS_CONNECTABLE:
5101         case BT_GET_CONNECTABLE:
5102         case BT_GET_BONDED_DEVICES:
5103         case BT_GET_PROFILE_CONNECTED_DEVICES:
5104         case BT_GET_BONDED_DEVICE:
5105         case BT_PASSKEY_REPLY:
5106         case BT_PASSKEY_CONFIRMATION_REPLY:
5107         case BT_GET_IS_ALIAS_SET:
5108         case BT_IS_DEVICE_CONNECTED:
5109         case BT_GET_CONNECTED_LINK_TYPE:
5110         case BT_SET_PROFILE_TRUSTED:
5111         case BT_GET_PROFILE_TRUSTED:
5112         case BT_GET_SPEAKER_GAIN:
5113         case BT_SET_SPEAKER_GAIN:
5114         case BT_RFCOMM_CLIENT_IS_CONNECTED:
5115         case BT_RFCOMM_IS_UUID_AVAILABLE:
5116         case BT_GET_ADVERTISING_DATA:
5117         case BT_GET_SCAN_RESPONSE_DATA:
5118         case BT_IS_ADVERTISING:
5119         case BT_IS_LE_2M_PHY_SUPPORTED:
5120         case BT_IS_LE_CODED_PHY_SUPPORTED:
5121         case BT_REGISTER_SCAN_FILTER:
5122         case BT_IS_SCAN_FILTER_SUPPORTED:
5123         case BT_GET_ATT_MTU:
5124
5125         case BT_OBEX_SERVER_ALLOCATE:
5126         case BT_OBEX_SERVER_DEALLOCATE:
5127         case BT_OBEX_SERVER_IS_ACTIVATED:
5128         case BT_OPP_GET_TRANSFER_PROGRESS:
5129                 /* Non-privilege control */
5130                 break;
5131         default:
5132                 BT_ERR("Unknown function!");
5133                 result = FALSE;
5134                 break;
5135         }
5136
5137         if (function_name >= BT_FUNC_MESH_BASE) {
5138                 BT_INFO("MESH Function called");
5139                 char app_id[256] = { 0, };
5140
5141                 if (BLUETOOTH_ERROR_NONE == __bt_service_get_requester_app_id(unique_name,
5142                                         app_id, sizeof(app_id))) {
5143                         requester_unique_creds = g_strdup(app_id);
5144                 } else {
5145                         BT_DBG("Requester app_id not found, use client creds");
5146                         requester_unique_creds = g_strdup(client_creds);
5147                 }
5148                 BT_DBG("Requester unique_creds: %s", requester_unique_creds);
5149         } else {
5150                 BT_INFO("Non MESH Function called client creds [%s]", client_creds);
5151         }
5152
5153         if (client_creds)
5154                 free(client_creds);
5155
5156         if (user_creds)
5157                 free(user_creds);
5158
5159         return result;
5160 }
5161
5162 GDBusNodeInfo *__bt_service_create_method_node_info
5163                                         (const gchar *introspection_data)
5164 {
5165         GError *err = NULL;
5166         GDBusNodeInfo *node_info = NULL;
5167
5168         if (introspection_data == NULL) {
5169                 BT_ERR("Introspection XML not present");
5170                 return NULL;
5171         }
5172
5173         node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
5174
5175         if (err) {
5176                 BT_ERR("Unable to create node: %s", err->message);
5177                 g_clear_error(&err);
5178         }
5179         return node_info;
5180 }
5181
5182 int __bt_service_register_object(GDBusConnection *conn,
5183                 GDBusNodeInfo *node_info, gboolean reg)
5184 {
5185         static guint service_id = 0;
5186         GError *error = NULL;
5187
5188         if (reg) {
5189                 if (node_info == NULL)
5190                         return -1;
5191
5192                 service_id = g_dbus_connection_register_object(conn,
5193                                 BT_SERVICE_PATH,
5194                                 node_info->interfaces[0],
5195                                 &method_table,
5196                                 NULL, NULL, &error);
5197                 if (service_id == 0)
5198                         return -1;
5199         } else {
5200                 if (service_id > 0) {
5201                         g_dbus_connection_unregister_object(conn,
5202                                         service_id);
5203                         service_id = 0;
5204                 }
5205         }
5206
5207         return 0;
5208 }
5209
5210 static void __name_owner_changed(GDBusConnection *connection,
5211                 const gchar *sender_name,
5212                 const gchar *object_path,
5213                 const gchar *interface_name,
5214                 const gchar *signal_name,
5215                 GVariant *parameters,
5216                 gpointer user_data)
5217 {
5218         const char *name = NULL;
5219         const char *old_owner = NULL;
5220         const char *new_owner = NULL;
5221
5222         g_variant_get(parameters, "(&s&s&s)", &name, &old_owner, &new_owner);
5223         if (*new_owner != '\0')
5224                 return;
5225
5226         _bt_check_hdp_app_termination(name);
5227
5228         /* Advertising App Termination */
5229         _bt_check_adv_app_termination(name);
5230
5231         /* Check if le_scanning app is terminated */
5232         _bt_check_le_scanner_app_termination(name);
5233
5234         /* Check if RFCOMM app is terminated */
5235         _bt_rfcomm_check_termination(name);
5236 }
5237
5238 static void __bt_service_bus_acquired_handler(GDBusConnection *connection,
5239                 const gchar *name, gpointer user_data)
5240 {
5241         GDBusNodeInfo *node_info = NULL;
5242
5243         BT_INFO("bus acquired");
5244
5245         ret_if(connection == NULL);
5246
5247         node_info = __bt_service_create_method_node_info(
5248                         bt_service_introspection_xml);
5249         ret_if(node_info == NULL);
5250
5251         __bt_service_register_object(connection, node_info, TRUE);
5252         g_dbus_node_info_unref(node_info);
5253
5254         bt_service_conn = connection;
5255 }
5256
5257 static void __bt_service_name_acquired_handler(GDBusConnection *connection,
5258                 const gchar *name, gpointer user_data)
5259 {
5260         BT_INFO("name acquired");
5261         name_acquired = TRUE;
5262 }
5263
5264 static void __bt_service_name_lost_handler(GDBusConnection *connection,
5265                 const gchar *name, gpointer user_data)
5266 {
5267         BT_INFO("name lost");
5268         name_acquired = FALSE;
5269 }
5270
5271 gboolean _is_name_acquired(void)
5272 {
5273         return name_acquired;
5274 }
5275
5276 void _bt_service_unref_connection(void)
5277 {
5278         BT_INFO("+");
5279
5280         if (bt_service_conn) {
5281                 g_dbus_connection_flush_sync(bt_service_conn, NULL, NULL);
5282                 g_object_unref(bt_service_conn);
5283                 bt_service_conn = NULL;
5284         }
5285
5286         BT_INFO("-");
5287 }
5288
5289 int _bt_service_register(void)
5290 {
5291         GDBusConnection *conn;
5292         GError *err = NULL;
5293
5294         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
5295         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
5296         bt_service_conn = conn;
5297
5298         owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
5299                                 BT_SERVICE_NAME,
5300                                 G_BUS_NAME_OWNER_FLAGS_NONE,
5301                                 __bt_service_bus_acquired_handler,
5302                                 __bt_service_name_acquired_handler,
5303                                 __bt_service_name_lost_handler,
5304                                 NULL, NULL);
5305         BT_DBG("owner_id is [%d]", owner_id);
5306         if (owner_id == 0)
5307                 goto fail;
5308
5309         /* Subscribe for name owner changed signal */
5310         owner_sig_id = g_dbus_connection_signal_subscribe(conn,
5311                         BT_FREEDESKTOP_SERVICE, BT_FREEDESKTOP_INTERFACE,
5312                         BT_NAME_OWNER_CHANGED, NULL, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
5313                         __name_owner_changed, NULL, NULL);
5314         BT_INFO("owner_sig_id: %d", owner_sig_id);
5315
5316         return BLUETOOTH_ERROR_NONE;
5317
5318 fail:
5319         if (bt_service_conn) {
5320                 g_object_unref(bt_service_conn);
5321                 bt_service_conn = NULL;
5322         }
5323
5324         return BLUETOOTH_ERROR_INTERNAL;
5325 }
5326
5327 void _bt_service_unregister(void)
5328 {
5329         if (bt_service_conn) {
5330                 sd_notify(0, "STOPPING=1");
5331
5332                 if (owner_sig_id > 0) {
5333                         g_dbus_connection_signal_unsubscribe(
5334                                         bt_service_conn, owner_sig_id);
5335                         owner_sig_id = 0;
5336                 }
5337
5338                 if (owner_id > 0) {
5339                         g_bus_unown_name(owner_id);
5340                         owner_id = 0;
5341                 }
5342
5343                 __bt_service_register_object(bt_service_conn, NULL, FALSE);
5344         }
5345 }
5346
5347 int _bt_service_cynara_init(void)
5348 {
5349         int result;
5350         char err_msg[256] = {0, };
5351
5352         retv_if(p_cynara != NULL, BLUETOOTH_ERROR_ALREADY_INITIALIZED);
5353
5354         result = cynara_initialize(&p_cynara, conf);
5355
5356         if (result != CYNARA_API_SUCCESS) {
5357                 cynara_strerror(result, err_msg, sizeof(err_msg));
5358                 BT_ERR("Fail to initialize cynara: [%s]", err_msg);
5359                 return BLUETOOTH_ERROR_INTERNAL;
5360         }
5361
5362         return BLUETOOTH_ERROR_NONE;
5363 }
5364
5365 void _bt_service_cynara_deinit(void)
5366 {
5367         int result;
5368         char err_msg[256] = {0, };
5369
5370         ret_if(p_cynara == NULL);
5371
5372         result = cynara_finish(p_cynara);
5373
5374         if (result != CYNARA_API_SUCCESS) {
5375                 cynara_strerror(result, err_msg, sizeof(err_msg));
5376                 BT_ERR("Fail to finish cynara: [%s]", err_msg);
5377                 return;
5378         }
5379
5380         p_cynara = NULL;
5381         conf = NULL;
5382 }
5383
5384 void _bt_service_method_return(GDBusMethodInvocation *invocation,
5385                 GArray *out_param, int result)
5386 {
5387         GVariant *out_var;
5388         BT_DBG("");
5389         out_var = g_variant_new_from_data((const GVariantType *)"ay",
5390                         out_param->data, out_param->len, TRUE, NULL, NULL);
5391
5392         g_dbus_method_invocation_return_value(invocation,
5393                         g_variant_new("(iv)", result, out_var));
5394 }
5395
5396 void _bt_service_method_return_with_unix_fd_list(GDBusMethodInvocation *invocation,
5397                 GArray *out_param, int result, GUnixFDList *fd_list)
5398 {
5399         GVariant *out_var;
5400         BT_DBG("");
5401         out_var = g_variant_new_from_data((const GVariantType *)"ay",
5402                         out_param->data, out_param->len, TRUE, NULL, NULL);
5403
5404         g_dbus_method_invocation_return_value_with_unix_fd_list(invocation,
5405                         g_variant_new("(iv)", result, out_var), fd_list);
5406 }