From 7d4698709390783e410a117f8e16ff27b27fec85 Mon Sep 17 00:00:00 2001 From: Nishant Chaprana Date: Tue, 12 Jul 2016 12:31:28 +0530 Subject: [PATCH] Added support for Multi-threaded Applicaitons. This patch adds support for multi-threaded applications in wifi-direct library. This patch also cleans unused variables and 80 character line length issue in src/wifi-direct-dbus.c file. Change-Id: I91b59394c79ff6a4f8f57e2ef1cd046407bbed95 Signed-off-by: Nishant Chaprana --- include/wifi-direct.h | 34 ++++++++++++------ packaging/capi-network-wifi-direct.spec | 4 ++- src/include/wifi-direct-client-proxy.h | 39 ++++++++++----------- src/include/wifi-direct-dbus.h | 14 +++++--- src/wifi-direct-client-proxy.c | 53 ++++++++++------------------ src/wifi-direct-dbus.c | 61 ++++++++++++++++++++------------- 6 files changed, 109 insertions(+), 96 deletions(-) diff --git a/include/wifi-direct.h b/include/wifi-direct.h index 45ddaff..8c6b618 100755 --- a/include/wifi-direct.h +++ b/include/wifi-direct.h @@ -319,8 +319,10 @@ typedef struct { * @param[in] error_code The error code * @param[in] discovery_state The discovery state * @param[in] user_data The user data passed from the callback registration function - * @pre Either wifi_direct_start_discovery() or wifi_direct_cancel_discovery() will invoke this callback - * if you register this callback using wifi_direct_set_discovery_state_changed_cb(). + * @pre Either wifi_direct_start_discovery() or wifi_direct_cancel_discovery() + * will invoke this callback in the thread-default main context of the thread + * from which you registered this callback using + * wifi_direct_set_discovery_state_changed_cb(). * @see wifi_direct_start_discovery() * @see wifi_direct_cancel_discovery() * @see wifi_direct_set_discovery_state_changed_cb() @@ -341,12 +343,13 @@ typedef void (*wifi_direct_discovery_state_chagned_cb) (int error_code, * @param[in] discovery_state The discovery state * @param[in] mac_address The MAC address of found peer * @param[in] user_data The user data passed from the callback registration function - * @pre Either wifi_direct_start_discovery() or wifi_direct_cancel_discovery() will invoke this callback - * if you register this callback using wifi_direct_set_discovery_state_changed_cb(). + * @pre Either wifi_direct_start_discovery() or wifi_direct_cancel_discovery() + * will invoke this callback in the thread-default main context of the thread + * from which you registered this callback using wifi_direct_set_peer_found_cb() * @see wifi_direct_start_discovery() * @see wifi_direct_cancel_discovery() - * @see wifi_direct_set_discovery_state_changed_cb() - * @see wifi_direct_unset_discovery_state_changed_cb() + * @see wifi_direct_set_peer_found_cb() + * @see wifi_direct_unset_peer_found_cb() */ typedef void (*wifi_direct_peer_found_cb) (int error_code, wifi_direct_discovery_state_e discovery_state, @@ -363,8 +366,9 @@ typedef void (*wifi_direct_peer_found_cb) (int error_code, * @param[in] error_code The error code * @param[in] device_state The device state * @param[in] user_data The user data passed from the callback registration function - * @pre Either wifi_direct_activate() or wifi_direct_deactivate() will invoke this callback - * if you register this callback using wifi_direct_set_device_state_changed_cb(). + * @pre Either wifi_direct_activate() or wifi_direct_deactivate() will invoke + * this callback in the thread-default main context of the thread from which you + * registered this callback using wifi_direct_set_device_state_changed_cb(). * @see wifi_direct_activate() * @see wifi_direct_deactivate() * @see wifi_direct_set_device_state_changed_cb() @@ -386,7 +390,10 @@ typedef void (*wifi_direct_device_state_changed_cb) (int error_code, * @param[in] connection_state The connection state * @param[in] mac_address The MAC address of the connection peer * @param[in] user_data The user data passed from the callback registration function - * @pre wifi_direct_create_group(), wifi_direct_destroy_group(), wifi_direct_connect(), wifi_direct_disconnect() or wifi_direct_disconnect_all() will invoke this callback + * @pre wifi_direct_create_group(), wifi_direct_destroy_group(), + * wifi_direct_connect(), wifi_direct_disconnect() or + * wifi_direct_disconnect_all() will invoke this callback in the thread-default + * main context of the thread from which you registered this callback using * if you register this callback using wifi_direct_set_connection_state_changed_cb(). * @see wifi_direct_connect() * @see wifi_direct_disconnect() @@ -410,6 +417,9 @@ typedef void (*wifi_direct_connection_state_changed_cb) (int error_code, * @param[in] ip_address The IP address of connection peer * @param[in] interface_address The interface address of connection peer * @param[in] user_data The user data passed from the callback registration function + * @pre This callback will be invoked in the thread-default main context of the + * thread from which you registered this callback using + * wifi_direct_set_client_ip_address_assigned_cb(). * @see wifi_direct_set_client_ip_address_assigned_cb() * @see wifi_direct_unset_client_ip_address_assigned_cb() */ @@ -430,8 +440,10 @@ typedef void (*wifi_direct_client_ip_address_assigned_cb) (const char *mac_addre * @param[in] service_type Specifies the types of service * @param[in] response_data Received response * @param[in] user_data User can transfer the user specific data in callback - * @pre Either wifi_direct_start_service_discovery() or wifi_direct_cancel_service_discovery() will invoke this callback - * if you register this callback using wifi_direct_set_service_state_changed_cb(). + * @pre Either wifi_direct_start_service_discovery() or + * wifi_direct_cancel_service_discovery() will invoke this callback in the + * thread-default main context of thethread from which you registered this + * callback using wifi_direct_set_service_state_changed_cb(). * @see wifi_direct_start_discovery() * @see wifi_direct_cancel_discovery() * @see wifi_direct_set_discovery_state_changed_cb() diff --git a/packaging/capi-network-wifi-direct.spec b/packaging/capi-network-wifi-direct.spec index 2a8c3b3..16b995f 100644 --- a/packaging/capi-network-wifi-direct.spec +++ b/packaging/capi-network-wifi-direct.spec @@ -1,13 +1,15 @@ Name: capi-network-wifi-direct Summary: Network WiFi-Direct Library -Version: 1.2.68 +Version: 1.2.69 Release: 1 Group: Network & Connectivity/API License: Apache-2.0 Source0: %{name}-%{version}.tar.gz Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig +BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(gio-2.0) +BuildRequires: pkgconfig(gthread-2.0) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(vconf) BuildRequires: pkgconfig(capi-base-common) diff --git a/src/include/wifi-direct-client-proxy.h b/src/include/wifi-direct-client-proxy.h index f04bde2..3ccab11 100755 --- a/src/include/wifi-direct-client-proxy.h +++ b/src/include/wifi-direct-client-proxy.h @@ -25,22 +25,23 @@ #include #include "wifi-direct.h" -#define SOCK_FD_MIN 3 -#define WIFI_DIRECT_WPA_LEN 64 #define MACSTR_LEN 18 #define MACADDR_LEN 6 #define IPADDR_LEN 4 #define IPSTR_LEN 16 -#define WFD_SOCK_FILE_PATH "/tmp/wfd_client_socket" -#define WIFIDIRECT_FEATURE "http://tizen.org/feature/network.wifi.direct" -#define WIFIDIRECT_DISPLAY_FEATURE "http://tizen.org/feature/network.wifi.direct.display" -#define WIFIDIRECT_SERVICE_DISCOVERY_FEATURE "http://tizen.org/feature/network.wifi.direct.service_discovery" +#define WIFIDIRECT_FEATURE\ + "http://tizen.org/feature/network.wifi.direct" +#define WIFIDIRECT_DISPLAY_FEATURE\ + "http://tizen.org/feature/network.wifi.direct.display" +#define WIFIDIRECT_SERVICE_DISCOVERY_FEATURE\ + "http://tizen.org/feature/network.wifi.direct.service_discovery" #define CHECK_FEATURE_SUPPORTED(feature_name)\ do {\ bool feature_supported = FALSE;\ - if (!system_info_get_platform_bool(feature_name, &feature_supported)) {\ + if (!system_info_get_platform_bool(feature_name,\ + &feature_supported)) {\ if (feature_supported == FALSE) {\ LOGE("%s feature is disabled", feature_name);\ return WIFI_DIRECT_ERROR_NOT_SUPPORTED;\ @@ -53,24 +54,23 @@ typedef struct { bool is_registered; - int client_id; - int sync_sockfd; - int async_sockfd; - int g_client_info; - int g_source_id; - wifi_direct_device_state_changed_cb activation_cb; - wifi_direct_discovery_state_chagned_cb discover_cb; - wifi_direct_connection_state_changed_cb connection_cb; - wifi_direct_client_ip_address_assigned_cb ip_assigned_cb; - wifi_direct_peer_found_cb peer_found_cb; - wifi_direct_state_changed_cb state_cb; + wifi_direct_device_state_changed_cb activation_cb; void *user_data_for_cb_activation; + + wifi_direct_discovery_state_chagned_cb discover_cb; void *user_data_for_cb_discover; + + wifi_direct_connection_state_changed_cb connection_cb; void *user_data_for_cb_connection; + + wifi_direct_client_ip_address_assigned_cb ip_assigned_cb; void *user_data_for_cb_ip_assigned; + + wifi_direct_peer_found_cb peer_found_cb; void *user_data_for_cb_peer_found; - void *user_data_for_cb_device_name; + + wifi_direct_state_changed_cb state_cb; void *user_data_for_cb_state; #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY @@ -78,7 +78,6 @@ typedef struct { void *user_data_for_cb_service; #endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */ - pthread_mutex_t mutex; } wifi_direct_client_info_s; extern char *wfd_debug_print(char *file, int line, char *format, ...); diff --git a/src/include/wifi-direct-dbus.h b/src/include/wifi-direct-dbus.h index 3e88bde..86d9a9c 100644 --- a/src/include/wifi-direct-dbus.h +++ b/src/include/wifi-direct-dbus.h @@ -47,15 +47,19 @@ extern "C" { #define WFD_MANAGER_QUARK (g_quark_from_string("wifi-direct-manager")) -#define wifi_direct_dbus_method_call_sync(interface_name, method, params, error) \ - wifi_direct_dbus_method_call_sync_debug(interface_name, method, params, error, __func__) +#define wifi_direct_dbus_method_call_sync(interface_name, method, params,\ + error)\ + wifi_direct_dbus_method_call_sync_debug(interface_name, method, params,\ + error, __func__) -#define DBUS_DEBUG_VARIANT(parameters) \ +#define DBUS_DEBUG_VARIANT(parameters)\ do {\ gchar *parameters_debug_str = NULL;\ if (parameters)\ - parameters_debug_str = g_variant_print(parameters, TRUE);\ - WDC_LOGD("signal params [%s]", parameters_debug_str ? parameters_debug_str : "NULL");\ + parameters_debug_str = g_variant_print(parameters,\ + TRUE);\ + WDC_LOGD("signal params [%s]",\ + parameters_debug_str ? parameters_debug_str : "NULL");\ g_free(parameters_debug_str);\ } while (0) diff --git a/src/wifi-direct-client-proxy.c b/src/wifi-direct-client-proxy.c index 6022dfb..e9c3913 100755 --- a/src/wifi-direct-client-proxy.c +++ b/src/wifi-direct-client-proxy.c @@ -68,31 +68,7 @@ * Global Variables *****************************************************************************/ -wifi_direct_client_info_s g_client_info = { - .is_registered = FALSE, - .client_id = -1, - .sync_sockfd = -1, - .async_sockfd = -1, - .activation_cb = NULL, - .discover_cb = NULL, - .connection_cb = NULL, - .ip_assigned_cb = NULL, - .peer_found_cb = NULL, - .state_cb = NULL, - .user_data_for_cb_activation = NULL, - .user_data_for_cb_discover = NULL, - .user_data_for_cb_connection = NULL, - .user_data_for_cb_ip_assigned = NULL, - .user_data_for_cb_peer_found = NULL, - .user_data_for_cb_device_name = NULL, - .user_data_for_cb_state = NULL, -#ifdef TIZEN_FEATURE_SERVICE_DISCOVERY - .service_cb = NULL, - .user_data_for_cb_service = NULL, -#endif /* TIZEN_FEATURE_SERVICE_DISCOVERY */ - - .mutex = PTHREAD_MUTEX_INITIALIZER -}; +static __thread wifi_direct_client_info_s g_client_info = {0, }; /***************************************************************************** * Local Functions Definition @@ -157,7 +133,8 @@ void __wfd_vconf_state_changed_cb(keynode_t *key, void *data) res = vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &state); if (res < 0) { - WDC_LOGE("Failed to get vconf value [%s]\n", VCONFKEY_WIFI_DIRECT_STATE); + WDC_LOGE("Failed to get vconf value [%s]\n", + VCONFKEY_WIFI_DIRECT_STATE); __WDC_LOG_FUNC_END__; return; } @@ -699,17 +676,19 @@ int wifi_direct_initialize(void) /* Initialize callbacks */ g_client_info.activation_cb = NULL; + g_client_info.user_data_for_cb_activation = NULL; + g_client_info.discover_cb = NULL; + g_client_info.user_data_for_cb_discover = NULL; + g_client_info.connection_cb = NULL; + g_client_info.user_data_for_cb_connection = NULL; + g_client_info.ip_assigned_cb = NULL; + g_client_info.user_data_for_cb_ip_assigned = NULL; g_client_info.peer_found_cb = NULL; - g_client_info.user_data_for_cb_activation = NULL; - g_client_info.user_data_for_cb_discover = NULL; - g_client_info.user_data_for_cb_connection = NULL; - g_client_info.user_data_for_cb_ip_assigned = NULL; g_client_info.user_data_for_cb_peer_found = NULL; - g_client_info.user_data_for_cb_device_name = NULL; #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY g_client_info.service_cb = NULL; @@ -735,14 +714,18 @@ int wifi_direct_deinitialize(void) wifi_direct_dbus_deinit(); g_client_info.activation_cb = NULL; - g_client_info.discover_cb = NULL; - g_client_info.connection_cb = NULL; - g_client_info.ip_assigned_cb = NULL; - g_client_info.peer_found_cb = NULL; g_client_info.user_data_for_cb_activation = NULL; + + g_client_info.discover_cb = NULL; g_client_info.user_data_for_cb_discover = NULL; + + g_client_info.connection_cb = NULL; g_client_info.user_data_for_cb_connection = NULL; + + g_client_info.ip_assigned_cb = NULL; g_client_info.user_data_for_cb_ip_assigned = NULL; + + g_client_info.peer_found_cb = NULL; g_client_info.user_data_for_cb_peer_found = NULL; #ifdef TIZEN_FEATURE_SERVICE_DISCOVERY diff --git a/src/wifi-direct-dbus.c b/src/wifi-direct-dbus.c index 1a1c407..2d4107e 100644 --- a/src/wifi-direct-dbus.c +++ b/src/wifi-direct-dbus.c @@ -34,7 +34,7 @@ typedef struct { guint signal_subscribe_id; } gdbus_connection_data; -static gdbus_connection_data gdbus_conn = {NULL, 0}; +static __thread gdbus_connection_data gdbus_conn = {NULL, 0}; static struct { const char *interface; @@ -128,9 +128,12 @@ static struct { }; static void _wifi_direct_dbus_signal_cb(GDBusConnection *connection, - const gchar *sender, const gchar *object_path, - const gchar *interface, const gchar *signal, - GVariant *parameters, gpointer user_data) + const gchar *sender, + const gchar *object_path, + const gchar *interface, + const gchar *signal, + GVariant *parameters, + gpointer user_data) { int i = 0; @@ -139,9 +142,12 @@ static void _wifi_direct_dbus_signal_cb(GDBusConnection *connection, for (i = 0; wifi_direct_dbus_signal_map[i].member != NULL; i++) { if (!g_strcmp0(signal, wifi_direct_dbus_signal_map[i].member) && - !g_strcmp0(interface, wifi_direct_dbus_signal_map[i].interface) && + !g_strcmp0(interface, + wifi_direct_dbus_signal_map[i].interface) && wifi_direct_dbus_signal_map[i].function != NULL) { - wifi_direct_dbus_signal_map[i].function(connection, object_path, parameters); + wifi_direct_dbus_signal_map[i].function(connection, + object_path, + parameters); break; } } @@ -171,7 +177,8 @@ GVariant *wifi_direct_dbus_method_call_sync_debug(const char* interface_name, params, /* GVariant *params */ NULL, /* reply_type */ G_DBUS_CALL_FLAGS_NONE, /* flags */ - WIFI_DIRECT_DBUS_REPLY_TIMEOUT_SYNC, /* timeout */ + WIFI_DIRECT_DBUS_REPLY_TIMEOUT_SYNC, + /* timeout */ NULL, /* cancellable */ error); /* error */ DBUS_DEBUG_VARIANT(reply); @@ -181,29 +188,34 @@ GVariant *wifi_direct_dbus_method_call_sync_debug(const char* interface_name, gboolean wifi_direct_dbus_init(void) { GError *Error = NULL; - GDBusConnection *connection = NULL; - connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &Error); - if (connection == NULL) { + if (gdbus_conn.signal_subscribe_id > 0) { + WDC_LOGI("GDbusConnection already initialized"); + return TRUE; + } + + gdbus_conn.connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &Error); + if (gdbus_conn.connection == NULL) { WDC_LOGE("Failed to get connection, Error[%s]", Error->message); g_error_free(Error); //LCOV_EXCL_LINE return FALSE; //LCOV_EXCL_LINE } - gdbus_conn.connection = connection; - /* subscribe signal handler */ - gdbus_conn.signal_subscribe_id = g_dbus_connection_signal_subscribe(connection, - WFD_MANAGER_SERVICE, /* bus name */ - NULL, /* interface */ - NULL, /* member */ - WFD_MANAGER_PATH, /* object_path */ - NULL, /* arg0 */ - G_DBUS_SIGNAL_FLAGS_NONE, - _wifi_direct_dbus_signal_cb, - NULL, - NULL); - WDC_LOGI("Subscribed dbus signals [%d]", gdbus_conn.signal_subscribe_id); + gdbus_conn.signal_subscribe_id = + g_dbus_connection_signal_subscribe(gdbus_conn.connection, + WFD_MANAGER_SERVICE, /* bus name */ + NULL, /* interface */ + NULL, /* member */ + WFD_MANAGER_PATH, /* object_path */ + NULL, /* arg0 */ + G_DBUS_SIGNAL_FLAGS_NONE, + _wifi_direct_dbus_signal_cb, + NULL, + NULL); + WDC_LOGI("Subscribed dbus signals [%d]", + gdbus_conn.signal_subscribe_id); + return TRUE; } @@ -213,7 +225,8 @@ void wifi_direct_dbus_deinit(void) return; /* unsubscribe signal handler */ - g_dbus_connection_signal_unsubscribe(gdbus_conn.connection, gdbus_conn.signal_subscribe_id); + g_dbus_connection_signal_unsubscribe(gdbus_conn.connection, + gdbus_conn.signal_subscribe_id); gdbus_conn.signal_subscribe_id = 0; /* unref gdbus connection */ -- 2.7.4