From e7012525583f241d7ef08c7de26d8578c7a38987 Mon Sep 17 00:00:00 2001 From: legendlee1314 Date: Tue, 17 Sep 2013 15:42:11 +0800 Subject: [PATCH] [SystemInfo][Network] Add WIFI network-type and uniform to use C-API listener --- system_info/system_info_network.cc | 6 -- system_info/system_info_network.h | 25 +++---- system_info/system_info_network_desktop.cc | 12 ++++ system_info/system_info_network_mobile.cc | 108 +++++++++++++++++++++-------- 4 files changed, 100 insertions(+), 51 deletions(-) diff --git a/system_info/system_info_network.cc b/system_info/system_info_network.cc index 805491c..e8554be 100644 --- a/system_info/system_info_network.cc +++ b/system_info/system_info_network.cc @@ -6,12 +6,6 @@ #include "system_info/system_info_utils.h" -SysInfoNetwork::SysInfoNetwork(ContextAPI* api) - : type_(SYSTEM_INFO_NETWORK_UNKNOWN) { - api_ = api; - PlatformInitialize(); -} - void SysInfoNetwork::Get(picojson::value& error, picojson::value& data) { if (!Update(error)) { diff --git a/system_info/system_info_network.h b/system_info/system_info_network.h index ee9e6ff..77ec2c7 100644 --- a/system_info/system_info_network.h +++ b/system_info/system_info_network.h @@ -8,7 +8,7 @@ #if defined(GENERIC_DESKTOP) #include #elif defined(TIZEN_MOBILE) -#include +#include #endif #include @@ -42,20 +42,8 @@ class SysInfoNetwork { explicit SysInfoNetwork(ContextAPI* api); ~SysInfoNetwork(); void Get(picojson::value& error, picojson::value& data); - inline void StartListening() { -#if defined(TIZEN_MOBILE) - timeout_cb_id_ = g_timeout_add(system_info::default_timeout_interval, - SysInfoNetwork::OnUpdateTimeout, - static_cast(this)); -#endif - } - inline void StopListening() { -#if defined(TIZEN_MOBILE) - if (timeout_cb_id_ > 0) { - g_source_remove(timeout_cb_id_); - } -#endif -} + void StartListening(); + void StopListening(); private: void PlatformInitialize(); @@ -88,8 +76,11 @@ class SysInfoNetwork { std::string active_device_; guint device_type_; #elif defined(TIZEN_MOBILE) - static gboolean OnUpdateTimeout(gpointer user_data); - int timeout_cb_id_; + bool GetNetworkType(); + static void OnTypeChanged(connection_type_e type, void* user_data); + + bool is_registered_; + connection_h connection_handle_; #endif DISALLOW_COPY_AND_ASSIGN(SysInfoNetwork); diff --git a/system_info/system_info_network_desktop.cc b/system_info/system_info_network_desktop.cc index 34a6bb6..f8ea120 100644 --- a/system_info/system_info_network_desktop.cc +++ b/system_info/system_info_network_desktop.cc @@ -19,6 +19,12 @@ const char sDeviceInterface[] = "org.freedesktop.NetworkManager.Device"; } // namespace +SysInfoNetwork::SysInfoNetwork(ContextAPI* api) + : type_(SYSTEM_INFO_NETWORK_UNKNOWN) { + api_ = api; + PlatformInitialize(); +} + void SysInfoNetwork::PlatformInitialize() { active_connection_ = ""; active_device_ = ""; @@ -38,6 +44,12 @@ void SysInfoNetwork::PlatformInitialize() { SysInfoNetwork::~SysInfoNetwork() { } +void SysInfoNetwork::StartListening() { +} + +void SysInfoNetwork::StopListening() { +} + void SysInfoNetwork::OnNetworkManagerCreated(GObject*, GAsyncResult* res) { GError* err = 0; GDBusProxy* proxy = g_dbus_proxy_new_for_bus_finish(res, &err); diff --git a/system_info/system_info_network_mobile.cc b/system_info/system_info_network_mobile.cc index 6b15f72..306c872 100644 --- a/system_info/system_info_network_mobile.cc +++ b/system_info/system_info_network_mobile.cc @@ -6,21 +6,78 @@ #include -SysInfoNetwork::~SysInfoNetwork() { - if (timeout_cb_id_ > 0) - g_source_remove(timeout_cb_id_); +SysInfoNetwork::SysInfoNetwork(ContextAPI* api) + : type_(SYSTEM_INFO_NETWORK_UNKNOWN), + is_registered_(false), + connection_handle_(NULL) { + api_ = api; + PlatformInitialize(); } void SysInfoNetwork::PlatformInitialize() { - timeout_cb_id_ = 0; + if (connection_create(&connection_handle_) != CONNECTION_ERROR_NONE) + connection_handle_ = NULL; +} + +SysInfoNetwork::~SysInfoNetwork() { + if (is_registered_) + StopListening(); + if (connection_handle_) + free(connection_handle_); +} + +void SysInfoNetwork::StartListening() { + if (connection_handle_ && !is_registered_) { + connection_set_type_changed_cb(connection_handle_, + OnTypeChanged, this); + is_registered_ = true; + } +} + +void SysInfoNetwork::StopListening() { + if (connection_handle_) { + connection_unset_type_changed_cb(connection_handle_); + is_registered_ = false; + } } bool SysInfoNetwork::Update(picojson::value& error) { - int service_type = 0; - if (vconf_get_int(VCONFKEY_TELEPHONY_SVCTYPE, &service_type)) { + if (!connection_handle_) { + if (error.get("message").to_str().empty()) + system_info::SetPicoJsonObjectValue(error, "message", + picojson::value("Get connection faild.")); + return false; + } + + connection_type_e connection_type; + if (connection_get_type(connection_handle_, &connection_type) != + CONNECTION_ERROR_NONE) { + if (error.get("message").to_str().empty()) + system_info::SetPicoJsonObjectValue(error, "message", + picojson::value("Get net state faild.")); return false; } + if (connection_type == CONNECTION_TYPE_WIFI) { + type_ = SYSTEM_INFO_NETWORK_WIFI; + return true; + } + + if (!GetNetworkType()) { + if (error.get("message").to_str().empty()) + system_info::SetPicoJsonObjectValue(error, "message", + picojson::value("Get network type at vconf faild.")); + return false; + } + + return true; +} + +bool SysInfoNetwork::GetNetworkType() { + int service_type = 0; + if (vconf_get_int(VCONFKEY_TELEPHONY_SVCTYPE, &service_type)) + return false; + switch (service_type) { case VCONFKEY_TELEPHONY_SVCTYPE_NONE: case VCONFKEY_TELEPHONY_SVCTYPE_NOSVC: @@ -47,30 +104,25 @@ bool SysInfoNetwork::Update(picojson::value& error) { return true; } -gboolean SysInfoNetwork::OnUpdateTimeout(gpointer user_data) { - SysInfoNetwork* instance = static_cast(user_data); +void SysInfoNetwork::OnTypeChanged(connection_type_e type, void* user_data) { + SysInfoNetwork* network = static_cast(user_data); - SystemInfoNetworkType old_type = instance->type_; - picojson::value error = picojson::value(picojson::object());; - if (!instance->Update(error)) { - // Fail to update, wait for next round. - return TRUE; - } - - if (old_type != instance->type_) { - picojson::value output = picojson::value(picojson::object());; - picojson::value data = picojson::value(picojson::object()); + if (type == CONNECTION_TYPE_WIFI && + network->type_ != SYSTEM_INFO_NETWORK_WIFI) + network->type_ = SYSTEM_INFO_NETWORK_WIFI; + else if (!network->GetNetworkType()) + network->type_ = SYSTEM_INFO_NETWORK_NONE; - instance->SetData(data); - system_info::SetPicoJsonObjectValue(output, "cmd", - picojson::value("SystemInfoPropertyValueChanged")); - system_info::SetPicoJsonObjectValue(output, "prop", - picojson::value("NETWORK")); - system_info::SetPicoJsonObjectValue(output, "data", data); + picojson::value output = picojson::value(picojson::object()); + picojson::value data = picojson::value(picojson::object()); - std::string result = output.serialize(); - instance->api_->PostMessage(result.c_str()); - } + network->SetData(data); + system_info::SetPicoJsonObjectValue(output, "cmd", + picojson::value("SystemInfoPropertyValueChanged")); + system_info::SetPicoJsonObjectValue(output, "prop", + picojson::value("NETWORK")); + system_info::SetPicoJsonObjectValue(output, "data", data); - return TRUE; + std::string result = output.serialize(); + network->api_->PostMessage(result.c_str()); } -- 2.7.4