7
7
#include < flutter/method_channel.h>
8
8
#include < flutter/plugin_registrar.h>
9
9
#include < flutter/standard_method_codec.h>
10
- #include < net_connection .h>
10
+ #include < tizen .h>
11
11
#include < wifi-manager.h>
12
12
13
- #include < map>
14
13
#include < memory>
15
- #include < sstream>
16
14
#include < string>
17
15
18
- # include " log.h "
16
+ namespace {
19
17
20
18
class NetworkInfoPlusTizenPlugin : public flutter ::Plugin {
21
19
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
+ };
23
28
24
29
static void RegisterWithRegistrar (flutter::PluginRegistrar *registrar) {
25
30
auto channel =
@@ -37,117 +42,108 @@ class NetworkInfoPlusTizenPlugin : public flutter::Plugin {
37
42
registrar->AddPlugin (std::move (plugin));
38
43
}
39
44
40
- NetworkInfoPlusTizenPlugin () : wifi_manager_(nullptr ) {
41
- EnsureConnectionHandle ();
42
- }
45
+ NetworkInfoPlusTizenPlugin () {}
43
46
44
- virtual ~NetworkInfoPlusTizenPlugin () {
45
- if (wifi_manager_ != nullptr ) {
46
- wifi_manager_deinitialize (wifi_manager_);
47
- wifi_manager_ = nullptr ;
48
- }
49
- }
47
+ virtual ~NetworkInfoPlusTizenPlugin () {}
50
48
51
49
private:
52
50
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
+
54
57
wifi_manager_ap_h current_ap = nullptr ;
55
- char *name = nullptr ;
56
- int errorcode;
57
-
58
- errorcode = wifi_manager_get_connected_ap (wifi_manager_, ¤t_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, ¤t_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);
84
88
}
89
+
90
+ wifi_manager_ap_destroy (current_ap);
91
+ wifi_manager_deinitialize (wifi_manager);
92
+
85
93
return result;
86
94
}
87
95
88
96
void HandleMethodCall (
89
97
const flutter::MethodCall<flutter::EncodableValue> &method_call,
90
98
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 );
111
117
if (!ipv4.empty () && !subnet_mask.empty ()) {
112
- reply = IntegerToDottedDecimal (DottedDecimalToInteger (ipv4) |
118
+ value = IntegerToDottedDecimal (DottedDecimalToInteger (ipv4) |
113
119
~DottedDecimalToInteger (subnet_mask));
114
120
}
115
121
} else {
116
122
result->NotImplemented ();
117
123
return ;
118
124
}
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
- }
127
125
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 ;
134
130
}
135
- return true ;
131
+ result-> Success ( flutter::EncodableValue (value)) ;
136
132
}
137
133
138
- unsigned int DottedDecimalToInteger (std::string dottedDecimal ) {
134
+ unsigned int DottedDecimalToInteger (std::string dotted_decimal ) {
139
135
size_t pos = 0 ;
140
- size_t len = dottedDecimal .size ();
136
+ size_t len = dotted_decimal .size ();
141
137
std::string token;
142
138
unsigned int value = 0U ;
143
139
unsigned int base = 1U << 24U ;
144
140
while (pos < len) {
145
- if (dottedDecimal [pos] == ' .' ) {
141
+ if (dotted_decimal [pos] == ' .' ) {
146
142
value += std::stoul (token) * base;
147
143
base >>= 8U ;
148
144
token.clear ();
149
145
} else {
150
- token.push_back (dottedDecimal [pos]);
146
+ token.push_back (dotted_decimal [pos]);
151
147
}
152
148
pos++;
153
149
}
@@ -160,10 +156,10 @@ class NetworkInfoPlusTizenPlugin : public flutter::Plugin {
160
156
std::to_string ((value >> 8U ) % 256U ) + " ." +
161
157
std::to_string (value % 256U );
162
158
}
163
-
164
- wifi_manager_h wifi_manager_;
165
159
};
166
160
161
+ } // namespace
162
+
167
163
void NetworkInfoPlusTizenPluginRegisterWithRegistrar (
168
164
FlutterDesktopPluginRegistrarRef registrar) {
169
165
NetworkInfoPlusTizenPlugin::RegisterWithRegistrar (
0 commit comments