Skip to content

Commit 12f131c

Browse files
committed
[fix] #2
1 parent 100251f commit 12f131c

File tree

2 files changed

+99
-95
lines changed

2 files changed

+99
-95
lines changed

iot_protocol.cpp

Lines changed: 89 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ void IoTApp::runMiddleware(IoTRequest *request, int index = 0)
2323
if (index == this->middlewares.size() - 1)
2424
{
2525
/* Free memory */
26-
this->freeRequest(request);
26+
free(request->path);
27+
free(request->body);
2728
}
2829
}
2930

@@ -32,86 +33,11 @@ void IoTApp::listen(Client *client)
3233
this->clients.push_back(client);
3334
}
3435

35-
uint16_t IoTApp::generateRequestId()
36-
{
37-
uint16_t id = (uint16_t)(millis() % 10000);
38-
if (this->requestResponse.find(id) != this->requestResponse.end() || id == 0)
39-
{
40-
return this->generateRequestId();
41-
}
42-
return id;
43-
}
44-
45-
IoTRequest *IoTApp::send(IoTRequest *request, IoTRequestResponse *requestResponse)
46-
{
47-
48-
request->version = IOT_VERSION;
49-
50-
if (request->id == 0)
51-
{
52-
request->id = this->generateRequestId();
53-
}
54-
55-
if (request->path == NULL)
56-
{
57-
request->path = static_cast<char *>(malloc(1 * sizeof(char) + 1));
58-
request->path[0] = '/';
59-
request->path[1] = '\0';
60-
}
61-
size_t pathLength = strlen(request->path);
62-
63-
String headers = "";
64-
for (auto header = request->headers.begin(); header != request->headers.end(); ++header)
65-
{
66-
headers += header->first + ":" + header->second + "\n";
67-
}
68-
69-
size_t dataLength = 1 + 1 + 1 + 2 + 1 /* (version+\n+method+id+\n) */ + pathLength + 1 /* (\n) */ + headers.length();
70-
71-
if (request->body != NULL)
72-
{
73-
dataLength += 1 + 1 /* (B+\n) */ + request->bodyLength;
74-
}
75-
76-
uint8_t *data = (uint8_t *)(malloc(dataLength * sizeof(uint8_t)));
77-
data[0] = request->id;
78-
data[1] = '\n';
79-
data[2] = static_cast<uint8_t>((char)request->method);
80-
data[3] = request->id >> 8; /* Id as Big Endian (MSB first) */
81-
data[4] = request->id - (data[3] << 8);
82-
data[5] = '\n';
83-
memcpy((data + 6), request->path, pathLength);
84-
size_t nextIndex = 6 + pathLength + 1;
85-
data[nextIndex] = '\n';
86-
nextIndex++;
87-
88-
if (headers.length() > 0)
89-
{
90-
memcpy((data + nextIndex), headers.c_str(), headers.length());
91-
nextIndex += (headers.length() + 1);
92-
}
93-
if (request->body != NULL)
94-
{
95-
data[nextIndex] = 'B';
96-
data[++nextIndex] = '\n';
97-
memcpy((data + nextIndex + 1), request->body, request->bodyLength);
98-
}
99-
100-
request->client->write(data, dataLength);
101-
102-
return request;
103-
}
104-
105-
void IoTApp::resetClients()
106-
{
107-
this->clients.clear();
108-
}
109-
11036
void IoTApp::onData(Client *client, uint8_t *buffer, size_t bufLen)
11137
{
11238

11339
IoTRequest request = {
114-
1,
40+
0,
11541
EIoTMethod::SIGNAL,
11642
0,
11743
NULL,
@@ -225,6 +151,92 @@ void IoTApp::onData(Client *client, uint8_t *buffer, size_t bufLen)
225151
this->runMiddleware(&request);
226152
}
227153

154+
uint16_t IoTApp::generateRequestId()
155+
{
156+
uint16_t id = (uint16_t)(millis() % 10000);
157+
if (this->requestResponse.find(id) != this->requestResponse.end() || id == 0)
158+
{
159+
return this->generateRequestId();
160+
}
161+
return id;
162+
}
163+
164+
IoTRequest *IoTApp::send(IoTRequest *request, IoTRequestResponse *requestResponse)
165+
{
166+
167+
if (request->version == 0)
168+
{
169+
request->version = IOT_VERSION;
170+
}
171+
172+
if (request->id == 0)
173+
{
174+
request->id = this->generateRequestId();
175+
}
176+
177+
bool shouldFreePath = false;
178+
if (request->path == NULL)
179+
{
180+
request->path = static_cast<char *>(malloc(1 * sizeof(char) + 1));
181+
request->path[0] = '/';
182+
request->path[1] = '\0';
183+
shouldFreePath = true;
184+
}
185+
size_t pathLength = strlen(request->path);
186+
187+
String headers = "";
188+
for (auto header = request->headers.begin(); header != request->headers.end(); ++header)
189+
{
190+
headers += header->first + ":" + header->second + "\n";
191+
}
192+
193+
size_t dataLength = 1 + 1 + 1 + 2 + 1 /* (version+\n+method+id+\n) */ + pathLength + 1 /* (\n) */ + headers.length();
194+
195+
if (request->body != NULL)
196+
{
197+
dataLength += 1 + 1 /* (B+\n) */ + request->bodyLength;
198+
}
199+
200+
uint8_t *data = (uint8_t *)(malloc(dataLength * sizeof(uint8_t)));
201+
data[0] = request->version;
202+
data[1] = '\n';
203+
data[2] = static_cast<uint8_t>((char)request->method);
204+
data[3] = request->id >> 8; /* Id as Big Endian - (MSB first) */
205+
data[4] = request->id - (data[3] << 8); /* Id as Big Endian - (LSB last) */
206+
data[5] = '\n';
207+
memcpy((data + 6), request->path, pathLength);
208+
size_t nextIndex = 5 /* (last index) */ + pathLength + 1 /* next */;
209+
data[nextIndex] = '\n';
210+
nextIndex++;
211+
212+
if (headers.length() > 0)
213+
{
214+
memcpy((data + nextIndex), headers.c_str(), headers.length());
215+
nextIndex += headers.length();
216+
}
217+
if (request->body != NULL)
218+
{
219+
data[nextIndex] = 'B';
220+
data[++nextIndex] = '\n';
221+
memcpy((data + nextIndex + 1), request->body, request->bodyLength);
222+
}
223+
224+
request->client->write(data, dataLength);
225+
226+
free(data);
227+
if (shouldFreePath)
228+
{
229+
free(request->path);
230+
}
231+
232+
return request;
233+
}
234+
235+
void IoTApp::resetClients()
236+
{
237+
this->clients.clear();
238+
}
239+
228240
void IoTApp::loop()
229241
{
230242

@@ -244,10 +256,4 @@ void IoTApp::loop()
244256
this->onData(client, buffer, (indexBuffer - 1));
245257
}
246258
}
247-
}
248-
249-
void IoTApp::freeRequest(IoTRequest *request)
250-
{
251-
free(request->path);
252-
free(request->body);
253259
}

iot_protocol.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,21 @@
1919
#define IOT_VERSION (byte)1
2020
#define IOT_PROTOCOL_MAX_READ_LENGTH 1024
2121

22+
enum class EIoTMethod : char
23+
{
24+
SIGNAL = 'S',
25+
REQUEST = 'R',
26+
RESPONSE = 'r'
27+
};
28+
2229
struct IoTRequest
2330
{
2431
byte version;
2532
EIoTMethod method;
2633
uint16_t id;
2734
char *path;
2835
std::map<String, String> headers;
29-
36+
3037
uint8_t *body;
3138
size_t bodyLength;
3239
Client *client;
@@ -35,13 +42,6 @@ struct IoTRequest
3542
typedef std::function<void(void)> Next;
3643
typedef void (*IoTMiddleware)(IoTRequest *, Next *);
3744

38-
enum class EIoTMethod : char
39-
{
40-
SIGNAL = 'S',
41-
REQUEST = 'R',
42-
RESPONSE = 'r'
43-
};
44-
4545
enum class EIoTRequestPart : char
4646
{
4747
BODY = 'B',
@@ -50,7 +50,7 @@ enum class EIoTRequestPart : char
5050
struct IoTRequestResponse
5151
{
5252
std::function<void(IoTRequest *response)> onResponse;
53-
std::function<void(IoTRequest *request)> onResponse;
53+
std::function<void(IoTRequest *request)> onTimeout;
5454
int timeout;
5555
};
5656

@@ -59,7 +59,7 @@ class IoTApp
5959
private:
6060
std::vector<Client *> clients;
6161
void onData(Client *client, uint8_t *buffer, size_t bufLen);
62-
std::map<uint16_t, IoTRequestResponse*> requestResponse;
62+
std::map<uint16_t, IoTRequestResponse *> requestResponse = std::map<uint16_t, IoTRequestResponse *>();
6363

6464
public:
6565
std::vector<IoTMiddleware> middlewares;
@@ -74,11 +74,9 @@ class IoTApp
7474
IoTRequest *response(IoTRequest *request, uint8_t *body);
7575
IoTRequest *send(IoTRequest *request, IoTRequestResponse *requestResponse);
7676

77-
7877
/* Helpers methods */
7978
void resetClients();
8079
void loop();
81-
void freeRequest(IoTRequest * request);
8280
};
8381

8482
// #ifdef __cplusplus

0 commit comments

Comments
 (0)