Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FTP server as service (with settings in menu) #182

Merged
merged 4 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 0 additions & 117 deletions firmware/keira/src/apps/ftp/ftp_server.cpp

This file was deleted.

11 changes: 0 additions & 11 deletions firmware/keira/src/apps/ftp/ftp_server.h

This file was deleted.

53 changes: 49 additions & 4 deletions firmware/keira/src/apps/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "servicemanager.h"
#include "services/network.h"
#include "services/ftp.h"

#include "wifi_config.h"
#include "demos/lines.h"
Expand All @@ -26,7 +27,6 @@
#include "lua/luarunner.h"
#include "mjs/mjsrunner.h"
#include "nes/nesapp.h"
#include "ftp/ftp_server.h"
#include "weather/weather.h"
#include "modplayer/modplayer.h"
#include "liltracker/liltracker.h"
Expand All @@ -39,6 +39,7 @@
#include "icons/dev.h"
#include "icons/settings.h"
#include "icons/info.h"
#include "icons/app_group.h"

#include "icons/normalfile.h"
#include "icons/folder.h"
Expand Down Expand Up @@ -77,7 +78,8 @@ void LauncherApp::run() {
ITEM::APP("Куб", [this]() { this->runApp<CubeApp>(); }),
ITEM::APP("Епілепсія", [this]() { this->runApp<EpilepsyApp>(); }),
ITEM::APP("PetPet", [this]() { this->runApp<PetPetApp>(); }),
}
},
&app_group_img, 0
),
ITEM::SUBMENU(
"Тести",
Expand All @@ -87,7 +89,8 @@ void LauncherApp::run() {
ITEM::APP("I2C-сканер", [this]() { this->runApp<ScanI2CApp>(); }),
ITEM::APP("GPIO-менеджер", [this]() { this->runApp<GPIOManagerApp>(); }),
ITEM::APP("Combo", [this]() { this->runApp<ComboApp>(); }),
}
},
&app_group_img, 0
),
ITEM::APP("ЛілТрекер", [this]() { this->runApp<LilTrackerApp>(); }),
ITEM::APP("Летріс", [this]() { this->runApp<LetrisApp>(); }),
Expand Down Expand Up @@ -115,7 +118,6 @@ void LauncherApp::run() {
{
ITEM::APP("Live Lua", [this]() { this->runApp<LuaLiveRunnerApp>(); }),
ITEM::APP("Lua REPL", [this]() { this->runApp<LuaReplApp>(); }),
ITEM::APP("FTP сервер", [this]() { this->runApp<FTPServerApp>(); }),
},
&dev_img,
lilka::colors::Jasmine
Expand All @@ -136,6 +138,49 @@ void LauncherApp::run() {
ITEM::MENU("Мережі WiFi", [this]() { this->wifiManager(); }),
ITEM::MENU("Потужність WiFi", [this]() { this->setWiFiTxPower(); }),
ITEM::MENU("Звук", [this]() { this->runApp<SoundConfigApp>(); }),
ITEM::SUBMENU("Сервіси", {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

я так розумію що більше двох рівнів вкладень меню, тут не підтримується, судячи з того як ти вирішив проблему з таблицею розділів :)
Сервіси було б непогано запхати в налаштування.

може залишити просто від цих трьох ITEM:MENU, ITEM:SUBMENU і ITEM:APP щось одне, наприклад вон той самий MENU, а всередини в лямбді організовати ще одне ITEM:MENU і т.д.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Чого ж не пітримується? Налаштування - Сервіси - FTP - ...
Не хочеться рефакторити ITEM... бо ця структура доживає свої останні дні. По суті після додавання коллбеку в меню, можна стіорювати одразу його структуру, тоді вже і оптимізувати.
Зараз по суті APP та MENU відрізняються тільки наявністью дефолтної іконки

ITEM::SUBMENU("FTP", {
ITEM::MENU(
"Статус",
[this]() {
FTPService* ftpService = static_cast<FTPService*>(ServiceManager::getInstance()->getService<FTPService>("ftp"));
ftpService->setEnabled(!ftpService->getEnabled());
},
nullptr,
0,
[this](void* item) {
lilka::MenuItem* menuItem = static_cast<lilka::MenuItem*>(item);
FTPService* ftpService = static_cast<FTPService*>(ServiceManager::getInstance()->getService<FTPService>("ftp"));
menuItem->postfix = ftpService->getEnabled() ? "ON" : "OFF";
}
),
ITEM::MENU(
"Користувач",
nullptr,
nullptr,
0,
[this](void* item) {
lilka::MenuItem* menuItem = static_cast<lilka::MenuItem*>(item);
FTPService* ftpService = static_cast<FTPService*>(ServiceManager::getInstance()->getService<FTPService>("ftp"));
menuItem->postfix = ftpService->getUser();
}
),
ITEM::MENU(
"Пароль",
[this]() {
FTPService* ftpService = static_cast<FTPService*>(ServiceManager::getInstance()->getService<FTPService>("ftp"));
ftpService->createPassword();
},
nullptr,
0,
[this](void* item) {
lilka::MenuItem* menuItem = static_cast<lilka::MenuItem*>(item);
FTPService* ftpService = static_cast<FTPService*>(ServiceManager::getInstance()->getService<FTPService>("ftp"));
menuItem->postfix = ftpService->getPassword();
}
),
}),
}),
ITEM::MENU("Про систему", [this]() { this->about(); }),
ITEM::MENU("Інфо про пристрій", [this]() { this->info(); }),
ITEM::MENU("Таблиця розділів", [this]() { this->partitions(); }),
Expand Down
3 changes: 1 addition & 2 deletions firmware/keira/src/apps/launcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "services/network.h"
#include "icons/app.h"
#include "icons/app_group.h"
#include "app.h"

typedef struct item_t {
Expand All @@ -19,7 +18,7 @@ typedef struct item_t {
) {
return item_t{
name,
icon ? icon : &app_group_img,
icon,
color,
submenu,
};
Expand Down
2 changes: 2 additions & 0 deletions firmware/keira/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "services/screenshot.h"
#include "services/telnet.h"
#include "services/KeiraBLEService.h"
#include "services/ftp.h"
#include "apps/statusbar.h"
#include "apps/launcher.h"

Expand All @@ -23,6 +24,7 @@ void setup() {
serviceManager->addService(new ClockService());
serviceManager->addService(new ScreenshotService());
serviceManager->addService(new TelnetService());
serviceManager->addService(new FTPService());
serviceManager->addService(new KeiraBLEService());
appManager->setPanel(new StatusBarApp());
appManager->runApp(new LauncherApp());
Expand Down
92 changes: 92 additions & 0 deletions firmware/keira/src/services/ftp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "ftp.h"
#include "Preferences.h"
#include "servicemanager.h"
#include "network.h"

FTPService::FTPService() : Service("ftp") {
Preferences prefs;
prefs.begin(FTP_SETTINGS, true);
enabled = prefs.getBool("enabled", false);
password = prefs.getString("password", "");
prefs.end();
if (password.isEmpty()) {
createPassword();
}

networkService = ServiceManager::getInstance()->getService<NetworkService>("network");
lilka::fileutils.initSD();
}

FTPService::~FTPService() {
if (ftpServer) {
delete ftpServer;
}
}

void FTPService::run() {
bool wasOnline = false;
while (true) {
if (!networkService) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
continue;
}

bool isOnline = networkService->getNetworkState() == NetworkState::NETWORK_STATE_ONLINE;
if ((enabled && isOnline) && !wasOnline) {
ftpServer = new FtpServer();
ftpServer->begin(getUser().c_str(), getPassword().c_str());
wasOnline = true;
} else if ((!enabled || !isOnline) && wasOnline) {
ftpServer->end();
delete ftpServer;
ftpServer = nullptr;
wasOnline = false;
}

if (enabled && isOnline) {
ftpServer->handleFTP();
taskYIELD();
} else {
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}
}

bool FTPService::getEnabled() {
return enabled;
}

void FTPService::setEnabled(bool enabled) {
this->enabled = enabled;
Preferences prefs;
prefs.begin(FTP_SETTINGS, false);
prefs.putBool("enabled", enabled);
prefs.end();
}

String FTPService::getUser() {
return user;
}

String FTPService::getPassword() {
return password;
}

void FTPService::createPassword() {
char pwd[FTP_PASSWORD_LENGTH + 1];
for (int i = 0; i < FTP_PASSWORD_LENGTH; i++) {
pwd[i] = random(0, 2) == 0 ? random(48, 57) : random(97, 122);
}
pwd[FTP_PASSWORD_LENGTH] = 0;

Preferences prefs;
prefs.begin(FTP_SETTINGS, false);
prefs.putString("password", pwd);
prefs.end();

password = String(pwd);

if (ftpServer) {
ftpServer->credentials(user.c_str(), password.c_str());
}
}
30 changes: 30 additions & 0 deletions firmware/keira/src/services/ftp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <FtpServer.h>
#include "services/network.h"
#include "service.h"

#define FTP_SETTINGS "ftp"
#define FTP_USER "lilka"
#define FTP_PASSWORD_LENGTH 6

class FTPService : public Service {
private:
bool enabled;
String user = FTP_USER;
String password;
NetworkService* networkService;
FtpServer* ftpServer = nullptr;

public:
FTPService();
~FTPService();

bool getEnabled();
void setEnabled(bool enabled);
String getUser();
String getPassword();
String getEndpoint();
void createPassword();

private:
void run() override;
};
Loading