-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcordance.c
1387 lines (1221 loc) · 31.7 KB
/
concordance.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
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
/*
* vi: formatoptions+=tc textwidth=80 tabstop=8 shiftwidth=8 noexpandtab:
*
* $Id: concordance.c,v 1.42 2010/08/28 16:26:27 jaymzh Exp $
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* (C) Copyright Kevin Timmerman 2007
* (C) Copyright Phil Dibowitz 2007
*/
/* Platform-agnostic includes */
#define _SVID_SOURCE
#include "libconcord.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
/* Windows includes*/
#include "win/getopt/getopt.h"
#include <conio.h>
#include <winsock.h>
#define strcasecmp stricmp
#define strncasecmp strnicmp
#define sleep(x) Sleep((x) * 1000)
#define strdup _strdup
/*
* Windows, in it's infinite awesomeness doesn't include POSIX things
* like basename. This little hack will work for non-unicode filenames.
* Thanks to Marco Bleich.
*/
char* basename(char* file_name)
{
char* _basename = strrchr(file_name, '\\');
return _basename ? _basename+1 : file_name;
}
HANDLE con;
/*
* (see below) Windows does not need this, since _getch already
* does what we need - just make set_canon do nothing
* Note that _getch returns '\r' when user hits the <Enter> key
*/
int set_canon(int flag)
{
return(1);
}
#define read_key _getch
#define ENTER_KEY '\r'
#else
/* NON-Windows */
#include <getopt.h>
#include <strings.h>
#include <libgen.h>
#include <unistd.h>
#include <termios.h>
/*
* set_canon in LINUX modifies stdin such that getchar behaves like
* _getch in Windows, i.e. the next key is returned immediately.
* Note that getchar returns '\n' when user hits the <Enter> key
*/
int set_canon(int flag)
{
struct termios t;
tcgetattr(0, &t);
if (flag) {
t.c_lflag |= ICANON;
} else {
t.c_lflag &= ~ICANON;
}
tcsetattr(0, TCSANOW, &t);
return(1);
}
/* Thus, we can define: */
#define read_key getchar
#define ENTER_KEY '\n'
#endif
#define MAX_WAIT_FOR_BOOT 5
#define WAIT_FOR_BOOT_SLEEP 5
#if defined(__APPLE__) || defined(__FreeBSD__)
#include <libgen.h>
#endif
#define DEFAULT_CONFIG_FILENAME "Update.EZHex"
#define DEFAULT_CONFIG_FILENAME_BIN "update.bin"
#define DEFAULT_FW_FILENAME "firmware.EZUp"
#define DEFAULT_FW_FILENAME_BIN "firmware.bin"
#define DEFAULT_SAFE_FILENAME "safe.bin"
const char * const VERSION = "0.23";
struct options_t {
int binary;
int verbose;
int noweb;
int direct;
int noreset;
int force;
};
enum {
MODE_UNSET,
MODE_CONNECTIVITY,
MODE_DUMP_CONFIG,
MODE_WRITE_CONFIG,
MODE_DUMP_FIRMWARE,
MODE_WRITE_FIRMWARE,
MODE_DUMP_SAFEMODE,
MODE_HELP,
MODE_LEARN_IR,
MODE_RESET,
MODE_GET_TIME,
MODE_SET_TIME,
MODE_PRINT_INFO,
MODE_VERSION
};
/*
* Start callbacks
*/
void cb_print_percent_status(uint32_t count, uint32_t curr, uint32_t total,
void *arg)
{
int is_bytes;
#ifdef WIN32
CONSOLE_SCREEN_BUFFER_INFO sbi;
#endif
if (count != 0) {
#ifdef WIN32
GetConsoleScreenBufferInfo(con, &sbi);
sbi.dwCursorPosition.X -= 14;
if (sbi.dwCursorPosition.X < 0) {
sbi.dwCursorPosition.X = 0;
}
SetConsoleCursorPosition(con, sbi.dwCursorPosition);
#else
printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
#endif
}
is_bytes = 0;
if (arg) {
is_bytes = (int)(arg);
}
if (is_bytes) {
printf("%3i%% %4i KiB", curr*100/total, curr>>10);
} else {
printf("%3i%% ", curr*100/total);
}
fflush(stdout);
}
void print_ir_burst(uint32_t length)
{
if (length < 250) {
printf("|");
} else if (length < 1000) {
printf("#");
} else {
printf("##");
}
}
void print_ir_space(uint32_t length)
{
if (length < 250) {
printf(".");
} else if (length < 1000) {
printf("_");
} else if (length < 10000) {
printf("__");
} else {
printf("\n");
}
}
void print_received_ir_signal(uint32_t carrier_clock, uint32_t *ir_signal,
uint32_t ir_signal_length, struct options_t *options)
{
uint32_t index;
printf("\nASCII-graph of received IR signal:\n");
for (index=0; index < ir_signal_length; index += 2){
print_ir_burst(ir_signal[index]);
print_ir_space(ir_signal[index+1]);
}
printf("\n");
printf("Carrier clock : %u Hz\n", carrier_clock);
printf("Total mark/space pairs : %u\n\n", ir_signal_length/2);
#ifdef _DEBUG
/*
* full dump of new IR signal:
*/
for (index=0; index < ir_signal_length; index += 2){
printf("\tP:%6u\tS:%6u\n", ir_signal[index],
ir_signal[index+1]);
}
#endif
}
char get_cmd(char *prompt, char *allowed, char def) {
char result = 0;
char got_key;
uint32_t index;
set_canon(0);
while ( result == 0 ) {
printf("%s (%c)?", prompt, def);
got_key = read_key();
printf("\n");
if (got_key == ENTER_KEY) {
result = def;
} else {
for (index = 0; index < strlen(allowed); index++) {
if (allowed[index] == got_key) {
result = got_key;
break;
}
}
}
}
set_canon(1);
return result;
}
int learn_ir_commands(uint8_t *data, uint32_t size, struct options_t *options)
{
int err = 0;
uint32_t carrier_clock = 0;
uint32_t *ir_signal = NULL;
uint32_t ir_signal_length = 0;
uint32_t index = 0;
char **key_names;
uint32_t key_names_length = 0;
char *post_string = NULL;
char user_cmd;
err = get_key_names(data, size, &key_names, &key_names_length);
if ((err != 0) || (key_names_length == 0)) { return err; }
printf("Received file contains %u key names to be learned.\n",
key_names_length);
while (1) {
if (index >= key_names_length) {
index--;
printf("Last key in list!\n");
}
printf("\nKey name : <%s> : \n", key_names[index]);
user_cmd = get_cmd(
"[L]earn, [N]ext, [P]revious, [Q]uit",
"LlHhNnPpQq", 'L');
err = -1;
/* have no code yet */
switch (user_cmd) {
case 'L':
case 'l':
/* learn from remote: */
printf("press corresponding key ");
printf("on original remote within 5 sec:\n");
printf("Learning IR signal: ");
err = learn_from_remote(&carrier_clock,
&ir_signal, &ir_signal_length,
cb_print_percent_status, NULL);
if (err == 0) {
printf(" done\n");
}
break;
case 'P':
case 'p':
if (index > 0) {
index--;
} else {
printf("First key in list!\n");
}
break;
case 'N':
case 'n':
index++;
break;
default:
break;
}
if ( err == 0 ) {
/* have new IR code: */
if ((*options).verbose) {
print_received_ir_signal(carrier_clock,
ir_signal, ir_signal_length, options);
}
err = encode_for_posting(carrier_clock, ir_signal,
ir_signal_length, &post_string);
/* done with learned signal, free memory: */
delete_ir_signal(ir_signal);
}
if ( err == 0 ) {
/* have successfully encoded new code: */
#ifdef _DEBUG
if ((*options).verbose) {
printf("%s\n\n", post_string );
}
#endif
if (!(*options).noweb) {
user_cmd = get_cmd(
"[U]pload new code, [R]etry same key, [N]ext key, [Q]uit",
"UuRrNnQq", 'U');
} else {
/* no upload: just skip to next key */
user_cmd = 'N';
}
switch (user_cmd) {
case 'U':
case 'u':
printf("Upload to website: ");
cb_print_percent_status(
0, 0, 1, NULL);
err = post_new_code(data, size,
key_names[index], post_string);
if ( err == 0 ) {
cb_print_percent_status(
1, 1, 1, NULL);
printf(" done\n");
index++;
} else {
printf(" failed\n");
}
break;
case 'N':
case 'n':
index++;
break;
default:
break;
}
/* done, free memory */
delete_encoded_signal(post_string);
} else {
if (err > 0) {
fprintf(stderr, "\nERROR:Problemreceiving IR");
fprintf(stderr, " signal:\n\t%s \n",
lc_strerror(err));
}
}
if (user_cmd == 'Q' || user_cmd == 'q') {
break;
}
}
/* done, free memory */
delete_key_names(key_names, key_names_length);
return 0;
}
/*
* Start helper functions
*/
void direct_warning()
{
printf("WARNING: You have requested direct mode. This only affects\n");
printf("\t firmware updates. To do this safely you MUST be in\n");
printf("\t SAFEMODE! This is only for those devices where we\n");
printf("\t don't yet support live firmware updates. See the docs\n");
printf("\t for information on how to boot into safemode.\n");
printf("\t Press <enter> to continue.\n");
getchar();
}
void set_mode(int *mode, int val)
{
if (*mode == MODE_UNSET) {
*mode = val;
} else {
fprintf(stderr,
"ERROR: More than one mode specified. Please specify");
fprintf(stderr, " only one.\n");
exit(1);
}
}
int dump_config(struct options_t *options, char *file_name,
lc_callback cb, void *cb_arg)
{
int err = 0;
uint8_t *config;
uint32_t size = 0;
if ((err = read_config_from_remote(&config, &size, cb, (void *)1))) {
return err;
}
if ((err = write_config_to_file(config, size, file_name,
(*options).binary))) {
return err;
}
delete_blob(config);
return 0;
}
void print_time(int action)
{
static const char * const dow[8] =
{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "" };
if (action == 0) {
printf("Remote time is ");
} else {
printf("Remote time has been set to ");
}
printf("%04i/%02i/%02i %s %02i:%02i:%02i %+i %s\n",
get_time_year(), get_time_month(), get_time_day(),
dow[get_time_dow() & 7], get_time_hour(), get_time_minute(),
get_time_second(), get_time_utc_offset(), get_time_timezone());
}
/*
* Read the config from a file and write it to the remote
*/
int upload_config(uint8_t *data, uint32_t size, struct options_t *options,
lc_callback cb, void *cb_arg)
{
int err, i;
uint8_t *binary_data;
uint32_t binary_size;
binary_data = data;
binary_size = size;
if (!(*options).binary) {
if ((err = find_config_binary(data, size,
&binary_data, &binary_size)))
return LC_ERROR;
if (!(*options).noweb)
post_preconfig(data, size);
}
printf("Preparing Update: ");
if ((err = prep_config())) {
return err;
}
printf(" done\n");
/*
* We must invalidate flash before we erase and write so that
* nothing will attempt to reference it while we're working.
*/
printf("Invalidating Flash: ");
if ((err = invalidate_flash())) {
return err;
}
printf(" done\n");
/*
* Flash can be changed to 0, but not back to 1, so you must
* erase the flash (to 1) in order to write the flash.
*/
printf("Erasing Flash: ");
if ((err = erase_config(binary_size, cb, (void *)0))) {
return err;
}
printf(" done\n");
printf("Writing Config: ");
if ((err = write_config_to_remote(binary_data, binary_size, cb,
(void *)1))) {
return err;
}
printf(" done\n");
printf("Verifying Config: ");
if ((err = verify_remote_config(binary_data,
binary_size, cb, (void *)1))) {
return err;
}
printf(" done\n");
printf("Finalizing Update: ");
if ((err = finish_config())) {
return err;
}
printf(" done\n");
if ((*options).noreset) {
return 0;
}
printf("Resetting Remote: ");
reset_remote();
deinit_concord();
for (i = 0; i < MAX_WAIT_FOR_BOOT; i++) {
sleep(WAIT_FOR_BOOT_SLEEP);
err = init_concord();
if (err == 0) {
err = get_identity(cb_print_percent_status, NULL);
if (err == 0) {
break;
}
deinit_concord();
}
}
if (err != 0) {
return err;
}
printf(" done\n");
printf("Setting Time: ");
if ((err = set_time())) {
return err;
}
printf(" done\n");
if (!(*options).binary && !(*options).noweb) {
printf("Contacting website: ");
if ((err = post_postconfig(data, size))) {
return err;
}
printf(" done\n");
}
return 0;
}
int dump_safemode(char *file_name, lc_callback cb, void *cb_arg)
{
uint8_t * safe = 0;
uint32_t safe_size;
int err = 0;
if ((err = read_safemode_from_remote(&safe, &safe_size, cb,
cb_arg))) {
delete_blob(safe);
return err;
}
if ((err = write_safemode_to_file(safe, safe_size, file_name))) {
delete_blob(safe);
return err;
}
delete_blob(safe);
return 0;
}
int upload_firmware(uint8_t *firmware, uint32_t firmware_size,
struct options_t *options, lc_callback cb, void *cb_arg)
{
int err, i;
uint8_t *firmware_bin;
uint32_t firmware_bin_size;
err = 0;
firmware_bin = 0;
if ((err = is_fw_update_supported((*options).direct))) {
fprintf(stderr, "Sorry, firmware upgrades are not yet");
fprintf(stderr, " on your remote model yet.\n");
return err;
}
if ((*options).direct) {
direct_warning();
} else {
if (is_config_safe_after_fw() != 0) {
printf("NOTE: A firmware upgrade, will erase your");
printf(" remote's config and you will need to update");
printf(" it. You may want to make a backup with -c");
printf(" or otherwise just use the website.\n");
printf("Press <enter> to continue.\n");
getchar();
}
}
if ((*options).binary) {
firmware_bin = firmware;
firmware_bin_size = firmware_size;
} else {
if ((err = extract_firmware_binary(firmware, firmware_size,
&firmware_bin, &firmware_bin_size))) {
printf("Failed to extract firmware from file\n");
delete_blob(firmware_bin);
return err;
}
}
if (!(*options).direct) {
if ((err = prep_firmware())) {
printf("Failed to prepare remote for FW update\n");
if (firmware_bin != firmware) {
delete_blob(firmware_bin);
}
return err;
}
}
printf("Invalidating Flash: ");
if ((err = invalidate_flash())) {
if (firmware_bin != firmware) {
delete_blob(firmware_bin);
}
return err;
}
printf(" done\n");
printf("Erasing Flash: ");
if ((err = erase_firmware((*options).direct, cb, (void *)0))) {
if (firmware_bin != firmware) {
delete_blob(firmware_bin);
}
return err;
}
printf(" done\n");
printf("Writing firmware: ");
if ((err = write_firmware_to_remote(firmware_bin, firmware_bin_size,
(*options).direct, cb, cb_arg))) {
if (firmware_bin != firmware) {
delete_blob(firmware_bin);
}
return err;
}
printf(" done\n");
/* Done with this... */
if (firmware_bin != firmware) {
delete_blob(firmware_bin);
}
if (!(*options).direct) {
if ((err = finish_firmware())) {
printf("Failed to finalize FW update\n");
return err;
}
}
if ((*options).noreset) {
return 0;
}
printf("Resetting Remote: ");
reset_remote();
deinit_concord();
for (i = 0; i < MAX_WAIT_FOR_BOOT; i++) {
sleep(WAIT_FOR_BOOT_SLEEP);
err = init_concord();
if (err == 0) {
err = get_identity(cb_print_percent_status, NULL);
if (err == 0) {
break;
}
deinit_concord();
}
}
if (err != 0) {
return err;
}
printf(" done\n");
printf("Setting Time: ");
if ((err = set_time())) {
return err;
}
printf(" done\n");
if (!(*options).binary && !(*options).noweb) {
printf("Contacting website: ");
if ((err = post_postfirmware(firmware, firmware_size))) {
return err;
}
printf(" done\n");
}
return 0;
}
int dump_firmware(struct options_t *options, char *file_name,
lc_callback cb, void *cb_arg)
{
int err = 0;
uint8_t *firmware = 0;
uint32_t firmware_size;
if ((err = read_firmware_from_remote(&firmware, &firmware_size, cb,
cb_arg))) {
delete_blob(firmware);
return err;
}
if ((err = write_firmware_to_file(firmware, firmware_size, file_name,
(*options).binary))) {
delete_blob(firmware);
return err;
}
delete_blob(firmware);
return 0;
}
/*
* Parse our options.
*/
void parse_options(struct options_t *options, int *mode, char **file_name,
int argc, char *argv[])
{
int tmpint, option_index;
static struct option long_options[] = {
{"binary", no_argument, 0, 'b'},
{"dump-config", optional_argument, 0, 'c'},
{"write-config", required_argument, 0, 'C'},
{"direct", no_argument, 0, 'd'},
{"force", no_argument, 0, 0},
{"dump-firmware", optional_argument, 0, 'f'},
{"write-firmware", required_argument, 0, 'F'},
{"help", no_argument, 0, 'h'},
{"print-remote-info", no_argument, 0, 'i'},
{"learn-ir", required_argument, 0, 'l'},
{"reset", no_argument, 0, 'r'},
{"no-reset", no_argument, 0, 'R'},
{"dump-safemode", optional_argument, 0, 's'},
{"connectivity-test", required_argument, 0, 't'},
{"get-time", no_argument, 0, 'k' },
{"set-time", no_argument, 0, 'K' },
{"verbose", no_argument, 0, 'v'},
{"version", no_argument, 0, 'V'},
{"no-web", no_argument, 0, 'w'},
{0,0,0,0} /* terminating null entry */
};
(*options).verbose = 0;
(*options).binary = 0;
(*options).noweb = 0;
(*options).direct = 0;
(*options).force = 0;
(*options).noreset = 0;
*mode = MODE_UNSET;
tmpint = 0;
option_index = 0;
while ((tmpint = getopt_long(argc, argv, "bc::C:df::F:hil:rs::t:kKvVw",
long_options, &option_index)) != EOF) {
switch (tmpint) {
case 0:
/* Long-only options go here */
if (!strcmp(long_options[option_index].name, "force")) {
(*options).force = 1;
break;
}
break;
case 'b':
(*options).binary = 1;
break;
case 'c':
set_mode(mode, MODE_DUMP_CONFIG);
if (optarg != NULL) {
*file_name = optarg;
}
break;
case 'C':
if (optarg == NULL) {
fprintf(stderr,
"Missing config file to read from.\n");
exit(1);
}
set_mode(mode, MODE_WRITE_CONFIG);
*file_name = optarg;
break;
case 'd':
(*options).direct = 1;
break;
case 'f':
set_mode(mode, MODE_DUMP_FIRMWARE);
if (optarg != NULL)
*file_name = optarg;
break;
case 'F':
if (optarg == NULL) {
fprintf(stderr,
"Missing firmware file to read from.\n");
exit(1);
}
set_mode(mode, MODE_WRITE_FIRMWARE);
*file_name = optarg;
break;
case 'h':
set_mode(mode, MODE_HELP);
break;
case 'i':
set_mode(mode, MODE_PRINT_INFO);
break;
case 'l':
if (optarg == NULL) {
fprintf(stderr,
"Missing config file to read from.\n");
exit(1);
}
set_mode(mode, MODE_LEARN_IR);
*file_name = optarg;
break;
case 'r':
set_mode(mode, MODE_RESET);
break;
case 'R':
(*options).noreset = 1;
break;
case 's':
set_mode(mode, MODE_DUMP_SAFEMODE);
if (optarg != NULL)
*file_name = optarg;
break;
case 't':
if (optarg == NULL) {
fprintf(stderr,
"Missing connectivity file to read from.\n");
exit(1);
}
set_mode(mode, MODE_CONNECTIVITY);
*file_name = optarg;
break;
case 'k':
set_mode(mode, MODE_GET_TIME);
break;
case 'K':
set_mode(mode, MODE_SET_TIME);
break;
case 'v':
(*options).verbose = 1;
break;
case 'V':
set_mode(mode, MODE_VERSION);
break;
case 'w':
(*options).noweb = 1;
break;
default:
exit(1);
}
}
if (optind + 1 < argc) {
fprintf(stderr, "Multiple arguments were");
fprintf(stderr, " specified after the options");
fprintf(stderr, " - not sure what to do,");
fprintf(stderr, " exiting\n");
exit(1);
} else if (optind < argc) {
/* one thing left over, lets hope its a filename */
*file_name = argv[optind];
}
}
int detect_mode(uint8_t *data, uint32_t size, int *mode)
{
int err, type;
*mode = MODE_UNSET;
err = identify_file(data, size, &type);
if (err) {
return err;
}
switch (type) {
case LC_FILE_TYPE_CONNECTIVITY:
*mode = MODE_CONNECTIVITY;
break;
case LC_FILE_TYPE_CONFIGURATION:
*mode = MODE_WRITE_CONFIG;
break;
case LC_FILE_TYPE_FIRMWARE:
*mode = MODE_WRITE_FIRMWARE;
break;
case LC_FILE_TYPE_LEARN_IR:
*mode = MODE_LEARN_IR;
break;
default:
return LC_ERROR;
break;
}
return 0;
}
char *mode_string(int mode)
{
switch (mode) {
case MODE_CONNECTIVITY:
return "Connectivity Check";
break;
case MODE_WRITE_CONFIG:
return "Write Configuration";
break;
case MODE_WRITE_FIRMWARE:
return "Write Firmware";
break;
case MODE_LEARN_IR:
return "Learn IR";
break;
default:
return "Unknown";
break;
}
}
void report_mode_mismatch(int mode, int file_mode, int force)
{
if (force) {
fprintf(stderr, "WARNING:");
} else {
fprintf(stderr, "ERROR:");
}
fprintf(stderr, " Requested mode is: %s\n",
mode_string(mode));
fprintf(stderr, " but file detected as: %s\n",
mode_string(file_mode));
fprintf(stderr, "Try not specifying a mode at all, I will figure it");
fprintf(stderr, " out for you.\n");
}
void help()
{
printf("There are two ways to invoke this software. The primary");
printf(" way is \"automatic\nmode\" where you just pass in a file");
printf(" and the software figures out what to\ndo for you:\n");
printf("\tconcordance <file>\n\n");
printf("Or you can specify what to do:\n");
printf("\tconcordance <options>\n\n");
printf("When specifying options, you must first choose a mode:\n\n");
printf(" -c, --dump-config [<filename>]\n");
printf("\tRead the config from the remote and write it to a file.");
printf("\n\tIf no filename is specified, config.EZHex is used.\n\n");
printf(" -C, --write-config <filename>\n");
printf("\tRead a config from <filename> and write it to the");
printf(" remote.\n\n");
printf(" -f, --dump-firmware [<filename>]\n");
printf("\tRead firmware from the remote and write it to a file.\n");
printf("\tIf no filename is specified firmware.EZUp is used.\n\n");
printf(" -F, --write-firmware <filename>\n");
printf("\tRead firmware from <filename> and write it to the");
printf(" remote\n\n");
printf(" -h, --help\n");
printf("\tPrint this help message and exit.\n\n");
printf(" -i, --print-remote-info\n");
printf("\tPrint information about the remote. Additional");
printf(" information will\n\tbe printed if -v is also used.\n\n");
printf(" -k, --get-time\n");
printf("\t Get time from the remote\n\n");
printf(" -K, --set-time\n");
printf("\t Set the remote's time clock\n\n");
printf(" -l, --learn-ir <filename>\n");
printf("\t Learn IR from other remotes. Use <filename>.\n\n");
printf(" -r, --reset\n");
printf("\tReset (power-cycle) the remote control\n\n");
printf(" -s, --dump-safemode [<filename>]\n");
printf("\tRead the safemode firmware from the remote and write it");
printf(" to a file.\n\tIf no filename is psecified, safe.bin is");
printf(" used.\n\n");
printf(" -t, --connectivity-test <filename>\n");
printf("\tDo a connectivity test using <filename>\n\n");
printf(" -V, --version\n");
printf("\t Print the version and exit\n\n");