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