Skip to content

Commit 772056a

Browse files
committed
[feature] implement v1 with body's length
1 parent 14d4535 commit 772056a

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
IoT Protocol is a protocol over TCP based on HTTP for light data traffic.
44

5-
**Motivation**: HTTP 1.1 (*http://*) request minimum size is 26 bytes (https://stackoverflow.com/a/25065027/1956719) and the HOST param is mandatory for all requests.
5+
**Motivation**: HTTP 1.1 (*http://*) protocol uses too much data traffic for IoT context. Its request minimum size is 26 bytes (https://stackoverflow.com/a/25065027/1956719) and the HOST param is mandatory for all requests.
66

7-
The IOT_PROTOCOL (*iot://*) request minimum size is 8 bytes withless to require HOST param for requests.
7+
The IOT_PROTOCOL (*iot://*) is adapted for IoT context with light data traffic. Its request minimum size is 8 bytes withless to require HOST param for requests.
88

99
## Preamble Version 1
1010

iot_protocol.cpp

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ void IoTApp::onData(Client *client, uint8_t *buffer, size_t bufLen)
5555
int indexStart = 0; // 9
5656
int indexN = -1;
5757
bool isBody = false;
58+
uint8_t *remainBuffer = NULL; /* Reamins data on buffe rto be processed */
59+
size_t remainBufferSize = 0;
5860

5961
bool invalidRequest = false;
6062

@@ -121,18 +123,23 @@ void IoTApp::onData(Client *client, uint8_t *buffer, size_t bufLen)
121123
}
122124

123125
/* Body */
124-
if (isBody == false && bufferPartLength == 1 && static_cast<EIoTRequestPart>((char)bufferPart[0]) == EIoTRequestPart::BODY)
126+
if (isBody == false && bufferPartLength == 3 && static_cast<EIoTRequestPart>((char)bufferPart[0]) == EIoTRequestPart::BODY)
125127
{
128+
request.bodyLength = (bufferPart[1] << 8) + bufferPart[2];
126129
isBody = true;
127130
break;
128131
}
129132

130133
if (isBody)
131134
{
132-
request.body = (uint8_t *)(malloc(bufferPartLength * sizeof(uint8_t) + 1));
133-
memcpy(request.body, bufferPart, bufferPartLength);
134-
request.body[bufferPartLength] = '\0';
135-
request.bodyLength = bufferPartLength;
135+
request.body = (uint8_t *)(malloc(request.bodyLength * sizeof(uint8_t) + 1));
136+
memcpy(request.body, bufferPart, request.bodyLength);
137+
request.body[request.bodyLength] = '\0';
138+
if (request.bodyLength < bufferPartLength)
139+
{
140+
remainBuffer = (bufferPart + request.bodyLength);
141+
remainBufferSize = bufferPartLength - request.bodyLength;
142+
}
136143
isBody = false;
137144
break;
138145
}
@@ -141,34 +148,38 @@ void IoTApp::onData(Client *client, uint8_t *buffer, size_t bufLen)
141148
foundN++;
142149
indexStart = indexN + 1;
143150

144-
} while (indexN >= 0 && invalidRequest == false);
151+
} while (indexN >= 0 && invalidRequest == false && remainBuffer == NULL);
145152

146153
if (invalidRequest)
147154
return;
148155

149156
/* Response */
150-
auto rr = this->requestResponse.find(request.id);
151-
if (rr != this->requestResponse.end())
157+
if (request.method == EIoTMethod::RESPONSE)
152158
{
153-
if (rr->second.onResponse != NULL)
159+
auto rr = this->requestResponse.find(request.id);
160+
if (rr != this->requestResponse.end())
154161
{
155-
(*(rr->second.onResponse))(&request);
156-
// (rr->second.onResponse)(&request);
162+
if (rr->second.onResponse != NULL)
163+
{
164+
(*(rr->second.onResponse))(&request);
165+
// (rr->second.onResponse)(&request);
157166

158-
/* free body and path ?! hehe */
159-
free(request.path);
160-
free(request.body);
167+
/* free body and path ?! hehe */
168+
free(request.path);
169+
free(request.body);
170+
}
171+
this->requestResponse.erase(request.id);
161172
}
162-
this->requestResponse.erase(request.id);
163-
return;
164173
}
165-
if (request.method == EIoTMethod::RESPONSE)
174+
else
166175
{
167-
return;
176+
/* Middleware */
177+
this->runMiddleware(&request);
168178
}
169179

170-
/* Middleware */
171-
this->runMiddleware(&request);
180+
if(remainBuffer != NULL) {
181+
this->onData(client, remainBuffer, remainBufferSize);
182+
}
172183
}
173184

174185
uint16_t IoTApp::generateRequestId()
@@ -215,7 +226,7 @@ IoTRequest *IoTApp::send(IoTRequest *request, IoTRequestResponse *requestRespons
215226

216227
if (request->body != NULL)
217228
{
218-
dataLength += 2 + request->bodyLength; /* 1 + 1 (B+\n) + request->bodyLength */
229+
dataLength += 4 + request->bodyLength; /* 1 + 2 + 1 (BODY_CHAR+BODY_LENGTH\n) + request->bodyLength */
219230
}
220231

221232
uint8_t data[dataLength + 1]; /* +1 (\0) */
@@ -234,7 +245,7 @@ IoTRequest *IoTApp::send(IoTRequest *request, IoTRequestResponse *requestRespons
234245
}
235246

236247
data[++nextIndex] = '\n';
237-
248+
238249
if (headers.length() > 0)
239250
{
240251
for (size_t i = 0; i < headers.length(); i++)
@@ -247,6 +258,8 @@ IoTRequest *IoTApp::send(IoTRequest *request, IoTRequestResponse *requestRespons
247258
{
248259
data[++nextIndex] = 'B';
249260
data[++nextIndex] = '\n';
261+
data[++nextIndex] = request->bodyLength >> 8; /* Body Length as Big Endian - (MSB first) */
262+
data[++nextIndex] = request->bodyLength - (data[(nextIndex)] << 8); /* Id as Big Endian - (LSB last) */
250263
for (size_t i = 0; i < request->bodyLength; i++)
251264
{
252265
data[++nextIndex] = *(request->body + i);
@@ -268,7 +281,7 @@ IoTRequest *IoTApp::send(IoTRequest *request, IoTRequestResponse *requestRespons
268281
}
269282

270283
request->client->write(data, dataLength);
271-
284+
272285
if (shouldFreePath)
273286
{
274287
free(request->path);

0 commit comments

Comments
 (0)