-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModem.cpp
More file actions
874 lines (650 loc) · 24.8 KB
/
Modem.cpp
File metadata and controls
874 lines (650 loc) · 24.8 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
/*
Modem.cpp - Library for using a SIM800L modem
Created by Scott Alexander Gray - 2020
*/
#include "Modem.h"
#include <Arduino.h>
#include <string>
#define MODEM_RST 5
#define MODEM_PWKEY 4
#define MODEM_POWER_ON 23
#define MODEM_TX 27
#define MODEM_RX 26
#define I2C_SDA 21
#define I2C_SCL 22
#define endline "\r\n\0"
Modem::Modem(HardwareSerial& modemSerialConn, HardwareSerial& deviceSerialConn, int bRate)
{
RDY = false;
//set the serial streams to the user specified streams
_modemPort = &modemSerialConn;
_devicePort = &deviceSerialConn;
baudRate = bRate;
}
void Modem::SetSimSettings(const char* apn, const char* pin)
{
APN = apn;
PIN = pin;
}
void Modem::ConnectGPRSBlocking()
{
SetCFUN(1); //set the functionality of the modem
SetCSTT(APN); //set the sim access point name
SetCPIN(PIN); //set the sim pin
_devicePort->print("\nWaiting For CGREG... ");
while (cgreg != Roaming && cgreg != HomeRegistered)
{
RefreshCGREG();
}
_devicePort->print("Done");
SetCGATT(1); //attach to gsm network
SetCGACT(1, 1);
SetCIICR();
SetCIFSR();
SetSAPBR(); //Bearer Registration
}
void Modem::SetCFUN(int val)
{
int bufferSize = 32; //length of buffer
char* full_cmd = new char[bufferSize]; //create buffer for command to be sent
const char* at_cmd = "AT+CFUN="; //AT command
char valChar[5]; //create buffer for char version of val
itoa(val, valChar, 10); //convert int to char array
strcpy(full_cmd, at_cmd); //copy the base command to the buffer
strcat(full_cmd, valChar); //append the valChar to the cmd buffer
//_devicePort->print("Command to be sent:\n");
//_devicePort->print(full_cmd);
int responseLen = 6; //two characters and one null character
char responseBuffer[responseLen];
SendCmdAndWait(full_cmd, responseBuffer, responseLen, false, false); //send command to the modem
_devicePort->print("\nCFUN Response: ");
_devicePort->print(responseBuffer);
}
void Modem::SetCSTT(const char* apn)
{
int bufferSize = 32; //length of buffer
char* full_cmd = new char[bufferSize]; //create buffer for command to be sent
const char* at_cmd = "AT+CSTT="; //AT command
strcpy(full_cmd, at_cmd); //copy the base command to the buffer
strcat(full_cmd, apn); //append the valChar to the cmd buffer
int responseLen = 6; //two characters and one null character
char responseBuffer[responseLen];
SendCmdAndWait(full_cmd, responseBuffer, responseLen, false, false); //send command to the modem
_devicePort->print("\nCSTT Response: ");
_devicePort->print(responseBuffer);
}
void Modem::SetCPIN(const char* pin)
{
if (*pin == '\0')
{
//_devicePort->print("Found pin char to contain null character");
return; //This skips sending a command if the pin is empty
}
//_devicePort->print("Didn't find null char");
int bufferSize = 32; //length of buffer
char* full_cmd = new char[bufferSize]; //create buffer for command to be sent
const char* at_cmd = "AT+CPIN="; //AT command
strcpy(full_cmd, at_cmd); //copy the base command to the buffer
strcat(full_cmd, pin); //append the valChar to the cmd buffer
int responseLen = 6; //two characters and one null character
char responseBuffer[responseLen];
SendCmdAndWait(full_cmd, responseBuffer, responseLen, false, false); //send command to the modem
_devicePort->print("\nSetCPIN Response: ");
_devicePort->print(responseBuffer);
}
void Modem::GetCPIN()
{
int bufferSize = 32; //length of buffer
char* full_cmd = new char[bufferSize]; //create buffer for command to be sent
const char* at_cmd = "AT+CPIN=?"; //AT command
strcpy(full_cmd, at_cmd); //copy the base command to the buffer
int responseLen = 16;
char responseBuffer[responseLen];
SendCmdAndWait(full_cmd, responseBuffer, responseLen, false, false); //send command to the modem
_devicePort->print("\nGetCPIN Response: ");
_devicePort->print(responseBuffer);
}
void Modem::RefreshCGREG()
{
int bufferSize = 32; //length of buffer
char* full_cmd = new char[bufferSize]; //create buffer for command to be sent
const char* at_cmd = "AT+CGREG?"; //AT command
strcpy(full_cmd, at_cmd); //copy the base command to the buffer
int responseLen = 16; //two characters and one null character
char wholeResponseBuffer[responseLen];
SendCmdAndWait(full_cmd, wholeResponseBuffer, responseLen, false, false); //send command to the modem
int stateBufferLen = 16; //state only length
int startIndex = 0; //index to start from
int endIndex = 11; //index to end on
char stateBuffer[stateBufferLen]; //buffer for state only char array
trimChar(wholeResponseBuffer, stateBuffer, stateBufferLen, startIndex, endIndex); //trim the char to convert it to more appropriate terms for checking
//comparing the state buffer to known responses to update cgreg enum
if (strcmp(stateBuffer, "+CGREG: 0,0") == 0)
{
cgreg = NotRegistered;
}
else if (strcmp(stateBuffer, "+CGREG: 0,1") == 0)
{
cgreg = HomeRegistered;
}
else if (strcmp(stateBuffer, "+CGREG: 0,2") == 0)
{
cgreg = Searching;
}
else if (strcmp(stateBuffer, "+CGREG: 0,3") == 0)
{
cgreg = RegistrationDenied;
}
else if (strcmp(stateBuffer, "+CGREG: 0,4") == 0)
{
cgreg = Unknown;
}
else if (strcmp(stateBuffer, "+CGREG: 0,5") == 0) //rare
{
cgreg = Roaming;
}
}
CGREG Modem::GetCGREG()
{
RefreshCGREG();
return cgreg;
}
void Modem::SetCGATT(int val) //val can only be 1 or 0, 1 attaches, 0 detaches
{
int bufferSize = 32; //length of buffer
char* full_cmd = new char[bufferSize]; //create buffer for command to be sent
const char* at_cmd = "AT+CGATT="; //AT command
char valChar[5]; //create buffer for char version of val
itoa(val, valChar, 10); //convert int to char array
strcpy(full_cmd, at_cmd); //copy the base command to the buffer
strcat(full_cmd, valChar); //append the valChar to the cmd buffer
int responseLen = 6; //two characters and one null character
char responseBuffer[responseLen];
SendCmdAndWait(full_cmd, responseBuffer, responseLen, false, false); //send command to the modem
_devicePort->print("\nSetCGATT Response: ");
_devicePort->print(responseBuffer);
}
void Modem::SetCGDCONT(int PDPNum, const char* ipMode, const char* apn)
{
int bufferSize = 32; //length of buffer
char* full_cmd = new char[bufferSize]; //create buffer for command to be sent
const char* at_cmd = "AT+CGDCONT="; //AT command
char pdpNum[5]; //create buffer for char version of PDPNum
itoa(PDPNum, pdpNum, 10); //convert int to char array
strcpy(full_cmd, at_cmd); //copy the base command to the buffer
strcat(full_cmd, pdpNum); //append the valChar to the cmd buffer
strcat(full_cmd, ",");
strcat(full_cmd, ipMode);
strcat(full_cmd, ",");
strcat(full_cmd, apn);
SendAT(full_cmd); //send command to the modem
}
void Modem::SetCGACT(int state, int cid)
{
int bufferSize = 32;
char* full_cmd = new char[bufferSize];//full command buffer
const char* at_cmd = "AT+CGACT="; //AT command
char stateChar[5]; //create buffer for char version of state
itoa(state, stateChar, 10); //convert int to char array
char cidChar[5]; //create buffer for char version of the id of the pdp context
itoa(cid, cidChar, 10); //convert int to char array
strcpy(full_cmd, at_cmd); //copy at command to command buffer
strcat(full_cmd, stateChar); //append this to command buffer
strcat(full_cmd, ","); //append this to command buffer
strcat(full_cmd, cidChar); //append this to command buffer
int responseLen = 6; //two characters and one null character
char responseBuffer[responseLen];
SendCmdAndWait(full_cmd, responseBuffer, responseLen, false, false); //send command to the modem
_devicePort->print("\nSetCGACT Response: ");
_devicePort->print(responseBuffer);
}
void Modem::GetCEER()
{
int bufferSize = 32;
char* full_cmd = new char[bufferSize];//full command buffer
const char* at_cmd = "AT+CEER?"; //AT command
strcpy(full_cmd, at_cmd); //copy at command to command buffer
int responseLen = 6; //two characters and one null character
char responseBuffer[responseLen];
SendCmdAndWait(full_cmd, responseBuffer, responseLen, false, false); //send command to the modem
_devicePort->print("\nGetCEER Response: ");
_devicePort->print(responseBuffer);
}
void Modem::SetCIICR()
{
int bufferSize = 32;
char* full_cmd = new char[bufferSize];//full command buffer
const char* at_cmd = "AT+CIICR"; //AT command
strcpy(full_cmd, at_cmd); //copy at command to command buffer
int responseLen = 32; //two characters and one null character
char responseBuffer[responseLen];
SendCmdAndWait(full_cmd, responseBuffer, responseLen, false, false); //send command to the modem
_devicePort->print("\nSetCIICR Response: ");
_devicePort->print(responseBuffer);
}
void Modem::SetCIFSR()
{
int bufferSize = 32;
char* full_cmd = new char[bufferSize];//full command buffer
const char* at_cmd = "AT+CIFSR"; //AT command
strcpy(full_cmd, at_cmd); //copy at command to command buffer
int responseLen = 32; //two characters and one null character
char responseBuffer[responseLen];
SendCmdAndWait(full_cmd, responseBuffer, responseLen, false, false); //send command to the modem
_devicePort->print("\nSetCIFSR Response: ");
_devicePort->print(responseBuffer);
}
void Modem::SetSAPBR()
{
SAPBR_Contype();//set contype
SAPBR_APN();//APN
SAPBR_Enable();//
SAPBR_Test();// Test Bearer
}
void Modem::SAPBR_Contype()
{
int bufferSize = 32;
char* full_cmd = new char[bufferSize];//full command buffer
int responseLen = 6; //length od return buffer. two characters and one null character
//contype
full_cmd = new char[bufferSize];//clearing full command buffer
const char* at_cmd = "AT+SAPBR=3,1,\"contype\",\"GPRS\""; //AT command
strcpy(full_cmd, at_cmd); //copy at command to command buffer
char responseBufferContype[responseLen];
SendCmdAndWait(full_cmd, responseBufferContype, responseLen, false, false); //send command to the modem
_devicePort->print("\nSAPBR contype: ");
_devicePort->print(responseBufferContype);
}
void Modem::SAPBR_APN()
{
int bufferSize = 32;
char* full_cmd = new char[bufferSize];//full command buffer
int responseLen = 6; //length od return buffer. two characters and one null character
const char* at_cmd = "AT+SAPBR=3,1,\"APN\",\""; //AT command
strcpy(full_cmd, at_cmd); //copy at command to command buffer
strcat(full_cmd, APN); //append this to command buffer
strcat(full_cmd, "\""); //append this to command buffer
char responseBufferAPN[responseLen];
SendCmdAndWait(full_cmd, responseBufferAPN, responseLen, false, false); //send command to the modem
_devicePort->print("\nSAPBR APN: ");
_devicePort->print(responseBufferAPN);
}
void Modem::SAPBR_Enable()
{
int bufferSize = 32;
char* full_cmd = new char[bufferSize];//full command buffer
int responseLen = 6; //length od return buffer. two characters and one null character
const char* at_cmd = "AT+SAPBR=1,1"; //AT command
strcpy(full_cmd, at_cmd); //copy at command to command buffer
char responseBufferEnable[responseLen];
SendCmdAndWait(full_cmd, responseBufferEnable, responseLen, false, false); //send command to the modem
_devicePort->print("\nSAPBR enable: ");
_devicePort->print(responseBufferEnable);
}
void Modem::SAPBR_Test()
{
int bufferSize = 32;
char* full_cmd = new char[bufferSize];//full command buffer
int responseLen = 6; //length od return buffer. two characters and one null character
const char* at_cmd = "AT+SAPBR=2,1"; //AT command
strcpy(full_cmd, at_cmd); //copy at command to command buffer
char responseBufferTestBearer[16];
SendCmdAndWait(full_cmd, responseBufferTestBearer, responseLen, false, false); //send command to the modem
_devicePort->print("\nSAPBR Bearer Test: ");
_devicePort->print(responseBufferTestBearer);
}
void Modem::GetCOPS()
{
int bufferSize = 32; //length of buffer
char* full_cmd = new char[bufferSize]; //create buffer for command to be sent
const char* at_cmd = "AT+CGREG=?"; //AT command
strcpy(full_cmd, at_cmd); //copy the base command to the buffer
int responseLen = 20; //two characters and one null character
char responseBuffer[responseLen];
SendCmdAndWait(full_cmd, responseBuffer, responseLen, false, false); //send command to the modem
_devicePort->print("\nGetCOPS Response: ");
_devicePort->print(responseBuffer);
}
int Modem::GetCSQ()
{
int bufferSize = 32; //length of buffer
char* full_cmd = new char[bufferSize]; //create buffer for command to be sent
const char* at_cmd = "AT+CSQ"; //AT command
strcpy(full_cmd, at_cmd); //copy the base command to the buffer
int responseLen = 16; //two characters and one null character
char responseBuffer[responseLen];
SendCmdAndWait(full_cmd, responseBuffer, responseLen, false, false); //send command to the modem
int stateBufferLen = 16; //state only length
int startIndex = 6; //index to start from
int endIndex = 7; //index to end on
char stateBuffer[stateBufferLen]; //buffer for state only char array
trimChar(responseBuffer, stateBuffer, stateBufferLen, startIndex, endIndex); //trim the char to convert it to more appropriate terms for checking
int retVal = atoi(stateBuffer); //convert char array to int
return retVal;
}
void Modem::RefreshCIPSTATUS()
{
int bufferSize = 32; //length of buffer
char* full_cmd = new char[bufferSize]; //create buffer for command to be sent
const char* at_cmd = "AT+CIPSTATUS"; //AT command
strcpy(full_cmd, at_cmd); //copy the base command to the buffer
//Manipulating data returned by modem
int bufferLen = 32;
char wholeResponseBuffer[bufferLen];
SendCmdAndWait(full_cmd, wholeResponseBuffer, bufferLen, true, false); //now we have the response without the command
int stateBufferLen = 16; //state only length
int startIndex = 13; //index to start from
int endIndex = 0;
char stateBuffer[stateBufferLen]; //buffer for state only char array
trimChar(wholeResponseBuffer, stateBuffer, stateBufferLen, startIndex, endIndex); //trim the char to convert it to more appropriate terms for checking
//_devicePort->print("\ntrimmed Response:\n");
//_devicePort->print(stateBuffer);
//setting enum variable based on returned value
if (strcmp(stateBuffer, "IP INITIAL") == 0)
{
cipstatus = IPInitial;
}
else if (strcmp(stateBuffer, "IP START") == 0)
{
cipstatus = IPStart;
}
else if (strcmp(stateBuffer, "IP GPRSACT") == 0)
{
cipstatus = IPGprsAct;
}
else if (strcmp(stateBuffer, "IP STATUS") == 0)
{
cipstatus = IPStatus;
}
else if (strcmp(stateBuffer, "PDP DEACT") == 0)
{
cipstatus = PDPDeact;
}
else if (strcmp(stateBuffer, "IP CONFIG") == 0) //rare
{
cipstatus = IPConfig;
}
else if (strcmp(stateBuffer, "IP PROCESSING") == 0) //rare
{
cipstatus = IPProcessing;
}
}
CIPSTATUS Modem::GetCIPSTATUS()
{
RefreshCIPSTATUS();
return cipstatus;
}
void Modem::Init()
{
RDY = false;
// I2C for SIM800 (to keep it running when powered from battery)//not sure if nessesary
//TwoWire I2CPower = TwoWire(0);
//_devicePort->begin(baudRate);
// Set modem reset, enable, power pins
pinMode(MODEM_PWKEY, OUTPUT);
pinMode(MODEM_RST, OUTPUT);
pinMode(MODEM_POWER_ON, OUTPUT);
_devicePort->print("Initializing modem...");
// Set GSM module baud rate and UART pins
_modemPort->begin(baudRate, SERIAL_8N1, MODEM_RX, MODEM_TX);
while (!_modemPort)
{
//wait here until serial connection is ready
}
//set the power on the pins
digitalWrite(MODEM_PWKEY, LOW);
digitalWrite(MODEM_RST, HIGH);
digitalWrite(MODEM_POWER_ON, HIGH);
//delay(6000);
ModemRDYCheck();
unsigned long timeoutTime = 8000;
unsigned long startTime = millis();
while (!RDY)
{
unsigned long currentTime = millis();
if ((currentTime - startTime) > timeoutTime)
{
_devicePort->print("timeout_");
return;
}
}
_devicePort->print("done\n");
}
void Modem::DeInit()
{
RDY = false;
_devicePort->print("De-initializing modem...");
//as per the manual
digitalWrite(MODEM_PWKEY, HIGH);
digitalWrite(MODEM_PWKEY, LOW);
delay(1500);
digitalWrite(MODEM_PWKEY, HIGH);
//turn off things that were on
digitalWrite(MODEM_RST, LOW);
digitalWrite(MODEM_POWER_ON, LOW);
//disconnect from the serial connection
_modemPort->end();
delay(500);
_devicePort->print("done\n");
}
void Modem::ModemRDYCheck()
{
if (RDY)
{
return;
}
int bufferLen = 4;
char responseBuffer[bufferLen];
WaitForResponse(bufferLen, responseBuffer, 6000); //will timeout after 6s
//_devicePort->print("Zero if == RDY: ");
int state = strcmp(responseBuffer, "RDY");
if (state == 0)
{
RDY = true;
}
//_devicePort->println(state);
//_devicePort->println(responseBuffer);
}
//When this gets run, it copies all the available chars, up to the length of bufferLen into repsonse
void Modem::WaitForResponse(int bufferLen, char response[], unsigned long timeoutTime)
{
//TODO make timeout so that it doesn't hang here foreve causing device to Freeze forever
//int bufferLen = 4;
char initialResponseBuffer[bufferLen];
bool done = false; //used to exit while loop, not for actual modem ready state
//int actualResponseLength = 0;
//int readIndex = 0;
int responseIndex = 0;
//_devicePort->print("Entering Wait Loop..\n");
unsigned long timeStarted;
unsigned long timeLastAvailable;
timeStarted = millis();
while (!done)
{
if (_modemPort->available())
{
timeStarted = millis();
char c = _modemPort->read();
if (responseIndex < bufferLen - 1)
{
if (c == '\0' || c == '\n' || c == '\r')
{
//do nothing with it
}
else
{
//_devicePort->write(c);
initialResponseBuffer[responseIndex] = c;
//_devicePort->write(initialResponseBuffer[responseIndex]);
//_devicePort->println(responseIndex);
responseIndex += 1;
}
}
else
{
initialResponseBuffer[responseIndex] = '\0';
//_devicePort->print("Exiting Wait Loop..\n");
done = true;
}
}
else
{
unsigned long timeCurrent = millis();
if ((timeCurrent - timeStarted) > timeoutTime)
{
done = true;
//also make last character null
initialResponseBuffer[bufferLen-1] = '\0';
_devicePort->println("Timout!!!");
}
}
}
strcpy(response, initialResponseBuffer);
}
void Modem::SendAT(const char* cmd)
{
_modemPort->write(cmd); //write command to stream
_modemPort->write(endline); //write endline character to stream to end stream
_modemPort->flush();
delay(1000); //not sure why this delay is here
}
void Modem::removeCmd(char inputBuffer[], const char* cmd, char returnBuffer[], int returnBufferSize)
{
int returnIndex = 0;
for (int i = (strlen(cmd) + 3); i < strlen(inputBuffer); i++)
{
returnBuffer[returnIndex] = inputBuffer[i];
returnIndex++;
if (returnIndex == (returnBufferSize - 1) || i == (strlen(inputBuffer) - 1))
{
//SerialMon.println("Adding Null Character");
returnBuffer[returnIndex] = '\0';
break;
}
}
}
void Modem::trimChar(char inputBuffer[], char outputBuffer[], int outputBufferSize, int startIndex, int endIndex)
{
char tempBuffer[outputBufferSize]; //create temporary buffer to store trimmed array
int tempIndex = 0;
if (startIndex == 0 && endIndex == 0) //means nothing must get trimmed
{
strcpy(outputBuffer, inputBuffer);
}
else if (startIndex == 0 && endIndex > 0) //means end must get trimmed
{
for (int i = 0; i < strlen(inputBuffer); i++)
{
tempBuffer[tempIndex] = inputBuffer[i]; //setting value of new char array
//increase index
tempIndex++;
if (tempIndex == outputBufferSize - 1) //means output buffer is full
{
break;
}
if (tempIndex == endIndex) //reached the index at which to stop at
{
break;
}
}
tempBuffer[tempIndex] = '\0'; //add null character to end of array when at end of input or reached output size
strcpy(outputBuffer, tempBuffer);
return;
}
else if (startIndex > 0 && endIndex == 0) //means beginning must get trimmed
{
//_devicePort->write("Trimming Beginning:\n");
for (int i = startIndex; i < strlen(inputBuffer); i++)
{
//printing to serial for debugging
/*char indexChar[4];
itoa(i, indexChar, 10);
_devicePort->write("Char[");
_devicePort->write(indexChar);
_devicePort->write("]: ");
_devicePort->write(inputBuffer[i]);
_devicePort->write("\n");*/
tempBuffer[tempIndex] = inputBuffer[i]; //setting value of new char array
//increase index
tempIndex++;
if (tempIndex == outputBufferSize - 1) //means output buffer is full
{
break;
}
}
tempBuffer[tempIndex] = '\0'; //add null character to end of array when at end of input or reached output size
strcpy(outputBuffer, tempBuffer);
return;
}
else if (startIndex > 0 && endIndex > 0) //means some of the end and some of the beginnning must get trimmed
{
for (int i = startIndex; i < strlen(inputBuffer); i++)
{
tempBuffer[tempIndex] = inputBuffer[i]; //setting value of new char array
//increase index
tempIndex++;
if (tempIndex == outputBufferSize - 1) //means output buffer is full
{
break;
}
if (i == endIndex) //reached the index at which to stop at
{
break;
}
}
tempBuffer[tempIndex] = '\0'; //add null character to end of array when at end of input or reached output size
strcpy(outputBuffer, tempBuffer); //copy the chars in tempBuffer to outputBuffer
return;
}
}
void Modem::SendCmdAndWait(const char* cmd, char returnBuffer[], int returnBufferSize, bool removeOKFromResponse, bool printResponse)
{
//clear modemportBuffer before doing all this
//send cmd
SendAT(cmd);
//now wait
//setting up entire response buffer
unsigned int bufferLen = 100;
char responseBuffer[bufferLen];
int responseIndex = 0;
//retrieve entire response and insert it into buffer
for (int i = 0; i < bufferLen; i++)
{
if (_modemPort->available())
{
char c = _modemPort->read();
if (c == '\n')
{
responseBuffer[i] = ' ';
}
else if (c == '\r')
{
responseBuffer[i] = ' ';
}
else
{
responseBuffer[i] = c;
}
}
else
{
responseBuffer[i] = '\0';
responseBuffer[i - 1] = '\0';
responseBuffer[i - 2] = '\0';
//this is done because I know the last two characters are newline and carraige return
break;
}
}
//setting up buffer to return
int responseOnlyBufferSize = returnBufferSize;
char responseOnly[responseOnlyBufferSize];
removeCmd(responseBuffer, cmd, responseOnly, responseOnlyBufferSize);
strcpy(returnBuffer, responseOnly);
//if print response bool is true, print response to serialMon
if (printResponse)
{
_devicePort->write("\nResponse of Cmd:\n");
_devicePort->write(responseOnly);
_devicePort->write(endline);
}
}