WiFi improvements (#102)
This commit is contained in:
committed by
GitHub
parent
505befef42
commit
c7314546fe
+3
-3
@@ -7,8 +7,8 @@ namespace tt::app::wificonnect {
|
||||
typedef void (*OnConnectSsid)(const service::wifi::settings::WifiApSettings* settings, bool store, void* context);
|
||||
|
||||
typedef struct {
|
||||
OnConnectSsid on_connect_ssid;
|
||||
void* on_connect_ssid_context;
|
||||
} WifiConnectBindings;
|
||||
OnConnectSsid onConnectSsid;
|
||||
void* onConnectSsidContext;
|
||||
} Bindings;
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,48 @@
|
||||
#include "State.h"
|
||||
#include "Check.h"
|
||||
#include <cstring>
|
||||
|
||||
namespace tt::app::wificonnect {
|
||||
|
||||
void State::setConnectionError(bool error) {
|
||||
tt_check(lock.acquire(TtWaitForever) == TtStatusOk);
|
||||
connectionError = error;
|
||||
tt_check(lock.release() == TtStatusOk);
|
||||
}
|
||||
|
||||
bool State::hasConnectionError() const {
|
||||
tt_check(lock.acquire(TtWaitForever) == TtStatusOk);
|
||||
auto result = connectionError;
|
||||
tt_check(lock.release() == TtStatusOk);
|
||||
return result;
|
||||
}
|
||||
|
||||
void State::setApSettings(const service::wifi::settings::WifiApSettings* newSettings) {
|
||||
tt_check(lock.acquire(TtWaitForever) == TtStatusOk);
|
||||
memcpy(&this->apSettings, newSettings, sizeof(service::wifi::settings::WifiApSettings));
|
||||
tt_check(lock.release() == TtStatusOk);
|
||||
}
|
||||
|
||||
const service::wifi::settings::WifiApSettings& State::lockApSettings() {
|
||||
tt_check(lock.acquire(TtWaitForever) == TtStatusOk);
|
||||
return apSettings;
|
||||
}
|
||||
|
||||
void State::unlockApSettings() {
|
||||
tt_check(lock.release() == TtStatusOk);
|
||||
}
|
||||
|
||||
void State::setConnecting(bool isConnecting) {
|
||||
tt_check(lock.acquire(TtWaitForever) == TtStatusOk);
|
||||
connecting = isConnecting;
|
||||
tt_check(lock.release() == TtStatusOk);
|
||||
}
|
||||
|
||||
bool State::isConnecting() const {
|
||||
tt_check(lock.acquire(TtWaitForever) == TtStatusOk);
|
||||
auto result = connecting;
|
||||
tt_check(lock.release() == TtStatusOk);
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include "Mutex.h"
|
||||
#include "service/wifi/Wifi.h"
|
||||
#include "service/wifi/WifiSettings.h"
|
||||
|
||||
namespace tt::app::wificonnect {
|
||||
|
||||
class State {
|
||||
Mutex lock;
|
||||
service::wifi::settings::WifiApSettings apSettings = {
|
||||
.ssid = { 0 },
|
||||
.password = { 0 },
|
||||
.auto_connect = false
|
||||
};
|
||||
bool connectionError = false;
|
||||
bool connecting = false;
|
||||
public:
|
||||
|
||||
void setConnectionError(bool error);
|
||||
bool hasConnectionError() const;
|
||||
|
||||
const service::wifi::settings::WifiApSettings& lockApSettings();
|
||||
void unlockApSettings();
|
||||
|
||||
void setApSettings(const service::wifi::settings::WifiApSettings* newSettings);
|
||||
|
||||
void setConnecting(bool isConnecting);
|
||||
bool isConnecting() const;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,214 @@
|
||||
#include "View.h"
|
||||
#include "State.h"
|
||||
#include "Parameters.h"
|
||||
#include "WifiConnect.h"
|
||||
|
||||
#include "Log.h"
|
||||
#include "lvgl.h"
|
||||
#include "service/gui/Gui.h"
|
||||
#include "service/wifi/WifiSettings.h"
|
||||
#include "lvgl/Style.h"
|
||||
#include "lvgl/Toolbar.h"
|
||||
|
||||
namespace tt::app::wificonnect {
|
||||
|
||||
#define TAG "wifi_connect"
|
||||
|
||||
void View::resetErrors() {
|
||||
lv_obj_add_flag(password_error, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag(ssid_error, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag(connection_error, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
|
||||
static void onConnect(lv_event_t* event) {
|
||||
auto* wifi = (WifiConnect*)lv_event_get_user_data(event);
|
||||
auto& view = wifi->getView();
|
||||
|
||||
wifi->getState().setConnectionError(false);
|
||||
view.resetErrors();
|
||||
|
||||
const char* ssid = lv_textarea_get_text(view.ssid_textarea);
|
||||
size_t ssid_len = strlen(ssid);
|
||||
if (ssid_len > TT_WIFI_SSID_LIMIT) {
|
||||
TT_LOG_E(TAG, "SSID too long");
|
||||
lv_label_set_text(view.ssid_error, "SSID too long");
|
||||
lv_obj_remove_flag(view.ssid_error, LV_OBJ_FLAG_HIDDEN);
|
||||
return;
|
||||
}
|
||||
|
||||
const char* password = lv_textarea_get_text(view.password_textarea);
|
||||
size_t password_len = strlen(password);
|
||||
if (password_len > TT_WIFI_CREDENTIALS_PASSWORD_LIMIT) {
|
||||
TT_LOG_E(TAG, "Password too long");
|
||||
lv_label_set_text(view.password_error, "Password too long");
|
||||
lv_obj_remove_flag(view.password_error, LV_OBJ_FLAG_HIDDEN);
|
||||
return;
|
||||
}
|
||||
|
||||
bool store = lv_obj_get_state(view.remember_switch) & LV_STATE_CHECKED;
|
||||
|
||||
view.setLoading(true);
|
||||
|
||||
service::wifi::settings::WifiApSettings settings;
|
||||
strcpy((char*)settings.password, password);
|
||||
strcpy((char*)settings.ssid, ssid);
|
||||
settings.auto_connect = TT_WIFI_AUTO_CONNECT; // No UI yet, so use global setting:w
|
||||
|
||||
auto* bindings = &wifi->getBindings();
|
||||
bindings->onConnectSsid(
|
||||
&settings,
|
||||
store,
|
||||
bindings->onConnectSsidContext
|
||||
);
|
||||
}
|
||||
|
||||
void View::setLoading(bool loading) {
|
||||
if (loading) {
|
||||
lv_obj_add_flag(connect_button, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_remove_flag(connecting_spinner, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_state(password_textarea, LV_STATE_DISABLED);
|
||||
lv_obj_add_state(ssid_textarea, LV_STATE_DISABLED);
|
||||
lv_obj_add_state(remember_switch, LV_STATE_DISABLED);
|
||||
} else {
|
||||
lv_obj_remove_flag(connect_button, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag(connecting_spinner, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_remove_state(password_textarea, LV_STATE_DISABLED);
|
||||
lv_obj_remove_state(ssid_textarea, LV_STATE_DISABLED);
|
||||
lv_obj_remove_state(remember_switch, LV_STATE_DISABLED);
|
||||
}
|
||||
}
|
||||
|
||||
void View::createBottomButtons(WifiConnect* wifi, lv_obj_t* parent) {
|
||||
lv_obj_t* button_container = lv_obj_create(parent);
|
||||
lv_obj_set_width(button_container, LV_PCT(100));
|
||||
lv_obj_set_height(button_container, LV_SIZE_CONTENT);
|
||||
lvgl::obj_set_style_no_padding(button_container);
|
||||
lv_obj_set_style_border_width(button_container, 0, 0);
|
||||
|
||||
remember_switch = lv_switch_create(button_container);
|
||||
lv_obj_add_state(remember_switch, LV_STATE_CHECKED);
|
||||
lv_obj_align(remember_switch, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
|
||||
lv_obj_t* remember_label = lv_label_create(button_container);
|
||||
lv_label_set_text(remember_label, "Remember");
|
||||
lv_obj_align(remember_label, LV_ALIGN_CENTER, 0, 0);
|
||||
lv_obj_align_to(remember_label, remember_switch, LV_ALIGN_OUT_RIGHT_MID, 4, 0);
|
||||
|
||||
connecting_spinner = lv_spinner_create(button_container);
|
||||
lv_obj_set_size(connecting_spinner, 32, 32);
|
||||
lv_obj_align(connecting_spinner, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_obj_add_flag(connecting_spinner, LV_OBJ_FLAG_HIDDEN);
|
||||
|
||||
connect_button = lv_btn_create(button_container);
|
||||
lv_obj_t* connect_label = lv_label_create(connect_button);
|
||||
lv_label_set_text(connect_label, "Connect");
|
||||
lv_obj_align(connect_button, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_obj_add_event_cb(connect_button, &onConnect, LV_EVENT_CLICKED, wifi);
|
||||
}
|
||||
|
||||
// TODO: Standardize dialogs
|
||||
void View::init(App& app, WifiConnect* wifiConnect, lv_obj_t* parent) {
|
||||
|
||||
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
||||
lvgl::toolbar_create(parent, app);
|
||||
|
||||
lv_obj_t* wrapper = lv_obj_create(parent);
|
||||
lv_obj_set_width(wrapper, LV_PCT(100));
|
||||
lv_obj_set_flex_grow(wrapper, 1);
|
||||
lv_obj_set_flex_flow(wrapper, LV_FLEX_FLOW_COLUMN);
|
||||
|
||||
// SSID
|
||||
|
||||
lv_obj_t* ssid_wrapper = lv_obj_create(wrapper);
|
||||
lv_obj_set_width(ssid_wrapper, LV_PCT(100));
|
||||
lv_obj_set_height(ssid_wrapper, LV_SIZE_CONTENT);
|
||||
lvgl::obj_set_style_no_padding(ssid_wrapper);
|
||||
lv_obj_set_style_border_width(ssid_wrapper, 0, 0);
|
||||
|
||||
lv_obj_t* ssid_label_wrapper = lv_obj_create(ssid_wrapper);
|
||||
lv_obj_set_width(ssid_label_wrapper, LV_PCT(50));
|
||||
lv_obj_set_height(ssid_label_wrapper, LV_SIZE_CONTENT);
|
||||
lv_obj_align(ssid_label_wrapper, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
lv_obj_set_style_border_width(ssid_label_wrapper, 0, 0);
|
||||
lv_obj_set_style_pad_left(ssid_label_wrapper, 0, 0);
|
||||
lv_obj_set_style_pad_right(ssid_label_wrapper, 0, 0);
|
||||
|
||||
lv_obj_t* ssid_label = lv_label_create(ssid_label_wrapper);
|
||||
lv_label_set_text(ssid_label, "Network:");
|
||||
|
||||
ssid_textarea = lv_textarea_create(ssid_wrapper);
|
||||
lv_textarea_set_one_line(ssid_textarea, true);
|
||||
lv_obj_align(ssid_textarea, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_obj_set_width(ssid_textarea, LV_PCT(50));
|
||||
|
||||
ssid_error = lv_label_create(wrapper);
|
||||
lv_obj_set_style_text_color(ssid_error, lv_color_make(255, 50, 50), 0);
|
||||
lv_obj_add_flag(ssid_error, LV_OBJ_FLAG_HIDDEN);
|
||||
|
||||
// Password
|
||||
|
||||
lv_obj_t* password_wrapper = lv_obj_create(wrapper);
|
||||
lv_obj_set_width(password_wrapper, LV_PCT(100));
|
||||
lv_obj_set_height(password_wrapper, LV_SIZE_CONTENT);
|
||||
lvgl::obj_set_style_no_padding(password_wrapper);
|
||||
lv_obj_set_style_border_width(password_wrapper, 0, 0);
|
||||
|
||||
lv_obj_t* password_label_wrapper = lv_obj_create(password_wrapper);
|
||||
lv_obj_set_width(password_label_wrapper, LV_PCT(50));
|
||||
lv_obj_set_height(password_label_wrapper, LV_SIZE_CONTENT);
|
||||
lv_obj_align_to(password_label_wrapper, password_wrapper, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
lv_obj_set_style_border_width(password_label_wrapper, 0, 0);
|
||||
lv_obj_set_style_pad_left(password_label_wrapper, 0, 0);
|
||||
lv_obj_set_style_pad_right(password_label_wrapper, 0, 0);
|
||||
|
||||
lv_obj_t* password_label = lv_label_create(password_label_wrapper);
|
||||
lv_label_set_text(password_label, "Password:");
|
||||
|
||||
password_textarea = lv_textarea_create(password_wrapper);
|
||||
lv_textarea_set_one_line(password_textarea, true);
|
||||
lv_textarea_set_password_mode(password_textarea, true);
|
||||
lv_obj_align(password_textarea, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_obj_set_width(password_textarea, LV_PCT(50));
|
||||
|
||||
password_error = lv_label_create(wrapper);
|
||||
lv_obj_set_style_text_color(password_error, lv_color_make(255, 50, 50), 0);
|
||||
lv_obj_add_flag(password_error, LV_OBJ_FLAG_HIDDEN);
|
||||
|
||||
// Connection error
|
||||
connection_error = lv_label_create(wrapper);
|
||||
lv_obj_set_style_text_color(connection_error, lv_color_make(255, 50, 50), 0);
|
||||
lv_obj_add_flag(connection_error, LV_OBJ_FLAG_HIDDEN);
|
||||
|
||||
// Bottom buttons
|
||||
createBottomButtons(wifiConnect, wrapper);
|
||||
|
||||
// Keyboard bindings
|
||||
service::gui::keyboardAddTextArea(ssid_textarea);
|
||||
service::gui::keyboardAddTextArea(password_textarea);
|
||||
|
||||
// Init from app parameters
|
||||
const Bundle& bundle = app.getParameters();
|
||||
std::string ssid;
|
||||
if (bundle.optString(WIFI_CONNECT_PARAM_SSID, ssid)) {
|
||||
lv_textarea_set_text(ssid_textarea, ssid.c_str());
|
||||
}
|
||||
|
||||
std::string password;
|
||||
if (bundle.optString(WIFI_CONNECT_PARAM_PASSWORD, password)) {
|
||||
lv_textarea_set_text(password_textarea, password.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void View::update(
|
||||
TT_UNUSED Bindings* bindings,
|
||||
State* state
|
||||
) {
|
||||
if (state->hasConnectionError()) {
|
||||
setLoading(false);
|
||||
resetErrors();
|
||||
lv_label_set_text(connection_error, "Connection failed");
|
||||
lv_obj_remove_flag(connection_error, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include "Bindings.h"
|
||||
#include "State.h"
|
||||
|
||||
#include "app/App.h"
|
||||
#include "lvgl.h"
|
||||
|
||||
namespace tt::app::wificonnect {
|
||||
|
||||
class WifiConnect;
|
||||
|
||||
class View {
|
||||
|
||||
public:
|
||||
|
||||
lv_obj_t* ssid_textarea = nullptr;
|
||||
lv_obj_t* ssid_error = nullptr;
|
||||
lv_obj_t* password_textarea = nullptr;
|
||||
lv_obj_t* password_error = nullptr;
|
||||
lv_obj_t* connect_button = nullptr;
|
||||
lv_obj_t* cancel_button = nullptr;
|
||||
lv_obj_t* remember_switch = nullptr;
|
||||
lv_obj_t* connecting_spinner = nullptr;
|
||||
lv_obj_t* connection_error = nullptr;
|
||||
lv_group_t* group = nullptr;
|
||||
|
||||
void init(App& app, WifiConnect* wifiConnect, lv_obj_t* parent);
|
||||
void update(Bindings* bindings, State* state);
|
||||
|
||||
void createBottomButtons(WifiConnect* wifi, lv_obj_t* parent);
|
||||
void setLoading(bool loading);
|
||||
void resetErrors();
|
||||
};
|
||||
|
||||
|
||||
} // namespace
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
#include "app/App.h"
|
||||
#include "TactilityCore.h"
|
||||
#include "WifiConnectStateUpdating.h"
|
||||
#include "service/loader/Loader.h"
|
||||
#include "service/wifi/Wifi.h"
|
||||
#include "lvgl/LvglSync.h"
|
||||
@@ -11,124 +10,106 @@ namespace tt::app::wificonnect {
|
||||
|
||||
#define TAG "wifi_connect"
|
||||
|
||||
// Forward declarations
|
||||
static void event_callback(const void* message, void* context);
|
||||
|
||||
static void on_connect(const service::wifi::settings::WifiApSettings* ap_settings, bool remember, TT_UNUSED void* parameter) {
|
||||
auto* wifi = static_cast<WifiConnect*>(parameter);
|
||||
state_set_ap_settings(wifi, ap_settings);
|
||||
state_set_connecting(wifi, true);
|
||||
service::wifi::connect(ap_settings, remember);
|
||||
}
|
||||
|
||||
static WifiConnect* wifi_connect_alloc() {
|
||||
auto* wifi = static_cast<WifiConnect*>(malloc(sizeof(WifiConnect)));
|
||||
|
||||
PubSub* wifi_pubsub = service::wifi::getPubsub();
|
||||
wifi->wifi_subscription = tt_pubsub_subscribe(wifi_pubsub, &event_callback, wifi);
|
||||
wifi->mutex = tt_mutex_alloc(MutexTypeNormal);
|
||||
wifi->state = (WifiConnectState) {
|
||||
.settings = {
|
||||
.ssid = { 0 },
|
||||
.password = { 0 },
|
||||
.auto_connect = false,
|
||||
},
|
||||
.connection_error = false,
|
||||
.is_connecting = false
|
||||
};
|
||||
wifi->bindings = (WifiConnectBindings) {
|
||||
.on_connect_ssid = &on_connect,
|
||||
.on_connect_ssid_context = wifi,
|
||||
};
|
||||
wifi->view_enabled = false;
|
||||
|
||||
return wifi;
|
||||
}
|
||||
|
||||
static void wifi_connect_free(WifiConnect* wifi) {
|
||||
PubSub* wifi_pubsub = service::wifi::getPubsub();
|
||||
tt_pubsub_unsubscribe(wifi_pubsub, wifi->wifi_subscription);
|
||||
tt_mutex_free(wifi->mutex);
|
||||
|
||||
free(wifi);
|
||||
}
|
||||
|
||||
void lock(WifiConnect* wifi) {
|
||||
tt_assert(wifi);
|
||||
tt_assert(wifi->mutex);
|
||||
tt_mutex_acquire(wifi->mutex, TtWaitForever);
|
||||
}
|
||||
|
||||
void unlock(WifiConnect* wifi) {
|
||||
tt_assert(wifi);
|
||||
tt_assert(wifi->mutex);
|
||||
tt_mutex_release(wifi->mutex);
|
||||
}
|
||||
|
||||
void request_view_update(WifiConnect* wifi) {
|
||||
lock(wifi);
|
||||
if (wifi->view_enabled) {
|
||||
if (lvgl::lock(1000)) {
|
||||
view_update(&wifi->view, &wifi->bindings, &wifi->state);
|
||||
lvgl::unlock();
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Failed to lock lvgl");
|
||||
}
|
||||
}
|
||||
unlock(wifi);
|
||||
}
|
||||
|
||||
static void event_callback(const void* message, void* context) {
|
||||
static void eventCallback(const void* message, void* context) {
|
||||
auto* event = static_cast<const service::wifi::WifiEvent*>(message);
|
||||
auto* wifi = static_cast<WifiConnect*>(context);
|
||||
State& state = wifi->getState();
|
||||
switch (event->type) {
|
||||
case service::wifi::WifiEventTypeConnectionFailed:
|
||||
if (wifi->state.is_connecting) {
|
||||
state_set_connecting(wifi, false);
|
||||
state_set_radio_error(wifi, true);
|
||||
request_view_update(wifi);
|
||||
if (state.isConnecting()) {
|
||||
state.setConnecting(false);
|
||||
state.setConnectionError(true);
|
||||
wifi->requestViewUpdate();
|
||||
}
|
||||
break;
|
||||
case service::wifi::WifiEventTypeConnectionSuccess:
|
||||
if (wifi->state.is_connecting) {
|
||||
state_set_connecting(wifi, false);
|
||||
if (wifi->getState().isConnecting()) {
|
||||
state.setConnecting(false);
|
||||
service::loader::stopApp();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
request_view_update(wifi);
|
||||
wifi->requestViewUpdate();
|
||||
}
|
||||
|
||||
static void app_show(App& app, lv_obj_t* parent) {
|
||||
auto* wifi = static_cast<WifiConnect*>(app.getData());
|
||||
|
||||
lock(wifi);
|
||||
wifi->view_enabled = true;
|
||||
view_create(app, wifi, parent);
|
||||
view_update(&wifi->view, &wifi->bindings, &wifi->state);
|
||||
unlock(wifi);
|
||||
static void onConnect(const service::wifi::settings::WifiApSettings* ap_settings, bool remember, TT_UNUSED void* parameter) {
|
||||
auto* wifi = static_cast<WifiConnect*>(parameter);
|
||||
wifi->getState().setApSettings(ap_settings);
|
||||
wifi->getState().setConnecting(true);
|
||||
service::wifi::connect(ap_settings, remember);
|
||||
}
|
||||
|
||||
static void app_hide(App& app) {
|
||||
auto* wifi = static_cast<WifiConnect*>(app.getData());
|
||||
WifiConnect::WifiConnect() {
|
||||
PubSub* wifi_pubsub = service::wifi::getPubsub();
|
||||
wifiSubscription = tt_pubsub_subscribe(wifi_pubsub, &eventCallback, this);
|
||||
bindings = (Bindings) {
|
||||
.onConnectSsid = onConnect,
|
||||
.onConnectSsidContext = this,
|
||||
};
|
||||
}
|
||||
|
||||
WifiConnect::~WifiConnect() {
|
||||
PubSub* pubsub = service::wifi::getPubsub();
|
||||
tt_pubsub_unsubscribe(pubsub, wifiSubscription);
|
||||
}
|
||||
|
||||
void WifiConnect::lock() {
|
||||
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
}
|
||||
|
||||
void WifiConnect::unlock() {
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
}
|
||||
|
||||
void WifiConnect::requestViewUpdate() {
|
||||
lock();
|
||||
if (view_enabled) {
|
||||
if (lvgl::lock(1000)) {
|
||||
view.update(&bindings, &state);
|
||||
lvgl::unlock();
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Failed to lock lvgl");
|
||||
}
|
||||
}
|
||||
unlock();
|
||||
}
|
||||
|
||||
void WifiConnect::onShow(App& app, lv_obj_t* parent) {
|
||||
lock();
|
||||
view_enabled = true;
|
||||
view.init(app, this, parent);
|
||||
view.update(&bindings, &state);
|
||||
unlock();
|
||||
}
|
||||
|
||||
void WifiConnect::onHide(App& app) {
|
||||
// No need to lock view, as this is called from within Gui's LVGL context
|
||||
view_destroy(&wifi->view);
|
||||
lock(wifi);
|
||||
wifi->view_enabled = false;
|
||||
unlock(wifi);
|
||||
lock();
|
||||
view_enabled = false;
|
||||
unlock();
|
||||
}
|
||||
|
||||
static void app_start(App& app) {
|
||||
auto* wifi_connect = wifi_connect_alloc();
|
||||
static void onShow(App& app, lv_obj_t* parent) {
|
||||
auto* wifi = static_cast<WifiConnect*>(app.getData());
|
||||
wifi->onShow(app, parent);
|
||||
}
|
||||
|
||||
static void onHide(App& app) {
|
||||
auto* wifi = static_cast<WifiConnect*>(app.getData());
|
||||
wifi->onHide(app);
|
||||
}
|
||||
|
||||
static void onStart(App& app) {
|
||||
auto* wifi_connect = new WifiConnect();
|
||||
app.setData(wifi_connect);
|
||||
}
|
||||
|
||||
static void app_stop(App& app) {
|
||||
auto* wifi = static_cast<WifiConnect*>(app.getData());
|
||||
tt_assert(wifi != nullptr);
|
||||
wifi_connect_free(wifi);
|
||||
static void onStop(App& app) {
|
||||
auto* wifi_connect = static_cast<WifiConnect*>(app.getData());
|
||||
tt_assert(wifi_connect != nullptr);
|
||||
delete wifi_connect;
|
||||
}
|
||||
|
||||
extern const Manifest manifest = {
|
||||
@@ -136,10 +117,10 @@ extern const Manifest manifest = {
|
||||
.name = "Wi-Fi Connect",
|
||||
.icon = LV_SYMBOL_WIFI,
|
||||
.type = TypeSettings,
|
||||
.onStart = &app_start,
|
||||
.onStop = &app_stop,
|
||||
.onShow = &app_show,
|
||||
.onHide = &app_hide
|
||||
.onStart = &onStart,
|
||||
.onStop = &onStop,
|
||||
.onShow = &onShow,
|
||||
.onHide = &onHide
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -1,21 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include "Bindings.h"
|
||||
#include "State.h"
|
||||
#include "View.h"
|
||||
|
||||
#include "Mutex.h"
|
||||
#include "WifiConnectBindings.h"
|
||||
#include "WifiConnectState.h"
|
||||
#include "WifiConnectView.h"
|
||||
#include "service/wifi/Wifi.h"
|
||||
|
||||
namespace tt::app::wificonnect {
|
||||
|
||||
typedef struct {
|
||||
PubSubSubscription* wifi_subscription;
|
||||
Mutex* mutex;
|
||||
WifiConnectState state;
|
||||
WifiConnectView view;
|
||||
bool view_enabled;
|
||||
WifiConnectBindings bindings;
|
||||
} WifiConnect;
|
||||
class WifiConnect {
|
||||
PubSubSubscription* wifiSubscription;
|
||||
Mutex mutex;
|
||||
State state;
|
||||
View view;
|
||||
bool view_enabled = false;
|
||||
Bindings bindings = {
|
||||
.onConnectSsid = nullptr,
|
||||
.onConnectSsidContext = nullptr
|
||||
};
|
||||
|
||||
public:
|
||||
|
||||
WifiConnect();
|
||||
~WifiConnect();
|
||||
|
||||
void lock();
|
||||
void unlock();
|
||||
|
||||
void onShow(App& app, lv_obj_t* parent);
|
||||
void onHide(App& app);
|
||||
|
||||
State& getState() { return state; }
|
||||
Bindings& getBindings() { return bindings; }
|
||||
View& getView() { return view; }
|
||||
|
||||
|
||||
void requestViewUpdate();
|
||||
};
|
||||
|
||||
void lock(WifiConnect* wifi);
|
||||
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "service/wifi/Wifi.h"
|
||||
#include "service/wifi/WifiSettings.h"
|
||||
|
||||
namespace tt::app::wificonnect {
|
||||
|
||||
/**
|
||||
* View's state
|
||||
*/
|
||||
typedef struct {
|
||||
service::wifi::settings::WifiApSettings settings;
|
||||
bool connection_error;
|
||||
bool is_connecting;
|
||||
} WifiConnectState;
|
||||
|
||||
} // namespace
|
||||
@@ -1,23 +0,0 @@
|
||||
#include "WifiConnectStateUpdating.h"
|
||||
|
||||
namespace tt::app::wificonnect {
|
||||
|
||||
void state_set_radio_error(WifiConnect* wifi, bool error) {
|
||||
lock(wifi);
|
||||
wifi->state.connection_error = error;
|
||||
unlock(wifi);
|
||||
}
|
||||
|
||||
void state_set_ap_settings(WifiConnect* wifi, const service::wifi::settings::WifiApSettings* settings) {
|
||||
lock(wifi);
|
||||
memcpy(&(wifi->state.settings), settings, sizeof(service::wifi::settings::WifiApSettings));
|
||||
unlock(wifi);
|
||||
}
|
||||
|
||||
void state_set_connecting(WifiConnect* wifi, bool is_connecting) {
|
||||
lock(wifi);
|
||||
wifi->state.is_connecting = is_connecting;
|
||||
unlock(wifi);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -1,11 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "WifiConnect.h"
|
||||
|
||||
namespace tt::app::wificonnect {
|
||||
|
||||
void state_set_radio_error(WifiConnect* wifi, bool error);
|
||||
void state_set_ap_settings(WifiConnect* wifi, const service::wifi::settings::WifiApSettings* settings);
|
||||
void state_set_connecting(WifiConnect* wifi, bool is_connecting);
|
||||
|
||||
} // namespace
|
||||
@@ -1,227 +0,0 @@
|
||||
#include "WifiConnectView.h"
|
||||
|
||||
#include "Log.h"
|
||||
#include "WifiConnect.h"
|
||||
#include "WifiConnectBundle.h"
|
||||
#include "WifiConnectState.h"
|
||||
#include "WifiConnectStateUpdating.h"
|
||||
#include "lvgl.h"
|
||||
#include "service/gui/Gui.h"
|
||||
#include "service/wifi/WifiSettings.h"
|
||||
#include "lvgl/Style.h"
|
||||
#include "lvgl/Toolbar.h"
|
||||
|
||||
namespace tt::app::wificonnect {
|
||||
|
||||
#define TAG "wifi_connect"
|
||||
|
||||
static void view_set_loading(WifiConnectView* view, bool loading);
|
||||
|
||||
static void reset_errors(WifiConnectView* view) {
|
||||
lv_obj_add_flag(view->password_error, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag(view->ssid_error, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag(view->connection_error, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
|
||||
static void on_connect(lv_event_t* event) {
|
||||
WifiConnect* wifi = (WifiConnect*)lv_event_get_user_data(event);
|
||||
WifiConnectView* view = &wifi->view;
|
||||
|
||||
state_set_radio_error(wifi, false);
|
||||
reset_errors(view);
|
||||
|
||||
const char* ssid = lv_textarea_get_text(view->ssid_textarea);
|
||||
size_t ssid_len = strlen(ssid);
|
||||
if (ssid_len > TT_WIFI_SSID_LIMIT) {
|
||||
TT_LOG_E(TAG, "SSID too long");
|
||||
lv_label_set_text(view->ssid_error, "SSID too long");
|
||||
lv_obj_remove_flag(view->ssid_error, LV_OBJ_FLAG_HIDDEN);
|
||||
return;
|
||||
}
|
||||
|
||||
const char* password = lv_textarea_get_text(view->password_textarea);
|
||||
size_t password_len = strlen(password);
|
||||
if (password_len > TT_WIFI_CREDENTIALS_PASSWORD_LIMIT) {
|
||||
TT_LOG_E(TAG, "Password too long");
|
||||
lv_label_set_text(view->password_error, "Password too long");
|
||||
lv_obj_remove_flag(view->password_error, LV_OBJ_FLAG_HIDDEN);
|
||||
return;
|
||||
}
|
||||
|
||||
bool store = lv_obj_get_state(view->remember_switch) & LV_STATE_CHECKED;
|
||||
|
||||
view_set_loading(view, true);
|
||||
|
||||
service::wifi::settings::WifiApSettings settings;
|
||||
strcpy((char*)settings.password, password);
|
||||
strcpy((char*)settings.ssid, ssid);
|
||||
settings.auto_connect = TT_WIFI_AUTO_CONNECT; // No UI yet, so use global setting:w
|
||||
|
||||
WifiConnectBindings* bindings = &wifi->bindings;
|
||||
bindings->on_connect_ssid(
|
||||
&settings,
|
||||
store,
|
||||
bindings->on_connect_ssid_context
|
||||
);
|
||||
}
|
||||
|
||||
static void view_set_loading(WifiConnectView* view, bool loading) {
|
||||
if (loading) {
|
||||
lv_obj_add_flag(view->connect_button, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_remove_flag(view->connecting_spinner, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_state(view->password_textarea, LV_STATE_DISABLED);
|
||||
lv_obj_add_state(view->ssid_textarea, LV_STATE_DISABLED);
|
||||
lv_obj_add_state(view->remember_switch, LV_STATE_DISABLED);
|
||||
} else {
|
||||
lv_obj_remove_flag(view->connect_button, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag(view->connecting_spinner, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_remove_state(view->password_textarea, LV_STATE_DISABLED);
|
||||
lv_obj_remove_state(view->ssid_textarea, LV_STATE_DISABLED);
|
||||
lv_obj_remove_state(view->remember_switch, LV_STATE_DISABLED);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void view_create_bottom_buttons(WifiConnect* wifi, lv_obj_t* parent) {
|
||||
WifiConnectView* view = &wifi->view;
|
||||
|
||||
lv_obj_t* button_container = lv_obj_create(parent);
|
||||
lv_obj_set_width(button_container, LV_PCT(100));
|
||||
lv_obj_set_height(button_container, LV_SIZE_CONTENT);
|
||||
lvgl::obj_set_style_no_padding(button_container);
|
||||
lv_obj_set_style_border_width(button_container, 0, 0);
|
||||
|
||||
view->remember_switch = lv_switch_create(button_container);
|
||||
lv_obj_add_state(view->remember_switch, LV_STATE_CHECKED);
|
||||
lv_obj_align(view->remember_switch, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
|
||||
lv_obj_t* remember_label = lv_label_create(button_container);
|
||||
lv_label_set_text(remember_label, "Remember");
|
||||
lv_obj_align(remember_label, LV_ALIGN_CENTER, 0, 0);
|
||||
lv_obj_align_to(remember_label, view->remember_switch, LV_ALIGN_OUT_RIGHT_MID, 4, 0);
|
||||
|
||||
view->connecting_spinner = lv_spinner_create(button_container);
|
||||
lv_obj_set_size(view->connecting_spinner, 32, 32);
|
||||
lv_obj_align(view->connecting_spinner, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_obj_add_flag(view->connecting_spinner, LV_OBJ_FLAG_HIDDEN);
|
||||
|
||||
view->connect_button = lv_btn_create(button_container);
|
||||
lv_obj_t* connect_label = lv_label_create(view->connect_button);
|
||||
lv_label_set_text(connect_label, "Connect");
|
||||
lv_obj_align(view->connect_button, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_obj_add_event_cb(view->connect_button, &on_connect, LV_EVENT_CLICKED, wifi);
|
||||
}
|
||||
|
||||
// TODO: Standardize dialogs
|
||||
void view_create(const App& app, void* wifi, lv_obj_t* parent) {
|
||||
WifiConnect* wifi_connect = (WifiConnect*)wifi;
|
||||
WifiConnectView* view = &wifi_connect->view;
|
||||
|
||||
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
||||
lvgl::toolbar_create(parent, app);
|
||||
|
||||
lv_obj_t* wrapper = lv_obj_create(parent);
|
||||
lv_obj_set_width(wrapper, LV_PCT(100));
|
||||
lv_obj_set_flex_grow(wrapper, 1);
|
||||
lv_obj_set_flex_flow(wrapper, LV_FLEX_FLOW_COLUMN);
|
||||
|
||||
// SSID
|
||||
|
||||
lv_obj_t* ssid_wrapper = lv_obj_create(wrapper);
|
||||
lv_obj_set_width(ssid_wrapper, LV_PCT(100));
|
||||
lv_obj_set_height(ssid_wrapper, LV_SIZE_CONTENT);
|
||||
lvgl::obj_set_style_no_padding(ssid_wrapper);
|
||||
lv_obj_set_style_border_width(ssid_wrapper, 0, 0);
|
||||
|
||||
lv_obj_t* ssid_label_wrapper = lv_obj_create(ssid_wrapper);
|
||||
lv_obj_set_width(ssid_label_wrapper, LV_PCT(50));
|
||||
lv_obj_set_height(ssid_label_wrapper, LV_SIZE_CONTENT);
|
||||
lv_obj_align(ssid_label_wrapper, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
lv_obj_set_style_border_width(ssid_label_wrapper, 0, 0);
|
||||
lv_obj_set_style_pad_left(ssid_label_wrapper, 0, 0);
|
||||
lv_obj_set_style_pad_right(ssid_label_wrapper, 0, 0);
|
||||
|
||||
lv_obj_t* ssid_label = lv_label_create(ssid_label_wrapper);
|
||||
lv_label_set_text(ssid_label, "Network:");
|
||||
|
||||
view->ssid_textarea = lv_textarea_create(ssid_wrapper);
|
||||
lv_textarea_set_one_line(view->ssid_textarea, true);
|
||||
lv_obj_align(view->ssid_textarea, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_obj_set_width(view->ssid_textarea, LV_PCT(50));
|
||||
|
||||
view->ssid_error = lv_label_create(wrapper);
|
||||
lv_obj_set_style_text_color(view->ssid_error, lv_color_make(255, 50, 50), 0);
|
||||
lv_obj_add_flag(view->ssid_error, LV_OBJ_FLAG_HIDDEN);
|
||||
|
||||
// Password
|
||||
|
||||
lv_obj_t* password_wrapper = lv_obj_create(wrapper);
|
||||
lv_obj_set_width(password_wrapper, LV_PCT(100));
|
||||
lv_obj_set_height(password_wrapper, LV_SIZE_CONTENT);
|
||||
lvgl::obj_set_style_no_padding(password_wrapper);
|
||||
lv_obj_set_style_border_width(password_wrapper, 0, 0);
|
||||
|
||||
lv_obj_t* password_label_wrapper = lv_obj_create(password_wrapper);
|
||||
lv_obj_set_width(password_label_wrapper, LV_PCT(50));
|
||||
lv_obj_set_height(password_label_wrapper, LV_SIZE_CONTENT);
|
||||
lv_obj_align_to(password_label_wrapper, password_wrapper, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
lv_obj_set_style_border_width(password_label_wrapper, 0, 0);
|
||||
lv_obj_set_style_pad_left(password_label_wrapper, 0, 0);
|
||||
lv_obj_set_style_pad_right(password_label_wrapper, 0, 0);
|
||||
|
||||
lv_obj_t* password_label = lv_label_create(password_label_wrapper);
|
||||
lv_label_set_text(password_label, "Password:");
|
||||
|
||||
view->password_textarea = lv_textarea_create(password_wrapper);
|
||||
lv_textarea_set_one_line(view->password_textarea, true);
|
||||
lv_textarea_set_password_mode(view->password_textarea, true);
|
||||
lv_obj_align(view->password_textarea, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
lv_obj_set_width(view->password_textarea, LV_PCT(50));
|
||||
|
||||
view->password_error = lv_label_create(wrapper);
|
||||
lv_obj_set_style_text_color(view->password_error, lv_color_make(255, 50, 50), 0);
|
||||
lv_obj_add_flag(view->password_error, LV_OBJ_FLAG_HIDDEN);
|
||||
|
||||
// Connection error
|
||||
view->connection_error = lv_label_create(wrapper);
|
||||
lv_obj_set_style_text_color(view->connection_error, lv_color_make(255, 50, 50), 0);
|
||||
lv_obj_add_flag(view->connection_error, LV_OBJ_FLAG_HIDDEN);
|
||||
|
||||
// Bottom buttons
|
||||
view_create_bottom_buttons(wifi_connect, wrapper);
|
||||
|
||||
// Keyboard bindings
|
||||
service::gui::keyboardAddTextArea(view->ssid_textarea);
|
||||
service::gui::keyboardAddTextArea(view->password_textarea);
|
||||
|
||||
// Init from app parameters
|
||||
const Bundle& bundle = app.getParameters();
|
||||
std::string ssid;
|
||||
if (bundle.optString(WIFI_CONNECT_PARAM_SSID, ssid)) {
|
||||
lv_textarea_set_text(view->ssid_textarea, ssid.c_str());
|
||||
}
|
||||
|
||||
std::string password;
|
||||
if (bundle.optString(WIFI_CONNECT_PARAM_PASSWORD, password)) {
|
||||
lv_textarea_set_text(view->password_textarea, password.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void view_destroy(TT_UNUSED WifiConnectView* view) {
|
||||
// NO-OP
|
||||
}
|
||||
|
||||
void view_update(
|
||||
WifiConnectView* view,
|
||||
TT_UNUSED WifiConnectBindings* bindings,
|
||||
WifiConnectState* state
|
||||
) {
|
||||
if (state->connection_error) {
|
||||
view_set_loading(view, false);
|
||||
reset_errors(view);
|
||||
lv_label_set_text(view->connection_error, "Connection failed");
|
||||
lv_obj_remove_flag(view->connection_error, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -1,27 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "app/App.h"
|
||||
#include "WifiConnectBindings.h"
|
||||
#include "WifiConnectState.h"
|
||||
#include "lvgl.h"
|
||||
|
||||
namespace tt::app::wificonnect {
|
||||
|
||||
typedef struct {
|
||||
lv_obj_t* ssid_textarea;
|
||||
lv_obj_t* ssid_error;
|
||||
lv_obj_t* password_textarea;
|
||||
lv_obj_t* password_error;
|
||||
lv_obj_t* connect_button;
|
||||
lv_obj_t* cancel_button;
|
||||
lv_obj_t* remember_switch;
|
||||
lv_obj_t* connecting_spinner;
|
||||
lv_obj_t* connection_error;
|
||||
lv_group_t* group;
|
||||
} WifiConnectView;
|
||||
|
||||
void view_create(const App& app, void* wifi, lv_obj_t* parent);
|
||||
void view_update(WifiConnectView* view, WifiConnectBindings* bindings, WifiConnectState* state);
|
||||
void view_destroy(WifiConnectView* view);
|
||||
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user