diff --git a/src/app/CFGFileParser.cpp b/src/app/CFGFileParser.cpp index 8fb51f5..1df9657 100644 --- a/src/app/CFGFileParser.cpp +++ b/src/app/CFGFileParser.cpp @@ -168,37 +168,3 @@ boolean CFGFileParser::save(void *data) { return true; } - -char *CFGFileParser::addChar(char *pointer, const char character) -{ - char *tempAddr = NULL; - if(pointer == NULL) - { - tempAddr = (char *) realloc(pointer, 2*sizeof(char)); - if(tempAddr == NULL) - return NULL; - else - { - pointer = tempAddr; - pointer[0] = character; - pointer[1] = '\0'; - } - } - else - { - tempAddr = (char *) realloc(pointer, (strlen(pointer)+2)*sizeof(char)); - if(tempAddr == NULL) - { - free(pointer); - return NULL; - } - else - { - pointer = tempAddr; - pointer[strlen(pointer)+1] = '\0'; - pointer[strlen(pointer)] = character; - } - } - - return pointer; -} diff --git a/src/app/CFGFileParser.h b/src/app/CFGFileParser.h index 7edb6d3..f939359 100644 --- a/src/app/CFGFileParser.h +++ b/src/app/CFGFileParser.h @@ -16,8 +16,6 @@ public: protected: private: //This part handles the _buff realloc - char *addChar(char *pointer, const char character); - enum State {INIT, COMMENT_SECTION, LINE_BREAK, PARAM_SECTION, ERROR, OPENING_QUOTE, SEPARATION}; enum Type {PARAMETER, VALUE}; State _state; diff --git a/src/app/Dictionary.h b/src/app/Dictionary.h index d11a2fc..5c7364e 100644 --- a/src/app/Dictionary.h +++ b/src/app/Dictionary.h @@ -6,6 +6,9 @@ #include #include +template +class Dictionary; + namespace DictionaryHelper { //String helper class with c style char array @@ -32,6 +35,7 @@ namespace DictionaryHelper } ~StringEntity(){free(_string);} char *getString(){return _string;} + Dictionary *split(char character); private: char *_string; }; @@ -78,12 +82,28 @@ public: return addNewNodeAtTheEnd(dictionaryNode); } + boolean add(int indice, T *value) + { + char indiceToStr[10]; + sprintf(indiceToStr,"%d", indice); + Dictionary *dictionaryNode = new Dictionary(indiceToStr, value); + return addNewNodeAtTheEnd(dictionaryNode); + } + boolean add(const char *parameter, T value) { Dictionary *dictionaryNode = new Dictionary(parameter, new T(value)); return addNewNodeAtTheEnd(dictionaryNode); } + boolean add(int indice, T value) + { + char indiceToStr[10]; + sprintf(indiceToStr,"%d", indice); + Dictionary *dictionaryNode = new Dictionary(indiceToStr, new T(value)); + return addNewNodeAtTheEnd(dictionaryNode); + } + boolean remove(const char *parameter) { if(_head->_next == NULL) return false; diff --git a/src/app/ScreenManager.cpp b/src/app/ScreenManager.cpp index c4b4491..4985d5b 100644 --- a/src/app/ScreenManager.cpp +++ b/src/app/ScreenManager.cpp @@ -1,6 +1,6 @@ #include "ScreenManager.h" -ScreenManager::ScreenManager(Adafruit_SSD1306 &display) : _displayRef(display), _displayColorInverted(false), _displayDimmed(false), _currentView(NO_CURRENT_VIEW), _error(0) +ScreenManager::ScreenManager(Adafruit_SSD1306 &display) : _displayRef(display), _displayColorInverted(false), _displayDimmed(false), _currentView(NO_CURRENT_VIEW), _error(OK) , _viewNotFound{&(displayError), (ErrorInfo *)malloc(sizeof(ErrorInfo)), RESERVED_VIEW_UID, NULL} , _viewFuncUndefined{&(displayError), (ErrorInfo *)malloc(sizeof(ErrorInfo)), RESERVED_VIEW_UID, NULL} , _currentViewUndefined{&(displayError), (ErrorInfo *)malloc(sizeof(ErrorInfo)), RESERVED_VIEW_UID, NULL} @@ -27,7 +27,7 @@ boolean ScreenManager::addNewLinkAtTheEnd(ViewLinkedList *viewLinkedList, ViewLi ViewLink *newViewLink = (ViewLink*) malloc(sizeof(ViewLink)); if(newViewLink == NULL) { - _error |= MALLOC_ERR; + _error = MALLOC_FAILED; return false; } @@ -103,7 +103,7 @@ boolean ScreenManager::isListEmpty(ViewLinkedList viewLinkedList) return viewLinkedList == NULL; } -unsigned char ScreenManager::getError() const +ScreenManager::Error ScreenManager::getError() const { return _error; } @@ -148,23 +148,37 @@ boolean ScreenManager::displayView(const int UID) ((ErrorInfo *)_currentViewUndefined.pData)->viewUID = UID; (*_currentViewUndefined.viewLogicFunction)(_displayRef, _currentViewUndefined.pData); _displayRef.display(); + + _error = CURRENT_VIEW_UNDEFINED; return false; } else if(UID == -1) { - if(!(*_currentView->viewLogicFunction)(_displayRef, _currentView->pData)) + if(_currentView->viewLogicFunction == NULL) + { + //We display an error message on the screen + ((ErrorInfo *)_viewFuncUndefined.pData)->errorMessage = F("View function is NULL"); + ((ErrorInfo *)_viewFuncUndefined.pData)->viewUID = _currentView->UID; + (*_viewFuncUndefined.viewLogicFunction)(_displayRef, _viewFuncUndefined.pData); + _displayRef.display(); + + _error = VIEW_FUNC_UNDEFINED; + return false; + } + else if(!(*_currentView->viewLogicFunction)(_displayRef, _currentView->pData)) { //We display an error message on the screen _displayRef.clearDisplay(); ((ErrorInfo *)_viewFunctionFailedToExecute.pData)->errorMessage = F("View function failed\nto execute"); - ((ErrorInfo *)_viewFunctionFailedToExecute.pData)->viewUID = UID; + ((ErrorInfo *)_viewFunctionFailedToExecute.pData)->viewUID = _currentView->UID; (*_viewFunctionFailedToExecute.viewLogicFunction)(_displayRef, _viewFunctionFailedToExecute.pData); _displayRef.display(); - - _currentView = &_viewFunctionFailedToExecute; + + _error = VIEW_FAILED_TO_EXECUTE; return false; } - + + _error = OK; _displayRef.display(); return true; } @@ -178,6 +192,7 @@ boolean ScreenManager::displayView(const int UID) (*_viewNotFound.viewLogicFunction)(_displayRef, _viewNotFound.pData); _displayRef.display(); + _error = VIEW_NOT_FOUND; _currentView = &_viewNotFound; return false; }else if(viewLink->viewLogicFunction == NULL) @@ -187,8 +202,9 @@ boolean ScreenManager::displayView(const int UID) ((ErrorInfo *)_viewFuncUndefined.pData)->viewUID = UID; (*_viewFuncUndefined.viewLogicFunction)(_displayRef, _viewFuncUndefined.pData); _displayRef.display(); - - _currentView = &_viewFuncUndefined; + + _error = VIEW_FUNC_UNDEFINED; + _currentView = viewLink; return false; } @@ -201,20 +217,20 @@ boolean ScreenManager::displayView(const int UID) ((ErrorInfo *)_viewFunctionFailedToExecute.pData)->viewUID = UID; (*_viewFunctionFailedToExecute.viewLogicFunction)(_displayRef, _viewFunctionFailedToExecute.pData); _displayRef.display(); - - _currentView = &_viewFunctionFailedToExecute; + + _error = VIEW_FAILED_TO_EXECUTE; + _currentView = viewLink; return false; } _displayRef.display(); _currentView = viewLink; + _error = OK; return true; } boolean ScreenManager::displayNextView() { - if(_currentView->UID < 0) return false; - if(_currentView == NO_CURRENT_VIEW && !isListEmpty(_viewLinkedList)) { _currentView = _viewLinkedList; @@ -317,3 +333,25 @@ boolean ScreenManager::displayError(Adafruit_SSD1306 &display, void *pData) return true; } +const char* ScreenManager::getErrorMessage()const +{ + //NO_VIEW_ERROR, MALLOC_FAILED, VIEW_NOT_FOUND, VIEW_FUNC_UNDEFINED, VIEW_FAILED_TO_EXECUTE, CURRENT_VIEW_UNDEFINED + switch(_error) + { + case OK: + return "NO ERROR"; + case MALLOC_FAILED: + return "MALLOC FAILED"; + case VIEW_NOT_FOUND: + return "VIEW NOT FOUND"; + case VIEW_FUNC_UNDEFINED: + return "VIEW FUNCTION UNDEFINED (NULL)"; + case VIEW_FAILED_TO_EXECUTE: + return "VIEW FAILED TO EXECUTE (RETURNED FALSE)"; + case CURRENT_VIEW_UNDEFINED: + return "CURRENT VIEW UNDEFINED"; + default : + return "UNKNOWN ERROR"; + } +} + diff --git a/src/app/ScreenManager.h b/src/app/ScreenManager.h index 6af0e59..8d34d78 100644 --- a/src/app/ScreenManager.h +++ b/src/app/ScreenManager.h @@ -7,6 +7,8 @@ class ScreenManager { friend class SAB; public: + enum Error {OK, MALLOC_FAILED, VIEW_NOT_FOUND, VIEW_FUNC_UNDEFINED, VIEW_FAILED_TO_EXECUTE, CURRENT_VIEW_UNDEFINED}; + boolean addView(boolean (*viewLogicFunction)(Adafruit_SSD1306&, void*), void *pData, const unsigned char UID); boolean removeView(const unsigned char UID); boolean displayView(const int UID = -1); @@ -16,7 +18,8 @@ class ScreenManager void dimDisplay(const boolean dimmed); void clearDisplay(const boolean bufferOnly = false); - unsigned char getError() const; + Error getError() const; + const char* getErrorMessage() const; boolean isDisplayColorInverted() const; Orientation getDisplayOrientation() const; boolean isDisplayDimmed() const; @@ -38,7 +41,7 @@ class ScreenManager Adafruit_SSD1306 &_displayRef; ViewLinkedList _viewLinkedList; - unsigned char _error; + Error _error; boolean _displayColorInverted; boolean _displayDimmed; ViewLink* _currentView; diff --git a/src/app/WEBServerManager.cpp b/src/app/WEBServerManager.cpp index bd59b9f..88ac8d2 100644 --- a/src/app/WEBServerManager.cpp +++ b/src/app/WEBServerManager.cpp @@ -252,40 +252,6 @@ unsigned int WEBServerManager::getPort() const return _port; } -char *WEBServerManager::addChar(char *pointer, const char character) -{ - char *tempAddr = NULL; - if(pointer == NULL) - { - tempAddr = (char *) realloc(pointer, 2*sizeof(char)); - if(tempAddr == NULL) - return NULL; - else - { - pointer = tempAddr; - pointer[0] = character; - pointer[1] = '\0'; - } - } - else - { - tempAddr = (char *) realloc(pointer, (strlen(pointer)+2)*sizeof(char)); - if(tempAddr == NULL) - { - free(pointer); - return NULL; - } - else - { - pointer = tempAddr; - pointer[strlen(pointer)+1] = '\0'; - pointer[strlen(pointer)] = character; - } - } - - return pointer; -} - boolean WEBServerManager::sendPageToClientFromSdCard(WiFiClient *wifiClient) { if(_sdCardManager != NULL) diff --git a/src/app/WEBServerManager.h b/src/app/WEBServerManager.h index 7109332..9c195a2 100644 --- a/src/app/WEBServerManager.h +++ b/src/app/WEBServerManager.h @@ -46,7 +46,6 @@ class WEBServerManager boolean parseQuery(WiFiClient *wifiClient); boolean sendPageToClientFromSdCard(WiFiClient *wifiClient); boolean sendPageToClientFromApiDictio(WiFiClient *wifiClient); - char *addChar(char *pointer, const char character); HttpRequestMethod getHttpVerbEnumValue(const char *parseBuffer); HttpVersion getHttpVersionEnumValue(const char *parseBuffer); char *getFilePathByHttpResource(const char *res); diff --git a/src/app/app.ino b/src/app/app.ino index 768b6a0..5937c12 100644 --- a/src/app/app.ino +++ b/src/app/app.ino @@ -31,6 +31,10 @@ void setup() sab.getScreenManager().addView(&(view_1), &v1p, 0); sab.getScreenManager().addView(&(view_2), &vap, 1); sab.getScreenManager().addView(&(view_3), &vstap, 2); + sab.getScreenManager().addView(&(memInfo), NULL, 3); + sab.getScreenManager().addView(NULL, NULL, 4); //for testin purposes + sab.getScreenManager().addView(&(dummy), NULL, 5); //for testin purposes + sab.getScreenManager().displayView(0); if(sab.getRtcManager().hasLostPower()) { @@ -38,11 +42,15 @@ void setup() sab.getRtcManager().setDateTime(DateTime(F(__DATE__), F(__TIME__))); } - sab.getWebServerManager().addApiRoutine("/helloServer", &(helloServerApi), NULL); - sab.getWebServerManager().addApiRoutine("/view/next", &(nextViewApi), &sab, WEBServerManager::GET); - sab.getWebServerManager().addApiRoutine("/rtc/get/time", &(rtcTimeApi), &sab, WEBServerManager::GET); - sab.getWebServerManager().addApiRoutine("/sdcard/eject", &(sdCardEjectApi), &sab, WEBServerManager::GET); - sab.getWebServerManager().addApiRoutine("/sdcard/insert", &(sdCardInsertApi), &sab, WEBServerManager::GET); + sab.getWebServerManager().addApiRoutine("/sab/web/apitester", &(apiTesterApi), NULL); + sab.getWebServerManager().addApiRoutine("/sab/view/next", &(nextViewApi), &sab, WEBServerManager::GET); + sab.getWebServerManager().addApiRoutine("/sab/view", &(ViewByUIDApi), &sab, WEBServerManager::GET); + sab.getWebServerManager().addApiRoutine("/sab/rtc/get/datetime", &(rtcGetTimeApi), &sab, WEBServerManager::GET); + sab.getWebServerManager().addApiRoutine("/sab/rtc/set/datetime", &(rtcSetTimeApi), &sab, WEBServerManager::GET); + sab.getWebServerManager().addApiRoutine("/sab/sdcard/unmount", &(sdCardUnmountApi), &sab, WEBServerManager::GET); + sab.getWebServerManager().addApiRoutine("/sab/sdcard/mount", &(sdCardMountApi), &sab, WEBServerManager::GET); + sab.getWebServerManager().addApiRoutine("/esp/restart", &(espRestartApi), NULL, WEBServerManager::GET); + sab.getWebServerManager().addApiRoutine("/esp/reset", &(espResetApi), NULL, WEBServerManager::GET); Serial.println("End setup"); } diff --git a/src/app/definition.h b/src/app/definition.h index acfa705..7148e3a 100644 --- a/src/app/definition.h +++ b/src/app/definition.h @@ -67,6 +67,7 @@ typedef struct powerInfo PowerType powerType; unsigned char level; }PowerInfo; - + +char *addChar(char *pointer, const char character); #endif //DEFINITION_H