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