Mesh: Add framework event handlers
[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_NODE_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_CONFIG_GROUP_SUB:
377                 *event_type = BT_MESH_EVENT;
378                 *event = BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_CONFGURED;
379                 ret_if(output == NULL);
380                 *param_data = &g_array_index(output,
381                                 bluetooth_mesh_model_configure_t, 0);
382                 break;
383         case BT_MESH_MODEL_GET_APPKEY_LIST:
384                 *event_type = BT_MESH_EVENT;
385                 *event = BLUETOOTH_EVENT_MESH_MODEL_APPKEY_LIST;
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_SUBSCRIPTION_LIST:
391                 *event_type = BT_MESH_EVENT;
392                 *event = BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_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_CONFIG_VIRTUAL_GROUP_SUB:
398                 *event_type = BT_MESH_EVENT;
399                 *event = BLUETOOTH_EVENT_MESH_MODEL_VIRTUAL_SUBSCRIPTION_CONFGURED;
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_SET_PUBLICATION:
405                 *event_type = BT_MESH_EVENT;
406                 *event = BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS;
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_GET_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         default:
419                 BT_ERR("Unknown function");
420                 return;
421         }
422 }
423
424 /*
425 out param1: API result
426 out param2: return paramter
427 out param3:
428 */
429 static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
430 {
431         char *data;
432         int size;
433
434         size = g_variant_get_size(var);
435         if (size > 0) {
436                 data = (char *)g_variant_get_data(var);
437                 if (data)
438                         param = g_array_append_vals(param, data, size);
439
440         }
441 }
442
443 static void __send_request_cb(GDBusProxy *proxy,
444                                 GAsyncResult *res,
445                                 gpointer user_data)
446 {
447         bluetooth_event_param_t bt_event;
448         bt_req_info_t *cb_data = user_data;
449         int result = BLUETOOTH_ERROR_NONE;
450         int event_type = BT_ADAPTER_EVENT;
451         int request_id;
452         GError *error = NULL;
453         GVariant *value;
454         GVariant *param1;
455 //      GVariant *param2;
456         GArray *out_param1 = NULL;
457 //      GArray *out_param2 = NULL;
458
459         BT_DBG("+");
460         memset(&bt_event, 0x00, sizeof(bluetooth_event_param_t));
461
462         value = g_dbus_proxy_call_finish(proxy, res, &error);
463         if (value == NULL) {
464                 if (error) {
465                         /* dBUS gives error cause */
466                         BT_ERR("D-Bus API failure: message[%s]",
467                                                         error->message);
468                         g_clear_error(&error);
469                 }
470                 result = BLUETOOTH_ERROR_TIMEOUT;
471
472                 ret_if(cb_data == NULL);
473
474                 __bt_get_event_info(cb_data->service_function, NULL,
475                                 &bt_event.event, &event_type,
476                                 &bt_event.param_data);
477         } else {
478                 g_variant_get(value, "(iv)", &result, &param1);
479                 g_variant_unref(value);
480
481                 if (param1) {
482                         out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
483                         __bt_fill_garray_from_variant(param1, out_param1);
484                         g_variant_unref(param1);
485                 }
486
487 //              if (param2) {
488 //                      out_param2 = g_array_new(TRUE, TRUE, sizeof(gchar));
489 //                      __bt_fill_garray_from_variant(param2, out_param2);
490 //                      result = g_array_index(out_param2, int, 0);
491 //                      g_variant_unref(param2);
492 //                      g_array_free(out_param2, TRUE);
493 //              } else {
494 //                      result = BLUETOOTH_ERROR_INTERNAL;
495 //              }
496
497                 ret_if(cb_data == NULL);
498
499                 __bt_get_event_info(cb_data->service_function, out_param1,
500                                 &bt_event.event, &event_type,
501                                 &bt_event.param_data);
502
503                 BT_DBG("service_function [%d]", cb_data->service_function);
504                 if (result == BLUETOOTH_ERROR_NONE && out_param1) {
505                         if (cb_data->service_function == BT_OPP_PUSH_FILES) {
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_LIST_FOLDERS) {
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                         } else if (cb_data->service_function == BT_MAP_LIST_FILTER_FIELDS) {
514                                 request_id = g_array_index(out_param1, int, 0);
515                                 BT_DBG("request_id : %d", request_id);
516                                 _bt_add_push_request_id(request_id);
517                         } else if (cb_data->service_function == BT_MAP_LIST_MESSAGES) {
518                                 request_id = g_array_index(out_param1, int, 0);
519                                 BT_DBG("request_id : %d", request_id);
520                                 _bt_add_push_request_id(request_id);
521                         } else if (cb_data->service_function == BT_MAP_GET_MESSAGE) {
522                                 request_id = g_array_index(out_param1, int, 0);
523                                 BT_DBG("request_id : %d", request_id);
524                                 _bt_add_push_request_id(request_id);
525                         } else if (cb_data->service_function == BT_MAP_PUSH_MESSAGE) {
526                                 request_id = g_array_index(out_param1, int, 0);
527                                 BT_DBG("request_id : %d", request_id);
528                                 _bt_add_push_request_id(request_id);
529                         }
530
531                         goto done;
532                 }
533         }
534
535         if (cb_data->cb == NULL)
536                 goto done;
537
538         /* Only if fail case, call the callback function*/
539         bt_event.result = result;
540         BT_INFO("event_type[%d], result= %s [0x%x]", event_type,
541                 _bt_convert_error_to_string(result), result);
542
543         if (event_type == BT_ADAPTER_EVENT || event_type == BT_RFCOMM_CLIENT_EVENT) {
544                 ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
545                                 &bt_event,
546                                 cb_data->user_data);
547         } else if (event_type == BT_HID_EVENT) {
548                 ((hid_cb_func_ptr)cb_data->cb)(bt_event.event,
549                                 (hid_event_param_t *)&bt_event,
550                                 cb_data->user_data);
551         } else if (event_type == BT_HEADSET_EVENT) {
552                 ((bt_audio_func_ptr)cb_data->cb)(bt_event.event,
553                                 (bt_audio_event_param_t *)&bt_event,
554                                 cb_data->user_data);
555         } else if (event_type == BT_HF_AGENT_EVENT) {
556                 ((bt_audio_func_ptr)cb_data->cb)(bt_event.event,
557                                 (bt_audio_event_param_t *)&bt_event,
558                                 cb_data->user_data);
559         } else if (event_type == BT_AVRCP_CONTROL_EVENT) {
560                 ((media_cb_func_ptr)cb_data->cb)(bt_event.event,
561                                 (media_event_param_t *)&bt_event,
562                                 cb_data->user_data);
563         } else if (event_type == BT_A2DP_SOURCE_EVENT) {
564                 ((bt_audio_func_ptr)cb_data->cb)(bt_event.event,
565                                 (bt_audio_event_param_t *)&bt_event,
566                                 cb_data->user_data);
567         } else if (event_type == BT_DEVICE_EVENT) {
568                 ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
569                                 &bt_event,
570                                 cb_data->user_data);
571         } else if (event_type == BT_TDS_EVENT) {
572                 ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
573                                 &bt_event,
574                                 cb_data->user_data);
575         } else if (event_type == BT_HDP_EVENT) {
576                 ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
577                         &bt_event, cb_data->user_data);
578         } else if (event_type == BT_AVRCP_EVENT) {
579                 ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
580                                 &bt_event, cb_data->user_data);
581 #ifdef TIZEN_GATT_CLIENT
582         } else if (event_type == BT_GATT_CLIENT_EVENT) {
583                 ((gatt_client_cb_func_ptr)cb_data->cb)(bt_event.event,
584                                 (gatt_client_event_param_t*)&bt_event, cb_data->user_data);
585 #endif
586         } else if (event_type == BT_MESH_EVENT) {
587                 ((mesh_cb_func_ptr)cb_data->cb)(bt_event.event,
588                                 (mesh_event_param_t*)&bt_event, cb_data->user_data);
589         } else {
590                 BT_INFO("Not handled event type : %d", event_type);
591         }
592 done:
593         if (out_param1)
594                 g_array_free(out_param1, TRUE);
595
596         sending_requests = g_slist_remove(sending_requests, (void *)cb_data);
597
598         g_free(cb_data);
599         BT_DBG("-");
600 }
601
602 int _bt_sync_send_request(int service_type, int service_function,
603                         GArray *in_param1, GArray *in_param2,
604                         GArray *in_param3, GArray *in_param4,
605                         GArray **out_param1)
606 {
607         int result = BLUETOOTH_ERROR_NONE;
608         GError *error = NULL;
609         GArray *in_param5 = NULL;
610 //      GArray *out_param2 = NULL;
611
612         GDBusProxy  *proxy;
613         GVariant *ret;
614         GVariant *param1;
615         GVariant *param2;
616         GVariant *param3;
617         GVariant *param4;
618         GVariant *param5;
619
620         switch (service_type) {
621         case BT_BLUEZ_SERVICE:
622         case BT_OBEX_SERVICE:
623         case BT_AGENT_SERVICE:
624         case BT_CHECK_PRIVILEGE:
625                 proxy = __bt_gdbus_get_service_proxy();
626                 if (!proxy)
627                         return BLUETOOTH_ERROR_INTERNAL;
628
629                 in_param5 = g_array_new(TRUE, TRUE, sizeof(gchar));
630
631
632
633                 param1 = g_variant_new_from_data((const GVariantType *)"ay",
634                                         in_param1->data, in_param1->len,
635                                         TRUE, NULL, NULL);
636                 param2 = g_variant_new_from_data((const GVariantType *)"ay",
637                                         in_param2->data, in_param2->len,
638                                         TRUE, NULL, NULL);
639                 param3 = g_variant_new_from_data((const GVariantType *)"ay",
640                                         in_param3->data, in_param3->len,
641                                         TRUE, NULL, NULL);
642                 param4 = g_variant_new_from_data((const GVariantType *)"ay",
643                                         in_param4->data, in_param4->len,
644                                         TRUE, NULL, NULL);
645                 param5 = g_variant_new_from_data((const GVariantType *)"ay",
646                                         in_param5->data, in_param5->len,
647                                         TRUE, NULL, NULL);
648
649                 ret = g_dbus_proxy_call_sync(proxy, "service_request",
650                                         g_variant_new("(iii@ay@ay@ay@ay@ay)",
651                                                 service_type, service_function,
652                                                 BT_SYNC_REQ, param1,
653                                                 param2, param3,
654                                                 param4, param5),
655                                         G_DBUS_CALL_FLAGS_NONE, -1,
656                                         NULL, &error);
657
658                 g_array_free(in_param5, TRUE);
659
660                 if (ret == NULL) {
661                         /* dBUS-RPC is failed */
662                         BT_ERR("dBUS-RPC is failed");
663
664                         if (error != NULL) {
665                                 /* dBUS gives error cause */
666                                 BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
667                                        error->code, error->message);
668
669                                 g_clear_error(&error);
670                         } else {
671                                 /* dBUS does not give error cause dBUS-RPC is failed */
672                                 BT_ERR("error returned was NULL");
673                         }
674
675                         return BLUETOOTH_ERROR_INTERNAL;
676                 }
677
678                 param1 = NULL;
679 //              param2 = NULL;
680
681                 g_variant_get(ret, "(iv)", &result, &param1);
682
683                 if (param1) {
684                         *out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
685                         __bt_fill_garray_from_variant(param1, *out_param1);
686                         g_variant_unref(param1);
687                 }
688
689 //              if (param2) {
690 //                      out_param2 = g_array_new(TRUE, TRUE, sizeof(gchar));
691 //                      __bt_fill_garray_from_variant(param2, out_param2);
692 //                      result = g_array_index(out_param2, int, 0);
693 //                      g_variant_unref(param2);
694 //                      g_array_free(out_param2, TRUE);
695 //              } else {
696 //                      result = BLUETOOTH_ERROR_INTERNAL;
697 //              }
698
699                 g_variant_unref(ret);
700                 break;
701         default:
702                 BT_ERR("Unknown service type");
703                 return BLUETOOTH_ERROR_INTERNAL;
704         }
705
706         return result;
707 }
708
709 int _bt_async_send_request(int service_type, int service_function,
710                         GArray *in_param1, GArray *in_param2,
711                         GArray *in_param3, GArray *in_param4,
712                         void *callback, void *user_data)
713 {
714         GArray* in_param5 = NULL;
715         bt_req_info_t *cb_data;
716
717         GDBusProxy *proxy;
718         int timeout;
719         GVariant *param1;
720         GVariant *param2;
721         GVariant *param3;
722         GVariant *param4;
723         GVariant *param5;
724
725         BT_DBG("service_function : %s (0x%x)",
726                         _bt_convert_service_function_to_string(service_function),
727                         service_function);
728
729         cb_data = g_new0(bt_req_info_t, 1);
730
731         cb_data->service_function = service_function;
732         cb_data->cb = callback;
733         cb_data->user_data = user_data;
734
735         switch (service_type) {
736         case BT_BLUEZ_SERVICE:
737         case BT_OBEX_SERVICE:
738                 proxy =  __bt_gdbus_get_service_proxy();
739                 if (!proxy) {
740                         g_free(cb_data);
741                         return BLUETOOTH_ERROR_INTERNAL;
742                 }
743
744                 /* Do not timeout the request in certain cases. Sometime the
745                  * request may take undeterministic time to reponse.
746                  * (for ex: pairing retry) */
747                 if (service_function == BT_BOND_DEVICE ||
748                         service_function == BT_BOND_DEVICE_BY_TYPE)
749                         timeout = INT_MAX;
750                 else
751                         timeout = BT_DBUS_TIMEOUT_MAX;
752
753                 in_param5 = g_array_new(TRUE, TRUE, sizeof(gchar));
754
755                 param1 = g_variant_new_from_data((const GVariantType *)"ay",
756                                         in_param1->data, in_param1->len,
757                                         TRUE, NULL, NULL);
758                 param2 = g_variant_new_from_data((const GVariantType *)"ay",
759                                         in_param2->data, in_param2->len,
760                                         TRUE, NULL, NULL);
761                 param3 = g_variant_new_from_data((const GVariantType *)"ay",
762                                         in_param3->data, in_param3->len,
763                                         TRUE, NULL, NULL);
764                 param4 = g_variant_new_from_data((const GVariantType *)"ay",
765                                         in_param4->data, in_param4->len,
766                                         TRUE, NULL, NULL);
767                 param5 = g_variant_new_from_data((const GVariantType *)"ay",
768                                         in_param5->data, in_param5->len,
769                                         TRUE, NULL, NULL);
770
771                 g_dbus_proxy_call(proxy, "service_request",
772                                         g_variant_new("(iii@ay@ay@ay@ay@ay)",
773                                                 service_type, service_function,
774                                                 BT_ASYNC_REQ, param1, param2,
775                                                 param3, param4, param5),
776                                         G_DBUS_CALL_FLAGS_NONE,
777                                         timeout, NULL,
778                                         (GAsyncReadyCallback)__send_request_cb,
779                                         (gpointer)cb_data);
780                 sending_requests = g_slist_append(sending_requests, cb_data);
781
782                 g_array_free(in_param5, TRUE);
783                 break;
784         default:
785                 g_free(cb_data);
786                 break;
787         }
788
789         return BLUETOOTH_ERROR_NONE;
790 }
791
792 int _bt_async_send_request_with_unix_fd_list(int service_type, int service_function,
793                         GArray *in_param1, GArray *in_param2,
794                         GArray *in_param3, GArray *in_param4,
795                         void *callback, void *user_data,
796                         GUnixFDList *fd_list, GAsyncReadyCallback __async_req_cb)
797 {
798         GArray* in_param5 = NULL;
799         bt_req_info_t *cb_data;
800
801         GDBusProxy *proxy;
802         int timeout;
803         GVariant *param1;
804         GVariant *param2;
805         GVariant *param3;
806         GVariant *param4;
807         GVariant *param5;
808
809         BT_DBG("service_function : %d", service_function);
810
811         cb_data = g_new0(bt_req_info_t, 1);
812         cb_data->service_function = service_function;
813         cb_data->cb = callback;
814         cb_data->user_data = user_data;
815
816         switch (service_type) {
817         case BT_BLUEZ_SERVICE:
818         case BT_OBEX_SERVICE:
819                 proxy =  __bt_gdbus_get_service_proxy();
820                 if (!proxy) {
821                         g_free(cb_data);
822                         return BLUETOOTH_ERROR_INTERNAL;
823                 }
824
825                 /* Do not timeout the request in certain cases. Sometime the
826                  * request may take undeterministic time to reponse.
827                  * (for ex: pairing retry) */
828                 if (service_function == BT_BOND_DEVICE ||
829                                 service_function == BT_BOND_DEVICE_BY_TYPE)
830                         timeout = INT_MAX;
831                 else
832                         timeout = BT_DBUS_TIMEOUT_MAX;
833
834                 in_param5 = g_array_new(TRUE, TRUE, sizeof(gchar));
835
836                 param1 = g_variant_new_from_data((const GVariantType *)"ay",
837                                 in_param1->data, in_param1->len,
838                                 TRUE, NULL, NULL);
839                 param2 = g_variant_new_from_data((const GVariantType *)"ay",
840                                 in_param2->data, in_param2->len,
841                                 TRUE, NULL, NULL);
842                 param3 = g_variant_new_from_data((const GVariantType *)"ay",
843                                 in_param3->data, in_param3->len,
844                                 TRUE, NULL, NULL);
845                 param4 = g_variant_new_from_data((const GVariantType *)"ay",
846                                 in_param4->data, in_param4->len,
847                                 TRUE, NULL, NULL);
848                 param5 = g_variant_new_from_data((const GVariantType *)"ay",
849                                 in_param5->data, in_param5->len,
850                                 TRUE, NULL, NULL);
851
852                 g_dbus_proxy_call_with_unix_fd_list(proxy, "service_request",
853                                 g_variant_new("(iii@ay@ay@ay@ay@ay)",
854                                         service_type, service_function,
855                                         BT_ASYNC_REQ, param1, param2,
856                                         param3, param4, param5),
857                                 G_DBUS_CALL_FLAGS_NONE,
858                                 timeout, fd_list, NULL,
859                                 __async_req_cb, (gpointer)cb_data);
860                 sending_requests = g_slist_append(sending_requests, cb_data);
861
862                 g_array_free(in_param5, TRUE);
863                 break;
864         default:
865                 g_free(cb_data);
866                 break;
867         }
868
869         return BLUETOOTH_ERROR_NONE;
870 }
871
872 int _bt_sync_send_request_with_unix_fd_list(
873                 int service_type, int service_function,
874                 GArray *in_param1, GArray *in_param2,
875                 GArray *in_param3, GArray *in_param4,
876                 GUnixFDList *fd_list, GArray **out_param1,
877                 GUnixFDList **out_fd_list)
878 {
879         int result = BLUETOOTH_ERROR_NONE;
880         GError *error = NULL;
881         GArray *in_param5 = NULL;
882
883         GDBusProxy  *proxy;
884         GVariant *ret;
885         GVariant *param1;
886         GVariant *param2;
887         GVariant *param3;
888         GVariant *param4;
889         GVariant *param5;
890
891         switch (service_type) {
892         case BT_BLUEZ_SERVICE:
893         case BT_OBEX_SERVICE:
894         case BT_AGENT_SERVICE:
895         case BT_CHECK_PRIVILEGE:
896                 proxy = __bt_gdbus_get_service_proxy();
897                 if (!proxy)
898                         return BLUETOOTH_ERROR_INTERNAL;
899
900                 in_param5 = g_array_new(TRUE, TRUE, sizeof(gchar));
901
902                 param1 = g_variant_new_from_data((const GVariantType *)"ay",
903                                         in_param1->data, in_param1->len,
904                                         TRUE, NULL, NULL);
905                 param2 = g_variant_new_from_data((const GVariantType *)"ay",
906                                         in_param2->data, in_param2->len,
907                                         TRUE, NULL, NULL);
908                 param3 = g_variant_new_from_data((const GVariantType *)"ay",
909                                         in_param3->data, in_param3->len,
910                                         TRUE, NULL, NULL);
911                 param4 = g_variant_new_from_data((const GVariantType *)"ay",
912                                         in_param4->data, in_param4->len,
913                                         TRUE, NULL, NULL);
914                 param5 = g_variant_new_from_data((const GVariantType *)"ay",
915                                         in_param5->data, in_param5->len,
916                                         TRUE, NULL, NULL);
917
918                 ret = g_dbus_proxy_call_with_unix_fd_list_sync(proxy, "service_request",
919                                 g_variant_new("(iii@ay@ay@ay@ay@ay)",
920                                         service_type, service_function,
921                                         BT_SYNC_REQ, param1, param2,
922                                         param3, param4, param5),
923                                 G_DBUS_CALL_FLAGS_NONE, -1,
924                                 fd_list, out_fd_list, NULL, &error);
925                 g_array_free(in_param5, TRUE);
926
927                 if (ret == NULL) {
928                         /* dBUS-RPC is failed */
929                         BT_ERR("dBUS-RPC is failed");
930
931                         if (error != NULL) {
932                                 /* dBUS gives error cause */
933                                 BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
934                                        error->code, error->message);
935
936                                 g_clear_error(&error);
937                         } else {
938                                 /* dBUS does not give error cause dBUS-RPC is failed */
939                                 BT_ERR("error returned was NULL");
940                         }
941
942                         return BLUETOOTH_ERROR_INTERNAL;
943                 }
944
945                 param1 = NULL;
946                 g_variant_get(ret, "(iv)", &result, &param1);
947
948                 if (param1) {
949                         *out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
950                         __bt_fill_garray_from_variant(param1, *out_param1);
951                         g_variant_unref(param1);
952                 }
953
954                 g_variant_unref(ret);
955                 break;
956         default:
957                 BT_ERR("Unknown service type");
958                 return BLUETOOTH_ERROR_INTERNAL;
959         }
960
961         return result;
962 }