-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhayes.cpp
1217 lines (1118 loc) · 34 KB
/
hayes.cpp
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
hayes.cpp - AT-Hayes commands interface
Copyright (C) 2018 Costin STROIE <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "hayes.h"
Profile profile;
HAYES::HAYES(CFG_t *conf, AFSK *afsk): cfg(conf), afskModem(afsk) {
// Try to restore the profile or use factory defaults
profile.init(cfg);
}
HAYES::~HAYES() {
}
/**
Print \r\n, as configured in S registers
*/
void HAYES::printCRLF() {
Serial.write(cfg->sregs[3]);
Serial.write(cfg->sregs[4]);
}
/**
Print a character array from program memory
@param str the character array to print
@param newline print the EOL
*/
void HAYES::print_P(const char *str, bool newline) {
uint8_t val;
do {
val = pgm_read_byte(str++);
if (val) Serial.write(val);
} while (val);
if (newline) printCRLF();
}
/**
Print the startup banner
*/
void HAYES::banner() {
printCRLF();
print_P(DEVNAME);
Serial.write(' ');
print_P(VERSION);
Serial.print(" (");
print_P(DATE);
Serial.print(")");
printCRLF();
}
/**
Get the uptime
@param upt the time in seconds
@param buf character array to return the text to
@param len the maximum length of the character array
@return uptime in seconds
*/
void HAYES::getUptime(uint32_t upt, char *buf, size_t len) {
// Compute days, hours, minutes and seconds
int ss = upt % 60;
int mm = (upt % 3600) / 60;
int hh = (upt % 86400L) / 3600;
int dd = upt / 86400L;
// Create the formatted time
if (dd == 0) snprintf_P(buf, len, PSTR("%02d:%02d:%02d"), hh, mm, ss);
else if (dd == 1) snprintf_P(buf, len, PSTR("%d day, %02d:%02d:%02d"), dd, hh, mm, ss);
else snprintf_P(buf, len, PSTR("%d days, %02d:%02d:%02d"), dd, hh, mm, ss);
}
/**
Parse the buffer and return an integer
@param buf the char buffer to parse
@param idx index to start with
@param len maximum to parse, 0 for no limit
@return the integer found or zero, also set the cmdResult
*/
int16_t HAYES::getInteger(char* buf, int8_t idx, uint8_t len = 32) {
int16_t result = 0; // The result
bool isNeg = false; // Negative flag
uint8_t sdx = 0; // Start index
// Be positive
cmdResult = RC_OK;
// If the specified index is negative, use the last local index;
// if it is positive, re-set the local index
if (idx >= 0) {
// Use the new index
ldx = idx;
sdx = idx;
}
// Preamble: find the first sign or digit character
while ((ldx - sdx < len)
and (buf[ldx] != 0)
and (buf[ldx] != '-')
and (buf[ldx] != '+')
and (not isdigit(buf[ldx])))
ldx++;
// At this point, check if we have ended the buffer or the length
if ((ldx - sdx <= len) and (buf[ldx] != 0)) {
// Might start with '+' or '-', keep the sign and move forward
// only if the sign is the first character ever
if (buf[ldx] == '-') {
if (idx >= 0) isNeg = true;
ldx++;
}
else if (buf[ldx] == '+') {
isNeg = false;
ldx++;
}
// Parse the digits
while ((isdigit(buf[ldx]))
and (ldx - sdx <= len)
and (buf[ldx] != 0)) {
// Build the result
result = (result * 10) + (buf[ldx] - '0');
// Move forward
ldx++;
}
// Buffer check
if (ldx - sdx > len) {
// Break because length exceeded
cmdResult = RC_ERROR;
result = 0;
}
// Check if negative
if (isNeg)
result = -result;
}
// Return the result
return result;
}
/**
Parse the default buffer and return the integer value if it fits
in the specified interval or the default value if not.
@param idx index to start with
@param low minimal valid value
@param hgh maximal valid value
@param def default value
@param len maximum to parse, 0 for no limit
@return the integer value or default
*/
int16_t HAYES::getValidInteger(int16_t low, int16_t hgh, int16_t def, uint8_t len) {
return getValidInteger(buf, idx, low, hgh, def, len);
}
/**
Parse the buffer and return the integer value if it fits
in the specified interval or the default value if not.
@param buf the char buffer to parse
@param idx index to start with
@param low minimal valid value
@param hgh maximal valid value
@param def default value
@param len maximum to parse, 0 for no limit
@return the integer value or default
*/
int16_t HAYES::getValidInteger(char* buf, uint8_t idx, int16_t low, int16_t hgh, int16_t def, uint8_t len) {
cmdResult = RC_OK;
// Get the integer value
int16_t res = getInteger(buf, (int8_t)idx, len);
// Check if valid
if ((cmdResult != RC_OK) or (res < low) or (res > hgh)) {
res = def;
cmdResult = RC_ERROR;
}
// Return the result
return res;
}
/**
Parse the default buffer and return one digit integer
@param def default value
@return the integer found or HAYES_NUM_ERROR
*/
int8_t HAYES::getDigit(int8_t def) {
return getDigit(buf, idx, def);
}
/**
Parse the buffer and return one digit integer
@param buf the char buffer to parse
@param idx index to start with
@param def default value
@return the integer found or HAYES_NUM_ERROR
*/
int8_t HAYES::getDigit(char* buf, uint8_t idx, int8_t def) {
// The result is signed integer
int8_t value = def;
cmdResult = RC_OK;
// Check the pointed char
if ((buf[idx] == '\0') or (buf[idx] == ' ') or (buf[idx] == '='))
// If it is the last char ('\0') or space (' ') or equal ('='),
// the value is zero
value = 0;
else if (isdigit(buf[idx]))
// If it is a digit, get the numeric value
value = buf[idx] - '0';
else
cmdResult = RC_ERROR;
// Index up
idx++;
// Return the resulting value
return value;
}
/**
Parse the buffer and return the digit value if it fits
in the specified interval or the default value if not.
@param buf the char buffer to parse
@param idx index to start with
@param low minimal valid value
@param hgh maximal valid value
@param def default value
@return the digit value or default
*/
int8_t HAYES::getValidDigit(char* buf, int8_t idx, int8_t low, int8_t hgh, int8_t def) {
// Get the digit value
int8_t res = getDigit(buf, idx);
// Check if valid
if ((res != HAYES_NUM_ERROR) and
((res < low) or (res > hgh)))
res = def;
// Return the result
return res;
}
/**
Parse the default buffer and return the digit value if it fits
in the specified interval or the default value if not.
@param low minimal valid value
@param hgh maximal valid value
@param def default value
@return the digit value or default
*/
int8_t HAYES::getValidDigit(int8_t low, int8_t hgh, int8_t def) {
// Get the digit value
int8_t res = getDigit(def);
// Check if valid
if ((cmdResult == RC_OK) and ((res < low) or (res > hgh))) {
res = def;
cmdResult = RC_ERROR;
}
// Return the result
return res;
}
/**
Print a command and its value
@param cmd the command
@param mod the command modifier (&,+,*)
@param value the value to print
@param newline print a new line after
*/
void HAYES::cmdPrint(char cmd, char mod, uint8_t value, bool newline) {
// Print the command
if (cmd != '\0') {
if (mod != '\0')
Serial.print(mod);
Serial.print(cmd);
}
// Print the value
Serial.print(value);
// Print the newline, if requested
if (newline) printCRLF();
else Serial.print(F(" "));
// Response code OK
cmdResult = RC_OK;
}
/**
Print a command and its value, no modifier
@param cmd the command
@param value the value to print
@param newline print a new line after
*/
void HAYES::cmdPrint(char cmd, uint8_t value, bool newline) {
cmdPrint(cmd, '\0', value, newline);
}
/**
Print a command and its value, no modifier, get the command
from the default buffer.
@param value the value to print
*/
void HAYES::cmdPrint(uint8_t value) {
// Print the value
cmdPrint(buf[idx - 1], value);
}
/**
Print a S register
@param conf the configuration structure
@param reg the specified S register
@param newline print a new line after
*/
void HAYES::sregPrint(CFG_t *conf, uint8_t reg, bool newline) {
// Print the command
Serial.print(F("S"));
if (reg < 10) Serial.print(F("0"));
Serial.print(reg);
Serial.print(F(":"));
// Print the value
if (conf->sregs[reg] < 100)
Serial.print(F("0"));
if (conf->sregs[reg] < 10)
Serial.print(F("0"));
Serial.print(conf->sregs[reg]);
// Print the newline, if requested
if (newline) printCRLF();
else Serial.print(F(" "));
// Response code OK
cmdResult = RC_OK;
}
/**
Show a configuration profile
@param conf the configuration structure
*/
void HAYES::showProfile(CFG_t *conf) {
// Print the main configuration
cmdPrint('B', conf->compro, false);
cmdPrint('C', conf->txcarr, false);
cmdPrint('E', conf->cmecho, false);
cmdPrint('F', conf->dtecho, false);
cmdPrint('L', conf->spklvl, false);
cmdPrint('M', conf->spkmod, false);
if (not conf->dialpt) Serial.print(F("P "));
cmdPrint('Q', conf->quiet, false);
if (conf->dialpt) Serial.print(F("T "));
cmdPrint('V', conf->verbal, false);
cmdPrint('X', conf->selcpm, false);
printCRLF();
cmdPrint('A', '&', conf->revans, false);
cmdPrint('C', '&', conf->dcdopt, false);
cmdPrint('D', '&', conf->dtropt, false);
cmdPrint('J', '&', conf->jcksel, false);
cmdPrint('K', '&', conf->flwctr, false);
cmdPrint('L', '&', conf->lnetpe, false);
cmdPrint('P', '&', conf->plsrto, false);
cmdPrint('R', '&', conf->rtsopt, false);
cmdPrint('S', '&', conf->dsropt, false);
printCRLF();
// Print the S registers
for (uint8_t reg = 0; reg < 16; reg++) {
sregPrint(conf, reg, false);
if (reg == 0x07 or reg == 0x0F)
printCRLF();
}
}
/**
Process serial I/O in command mode: read the chars into a buffer,
check them, echo them (uppercase), run commands and print results
*/
uint8_t HAYES::doSIO(uint8_t rcRemote) {
char c;
// Check if we just have to print a result
if (rcRemote != RC_NONE) {
// Check for some special cases
if (rcRemote == RC_NO_CARRIER) {
// If NO CARRIER, show the call time
char buf[20];
getUptime(afskModem->callTime(), buf, 20);
printResult(rcRemote, buf);
}
else if (rcRemote == RC_RING and \
cfg->sregs[1] >= cfg->sregs[0] and \
cfg->sregs[0] > 0) {
// Ringing and auto answer is enabled and reached,
// so auto-answer by mangling the input buffer
strcpy_P(buf, PSTR("ATA"));
// Print the remote result first
printResult(rcRemote);
// Print the input buffer
Serial.print(buf);
// Send the newline
printCRLF();
// Parse the line
doCommand();
// Print the command response
printResult(cmdResult);
}
else
// Just print the remote result
printResult(rcRemote);
// Bail out
return;
}
// Read from serial only if there is room in buffer
if (len < MAX_INPUT_SIZE - 1) {
c = Serial.read();
// Check if we have a valid character
if (c >= 0) {
// Uppercase
c = toupper(c);
// Local terminal command mode echo
if (cfg->cmecho)
Serial.write(c);
// Check the character
if (c == cfg->sregs[5] and len > 0)
// Backspace
len--;
else if (len == 0) {
// First character in command buffer
if (c == 'A')
// First character is capital 'A'
sChr = c;
else {
// Anything else
sChr = '\0';
buf[0] = c;
}
// Count up
len++;
}
else if (len == 1) {
// Second character in command buffer
if (sChr == 'A') {
// First character is capital 'A'
if (c == '/') {
// Second character is slash '/', repeat the
// previous command, but first check its length
if (strlen(buf) > 0) {
// Send the newline
printCRLF();
// Print the buffer again
Serial.print(buf);
// Send the newline
printCRLF();
// Parse the line
doCommand();
// Print the command response
printResult(cmdResult);
}
else {
// No previous command
cmdResult = RC_ERROR;
printResult(cmdResult);
}
// Reset the buffer length
len = 0;
}
else {
// Second character is not slash, may be 'T', go to buffer
buf[0] = sChr;
buf[1] = c;
len++;
sChr = '\0';
}
}
else {
// Not starting with 'A', put into buffer, length up
buf[len++] = c;
}
}
else {
// Append to buffer all characters starting with the third
buf[len++] = c;
}
// Check for EOL
if (c == '\r' or c == '\n') {
// Flush the rest
Serial.flush();
// Send the newline
printCRLF();
// Make sure the last char is null
buf[--len] = '\0';
// Check the line length before processing
if (strlen(buf) > 0) {
// Parse the line
doCommand();
// Print the command response
printResult(cmdResult);
}
// Reset the buffer length
len = 0;
}
}
}
}
/**
Run one or multiple commands
*/
void HAYES::doCommand() {
// Reset the command result
cmdResult = RC_ERROR;
// Start by finding the 'AT' sequence
char *pch = strstr_P(buf, PSTR("AT"));
if (pch != NULL) {
// Jump over those two chars from the start
idx = pch - buf + 2;
// New line, just "AT"
if (strlen(buf) <= 2)
cmdResult = RC_OK;
// Process the line
while (idx < strlen(buf)) {
this->dispatch();
if (cmdResult == RC_ERROR)
break;
}
}
}
/**
Print the command result code or message
http://www.messagestick.net/modem/Hayes_Ch1-2.html
@param code the response code
*/
void HAYES::printResult(uint8_t code, char* buf) {
if (code != RC_NONE and cfg->quiet != 1)
if (cfg->verbal) {
printCRLF();
print_P(rcMsg[code]);
if ((buf != NULL) and (cfg->selcpm != 0)) {
Serial.write(' ');
Serial.print(buf);
}
printCRLF();
}
else {
Serial.print(code);
printCRLF();
}
}
/**
AT commands dispatcher: each command detailed
*/
void HAYES::dispatch() {
int8_t option;
// Check the first character, could be a symbol or a letter
switch (buf[idx++]) {
// Skip over some characters
case ' ':
case '\0':
break;
// AT? Print the long help message, only for this syntax
case '?':
if (idx == 3) {
print_P(atHelp, false);
cmdResult = RC_OK;
}
break;
// ATA Answer incoming call
case 'A':
cmdResult = RC_ERROR;
// Phase 0: Clear any ringing counters and signals
afskModem->clearRing();
// Phase 1: Set direction
afskModem->setDirection(ANSWERING);
// Phase 2: Go online
afskModem->setLine(ON);
// Phase 2: Answering carrier on (after a while)
afskModem->setTxCarrier(ON);
// Phase 3: Wait for originating carrier for S7 seconds
if (afskModem->getRxCarrier()) {
// Phase 4: Data mode if carrier found
afskModem->setMode(DATA_MODE);
if (cfg->selcpm == 0)
cmdResult = RC_CONNECT;
else
cmdResult = RC_CONNECT_300;
}
else {
// No carrier, go offline
afskModem->setLine(OFF);
cmdResult = RC_NO_CARRIER;
}
// Disable result codes if ATQ2
if (cfg->quiet == 2) cmdResult = RC_NONE;
break;
// ATB Select Communication Protocol
// ATB15 set ITU V.21 modem type
// ATB16 set Bell103 modem type
case 'B':
if (buf[idx] == '?')
cmdPrint(cfg->compro);
else {
cfg->compro = getValidInteger(0, 31, cfg->compro);
if (cmdResult == RC_OK)
// Change the modem type
switch (cfg->compro) {
case 15: afskModem->setModemType(V_21); break;
case 16: afskModem->setModemType(BELL103); break;
// Default to Bell 103
default:
cfg->compro = 16;
afskModem->setModemType(BELL103);
cmdResult = RC_ERROR;
}
}
break;
// ATC Transmit carrier
// ATC0 disable running TX carrier
// ATC1 enable running TX carrier
case 'C':
if (buf[idx] == '?')
cmdPrint(cfg->txcarr);
else {
cfg->txcarr = getValidDigit(0, 1, cfg->txcarr);
afskModem->setTxCarrier(ON);
}
break;
// ATD Call
// ATDxnnn
// T tone dialing
// P pulse dialing
// nnn phone number
case 'D':
cmdResult = RC_ERROR;
// Phase 1: Get the dial number and parameters
if (getDialNumber(dialNumber, sizeof(dialNumber) - 1)) {
// Phase 2: Set direction
afskModem->setDirection(ORIGINATING, dialReverse);
// Phase 3: Go online
afskModem->setLine(ON);
// Phase 4: Wait for dialtone / busy (NO_DIALTONE / BUSY)
// Phase 5: Dial: DTMF/Pulses
if (afskModem->dial(dialNumber)) {
// Phase 6: Wait for incoming carrier for S7 seconds
if (afskModem->getRxCarrier()) {
// Phase 7: Enable outgoing carrier
afskModem->setTxCarrier(ON);
// Phase 8: Enter data mode or stay in command mode
if (dialCmdMode)
cmdResult = RC_OK;
else {
afskModem->setMode(DATA_MODE);
if (cfg->selcpm == 0)
cmdResult = RC_CONNECT;
else
cmdResult = RC_CONNECT_300;
}
}
else {
// No carrier, go offline
afskModem->setLine(OFF);
cmdResult = RC_NO_CARRIER;
}
}
else
// Interrupted
cmdResult = RC_ERROR;
}
else
// Invalid dial number
cmdResult = RC_ERROR;
break;
// ATE Set local command mode echo
// ATE0 disable local character echo in command mode
// ATE1 enable local character echo in command mode
case 'E':
if (buf[idx] == '?')
cmdPrint(cfg->cmecho);
else
cfg->cmecho = getValidDigit(0, 1, cfg->cmecho);
break;
// ATF Set local data mode echo
// ATF0 Half Duplex, modem echoes characters in data mode
// ATF1 Full Duplex, modem does not echo characters in data mode
case 'F':
if (buf[idx] == '?')
cmdPrint(cfg->dtecho);
else
cfg->dtecho = getValidDigit(0, 1, cfg->dtecho);
break;
// ATH Hook control
// ATH0 force line on hook (offline)
// ATH1 force line off hook (online)
case 'H':
afskModem->setLine(getValidDigit(0, 1, 0));
if (afskModem->getLine() == OFF) {
char buf[20];
uint32_t upt = afskModem->callTime();
if (upt > 0) {
getUptime(upt, buf, 20);
printResult(RC_NO_CARRIER, buf);
// No other response
cmdResult = RC_NONE;
}
}
break;
// ATI Show info
// ATI0 device name and speed
// ATI1 ROM checksum
// ATI2 tests ROM checksum, then reports it
// ATI3 firmware revision
// ATI4 data connection info (modem features)
// ATI5 regional settings
// ATI6 long device description
// ATI7 manufacturer info
case 'I': {
uint8_t rqInfo = 0x00;
// Get the digit value
uint8_t value = getValidDigit(0, 7, 0);
if (cmdResult == RC_OK) {
// Specify the line to display
rqInfo = 0x01 << value;
// 0 Device name and speed
if (rqInfo & 0x01)
print_P(DEVNAME, true);
rqInfo = rqInfo >> 1;
// 1 ROM checksum
if (rqInfo & 0x01)
cmdPrint('\0', cfg->crc8);
rqInfo = rqInfo >> 1;
// 2 Tests ROM checksum, then reports it
if (rqInfo & 0x01) {
struct CFG_t cfgTemp;
cmdResult = profile.read(&cfgTemp) ? RC_OK : RC_ERROR;
}
rqInfo = rqInfo >> 1;
// 3 Firmware revision
if (rqInfo & 0x01) {
print_P(VERSION, true);
print_P(DATE, true);
}
rqInfo = rqInfo >> 1;
// 4 Data connection info
if (rqInfo & 0x01)
print_P(FTRS, true);
rqInfo = rqInfo >> 1;
// 5 Regional Settings
rqInfo = rqInfo >> 1;
// 6 Long device description
if (rqInfo & 0x01)
print_P(DESCRP, true);
rqInfo = rqInfo >> 1;
// 7 Manufacturer info
if (rqInfo & 0x01)
print_P(AUTHOR, true);
rqInfo = rqInfo >> 1;
}
}
break;
// ATL Set speaker volume level
// ATL0 medium volume, -9dB
// ATL1 medium volume, -6dB
// ATL2 medium volume, -3dB
// ATL3 maximum volume, 0dB
case 'L':
if (buf[idx] == '?')
cmdPrint(cfg->spklvl);
else
cfg->spklvl = getValidDigit(0, 3, cfg->spklvl);
break;
// ATM Speaker control
// ATM0 speaker always off
// ATM1 speaker on for TX
// ATM2 speaker on for RX
// ATM3 speaker on for both TX and RX
case 'M':
if (buf[idx] == '?')
cmdPrint(cfg->spkmod);
else
cfg->spkmod = getValidDigit(0, 3, cfg->spkmod);
break;
// ATO Return to data mode
// ATO0 back to data mode, while in command mode
// ATO1 stay in command mode (nonsense)
case 'O':
// Data mode or command mode
afskModem->setMode(getValidDigit(0, 1, 0) == 0 ? DATA_MODE : COMMAND_MODE);
// Return CONNECT if back to data mode
cmdResult = afskModem->getMode() == DATA_MODE ? RC_CONNECT : RC_NONE;
break;
// ATP Use pulse dialing for the next call
case 'P':
cfg->dialpt = OFF;
cmdResult = RC_OK;
break;
// ATQ Quiet Mode
// ATQ0 modem returns result codes
// ATQ1 modem does not return result codes
// ATQ2 modem does not return result codes for ATA command
case 'Q':
if (buf[idx] == '?')
cmdPrint(cfg->quiet);
else
cfg->quiet = getValidDigit(0, 2, cfg->quiet);
break;
// ATS Addresses An S-register
// ATSx=y set value "y" in register "x"
case 'S':
// Get the register number
_sreg = getValidInteger(0, 15, 255, (uint8_t)2);
if (_sreg == 255) {
_sreg = 0;
cmdResult = RC_ERROR;
}
else {
// Move the index to the last local index
idx = ldx;
// Check the next character
if (buf[idx] == '?')
sregPrint(cfg, _sreg, true);
else if (buf[idx] == '=') {
idx++;
cfg->sregs[_sreg] = getValidInteger(0, 255, cfg->sregs[_sreg], (uint8_t)3);
}
}
break;
// ATT Use tone dialing for the next call
case 'T':
cfg->dialpt = ON;
cmdResult = RC_OK;
break;
// ATV Verbose mode
// ATV0 send numeric codes
// ATV1 send text result codes (English)
case 'V':
if (buf[idx] == '?')
cmdPrint(cfg->verbal);
else
cfg->verbal = getValidDigit(0, 1, cfg->verbal);
break;
// ATX Select call progress method
// ATX0 basic result codes: "CONNECT" and "NO CARRIER"
// ATX1 extended result codes: "CONNECT 300" and "NO CARRIER 00:00:00" (call time)
case 'X':
if (buf[idx] == '?')
cmdPrint(cfg->selcpm);
else
cfg->selcpm = getValidDigit(0, 4, cfg->selcpm) == 0 ? 0 : 1;
break;
// ATZ MCU (and modem) reset
case 'Z':
// No more response messages
cmdResult = RC_NONE;
wdt_enable(WDTO_250MS);
// Wait for the prescaller time to expire
// without sending the reset signal
while (true) {};
break;
// Standard '&' extension
case '&':
switch (buf[idx++]) {
// AT&A Reverse answering frequencies
// AT&A0 use receiving modem frequencies on answering
// AT&A1 use originating modem frequencies on answering
case 'A':
if (buf[idx] == '?')
cmdPrint('A', '&', cfg->revans);
else
cfg->revans = getValidDigit(0, 1, cfg->revans);
break;
// AT&C DCD Option
// AT&C0 always keep DCD on (consider RX carrier present)
// AT&C1 DCD follows RX carrier
case 'C':
if (buf[idx] == '?')
cmdPrint('C', '&', cfg->dcdopt);
else
cfg->dcdopt = getValidDigit(0, 1, cfg->dcdopt);
break;
// AT&D DTR Option
// AT&D0 ignore DTR
// AT&D1 return to command mode after losing DTR
// AT&D2 hang up, turn off auto answer, return to command mode after losing DTR
// AT&D3 reset after losing DTR
case 'D':
if (buf[idx] == '?')
cmdPrint('D', '&', cfg->dtropt);
else
cfg->dtropt = getValidDigit(0, 3, cfg->dtropt);
break;
// AT&F Load factory defaults
case 'F':
cmdResult = profile.factory(cfg) ? RC_OK : RC_ERROR;
break;
// AT&J Jack Type Selection (choose OCR2A or OCR2B)
// AT&J0 OCR2A primary, OCR2B secondary
// AT&J1 OCR2A secondary, OCR2B primary
case 'J':
if (buf[idx] == '?')
cmdPrint('J', '&', cfg->jcksel);
else
cfg->jcksel = getValidDigit(0, 1, cfg->jcksel);
break;
// AT&K Flow Control Selection
// AT&K0 disable flow control
// AT&K3 enables CTS/RTS hardware flow control
// AT&K4 enables XON/XOFF software flow control
case 'K':
if (buf[idx] == '?')
cmdPrint('K', '&', cfg->flwctr);
else
cfg->flwctr = getValidDigit(0, 6, cfg->flwctr);
break;
// AT&L Line Type Selection
// AT&L0 Selects PSTN (normal dialup)
// AT&L1 Selects leased line (no dial, no carrier detection)
case 'L':
if (buf[idx] == '?')
cmdPrint('L', '&', cfg->lnetpe);
else
cfg->lnetpe = getValidDigit(0, 1, cfg->lnetpe);
break;
// AT&P Make/Break Ratio for Pulse Dialing
// AT&P0 Selects 39%-61% make/break ratio at 10 pulses per second (NA)
// AT&P1 Selects 33%-67% make/break ratio at 10 pulses per second (EU)
// AT&P2 Selects 39%-61% make/break ratio at 20 pulses per second (NA)
// AT&P3 Selects 33%-67% make/break ratio at 20 pulses per second (EU)
case 'P':
if (buf[idx] == '?')
cmdPrint('P', '&', cfg->plsrto);
else
cfg->plsrto = getValidDigit(0, 3, cfg->plsrto);
break;
// AT&R RTS/CTS Option Selection
// AT&R0 ignore RTS
// AT&R1 read RTS to control outgoing flow
case 'R':
if (buf[idx] == '?')
cmdPrint('R', '&', cfg->rtsopt);
else
cfg->rtsopt = getValidDigit(0, 1, cfg->rtsopt);
break;
// AT&S DSR Option Selection
// AT&S0 DSR line is always on, except when on-hook
// AT&S1 DSR line follows CD
case 'S':
if (buf[idx] == '?')