-
Notifications
You must be signed in to change notification settings - Fork 2
/
kaa.h
executable file
·268 lines (234 loc) · 9.3 KB
/
kaa.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
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
#ifndef KAA_IOT_PLATFORM_LIB_H
#define KAA_IOT_PLATFORM_LIB_H
#include <PubSubClient.h>
#include <ArduinoJson.h>
#define PRINT_DBG(...) printMsg(__VA_ARGS__)
#define KPC_INSTANCE_NAME "kp1"
#define EPMX_INSTANCE_NAME "epmx"
#define CMX_INSTANCE_NAME "cmx"
#define DCX_INSTANCE_NAME "dcx"
#define CEX_INSTANCE_NAME "cex"
class Kaa {
private:
PubSubClient* pub_sub_client_ptr;
char* kaa_token;
char* kaa_application_version;
char metadata_update_topic[100];
char config_topic[100];
char config_response_topic[100];
char data_topic[100];
char command_topic[100];
char command_result_topic[100];
int (*commandCallback)(char* command_type, char* payload, unsigned int len);
void (*configResponseCallback)(char* request_status, char* payload, unsigned int len);
void (*configPushCallback)(char* payload, unsigned int len);
#ifndef EXTERNAL_DEBUG_IMPLEMENTATION
void printMsg(const char * msg, ...) {
char buff[256];
va_list args;
va_start(args, msg);
vsnprintf(buff, sizeof(buff) - 2, msg, args);
buff[sizeof(buff) - 1] = '\0';
Serial.print(buff);
}
#endif
bool isNumber(char* token) {
for (int i = 0; i < strlen(token); i++) {
char c = token[i];
if(c < '0' || c > '9'){
return false;
}
}
return true;
}
void cmxMessageHandler(char* topic_part, char* payload, unsigned int len) {
char* rest = topic_part;
char* token = strtok_r(rest, "/", &rest);
if (!strcmp(token, kaa_token)) {
token = strtok_r(rest, "/", &rest);
if (!strcmp(token, "config")) {
token = strtok_r(rest, "/", &rest);
if (!strcmp(token, "json")) {
token = strtok_r(rest, "/", &rest);
if (isNumber(token)) {
token = strtok_r(rest, "/", &rest);
PRINT_DBG("\nConfig response [%s] received\n", token);
if (configResponseCallback != NULL) {
configResponseCallback(token, payload, len);
} else {
PRINT_DBG("Config response callback is not set. Ignoring incoming config response...\n");
}
} else if (!strcmp(token, "status")) {
PRINT_DBG("\nConfig push received\n");
if (configPushCallback != NULL) {
configPushCallback(payload, len);
} else {
PRINT_DBG("Config push callback is not set. Ignoring incoming config push...\n");
}
} else {
PRINT_DBG("\nError config message was received: %s. Ignoring it...\n", token);
}
}
}
}
}
void cexMessageHandler(char* topic_part, char* payload, unsigned int len) {
char command_type[50];
char* rest = topic_part;
char* token = strtok_r(rest, "/", &rest);
if (!strcmp(token, kaa_token)) {
token = strtok_r(rest, "/", &rest);
if (!strcmp(token, "command")) {
token = strtok_r(rest, "/", &rest);
//command_type = token;
strncpy(command_type, token, sizeof(command_type) - 1);
command_type[sizeof(command_type) - 1] = '\0';
token = strtok_r(rest, "/", &rest);
if (!strcmp(token, "status")) {
PRINT_DBG("\nCommand %s received\n", command_type);
//commandProcess(command_type, payload, len);
commandCallback(command_type, payload, len);
}
else if (!strcmp(token, "error")) {
PRINT_DBG("\nCommand %s error\n", command_type);
PRINT_DBG("%s\n", payload);
}
}
}
}
public:
Kaa(PubSubClient* _pub_sub_client_ptr, char* _kaa_token, char* _kaa_application_version){
pub_sub_client_ptr = _pub_sub_client_ptr;
kaa_token = _kaa_token;
kaa_application_version = _kaa_application_version;
composeTopics();
}
void setCommandCallback(int (*_commandCallback)(char* command_type, char* payload, unsigned int len)){
commandCallback = _commandCallback;
}
void setConfigResponseCallback(void (*_configResponseCallback)(char* request_status, char* payload, unsigned int len)) {
configResponseCallback = _configResponseCallback;
}
void setConfigPushCallback(void (*_configPushCallback)(char* payload, unsigned int len)) {
configPushCallback = _configPushCallback;
}
void setToken(char* _kaa_token){
kaa_token = _kaa_token;
}
void setAppVersion(char* _kaa_application_version){
kaa_application_version = _kaa_application_version;
}
void composeTopics() {
sprintf(metadata_update_topic, "%s/%s/%s/%s/update/keys", KPC_INSTANCE_NAME, kaa_application_version, EPMX_INSTANCE_NAME, kaa_token);
sprintf(config_topic, "%s/%s/%s/%s/config/json", KPC_INSTANCE_NAME, kaa_application_version, CMX_INSTANCE_NAME, kaa_token);
sprintf(config_response_topic, "%s/%s/%s/%s/config/json/+/status", KPC_INSTANCE_NAME, kaa_application_version, CMX_INSTANCE_NAME, kaa_token);
sprintf(data_topic, "%s/%s/%s/%s/json", KPC_INSTANCE_NAME, kaa_application_version, DCX_INSTANCE_NAME, kaa_token);
sprintf(command_topic, "%s/%s/%s/%s/command", KPC_INSTANCE_NAME, kaa_application_version, CEX_INSTANCE_NAME, kaa_token);
sprintf(command_result_topic, "%s/%s/%s/%s/result", KPC_INSTANCE_NAME, kaa_application_version, CEX_INSTANCE_NAME, kaa_token);
}
void connect() {
pub_sub_client_ptr->subscribe(config_topic);
pub_sub_client_ptr->subscribe(config_response_topic);
}
void messageArrivedCallback(const char* topic_recv, const char* payload_recv, unsigned int len) {
char topic[512];
char payload[1023];
strncpy(topic, topic_recv, sizeof(topic) - 1);
topic[sizeof(topic) - 1] = '\0';
if (len < 1023) {
strncpy(payload, payload_recv, len);
payload[len] = '\0'; //Add terminator after last payload byte
}
else {
PRINT_DBG("Error. payload len >= 1023\n");
return;
}
char* rest = topic;
char* token = strtok_r(rest, "/", &rest);
if (!strcmp(token, KPC_INSTANCE_NAME)) {
token = strtok_r(rest, "/", &rest);
if (!strcmp(token, kaa_application_version)) {
token = strtok_r(rest, "/", &rest);
//EPMX
if (!strcmp(token, EPMX_INSTANCE_NAME)) {
PRINT_DBG("Message for EPMX\n");
//Do something
}
//DCX
else if (!strcmp(token, DCX_INSTANCE_NAME)) {
PRINT_DBG("Message for DCX\n");
//Do something
}
//CMX
else if (!strcmp(token, CMX_INSTANCE_NAME)) {
PRINT_DBG("Message for CMX\n");
cmxMessageHandler(rest, payload, len);
}
//CEX
else if (!strcmp(token, CEX_INSTANCE_NAME)) {
PRINT_DBG("Message for CEX\n");
cexMessageHandler(rest, payload, len);
}
}
}
}
void sendCommandResultAllIds(char* command_type, JsonVariant json_var){
//Sending result to all command ids
PRINT_DBG("number of ids = %d\n", json_var.size());
DynamicJsonDocument doc_result(1023);
//StaticJsonDocument<255> doc_result;
for (int i = 0; i < json_var.size(); i++) {
unsigned int command_id = json_var[i]["id"].as<unsigned int>();
PRINT_DBG("command_id = %d\n", command_id);
doc_result.createNestedObject();
doc_result[i]["id"] = command_id;
doc_result[i]["statusCode"] = 200;
doc_result[i]["payload"] = "done";
}
PRINT_DBG("command_type = %s\n", command_type);
sendCommandResultRaw(command_type, doc_result.as<String>().c_str());
}
void sendCommandResult(const char* command_type, int command_id) {
char topic_buffer[200];
char command_result_payload_buffer[200];
sprintf( topic_buffer, "%s/%s", command_result_topic, command_type );
sprintf( command_result_payload_buffer, "[{\"id\": %d, \"statusCode\": 200, \"payload\": \"done\"}]", command_id);
PRINT_DBG("Publishing: %s %s\n", topic_buffer, command_result_payload_buffer);
pub_sub_client_ptr->publish(topic_buffer, command_result_payload_buffer);
}
void sendCommandResultRaw(const char* command_type, const char* json_payload) {
char topic_buffer[200];
sprintf( topic_buffer, "%s/%s", command_result_topic, command_type );
PRINT_DBG("Publishing: %s %s\n", topic_buffer, json_payload);
pub_sub_client_ptr->publish(topic_buffer, json_payload);
}
void sendDataRaw(const char* payload) {
char topic_buffer[200];
sprintf( topic_buffer, "%s/%d", data_topic, random(1, 100000) );
PRINT_DBG("Publishing: %s %s\n", topic_buffer, payload);
pub_sub_client_ptr->publish(topic_buffer, payload);
}
void sendDataRawUnreliably(const char* payload) {
PRINT_DBG("Publishing: %s %s\n", data_topic, payload);
pub_sub_client_ptr->publish(data_topic, payload);
}
void sendMetadata(const char* metadata_str) {
char topic_buffer[200];
sprintf( topic_buffer, "%s/%d", metadata_update_topic, random(1, 100000) );
PRINT_DBG("Publishing: %s %s\n", topic_buffer, metadata_str);
pub_sub_client_ptr->publish(topic_buffer, metadata_str);
}
int requestConfig() {
if (configResponseCallback == NULL) {
PRINT_DBG("Config response callback must be set to request config\n");
return 1;
}
char topic_buffer[200];
char request_config_payload[] = "{\"observe\": true}";
sprintf( topic_buffer, "%s/%d", config_topic, random(1, 100000) );
PRINT_DBG("Publishing: %s %s\n", topic_buffer, request_config_payload);
pub_sub_client_ptr->publish(topic_buffer, request_config_payload);
return 0;
}
};
#endif