diff --git a/README.md b/README.md index 42ecb4b9..049be878 100644 --- a/README.md +++ b/README.md @@ -25,11 +25,16 @@ A 3d-printable housing can be found here: https://www.thingiverse.com/thing:4571 **General remark:** Beside the `firmware.bin`, typically also the content of `/html` needs to be updated! -##### Rolling - (2020-12-06) +##### Rolling - (2020-12-07) + +* Improvement: internal file handling + + +2020-12-06 * Option for fixed IP settings in `wlan.ini` - description see inside file -* based on v5.0.05 (2020-12-06) +* based on v5.0.0 (2020-12-06) diff --git a/code/components/connect_wlan/connect_wlan.cpp b/code/components/connect_wlan/connect_wlan.cpp index 0e75413e..81b144e7 100644 --- a/code/components/connect_wlan/connect_wlan.cpp +++ b/code/components/connect_wlan/connect_wlan.cpp @@ -232,7 +232,7 @@ void LoadWlanFromFile(std::string fn, std::string &_ssid, std::string &_passphra FILE* pFile; fn = FormatFileName(fn); - pFile = fopen(fn.c_str(), "r"); + pFile = OpenFileAndWait(fn.c_str(), "r"); printf("file loaded\n"); @@ -326,7 +326,7 @@ void LoadNetConfigFromFile(std::string fn, std::string &_ip, std::string &_gw, s FILE* pFile; fn = FormatFileName(fn); - pFile = fopen(fn.c_str(), "r"); + pFile = OpenFileAndWait(fn.c_str(), "r"); printf("file loaded\n"); diff --git a/code/components/jomjol_controlcamera/ClassControllCamera.cpp b/code/components/jomjol_controlcamera/ClassControllCamera.cpp index b24c8831..9a1a4bc3 100644 --- a/code/components/jomjol_controlcamera/ClassControllCamera.cpp +++ b/code/components/jomjol_controlcamera/ClassControllCamera.cpp @@ -255,7 +255,7 @@ esp_err_t CCamera::CaptureToFile(std::string nm, int delay) } } - FILE * fp = fopen(nm.c_str(), "wb"); + FILE * fp = OpenFileAndWait(nm.c_str(), "wb"); if (fp == NULL) /* If an error occurs during the file creation */ { fprintf(stderr, "fopen() failed for '%s'\n", nm.c_str()); diff --git a/code/components/jomjol_fileserver_ota/server_file.cpp b/code/components/jomjol_fileserver_ota/server_file.cpp index 3c8eb2dc..5a091b05 100644 --- a/code/components/jomjol_fileserver_ota/server_file.cpp +++ b/code/components/jomjol_fileserver_ota/server_file.cpp @@ -121,7 +121,7 @@ static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath, const ///////////////////////////////////////////////// if (!readonly) { - FILE *fd = fopen("/sdcard/html/upload_script.html", "r"); + FILE *fd = OpenFileAndWait("/sdcard/html/upload_script.html", "r"); char *chunk = ((struct file_server_data *)req->user_ctx)->scratch; size_t chunksize; do { @@ -231,7 +231,7 @@ static esp_err_t logfileact_get_handler(httpd_req_t *req) std::string currentfilename = LogFile.GetCurrentFileName(); - fd = fopen(currentfilename.c_str(), "r"); + fd = OpenFileAndWait(currentfilename.c_str(), "r"); if (!fd) { ESP_LOGE(TAG, "Failed to read existing file : %s", filepath); /* Respond with 500 Internal Server Error */ @@ -337,7 +337,7 @@ static esp_err_t download_get_handler(httpd_req_t *req) return ESP_FAIL; } - fd = fopen(filepath, "r"); + fd = OpenFileAndWait(filepath, "r"); if (!fd) { ESP_LOGE(TAG, "Failed to read existing file : %s", filepath); /* Respond with 500 Internal Server Error */ @@ -424,7 +424,7 @@ static esp_err_t upload_post_handler(httpd_req_t *req) return ESP_FAIL; } - fd = fopen(filepath, "w"); + fd = OpenFileAndWait(filepath, "w"); if (!fd) { ESP_LOGE(TAG, "Failed to create file : %s", filepath); /* Respond with 500 Internal Server Error */ @@ -710,7 +710,7 @@ void unzip(std::string _in_zip_file, std::string _target_directory){ zw = std::string(archive_filename); zw = _target_directory + zw; printf("Filename to extract: %s", zw.c_str()); - FILE* fpTargetFile = fopen(zw.c_str(), "wb"); + FILE* fpTargetFile = OpenFileAndWait(zw.c_str(), "wb"); fwrite(p, 1, (uint)uncomp_size, fpTargetFile); fclose(fpTargetFile); diff --git a/code/components/jomjol_fileserver_ota/server_help.cpp b/code/components/jomjol_fileserver_ota/server_help.cpp index 81ed8a9d..17faa622 100644 --- a/code/components/jomjol_fileserver_ota/server_help.cpp +++ b/code/components/jomjol_fileserver_ota/server_help.cpp @@ -10,6 +10,8 @@ #include "esp_err.h" #include "esp_log.h" +#include "Helper.h" + #include "esp_http_server.h" @@ -25,7 +27,7 @@ char scratch[SCRATCH_BUFSIZE]; esp_err_t send_file(httpd_req_t *req, std::string filename, struct stat * file_stat) { - FILE *fd = fopen(filename.c_str(), "r"); + FILE *fd = OpenFileAndWait(filename.c_str(), "r"); if (!fd) { ESP_LOGE(TAG, "Failed to read existing file : %s", filename.c_str()); /* Respond with 500 Internal Server Error */ diff --git a/code/components/jomjol_fileserver_ota/server_ota.cpp b/code/components/jomjol_fileserver_ota/server_ota.cpp index c18409c7..44efdce2 100644 --- a/code/components/jomjol_fileserver_ota/server_ota.cpp +++ b/code/components/jomjol_fileserver_ota/server_ota.cpp @@ -31,6 +31,8 @@ #include "ClassLogFile.h" +#include "Helper.h" + #define BUFFSIZE 1024 @@ -89,7 +91,7 @@ static bool ota_example_task(std::string fn) int data_read; - FILE* f = fopen(fn.c_str(), "rb"); // vorher nur "r" + FILE* f = OpenFileAndWait(fn.c_str(), "rb"); // vorher nur "r" data_read = fread(ota_write_data, 1, BUFFSIZE, f); while (data_read > 0) { diff --git a/code/components/jomjol_flowcontroll/ClassFlowControll.cpp b/code/components/jomjol_flowcontroll/ClassFlowControll.cpp index 2eb72fe6..5734ef26 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowControll.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowControll.cpp @@ -122,7 +122,7 @@ void ClassFlowControll::InitFlow(std::string config) ClassFlow* cfc; FILE* pFile; config = FormatFileName(config); - pFile = fopen(config.c_str(), "r"); + pFile = OpenFileAndWait(config.c_str(), "r"); line = ""; diff --git a/code/components/jomjol_helper/CMakeLists.txt b/code/components/jomjol_helper/CMakeLists.txt index bd92d82d..948e3829 100644 --- a/code/components/jomjol_helper/CMakeLists.txt +++ b/code/components/jomjol_helper/CMakeLists.txt @@ -2,6 +2,6 @@ FILE(GLOB_RECURSE app_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.*) idf_component_register(SRCS ${app_sources} INCLUDE_DIRS "." - REQUIRES tfmicro) + REQUIRES tfmicro jomjol_logfile) diff --git a/code/components/jomjol_helper/Helper.cpp b/code/components/jomjol_helper/Helper.cpp index 5ffd73e0..00544c1e 100644 --- a/code/components/jomjol_helper/Helper.cpp +++ b/code/components/jomjol_helper/Helper.cpp @@ -1,5 +1,8 @@ //#pragma warning(disable : 4996) +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + #include "Helper.h" #include #include @@ -7,11 +10,33 @@ #include #include +#include "ClassLogFile.h" +//#include "ClassLogFile.h" + //#define ISWINDOWS_TRUE #define PATH_MAX_STRING_SIZE 256 using namespace std; + +FILE* OpenFileAndWait(const char* nm, char* _mode, int _waitsec) +{ + FILE *pfile = fopen(nm, _mode); + + if (pfile == NULL) + { + TickType_t xDelay; + xDelay = _waitsec * 1000 / portTICK_PERIOD_MS; + std::string zw = "File is locked: " + std::string(nm) + " - wait for " + std::to_string(_waitsec); + printf(zw.c_str()); + printf("\n"); + LogFile.WriteToFile(zw); + vTaskDelay( xDelay ); + pfile = fopen(nm, _mode); + } + return pfile; +} + std::string FormatFileName(std::string input) { #ifdef ISWINDOWS_TRUE @@ -126,14 +151,14 @@ void CopyFile(string input, string output) } char cTemp; - FILE* fpSourceFile = fopen(input.c_str(), "rb"); + FILE* fpSourceFile = OpenFileAndWait(input.c_str(), "rb"); if (!fpSourceFile) // Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch! { printf("File %s existiert nicht!\n", input.c_str()); return; } - FILE* fpTargetFile = fopen(output.c_str(), "wb"); + FILE* fpTargetFile = OpenFileAndWait(output.c_str(), "wb"); // Code Section diff --git a/code/components/jomjol_helper/Helper.h b/code/components/jomjol_helper/Helper.h index 7891263c..3c65d222 100644 --- a/code/components/jomjol_helper/Helper.h +++ b/code/components/jomjol_helper/Helper.h @@ -10,6 +10,8 @@ void FindReplace(std::string& line, std::string& oldString, std::string& newStri void CopyFile(string input, string output); +FILE* OpenFileAndWait(const char* nm, char* _mode, int _waitsec = 10); + size_t findDelimiterPos(string input, string delimiter); //string trim(string istring); string trim(string istring, string adddelimiter = ""); diff --git a/code/components/jomjol_logfile/ClassLogFile.cpp b/code/components/jomjol_logfile/ClassLogFile.cpp index 78b0df19..d134448b 100644 --- a/code/components/jomjol_logfile/ClassLogFile.cpp +++ b/code/components/jomjol_logfile/ClassLogFile.cpp @@ -19,7 +19,8 @@ void ClassLogFile::WriteToDedicatedFile(std::string _fn, std::string info, bool return; } - pFile = fopen(_fn.c_str(), "a+"); + pFile = OpenFileAndWait(_fn.c_str(), "a+"); + if (pFile!=NULL) { if (_time) { diff --git a/code/components/jomjol_tfliteclass/CMakeLists.txt b/code/components/jomjol_tfliteclass/CMakeLists.txt index 8d140751..7c2c5594 100644 --- a/code/components/jomjol_tfliteclass/CMakeLists.txt +++ b/code/components/jomjol_tfliteclass/CMakeLists.txt @@ -2,6 +2,6 @@ FILE(GLOB_RECURSE app_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.*) idf_component_register(SRCS ${app_sources} INCLUDE_DIRS "." - REQUIRES jomjol_image_proc jomjol_logfile esp_http_server esp32-camera-master jomjol_controlcamera jomjol_flowcontroll) + REQUIRES jomjol_image_proc jomjol_logfile esp_http_server esp32-camera-master jomjol_controlcamera jomjol_flowcontroll jomjol_helper) diff --git a/code/components/jomjol_tfliteclass/CTfLiteClass.cpp b/code/components/jomjol_tfliteclass/CTfLiteClass.cpp index c6a2d7c5..14458d38 100644 --- a/code/components/jomjol_tfliteclass/CTfLiteClass.cpp +++ b/code/components/jomjol_tfliteclass/CTfLiteClass.cpp @@ -4,6 +4,8 @@ #include "ClassLogFile.h" +#include "Helper.h" + #include bool debugdetailtflite = false; @@ -199,7 +201,7 @@ unsigned char* CTfLiteClass::ReadFileToCharArray(std::string _fn) if(result != NULL) { // printf("\nSpeicher ist reserviert\n"); - FILE* f = fopen(_fn.c_str(), "rb"); // vorher nur "r" + FILE* f = OpenFileAndWait(_fn.c_str(), "rb"); // vorher nur "r" fread(result, 1, size, f); fclose(f); }else { diff --git a/code/main/version.cpp b/code/main/version.cpp index 8fe82fff..74c9015a 100644 --- a/code/main/version.cpp +++ b/code/main/version.cpp @@ -1,4 +1,4 @@ -const char* GIT_REV="a8fb2e3"; +const char* GIT_REV="793f928"; const char* GIT_TAG=""; const char* GIT_BRANCH="rolling"; -const char* BUILD_TIME="2020-12-06 21:10"; \ No newline at end of file +const char* BUILD_TIME="2020-12-07 20:40"; \ No newline at end of file diff --git a/code/version.cpp b/code/version.cpp index 8fe82fff..74c9015a 100644 --- a/code/version.cpp +++ b/code/version.cpp @@ -1,4 +1,4 @@ -const char* GIT_REV="a8fb2e3"; +const char* GIT_REV="793f928"; const char* GIT_TAG=""; const char* GIT_BRANCH="rolling"; -const char* BUILD_TIME="2020-12-06 21:10"; \ No newline at end of file +const char* BUILD_TIME="2020-12-07 20:40"; \ No newline at end of file diff --git a/firmware/bootloader.bin b/firmware/bootloader.bin index 94019446..dc68b2b7 100644 Binary files a/firmware/bootloader.bin and b/firmware/bootloader.bin differ diff --git a/firmware/firmware.bin b/firmware/firmware.bin index dc33e4dd..c81a80d6 100644 Binary files a/firmware/firmware.bin and b/firmware/firmware.bin differ