REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
aWOT.h
Go to the documentation of this file.
1/*
2 aWOT, Express.js inspired microcontreller web framework for the Web of Things
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21*/
22
23#ifdef ARDUINO
24
25#ifndef AWOT_H_
26#define AWOT_H_
27
28#include <stdlib.h>
29#include <string.h>
30
31#include "Client.h"
32#include "QNEthernet.h"
33
34#if defined(STD_FUNCTION_MIDDLEWARE)
35#include <functional>
36#define MIDDLEWARE_PARAM Middleware
37#define MIDDLEWARE_FUNCTION std::function<void(Request & request, Response & response)> Middleware
38#else
39#define MIDDLEWARE_PARAM Middleware *
40#define MIDDLEWARE_FUNCTION void Middleware(Request &request, Response &response)
41#endif
42
43#define CRLF "\r\n"
44
45#if defined(__AVR_ATmega328P__) || defined(__AVR_Atmega32U4__) || defined(__AVR_ATmega16U4__) || \
46 defined(_AVR_ATmega328__)
47#define LOW_MEMORY_MCU
48#endif
49
50#ifndef SERVER_URL_BUFFER_SIZE
51#if defined(LOW_MEMORY_MCU)
52#define SERVER_URL_BUFFER_SIZE 64
53#else
54#define SERVER_URL_BUFFER_SIZE 256
55#endif
56#endif
57
58#ifndef SERVER_PUSHBACK_BUFFER_SIZE
59#if defined(LOW_MEMORY_MCU)
60#define SERVER_PUSHBACK_BUFFER_SIZE 32
61#else
62#define SERVER_PUSHBACK_BUFFER_SIZE 128
63#endif
64#endif
65
66#ifndef SERVER_OUTPUT_BUFFER_SIZE
67#if defined(LOW_MEMORY_MCU)
68#define SERVER_OUTPUT_BUFFER_SIZE 32
69#else
70#define SERVER_OUTPUT_BUFFER_SIZE 1024
71#endif
72#endif
73
74#ifndef SERVER_MAX_HEADERS
75#define SERVER_MAX_HEADERS 10
76#endif
77
78#ifdef __AVR__
79#define P(name) static const unsigned char name[] __attribute__((section(".progmem." #name)))
80#else
81#define P(name) static const unsigned char name[] PROGMEM
82#endif
83
84namespace awot {
85
86using qindesign::network::EthernetClient;
87
88class StreamClient : public Client {
89private:
90 Stream *s;
91
92public:
93 StreamClient(Stream *stream) : s(stream){};
94
95 int connect(IPAddress, uint16_t) { return 1; };
96
97 int connect(const char *, uint16_t) { return 1; };
98
99 size_t write(uint8_t byte) { return s->write(byte); };
100
101 size_t write(const uint8_t *buffer, size_t length) { return s->write(buffer, length); };
102
103 int available() { return s->available(); };
104
105 int read() { return s->read(); };
106
107 int read(uint8_t *buffer, size_t length) {
108 size_t count = 0;
109
110 while (count < length) {
111 int c = read();
112 if (c < 0) {
113 break;
114 }
115
116 *buffer++ = (uint8_t)c;
117 count++;
118 }
119
120 return count;
121 }
122
123 int peek() { return s->peek(); };
124
125 void flush() { return s->flush(); };
126
127 void stop(){};
128
129 uint8_t connected() { return 1; };
130
131 operator bool() { return true; };
132};
133
134class Response : public Print {
135 friend class Application;
136 friend class Router;
137
138public:
139 int availableForWrite();
140 int bytesSent();
141 void beginHeaders();
142 void end();
143 void endHeaders();
144 bool ended();
145 void flush();
146 const char *get(const char *name);
147 bool headersSent();
148 void printP(const unsigned char *string);
149 void printP(const char *string);
150 void sendStatus(int code);
151 void set(const char *name, const char *value);
152 void setDefaults();
153 void status(int code);
154 int statusSent();
155 size_t write(uint8_t data);
156 size_t write(uint8_t *buffer, size_t bufferLength);
157 void writeP(const unsigned char *data, size_t length);
158
159private:
160 Response(EthernetClient *client, uint8_t *writeBuffer, int writeBufferLength);
161
162 void m_printStatus(int code);
163 bool m_shouldPrintHeaders();
164 void m_printHeaders();
165 void m_printCRLF();
166 void m_flushBuf();
167 void m_finalize();
168
169 EthernetClient *m_stream;
170
171 struct Headers {
172 const char *name;
173 const char *value;
174 } m_headers[SERVER_MAX_HEADERS];
175
176 bool m_contentLenghtSet;
177 bool m_contentTypeSet;
178 bool m_keepAlive;
179 int m_statusSent;
180 bool m_headersSent;
181 bool m_sendingStatus;
182 bool m_sendingHeaders;
183 int m_headersCount;
184 int m_bytesSent;
185 bool m_ended;
186 uint8_t *m_buffer;
187 int m_bufferLength;
188 int m_bufFill;
189};
190
191class Request : public Stream {
192 friend class Application;
193 friend class Router;
194
195public:
197
198 void *context;
199
200 int available();
201 int availableForWrite();
202 int bytesRead();
203 Stream *stream();
204 void flush();
205 bool form(char *name, int nameLength, char *value, int valueLength);
206 char *get(const char *name);
207 int left();
209 char *path();
210 int peek();
211 void push(uint8_t ch);
212 char *query();
213 bool query(const char *name, char *buffer, int bufferLength);
214 int read();
215 int read(uint8_t *buf, size_t size);
216 bool route(const char *name, char *buffer, int bufferLength);
217 bool route(int number, char *buffer, int bufferLength);
218 int minorVersion();
219 size_t write(uint8_t data);
220 size_t write(uint8_t *buffer, size_t bufferLength);
221
222private:
223 struct HeaderNode {
224 const char *name;
225 char *buffer;
226 int bufferLength;
227 HeaderNode *next;
228 };
229
230 Request(EthernetClient *client, Response *m_response, HeaderNode *headerTail, char *urlBuffer,
231 int urlBufferLength, unsigned long timeout, void *context);
232 bool m_processMethod();
233 bool m_readURL();
234 bool m_readVersion();
235 void m_processURL();
236 bool m_processHeaders();
237 bool m_headerValue(char *buffer, int bufferLength);
238 bool m_readInt(int &number);
239 void m_setRoute(const char *route, const char *pattern);
240 int m_getUrlPathLength();
241 bool m_expect(const char *expected);
242 bool m_expectP(const unsigned char *expected);
243 bool m_skipSpace();
244 void m_reset();
245 int m_timedRead();
246 bool m_timedout();
247
248 EthernetClient *m_stream;
249 Response *m_response;
250 MethodType m_method;
251 int m_minorVersion;
252 unsigned char m_pushback[SERVER_PUSHBACK_BUFFER_SIZE];
253 int m_pushbackDepth;
254 bool m_readingContent;
255 int m_left;
256 int m_bytesRead;
257 HeaderNode *m_headerTail;
258 char *m_query;
259 int m_queryLength;
260 bool m_readTimedout;
261 char *m_path;
262 int m_pathLength;
263 const char *m_pattern;
264 const char *m_route;
265};
266
267class Router {
268 friend class Application;
269
270public:
272
273 Router();
274 ~Router();
275
276 void del(const char *path, MIDDLEWARE_PARAM middleware);
277 void del(MIDDLEWARE_PARAM middleware);
278 void get(const char *path, MIDDLEWARE_PARAM middleware);
279 void get(MIDDLEWARE_PARAM middleware);
280 void head(const char *path, MIDDLEWARE_PARAM middleware);
281 void head(MIDDLEWARE_PARAM middleware);
282 void options(const char *path, MIDDLEWARE_PARAM middleware);
283 void options(MIDDLEWARE_PARAM middleware);
284 void patch(const char *path, MIDDLEWARE_PARAM middleware);
285 void patch(MIDDLEWARE_PARAM middleware);
286 void post(const char *path, MIDDLEWARE_PARAM middleware);
287 void post(MIDDLEWARE_PARAM middleware);
288 void put(const char *path, MIDDLEWARE_PARAM middleware);
289 void put(MIDDLEWARE_PARAM middleware);
290 void use(const char *path, Router *router);
291 void use(Router *router);
292 void use(const char *path, MIDDLEWARE_PARAM middleware);
293 void use(MIDDLEWARE_PARAM middleware);
294
295private:
296 struct MiddlewareNode {
297 const char *path;
298 MIDDLEWARE_PARAM middleware;
299 Router *router;
301 MiddlewareNode *next;
302 };
303
304 void m_addMiddleware(Request::MethodType type, const char *path, MIDDLEWARE_PARAM middleware);
305 void m_mountMiddleware(MiddlewareNode *tail);
306 void m_setNext(Router *next);
307 Router *m_getNext();
308 void m_dispatchMiddleware(Request &request, Response &response, int urlShift = 0);
309 bool m_routeMatch(const char *route, const char *pattern);
310
311 MiddlewareNode *m_head;
312};
313
315public:
316 Application();
317 ~Application();
318
319 static int strcmpi(const char *s1, const char *s2);
320 static int strcmpiP(const char *s1, const unsigned char *s2);
321
322 void del(const char *path, Router::MIDDLEWARE_PARAM middleware);
323 void del(Router::MIDDLEWARE_PARAM middleware);
324 void finally(Router::MIDDLEWARE_PARAM middleware);
325 void get(const char *path, Router::MIDDLEWARE_PARAM middleware);
326 void get(Router::MIDDLEWARE_PARAM middleware);
327 void head(const char *path, Router::MIDDLEWARE_PARAM middleware);
328 void head(Router::MIDDLEWARE_PARAM middleware);
329 void header(const char *name, char *buffer, int bufferLength);
330 void notFound(Router::MIDDLEWARE_PARAM middleware);
331 void options(const char *path, Router::MIDDLEWARE_PARAM middleware);
332 void options(Router::MIDDLEWARE_PARAM middleware);
333 void patch(const char *path, Router::MIDDLEWARE_PARAM middleware);
334 void patch(Router::MIDDLEWARE_PARAM middleware);
335 void post(const char *path, Router::MIDDLEWARE_PARAM middleware);
336 void post(Router::MIDDLEWARE_PARAM middleware);
337 void put(const char *path, Router::MIDDLEWARE_PARAM middleware);
338 void put(Router::MIDDLEWARE_PARAM middleware);
339 void process(EthernetClient *client, void *context = NULL);
340 void process(EthernetClient *client, char *urlbuffer, int urlBufferLength, void *context = NULL);
341 void process(EthernetClient *client, char *urlBuffer, int urlBufferLength, uint8_t *writeBuffer,
342 int writeBufferLength, void *context = NULL);
343 void process(Stream *stream, void *context = NULL);
344 void process(Stream *stream, char *urlbuffer, int urlBufferLength, void *context = NULL);
345 void process(Stream *stream, char *urlBuffer, int urlBufferLength, uint8_t *writeBuffer,
346 int writeBufferLength, void *context = NULL);
347
348 void setTimeout(unsigned long timeoutMillis);
349 void use(const char *path, Router *router);
350 void use(Router *router);
351 void use(const char *path, Router::MIDDLEWARE_PARAM middleware);
352 void use(Router::MIDDLEWARE_PARAM middleware);
353
354private:
355 void m_process(Request &req, Response &res);
356
357 Router::MIDDLEWARE_PARAM m_final;
358 Router::MIDDLEWARE_PARAM m_notFound;
359 Router m_defaultRouter;
360 Request::HeaderNode *m_headerTail;
361 unsigned long m_timeout;
362};
363
364} // namespace awot
365
366#ifndef ENABLE_AWOT_NAMESPACE
367using namespace awot;
368#endif
369
370#endif
371
372#endif // ARDUINO
#define MIDDLEWARE_FUNCTION
Definition aWOT.h:40
#define SERVER_MAX_HEADERS
Definition aWOT.h:75
#define SERVER_PUSHBACK_BUFFER_SIZE
Definition aWOT.h:62
#define MIDDLEWARE_PARAM
Definition aWOT.h:39
void options(const char *path, Router::Middleware *middleware)
Definition aWOT.cpp:1470
void head(const char *path, Router::Middleware *middleware)
Definition aWOT.cpp:1462
static int strcmpiP(const char *s1, const unsigned char *s2)
Definition aWOT.cpp:1413
void post(const char *path, Router::Middleware *middleware)
Definition aWOT.cpp:1482
void use(const char *path, Router *router)
Definition aWOT.cpp:1572
void setTimeout(unsigned long timeoutMillis)
Definition aWOT.cpp:1570
void patch(const char *path, Router::Middleware *middleware)
Definition aWOT.cpp:1476
void process(EthernetClient *client, void *context=NULL)
Definition aWOT.cpp:1494
void put(const char *path, Router::Middleware *middleware)
Definition aWOT.cpp:1488
static int strcmpi(const char *s1, const char *s2)
Definition aWOT.cpp:1391
void del(const char *path, Router::Middleware *middleware)
Definition aWOT.cpp:1448
void header(const char *name, char *buffer, int bufferLength)
Definition aWOT.cpp:1627
void notFound(Router::Middleware *middleware)
Definition aWOT.cpp:1468
void get(const char *path, Router::Middleware *middleware)
Definition aWOT.cpp:1456
int peek()
Definition aWOT.cpp:778
size_t write(uint8_t data)
Definition aWOT.cpp:921
void push(uint8_t ch)
Definition aWOT.cpp:788
Stream * stream()
Definition aWOT.cpp:701
void * context
Definition aWOT.h:198
char * query()
Definition aWOT.cpp:797
char * path()
Definition aWOT.cpp:776
int bytesRead()
Definition aWOT.cpp:699
int available()
Definition aWOT.cpp:697
bool route(const char *name, char *buffer, int bufferLength)
Definition aWOT.cpp:871
bool form(char *name, int nameLength, char *value, int valueLength)
Definition aWOT.cpp:719
MethodType method()
Definition aWOT.cpp:774
void flush()
Definition aWOT.cpp:717
int minorVersion()
Definition aWOT.cpp:919
int read()
Definition aWOT.cpp:823
int availableForWrite()
Definition aWOT.cpp:695
char * get(const char *name)
Definition aWOT.cpp:703
int left()
Definition aWOT.cpp:772
bool ended()
Definition aWOT.cpp:63
void flush()
Definition aWOT.cpp:65
const char * get(const char *name)
Definition aWOT.cpp:71
int bytesSent()
Definition aWOT.cpp:52
void setDefaults()
Definition aWOT.cpp:131
int statusSent()
Definition aWOT.cpp:175
int availableForWrite()
Definition aWOT.cpp:34
size_t write(uint8_t data)
Definition aWOT.cpp:177
void writeP(const unsigned char *data, size_t length)
Definition aWOT.cpp:226
void set(const char *name, const char *value)
Definition aWOT.cpp:105
void printP(const unsigned char *string)
Definition aWOT.cpp:83
void endHeaders()
Definition aWOT.cpp:56
bool headersSent()
Definition aWOT.cpp:81
void sendStatus(int code)
Definition aWOT.cpp:95
void end()
Definition aWOT.cpp:54
void beginHeaders()
Definition aWOT.cpp:36
void use(const char *path, Router *router)
Definition aWOT.cpp:1291
void head(const char *path, Middleware *middleware)
Definition aWOT.cpp:1255
void options(const char *path, Middleware *middleware)
Definition aWOT.cpp:1261
void patch(const char *path, Middleware *middleware)
Definition aWOT.cpp:1279
void del(const char *path, Middleware *middleware)
Definition aWOT.cpp:1243
void get(const char *path, Middleware *middleware)
Definition aWOT.cpp:1249
void put(const char *path, Middleware *middleware)
Definition aWOT.cpp:1273
void post(const char *path, Middleware *middleware)
Definition aWOT.cpp:1267
int available()
Definition aWOT.h:103
int connect(IPAddress, uint16_t)
Definition aWOT.h:95
int read(uint8_t *buffer, size_t length)
Definition aWOT.h:107
StreamClient(Stream *stream)
Definition aWOT.h:93
size_t write(uint8_t byte)
Definition aWOT.h:99
int connect(const char *, uint16_t)
Definition aWOT.h:97
uint8_t connected()
Definition aWOT.h:129
size_t write(const uint8_t *buffer, size_t length)
Definition aWOT.h:101
utils::status status
Definition daq.h:28
uint32_t uint32_t size
Definition flasher.cpp:63
Definition aWOT.h:84