Browse Source
Merge pull request #68 from rjwats/clang-format
Merge pull request #68 from rjwats/clang-format
reformat with .clang-format based on google's spec with some minor ch…master
rjwats
5 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
40 changed files with 919 additions and 914 deletions
-
15.clang-format
-
13lib/framework/APSettingsService.cpp
-
63lib/framework/APSettingsService.h
-
13lib/framework/APStatus.cpp
-
24lib/framework/APStatus.h
-
74lib/framework/AdminSettingsService.h
-
59lib/framework/ArduinoJsonJWT.cpp
-
22lib/framework/ArduinoJsonJWT.h
-
38lib/framework/AsyncJsonCallbackResponse.h
-
199lib/framework/AsyncJsonWebHandler.h
-
32lib/framework/AuthenticationService.cpp
-
32lib/framework/AuthenticationService.h
-
36lib/framework/ESP8266React.cpp
-
81lib/framework/ESP8266React.h
-
46lib/framework/NTPSettingsService.cpp
-
53lib/framework/NTPSettingsService.h
-
17lib/framework/NTPStatus.cpp
-
26lib/framework/NTPStatus.h
-
45lib/framework/OTASettingsService.cpp
-
49lib/framework/OTASettingsService.h
-
7lib/framework/RestartService.cpp
-
22lib/framework/RestartService.h
-
26lib/framework/SecurityManager.cpp
-
166lib/framework/SecurityManager.h
-
12lib/framework/SecuritySettingsService.cpp
-
18lib/framework/SecuritySettingsService.h
-
62lib/framework/SettingsPersistence.h
-
72lib/framework/SettingsService.h
-
91lib/framework/SimpleService.h
-
13lib/framework/SystemStatus.cpp
-
24lib/framework/SystemStatus.h
-
48lib/framework/WiFiScanner.cpp
-
30lib/framework/WiFiScanner.h
-
118lib/framework/WiFiSettingsService.cpp
-
76lib/framework/WiFiSettingsService.h
-
27lib/framework/WiFiStatus.cpp
-
48lib/framework/WiFiStatus.h
-
9src/DemoProject.cpp
-
25src/DemoProject.h
-
2src/main.cpp
@ -0,0 +1,15 @@ |
|||
Language: Cpp |
|||
BasedOnStyle: Google |
|||
ColumnLimit: 120 |
|||
AllowShortBlocksOnASingleLine: false |
|||
AllowShortFunctionsOnASingleLine: false |
|||
AllowShortIfStatementsOnASingleLine: false |
|||
AllowShortLoopsOnASingleLine: false |
|||
BinPackArguments: false |
|||
BinPackParameters: false |
|||
BreakConstructorInitializers: AfterColon |
|||
AllowAllParametersOfDeclarationOnNextLine: false |
|||
ConstructorInitializerAllOnOneLineOrOnePerLine: true |
|||
ExperimentalAutoDetectBinPacking: false |
|||
KeepEmptyLinesAtTheStartOfBlocks: false |
|||
DerivePointerAlignment: false |
@ -1,31 +1,33 @@ |
|||
#ifndef _AsyncJsonCallbackResponse_H_ |
|||
#define _AsyncJsonCallbackResponse_H_ |
|||
|
|||
#include <ESPAsyncWebServer.h> |
|||
#include <AsyncJson.h> |
|||
#include <ESPAsyncWebServer.h> |
|||
|
|||
/* |
|||
* Listens for a response being destroyed and calls a callback during said distruction. |
|||
* used so we can take action after the response has been rendered to the client. |
|||
* |
|||
* Avoids having to fork ESPAsyncWebServer with a callback feature, but not nice! |
|||
*/ |
|||
* Listens for a response being destroyed and calls a callback during said distruction. |
|||
* used so we can take action after the response has been rendered to the client. |
|||
* |
|||
* Avoids having to fork ESPAsyncWebServer with a callback feature, but not nice! |
|||
*/ |
|||
|
|||
typedef std::function<void()> AsyncJsonCallback; |
|||
|
|||
class AsyncJsonCallbackResponse : public AsyncJsonResponse |
|||
{ |
|||
class AsyncJsonCallbackResponse : public AsyncJsonResponse { |
|||
private: |
|||
AsyncJsonCallback _callback; |
|||
|
|||
private: |
|||
AsyncJsonCallback _callback; |
|||
public: |
|||
AsyncJsonCallbackResponse(AsyncJsonCallback callback, |
|||
bool isArray = false, |
|||
size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE) : |
|||
AsyncJsonResponse(isArray, maxJsonBufferSize), |
|||
_callback{callback} { |
|||
} |
|||
|
|||
public: |
|||
AsyncJsonCallbackResponse(AsyncJsonCallback callback, bool isArray = false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE) |
|||
: AsyncJsonResponse(isArray, maxJsonBufferSize), _callback{callback} {} |
|||
~AsyncJsonCallbackResponse() |
|||
{ |
|||
_callback(); |
|||
} |
|||
~AsyncJsonCallbackResponse() { |
|||
_callback(); |
|||
} |
|||
}; |
|||
|
|||
#endif // end _AsyncJsonCallbackResponse_H_ |
|||
#endif // end _AsyncJsonCallbackResponse_H_ |
@ -1,120 +1,131 @@ |
|||
#ifndef Async_Json_Request_Web_Handler_H_ |
|||
#define Async_Json_Request_Web_Handler_H_ |
|||
|
|||
#include <ESPAsyncWebServer.h> |
|||
#include <ArduinoJson.h> |
|||
#include <ESPAsyncWebServer.h> |
|||
|
|||
#define ASYNC_JSON_REQUEST_DEFAULT_MAX_SIZE 1024 |
|||
#define ASYNC_JSON_REQUEST_MIMETYPE "application/json" |
|||
|
|||
/* |
|||
* Handy little utility for dealing with small JSON request body payloads. |
|||
* |
|||
* Need to be careful using this as we are somewhat limited by RAM. |
|||
* |
|||
* Really only of use where there is a determinate payload size. |
|||
*/ |
|||
|
|||
typedef std::function<void(AsyncWebServerRequest *request, JsonDocument &jsonDocument)> JsonRequestCallback; |
|||
|
|||
class AsyncJsonWebHandler: public AsyncWebHandler { |
|||
|
|||
private: |
|||
WebRequestMethodComposite _method; |
|||
JsonRequestCallback _onRequest; |
|||
size_t _maxContentLength; |
|||
|
|||
protected: |
|||
String _uri; |
|||
|
|||
public: |
|||
|
|||
AsyncJsonWebHandler() : |
|||
_method(HTTP_POST|HTTP_PUT|HTTP_PATCH), |
|||
* Handy little utility for dealing with small JSON request body payloads. |
|||
* |
|||
* Need to be careful using this as we are somewhat limited by RAM. |
|||
* |
|||
* Really only of use where there is a determinate payload size. |
|||
*/ |
|||
|
|||
typedef std::function<void(AsyncWebServerRequest* request, JsonDocument& jsonDocument)> JsonRequestCallback; |
|||
|
|||
class AsyncJsonWebHandler : public AsyncWebHandler { |
|||
private: |
|||
WebRequestMethodComposite _method; |
|||
JsonRequestCallback _onRequest; |
|||
size_t _maxContentLength; |
|||
|
|||
protected: |
|||
String _uri; |
|||
|
|||
public: |
|||
AsyncJsonWebHandler() : |
|||
_method(HTTP_POST | HTTP_PUT | HTTP_PATCH), |
|||
_onRequest(nullptr), |
|||
_maxContentLength(ASYNC_JSON_REQUEST_DEFAULT_MAX_SIZE), |
|||
_uri() {} |
|||
|
|||
~AsyncJsonWebHandler() {} |
|||
|
|||
void setUri(const String& uri) { _uri = uri; } |
|||
void setMethod(WebRequestMethodComposite method) { _method = method; } |
|||
void setMaxContentLength(size_t maxContentLength) { _maxContentLength = maxContentLength; } |
|||
void onRequest(JsonRequestCallback fn) { _onRequest = fn; } |
|||
|
|||
virtual bool canHandle(AsyncWebServerRequest *request) override final { |
|||
if(!_onRequest) |
|||
return false; |
|||
|
|||
if(!(_method & request->method())) |
|||
return false; |
|||
|
|||
if(_uri.length() && (_uri != request->url() && !request->url().startsWith(_uri+"/"))) |
|||
return false; |
|||
|
|||
if (!request->contentType().equalsIgnoreCase(ASYNC_JSON_REQUEST_MIMETYPE)) |
|||
return false; |
|||
_uri() { |
|||
} |
|||
|
|||
~AsyncJsonWebHandler() { |
|||
} |
|||
|
|||
void setUri(const String& uri) { |
|||
_uri = uri; |
|||
} |
|||
void setMethod(WebRequestMethodComposite method) { |
|||
_method = method; |
|||
} |
|||
void setMaxContentLength(size_t maxContentLength) { |
|||
_maxContentLength = maxContentLength; |
|||
} |
|||
void onRequest(JsonRequestCallback fn) { |
|||
_onRequest = fn; |
|||
} |
|||
|
|||
virtual bool canHandle(AsyncWebServerRequest* request) override final { |
|||
if (!_onRequest) |
|||
return false; |
|||
|
|||
if (!(_method & request->method())) |
|||
return false; |
|||
|
|||
if (_uri.length() && (_uri != request->url() && !request->url().startsWith(_uri + "/"))) |
|||
return false; |
|||
|
|||
if (!request->contentType().equalsIgnoreCase(ASYNC_JSON_REQUEST_MIMETYPE)) |
|||
return false; |
|||
|
|||
request->addInterestingHeader("ANY"); |
|||
return true; |
|||
} |
|||
|
|||
virtual void handleRequest(AsyncWebServerRequest* request) override final { |
|||
// no request configured |
|||
if (!_onRequest) { |
|||
Serial.print("No request callback was configured for endpoint: "); |
|||
Serial.println(_uri); |
|||
request->send(500); |
|||
return; |
|||
} |
|||
|
|||
request->addInterestingHeader("ANY"); |
|||
return true; |
|||
// we have been handed too much data, return a 413 (payload too large) |
|||
if (request->contentLength() > _maxContentLength) { |
|||
request->send(413); |
|||
return; |
|||
} |
|||
|
|||
virtual void handleRequest(AsyncWebServerRequest *request) override final { |
|||
// no request configured |
|||
if(!_onRequest) { |
|||
Serial.print("No request callback was configured for endpoint: "); |
|||
Serial.println(_uri); |
|||
request->send(500); |
|||
return; |
|||
// parse JSON and if possible handle the request |
|||
if (request->_tempObject) { |
|||
DynamicJsonDocument jsonDocument(_maxContentLength); |
|||
DeserializationError error = deserializeJson(jsonDocument, (uint8_t*)request->_tempObject); |
|||
if (error == DeserializationError::Ok) { |
|||
_onRequest(request, jsonDocument); |
|||
} else { |
|||
request->send(400); |
|||
} |
|||
return; |
|||
} |
|||
|
|||
// we have been handed too much data, return a 413 (payload too large) |
|||
if (request->contentLength() > _maxContentLength) { |
|||
request->send(413); |
|||
// fallthrough, we have a null pointer, return 500. |
|||
// this can be due to running out of memory or never receiving body data. |
|||
request->send(500); |
|||
} |
|||
|
|||
virtual void handleBody(AsyncWebServerRequest* request, |
|||
uint8_t* data, |
|||
size_t len, |
|||
size_t index, |
|||
size_t total) override final { |
|||
if (_onRequest) { |
|||
// don't allocate if data is too large |
|||
if (total > _maxContentLength) { |
|||
return; |
|||
} |
|||
|
|||
// parse JSON and if possible handle the request |
|||
if (request->_tempObject) { |
|||
DynamicJsonDocument jsonDocument(_maxContentLength); |
|||
DeserializationError error = deserializeJson(jsonDocument, (uint8_t *) request->_tempObject); |
|||
if (error == DeserializationError::Ok) { |
|||
_onRequest(request, jsonDocument); |
|||
}else{ |
|||
request->send(400); |
|||
} |
|||
return; |
|||
// try to allocate memory on first call |
|||
// NB: the memory allocated here is freed by ~AsyncWebServerRequest |
|||
if (index == 0 && !request->_tempObject) { |
|||
request->_tempObject = malloc(total); |
|||
} |
|||
|
|||
// fallthrough, we have a null pointer, return 500. |
|||
// this can be due to running out of memory or never receiving body data. |
|||
request->send(500); |
|||
} |
|||
|
|||
virtual void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) override final { |
|||
if (_onRequest) { |
|||
// don't allocate if data is too large |
|||
if (total > _maxContentLength){ |
|||
return; |
|||
} |
|||
|
|||
// try to allocate memory on first call |
|||
// NB: the memory allocated here is freed by ~AsyncWebServerRequest |
|||
if(index == 0 && !request->_tempObject){ |
|||
request->_tempObject = malloc(total); |
|||
} |
|||
|
|||
// copy the data into the buffer, if we have a buffer! |
|||
if (request->_tempObject) { |
|||
memcpy((uint8_t *) request->_tempObject+index, data, len); |
|||
} |
|||
// copy the data into the buffer, if we have a buffer! |
|||
if (request->_tempObject) { |
|||
memcpy((uint8_t*)request->_tempObject + index, data, len); |
|||
} |
|||
} |
|||
} |
|||
|
|||
virtual bool isRequestHandlerTrivial() override final { |
|||
return _onRequest ? false : true; |
|||
} |
|||
|
|||
virtual bool isRequestHandlerTrivial() override final { |
|||
return _onRequest ? false : true; |
|||
} |
|||
}; |
|||
|
|||
#endif // end Async_Json_Request_Web_Handler_H_ |
|||
#endif // end Async_Json_Request_Web_Handler_H_ |
@ -1,32 +1,28 @@ |
|||
#ifndef AuthenticationService_H_ |
|||
#define AuthenticationService_H_ |
|||
|
|||
#include <SecurityManager.h> |
|||
#include <ESPAsyncWebServer.h> |
|||
#include <AsyncJsonWebHandler.h> |
|||
#include <AsyncJson.h> |
|||
#include <AsyncJsonWebHandler.h> |
|||
#include <ESPAsyncWebServer.h> |
|||
#include <SecurityManager.h> |
|||
|
|||
#define VERIFY_AUTHORIZATION_PATH "/rest/verifyAuthorization" |
|||
#define SIGN_IN_PATH "/rest/signIn" |
|||
|
|||
#define MAX_AUTHENTICATION_SIZE 256 |
|||
|
|||
class AuthenticationService { |
|||
|
|||
public: |
|||
|
|||
AuthenticationService(AsyncWebServer* server, SecurityManager* securityManager); |
|||
~AuthenticationService(); |
|||
|
|||
private: |
|||
|
|||
SecurityManager* _securityManager; |
|||
AsyncJsonWebHandler _signInHandler; |
|||
class AuthenticationService { |
|||
public: |
|||
AuthenticationService(AsyncWebServer* server, SecurityManager* securityManager); |
|||
~AuthenticationService(); |
|||
|
|||
// endpoint functions |
|||
void signIn(AsyncWebServerRequest *request, JsonDocument &jsonDocument); |
|||
void verifyAuthorization(AsyncWebServerRequest *request); |
|||
private: |
|||
SecurityManager* _securityManager; |
|||
AsyncJsonWebHandler _signInHandler; |
|||
|
|||
// endpoint functions |
|||
void signIn(AsyncWebServerRequest* request, JsonDocument& jsonDocument); |
|||
void verifyAuthorization(AsyncWebServerRequest* request); |
|||
}; |
|||
|
|||
#endif // end SecurityManager_h |
|||
#endif // end SecurityManager_h |
Write
Preview
Loading…
Cancel
Save
Reference in new issue