#if defined(GENERIC_DESKTOP)
#include <gio/gio.h>
#elif defined(TIZEN_MOBILE)
-#include <glib.h>
+#include <net_connection.h>
#endif
#include <string>
explicit SysInfoWifiNetwork(ContextAPI* api);
~SysInfoWifiNetwork();
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,
- SysInfoWifiNetwork::OnUpdateTimeout,
- static_cast<gpointer>(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();
void UpdateIPAddress(GVariant* value);
void UpdateIPv6Address(GVariant* value);
void UpdateSignalStrength(GVariant* value);
- void UpdateSsid(GVariant* value);
+ void UpdateSSID(GVariant* value);
void UpdateStatus(guint new_device_type);
static void OnAccessPointSignal(GDBusProxy* proxy, gchar* sender,
std::string active_access_point_;
std::string active_connection_;
std::string active_device_;
- std::string ip6_config_;
+ std::string ipv6_config_;
unsigned int ip_address_desktop_;
#elif defined(TIZEN_MOBILE)
- static gboolean OnUpdateTimeout(gpointer user_data);
- int timeout_cb_id_;
+ bool GetIPv4Address();
+ bool GetIPv6Address();
+ bool GetSignalStrength();
+ bool GetSSID();
+ bool GetType();
+ static void OnIPChanged(const char* ipv4_addr, const char* ipv6_addr,
+ void* user_data);
+ static void OnTypeChanged(connection_type_e type, void* user_data);
+
+ bool is_registered_;
+ connection_h connection_handle_;
+ connection_profile_h connection_profile_handle_;
#endif
DISALLOW_COPY_AND_ASSIGN(SysInfoWifiNetwork);
#define NM_WIRELESS NM_DBUS_INTERFACE_DEVICE ".Wireless"
#define NM_IP4_ADDRESS NM_DBUS_INTERFACE_DEVICE ".Ip4Address"
+namespace {
+
+const double kWifiSignalStrengthDivisor = 100.0;
+
+} // namespace
+
+SysInfoWifiNetwork::SysInfoWifiNetwork(ContextAPI* api)
+ : signal_strength_(0.0),
+ ip_address_(""),
+ ipv6_address_(""),
+ ssid_(""),
+ status_("OFF") {
+ api_ = api;
+ PlatformInitialize();
+}
+
void SysInfoWifiNetwork::PlatformInitialize() {
active_access_point_ = "";
active_connection_ = "";
active_device_ = "";
device_type_ = NM_DEVICE_TYPE_UNKNOWN;
- ip6_config_ = "";
+ ipv6_config_ = "";
g_dbus_proxy_new_for_bus(G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
SysInfoWifiNetwork::~SysInfoWifiNetwork() {
}
+void SysInfoWifiNetwork::StartListening() {
+}
+
+void SysInfoWifiNetwork::StopListening() {
+}
+
void SysInfoWifiNetwork::SetData(picojson::value& data) {
system_info::SetPicoJsonObjectValue(data, "status",
picojson::value(status_));
g_printerr("Failed to get ssid or strength.");
return;
}
- UpdateSsid(ssid);
+ UpdateSSID(ssid);
UpdateSignalStrength(strength);
g_signal_connect(proxy, "g-signal",
}
const char* str = g_variant_get_string(value, NULL);
- if (str && (strcmp(ip6_config_.c_str(), str) != 0)) {
+ if (str && (strcmp(ipv6_config_.c_str(), str) != 0)) {
if (strlen(str) > 2) {
- ip6_config_ = std::string(str);
+ ipv6_config_ = std::string(str);
g_dbus_proxy_new_for_bus(G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
NM_DBUS_SERVICE,
- ip6_config_.c_str(),
+ ipv6_config_.c_str(),
NM_DBUS_INTERFACE_IP6_CONFIG,
NULL,
OnUpdateIPv6AddressThunk,
SendUpdate();
}
-void SysInfoWifiNetwork::UpdateSsid(GVariant* value) {
+void SysInfoWifiNetwork::UpdateSSID(GVariant* value) {
gsize length = g_variant_get_size(value);
gconstpointer g_pointer = g_variant_get_fixed_array(value, &length,
sizeof(guchar));
void SysInfoWifiNetwork::UpdateSignalStrength(GVariant* value) {
int result = static_cast<int>(g_variant_get_byte(value));
- double strength = static_cast<double>(result) / 100.0;
+ double strength = static_cast<double>(result) / kWifiSignalStrengthDivisor;
if (strength == signal_strength_)
return;
signal_strength_ = strength;
g_variant_get(parameters, "(a{sv})", &iter);
while (g_variant_iter_loop(iter, "{&sv}", &key, &value)) {
if (strcmp(key, "Ssid") == 0) {
- self->UpdateSsid(value);
+ self->UpdateSSID(value);
} else if (strcmp(key, "Strength") == 0) {
self->UpdateSignalStrength(value);
}
#include "system_info/system_info_wifi_network.h"
-#include <net_connection.h>
-
#include "system_info/system_info_utils.h"
-const char* WIFI_STATE = "memory/wifi/state";
+namespace {
-SysInfoWifiNetwork::~SysInfoWifiNetwork() {
- if (timeout_cb_id_ > 0) {
- g_source_remove(timeout_cb_id_);
- }
+const double kWifiSignalStrengthDivisor = 100.0;
+
+} // namespace
+
+SysInfoWifiNetwork::SysInfoWifiNetwork(ContextAPI* api)
+ : signal_strength_(0.0),
+ ip_address_(""),
+ ipv6_address_(""),
+ ssid_(""),
+ status_("OFF"),
+ is_registered_(false),
+ connection_handle_(NULL),
+ connection_profile_handle_(NULL) {
+ api_ = api;
+ PlatformInitialize();
}
void SysInfoWifiNetwork::PlatformInitialize() {
- timeout_cb_id_ = 0;
+ if (connection_create(&connection_handle_) != CONNECTION_ERROR_NONE) {
+ connection_handle_ = NULL;
+ connection_profile_handle_ = NULL;
+ } else {
+ if (connection_get_current_profile(connection_handle_,
+ &connection_profile_handle_) !=
+ CONNECTION_ERROR_NONE)
+ connection_profile_handle_ = NULL;
+ }
+}
+
+SysInfoWifiNetwork::~SysInfoWifiNetwork() {
+ if (is_registered_)
+ StopListening();
+ if (connection_profile_handle_)
+ free(connection_profile_handle_);
+ if (connection_handle_)
+ free(connection_handle_);
+}
+
+void SysInfoWifiNetwork::StartListening() {
+ if (connection_handle_ && !is_registered_) {
+ connection_set_type_changed_cb(connection_handle_,
+ OnTypeChanged, this);
+ connection_set_ip_address_changed_cb(connection_handle_,
+ OnIPChanged, this);
+ is_registered_ = true;
+ }
+}
+
+void SysInfoWifiNetwork::StopListening() {
+ if (connection_handle_) {
+ connection_unset_type_changed_cb(connection_handle_);
+ connection_unset_ip_address_changed_cb(connection_handle_);
+ is_registered_ = false;
+ }
+}
+
+void SysInfoWifiNetwork::SetData(picojson::value& data) {
+ system_info::SetPicoJsonObjectValue(data, "status",
+ picojson::value(status_));
+ system_info::SetPicoJsonObjectValue(data, "ssid",
+ picojson::value(ssid_));
+ system_info::SetPicoJsonObjectValue(data, "ipAddress",
+ picojson::value(ip_address_));
+ system_info::SetPicoJsonObjectValue(data, "ipv6Address",
+ picojson::value(ipv6_address_));
+ system_info::SetPicoJsonObjectValue(data, "signalStrength",
+ picojson::value(signal_strength_));
}
bool SysInfoWifiNetwork::Update(picojson::value& error) {
- connection_h connect = NULL;
- if (connection_create(&connect) != CONNECTION_ERROR_NONE) {
+ if (!connection_handle_) {
if (error.get("message").to_str().empty())
system_info::SetPicoJsonObjectValue(error, "message",
picojson::value("Get wifi connection faild."));
return false;
}
- connection_wifi_state_e state;
- if (connection_get_wifi_state(connect, &state) != CONNECTION_ERROR_NONE) {
- if (error.get("message").to_str().empty())
- system_info::SetPicoJsonObjectValue(error, "message",
- picojson::value("Get wifi state faild."));
- return false;
+ if (!GetType()) {
+ status_ = "OFF";
+ ssid_ = "";
+ ip_address_ = "";
+ ipv6_address_ = "";
+ signal_strength_ = 0.0;
+ return true;
}
- connection_profile_h profile = NULL;
- if (connection_get_current_profile(connect, &profile) !=
+ if (!GetIPv4Address())
+ ip_address_ = "";
+
+ if (!GetIPv6Address())
+ ipv6_address_ = "";
+
+ if (!GetSSID())
+ ssid_ = "";
+
+ if (!GetSignalStrength())
+ signal_strength_ = 0.0;
+
+ return true;
+}
+
+bool SysInfoWifiNetwork::GetType() {
+ connection_type_e type_state;
+ if (connection_get_type(connection_handle_, &type_state) !=
+ CONNECTION_ERROR_NONE)
+ return false;
+
+ if (type_state != CONNECTION_TYPE_WIFI)
+ return false;
+
+ if (connection_get_current_profile(connection_handle_,
+ &connection_profile_handle_) !=
CONNECTION_ERROR_NONE) {
- if (error.get("message").to_str().empty())
- system_info::SetPicoJsonObjectValue(error, "message",
- picojson::value("Get wifi connection profile faild."));
+ connection_profile_handle_ = NULL;
return false;
}
+ status_ = "ON";
+ return true;
+}
+
+bool SysInfoWifiNetwork::GetIPv4Address() {
char* ipv4 = NULL;
- if (connection_profile_get_ip_address(profile,
+ if (connection_profile_get_ip_address(connection_profile_handle_,
CONNECTION_ADDRESS_FAMILY_IPV4, &ipv4) !=
CONNECTION_ERROR_NONE) {
- if (error.get("message").to_str().empty())
- system_info::SetPicoJsonObjectValue(error, "message",
- picojson::value("Get wifi ipv4 address faild."));
+ free(ipv4);
return false;
}
- char* essid = NULL;
- if (connection_profile_get_wifi_essid(profile, &essid) !=
+ ip_address_ = std::string(ipv4);
+ free(ipv4);
+ return true;
+}
+
+bool SysInfoWifiNetwork::GetIPv6Address() {
+ char* ipv6 = NULL;
+ if (connection_profile_get_ip_address(connection_profile_handle_,
+ CONNECTION_ADDRESS_FAMILY_IPV6, &ipv6) !=
CONNECTION_ERROR_NONE) {
- if (error.get("message").to_str().empty())
- system_info::SetPicoJsonObjectValue(error, "message",
- picojson::value("Get wifi essid faild."));
+ free(ipv6);
return false;
}
- int rssi = 0;
- if (connection_profile_get_wifi_rssi(profile, &rssi) !=
+ // FIXME(guanxian): get ipv6 address not supported at connection.
+ ipv6_address_ = "";
+ free(ipv6);
+ return true;
+}
+
+bool SysInfoWifiNetwork::GetSSID() {
+ char* essid = NULL;
+ if (connection_profile_get_wifi_essid(connection_profile_handle_, &essid) !=
CONNECTION_ERROR_NONE) {
- if (error.get("message").to_str().empty())
- system_info::SetPicoJsonObjectValue(error, "message",
- picojson::value("Get wifi rssi faild."));
+ free(essid);
return false;
}
+ ssid_ = std::string(essid);
+ free(essid);
+ return true;
+}
+
+bool SysInfoWifiNetwork::GetSignalStrength() {
+ int rssi = 0;
+ if (connection_profile_get_wifi_rssi(connection_profile_handle_, &rssi) !=
+ CONNECTION_ERROR_NONE)
+ return false;
+
+ signal_strength_ = static_cast<double>(rssi) / kWifiSignalStrengthDivisor;
+ return true;
+}
- if (state == CONNECTION_WIFI_STATE_CONNECTED) {
- status_ = "ON";
+void SysInfoWifiNetwork::OnIPChanged(const char* ipv4_addr,
+ const char* ipv6_addr, void* user_data) {
+ SysInfoWifiNetwork* wifi = static_cast<SysInfoWifiNetwork*>(user_data);
+ if (!wifi->GetType()) {
+ wifi->status_ = "OFF";
+ wifi->ip_address_ = "";
+ wifi->ipv6_address_ = "";
+ wifi->ssid_ = "";
+ wifi->signal_strength_ = 0.0;
} else {
- status_ = "OFF";
- ip_address_ = "";
- ipv6_address_ = "";
- ssid_ = "";
- signal_strength_ = 0.0;
- return true;
- }
+ std::string ipv4_address(ipv4_addr);
+ if (ipv4_address != wifi->ip_address_)
+ wifi->ip_address_ = ipv4_address;
- // FIXME(guanxian): get ipv6 address not supported at connection.
- char* ipv6 = NULL;
- if (connection_profile_get_ip_address(profile,
- CONNECTION_ADDRESS_FAMILY_IPV6, &ipv6) !=
- CONNECTION_ERROR_NONE) {
- ipv6_address_ = "";
- }
+ std::string ipv6_address(ipv6_addr);
+ if (ipv6_address != wifi->ipv6_address_)
+ wifi->ipv6_address_ = ipv6_address;
- ip_address_.assign(ipv4);
- ssid_.assign(essid);
- signal_strength_ = rssi / 100.0;
+ if (!wifi->GetSSID())
+ wifi->ssid_ = "";
- return true;
-}
+ if (!wifi->GetSignalStrength())
+ wifi->signal_strength_ = 0.0;
+ }
-void SysInfoWifiNetwork::SetData(picojson::value& data) {
- system_info::SetPicoJsonObjectValue(data, "status",
- picojson::value(status_));
- system_info::SetPicoJsonObjectValue(data, "ssid",
- picojson::value(ssid_));
- system_info::SetPicoJsonObjectValue(data, "ipAddress",
- picojson::value(ip_address_));
- system_info::SetPicoJsonObjectValue(data, "ipv6Address",
- picojson::value(ipv6_address_));
- system_info::SetPicoJsonObjectValue(data, "signalStrength",
- picojson::value(signal_strength_));
+ wifi->SendUpdate();
}
-gboolean SysInfoWifiNetwork::OnUpdateTimeout(gpointer user_data) {
- SysInfoWifiNetwork* instance = static_cast<SysInfoWifiNetwork*>(user_data);
-
- double signal_strength = instance->signal_strength_;
- std::string status = instance->status_;
- std::string ssid = instance->ssid_;
- std::string ip_address = instance->ip_address_;
- std::string ipv6_address = instance->ipv6_address_;
- picojson::value error = picojson::value(picojson::object());
- if (!instance->Update(error)) {
- // Failed to update, wait for next round.
- return TRUE;
- }
+void SysInfoWifiNetwork::OnTypeChanged(connection_type_e type,
+ void* user_data) {
+ SysInfoWifiNetwork* wifi = static_cast<SysInfoWifiNetwork*>(user_data);
+ if (type != CONNECTION_TYPE_WIFI) {
+ wifi->status_ = "OFF";
+ wifi->ip_address_ = "";
+ wifi->ipv6_address_ = "";
+ wifi->ssid_ = "";
+ wifi->signal_strength_ = 0.0;
+ } else {
+ if (!wifi->GetType())
+ wifi->status_ = "OFF";
+
+ if (!wifi->GetIPv4Address())
+ wifi->ip_address_ = "";
+
+ if (!wifi->GetIPv6Address())
+ wifi->ipv6_address_ = "";
+
+ if (!wifi->GetSSID())
+ wifi->ssid_ = "";
- if ((status != instance->status_) ||
- (ssid != instance->ssid_) ||
- (ip_address != instance->ip_address_) ||
- (ipv6_address != instance->ipv6_address_) ||
- (signal_strength != instance->signal_strength_)) {
- instance->SendUpdate();
+ if (!wifi->GetSignalStrength())
+ wifi->signal_strength_ = 0.0;
}
- return TRUE;
+ wifi->SendUpdate();
}