Compare commits

..

11 Commits

12 changed files with 61 additions and 142 deletions

View File

@ -4,7 +4,7 @@
#include "AbstractParser.h" #include "AbstractParser.h"
#include "CFGDictionary.h" #include "CFGDictionary.h"
#include "SDCardManager.h" #include "SDCardManager.h"
#include "definition.h" #include "utilities.h"
#include "CFGParameterValue.h" #include "CFGParameterValue.h"
//Forward class declaration because of cross includes... //Forward class declaration because of cross includes...

View File

@ -1,9 +1,9 @@
#ifndef FTPSERVER_H #ifndef FTPSERVER_H
#define FTPSERVER_H #define FTPSERVER_H
#include <SD.h>
#include "TCPServer.h" #include "TCPServer.h"
#include "SDCardManager.h" #include "utilities.h"
#include "definition.h"
#include "Dictionary.h" #include "Dictionary.h"
//#define DEBUG_FTPS //#define DEBUG_FTPS
#define READ_BUFFER_SIZE 2048 //2048 is max to read sd card, more will crash #define READ_BUFFER_SIZE 2048 //2048 is max to read sd card, more will crash
@ -18,12 +18,12 @@ class FTPServer : public TCPServer<T>
enum BinaryFlag {OFF = 0, ON}; enum BinaryFlag {OFF = 0, ON};
enum FtpMsgCode {_150, _200, _215, _220, _221, _230, _226, _227, _250, _257, _331, _350, _451, _5_502, _504, _530, _550 }; enum FtpMsgCode {_150, _200, _215, _220, _221, _230, _226, _227, _250, _257, _331, _350, _451, _5_502, _504, _530, _550 };
FTPServer(uint16_t port = 21, SDCardManager *sdCardManager = NULL, const char *login = NULL, const char *password = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientCommandDataBufferSize = 255) : TCPServer<T>(port, maxClient, clientCommandDataBufferSize), FTPServer(uint16_t port = 21, SDClass *sdClass = NULL, const char *login = NULL, const char *password = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientCommandDataBufferSize = 255) : TCPServer<T>(port, maxClient, clientCommandDataBufferSize),
_login(NULL), _login(NULL),
_password(NULL), _password(NULL),
_dataPort(1024), _dataPort(1024),
_dataServer(_dataPort), _dataServer(_dataPort),
_sdCardManager(sdCardManager) _sdClass(sdClass)
{ {
if (login != NULL) if (login != NULL)
{ {
@ -65,6 +65,11 @@ class FTPServer : public TCPServer<T>
} }
} }
virtual void setFTPDir(const char *FTPDir)
{
_FTPDir = FTPDir;
}
protected: protected:
virtual T* createNewClient(WiFiClient wc) virtual T* createNewClient(WiFiClient wc)
{ {
@ -191,10 +196,10 @@ class FTPServer : public TCPServer<T>
{ {
if(client->_fileRecvBytes == 0) //File was just created empty if(client->_fileRecvBytes == 0) //File was just created empty
{ {
if(_sdCardManager->exists(client->_currentFile)) if(_sdClass->exists(client->_currentFile))
_sdCardManager->remove(client->_currentFile); _sdClass->remove(client->_currentFile);
File file2create = _sdCardManager->open(client->_currentFile, FILE_WRITE); File file2create = _sdClass->open(client->_currentFile, FILE_WRITE);
file2create.close(); file2create.close();
#ifdef DEBUG_FTPS #ifdef DEBUG_FTPS
@ -275,7 +280,7 @@ class FTPServer : public TCPServer<T>
processCommands(client); processCommands(client);
break; break;
case INIT: case INIT:
client->setCurrentDirectory(FTP_DIR); _FTPDir ? client->setCurrentDirectory(_FTPDir) : client->setCurrentDirectory("");
client->_ftpClientState = WAITING_FOR_COMMANDS; client->_ftpClientState = WAITING_FOR_COMMANDS;
break; break;
} }
@ -555,7 +560,7 @@ class FTPServer : public TCPServer<T>
Serial.printf("Final dirName : #%s#\n",dirNameWithPath); Serial.printf("Final dirName : #%s#\n",dirNameWithPath);
#endif #endif
if(_sdCardManager->mkdir(dirNameWithPath)) if(_sdClass->mkdir(dirNameWithPath))
{ {
client->_client.printf("257 \"%s\"\r\n", dirNameWithPath); client->_client.printf("257 \"%s\"\r\n", dirNameWithPath);
} }
@ -591,7 +596,7 @@ class FTPServer : public TCPServer<T>
Serial.printf("pathDirName to delete : #%s#\n",dirNameWithPath); Serial.printf("pathDirName to delete : #%s#\n",dirNameWithPath);
#endif #endif
if(_sdCardManager->rmdir(dirNameWithPath)) if(_sdClass->rmdir(dirNameWithPath))
{ {
client->_client.println("250 Requested file action okay."); client->_client.println("250 Requested file action okay.");
} }
@ -677,7 +682,7 @@ class FTPServer : public TCPServer<T>
Serial.printf("file to delete : #%s#\n",file2deleteNameWithPath); Serial.printf("file to delete : #%s#\n",file2deleteNameWithPath);
#endif #endif
if(_sdCardManager->remove(file2deleteNameWithPath)) if(_sdClass->remove(file2deleteNameWithPath))
{ {
client->_client.println("250 Requested file action okay."); client->_client.println("250 Requested file action okay.");
} }
@ -747,7 +752,7 @@ class FTPServer : public TCPServer<T>
Serial.printf("Old name : %s --> %s\n",client->_currentFile,file2RenameNameWithPath); Serial.printf("Old name : %s --> %s\n",client->_currentFile,file2RenameNameWithPath);
#endif #endif
if(_sdCardManager->rename(client->_currentFile,file2RenameNameWithPath)) if(_sdClass->rename(client->_currentFile,file2RenameNameWithPath))
{ {
client->_client.println("250 Requested file action okay."); client->_client.println("250 Requested file action okay.");
}else }else
@ -789,10 +794,10 @@ class FTPServer : public TCPServer<T>
{ {
if (client->_currentFile != NULL) if (client->_currentFile != NULL)
{ {
if(_sdCardManager->exists(client->_currentFile) && client->_fileRecvBytes == 0 && !append) if(_sdClass->exists(client->_currentFile) && client->_fileRecvBytes == 0 && !append)
_sdCardManager->remove(client->_currentFile); _sdClass->remove(client->_currentFile);
File fileBeeingReceived = _sdCardManager->open(client->_currentFile, FILE_WRITE); File fileBeeingReceived = _sdClass->open(client->_currentFile, FILE_WRITE);
if(fileBeeingReceived) if(fileBeeingReceived)
{ {
@ -830,7 +835,7 @@ class FTPServer : public TCPServer<T>
#ifdef DEBUG_FTPS #ifdef DEBUG_FTPS
Serial.printf("Directory : %s\n",client->_currentDirectory); Serial.printf("Directory : %s\n",client->_currentDirectory);
#endif #endif
File currentDirectory = _sdCardManager->open(client->_currentDirectory); File currentDirectory = _sdClass->open(client->_currentDirectory);
if (currentDirectory) if (currentDirectory)
{ {
currentDirectory.rewindDirectory(); currentDirectory.rewindDirectory();
@ -886,7 +891,7 @@ class FTPServer : public TCPServer<T>
if (client->_currentFile != NULL) if (client->_currentFile != NULL)
{ {
uint8_t sendBuffer[READ_BUFFER_SIZE]; uint8_t sendBuffer[READ_BUFFER_SIZE];
File fileToSend = _sdCardManager->open(client->_currentFile); File fileToSend = _sdClass->open(client->_currentFile);
if (fileToSend) if (fileToSend)
{ {
@ -992,10 +997,11 @@ class FTPServer : public TCPServer<T>
char *_login; char *_login;
char *_password; char *_password;
const char *_FTPDir = NULL; //Pointer to constant string and char * cont is a constant pointer to string
uint16_t _dataPort; uint16_t _dataPort;
WiFiServer _dataServer; //In passive mode, the FTP server opens two different ports (one for the commands and the other for the data stream) WiFiServer _dataServer; //In passive mode, the FTP server opens two different ports (one for the commands and the other for the data stream)
SDCardManager *_sdCardManager; SDClass *_sdClass;
}; };
#endif //FTPSERVER_H #endif //FTPSERVER_H

View File

@ -1,7 +1,8 @@
#ifndef IOMANAGER_H #ifndef IOMANAGER_H
#define IOMANAGER_H #define IOMANAGER_H
#include "definition.h"
#include <PCF8574.h> #include <PCF8574.h>
#include "definition.h"
class IOManager class IOManager
{ {

View File

@ -13,7 +13,7 @@ _rtcManager(_rtc),
_connectivityManager(NULL), _connectivityManager(NULL),
//_webServerManager(80, &_sdCardManager), //_webServerManager(80, &_sdCardManager),
_webServer(80, &_sdCardManager, 10), _webServer(80, &_sdCardManager, 10),
_ftpServer(21, &_sdCardManager, "ESP8266", "12345678"), _ftpServer(21, &_sdCardManager, "ESP8266", "12345678", 10),
_dbWSServer(81), _dbWSServer(81),
_pcf(_boardConfig.getI2C_IOExpanderAddress(), Wire), _pcf(_boardConfig.getI2C_IOExpanderAddress(), Wire),
_ioManager(_pcf), _ioManager(_pcf),
@ -32,7 +32,7 @@ _rtcManager(_rtc),
_connectivityManager(NULL), _connectivityManager(NULL),
//_webServerManager(webServerPort, &_sdCardManager), //_webServerManager(webServerPort, &_sdCardManager),
_webServer(webServerPort, &_sdCardManager, 10), _webServer(webServerPort, &_sdCardManager, 10),
_ftpServer(ftpServerPort, &_sdCardManager, "ESP8266", "12345678"), _ftpServer(ftpServerPort, &_sdCardManager, "ESP8266", "12345678", 10),
_dbWSServer(81), _dbWSServer(81),
_pcf(_boardConfig.getI2C_IOExpanderAddress(), Wire), _pcf(_boardConfig.getI2C_IOExpanderAddress(), Wire),
_ioManager(_pcf), _ioManager(_pcf),
@ -70,6 +70,9 @@ void SAB::initCommonConfig()
_powerUpTime = _rtcManager.getDateTime(); _powerUpTime = _rtcManager.getDateTime();
//We set the different servers : //We set the different servers :
_webServer.setWWWDir(WWW_DIR);
_ftpServer.setFTPDir(FTP_DIR);
//We start the servers
_dbWSServer.begin(); _dbWSServer.begin();
_webServer.enableTCPKeepAlive(15,5,5); _webServer.enableTCPKeepAlive(15,5,5);
_ftpServer.enableTCPKeepAlive(15,5,5); _ftpServer.enableTCPKeepAlive(15,5,5);

View File

@ -196,7 +196,7 @@ void ScreenManager::iterateThroughList()
} }
} }
ViewLink* ScreenManager::getLinkByUID(ViewLinkedList viewLinkedList, const unsigned char UID) ScreenManager::ViewLink* ScreenManager::getLinkByUID(ViewLinkedList viewLinkedList, const unsigned char UID)
{ {
if(isListEmpty(viewLinkedList)) if(isListEmpty(viewLinkedList))
return NULL; return NULL;

View File

@ -2,7 +2,6 @@
#define SCREENMANAGER_H #define SCREENMANAGER_H
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
#include "definition.h"
#include "SDCardManager.h" #include "SDCardManager.h"
#include "CFGFileParser.h" #include "CFGFileParser.h"
#include "CFGDictionary.h" #include "CFGDictionary.h"
@ -13,6 +12,14 @@ class ScreenManager
friend class SAB; friend class SAB;
public: public:
enum Error {OK, MALLOC_FAILED, VIEW_NOT_FOUND, VIEW_FUNC_UNDEFINED, VIEW_FAILED_TO_EXECUTE, CURRENT_VIEW_UNDEFINED}; enum Error {OK, MALLOC_FAILED, VIEW_NOT_FOUND, VIEW_FUNC_UNDEFINED, VIEW_FAILED_TO_EXECUTE, CURRENT_VIEW_UNDEFINED};
//Data structure for the view handling
typedef struct viewLink
{
boolean (*viewLogicFunction)(Adafruit_SSD1306&, void*);
void *pData;
const int UID;
struct viewLink *next, *previous;
} ViewLink, *ViewLinkedList;
boolean addView(boolean (*viewLogicFunction)(Adafruit_SSD1306&, void*), void *pData, const unsigned char UID); boolean addView(boolean (*viewLogicFunction)(Adafruit_SSD1306&, void*), void *pData, const unsigned char UID);
boolean removeView(const unsigned char UID); boolean removeView(const unsigned char UID);

View File

@ -1,5 +1,5 @@
#include "Dictionary.h" #include "Dictionary.h"
#include "definition.h" #include "utilities.h"
Dictionary<DictionaryHelper::StringEntity> *DictionaryHelper::StringEntity::split(char character) Dictionary<DictionaryHelper::StringEntity> *DictionaryHelper::StringEntity::split(char character)
{ {

View File

@ -1,10 +1,11 @@
#ifndef WEBSERVER_H #ifndef WEBSERVER_H
#define WEBSERVER_H #define WEBSERVER_H
#include <SD.h>
#include "TCPServer.h" #include "TCPServer.h"
#include "Dictionary.h" #include "Dictionary.h"
#include "SDCardManager.h"
#include "HttpConstants.h" #include "HttpConstants.h"
#include "utilities.h"
//#define DEBUG_WEBS //#define DEBUG_WEBS
#define READ_WRITE_BUFFER_SIZE 2000 #define READ_WRITE_BUFFER_SIZE 2000
@ -32,7 +33,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
uint16_t maxBodyBuffer; uint16_t maxBodyBuffer;
}; };
WEBServer(uint16_t port = 80, SDCardManager *sdCardManager = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientDataBufferSize = 255) : TCPServer<T>(port, maxClient, clientDataBufferSize), _sdCardManager(sdCardManager) {} WEBServer(uint16_t port = 80, SDClass *sdClass = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientDataBufferSize = 255) : TCPServer<T>(port, maxClient, clientDataBufferSize), _sdClass(sdClass) {}
boolean addApiRoutine(const char *uri, boolean (*apiRoutine)(HttpRequestData&, WiFiClient*, void*), void *pData, HttpRequestMethod HRM = UNDEFINED) boolean addApiRoutine(const char *uri, boolean (*apiRoutine)(HttpRequestData&, WiFiClient*, void*), void *pData, HttpRequestMethod HRM = UNDEFINED)
{ {
@ -57,6 +58,11 @@ class WEBServer : public TCPServer<T>, public HttpConstants
free(buffer); free(buffer);
} }
} }
void setWWWDir(const char *WWWDir)
{
_WWWDir = WWWDir;
}
protected: protected:
private: private:
virtual T* createNewClient(WiFiClient wc) virtual T* createNewClient(WiFiClient wc)
@ -470,7 +476,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
boolean sendPageToClientFromSdCard(T *client) boolean sendPageToClientFromSdCard(T *client)
{ {
if(_sdCardManager != NULL) if(_sdClass != NULL)
{ {
File pageToSend; File pageToSend;
char *filePath(NULL), *header(NULL); char *filePath(NULL), *header(NULL);
@ -480,7 +486,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
switch(client->_httpRequestData.HRM) switch(client->_httpRequestData.HRM)
{ {
case GET: case GET:
filePath = getFilePathByHttpResource(client->_httpRequestData.httpResource); filePath = getFilePathByHttpResource(_WWWDir, client->_httpRequestData.httpResource);
if(filePath == NULL) if(filePath == NULL)
{ {
sendInfoResponse(HTTP_CODE::HTTP_CODE_INTERNAL_SERVER_ERROR, client, "Failed to allocate memory for the filePath"); sendInfoResponse(HTTP_CODE::HTTP_CODE_INTERNAL_SERVER_ERROR, client, "Failed to allocate memory for the filePath");
@ -492,7 +498,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
Serial.println(filePath); Serial.println(filePath);
#endif #endif
pageToSend = _sdCardManager->open(filePath); pageToSend = _sdClass->open(filePath);
free(filePath);filePath = NULL; free(filePath);filePath = NULL;
//If we couldn't open the file //If we couldn't open the file
@ -710,16 +716,16 @@ class WEBServer : public TCPServer<T>, public HttpConstants
return p != NULL ? p+1 : NULL; return p != NULL ? p+1 : NULL;
} }
static char *getFilePathByHttpResource(char *res) static char *getFilePathByHttpResource(const char *WWWDir, char *res)
{ {
uint16_t buffSize = strlen(WWW_DIR) + (strcmp(res, "/") == 0 ? 10:strlen(res)) + 1;//10 for /index.htm +1 for \0 uint16_t buffSize = (WWWDir ? strlen(WWWDir) : 0 /*default is / */) + (strcmp(res, "/") == 0 ? 10:strlen(res)) + 1;//10 for /index.htm +1 for \0
char *filePath = (char*) malloc( sizeof(char) * buffSize); char *filePath = (char*) malloc( sizeof(char) * buffSize);
if(filePath == NULL) if(filePath == NULL)
return NULL; return NULL;
strcpy(filePath, WWW_DIR); WWWDir ? strcpy(filePath, WWWDir) : strcpy(filePath, "");
strcat(filePath, strcmp(res, "/") == 0 ? "/index.htm":res); strcat(filePath, (strcmp(res, "/") == 0) ? "/index.htm":res);
#ifdef DEBUG_FILEPATH #ifdef DEBUG_FILEPATH
Serial.println(res); Serial.println(res);
@ -750,7 +756,8 @@ class WEBServer : public TCPServer<T>, public HttpConstants
}; };
Dictionary<ApiRoutine> _apiDictionary; Dictionary<ApiRoutine> _apiDictionary;
SDCardManager *_sdCardManager; SDClass *_sdClass;
const char *_WWWDir = NULL; //Website root folder
}; };
#endif //WEBSERVER_H #endif //WEBSERVER_H

View File

@ -1,88 +0,0 @@
#include "definition.h"
char *addChar(char *pointer, const char character)
{
char *tempAddr = NULL;
if(pointer == NULL)
{
tempAddr = (char *) realloc(pointer, 2*sizeof(char));
if(tempAddr == NULL)
return NULL;
else
{
pointer = tempAddr;
pointer[0] = character;
pointer[1] = '\0';
}
}
else
{
tempAddr = (char *) realloc(pointer, (strlen(pointer)+2)*sizeof(char));
if(tempAddr == NULL)
{
free(pointer);
return NULL;
}
else
{
pointer = tempAddr;
pointer[strlen(pointer)+1] = '\0';
pointer[strlen(pointer)] = character;
}
}
return pointer;
}
char *dateTimeFormater(char *pointer, const uint8_t value, const char character)
{
if(pointer == NULL)
return pointer;
if(value < 10)
{
sprintf(pointer,"%d", value);
*(pointer+1) = *(pointer);*(pointer) = character;*(pointer+2) = '\0';
}
else
sprintf(pointer,"%d", value);
return pointer;
}
char *lastIndexOf(char *str, const char character)
{
char *last(NULL), *current(str);
do
{
current = strchr(current, character);
if(current != NULL)
{
last = current;
if(*(current+1) == '\0')break;
current += 1;
}
}while(current != NULL);
return last;
}
/**
* The monthNumTo3LetterAbbreviation function takes the month number from 1 to 12 and returns the abbreviation in a 3 letter
* format.
*/
uint32_t monthNumTo3LetterAbbreviation(const uint8_t monthNumber)
{
/**
* This array contains months as 3 letter abbreviations
* Jan is written \0naJ and in hex : 0x006E614A an so on. They are thus 4Bytes aligned and easy to put and read back from prog mem :)
*/
static const uint32_t PROGMEM monthArray[] = {0x6E614A, 0x626546, 0x72614D, 0x727041, 0x79614D, 0x6E754A, 0x6C754A, 0x677541, 0x706553, 0x74634F, 0x766f4E, 0x636544};
uint32_t toReturn(0x6E614A); //Default to Jan.
if(monthNumber >= 1 && monthNumber <= 12)
toReturn = monthArray[monthNumber - 1];
return toReturn;
}

View File

@ -42,21 +42,4 @@ typedef enum { GPIO_0 = 0,
typedef enum { OR_0 = 2, OR_90 = 3, OR_180 = 0, OR_270 = 1 } Orientation; typedef enum { OR_0 = 2, OR_90 = 3, OR_180 = 0, OR_270 = 1 } Orientation;
typedef enum { BIT = 0, BYTE, KBIT, KBYTE, MBIT, MBYTE, GBIT, GBYTE } SizeUnit; typedef enum { BIT = 0, BYTE, KBIT, KBYTE, MBIT, MBYTE, GBIT, GBYTE } SizeUnit;
//Data structure for the view handling
typedef struct viewLink{
boolean (*viewLogicFunction)(Adafruit_SSD1306&, void*);
void *pData;
const int UID;
struct viewLink *next, *previous;
} ViewLink, *ViewLinkedList;
char *addChar(char *pointer, const char character);
char *lastIndexOf(char *str, const char character);
char *dateTimeFormater(char *pointer, const uint8_t value, const char character);
uint32_t monthNumTo3LetterAbbreviation(const uint8_t monthNumber);
#endif //DEFINITION_H #endif //DEFINITION_H

View File

@ -11,7 +11,7 @@ boolean task_blink(void *pData)
boolean task_batt_sensing(void *pData) boolean task_batt_sensing(void *pData)
{ {
View1Packet *p = (View1Packet *) pData; View1Packet *p = (View1Packet *) pData;
Serial.printf_P(PSTR("BATT SENSING...\nRunning since : %d s\n"), millis()/1000); Serial.printf_P(PSTR("BATT SENSING...\nRunning since : %d s\nWEB Server clients : %u\n"), millis()/1000,p->sab->getWebServer().getConnectedClientsCount());
p->powerInfo = p->sab->getPowerManager().getPowerInfo(); p->powerInfo = p->sab->getPowerManager().getPowerInfo();
return true; return true;