-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLiveObjectsCellular.cpp
More file actions
371 lines (352 loc) · 10.4 KB
/
Copy pathLiveObjectsCellular.cpp
File metadata and controls
371 lines (352 loc) · 10.4 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright (C) Orange
*
* This software is distributed under the terms and conditions of the 'MIT'
* license which can be found in the file 'LICENSE.md' in this package distribution
*/
#include "LiveObjectsCellular.h"
#if defined NBD || defined GSMD
LiveObjectsCellular::LiveObjectsCellular()
:
LiveObjectsMKR()
,m_Acces()
,m_Scanner()
,m_Sms()
#ifdef GSMD
,m_GPRSAcces()
#endif
#ifdef LIVE_OBJECTS_IOT_SAFE
,m_IoTSafe(IOT_SAFE_CUSTOM_AID, IOT_SAFE_CUSTOM_AID_LEN)
#endif
{
String num = SECRET_SERVER_MSISDN;
for (int i = 0; i < num.length(); i+=2)
{
if ((i + 1) == num.length() && num.length() % 2 == 1)
{
num += num[i];
num[i] = 'F';
}
else
{
char tmp = num[i + 1];
num[i + 1] = num[i];
num[i] = tmp;
}
}
m_sNumber += ToHexT((char)(num.length() - 1));
m_sNumber += "A1";
m_sNumber += num;
}
LiveObjectsCellular::~LiveObjectsCellular()
{}
void LiveObjectsCellular::begin(Protocol p, Encoding s, bool bDebug)
{
LiveObjectsBase::begin(p,s,bDebug);
if(p==MQTT)
{
switch(m_Security)
{
#ifdef LIVE_OBJECTS_IOT_SAFE
case MUTUAL_TLS_WITH_IOT_SAFE:
#ifdef NBD
m_pClient = new NBClient();
#elif defined GSMD
m_pClient = new GSMClient();
#endif
m_pBearSSLClient = new BearSSLClient(*m_pClient, TAs, 1);
m_pMqttclient = new MqttClient(m_pBearSSLClient);
m_nPort = 8883;
break;
#endif
case TLS:
#ifdef NBD
m_pClient = new NBSSLClient();
#elif defined GSMD
m_pClient = new GSMSSLClient();
#endif
m_pMqttclient = new MqttClient(m_pClient);
m_nPort = 8883;
break;
case NONE:
#ifdef NBD
m_pClient = new NBClient();
#elif defined GSMD
m_pClient = new GSMClient();
#endif
m_pMqttclient = new MqttClient(m_pClient);
m_nPort = 1883;
break;
default:
outputDebug(ERR,"Wrong mode! stopping...");
while(true);
}
m_pMqttclient->onMessage(messageCallback);
}
else
{
if(SECRET_SERVER_MSISDN.length()<3)
{
outputDebug(ERR,"SERVER MSISDN is empty! Check arduino_secrets.h! Stopping here....");
while(true);
}
}
m_bInitialized = true;
}
#ifdef LIVE_OBJECTS_IOT_SAFE
unsigned long LiveObjectsCellular::getTime() {
outputDebug(INFO,"Getting time from the cellular module...");
// get the current time from the cellular module
return m_Acces.getTime();
}
unsigned long LiveObjectsCellular::trampolineGetTime() {
return LiveObjects::get().getTime();
}
#endif
void LiveObjectsCellular::connectNetwork()
{
//Set client id as IMEI
if (!m_bInitialized)
{
outputDebug(WARN,"missing begin() call, calling with default protcol=MQTT, security protcol=TLS, debug=true");
begin();
}
#ifdef NBD
if(m_Acces.status()!= NB_READY)
#elif defined GSMD
if(m_Acces.status()!= GSM_READY)
#endif
{
#ifdef NBD
NBModem modem;
#elif defined GSMD
GSMModem modem;
#endif
if(modem.begin())
{
#ifdef LIVE_OBJECTS_IOT_SAFE
if (m_Security == MUTUAL_TLS_WITH_IOT_SAFE)
{
// Set a callback to get the current time used to validate the servers certificate
ArduinoBearSSL.onGetTime(trampolineGetTime);
// Wait a little before sending command to applet as we're getting strange
// behavior otherwise (i.e. an empty file is returned)
delay(2000);
br_x509_certificate client_certificate = m_IoTSafe.readClientCertificate();
m_pBearSSLClient->setEccCert(client_certificate);
m_sMqttid = m_IoTSafe.getClientCertificateCommonName();
outputDebug(INFO,"m_sMqttid retrieved from client certificate: ", m_sMqttid);
m_pBearSSLClient->setEccSign(m_IoTSafe.trampolineSign);
}
#endif
if(m_sMqttid.length()==0)
{
String imei="";
for(int i=1;i<=3;i++)
{
imei=modem.getIMEI();
if(imei.length()!=0) break;
delay(100*i);
}
m_sMqttid = imei;
}
}
else
{
outputDebug(ERR,"Failed to initialize modem!" );
while(true);
}
outputDebug(INFO,"Connecting to cellular network");
#ifdef NBD
while (m_Acces.begin(SECRET_PINNUMBER.c_str(), SECRET_APN.c_str(), SECRET_APN_USER.c_str(), SECRET_APN_PASS.c_str()) != NB_READY)
outputDebug(TXT,".");
outputDebug();
#elif defined GSMD
while ((m_Acces.begin(SECRET_PINNUMBER.c_str()) != GSM_READY)) outputDebug(TXT, ".");
if(m_Protocol != SMS) while ((m_GPRSAcces.attachGPRS(SECRET_APN.c_str(), SECRET_APN_USER.c_str(), SECRET_APN_PASS.c_str()) != GPRS_READY)) outputDebug(TXT, ".");
outputDebug();
#endif
if(m_Encoding==BINARY) MODEM.send("AT+CMGF=0");
delay(200);
outputDebug(INFO,"You're connected to the network");
}
else
{
#ifdef NBD
while (m_Acces.begin(SECRET_PINNUMBER.c_str(), SECRET_APN.c_str(), SECRET_APN_USER.c_str(), SECRET_APN_PASS.c_str()) != NB_READY)
#elif defined GSMD
if(m_Protocol==MQTT) while ((m_GPRSAcces.attachGPRS(SECRET_APN.c_str(), SECRET_APN_USER.c_str(), SECRET_APN_PASS.c_str()) != GPRS_READY)) outputDebug(TXT, ".");
#endif
delay(100);//outputDebug(INFO, "Sending atcmgf");
if(m_Encoding==BINARY) MODEM.send("AT+CMGF=0");
else MODEM.send("AT+CMGF=1");
delay(200);
}
if(m_nPort==8883){
if (!m_bCertLoaded) {
int i;
String response;
for (i=0; i<(sizeof LO_ROOT_CERT / sizeof *LO_ROOT_CERT); ++i) {
// get MD5 of LO_ROOT_CERT[i].name certificate
MODEM.sendf("AT+USECMNG=4,0,\"%s\"", LO_ROOT_CERT[i].name);
MODEM.waitForResponse(2000, &response);
int rl = response.length();
String md5=response.substring(rl-1-32, rl-1); // MD5 has 32-hex digits
// uploading certificate if necessary
if (md5!=LO_ROOT_CERT[i].md5) {
outputDebug(INFO, "Loading Root CA certificate: ", LO_ROOT_CERT[i].name, ", size: " ,LO_ROOT_CERT[i].size, ", md5: " ,LO_ROOT_CERT[i].md5);
MODEM.sendf("AT+USECMNG=0,0,\"%s\",%d", LO_ROOT_CERT[i].name, LO_ROOT_CERT[i].size);
if (MODEM.waitForPrompt() != 1) {
outputDebug(ERR,"Problem loading certificate!\nStopping here.");
while (1) ;
}
else {
MODEM.write(LO_ROOT_CERT[i].data, LO_ROOT_CERT[i].size);
int ready;
while (!MODEM.ready());
m_bCertLoaded = true;
outputDebug(INFO,"Certificate loaded");
}
}
else {
outputDebug(INFO,"Certificate ", LO_ROOT_CERT[i].name, " has been already uploaded");
}
}
}
}
m_sModel = m_sMqttid;
}
void LiveObjectsCellular::checkNetwork()
{
#ifdef NBD
if(m_Acces.status()!= NB_READY)
connectNetwork();
#elif defined GSMD
if(m_Acces.status()!= GSM_READY)
connectNetwork();
#endif
if(m_Protocol == SMS)
{
String msg;
if(m_Sms.available())
{
int c =0;
while(true)
{
c = m_Sms.read();
if(c==-1)break;
msg+=(char)c;
}
String s;
if(m_Encoding==TEXT) s = msg;
else
{
s = parseCommand(msg);
s = from7bit(s);
}
int spaceIndex = s.indexOf(' ');
String cmd_name = s.substring(0,spaceIndex);
s = s.substring(spaceIndex+1);
outputDebug(INFO,"Received command: ",cmd_name);
LiveObjects_command cmd(cmd_name,nullptr);
int index = commands.find(&cmd);
if(index >= 0 )
{
outputDebug(INFO,"Found command");
commands[index]->callback(s,msg);
}
else outputDebug(INFO,"Unknown command");
m_Sms.flush();
}
}
}
String LiveObjectsCellular::parseCommand(String inputString)
{
String len; len += inputString[0]; len += inputString[1];
int index = 2;
index += 2 * strtol(len.c_str(), nullptr, 16);
index += 2;
len = ""; len += inputString[index]; index++; len += inputString[index]; index++;
index += 2;
index += (int)ceil(strtol(len.c_str(), nullptr, 16)/2.)*2;
index += 2 * 9;
len = ""; len += inputString[index]; index++; len += inputString[index]; index++;
String retVal;
retVal = inputString.substring(index);
//outputDebug(INFO, "PARSED: ", retVal);
return retVal;
}
void LiveObjectsCellular::disconnectNetwork()
{
outputDebug(INFO,"Disconnecting from cellular network...");
m_Acces.shutdown();
}
void LiveObjectsCellular::messageCallback(int msg)
{
LiveObjects::get().onMQTTmessage(msg);
};
void LiveObjectsCellular::addNetworkInfo()
{
String strength= m_Scanner.getSignalStrength();
String carrier = m_Scanner.getCurrentCarrier();
if(m_Protocol == MQTT && m_Encoding==TEXT)
{
bool status;
#ifdef NBD
status = m_Acces.status() == NB_READY;
#elif defined GSMD
status = m_Acces.status() == GSM_READY;
#endif
addToPayload(easyDataPayload[JSONVALUE].createNestedObject("networkInfo"),"connection_status",status,"strength",strength,"carrier",carrier);
}
#ifdef NBD
else addToStringPayload((m_Acces.status() == NB_READY),strength,carrier);
#elif defined GSMD
else addToStringPayload(m_Acces.status() == GSM_READY,strength,carrier);
#endif
}
void LiveObjectsCellular::sendData()
{
if(m_Protocol == MQTT) LiveObjectsBase::sendData();
else
{
if(m_sPayload.length() > 160)
{
outputDebug(ERR,"Payload to big, skipping sending...");
return;
}
else if (m_sPayload.length() < 1)
{
outputDebug(WARN,"Payload is empty, skipping...");
return;
}
if(m_Encoding==TEXT)
{
outputDebug(INFO,"Publishing message: ", m_sPayload);
if(m_Sms.beginSMS(SECRET_SERVER_MSISDN.c_str())!=1) outputDebug(ERR,"Error occured while sending SMS");
m_Sms.print(m_sPayload);
if(m_Sms.endSMS()!=1) outputDebug(ERR,"Error occured while sending SMS");
}
else
{
outputDebug(INFO,"Payload before conversion: ",m_sPayload);
String msg;
static String fixedStart = "001100";
static String fixedInfo = "0000FF";
msg+=fixedStart;
msg+=m_sNumber;
msg+= fixedInfo;
msg+=ToHex((char)m_sPayload.length());
msg+=to7bit(m_sPayload);
int msgSize = (msg.length()-2)/2;
msg+='\x1A';
outputDebug(INFO,"Message len: ",msgSize);
outputDebug(INFO,"Publishing message: ", msg);
MODEM.sendf("AT+CMGS=%d\r", msgSize);
delay(100);
MODEM.sendf(msg.c_str());
}
m_sPayload="";
}
}
#endif