-
Notifications
You must be signed in to change notification settings - Fork 1
/
SerialCmd.ino
1605 lines (1497 loc) · 42.1 KB
/
SerialCmd.ino
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
/* ===================================================================
* Command Interface Functions
* ===================================================================*/
SerialCommand SCmd; // SerialCommand object
enum commands
{
CMD_HELP=0
,CMD_INFO
,CMD_LOGIN
,CMD_LOGOUT
,CMD_ECHO
,CMD_TERM
,CMD_STATUS
,CMD_UPTIME
,CMD_FS20
,CMD_LED
,CMD_MOTOR
,CMD_MOTORNAME
,CMD_MOTORTIME
,CMD_MOTORTYPE
,CMD_PUSHBUTTON
,CMD_RAIN
,CMD_BACKUP
,CMD_RESTORE
,CMD_FACTORY
,CMD_REBOOT
,CMD_PASSWD
} COMMANDS;
/* ===================================================================
* PROGMEM Strings & Arrays
* ===================================================================*/
const char fstrCR[] PROGMEM = "CR";
const char fstrLF[] PROGMEM = "LF";
const char fstrENABLE[] PROGMEM = "ENABLE";
const char fstrDISABLE[] PROGMEM = "DISABLE";
const char fstrAUTO[] PROGMEM = "AUTO";
const char fstrMANUAL[] PROGMEM = "MANUAL";
const char fstrRESUME[] PROGMEM = "RESUME";
const char fstrFORGET[] PROGMEM = "FORGET";
const char fstrWET[] PROGMEM = "WET";
const char fstrDRY[] PROGMEM = "DRY";
const char fstrWINDOW[] PROGMEM = "WINDOW";
const char fstrJALOUSIE[] PROGMEM = "JALOUSIE";
const char sCmdHELP[] PROGMEM = "HELP";
const char sCmdINFO[] PROGMEM = "INFO";
const char sCmdLOGIN[] PROGMEM = "LOGIN";
const char sCmdLOGOUT[] PROGMEM = "LOGOUT";
const char sCmdECHO[] PROGMEM = "ECHO";
const char sCmdTERM[] PROGMEM = "TERM";
const char sCmdSTATUS[] PROGMEM = "STATUS";
const char sCmdUPTIME[] PROGMEM = "UPTIME";
const char sCmdLED[] PROGMEM = "LED";
const char sCmdMOTOR[] PROGMEM = "MOTOR";
const char sCmdMOTORNAME[] PROGMEM = "MOTORNAME";
const char sCmdMOTORTIME[] PROGMEM = "MOTORTIME";
const char sCmdMOTORTYPE[] PROGMEM = "MOTORTYPE";
const char sCmdFS20[] PROGMEM = "FS20";
const char sCmdPUSHBUTTON[] PROGMEM = "PB";
const char sCmdRAIN[] PROGMEM = "RAIN";
const char sCmdBACKUP[] PROGMEM = "BACKUP";
const char sCmdRESTORE[] PROGMEM = "RESTORE";
const char sCmdFACTORY[] PROGMEM = "FACTORY";
const char sCmdREBOOT[] PROGMEM = "REBOOT";
const char sCmdPASSWD[] PROGMEM = "PASSWD";
#ifndef DEBUG_OUTPUT
const char sParmHELP[] PROGMEM = "[<cmd>|ALL]";
const char sParmINFO[] PROGMEM = "";
const char sParmLOGIN[] PROGMEM = "[<password> [<timeout>]]";
const char sParmLOGOUT[] PROGMEM = "";
const char sParmECHO[] PROGMEM = "[ON|OFF]";
const char sParmTERM[] PROGMEM = "[CR|LF]";
const char sParmSTATUS[] PROGMEM = "[ON|OFF]";
const char sParmUPTIME[] PROGMEM = "[<uptime> [<h>]]";
const char sParmLED[] PROGMEM = "[<len> [<count> [<normal> [<rain>]]]]";
const char sParmMOTOR[] PROGMEM = "[<m> [[T]OPEN|[T]CLOSE|TOOGLE|[GOTO ]<pos>|OFF|SYNC|STATUS]";
const char sParmMOTORNAME[] PROGMEM = "[<m> [<name>]";
const char sParmMOTORTIME[] PROGMEM = "[<m> [<sec> [<overtravel>]]";
const char sParmMOTORTYPE[] PROGMEM = "[<m> [WIN|JAL]";
const char sParmFS20[] PROGMEM = "[<ch> [ON|OFF|PRG]]";
const char sParmPUSHBUTTON[] PROGMEM = "[<b> [ON|OFF]]";
const char sParmRAIN[] PROGMEM = "[AUTO|ENABLE|DISABLE|WET|ON|DRY|OFF|RESUME [<delay>]|FORGET]";
const char sParmBACKUP[] PROGMEM = "";
const char sParmRESTORE[] PROGMEM = "<addr> <data>";
const char sParmFACTORY[] PROGMEM = "";
const char sParmREBOOT[] PROGMEM = "";
const char sParmPASSWD[] PROGMEM = "<current> <new> <retype>";
const char sDescHELP[] PROGMEM = "Print command help";
const char sDescINFO[] PROGMEM = "Print version";
const char sDescLOGIN[] PROGMEM = "Login";
const char sDescLOGOUT[] PROGMEM = "Logout";
const char sDescECHO[] PROGMEM = "Get/Set local echo";
const char sDescTERM[] PROGMEM = "Get/Set command terminator";
const char sDescSTATUS[] PROGMEM = "Get/Set status messages";
const char sDescUPTIME[] PROGMEM = "Get/Set system uptime";
const char sDescLED[] PROGMEM = "Get/Set LED control";
const char sDescMOTOR[] PROGMEM = "Get/Set motor control";
const char sDescMOTORNAME[] PROGMEM = "Get/Set motor name";
const char sDescMOTORTIME[] PROGMEM = "Get/Set motor runtime";
const char sDescMOTORTYPE[] PROGMEM = "Get/Set motor type";
const char sDescFS20[] PROGMEM = "Get/Set FS20 status";
const char sDescPUSHBUTTON[] PROGMEM = "Get/Set pushbutton status";
const char sDescRAIN[] PROGMEM = "Rain sensor function";
const char sDescBACKUP[] PROGMEM = "Create backup";
const char sDescRESTORE[] PROGMEM = "Restore data";
const char sDescFACTORY[] PROGMEM = "Reset to factory defaults";
const char sDescREBOOT[] PROGMEM = "Restart";
const char sDescPASSWD[] PROGMEM = "Set password";
const char sPDescHELP[] PROGMEM = " <cmd> command for getting help\r\n"
" ALL print full help\r\n";
const char sPDescINFO[] PROGMEM = "";
const char sPDescLOGIN[] PROGMEM = " <password> use password to login\r\n"
" <timeout> logout after <timeout> sec";
const char sPDescLOGOUT[] PROGMEM = "";
const char sPDescECHO[] PROGMEM = " ON|OFF set to ON or OFF";
const char sPDescTERM[] PROGMEM = " CR|LF set to CR or LF";
const char sPDescSTATUS[] PROGMEM = " ON|OFF set to ON or OFF";
const char sPDescUPTIME[] PROGMEM = " <uptime> set uptime (in ms since start)\r\n"
" <h> set operation hours";
const char sPDescLED[] PROGMEM = " <len> bit time in ms\r\n"
" <count> bit count [1.." TOSTRING(MAX_LEDPATTERN_BITS) "]\r\n"
" <normal> normal pattern\r\n"
" <rain> rain pattern";
const char sPDescMOTOR[] PROGMEM = " <m> motor number [1.." TOSTRING(MAX_MOTORS) "]\r\n"
" <cmd> can be\r\n"
" OPEN,CLOSE,TOPEN,TCLOSE,TOOGLE\r\n"
" [GOTO] <p> goto position <p> (in %, 0-100)\r\n"
" OFF stop\r\n"
" SYNC set defaults\r\n"
" STATUS get status";
const char sPDescMOTORNAME[] PROGMEM = " <m> motor number [1.." TOSTRING(MAX_MOTORS) "]\r\n"
" <name> motor name";
const char sPDescMOTORTIME[] PROGMEM = " <m> motor number [1.." TOSTRING(MAX_MOTORS) "]\r\n"
" <sec> maximum runtime [s]\r\n"
" <overtravel> overtravel time [s]";
const char sPDescMOTORTYPE[] PROGMEM = " <m> motor number [1.." TOSTRING(MAX_MOTORS) "]\r\n"
" WIN|JAL motor type";
const char sPDescFS20[] PROGMEM = " <ch> FS20 channel number [1.." TOSTRING(IOBITS_CNT) "]\r\n"
" ON|OFF set <ch> ON or OFF\r\n"
" PRG set <ch> into programming mode";
const char sPDescPUSHBUTTON[] PROGMEM = " <b> pushbutton number [1.." TOSTRING(IOBITS_CNT) "]\r\n"
" ON|OFF set <b> ON or OFF";
const char sPDescRAIN[] PROGMEM = " <cmd> can be\r\n"
" AUTO rain detection enabled from input signals\r\n"
" ENABLE enable detection\r\n"
" DISABLE disable detection\r\n"
" WET|ON start raining\r\n"
" DRY|OFF stop raining\r\n"
" RESUME <s> resume window pos\r\n"
" <s> delay (in sec) before resume\r\n"
" FORGET no resume";
const char sPDescBACKUP[] PROGMEM = "";
const char sPDescRESTORE[] PROGMEM = " <addr> 4-digit hex addr\r\n"
" <data> hex data to restore";
const char sPDescFACTORY[] PROGMEM = "";
const char sPDescREBOOT[] PROGMEM = "";
const char sPDescPASSWD[] PROGMEM = " <current> old pw\r\n"
" <new> new pw\r\n"
" <retype> retyped new pw";
const char* const sCmdTable[][5] PROGMEM = {
{sCmdHELP ,sParmHELP ,sDescHELP ,sPDescHELP ,(const char *)0}
,{sCmdINFO ,sParmINFO ,sDescINFO ,sPDescINFO ,(const char *)0}
,{sCmdLOGIN ,sParmLOGIN ,sDescLOGIN ,sPDescLOGIN ,(const char *)0}
,{sCmdLOGOUT ,sParmLOGOUT ,sDescLOGOUT ,sPDescLOGOUT ,(const char *)0}
,{sCmdECHO ,sParmECHO ,sDescECHO ,sPDescECHO ,(const char *)1}
,{sCmdTERM ,sParmTERM ,sDescTERM ,sPDescTERM ,(const char *)1}
,{sCmdSTATUS ,sParmSTATUS ,sDescSTATUS ,sPDescSTATUS ,(const char *)1}
,{sCmdUPTIME ,sParmUPTIME ,sDescUPTIME ,sPDescUPTIME ,(const char *)1}
,{sCmdLED ,sParmLED ,sDescLED ,sPDescLED ,(const char *)1}
,{sCmdMOTOR ,sParmMOTOR ,sDescMOTOR ,sPDescMOTOR ,(const char *)1}
,{sCmdMOTORNAME ,sParmMOTORNAME ,sDescMOTORNAME ,sPDescMOTORNAME,(const char *)1}
,{sCmdMOTORTIME ,sParmMOTORTIME ,sDescMOTORTIME ,sPDescMOTORTIME,(const char *)1}
,{sCmdMOTORTYPE ,sParmMOTORTYPE ,sDescMOTORTYPE ,sPDescMOTORTYPE,(const char *)1}
,{sCmdFS20 ,sParmFS20 ,sDescFS20 ,sPDescFS20 ,(const char *)1}
,{sCmdPUSHBUTTON,sParmPUSHBUTTON,sDescPUSHBUTTON,sPDescPUSHBUTTON,(const char *)1}
,{sCmdRAIN ,sParmRAIN ,sDescRAIN ,sPDescRAIN ,(const char *)1}
,{sCmdBACKUP ,sParmBACKUP ,sDescBACKUP ,sPDescBACKUP ,(const char *)1}
,{sCmdRESTORE ,sParmRESTORE ,sDescRESTORE ,sPDescRESTORE ,(const char *)1}
,{sCmdFACTORY ,sParmFACTORY ,sDescFACTORY ,sPDescFACTORY ,(const char *)1}
,{sCmdREBOOT ,sParmREBOOT ,sDescREBOOT ,sPDescREBOOT ,(const char *)1}
,{sCmdPASSWD ,sParmPASSWD ,sDescPASSWD ,sPDescPASSWD ,(const char *)1}
};
const void (*cmdTable[])() = {
(const void (*)())cmdHelp
,(const void (*)())cmdInfo
,(const void (*)())cmdLogin
,(const void (*)())cmdLogout
,(const void (*)())cmdEcho
,(const void (*)())cmdTerm
,(const void (*)())cmdStatus
,(const void (*)())cmdUptime
,(const void (*)())cmdLed
,(const void (*)())cmdMotor
,(const void (*)())cmdMotorName
,(const void (*)())cmdMotorTime
,(const void (*)())cmdMotorType
,(const void (*)())cmdFS20
,(const void (*)())cmdPushButton
,(const void (*)())cmdRain
,(const void (*)())cmdBackup
,(const void (*)())cmdRestore
,(const void (*)())cmdFactory
,(const void (*)())cmdReboot
,(const void (*)())cmdPassword
};
#endif
/* ===================================================================
* Helper Function
* ===================================================================*/
void setupSerialCommand(void)
{
#ifndef DEBUG_OUTPUT
// Setup callbacks for SerialCommand commands
for(byte i=0; i<sizeof(sCmdTable)/sizeof(sCmdTable[0]); i++) {
SCmd.addCommand( (char*)pgm_read_word(&(sCmdTable[i][PRINT_CMD])), (void (*)())cmdTable[i] );
}
#else
// do it step by step without help texts to save all the space for program memory
SCmd.addCommand( sCmdHELP, cmdHelp );
SCmd.addCommand( sCmdINFO, cmdInfo );
SCmd.addCommand( sCmdLOGIN, cmdLogin );
SCmd.addCommand( sCmdLOGOUT, cmdLogout );
SCmd.addCommand( sCmdECHO, cmdEcho );
SCmd.addCommand( sCmdTERM, cmdTerm );
SCmd.addCommand( sCmdSTATUS, cmdStatus );
SCmd.addCommand( sCmdUPTIME, cmdUptime );
SCmd.addCommand( sCmdLED, cmdLed );
SCmd.addCommand( sCmdMOTOR, cmdMotor );
SCmd.addCommand( sCmdMOTORNAME, cmdMotorName );
SCmd.addCommand( sCmdMOTORTIME, cmdMotorTime );
SCmd.addCommand( sCmdMOTORTYPE, cmdMotorType );
SCmd.addCommand( sCmdFS20, cmdFS20 );
SCmd.addCommand( sCmdPUSHBUTTON,cmdPushButton );
SCmd.addCommand( sCmdRAIN, cmdRain );
SCmd.addCommand( sCmdBACKUP, cmdBackup );
SCmd.addCommand( sCmdRESTORE, cmdRestore );
SCmd.addCommand( sCmdFACTORY, cmdFactory );
SCmd.addCommand( sCmdREBOOT, cmdReboot );
SCmd.addCommand( sCmdPASSWD, cmdPassword );
#endif
SCmd.addDefaultHandler(unrecognized); // Handler for command that isn't matched (says "What?")
SCmd.setEcho(eeprom.Echo);
SCmd.setTerm(eeprom.Term);
}
void processSerialCommand(void)
{
SCmd.readSerial(); // We don't do much, just process serial commands
watchdogReset();
}
void cryptPassword(char pw[16], unsigned long key[4], CRYPT_MODE mode)
{
unsigned long v[2];
Xtea x(key);
#ifdef DEBUG_PASSWD
SerialTimePrintf(F("cryptPassword - Input:\r\n "));
for( int i=0; i<16; i++) {
SerialPrintf(F("0x%02x "), (byte)pw[i]);
}
printCRLF();
#endif
for (int i=0; i<4; i+=2) {
memcpy(&v[0], pw+(4*i) , 4);
memcpy(&v[1], pw+(4*i+4), 4);
if ( mode== ENCRYPT ) {
x.encrypt( v );
}
else {
x.decrypt( v );
}
memcpy(pw+(4*i) ,&v[0], 4);
memcpy(pw+(4*i+4),&v[1], 4);
}
#ifdef DEBUG_PASSWD
SerialTimePrintf(F("cryptPassword - Result:\r\n "));
for( int i=0; i<16; i++) {
SerialPrintf(F("0x%02x "), (byte)pw[i]);
}
printCRLF();
#endif
}
bool cmdGetPassword()
{
char buffer[sizeof(eeprom.Password)];
memset(buffer, 0, sizeof(buffer));
SerialGetString(buffer, sizeof(buffer), eeprom.Term, eeprom.Echo, true);
printCRLF();
#ifdef DEBUG_PASSWD
SerialTimePrintfln(F("Entered password: %s"), buffer);
#endif
cryptPassword(buffer, eeprom.EncryptKey, ENCRYPT);
#ifdef DEBUG_PASSWD
SerialTimePrintf(F("Stored encrypted password:\r\n "));
for( int i=0; i<16; i++) {
SerialPrintf(F("0x%02x "), (byte)eeprom.Password[i]);
}
printCRLF();
#endif
if ( memcmp(eeprom.Password, buffer, sizeof(buffer)-1 )==0 ) {
return true;
}
return false;
}
void cmdOK(void)
{
Serial.println(F("OK"));
}
void cmdError(const __FlashStringHelper *str)
{
Serial.print(F("ERROR: "));
Serial.println(str);
}
void cmdErrorParameter(const __FlashStringHelper *err)
{
Serial.print(F("ERROR: Parameter error (use "));
Serial.print(err);
Serial.println(")");
}
void cmdErrorOutOfRange(const __FlashStringHelper *str)
{
Serial.print(F("ERROR: <"));
Serial.print(str);
Serial.println(F("> out of range"));
}
void cmdErrorNotLoggedIn(void)
{
cmdError(F("Login first"));
}
void unrecognized(char *token)
{
SerialPrintfln(F("ERROR: Unknown command '%s', try HELP"), token);
}
/* ===================================================================
* Command interface function implementation
* ===================================================================*/
/* HELP [cmd]
* Command help
*/
void cmdHelp()
{
#ifdef DEBUG_OUTPUT
Serial.print(F("No HELP available"));
#else
char *arg;
arg = SCmd.next();
if ( (arg == NULL) || (strcasecmp_P(arg, PSTR("ALL"))==0) ) {
helpHeader();
for(byte i=0; i<sizeof(sCmdTable)/sizeof(sCmdTable[0]); i++) {
printHelp(i, PRINT_CMD, " ");
printHelp(i, PRINT_PARM, "\r\n ");
printHelp(i, PRINT_DESC, "\r\n");
if ( arg != NULL ) {
printHelp(i, PRINT_PDESC, "\r\n");
printCRLF();
}
}
if ( arg == NULL ) {
Serial.println(F("\r\nuse 'HELP cmd' for more"));
}
}
else {
bool cmdFound=false;
for(byte i=0; !cmdFound && i<sizeof(sCmdTable)/sizeof(sCmdTable[0]); i++) {
if ( strcasecmp_P(arg, (char *)pgm_read_word(&(sCmdTable[i][PRINT_CMD])))==0 ) {
printHelp(i, PRINT_CMD, " ");
printHelp(i, PRINT_PARM, "\r\n ");
printHelp(i, PRINT_DESC, "\r\n");
printHelp(i, PRINT_PDESC, "\r\n");
cmdFound=true;
}
}
if ( !cmdFound ) {
SerialPrintfln(F("ERROR: Unknown cmd '%s', use HELP"), arg);
return;
}
}
#endif
watchdogReset();
cmdOK();
}
#ifndef DEBUG_OUTPUT
int printHelp(byte cmd, PRINTCMDTYPE type, const char *postfix)
{
if ( cmdUnlocked || pgm_read_word(&(sCmdTable[cmd][PRINT_PROTECT])) == 0 ) {
char *p = NULL;
switch ( type ) {
case PRINT_CMD:
case PRINT_PARM:
case PRINT_DESC:
case PRINT_PDESC:
p = pgm_read_word(&(sCmdTable[cmd][type]));
break;
default:
break;
}
if ( p!=NULL ) {
unsigned char c;
if ( type!=PRINT_PDESC || pgm_read_byte(p) != 0 ) {
while( (c = pgm_read_byte(p++)) != 0 ) {
Serial.write(c);
}
if ( postfix!=NULL ) {
Serial.print(postfix);
}
}
return strlen_P(p);
}
return 0;
}
return 0;
}
void helpHeader()
{
printCRLF();
Serial.print(PROGRAM);
Serial.println(" COMMAND LIST");
printCRLF();
//~ SerialPrintfln(F("\r\n"
//~ "%S COMMAND LIST\r\n"
//~ "\r\n"), PROGRAM);
}
#endif
/* INFO
* Print version
*/
void cmdInfo()
{
printProgramInfo(false);
if ( cmdUnlocked ) {
struct MYEEPROMHEADER header;
cmdUptimeStatus();
sendStatus(true,SYSTEM,F("EEPROM ADDR %d"), eepromStartAddr());
// Read out eeprom write counter
EEPROM.get(eepromStartAddr()-sizeof(header.WriteCount), header.WriteCount);
sendStatus(true,SYSTEM,F("EEPROM WEAR-LEVELING %d%%"), (header.WriteCount*100) / EEPROM_WEARLEVELING_WRITECNT);
}
cmdOK();
}
/* LOGIN [<password> [<timeout>]]
* Login
*/
void cmdLogin()
{
char *arg;
arg = SCmd.next();
if (arg == NULL) {
cmdLoginStatus(true);
cmdOK();
}
else {
// password given
char password[sizeof(eeprom.Password)];
memset(password, 0, sizeof(password));
strcpy(password, arg);
cryptPassword(password, eeprom.EncryptKey, ENCRYPT);
if ( memcmp(eeprom.Password, password, sizeof(password)-1 )==0 ) {
// Password check ok, unlock cmd interface
cmdUnlocked = true;
// set login timeout to stored value
cmdLoginTimeout = eeprom.LoginTimeout;
// get optional new timeout value
arg = SCmd.next();
if (arg != NULL) {
int timeout=atoi(arg);
if ( timeout<0 ) {
cmdErrorOutOfRange(F("timeout"));
}
else {
// store new timeout value
eeprom.LoginTimeout = timeout;
eepromWriteVars();
// set login timeout to stored value
cmdLoginTimeout = timeout;
cmdOK();
}
}
else {
cmdOK();
}
}
else {
cmdError(F("Wrong passcode"));
}
}
}
void cmdLoginStatus(bool send)
{
if ( eeprom.LoginTimeout && cmdUnlocked ) {
sendStatus(send,SYSTEM,F("LOGGED IN (%d s)"),cmdLoginTimeout);
}
else {
sendStatus(send,SYSTEM,F("LOGGED %S"),cmdUnlocked ? F("IN"):F("OUT"));
}
}
/* LOGOUT
* Logout
*/
void cmdLogout()
{
cmdUnlocked = false;
cmdOK();
}
/* ECHO [ON|OFF]
* Local echo
*/
void cmdEcho(void)
{
if ( cmdUnlocked ) {
char *arg;
arg = SCmd.next();
if (arg == NULL) {
sendStatus(true,SYSTEM,F("ECHO %S"), eeprom.Echo?fstrON:fstrOFF);
cmdOK();
}
else if ( strnicmp_P(arg, fstrON,2)==0 ) {
eeprom.Echo = true;
SCmd.setEcho(eeprom.Echo);
eepromWriteVars();
cmdOK();
}
else if ( strnicmp_P(arg, fstrOFF,2)==0 ) {
eeprom.Echo = false;
SCmd.setEcho(eeprom.Echo);
eepromWriteVars();
cmdOK();
}
else {
cmdErrorParameter(F("'ON' or 'OFF'"));
}
}
else {
cmdErrorNotLoggedIn();
}
}
/* TERM [CR|LF]
* Command terminator
*/
void cmdTerm(void)
{
if ( cmdUnlocked ) {
char *arg;
arg = SCmd.next();
if (arg == NULL) {
sendStatus(true,SYSTEM,F("TERM %S"),eeprom.Term=='\r'?fstrCR:fstrLF);
cmdOK();
}
else if ( strnicmp_P(arg, fstrCR,2)==0 ) {
eeprom.Term = '\r';
SCmd.setTerm(eeprom.Term);
eepromWriteVars();
cmdOK();
}
else if ( strnicmp_P(arg, fstrLF,2)==0 ) {
eeprom.Term = '\n';
SCmd.setTerm(eeprom.Term);
eepromWriteVars();
cmdOK();
}
else {
cmdErrorParameter(F("'CR' or 'LF'"));
}
}
else {
cmdErrorNotLoggedIn();
}
}
/* STATUS [ON|OFF]
* Send status messages
*/
void cmdStatus()
{
if ( cmdUnlocked ) {
char *arg;
arg = SCmd.next();
if (arg == NULL) {
sendStatus(true,SYSTEM,F("STATUS %S"), eeprom.SendStatus?fstrON:fstrOFF);
cmdOK();
}
else {
if ( strnicmp_P(arg, fstrON,2)==0 ) {
eeprom.SendStatus = true;
eepromWriteVars();
cmdOK();
}
else if ( strnicmp_P(arg, fstrOFF, 2)==0 ) {
eeprom.SendStatus = false;
eepromWriteVars();
cmdOK();
}
else {
cmdErrorParameter(F("'ON' or 'OFF'"));
}
}
}
else {
cmdErrorNotLoggedIn();
}
}
/* UPTIME [<uptime> [<h>]]
* System uptime
* <uptime> Set uptime (in ms since start)
* <h> Set operation hours (in h)
*/
void cmdUptime()
{
if ( cmdUnlocked ) {
char *arg;
arg = SCmd.next();
if (arg != NULL) {
cli(); //halt the interrupts
timer0_millis = (unsigned long)atol(arg);
sei(); //re-enable the interrupts
operationHours(true);
arg = SCmd.next();
if (arg != NULL) {
eeprom.OperatingHours=atol(arg);
eepromWriteVars();
}
}
ledTimer = millis() + (TIMER)eeprom.LEDBitLenght;
cmdUptimeStatus();
cmdOK();
}
else {
cmdErrorNotLoggedIn();
}
}
void cmdUptimeStatus(void)
{
sendStatus(true,SYSTEM,F("UPTIME %s"), getSystemUptime());
sendStatus(true,SYSTEM,F("OPERATION %lu h"), eeprom.OperatingHours);
}
/* LED [<len> [<count> [<normal> [<rain>]]]]
* LED control
* <len> pattern bit time in ms
* <count> pattern bit count (max 32)
* <normal> normal blink pattern
* <rain> rain blink pattern
*/
void cmdLed()
{
if ( cmdUnlocked ) {
LEDPATTERN LEDPatternNormal;
LEDPATTERN LEDPatternRain;
byte LEDBitCount;
WORD LEDBitLenght;
char *argLEDPatternNormal;
char *argLEDPatternRain;
char *argLEDBitCount;
char *argLEDBitLenght;
LEDBitLenght = eeprom.LEDBitLenght;
LEDBitCount = eeprom.LEDBitCount;
LEDPatternNormal = eeprom.LEDPatternNormal;
LEDPatternRain = eeprom.LEDPatternRain;
argLEDBitLenght = SCmd.next();
argLEDBitCount = SCmd.next();
argLEDPatternNormal = SCmd.next();
argLEDPatternRain = SCmd.next();
if ( argLEDBitLenght == NULL ) {
sendStatus(true,SYSTEM,F("LED %d %d 0x%08lx 0x%08lx")
,eeprom.LEDBitLenght
,eeprom.LEDBitCount
,eeprom.LEDPatternNormal
,eeprom.LEDPatternRain
);
cmdOK();
}
else {
LEDBitLenght = (WORD)atoi(argLEDBitLenght);
if ( argLEDBitCount!=NULL ) {
LEDBitCount = (byte)atoi(argLEDBitCount);
if ( LEDBitCount<1 || LEDBitCount>MAX_LEDPATTERN_BITS ) {
cmdErrorOutOfRange(F("bits"));
return;
}
}
if ( argLEDPatternNormal!=NULL ) {
LEDPatternNormal = (LEDPATTERN)strtoul(argLEDPatternNormal, NULL, 0);
if ( errno == ERANGE ) {
cmdErrorOutOfRange(F("normal"));
return;
}
}
if ( argLEDBitCount!=NULL ) {
LEDPatternRain = (LEDPATTERN)strtoul(argLEDPatternRain, NULL, 0);
if ( errno == ERANGE ) {
cmdErrorOutOfRange(F("rain"));
return;
}
}
eeprom.LEDPatternNormal = LEDPatternNormal;
eeprom.LEDPatternRain = LEDPatternRain;
eeprom.LEDBitCount = LEDBitCount;
eeprom.LEDBitLenght = LEDBitLenght;
eepromWriteVars();
cmdOK();
}
}
else {
cmdErrorNotLoggedIn();
}
}
/* MOTOR [<m> [[T]OPEN|[T]CLOSE|TOOGLE|[GOTO ]<pos>|OFF|SYNC|STATUS]
* Motor control
* <m> Motor number [1..MAX_MOTORS]
* <cmd> can be
* OPEN Motor in OPEN direction
* CLOSE Motor in CLOSE direction
* TOPEN Toogle OPEN direction
* TCLOSE Toogle CLOSE direction
* TOOGLE Toogle direction
* [GOTO] <p> Goto position <p> (in %, 0-100)
* OFF Stop motor
* SYNC Set motor in a default defined state
* STATUS Return the current status
*/
void cmdMotor()
{
if ( cmdUnlocked ) {
int motor;
char *arg;
arg = SCmd.next();
if (arg == NULL) {
for(motor=0; motor<MAX_MOTORS; motor++) {
sendMotorStatus(true,motor);
}
cmdOK();
}
else {
motor=atoi(arg)-1;
if ( motor<0 || motor>=MAX_MOTORS ) {
cmdErrorOutOfRange(F("m"));
}
else {
arg = SCmd.next();
if (arg != NULL) {
if ( strnicmp_P(arg, PSTR("OP"),2)==0 ) {
if ( getMotorDirection(motor)<MOTOR_OPEN ) {
setMotorDirection(motor, MOTOR_OPEN);
}
cmdOK();
}
else if ( strnicmp_P(arg, PSTR("CL"),2)==0 ) {
if ( getMotorDirection(motor)>MOTOR_CLOSE ) {
setMotorDirection(motor, MOTOR_CLOSE);
}
cmdOK();
}
else if ( strnicmp_P(arg, fstrOFF, 2)==0 ) {
if ( getMotorDirection(motor)!=MOTOR_OFF ) {
setMotorDirection(motor, MOTOR_OFF);
}
cmdOK();
}
else if ( strnicmp_P(arg, PSTR("GO"),2)==0 ) {
arg = SCmd.next();
int percent=atoi(arg);
if ( percent<0 || percent>100 ) {
cmdErrorOutOfRange(F("p"));
}
else {
setMotorPosition(motor, percent);
cmdOK();
}
}
else if ( strnicmp_P(arg, PSTR("TOP"),3)==0 ) {
if ( getMotorDirection(motor)==MOTOR_OFF || getMotorDirection(motor)<=MOTOR_CLOSE ) {
setMotorDirection(motor, MOTOR_OPEN);
}
else {
setMotorDirection(motor, MOTOR_OFF);
}
cmdOK();
}
else if ( strnicmp_P(arg, PSTR("TCL"),3)==0 ) {
if ( getMotorDirection(motor)==MOTOR_OFF || getMotorDirection(motor)>=MOTOR_OPEN ) {
setMotorDirection(motor, MOTOR_CLOSE);
}
else {
setMotorDirection(motor, MOTOR_OFF);
}
cmdOK();
}
else if ( strnicmp_P(arg, PSTR("TOG"),3)==0 ) {
if ( getMotorDirection(motor)==MOTOR_OFF ) {
setMotorDirection(motor, MOTOR_OPEN);
}
else if ( getMotorDirection(motor)>MOTOR_CLOSE ) {
setMotorDirection(motor, MOTOR_CLOSE);
}
else if ( getMotorDirection(motor)<MOTOR_OPEN ) {
setMotorDirection(motor, MOTOR_OPEN);
}
cmdOK();
}
else if ( strnicmp_P(arg, PSTR("SY"),2)==0 ) {
setMotorDirection(motor, MOTOR_CLOSE);
cmdOK();
}
else if ( (atoi(arg)>=0 && atoi(arg)<=100) ) {
setMotorPosition(motor, (byte)atoi(arg) );
cmdOK();
}
else {
cmdErrorParameter(F("'HELP MOTOR' for more info"));
}
}
if (arg == NULL || strnicmp_P(arg, PSTR("ST"),2)==0 ) {
sendMotorStatus(true,motor);
cmdOK();
}
}
}
}
else {
cmdErrorNotLoggedIn();
}
}
/* MOTORTIME [<m> [<runtime> [<overtravel>]]
* Motor runtime
* <m> Motor number [1..8]
* <runtime> Maximum runtime [ms]
* <overtravel> Overtravel time [ms]
*/
void cmdMotorTime()
{
if ( cmdUnlocked ) {
int motor;
DWORD runtime;
char *arg;
arg = SCmd.next();
if (arg == NULL) {
for(motor=0; motor<MAX_MOTORS; motor++) {
cmdMotorTimePrintStatus(motor);
}
cmdOK();
}
else {
motor=atoi(arg)-1;
if ( motor<0 || motor>=MAX_MOTORS ) {
cmdErrorOutOfRange(F("m"));
}
else {
arg = SCmd.next();
if (arg == NULL) {
// Status
cmdMotorTimePrintStatus(motor);
cmdOK();
}
else {
// Set new runtime value
runtime = strtoul(arg, NULL, 0);
if ( runtime<MOTOR_MINRUNTIME || runtime>MOTOR_MAXRUNTIME ) {
cmdErrorOutOfRange(F("runtime"));
}
else {
eeprom.MaxRuntime[motor] = runtime;
eepromWriteVars();
arg = SCmd.next();
if (arg == NULL) {
cmdMotorTimePrintStatus(motor);
cmdOK();
}
else {
runtime = strtoul(arg, NULL, 0);
if ( runtime<MOTOR_MINRUNTIME || runtime>MOTOR_MAXRUNTIME ) {
cmdErrorOutOfRange(F("overtravel"));
}
else {
eeprom.OvertravelTime[motor] = runtime;
eepromWriteVars();
cmdMotorTimePrintStatus(motor);
cmdOK();
}
}
}
}
}
}
}
else {
cmdErrorNotLoggedIn();
}
}
void cmdMotorTimePrintStatus(int motor)
{
sendStatus(true,MOTOR,F("%02d TIME %6ld %6ld (%s)")
,(int)(motor+1)
,eeprom.MaxRuntime[motor]
,eeprom.OvertravelTime[motor]
,(char *)eeprom.MotorName[motor]
);
}
/* MOTORNAME [<m> [<name>]
* Motor names
* <m> Motor number [1..MAX_MOTORS]
* <name Motor name
*/
void cmdMotorName()
{
if ( cmdUnlocked ) {
int motor;
char *arg;
arg = SCmd.next();
if (arg == NULL) {
for(motor=0; motor<MAX_MOTORS; motor++) {
cmdMotorNamePrintStatus(motor);
}
cmdOK();
}
else {
motor=atoi(arg)-1;
if ( motor<0 || motor>=MAX_MOTORS ) {
cmdErrorOutOfRange(F("m"));
}
else {
arg = SCmd.next();
if (arg != NULL) {
char sName[MAX_NAMELEN];
#ifdef DEBUG_OUTPUT
SerialTimePrintfln(F("cmdMotorName arg=%s"), arg);
SerialTimePrintfln(F("cmdMotorName sizeof(eeprom.MotorName[motor])=%d"), sizeof(eeprom.MotorName[motor]) );
SerialTimePrintfln(F("cmdMotorName &eeprom.MotorName[motor]=%p"), &eeprom.MotorName[motor] );
#endif
strncpy(sName, arg, sizeof(sName)-1);
strReplaceChar(sName, '_', ' ');
strcpy(eeprom.MotorName[motor], sName);
eeprom.MotorName[motor][sizeof(eeprom.MotorName[motor])-1]='\0';
eepromWriteVars();
cmdOK();
}
else {
cmdMotorNamePrintStatus(motor);