Merge "Fix SAM tool violations (GlobalVariable)" into tizen
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-request-sender.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 <glib.h>
19 #include <dlog.h>
20 #include <string.h>
21
22 #include "bluetooth-api.h"
23 #include "bluetooth-hid-api.h"
24 #include "bluetooth-audio-api.h"
25 #include "bt-internal-types.h"
26 #include "bluetooth-ipsp-api.h"
27
28 #include "bt-common.h"
29 #include "bt-request-sender.h"
30 #include "bt-event-handler.h"
31 #include "bluetooth-media-control.h"
32
33 #ifdef TIZEN_GATT_CLIENT
34 #include "bluetooth-gatt-client-api.h"
35 #endif
36
37 #include "bluetooth-mesh-api.h"
38
39 static GSList *sending_requests;
40
41 static GDBusProxy *service_gproxy;
42
43 static GDBusProxy *__bt_gdbus_init_service_proxy(void)
44 {
45         GDBusConnection *service_gconn;
46         GDBusProxy *proxy;
47         GError *err = NULL;
48
49         service_gconn = _bt_get_system_private_conn();
50
51         if (!service_gconn)
52                 return NULL;
53
54         proxy =  g_dbus_proxy_new_sync(service_gconn,
55                         G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES, NULL,
56                         BT_DBUS_NAME,
57                         BT_SERVICE_PATH,
58                         BT_DBUS_NAME,
59                         NULL, &err);
60         if (!proxy) {
61                 if (err) {
62                          BT_ERR("Unable to create proxy: %s", err->message);
63                          g_clear_error(&err);
64                 }
65
66                 return NULL;
67         }
68
69         service_gproxy = proxy;
70
71         return proxy;
72 }
73
74 static GDBusProxy *__bt_gdbus_get_service_proxy(void)
75 {
76         return (service_gproxy) ? service_gproxy : __bt_gdbus_init_service_proxy();
77 }
78
79 void _bt_gdbus_deinit_proxys(void)
80 {
81         if (service_gproxy) {
82                 g_object_unref(service_gproxy);
83                 service_gproxy = NULL;
84         }
85 }
86
87 static void __bt_get_event_info(int service_function, GArray *output,
88                         int *event, int *event_type, void **param_data)
89 {
90         ret_if(event == NULL);
91
92         BT_DBG("service_function : %s (0x%x)",
93                 _bt_convert_service_function_to_string(service_function),
94                 service_function);
95         switch (service_function) {
96         case BT_BOND_DEVICE:
97         case BT_BOND_DEVICE_BY_TYPE:
98                 *event_type = BT_ADAPTER_EVENT;
99                 *event = BLUETOOTH_EVENT_BONDING_FINISHED;
100                 ret_if(output == NULL);
101                 *param_data = &g_array_index(output,
102                                 bluetooth_device_info_t, 0);
103                 break;
104         case BT_UNBOND_DEVICE:
105                 *event_type = BT_ADAPTER_EVENT;
106                 *event = BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED;
107                 ret_if(output == NULL);
108                 *param_data = &g_array_index(output,
109                                 bluetooth_device_address_t, 0);
110                 break;
111         case BT_SEARCH_SERVICE:
112                 *event_type = BT_ADAPTER_EVENT;
113                 *event = BLUETOOTH_EVENT_SERVICE_SEARCHED;
114                 ret_if(output == NULL);
115                 *param_data = &g_array_index(output,
116                                 bt_sdp_info_t, 0);
117                 break;
118         case BT_HID_CONNECT:
119                 *event_type = BT_HID_EVENT;
120                 *event = BLUETOOTH_HID_CONNECTED;
121                 ret_if(output == NULL);
122                 *param_data = &g_array_index(output,
123                                 bluetooth_device_address_t, 0);
124                 break;
125         case BT_HID_DISCONNECT:
126                 *event_type = BT_HID_EVENT;
127                 *event = BLUETOOTH_HID_DISCONNECTED;
128                 ret_if(output == NULL);
129                 *param_data = &g_array_index(output,
130                                 bluetooth_device_address_t, 0);
131                 break;
132         case BT_AUDIO_CONNECT:
133         case BT_AG_CONNECT:
134                 *event_type = BT_HEADSET_EVENT;
135                 *event = BLUETOOTH_EVENT_AG_CONNECTED;
136                 ret_if(output == NULL);
137                 *param_data = &g_array_index(output, char, 0);
138                 break;
139         case BT_AUDIO_DISCONNECT:
140         case BT_AG_DISCONNECT:
141                 *event_type = BT_HEADSET_EVENT;
142                 *event = BLUETOOTH_EVENT_AG_DISCONNECTED;
143                 ret_if(output == NULL);
144                 *param_data = &g_array_index(output, char, 0);
145                 break;
146         case BT_AV_CONNECT:
147                 *event_type = BT_HEADSET_EVENT;
148                 *event = BLUETOOTH_EVENT_AV_CONNECTED;
149                 ret_if(output == NULL);
150                 *param_data = &g_array_index(output, char, 0);
151                 break;
152         case BT_AV_DISCONNECT:
153                 *event_type = BT_HEADSET_EVENT;
154                 *event = BLUETOOTH_EVENT_AV_DISCONNECTED;
155                 ret_if(output == NULL);
156                 *param_data = &g_array_index(output, char, 0);
157                 break;
158         case BT_AV_SOURCE_CONNECT:
159                 *event_type = BT_A2DP_SOURCE_EVENT;
160                 *event = BLUETOOTH_EVENT_AV_SOURCE_CONNECTED;
161                 ret_if(output == NULL);
162                 *param_data = &g_array_index(output, char, 0);
163                 break;
164         case BT_AV_SOURCE_DISCONNECT:
165                 *event_type = BT_A2DP_SOURCE_EVENT;
166                 *event = BLUETOOTH_EVENT_AV_SOURCE_DISCONNECTED;
167                 ret_if(output == NULL);
168                 *param_data = &g_array_index(output, char, 0);
169                 break;
170         case BT_HF_CONNECT:
171                 *event_type = BT_HF_AGENT_EVENT;
172                 *event = BLUETOOTH_EVENT_HF_CONNECTED;
173                 ret_if(output == NULL);
174                 *param_data = &g_array_index(output, char, 0);
175                 break;
176         case BT_HF_DISCONNECT:
177                 *event_type = BT_HF_AGENT_EVENT;
178                 *event = BLUETOOTH_EVENT_HF_DISCONNECTED;
179                 ret_if(output == NULL);
180                 *param_data = &g_array_index(output, char, 0);
181                 break;
182         case BT_NETWORK_CONNECT:
183                 *event_type = BT_ADAPTER_EVENT;
184                 *event = BLUETOOTH_EVENT_NETWORK_CONNECTED;
185                 ret_if(output == NULL);
186                 *param_data = &g_array_index(output,
187                                 bluetooth_device_address_t, 0);
188                 break;
189         case BT_NETWORK_DISCONNECT:
190                 *event_type = BT_ADAPTER_EVENT;
191                 *event = BLUETOOTH_EVENT_NETWORK_DISCONNECTED;
192                 ret_if(output == NULL);
193                 *param_data = &g_array_index(output,
194                                 bluetooth_device_address_t, 0);
195                 break;
196         case BT_RFCOMM_CLIENT_CONNECT:
197                 *event_type = BT_RFCOMM_CLIENT_EVENT;
198                 *event = BLUETOOTH_EVENT_RFCOMM_CONNECTED;
199                 ret_if(output == NULL);
200                 *param_data = &g_array_index(output,
201                                 bluetooth_rfcomm_connection_t, 0);
202                 break;
203         case BT_AVRCP_TARGET_CONNECT:
204                 *event_type = BT_AVRCP_EVENT;
205                 *event = BLUETOOTH_EVENT_AVRCP_CONNECTED;
206                 ret_if(output == NULL);
207                 *param_data = &g_array_index(output, char, 0);
208                 break;
209         case BT_AVRCP_TARGET_DISCONNECT:
210                 *event_type = BT_AVRCP_EVENT;
211                 *event = BLUETOOTH_EVENT_AVRCP_DISCONNECTED;
212                 ret_if(output == NULL);
213                 *param_data = &g_array_index(output, char, 0);
214                 break;
215         case BT_AVRCP_CONTROL_CONNECT:
216                 *event_type = BT_AVRCP_CONTROL_EVENT;
217                 *event = BLUETOOTH_EVENT_AVRCP_CONTROL_CONNECTED;
218                 ret_if(output == NULL);
219                 *param_data = &g_array_index(output, char, 0);
220                 break;
221         case BT_AVRCP_CONTROL_DISCONNECT:
222                 *event_type = BT_AVRCP_CONTROL_EVENT;
223                 *event = BLUETOOTH_EVENT_AVRCP_CONTROL_DISCONNECTED;
224                 ret_if(output == NULL);
225                 *param_data = &g_array_index(output, char, 0);
226                 break;
227         case BT_REQ_ATT_MTU:
228                 *event_type = BT_DEVICE_EVENT;
229                 *event = BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED;
230                 ret_if(output == NULL);
231                 *param_data = &g_array_index(output,
232                                 bluetooth_device_address_t, 0);
233                 break;
234 #ifdef TIZEN_GATT_CLIENT
235         case BT_CONNECT_LE:
236                 *event_type = BT_DEVICE_EVENT;
237                 *event = BLUETOOTH_EVENT_GATT_CLIENT_CONNECTED;
238                 ret_if(output == NULL);
239                 *param_data = &g_array_index(output,
240                                 bluetooth_device_address_t, 0);
241                 break;
242         case BT_DISCONNECT_LE:
243                 *event_type = BT_DEVICE_EVENT;
244                 *event = BLUETOOTH_EVENT_GATT_CLIENT_DISCONNECTED;
245                 ret_if(output == NULL);
246                 *param_data = &g_array_index(output,
247                                 bluetooth_device_address_t, 0);
248                 break;
249 #else
250         case BT_CONNECT_LE:
251                 *event_type = BT_DEVICE_EVENT;
252                 *event = BLUETOOTH_EVENT_GATT_CONNECTED;
253                 ret_if(output == NULL);
254                 *param_data = &g_array_index(output,
255                                 bluetooth_device_address_t, 0);
256                 break;
257         case BT_DISCONNECT_LE:
258                 *event_type = BT_DEVICE_EVENT;
259                 *event = BLUETOOTH_EVENT_GATT_DISCONNECTED;
260                 ret_if(output == NULL);
261                 *param_data = &g_array_index(output,
262                                 bluetooth_device_address_t, 0);
263                 break;
264 #endif
265 #ifdef TIZEN_GATT_CLIENT
266         case BT_GATT_READ_CHARACTERISTIC:
267                 BT_INFO("BT_GATT_READ_CHARACTERISTIC");
268                 *event_type = BT_GATT_CLIENT_EVENT;
269                 *event = BLUETOOTH_EVENT_GATT_READ_CHAR;
270                 ret_if(output == NULL);
271                 *param_data = &g_array_index(output,
272                                 bluetooth_gatt_client_char_prop_info_t, 0);
273                 break;
274         case BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE:
275                 BT_INFO("BT_GATT_WRITE_CHARACTERISTIC_VALUE_BY_TYPE");
276                 *event_type = BT_GATT_CLIENT_EVENT;
277                 *event = BLUETOOTH_EVENT_GATT_WRITE_CHAR;
278                 ret_if(output == NULL);
279                 *param_data = &g_array_index(output,
280                                 bluetooth_gatt_client_char_prop_info_t, 0);
281                 break;
282         case BT_GATT_READ_DESCRIPTOR_VALUE:
283                 BT_INFO("BT_GATT_READ_DESCRIPTOR_VALUE");
284                 *event_type = BT_GATT_CLIENT_EVENT;
285                 *event = BLUETOOTH_EVENT_GATT_READ_DESC;
286                 ret_if(output == NULL);
287                 *param_data = &g_array_index(output,
288                                 bluetooth_gatt_client_desc_prop_info_t, 0);
289                 break;
290         case BT_GATT_WRITE_DESCRIPTOR_VALUE:
291                 BT_INFO("BT_GATT_WRITE_DESCRIPTOR_VALUE");
292                 *event_type = BT_GATT_CLIENT_EVENT;
293                 *event = BLUETOOTH_EVENT_GATT_WRITE_DESC;
294                 ret_if(output == NULL);
295                 *param_data = &g_array_index(output,
296                                 bluetooth_gatt_client_desc_prop_info_t, 0);
297                 break;
298 #endif
299         case BT_TDS_READ_TRANSPORT_DATA:
300                 *event_type = BT_TDS_EVENT;
301                 *event = BLUETOOTH_EVENT_TDS_TRANSPORT_DATA_RECEIVED;
302                 ret_if(output == NULL);
303                 *param_data = &g_array_index(output,
304                         bluetooth_device_address_t, 0);
305                 break;
306         case BT_TDS_ENABLE_CONTROL_POINT:
307                 *event_type = BT_TDS_EVENT;
308                 *event = BLUETOOTH_EVENT_TDS_CONTROL_POINT_ENABLED;
309                 ret_if(output == NULL);
310                 *param_data = &g_array_index(output,
311                         bluetooth_device_address_t, 0);
312                 break;
313         case BT_TDS_ACTIVATE_CONTROL_POINT:
314                 *event_type = BT_TDS_EVENT;
315                 *event = BLUETOOTH_EVENT_TDS_ACTIVATION_RESULT;
316                 ret_if(output == NULL);
317                 *param_data = &g_array_index(output,
318                         bluetooth_device_address_t, 0);
319                 break;
320         case BT_HDP_CONNECT:
321                 *event_type = BT_HDP_EVENT;
322                 *event = BLUETOOTH_EVENT_HDP_CONNECTED;
323                 ret_if(output == NULL);
324                 *param_data = &g_array_index(output,
325                                 bt_hdp_connected_t, 0);
326                 break;
327         case BT_HDP_DISCONNECT:
328                 *event_type = BT_HDP_EVENT;
329                 *event = BLUETOOTH_EVENT_HDP_DISCONNECTED;
330                 ret_if(output == NULL);
331                 *param_data = &g_array_index(output,
332                                 bt_hdp_disconnected_t, 0);
333                 break;
334         case BT_MESH_NETWORK_PROVISION_DEVICE:
335                 *event_type = BT_MESH_EVENT;
336                 *event = BLUETOOTH_EVENT_MESH_PROVISIONING_FINISHED;
337                 ret_if(output == NULL);
338                 *param_data = &g_array_index(output,
339                                 bluetooth_mesh_provisioning_request_t, 0);
340                 break;
341         case BT_MESH_NODE_BROWSE:
342                 *event_type = BT_MESH_EVENT;
343                 *event = BLUETOOTH_EVENT_MESH_NODE_BROWSED;
344                 ret_if(output == NULL);
345                 *param_data = &g_array_index(output,
346                                 bluetooth_mesh_node_discover_t, 0);
347                 break;
348         case BT_MESH_NODE_GET_VENDOR_FEATURES:
349                 *event_type = BT_MESH_EVENT;
350                 *event = BLUETOOTH_EVENT_MESH_NODE_VENDOR_FEATURES;
351                 ret_if(output == NULL);
352                 *param_data = &g_array_index(output,
353                                 bluetooth_mesh_node_features_t, 0);
354                 break;
355         case BT_MESH_NODE_CONFIGURE_KEY:
356                 *event_type = BT_MESH_EVENT;
357                 *event = BLUETOOTH_EVENT_MESH_NODE_KEY_CONFIGURED;
358                 ret_if(output == NULL);
359                 *param_data = &g_array_index(output,
360                                 bluetooth_mesh_key_configure_t, 0);
361                 break;
362         case BT_MESH_NODE_TTL_EXECUTE:
363                 *event_type = BT_MESH_EVENT;
364                 *event = BLUETOOTH_EVENT_MESH_NODE_TTL_CONFIGURED;
365                 ret_if(output == NULL);
366                 *param_data = &g_array_index(output,
367                                 bluetooth_mesh_node_ttl_info_t, 0);
368                 break;
369         case BT_MESH_MODEL_CONFIGURE_APPKEY:
370                 *event_type = BT_MESH_EVENT;
371                 *event = BLUETOOTH_EVENT_MESH_MODEL_APPKEY_BIND;
372                 ret_if(output == NULL);
373                 *param_data = &g_array_index(output,
374                                 bluetooth_mesh_model_configure_t, 0);
375                 break;
376         case BT_MESH_MODEL_EXECUTE_MSG:
377                 *event_type = BT_MESH_EVENT;
378                 *event = BLUETOOTH_EVENT_MESH_MODEL_MSG_EXECUTED;
379                 ret_if(output == NULL);
380                 *param_data = &g_array_index(output,
381                                 bluetooth_mesh_model_msg_t, 0);
382                 break;
383         case BT_MESH_MODEL_CONFIG_GROUP_SUB:
384                 *event_type = BT_MESH_EVENT;
385                 *event = BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_CONFGURED;
386                 ret_if(output == NULL);
387                 *param_data = &g_array_index(output,
388                                 bluetooth_mesh_model_configure_t, 0);
389                 break;
390         case BT_MESH_MODEL_GET_APPKEY_LIST:
391                 *event_type = BT_MESH_EVENT;
392                 *event = BLUETOOTH_EVENT_MESH_MODEL_APPKEY_LIST;
393                 ret_if(output == NULL);
394                 *param_data = &g_array_index(output,
395                                 bluetooth_mesh_model_configure_t, 0);
396                 break;
397         case BT_MESH_MODEL_GET_SUBSCRIPTION_LIST:
398                 *event_type = BT_MESH_EVENT;
399                 *event = BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_LIST;
400                 ret_if(output == NULL);
401                 *param_data = &g_array_index(output,
402                                 bluetooth_mesh_model_configure_t, 0);
403                 break;
404         case BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB:
405                 *event_type = BT_MESH_EVENT;
406                 *event = BLUETOOTH_EVENT_MESH_MODEL_VIRTUAL_SUBSCRIPTION_CONFGURED;
407                 ret_if(output == NULL);
408                 *param_data = &g_array_index(output,
409                                 bluetooth_mesh_model_configure_t, 0);
410                 break;
411         case BT_MESH_MODEL_SET_PUBLICATION:
412                 *event_type = BT_MESH_EVENT;
413                 *event = BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS;
414                 ret_if(output == NULL);
415                 *param_data = &g_array_index(output,
416                                 bluetooth_mesh_model_configure_t, 0);
417                 break;
418         case BT_MESH_MODEL_GET_PUBLICATION:
419                 *event_type = BT_MESH_EVENT;
420                 *event = BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS;
421                 ret_if(output == NULL);
422                 *param_data = &g_array_index(output,
423                                 bluetooth_mesh_model_configure_t, 0);
424                 break;
425         default:
426                 BT_ERR("Unknown function");
427                 return;
428         }
429 }
430
431 /*
432 out param1: API result
433 out param2: return paramter
434 out param3:
435 */
436 static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
437 {
438         char *data;
439         int size;
440
441         size = g_variant_get_size(var);
442         if (size > 0) {
443                 data = (char *)g_variant_get_data(var);
444                 if (data)
445                         param = g_array_append_vals(param, data, size);
446
447         }
448 }
449
450 static void __send_request_cb(GDBusProxy *proxy,
451                                 GAsyncResult *res,
452                                 gpointer user_data)
453 {
454         bluetooth_event_param_t bt_event;
455         bt_req_info_t *cb_data = user_data;
456         int result = BLUETOOTH_ERROR_NONE;
457         int event_type = BT_ADAPTER_EVENT;
458         int request_id;
459         GError *error = NULL;
460         GVariant *value;
461         GVariant *param1;
462 //      GVariant *param2;
463         GArray *out_param1 = NULL;
464 //      GArray *out_param2 = NULL;
465
466         BT_DBG("+");
467         memset(&bt_event, 0x00, sizeof(bluetooth_event_param_t));
468
469         value = g_dbus_proxy_call_finish(proxy, res, &error);
470         if (value == NULL) {
471                 if (error) {
472                         /* dBUS gives error cause */
473                         BT_ERR("D-Bus API failure: message[%s]",
474                                                         error->message);
475                         g_clear_error(&error);
476                 }
477                 result = BLUETOOTH_ERROR_TIMEOUT;
478
479                 ret_if(cb_data == NULL);
480
481                 __bt_get_event_info(cb_data->service_function, NULL,
482                                 &bt_event.event, &event_type,
483                                 &bt_event.param_data);
484         } else {
485                 g_variant_get(value, "(iv)", &result, &param1);
486                 g_variant_unref(value);
487
488                 if (param1) {
489                         out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
490                         __bt_fill_garray_from_variant(param1, out_param1);
491                         g_variant_unref(param1);
492                 }
493
494 //              if (param2) {
495 //                      out_param2 = g_array_new(TRUE, TRUE, sizeof(gchar));
496 //                      __bt_fill_garray_from_variant(param2, out_param2);
497 //                      result = g_array_index(out_param2, int, 0);
498 //                      g_variant_unref(param2);
499 //                      g_array_free(out_param2, TRUE);
500 //              } else {
501 //                      result = BLUETOOTH_ERROR_INTERNAL;
502 //              }
503
504                 ret_if(cb_data == NULL);
505
506                 __bt_get_event_info(cb_data->service_function, out_param1,
507                                 &bt_event.event, &event_type,
508                                 &bt_event.param_data);
509
510                 BT_DBG("service_function [%d]", cb_data->service_function);
511                 if (result == BLUETOOTH_ERROR_NONE && out_param1) {
512                         if (cb_data->service_function == BT_OPP_PUSH_FILES) {
513                                 request_id = g_array_index(out_param1, int, 0);
514                                 BT_DBG("request_id : %d", request_id);
515                                 _bt_add_push_request_id(request_id);
516                         } else if (cb_data->service_function == BT_MAP_LIST_FOLDERS) {
517                                 request_id = g_array_index(out_param1, int, 0);
518                                 BT_DBG("request_id : %d", request_id);
519                                 _bt_add_push_request_id(request_id);
520                         } else if (cb_data->service_function == BT_MAP_LIST_FILTER_FIELDS) {
521                                 request_id = g_array_index(out_param1, int, 0);
522                                 BT_DBG("request_id : %d", request_id);
523                                 _bt_add_push_request_id(request_id);
524                         } else if (cb_data->service_function == BT_MAP_LIST_MESSAGES) {
525                                 request_id = g_array_index(out_param1, int, 0);
526                                 BT_DBG("request_id : %d", request_id);
527                                 _bt_add_push_request_id(request_id);
528                         } else if (cb_data->service_function == BT_MAP_GET_MESSAGE) {
529                                 request_id = g_array_index(out_param1, int, 0);
530                                 BT_DBG("request_id : %d", request_id);
531                                 _bt_add_push_request_id(request_id);
532                         } else if (cb_data->service_function == BT_MAP_PUSH_MESSAGE) {
533                                 request_id = g_array_index(out_param1, int, 0);
534                                 BT_DBG("request_id : %d", request_id);
535                                 _bt_add_push_request_id(request_id);
536                         }
537
538                         goto done;
539                 }
540         }
541
542         if (cb_data->cb == NULL)
543                 goto done;
544
545         /* Only if fail case, call the callback function*/
546         bt_event.result = result;
547         BT_INFO("event_type[%d], result= %s [0x%x]", event_type,
548                 _bt_convert_error_to_string(result), result);
549
550         if (event_type == BT_ADAPTER_EVENT || event_type == BT_RFCOMM_CLIENT_EVENT) {
551                 ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
552                                 &bt_event,
553                                 cb_data->user_data);
554         } else if (event_type == BT_HID_EVENT) {
555                 ((hid_cb_func_ptr)cb_data->cb)(bt_event.event,
556                                 (hid_event_param_t *)&bt_event,
557                                 cb_data->user_data);
558         } else if (event_type == BT_HEADSET_EVENT) {
559                 ((bt_audio_func_ptr)cb_data->cb)(bt_event.event,
560                                 (bt_audio_event_param_t *)&bt_event,
561                                 cb_data->user_data);
562         } else if (event_type == BT_HF_AGENT_EVENT) {
563                 ((bt_audio_func_ptr)cb_data->cb)(bt_event.event,
564                                 (bt_audio_event_param_t *)&bt_event,
565                                 cb_data->user_data);
566         } else if (event_type == BT_AVRCP_CONTROL_EVENT) {
567                 ((media_cb_func_ptr)cb_data->cb)(bt_event.event,
568                                 (media_event_param_t *)&bt_event,
569                                 cb_data->user_data);
570         } else if (event_type == BT_A2DP_SOURCE_EVENT) {
571                 ((bt_audio_func_ptr)cb_data->cb)(bt_event.event,
572                                 (bt_audio_event_param_t *)&bt_event,
573                                 cb_data->user_data);
574         } else if (event_type == BT_DEVICE_EVENT) {
575                 ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
576                                 &bt_event,
577                                 cb_data->user_data);
578         } else if (event_type == BT_TDS_EVENT) {
579                 ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
580                                 &bt_event,
581                                 cb_data->user_data);
582         } else if (event_type == BT_HDP_EVENT) {
583                 ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
584                         &bt_event, cb_data->user_data);
585         } else if (event_type == BT_AVRCP_EVENT) {
586                 ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
587                                 &bt_event, cb_data->user_data);
588 #ifdef TIZEN_GATT_CLIENT
589         } else if (event_type == BT_GATT_CLIENT_EVENT) {
590                 ((gatt_client_cb_func_ptr)cb_data->cb)(bt_event.event,
591                                 (gatt_client_event_param_t*)&bt_event, cb_data->user_data);
592 #endif
593         } else if (event_type == BT_MESH_EVENT) {
594                 ((mesh_cb_func_ptr)cb_data->cb)(bt_event.event,
595                                 (mesh_event_param_t*)&bt_event, cb_data->user_data);
596         } else {
597                 BT_INFO("Not handled event type : %d", event_type);
598         }
599 done:
600         if (out_param1)
601                 g_array_free(out_param1, TRUE);
602
603         sending_requests = g_slist_remove(sending_requests, (void *)cb_data);
604
605         g_free(cb_data);
606         BT_DBG("-");
607 }
608
609 int _bt_sync_send_request(int service_type, int service_function,
610                         GArray *in_param1, GArray *in_param2,
611                         GArray *in_param3, GArray *in_param4,
612                         GArray **out_param1)
613 {
614         int result = BLUETOOTH_ERROR_NONE;
615         GError *error = NULL;
616         GArray *in_param5 = NULL;
617 //      GArray *out_param2 = NULL;
618
619         GDBusProxy  *proxy;
620         GVariant *ret;
621         GVariant *param1;
622         GVariant *param2;
623         GVariant *param3;
624         GVariant *param4;
625         GVariant *param5;
626
627         switch (service_type) {
628         case BT_BLUEZ_SERVICE:
629         case BT_OBEX_SERVICE:
630         case BT_AGENT_SERVICE:
631         case BT_CHECK_PRIVILEGE:
632                 proxy = __bt_gdbus_get_service_proxy();
633                 if (!proxy)
634                         return BLUETOOTH_ERROR_INTERNAL;
635
636                 in_param5 = g_array_new(TRUE, TRUE, sizeof(gchar));
637
638
639
640                 param1 = g_variant_new_from_data((const GVariantType *)"ay",
641                                         in_param1->data, in_param1->len,
642                                         TRUE, NULL, NULL);
643                 param2 = g_variant_new_from_data((const GVariantType *)"ay",
644                                         in_param2->data, in_param2->len,
645                                         TRUE, NULL, NULL);
646                 param3 = g_variant_new_from_data((const GVariantType *)"ay",
647                                         in_param3->data, in_param3->len,
648                                         TRUE, NULL, NULL);
649                 param4 = g_variant_new_from_data((const GVariantType *)"ay",
650                                         in_param4->data, in_param4->len,
651                                         TRUE, NULL, NULL);
652                 param5 = g_variant_new_from_data((const GVariantType *)"ay",
653                                         in_param5->data, in_param5->len,
654                                         TRUE, NULL, NULL);
655
656                 ret = g_dbus_proxy_call_sync(proxy, "service_request",
657                                         g_variant_new("(iii@ay@ay@ay@ay@ay)",
658                                                 service_type, service_function,
659                                                 BT_SYNC_REQ, param1,
660                                                 param2, param3,
661                                                 param4, param5),
662                                         G_DBUS_CALL_FLAGS_NONE, -1,
663                                         NULL, &error);
664
665                 g_array_free(in_param5, TRUE);
666
667                 if (ret == NULL) {
668                         /* dBUS-RPC is failed */
669                         BT_ERR("dBUS-RPC is failed");
670
671                         if (error != NULL) {
672                                 /* dBUS gives error cause */
673                                 BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
674                                        error->code, error->message);
675
676                                 g_clear_error(&error);
677                         } else {
678                                 /* dBUS does not give error cause dBUS-RPC is failed */
679                                 BT_ERR("error returned was NULL");
680                         }
681
682                         return BLUETOOTH_ERROR_INTERNAL;
683                 }
684
685                 param1 = NULL;
686 //              param2 = NULL;
687
688                 g_variant_get(ret, "(iv)", &result, &param1);
689
690                 if (param1) {
691                         *out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
692                         __bt_fill_garray_from_variant(param1, *out_param1);
693                         g_variant_unref(param1);
694                 }
695
696 //              if (param2) {
697 //                      out_param2 = g_array_new(TRUE, TRUE, sizeof(gchar));
698 //                      __bt_fill_garray_from_variant(param2, out_param2);
699 //                      result = g_array_index(out_param2, int, 0);
700 //                      g_variant_unref(param2);
701 //                      g_array_free(out_param2, TRUE);
702 //              } else {
703 //                      result = BLUETOOTH_ERROR_INTERNAL;
704 //              }
705
706                 g_variant_unref(ret);
707                 break;
708         default:
709                 BT_ERR("Unknown service type");
710                 return BLUETOOTH_ERROR_INTERNAL;
711         }
712
713         return result;
714 }
715
716 int _bt_async_send_request(int service_type, int service_function,
717                         GArray *in_param1, GArray *in_param2,
718                         GArray *in_param3, GArray *in_param4,
719                         void *callback, void *user_data)
720 {
721         GArray* in_param5 = NULL;
722         bt_req_info_t *cb_data;
723
724         GDBusProxy *proxy;
725         int timeout;
726         GVariant *param1;
727         GVariant *param2;
728         GVariant *param3;
729         GVariant *param4;
730         GVariant *param5;
731
732         BT_DBG("service_function : %s (0x%x)",
733                         _bt_convert_service_function_to_string(service_function),
734                         service_function);
735
736         cb_data = g_new0(bt_req_info_t, 1);
737
738         cb_data->service_function = service_function;
739         cb_data->cb = callback;
740         cb_data->user_data = user_data;
741
742         switch (service_type) {
743         case BT_BLUEZ_SERVICE:
744         case BT_OBEX_SERVICE:
745                 proxy =  __bt_gdbus_get_service_proxy();
746                 if (!proxy) {
747                         g_free(cb_data);
748                         return BLUETOOTH_ERROR_INTERNAL;
749                 }
750
751                 /* Do not timeout the request in certain cases. Sometime the
752                  * request may take undeterministic time to reponse.
753                  * (for ex: pairing retry) */
754                 if (service_function == BT_BOND_DEVICE ||
755                         service_function == BT_BOND_DEVICE_BY_TYPE)
756                         timeout = INT_MAX;
757                 else
758                         timeout = BT_DBUS_TIMEOUT_MAX;
759
760                 in_param5 = g_array_new(TRUE, TRUE, sizeof(gchar));
761
762                 param1 = g_variant_new_from_data((const GVariantType *)"ay",
763                                         in_param1->data, in_param1->len,
764                                         TRUE, NULL, NULL);
765                 param2 = g_variant_new_from_data((const GVariantType *)"ay",
766                                         in_param2->data, in_param2->len,
767                                         TRUE, NULL, NULL);
768                 param3 = g_variant_new_from_data((const GVariantType *)"ay",
769                                         in_param3->data, in_param3->len,
770                                         TRUE, NULL, NULL);
771                 param4 = g_variant_new_from_data((const GVariantType *)"ay",
772                                         in_param4->data, in_param4->len,
773                                         TRUE, NULL, NULL);
774                 param5 = g_variant_new_from_data((const GVariantType *)"ay",
775                                         in_param5->data, in_param5->len,
776                                         TRUE, NULL, NULL);
777
778                 g_dbus_proxy_call(proxy, "service_request",
779                                         g_variant_new("(iii@ay@ay@ay@ay@ay)",
780                                                 service_type, service_function,
781                                                 BT_ASYNC_REQ, param1, param2,
782                                                 param3, param4, param5),
783                                         G_DBUS_CALL_FLAGS_NONE,
784                                         timeout, NULL,
785                                         (GAsyncReadyCallback)__send_request_cb,
786                                         (gpointer)cb_data);
787                 sending_requests = g_slist_append(sending_requests, cb_data);
788
789                 g_array_free(in_param5, TRUE);
790                 break;
791         default:
792                 g_free(cb_data);
793                 break;
794         }
795
796         return BLUETOOTH_ERROR_NONE;
797 }
798
799 int _bt_async_send_request_with_unix_fd_list(int service_type, int service_function,
800                         GArray *in_param1, GArray *in_param2,
801                         GArray *in_param3, GArray *in_param4,
802                         void *callback, void *user_data,
803                         GUnixFDList *fd_list, GAsyncReadyCallback __async_req_cb)
804 {
805         GArray* in_param5 = NULL;
806         bt_req_info_t *cb_data;
807
808         GDBusProxy *proxy;
809         int timeout;
810         GVariant *param1;
811         GVariant *param2;
812         GVariant *param3;
813         GVariant *param4;
814         GVariant *param5;
815
816         BT_DBG("service_function : %d", service_function);
817
818         cb_data = g_new0(bt_req_info_t, 1);
819         cb_data->service_function = service_function;
820         cb_data->cb = callback;
821         cb_data->user_data = user_data;
822
823         switch (service_type) {
824         case BT_BLUEZ_SERVICE:
825         case BT_OBEX_SERVICE:
826                 proxy =  __bt_gdbus_get_service_proxy();
827                 if (!proxy) {
828                         g_free(cb_data);
829                         return BLUETOOTH_ERROR_INTERNAL;
830                 }
831
832                 /* Do not timeout the request in certain cases. Sometime the
833                  * request may take undeterministic time to reponse.
834                  * (for ex: pairing retry) */
835                 if (service_function == BT_BOND_DEVICE ||
836                                 service_function == BT_BOND_DEVICE_BY_TYPE)
837                         timeout = INT_MAX;
838                 else
839                         timeout = BT_DBUS_TIMEOUT_MAX;
840
841                 in_param5 = g_array_new(TRUE, TRUE, sizeof(gchar));
842
843                 param1 = g_variant_new_from_data((const GVariantType *)"ay",
844                                 in_param1->data, in_param1->len,
845                                 TRUE, NULL, NULL);
846                 param2 = g_variant_new_from_data((const GVariantType *)"ay",
847                                 in_param2->data, in_param2->len,
848                                 TRUE, NULL, NULL);
849                 param3 = g_variant_new_from_data((const GVariantType *)"ay",
850                                 in_param3->data, in_param3->len,
851                                 TRUE, NULL, NULL);
852                 param4 = g_variant_new_from_data((const GVariantType *)"ay",
853                                 in_param4->data, in_param4->len,
854                                 TRUE, NULL, NULL);
855                 param5 = g_variant_new_from_data((const GVariantType *)"ay",
856                                 in_param5->data, in_param5->len,
857                                 TRUE, NULL, NULL);
858
859                 g_dbus_proxy_call_with_unix_fd_list(proxy, "service_request",
860                                 g_variant_new("(iii@ay@ay@ay@ay@ay)",
861                                         service_type, service_function,
862                                         BT_ASYNC_REQ, param1, param2,
863                                         param3, param4, param5),
864                                 G_DBUS_CALL_FLAGS_NONE,
865                                 timeout, fd_list, NULL,
866                                 __async_req_cb, (gpointer)cb_data);
867                 sending_requests = g_slist_append(sending_requests, cb_data);
868
869                 g_array_free(in_param5, TRUE);
870                 break;
871         default:
872                 g_free(cb_data);
873                 break;
874         }
875
876         return BLUETOOTH_ERROR_NONE;
877 }
878
879 int _bt_sync_send_request_with_unix_fd_list(
880                 int service_type, int service_function,
881                 GArray *in_param1, GArray *in_param2,
882                 GArray *in_param3, GArray *in_param4,
883                 GUnixFDList *fd_list, GArray **out_param1,
884                 GUnixFDList **out_fd_list)
885 {
886         int result = BLUETOOTH_ERROR_NONE;
887         GError *error = NULL;
888         GArray *in_param5 = NULL;
889
890         GDBusProxy  *proxy;
891         GVariant *ret;
892         GVariant *param1;
893         GVariant *param2;
894         GVariant *param3;
895         GVariant *param4;
896         GVariant *param5;
897
898         switch (service_type) {
899         case BT_BLUEZ_SERVICE:
900         case BT_OBEX_SERVICE:
901         case BT_AGENT_SERVICE:
902         case BT_CHECK_PRIVILEGE:
903                 proxy = __bt_gdbus_get_service_proxy();
904                 if (!proxy)
905                         return BLUETOOTH_ERROR_INTERNAL;
906
907                 in_param5 = g_array_new(TRUE, TRUE, sizeof(gchar));
908
909                 param1 = g_variant_new_from_data((const GVariantType *)"ay",
910                                         in_param1->data, in_param1->len,
911                                         TRUE, NULL, NULL);
912                 param2 = g_variant_new_from_data((const GVariantType *)"ay",
913                                         in_param2->data, in_param2->len,
914                                         TRUE, NULL, NULL);
915                 param3 = g_variant_new_from_data((const GVariantType *)"ay",
916                                         in_param3->data, in_param3->len,
917                                         TRUE, NULL, NULL);
918                 param4 = g_variant_new_from_data((const GVariantType *)"ay",
919                                         in_param4->data, in_param4->len,
920                                         TRUE, NULL, NULL);
921                 param5 = g_variant_new_from_data((const GVariantType *)"ay",
922                                         in_param5->data, in_param5->len,
923                                         TRUE, NULL, NULL);
924
925                 ret = g_dbus_proxy_call_with_unix_fd_list_sync(proxy, "service_request",
926                                 g_variant_new("(iii@ay@ay@ay@ay@ay)",
927                                         service_type, service_function,
928                                         BT_SYNC_REQ, param1, param2,
929                                         param3, param4, param5),
930                                 G_DBUS_CALL_FLAGS_NONE, -1,
931                                 fd_list, out_fd_list, NULL, &error);
932                 g_array_free(in_param5, TRUE);
933
934                 if (ret == NULL) {
935                         /* dBUS-RPC is failed */
936                         BT_ERR("dBUS-RPC is failed");
937
938                         if (error != NULL) {
939                                 /* dBUS gives error cause */
940                                 BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
941                                        error->code, error->message);
942
943                                 g_clear_error(&error);
944                         } else {
945                                 /* dBUS does not give error cause dBUS-RPC is failed */
946                                 BT_ERR("error returned was NULL");
947                         }
948
949                         return BLUETOOTH_ERROR_INTERNAL;
950                 }
951
952                 param1 = NULL;
953                 g_variant_get(ret, "(iv)", &result, &param1);
954
955                 if (param1) {
956                         *out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
957                         __bt_fill_garray_from_variant(param1, *out_param1);
958                         g_variant_unref(param1);
959                 }
960
961                 g_variant_unref(ret);
962                 break;
963         default:
964                 BT_ERR("Unknown service type");
965                 return BLUETOOTH_ERROR_INTERNAL;
966         }
967
968         return result;
969 }