Skip to content

Commit

Permalink
Improve initial setup proc (#1692)
Browse files Browse the repository at this point in the history
* Initial Test

* Initial functional version.
  • Loading branch information
jomjol authored Dec 27, 2022
1 parent de223f5 commit 85030e3
Show file tree
Hide file tree
Showing 12 changed files with 674 additions and 15 deletions.
20 changes: 11 additions & 9 deletions code/components/jomjol_fileserver_ota/server_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ void delete_all_in_directory(std::string _directory)
closedir(dir);
}

std::string unzip_new(std::string _in_zip_file, std::string _target_zip, std::string _target_bin, std::string _main)
std::string unzip_new(std::string _in_zip_file, std::string _target_zip, std::string _target_bin, std::string _main, bool _initial_setup)
{
int i, sort_iter;
mz_bool status;
Expand All @@ -905,13 +905,9 @@ std::string unzip_new(std::string _in_zip_file, std::string _target_zip, std::st
char archive_filename[64];
std::string zw, ret = "";
std::string directory = "";
// static const char* s_Test_archive_filename = "testhtml.zip";

ESP_LOGD(TAG, "miniz.c version: %s", MZ_VERSION);
ESP_LOGD(TAG, "Zipfile: %s", _in_zip_file.c_str());
// ESP_LOGD(TAG, "Target Dir ZIP: %s", _target_zip.c_str());
// ESP_LOGD(TAG, "Target Dir BIN: %s", _target_bin.c_str());
// ESP_LOGD(TAG, "Target Dir main: %s", _main.c_str());

// Now try to open the archive.
memset(&zip_archive, 0, sizeof(zip_archive));
Expand All @@ -928,7 +924,6 @@ std::string unzip_new(std::string _in_zip_file, std::string _target_zip, std::st


sort_iter = 0;
// for (sort_iter = 0; sort_iter < 2; sort_iter++)
{
memset(&zip_archive, 0, sizeof(zip_archive));
status = mz_zip_reader_init_file(&zip_archive, _in_zip_file.c_str(), sort_iter ? MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY : 0);
Expand Down Expand Up @@ -966,6 +961,16 @@ std::string unzip_new(std::string _in_zip_file, std::string _target_zip, std::st
else
{
std::string _dir = getDirectory(zw);
if ((_dir == "config-initial") && !_initial_setup)
{
continue;
}
else
{
_dir = "config";
std::string _s1 = "config-initial";
FindReplace(zw, _s1, _dir);
}

if (_dir.length() > 0)
{
Expand Down Expand Up @@ -1006,9 +1011,6 @@ std::string unzip_new(std::string _in_zip_file, std::string _target_zip, std::st
isokay = isokay && RenameFile(filename_zw, zw);
if (!isokay)
ESP_LOGE(TAG, "ERROR in Rename \"%s\" to \"%s\"", filename_zw.c_str(), zw.c_str());
// isokay = isokay && DeleteFile(filename_zw);
// if (!isokay)
// ESP_LOGE(TAG, "ERROR in Delete \"%s\"", filename_zw.c_str());

if (isokay)
ESP_LOGI(TAG, "Successfully extracted file \"%s\", size %u", archive_filename, (uint)uncomp_size);
Expand Down
2 changes: 1 addition & 1 deletion code/components/jomjol_fileserver_ota/server_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
void register_server_file_uri(httpd_handle_t server, const char *base_path);

void unzip(std::string _in_zip_file, std::string _target_directory);
std::string unzip_new(std::string _in_zip_file, std::string _target_zip, std::string _target_bin, std::string _main = "/sdcard/");
std::string unzip_new(std::string _in_zip_file, std::string _target_zip, std::string _target_bin, std::string _main = "/sdcard/", bool _initial_setup = false);


void delete_all_in_directory(std::string _directory);
Expand Down
19 changes: 17 additions & 2 deletions code/components/jomjol_fileserver_ota/server_ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ esp_err_t handler_reboot(httpd_req_t *req);
static bool ota_update_task(std::string fn);

std::string _file_name_update;

bool initial_setup = false;


static void infinite_loop(void)
Expand Down Expand Up @@ -76,14 +76,20 @@ void task_do_Update_ZIP(void *pvParameter)
out = "/sdcard/html";
outbin = "/sdcard/firmware";

retfirmware = unzip_new(_file_name_update, out+"/", outbin+"/");
retfirmware = unzip_new(_file_name_update, out+"/", outbin+"/", "/sdcard/", initial_setup);
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Files unzipped.");

if (retfirmware.length() > 0)
{
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Found firmware.bin");
ota_update_task(retfirmware);
}

if (initial_setup)
{

}

LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Trigger reboot due to firmware update.");
doReboot();
}
Expand All @@ -106,6 +112,15 @@ void CheckUpdate()
char zw[1024] = "";
fgets(zw, 1024, pfile);
_file_name_update = std::string(zw);
if (fgets(zw, 1024, pfile))
{
std::string _szw = std::string(zw);
if (_szw == "init")
{
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Inital Setup triggered.");
initial_setup = true; }
}

fclose(pfile);
DeleteFile("/sdcard/update.txt"); // Prevent Boot Loop!!!
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Update during boot triggered - Update File: " + _file_name_update);
Expand Down
22 changes: 22 additions & 0 deletions code/components/jomjol_helper/Helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,17 @@ bool RenameFile(string from, string to)
return true;
}

bool FileExists(string filename)
{
FILE* fpSourceFile = fopen(filename.c_str(), "rb");
if (!fpSourceFile) // Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch!
{
return false;
}
fclose(fpSourceFile);
return true;
}


bool DeleteFile(string fn)
{
Expand Down Expand Up @@ -582,6 +593,17 @@ std::vector<string> ZerlegeZeile(std::string input, std::string delimiter)
}


std::string ReplaceString(std::string subject, const std::string& search,
const std::string& replace) {
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
return subject;
}


/* Source: https://git.kernel.org/pub/scm/utils/mmc/mmc-utils.git/tree/lsmmc.c */
/* SD Card Manufacturer Database */
struct SDCard_Manufacturer_database {
Expand Down
2 changes: 2 additions & 0 deletions code/components/jomjol_helper/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ bool CopyFile(string input, string output);
bool DeleteFile(string fn);
bool RenameFile(string from, string to);
bool MakeDir(std::string _what);
bool FileExists(string filename);


string RundeOutput(double _in, int _anzNachkomma);
Expand All @@ -31,6 +32,7 @@ string getFileType(string filename);
string getFileFullFileName(string filename);
string getDirectory(string filename);


int mkdir_r(const char *dir, const mode_t mode);
int removeFolder(const char* folderPath, const char* logTag);

Expand Down
8 changes: 6 additions & 2 deletions code/components/jomjol_wlan/read_wlanini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ std::vector<string> ZerlegeZeileWLAN(std::string input, std::string _delimiter =



void LoadWlanFromFile(std::string fn, char *&_ssid, char *&_password, char *&_hostname, char *&_ipadr, char *&_gw, char *&_netmask, char *&_dns, int &_rssithreashold)
bool LoadWlanFromFile(std::string fn, char *&_ssid, char *&_password, char *&_hostname, char *&_ipadr, char *&_gw, char *&_netmask, char *&_dns, int &_rssithreashold)
{
std::string ssid = "";
std::string passphrase = "";
Expand All @@ -59,10 +59,13 @@ void LoadWlanFromFile(std::string fn, char *&_ssid, char *&_password, char *&_ho
fn = FormatFileName(fn);

pFile = fopen(fn.c_str(), "r");
if (!pFile)
return false;

ESP_LOGD(TAG, "file loaded");

if (pFile == NULL)
return;
return false;

char zw[1024];
fgets(zw, 1024, pFile);
Expand Down Expand Up @@ -193,6 +196,7 @@ void LoadWlanFromFile(std::string fn, char *&_ssid, char *&_password, char *&_ho

_rssithreashold = rssithreshold;
RSSIThreashold = rssithreshold;
return true;
}


Expand Down
2 changes: 1 addition & 1 deletion code/components/jomjol_wlan/read_wlanini.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <string>

void LoadWlanFromFile(std::string fn, char *&_ssid, char *&_password, char *&_hostname, char *&_ipadr, char *&_gw, char *&_netmask, char *&_dns, int &_rssithreashold);
bool LoadWlanFromFile(std::string fn, char *&_ssid, char *&_password, char *&_hostname, char *&_ipadr, char *&_gw, char *&_netmask, char *&_dns, int &_rssithreashold);

bool ChangeHostName(std::string fn, std::string _newhostname);
bool ChangeRSSIThreashold(std::string fn, int _newrssithreashold);
Expand Down
5 changes: 5 additions & 0 deletions code/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include "../../include/defines.h"
//#include "server_GPIO.h"

#include "softAP.h"

extern const char* GIT_TAG;
extern const char* GIT_REV;
extern const char* GIT_BRANCH;
Expand Down Expand Up @@ -175,6 +177,8 @@ extern "C" void app_main(void)

CheckOTAUpdate();
CheckUpdate();
CheckStartAPMode(); // if no wlan.ini and/or config.ini --> AP ist startet and this function does not exit anymore until reboot


char *ssid = NULL, *passwd = NULL, *hostname = NULL, *ip = NULL, *gateway = NULL, *netmask = NULL, *dns = NULL; int rssithreashold = 0;
LoadWlanFromFile("/sdcard/wlan.ini", ssid, passwd, hostname, ip, gateway, netmask, dns, rssithreashold);
Expand Down Expand Up @@ -318,3 +322,4 @@ extern "C" void app_main(void)
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Initialization failed. Not starting flows!");
}
}

4 changes: 4 additions & 0 deletions code/main/server_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "server_tflite.h"
#include "esp_log.h"

#include <stdio.h>

#include "Helper.h"

httpd_handle_t server = NULL;
Expand Down Expand Up @@ -350,6 +352,7 @@ esp_err_t img_tmp_virtual_handler(httpd_req_t *req)




esp_err_t sysinfo_handler(httpd_req_t *req)
{
const char* resp_str;
Expand Down Expand Up @@ -421,6 +424,7 @@ void register_server_main_uri(httpd_handle_t server, const char *base_path)
httpd_register_uri_handler(server, &starttime_tmp_handle);



httpd_uri_t img_tmp_handle = {
.uri = "/img_tmp/*", // Match all URIs of type /path/to/file
.method = HTTP_GET,
Expand Down
Loading

0 comments on commit 85030e3

Please sign in to comment.