From 1ba1d43ff86ce4637d480077763ac4b5f18edd80 Mon Sep 17 00:00:00 2001 From: Jaehyun Kim Date: Sun, 21 Apr 2013 23:45:43 +0900 Subject: [PATCH] Add WPS(PBC & PIN) connection support and Revise agent code --- packaging/libnet-client.spec | 2 +- src/include/network-dbus-request.h | 10 +- src/network-dbus-request.c | 181 +++++++++++++++++++++++-------------- src/network-profile-intf.c | 10 +- src/network-wifi-intf.c | 33 ++++--- test/main.c | 2 +- 6 files changed, 147 insertions(+), 91 deletions(-) diff --git a/packaging/libnet-client.spec b/packaging/libnet-client.spec index b6ddc42..8d29115 100644 --- a/packaging/libnet-client.spec +++ b/packaging/libnet-client.spec @@ -1,6 +1,6 @@ Name: libnet-client Summary: Network Client library (Shared library) -Version: 0.1.77_23 +Version: 0.1.77_24 Release: 1 Group: System/Network License: Flora License diff --git a/src/include/network-dbus-request.h b/src/include/network-dbus-request.h index f13d938..77e180c 100644 --- a/src/include/network-dbus-request.h +++ b/src/include/network-dbus-request.h @@ -47,9 +47,9 @@ extern "C" { #define CONNMAN_CLIENT_DBUS_TYPE_ARRAY "array" #define CONNMAN_CLIENT_DBUS_TYPE_DICT_ENTRY "dict" -#define NETCONFIG_AGENT_FIELD_NAME "Name" -#define NETCONFIG_AGENT_FIELD_PASSPHRASE "Passphrase" -#define NETCONFIG_AGENT_FIELD_IDENTITY "Identity" +#define NETCONFIG_AGENT_FIELD_PASSPHRASE "Passphrase" +#define NETCONFIG_AGENT_FIELD_WPS_PBC "WPS_PBC" +#define NETCONFIG_AGENT_FIELD_WPS_PIN "WPS_PIN" #define CONNMAN_CONFIG_FIELD_TYPE "Type" #define CONNMAN_CONFIG_FIELD_NAME "Name" @@ -89,7 +89,9 @@ typedef struct { int _net_dbus_scan_request(void); int _net_dbus_set_bgscan_mode(net_wifi_background_scan_mode_t mode); int _net_dbus_get_state(char* state); -int _net_dbus_set_agent_fields(const char *name, const char *passphrase); +int _net_dbus_set_agent_passphrase(const char *passphrase); +int _net_dbus_set_agent_wps_pbc(void); +int _net_dbus_set_agent_wps_pin(char *wps_pin); int _net_dbus_open_connection(const char* profile_name); int _net_dbus_close_connection(const char* profile_name); int _net_dbus_get_network_status(net_device_t device_type, net_cm_network_status_t* network_status); diff --git a/src/network-dbus-request.c b/src/network-dbus-request.c index f1cb465..e7aacfe 100644 --- a/src/network-dbus-request.c +++ b/src/network-dbus-request.c @@ -633,6 +633,77 @@ static inline void __net_dict_append_strings(DBusMessageIter *dict, dbus_message_iter_close_container(dict, &entry); } +static int __net_dbus_set_agent_field(char *field_name, char *field_value) +{ + DBusConnection* conn = NULL; + net_err_t Error = NET_ERR_NONE; + DBusError error; + DBusMessage *message = NULL; + DBusMessage *reply = NULL; + DBusMessageIter itr, dict; + + __NETWORK_FUNC_ENTER__; + + conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL); + if (NULL == conn) { + NETWORK_LOG(NETWORK_ERROR, "Failed to get a system bus\n"); + __NETWORK_FUNC_EXIT__; + return NET_ERR_UNKNOWN; + } + + message = dbus_message_new_method_call(NETCONFIG_SERVICE, + NETCONFIG_WIFI_PATH, CONNMAN_AGENT_INTERFACE, + "SetField"); + if (NULL == message) { + NETWORK_LOG(NETWORK_ERROR, "dbus_message_new_method_call() " + "failed\n"); + dbus_connection_unref(conn); + __NETWORK_FUNC_EXIT__; + return NET_ERR_UNKNOWN; + } + + dbus_message_iter_init_append(message, &itr); + + dbus_message_iter_open_container(&itr, DBUS_TYPE_ARRAY, + (DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING + DBUS_TYPE_STRING_AS_STRING + DBUS_TYPE_STRING_AS_STRING + DBUS_DICT_ENTRY_END_CHAR_AS_STRING), &dict); + + __net_dict_append_strings(&dict, field_name, field_value); + NETWORK_LOG(NETWORK_HIGH, "Adding - %s %s\n", field_name, field_value); + + dbus_message_iter_close_container(&itr, &dict); + + dbus_error_init(&error); + + reply = dbus_connection_send_with_reply_and_block(conn, message, + DBUS_REPLY_TIMEOUT, &error); + if (NULL == reply) { + Error = NET_ERR_UNKNOWN; + if (dbus_error_is_set (&error) == TRUE) { + NETWORK_LOG(NETWORK_ERROR, + "dbus_connection_send_with_reply_and_block() " + "failed, Error[%s: %s]\n", error.name, + error.message); + Error = __net_error_string_to_enum(error.name); + dbus_error_free(&error); + } + + dbus_message_unref(message); + dbus_connection_unref(conn); + __NETWORK_FUNC_EXIT__; + return Error; + } + + dbus_message_unref(reply); + dbus_message_unref(message); + dbus_connection_unref(conn); + + __NETWORK_FUNC_EXIT__; + return NET_ERR_NONE; +} + /***************************************************************************** * Global Functions Definition *****************************************************************************/ @@ -1267,90 +1338,65 @@ done: return Error; } -int _net_dbus_set_agent_fields(const char *name, const char *passphrase) +int _net_dbus_set_agent_passphrase(const char *passphrase) { __NETWORK_FUNC_ENTER__; - net_err_t Error = NET_ERR_NONE; - DBusError error; - DBusMessage *message = NULL; - DBusMessage *reply = NULL; - DBusMessageIter itr, dict; - DBusConnection* conn = NULL; + int ret_val; - NETWORK_LOG(NETWORK_HIGH, "Name - [%s] Passphrase - [%s]", - name, passphrase); - - conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL); - if (NULL == conn) { - NETWORK_LOG(NETWORK_ERROR, "Failed to get a system bus\n"); - __NETWORK_FUNC_EXIT__; - return NET_ERR_UNKNOWN; + if (NULL == passphrase || strlen(passphrase) <= 0) { + NETWORK_LOG(NETWORK_ERROR, "Invalid param \n"); + return NET_ERR_INVALID_PARAM; } - message = dbus_message_new_method_call(NETCONFIG_SERVICE, - NETCONFIG_WIFI_PATH, CONNMAN_AGENT_INTERFACE, - "SetField"); - if (NULL == message) { - NETWORK_LOG(NETWORK_ERROR, "dbus_message_new_method_call() " - "failed\n"); - dbus_connection_unref(conn); - __NETWORK_FUNC_EXIT__; - return NET_ERR_UNKNOWN; + ret_val = __net_dbus_set_agent_field(NETCONFIG_AGENT_FIELD_PASSPHRASE, passphrase); + if (NET_ERR_NONE != ret_val) { + NETWORK_LOG(NETWORK_ERROR, "__net_dbus_set_agent_field failed. Error = %d \n", ret_val); + return ret_val; } - dbus_message_iter_init_append(message, &itr); + NETWORK_LOG(NETWORK_HIGH, "Successfully sent passphrase\n"); - dbus_message_iter_open_container(&itr, DBUS_TYPE_ARRAY, - (DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING - DBUS_TYPE_STRING_AS_STRING - DBUS_TYPE_STRING_AS_STRING - DBUS_DICT_ENTRY_END_CHAR_AS_STRING), &dict); + __NETWORK_FUNC_EXIT__; + return NET_ERR_NONE; +} - if (NULL != name) { - __net_dict_append_strings(&dict, NETCONFIG_AGENT_FIELD_NAME, - name); - NETWORK_LOG(NETWORK_HIGH, "Adding - %s %s\n", - NETCONFIG_AGENT_FIELD_NAME, name); - } +int _net_dbus_set_agent_wps_pbc(void) +{ + __NETWORK_FUNC_ENTER__; - if (NULL != passphrase) { - __net_dict_append_strings(&dict, - NETCONFIG_AGENT_FIELD_PASSPHRASE, passphrase); - NETWORK_LOG(NETWORK_HIGH, "Adding - %s %s\n", - NETCONFIG_AGENT_FIELD_PASSPHRASE, passphrase); + int ret_val; + + ret_val = __net_dbus_set_agent_field(NETCONFIG_AGENT_FIELD_WPS_PBC, "enable"); + if (NET_ERR_NONE != ret_val) { + NETWORK_LOG(NETWORK_ERROR, "__net_dbus_set_agent_field failed. Error = %d \n", ret_val); + return ret_val; } - dbus_message_iter_close_container(&itr, &dict); + NETWORK_LOG(NETWORK_HIGH, "Successfully sent wps pbc\n"); - dbus_error_init(&error); + __NETWORK_FUNC_EXIT__; + return NET_ERR_NONE; +} - reply = dbus_connection_send_with_reply_and_block(conn, message, - DBUS_REPLY_TIMEOUT, &error); - if (NULL == reply) { - if (dbus_error_is_set (&error) == TRUE) { - NETWORK_LOG(NETWORK_ERROR, - "dbus_connection_send_with_reply_and_block() " - "failed, Error[%s: %s]\n", error.name, - error.message); - Error = __net_error_string_to_enum(error.name); - dbus_error_free(&error); - dbus_message_unref(message); - __NETWORK_FUNC_EXIT__; - return Error; - } +int _net_dbus_set_agent_wps_pin(char *wps_pin) +{ + __NETWORK_FUNC_ENTER__; - dbus_message_unref(message); - dbus_connection_unref(conn); - __NETWORK_FUNC_EXIT__; - return NET_ERR_UNKNOWN; + int ret_val; + + if (NULL == wps_pin || strlen(wps_pin) <= 0) { + NETWORK_LOG(NETWORK_ERROR, "Invalid param \n"); + return NET_ERR_INVALID_PARAM; } - dbus_message_unref(reply); - dbus_message_unref(message); - dbus_connection_unref(conn); + ret_val = __net_dbus_set_agent_field(NETCONFIG_AGENT_FIELD_WPS_PIN, wps_pin); + if (NET_ERR_NONE != ret_val) { + NETWORK_LOG(NETWORK_ERROR, "__net_dbus_set_agent_field failed. Error = %d \n", ret_val); + return ret_val; + } - NETWORK_LOG(NETWORK_HIGH, "Successfully sent agent fields\n"); + NETWORK_LOG(NETWORK_HIGH, "Successfully sent wps pin\n"); __NETWORK_FUNC_EXIT__; return NET_ERR_NONE; @@ -1415,10 +1461,9 @@ int _net_dbus_connect_service(const net_wifi_connect_service_info_t *wifi_connec goto done; } } else { - Error = _net_dbus_set_agent_fields(wifi_connection_info->ssid, - wifi_connection_info->passphrase); + Error = _net_dbus_set_agent_passphrase(wifi_connection_info->passphrase); if (NET_ERR_NONE != Error) { - NETWORK_LOG(NETWORK_ERROR, "Fail to set agent_fields\n"); + NETWORK_LOG(NETWORK_ERROR, "Fail to set agent_passphrase\n"); goto done; } diff --git a/src/network-profile-intf.c b/src/network-profile-intf.c index 53f24b2..d7b66dd 100644 --- a/src/network-profile-intf.c +++ b/src/network-profile-intf.c @@ -1768,8 +1768,7 @@ static int __net_modify_wlan_profile_info(const char* ProfileName, if (ex_security_info->sec_mode == WLAN_SEC_MODE_WEP) { if (strcmp(security_info->authentication.wep.wepKey, ex_security_info->authentication.wep.wepKey) != 0) { - Error = _net_dbus_set_agent_fields(NULL, - security_info->authentication.wep.wepKey); + Error = _net_dbus_set_agent_passphrase(security_info->authentication.wep.wepKey); if (NET_ERR_NONE != Error) { NETWORK_LOG(NETWORK_ERROR, "Failed to set agent field\n"); @@ -1779,11 +1778,10 @@ static int __net_modify_wlan_profile_info(const char* ProfileName, } } } else if (ex_security_info->sec_mode == WLAN_SEC_MODE_WPA_PSK || - ex_security_info->sec_mode == WLAN_SEC_MODE_WPA2_PSK) { + ex_security_info->sec_mode == WLAN_SEC_MODE_WPA2_PSK) { if (strcmp(security_info->authentication.psk.pskKey, - ex_security_info->authentication.psk.pskKey) != 0) { - Error = _net_dbus_set_agent_fields(NULL, - security_info->authentication.psk.pskKey); + ex_security_info->authentication.psk.pskKey) != 0) { + Error = _net_dbus_set_agent_passphrase(security_info->authentication.psk.pskKey); if (NET_ERR_NONE != Error) { NETWORK_LOG(NETWORK_ERROR, "Failed to set agent field\n"); diff --git a/src/network-wifi-intf.c b/src/network-wifi-intf.c index 82e77a9..a0f4db2 100644 --- a/src/network-wifi-intf.c +++ b/src/network-wifi-intf.c @@ -418,27 +418,38 @@ EXPORT_API int net_wifi_enroll_wps(const char *profile_name, net_wifi_wps_info_t profile_name, NET_PROFILE_NAME_LEN_MAX+1); if (wps_info->type == WIFI_WPS_PBC) { - Error = _net_dbus_open_connection(profile_name); - if (Error != NET_ERR_NONE) { + Error = _net_dbus_set_agent_wps_pbc(); + if (NET_ERR_NONE != Error) { NETWORK_LOG(NETWORK_ERROR, - "Failed to request open connection, Error [%s]\n", - _net_print_error(Error)); - - memset(&request_table[NETWORK_REQUEST_TYPE_ENROLL_WPS], 0, - sizeof(network_request_table_t)); - + "_net_dbus_set_agent_wps_pbc() failed\n"); __NETWORK_FUNC_EXIT__; - return Error; + return NET_ERR_INVALID_OPERATION; } } else if(wps_info->type == WIFI_WPS_PIN) { - // TODO: handle wps pin + Error = _net_dbus_set_agent_wps_pin(wps_info->pin); + if (NET_ERR_NONE != Error) { + NETWORK_LOG(NETWORK_ERROR, + "_net_dbus_set_agent_wps_pin() failed\n"); + __NETWORK_FUNC_EXIT__; + return NET_ERR_INVALID_OPERATION; + } } else { __NETWORK_FUNC_EXIT__; return NET_ERR_INVALID_PARAM; } + Error = _net_dbus_open_connection(profile_name); + if (Error != NET_ERR_NONE) { + NETWORK_LOG(NETWORK_ERROR, + "Failed to request open connection, Error [%s]\n", + _net_print_error(Error)); + + memset(&request_table[NETWORK_REQUEST_TYPE_ENROLL_WPS], 0, + sizeof(network_request_table_t)); + } + __NETWORK_FUNC_EXIT__; - return NET_ERR_NONE; + return Error; } /***************************************************************************** diff --git a/test/main.c b/test/main.c index 433f142..e5d6783 100644 --- a/test/main.c +++ b/test/main.c @@ -516,7 +516,7 @@ static void __network_evt_cb(net_event_info_t* event_cb, void* user_data) for (; bss_info_list; bss_info_list = bss_info_list->next){ net_wifi_connection_info_t *resp_data = bss_info_list->data; if (resp_data) - debug_print("essid:%s, sec type:%d", + debug_print("essid:%s, sec type:%d\n", resp_data->essid, resp_data->security_info.sec_mode); } -- 2.7.4