4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Girishashok Joshi <girish.joshi@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
24 #include <dbus/dbus-glib.h>
29 #include "bluetooth-api.h"
30 #include "bluetooth-hid-api.h"
31 #include "bluetooth-audio-api.h"
32 #include "bt-internal-types.h"
34 #include "bt-common.h"
35 #include "bt-request-sender.h"
36 #include "bt-event-handler.h"
37 #include "bluetooth-media-control.h"
39 /* auto generated header by bt-request-service.xml*/
40 #include "bt-request-service.h"
42 static GSList *sending_requests;
44 DBusGConnection *service_conn;
45 DBusGConnection *system_conn;
46 DBusGProxy *service_proxy;
48 static GDBusConnection *service_gconn;
49 static GDBusProxy *service_gproxy;
51 static GDBusProxy *__bt_gdbus_init_service_proxy(void)
58 if (service_gconn == NULL)
59 service_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
63 BT_ERR("Unable to connect to dbus: %s", err->message);
69 proxy = g_dbus_proxy_new_sync(service_gconn,
70 G_DBUS_PROXY_FLAGS_NONE, NULL,
77 BT_ERR("Unable to create proxy: %s", err->message);
81 g_object_unref(service_gconn);
86 service_gproxy = proxy;
91 static GDBusProxy *__bt_gdbus_get_service_proxy(void)
93 return (service_gproxy) ? service_gproxy : __bt_gdbus_init_service_proxy();
96 void _bt_gdbus_deinit_proxys(void)
99 g_object_unref(service_proxy);
100 service_proxy = NULL;
104 g_object_unref(service_gconn);
105 service_gconn = NULL;
109 static void __bt_get_event_info(int service_function, GArray *output,
110 int *event, int *event_type, void **param_data)
112 ret_if(event == NULL);
114 BT_DBG("service_function : %x", service_function);
115 switch (service_function) {
117 case BT_BOND_DEVICE_BY_TYPE:
118 *event_type = BT_ADAPTER_EVENT;
119 *event = BLUETOOTH_EVENT_BONDING_FINISHED;
120 ret_if(output == NULL);
121 *param_data = &g_array_index(output,
122 bluetooth_device_info_t, 0);
124 case BT_UNBOND_DEVICE:
125 *event_type = BT_ADAPTER_EVENT;
126 *event = BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED;
127 ret_if(output == NULL);
128 *param_data = &g_array_index(output,
129 bluetooth_device_info_t, 0);
131 case BT_SEARCH_SERVICE:
132 *event_type = BT_ADAPTER_EVENT;
133 *event = BLUETOOTH_EVENT_SERVICE_SEARCHED;
134 ret_if(output == NULL);
135 *param_data = &g_array_index(output,
136 bluetooth_device_info_t, 0);
139 *event_type = BT_HID_EVENT;
140 *event = BLUETOOTH_HID_CONNECTED;
141 ret_if(output == NULL);
142 *param_data = &g_array_index(output,
143 bluetooth_device_address_t, 0);
145 case BT_HID_DISCONNECT:
146 *event_type = BT_HID_EVENT;
147 *event = BLUETOOTH_HID_DISCONNECTED;
148 ret_if(output == NULL);
149 *param_data = &g_array_index(output,
150 bluetooth_device_address_t, 0);
152 case BT_AUDIO_CONNECT:
154 *event_type = BT_HEADSET_EVENT;
155 *event = BLUETOOTH_EVENT_AG_CONNECTED;
156 ret_if(output == NULL);
157 *param_data = &g_array_index(output, char, 0);
159 case BT_AUDIO_DISCONNECT:
160 case BT_AG_DISCONNECT:
161 *event_type = BT_HEADSET_EVENT;
162 *event = BLUETOOTH_EVENT_AG_DISCONNECTED;
163 ret_if(output == NULL);
164 *param_data = &g_array_index(output, char, 0);
167 *event_type = BT_HEADSET_EVENT;
168 *event = BLUETOOTH_EVENT_AV_CONNECTED;
169 ret_if(output == NULL);
170 *param_data = &g_array_index(output, char, 0);
172 case BT_AV_DISCONNECT:
173 *event_type = BT_HEADSET_EVENT;
174 *event = BLUETOOTH_EVENT_AV_DISCONNECTED;
175 ret_if(output == NULL);
176 *param_data = &g_array_index(output, char, 0);
179 *event_type = BT_HF_AGENT_EVENT;
180 *event = BLUETOOTH_EVENT_HF_CONNECTED;
181 ret_if(output == NULL);
182 *param_data = &g_array_index(output, char, 0);
184 case BT_HF_DISCONNECT:
185 *event_type = BT_HF_AGENT_EVENT;
186 *event = BLUETOOTH_EVENT_HF_DISCONNECTED;
187 ret_if(output == NULL);
188 *param_data = &g_array_index(output, char, 0);
190 case BT_NETWORK_CONNECT:
191 *event_type = BT_ADAPTER_EVENT;
192 *event = BLUETOOTH_EVENT_NETWORK_CONNECTED;
193 ret_if(output == NULL);
194 *param_data = &g_array_index(output,
195 bluetooth_device_address_t, 0);
197 case BT_NETWORK_DISCONNECT:
198 *event_type = BT_ADAPTER_EVENT;
199 *event = BLUETOOTH_EVENT_NETWORK_DISCONNECTED;
200 ret_if(output == NULL);
201 *param_data = &g_array_index(output,
202 bluetooth_device_address_t, 0);
204 case BT_RFCOMM_CLIENT_CONNECT:
205 *event_type = BT_RFCOMM_CLIENT_EVENT;
206 *event = BLUETOOTH_EVENT_RFCOMM_CONNECTED;
207 ret_if(output == NULL);
208 *param_data = &g_array_index(output,
209 bluetooth_rfcomm_connection_t, 0);
211 case BT_AVRCP_CONTROL_CONNECT:
212 *event_type = BT_AVRCP_CONTROL_EVENT;
213 *event = BLUETOOTH_EVENT_AVRCP_CONTROL_CONNECTED;
214 ret_if(output == NULL);
215 *param_data = &g_array_index(output, char, 0);
217 case BT_AVRCP_CONTROL_DISCONNECT:
218 *event_type = BT_AVRCP_CONTROL_EVENT;
219 *event = BLUETOOTH_EVENT_AVRCP_CONTROL_DISCONNECTED;
220 ret_if(output == NULL);
221 *param_data = &g_array_index(output, char, 0);
224 BT_ERR("Unknown function");
230 out param1: API result
231 out param2: return paramter
234 static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
239 size = g_variant_get_size(var);
241 data = (char *)g_variant_get_data(var);
243 param = g_array_append_vals(param, data, size);
248 static void __send_request_cb(GDBusProxy *proxy,
252 bluetooth_event_param_t bt_event;
253 bt_req_info_t *cb_data = user_data;
254 int result = BLUETOOTH_ERROR_NONE;
255 int event_type = BT_ADAPTER_EVENT;
257 GError *error = NULL;
261 GArray *out_param1 = NULL;
262 GArray *out_param2 = NULL;
265 memset(&bt_event, 0x00, sizeof(bluetooth_event_param_t));
267 value = g_dbus_proxy_call_finish(proxy, res, &error);
270 /* dBUS gives error cause */
271 BT_ERR("D-Bus API failure: message[%s]",
273 g_clear_error(&error);
275 result = BLUETOOTH_ERROR_TIMEOUT;
277 ret_if(cb_data == NULL);
279 __bt_get_event_info(cb_data->service_function, NULL,
280 &bt_event.event, &event_type,
281 &bt_event.param_data);
283 g_variant_get(value, "(@ay@ay)", ¶m1, ¶m2);
284 g_variant_unref(value);
287 out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
288 __bt_fill_garray_from_variant(param1, out_param1);
289 g_variant_unref(param1);
293 out_param2 = g_array_new(TRUE, TRUE, sizeof(gchar));
294 __bt_fill_garray_from_variant(param2, out_param2);
295 result = g_array_index(out_param2, int, 0);
296 g_variant_unref(param2);
297 g_array_free(out_param2, TRUE);
299 result = BLUETOOTH_ERROR_INTERNAL;
302 ret_if(cb_data == NULL);
304 __bt_get_event_info(cb_data->service_function, out_param1,
305 &bt_event.event, &event_type,
306 &bt_event.param_data);
308 if (result == BLUETOOTH_ERROR_NONE && out_param1) {
309 if (cb_data->service_function == BT_OPP_PUSH_FILES) {
310 request_id = g_array_index(out_param1, int, 0);
311 BT_DBG("request_id : %d", request_id);
312 _bt_add_push_request_id(request_id);
320 if (cb_data->cb == NULL)
323 /* Only if fail case, call the callback function*/
324 bt_event.result = result;
325 BT_INFO("event_type[%d], result=[%d]", event_type, result);
327 if (event_type == BT_ADAPTER_EVENT || event_type == BT_RFCOMM_CLIENT_EVENT) {
328 ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
331 } else if (event_type == BT_HID_EVENT) {
332 ((hid_cb_func_ptr)cb_data->cb)(bt_event.event,
333 (hid_event_param_t *)&bt_event,
335 } else if (event_type == BT_HEADSET_EVENT) {
336 ((bt_audio_func_ptr)cb_data->cb)(bt_event.event,
337 (bt_audio_event_param_t *)&bt_event,
339 } else if (event_type == BT_HF_AGENT_EVENT) {
340 ((bt_audio_func_ptr)cb_data->cb)(bt_event.event,
341 (bt_audio_event_param_t *)&bt_event,
343 } else if (event_type == BT_AVRCP_CONTROL_EVENT) {
344 ((media_cb_func_ptr)cb_data->cb)(bt_event.event,
345 (media_event_param_t *)&bt_event,
348 BT_INFO("Not handled event type : %d", event_type);
352 g_array_free(out_param1, TRUE);
354 sending_requests = g_slist_remove(sending_requests, (void *)cb_data);
360 int _bt_sync_send_request(int service_type, int service_function,
361 GArray *in_param1, GArray *in_param2,
362 GArray *in_param3, GArray *in_param4,
365 int result = BLUETOOTH_ERROR_NONE;
367 GError *error = NULL;
368 GArray *in_param5 = NULL;
369 GArray *out_param2 = NULL;
379 switch (service_type) {
380 case BT_BLUEZ_SERVICE:
381 case BT_OBEX_SERVICE:
382 case BT_AGENT_SERVICE:
383 case BT_CHECK_PRIVILEGE:
384 proxy = __bt_gdbus_get_service_proxy();
386 return BLUETOOTH_ERROR_INTERNAL;
388 in_param5 = g_array_new(TRUE, TRUE, sizeof(gchar));
390 cookie = _bt_get_cookie();
393 g_array_append_vals(in_param5, cookie,
394 _bt_get_cookie_size());
397 param1 = g_variant_new_from_data((const GVariantType *)"ay",
398 in_param1->data, in_param1->len,
400 param2 = g_variant_new_from_data((const GVariantType *)"ay",
401 in_param2->data, in_param2->len,
403 param3 = g_variant_new_from_data((const GVariantType *)"ay",
404 in_param3->data, in_param3->len,
406 param4 = g_variant_new_from_data((const GVariantType *)"ay",
407 in_param4->data, in_param4->len,
409 param5 = g_variant_new_from_data((const GVariantType *)"ay",
410 in_param5->data, in_param5->len,
413 ret = g_dbus_proxy_call_sync(proxy, "service_request",
414 g_variant_new("(iii@ay@ay@ay@ay@ay)",
415 service_type, service_function,
419 G_DBUS_CALL_FLAGS_NONE, -1,
422 g_array_free(in_param5, TRUE);
425 /* dBUS-RPC is failed */
426 BT_ERR("dBUS-RPC is failed");
429 /* dBUS gives error cause */
430 BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
431 error->code, error->message);
433 g_clear_error(&error);
435 /* dBUS does not give error cause dBUS-RPC is failed */
436 BT_ERR("error returned was NULL");
439 return BLUETOOTH_ERROR_INTERNAL;
445 g_variant_get(ret, "(@ay@ay)", ¶m1, ¶m2);
448 *out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
449 __bt_fill_garray_from_variant(param1, *out_param1);
450 g_variant_unref(param1);
454 out_param2 = g_array_new(TRUE, TRUE, sizeof(gchar));
455 __bt_fill_garray_from_variant(param2, out_param2);
456 result = g_array_index(out_param2, int, 0);
457 g_variant_unref(param2);
458 g_array_free(out_param2, TRUE);
460 result = BLUETOOTH_ERROR_INTERNAL;
463 g_variant_unref(ret);
466 BT_ERR("Unknown service type");
467 return BLUETOOTH_ERROR_INTERNAL;
473 int _bt_async_send_request(int service_type, int service_function,
474 GArray *in_param1, GArray *in_param2,
475 GArray *in_param3, GArray *in_param4,
476 void *callback, void *user_data)
478 GArray* in_param5 = NULL;
479 bt_req_info_t *cb_data;
490 BT_DBG("service_function : %x", service_function);
492 cb_data = g_new0(bt_req_info_t, 1);
494 cb_data->service_function = service_function;
495 cb_data->cb = callback;
496 cb_data->user_data = user_data;
498 switch (service_type) {
499 case BT_BLUEZ_SERVICE:
500 case BT_OBEX_SERVICE:
501 proxy = __bt_gdbus_get_service_proxy();
504 return BLUETOOTH_ERROR_INTERNAL;
507 /* Do not timeout the request in certain cases. Sometime the
508 * request may take undeterministic time to reponse.
509 * (for ex: pairing retry) */
510 if (service_function == BT_BOND_DEVICE ||
511 service_function == BT_BOND_DEVICE_BY_TYPE)
514 timeout = BT_DBUS_TIMEOUT_MAX;
516 in_param5 = g_array_new(TRUE, TRUE, sizeof(gchar));
518 cookie = _bt_get_cookie();
521 g_array_append_vals(in_param5, cookie,
522 _bt_get_cookie_size());
524 param1 = g_variant_new_from_data((const GVariantType *)"ay",
525 in_param1->data, in_param1->len,
527 param2 = g_variant_new_from_data((const GVariantType *)"ay",
528 in_param2->data, in_param2->len,
530 param3 = g_variant_new_from_data((const GVariantType *)"ay",
531 in_param3->data, in_param3->len,
533 param4 = g_variant_new_from_data((const GVariantType *)"ay",
534 in_param4->data, in_param4->len,
536 param5 = g_variant_new_from_data((const GVariantType *)"ay",
537 in_param5->data, in_param5->len,
540 g_dbus_proxy_call(proxy, "service_request",
541 g_variant_new("(iii@ay@ay@ay@ay@ay)",
542 service_type, service_function,
543 BT_ASYNC_REQ, param1, param2,
544 param3, param4, param5),
545 G_DBUS_CALL_FLAGS_NONE,
547 (GAsyncReadyCallback)__send_request_cb,
549 sending_requests = g_slist_append(sending_requests, cb_data);
551 g_array_free(in_param5, TRUE);
555 return BLUETOOTH_ERROR_NONE;