Code formating using astyle --style=otbs *.h *.cpp *.ino

To get nice looking code with a minimum effort
This commit is contained in:
Luc 2016-01-25 23:38:26 +08:00
parent 0c306a2aff
commit 70228adb3d
12 changed files with 3832 additions and 3639 deletions

View File

@ -15,83 +15,83 @@
#define LinkedList_h #define LinkedList_h
template<class T> template<class T>
struct ListNode struct ListNode {
{ T data;
T data; ListNode<T> *next;
ListNode<T> *next;
}; };
template <typename T> template <typename T>
class LinkedList{ class LinkedList
{
protected: protected:
int _size; int _size;
ListNode<T> *root; ListNode<T> *root;
ListNode<T> *last; ListNode<T> *last;
// Helps "get" method, by saving last position // Helps "get" method, by saving last position
ListNode<T> *lastNodeGot; ListNode<T> *lastNodeGot;
int lastIndexGot; int lastIndexGot;
// isCached should be set to FALSE // isCached should be set to FALSE
// everytime the list suffer changes // everytime the list suffer changes
bool isCached; bool isCached;
ListNode<T>* getNode(int index); ListNode<T>* getNode(int index);
public: public:
LinkedList(); LinkedList();
~LinkedList(); ~LinkedList();
/* /*
Returns current size of LinkedList Returns current size of LinkedList
*/ */
virtual int size(); virtual int size();
/* /*
Adds a T object in the specified index; Adds a T object in the specified index;
Unlink and link the LinkedList correcly; Unlink and link the LinkedList correcly;
Increment _size Increment _size
*/ */
virtual bool add(int index, T); virtual bool add(int index, T);
/* /*
Adds a T object in the end of the LinkedList; Adds a T object in the end of the LinkedList;
Increment _size; Increment _size;
*/ */
virtual bool add(T); virtual bool add(T);
/* /*
Adds a T object in the start of the LinkedList; Adds a T object in the start of the LinkedList;
Increment _size; Increment _size;
*/ */
virtual bool unshift(T); virtual bool unshift(T);
/* /*
Set the object at index, with T; Set the object at index, with T;
Increment _size; Increment _size;
*/ */
virtual bool set(int index, T); virtual bool set(int index, T);
/* /*
Remove object at index; Remove object at index;
If index is not reachable, returns false; If index is not reachable, returns false;
else, decrement _size else, decrement _size
*/ */
virtual T remove(int index); virtual T remove(int index);
/* /*
Remove last object; Remove last object;
*/ */
virtual T pop(); virtual T pop();
/* /*
Remove first object; Remove first object;
*/ */
virtual T shift(); virtual T shift();
/* /*
Get the index'th element on the list; Get the index'th element on the list;
Return Element if accessible, Return Element if accessible,
else, return false; else, return false;
*/ */
virtual T get(int index); virtual T get(int index);
/* /*
Clear the entire array Clear the entire array
*/ */
virtual void clear(); virtual void clear();
}; };
@ -99,29 +99,28 @@ public:
template<typename T> template<typename T>
LinkedList<T>::LinkedList() LinkedList<T>::LinkedList()
{ {
root=NULL; root=NULL;
last=NULL; last=NULL;
_size=0; _size=0;
lastNodeGot = root; lastNodeGot = root;
lastIndexGot = 0; lastIndexGot = 0;
isCached = false; isCached = false;
} }
// Clear Nodes and free Memory // Clear Nodes and free Memory
template<typename T> template<typename T>
LinkedList<T>::~LinkedList() LinkedList<T>::~LinkedList()
{ {
ListNode<T>* tmp; ListNode<T>* tmp;
while(root!=NULL) while(root!=NULL) {
{ tmp=root;
tmp=root; root=root->next;
root=root->next; delete tmp;
delete tmp; }
} last = NULL;
last = NULL; _size=0;
_size=0; isCached = false;
isCached = false;
} }
/* /*
@ -129,196 +128,213 @@ LinkedList<T>::~LinkedList()
*/ */
template<typename T> template<typename T>
ListNode<T>* LinkedList<T>::getNode(int index){ ListNode<T>* LinkedList<T>::getNode(int index)
{
int _pos = 0; int _pos = 0;
ListNode<T>* current = root; ListNode<T>* current = root;
// Check if the node trying to get is // Check if the node trying to get is
// immediatly AFTER the previous got one // immediatly AFTER the previous got one
if(isCached && lastIndexGot <= index){ if(isCached && lastIndexGot <= index) {
_pos = lastIndexGot; _pos = lastIndexGot;
current = lastNodeGot; current = lastNodeGot;
} }
while(_pos < index && current){ while(_pos < index && current) {
current = current->next; current = current->next;
_pos++; _pos++;
} }
// Check if the object index got is the same as the required // Check if the object index got is the same as the required
if(_pos == index){ if(_pos == index) {
isCached = true; isCached = true;
lastIndexGot = index; lastIndexGot = index;
lastNodeGot = current; lastNodeGot = current;
return current; return current;
} }
return NULL; return NULL;
} }
template<typename T> template<typename T>
int LinkedList<T>::size(){ int LinkedList<T>::size()
return _size; {
return _size;
} }
template<typename T> template<typename T>
bool LinkedList<T>::add(int index, T _t){ bool LinkedList<T>::add(int index, T _t)
{
if(index >= _size) if(index >= _size) {
return add(_t); return add(_t);
}
if(index == 0) if(index == 0) {
return unshift(_t); return unshift(_t);
}
ListNode<T> *tmp = new ListNode<T>(), ListNode<T> *tmp = new ListNode<T>(),
*_prev = getNode(index-1); *_prev = getNode(index-1);
tmp->data = _t; tmp->data = _t;
tmp->next = _prev->next; tmp->next = _prev->next;
_prev->next = tmp; _prev->next = tmp;
_size++; _size++;
isCached = false; isCached = false;
return true; return true;
} }
template<typename T> template<typename T>
bool LinkedList<T>::add(T _t){ bool LinkedList<T>::add(T _t)
{
ListNode<T> *tmp = new ListNode<T>(); ListNode<T> *tmp = new ListNode<T>();
tmp->data = _t; tmp->data = _t;
tmp->next = NULL; tmp->next = NULL;
if(root){
// Already have elements inserted
last->next = tmp;
last = tmp;
}else{
// First element being inserted
root = tmp;
last = tmp;
}
_size++; if(root) {
isCached = false; // 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<typename T> template<typename T>
bool LinkedList<T>::unshift(T _t){ bool LinkedList<T>::unshift(T _t)
{
if(_size == 0) if(_size == 0) {
return add(_t); return add(_t);
}
ListNode<T> *tmp = new ListNode<T>(); ListNode<T> *tmp = new ListNode<T>();
tmp->next = root; tmp->next = root;
tmp->data = _t; tmp->data = _t;
root = tmp; root = tmp;
_size++; _size++;
isCached = false; isCached = false;
return true; return true;
} }
template<typename T> template<typename T>
bool LinkedList<T>::set(int index, T _t){ bool LinkedList<T>::set(int index, T _t)
// Check if index position is in bounds {
if(index < 0 || index >= _size) // Check if index position is in bounds
return false; if(index < 0 || index >= _size) {
return false;
}
getNode(index)->data = _t; getNode(index)->data = _t;
return true; return true;
} }
template<typename T> template<typename T>
T LinkedList<T>::pop(){ T LinkedList<T>::pop()
if(_size <= 0) {
return T(); if(_size <= 0) {
return T();
isCached = false; }
if(_size >= 2){ isCached = false;
ListNode<T> *tmp = getNode(_size - 2);
T ret = tmp->next->data; if(_size >= 2) {
delete(tmp->next); ListNode<T> *tmp = getNode(_size - 2);
tmp->next = NULL; T ret = tmp->next->data;
last = tmp; delete(tmp->next);
_size--; tmp->next = NULL;
return ret; last = tmp;
}else{ _size--;
// Only one element left on the list return ret;
T ret = root->data; } else {
delete(root); // Only one element left on the list
root = NULL; T ret = root->data;
last = NULL; delete(root);
_size = 0; root = NULL;
return ret; last = NULL;
} _size = 0;
return ret;
}
} }
template<typename T> template<typename T>
T LinkedList<T>::shift(){ T LinkedList<T>::shift()
if(_size <= 0) {
return T(); if(_size <= 0) {
return T();
}
if(_size > 1){ if(_size > 1) {
ListNode<T> *_next = root->next; ListNode<T> *_next = root->next;
T ret = root->data; T ret = root->data;
delete(root); delete(root);
root = _next; root = _next;
_size --; _size --;
isCached = false; isCached = false;
return ret; return ret;
}else{ } else {
// Only one left, then pop() // Only one left, then pop()
return pop(); return pop();
} }
} }
template<typename T> template<typename T>
T LinkedList<T>::remove(int index){ T LinkedList<T>::remove(int index)
if (index < 0 || index >= _size) {
{ if (index < 0 || index >= _size) {
return T(); return T();
} }
if(index == 0) if(index == 0) {
return shift(); return shift();
}
if (index == _size-1)
{
return pop();
}
ListNode<T> *tmp = getNode(index - 1); if (index == _size-1) {
ListNode<T> *toDelete = tmp->next; return pop();
T ret = toDelete->data; }
tmp->next = tmp->next->next;
delete(toDelete); ListNode<T> *tmp = getNode(index - 1);
_size--; ListNode<T> *toDelete = tmp->next;
isCached = false; T ret = toDelete->data;
return ret; tmp->next = tmp->next->next;
delete(toDelete);
_size--;
isCached = false;
return ret;
} }
template<typename T> template<typename T>
T LinkedList<T>::get(int index){ T LinkedList<T>::get(int index)
ListNode<T> *tmp = getNode(index); {
ListNode<T> *tmp = getNode(index);
return (tmp ? tmp->data : T()); return (tmp ? tmp->data : T());
} }
template<typename T> template<typename T>
void LinkedList<T>::clear(){ void LinkedList<T>::clear()
while(size() > 0) {
shift(); while(size() > 0) {
shift();
}
} }
#endif #endif

View File

@ -1,8 +1,8 @@
/* /*
command.cpp - esp8266 configuration class command.cpp - esp8266 configuration class
Copyright (c) 2014 Luc Lebosse. All rights reserved. Copyright (c) 2014 Luc Lebosse. All rights reserved.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 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) void COMMAND::execute_command(int cmd,String cmd_params)
{ {
//manage parameters //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:
if (cmd_params=="STA")mode = CLIENT_MODE; switch(cmd) {
else mode=AP_MODE; byte mode;
if(!CONFIG::write_byte(EP_WIFI_MODE,mode))Serial.println("\nError"); case 800:
else Serial.println("\nOk"); Serial.println("\nCommand received");
break; break;
case 104: case 100:
if (cmd_params=="STATIC")mode = STATIC_IP_MODE; if(!CONFIG::write_string(EP_SSID,cmd_params.c_str())) {
else mode=DHCP_MODE; Serial.println("\nError");
if(!CONFIG::write_byte(EP_IP_MODE,mode))Serial.println("\nError"); } else {
else Serial.println("\nOk"); Serial.println("\nOk");
break; }
case 111: break;
{ case 101:
String currentIP ; if(!CONFIG::write_string(EP_PASSWORD,cmd_params.c_str())) {
if (wifi_get_opmode()==WIFI_STA)currentIP=WiFi.localIP().toString(); Serial.println("\nError");
else currentIP=WiFi.softAPIP().toString(); } else {
Serial.print("\n\r"); Serial.println("\nOk");
Serial.print(cmd_params); }
Serial.println(currentIP); break;
Serial.print("\r\n"); case 103:
}
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:
} 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) void COMMAND::check_command(String buffer)
{ {
String ESP_Command; String ESP_Command;
static bool bfileslist=false; static bool bfileslist=false;
static uint32_t start_list=0; static uint32_t start_list=0;
if (!bfileslist) if (!bfileslist) {
{ int filesstart = buffer.indexOf("Begin file list");
int filesstart = buffer.indexOf("Begin file list"); if (filesstart>-1) {
if (filesstart>-1) Serial.print("Starting");
{ start_list=system_get_time();
Serial.print("Starting"); bfileslist=true;
start_list=system_get_time(); web_interface->fileslist.clear();
bfileslist=true; }
web_interface->fileslist.clear(); int ESPpos = buffer.indexOf("[ESP");
} int Tpos = buffer.indexOf("T:");
int ESPpos = buffer.indexOf("[ESP"); int Xpos = buffer.indexOf("X:");
int Tpos = buffer.indexOf("T:"); int Ypos = buffer.indexOf("Y:");
int Xpos = buffer.indexOf("X:"); int Zpos = buffer.indexOf("Z:");
int Ypos = buffer.indexOf("Y:"); int Speedpos = buffer.indexOf("SpeedMultiply:");
int Zpos = buffer.indexOf("Z:"); int Flowpos = buffer.indexOf("FlowMultiply:");
int Speedpos = buffer.indexOf("SpeedMultiply:"); int Errorpos= buffer.indexOf("Error:");
int Flowpos = buffer.indexOf("FlowMultiply:"); int Infopos= buffer.indexOf("Info:");
int Errorpos= buffer.indexOf("Error:"); int Statuspos= buffer.indexOf("Status:");
int Infopos= buffer.indexOf("Info:"); if (ESPpos>-1) {
int Statuspos= buffer.indexOf("Status:"); //is there the second part?
if (ESPpos>-1) int ESPpos2 = buffer.indexOf("]",ESPpos);
{//is there the second part? if (ESPpos2>-1) {
int ESPpos2 = buffer.indexOf("]",ESPpos); //Split in command and parameters
if (ESPpos2>-1) String cmd_part1=buffer.substring(ESPpos+4,ESPpos2);
{ //Split in command and parameters String cmd_part2="";
String cmd_part1=buffer.substring(ESPpos+4,ESPpos2); //is there space for parameters?
String cmd_part2=""; if (ESPpos2<buffer.length()) {
//is there space for parameters? cmd_part2=buffer.substring(ESPpos2+1);
if (ESPpos2<buffer.length()) }
{ //if command is a valid number then execute command
cmd_part2=buffer.substring(ESPpos2+1); if(cmd_part1.toInt()!=0) {
} execute_command(cmd_part1.toInt(),cmd_part2);
//if command is a valid number then execute command }
if(cmd_part1.toInt()!=0)execute_command(cmd_part1.toInt(),cmd_part2); //if not is not a valid [ESPXXX] command
//if not is not a valid [ESPXXX] command }
} }
} //check for temperature
//check for temperature if (Tpos>-1) {
if (Tpos>-1) //look for valid temperature answer
{ int slashpos = buffer.indexOf(" /",Tpos);
//look for valid temperature answer int spacepos = buffer.indexOf(" ",slashpos+1);
int slashpos = buffer.indexOf(" /",Tpos); //if match mask T:xxx.xx /xxx.xx
int spacepos = buffer.indexOf(" ",slashpos+1); if(spacepos-Tpos < 17) {
//if match mask T:xxx.xx /xxx.xx web_interface->answer4M105=buffer; //do not interprete just need when requested so store it
if(spacepos-Tpos < 17) web_interface->last_temp=system_get_time();
{ }
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;
//Position of axis }
if (Xpos>-1 && Ypos>-1 && Zpos>-1)web_interface->answer4M114=buffer; //Speed
//Speed if (Speedpos>-1) {
if (Speedpos>-1)web_interface->answer4M220=buffer.substring(Speedpos+14); web_interface->answer4M220=buffer.substring(Speedpos+14);
//Flow }
if (Flowpos>-1)web_interface->answer4M221=buffer.substring(Flowpos+13); //Flow
//Error if (Flowpos>-1) {
if (Errorpos>-1 && !(buffer.indexOf("Format error")!=-1 || buffer.indexOf("wait")==Errorpos+6) )(web_interface->error_msg).add(buffer.substring(Errorpos+6).c_str()); web_interface->answer4M221=buffer.substring(Flowpos+13);
//Info }
if (Infopos>-1)(web_interface->info_msg).add(buffer.substring(Infopos+5).c_str()); //Error
//Status if (Errorpos>-1 && !(buffer.indexOf("Format error")!=-1 || buffer.indexOf("wait")==Errorpos+6) ) {
if (Statuspos>-1)(web_interface->status_msg).add(buffer.substring(Statuspos+7).c_str()); (web_interface->error_msg).add(buffer.substring(Errorpos+6).c_str());
} }
else //Info
{ if (Infopos>-1) {
if ((system_get_time()-start_list)>30000000) //timeout in case of problem (web_interface->info_msg).add(buffer.substring(Infopos+5).c_str());
{ }
bfileslist=false; //Status
Serial.print("time out"); if (Statuspos>-1) {
} (web_interface->status_msg).add(buffer.substring(Statuspos+7).c_str());
else }
{ } else {
if (buffer.indexOf("End file list")>-1) if ((system_get_time()-start_list)>30000000) { //timeout in case of problem
{ bfileslist=false;
Serial.print("Ending"); Serial.print("time out");
bfileslist=false; } else {
} if (buffer.indexOf("End file list")>-1) {
else Serial.print("Ending");
{ bfileslist=false;
Serial.print(buffer); } else {
web_interface->fileslist.add(buffer); Serial.print(buffer);
} web_interface->fileslist.add(buffer);
} }
}
}
}
} }
//read a buffer in an array //read a buffer in an array
void COMMAND::read_buffer_serial(uint8_t *b, size_t len) void COMMAND::read_buffer_serial(uint8_t *b, size_t len)
{ {
for (long i; i< len;i++) for (long i; i< len; i++) {
{ read_buffer_serial(*b);
read_buffer_serial(*b); *b++;
*b++; }
}
} }
//read buffer as char //read buffer as char
void COMMAND::read_buffer_tcp(uint8_t b) 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 //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 //it is a char so add it to buffer
if (isPrintable(b)) if (isPrintable(b)) {
{ previous_was_char=true;
previous_was_char=true; buffer_tcp+=char(b);
buffer_tcp+=char(b); } else {
} previous_was_char=false; //next call will reset the buffer
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 //this is not printable but end of command check if need to handle it
if (b==13 ||b==10) if (b==13 ||b==10) {
{//Minimum is something like M10 so 3 char //Minimum is something like M10 so 3 char
if (buffer_tcp.length()>3) if (buffer_tcp.length()>3) {
check_command(buffer_tcp); check_command(buffer_tcp);
} }
}
} }
//read buffer as char //read buffer as char
void COMMAND::read_buffer_serial(uint8_t b) 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 //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 //it is a char so add it to buffer
if (isPrintable(b)) if (isPrintable(b)) {
{ previous_was_char=true;
previous_was_char=true; buffer_serial+=char(b);
buffer_serial+=char(b); } else {
} previous_was_char=false; //next call will reset the buffer
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 //this is not printable but end of command check if need to handle it
if (b==13 ||b==10) if (b==13 ||b==10) {
{//Minimum is something like M10 so 3 char //Minimum is something like M10 so 3 char
if (buffer_serial.length()>3) if (buffer_serial.length()>3) {
check_command(buffer_serial); check_command(buffer_serial);
} }
}
} }

View File

@ -1,8 +1,8 @@
/* /*
command.h - esp8266 configuration class command.h - esp8266 configuration class
Copyright (c) 2014 Luc Lebosse. All rights reserved. Copyright (c) 2014 Luc Lebosse. All rights reserved.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
@ -24,14 +24,14 @@
class COMMAND class COMMAND
{ {
public: public:
static String buffer_serial; static String buffer_serial;
static String buffer_tcp; static String buffer_tcp;
static void read_buffer_serial(uint8_t *b, size_t len); static void read_buffer_serial(uint8_t *b, size_t len);
static void read_buffer_serial(uint8_t b); static void read_buffer_serial(uint8_t b);
static void read_buffer_tcp(uint8_t b); static void read_buffer_tcp(uint8_t b);
static void check_command(String buffer); static void check_command(String buffer);
static void execute_command(int cmd,String cmd_params); static void execute_command(int cmd,String cmd_params);
}; };

View File

@ -1,8 +1,8 @@
/* /*
CONFIG.CPP- esp8266 configuration class CONFIG.CPP- esp8266 configuration class
Copyright (c) 2014 Luc Lebosse. All rights reserved. Copyright (c) 2014 Luc Lebosse. All rights reserved.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
@ -24,332 +24,416 @@ extern "C" {
#include "user_interface.h" #include "user_interface.h"
} }
//read a string //read a string
//a string is multibyte + \0, this is won't work if 1 char is multibyte like chinese char //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) bool CONFIG::read_string(int pos, char byte_buffer[], int size_max)
{ {
//check if parameters are acceptable //check if parameters are acceptable
if (size_max==0 || pos+size_max+1 > EEPROM_SIZE || byte_buffer== NULL)return false; if (size_max==0 || pos+size_max+1 > EEPROM_SIZE || byte_buffer== NULL) {
EEPROM.begin(EEPROM_SIZE); return false;
byte b = 13; // non zero for the while loop below }
int i=0; 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 //read until max size is reached or \0 is found
while (i < size_max && b != 0) while (i < size_max && b != 0) {
{ b = EEPROM.read(pos+i);
b = EEPROM.read(pos+i); byte_buffer[i]=b;
byte_buffer[i]=b; i++;
i++; }
}
// Be sure there is a 0 at the end.
// Be sure there is a 0 at the end. if (b!=0) {
if (b!=0)byte_buffer[i-1]=0x00; byte_buffer[i-1]=0x00;
EEPROM.end(); }
EEPROM.end();
return true;
return true;
} }
bool CONFIG::read_string(int pos, String & sbuffer, int size_max) bool CONFIG::read_string(int pos, String & sbuffer, int size_max)
{ {
//check if parameters are acceptable //check if parameters are acceptable
if (size_max==0 || pos+size_max+1 > EEPROM_SIZE )return false; if (size_max==0 || pos+size_max+1 > EEPROM_SIZE ) {
byte b = 13; // non zero for the while loop below return false;
int i=0; }
sbuffer=""; byte b = 13; // non zero for the while loop below
int i=0;
sbuffer="";
EEPROM.begin(EEPROM_SIZE); EEPROM.begin(EEPROM_SIZE);
//read until max size is reached or \0 is found //read until max size is reached or \0 is found
while (i < size_max && b != 0) while (i < size_max && b != 0) {
{ b = EEPROM.read(pos+i);
b = EEPROM.read(pos+i); sbuffer+=char(b);
sbuffer+=char(b); i++;
i++; }
} EEPROM.end();
EEPROM.end();
return true; return true;
} }
//read a buffer of size_buffer //read a buffer of size_buffer
bool CONFIG::read_buffer(int pos, byte byte_buffer[], int size_buffer) bool CONFIG::read_buffer(int pos, byte byte_buffer[], int size_buffer)
{ {
//check if parameters are acceptable //check if parameters are acceptable
if (size_buffer==0 || pos+size_buffer > EEPROM_SIZE || byte_buffer== NULL)return false; if (size_buffer==0 || pos+size_buffer > EEPROM_SIZE || byte_buffer== NULL) {
int i=0; return false;
EEPROM.begin(EEPROM_SIZE); }
//read until max size is reached int i=0;
while (i<size_buffer ) EEPROM.begin(EEPROM_SIZE);
{ //read until max size is reached
byte_buffer[i]=EEPROM.read(pos+i); while (i<size_buffer ) {
i++; byte_buffer[i]=EEPROM.read(pos+i);
} i++;
EEPROM.end(); }
return true; EEPROM.end();
return true;
} }
//read a flag / byte //read a flag / byte
bool CONFIG::read_byte(int pos, byte * value) bool CONFIG::read_byte(int pos, byte * value)
{ {
//check if parameters are acceptable //check if parameters are acceptable
if (pos+1 > EEPROM_SIZE)return false; if (pos+1 > EEPROM_SIZE) {
EEPROM.begin(EEPROM_SIZE); return false;
value[0] = EEPROM.read(pos); }
EEPROM.end(); EEPROM.begin(EEPROM_SIZE);
return true; value[0] = EEPROM.read(pos);
EEPROM.end();
return true;
} }
bool CONFIG::write_string(int pos, const __FlashStringHelper *str) bool CONFIG::write_string(int pos, const __FlashStringHelper *str)
{ {
String stmp = str; String stmp = str;
return write_string(pos,stmp.c_str()); return write_string(pos,stmp.c_str());
} }
//write a string (array of byte with a 0x00 at the end) //write a string (array of byte with a 0x00 at the end)
bool CONFIG::write_string(int pos, const char * byte_buffer) bool CONFIG::write_string(int pos, const char * byte_buffer)
{ {
int size_buffer; int size_buffer;
size_buffer= strlen(byte_buffer); size_buffer= strlen(byte_buffer);
//check if parameters are acceptable //check if parameters are acceptable
if (size_buffer==0 || pos+size_buffer+1 > EEPROM_SIZE || byte_buffer== NULL)return false; if (size_buffer==0 || pos+size_buffer+1 > EEPROM_SIZE || byte_buffer== NULL) {
//copy the value(s) return false;
EEPROM.begin(EEPROM_SIZE); }
for (int i = 0; i < size_buffer; i++) { //copy the value(s)
EEPROM.write(pos + i, byte_buffer[i]); 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(); //0 terminal
EEPROM.end(); EEPROM.write(pos + size_buffer, 0x00);
return true; 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) bool CONFIG::write_buffer(int pos, const byte * byte_buffer, int size_buffer)
{ {
//check if parameters are acceptable //check if parameters are acceptable
if (size_buffer==0 || pos+size_buffer > EEPROM_SIZE || byte_buffer== NULL)return false; if (size_buffer==0 || pos+size_buffer > EEPROM_SIZE || byte_buffer== NULL) {
EEPROM.begin(EEPROM_SIZE); return false;
//copy the value(s) }
for (int i = 0; i < size_buffer; i++) { EEPROM.begin(EEPROM_SIZE);
EEPROM.write(pos + i, byte_buffer[i]); //copy the value(s)
} for (int i = 0; i < size_buffer; i++) {
EEPROM.commit(); EEPROM.write(pos + i, byte_buffer[i]);
EEPROM.end(); }
return true; EEPROM.commit();
EEPROM.end();
return true;
} }
//read a flag / byte //read a flag / byte
bool CONFIG::write_byte(int pos, const byte value) bool CONFIG::write_byte(int pos, const byte value)
{ {
//check if parameters are acceptable //check if parameters are acceptable
if (pos+1 > EEPROM_SIZE)return false; if (pos+1 > EEPROM_SIZE) {
EEPROM.begin(EEPROM_SIZE); return false;
EEPROM.write(pos, value); }
EEPROM.commit(); EEPROM.begin(EEPROM_SIZE);
EEPROM.end(); EEPROM.write(pos, value);
return true; EEPROM.commit();
EEPROM.end();
return true;
} }
bool CONFIG::reset_config() bool CONFIG::reset_config()
{ {
if(!CONFIG::write_byte(EP_WIFI_MODE,DEFAULT_WIFI_MODE))return false; if(!CONFIG::write_byte(EP_WIFI_MODE,DEFAULT_WIFI_MODE)) {
if(!CONFIG::write_string(EP_SSID,FPSTR(DEFAULT_SSID)))return false; 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_string(EP_SSID,FPSTR(DEFAULT_SSID))) {
if(!CONFIG::write_buffer(EP_IP_VALUE,DEFAULT_IP_VALUE,IP_LENGTH))return false; 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_string(EP_PASSWORD,FPSTR(DEFAULT_PASSWORD))) {
if(!CONFIG::write_buffer(EP_BAUD_RATE,(const byte *)&DEFAULT_BAUD_RATE,INTEGER_LENGTH))return false; 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_IP_MODE,DEFAULT_IP_MODE)) {
if(!CONFIG::write_byte(EP_CHANNEL,DEFAULT_CHANNEL))return false; 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_IP_VALUE,DEFAULT_IP_VALUE,IP_LENGTH)) {
if(!CONFIG::write_buffer(EP_WEB_PORT,(const byte *)&DEFAULT_WEB_PORT,INTEGER_LENGTH))return false; 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_buffer(EP_MASK_VALUE,DEFAULT_MASK_VALUE,IP_LENGTH)) {
if(!CONFIG::write_string(EP_HOSTNAME,wifi_config.get_default_hostname()))return false; 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_GATEWAY_VALUE,DEFAULT_GATEWAY_VALUE,IP_LENGTH)) {
if(!CONFIG::write_buffer(EP_E_FEEDRATE,(const byte *)&DEFAULT_E_FEEDRATE,INTEGER_LENGTH))return false; return false;
if(!CONFIG::write_string(EP_ADMIN_PWD,FPSTR(DEFAULT_ADMIN)))return false; }
return true; 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() void CONFIG::print_config()
{ {
//use biggest size for buffer //use biggest size for buffer
char sbuf[MAX_PASSWORD_LENGTH+1]; char sbuf[MAX_PASSWORD_LENGTH+1];
byte bbuf=0; byte bbuf=0;
int ibuf=0; int ibuf=0;
if (CONFIG::read_byte(EP_WIFI_MODE, &bbuf )) if (CONFIG::read_byte(EP_WIFI_MODE, &bbuf )) {
{ Serial.print(F("Mode: "));
Serial.print(F("Mode: ")); if (byte(bbuf) == CLIENT_MODE) {
if (byte(bbuf) == CLIENT_MODE) Serial.println(F("Station")); Serial.println(F("Station"));
else if (byte(bbuf)==AP_MODE) Serial.println(F("Access Point")); } else if (byte(bbuf)==AP_MODE) {
else Serial.println("???"); Serial.println(F("Access Point"));
} } else {
else Serial.println(F("Error reading mode")); Serial.println("???");
}
if (CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGTH)) } else {
{ Serial.println(F("Error reading mode"));
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));
} }
else Serial.println(F("Error reading gateway"));
if (CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGTH)) {
if (CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&ibuf , INTEGER_LENGTH)) Serial.print(F("SSID: "));
{ Serial.println(sbuf);
Serial.print(F("Baud rate: ")); } else {
Serial.println(ibuf); Serial.println(F("Error reading SSID"));
}
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_string(EP_PASSWORD, sbuf , MAX_PASSWORD_LENGTH))Serial.println(sbuf);
if (CONFIG::read_byte(EP_SLEEP_MODE, &bbuf )) if (CONFIG::read_byte(EP_IP_MODE, &bbuf )) {
{ Serial.print(F("IP Mode: "));
Serial.print(F("Sleep mode: ")); if (byte(bbuf)==STATIC_IP_MODE) {
if (byte(bbuf)==NONE_SLEEP_T) Serial.println(F("None")); Serial.println(F("Static"));
else if (byte(bbuf)==LIGHT_SLEEP_T) Serial.println(F("Light")); } else if (byte(bbuf)==DHCP_MODE) {
else if (byte(bbuf)==MODEM_SLEEP_T) Serial.println(F("Modem")); Serial.println(F("DHCP"));
else Serial.println(F("???")); } else {
} Serial.println(F("???"));
else Serial.println(F("Error reading sleep mode")); }
} else {
if (CONFIG::read_byte(EP_CHANNEL, &bbuf )) Serial.println(F("Error reading IP mode"));
{ }
Serial.print(F("Channel: "));
Serial.println(byte(bbuf)); if (CONFIG::read_buffer(EP_IP_VALUE,(byte *)sbuf , IP_LENGTH)) {
} Serial.print(F("IP: "));
else Serial.println(F("Error reading channel")); Serial.println(wifi_config.ip2str((byte *)sbuf));
} else {
if (CONFIG::read_byte(EP_AUTH_TYPE, &bbuf )) Serial.println(F("Error reading IP"));
{ }
Serial.print(F("Authentification: "));
if (byte(bbuf)==AUTH_OPEN) Serial.println(F("None")); if (CONFIG::read_buffer(EP_MASK_VALUE, (byte *)sbuf , IP_LENGTH)) {
else if (byte(bbuf)==AUTH_WEP) Serial.println(F("WEP")); Serial.print(F("Subnet: "));
else if (byte(bbuf)==AUTH_WPA_PSK) Serial.println(F("WPA")); Serial.println(wifi_config.ip2str((byte *)sbuf));
else if (byte(bbuf)==AUTH_WPA2_PSK) Serial.println(F("WPA2")); } else {
else if (byte(bbuf)==AUTH_WPA_WPA2_PSK) Serial.println(F("WPA/WPA2")); Serial.println(F("Error reading subnet"));
else Serial.println(F("???")); }
}
else Serial.println(F("Error reading authentification")); if (CONFIG::read_buffer(EP_GATEWAY_VALUE, (byte *)sbuf , IP_LENGTH)) {
Serial.print(F("Gateway: "));
if (CONFIG::read_byte(EP_SSID_VISIBLE, &bbuf )) Serial.println(wifi_config.ip2str((byte *)sbuf));
{ } else {
Serial.print(F("SSID visibility: ")); Serial.println(F("Error reading gateway"));
if (bbuf==0) Serial.println(F("Hidden")); }
else if (bbuf==1) Serial.println(F("Visible"));
else Serial.println(bbuf); if (CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&ibuf , INTEGER_LENGTH)) {
} Serial.print(F("Baud rate: "));
else Serial.println(F("Error reading SSID visibility")); Serial.println(ibuf);
} else {
if (CONFIG::read_buffer(EP_WEB_PORT, (byte *)&ibuf , INTEGER_LENGTH)) Serial.println(F("Error reading baud rate"));
{ }
Serial.print(F("Web port: "));
Serial.println(ibuf); if (CONFIG::read_byte(EP_PHY_MODE, &bbuf )) {
} Serial.print(F("Phy mode: "));
else Serial.println(F("Error reading web port")); if (byte(bbuf)==PHY_MODE_11B) {
Serial.println(F("11b"));
if (CONFIG::read_buffer(EP_DATA_PORT, (byte *)&ibuf , INTEGER_LENGTH)) } else if (byte(bbuf)==PHY_MODE_11G) {
{ Serial.println(F("11g"));
Serial.print(F("Data port: ")); } else if (byte(bbuf)==PHY_MODE_11N) {
Serial.println(ibuf); Serial.println(F("11n"));
} } else {
else Serial.println(F("Error reading data port")); Serial.println(F("???"));
}
if (CONFIG::read_byte(EP_REFRESH_PAGE_TIME, &bbuf )) } else {
{ Serial.println(F("Error reading phy mode"));
Serial.print(F("Web page refresh time: ")); }
Serial.println(byte(bbuf));
} if (CONFIG::read_byte(EP_SLEEP_MODE, &bbuf )) {
else Serial.println(F("Error reading refresh page")); Serial.print(F("Sleep mode: "));
if (byte(bbuf)==NONE_SLEEP_T) {
if (CONFIG::read_string(EP_HOSTNAME, sbuf , MAX_HOSTNAME_LENGTH)) Serial.println(F("None"));
{ } else if (byte(bbuf)==LIGHT_SLEEP_T) {
Serial.print(F("Hostname: ")); Serial.println(F("Light"));
Serial.println(sbuf); } else if (byte(bbuf)==MODEM_SLEEP_T) {
} Serial.println(F("Modem"));
else Serial.println(F("Error reading hostname")); } else {
Serial.println(F("???"));
if (CONFIG::read_buffer(EP_XY_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH)) }
{ } else {
Serial.print(F("XY feed rate: ")); Serial.println(F("Error reading sleep mode"));
Serial.println(ibuf); }
}
else Serial.println(F("Error reading XY feed rate")); if (CONFIG::read_byte(EP_CHANNEL, &bbuf )) {
Serial.print(F("Channel: "));
if (CONFIG::read_buffer(EP_Z_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH)) Serial.println(byte(bbuf));
{ } else {
Serial.print(F("Z feed rate: ")); Serial.println(F("Error reading channel"));
Serial.println(ibuf); }
}
else Serial.println(F("Error reading Z feed rate")); if (CONFIG::read_byte(EP_AUTH_TYPE, &bbuf )) {
Serial.print(F("Authentification: "));
if (CONFIG::read_buffer(EP_E_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH)) if (byte(bbuf)==AUTH_OPEN) {
{ Serial.println(F("None"));
Serial.print(F("E feed rate: ")); } else if (byte(bbuf)==AUTH_WEP) {
Serial.println(ibuf); Serial.println(F("WEP"));
} } else if (byte(bbuf)==AUTH_WPA_PSK) {
else Serial.println(F("Error reading E feed rate")); Serial.println(F("WPA"));
} else if (byte(bbuf)==AUTH_WPA2_PSK) {
Serial.print(F("Captive portal: ")); Serial.println(F("WPA2"));
#ifdef CAPTIVE_PORTAL_FEATURE } else if (byte(bbuf)==AUTH_WPA_WPA2_PSK) {
Serial.println(F("Enabled")); Serial.println(F("WPA/WPA2"));
#else } else {
Serial.println(F("Disabled")); 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 #endif
Serial.print(F("SSDP: ")); Serial.print(F("SSDP: "));
#ifdef SSDP_FEATURE #ifdef SSDP_FEATURE
Serial.println(F("Enabled")); Serial.println(F("Enabled"));
#else #else
Serial.println(F("Disabled")); Serial.println(F("Disabled"));
#endif #endif
Serial.print(F("mDNS: ")); Serial.print(F("mDNS: "));
#ifdef MDNS_FEATURE #ifdef MDNS_FEATURE
Serial.println(F("Enabled")); Serial.println(F("Enabled"));
#else #else
Serial.println(F("Disabled")); Serial.println(F("Disabled"));
#endif #endif
} }

View File

@ -20,7 +20,7 @@
//comment to disable //comment to disable
//MDNS_FEATURE: this feature allow type the name defined //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 #define MDNS_FEATURE
//SSDD_FEATURE: this feature is a discovery protocol, supported on Windows out of the box //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 class CONFIG
{ {
public: public:
static bool read_string(int pos, char byte_buffer[], int size_max); 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_string(int pos, String & sbuffer, int size_max);
static bool read_buffer(int pos, byte byte_buffer[], int size_buffer); static bool read_buffer(int pos, byte byte_buffer[], int size_buffer);

View File

@ -19,7 +19,7 @@
Latest version of the code and documentation can be found here : Latest version of the code and documentation can be found here :
https://github.com/luc-github/ESP8266 https://github.com/luc-github/ESP8266
Main author: luc lebosse Main author: luc lebosse
*/ */
@ -55,90 +55,101 @@ extern "C" {
WiFiServer * data_server; WiFiServer * data_server;
WiFiClient serverClients[MAX_SRV_CLIENTS]; WiFiClient serverClients[MAX_SRV_CLIENTS];
void setup() { void setup()
// init: {
web_interface = NULL; // init:
data_server = NULL; web_interface = NULL;
// ESP.wdtDisable(); data_server = NULL;
system_update_cpu_freq(SYS_CPU_160MHZ); // ESP.wdtDisable();
delay(8000); system_update_cpu_freq(SYS_CPU_160MHZ);
bool breset_config=false; delay(8000);
//check if reset config is requested bool breset_config=false;
pinMode(RESET_CONFIG_PIN, INPUT); //check if reset config is requested
if (digitalRead(RESET_CONFIG_PIN)==0)breset_config=true;//if requested =>reset settings pinMode(RESET_CONFIG_PIN, INPUT);
//default baud rate if (digitalRead(RESET_CONFIG_PIN)==0) {
long baud_rate=0; breset_config=true; //if requested =>reset settings
//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 //default baud rate
long baud_rate=0;
//reset is requested //check if EEPROM has value
if(breset_config) 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
//update EEPROM with default settings if ( ! (baud_rate==9600 || baud_rate==19200 ||baud_rate==38400 ||baud_rate==57600 ||baud_rate==115200 ||baud_rate==230400 ||baud_rate==250000) ) {
Serial.begin(9600); breset_config=true; //baud rate is incorrect =>reset settings
delay(2000); }
Serial.println(F("M117 ESP EEPROM reset")); if (wifi_config.iweb_port<1 ||wifi_config.iweb_port>65001 || wifi_config.idata_port <1 || wifi_config.idata_port >65001) {
CONFIG::reset_config(); 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); delay(1000);
//put some default value to a void some exception at first start wifi_config.baud_rate=baud_rate;
WiFi.mode(WIFI_AP); //setup wifi according settings
wifi_set_phy_mode(PHY_MODE_11G); if (!wifi_config.Setup()) {
Serial.flush(); wifi_config.Safe_Setup();
delay(500);
Serial.swap();
delay(100);
//restart once reset config is done
ESP.restart();
while (1){delay(1);};
} }
//setup serial delay(1000);
Serial.begin(baud_rate); //start interfaces
delay(1000); web_interface = new WEBINTERFACE_CLASS(wifi_config.iweb_port);
wifi_config.baud_rate=baud_rate; data_server = new WiFiServer (wifi_config.idata_port);
//setup wifi according settings //here the list of headers to be recorded
if (!wifi_config.Setup()) wifi_config.Safe_Setup(); const char * headerkeys[] = {"Cookie"} ;
delay(1000); size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*);
//start interfaces //ask server to track these headers
web_interface = new WEBINTERFACE_CLASS(wifi_config.iweb_port); web_interface->WebServer.collectHeaders(headerkeys, headerkeyssize );
data_server = new WiFiServer (wifi_config.idata_port); web_interface->WebServer.begin();
//here the list of headers to be recorded data_server->begin();
const char * headerkeys[] = {"Cookie"} ; data_server->setNoDelay(true);
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 #ifdef MDNS_FEATURE
// Check for any mDNS queries and send responses // Check for any mDNS queries and send responses
wifi_config.mdns.addService("http", "tcp", wifi_config.iweb_port); wifi_config.mdns.addService("http", "tcp", wifi_config.iweb_port);
#endif #endif
#ifdef CAPTIVE_PORTAL_FEATURE #ifdef CAPTIVE_PORTAL_FEATURE
if (wifi_get_opmode()!=WIFI_STA ) if (wifi_get_opmode()!=WIFI_STA ) {
{ // if DNSServer is started with "*" for domain name, it will reply with
// if DNSServer is started with "*" for domain name, it will reply with // provided IP to all DNS request
// provided IP to all DNS request dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.setErrorReplyCode(DNSReplyCode::NoError); dnsServer.start(DNS_PORT, "*", WiFi.softAPIP());
dnsServer.start(DNS_PORT, "*", WiFi.softAPIP()); }
} #endif
#endif
#ifdef SSDP_FEATURE #ifdef SSDP_FEATURE
String stmp; String stmp;
SSDP.setSchemaURL("description.xml"); SSDP.setSchemaURL("description.xml");
SSDP.setHTTPPort( wifi_config.iweb_port); 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()); SSDP.setName(stmp.c_str());
stmp=String(system_get_chip_id()); stmp=String(system_get_chip_id());
SSDP.setSerialNumber(stmp.c_str()); SSDP.setSerialNumber(stmp.c_str());
@ -150,70 +161,73 @@ void setup() {
SSDP.setManufacturerURL("http://espressif.com"); SSDP.setManufacturerURL("http://espressif.com");
SSDP.begin(); SSDP.begin();
#endif #endif
SPIFFS.begin(); SPIFFS.begin();
} }
//main loop //main loop
void loop() { void loop()
{
#ifdef CAPTIVE_PORTAL_FEATURE #ifdef CAPTIVE_PORTAL_FEATURE
if (wifi_get_opmode()!=WIFI_STA ) if (wifi_get_opmode()!=WIFI_STA ) {
{ dnsServer.processNextRequest();
dnsServer.processNextRequest(); }
}
#endif #endif
//web requests //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 //TODO use a method to handle serial also in class and call it instead of this one
uint8_t i,data; uint8_t i,data;
//check if there are any new clients //check if there are any new clients
if (data_server->hasClient()){ if (data_server->hasClient()) {
for(i = 0; i < MAX_SRV_CLIENTS; i++){ for(i = 0; i < MAX_SRV_CLIENTS; i++) {
//find free/disconnected spot //find free/disconnected spot
if (!serverClients[i] || !serverClients[i].connected()){ if (!serverClients[i] || !serverClients[i].connected()) {
if(serverClients[i]) serverClients[i].stop(); if(serverClients[i]) {
serverClients[i] = data_server->available(); serverClients[i].stop();
continue; }
} 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 //check clients for data
WiFiClient serverClient = data_server->available(); for(i = 0; i < MAX_SRV_CLIENTS; i++) {
serverClient.stop(); if (serverClients[i] && serverClients[i].connected()) {
} if(serverClients[i].available()) {
//check clients for data //get data from the tcp client and push it to the UART
for(i = 0; i < MAX_SRV_CLIENTS; i++){ while(serverClients[i].available()) {
if (serverClients[i] && serverClients[i].connected()){ data = serverClients[i].read();
if(serverClients[i].available()){ Serial.write(data);
//get data from the tcp client and push it to the UART COMMAND::read_buffer_tcp(data);
while(serverClients[i].available()){ }
data = serverClients[i].read(); }
Serial.write(data); }
COMMAND::read_buffer_tcp(data);
}
}
} }
} //check UART for data
//check UART for data if(Serial.available()) {
if(Serial.available()){ size_t len = Serial.available();
size_t len = Serial.available(); uint8_t sbuf[len];
uint8_t sbuf[len]; Serial.readBytes(sbuf, len);
Serial.readBytes(sbuf, len); //push UART data to all connected tcp clients
//push UART data to all connected tcp clients for(i = 0; i < MAX_SRV_CLIENTS; i++) {
for(i = 0; i < MAX_SRV_CLIENTS; i++){ if (serverClients[i] && serverClients[i].connected()) {
if (serverClients[i] && serverClients[i].connected()){ serverClients[i].write(sbuf, len);
serverClients[i].write(sbuf, len); delay(1);
delay(1); }
} COMMAND::read_buffer_serial(sbuf, len);
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);};
}
} }

View File

@ -1,8 +1,8 @@
/* /*
storestrings.cpp - rolling storage class storestrings.cpp - rolling storage class
Copyright (c) 2014 Luc Lebosse. All rights reserved. Copyright (c) 2014 Luc Lebosse. All rights reserved.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
@ -19,107 +19,121 @@
*/ */
#include "storestrings.h" #include "storestrings.h"
//Constructor //Constructor
STORESTRINGS_CLASS::STORESTRINGS_CLASS (int maxsize , int maxstringlength){ STORESTRINGS_CLASS::STORESTRINGS_CLASS (int maxsize , int maxstringlength)
{
//for rolling buffer //for rolling buffer
//if max size is reached then remove oldest one and add the new one //if max size is reached then remove oldest one and add the new one
_maxsize=maxsize; _maxsize=maxsize;
//to limit the storage space //to limit the storage space
_maxstringlength=maxstringlength; _maxstringlength=maxstringlength;
//need space for the "..." //need space for the "..."
if (_maxstringlength<4 && _maxstringlength!=-1)_maxstringlength=4; if (_maxstringlength<4 && _maxstringlength!=-1) {
_maxstringlength=4;
}
} }
//Destructor //Destructor
STORESTRINGS_CLASS::~STORESTRINGS_CLASS (){ STORESTRINGS_CLASS::~STORESTRINGS_CLASS ()
// clear list and content {
clear(); // clear list and content
clear();
} }
bool STORESTRINGS_CLASS::setsize(int size) bool STORESTRINGS_CLASS::setsize(int size)
{ {
_maxsize=size; _maxsize=size;
return true; return true;
} }
bool STORESTRINGS_CLASS::setlength(int len) bool STORESTRINGS_CLASS::setlength(int len)
{ {
if (len < 4) return false; if (len < 4) {
_maxstringlength = len; return false;
return true; }
_maxstringlength = len;
return true;
} }
//Clear list and content //Clear list and content
void STORESTRINGS_CLASS::clear(){ void STORESTRINGS_CLASS::clear()
//while list is not empty {
while(_charlist.size()){ //while list is not empty
//remove element while(_charlist.size()) {
char * str = _charlist.pop(); //remove element
//destroy it char * str = _charlist.pop();
delete str; //destroy it
} delete str;
}
} }
bool STORESTRINGS_CLASS::add (const __FlashStringHelper *str) bool STORESTRINGS_CLASS::add (const __FlashStringHelper *str)
{ {
String stmp; String stmp;
stmp=str; stmp=str;
return add(stmp.c_str()); return add(stmp.c_str());
} }
//Add element in storage //Add element in storage
bool STORESTRINGS_CLASS::add (const char * string){ bool STORESTRINGS_CLASS::add (const char * string)
//if we reach max size {
if (_maxsize==_charlist.size()) //if we reach max size
{//remove oldest one if (_maxsize==_charlist.size()) {
char * str = _charlist.shift(); //remove oldest one
delete str; char * str = _charlist.shift();
} delete str;
//add new one }
//get size including \0 at the end //add new one
size_t size = strlen(string)+1; //get size including \0 at the end
bool need_resize=false; size_t size = strlen(string)+1;
if ( (_maxstringlength!=-1) && (size >_maxstringlength+1 )) bool need_resize=false;
{ if ( (_maxstringlength!=-1) && (size >_maxstringlength+1 )) {
need_resize = true; need_resize = true;
size=_maxstringlength+1; size=_maxstringlength+1;
} }
//reserve memory //reserve memory
char * ptr = new char[size*sizeof(char)]; char * ptr = new char[size*sizeof(char)];
//copy string to storage //copy string to storage
if (need_resize) if (need_resize) {
{ //copy maximum length minus 3 //copy maximum length minus 3
strncpy(ptr,string,_maxstringlength-3); strncpy(ptr,string,_maxstringlength-3);
strcpy(ptr+_maxstringlength-3,"..."); strcpy(ptr+_maxstringlength-3,"...");
} } else {
else //copy as it is
{ //copy as it is strcpy(ptr,string);
strcpy(ptr,string); }
} //add storage to list
//add storage to list _charlist.add(ptr);
_charlist.add(ptr); return true;
return true;
} }
//Remove element at pos position //Remove element at pos position
bool STORESTRINGS_CLASS::remove(int pos) bool STORESTRINGS_CLASS::remove(int pos)
{ //be sure index is in range {
if (pos<0 && pos>(_charlist.size()-1)) return false; //be sure index is in range
//remove item from list if (pos<0 && pos>(_charlist.size()-1)) {
char * str = _charlist.remove(pos); return false;
//destroy item }
delete str; //remove item from list
return true; char * str = _charlist.remove(pos);
//destroy item
delete str;
return true;
} }
//Get element at pos position //Get element at pos position
const char * STORESTRINGS_CLASS::get(int pos) const char * STORESTRINGS_CLASS::get(int pos)
{ //be sure index is in range {
if (pos<0 && pos>(_charlist.size()-1)) return NULL; //be sure index is in range
return (const char *) _charlist.get(pos); if (pos<0 && pos>(_charlist.size()-1)) {
return NULL;
}
return (const char *) _charlist.get(pos);
} }
//Get index for defined string //Get index for defined string
int STORESTRINGS_CLASS::get_index(const char * string) int STORESTRINGS_CLASS::get_index(const char * string)
{//parse the list until it is found {
for (int p=0;p<_charlist.size();p++) //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 (strcmp ( _charlist.get(p), string)==0) {
} return p;
//if not found return -1 }
return -1; }
//if not found return -1
return -1;
} }

View File

@ -1,8 +1,8 @@
/* /*
storestrings.h - rolling storage class storestrings.h - rolling storage class
Copyright (c) 2014 Luc Lebosse. All rights reserved. Copyright (c) 2014 Luc Lebosse. All rights reserved.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
@ -24,26 +24,38 @@
#include "LinkedList.h" #include "LinkedList.h"
class STORESTRINGS_CLASS class STORESTRINGS_CLASS
{ {
public: public:
STORESTRINGS_CLASS (int maxsize = -1, int maxstringlength=-1); STORESTRINGS_CLASS (int maxsize = -1, int maxstringlength=-1);
~STORESTRINGS_CLASS (); ~STORESTRINGS_CLASS ();
bool add (const char * string); bool add (const char * string);
inline bool add (String & string) {return add(string.c_str());}; inline bool add (String & string)
bool add (const __FlashStringHelper *str); {
bool remove(int pos); return add(string.c_str());
const char * get(int pos); };
int get_index(const char * string); bool add (const __FlashStringHelper *str);
void clear(); bool remove(int pos);
inline int size() {return _charlist.size();}; const char * get(int pos);
bool setsize(int size); int get_index(const char * string);
bool setlength(int len); void clear();
inline int getsize() {return _maxsize;}; inline int size()
inline int getlength() {return _maxstringlength;}; {
return _charlist.size();
};
bool setsize(int size);
bool setlength(int len);
inline int getsize()
{
return _maxsize;
};
inline int getlength()
{
return _maxstringlength;
};
private: private:
int _maxsize; int _maxsize;
int _maxstringlength; int _maxstringlength;
LinkedList<char *> _charlist; LinkedList<char *> _charlist;
}; };
#endif #endif

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,8 @@
/* /*
webinterface.h - esp8266 configuration class webinterface.h - esp8266 configuration class
Copyright (c) 2014 Luc Lebosse. All rights reserved. Copyright (c) 2014 Luc Lebosse. All rights reserved.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
@ -29,45 +29,45 @@
#define MAX_EXTRUDERS 4 #define MAX_EXTRUDERS 4
struct auth_ip{ struct auth_ip {
IPAddress ip; IPAddress ip;
char sessionID[17]; char sessionID[17];
uint32_t last_time; uint32_t last_time;
auth_ip * _next; auth_ip * _next;
}; };
class WEBINTERFACE_CLASS class WEBINTERFACE_CLASS
{ {
public: public:
WEBINTERFACE_CLASS (int port = 80); WEBINTERFACE_CLASS (int port = 80);
~WEBINTERFACE_CLASS(); ~WEBINTERFACE_CLASS();
ESP8266WebServer WebServer; ESP8266WebServer WebServer;
File fsUploadFile; File fsUploadFile;
void urldecode( String & dst, const char *src); void urldecode( String & dst, const char *src);
bool isSSIDValid(const char * ssid); bool isSSIDValid(const char * ssid);
bool isPasswordValid(const char * password); bool isPasswordValid(const char * password);
bool isAdminPasswordValid(const char * password); bool isAdminPasswordValid(const char * password);
bool isHostnameValid(const char * hostname); bool isHostnameValid(const char * hostname);
bool isIPValid(const char * IP); bool isIPValid(const char * IP);
String answer4M105; String answer4M105;
String answer4M114; String answer4M114;
String answer4M220; String answer4M220;
String answer4M221; String answer4M221;
STORESTRINGS_CLASS fileslist; STORESTRINGS_CLASS fileslist;
uint32_t last_temp; uint32_t last_temp;
STORESTRINGS_CLASS error_msg; STORESTRINGS_CLASS error_msg;
STORESTRINGS_CLASS info_msg; STORESTRINGS_CLASS info_msg;
STORESTRINGS_CLASS status_msg; STORESTRINGS_CLASS status_msg;
bool restartmodule; bool restartmodule;
char * create_session_ID(); char * create_session_ID();
bool is_authenticated(); bool is_authenticated();
bool AddAuthIP(auth_ip * item); bool AddAuthIP(auth_ip * item);
bool ResetAuthIP(IPAddress ip,const char * sessionID); bool ResetAuthIP(IPAddress ip,const char * sessionID);
uint8_t _upload_status; uint8_t _upload_status;
private: private:
auth_ip * _head; auth_ip * _head;
uint8_t _nb_ip; uint8_t _nb_ip;
}; };
extern WEBINTERFACE_CLASS * web_interface; extern WEBINTERFACE_CLASS * web_interface;

View File

@ -1,8 +1,8 @@
/* /*
wifi.cpp - esp8266 configuration class wifi.cpp - esp8266 configuration class
Copyright (c) 2014 Luc Lebosse. All rights reserved. Copyright (c) 2014 Luc Lebosse. All rights reserved.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
@ -35,235 +35,258 @@ extern DNSServer dnsServer;
WIFI_CONFIG::WIFI_CONFIG() WIFI_CONFIG::WIFI_CONFIG()
{ {
iweb_port=DEFAULT_WEB_PORT; iweb_port=DEFAULT_WEB_PORT;
idata_port=DEFAULT_DATA_PORT; idata_port=DEFAULT_DATA_PORT;
baud_rate=DEFAULT_BAUD_RATE; baud_rate=DEFAULT_BAUD_RATE;
sleep_mode=DEFAULT_SLEEP_MODE; sleep_mode=DEFAULT_SLEEP_MODE;
_hostname[0]=0; _hostname[0]=0;
} }
const char * WIFI_CONFIG::get_hostname(){ const char * WIFI_CONFIG::get_hostname()
if (WiFi.hostname().length()==0) {
{ if (WiFi.hostname().length()==0) {
if (!CONFIG::read_string(EP_HOSTNAME, _hostname , MAX_HOSTNAME_LENGTH))strcpy(_hostname,get_default_hostname()); if (!CONFIG::read_string(EP_HOSTNAME, _hostname , MAX_HOSTNAME_LENGTH)) {
} strcpy(_hostname,get_default_hostname());
else strcpy(_hostname,WiFi.hostname().c_str()); }
return _hostname; } else {
strcpy(_hostname,WiFi.hostname().c_str());
}
return _hostname;
} }
const char * WIFI_CONFIG::get_default_hostname() const char * WIFI_CONFIG::get_default_hostname()
{ {
static char hostname[13]; static char hostname[13];
uint8_t mac [WL_MAC_ADDR_LENGTH]; uint8_t mac [WL_MAC_ADDR_LENGTH];
WiFi.macAddress(mac); WiFi.macAddress(mac);
if (0>sprintf(hostname,"ESP_%02X%02X%02X",mac[3],mac[4],mac[5])) strcpy (hostname, "ESP8266"); if (0>sprintf(hostname,"ESP_%02X%02X%02X",mac[3],mac[4],mac[5])) {
return hostname; strcpy (hostname, "ESP8266");
}
return hostname;
} }
//no strtok so this is simplified version //no strtok so this is simplified version
//return number of part //return number of part
byte WIFI_CONFIG::split_ip (const char * ptr,byte * part) byte WIFI_CONFIG::split_ip (const char * ptr,byte * part)
{ {
if (strlen(ptr)>15 || strlen(ptr)< 7) if (strlen(ptr)>15 || strlen(ptr)< 7) {
{ part[0]=0;
part[0]=0; part[1]=0;
part[1]=0; part[2]=0;
part[2]=0; part[3]=0;
part[3]=0; return 0;
return 0; }
}
char pstart [16];
char pstart [16]; char * ptr2;
char * ptr2; strcpy(pstart,ptr);
strcpy(pstart,ptr); ptr2 = pstart;
ptr2 = pstart; byte i = strlen(pstart);
byte i = strlen(pstart); byte pos = 0;
byte pos = 0; for (byte j=0; j<i; j++) {
for (byte j=0;j<i;j++) if (pstart[j]=='.') {
{ if (pos==4) {
if (pstart[j]=='.') part[0]=0;
{ part[1]=0;
if (pos==4) part[2]=0;
{ part[3]=0;
part[0]=0; return 0;
part[1]=0; }
part[2]=0; pstart[j]=0x0;
part[3]=0; part[pos]=atoi(ptr2);
return 0; pos++;
} ptr2 = &pstart[j+1];
pstart[j]=0x0;
part[pos]=atoi(ptr2);
pos++;
ptr2 = &pstart[j+1];
} }
} }
part[pos]=atoi(ptr2); part[pos]=atoi(ptr2);
return pos+1; return pos+1;
} }
//just simple helper to convert mac address to string //just simple helper to convert mac address to string
char * WIFI_CONFIG::mac2str(uint8_t mac [WL_MAC_ADDR_LENGTH]) char * WIFI_CONFIG::mac2str(uint8_t mac [WL_MAC_ADDR_LENGTH])
{ {
static char macstr [18]; static char macstr [18];
if (0>sprintf(macstr,"%02X:%02X:%02X:%02X:%02X:%02X",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5])) strcpy (macstr, "00:00:00:00:00:00"); if (0>sprintf(macstr,"%02X:%02X:%02X:%02X:%02X:%02X",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5])) {
return macstr; strcpy (macstr, "00:00:00:00:00:00");
}
return macstr;
} }
//just simple helper to convert IP address to string //just simple helper to convert IP address to string
char * WIFI_CONFIG::ip2str(IPAddress Ip ) char * WIFI_CONFIG::ip2str(IPAddress Ip )
{ {
static char ipstr [16]; static char ipstr [16];
if (0>sprintf(ipstr, "%i.%i.%i.%i",Ip[0],Ip[1],Ip[2],Ip[3])) strcpy (ipstr, "0.0.0.0"); if (0>sprintf(ipstr, "%i.%i.%i.%i",Ip[0],Ip[1],Ip[2],Ip[3])) {
return ipstr; strcpy (ipstr, "0.0.0.0");
}
return ipstr;
} }
void WIFI_CONFIG::Safe_Setup() void WIFI_CONFIG::Safe_Setup()
{ {
#ifdef CAPTIVE_PORTAL_FEATURE #ifdef CAPTIVE_PORTAL_FEATURE
dnsServer.stop(); dnsServer.stop();
delay(100); delay(100);
#endif #endif
WiFi.disconnect(); WiFi.disconnect();
//setup Soft AP //setup Soft AP
WiFi.mode(WIFI_AP); WiFi.mode(WIFI_AP);
IPAddress local_ip (DEFAULT_IP_VALUE[0],DEFAULT_IP_VALUE[1],DEFAULT_IP_VALUE[2],DEFAULT_IP_VALUE[3]); IPAddress local_ip (DEFAULT_IP_VALUE[0],DEFAULT_IP_VALUE[1],DEFAULT_IP_VALUE[2],DEFAULT_IP_VALUE[3]);
IPAddress gateway (DEFAULT_GATEWAY_VALUE[0],DEFAULT_GATEWAY_VALUE[1],DEFAULT_GATEWAY_VALUE[2],DEFAULT_GATEWAY_VALUE[3]); IPAddress gateway (DEFAULT_GATEWAY_VALUE[0],DEFAULT_GATEWAY_VALUE[1],DEFAULT_GATEWAY_VALUE[2],DEFAULT_GATEWAY_VALUE[3]);
IPAddress subnet (DEFAULT_MASK_VALUE[0],DEFAULT_MASK_VALUE[1],DEFAULT_MASK_VALUE[2],DEFAULT_MASK_VALUE[3]); IPAddress subnet (DEFAULT_MASK_VALUE[0],DEFAULT_MASK_VALUE[1],DEFAULT_MASK_VALUE[2],DEFAULT_MASK_VALUE[3]);
String ssid = FPSTR(DEFAULT_SSID); String ssid = FPSTR(DEFAULT_SSID);
String pwd = FPSTR(DEFAULT_PASSWORD); String pwd = FPSTR(DEFAULT_PASSWORD);
WiFi.softAP(ssid.c_str(),pwd.c_str()); WiFi.softAP(ssid.c_str(),pwd.c_str());
delay(500); delay(500);
WiFi.softAPConfig( local_ip, gateway, subnet); WiFi.softAPConfig( local_ip, gateway, subnet);
delay(1000); delay(1000);
Serial.println(F("M117 Safe mode started")); Serial.println(F("M117 Safe mode started"));
} }
//Read configuration settings and apply them //Read configuration settings and apply them
bool WIFI_CONFIG::Setup() bool WIFI_CONFIG::Setup()
{ {
char pwd[MAX_PASSWORD_LENGTH+1]; char pwd[MAX_PASSWORD_LENGTH+1];
char sbuf[MAX_SSID_LENGTH+1]; char sbuf[MAX_SSID_LENGTH+1];
char hostname [MAX_HOSTNAME_LENGTH+1]; char hostname [MAX_HOSTNAME_LENGTH+1];
int wstatus; int wstatus;
IPAddress currentIP; IPAddress currentIP;
byte bflag=0; byte bflag=0;
//set the sleep mode //set the sleep mode
if (!CONFIG::read_byte(EP_SLEEP_MODE, &bflag )) if (!CONFIG::read_byte(EP_SLEEP_MODE, &bflag )) {
{ return false;
return false; }
} wifi_set_sleep_type ((sleep_type)bflag);
wifi_set_sleep_type ((sleep_type)bflag); sleep_mode=bflag;
sleep_mode=bflag; //AP or client ?
//AP or client ? if (!CONFIG::read_byte(EP_WIFI_MODE, &bflag ) || !CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGTH) ||!CONFIG::read_string(EP_PASSWORD, pwd , MAX_PASSWORD_LENGTH)) {
if (!CONFIG::read_byte(EP_WIFI_MODE, &bflag ) || !CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGTH) ||!CONFIG::read_string(EP_PASSWORD, pwd , MAX_PASSWORD_LENGTH)) return false;
{ }
return false; if (!CONFIG::read_string(EP_HOSTNAME, hostname , MAX_HOSTNAME_LENGTH)) {
} strcpy(hostname,get_default_hostname());
if (!CONFIG::read_string(EP_HOSTNAME, hostname , MAX_HOSTNAME_LENGTH))strcpy(hostname,get_default_hostname()); }
//disconnect if connected //disconnect if connected
WiFi.disconnect(); WiFi.disconnect();
//this is AP mode //this is AP mode
if (bflag==AP_MODE) if (bflag==AP_MODE) {
{ //setup Soft AP
//setup Soft AP WiFi.mode(WIFI_AP);
WiFi.mode(WIFI_AP); WiFi.softAP(sbuf, pwd);
WiFi.softAP(sbuf, pwd); //setup PHY_MODE
//setup PHY_MODE if (!CONFIG::read_byte(EP_PHY_MODE, &bflag )) {
if (!CONFIG::read_byte(EP_PHY_MODE, &bflag )) return false;
{ }
return false; wifi_set_phy_mode((phy_mode)bflag);
} //get current config
wifi_set_phy_mode((phy_mode)bflag); struct softap_config apconfig;
//get current config wifi_softap_get_config(&apconfig);
struct softap_config apconfig; //set the chanel
wifi_softap_get_config(&apconfig); if (!CONFIG::read_byte(EP_CHANNEL, &bflag )) {
//set the chanel return false;
if (!CONFIG::read_byte(EP_CHANNEL, &bflag ))return false; }
apconfig.channel=bflag; apconfig.channel=bflag;
//set Authentification type //set Authentification type
if (!CONFIG::read_byte(EP_AUTH_TYPE, &bflag ))return false; if (!CONFIG::read_byte(EP_AUTH_TYPE, &bflag )) {
apconfig.authmode=(AUTH_MODE)bflag; return false;
//set the visibility of SSID }
if (!CONFIG::read_byte(EP_SSID_VISIBLE, &bflag ))return false; apconfig.authmode=(AUTH_MODE)bflag;
apconfig.ssid_hidden=!bflag; //set the visibility of SSID
//no need to add these settings to configuration just use default ones if (!CONFIG::read_byte(EP_SSID_VISIBLE, &bflag )) {
apconfig.max_connection=DEFAULT_MAX_CONNECTIONS; return false;
apconfig.beacon_interval=DEFAULT_BEACON_INTERVAL; }
//apply settings to current and to default apconfig.ssid_hidden=!bflag;
if (!wifi_softap_set_config(&apconfig) || !wifi_softap_set_config_current(&apconfig)) //no need to add these settings to configuration just use default ones
{ apconfig.max_connection=DEFAULT_MAX_CONNECTIONS;
Serial.println(F("M117 Error Wifi AP!")); apconfig.beacon_interval=DEFAULT_BEACON_INTERVAL;
delay(1000); //apply settings to current and to default
} if (!wifi_softap_set_config(&apconfig) || !wifi_softap_set_config_current(&apconfig)) {
} Serial.println(F("M117 Error Wifi AP!"));
else delay(1000);
{//setup station mode }
WiFi.mode(WIFI_STA); } else {
WiFi.begin(sbuf, pwd); //setup station mode
delay(500); WiFi.mode(WIFI_STA);
//setup PHY_MODE WiFi.begin(sbuf, pwd);
if (!CONFIG::read_byte(EP_PHY_MODE, &bflag ))return false; delay(500);
wifi_set_phy_mode((phy_mode)bflag); //setup PHY_MODE
byte i=0; if (!CONFIG::read_byte(EP_PHY_MODE, &bflag )) {
//try to connect return false;
while (WiFi.status() != WL_CONNECTED && i<40) { }
switch(WiFi.status()) wifi_set_phy_mode((phy_mode)bflag);
{ byte i=0;
case 1: //try to connect
Serial.print(FPSTR(M117_)); while (WiFi.status() != WL_CONNECTED && i<40) {
Serial.println(F("No SSID found!")); switch(WiFi.status()) {
break; case 1:
Serial.print(FPSTR(M117_));
Serial.println(F("No SSID found!"));
break;
case 4: case 4:
Serial.print(FPSTR(M117_)); Serial.print(FPSTR(M117_));
Serial.println(F("No Connection!")); Serial.println(F("No Connection!"));
break; break;
default: default:
Serial.print(FPSTR(M117_)); Serial.print(FPSTR(M117_));
Serial.println(F("Connecting...")); Serial.println(F("Connecting..."));
break; break;
} }
delay(500); delay(500);
i++; i++;
} }
if (WiFi.status() != WL_CONNECTED) return false; if (WiFi.status() != WL_CONNECTED) {
WiFi.hostname(hostname); return false;
}
WiFi.hostname(hostname);
} }
//DHCP or Static IP ? //DHCP or Static IP ?
if (!CONFIG::read_byte(EP_IP_MODE, &bflag )) return false; if (!CONFIG::read_byte(EP_IP_MODE, &bflag )) {
if (bflag==STATIC_IP_MODE) return false;
{
byte ip_buf[4];
//get the IP
if (!CONFIG::read_buffer(EP_IP_VALUE,ip_buf , IP_LENGTH))return false;
IPAddress local_ip (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]);
//get the gateway
if (!CONFIG::read_buffer(EP_GATEWAY_VALUE,ip_buf , IP_LENGTH))return false;
IPAddress gateway (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]);
//get the mask
if (!CONFIG::read_buffer(EP_MASK_VALUE,ip_buf , IP_LENGTH))return false;
IPAddress subnet (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]);
//apply according active wifi mode
if (wifi_get_opmode()==WIFI_AP || wifi_get_opmode()==WIFI_AP_STA) WiFi.softAPConfig( local_ip, gateway, subnet);
else WiFi.config( local_ip, gateway, subnet);
} }
#ifdef MDNS_FEATURE if (bflag==STATIC_IP_MODE) {
byte ip_buf[4];
//get the IP
if (!CONFIG::read_buffer(EP_IP_VALUE,ip_buf , IP_LENGTH)) {
return false;
}
IPAddress local_ip (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]);
//get the gateway
if (!CONFIG::read_buffer(EP_GATEWAY_VALUE,ip_buf , IP_LENGTH)) {
return false;
}
IPAddress gateway (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]);
//get the mask
if (!CONFIG::read_buffer(EP_MASK_VALUE,ip_buf , IP_LENGTH)) {
return false;
}
IPAddress subnet (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]);
//apply according active wifi mode
if (wifi_get_opmode()==WIFI_AP || wifi_get_opmode()==WIFI_AP_STA) {
WiFi.softAPConfig( local_ip, gateway, subnet);
} else {
WiFi.config( local_ip, gateway, subnet);
}
}
#ifdef MDNS_FEATURE
// Set up mDNS responder: // Set up mDNS responder:
if (!mdns.begin(hostname)) { if (!mdns.begin(hostname)) {
Serial.print(FPSTR(M117_)); Serial.print(FPSTR(M117_));
Serial.println(F("Error with mDNS!")); Serial.println(F("Error with mDNS!"));
delay(1000); delay(1000);
} }
#endif #endif
//Get IP //Get IP
if (wifi_get_opmode()==WIFI_STA)currentIP=WiFi.localIP(); if (wifi_get_opmode()==WIFI_STA) {
else currentIP=WiFi.softAPIP(); currentIP=WiFi.localIP();
} else {
currentIP=WiFi.softAPIP();
}
Serial.print(FPSTR(M117_)); Serial.print(FPSTR(M117_));
Serial.println(currentIP); Serial.println(currentIP);
return true; return true;
} }
WIFI_CONFIG wifi_config; WIFI_CONFIG wifi_config;

View File

@ -1,8 +1,8 @@
/* /*
wifi.h - esp8266 configuration class wifi.h - esp8266 configuration class
Copyright (c) 2014 Luc Lebosse. All rights reserved. Copyright (c) 2014 Luc Lebosse. All rights reserved.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
@ -31,25 +31,25 @@
class WIFI_CONFIG class WIFI_CONFIG
{ {
public: public:
// multicast DNS responder feature // multicast DNS responder feature
#ifdef MDNS_FEATURE #ifdef MDNS_FEATURE
MDNSResponder mdns; MDNSResponder mdns;
#endif #endif
WIFI_CONFIG(); WIFI_CONFIG();
int iweb_port; int iweb_port;
int idata_port; int idata_port;
long baud_rate; long baud_rate;
int sleep_mode; int sleep_mode;
bool Setup(); bool Setup();
void Safe_Setup(); void Safe_Setup();
char * mac2str(uint8_t mac [WL_MAC_ADDR_LENGTH]); char * mac2str(uint8_t mac [WL_MAC_ADDR_LENGTH]);
char * ip2str(IPAddress Ip ); char * ip2str(IPAddress Ip );
byte split_ip (const char * ptr,byte * part); byte split_ip (const char * ptr,byte * part);
const char * get_default_hostname(); const char * get_default_hostname();
const char * get_hostname(); const char * get_hostname();
private: private:
char _hostname[33]; char _hostname[33];
}; };
extern WIFI_CONFIG wifi_config; extern WIFI_CONFIG wifi_config;