-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP32_DeVICE_Webif_Set_Cb.c
418 lines (281 loc) · 11.8 KB
/
ESP32_DeVICE_Webif_Set_Cb.c
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
412
413
414
415
416
417
418
//#################################################################################################
//### Function: CGI Code for SCDE Feature - Generic for SCD-Engine 2
//### ESP 8266 / 32 / IoT SOC Activities ...
//### (C) EcSUHA
//### Maik Schulze, Sandfuhren 4, 38448 Wolfsburg, Germany
//### MSchulze780@GMAIL.COM
//### EcSUHA - ECONOMIC SURVEILLANCE AND HOME AUTOMATION - WWW.EcSUHA.DE
//##################################################################################################
#include <ProjectConfig.h>
#include <esp8266.h>
#include <Platform.h>
// the Smart-Connected-Device-Engine structures & types ..
#include <SCDE_s.h>
// provides WebIf, need the structures & types ...
#include "WebIf_Module_s.h"
#include "WebIf_Module.h"
#include "ESP32_DeVICE_Module.h"
#include "ESP32_DeVICE_Webif_Set_Cb.h"
#include "ESP32_DeVICE_Set.h"
/*
*--------------------------------------------------------------------------------------------------
*FName: Webif_Set_Cb
* Desc: For ESP32_DEVICE - the WEBIF_set resource Remote Procedure (RP).
* Should be assinged to URI resources. Is used to process incoming set, ... requests
* to HTTP & HATP Server. POST ; Scheme HTTP ; Path opt. with UID ; Mime .set
* Used Header-Fields:
* An Finite-State-Machine processes requests and responses with code and message.
* Info: conn->getArgs -> Query String
* conn->body_data -> reserved for internals and attribute exchange
* Para: HTTPDConnSlotData *conn -> ptr to connection slot
* Rets: int -> get result cmd
*--------------------------------------------------------------------------------------------------
*/
// for: SCDE_set RPC, Finite State Machine processing State - Stage 2 (after parsing finished)
// #XX for debugging
enum SCDE_set_state {
S2S_set_Check_Stage1_Parser_State = 0 // #00 check if parser of stage 1 is in a state that stage 2 can continue work
, S2S_set_Action_S1_Parse_Body_Data // #01 detected that stage 1 has not parsed the complete message. Processing body data
, S2S_set_Action_S1_Msg_Prsd_Complete // #02 detected that stage 1 has parsed the complete message including body data
, S2S_set_Check_RPC_Authorization // #03 Check if request contains the required authorization
, S2S_set_Check_and_Get_ADID // #04 Check and get the Active-Directory-ID
, S2S_set_Parse_Query_String // #05 Parse the Query String
// done - start writing response to TX buff.
// No Error messages after this State !
, S2S_set_TX_Response_with_Code // #06 add response to the TX (response) buffer. TX + Disconn if error code
, S2S_set_TX_Add_HDR_Flds // #07 add header fields (if any) to the TX (response) buffer
, S2S_set_TX_Add_Body_Data // #08 add body data (if any) to the TX (response) buffer
, S2S_set_DONE_TX_Response_Buff // #09 done, incl. response, now TX response buffer and then disconnect
};
int
ESP32_DeVICE_Webif_Set_Cb(WebIf_HTTPDConnSlotData_t* conn)
{
// connection aborted? nothing to clean up.
if ( conn->conn == NULL )
return HTTPD_CGI_DISCONNECT_CONN;
//---------------------------------------------------------------------------------------------------
# if ESP32_SCDE_Module_DBG >= 4
printf("|SCDE_WEBIF_set>");
# endif
/*
#if ESP32_SCDE_Module_DBG >= 5
SCDEFn_at_ESP32_SCDE_M->Log3Fn(conn->p_entry_active_dir_matching_definition->name,
conn->p_entry_active_dir_matching_definition->nameLen,
5,
"SCDE_set Fn of Module '%.*s' is called.",
conn->p_entry_active_dir_matching_definition->module->provided->typeNameLen,
conn->p_entry_active_dir_matching_definition->module->provided->typeName);
#endif
*/
//---------------------------------------------------------------------------------------------------
// init variables (that are only living this call)
// pointer used to point to identified arguments (Query)
char* Args = NULL; // null requred to prevent errror msg
// the response message -> goes to body data
Entry_String_t* respMsg = NULL;
// the response header fields
// strTextMultiple_t* respHdrFlds = NULL;
// for extracted Active Directory ID
// int16_t ADID = 0; // 0 blodsinn?
// remember the readings affected by the parsing process
// uint32 AffectedReadings = 0;
// Response argument buffer & write position
// char RespArgsBuf[512];
// char* RespArgsWPos = RespArgsBuf;
// Header Fields buffer & write position
// char HdrFldsBuf[512];
// char* HdrFldsWPos = NULL;
// Body-Data buffer & write position
// char BdyDataBuf[512];
// char* BdyDataWPos = NULL;
// store errors ...
// int stage2_scde_errno = (enum http_errno) 0; //enum?
//--------------------------------------------------------------------------------------------------
// stage 2 processing state
// int S2_Statex;
// enum state S2_State = (enum state) conn->S2_State;
enum SCDE_set_state S2_State = (enum SCDE_set_state) S2S_set_Check_Stage1_Parser_State;
// set initial stage 2 processing state (= 0)
// S2_Statex = S2S_set_Check_Stage1_Parser_State;
# if ESP32_SCDE_Module_DBG >= 4
printf("|FST-Start>");
# endif
reexecute:
// Debug output, Stage 2, FSM State,
# if ESP32_SCDE_Module_DBG >= 4
printf("|S2.%d>",
S2_State);
# endif
switch (S2_State) {
//--------------------------------------------------------------------------------------------------
// #00 check if parser of stage 1 is in a state that stage 2 can continue work
case S2S_set_Check_Stage1_Parser_State:
if (conn->SlotParserState != s_HTTPD_Msg_Prsd_Complete) {
return HTTPD_CGI_WAITRX_CONN;
}
// else
// {
// // Header parsed, now parsing BodyData - update state
// S2_State = Step_Msg_Prs_Body_Data;
// }
else {
// Message parsed complete - update state
S2_State = S2S_set_Action_S1_Msg_Prsd_Complete;
}
break;
//--------------------------------------------------------------------------------------------------
case S2S_set_Action_S1_Parse_Body_Data: // #01
break;
//--------------------------------------------------------------------------------------------------
// #02 set Arguments according to method, check if arguments
case S2S_set_Action_S1_Msg_Prsd_Complete:
// GET or SET Method ?
if ( (conn->parser_method == HTTP_GET) || // GET Method ?
(conn->parser_method == HTTP_SET) ) { // SET Method ?
Args = conn->getArgs;
}
// POST Method ?
else if (conn->parser_method == HTTP_POST) {
Args = conn->p_body_data;
}
// do we have arguments ? -> Error & break if not
if (!Args) {
// no arguments error- finnish
conn->parser_http_errno = HPE_INVALID_URL;
// goto error;
S2_State = S2S_set_TX_Response_with_Code;
}
// else go to next step
else {
#if ESP32_SCDE_Module_DBG >= 4
printf("Args:%s>",
Args);
#endif
// we have args - go to next step
S2_State = S2S_set_Check_RPC_Authorization;
}
break;
//--------------------------------------------------------------------------------------------------
// #03 Check for valid authorization (AUTH_GENERIC_RESSOURCE)
case S2S_set_Check_RPC_Authorization:
/* skip auth temp..
if (SCDED_AuthCheck(conn, AUTH_GENERIC_RESSOURCE)) {
// not authorized - finnish with error
conn->parser_http_errno = HPE_NO_AUTH;
// goto error;
S2_State = S2S_set_TX_Response_with_Code;
}
else {
// authorized - go to next step
S2_State = S2S_set_Parse_Query_String; // SCHRITT übersprungen !!!S2S_set_Check_and_Get_ADID;
}
*/S2_State = S2S_set_Parse_Query_String;
break;
//--------------------------------------------------------------------------------------------------
/*
// #04 Check & Get Active Directory ID (ADID)
case S2S_set_Check_and_Get_ADID:
//ist immer valid!!!
// Get index value from conn->ActiveDirID
ADID = conn->ActiveDirID -1;
// resource available check ...
if ( ADID >= SysCfgRamFlashMirrored->MySCDE_FeatHWCfg.NumFeat)
{
// Active Directory ID (ADID) out of range error- finnish with error
conn->parser_http_errno = HPE_ADID_AOR;
//goto error;
S2_State = S2S_set_TX_Response_with_Code;
}
else {
#if ESP32_SCDE_Module_DBG >= 4
printf("ADID:%d>",
ADID);
#endif
// Got valid ADID - go to next step
S2_State = S2S_set_Parse_Query_String;
}
break;
*/
//--------------------------------------------------------------------------------------------------
// #05 Parse the Query String
case S2S_set_Parse_Query_String:
{
char **p_p_argv;
int argc;
// split the query to args
p_p_argv = SCDEFn_at_ESP32_DeVICE_M->ArgParse_SplitURLEncodedArgsToAllocatedMemFn(&argc, Args);
respMsg = ESP32_DeVICE_Set(conn->p_entry_active_dir_matching_definition
,p_p_argv, argc);
// ArgParse Split_ToArgsFn has allocated mem. Free it.
SCDEFn_at_ESP32_DeVICE_M->ArgParse_FreeSplittedArgsInAllocatedMemFn(p_p_argv);
// assuming processed OK
conn->parser_http_errno = 0;
// parsed query string - go to next step
S2_State = S2S_set_TX_Response_with_Code;
}
break;
//--------------------------------------------------------------------------------------------------
// #06 add response to the TX (response) buffer. TX + Disconn if error code
case S2S_set_TX_Response_with_Code:
// start resonse header with code and desc.
SCDED_StartRespHeader2(conn);
// >0 -> error, something went wrong, disconn
if (conn->parser_http_errno) {
// response added to TX buf, code ERROR - NO header fields, NO body data !!!
return HTTPD_CGI_DISCONNECT_CONN;
}
// response added to TX buf, code OK - go to next step
S2_State = S2S_set_TX_Add_HDR_Flds;
break;
//--------------------------------------------------------------------------------------------------
// #07 add header fields (if any) to the TX (response) buffer
case S2S_set_TX_Add_HDR_Flds:
// add 'CONTENT-LENGHT' Hdr-Fld, if we will send data
if (respMsg && respMsg->string.len) {
char *contentLengthStr;
int contentLengthLen = asprintf(&contentLengthStr
,"%d"
,respMsg->string.len);
if (contentLengthStr) {
SCDED_AddHdrFld(conn, "CONTENT-LENGHT", contentLengthStr, contentLengthLen);
free(contentLengthStr);
}
}
// end header by adding '\r\n'
SCDED_EndHeader(conn);
// header fields added - go to next step
S2_State = S2S_set_TX_Add_Body_Data;
break;
//--------------------------------------------------------------------------------------------------
// #08 add body data (if any) to the TX (response) buffer
case S2S_set_TX_Add_Body_Data:
if (respMsg) {
HTTPD_Send_To_Send_Buffer(conn
,(char *) respMsg->string.p_char
,respMsg->string.len);
if (respMsg->string.p_char) free(respMsg->string.p_char);
free(respMsg);
}
// body data added - go to next step
S2_State = S2S_set_DONE_TX_Response_Buff;
break;
//--------------------------------------------------------------------------------------------------
// #09 done, incl. response, now TX response buffer and then disconnect
case S2S_set_DONE_TX_Response_Buff:
# if ESP32_SCDE_Module_DBG >= 4
printf("|Done!>");
# endif
return HTTPD_CGI_DISCONNECT_CONN;
break;
//--------------------------------------------------------------------------------------------------
default:
// set error number
conn->parser_http_errno = HPE_UNHANDLED_STATE;
// error number set - go to next step (TX response with code and disconnect)
S2_State = S2S_set_TX_Response_with_Code;
break;
//--------------------------------------------------------------------------------------------------
}
goto reexecute;
return HTTPD_CGI_DISCONNECT_CONN;
}