@ -2,16 +2,23 @@
# include "DebugLog.h"
# include "DebugLog.h"
# include "DataLogger.h"
# include "DataLogger.h"
# include "Info.h"
# include "utils.h"
# include <Arduino.h>
# include <Arduino.h>
# include <SPIFFS.h>
# include <SPIFFS.h>
# include <string>
# include <string>
# include <cstring>
# include "HTTPServer.hpp"
# include "HTTPSS erver.hpp"
# include "SSLCert.hpp"
# include "SSLCert.hpp"
# include "HTTPRequest.hpp"
# include "HTTPRequest.hpp"
# include "HTTPResponse.hpp"
# include "HTTPResponse.hpp"
// generated certificate data
# include "cert.h"
# include "private_key.h"
/** Check if we have multiple cores */
/** Check if we have multiple cores */
# if CONFIG_FREERTOS_UNICORE
# if CONFIG_FREERTOS_UNICORE
# define WEBSERVER_RUNNING_CORE 0
# define WEBSERVER_RUNNING_CORE 0
@ -25,7 +32,13 @@ detail::WebServer WebServer;
namespace detail
namespace detail
{
{
HTTPServer httpServer ;
// Create an SSL certificate object from the files included above
SSLCert cert = SSLCert (
crt_DER , crt_DER_len ,
private_key_DER , private_key_DER_len
) ;
HTTPSServer httpServer = HTTPSServer ( & cert ) ;
WebServer : : WebServer ( )
WebServer : : WebServer ( )
{
{
@ -121,7 +134,76 @@ namespace detail
void WebServer : : HandlePostInfo_ ( httpsserver : : HTTPRequest * request , httpsserver : : HTTPResponse * response )
void WebServer : : HandlePostInfo_ ( httpsserver : : HTTPRequest * request , httpsserver : : HTTPResponse * response )
{
{
char body [ 128 ] ; // in current implementation, we expect the whole body to fit here
size_t bodySize = request - > readChars ( body , sizeof ( body ) - 1 ) ;
body [ bodySize ] = 0 ;
request - > discardRequestBody ( ) ;
request - > discardRequestBody ( ) ;
//::DebugLog.println(body.c_str());
Info receivedInfo ;
char * nextParam = body ;
while ( true )
{
char * sepPos = strstr ( nextParam , " & " ) ;
if ( sepPos = = nullptr ) sepPos = & body [ bodySize ] ;
if ( nextParam = = sepPos )
break ; // should happen only if body is an empty string or ends with an "&" character
* sepPos = 0 ; // split the string in-place, this overrides the separating character which is fine
//::DebugLog.println(nextParam);
char * eqPos = strstr ( nextParam , " = " ) ;
if ( eqPos ! = nullptr & & eqPos ! = nextParam & & * ( eqPos + 1 ) ! = 0 )
{
* eqPos = 0 ; // split the string in-place, overriding the equal sign
char * paramName = nextParam ;
char * paramValue = eqPos + 1 ;
utils : : replaceString ( paramValue , " %3A " , " : " ) ;
//::DebugLog.print(paramName); ::DebugLog.print(" = "); ::DebugLog.println(paramValue);
if ( strcmp ( paramName , " lat " ) = = 0 )
{
char * ending = nullptr ;
receivedInfo . latitude = strtof ( paramValue , & ending ) ;
if ( * ending ! = 0 )
receivedInfo . latitude = - 1000.0f ;
}
else if ( strcmp ( paramName , " lng " ) = = 0 )
{
char * ending = nullptr ;
receivedInfo . longitude = strtof ( paramValue , & ending ) ;
if ( * ending ! = 0 )
receivedInfo . longitude = - 1000.0f ;
}
else if ( strcmp ( paramName , " alt " ) = = 0 )
{
char * ending = nullptr ;
receivedInfo . gpsAltitude = strtof ( paramValue , & ending ) ;
if ( * ending ! = 0 )
receivedInfo . gpsAltitude = - 1000.0f ;
}
else if ( strcmp ( paramName , " time " ) = = 0 )
{
strncpy ( receivedInfo . realtime , paramValue , sizeof ( receivedInfo . realtime ) ) ;
}
}
if ( sepPos = = & body [ bodySize ] )
break ;
nextParam = sepPos + 1 ;
}
if ( : : WebServer . infoReceived_ ! = nullptr )
{
: : WebServer . infoReceived_ ( receivedInfo ) ;
}
}
}
void WebServer : : HandleDefault_ ( HTTPRequest * request , HTTPResponse * response )
void WebServer : : HandleDefault_ ( HTTPRequest * request , HTTPResponse * response )
@ -165,7 +247,7 @@ namespace detail
void WebServer : : SendContent_ ( Stream & stream , httpsserver : : HTTPResponse * response )
void WebServer : : SendContent_ ( Stream & stream , httpsserver : : HTTPResponse * response )
{
{
uint8_t buffer [ 5 12] ;
uint8_t buffer [ 128 ] ;
while ( stream . available ( ) )
while ( stream . available ( ) )
{
{
size_t numBytes = stream . readBytes ( buffer , sizeof ( buffer ) ) ;
size_t numBytes = stream . readBytes ( buffer , sizeof ( buffer ) ) ;