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