Skip to content

Commit 7a2bcac

Browse files
committed
[network_info_plus] Minor refactoring
1 parent c5edd33 commit 7a2bcac

File tree

2 files changed

+82
-110
lines changed

2 files changed

+82
-110
lines changed

packages/network_info_plus/tizen/src/log.h

Lines changed: 0 additions & 24 deletions
This file was deleted.

packages/network_info_plus/tizen/src/network_info_plus_tizen_plugin.cc

Lines changed: 82 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@
77
#include <flutter/method_channel.h>
88
#include <flutter/plugin_registrar.h>
99
#include <flutter/standard_method_codec.h>
10-
#include <net_connection.h>
10+
#include <tizen.h>
1111
#include <wifi-manager.h>
1212

13-
#include <map>
1413
#include <memory>
15-
#include <sstream>
1614
#include <string>
1715

18-
#include "log.h"
16+
namespace {
1917

2018
class NetworkInfoPlusTizenPlugin : public flutter::Plugin {
2119
public:
22-
enum WifiInfoType { ESSID, BSSID, IPV4, IPV6, SUBNET_MASK, GATEWAY_ADDR };
20+
enum class WifiInfoType {
21+
kESSID,
22+
kBSSID,
23+
kIPv4Address,
24+
kIPv6Address,
25+
kSubnetMask,
26+
kGatewayAddress
27+
};
2328

2429
static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar) {
2530
auto channel =
@@ -37,117 +42,108 @@ class NetworkInfoPlusTizenPlugin : public flutter::Plugin {
3742
registrar->AddPlugin(std::move(plugin));
3843
}
3944

40-
NetworkInfoPlusTizenPlugin() : wifi_manager_(nullptr) {
41-
EnsureConnectionHandle();
42-
}
45+
NetworkInfoPlusTizenPlugin() {}
4346

44-
virtual ~NetworkInfoPlusTizenPlugin() {
45-
if (wifi_manager_ != nullptr) {
46-
wifi_manager_deinitialize(wifi_manager_);
47-
wifi_manager_ = nullptr;
48-
}
49-
}
47+
virtual ~NetworkInfoPlusTizenPlugin() {}
5048

5149
private:
5250
std::string GetWifiInfo(WifiInfoType type) {
53-
std::string result;
51+
wifi_manager_h wifi_manager = nullptr;
52+
int ret = wifi_manager_initialize(&wifi_manager);
53+
if (ret != WIFI_MANAGER_ERROR_NONE) {
54+
return std::string();
55+
}
56+
5457
wifi_manager_ap_h current_ap = nullptr;
55-
char *name = nullptr;
56-
int errorcode;
57-
58-
errorcode = wifi_manager_get_connected_ap(wifi_manager_, &current_ap);
59-
if (errorcode == WIFI_MANAGER_ERROR_NONE && current_ap != nullptr) {
60-
if (type == WifiInfoType::ESSID) {
61-
errorcode = wifi_manager_ap_get_essid(current_ap, &name);
62-
} else if (type == WifiInfoType::BSSID) {
63-
errorcode = wifi_manager_ap_get_bssid(current_ap, &name);
64-
} else if (type == WifiInfoType::IPV4) {
65-
errorcode = wifi_manager_ap_get_ip_address(
66-
current_ap, WIFI_MANAGER_ADDRESS_FAMILY_IPV4, &name);
67-
} else if (type == WifiInfoType::IPV6) {
68-
errorcode = wifi_manager_ap_get_ip_address(
69-
current_ap, WIFI_MANAGER_ADDRESS_FAMILY_IPV6, &name);
70-
} else if (type == WifiInfoType::SUBNET_MASK) {
71-
errorcode = wifi_manager_ap_get_subnet_mask(
72-
current_ap, WIFI_MANAGER_ADDRESS_FAMILY_IPV4, &name);
73-
} else { // WifiInfoType::GATEWAY_ADDR
74-
// The requested gateway address is implicitly ipv4.
75-
// https://github.com/fluttercommunity/plus_plugins/blob/bd0262e5f4627358bfb42481a84122f60921d98b/packages/network_info_plus/network_info_plus/android/src/main/java/dev/fluttercommunity/plus/network_info/NetworkInfo.java#L108
76-
errorcode = wifi_manager_ap_get_gateway_address(
77-
current_ap, WIFI_MANAGER_ADDRESS_FAMILY_IPV4, &name);
78-
}
79-
if (errorcode == WIFI_MANAGER_ERROR_NONE) {
80-
result = name;
81-
free(name);
82-
}
83-
wifi_manager_ap_destroy(current_ap);
58+
ret = wifi_manager_get_connected_ap(wifi_manager, &current_ap);
59+
if (ret != WIFI_MANAGER_ERROR_NONE) {
60+
wifi_manager_deinitialize(wifi_manager);
61+
return std::string();
62+
}
63+
64+
char *value = nullptr;
65+
if (type == WifiInfoType::kESSID) {
66+
ret = wifi_manager_ap_get_essid(current_ap, &value);
67+
} else if (type == WifiInfoType::kBSSID) {
68+
ret = wifi_manager_ap_get_bssid(current_ap, &value);
69+
} else if (type == WifiInfoType::kIPv4Address) {
70+
ret = wifi_manager_ap_get_ip_address(
71+
current_ap, WIFI_MANAGER_ADDRESS_FAMILY_IPV4, &value);
72+
} else if (type == WifiInfoType::kIPv6Address) {
73+
ret = wifi_manager_ap_get_ip_address(
74+
current_ap, WIFI_MANAGER_ADDRESS_FAMILY_IPV6, &value);
75+
} else if (type == WifiInfoType::kSubnetMask) {
76+
ret = wifi_manager_ap_get_subnet_mask(
77+
current_ap, WIFI_MANAGER_ADDRESS_FAMILY_IPV4, &value);
78+
} else if (type == WifiInfoType::kGatewayAddress) {
79+
// The requested gateway address is implicitly IPv4.
80+
ret = wifi_manager_ap_get_gateway_address(
81+
current_ap, WIFI_MANAGER_ADDRESS_FAMILY_IPV4, &value);
82+
}
83+
84+
std::string result;
85+
if (value && ret == WIFI_MANAGER_ERROR_NONE) {
86+
result = value;
87+
free(value);
8488
}
89+
90+
wifi_manager_ap_destroy(current_ap);
91+
wifi_manager_deinitialize(wifi_manager);
92+
8593
return result;
8694
}
8795

8896
void HandleMethodCall(
8997
const flutter::MethodCall<flutter::EncodableValue> &method_call,
9098
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
91-
if (!EnsureConnectionHandle()) {
92-
result->Error("-1", "Initialization failed");
93-
return;
94-
}
95-
std::string reply;
96-
if (method_call.method_name().compare("wifiName") == 0) {
97-
reply = GetWifiInfo(WifiInfoType::ESSID);
98-
} else if (method_call.method_name().compare("wifiBSSID") == 0) {
99-
reply = GetWifiInfo(WifiInfoType::BSSID);
100-
} else if (method_call.method_name().compare("wifiIPAddress") == 0) {
101-
reply = GetWifiInfo(WifiInfoType::IPV4);
102-
} else if (method_call.method_name().compare("wifiIPv6Address") == 0) {
103-
reply = GetWifiInfo(WifiInfoType::IPV6);
104-
} else if (method_call.method_name().compare("wifiSubmask") == 0) {
105-
reply = GetWifiInfo(WifiInfoType::SUBNET_MASK);
106-
} else if (method_call.method_name().compare("wifiGatewayAddress") == 0) {
107-
reply = GetWifiInfo(WifiInfoType::GATEWAY_ADDR);
108-
} else if (method_call.method_name().compare("wifiBroadcast") == 0) {
109-
std::string ipv4 = GetWifiInfo(WifiInfoType::IPV4);
110-
std::string subnet_mask = GetWifiInfo(WifiInfoType::SUBNET_MASK);
99+
const auto &method_name = method_call.method_name();
100+
101+
std::string value;
102+
if (method_name == "wifiName") {
103+
value = GetWifiInfo(WifiInfoType::kESSID);
104+
} else if (method_name == "wifiBSSID") {
105+
value = GetWifiInfo(WifiInfoType::kBSSID);
106+
} else if (method_name == "wifiIPAddress") {
107+
value = GetWifiInfo(WifiInfoType::kIPv4Address);
108+
} else if (method_name == "wifiIPv6Address") {
109+
value = GetWifiInfo(WifiInfoType::kIPv6Address);
110+
} else if (method_name == "wifiSubmask") {
111+
value = GetWifiInfo(WifiInfoType::kSubnetMask);
112+
} else if (method_name == "wifiGatewayAddress") {
113+
value = GetWifiInfo(WifiInfoType::kGatewayAddress);
114+
} else if (method_name == "wifiBroadcast") {
115+
std::string ipv4 = GetWifiInfo(WifiInfoType::kIPv4Address);
116+
std::string subnet_mask = GetWifiInfo(WifiInfoType::kSubnetMask);
111117
if (!ipv4.empty() && !subnet_mask.empty()) {
112-
reply = IntegerToDottedDecimal(DottedDecimalToInteger(ipv4) |
118+
value = IntegerToDottedDecimal(DottedDecimalToInteger(ipv4) |
113119
~DottedDecimalToInteger(subnet_mask));
114120
}
115121
} else {
116122
result->NotImplemented();
117123
return;
118124
}
119-
if (reply.length() == 0) {
120-
result->Error("-1", "Not valid result");
121-
LOG_ERROR("Could not retrieve %s.", method_call.method_name().c_str());
122-
return;
123-
}
124-
flutter::EncodableValue msg(reply);
125-
result->Success(msg);
126-
}
127125

128-
bool EnsureConnectionHandle() {
129-
if (wifi_manager_ == nullptr) {
130-
if (wifi_manager_initialize(&wifi_manager_) != WIFI_MANAGER_ERROR_NONE) {
131-
wifi_manager_ = nullptr;
132-
return false;
133-
}
126+
if (value.empty()) {
127+
result->Error(std::to_string(get_last_result()),
128+
get_error_message(get_last_result()));
129+
return;
134130
}
135-
return true;
131+
result->Success(flutter::EncodableValue(value));
136132
}
137133

138-
unsigned int DottedDecimalToInteger(std::string dottedDecimal) {
134+
unsigned int DottedDecimalToInteger(std::string dotted_decimal) {
139135
size_t pos = 0;
140-
size_t len = dottedDecimal.size();
136+
size_t len = dotted_decimal.size();
141137
std::string token;
142138
unsigned int value = 0U;
143139
unsigned int base = 1U << 24U;
144140
while (pos < len) {
145-
if (dottedDecimal[pos] == '.') {
141+
if (dotted_decimal[pos] == '.') {
146142
value += std::stoul(token) * base;
147143
base >>= 8U;
148144
token.clear();
149145
} else {
150-
token.push_back(dottedDecimal[pos]);
146+
token.push_back(dotted_decimal[pos]);
151147
}
152148
pos++;
153149
}
@@ -160,10 +156,10 @@ class NetworkInfoPlusTizenPlugin : public flutter::Plugin {
160156
std::to_string((value >> 8U) % 256U) + "." +
161157
std::to_string(value % 256U);
162158
}
163-
164-
wifi_manager_h wifi_manager_;
165159
};
166160

161+
} // namespace
162+
167163
void NetworkInfoPlusTizenPluginRegisterWithRegistrar(
168164
FlutterDesktopPluginRegistrarRef registrar) {
169165
NetworkInfoPlusTizenPlugin::RegisterWithRegistrar(

0 commit comments

Comments
 (0)