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