X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=bt-api%2Fbt-request-sender.c;h=b240fb8f2534bee92eee54cd197019a444fd835f;hb=3e62974336300674483a5a4ec350344b72ca8f4e;hp=2438cb363f8dce4b18b94388be5c2495570f5552;hpb=25986f9247de588fc3bb272c5acf6544434f46bf;p=platform%2Fcore%2Fconnectivity%2Fbluetooth-frwk.git diff --git a/bt-api/bt-request-sender.c b/bt-api/bt-request-sender.c index 2438cb3..b240fb8 100644 --- a/bt-api/bt-request-sender.c +++ b/bt-api/bt-request-sender.c @@ -1,13 +1,11 @@ /* - * bluetooth-frwk - * - * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd. + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -17,7 +15,6 @@ * */ -#include #include #include #include @@ -26,84 +23,62 @@ #include "bluetooth-hid-api.h" #include "bluetooth-audio-api.h" #include "bt-internal-types.h" +#include "bluetooth-ipsp-api.h" #include "bt-common.h" #include "bt-request-sender.h" #include "bt-event-handler.h" +#include "bluetooth-media-control.h" /* auto generated header by bt-request-service.xml*/ #include "bt-request-service.h" static GSList *sending_requests; -DBusGConnection *service_conn; -DBusGConnection *system_conn; -DBusGProxy *service_proxy; +static GDBusProxy *service_gproxy; -static void __bt_remove_all_sending_requests(void) +static GDBusProxy *__bt_gdbus_init_service_proxy(void) { - GSList *l; - bt_req_info_t *info; - - for (l = sending_requests; l != NULL; l = g_slist_next(l)) { - info = l->data; - - if (info && info->proxy && info->proxy_call) - dbus_g_proxy_cancel_call(info->proxy, info->proxy_call); - } + GDBusConnection *service_gconn; + GDBusProxy *proxy; + GError *err = NULL; - g_slist_free(sending_requests); - sending_requests = NULL; -} - -DBusGProxy *_bt_init_service_proxy(void) -{ - DBusGProxy *proxy; + service_gconn = _bt_gdbus_get_system_gconn(); - g_type_init(); - - if (service_conn == NULL) { - service_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL); - retv_if(service_conn == NULL, NULL); - } + if (!service_gconn) + return NULL; - proxy = dbus_g_proxy_new_for_name(service_conn, BT_DBUS_NAME, BT_SERVICE_PATH, - BT_DBUS_NAME); + proxy = g_dbus_proxy_new_sync(service_gconn, + G_DBUS_PROXY_FLAGS_NONE, NULL, + BT_DBUS_NAME, + BT_SERVICE_PATH, + BT_DBUS_NAME, + NULL, &err); + if (!proxy) { + if (err) { + BT_ERR("Unable to create proxy: %s", err->message); + g_clear_error(&err); + } - if (proxy == NULL) { - dbus_g_connection_unref(service_conn); - service_conn = NULL; return NULL; } - service_proxy = proxy; + service_gproxy = proxy; return proxy; } -void _bt_deinit_proxys(void) +static GDBusProxy *__bt_gdbus_get_service_proxy(void) { - __bt_remove_all_sending_requests(); - - if (service_proxy) { - g_object_unref(service_proxy); - service_proxy = NULL; - } - - if (service_conn) { - dbus_g_connection_unref(service_conn); - service_conn = NULL; - } - - if (system_conn) { - dbus_g_connection_unref(system_conn); - system_conn = NULL; - } + return (service_gproxy) ? service_gproxy : __bt_gdbus_init_service_proxy(); } -static DBusGProxy *__bt_get_service_proxy(void) +void _bt_gdbus_deinit_proxys(void) { - return (service_proxy) ? service_proxy : _bt_init_service_proxy(); + if (service_gproxy) { + g_object_unref(service_gproxy); + service_gproxy = NULL; + } } static void __bt_get_event_info(int service_function, GArray *output, @@ -111,8 +86,10 @@ static void __bt_get_event_info(int service_function, GArray *output, { ret_if(event == NULL); + BT_DBG("service_function : %x", service_function); switch (service_function) { case BT_BOND_DEVICE: + case BT_BOND_DEVICE_BY_TYPE: *event_type = BT_ADAPTER_EVENT; *event = BLUETOOTH_EVENT_BONDING_FINISHED; ret_if(output == NULL); @@ -173,6 +150,30 @@ static void __bt_get_event_info(int service_function, GArray *output, ret_if(output == NULL); *param_data = &g_array_index(output, char, 0); break; + case BT_AV_SOURCE_CONNECT: + *event_type = BT_A2DP_SOURCE_EVENT; + *event = BLUETOOTH_EVENT_AV_SOURCE_CONNECTED; + ret_if(output == NULL); + *param_data = &g_array_index(output, char, 0); + break; + case BT_AV_SOURCE_DISCONNECT: + *event_type = BT_A2DP_SOURCE_EVENT; + *event = BLUETOOTH_EVENT_AV_SOURCE_DISCONNECTED; + ret_if(output == NULL); + *param_data = &g_array_index(output, char, 0); + break; + case BT_HF_CONNECT: + *event_type = BT_HF_AGENT_EVENT; + *event = BLUETOOTH_EVENT_HF_CONNECTED; + ret_if(output == NULL); + *param_data = &g_array_index(output, char, 0); + break; + case BT_HF_DISCONNECT: + *event_type = BT_HF_AGENT_EVENT; + *event = BLUETOOTH_EVENT_HF_DISCONNECTED; + ret_if(output == NULL); + *param_data = &g_array_index(output, char, 0); + break; case BT_NETWORK_CONNECT: *event_type = BT_ADAPTER_EVENT; *event = BLUETOOTH_EVENT_NETWORK_CONNECTED; @@ -193,7 +194,32 @@ static void __bt_get_event_info(int service_function, GArray *output, ret_if(output == NULL); *param_data = &g_array_index(output, bluetooth_rfcomm_connection_t, 0); - + break; + case BT_AVRCP_CONTROL_CONNECT: + *event_type = BT_AVRCP_CONTROL_EVENT; + *event = BLUETOOTH_EVENT_AVRCP_CONTROL_CONNECTED; + ret_if(output == NULL); + *param_data = &g_array_index(output, char, 0); + break; + case BT_AVRCP_CONTROL_DISCONNECT: + *event_type = BT_AVRCP_CONTROL_EVENT; + *event = BLUETOOTH_EVENT_AVRCP_CONTROL_DISCONNECTED; + ret_if(output == NULL); + *param_data = &g_array_index(output, char, 0); + break; + case BT_CONNECT_LE: + *event_type = BT_DEVICE_EVENT; + *event = BLUETOOTH_EVENT_GATT_CONNECTED; + ret_if(output == NULL); + *param_data = &g_array_index(output, + bluetooth_device_address_t, 0); + break; + case BT_DISCONNECT_LE: + *event_type = BT_DEVICE_EVENT; + *event = BLUETOOTH_EVENT_GATT_DISCONNECTED; + ret_if(output == NULL); + *param_data = &g_array_index(output, + bluetooth_device_address_t, 0); break; default: BT_ERR("Unknown function"); @@ -206,22 +232,47 @@ out param1: API result out param2: return paramter out param3: */ -void __send_request_cb(DBusGProxy *proxy, GArray *out_param1, - GArray *out_param2, GError *error, - gpointer userdata) +static void __bt_fill_garray_from_variant(GVariant *var, GArray *param) +{ + char *data; + int size; + + size = g_variant_get_size(var); + if (size > 0) { + data = (char *)g_variant_get_data(var); + if (data) + param = g_array_append_vals(param, data, size); + + } +} + +static void __send_request_cb(GDBusProxy *proxy, + GAsyncResult *res, + gpointer user_data) { bluetooth_event_param_t bt_event; - bt_req_info_t *cb_data = userdata; + bt_req_info_t *cb_data = user_data; int result = BLUETOOTH_ERROR_NONE; int event_type = BT_ADAPTER_EVENT; int request_id; + GError *error = NULL; + GVariant *value; + GVariant *param1; +// GVariant *param2; + GArray *out_param1 = NULL; +// GArray *out_param2 = NULL; + BT_DBG("+"); memset(&bt_event, 0x00, sizeof(bluetooth_event_param_t)); - if (error != NULL) { - /* dBUS gives error cause */ - BT_ERR("D-Bus API failure: message[%s]", error->message); - g_error_free(error); + value = g_dbus_proxy_call_finish(proxy, res, &error); + if (value == NULL) { + if (error) { + /* dBUS gives error cause */ + BT_ERR("D-Bus API failure: message[%s]", + error->message); + g_clear_error(&error); + } result = BLUETOOTH_ERROR_TIMEOUT; ret_if(cb_data == NULL); @@ -230,9 +281,24 @@ void __send_request_cb(DBusGProxy *proxy, GArray *out_param1, &bt_event.event, &event_type, &bt_event.param_data); } else { - ret_if(out_param2 == NULL); + g_variant_get(value, "(iv)", &result, ¶m1); + g_variant_unref(value); + + if (param1) { + out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar)); + __bt_fill_garray_from_variant(param1, out_param1); + g_variant_unref(param1); + } - result = g_array_index(out_param2, int, 0); +// if (param2) { +// out_param2 = g_array_new(TRUE, TRUE, sizeof(gchar)); +// __bt_fill_garray_from_variant(param2, out_param2); +// result = g_array_index(out_param2, int, 0); +// g_variant_unref(param2); +// g_array_free(out_param2, TRUE); +// } else { +// result = BLUETOOTH_ERROR_INTERNAL; +// } ret_if(cb_data == NULL); @@ -240,26 +306,16 @@ void __send_request_cb(DBusGProxy *proxy, GArray *out_param1, &bt_event.event, &event_type, &bt_event.param_data); - if (result == BLUETOOTH_ERROR_NONE) { + if (result == BLUETOOTH_ERROR_NONE && out_param1) { if (cb_data->service_function == BT_OPP_PUSH_FILES) { request_id = g_array_index(out_param1, int, 0); + BT_DBG("request_id : %d", request_id); _bt_add_push_request_id(request_id); } - if (out_param1) - g_array_free(out_param1, FALSE); - - if (out_param2) - g_array_free(out_param2, FALSE); - goto done; } - if (out_param1) - g_array_free(out_param1, FALSE); - - if (out_param2) - g_array_free(out_param2, FALSE); } if (cb_data->cb == NULL) @@ -267,6 +323,7 @@ void __send_request_cb(DBusGProxy *proxy, GArray *out_param1, /* Only if fail case, call the callback function*/ bt_event.result = result; + BT_INFO("event_type[%d], result=[%d]", event_type, result); if (event_type == BT_ADAPTER_EVENT || event_type == BT_RFCOMM_CLIENT_EVENT) { ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event, @@ -280,87 +337,159 @@ void __send_request_cb(DBusGProxy *proxy, GArray *out_param1, ((bt_audio_func_ptr)cb_data->cb)(bt_event.event, (bt_audio_event_param_t *)&bt_event, cb_data->user_data); + } else if (event_type == BT_HF_AGENT_EVENT) { + ((bt_audio_func_ptr)cb_data->cb)(bt_event.event, + (bt_audio_event_param_t *)&bt_event, + cb_data->user_data); + } else if (event_type == BT_AVRCP_CONTROL_EVENT) { + ((media_cb_func_ptr)cb_data->cb)(bt_event.event, + (media_event_param_t *)&bt_event, + cb_data->user_data); + } else if (event_type == BT_A2DP_SOURCE_EVENT) { + ((bt_audio_func_ptr)cb_data->cb)(bt_event.event, + (bt_audio_event_param_t *)&bt_event, + cb_data->user_data); + } else if (event_type == BT_DEVICE_EVENT) { + ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event, + &bt_event, + cb_data->user_data); + } else { + BT_INFO("Not handled event type : %d", event_type); } done: + if (out_param1) + g_array_free(out_param1, TRUE); + sending_requests = g_slist_remove(sending_requests, (void *)cb_data); g_free(cb_data); + BT_DBG("-"); } -int _bt_send_request(int service_type, int service_function, +int _bt_sync_send_request(int service_type, int service_function, GArray *in_param1, GArray *in_param2, GArray *in_param3, GArray *in_param4, GArray **out_param1) { int result = BLUETOOTH_ERROR_NONE; - gboolean ret; GError *error = NULL; GArray *in_param5 = NULL; - GArray *out_param2 = NULL; - DBusGProxy *proxy; +// GArray *out_param2 = NULL; + + GDBusProxy *proxy; + GVariant *ret; + GVariant *param1; + GVariant *param2; + GVariant *param3; + GVariant *param4; + GVariant *param5; switch (service_type) { case BT_BLUEZ_SERVICE: case BT_OBEX_SERVICE: - proxy = __bt_get_service_proxy(); - retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL); - - in_param5 = g_array_new(FALSE, FALSE, sizeof(gchar)); + case BT_AGENT_SERVICE: + case BT_CHECK_PRIVILEGE: + proxy = __bt_gdbus_get_service_proxy(); + if (!proxy) + return BLUETOOTH_ERROR_INTERNAL; - ret = org_projectx_bt_service_request(proxy, - service_type, service_function, - BT_SYNC_REQ, in_param1, in_param2, - in_param3, in_param4, in_param5, - out_param1, &out_param2, &error); + in_param5 = g_array_new(TRUE, TRUE, sizeof(gchar)); + + + + param1 = g_variant_new_from_data((const GVariantType *)"ay", + in_param1->data, in_param1->len, + TRUE, NULL, NULL); + param2 = g_variant_new_from_data((const GVariantType *)"ay", + in_param2->data, in_param2->len, + TRUE, NULL, NULL); + param3 = g_variant_new_from_data((const GVariantType *)"ay", + in_param3->data, in_param3->len, + TRUE, NULL, NULL); + param4 = g_variant_new_from_data((const GVariantType *)"ay", + in_param4->data, in_param4->len, + TRUE, NULL, NULL); + param5 = g_variant_new_from_data((const GVariantType *)"ay", + in_param5->data, in_param5->len, + TRUE, NULL, NULL); + + ret = g_dbus_proxy_call_sync(proxy, "service_request", + g_variant_new("(iii@ay@ay@ay@ay@ay)", + service_type, service_function, + BT_SYNC_REQ, param1, + param2, param3, + param4, param5), + G_DBUS_CALL_FLAGS_NONE, -1, + NULL, &error); g_array_free(in_param5, TRUE); - break; - default: - BT_ERR("Unknown service type"); - return BLUETOOTH_ERROR_INTERNAL; - } - if (out_param2) { - result = g_array_index(out_param2, int, 0); - g_array_free(out_param2, TRUE); - } else { - result = BLUETOOTH_ERROR_INTERNAL; - } + if (ret == NULL) { + /* dBUS-RPC is failed */ + BT_ERR("dBUS-RPC is failed"); - if (ret != TRUE) { - /* dBUS-RPC is failed */ - BT_ERR("dBUS-RPC is failed"); + if (error != NULL) { + /* dBUS gives error cause */ + BT_ERR("D-Bus API failure: errCode[%x], message[%s]", + error->code, error->message); - if (error != NULL) { - /* dBUS gives error cause */ - BT_ERR("D-Bus API failure: errCode[%x], message[%s]", - error->code, error->message); + g_clear_error(&error); + } else { + /* dBUS does not give error cause dBUS-RPC is failed */ + BT_ERR("error returned was NULL"); + } - g_error_free(error); + return BLUETOOTH_ERROR_INTERNAL; } - else { - /* dBUS does not give error cause dBUS-RPC is failed */ - BT_ERR("error returned was NULL"); + + param1 = NULL; +// param2 = NULL; + + g_variant_get(ret, "(iv)", &result, ¶m1); + + if (param1) { + *out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar)); + __bt_fill_garray_from_variant(param1, *out_param1); + g_variant_unref(param1); } - return result; +// if (param2) { +// out_param2 = g_array_new(TRUE, TRUE, sizeof(gchar)); +// __bt_fill_garray_from_variant(param2, out_param2); +// result = g_array_index(out_param2, int, 0); +// g_variant_unref(param2); +// g_array_free(out_param2, TRUE); +// } else { +// result = BLUETOOTH_ERROR_INTERNAL; +// } + + g_variant_unref(ret); + break; + default: + BT_ERR("Unknown service type"); + return BLUETOOTH_ERROR_INTERNAL; } - BT_DBG("dBUS RPC is successfully done. type=%d, function=0x%x", - service_type, service_function); - return result; } -int _bt_send_request_async(int service_type, int service_function, +int _bt_async_send_request(int service_type, int service_function, GArray *in_param1, GArray *in_param2, GArray *in_param3, GArray *in_param4, void *callback, void *user_data) { GArray* in_param5 = NULL; bt_req_info_t *cb_data; - DBusGProxy *proxy; - DBusGProxyCall *proxy_call; + + GDBusProxy *proxy; + int timeout; + GVariant *param1; + GVariant *param2; + GVariant *param3; + GVariant *param4; + GVariant *param5; + + BT_DBG("service_function : %x", service_function); cb_data = g_new0(bt_req_info_t, 1); @@ -371,36 +500,57 @@ int _bt_send_request_async(int service_type, int service_function, switch (service_type) { case BT_BLUEZ_SERVICE: case BT_OBEX_SERVICE: - proxy = __bt_get_service_proxy(); - retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL); - - dbus_g_proxy_set_default_timeout(proxy, BT_DBUS_TIMEOUT_MAX); - - in_param5 = g_array_new(FALSE, FALSE, sizeof(gchar)); - - proxy_call = org_projectx_bt_service_request_async(proxy, service_type, - service_function, BT_ASYNC_REQ, in_param1, in_param2, - in_param3, in_param4, in_param5, - (org_projectx_bt_service_request_reply)__send_request_cb, - (gpointer)cb_data); - - if (proxy_call == NULL) { - - BT_ERR("dBUS-RPC is failed"); - g_array_free(in_param5, TRUE); + proxy = __bt_gdbus_get_service_proxy(); + if (!proxy) { g_free(cb_data); return BLUETOOTH_ERROR_INTERNAL; } + /* Do not timeout the request in certain cases. Sometime the + * request may take undeterministic time to reponse. + * (for ex: pairing retry) */ + if (service_function == BT_BOND_DEVICE || + service_function == BT_BOND_DEVICE_BY_TYPE) + timeout = INT_MAX; + else + timeout = BT_DBUS_TIMEOUT_MAX; + + in_param5 = g_array_new(TRUE, TRUE, sizeof(gchar)); + + param1 = g_variant_new_from_data((const GVariantType *)"ay", + in_param1->data, in_param1->len, + TRUE, NULL, NULL); + param2 = g_variant_new_from_data((const GVariantType *)"ay", + in_param2->data, in_param2->len, + TRUE, NULL, NULL); + param3 = g_variant_new_from_data((const GVariantType *)"ay", + in_param3->data, in_param3->len, + TRUE, NULL, NULL); + param4 = g_variant_new_from_data((const GVariantType *)"ay", + in_param4->data, in_param4->len, + TRUE, NULL, NULL); + param5 = g_variant_new_from_data((const GVariantType *)"ay", + in_param5->data, in_param5->len, + TRUE, NULL, NULL); + + g_dbus_proxy_call(proxy, "service_request", + g_variant_new("(iii@ay@ay@ay@ay@ay)", + service_type, service_function, + BT_ASYNC_REQ, param1, param2, + param3, param4, param5), + G_DBUS_CALL_FLAGS_NONE, + timeout, NULL, + (GAsyncReadyCallback)__send_request_cb, + (gpointer)cb_data); sending_requests = g_slist_append(sending_requests, cb_data); g_array_free(in_param5, TRUE); break; + default: + g_free(cb_data); + break; } - BT_DBG("dBUS RPC is successfully done. type=%d, function=0x%x", - service_type, service_function); - return BLUETOOTH_ERROR_NONE; }