From 70228adb3d178b996f256596bb3f4acbb20e50b9 Mon Sep 17 00:00:00 2001 From: Luc Date: Mon, 25 Jan 2016 23:38:26 +0800 Subject: [PATCH] Code formating using astyle --style=otbs *.h *.cpp *.ino To get nice looking code with a minimum effort --- esp8266/LinkedList.h | 448 ++-- esp8266/command.cpp | 430 ++-- esp8266/command.h | 20 +- esp8266/config.cpp | 642 ++--- esp8266/config.h | 4 +- esp8266/esp8266.ino | 272 ++- esp8266/storestrings.cpp | 160 +- esp8266/storestrings.h | 54 +- esp8266/webinterface.cpp | 4924 +++++++++++++++++++------------------- esp8266/webinterface.h | 74 +- esp8266/wifi.cpp | 401 ++-- esp8266/wifi.h | 42 +- 12 files changed, 3832 insertions(+), 3639 deletions(-) diff --git a/esp8266/LinkedList.h b/esp8266/LinkedList.h index d9139520..d90336f9 100644 --- a/esp8266/LinkedList.h +++ b/esp8266/LinkedList.h @@ -15,83 +15,83 @@ #define LinkedList_h template -struct ListNode -{ - T data; - ListNode *next; +struct ListNode { + T data; + ListNode *next; }; template -class LinkedList{ +class LinkedList +{ protected: - int _size; - ListNode *root; - ListNode *last; + int _size; + ListNode *root; + ListNode *last; - // Helps "get" method, by saving last position - ListNode *lastNodeGot; - int lastIndexGot; - // isCached should be set to FALSE - // everytime the list suffer changes - bool isCached; + // Helps "get" method, by saving last position + ListNode *lastNodeGot; + int lastIndexGot; + // isCached should be set to FALSE + // everytime the list suffer changes + bool isCached; - ListNode* getNode(int index); + ListNode* getNode(int index); public: - LinkedList(); - ~LinkedList(); + LinkedList(); + ~LinkedList(); - /* - Returns current size of LinkedList - */ - virtual int size(); - /* - Adds a T object in the specified index; - Unlink and link the LinkedList correcly; - Increment _size - */ - virtual bool add(int index, T); - /* - Adds a T object in the end of the LinkedList; - Increment _size; - */ - virtual bool add(T); - /* - Adds a T object in the start of the LinkedList; - Increment _size; - */ - virtual bool unshift(T); - /* - Set the object at index, with T; - Increment _size; - */ - virtual bool set(int index, T); - /* - Remove object at index; - If index is not reachable, returns false; - else, decrement _size - */ - virtual T remove(int index); - /* - Remove last object; - */ - virtual T pop(); - /* - Remove first object; - */ - virtual T shift(); - /* - Get the index'th element on the list; - Return Element if accessible, - else, return false; - */ - virtual T get(int index); + /* + Returns current size of LinkedList + */ + virtual int size(); + /* + Adds a T object in the specified index; + Unlink and link the LinkedList correcly; + Increment _size + */ + virtual bool add(int index, T); + /* + Adds a T object in the end of the LinkedList; + Increment _size; + */ + virtual bool add(T); + /* + Adds a T object in the start of the LinkedList; + Increment _size; + */ + virtual bool unshift(T); + /* + Set the object at index, with T; + Increment _size; + */ + virtual bool set(int index, T); + /* + Remove object at index; + If index is not reachable, returns false; + else, decrement _size + */ + virtual T remove(int index); + /* + Remove last object; + */ + virtual T pop(); + /* + Remove first object; + */ + virtual T shift(); + /* + Get the index'th element on the list; + Return Element if accessible, + else, return false; + */ + virtual T get(int index); - /* - Clear the entire array - */ - virtual void clear(); + /* + Clear the entire array + */ + virtual void clear(); }; @@ -99,29 +99,28 @@ public: template LinkedList::LinkedList() { - root=NULL; - last=NULL; - _size=0; + root=NULL; + last=NULL; + _size=0; - lastNodeGot = root; - lastIndexGot = 0; - isCached = false; + lastNodeGot = root; + lastIndexGot = 0; + isCached = false; } // Clear Nodes and free Memory template LinkedList::~LinkedList() { - ListNode* tmp; - while(root!=NULL) - { - tmp=root; - root=root->next; - delete tmp; - } - last = NULL; - _size=0; - isCached = false; + ListNode* tmp; + while(root!=NULL) { + tmp=root; + root=root->next; + delete tmp; + } + last = NULL; + _size=0; + isCached = false; } /* @@ -129,196 +128,213 @@ LinkedList::~LinkedList() */ template -ListNode* LinkedList::getNode(int index){ +ListNode* LinkedList::getNode(int index) +{ - int _pos = 0; - ListNode* current = root; + int _pos = 0; + ListNode* current = root; - // Check if the node trying to get is - // immediatly AFTER the previous got one - if(isCached && lastIndexGot <= index){ - _pos = lastIndexGot; - current = lastNodeGot; - } + // Check if the node trying to get is + // immediatly AFTER the previous got one + if(isCached && lastIndexGot <= index) { + _pos = lastIndexGot; + current = lastNodeGot; + } - while(_pos < index && current){ - current = current->next; + while(_pos < index && current) { + current = current->next; - _pos++; - } + _pos++; + } - // Check if the object index got is the same as the required - if(_pos == index){ - isCached = true; - lastIndexGot = index; - lastNodeGot = current; + // Check if the object index got is the same as the required + if(_pos == index) { + isCached = true; + lastIndexGot = index; + lastNodeGot = current; - return current; - } + return current; + } - return NULL; + return NULL; } template -int LinkedList::size(){ - return _size; +int LinkedList::size() +{ + return _size; } template -bool LinkedList::add(int index, T _t){ +bool LinkedList::add(int index, T _t) +{ - if(index >= _size) - return add(_t); + if(index >= _size) { + return add(_t); + } - if(index == 0) - return unshift(_t); + if(index == 0) { + return unshift(_t); + } - ListNode *tmp = new ListNode(), - *_prev = getNode(index-1); - tmp->data = _t; - tmp->next = _prev->next; - _prev->next = tmp; + ListNode *tmp = new ListNode(), + *_prev = getNode(index-1); + tmp->data = _t; + tmp->next = _prev->next; + _prev->next = tmp; - _size++; - isCached = false; + _size++; + isCached = false; - return true; + return true; } template -bool LinkedList::add(T _t){ +bool LinkedList::add(T _t) +{ - ListNode *tmp = new ListNode(); - tmp->data = _t; - tmp->next = NULL; - - if(root){ - // Already have elements inserted - last->next = tmp; - last = tmp; - }else{ - // First element being inserted - root = tmp; - last = tmp; - } + ListNode *tmp = new ListNode(); + tmp->data = _t; + tmp->next = NULL; - _size++; - isCached = false; + if(root) { + // Already have elements inserted + last->next = tmp; + last = tmp; + } else { + // First element being inserted + root = tmp; + last = tmp; + } - return true; + _size++; + isCached = false; + + return true; } template -bool LinkedList::unshift(T _t){ +bool LinkedList::unshift(T _t) +{ - if(_size == 0) - return add(_t); + if(_size == 0) { + return add(_t); + } - ListNode *tmp = new ListNode(); - tmp->next = root; - tmp->data = _t; - root = tmp; - - _size++; - isCached = false; - - return true; + ListNode *tmp = new ListNode(); + tmp->next = root; + tmp->data = _t; + root = tmp; + + _size++; + isCached = false; + + return true; } template -bool LinkedList::set(int index, T _t){ - // Check if index position is in bounds - if(index < 0 || index >= _size) - return false; +bool LinkedList::set(int index, T _t) +{ + // Check if index position is in bounds + if(index < 0 || index >= _size) { + return false; + } - getNode(index)->data = _t; - return true; + getNode(index)->data = _t; + return true; } template -T LinkedList::pop(){ - if(_size <= 0) - return T(); - - isCached = false; +T LinkedList::pop() +{ + if(_size <= 0) { + return T(); + } - if(_size >= 2){ - ListNode *tmp = getNode(_size - 2); - T ret = tmp->next->data; - delete(tmp->next); - tmp->next = NULL; - last = tmp; - _size--; - return ret; - }else{ - // Only one element left on the list - T ret = root->data; - delete(root); - root = NULL; - last = NULL; - _size = 0; - return ret; - } + isCached = false; + + if(_size >= 2) { + ListNode *tmp = getNode(_size - 2); + T ret = tmp->next->data; + delete(tmp->next); + tmp->next = NULL; + last = tmp; + _size--; + return ret; + } else { + // Only one element left on the list + T ret = root->data; + delete(root); + root = NULL; + last = NULL; + _size = 0; + return ret; + } } template -T LinkedList::shift(){ - if(_size <= 0) - return T(); +T LinkedList::shift() +{ + if(_size <= 0) { + return T(); + } - if(_size > 1){ - ListNode *_next = root->next; - T ret = root->data; - delete(root); - root = _next; - _size --; - isCached = false; + if(_size > 1) { + ListNode *_next = root->next; + T ret = root->data; + delete(root); + root = _next; + _size --; + isCached = false; - return ret; - }else{ - // Only one left, then pop() - return pop(); - } + return ret; + } else { + // Only one left, then pop() + return pop(); + } } template -T LinkedList::remove(int index){ - if (index < 0 || index >= _size) - { - return T(); - } +T LinkedList::remove(int index) +{ + if (index < 0 || index >= _size) { + return T(); + } - if(index == 0) - return shift(); - - if (index == _size-1) - { - return pop(); - } + if(index == 0) { + return shift(); + } - ListNode *tmp = getNode(index - 1); - ListNode *toDelete = tmp->next; - T ret = toDelete->data; - tmp->next = tmp->next->next; - delete(toDelete); - _size--; - isCached = false; - return ret; + if (index == _size-1) { + return pop(); + } + + ListNode *tmp = getNode(index - 1); + ListNode *toDelete = tmp->next; + T ret = toDelete->data; + tmp->next = tmp->next->next; + delete(toDelete); + _size--; + isCached = false; + return ret; } template -T LinkedList::get(int index){ - ListNode *tmp = getNode(index); +T LinkedList::get(int index) +{ + ListNode *tmp = getNode(index); - return (tmp ? tmp->data : T()); + return (tmp ? tmp->data : T()); } template -void LinkedList::clear(){ - while(size() > 0) - shift(); +void LinkedList::clear() +{ + while(size() > 0) { + shift(); + } } #endif diff --git a/esp8266/command.cpp b/esp8266/command.cpp index 8f599e4d..73ab1de8 100644 --- a/esp8266/command.cpp +++ b/esp8266/command.cpp @@ -1,8 +1,8 @@ -/* +/* command.cpp - esp8266 configuration class Copyright (c) 2014 Luc Lebosse. All rights reserved. - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either @@ -32,240 +32,248 @@ String COMMAND::buffer_tcp; void COMMAND::execute_command(int cmd,String cmd_params) { - //manage parameters - - switch(cmd) - { - byte mode; - case 800: - Serial.println("\nCommand received"); - break; - case 100: - if(!CONFIG::write_string(EP_SSID,cmd_params.c_str()))Serial.println("\nError"); - else Serial.println("\nOk"); - break; - case 101: - if(!CONFIG::write_string(EP_PASSWORD,cmd_params.c_str()))Serial.println("\nError"); - else Serial.println("\nOk"); - break; - case 103: + //manage parameters - if (cmd_params=="STA")mode = CLIENT_MODE; - else mode=AP_MODE; - if(!CONFIG::write_byte(EP_WIFI_MODE,mode))Serial.println("\nError"); - else Serial.println("\nOk"); - break; - case 104: - if (cmd_params=="STATIC")mode = STATIC_IP_MODE; - else mode=DHCP_MODE; - if(!CONFIG::write_byte(EP_IP_MODE,mode))Serial.println("\nError"); - else Serial.println("\nOk"); - break; - case 111: - { - String currentIP ; - if (wifi_get_opmode()==WIFI_STA)currentIP=WiFi.localIP().toString(); - else currentIP=WiFi.softAPIP().toString(); - Serial.print("\n\r"); - Serial.print(cmd_params); - Serial.println(currentIP); - Serial.print("\r\n"); - } - break; - case 444: - if (cmd_params=="RESET") - { - CONFIG::reset_config(); - } - if (cmd_params=="SAFEMODE") - { - wifi_config.Safe_Setup(); - } - if (cmd_params=="CONFIG") - { - CONFIG::print_config(); - } - break; - case 888: - if (cmd_params=="RESTART") - { - Serial.print("\r"); - Serial.print(cmd_params); - web_interface->restartmodule=true; - Serial.print("\r\n"); - } - break; - case 999: - if (cmd_params=="ERROR") - { - web_interface->error_msg.clear(); - } - else if (cmd_params=="INFO") - { - web_interface->info_msg.clear(); - } - else if (cmd_params=="STATUS") - { - web_interface->status_msg.clear(); - } - else if (cmd_params=="ALL") - { - web_interface->error_msg.clear(); - web_interface->status_msg.clear(); - web_interface->info_msg.clear(); - } - break; - //default: + switch(cmd) { + byte mode; + case 800: + Serial.println("\nCommand received"); + break; + case 100: + if(!CONFIG::write_string(EP_SSID,cmd_params.c_str())) { + Serial.println("\nError"); + } else { + Serial.println("\nOk"); + } + break; + case 101: + if(!CONFIG::write_string(EP_PASSWORD,cmd_params.c_str())) { + Serial.println("\nError"); + } else { + Serial.println("\nOk"); + } + break; + case 103: - } + if (cmd_params=="STA") { + mode = CLIENT_MODE; + } else { + mode=AP_MODE; + } + if(!CONFIG::write_byte(EP_WIFI_MODE,mode)) { + Serial.println("\nError"); + } else { + Serial.println("\nOk"); + } + break; + case 104: + if (cmd_params=="STATIC") { + mode = STATIC_IP_MODE; + } else { + mode=DHCP_MODE; + } + if(!CONFIG::write_byte(EP_IP_MODE,mode)) { + Serial.println("\nError"); + } else { + Serial.println("\nOk"); + } + break; + case 111: { + String currentIP ; + if (wifi_get_opmode()==WIFI_STA) { + currentIP=WiFi.localIP().toString(); + } else { + currentIP=WiFi.softAPIP().toString(); + } + Serial.print("\n\r"); + Serial.print(cmd_params); + Serial.println(currentIP); + Serial.print("\r\n"); + } + break; + case 444: + if (cmd_params=="RESET") { + CONFIG::reset_config(); + } + if (cmd_params=="SAFEMODE") { + wifi_config.Safe_Setup(); + } + if (cmd_params=="CONFIG") { + CONFIG::print_config(); + } + break; + case 888: + if (cmd_params=="RESTART") { + Serial.print("\r"); + Serial.print(cmd_params); + web_interface->restartmodule=true; + Serial.print("\r\n"); + } + break; + case 999: + if (cmd_params=="ERROR") { + web_interface->error_msg.clear(); + } else if (cmd_params=="INFO") { + web_interface->info_msg.clear(); + } else if (cmd_params=="STATUS") { + web_interface->status_msg.clear(); + } else if (cmd_params=="ALL") { + web_interface->error_msg.clear(); + web_interface->status_msg.clear(); + web_interface->info_msg.clear(); + } + break; + //default: + + } } void COMMAND::check_command(String buffer) { - String ESP_Command; - static bool bfileslist=false; - static uint32_t start_list=0; - if (!bfileslist) - { - int filesstart = buffer.indexOf("Begin file list"); - if (filesstart>-1) - { - Serial.print("Starting"); - start_list=system_get_time(); - bfileslist=true; - web_interface->fileslist.clear(); - } - int ESPpos = buffer.indexOf("[ESP"); - int Tpos = buffer.indexOf("T:"); - int Xpos = buffer.indexOf("X:"); - int Ypos = buffer.indexOf("Y:"); - int Zpos = buffer.indexOf("Z:"); - int Speedpos = buffer.indexOf("SpeedMultiply:"); - int Flowpos = buffer.indexOf("FlowMultiply:"); - int Errorpos= buffer.indexOf("Error:"); - int Infopos= buffer.indexOf("Info:"); - int Statuspos= buffer.indexOf("Status:"); - if (ESPpos>-1) - {//is there the second part? - int ESPpos2 = buffer.indexOf("]",ESPpos); - if (ESPpos2>-1) - { //Split in command and parameters - String cmd_part1=buffer.substring(ESPpos+4,ESPpos2); - String cmd_part2=""; - //is there space for parameters? - if (ESPpos2-1) - { - //look for valid temperature answer - int slashpos = buffer.indexOf(" /",Tpos); - int spacepos = buffer.indexOf(" ",slashpos+1); - //if match mask T:xxx.xx /xxx.xx - if(spacepos-Tpos < 17) - { - web_interface->answer4M105=buffer; //do not interprete just need when requested so store it - web_interface->last_temp=system_get_time(); - } - } - //Position of axis - if (Xpos>-1 && Ypos>-1 && Zpos>-1)web_interface->answer4M114=buffer; - //Speed - if (Speedpos>-1)web_interface->answer4M220=buffer.substring(Speedpos+14); - //Flow - if (Flowpos>-1)web_interface->answer4M221=buffer.substring(Flowpos+13); - //Error - if (Errorpos>-1 && !(buffer.indexOf("Format error")!=-1 || buffer.indexOf("wait")==Errorpos+6) )(web_interface->error_msg).add(buffer.substring(Errorpos+6).c_str()); - //Info - if (Infopos>-1)(web_interface->info_msg).add(buffer.substring(Infopos+5).c_str()); - //Status - if (Statuspos>-1)(web_interface->status_msg).add(buffer.substring(Statuspos+7).c_str()); - } - else - { - if ((system_get_time()-start_list)>30000000) //timeout in case of problem - { - bfileslist=false; - Serial.print("time out"); - } - else - { - if (buffer.indexOf("End file list")>-1) - { - Serial.print("Ending"); - bfileslist=false; - } - else - { - Serial.print(buffer); - web_interface->fileslist.add(buffer); - } - } - - } + String ESP_Command; + static bool bfileslist=false; + static uint32_t start_list=0; + if (!bfileslist) { + int filesstart = buffer.indexOf("Begin file list"); + if (filesstart>-1) { + Serial.print("Starting"); + start_list=system_get_time(); + bfileslist=true; + web_interface->fileslist.clear(); + } + int ESPpos = buffer.indexOf("[ESP"); + int Tpos = buffer.indexOf("T:"); + int Xpos = buffer.indexOf("X:"); + int Ypos = buffer.indexOf("Y:"); + int Zpos = buffer.indexOf("Z:"); + int Speedpos = buffer.indexOf("SpeedMultiply:"); + int Flowpos = buffer.indexOf("FlowMultiply:"); + int Errorpos= buffer.indexOf("Error:"); + int Infopos= buffer.indexOf("Info:"); + int Statuspos= buffer.indexOf("Status:"); + if (ESPpos>-1) { + //is there the second part? + int ESPpos2 = buffer.indexOf("]",ESPpos); + if (ESPpos2>-1) { + //Split in command and parameters + String cmd_part1=buffer.substring(ESPpos+4,ESPpos2); + String cmd_part2=""; + //is there space for parameters? + if (ESPpos2-1) { + //look for valid temperature answer + int slashpos = buffer.indexOf(" /",Tpos); + int spacepos = buffer.indexOf(" ",slashpos+1); + //if match mask T:xxx.xx /xxx.xx + if(spacepos-Tpos < 17) { + web_interface->answer4M105=buffer; //do not interprete just need when requested so store it + web_interface->last_temp=system_get_time(); + } + } + //Position of axis + if (Xpos>-1 && Ypos>-1 && Zpos>-1) { + web_interface->answer4M114=buffer; + } + //Speed + if (Speedpos>-1) { + web_interface->answer4M220=buffer.substring(Speedpos+14); + } + //Flow + if (Flowpos>-1) { + web_interface->answer4M221=buffer.substring(Flowpos+13); + } + //Error + if (Errorpos>-1 && !(buffer.indexOf("Format error")!=-1 || buffer.indexOf("wait")==Errorpos+6) ) { + (web_interface->error_msg).add(buffer.substring(Errorpos+6).c_str()); + } + //Info + if (Infopos>-1) { + (web_interface->info_msg).add(buffer.substring(Infopos+5).c_str()); + } + //Status + if (Statuspos>-1) { + (web_interface->status_msg).add(buffer.substring(Statuspos+7).c_str()); + } + } else { + if ((system_get_time()-start_list)>30000000) { //timeout in case of problem + bfileslist=false; + Serial.print("time out"); + } else { + if (buffer.indexOf("End file list")>-1) { + Serial.print("Ending"); + bfileslist=false; + } else { + Serial.print(buffer); + web_interface->fileslist.add(buffer); + } + } + + } } //read a buffer in an array void COMMAND::read_buffer_serial(uint8_t *b, size_t len) { -for (long i; i< len;i++) - { - read_buffer_serial(*b); - *b++; - } + for (long i; i< len; i++) { + read_buffer_serial(*b); + *b++; + } } //read buffer as char void COMMAND::read_buffer_tcp(uint8_t b) { -static bool previous_was_char=false; + static bool previous_was_char=false; //to ensure it is continuous string, no char separated by binaries -if (!previous_was_char)buffer_tcp=""; + if (!previous_was_char) { + buffer_tcp=""; + } //it is a char so add it to buffer -if (isPrintable(b)) - { - previous_was_char=true; - buffer_tcp+=char(b); - } -else - { - previous_was_char=false; //next call will reset the buffer - } + if (isPrintable(b)) { + previous_was_char=true; + buffer_tcp+=char(b); + } else { + previous_was_char=false; //next call will reset the buffer + } //this is not printable but end of command check if need to handle it -if (b==13 ||b==10) - {//Minimum is something like M10 so 3 char - if (buffer_tcp.length()>3) - check_command(buffer_tcp); - } + if (b==13 ||b==10) { + //Minimum is something like M10 so 3 char + if (buffer_tcp.length()>3) { + check_command(buffer_tcp); + } + } } //read buffer as char void COMMAND::read_buffer_serial(uint8_t b) { -static bool previous_was_char=false; + static bool previous_was_char=false; //to ensure it is continuous string, no char separated by binaries -if (!previous_was_char)buffer_serial=""; + if (!previous_was_char) { + buffer_serial=""; + } //it is a char so add it to buffer -if (isPrintable(b)) - { - previous_was_char=true; - buffer_serial+=char(b); - } -else - { - previous_was_char=false; //next call will reset the buffer - } + if (isPrintable(b)) { + previous_was_char=true; + buffer_serial+=char(b); + } else { + previous_was_char=false; //next call will reset the buffer + } //this is not printable but end of command check if need to handle it -if (b==13 ||b==10) - {//Minimum is something like M10 so 3 char - if (buffer_serial.length()>3) - check_command(buffer_serial); - } + if (b==13 ||b==10) { + //Minimum is something like M10 so 3 char + if (buffer_serial.length()>3) { + check_command(buffer_serial); + } + } } diff --git a/esp8266/command.h b/esp8266/command.h index 126ae72e..a7d89992 100644 --- a/esp8266/command.h +++ b/esp8266/command.h @@ -1,8 +1,8 @@ -/* +/* command.h - esp8266 configuration class Copyright (c) 2014 Luc Lebosse. All rights reserved. - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either @@ -24,14 +24,14 @@ class COMMAND { - public: - static String buffer_serial; - static String buffer_tcp; - static void read_buffer_serial(uint8_t *b, size_t len); - static void read_buffer_serial(uint8_t b); - static void read_buffer_tcp(uint8_t b); - static void check_command(String buffer); - static void execute_command(int cmd,String cmd_params); +public: + static String buffer_serial; + static String buffer_tcp; + static void read_buffer_serial(uint8_t *b, size_t len); + static void read_buffer_serial(uint8_t b); + static void read_buffer_tcp(uint8_t b); + static void check_command(String buffer); + static void execute_command(int cmd,String cmd_params); }; diff --git a/esp8266/config.cpp b/esp8266/config.cpp index 0c4185cd..9069d724 100644 --- a/esp8266/config.cpp +++ b/esp8266/config.cpp @@ -1,8 +1,8 @@ -/* +/* CONFIG.CPP- esp8266 configuration class Copyright (c) 2014 Luc Lebosse. All rights reserved. - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either @@ -24,332 +24,416 @@ extern "C" { #include "user_interface.h" } -//read a string -//a string is multibyte + \0, this is won't work if 1 char is multibyte like chinese char +//read a string +//a string is multibyte + \0, this is won't work if 1 char is multibyte like chinese char bool CONFIG::read_string(int pos, char byte_buffer[], int size_max) { - //check if parameters are acceptable - if (size_max==0 || pos+size_max+1 > EEPROM_SIZE || byte_buffer== NULL)return false; - EEPROM.begin(EEPROM_SIZE); - byte b = 13; // non zero for the while loop below - int i=0; + //check if parameters are acceptable + if (size_max==0 || pos+size_max+1 > EEPROM_SIZE || byte_buffer== NULL) { + return false; + } + EEPROM.begin(EEPROM_SIZE); + byte b = 13; // non zero for the while loop below + int i=0; - //read until max size is reached or \0 is found - while (i < size_max && b != 0) - { - b = EEPROM.read(pos+i); - byte_buffer[i]=b; - i++; - } - - // Be sure there is a 0 at the end. - if (b!=0)byte_buffer[i-1]=0x00; - EEPROM.end(); - - return true; + //read until max size is reached or \0 is found + while (i < size_max && b != 0) { + b = EEPROM.read(pos+i); + byte_buffer[i]=b; + i++; + } + + // Be sure there is a 0 at the end. + if (b!=0) { + byte_buffer[i-1]=0x00; + } + EEPROM.end(); + + return true; } bool CONFIG::read_string(int pos, String & sbuffer, int size_max) { - //check if parameters are acceptable - if (size_max==0 || pos+size_max+1 > EEPROM_SIZE )return false; - byte b = 13; // non zero for the while loop below - int i=0; - sbuffer=""; + //check if parameters are acceptable + if (size_max==0 || pos+size_max+1 > EEPROM_SIZE ) { + return false; + } + byte b = 13; // non zero for the while loop below + int i=0; + sbuffer=""; - EEPROM.begin(EEPROM_SIZE); - //read until max size is reached or \0 is found - while (i < size_max && b != 0) - { - b = EEPROM.read(pos+i); - sbuffer+=char(b); - i++; - } - EEPROM.end(); + EEPROM.begin(EEPROM_SIZE); + //read until max size is reached or \0 is found + while (i < size_max && b != 0) { + b = EEPROM.read(pos+i); + sbuffer+=char(b); + i++; + } + EEPROM.end(); - return true; + return true; } //read a buffer of size_buffer bool CONFIG::read_buffer(int pos, byte byte_buffer[], int size_buffer) { - //check if parameters are acceptable - if (size_buffer==0 || pos+size_buffer > EEPROM_SIZE || byte_buffer== NULL)return false; - int i=0; - EEPROM.begin(EEPROM_SIZE); - //read until max size is reached - while (i EEPROM_SIZE || byte_buffer== NULL) { + return false; + } + int i=0; + EEPROM.begin(EEPROM_SIZE); + //read until max size is reached + while (i EEPROM_SIZE)return false; - EEPROM.begin(EEPROM_SIZE); - value[0] = EEPROM.read(pos); -EEPROM.end(); - return true; + //check if parameters are acceptable + if (pos+1 > EEPROM_SIZE) { + return false; + } + EEPROM.begin(EEPROM_SIZE); + value[0] = EEPROM.read(pos); + EEPROM.end(); + return true; } bool CONFIG::write_string(int pos, const __FlashStringHelper *str) { - String stmp = str; - return write_string(pos,stmp.c_str()); + String stmp = str; + return write_string(pos,stmp.c_str()); } //write a string (array of byte with a 0x00 at the end) bool CONFIG::write_string(int pos, const char * byte_buffer) { - int size_buffer; - size_buffer= strlen(byte_buffer); - //check if parameters are acceptable - if (size_buffer==0 || pos+size_buffer+1 > EEPROM_SIZE || byte_buffer== NULL)return false; - //copy the value(s) - EEPROM.begin(EEPROM_SIZE); - for (int i = 0; i < size_buffer; i++) { - EEPROM.write(pos + i, byte_buffer[i]); - } - - //0 terminal - EEPROM.write(pos + size_buffer, 0x00); - EEPROM.commit(); - EEPROM.end(); - return true; + int size_buffer; + size_buffer= strlen(byte_buffer); + //check if parameters are acceptable + if (size_buffer==0 || pos+size_buffer+1 > EEPROM_SIZE || byte_buffer== NULL) { + return false; + } + //copy the value(s) + EEPROM.begin(EEPROM_SIZE); + for (int i = 0; i < size_buffer; i++) { + EEPROM.write(pos + i, byte_buffer[i]); + } + + //0 terminal + EEPROM.write(pos + size_buffer, 0x00); + EEPROM.commit(); + EEPROM.end(); + return true; } -//write a buffer +//write a buffer bool CONFIG::write_buffer(int pos, const byte * byte_buffer, int size_buffer) { - //check if parameters are acceptable - if (size_buffer==0 || pos+size_buffer > EEPROM_SIZE || byte_buffer== NULL)return false; - EEPROM.begin(EEPROM_SIZE); - //copy the value(s) - for (int i = 0; i < size_buffer; i++) { - EEPROM.write(pos + i, byte_buffer[i]); - } - EEPROM.commit(); - EEPROM.end(); - return true; + //check if parameters are acceptable + if (size_buffer==0 || pos+size_buffer > EEPROM_SIZE || byte_buffer== NULL) { + return false; + } + EEPROM.begin(EEPROM_SIZE); + //copy the value(s) + for (int i = 0; i < size_buffer; i++) { + EEPROM.write(pos + i, byte_buffer[i]); + } + EEPROM.commit(); + EEPROM.end(); + return true; } //read a flag / byte bool CONFIG::write_byte(int pos, const byte value) { - //check if parameters are acceptable - if (pos+1 > EEPROM_SIZE)return false; - EEPROM.begin(EEPROM_SIZE); - EEPROM.write(pos, value); - EEPROM.commit(); - EEPROM.end(); - return true; + //check if parameters are acceptable + if (pos+1 > EEPROM_SIZE) { + return false; + } + EEPROM.begin(EEPROM_SIZE); + EEPROM.write(pos, value); + EEPROM.commit(); + EEPROM.end(); + return true; } bool CONFIG::reset_config() { - if(!CONFIG::write_byte(EP_WIFI_MODE,DEFAULT_WIFI_MODE))return false; - if(!CONFIG::write_string(EP_SSID,FPSTR(DEFAULT_SSID)))return false; - if(!CONFIG::write_string(EP_PASSWORD,FPSTR(DEFAULT_PASSWORD)))return false; - if(!CONFIG::write_byte(EP_IP_MODE,DEFAULT_IP_MODE))return false; - if(!CONFIG::write_buffer(EP_IP_VALUE,DEFAULT_IP_VALUE,IP_LENGTH))return false; - if(!CONFIG::write_buffer(EP_MASK_VALUE,DEFAULT_MASK_VALUE,IP_LENGTH))return false; - if(!CONFIG::write_buffer(EP_GATEWAY_VALUE,DEFAULT_GATEWAY_VALUE,IP_LENGTH))return false; - if(!CONFIG::write_buffer(EP_BAUD_RATE,(const byte *)&DEFAULT_BAUD_RATE,INTEGER_LENGTH))return false; - if(!CONFIG::write_byte(EP_PHY_MODE,DEFAULT_PHY_MODE))return false; - if(!CONFIG::write_byte(EP_SLEEP_MODE,DEFAULT_SLEEP_MODE))return false; - if(!CONFIG::write_byte(EP_CHANNEL,DEFAULT_CHANNEL))return false; - if(!CONFIG::write_byte(EP_AUTH_TYPE,DEFAULT_AUTH_TYPE))return false; - if(!CONFIG::write_byte(EP_SSID_VISIBLE,DEFAULT_SSID_VISIBLE))return false; - if(!CONFIG::write_buffer(EP_WEB_PORT,(const byte *)&DEFAULT_WEB_PORT,INTEGER_LENGTH))return false; - if(!CONFIG::write_buffer(EP_DATA_PORT,(const byte *)&DEFAULT_DATA_PORT,INTEGER_LENGTH))return false; - if(!CONFIG::write_byte(EP_REFRESH_PAGE_TIME,DEFAULT_REFRESH_PAGE_TIME))return false; - if(!CONFIG::write_string(EP_HOSTNAME,wifi_config.get_default_hostname()))return false; - if(!CONFIG::write_buffer(EP_XY_FEEDRATE,(const byte *)&DEFAULT_XY_FEEDRATE,INTEGER_LENGTH))return false; - if(!CONFIG::write_buffer(EP_Z_FEEDRATE,(const byte *)&DEFAULT_Z_FEEDRATE,INTEGER_LENGTH))return false; - if(!CONFIG::write_buffer(EP_E_FEEDRATE,(const byte *)&DEFAULT_E_FEEDRATE,INTEGER_LENGTH))return false; - if(!CONFIG::write_string(EP_ADMIN_PWD,FPSTR(DEFAULT_ADMIN)))return false; - return true; + if(!CONFIG::write_byte(EP_WIFI_MODE,DEFAULT_WIFI_MODE)) { + return false; + } + if(!CONFIG::write_string(EP_SSID,FPSTR(DEFAULT_SSID))) { + return false; + } + if(!CONFIG::write_string(EP_PASSWORD,FPSTR(DEFAULT_PASSWORD))) { + return false; + } + if(!CONFIG::write_byte(EP_IP_MODE,DEFAULT_IP_MODE)) { + return false; + } + if(!CONFIG::write_buffer(EP_IP_VALUE,DEFAULT_IP_VALUE,IP_LENGTH)) { + return false; + } + if(!CONFIG::write_buffer(EP_MASK_VALUE,DEFAULT_MASK_VALUE,IP_LENGTH)) { + return false; + } + if(!CONFIG::write_buffer(EP_GATEWAY_VALUE,DEFAULT_GATEWAY_VALUE,IP_LENGTH)) { + return false; + } + if(!CONFIG::write_buffer(EP_BAUD_RATE,(const byte *)&DEFAULT_BAUD_RATE,INTEGER_LENGTH)) { + return false; + } + if(!CONFIG::write_byte(EP_PHY_MODE,DEFAULT_PHY_MODE)) { + return false; + } + if(!CONFIG::write_byte(EP_SLEEP_MODE,DEFAULT_SLEEP_MODE)) { + return false; + } + if(!CONFIG::write_byte(EP_CHANNEL,DEFAULT_CHANNEL)) { + return false; + } + if(!CONFIG::write_byte(EP_AUTH_TYPE,DEFAULT_AUTH_TYPE)) { + return false; + } + if(!CONFIG::write_byte(EP_SSID_VISIBLE,DEFAULT_SSID_VISIBLE)) { + return false; + } + if(!CONFIG::write_buffer(EP_WEB_PORT,(const byte *)&DEFAULT_WEB_PORT,INTEGER_LENGTH)) { + return false; + } + if(!CONFIG::write_buffer(EP_DATA_PORT,(const byte *)&DEFAULT_DATA_PORT,INTEGER_LENGTH)) { + return false; + } + if(!CONFIG::write_byte(EP_REFRESH_PAGE_TIME,DEFAULT_REFRESH_PAGE_TIME)) { + return false; + } + if(!CONFIG::write_string(EP_HOSTNAME,wifi_config.get_default_hostname())) { + return false; + } + if(!CONFIG::write_buffer(EP_XY_FEEDRATE,(const byte *)&DEFAULT_XY_FEEDRATE,INTEGER_LENGTH)) { + return false; + } + if(!CONFIG::write_buffer(EP_Z_FEEDRATE,(const byte *)&DEFAULT_Z_FEEDRATE,INTEGER_LENGTH)) { + return false; + } + if(!CONFIG::write_buffer(EP_E_FEEDRATE,(const byte *)&DEFAULT_E_FEEDRATE,INTEGER_LENGTH)) { + return false; + } + if(!CONFIG::write_string(EP_ADMIN_PWD,FPSTR(DEFAULT_ADMIN))) { + return false; + } + return true; } void CONFIG::print_config() { - //use biggest size for buffer - char sbuf[MAX_PASSWORD_LENGTH+1]; - byte bbuf=0; - int ibuf=0; - if (CONFIG::read_byte(EP_WIFI_MODE, &bbuf )) - { - Serial.print(F("Mode: ")); - if (byte(bbuf) == CLIENT_MODE) Serial.println(F("Station")); - else if (byte(bbuf)==AP_MODE) Serial.println(F("Access Point")); - else Serial.println("???"); - } - else Serial.println(F("Error reading mode")); - - if (CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGTH)) - { - Serial.print(F("SSID: ")); - Serial.println(sbuf); - } - else Serial.println(F("Error reading SSID")); - //if (CONFIG::read_string(EP_PASSWORD, sbuf , MAX_PASSWORD_LENGTH))Serial.println(sbuf); - - if (CONFIG::read_byte(EP_IP_MODE, &bbuf )) - { - Serial.print(F("IP Mode: ")); - if (byte(bbuf)==STATIC_IP_MODE) Serial.println(F("Static")); - else if (byte(bbuf)==DHCP_MODE) Serial.println(F("DHCP")); - else Serial.println(F("???")); - } - else Serial.println(F("Error reading IP mode")); - - if (CONFIG::read_buffer(EP_IP_VALUE,(byte *)sbuf , IP_LENGTH)) - { - Serial.print(F("IP: ")); - Serial.println(wifi_config.ip2str((byte *)sbuf)); - } - else Serial.println(F("Error reading IP")); - - if (CONFIG::read_buffer(EP_MASK_VALUE, (byte *)sbuf , IP_LENGTH)) - { - Serial.print(F("Subnet: ")); - Serial.println(wifi_config.ip2str((byte *)sbuf)); - } - else Serial.println(F("Error reading subnet")); - - if (CONFIG::read_buffer(EP_GATEWAY_VALUE, (byte *)sbuf , IP_LENGTH)) - { - Serial.print(F("Gateway: ")); - Serial.println(wifi_config.ip2str((byte *)sbuf)); + //use biggest size for buffer + char sbuf[MAX_PASSWORD_LENGTH+1]; + byte bbuf=0; + int ibuf=0; + if (CONFIG::read_byte(EP_WIFI_MODE, &bbuf )) { + Serial.print(F("Mode: ")); + if (byte(bbuf) == CLIENT_MODE) { + Serial.println(F("Station")); + } else if (byte(bbuf)==AP_MODE) { + Serial.println(F("Access Point")); + } else { + Serial.println("???"); + } + } else { + Serial.println(F("Error reading mode")); } - else Serial.println(F("Error reading gateway")); - - if (CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&ibuf , INTEGER_LENGTH)) - { - Serial.print(F("Baud rate: ")); - Serial.println(ibuf); - } - else Serial.println(F("Error reading baud rate")); - - if (CONFIG::read_byte(EP_PHY_MODE, &bbuf )) - { - Serial.print(F("Phy mode: ")); - if (byte(bbuf)==PHY_MODE_11B) Serial.println(F("11b")); - else if (byte(bbuf)==PHY_MODE_11G) Serial.println(F("11g")); - else if (byte(bbuf)==PHY_MODE_11N) Serial.println(F("11n")); - else Serial.println(F("???")); + + if (CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGTH)) { + Serial.print(F("SSID: ")); + Serial.println(sbuf); + } else { + Serial.println(F("Error reading SSID")); } - else Serial.println(F("Error reading phy mode")); - - if (CONFIG::read_byte(EP_SLEEP_MODE, &bbuf )) - { - Serial.print(F("Sleep mode: ")); - if (byte(bbuf)==NONE_SLEEP_T) Serial.println(F("None")); - else if (byte(bbuf)==LIGHT_SLEEP_T) Serial.println(F("Light")); - else if (byte(bbuf)==MODEM_SLEEP_T) Serial.println(F("Modem")); - else Serial.println(F("???")); - } - else Serial.println(F("Error reading sleep mode")); - - if (CONFIG::read_byte(EP_CHANNEL, &bbuf )) - { - Serial.print(F("Channel: ")); - Serial.println(byte(bbuf)); - } - else Serial.println(F("Error reading channel")); - - if (CONFIG::read_byte(EP_AUTH_TYPE, &bbuf )) - { - Serial.print(F("Authentification: ")); - if (byte(bbuf)==AUTH_OPEN) Serial.println(F("None")); - else if (byte(bbuf)==AUTH_WEP) Serial.println(F("WEP")); - else if (byte(bbuf)==AUTH_WPA_PSK) Serial.println(F("WPA")); - else if (byte(bbuf)==AUTH_WPA2_PSK) Serial.println(F("WPA2")); - else if (byte(bbuf)==AUTH_WPA_WPA2_PSK) Serial.println(F("WPA/WPA2")); - else Serial.println(F("???")); - } - else Serial.println(F("Error reading authentification")); - - if (CONFIG::read_byte(EP_SSID_VISIBLE, &bbuf )) - { - Serial.print(F("SSID visibility: ")); - if (bbuf==0) Serial.println(F("Hidden")); - else if (bbuf==1) Serial.println(F("Visible")); - else Serial.println(bbuf); - } - else Serial.println(F("Error reading SSID visibility")); - - if (CONFIG::read_buffer(EP_WEB_PORT, (byte *)&ibuf , INTEGER_LENGTH)) - { - Serial.print(F("Web port: ")); - Serial.println(ibuf); - } - else Serial.println(F("Error reading web port")); - - if (CONFIG::read_buffer(EP_DATA_PORT, (byte *)&ibuf , INTEGER_LENGTH)) - { - Serial.print(F("Data port: ")); - Serial.println(ibuf); - } - else Serial.println(F("Error reading data port")); - - if (CONFIG::read_byte(EP_REFRESH_PAGE_TIME, &bbuf )) - { - Serial.print(F("Web page refresh time: ")); - Serial.println(byte(bbuf)); - } - else Serial.println(F("Error reading refresh page")); - - if (CONFIG::read_string(EP_HOSTNAME, sbuf , MAX_HOSTNAME_LENGTH)) - { - Serial.print(F("Hostname: ")); - Serial.println(sbuf); - } - else Serial.println(F("Error reading hostname")); - - if (CONFIG::read_buffer(EP_XY_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH)) - { - Serial.print(F("XY feed rate: ")); - Serial.println(ibuf); - } - else Serial.println(F("Error reading XY feed rate")); - - if (CONFIG::read_buffer(EP_Z_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH)) - { - Serial.print(F("Z feed rate: ")); - Serial.println(ibuf); - } - else Serial.println(F("Error reading Z feed rate")); - - if (CONFIG::read_buffer(EP_E_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH)) - { - Serial.print(F("E feed rate: ")); - Serial.println(ibuf); - } - else Serial.println(F("Error reading E feed rate")); - - Serial.print(F("Captive portal: ")); - #ifdef CAPTIVE_PORTAL_FEATURE - Serial.println(F("Enabled")); - #else - Serial.println(F("Disabled")); + //if (CONFIG::read_string(EP_PASSWORD, sbuf , MAX_PASSWORD_LENGTH))Serial.println(sbuf); + + if (CONFIG::read_byte(EP_IP_MODE, &bbuf )) { + Serial.print(F("IP Mode: ")); + if (byte(bbuf)==STATIC_IP_MODE) { + Serial.println(F("Static")); + } else if (byte(bbuf)==DHCP_MODE) { + Serial.println(F("DHCP")); + } else { + Serial.println(F("???")); + } + } else { + Serial.println(F("Error reading IP mode")); + } + + if (CONFIG::read_buffer(EP_IP_VALUE,(byte *)sbuf , IP_LENGTH)) { + Serial.print(F("IP: ")); + Serial.println(wifi_config.ip2str((byte *)sbuf)); + } else { + Serial.println(F("Error reading IP")); + } + + if (CONFIG::read_buffer(EP_MASK_VALUE, (byte *)sbuf , IP_LENGTH)) { + Serial.print(F("Subnet: ")); + Serial.println(wifi_config.ip2str((byte *)sbuf)); + } else { + Serial.println(F("Error reading subnet")); + } + + if (CONFIG::read_buffer(EP_GATEWAY_VALUE, (byte *)sbuf , IP_LENGTH)) { + Serial.print(F("Gateway: ")); + Serial.println(wifi_config.ip2str((byte *)sbuf)); + } else { + Serial.println(F("Error reading gateway")); + } + + if (CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&ibuf , INTEGER_LENGTH)) { + Serial.print(F("Baud rate: ")); + Serial.println(ibuf); + } else { + Serial.println(F("Error reading baud rate")); + } + + if (CONFIG::read_byte(EP_PHY_MODE, &bbuf )) { + Serial.print(F("Phy mode: ")); + if (byte(bbuf)==PHY_MODE_11B) { + Serial.println(F("11b")); + } else if (byte(bbuf)==PHY_MODE_11G) { + Serial.println(F("11g")); + } else if (byte(bbuf)==PHY_MODE_11N) { + Serial.println(F("11n")); + } else { + Serial.println(F("???")); + } + } else { + Serial.println(F("Error reading phy mode")); + } + + if (CONFIG::read_byte(EP_SLEEP_MODE, &bbuf )) { + Serial.print(F("Sleep mode: ")); + if (byte(bbuf)==NONE_SLEEP_T) { + Serial.println(F("None")); + } else if (byte(bbuf)==LIGHT_SLEEP_T) { + Serial.println(F("Light")); + } else if (byte(bbuf)==MODEM_SLEEP_T) { + Serial.println(F("Modem")); + } else { + Serial.println(F("???")); + } + } else { + Serial.println(F("Error reading sleep mode")); + } + + if (CONFIG::read_byte(EP_CHANNEL, &bbuf )) { + Serial.print(F("Channel: ")); + Serial.println(byte(bbuf)); + } else { + Serial.println(F("Error reading channel")); + } + + if (CONFIG::read_byte(EP_AUTH_TYPE, &bbuf )) { + Serial.print(F("Authentification: ")); + if (byte(bbuf)==AUTH_OPEN) { + Serial.println(F("None")); + } else if (byte(bbuf)==AUTH_WEP) { + Serial.println(F("WEP")); + } else if (byte(bbuf)==AUTH_WPA_PSK) { + Serial.println(F("WPA")); + } else if (byte(bbuf)==AUTH_WPA2_PSK) { + Serial.println(F("WPA2")); + } else if (byte(bbuf)==AUTH_WPA_WPA2_PSK) { + Serial.println(F("WPA/WPA2")); + } else { + Serial.println(F("???")); + } + } else { + Serial.println(F("Error reading authentification")); + } + + if (CONFIG::read_byte(EP_SSID_VISIBLE, &bbuf )) { + Serial.print(F("SSID visibility: ")); + if (bbuf==0) { + Serial.println(F("Hidden")); + } else if (bbuf==1) { + Serial.println(F("Visible")); + } else { + Serial.println(bbuf); + } + } else { + Serial.println(F("Error reading SSID visibility")); + } + + if (CONFIG::read_buffer(EP_WEB_PORT, (byte *)&ibuf , INTEGER_LENGTH)) { + Serial.print(F("Web port: ")); + Serial.println(ibuf); + } else { + Serial.println(F("Error reading web port")); + } + + if (CONFIG::read_buffer(EP_DATA_PORT, (byte *)&ibuf , INTEGER_LENGTH)) { + Serial.print(F("Data port: ")); + Serial.println(ibuf); + } else { + Serial.println(F("Error reading data port")); + } + + if (CONFIG::read_byte(EP_REFRESH_PAGE_TIME, &bbuf )) { + Serial.print(F("Web page refresh time: ")); + Serial.println(byte(bbuf)); + } else { + Serial.println(F("Error reading refresh page")); + } + + if (CONFIG::read_string(EP_HOSTNAME, sbuf , MAX_HOSTNAME_LENGTH)) { + Serial.print(F("Hostname: ")); + Serial.println(sbuf); + } else { + Serial.println(F("Error reading hostname")); + } + + if (CONFIG::read_buffer(EP_XY_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH)) { + Serial.print(F("XY feed rate: ")); + Serial.println(ibuf); + } else { + Serial.println(F("Error reading XY feed rate")); + } + + if (CONFIG::read_buffer(EP_Z_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH)) { + Serial.print(F("Z feed rate: ")); + Serial.println(ibuf); + } else { + Serial.println(F("Error reading Z feed rate")); + } + + if (CONFIG::read_buffer(EP_E_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH)) { + Serial.print(F("E feed rate: ")); + Serial.println(ibuf); + } else { + Serial.println(F("Error reading E feed rate")); + } + + Serial.print(F("Captive portal: ")); +#ifdef CAPTIVE_PORTAL_FEATURE + Serial.println(F("Enabled")); +#else + Serial.println(F("Disabled")); #endif - Serial.print(F("SSDP: ")); + Serial.print(F("SSDP: ")); #ifdef SSDP_FEATURE - Serial.println(F("Enabled")); - #else - Serial.println(F("Disabled")); + Serial.println(F("Enabled")); +#else + Serial.println(F("Disabled")); #endif - Serial.print(F("mDNS: ")); + Serial.print(F("mDNS: ")); #ifdef MDNS_FEATURE - Serial.println(F("Enabled")); - #else - Serial.println(F("Disabled")); + Serial.println(F("Enabled")); +#else + Serial.println(F("Disabled")); #endif -} +} diff --git a/esp8266/config.h b/esp8266/config.h index 8904b0db..f4ae1344 100644 --- a/esp8266/config.h +++ b/esp8266/config.h @@ -20,7 +20,7 @@ //comment to disable //MDNS_FEATURE: this feature allow type the name defined -//in web browser by default: http:\\esp8266.local and connect +//in web browser by default: http:\\esp8266.local and connect #define MDNS_FEATURE //SSDD_FEATURE: this feature is a discovery protocol, supported on Windows out of the box @@ -119,7 +119,7 @@ const char DEFAULT_ADMIN [] PROGMEM = "admin"; class CONFIG { - public: +public: static bool read_string(int pos, char byte_buffer[], int size_max); static bool read_string(int pos, String & sbuffer, int size_max); static bool read_buffer(int pos, byte byte_buffer[], int size_buffer); diff --git a/esp8266/esp8266.ino b/esp8266/esp8266.ino index 653f5d29..5ef9a906 100644 --- a/esp8266/esp8266.ino +++ b/esp8266/esp8266.ino @@ -19,7 +19,7 @@ Latest version of the code and documentation can be found here : https://github.com/luc-github/ESP8266 - + Main author: luc lebosse */ @@ -55,90 +55,101 @@ extern "C" { WiFiServer * data_server; WiFiClient serverClients[MAX_SRV_CLIENTS]; -void setup() { - // init: - web_interface = NULL; - data_server = NULL; - // ESP.wdtDisable(); - system_update_cpu_freq(SYS_CPU_160MHZ); - delay(8000); - bool breset_config=false; - //check if reset config is requested - pinMode(RESET_CONFIG_PIN, INPUT); - if (digitalRead(RESET_CONFIG_PIN)==0)breset_config=true;//if requested =>reset settings - //default baud rate - long baud_rate=0; - - //check if EEPROM has value - if ( CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&baud_rate , INTEGER_LENGTH)&&CONFIG::read_buffer(EP_WEB_PORT, (byte *)&(wifi_config.iweb_port) , INTEGER_LENGTH)&&CONFIG::read_buffer(EP_DATA_PORT, (byte *)&(wifi_config.idata_port) , INTEGER_LENGTH)) - { - //check if baud value is one of allowed ones - if ( ! (baud_rate==9600 || baud_rate==19200 ||baud_rate==38400 ||baud_rate==57600 ||baud_rate==115200 ||baud_rate==230400 ||baud_rate==250000) )breset_config=true;//baud rate is incorrect =>reset settings - if (wifi_config.iweb_port<1 ||wifi_config.iweb_port>65001 || wifi_config.idata_port <1 || wifi_config.idata_port >65001)breset_config=true; //out of range =>reset settings - +void setup() +{ + // init: + web_interface = NULL; + data_server = NULL; +// ESP.wdtDisable(); + system_update_cpu_freq(SYS_CPU_160MHZ); + delay(8000); + bool breset_config=false; + //check if reset config is requested + pinMode(RESET_CONFIG_PIN, INPUT); + if (digitalRead(RESET_CONFIG_PIN)==0) { + breset_config=true; //if requested =>reset settings } - else breset_config=true;//cannot access to config settings=> reset settings - - - //reset is requested - if(breset_config) - { - //update EEPROM with default settings - Serial.begin(9600); - delay(2000); - Serial.println(F("M117 ESP EEPROM reset")); - CONFIG::reset_config(); + //default baud rate + long baud_rate=0; + + //check if EEPROM has value + if ( CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&baud_rate , INTEGER_LENGTH)&&CONFIG::read_buffer(EP_WEB_PORT, (byte *)&(wifi_config.iweb_port) , INTEGER_LENGTH)&&CONFIG::read_buffer(EP_DATA_PORT, (byte *)&(wifi_config.idata_port) , INTEGER_LENGTH)) { + //check if baud value is one of allowed ones + if ( ! (baud_rate==9600 || baud_rate==19200 ||baud_rate==38400 ||baud_rate==57600 ||baud_rate==115200 ||baud_rate==230400 ||baud_rate==250000) ) { + breset_config=true; //baud rate is incorrect =>reset settings + } + if (wifi_config.iweb_port<1 ||wifi_config.iweb_port>65001 || wifi_config.idata_port <1 || wifi_config.idata_port >65001) { + breset_config=true; //out of range =>reset settings + } + + } else { + breset_config=true; //cannot access to config settings=> reset settings + } + + + //reset is requested + if(breset_config) { + //update EEPROM with default settings + Serial.begin(9600); + delay(2000); + Serial.println(F("M117 ESP EEPROM reset")); + CONFIG::reset_config(); + delay(1000); + //put some default value to a void some exception at first start + WiFi.mode(WIFI_AP); + wifi_set_phy_mode(PHY_MODE_11G); + Serial.flush(); + delay(500); + Serial.swap(); + delay(100); + //restart once reset config is done + ESP.restart(); + while (1) { + delay(1); + }; + } + //setup serial + Serial.begin(baud_rate); delay(1000); - //put some default value to a void some exception at first start - WiFi.mode(WIFI_AP); - wifi_set_phy_mode(PHY_MODE_11G); - Serial.flush(); - delay(500); - Serial.swap(); - delay(100); - //restart once reset config is done - ESP.restart(); - while (1){delay(1);}; + wifi_config.baud_rate=baud_rate; + //setup wifi according settings + if (!wifi_config.Setup()) { + wifi_config.Safe_Setup(); } - //setup serial - Serial.begin(baud_rate); - delay(1000); - wifi_config.baud_rate=baud_rate; - //setup wifi according settings - if (!wifi_config.Setup()) wifi_config.Safe_Setup(); - delay(1000); - //start interfaces - web_interface = new WEBINTERFACE_CLASS(wifi_config.iweb_port); - data_server = new WiFiServer (wifi_config.idata_port); - //here the list of headers to be recorded - const char * headerkeys[] = {"Cookie"} ; - size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*); - //ask server to track these headers - web_interface->WebServer.collectHeaders(headerkeys, headerkeyssize ); - web_interface->WebServer.begin(); - data_server->begin(); - data_server->setNoDelay(true); + delay(1000); + //start interfaces + web_interface = new WEBINTERFACE_CLASS(wifi_config.iweb_port); + data_server = new WiFiServer (wifi_config.idata_port); + //here the list of headers to be recorded + const char * headerkeys[] = {"Cookie"} ; + size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*); + //ask server to track these headers + web_interface->WebServer.collectHeaders(headerkeys, headerkeyssize ); + web_interface->WebServer.begin(); + data_server->begin(); + data_server->setNoDelay(true); #ifdef MDNS_FEATURE - // Check for any mDNS queries and send responses - wifi_config.mdns.addService("http", "tcp", wifi_config.iweb_port); + // Check for any mDNS queries and send responses + wifi_config.mdns.addService("http", "tcp", wifi_config.iweb_port); #endif #ifdef CAPTIVE_PORTAL_FEATURE - if (wifi_get_opmode()!=WIFI_STA ) - { - // if DNSServer is started with "*" for domain name, it will reply with - // provided IP to all DNS request - dnsServer.setErrorReplyCode(DNSReplyCode::NoError); - dnsServer.start(DNS_PORT, "*", WiFi.softAPIP()); - } - #endif + if (wifi_get_opmode()!=WIFI_STA ) { + // if DNSServer is started with "*" for domain name, it will reply with + // provided IP to all DNS request + dnsServer.setErrorReplyCode(DNSReplyCode::NoError); + dnsServer.start(DNS_PORT, "*", WiFi.softAPIP()); + } +#endif #ifdef SSDP_FEATURE - String stmp; - SSDP.setSchemaURL("description.xml"); + String stmp; + SSDP.setSchemaURL("description.xml"); SSDP.setHTTPPort( wifi_config.iweb_port); - if (!CONFIG::read_string(EP_HOSTNAME, stmp , MAX_HOSTNAME_LENGTH))stmp=wifi_config.get_default_hostname(); + if (!CONFIG::read_string(EP_HOSTNAME, stmp , MAX_HOSTNAME_LENGTH)) { + stmp=wifi_config.get_default_hostname(); + } SSDP.setName(stmp.c_str()); stmp=String(system_get_chip_id()); SSDP.setSerialNumber(stmp.c_str()); @@ -150,70 +161,73 @@ void setup() { SSDP.setManufacturerURL("http://espressif.com"); SSDP.begin(); #endif -SPIFFS.begin(); + SPIFFS.begin(); } //main loop -void loop() { +void loop() +{ #ifdef CAPTIVE_PORTAL_FEATURE - if (wifi_get_opmode()!=WIFI_STA ) - { - dnsServer.processNextRequest(); - } + if (wifi_get_opmode()!=WIFI_STA ) { + dnsServer.processNextRequest(); + } #endif //web requests -web_interface->WebServer.handleClient(); + web_interface->WebServer.handleClient(); //TODO use a method to handle serial also in class and call it instead of this one -uint8_t i,data; - //check if there are any new clients - if (data_server->hasClient()){ - for(i = 0; i < MAX_SRV_CLIENTS; i++){ - //find free/disconnected spot - if (!serverClients[i] || !serverClients[i].connected()){ - if(serverClients[i]) serverClients[i].stop(); - serverClients[i] = data_server->available(); - continue; - } + uint8_t i,data; + //check if there are any new clients + if (data_server->hasClient()) { + for(i = 0; i < MAX_SRV_CLIENTS; i++) { + //find free/disconnected spot + if (!serverClients[i] || !serverClients[i].connected()) { + if(serverClients[i]) { + serverClients[i].stop(); + } + serverClients[i] = data_server->available(); + continue; + } + } + //no free/disconnected spot so reject + WiFiClient serverClient = data_server->available(); + serverClient.stop(); } - //no free/disconnected spot so reject - WiFiClient serverClient = data_server->available(); - serverClient.stop(); - } - //check clients for data - for(i = 0; i < MAX_SRV_CLIENTS; i++){ - if (serverClients[i] && serverClients[i].connected()){ - if(serverClients[i].available()){ - //get data from the tcp client and push it to the UART - while(serverClients[i].available()){ - data = serverClients[i].read(); - Serial.write(data); - COMMAND::read_buffer_tcp(data); - } - } + //check clients for data + for(i = 0; i < MAX_SRV_CLIENTS; i++) { + if (serverClients[i] && serverClients[i].connected()) { + if(serverClients[i].available()) { + //get data from the tcp client and push it to the UART + while(serverClients[i].available()) { + data = serverClients[i].read(); + Serial.write(data); + COMMAND::read_buffer_tcp(data); + } + } + } } - } - //check UART for data - if(Serial.available()){ - size_t len = Serial.available(); - uint8_t sbuf[len]; - Serial.readBytes(sbuf, len); - //push UART data to all connected tcp clients - for(i = 0; i < MAX_SRV_CLIENTS; i++){ - if (serverClients[i] && serverClients[i].connected()){ - serverClients[i].write(sbuf, len); - delay(1); - } - COMMAND::read_buffer_serial(sbuf, len); + //check UART for data + if(Serial.available()) { + size_t len = Serial.available(); + uint8_t sbuf[len]; + Serial.readBytes(sbuf, len); + //push UART data to all connected tcp clients + for(i = 0; i < MAX_SRV_CLIENTS; i++) { + if (serverClients[i] && serverClients[i].connected()) { + serverClients[i].write(sbuf, len); + delay(1); + } + COMMAND::read_buffer_serial(sbuf, len); + } + } + if (web_interface->restartmodule) { + Serial.flush(); + delay(500); + Serial.swap(); + delay(100); + ESP.restart(); + while (1) { + delay(1); + }; } - } - if (web_interface->restartmodule) - { - Serial.flush(); - delay(500); - Serial.swap(); - delay(100); - ESP.restart(); - while (1){delay(1);}; - } } diff --git a/esp8266/storestrings.cpp b/esp8266/storestrings.cpp index 28a760e8..c5bcafb9 100644 --- a/esp8266/storestrings.cpp +++ b/esp8266/storestrings.cpp @@ -1,8 +1,8 @@ -/* +/* storestrings.cpp - rolling storage class Copyright (c) 2014 Luc Lebosse. All rights reserved. - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either @@ -19,107 +19,121 @@ */ #include "storestrings.h" //Constructor -STORESTRINGS_CLASS::STORESTRINGS_CLASS (int maxsize , int maxstringlength){ +STORESTRINGS_CLASS::STORESTRINGS_CLASS (int maxsize , int maxstringlength) +{ //for rolling buffer //if max size is reached then remove oldest one and add the new one -_maxsize=maxsize; + _maxsize=maxsize; //to limit the storage space -_maxstringlength=maxstringlength; + _maxstringlength=maxstringlength; //need space for the "..." -if (_maxstringlength<4 && _maxstringlength!=-1)_maxstringlength=4; + if (_maxstringlength<4 && _maxstringlength!=-1) { + _maxstringlength=4; + } } //Destructor -STORESTRINGS_CLASS::~STORESTRINGS_CLASS (){ - // clear list and content - clear(); +STORESTRINGS_CLASS::~STORESTRINGS_CLASS () +{ + // clear list and content + clear(); } bool STORESTRINGS_CLASS::setsize(int size) { - _maxsize=size; - return true; + _maxsize=size; + return true; } bool STORESTRINGS_CLASS::setlength(int len) { - if (len < 4) return false; - _maxstringlength = len; - return true; + if (len < 4) { + return false; + } + _maxstringlength = len; + return true; } //Clear list and content -void STORESTRINGS_CLASS::clear(){ - //while list is not empty - while(_charlist.size()){ - //remove element - char * str = _charlist.pop(); - //destroy it - delete str; - } +void STORESTRINGS_CLASS::clear() +{ + //while list is not empty + while(_charlist.size()) { + //remove element + char * str = _charlist.pop(); + //destroy it + delete str; + } } bool STORESTRINGS_CLASS::add (const __FlashStringHelper *str) { - String stmp; - stmp=str; - return add(stmp.c_str()); + String stmp; + stmp=str; + return add(stmp.c_str()); } //Add element in storage -bool STORESTRINGS_CLASS::add (const char * string){ - //if we reach max size - if (_maxsize==_charlist.size()) - {//remove oldest one - char * str = _charlist.shift(); - delete str; - } - //add new one - //get size including \0 at the end - size_t size = strlen(string)+1; - bool need_resize=false; - if ( (_maxstringlength!=-1) && (size >_maxstringlength+1 )) - { - need_resize = true; - size=_maxstringlength+1; - } - //reserve memory - char * ptr = new char[size*sizeof(char)]; - //copy string to storage - if (need_resize) - { //copy maximum length minus 3 - strncpy(ptr,string,_maxstringlength-3); - strcpy(ptr+_maxstringlength-3,"..."); - } - else - { //copy as it is - strcpy(ptr,string); - } - //add storage to list - _charlist.add(ptr); - return true; +bool STORESTRINGS_CLASS::add (const char * string) +{ + //if we reach max size + if (_maxsize==_charlist.size()) { + //remove oldest one + char * str = _charlist.shift(); + delete str; + } + //add new one + //get size including \0 at the end + size_t size = strlen(string)+1; + bool need_resize=false; + if ( (_maxstringlength!=-1) && (size >_maxstringlength+1 )) { + need_resize = true; + size=_maxstringlength+1; + } + //reserve memory + char * ptr = new char[size*sizeof(char)]; + //copy string to storage + if (need_resize) { + //copy maximum length minus 3 + strncpy(ptr,string,_maxstringlength-3); + strcpy(ptr+_maxstringlength-3,"..."); + } else { + //copy as it is + strcpy(ptr,string); + } + //add storage to list + _charlist.add(ptr); + return true; } //Remove element at pos position bool STORESTRINGS_CLASS::remove(int pos) -{ //be sure index is in range - if (pos<0 && pos>(_charlist.size()-1)) return false; - //remove item from list - char * str = _charlist.remove(pos); - //destroy item - delete str; - return true; +{ + //be sure index is in range + if (pos<0 && pos>(_charlist.size()-1)) { + return false; + } + //remove item from list + char * str = _charlist.remove(pos); + //destroy item + delete str; + return true; } //Get element at pos position const char * STORESTRINGS_CLASS::get(int pos) -{ //be sure index is in range - if (pos<0 && pos>(_charlist.size()-1)) return NULL; - return (const char *) _charlist.get(pos); +{ + //be sure index is in range + if (pos<0 && pos>(_charlist.size()-1)) { + return NULL; + } + return (const char *) _charlist.get(pos); } //Get index for defined string int STORESTRINGS_CLASS::get_index(const char * string) -{//parse the list until it is found - for (int p=0;p<_charlist.size();p++) - { - if (strcmp ( _charlist.get(p), string)==0)return p; - } - //if not found return -1 - return -1; +{ + //parse the list until it is found + for (int p=0; p<_charlist.size(); p++) { + if (strcmp ( _charlist.get(p), string)==0) { + return p; + } + } + //if not found return -1 + return -1; } diff --git a/esp8266/storestrings.h b/esp8266/storestrings.h index 4809084d..6589f50b 100644 --- a/esp8266/storestrings.h +++ b/esp8266/storestrings.h @@ -1,8 +1,8 @@ -/* +/* storestrings.h - rolling storage class Copyright (c) 2014 Luc Lebosse. All rights reserved. - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either @@ -24,26 +24,38 @@ #include "LinkedList.h" class STORESTRINGS_CLASS { - public: - STORESTRINGS_CLASS (int maxsize = -1, int maxstringlength=-1); - ~STORESTRINGS_CLASS (); - bool add (const char * string); - inline bool add (String & string) {return add(string.c_str());}; - bool add (const __FlashStringHelper *str); - bool remove(int pos); - const char * get(int pos); - int get_index(const char * string); - void clear(); - inline int size() {return _charlist.size();}; - bool setsize(int size); - bool setlength(int len); - inline int getsize() {return _maxsize;}; - inline int getlength() {return _maxstringlength;}; +public: + STORESTRINGS_CLASS (int maxsize = -1, int maxstringlength=-1); + ~STORESTRINGS_CLASS (); + bool add (const char * string); + inline bool add (String & string) + { + return add(string.c_str()); + }; + bool add (const __FlashStringHelper *str); + bool remove(int pos); + const char * get(int pos); + int get_index(const char * string); + void clear(); + inline int size() + { + return _charlist.size(); + }; + bool setsize(int size); + bool setlength(int len); + inline int getsize() + { + return _maxsize; + }; + inline int getlength() + { + return _maxstringlength; + }; - private: - int _maxsize; - int _maxstringlength; - LinkedList _charlist; +private: + int _maxsize; + int _maxstringlength; + LinkedList _charlist; }; #endif diff --git a/esp8266/webinterface.cpp b/esp8266/webinterface.cpp index 3b910a7a..9ab6a2b7 100644 --- a/esp8266/webinterface.cpp +++ b/esp8266/webinterface.cpp @@ -1,8 +1,8 @@ -/* +/* webinterface.cpp - esp8266 configuration class Copyright (c) 2014 Luc Lebosse. All rights reserved. - + This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either @@ -206,372 +206,380 @@ const char MISSING_DATA [] PROGMEM = "Error: Missing data"; const char EEPROM_NOWRITE [] PROGMEM = "Error: Cannot write to EEPROM"; bool WEBINTERFACE_CLASS::isHostnameValid(const char * hostname) -{ //limited size - char c; - if (strlen(hostname)>MAX_HOSTNAME_LENGTH || strlen(hostname) < 1) return false; - //only letter and digit - for (int i=0;i < strlen(hostname);i++) - { - c = hostname[i]; - if (!(isdigit(c) || isalpha(c) || c=='_')) return false; - if (c==' ') return false; - } - return true; +{ + //limited size + char c; + if (strlen(hostname)>MAX_HOSTNAME_LENGTH || strlen(hostname) < 1) { + return false; + } + //only letter and digit + for (int i=0; i < strlen(hostname); i++) { + c = hostname[i]; + if (!(isdigit(c) || isalpha(c) || c=='_')) { + return false; + } + if (c==' ') { + return false; + } + } + return true; } bool WEBINTERFACE_CLASS::isSSIDValid(const char * ssid) -{ //limited size - char c; - if (strlen(ssid)>MAX_SSID_LENGTH || strlen(ssid)MAX_SSID_LENGTH || strlen(ssid)MAX_PASSWORD_LENGTH)|| (strlen(password)MAX_PASSWORD_LENGTH)|| (strlen(password)MAX_ADMIN_PASSWORD_LENGTH)|| (strlen(password)MAX_ADMIN_PASSWORD_LENGTH)|| (strlen(password)15 || strlen(IP)==0) return false; - //cannot start with . - if (IP[0]=='.')return false; - //only letter and digit - for (int i=0;i < strlen(IP);i++) - { - c = IP[i]; - if (isdigit(c)) - {//only 3 digit at once - internalcount++; - previouswasdot=false; - if (internalcount>3)return false; - } - else if(c=='.') - { //cannot have 2 dots side by side - if (previouswasdot)return false; - previouswasdot=true; - internalcount=0; - dotcount++; - }//if not a dot neither a digit it is wrong - else return false; - } - //if not 3 dots then it is wrong - if (dotcount!=3)return false; - //cannot have the last dot as last char - if (IP[strlen(IP)-1]=='.')return false; - return true; + if (strlen(IP)>15 || strlen(IP)==0) { + return false; + } + //cannot start with . + if (IP[0]=='.') { + return false; + } + //only letter and digit + for (int i=0; i < strlen(IP); i++) { + c = IP[i]; + if (isdigit(c)) { + //only 3 digit at once + internalcount++; + previouswasdot=false; + if (internalcount>3) { + return false; + } + } else if(c=='.') { + //cannot have 2 dots side by side + if (previouswasdot) { + return false; + } + previouswasdot=true; + internalcount=0; + dotcount++; + }//if not a dot neither a digit it is wrong + else { + return false; + } + } + //if not 3 dots then it is wrong + if (dotcount!=3) { + return false; + } + //cannot have the last dot as last char + if (IP[strlen(IP)-1]=='.') { + return false; + } + return true; } //TODO should be in some tool class -char * intTostr(int value){ -static char result [12]; -sprintf(result,"%d",value); -return result; +char * intTostr(int value) +{ + static char result [12]; + sprintf(result,"%d",value); + return result; } //TODO: should be in webserver class bool processTemplate(const char * filename, STORESTRINGS_CLASS & KeysList , STORESTRINGS_CLASS & ValuesList ) { - if(KeysList.size() != ValuesList.size()) //Sanity check - return false; + if(KeysList.size() != ValuesList.size()) { //Sanity check + return false; + } - LinkedList myFileList = LinkedList(); - String buffer2send; - String bufferheader(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: ")); - int size_content=0; - bool header_sent=false; - - //one loop to calculate size + one loop to really send - //size_content is a mandatory element to avoid memory leak - for(int processing_step=0;processing_step<2;processing_step++) - { - buffer2send=""; - //open template file - File currentfile = SPIFFS.open(filename, "r"); - //if error display error on web page - if (!currentfile) - { - buffer2send = String(F("Error opening: ")) + filename; - web_interface->WebServer.send(200,"text/plain",buffer2send); - return false; - } - else //template is open - { - int b ; - String sLine; - bool bprocessing=true; + LinkedList myFileList = LinkedList(); + String buffer2send; + String bufferheader(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: ")); + int size_content=0; + bool header_sent=false; - while (bprocessing) //read all bytes - { - b = currentfile.read(); //from current open file - if (b!=-1) //if not EOF - { - sLine+=char(b); //add to current line - if (b=='\n') //end of line is reached - { //change all variables by their values - for (int k=0;k-1) - { //get value - nb=atoi(ValuesList.get(ipos)); - ipos=ipos-(nb*3); - } - //do a sanity check data are there - String Last_IP_Key = "$IP_CONNECTED["+String(nb-1)+"]$"; - if (nb>0 && (KeysList.get_index("$ROW_NUMBER[0]$")==ipos) &&(Last_IP_Key==KeysList.get(ipos-1+(nb*3)))) - { - for (int j=0;j-1) - { - tmppart.replace("$ROW_NUMBER$",ValuesList.get(ipos+0+(j*3))); - tmppart.replace("$MAC_CONNECTED$",ValuesList.get(ipos+1+(j*3))); - tmppart.replace("$IP_CONNECTED$",ValuesList.get(ipos+2+(j*3))); - } - tablepart +=tmppart; - } - } - //now build back - sLine = beforelinetoprocess + tablepart + afterlinetoprocess; - } - - pos_tag=sLine.indexOf("$AVAILABLE_AP["); - if (pos_tag!=-1) //if yes - { //extract line - int pos_tag_end = sLine.indexOf("]$",pos_tag); - int nb = -1; - int ipos = -1; - //part before repetitive section - String beforelinetoprocess=sLine.substring( 0,pos_tag); - //part after repetitive section - String afterlinetoprocess=sLine.substring( pos_tag_end+2); - //repetitive section itself - String linetoprocess =sLine.substring( pos_tag+strlen("$AVAILABLE_AP["),pos_tag_end); - String tablepart; - //get how many items - ipos=KeysList.get_index("$AVAILABLE_AP_NB_ITEMS$"); - if (ipos >-1) - { //get value - nb=atoi(ValuesList.get(ipos)); - ipos=ipos-(nb*4); - } - //do a sanity check data are there - String Last_IP_Key = "$IS_PROTECTED["+String(nb-1)+"]$"; - if (nb>0 && (KeysList.get_index("$ROW_NUMBER[0]$")==ipos) &&(Last_IP_Key==KeysList.get(ipos-1+(nb*4)))) - { - for (int j=0;j-1) - { - tmppart.replace("$ROW_NUMBER$",ValuesList.get(ipos+0+(j*4))); - tmppart.replace("$AP_SSID$",ValuesList.get(ipos+1+(j*4))); - tmppart.replace("$AP_SIGNAL$",ValuesList.get(ipos+2+(j*4))); - tmppart.replace("$IS_PROTECTED$",ValuesList.get(ipos+3+(j*4))); - } - tablepart +=tmppart; - } - } - //now build back - sLine = beforelinetoprocess + tablepart + afterlinetoprocess; - } - - //add current line to buffer - buffer2send+=sLine; - //if buffer limit is reached - if (buffer2send.length()>1200) - { //if we are just doing size calculation - if (processing_step==0) - { //add buffer size - size_content+=buffer2send.length(); - } - else //if no size calculation, send data - { //if header is not send yet - if (!header_sent) - {//send header with calculated size - header_sent=true; - web_interface->WebServer.sendContent(bufferheader); - } - //send data - web_interface->WebServer.sendContent(buffer2send); - } - //reset buffer - buffer2send=""; - } - } - //reset line - sLine=""; - //add a delay for safety for WDT - delay(1); - } - } - else //EOF is reached - { //close current file - currentfile.close(); - //if current file is not template file but included one - if (myFileList.size()>0) - { //get level +1 file description and continue - currentfile = myFileList.pop(); - } - else - {//we have done template parsing, let's stop reading - bprocessing=false; - } - } - } - } - //check if something is still in buffer and need to be send - if (buffer2send!=""){ - //if we are doing size calculation - if (processing_step==0) - { //add buffer size - size_content+=buffer2send.length(); - } - else //if no size calculation, send data - { //if header is not send yet - if (!header_sent) - {//send header with calculated size - web_interface->WebServer.sendContent(bufferheader); - } - //send data - web_interface->WebServer.sendContent(buffer2send); - } - } - //if we end size calculation loop - if (processing_step==0) - { //let's build the header with correct size' - bufferheader.concat(size_content); - bufferheader.concat(F("\r\nConnection: close\r\nAccess-Control-Allow-Origin: *\r\n\r\n")); - } - } - return true; + //one loop to calculate size + one loop to really send + //size_content is a mandatory element to avoid memory leak + for(int processing_step=0; processing_step<2; processing_step++) { + buffer2send=""; + //open template file + File currentfile = SPIFFS.open(filename, "r"); + //if error display error on web page + if (!currentfile) { + buffer2send = String(F("Error opening: ")) + filename; + web_interface->WebServer.send(200,"text/plain",buffer2send); + return false; + } else { //template is open + int b ; + String sLine; + bool bprocessing=true; + + while (bprocessing) { //read all bytes + b = currentfile.read(); //from current open file + if (b!=-1) { //if not EOF + sLine+=char(b); //add to current line + if (b=='\n') { //end of line is reached + //change all variables by their values + for (int k=0; k-1) { + //get value + nb=atoi(ValuesList.get(ipos)); + ipos=ipos-(nb*3); + } + //do a sanity check data are there + String Last_IP_Key = "$IP_CONNECTED["+String(nb-1)+"]$"; + if (nb>0 && (KeysList.get_index("$ROW_NUMBER[0]$")==ipos) &&(Last_IP_Key==KeysList.get(ipos-1+(nb*3)))) { + for (int j=0; j-1) { + tmppart.replace("$ROW_NUMBER$",ValuesList.get(ipos+0+(j*3))); + tmppart.replace("$MAC_CONNECTED$",ValuesList.get(ipos+1+(j*3))); + tmppart.replace("$IP_CONNECTED$",ValuesList.get(ipos+2+(j*3))); + } + tablepart +=tmppart; + } + } + //now build back + sLine = beforelinetoprocess + tablepart + afterlinetoprocess; + } + + pos_tag=sLine.indexOf("$AVAILABLE_AP["); + if (pos_tag!=-1) { //if yes + //extract line + int pos_tag_end = sLine.indexOf("]$",pos_tag); + int nb = -1; + int ipos = -1; + //part before repetitive section + String beforelinetoprocess=sLine.substring( 0,pos_tag); + //part after repetitive section + String afterlinetoprocess=sLine.substring( pos_tag_end+2); + //repetitive section itself + String linetoprocess =sLine.substring( pos_tag+strlen("$AVAILABLE_AP["),pos_tag_end); + String tablepart; + //get how many items + ipos=KeysList.get_index("$AVAILABLE_AP_NB_ITEMS$"); + if (ipos >-1) { + //get value + nb=atoi(ValuesList.get(ipos)); + ipos=ipos-(nb*4); + } + //do a sanity check data are there + String Last_IP_Key = "$IS_PROTECTED["+String(nb-1)+"]$"; + if (nb>0 && (KeysList.get_index("$ROW_NUMBER[0]$")==ipos) &&(Last_IP_Key==KeysList.get(ipos-1+(nb*4)))) { + for (int j=0; j-1) { + tmppart.replace("$ROW_NUMBER$",ValuesList.get(ipos+0+(j*4))); + tmppart.replace("$AP_SSID$",ValuesList.get(ipos+1+(j*4))); + tmppart.replace("$AP_SIGNAL$",ValuesList.get(ipos+2+(j*4))); + tmppart.replace("$IS_PROTECTED$",ValuesList.get(ipos+3+(j*4))); + } + tablepart +=tmppart; + } + } + //now build back + sLine = beforelinetoprocess + tablepart + afterlinetoprocess; + } + + //add current line to buffer + buffer2send+=sLine; + //if buffer limit is reached + if (buffer2send.length()>1200) { + //if we are just doing size calculation + if (processing_step==0) { + //add buffer size + size_content+=buffer2send.length(); + } else { //if no size calculation, send data + //if header is not send yet + if (!header_sent) { + //send header with calculated size + header_sent=true; + web_interface->WebServer.sendContent(bufferheader); + } + //send data + web_interface->WebServer.sendContent(buffer2send); + } + //reset buffer + buffer2send=""; + } + } + //reset line + sLine=""; + //add a delay for safety for WDT + delay(1); + } + } else { //EOF is reached + //close current file + currentfile.close(); + //if current file is not template file but included one + if (myFileList.size()>0) { + //get level +1 file description and continue + currentfile = myFileList.pop(); + } else { + //we have done template parsing, let's stop reading + bprocessing=false; + } + } + } + } + //check if something is still in buffer and need to be send + if (buffer2send!="") { + //if we are doing size calculation + if (processing_step==0) { + //add buffer size + size_content+=buffer2send.length(); + } else { //if no size calculation, send data + //if header is not send yet + if (!header_sent) { + //send header with calculated size + web_interface->WebServer.sendContent(bufferheader); + } + //send data + web_interface->WebServer.sendContent(buffer2send); + } + } + //if we end size calculation loop + if (processing_step==0) { + //let's build the header with correct size' + bufferheader.concat(size_content); + bufferheader.concat(F("\r\nConnection: close\r\nAccess-Control-Allow-Origin: *\r\n\r\n")); + } + } + return true; } // ----------------------------------------------------------------------------- // Helper for FreeMem and Firmware // ----------------------------------------------------------------------------- void GetFreeMem(STORESTRINGS_CLASS & KeysList, STORESTRINGS_CLASS & ValuesList) { - //FreeMem - KeysList.add(FPSTR(KEY_FREE_MEM)); - ValuesList.add(intTostr(system_get_free_heap_size())); - //FW Version - KeysList.add(FPSTR(KEY_FW_VER)); - ValuesList.add(FPSTR(VALUE_FW_VERSION)); + //FreeMem + KeysList.add(FPSTR(KEY_FREE_MEM)); + ValuesList.add(intTostr(system_get_free_heap_size())); + //FW Version + KeysList.add(FPSTR(KEY_FW_VER)); + ValuesList.add(FPSTR(VALUE_FW_VERSION)); } // ----------------------------------------------------------------------------- // Helper for IP+Web address // ----------------------------------------------------------------------------- void GetIpWeb(STORESTRINGS_CLASS & KeysList, STORESTRINGS_CLASS & ValuesList) { - String stmp; - - KeysList.add(FPSTR(KEY_IP)); - if (wifi_get_opmode() == WIFI_STA ) - stmp = WiFi.localIP().toString(); - else stmp = WiFi.softAPIP().toString(); - ValuesList.add(stmp); + String stmp; - //Web address = ip + port - KeysList.add(FPSTR(KEY_WEB_ADDRESS)); - if (wifi_config.iweb_port != 80) - { - stmp.concat(":"); - stmp.concat(wifi_config.iweb_port); + KeysList.add(FPSTR(KEY_IP)); + if (wifi_get_opmode() == WIFI_STA ) { + stmp = WiFi.localIP().toString(); + } else { + stmp = WiFi.softAPIP().toString(); } - ValuesList.add(stmp); + ValuesList.add(stmp); + + //Web address = ip + port + KeysList.add(FPSTR(KEY_WEB_ADDRESS)); + if (wifi_config.iweb_port != 80) { + stmp.concat(":"); + stmp.concat(wifi_config.iweb_port); + } + ValuesList.add(stmp); } // ----------------------------------------------------------------------------- // Helper for Wifi Mode // ----------------------------------------------------------------------------- void GetMode(STORESTRINGS_CLASS & KeysList, STORESTRINGS_CLASS & ValuesList) { - if (wifi_get_opmode() == WIFI_STA ) - { - KeysList.add(FPSTR(KEY_MODE)); - ValuesList.add(FPSTR(VALUE_STA)); - } - else - { - if (wifi_get_opmode() == WIFI_AP ) - { - KeysList.add(FPSTR(KEY_MODE)); - ValuesList.add(FPSTR(VALUE_AP)); - } - else - { - KeysList.add(FPSTR(KEY_MODE)); - ValuesList.add(FPSTR(VALUE_AP_STA)); - } + if (wifi_get_opmode() == WIFI_STA ) { + KeysList.add(FPSTR(KEY_MODE)); + ValuesList.add(FPSTR(VALUE_STA)); + } else { + if (wifi_get_opmode() == WIFI_AP ) { + KeysList.add(FPSTR(KEY_MODE)); + ValuesList.add(FPSTR(VALUE_AP)); + } else { + KeysList.add(FPSTR(KEY_MODE)); + ValuesList.add(FPSTR(VALUE_AP_STA)); + } } } // ----------------------------------------------------------------------------- @@ -579,31 +587,31 @@ void GetMode(STORESTRINGS_CLASS & KeysList, STORESTRINGS_CLASS & ValuesList) // ----------------------------------------------------------------------------- void GetPorts(STORESTRINGS_CLASS & KeysList, STORESTRINGS_CLASS & ValuesList) { - //Web port - KeysList.add(FPSTR(KEY_WEB_PORT)); - ValuesList.add(intTostr(wifi_config.iweb_port)); - //Data port - KeysList.add(FPSTR(KEY_DATA_PORT)); - ValuesList.add(intTostr(wifi_config.idata_port)); + //Web port + KeysList.add(FPSTR(KEY_WEB_PORT)); + ValuesList.add(intTostr(wifi_config.iweb_port)); + //Data port + KeysList.add(FPSTR(KEY_DATA_PORT)); + ValuesList.add(intTostr(wifi_config.idata_port)); } // ----------------------------------------------------------------------------- // Helper for Page properties // ----------------------------------------------------------------------------- void SetPageProp(STORESTRINGS_CLASS & KeysList, STORESTRINGS_CLASS & ValuesList, - const __FlashStringHelper *title, const __FlashStringHelper *filename) + const __FlashStringHelper *title, const __FlashStringHelper *filename) { - String fullFilename(filename); - fullFilename.concat(".tpl"); + String fullFilename(filename); + fullFilename.concat(".tpl"); - //page title - KeysList.add(FPSTR(KEY_PAGE_TITLE)); - ValuesList.add(title); - //tpl file name with extension - KeysList.add(FPSTR(KEY_FILE_NAME)); - ValuesList.add(fullFilename); - //tpl file name without extension - KeysList.add(FPSTR(KEY_SHORT_FILE_NAME)); - ValuesList.add(filename); + //page title + KeysList.add(FPSTR(KEY_PAGE_TITLE)); + ValuesList.add(title); + //tpl file name with extension + KeysList.add(FPSTR(KEY_FILE_NAME)); + ValuesList.add(fullFilename); + //tpl file name without extension + KeysList.add(FPSTR(KEY_SHORT_FILE_NAME)); + ValuesList.add(filename); } // ----------------------------------------------------------------------------- @@ -611,13 +619,19 @@ void SetPageProp(STORESTRINGS_CLASS & KeysList, STORESTRINGS_CLASS & ValuesList, // ----------------------------------------------------------------------------- void GetDHCPStatus(STORESTRINGS_CLASS & KeysList, STORESTRINGS_CLASS & ValuesList) { - KeysList.add(FPSTR(KEY_AP_DHCP_STATUS)); - if (wifi_softap_dhcps_status() == DHCP_STARTED) ValuesList.add(FPSTR(VALUE_STARTED)); - else ValuesList.add(FPSTR(VALUE_STOPPED)); + KeysList.add(FPSTR(KEY_AP_DHCP_STATUS)); + if (wifi_softap_dhcps_status() == DHCP_STARTED) { + ValuesList.add(FPSTR(VALUE_STARTED)); + } else { + ValuesList.add(FPSTR(VALUE_STOPPED)); + } - KeysList.add(FPSTR(KEY_STA_DHCP_STATUS)); - if (wifi_station_dhcpc_status()==DHCP_STARTED)ValuesList.add(FPSTR(VALUE_STARTED)); - else ValuesList.add(FPSTR(VALUE_STOPPED)); + KeysList.add(FPSTR(KEY_STA_DHCP_STATUS)); + if (wifi_station_dhcpc_status()==DHCP_STARTED) { + ValuesList.add(FPSTR(VALUE_STARTED)); + } else { + ValuesList.add(FPSTR(VALUE_STOPPED)); + } } // ----------------------------------------------------------------------------- @@ -625,18 +639,18 @@ void GetDHCPStatus(STORESTRINGS_CLASS & KeysList, STORESTRINGS_CLASS & ValuesLis // ----------------------------------------------------------------------------- void ProcessAlertError(STORESTRINGS_CLASS & KeysList, STORESTRINGS_CLASS & ValuesList, String & smsg) { - KeysList.add(FPSTR(KEY_ERROR_MSG)); - ValuesList.add(smsg); - KeysList.add(FPSTR(KEY_SUCCESS_MSG)); - ValuesList.add(""); - KeysList.add(FPSTR(KEY_ERROR_MSG_VISIBILITY )); - ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); - KeysList.add(FPSTR(KEY_SUCCESS_MSG_VISIBILITY)); - ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); - KeysList.add(FPSTR(KEY_SUBMIT_BUTTON_VISIBILITY)); - ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); - KeysList.add(FPSTR(KEY_SERVICE_PAGE)); - ValuesList.add(""); + KeysList.add(FPSTR(KEY_ERROR_MSG)); + ValuesList.add(smsg); + KeysList.add(FPSTR(KEY_SUCCESS_MSG)); + ValuesList.add(""); + KeysList.add(FPSTR(KEY_ERROR_MSG_VISIBILITY )); + ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); + KeysList.add(FPSTR(KEY_SUCCESS_MSG_VISIBILITY)); + ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); + KeysList.add(FPSTR(KEY_SUBMIT_BUTTON_VISIBILITY)); + ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); + KeysList.add(FPSTR(KEY_SERVICE_PAGE)); + ValuesList.add(""); } // ----------------------------------------------------------------------------- @@ -644,16 +658,16 @@ void ProcessAlertError(STORESTRINGS_CLASS & KeysList, STORESTRINGS_CLASS & Value // ----------------------------------------------------------------------------- void ProcessAlertSuccess(STORESTRINGS_CLASS & KeysList, STORESTRINGS_CLASS & ValuesList, String & smsg) { - KeysList.add(FPSTR(KEY_ERROR_MSG)); - ValuesList.add(""); - KeysList.add(FPSTR(KEY_SUCCESS_MSG)); - ValuesList.add(smsg); - KeysList.add(FPSTR(KEY_ERROR_MSG_VISIBILITY )); - ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); - KeysList.add(FPSTR(KEY_SUCCESS_MSG_VISIBILITY)); - ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); - KeysList.add(FPSTR(KEY_SUBMIT_BUTTON_VISIBILITY)); - ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); + KeysList.add(FPSTR(KEY_ERROR_MSG)); + ValuesList.add(""); + KeysList.add(FPSTR(KEY_SUCCESS_MSG)); + ValuesList.add(smsg); + KeysList.add(FPSTR(KEY_ERROR_MSG_VISIBILITY )); + ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); + KeysList.add(FPSTR(KEY_SUCCESS_MSG_VISIBILITY)); + ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); + KeysList.add(FPSTR(KEY_SUBMIT_BUTTON_VISIBILITY)); + ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); } // ----------------------------------------------------------------------------- @@ -661,2261 +675,2269 @@ void ProcessAlertSuccess(STORESTRINGS_CLASS & KeysList, STORESTRINGS_CLASS & Val // ----------------------------------------------------------------------------- void ProcessNoAlert(STORESTRINGS_CLASS & KeysList, STORESTRINGS_CLASS & ValuesList) { - KeysList.add(FPSTR(KEY_ERROR_MSG)); - ValuesList.add(""); - KeysList.add(FPSTR(KEY_SUCCESS_MSG)); - ValuesList.add(""); - KeysList.add(FPSTR(KEY_ERROR_MSG_VISIBILITY )); - ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); - KeysList.add(FPSTR(KEY_SUCCESS_MSG_VISIBILITY)); - ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); - KeysList.add(FPSTR(KEY_SUBMIT_BUTTON_VISIBILITY)); - ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); - KeysList.add(FPSTR(KEY_SERVICE_PAGE)); - ValuesList.add(""); + KeysList.add(FPSTR(KEY_ERROR_MSG)); + ValuesList.add(""); + KeysList.add(FPSTR(KEY_SUCCESS_MSG)); + ValuesList.add(""); + KeysList.add(FPSTR(KEY_ERROR_MSG_VISIBILITY )); + ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); + KeysList.add(FPSTR(KEY_SUCCESS_MSG_VISIBILITY)); + ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); + KeysList.add(FPSTR(KEY_SUBMIT_BUTTON_VISIBILITY)); + ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); + KeysList.add(FPSTR(KEY_SERVICE_PAGE)); + ValuesList.add(""); } //root insterface void handle_web_interface_root() { - String stmp; - long lstatus; - int istatus; - byte bbuf; - STORESTRINGS_CLASS KeysList ; - STORESTRINGS_CLASS ValuesList ; - struct softap_config apconfig; - struct ip_info info; - uint8_t mac [WL_MAC_ADDR_LENGTH]; + String stmp; + long lstatus; + int istatus; + byte bbuf; + STORESTRINGS_CLASS KeysList ; + STORESTRINGS_CLASS ValuesList ; + struct softap_config apconfig; + struct ip_info info; + uint8_t mac [WL_MAC_ADDR_LENGTH]; - KeysList.add(FPSTR(KEY_DISCONNECT_VISIBILITY)); - if (web_interface->is_authenticated())ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); - else ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); + KeysList.add(FPSTR(KEY_DISCONNECT_VISIBILITY)); + if (web_interface->is_authenticated()) { + ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); + } else { + ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); + } - //IP+Web - GetIpWeb(KeysList, ValuesList); + //IP+Web + GetIpWeb(KeysList, ValuesList); - //Hostname - if (wifi_get_opmode()==WIFI_STA ) - { - KeysList.add(FPSTR(KEY_MODE)); - ValuesList.add(FPSTR(VALUE_STA)); - KeysList.add(FPSTR(KEY_HOSTNAME)); - ValuesList.add(wifi_config.get_hostname()); - KeysList.add(FPSTR(KEY_HOSTNAME_VISIBLE)); - ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); - } - else - { - KeysList.add(FPSTR(KEY_HOSTNAME)); - ValuesList.add(FPSTR(KEY_NOT_APPLICABLE_4_AP)); - KeysList.add(FPSTR(KEY_HOSTNAME_VISIBLE)); - ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); - if (wifi_get_opmode()==WIFI_AP ) - { - KeysList.add(FPSTR(KEY_MODE)); - ValuesList.add(FPSTR(VALUE_AP)); - } - else - { - KeysList.add(FPSTR(KEY_MODE)); - ValuesList.add(FPSTR(VALUE_AP_STA)); - } - } + //Hostname + if (wifi_get_opmode()==WIFI_STA ) { + KeysList.add(FPSTR(KEY_MODE)); + ValuesList.add(FPSTR(VALUE_STA)); + KeysList.add(FPSTR(KEY_HOSTNAME)); + ValuesList.add(wifi_config.get_hostname()); + KeysList.add(FPSTR(KEY_HOSTNAME_VISIBLE)); + ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); + } else { + KeysList.add(FPSTR(KEY_HOSTNAME)); + ValuesList.add(FPSTR(KEY_NOT_APPLICABLE_4_AP)); + KeysList.add(FPSTR(KEY_HOSTNAME_VISIBLE)); + ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); + if (wifi_get_opmode()==WIFI_AP ) { + KeysList.add(FPSTR(KEY_MODE)); + ValuesList.add(FPSTR(VALUE_AP)); + } else { + KeysList.add(FPSTR(KEY_MODE)); + ValuesList.add(FPSTR(VALUE_AP_STA)); + } + } - //page title and filenames - SetPageProp(KeysList,ValuesList,FPSTR(VALUE_HOME),F("home")); - //menu item - KeysList.add(FPSTR(KEY_MENU_HOME)); - ValuesList.add(FPSTR(VALUE_ACTIVE)); - - //Chip ID - KeysList.add(FPSTR(KEY_CHIP_ID)); - ValuesList.add(intTostr(system_get_chip_id())); - //CPU Freq - KeysList.add(FPSTR(KEY_CPU_FREQ)); - ValuesList.add(intTostr(system_get_cpu_freq())); - //SDK Version - KeysList.add(FPSTR(KEY_SDK_VER)); - ValuesList.add(system_get_sdk_version()); + //page title and filenames + SetPageProp(KeysList,ValuesList,FPSTR(VALUE_HOME),F("home")); + //menu item + KeysList.add(FPSTR(KEY_MENU_HOME)); + ValuesList.add(FPSTR(VALUE_ACTIVE)); - //MDNS Feature - #ifdef MDNS_FEATURE - KeysList.add(FPSTR(KEY_MDNS_NAME)); - stmp="http://"; - stmp+=wifi_config.get_hostname(); - stmp+=".local"; - ValuesList.add(stmp); - KeysList.add(FPSTR(KEY_MDNS_VISIBLE)); - ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); - #else - KeysList.add(FPSTR(KEY_MDNS_NAME)); - ValuesList.add(FPSTR(VALUE_DISABLED)); - KeysList.add(FPSTR(KEY_MDNS_VISIBLE)); - ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); - #endif + //Chip ID + KeysList.add(FPSTR(KEY_CHIP_ID)); + ValuesList.add(intTostr(system_get_chip_id())); + //CPU Freq + KeysList.add(FPSTR(KEY_CPU_FREQ)); + ValuesList.add(intTostr(system_get_cpu_freq())); + //SDK Version + KeysList.add(FPSTR(KEY_SDK_VER)); + ValuesList.add(system_get_sdk_version()); - //SSDP Feature - #ifdef SSDP_FEATURE - KeysList.add(FPSTR(KEY_SSDP_STATUS)); - ValuesList.add(FPSTR(VALUE_ENABLED)); - KeysList.add(FPSTR(KEY_SSDP_VISIBLE)); - ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); - #else - KeysList.add(FPSTR(KEY_SSDP_STATUS)); - ValuesList.add(FPSTR(VALUE_DISABLED)); - KeysList.add(FPSTR(KEY_SSDP_VISIBLE)); - ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); - #endif + //MDNS Feature +#ifdef MDNS_FEATURE + KeysList.add(FPSTR(KEY_MDNS_NAME)); + stmp="http://"; + stmp+=wifi_config.get_hostname(); + stmp+=".local"; + ValuesList.add(stmp); + KeysList.add(FPSTR(KEY_MDNS_VISIBLE)); + ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); +#else + KeysList.add(FPSTR(KEY_MDNS_NAME)); + ValuesList.add(FPSTR(VALUE_DISABLED)); + KeysList.add(FPSTR(KEY_MDNS_VISIBLE)); + ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); +#endif - //Captive portal Feature - #ifdef CAPTIVE_PORTAL_FEATURE - if (wifi_get_opmode()==WIFI_AP) - { - KeysList.add(FPSTR(KEY_CAPTIVE_PORTAL_STATUS)); - ValuesList.add(FPSTR(VALUE_ENABLED)); - KeysList.add(FPSTR(KEY_CAPTIVE_PORTAL_VISIBLE)); - ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); - } - else - { - KeysList.add(FPSTR(KEY_CAPTIVE_PORTAL_STATUS)); - ValuesList.add(FPSTR(VALUE_DISABLED)); - KeysList.add(FPSTR(KEY_CAPTIVE_PORTAL_VISIBLE)); - ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); - } - #else - KeysList.add(FPSTR(KEY_CAPTIVE_PORTAL_STATUS)); - ValuesList.add(FPSTR(VALUE_DISABLED)); - KeysList.add(FPSTR(KEY_CAPTIVE_PORTAL_VISIBLE)); - ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); - #endif + //SSDP Feature +#ifdef SSDP_FEATURE + KeysList.add(FPSTR(KEY_SSDP_STATUS)); + ValuesList.add(FPSTR(VALUE_ENABLED)); + KeysList.add(FPSTR(KEY_SSDP_VISIBLE)); + ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); +#else + KeysList.add(FPSTR(KEY_SSDP_STATUS)); + ValuesList.add(FPSTR(VALUE_DISABLED)); + KeysList.add(FPSTR(KEY_SSDP_VISIBLE)); + ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); +#endif - //network - KeysList.add(FPSTR(KEY_NET_PHY)); - if (wifi_get_phy_mode()==PHY_MODE_11B) ValuesList.add(FPSTR(VALUE_11B)); - else if (wifi_get_phy_mode()==PHY_MODE_11G)ValuesList.add(FPSTR(VALUE_11G)); - else ValuesList.add(FPSTR(VALUE_11N)); - //sleep mode - KeysList.add(FPSTR(KEY_SLEEP_MODE)); - if (wifi_get_sleep_type()==NONE_SLEEP_T) ValuesList.add(FPSTR(VALUE_NONE)); - else if (wifi_get_sleep_type()==LIGHT_SLEEP_T) ValuesList.add(FPSTR(VALUE_LIGHT)); - else ValuesList.add(FPSTR(VALUE_MODEM)); - //Boot version - KeysList.add(FPSTR(KEY_BOOT_VER)); - ValuesList.add(intTostr(system_get_boot_version())); - //Baud rate - KeysList.add(FPSTR(KEY_BAUD_RATE)); - ValuesList.add(intTostr(wifi_config.baud_rate)); - // Web and Data ports - GetPorts(KeysList, ValuesList); + //Captive portal Feature +#ifdef CAPTIVE_PORTAL_FEATURE + if (wifi_get_opmode()==WIFI_AP) { + KeysList.add(FPSTR(KEY_CAPTIVE_PORTAL_STATUS)); + ValuesList.add(FPSTR(VALUE_ENABLED)); + KeysList.add(FPSTR(KEY_CAPTIVE_PORTAL_VISIBLE)); + ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); + } else { + KeysList.add(FPSTR(KEY_CAPTIVE_PORTAL_STATUS)); + ValuesList.add(FPSTR(VALUE_DISABLED)); + KeysList.add(FPSTR(KEY_CAPTIVE_PORTAL_VISIBLE)); + ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); + } +#else + KeysList.add(FPSTR(KEY_CAPTIVE_PORTAL_STATUS)); + ValuesList.add(FPSTR(VALUE_DISABLED)); + KeysList.add(FPSTR(KEY_CAPTIVE_PORTAL_VISIBLE)); + ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); +#endif - //AP part - if (wifi_get_opmode()==WIFI_AP || wifi_get_opmode()==WIFI_AP_STA) - { - int client_counter=0; - struct station_info * station; - //AP is enabled - KeysList.add(FPSTR(KEY_AP_STATUS_ENABLED)); - ValuesList.add(FPSTR(VALUE_ENABLED)); - //set visible - KeysList.add(FPSTR(KEY_AP_VISIBILITY)); - ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); - //list of connected clients - station = wifi_softap_get_station_info(); - while(station) - { - //Row number - stmp = "$ROW_NUMBER["+String(client_counter)+"]$"; - KeysList.add(stmp.c_str()); - stmp=String(client_counter+1); - ValuesList.add(stmp); - //BSSID - stmp = "$MAC_CONNECTED["+String(client_counter)+"]$"; - KeysList.add(stmp.c_str()); - ValuesList.add(wifi_config.mac2str(station->bssid)); - //IP - stmp = "$IP_CONNECTED["+String(client_counter)+"]$"; - KeysList.add(stmp.c_str()); - ValuesList.add(wifi_config.ip2str((byte *)&station->ip)); - //increment counter - client_counter++; - //go next record - station = STAILQ_NEXT(station, next); - } - wifi_softap_free_station_info(); - //Connected clients - KeysList.add(FPSTR(KEY_CONNECTED_STATIONS_NB_ITEMS)); - ValuesList.add(intTostr(client_counter)); - } - else - { - //AP is disabled - KeysList.add(FPSTR(KEY_AP_STATUS_ENABLED)); - ValuesList.add(FPSTR(VALUE_DISABLED)); - //set hide - KeysList.add(FPSTR(KEY_AP_VISIBILITY)); - ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); - //Connected clients - KeysList.add(FPSTR(KEY_CONNECTED_STATIONS_NB_ITEMS)); - ValuesList.add("0"); - } - //AP mac address - KeysList.add(FPSTR(KEY_AP_MAC)); - ValuesList.add(wifi_config.mac2str(WiFi.softAPmacAddress(mac))); - //AP configuration - if (wifi_softap_get_config(&apconfig)) - { - //SSID - KeysList.add(FPSTR(KEY_AP_SSID)); - ValuesList.add((char *)(apconfig.ssid)); - //AP visible or hidden - KeysList.add(FPSTR(KEY_AP_IS_VISIBLE)); - if(apconfig.ssid_hidden==1)ValuesList.add(FPSTR(VALUE_NO)); - else ValuesList.add(FPSTR(VALUE_YES)); - //Channel - KeysList.add(FPSTR(KEY_AP_CHANNEL)); - ValuesList.add(intTostr(apconfig.channel)); - //Authentification mode - KeysList.add(FPSTR(KEY_AP_AUTH)); - if (apconfig.authmode==AUTH_OPEN)ValuesList.add(FPSTR(VALUE_NONE)); - else if (apconfig.authmode==AUTH_WEP)ValuesList.add(FPSTR(VALUE_WEP)); - else if (apconfig.authmode==AUTH_WPA_PSK)ValuesList.add(FPSTR(VALUE_WPA)); - else if (apconfig.authmode==AUTH_WPA2_PSK)ValuesList.add(FPSTR(VALUE_WPA2)); - else ValuesList.add(FPSTR(VALUE_WPAWPA2)); + //network + KeysList.add(FPSTR(KEY_NET_PHY)); + if (wifi_get_phy_mode()==PHY_MODE_11B) { + ValuesList.add(FPSTR(VALUE_11B)); + } else if (wifi_get_phy_mode()==PHY_MODE_11G) { + ValuesList.add(FPSTR(VALUE_11G)); + } else { + ValuesList.add(FPSTR(VALUE_11N)); + } + //sleep mode + KeysList.add(FPSTR(KEY_SLEEP_MODE)); + if (wifi_get_sleep_type()==NONE_SLEEP_T) { + ValuesList.add(FPSTR(VALUE_NONE)); + } else if (wifi_get_sleep_type()==LIGHT_SLEEP_T) { + ValuesList.add(FPSTR(VALUE_LIGHT)); + } else { + ValuesList.add(FPSTR(VALUE_MODEM)); + } + //Boot version + KeysList.add(FPSTR(KEY_BOOT_VER)); + ValuesList.add(intTostr(system_get_boot_version())); + //Baud rate + KeysList.add(FPSTR(KEY_BAUD_RATE)); + ValuesList.add(intTostr(wifi_config.baud_rate)); + // Web and Data ports + GetPorts(KeysList, ValuesList); - //Max connections - KeysList.add(FPSTR(KEY_AP_MAX_CON)); - ValuesList.add(intTostr(apconfig.max_connection)); - } - else - { - //SSID - KeysList.add(FPSTR(KEY_AP_SSID)); - ValuesList.add(FPSTR(VALUE_NOT_AVAILABLE)); - //AP visible or hidden - KeysList.add(FPSTR(KEY_AP_IS_VISIBLE)); - ValuesList.add(FPSTR(VALUE_NOT_AVAILABLE)); - //Channel - KeysList.add(FPSTR(KEY_AP_CHANNEL)); - ValuesList.add(FPSTR(VALUE_NOT_AVAILABLE)); - //Authentification mode - KeysList.add(FPSTR(KEY_AP_AUTH)); - ValuesList.add(FPSTR(VALUE_NOT_AVAILABLE)); - //Max connections - KeysList.add(FPSTR(KEY_AP_MAX_CON)); - ValuesList.add(FPSTR(VALUE_NOT_AVAILABLE)); - } - //DHCP Status - GetDHCPStatus(KeysList, ValuesList); - //IP/GW/MASK - if (wifi_get_ip_info(SOFTAP_IF,&info)) - { - //IP address - KeysList.add(FPSTR(KEY_AP_IP)); - ValuesList.add(wifi_config.ip2str(info.ip.addr)); - //GW address - KeysList.add(FPSTR(KEY_AP_GW)); - ValuesList.add(wifi_config.ip2str(info.gw.addr)); - //Sub Net Mask - KeysList.add(FPSTR(KEY_AP_SUBNET)); - ValuesList.add(wifi_config.ip2str(info.netmask.addr)); - } - else - { - //IP address - KeysList.add(FPSTR(KEY_AP_IP)); - ValuesList.add(FPSTR(VALUE_NO_IP)); - //GW address - KeysList.add(FPSTR(KEY_AP_GW)); - ValuesList.add(FPSTR(VALUE_NO_IP)); - //Sub Net Mask - KeysList.add(FPSTR(KEY_AP_SUBNET)); - ValuesList.add(FPSTR(VALUE_NO_IP)); - } - //STA part - if (wifi_get_opmode()==WIFI_STA || wifi_get_opmode()==WIFI_AP_STA) - { - //STA is enabled - KeysList.add(FPSTR(KEY_STA_STATUS_ENABLED)); - ValuesList.add(FPSTR(VALUE_ENABLED)); - //set visible - KeysList.add(FPSTR(KEY_STA_VISIBILITY)); - ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); - } - else - { - //STA is disabled - KeysList.add(FPSTR(KEY_STA_STATUS_ENABLED)); - ValuesList.add(FPSTR(VALUE_DISABLED)); - //set hide - KeysList.add(FPSTR(KEY_STA_VISIBILITY)); - ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); - } - //STA mac address - KeysList.add(FPSTR(KEY_STA_MAC)); - ValuesList.add(wifi_config.mac2str(WiFi.macAddress(mac))); - //SSID used by STA - KeysList.add(FPSTR(KEY_STA_SSID)); - if (WiFi.SSID().length()==0)ValuesList.add(FPSTR(VALUE_NOT_AVAILABLE)); - else ValuesList.add(WiFi.SSID().c_str()); - //Channel - KeysList.add(FPSTR(KEY_STA_CHANNEL)); - ValuesList.add(intTostr (wifi_get_channel())); - //Connection status - istatus = wifi_station_get_connect_status(); - KeysList.add(FPSTR(KEY_STA_STATUS)); - if (istatus==STATION_GOT_IP) ValuesList.add(FPSTR(VALUE_CONNECTED)); - else if (istatus==STATION_NO_AP_FOUND) ValuesList.add(FPSTR(VALUE_NO_SSID)); - else if (istatus==STATION_CONNECT_FAIL) ValuesList.add(FPSTR(VALUE_CONNECTION_FAILED)); - else if (istatus==STATION_WRONG_PASSWORD) ValuesList.add(FPSTR(VALUE_CONNECTION_FAILED2)); - else if (istatus==STATION_IDLE) ValuesList.add(FPSTR(VALUE_IDLE));//should not happen - else ValuesList.add(FPSTR(VALUE_DISCONNECTED)); - //DHCP Client status - GetDHCPStatus(KeysList, ValuesList); - //IP address - KeysList.add(FPSTR(KEY_STA_IP)); - ValuesList.add(WiFi.localIP().toString().c_str()); - //GW address - KeysList.add(FPSTR(KEY_STA_GW)); - ValuesList.add(WiFi.gatewayIP().toString().c_str()); - //Sub Net Mask - KeysList.add(FPSTR(KEY_STA_SUBNET)); - ValuesList.add(WiFi.subnetMask().toString().c_str()); - //Service page / no need refresh on this page - KeysList.add(FPSTR(KEY_SERVICE_PAGE)); - ValuesList.add(""); - //Firmware & Free Mem, at the end to reflect situation - GetFreeMem(KeysList, ValuesList); + //AP part + if (wifi_get_opmode()==WIFI_AP || wifi_get_opmode()==WIFI_AP_STA) { + int client_counter=0; + struct station_info * station; + //AP is enabled + KeysList.add(FPSTR(KEY_AP_STATUS_ENABLED)); + ValuesList.add(FPSTR(VALUE_ENABLED)); + //set visible + KeysList.add(FPSTR(KEY_AP_VISIBILITY)); + ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); + //list of connected clients + station = wifi_softap_get_station_info(); + while(station) { + //Row number + stmp = "$ROW_NUMBER["+String(client_counter)+"]$"; + KeysList.add(stmp.c_str()); + stmp=String(client_counter+1); + ValuesList.add(stmp); + //BSSID + stmp = "$MAC_CONNECTED["+String(client_counter)+"]$"; + KeysList.add(stmp.c_str()); + ValuesList.add(wifi_config.mac2str(station->bssid)); + //IP + stmp = "$IP_CONNECTED["+String(client_counter)+"]$"; + KeysList.add(stmp.c_str()); + ValuesList.add(wifi_config.ip2str((byte *)&station->ip)); + //increment counter + client_counter++; + //go next record + station = STAILQ_NEXT(station, next); + } + wifi_softap_free_station_info(); + //Connected clients + KeysList.add(FPSTR(KEY_CONNECTED_STATIONS_NB_ITEMS)); + ValuesList.add(intTostr(client_counter)); + } else { + //AP is disabled + KeysList.add(FPSTR(KEY_AP_STATUS_ENABLED)); + ValuesList.add(FPSTR(VALUE_DISABLED)); + //set hide + KeysList.add(FPSTR(KEY_AP_VISIBILITY)); + ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); + //Connected clients + KeysList.add(FPSTR(KEY_CONNECTED_STATIONS_NB_ITEMS)); + ValuesList.add("0"); + } + //AP mac address + KeysList.add(FPSTR(KEY_AP_MAC)); + ValuesList.add(wifi_config.mac2str(WiFi.softAPmacAddress(mac))); + //AP configuration + if (wifi_softap_get_config(&apconfig)) { + //SSID + KeysList.add(FPSTR(KEY_AP_SSID)); + ValuesList.add((char *)(apconfig.ssid)); + //AP visible or hidden + KeysList.add(FPSTR(KEY_AP_IS_VISIBLE)); + if(apconfig.ssid_hidden==1) { + ValuesList.add(FPSTR(VALUE_NO)); + } else { + ValuesList.add(FPSTR(VALUE_YES)); + } + //Channel + KeysList.add(FPSTR(KEY_AP_CHANNEL)); + ValuesList.add(intTostr(apconfig.channel)); + //Authentification mode + KeysList.add(FPSTR(KEY_AP_AUTH)); + if (apconfig.authmode==AUTH_OPEN) { + ValuesList.add(FPSTR(VALUE_NONE)); + } else if (apconfig.authmode==AUTH_WEP) { + ValuesList.add(FPSTR(VALUE_WEP)); + } else if (apconfig.authmode==AUTH_WPA_PSK) { + ValuesList.add(FPSTR(VALUE_WPA)); + } else if (apconfig.authmode==AUTH_WPA2_PSK) { + ValuesList.add(FPSTR(VALUE_WPA2)); + } else { + ValuesList.add(FPSTR(VALUE_WPAWPA2)); + } - //process the template file and provide list of variables - processTemplate("/home.tpl", KeysList , ValuesList); - //need to clean to speed up memory recovery - KeysList.clear(); - ValuesList.clear(); + //Max connections + KeysList.add(FPSTR(KEY_AP_MAX_CON)); + ValuesList.add(intTostr(apconfig.max_connection)); + } else { + //SSID + KeysList.add(FPSTR(KEY_AP_SSID)); + ValuesList.add(FPSTR(VALUE_NOT_AVAILABLE)); + //AP visible or hidden + KeysList.add(FPSTR(KEY_AP_IS_VISIBLE)); + ValuesList.add(FPSTR(VALUE_NOT_AVAILABLE)); + //Channel + KeysList.add(FPSTR(KEY_AP_CHANNEL)); + ValuesList.add(FPSTR(VALUE_NOT_AVAILABLE)); + //Authentification mode + KeysList.add(FPSTR(KEY_AP_AUTH)); + ValuesList.add(FPSTR(VALUE_NOT_AVAILABLE)); + //Max connections + KeysList.add(FPSTR(KEY_AP_MAX_CON)); + ValuesList.add(FPSTR(VALUE_NOT_AVAILABLE)); + } + //DHCP Status + GetDHCPStatus(KeysList, ValuesList); + //IP/GW/MASK + if (wifi_get_ip_info(SOFTAP_IF,&info)) { + //IP address + KeysList.add(FPSTR(KEY_AP_IP)); + ValuesList.add(wifi_config.ip2str(info.ip.addr)); + //GW address + KeysList.add(FPSTR(KEY_AP_GW)); + ValuesList.add(wifi_config.ip2str(info.gw.addr)); + //Sub Net Mask + KeysList.add(FPSTR(KEY_AP_SUBNET)); + ValuesList.add(wifi_config.ip2str(info.netmask.addr)); + } else { + //IP address + KeysList.add(FPSTR(KEY_AP_IP)); + ValuesList.add(FPSTR(VALUE_NO_IP)); + //GW address + KeysList.add(FPSTR(KEY_AP_GW)); + ValuesList.add(FPSTR(VALUE_NO_IP)); + //Sub Net Mask + KeysList.add(FPSTR(KEY_AP_SUBNET)); + ValuesList.add(FPSTR(VALUE_NO_IP)); + } + //STA part + if (wifi_get_opmode()==WIFI_STA || wifi_get_opmode()==WIFI_AP_STA) { + //STA is enabled + KeysList.add(FPSTR(KEY_STA_STATUS_ENABLED)); + ValuesList.add(FPSTR(VALUE_ENABLED)); + //set visible + KeysList.add(FPSTR(KEY_STA_VISIBILITY)); + ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE)); + } else { + //STA is disabled + KeysList.add(FPSTR(KEY_STA_STATUS_ENABLED)); + ValuesList.add(FPSTR(VALUE_DISABLED)); + //set hide + KeysList.add(FPSTR(KEY_STA_VISIBILITY)); + ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN)); + } + //STA mac address + KeysList.add(FPSTR(KEY_STA_MAC)); + ValuesList.add(wifi_config.mac2str(WiFi.macAddress(mac))); + //SSID used by STA + KeysList.add(FPSTR(KEY_STA_SSID)); + if (WiFi.SSID().length()==0) { + ValuesList.add(FPSTR(VALUE_NOT_AVAILABLE)); + } else { + ValuesList.add(WiFi.SSID().c_str()); + } + //Channel + KeysList.add(FPSTR(KEY_STA_CHANNEL)); + ValuesList.add(intTostr (wifi_get_channel())); + //Connection status + istatus = wifi_station_get_connect_status(); + KeysList.add(FPSTR(KEY_STA_STATUS)); + if (istatus==STATION_GOT_IP) { + ValuesList.add(FPSTR(VALUE_CONNECTED)); + } else if (istatus==STATION_NO_AP_FOUND) { + ValuesList.add(FPSTR(VALUE_NO_SSID)); + } else if (istatus==STATION_CONNECT_FAIL) { + ValuesList.add(FPSTR(VALUE_CONNECTION_FAILED)); + } else if (istatus==STATION_WRONG_PASSWORD) { + ValuesList.add(FPSTR(VALUE_CONNECTION_FAILED2)); + } else if (istatus==STATION_IDLE) { + ValuesList.add(FPSTR(VALUE_IDLE)); //should not happen + } else { + ValuesList.add(FPSTR(VALUE_DISCONNECTED)); + } + //DHCP Client status + GetDHCPStatus(KeysList, ValuesList); + //IP address + KeysList.add(FPSTR(KEY_STA_IP)); + ValuesList.add(WiFi.localIP().toString().c_str()); + //GW address + KeysList.add(FPSTR(KEY_STA_GW)); + ValuesList.add(WiFi.gatewayIP().toString().c_str()); + //Sub Net Mask + KeysList.add(FPSTR(KEY_STA_SUBNET)); + ValuesList.add(WiFi.subnetMask().toString().c_str()); + //Service page / no need refresh on this page + KeysList.add(FPSTR(KEY_SERVICE_PAGE)); + ValuesList.add(""); + //Firmware & Free Mem, at the end to reflect situation + GetFreeMem(KeysList, ValuesList); + + //process the template file and provide list of variables + processTemplate("/home.tpl", KeysList , ValuesList); + //need to clean to speed up memory recovery + KeysList.clear(); + ValuesList.clear(); } void handle_web_interface_configSys() { - static const char NOT_AUTH_CS [] PROGMEM = "HTTP/1.1 301 OK\r\nLocation: /LOGIN?return=CONFIGSYS\r\nCache-Control: no-cache\r\n\r\n"; - - String stmp,smsg; - long lstatus; - int istatus; - byte bbuf; - long ibaud=DEFAULT_BAUD_RATE; - int iweb_port =DEFAULT_WEB_PORT; - int idata_port =DEFAULT_DATA_PORT; - byte bsleepmode=DEFAULT_SLEEP_MODE; - bool msg_alert_error=false; - bool msg_alert_success=false; - long lbaudlist[] = {9600 ,19200,38400,57600,115200,230400,250000,-1}; - int bmodemvaluelist[] = {NONE_SLEEP_T,LIGHT_SLEEP_T,MODEM_SLEEP_T, -1}; - const __FlashStringHelper *smodemdisplaylist[]={FPSTR(VALUE_NONE),FPSTR(VALUE_LIGHT),FPSTR(VALUE_MODEM),FPSTR(VALUE_MODEM)}; - STORESTRINGS_CLASS KeysList ; - STORESTRINGS_CLASS ValuesList ; + static const char NOT_AUTH_CS [] PROGMEM = "HTTP/1.1 301 OK\r\nLocation: /LOGIN?return=CONFIGSYS\r\nCache-Control: no-cache\r\n\r\n"; - if (!web_interface->is_authenticated()) - { - web_interface->WebServer.sendContent_P(NOT_AUTH_CS); - return; - } + String stmp,smsg; + long lstatus; + int istatus; + byte bbuf; + long ibaud=DEFAULT_BAUD_RATE; + int iweb_port =DEFAULT_WEB_PORT; + int idata_port =DEFAULT_DATA_PORT; + byte bsleepmode=DEFAULT_SLEEP_MODE; + bool msg_alert_error=false; + bool msg_alert_success=false; + long lbaudlist[] = {9600 ,19200,38400,57600,115200,230400,250000,-1}; + int bmodemvaluelist[] = {NONE_SLEEP_T,LIGHT_SLEEP_T,MODEM_SLEEP_T, -1}; + const __FlashStringHelper *smodemdisplaylist[]= {FPSTR(VALUE_NONE),FPSTR(VALUE_LIGHT),FPSTR(VALUE_MODEM),FPSTR(VALUE_MODEM)}; + STORESTRINGS_CLASS KeysList ; + STORESTRINGS_CLASS ValuesList ; - //IP+Web - GetIpWeb(KeysList, ValuesList); - //mode - GetMode(KeysList, ValuesList); - //page title and filenames - SetPageProp(KeysList,ValuesList,FPSTR(VALUE_HOME),F("system")); - //menu item - KeysList.add(FPSTR(KEY_MENU_SYSTEM)); - ValuesList.add(FPSTR(VALUE_ACTIVE)); + if (!web_interface->is_authenticated()) { + web_interface->WebServer.sendContent_P(NOT_AUTH_CS); + return; + } - //check is it is a submission or a display - if (web_interface->WebServer.hasArg("SUBMIT")) - { //is there a correct list of values? - if (web_interface->WebServer.hasArg("BAUD_RATE") && web_interface->WebServer.hasArg("SLEEP_MODE")&& web_interface->WebServer.hasArg("DATAPORT")&& web_interface->WebServer.hasArg("WEBPORT")) - { //is each value correct ? - ibaud = web_interface->WebServer.arg("BAUD_RATE").toInt(); - iweb_port = web_interface->WebServer.arg("WEBPORT").toInt(); - idata_port = web_interface->WebServer.arg("DATAPORT").toInt(); - bsleepmode = web_interface->WebServer.arg("SLEEP_MODE").toInt(); + //IP+Web + GetIpWeb(KeysList, ValuesList); + //mode + GetMode(KeysList, ValuesList); + //page title and filenames + SetPageProp(KeysList,ValuesList,FPSTR(VALUE_HOME),F("system")); + //menu item + KeysList.add(FPSTR(KEY_MENU_SYSTEM)); + ValuesList.add(FPSTR(VALUE_ACTIVE)); - if (!(iweb_port>0 && iweb_port<65001)) - { - msg_alert_error=true; - smsg.concat(F("Error: invalid port value for web port
")); - KeysList.add(FPSTR(KEY_WEB_PORT_STATUS)); - ValuesList.add(FPSTR(VALUE_HAS_ERROR)); - } - if (!(idata_port>0 && idata_port<65001)) - { - msg_alert_error=true; - smsg.concat("Error: invalid port value for data port
"); - KeysList.add(FPSTR(KEY_DATA_PORT_STATUS)); - ValuesList.add(FPSTR(VALUE_HAS_ERROR)); - } - if (iweb_port== idata_port) - { - msg_alert_error=true; - smsg.concat("Error: web port and data port cannot be identical
"); - KeysList.add(FPSTR(KEY_WEB_PORT_STATUS)); - ValuesList.add(FPSTR(VALUE_HAS_ERROR)); - KeysList.add(FPSTR(KEY_DATA_PORT_STATUS)); - ValuesList.add(FPSTR(VALUE_HAS_ERROR)); - } - if (!(ibaud==9600 || ibaud==19200|| ibaud==38400|| ibaud==57600|| ibaud==115200|| ibaud==230400 || ibaud==250000)) - { - msg_alert_error=true; - smsg.concat(F("Error: value for baud rate is not correct
")); - KeysList.add(FPSTR(KEY_BAUD_RATE_STATUS)); - ValuesList.add(FPSTR(VALUE_HAS_ERROR)); - } - if (!(bsleepmode==NONE_SLEEP_T ||bsleepmode==LIGHT_SLEEP_T ||bsleepmode==MODEM_SLEEP_T )) - { - msg_alert_error=true; - smsg.concat(F("Error: value for sleeping mode is not correct
")); - KeysList.add(FPSTR(KEY_SLEEP_MODE_STATUS)); - ValuesList.add(FPSTR(VALUE_HAS_ERROR)); - } - } - else - { - msg_alert_error=true; - smsg = FPSTR(MISSING_DATA); - } - //if no error apply the changes - if (msg_alert_error!=true) - { - if(!CONFIG::write_buffer(EP_BAUD_RATE,(const byte *)&ibaud,INTEGER_LENGTH)||!CONFIG::write_buffer(EP_WEB_PORT,(const byte *)&iweb_port,INTEGER_LENGTH)||!CONFIG::write_buffer(EP_DATA_PORT,(const byte *)&idata_port,INTEGER_LENGTH)||!CONFIG::write_byte(EP_SLEEP_MODE,bsleepmode)) - { - msg_alert_error=true; - smsg = FPSTR(EEPROM_NOWRITE); - } - else - { - msg_alert_success=true; - wifi_config.iweb_port=iweb_port; - wifi_config.idata_port=idata_port; - smsg = F("Changes saved to EEPROM, restarting...."); - } - } - } - else //no submit need to get data from EEPROM - { - if (!CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&ibaud , INTEGER_LENGTH))ibaud=DEFAULT_BAUD_RATE; - if (!CONFIG::read_byte(EP_SLEEP_MODE, &bsleepmode ))bsleepmode=DEFAULT_SLEEP_MODE; - if (!CONFIG::read_buffer(EP_WEB_PORT, (byte *)&iweb_port , INTEGER_LENGTH))iweb_port=DEFAULT_WEB_PORT; - if (!CONFIG::read_buffer(EP_DATA_PORT, (byte *)&idata_port , INTEGER_LENGTH))idata_port=DEFAULT_DATA_PORT; - } - //Baud rate list - istatus = 0; - stmp=""; - while (lbaudlist[istatus]>-1) - { - stmp+="