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