-
Notifications
You must be signed in to change notification settings - Fork 1
/
TelitTaskV2.c
327 lines (267 loc) · 7.45 KB
/
TelitTaskV2.c
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include "telit_api.h"
#include "util.h"
#include "azure.h"
#include "common.h"
extern void openComPort();
/////////////////////////////////////////////////////////////////////////////////
static const char cfg_APN[] = "www.ab.kyivstar.net";
static const char cfg_BLOB[] = "farmedge.blob.core.windows.net";
static const char cfg_HUB[] = "farmedge.azure-devices.net";
static const char cfg_DEVICE_ID[] = "WeatherStation";
static const char cfg_API_VER[] = "2016-11-14";
static const char cfg_SAS[] = "SharedAccessSignature sr=farmedge.azure-devices.net&sig=yxUw4DSR58ZRkzngZNcUfZHE9hDrs%2FB12nIeKXX5it4%3D&se=1539956606&skn=device";
/////////////////////////////////////////////////////////////////////////////////
static const char at_INIT[] = "E0Q0V1X1&K0";
static const char at_REBOOT[] = "#REBOOT";
static const char at_CMEE[] = "+CMEE=2";
static const char at_CGDCONT[] = "+CGDCONT=1,\"IP\",\"%s\"";
static const char at_SGACT[] = "#SGACT=1,1";
static const char at_SGACT_OFF[] = "#SGACT=1,0";
static const char at_SSLEN[] = "#SSLEN=1,1";
static const char at_SSLEN_OFF[] = "#SSLEN=1,0";
static const char at_SSLCFG[] = "#SSLCFG=1,1,300,90,100,1,1";
static const char at_SSLSECCFG[] = "#SSLSECCFG=1,0,0";
static const char at_SSLD[] = "#SSLD=1,443,\"%s\",0,1";
static const char at_SSLH[] = "#SSLH=1";
static const char at_SSLSEND[] = "#SSLSEND=1";
static const char c_end[] = { (char)0x1A, (char)0x0D, 0 };
static const char c_prompt[] = ">";
static const char c_post_msg[] =
"POST /devices/%s/messages/events?api-version=%s HTTP/1.1\n"\
"Host: %s\n"\
"Authorization: %s\n"\
"Content-Type: application/json\n"\
"Content-Length: %d\n"\
"\n"\
"%s"\
"%s";
static const char c_post_msg_chunked[] =
"POST /devices/%s/messages/events?api-version=%s HTTP/1.1\n"\
"Host: %s\n"\
"Authorization: %s\n"\
"Content-Type: application/json\n"\
"Transfer-Encoding: chunked\n"\
"\n";
static const char c_msg_type_status[] = "[Status]\n";
static const char c_msg_type_telemetry[] = "[Telemetry]\n";
/////////////////////////////////////////////////////////////////////////////////
#define INPUT_BUFF_SIZE 3000
typedef struct
{
unsigned int head;
char data[INPUT_BUFF_SIZE];
} data_buffer_t;
static data_buffer_t _input_buffer;
#define OUTPUT_BUFF_SIZE 1000
static char _output_buffer[OUTPUT_BUFF_SIZE];
static device_config_t devCfg;
/////////////////////////////////////////////////////////////////////////////////
// Callbacks
void parseIP(const char* token)
{
//"#SGACT: 10.250.64.139"
static const char cSGACT[] = "#SGACT:";
if (strncmp(token, cSGACT, strlen(cSGACT)) == 0)
{
token += strlen(cSGACT);
char ip[20];
strtrim(token, ip);
__trace_log("[IP]:%s", ip);
}
}
void requestDownloadFirmware(const char* token)
{
if (strncmp(token, c_prompt, strlen(c_prompt)) == 0)
{
const char getFirm[] = "GET /firmware/baltimore.cer HTTP/1.1\n"\
"Host: farmedge.blob.core.windows.net\n"\
"\n";
at_raw(getFirm, strlen(getFirm));
at_raw(c_end, strlen(c_end));
}
}
void onProcessResponse(const char* data, int len)
{
if ((_input_buffer.head + len) < INPUT_BUFF_SIZE)
{
memcpy(_input_buffer.data+ _input_buffer.head, data, len);
_input_buffer.head += len;
}
}
void requestGetConfig(const char* token)
{
if (strncmp(token, c_prompt, strlen(c_prompt)) == 0)
{
const char cGET_MSG[] = "GET /devices/%s/messages/devicebound?api-version=%s HTTP/1.1\n"\
"Host: %s\n"\
"Authorization: %s\n\n";
sprintf(_output_buffer,
cGET_MSG,
cfg_DEVICE_ID,
cfg_API_VER,
cfg_HUB,
cfg_SAS);
at_raw(_output_buffer, strlen(_output_buffer));
at_raw(c_end, strlen(c_end));
}
}
void requestDeleteConfig(const char* token)
{
const char cDEL_MSG[] =
"DELETE /devices/%s/messages/devicebound/%s?api-version=%s HTTP/1.1\n"\
"Host: %s\n"\
"Authorization: %s\n\n";
if (strncmp(token, c_prompt, strlen(c_prompt)) == 0)
{
sprintf(_output_buffer,
cDEL_MSG,
cfg_DEVICE_ID,
devCfg.etag,
cfg_API_VER,
cfg_HUB,
cfg_SAS);
at_raw(_output_buffer, strlen(_output_buffer));
at_raw(c_end, strlen(c_end));
}
}
void requestSendStatus(const char* token)
{
if (strncmp(token, c_prompt, strlen(c_prompt)) == 0)
{
const char cTEST_STATUS[] = "WX0000001\n"\
"Version: 1.1.7\n"\
"Interface : 601\n"\
"Model : Telit HE910 - EUD 12.00.225\n"\
"Strength : 10\n"\
"Quality : 3\n"\
"Retries : 37\n"\
"Maxtries : 17\n"\
"Update : -4\n"\
"Upload : 255";
sprintf(_output_buffer,
c_post_msg,
cfg_DEVICE_ID,
cfg_API_VER,
cfg_HUB,
cfg_SAS,
strlen(cTEST_STATUS) + strlen(c_msg_type_status),
c_msg_type_status,
cTEST_STATUS);;
at_raw(_output_buffer, strlen(_output_buffer));
at_raw(c_end, strlen(c_end));
}
}
void requestSendTelemetry(const char* token)
{
if (strncmp(token, c_prompt, strlen(c_prompt)) == 0)
{
const char* cTEST_TELEMETRY[4] =
{
"2700147848000 15M2017 - 11 - 07 12:180303030303030503092930311014\n" ,
"201711071215 64.1 15.4 00.0 170 003 000\n",
"201711071200 66.4 15.6 00.0 169 003 001\n",
"201711071145 57.7 17.1 00.0 159 001 000" };
sprintf(_output_buffer,
c_post_msg_chunked,
cfg_DEVICE_ID,
cfg_API_VER,
cfg_HUB,
cfg_SAS);
at_raw(_output_buffer, strlen(_output_buffer));
for (int chunk = 0; chunk < 4; chunk++)
{
sprintf(_output_buffer,
"%x\r\n"\
"%s\r\n",
strlen(cTEST_TELEMETRY[chunk]),
cTEST_TELEMETRY[chunk]);
at_raw(_output_buffer, strlen(_output_buffer));
}
const char endChunk[] = "0\r\n\r\n";
at_raw(endChunk, strlen(endChunk));
at_raw(c_end, strlen(c_end));
}
}
/////////////////////////////////////////////////////////////////////////////////
int initModem()
{
at_cmd(at_INIT);
at_cmd_arg(at_CGDCONT, cfg_APN);
if (at_req(at_SGACT, parseIP) == AT_OK)
{
if (at_cmd(at_SSLEN) == AT_OK)
{
at_cmd(at_SSLCFG);
at_cmd(at_SSLSECCFG);
return 1;
}
}
return 0;
}
void downloadFirmware()
{
at_cmd_arg(at_SSLD, cfg_BLOB);
at_req(at_SSLSEND, requestDownloadFirmware);
_input_buffer.head = 0;
at_recv(onProcessResponse);
at_cmd(at_SSLH);
printf("\n\r[Response]:\n\r%s\n\r",_input_buffer.data);
}
void acknowledgeConfig()
{
at_req(at_SSLSEND, requestDeleteConfig);
at_recv(0);
}
void retrieveConfig()
{
device_config_status_t status;
do
{
at_req(at_SSLSEND, requestGetConfig);
_input_buffer.head = 0;
at_recv(onProcessResponse);
status = parseDeviceConfig(_input_buffer.data, _input_buffer.head, &devCfg);
if (status == DEV_CFG_OK)
{
acknowledgeConfig();
}
} while (status == DEV_CFG_OK); //loop until there are no config to be pulled
}
void sendStatus()
{
at_req(at_SSLSEND, requestSendStatus);
at_recv(0);
}
void sendTelemetry()
{
at_req(at_SSLSEND, requestSendTelemetry);
at_recv(0);
}
/////////////////////////////////////////////////////////////////////////////////
int main()
{
openComPort();
//at_cmd_send(at_REBOOT);
//sleep(8000);
printf("Press any key...\n\r");
getchar();
if (initModem())
{
if (at_cmd_arg(at_SSLD, cfg_HUB) == AT_OK)
{
retrieveConfig();
sendTelemetry();
sendStatus();
at_cmd(at_SSLH);
}
at_cmd(at_SSLEN_OFF);
at_cmd(at_SGACT_OFF);
}
printf("Press any key to exit\r\n");
getchar();
return 0;
}