mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-02-19 23:15:05 +00:00
Compare commits
6 Commits
7e6b6d5463
...
1fe857e73a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1fe857e73a | ||
|
|
a7fe572d63 | ||
|
|
00fae61b14 | ||
|
|
f876f76c7e | ||
|
|
06f9051b52 | ||
|
|
c3ceda3072 |
19
Tactility/Include/Tactility/network/Url.h
Normal file
19
Tactility/Include/Tactility/network/Url.h
Normal file
@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
namespace tt::network {
|
||||
|
||||
/**
|
||||
* Parse a query from a URL
|
||||
* @param[in] query
|
||||
* @return a map with key-values
|
||||
*/
|
||||
std::map<std::string, std::string> parseUrlQuery(std::string query);
|
||||
|
||||
std::string urlEncode(const std::string& input);
|
||||
|
||||
std::string urlDecode(const std::string& input);
|
||||
|
||||
} // namespace
|
||||
@ -113,6 +113,11 @@ void setScanRecords(uint16_t records);
|
||||
*/
|
||||
void setEnabled(bool enabled);
|
||||
|
||||
/**
|
||||
* @return the IPv4 address or empty string
|
||||
*/
|
||||
std::string getIp();
|
||||
|
||||
/**
|
||||
* @brief Connect to a network. Disconnects any existing connection.
|
||||
* Returns immediately but runs in the background. Results are through pubsub.
|
||||
|
||||
11
Tactility/Private/Tactility/app/development/Development.h
Normal file
11
Tactility/Private/Tactility/app/development/Development.h
Normal file
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
namespace tt::app::development {
|
||||
|
||||
void start();
|
||||
|
||||
}
|
||||
|
||||
#endif // ESP_PLATFORM
|
||||
@ -11,7 +11,7 @@
|
||||
|
||||
namespace tt::service::development {
|
||||
|
||||
class Development final : public Service {
|
||||
class DevelopmentService final : public Service {
|
||||
|
||||
Mutex mutex = Mutex(Mutex::Type::Recursive);
|
||||
httpd_handle_t server = nullptr;
|
||||
@ -36,7 +36,7 @@ class Development final : public Service {
|
||||
|
||||
httpd_uri_t appInstallEndpoint = {
|
||||
.uri = "/app/install",
|
||||
.method = HTTP_POST,
|
||||
.method = HTTP_PUT,
|
||||
.handler = handleAppInstall,
|
||||
.user_ctx = this
|
||||
};
|
||||
@ -62,16 +62,35 @@ public:
|
||||
|
||||
// region Internal API
|
||||
|
||||
/**
|
||||
* Enabling the service means that the user is willing to start the web server.
|
||||
* @return true when the service is enabled
|
||||
*/
|
||||
bool isEnabled() const;
|
||||
|
||||
/**
|
||||
* Enabling the service means that the user is willing to start the web server.
|
||||
* @param[in] enabled
|
||||
*/
|
||||
void setEnabled(bool enabled);
|
||||
|
||||
bool isEnabled() const;
|
||||
/**
|
||||
* @return true if the service will enable itself when it is started (e.g. on boot, or manual start)
|
||||
*/
|
||||
bool isEnabledOnStart() const;
|
||||
|
||||
/**
|
||||
* Set whether the service should auto-enable when it is started.
|
||||
* @param enabled
|
||||
*/
|
||||
void setEnabledOnStart(bool enabled);
|
||||
|
||||
bool isStarted() const;
|
||||
|
||||
// region Internal API
|
||||
};
|
||||
|
||||
std::shared_ptr<Development> findService();
|
||||
std::shared_ptr<DevelopmentService> findService();
|
||||
|
||||
}
|
||||
|
||||
@ -33,9 +33,10 @@ namespace app {
|
||||
namespace addgps { extern const AppManifest manifest; }
|
||||
namespace alertdialog { extern const AppManifest manifest; }
|
||||
namespace applist { extern const AppManifest manifest; }
|
||||
namespace boot { extern const AppManifest manifest; }
|
||||
namespace calculator { extern const AppManifest manifest; }
|
||||
namespace chat { extern const AppManifest manifest; }
|
||||
namespace boot { extern const AppManifest manifest; }
|
||||
namespace development { extern const AppManifest manifest; }
|
||||
namespace display { extern const AppManifest manifest; }
|
||||
namespace filebrowser { extern const AppManifest manifest; }
|
||||
namespace fileselection { extern const AppManifest manifest; }
|
||||
@ -73,6 +74,7 @@ namespace app {
|
||||
|
||||
// endregion
|
||||
|
||||
// List of all apps excluding Boot app (as Boot app calls this function indirectly)
|
||||
static void registerSystemApps() {
|
||||
addApp(app::addgps::manifest);
|
||||
addApp(app::alertdialog::manifest);
|
||||
@ -109,6 +111,7 @@ static void registerSystemApps() {
|
||||
#ifdef ESP_PLATFORM
|
||||
addApp(app::chat::manifest);
|
||||
addApp(app::crashdiagnostics::manifest);
|
||||
addApp(app::development::manifest);
|
||||
#endif
|
||||
|
||||
if (getConfiguration()->hardware->power != nullptr) {
|
||||
|
||||
160
Tactility/Source/app/development/Development.cpp
Normal file
160
Tactility/Source/app/development/Development.cpp
Normal file
@ -0,0 +1,160 @@
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#include "Tactility/app/AppManifest.h"
|
||||
#include "Tactility/lvgl/Style.h"
|
||||
#include "Tactility/lvgl/Toolbar.h"
|
||||
#include "Tactility/service/development/DevelopmentService.h"
|
||||
|
||||
#include <Tactility/Timer.h>
|
||||
#include <Tactility/service/wifi/Wifi.h>
|
||||
#include <cstring>
|
||||
#include <lvgl.h>
|
||||
#include <Tactility/lvgl/LvglSync.h>
|
||||
#include <Tactility/service/loader/Loader.h>
|
||||
#include <Tactility/service/wifi/Wifi.h>
|
||||
|
||||
namespace tt::app::development {
|
||||
|
||||
constexpr const char* TAG = "AddGps";
|
||||
|
||||
class DevelopmentApp final : public App {
|
||||
|
||||
lv_obj_t* enableSwitch = nullptr;
|
||||
lv_obj_t* enableOnBootSwitch = nullptr;
|
||||
lv_obj_t* statusLabel = nullptr;
|
||||
std::shared_ptr<service::development::DevelopmentService> service;
|
||||
|
||||
Timer timer = Timer(Timer::Type::Periodic, [this] {
|
||||
auto lock = lvgl::getSyncLock()->asScopedLock();
|
||||
if (lock.lock(lvgl::defaultLockTime)) {
|
||||
updateViewState();
|
||||
}
|
||||
});
|
||||
|
||||
static void onEnableSwitchChanged(lv_event_t* event) {
|
||||
lv_event_code_t code = lv_event_get_code(event);
|
||||
auto* widget = static_cast<lv_obj_t*>(lv_event_get_target(event));
|
||||
if (code == LV_EVENT_VALUE_CHANGED) {
|
||||
bool is_on = lv_obj_has_state(widget, LV_STATE_CHECKED);
|
||||
auto* app = static_cast<DevelopmentApp*>(lv_event_get_user_data(event));
|
||||
bool is_changed = is_on != app->service->isEnabled();
|
||||
if (is_changed) {
|
||||
app->service->setEnabled(is_on);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void onEnableOnBootSwitchChanged(lv_event_t* event) {
|
||||
lv_event_code_t code = lv_event_get_code(event);
|
||||
auto* widget = static_cast<lv_obj_t*>(lv_event_get_target(event));
|
||||
if (code == LV_EVENT_VALUE_CHANGED) {
|
||||
bool is_on = lv_obj_has_state(widget, LV_STATE_CHECKED);
|
||||
auto* app = static_cast<DevelopmentApp*>(lv_event_get_user_data(event));
|
||||
bool is_changed = is_on != app->service->isEnabledOnStart();
|
||||
if (is_changed) {
|
||||
app->service->setEnabledOnStart(is_on);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void updateViewState() {
|
||||
if (!service->isEnabled()) {
|
||||
lv_label_set_text(statusLabel, "Service disabled");
|
||||
} else if (!service->isStarted()) {
|
||||
lv_label_set_text(statusLabel, "Waiting for connection...");
|
||||
} else { // enabled and started
|
||||
auto ip = service::wifi::getIp();
|
||||
if (ip.empty()) {
|
||||
lv_label_set_text(statusLabel, "Waiting for IP...");
|
||||
} else {
|
||||
std::string status = std::string("Available at ") + ip;
|
||||
lv_label_set_text(statusLabel, status.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
void onCreate(AppContext& appContext) override {
|
||||
service = service::development::findService();
|
||||
if (service == nullptr) {
|
||||
TT_LOG_E(TAG, "Service not found");
|
||||
service::loader::stopApp();
|
||||
}
|
||||
}
|
||||
|
||||
void onShow(AppContext& app, lv_obj_t* parent) override {
|
||||
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
||||
|
||||
// Toolbar
|
||||
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_t* toolbar = lvgl::toolbar_create(parent, app);
|
||||
|
||||
enableSwitch = lvgl::toolbar_add_switch_action(toolbar);
|
||||
lv_obj_add_event_cb(enableSwitch, onEnableSwitchChanged, LV_EVENT_VALUE_CHANGED, this);
|
||||
|
||||
if (service->isEnabled()) {
|
||||
lv_obj_add_state(enableSwitch, LV_STATE_CHECKED);
|
||||
} else {
|
||||
lv_obj_remove_state(enableSwitch, LV_STATE_CHECKED);
|
||||
}
|
||||
|
||||
// Wrappers
|
||||
|
||||
lv_obj_t* secondary_flex = lv_obj_create(parent);
|
||||
lv_obj_set_width(secondary_flex, LV_PCT(100));
|
||||
lv_obj_set_flex_grow(secondary_flex, 1);
|
||||
lv_obj_set_flex_flow(secondary_flex, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_style_border_width(secondary_flex, 0, 0);
|
||||
lv_obj_set_style_pad_all(secondary_flex, 0, 0);
|
||||
lv_obj_set_style_pad_gap(secondary_flex, 0, 0);
|
||||
lvgl::obj_set_style_bg_invisible(secondary_flex);
|
||||
|
||||
// align() methods don't work on flex, so we need this extra wrapper
|
||||
lv_obj_t* wrapper = lv_obj_create(secondary_flex);
|
||||
lv_obj_set_size(wrapper, LV_PCT(100), LV_SIZE_CONTENT);
|
||||
lvgl::obj_set_style_bg_invisible(wrapper);
|
||||
lv_obj_set_style_border_width(wrapper, 0, 0);
|
||||
|
||||
// Enable on boot
|
||||
|
||||
lv_obj_t* enable_label = lv_label_create(wrapper);
|
||||
lv_label_set_text(enable_label, "Enable on boot");
|
||||
lv_obj_align(enable_label, LV_ALIGN_TOP_LEFT, 0, 6);
|
||||
|
||||
enableOnBootSwitch = lv_switch_create(wrapper);
|
||||
lv_obj_add_event_cb(enableOnBootSwitch, onEnableOnBootSwitchChanged, LV_EVENT_VALUE_CHANGED, this);
|
||||
lv_obj_align(enableOnBootSwitch, LV_ALIGN_TOP_RIGHT, 0, 0);
|
||||
if (service->isEnabledOnStart()) {
|
||||
lv_obj_add_state(enableOnBootSwitch, LV_STATE_CHECKED);
|
||||
} else {
|
||||
lv_obj_remove_state(enableOnBootSwitch, LV_STATE_CHECKED);
|
||||
}
|
||||
|
||||
statusLabel = lv_label_create(wrapper);
|
||||
lv_obj_align(statusLabel, LV_ALIGN_TOP_LEFT, 0, 50);
|
||||
|
||||
updateViewState();
|
||||
|
||||
timer.start(1000);
|
||||
}
|
||||
|
||||
void onHide(AppContext& appContext) override {
|
||||
timer.stop();
|
||||
}
|
||||
};
|
||||
|
||||
extern const AppManifest manifest = {
|
||||
.id = "Development",
|
||||
.name = "Development",
|
||||
.type = Type::Settings,
|
||||
.createApp = create<DevelopmentApp>
|
||||
};
|
||||
|
||||
void start() {
|
||||
app::start(manifest.id);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // ESP_PLATFORM
|
||||
82
Tactility/Source/network/Url.cpp
Normal file
82
Tactility/Source/network/Url.cpp
Normal file
@ -0,0 +1,82 @@
|
||||
#include "Tactility/network/Url.h"
|
||||
|
||||
#include <Tactility/Log.h>
|
||||
|
||||
namespace tt::network {
|
||||
|
||||
std::map<std::string, std::string> parseUrlQuery(std::string query) {
|
||||
std::map<std::string, std::string> result;
|
||||
|
||||
if (query.empty()) {
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t current_index = query[0] == '?' ? 1U : 0U;
|
||||
auto equals_index = query.find_first_of('=', current_index);
|
||||
while (equals_index != std::string::npos) {
|
||||
auto index_boundary = query.find_first_of('&', equals_index + 1);
|
||||
if (index_boundary == std::string::npos) {
|
||||
index_boundary = query.size();
|
||||
}
|
||||
auto key = query.substr(current_index, (equals_index - current_index));
|
||||
auto decodedKey = urlDecode(key);
|
||||
auto value = query.substr(equals_index + 1, (index_boundary - equals_index - 1));
|
||||
auto decodedValue = urlDecode(value);
|
||||
|
||||
result[decodedKey] = decodedValue;
|
||||
|
||||
// Find next token
|
||||
current_index = index_boundary + 1;
|
||||
equals_index = query.find_first_of('=', current_index);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Adapted from https://stackoverflow.com/a/29962178/3848666
|
||||
std::string urlEncode(const std::string& input) {
|
||||
std::string result = "";
|
||||
const char* characters = input.c_str();
|
||||
char hex_buffer[10];
|
||||
size_t input_length = input.length();
|
||||
|
||||
for (size_t i = 0;i < input_length;i++) {
|
||||
unsigned char c = characters[i];
|
||||
// uncomment this if you want to encode spaces with +
|
||||
if (c==' ') {
|
||||
result += '+';
|
||||
} else if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
|
||||
result += c;
|
||||
} else {
|
||||
sprintf(hex_buffer, "%%%02X", c); //%% means '%' literal, %02X means at least two digits, paddable with a leading zero
|
||||
result += hex_buffer;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Adapted from https://stackoverflow.com/a/29962178/3848666
|
||||
std::string urlDecode(const std::string& input) {
|
||||
std::string result;
|
||||
size_t conversion_buffer, input_length = input.length();
|
||||
|
||||
for (size_t i = 0; i < input_length; i++) {
|
||||
if (input[i] != '%') {
|
||||
if (input[i] == '+') {
|
||||
result += ' ';
|
||||
} else {
|
||||
result += input[i];
|
||||
}
|
||||
} else {
|
||||
sscanf(input.substr(i + 1, 2).c_str(), "%x", &conversion_buffer);
|
||||
char c = static_cast<char>(conversion_buffer);
|
||||
result += c;
|
||||
i = i + 2;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@ -1,14 +1,19 @@
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#include "Tactility/service/development/Development.h"
|
||||
#include "Tactility/service/development/DevelopmentService.h"
|
||||
|
||||
#include "Tactility/network/Url.h"
|
||||
#include "Tactility/TactilityHeadless.h"
|
||||
#include "Tactility/service/ServiceManifest.h"
|
||||
#include "Tactility/service/ServiceRegistry.h"
|
||||
#include "Tactility/service/wifi/Wifi.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <esp_wifi.h>
|
||||
#include <sstream>
|
||||
#include <Tactility/Preferences.h>
|
||||
#include <Tactility/app/App.h>
|
||||
#include <Tactility/app/ManifestRegistry.h>
|
||||
|
||||
namespace tt::service::development {
|
||||
|
||||
@ -24,14 +29,14 @@ static char* rest_read_buffer(httpd_req_t* request) {
|
||||
if (contentLength >= 1024) {
|
||||
// Respond with 500 Internal Server Error
|
||||
httpd_resp_send_err(request, HTTPD_500_INTERNAL_SERVER_ERROR, "content too long");
|
||||
return NULL;
|
||||
return nullptr;
|
||||
}
|
||||
while (currentLength < contentLength) {
|
||||
received = httpd_req_recv(request, buffer + currentLength, contentLength);
|
||||
if (received <= 0) {
|
||||
// Respond with 500 Internal Server Error
|
||||
httpd_resp_send_err(request, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to post control value");
|
||||
return NULL;
|
||||
return nullptr;
|
||||
}
|
||||
currentLength += received;
|
||||
}
|
||||
@ -39,7 +44,7 @@ static char* rest_read_buffer(httpd_req_t* request) {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void Development::onStart(ServiceContext& service) {
|
||||
void DevelopmentService::onStart(ServiceContext& service) {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
@ -52,10 +57,10 @@ void Development::onStart(ServiceContext& service) {
|
||||
[this](kernel::SystemEvent) { onNetworkDisconnected(); }
|
||||
);
|
||||
|
||||
setEnabled(true);
|
||||
setEnabled(isEnabledOnStart());
|
||||
}
|
||||
|
||||
void Development::onStop(ServiceContext& service) {
|
||||
void DevelopmentService::onStop(ServiceContext& service) {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
@ -69,21 +74,41 @@ void Development::onStop(ServiceContext& service) {
|
||||
|
||||
// region Enable/disable
|
||||
|
||||
void Development::setEnabled(bool enabled) {
|
||||
void DevelopmentService::setEnabled(bool enabled) {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
this->enabled = enabled;
|
||||
|
||||
// We might already have an IP address, so in case we do, we start the server manually
|
||||
// Or we started the server while it shouldn't be
|
||||
if (enabled && !isStarted() && wifi::getRadioState() == wifi::RadioState::ConnectionActive) {
|
||||
startServer();
|
||||
} else if (!enabled && isStarted()) {
|
||||
stopServer();
|
||||
}
|
||||
}
|
||||
|
||||
bool Development::isEnabled() const {
|
||||
bool DevelopmentService::isEnabled() const {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
return enabled;
|
||||
}
|
||||
|
||||
bool DevelopmentService::isEnabledOnStart() const {
|
||||
Preferences preferences = Preferences(manifest.id.c_str());
|
||||
bool enabled_on_boot = false;
|
||||
preferences.optBool("enabledOnBoot", enabled_on_boot);
|
||||
return enabled_on_boot;
|
||||
}
|
||||
|
||||
void DevelopmentService::setEnabledOnStart(bool enabled) {
|
||||
Preferences preferences = Preferences(manifest.id.c_str());
|
||||
preferences.putBool("enabledOnBoot", enabled);
|
||||
}
|
||||
|
||||
// region Enable/disable
|
||||
|
||||
void Development::startServer() {
|
||||
void DevelopmentService::startServer() {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
@ -117,7 +142,7 @@ void Development::startServer() {
|
||||
}
|
||||
}
|
||||
|
||||
void Development::stopServer() {
|
||||
void DevelopmentService::stopServer() {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
@ -133,13 +158,13 @@ void Development::stopServer() {
|
||||
server = nullptr;
|
||||
}
|
||||
|
||||
bool Development::isStarted() const {
|
||||
bool DevelopmentService::isStarted() const {
|
||||
auto lock = mutex.asScopedLock();
|
||||
lock.lock();
|
||||
return server != nullptr;
|
||||
}
|
||||
|
||||
void Development::onNetworkConnected() {
|
||||
void DevelopmentService::onNetworkConnected() {
|
||||
TT_LOG_I(TAG, "onNetworkConnected");
|
||||
mutex.withLock([this] {
|
||||
if (isEnabled() && !isStarted()) {
|
||||
@ -148,7 +173,7 @@ void Development::onNetworkConnected() {
|
||||
});
|
||||
}
|
||||
|
||||
void Development::onNetworkDisconnected() {
|
||||
void DevelopmentService::onNetworkDisconnected() {
|
||||
TT_LOG_I(TAG, "onNetworkDisconnected");
|
||||
mutex.withLock([this] {
|
||||
if (isStarted()) {
|
||||
@ -159,13 +184,13 @@ void Development::onNetworkDisconnected() {
|
||||
|
||||
// region endpoints
|
||||
|
||||
esp_err_t Development::handleGetInfo(httpd_req_t* request) {
|
||||
esp_err_t DevelopmentService::handleGetInfo(httpd_req_t* request) {
|
||||
if (httpd_resp_set_type(request, "application/json") != ESP_OK) {
|
||||
TT_LOG_W(TAG, "Failed to send header");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
auto* service = static_cast<Development*>(request->user_ctx);
|
||||
auto* service = static_cast<DevelopmentService*>(request->user_ctx);
|
||||
|
||||
if (httpd_resp_sendstr(request, service->deviceResponse.c_str()) != ESP_OK) {
|
||||
TT_LOG_W(TAG, "Failed to send response body");
|
||||
@ -176,29 +201,73 @@ esp_err_t Development::handleGetInfo(httpd_req_t* request) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t Development::handleAppRun(httpd_req_t* request) {
|
||||
esp_err_t DevelopmentService::handleAppRun(httpd_req_t* request) {
|
||||
size_t buffer_length = httpd_req_get_url_query_len(request);
|
||||
if (buffer_length == 0) {
|
||||
TT_LOG_W(TAG, "[400] /app/run id not specified");
|
||||
httpd_resp_send_err(request, HTTPD_400_BAD_REQUEST, "id not specified");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
auto buffer = std::make_unique<char[]>(buffer_length + 1);
|
||||
if (buffer.get() == nullptr || httpd_req_get_url_query_str(request, buffer.get(), buffer_length + 1) != ESP_OK) {
|
||||
TT_LOG_W(TAG, "[500] /app/run");
|
||||
httpd_resp_send_500(request);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
auto parameters = network::parseUrlQuery(std::string(buffer.get()));
|
||||
auto id_key_pos = parameters.find("id");
|
||||
if (id_key_pos == parameters.end()) {
|
||||
TT_LOG_W(TAG, "[400] /app/run id not specified");
|
||||
httpd_resp_send_err(request, HTTPD_400_BAD_REQUEST, "id not specified");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
auto app_id = id_key_pos->second;
|
||||
if (!app::findAppById(app_id.c_str())) {
|
||||
TT_LOG_W(TAG, "[400] /app/run app not found");
|
||||
httpd_resp_send_err(request, HTTPD_400_BAD_REQUEST, "app not found");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
app::start(app_id);
|
||||
TT_LOG_I(TAG, "[200] /app/run %s", app_id.c_str());
|
||||
httpd_resp_send(request, nullptr, 0);
|
||||
TT_LOG_I(TAG, "[200] /app/run");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t Development::handleAppInstall(httpd_req_t* request) {
|
||||
esp_err_t DevelopmentService::handleAppInstall(httpd_req_t* request) {
|
||||
size_t header_size = httpd_req_get_hdr_value_len(request, "Content-Type");
|
||||
if (header_size == 0) {
|
||||
TT_LOG_W(TAG, "[400] /app/install Content-Type header not specified");
|
||||
httpd_resp_send_err(request, HTTPD_400_BAD_REQUEST, "Content-Type header not specified");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
auto header_buffer = std::make_unique<char[]>(header_size + 1);
|
||||
if (header_buffer.get() == nullptr || httpd_req_get_hdr_value_str(request, "Content-Type", header_buffer.get(), header_size + 1) != ESP_OK) {
|
||||
TT_LOG_W(TAG, "[500] /app/run");
|
||||
httpd_resp_send_500(request);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
TT_LOG_I(TAG, "[200] /app/install %s", header_buffer.get());
|
||||
httpd_resp_send(request, nullptr, 0);
|
||||
TT_LOG_I(TAG, "[200] /app/install");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
std::shared_ptr<Development> findService() {
|
||||
return std::static_pointer_cast<Development>(
|
||||
std::shared_ptr<DevelopmentService> findService() {
|
||||
return std::static_pointer_cast<DevelopmentService>(
|
||||
findServiceById(manifest.id)
|
||||
);
|
||||
}
|
||||
|
||||
extern const ServiceManifest manifest = {
|
||||
.id = "Development",
|
||||
.createService = create<Development>
|
||||
.createService = create<DevelopmentService>
|
||||
};
|
||||
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#include <lwip/esp_netif_net_stack.h>
|
||||
#include "Tactility/service/wifi/Wifi.h"
|
||||
|
||||
#include "Tactility/TactilityHeadless.h"
|
||||
@ -72,6 +73,7 @@ public:
|
||||
};
|
||||
bool pause_auto_connect = false; // Pause when manually disconnecting until manually connecting again
|
||||
bool connection_target_remember = false; // Whether to store the connection_target on successful connection or not
|
||||
esp_netif_ip_info_t ip_info;
|
||||
|
||||
RadioState getRadioState() const {
|
||||
auto lock = dataMutex.asScopedLock();
|
||||
@ -231,6 +233,19 @@ void disconnect() {
|
||||
getMainDispatcher().dispatch([wifi]() { dispatchDisconnectButKeepActive(wifi); });
|
||||
}
|
||||
|
||||
void clearIp() {
|
||||
auto wifi = wifi_singleton;
|
||||
if (wifi == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto lock = wifi->dataMutex.asScopedLock();
|
||||
if (!lock.lock(10 / portTICK_PERIOD_MS)) {
|
||||
return;
|
||||
}
|
||||
|
||||
memset(&wifi->ip_info, 0, sizeof(esp_netif_ip_info_t));
|
||||
}
|
||||
void setScanRecords(uint16_t records) {
|
||||
TT_LOG_I(TAG, "setScanRecords(%d)", records);
|
||||
auto wifi = wifi_singleton;
|
||||
@ -464,6 +479,7 @@ static void eventHandler(TT_UNUSED void* arg, esp_event_base_t event_base, int32
|
||||
}
|
||||
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
TT_LOG_I(TAG, "eventHandler: disconnected");
|
||||
clearIp();
|
||||
switch (wifi->getRadioState()) {
|
||||
case RadioState::ConnectionPending:
|
||||
wifi->connection_wait_flags.set(WIFI_FAIL_BIT);
|
||||
@ -480,6 +496,7 @@ static void eventHandler(TT_UNUSED void* arg, esp_event_base_t event_base, int32
|
||||
kernel::publishSystemEvent(kernel::SystemEvent::NetworkDisconnected);
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
auto* event = static_cast<ip_event_got_ip_t*>(event_data);
|
||||
memcpy(&wifi->ip_info, &event->ip_info, sizeof(esp_netif_ip_info_t));
|
||||
TT_LOG_I(TAG, "eventHandler: got ip:" IPSTR, IP2STR(&event->ip_info.ip));
|
||||
if (wifi->getRadioState() == RadioState::ConnectionPending) {
|
||||
wifi->connection_wait_flags.set(WIFI_CONNECTED_BIT);
|
||||
@ -877,6 +894,15 @@ void onAutoConnectTimer() {
|
||||
}
|
||||
}
|
||||
|
||||
std::string getIp() {
|
||||
auto wifi = std::static_pointer_cast<Wifi>(wifi_singleton);
|
||||
|
||||
auto lock = wifi->dataMutex.asScopedLock();
|
||||
lock.lock();
|
||||
|
||||
return std::format("{}.{}.{}.{}", IP2STR(&wifi->ip_info.ip));
|
||||
}
|
||||
|
||||
class WifiService final : public Service {
|
||||
|
||||
public:
|
||||
|
||||
@ -135,6 +135,10 @@ int getRssi() {
|
||||
}
|
||||
}
|
||||
|
||||
std::string getIp() {
|
||||
return "192.168.1.2";
|
||||
}
|
||||
|
||||
// endregion Public functions
|
||||
|
||||
class WifiService final : public Service {
|
||||
|
||||
@ -13,7 +13,6 @@ class Timer {
|
||||
public:
|
||||
|
||||
typedef std::function<void()> Callback;
|
||||
// typedef std::function<void(uint32_t)> PendingCallback;
|
||||
typedef void (*PendingCallback)(void* context, uint32_t arg);
|
||||
|
||||
private:
|
||||
|
||||
61
Tests/Tactility/UrlTest.cpp
Normal file
61
Tests/Tactility/UrlTest.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
#include "doctest.h"
|
||||
#include <Tactility/network/Url.h>
|
||||
|
||||
using namespace tt;
|
||||
|
||||
TEST_CASE("parseUrlQuery can handle a single key-value pair") {
|
||||
auto map = network::parseUrlQuery("?key=value");
|
||||
CHECK_EQ(map.size(), 1);
|
||||
CHECK_EQ(map["key"], "value");
|
||||
}
|
||||
|
||||
TEST_CASE("parseUrlQuery can handle empty value in the middle") {
|
||||
auto map = network::parseUrlQuery("?a=1&b=&c=3");
|
||||
CHECK_EQ(map.size(), 3);
|
||||
CHECK_EQ(map["a"], "1");
|
||||
CHECK_EQ(map["b"], "");
|
||||
CHECK_EQ(map["c"], "3");
|
||||
}
|
||||
|
||||
TEST_CASE("parseUrlQuery can handle empty value at the end") {
|
||||
auto map = network::parseUrlQuery("?a=1&b=");
|
||||
CHECK_EQ(map.size(), 2);
|
||||
CHECK_EQ(map["a"], "1");
|
||||
CHECK_EQ(map["b"], "");
|
||||
}
|
||||
|
||||
TEST_CASE("parseUrlQuery returns empty map when query s questionmark with a key without a value") {
|
||||
auto map = network::parseUrlQuery("?a");
|
||||
CHECK_EQ(map.size(), 0);
|
||||
}
|
||||
|
||||
TEST_CASE("parseUrlQuery returns empty map when query is a questionmark") {
|
||||
auto map = network::parseUrlQuery("?");
|
||||
CHECK_EQ(map.size(), 0);
|
||||
}
|
||||
|
||||
TEST_CASE("parseUrlQuery should url-decode the value") {
|
||||
auto map = network::parseUrlQuery("?key=Test%21Test");
|
||||
CHECK_EQ(map.size(), 1);
|
||||
CHECK_EQ(map["key"], "Test!Test");
|
||||
}
|
||||
|
||||
TEST_CASE("parseUrlQuery should url-decode the key") {
|
||||
auto map = network::parseUrlQuery("?Test%21Test=value");
|
||||
CHECK_EQ(map.size(), 1);
|
||||
CHECK_EQ(map["Test!Test"], "value");
|
||||
}
|
||||
|
||||
TEST_CASE("urlDecode") {
|
||||
auto input = std::string("prefix!*'();:@&=+$,/?#[]<>%-.^_`{}|~ \\");
|
||||
auto expected = std::string("prefix%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D%3C%3E%25-.%5E_%60%7B%7D%7C~+%5C");
|
||||
auto encoded = network::urlEncode(input);
|
||||
CHECK_EQ(encoded, expected);
|
||||
}
|
||||
|
||||
TEST_CASE("urlDecode") {
|
||||
auto input = std::string("prefix%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D%3C%3E%25-.%5E_%60%7B%7D%7C~+%5C");
|
||||
auto expected = std::string("prefix!*'();:@&=+$,/?#[]<>%-.^_`{}|~ \\");
|
||||
auto decoded = network::urlDecode(input);
|
||||
CHECK_EQ(decoded, expected);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user