-
Notifications
You must be signed in to change notification settings - Fork 0
/
iot_protocol.h
158 lines (129 loc) · 3.97 KB
/
iot_protocol.h
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
#pragma once
#ifndef __IOT_PROTOCOL_H__
#define __IOT_PROTOCOL_H__
// #ifdef __cplusplus
// extern "C"
// {
// #endif
#include "Arduino.h"
#include <vector>
#include <functional>
#include <map>
#include <algorithm>
#include "iot_helpers.h"
#define IOT_VERSION (uint8_t)1
#define IOT_ETX 0x3
#define IOT_RS 0x1E
#define IOT_MSCB_ID 0b00000010
#define IOT_MSCB_PATH 0b00000001
#define IOT_LSCB_HEADER 0b00000010
#define IOT_LSCB_BODY 0b00000001
#ifndef IOT_PROTOCOL_DEFAULT_ALIVE_INTERVAL
#define IOT_PROTOCOL_DEFAULT_ALIVE_INTERVAL 60
#endif
#ifndef IOT_PROTOCOL_DEFAULT_BUFFER_SIZE
#define IOT_PROTOCOL_DEFAULT_BUFFER_SIZE 1024
#endif
#define IOT_MULTIPART_TIMEOUT 5000
enum class EIoTMethod : uint8_t
{
SIGNAL = 0x1,
REQUEST = 0x2,
RESPONSE = 0x3,
STREAMING = 0x4,
ALIVE_REQUEST = 0x5,
ALIVE_RESPONSE = 0x6,
BUFFER_SIZE_REQUEST = 0x7,
BUFFER_SIZE_RESPONSE = 0x8
};
struct IoTClient;
struct IoTRequest
{
uint8_t version;
EIoTMethod method;
uint16_t id;
char *path;
std::map<char *, char *> headers;
uint8_t *body;
size_t bodyLength;
size_t totalBodyLength;
size_t parts;
IoTClient *iotClient;
};
typedef std::function<void(void)> Next;
typedef void (*IoTMiddleware)(IoTRequest *, Next *);
typedef std::function<void(IoTRequest *response)> OnResponse;
typedef std::function<void(IoTRequest *request)> OnTimeout;
typedef std::function<void(IoTRequest *request, uint32_t totalDataSent, uint32_t part)> OnPartSent;
struct IoTRequestResponse
{
OnResponse *onResponse;
OnTimeout *onTimeout;
OnPartSent *onPartSent;
unsigned long timeout;
IoTRequest request;
};
struct IoTMultiPart
{
uint32_t parts; /* Number of Parts */
uint32_t received; /* Bytes received */
unsigned long timeout;
};
typedef std::function<void(IoTClient *iotClient)> OnDisconnect;
struct IoTClient
{
Client *client;
std::map<uint16_t, IoTRequestResponse> requestResponse;
std::map<uint16_t, IoTMultiPart> multiPartControl;
uint8_t *remainBuffer; /* Remain data on buffer o be processed */
size_t remainBufferLength;
bool lockedForWrite;
/* Alive */
uint16_t aliveInterval;
unsigned long aliveNextRequest;
/* Buffer */
uint32_t bufferSize;
OnDisconnect *onDisconnect;
};
class IoTProtocol
{
private:
std::map<Client *, IoTClient *> clients = std::map<Client *, IoTClient *>();
void onData(IoTClient *iotClient, uint8_t *buffer, size_t bufLen);
/* Alive Request Response Timeout */
OnTimeout onAliveRequestTimeout;
/* Buffer Size Response */
OnResponse onBufferSizeResponse;
public:
IoTProtocol(unsigned long timeout = 1000, uint32_t delay = 300);
uint32_t delay = 300;
unsigned long timeout = 1000;
std::vector<IoTMiddleware> middlewares;
/* Common methods */
void use(IoTMiddleware middleware);
void runMiddleware(IoTRequest *request, int index);
void listen(IoTClient *iotClient);
uint16_t generateRequestId(IoTClient *iotClient);
IoTRequest *signal(IoTRequest *request);
IoTRequest *request(IoTRequest *request, IoTRequestResponse *requestResponse);
IoTRequest *response(IoTRequest *request);
IoTRequest *streaming(IoTRequest *request, IoTRequestResponse *requestResponse);
IoTRequest *aliveRequest(IoTRequest *request, IoTRequestResponse *requestResponse);
IoTRequest *aliveResponse(IoTRequest *request);
IoTRequest *bufferSizeRequest(IoTClient *iotClient, uint32_t size);
IoTRequest *bufferSizeResponse(IoTRequest *request);
IoTRequest *send(IoTRequest *request, IoTRequestResponse *requestResponse);
void resetRemainBuffer(IoTClient *iotClient);
void scheduleNextAliveRequest(IoTClient *iotClient);
/* Helper methods */
void freeRequest(IoTRequest *request);
// void resetClients();
void readClient(IoTClient *iotClient);
void loop();
/* Utils for app layer */
const char *getHeader(IoTRequest *request, const char *headerKey);
};
// #ifdef __cplusplus
// }
// #endif
#endif