-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathESP8266Server.c
More file actions
411 lines (342 loc) · 18.7 KB
/
Copy pathESP8266Server.c
File metadata and controls
411 lines (342 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "ESP8266Server.h"
#define COMMAND_MAX_LENGTH 100
#define TMP_TX_BUFFER_MAX_LENGTH 100
#define DATA_RECEIVED_STATUS "+IPD,"
#define SEND_OK_STATUS "\r\nSEND OK\r\n"
#define SEND_FAIL_STATUS "\r\nSEND FAIL\r\n"
#define OK_STATUS "\r\nOK\r\n"
#define CLOSED_STATUS "CLOSED\r\n"
#define ERROR_STATUS "\r\nERROR\r\n"
#define FAIL_STATUS "\r\nFAIL\r\n"
#define NEW_LINE "\r\n"
#define READY_TO_RECEIVE_DATA ">"
#define ESP8266_ADDITIONAL_HEADER_LENGTH 100
#define ESP8266_STATION_AND_AP 3
#define ESP8266_SHOW_REQUEST_IP_AND_PORT 1
#define ESP8266_DISABLE_AUTO_CONNECT_TO_AP 0
#define ESP8266_CONNECTION_MULTIPLE 1
#define ESP8266_MAX_ALLOWED_TIMEOUT 7200
#define ESP8266_CREATE_SERVER_MODE 1
#define ESP8266_DELETE_SERVER_MODE 0
#define ESP8266_MAX_SSID_LENGTH 32
#define ESP8266_MAX_PASSWORD_LENGTH 64
#define ESP8266_DATA_MARKER_MAX_LENGTH 100
#define ESP8266_REQUEST_END_MARKER_LENGTH 4
#define ESP8266_IPD_MARKER_REQUEST_ID_INDEX 5
#define ESP8266_ALL_CONNECTIONS_ID 5
static USART *USARTInstance = NULL;
static HTTPParser *httpParser = NULL;
static StringRingBuffer *tmpTxBuffer = NULL;
static char commandBuffer[COMMAND_MAX_LENGTH];
static ESP8266ServerStatus sendATCommand(ServerContext *context, const char *ATCommandPattern, ...);
static ESP8266ServerStatus readCommandResponse(ServerContext *context, char *rxBufferPointer);
static inline bool isResponseOK(char *responseBody);
static inline bool isResponseError(char *responseBody);
static inline bool isSsidValid(char *ssid);
static inline bool isPasswordValid(char *password);
static void getIPDMarkerValue(char *rawRequest, char *valueBuffer);
static IPAddress parseRequestIPAddress(char *requestPointer);
static ESP8266ServerStatus sendSingleResponse(ServerContext *context, HashMap headers, const char *body, uint32_t bodyLength, char *commandResponsePointer);
static ESP8266ServerStatus sendChunkedResponse(ServerContext *context, HashMap headers, const char *body, uint32_t bodyLength, char *commandResponsePointer);
static ESP8266ServerStatus sendHTTPResponseESP8266(ServerContext *context, uint32_t dataLength, char *commandResponsePointer);
static void closeConnectionESP8266(ServerContext *context, uint32_t connectionId, char *commandResponsePointer);
ServerContext *initServerESP8266(USART_TypeDef *USARTx, ServerConfiguration *configuration) {
ServerContext *context = initHTTPServerContext(configuration);
if (context == NULL) return NULL;
tmpTxBuffer = getStringRingBufferInstance(TMP_TX_BUFFER_MAX_LENGTH);
USARTInstance = initBufferedUSART(USARTx, configuration->rxDataBufferSize, ESP8266_INNER_TX_BUFFER_SIZE);
httpParser = getHttpParserInstance();
if (tmpTxBuffer == NULL || USARTInstance == NULL || httpParser == NULL) {
deleteServerESP8266(context);
return NULL;
}
context->txDataBufferPointer = USARTInstance->TxBuffer->dataBuffer;
dwtDelayInit();
delay_ms(100); // initial delay
sendATCommand(context, "AT+RST");
delay_ms(5000);
if (sendATCommand(context, "AT") != ESP8266_SERVER_SUCCESS) {
deleteServerESP8266(context);
return NULL;
}
sendATCommand(context, "AT+GMR");
return context;
}
ServerIPConfig startServerESP8266(ServerContext *context, char *ssid, char *password) {
ServerIPConfig serverConfig = {0};
if (!isSsidValid(ssid) || !isPasswordValid(password)) return serverConfig;
sendATCommand(context, "AT+CWMODE_DEF=%d", ESP8266_STATION_AND_AP);
sendATCommand(context, "AT+CWAUTOCONN=%d", ESP8266_DISABLE_AUTO_CONNECT_TO_AP);
sendATCommand(context, "AT+CIPDINFO=%d", ESP8266_SHOW_REQUEST_IP_AND_PORT); // show ip with +IPD
sendATCommand(context, "AT+CWJAP_CUR=\"%s\",\"%s\"", ssid, password);
if (sendATCommand(context, "AT+CIFSR") == ESP8266_SERVER_SUCCESS) {
char *responseBody = USARTInstance->RxBuffer->dataPointer;
char dataBuffer[20] = {[0 ... 20 - 1] = 0};
substringString("STAIP,\"", "\"", responseBody, dataBuffer);
serverConfig.localIP = ipAddressFromString(dataBuffer);
memset(dataBuffer, 0, 20);
substringString("STAMAC,\"", "\"", responseBody, dataBuffer);
serverConfig.localMAC = macAddressFromString(dataBuffer);
sendATCommand(context, "AT+CIPMUX=%d", ESP8266_CONNECTION_MULTIPLE);
sendATCommand(context, "AT+CIPSERVER=%d,%d", ESP8266_CREATE_SERVER_MODE, context->configuration->serverPort);
if (context->configuration->serverTimeoutMs <= ESP8266_MAX_ALLOWED_TIMEOUT) {
sendATCommand(context, "AT+CIPSTO=%d", context->configuration->serverTimeoutMs);
}
context->isServerRunning = true;
}
clearStringRingBuffer(USARTInstance->RxBuffer, USARTInstance->RxBuffer->maxSize);
clearStringRingBuffer(USARTInstance->TxBuffer, USARTInstance->TxBuffer->maxSize);
return serverConfig;
}
ESP8266ServerStatus startSoftApESP8266(ServerContext *context, char *ssid, char *password, uint16_t channelId, uint8_t encryption) {
return sendATCommand(context, "AT+CWSAP_DEF=\"%s\",\"%s\",%d,%d", ssid, password, channelId, encryption);
}
ESP8266ServerStatus startMulticastDnsESP8266(ServerContext *context, char *host, char *serverName, uint16_t port) {
return sendATCommand(context, "AT+MDNS=1,\"%s\",\"%s\",%d", host, serverName, port);
}
void processServerRequestsESP8266(ServerContext *context) {
if (isStringRingBufferFull(USARTInstance->RxBuffer)) {
LL_USART_DisableIT_RXNE(USARTInstance->USARTx);
resetRxBufferUSART(USARTInstance);
LL_USART_EnableIT_RXNE(USARTInstance->USARTx);
}
char *requestBody = USARTInstance->RxBuffer->dataPointer;
if (requestBody[0] != '\0') { // check that rx buffer is not empty
char *requestStartPointer = strstr(requestBody, "+IPD,");
if (requestStartPointer == NULL) return;
char *requestEndPointer = strstr(requestStartPointer, "\r\n\r\n");
if (requestEndPointer == NULL) return;
static char ipdMarker[ESP8266_DATA_MARKER_MAX_LENGTH];
memset(ipdMarker, 0, ESP8266_DATA_MARKER_MAX_LENGTH);
getIPDMarkerValue(requestStartPointer, ipdMarker); // ESP8266 Data received +IPD marker
if (isStringEmpty(ipdMarker)) return;
requestStartPointer += strlen(ipdMarker);
context->socketId = ipdMarker[ESP8266_IPD_MARKER_REQUEST_ID_INDEX] - '0'; // convert char to id. Example +IPD,0,... -> id is at index 5
context->requestIP = parseRequestIPAddress(ipdMarker);
uint32_t requestLength = (requestEndPointer - requestBody) + ESP8266_REQUEST_END_MARKER_LENGTH;
USARTInstance->RxBuffer->dataPointer += requestLength; // set to the next request
parseHttpBuffer(requestStartPointer, httpParser, HTTP_REQUEST);
if (httpParser->parserStatus == HTTP_PARSE_OK) {
parseHttpHeaders(httpParser, requestStartPointer);
parseHttpQueryParameters(httpParser, requestStartPointer);
RequestHandlerFunction handlerFunction = handleIncomingServerRequest(context, httpParser);
handlerFunction(context, httpParser);
}
bool havePendingRequests = strstr(USARTInstance->RxBuffer->dataPointer, "+IPD,") != NULL; // check for pending requests
if (!havePendingRequests || httpParser->parserStatus != HTTP_PARSE_OK) { // shrink rx buffer if no new requests arrived
LL_USART_DisableIT_RXNE(USARTInstance->USARTx);
clearStringRingBuffer(USARTInstance->RxBuffer, USARTInstance->RxBuffer->head);
LL_USART_EnableIT_RXNE(USARTInstance->USARTx);
}
}
}
void sendServerResponseESP8266(ServerContext *context, HTTPStatus status, HashMap headers, const char *body) {
if (headers == NULL) return;
hashMapPut(headers, "Server", SERVER_NAME);
hashMapPut(headers, "Cache-Control", "no-cache");
hashMapPut(headers, "Pragma", "no-cache");
hashMapPut(headers, "Accept-Ranges", "bytes");
uint32_t statusLineLength = formatHTTPServerStatusLine(context->txDataBufferPointer, status);
uint32_t headersLength = getHTTPServerHeadersLength(headers);
uint32_t bodyLength = isStringNotBlank(body) ? strlen(body) : 0;
uint32_t totalResponseLength = statusLineLength + headersLength + bodyLength;
uint32_t bytesInRxBuffer = (USARTInstance->RxBuffer->dataPointer - USARTInstance->RxBuffer->dataBuffer);
char *commandResponsePointer = USARTInstance->RxBuffer->dataPointer + (USARTInstance->RxBuffer->head - bytesInRxBuffer);
if (totalResponseLength <= (ESP8266_INNER_TX_BUFFER_SIZE - ESP8266_ADDITIONAL_HEADER_LENGTH)) {
ESP8266ServerStatus responseSendStatus = sendSingleResponse(context, headers, body, bodyLength, commandResponsePointer);
if (responseSendStatus != ESP8266_SERVER_SUCCESS) {
return;
}
} else if (totalResponseLength >= ESP8266_INNER_TX_BUFFER_SIZE) {
if (isStringNotEquals(httpParser->httpVersion, SERVER_HTTP_VERSION)) { //Not to send transfer-encoded messages to non-HTTP/1.1 applications.
formatHTTPServerStatusLine(context->txDataBufferPointer, HTTP_NOT_IMPLEMENTED);
sendSingleResponse(context, headers, NULL, 0, commandResponsePointer);
closeConnectionESP8266(context, context->socketId, commandResponsePointer);
return;
}
ESP8266ServerStatus responseSendStatus = sendChunkedResponse(context, headers, body, bodyLength, commandResponsePointer);
if (responseSendStatus != ESP8266_SERVER_SUCCESS) {
return;
}
}
char *connectionStatus = hashMapGet(headers, getHeaderValueByKey(CONNECTION));
if (isStringEquals(connectionStatus, "close")) {
closeConnectionESP8266(context, context->socketId, commandResponsePointer);
}
}
void deleteServerESP8266(ServerContext *context) {
deleteHTTPServer(context);
deleteUSART(USARTInstance);
stringRingBufferDelete(tmpTxBuffer);
deleteHttpParser(httpParser);
httpParser = NULL;
tmpTxBuffer = NULL;
}
static ESP8266ServerStatus sendATCommand(ServerContext *context, const char *ATCommandPattern, ...) {
resetTxBufferUSART(USARTInstance);
memset(commandBuffer, 0, COMMAND_MAX_LENGTH);
va_list valist;
va_start(valist, ATCommandPattern);
vsprintf(commandBuffer, ATCommandPattern, valist);
va_end(valist);
strcat(commandBuffer, NEW_LINE); // ESP8266 expects <CR><LF> or CarriageReturn and LineFeed at the end of each command
for (uint8_t i = 0; i < ESP8266_KEEPALIVE_ATTEMPT_COUNT; i++) {
clearStringRingBuffer(USARTInstance->RxBuffer, COMMAND_MAX_LENGTH);
sendStringUSART(USARTInstance, commandBuffer);
ESP8266ServerStatus status = readCommandResponse(context, USARTInstance->RxBuffer->dataPointer);
if (status != ESP8266_SERVER_TIMEOUT) {
return status;
}
}
return ESP8266_SERVER_TIMEOUT;
}
static ESP8266ServerStatus readCommandResponse(ServerContext *context, char *rxBufferPointer) {
uint32_t startTimeMillis = currentMilliSeconds();
ESP8266ServerStatus status = ESP8266_SERVER_TIMEOUT;
uint32_t byteCountInBuffer;
while (true) {
if ((currentMilliSeconds() - startTimeMillis) >= context->configuration->serverTimeoutMs) {
break;
}
byteCountInBuffer = getStringRingBufferSize(USARTInstance->RxBuffer);
if (isRxBufferNotEmptyUSART(USARTInstance)) {
delay_ms(1);
if (byteCountInBuffer == getStringRingBufferSize(USARTInstance->RxBuffer)) { // check that no new data is arrived
if (isResponseOK(rxBufferPointer)) {
return ESP8266_SERVER_SUCCESS;
} else if (isResponseError(rxBufferPointer)) {
return ESP8266_SERVER_ERROR;
} else if (isRxBufferFullUSART(USARTInstance)) {
return ESP8266_SERVER_ERROR_BUFFER_FULL;
}
}
}
delay_ms(1);
}
return status;
}
static inline bool isResponseOK(char *responseBody) {
return strstr(responseBody, OK_STATUS) ||
strstr(responseBody, CLOSED_STATUS) ||
strstr(responseBody, READY_TO_RECEIVE_DATA);
}
static inline bool isResponseError(char *responseBody) { // find for "error" or "fail" response status
return strstr(responseBody, ERROR_STATUS) ||
strstr(responseBody, FAIL_STATUS) ||
strstr(responseBody, SEND_FAIL_STATUS);
}
static inline bool isSsidValid(char *ssid) {
return (isStringNotBlank(ssid) && strlen(ssid) < ESP8266_MAX_SSID_LENGTH);
}
static inline bool isPasswordValid(char *password) {
return (isStringNotBlank(password) && strlen(password) < ESP8266_MAX_PASSWORD_LENGTH);
}
static void getIPDMarkerValue(char *rawRequest, char *valueBuffer) {
char *dataMarker = rawRequest;
uint8_t markerCounter = 0;
while (*dataMarker != '\0' && markerCounter < ESP8266_DATA_MARKER_MAX_LENGTH) {
if (*dataMarker == ':') {
valueBuffer[markerCounter] = *dataMarker;
break;
}
valueBuffer[markerCounter] = *dataMarker;
markerCounter++;
dataMarker++;
}
}
static IPAddress parseRequestIPAddress(char *requestPointer) {
static char ipAddressBuffer[50] = {0};
substringString(",", ":", requestPointer, ipAddressBuffer);
char *nextPointer;
char *token = splitStringReentrant(ipAddressBuffer, ",", &nextPointer);
for (int i = 0; i < 2; i++) {// skip request id and size
token = splitStringReentrant(NULL, ",", &nextPointer);
}
return ipAddressFromString(token);
}
static ESP8266ServerStatus sendSingleResponse(ServerContext *context, HashMap headers, const char *body, uint32_t bodyLength, char *commandResponsePointer) {
char dataLengthBuffer[sizeof(uint32_t) * 8 + 1] = {0}; // u32 max length
sprintf(dataLengthBuffer, "%lu", bodyLength);
hashMapPut(headers, "Content-Length", dataLengthBuffer);
formatHTTPServerHeaders(context->txDataBufferPointer, headers);
formatHTTPServerResponseBody(context, body);
uint32_t totalResponseLength = strlen(context->txDataBufferPointer);
USARTInstance->TxBuffer->tail = 0;
USARTInstance->TxBuffer->head = totalResponseLength;
ESP8266ServerStatus responseSendStatus = sendHTTPResponseESP8266(context, totalResponseLength, commandResponsePointer);
if (responseSendStatus != ESP8266_SERVER_SUCCESS) {
closeConnectionESP8266(context, ESP8266_ALL_CONNECTIONS_ID, commandResponsePointer);
return responseSendStatus;
}
return responseSendStatus;
}
static ESP8266ServerStatus sendChunkedResponse(ServerContext *context, HashMap headers, const char *body, uint32_t bodyLength, char *commandResponsePointer) {
hashMapRemove(headers, "Content-Length"); // removing content length header for chunked response if present
hashMapPut(headers, "Transfer-Encoding", "chunked");
formatHTTPServerHeaders(context->txDataBufferPointer, headers);
uint32_t totalResponseLength = strlen(context->txDataBufferPointer);
USARTInstance->TxBuffer->tail = 0;
USARTInstance->TxBuffer->head = totalResponseLength;
ESP8266ServerStatus responseSendStatus = sendHTTPResponseESP8266(context, totalResponseLength, commandResponsePointer);
if (responseSendStatus != ESP8266_SERVER_SUCCESS) {
closeConnectionESP8266(context, ESP8266_ALL_CONNECTIONS_ID, commandResponsePointer);
return responseSendStatus;
}
ChunkedResponse chunkedResponse = prepareChunkedResponse(ESP8266_INNER_TX_BUFFER_SIZE, body, bodyLength);
while (hasNextHTTPChunk(&chunkedResponse, context->txDataBufferPointer)) {
totalResponseLength = strlen(context->txDataBufferPointer);
USARTInstance->TxBuffer->tail = 0;
USARTInstance->TxBuffer->head = totalResponseLength;
responseSendStatus = sendHTTPResponseESP8266(context, totalResponseLength, commandResponsePointer);
if (responseSendStatus != ESP8266_SERVER_SUCCESS) {
closeConnectionESP8266(context, ESP8266_ALL_CONNECTIONS_ID, commandResponsePointer);
return responseSendStatus;
}
}
return responseSendStatus;
}
static ESP8266ServerStatus sendHTTPResponseESP8266(ServerContext *context, uint32_t dataLength, char *commandResponsePointer) {
StringRingBuffer *txBufferPointer = USARTInstance->TxBuffer; // save base tx buffer
USARTInstance->TxBuffer = tmpTxBuffer; // set tmp tx buffer for command sending
LL_USART_DisableIT_RXNE(USARTInstance->USARTx); // turn off receiver while data transmission
memset(commandBuffer, 0, COMMAND_MAX_LENGTH);
sprintf(commandBuffer, "AT+CIPSEND=%lu,%lu\r\n", context->socketId, dataLength);
sendStringUSART(USARTInstance, commandBuffer);
while (isStringRingBufferNotEmpty(USARTInstance->TxBuffer));
LL_USART_EnableIT_RXNE(USARTInstance->USARTx); // data is sent, enable receiver
ESP8266ServerStatus serverStatus = readCommandResponse(context, commandResponsePointer);
if (serverStatus == ESP8266_SERVER_SUCCESS) { // check that module ready to receive data
LL_USART_DisableIT_RXNE(USARTInstance->USARTx); // disable receiver while data send, preventing deadlock
USARTInstance->TxBuffer = txBufferPointer; // return already formatted response buffer
LL_USART_EnableIT_TXE(USARTInstance->USARTx); // transmit response data
while (isStringRingBufferNotEmpty(USARTInstance->TxBuffer)); // wait until all data is sent
LL_USART_EnableIT_RXNE(USARTInstance->USARTx); // data is sent, enable receiver
uint32_t startTimeMillis = currentMilliSeconds();
while (!strstr(commandResponsePointer, SEND_OK_STATUS)) { // wait for data send
if ((currentMilliSeconds() - startTimeMillis) >= context->configuration->serverTimeoutMs) {
serverStatus = ESP8266_SERVER_TIMEOUT;
break;
}
if (strstr(commandResponsePointer, SEND_FAIL_STATUS) ||
strstr(commandResponsePointer, ERROR_STATUS)) {// new request can occur while response send and close previous connection
serverStatus = ESP8266_SERVER_ERROR;
break;
}
delay_ms(1);
}
}
uint32_t commandResponseLength = strlen(commandResponsePointer);
memset(commandResponsePointer, 0, commandResponseLength);
USARTInstance->RxBuffer->head -= commandResponseLength;
USARTInstance->TxBuffer = txBufferPointer;
return serverStatus;
}
static void closeConnectionESP8266(ServerContext *context, uint32_t connectionId, char *commandResponsePointer) {
memset(commandBuffer, 0, COMMAND_MAX_LENGTH);
sprintf(commandBuffer, "AT+CIPCLOSE=%lu\r\n", connectionId);
LL_USART_DisableIT_RXNE(USARTInstance->USARTx);
sendStringUSART(USARTInstance, commandBuffer);
while (isStringRingBufferNotEmpty(USARTInstance->TxBuffer));
LL_USART_EnableIT_RXNE(USARTInstance->USARTx);
readCommandResponse(context, commandResponsePointer);
}