Skip to content

Commit 6fd5da4

Browse files
committed
[feaure] multipart body: sender ok
1 parent bbce281 commit 6fd5da4

File tree

4 files changed

+82
-17
lines changed

4 files changed

+82
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

README.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ IOT PROTOCOL uses middlewares and router's filtering features based on [express
1919
- Minimum request size is 2 bytes
2020
- Request-response model like HTTP protocol
2121
- Adaptive requests methods for optimizing data length
22+
- Multipart (Send large data)
2223

2324
---
2425

2526
## Preamble Version 1
2627

2728
```
28-
<MCB + LCB>
29+
<MSCB + LSCB>
2930
[ID]
3031
[PATH + ETX]
3132
[HEADER + ETX]
@@ -36,8 +37,34 @@ IOT PROTOCOL uses middlewares and router's filtering features based on [express
3637
3738
> `[...]` OPTIONAL
3839
40+
> `[PATH + ETX] + [HEADER + ETX]` **MUST NOT BE MORE THAN 1016 Bytes**
41+
>
42+
> + 1024 Bytes : IOT_PROTOCOL_BUFFER_SIZE
43+
> - 1 Byte : MSCB_SIZE
44+
> - 1 Byte : LSCB_SIZE
45+
> - 2 Bytes : ID_SIZE
46+
> - 4 Bytes : BODY_LENGTH_MAXIMUM_SIZE (Streaming)
47+
> ----------------
48+
> + 1016 Bytes : [PATH + ETX]_SIZE + [HEADER + ETX]_SIZE
49+
>
50+
> |-----------------------------------------IOT_PROTOCOL_BUFFER_SIZE(1024)-----------------------------------|
51+
>
52+
> |--MSCB(1)--|
53+
>
54+
> |--LSCB(1)--|
55+
>
56+
> |--ID_SIZE(2)--|
57+
>
58+
> |--[PATH + ETX]--|
59+
>
60+
> |--[HEADER + ETX]--|
61+
>
62+
> |--BODY_LENGTH_MAXIMUM_SIZE(4)--|
63+
>
64+
>
65+
3966
---
40-
### [0] **MCB**: MSB_CONTROL_BYTE
67+
### [0] **MSCB**: MSB_CONTROL_BYTE
4168

4269
The Most Significant Control Byte. **REQUIRED**
4370

@@ -54,7 +81,7 @@ The Most Significant Control Byte. **REQUIRED**
5481
- Range: `from 1 up to 63`. Zero is reserved.
5582

5683
---
57-
### [1] **LCB**: LSB_CONTROL_BYTE
84+
### [1] **LSCB**: LSB_CONTROL_BYTE
5885

5986
The Least Significant Control Byte. **REQUIRED**
6087
* Size: `1 byte`
@@ -73,7 +100,7 @@ The Least Significant Control Byte. **REQUIRED**
73100

74101
Methods Types
75102

76-
|Name | Description | MCB::ID | MCB::PATH | LCB::METHOD | LCB::HEADER | LCB::BODY | BODY::LENGTH | Minimum Total Length |
103+
|Name | Description | MSCB::ID | MSCB::PATH | LSCB::METHOD | LSCB::HEADER | LSCB::BODY | BODY::LENGTH | Minimum Total Length |
77104
|:-- | :-- | :--: | :--: | :--: | :--: | :--: | :-- | :--: |
78105
| *Signal* | Ligthweight signals like events | 0 | 0/1 | `0b000001` | 0/1 | 0/1 | *up to 255 bytes* | 2 bytes |
79106
| *Request* | Request that needs response | 1 | 0/1 | `0b000010` | 0/1 | 0/1 | *up to 65535 bytes* | 4 bytes |

iot_protocol.cpp

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "iot_protocol.h"
22

3-
IoTApp::IoTApp(uint32_t delay = 300)
4-
{
3+
IoTApp::IoTApp(uint32_t delay)
4+
{
55
this->delay = delay;
66
}
77

@@ -54,6 +54,7 @@ void IoTApp::onData(Client *client, uint8_t *buffer, size_t bufLen)
5454
std::map<char *, char *>(),
5555
NULL,
5656
0,
57+
0,
5758
client};
5859

5960
size_t offset = 0;
@@ -274,14 +275,24 @@ IoTRequest *IoTApp::send(IoTRequest *request, IoTRequestResponse *requestRespons
274275
dataLength += headersLength;
275276
}
276277

277-
if((pathLength + headersLength) > IOT_PROTOCOL_BUFFER_SIZE - 8) {
278+
if ((pathLength + headersLength) > IOT_PROTOCOL_BUFFER_SIZE - 8)
279+
{
278280
throw "Path and Headers too big";
279281
}
280282

281283
if (LSCB & IOT_LSCB_BODY)
282284
{
283285
dataLength += bodyLengthSize + request->bodyLength;
284286
}
287+
else
288+
{
289+
bodyLengthSize = 0;
290+
}
291+
292+
if (dataLength > IOT_PROTOCOL_BUFFER_SIZE)
293+
{
294+
dataLength = IOT_PROTOCOL_BUFFER_SIZE;
295+
}
285296

286297
/* Record Data */
287298

@@ -343,14 +354,43 @@ IoTRequest *IoTApp::send(IoTRequest *request, IoTRequestResponse *requestRespons
343354
{
344355
data[++nextIndex] = (request->bodyLength >> ((i - 1) * 8)) & 255;
345356
}
357+
}
358+
359+
size_t prefixDataIndex = nextIndex;
360+
361+
std::function<size_t(size_t, size_t)> writeBodyPart = [&writeBodyPart, this, request, prefixDataIndex, &data](size_t i = 0, size_t parts = 0)
362+
{
363+
size_t indexData = prefixDataIndex + 1; //42
346364
/* Body */
347-
for (size_t i = 0; i < request->bodyLength; i++)
365+
if (request->bodyLength > 0) //1060 > 0
348366
{
349-
data[++nextIndex] = *(request->body + i);
367+
size_t bodyBufferRemain = (request->bodyLength - i); //1984 //
368+
size_t bodyUntilIndex = ((bodyBufferRemain + indexData) > IOT_PROTOCOL_BUFFER_SIZE) ? i + (IOT_PROTOCOL_BUFFER_SIZE - indexData) : i + bodyBufferRemain; // 1004 //1060
369+
for (; i <= bodyUntilIndex; i++) //[0-1004] //[1005 - 1060]
370+
{
371+
//i = 1005 indexData = 21 ... i = 1060 indexData = 76
372+
*(data + (indexData++)) = *(request->body + i);
373+
//i = 1006 indexData = 22 ... i = 1061 indexData = 77
374+
}
350375
}
351-
}
352376

353-
data[++nextIndex] = '\0';
377+
*(data + (indexData)) = '\0';
378+
request->client->write(data, indexData-1);
379+
request->client->flush();
380+
vTaskDelay(this->delay);
381+
382+
parts++;
383+
if (i >= request->bodyLength)
384+
{
385+
return parts;
386+
}
387+
else
388+
{
389+
return writeBodyPart(i,parts);
390+
}
391+
};
392+
393+
request->parts = writeBodyPart(0, 0);
354394

355395
if (requestResponse != NULL)
356396
{
@@ -364,11 +404,6 @@ IoTRequest *IoTApp::send(IoTRequest *request, IoTRequestResponse *requestRespons
364404
this->requestResponse.insert(std::make_pair(request->id, *requestResponse));
365405
}
366406

367-
368-
vTaskDelay(this->delay);
369-
370-
request->client->write(data, dataLength);
371-
372407
return request;
373408
}
374409

@@ -396,7 +431,8 @@ void IoTApp::readClient(Client *client)
396431
this->onData(client, buffer, (bufferLength));
397432
}
398433

399-
if(bufferLength >= IOT_PROTOCOL_BUFFER_SIZE) {
434+
if (bufferLength >= IOT_PROTOCOL_BUFFER_SIZE)
435+
{
400436
return this->readClient(client);
401437
}
402438
}

iot_protocol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct IoTRequest
4444
std::map<char *, char *> headers;
4545
uint8_t *body;
4646
size_t bodyLength;
47+
size_t parts;
4748
Client *client;
4849
};
4950

0 commit comments

Comments
 (0)