Skip to content

Commit c75dce5

Browse files
authored
[connectivity_plus] Refactor the C++ code (#352)
1 parent 946d969 commit c75dce5

File tree

3 files changed

+208
-129
lines changed

3 files changed

+208
-129
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2022 Samsung Electronics Co., Ltd. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "connection.h"
6+
7+
#include "log.h"
8+
9+
static ConnectionType ToConnectionType(connection_type_e type) {
10+
switch (type) {
11+
case CONNECTION_TYPE_WIFI:
12+
return ConnectionType::kWiFi;
13+
case CONNECTION_TYPE_CELLULAR:
14+
return ConnectionType::kMobile;
15+
case CONNECTION_TYPE_ETHERNET:
16+
return ConnectionType::kEthernet;
17+
case CONNECTION_TYPE_DISCONNECTED:
18+
default:
19+
return ConnectionType::kNone;
20+
}
21+
}
22+
23+
Connection::Connection() {
24+
int ret = connection_create(&connection_);
25+
if (ret != CONNECTION_ERROR_NONE) {
26+
LOG_ERROR("Failed to create handle: %s", get_error_message(ret));
27+
last_error_ = ret;
28+
}
29+
}
30+
31+
Connection::~Connection() {
32+
if (connection_) {
33+
connection_unset_type_changed_cb(connection_);
34+
connection_destroy(connection_);
35+
connection_ = nullptr;
36+
}
37+
}
38+
39+
bool Connection::StartListen(ConnectionTypeCallback callback) {
40+
int ret = connection_set_type_changed_cb(
41+
connection_,
42+
[](connection_type_e type, void *user_data) -> void {
43+
auto *self = static_cast<Connection *>(user_data);
44+
self->callback_(ToConnectionType(type));
45+
},
46+
this);
47+
if (ret != CONNECTION_ERROR_NONE) {
48+
LOG_ERROR("Failed to add callback: %s", get_error_message(ret));
49+
last_error_ = ret;
50+
return false;
51+
}
52+
53+
callback_ = callback;
54+
return true;
55+
}
56+
57+
void Connection::StopListen() {
58+
int ret = connection_unset_type_changed_cb(connection_);
59+
if (ret != CONNECTION_ERROR_NONE) {
60+
LOG_ERROR("Failed to remove callback: %s", get_error_message(ret));
61+
last_error_ = ret;
62+
}
63+
}
64+
65+
ConnectionType Connection::GetType() {
66+
connection_type_e type;
67+
int ret = connection_get_type(connection_, &type);
68+
if (ret != CONNECTION_ERROR_NONE) {
69+
LOG_ERROR("Failed to get connection type: %s", get_error_message(ret));
70+
last_error_ = ret;
71+
return ConnectionType::kError;
72+
}
73+
return ToConnectionType(type);
74+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2022 Samsung Electronics Co., Ltd. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_PLUGIN_CONNECTION_H_
6+
#define FLUTTER_PLUGIN_CONNECTION_H_
7+
8+
#include <net_connection.h>
9+
#include <tizen.h>
10+
11+
#include <functional>
12+
#include <string>
13+
14+
enum class ConnectionType { kNone, kEthernet, kWiFi, kMobile, kError };
15+
16+
typedef std::function<void(ConnectionType)> ConnectionTypeCallback;
17+
18+
class Connection {
19+
public:
20+
Connection();
21+
~Connection();
22+
23+
int GetLastError() { return last_error_; }
24+
25+
std::string GetLastErrorString() { return get_error_message(last_error_); }
26+
27+
bool StartListen(ConnectionTypeCallback callback);
28+
29+
void StopListen();
30+
31+
ConnectionType GetType();
32+
33+
private:
34+
int last_error_ = TIZEN_ERROR_NONE;
35+
ConnectionTypeCallback callback_ = nullptr;
36+
connection_h connection_;
37+
};
38+
39+
#endif // FLUTTER_PLUGIN_CONNECTION_H_

packages/connectivity_plus/tizen/src/connectivity_plus_tizen_plugin.cc

Lines changed: 95 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -6,162 +6,128 @@
66

77
#include <flutter/event_channel.h>
88
#include <flutter/event_sink.h>
9-
#include <flutter/event_stream_handler_functions.h>
9+
#include <flutter/event_stream_handler.h>
1010
#include <flutter/method_channel.h>
1111
#include <flutter/plugin_registrar.h>
1212
#include <flutter/standard_method_codec.h>
13-
#include <net_connection.h>
14-
#include <wifi-manager.h>
1513

1614
#include <memory>
1715
#include <string>
1816

19-
#include "log.h"
20-
21-
class ConnectivityPlusTizenPlugin : public flutter::Plugin {
22-
public:
23-
static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar) {
24-
auto plugin = std::make_unique<ConnectivityPlusTizenPlugin>();
25-
plugin->SetupChannels(registrar);
26-
registrar->AddPlugin(std::move(plugin));
27-
}
28-
29-
ConnectivityPlusTizenPlugin() : connection_(nullptr), events_(nullptr) {
30-
EnsureConnectionHandle();
17+
#include "connection.h"
18+
19+
namespace {
20+
21+
typedef flutter::EventChannel<flutter::EncodableValue> FlEventChannel;
22+
typedef flutter::EventSink<flutter::EncodableValue> FlEventSink;
23+
typedef flutter::MethodCall<flutter::EncodableValue> FlMethodCall;
24+
typedef flutter::MethodResult<flutter::EncodableValue> FlMethodResult;
25+
typedef flutter::MethodChannel<flutter::EncodableValue> FlMethodChannel;
26+
typedef flutter::StreamHandler<flutter::EncodableValue> FlStreamHandler;
27+
typedef flutter::StreamHandlerError<flutter::EncodableValue>
28+
FlStreamHandlerError;
29+
30+
std::string ConnectionTypeToString(ConnectionType type) {
31+
switch (type) {
32+
case ConnectionType::kEthernet:
33+
return "ethernet";
34+
case ConnectionType::kWiFi:
35+
return "wifi";
36+
case ConnectionType::kMobile:
37+
return "mobile";
38+
case ConnectionType::kNone:
39+
default:
40+
return "none";
3141
}
42+
}
3243

33-
virtual ~ConnectivityPlusTizenPlugin() {
34-
if (connection_ != nullptr) {
35-
connection_destroy(connection_);
36-
connection_ = nullptr;
37-
}
38-
}
44+
class ConnectivityStreamHandler : public FlStreamHandler {
45+
protected:
46+
std::unique_ptr<FlStreamHandlerError> OnListenInternal(
47+
const flutter::EncodableValue *arguments,
48+
std::unique_ptr<FlEventSink> &&events) override {
49+
events_ = std::move(events);
3950

40-
void RegisterObserver(
41-
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>> &&events) {
42-
EnsureConnectionHandle();
43-
if (connection_set_type_changed_cb(
44-
connection_,
45-
[](connection_type_e state, void *data) -> void {
46-
auto *self = static_cast<ConnectivityPlusTizenPlugin *>(data);
47-
self->SendConnectivityChangedEvent(state);
48-
},
49-
this) != CONNECTION_ERROR_NONE) {
50-
return;
51+
ConnectionTypeCallback callback = [this](ConnectionType type) -> void {
52+
if (type != ConnectionType::kError) {
53+
events_->Success(flutter::EncodableValue(ConnectionTypeToString(type)));
54+
} else {
55+
events_->Error(std::to_string(connection_.GetLastError()),
56+
connection_.GetLastErrorString());
57+
}
58+
};
59+
if (!connection_.StartListen(callback)) {
60+
return std::make_unique<FlStreamHandlerError>(
61+
std::to_string(connection_.GetLastError()),
62+
connection_.GetLastErrorString(), nullptr);
5163
}
52-
events_ = std::move(events);
53-
}
5464

55-
void ClearObserver() {
56-
if (connection_ == nullptr || events_ == nullptr) return;
65+
// Send an initial event once the stream has been set up.
66+
callback(connection_.GetType());
5767

58-
connection_unset_type_changed_cb(connection_);
59-
events_ = nullptr;
68+
return nullptr;
6069
}
6170

62-
void SendConnectivityChangedEvent(connection_type_e state) {
63-
if (events_ == nullptr) {
64-
return;
65-
}
66-
std::string replay = ConvertConnectionTypeToString(state);
67-
flutter::EncodableValue msg(replay);
68-
events_->Success(msg);
71+
std::unique_ptr<FlStreamHandlerError> OnCancelInternal(
72+
const flutter::EncodableValue *arguments) override {
73+
connection_.StopListen();
74+
events_.reset();
75+
return nullptr;
6976
}
7077

7178
private:
72-
void EnsureConnectionHandle() {
73-
if (connection_ == nullptr) {
74-
if (connection_create(&connection_) != CONNECTION_ERROR_NONE) {
75-
connection_ = nullptr;
76-
}
77-
}
78-
}
79+
Connection connection_;
80+
std::unique_ptr<FlEventSink> events_;
81+
};
7982

80-
std::string ConvertConnectionTypeToString(connection_type_e net_state) {
81-
std::string result;
82-
switch (net_state) {
83-
case CONNECTION_TYPE_WIFI:
84-
result = "wifi";
85-
break;
86-
case CONNECTION_TYPE_ETHERNET:
87-
result = "ethernet";
88-
break;
89-
case CONNECTION_TYPE_CELLULAR:
90-
result = "mobile";
91-
break;
92-
case CONNECTION_TYPE_DISCONNECTED:
93-
default:
94-
result = "none";
95-
}
96-
return result;
83+
class ConnectivityPlusTizenPlugin : public flutter::Plugin {
84+
public:
85+
static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar) {
86+
auto plugin = std::make_unique<ConnectivityPlusTizenPlugin>();
87+
88+
auto method_channel = std::make_unique<FlMethodChannel>(
89+
registrar->messenger(), "dev.fluttercommunity.plus/connectivity",
90+
&flutter::StandardMethodCodec::GetInstance());
91+
method_channel->SetMethodCallHandler(
92+
[plugin_pointer = plugin.get()](const auto &call, auto result) {
93+
plugin_pointer->HandleMethodCall(call, std::move(result));
94+
});
95+
96+
auto event_channel = std::make_unique<FlEventChannel>(
97+
registrar->messenger(), "dev.fluttercommunity.plus/connectivity_status",
98+
&flutter::StandardMethodCodec::GetInstance());
99+
event_channel->SetStreamHandler(
100+
std::make_unique<ConnectivityStreamHandler>());
101+
102+
registrar->AddPlugin(std::move(plugin));
97103
}
98104

99-
void HandleMethodCall(
100-
const flutter::MethodCall<flutter::EncodableValue> &method_call,
101-
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
102-
EnsureConnectionHandle();
103-
LOG_INFO("method : %s", method_call.method_name().data());
104-
105-
std::string replay = "";
106-
if (method_call.method_name().compare("check") == 0) {
107-
connection_type_e net_state;
108-
if (connection_get_type(connection_, &net_state) !=
109-
CONNECTION_ERROR_NONE) {
110-
result->Error("-1", "Couldn't know current connection type");
111-
return;
105+
ConnectivityPlusTizenPlugin() {}
106+
107+
virtual ~ConnectivityPlusTizenPlugin() {}
108+
109+
private:
110+
void HandleMethodCall(const FlMethodCall &method_call,
111+
std::unique_ptr<FlMethodResult> result) {
112+
const auto &method_name = method_call.method_name();
113+
114+
if (method_name == "check") {
115+
Connection connection;
116+
ConnectionType type = connection.GetType();
117+
if (type != ConnectionType::kError) {
118+
result->Success(flutter::EncodableValue(ConnectionTypeToString(type)));
119+
} else {
120+
result->Error(std::to_string(connection.GetLastError()),
121+
connection.GetLastErrorString());
112122
}
113-
replay = ConvertConnectionTypeToString(net_state);
114123
} else {
115-
result->Error("-1", "Not supported method");
116-
return;
124+
result->NotImplemented();
117125
}
118-
if (replay.length() == 0) {
119-
result->Error("-1", "Not valid result");
120-
return;
121-
}
122-
123-
flutter::EncodableValue msg(replay);
124-
result->Success(msg);
125126
}
126-
127-
void SetupChannels(flutter::PluginRegistrar *registrar) {
128-
auto method_channel =
129-
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
130-
registrar->messenger(), "dev.fluttercommunity.plus/connectivity",
131-
&flutter::StandardMethodCodec::GetInstance());
132-
event_channel_ =
133-
std::make_unique<flutter::EventChannel<flutter::EncodableValue>>(
134-
registrar->messenger(),
135-
"dev.fluttercommunity.plus/connectivity_status",
136-
&flutter::StandardMethodCodec::GetInstance());
137-
method_channel->SetMethodCallHandler([this](const auto &call, auto result) {
138-
HandleMethodCall(call, std::move(result));
139-
});
140-
141-
auto event_channel_handler =
142-
std::make_unique<flutter::StreamHandlerFunctions<>>(
143-
[this](const flutter::EncodableValue *arguments,
144-
std::unique_ptr<flutter::EventSink<>> &&events)
145-
-> std::unique_ptr<flutter::StreamHandlerError<>> {
146-
LOG_INFO("OnListen");
147-
RegisterObserver(std::move(events));
148-
return nullptr;
149-
},
150-
[this](const flutter::EncodableValue *arguments)
151-
-> std::unique_ptr<flutter::StreamHandlerError<>> {
152-
LOG_INFO("OnCancel");
153-
ClearObserver();
154-
return nullptr;
155-
});
156-
event_channel_->SetStreamHandler(std::move(event_channel_handler));
157-
}
158-
159-
std::unique_ptr<flutter::EventChannel<flutter::EncodableValue>>
160-
event_channel_;
161-
connection_h connection_;
162-
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>> events_;
163127
};
164128

129+
} // namespace
130+
165131
void ConnectivityPlusTizenPluginRegisterWithRegistrar(
166132
FlutterDesktopPluginRegistrarRef registrar) {
167133
ConnectivityPlusTizenPlugin::RegisterWithRegistrar(

0 commit comments

Comments
 (0)