diff --git a/src/AutoConnect.cpp b/src/AutoConnect.cpp index 471397e..982f403 100644 --- a/src/AutoConnect.cpp +++ b/src/AutoConnect.cpp @@ -3,7 +3,7 @@ * @file AutoConnect.cpp * @author hieromon@gmail.com * @version 1.2.0 - * @date 2020-04-23 + * @date 2020-04-24 * @copyright MIT license. */ @@ -438,6 +438,66 @@ void AutoConnect::join(AutoConnectAuxVT auxVector) { join(aux.get()); } +/** + * Creates an AutoConnectAux dynamically with the specified URI and + * integrates it into the menu. Returns false if a menu item with + * the same URI already exists. + * @param uri An uri of a new item to add + * @param title Title of the menu item + * @return true Added + * @return false The same item has existed. + */ +bool AutoConnect::addMenuItem(const String& uri, const String& title) { + AutoConnectAux* reg = aux(uri); + if (!reg) { + reg = new AutoConnectAux(uri, title); + join(*reg); + return true; + } + return false; +} + +/** + * Creates an AutoConnectAux dynamically with the specified URI and + * integrates it into the menu. It will register the request handler + * for the WebServer after the addMenuItem works. It has similar + * efficacy to calling addMenuItem and WebSever::on at once. + * @param uri An uri of a new item to add + * @param title Title of the menu item + * @param handler Function of the request handler for WebServer class + * @return true Added + * @return false The same item has existed. + */ +bool AutoConnect::addMenuItem(const String& uri, const String& title, WebServerClass::THandlerFunction handler) { + if (_webServer) { + if (addMenuItem(uri, title)) { + _webServer->on(uri, handler); + return true; + } + } + return false; +} + +/** + * Release a AutoConnectAux from the portal. + * @param uri An uri of the AutoConnectAux should be released + * @return true Specified AUX has released + * @return false Specified AUX not registered + */ +AutoConnectAux* AutoConnect::release(const String &uri) { + AutoConnectAux** self = &_aux; + while (*self) { + if (!strcmp((*self)->uri(), uri.c_str())) { + AC_DBG("%s released\n", (*self)->uri()); + AutoConnectAux* ref = *self; + *self = (*self)->_next; + return ref; + } + self = &((*self)->_next); + } + return nullptr; +} + /** * Starts Web server for AutoConnect service. */ @@ -675,7 +735,7 @@ void AutoConnect::onNotFound(WebServerClass::THandlerFunction fn) { * Load current available credential * @param ssid A pointer to the buffer that SSID should be stored. * @param password A pointer to the buffer that password should be stored. - * @param priciple WiFi connection principle. + * @param principle WiFi connection principle. * @param excludeCurrent Skip loading the current SSID. * @return true Current SSID and password returned. * @return false There is no available SSID. diff --git a/src/AutoConnect.h b/src/AutoConnect.h index 91d27d9..db21019 100644 --- a/src/AutoConnect.h +++ b/src/AutoConnect.h @@ -3,7 +3,7 @@ * @file AutoConnect.h * @author hieromon@gmail.com * @version 1.2.0 - * @date 2020-04-22 + * @date 2020-04-24 * @copyright MIT license. */ @@ -218,22 +218,25 @@ class AutoConnect { AutoConnect(WebServerClass& webServer); ~AutoConnect(); AutoConnectAux* aux(const String& uri) const; - bool config(AutoConnectConfig& Config); - bool config(const char* ap, const char* password = nullptr); - void home(const String& uri); + AutoConnectAux* release(const String& uri); bool begin(void); bool begin(const char* ssid, const char* passphrase = nullptr, unsigned long timeout = AUTOCONNECT_TIMEOUT); + bool config(AutoConnectConfig& Config); + bool config(const char* ap, const char* password = nullptr); void end(void); + uint16_t getEEPROMUsedSize(void); void handleClient(void); void handleRequest(void); + void home(const String& uri); WebServerClass& host(void); void join(AutoConnectAux& aux); void join(AutoConnectAuxVT auxVector); bool on(const String& uri, const AuxHandlerFunctionT handler, AutoConnectExitOrder_t order = AC_EXIT_AHEAD); String where(void) const { return _auxUri; } + bool addMenuItem(const String& uri, const String& title); + bool addMenuItem(const String& uri, const String& title, WebServerClass::THandlerFunction handler); inline void enableMenu(const uint16_t items) { _apConfig.menuItems |= items; } inline void disableMenu(const uint16_t items) { _apConfig.menuItems &= (0xffff ^ items); } - uint16_t getEEPROMUsedSize(void); /** For AutoConnectAux described in JSON */ #ifdef AUTOCONNECT_USE_JSON