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