File: remote.cpp

package info (click to toggle)
concordance 1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,700 kB
  • sloc: cpp: 7,203; sh: 4,621; ansic: 882; python: 707; perl: 171; makefile: 132; xml: 34
file content (1066 lines) | stat: -rw-r--r-- 31,139 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
/*
 * vim:tw=80:ai:tabstop=4:softtabstop=4:shiftwidth=4:expandtab
 *
 * 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
 */

#include "remote.h"

#include <string.h>
#include <errno.h>

#include "libconcord.h"
#include "lc_internal.h"
#include "hid.h"
#include "protocol.h"
#include "remote_info.h"

#define GUID_STR \
  "{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}"

void setup_ri_pointers(TRemoteInfo &ri)
{
    unsigned int u;
    for (u = 0; u < sizeof(FlashList)/sizeof(TFlash)-1; ++u) {
        if (ri.flash_id == FlashList[u].id
            && ri.flash_mfg == FlashList[u].mfg)
            break;
    }
    ri.flash = &FlashList[u];

    ri.arch = (ri.architecture < sizeof(ArchList)/sizeof(TArchInfo))
            ? &ArchList[ri.architecture] : NULL;

    ri.model = (ri.skin < max_model)
            ? &ModelList[ri.skin] : &ModelList[max_model];
}

void make_guid(const uint8_t * const in, char*&out)
{
    char x[48];
    // Non-HID remotes (ZWave-HID, ZWave-USBNet, MH) as well as Arch 14 seem
    // to use a more normal byte ordering for serial #'s.
    if (is_z_remote() || is_mh_remote() || (get_arch() == 14)) {
        sprintf(x, GUID_STR, in[0], in[1], in[2], in[3], in[4], in[5], in[6],
                in[7], in[8], in[9], in[10], in[11], in[12], in[13], in[14],
                in[15]);
    }
    else {
        sprintf(x, GUID_STR, in[3], in[2], in[1], in[0], in[5], in[4], in[7],
                in[6], in[8], in[9], in[10], in[11], in[12], in[13], in[14],
                in[15]);
    }
    out = strdup(x);
}

void make_serial(uint8_t *ser, TRemoteInfo &ri)
{
    make_guid(ser, ri.serial1);
    make_guid(ser+16, ri.serial2);
    make_guid(ser+32, ri.serial3);
}

int CRemote::Reset(uint8_t kind)
{
    uint8_t reset_cmd[64] = { COMMAND_RESET, kind };
    int err;

    err = HID_WriteReport(reset_cmd);
    /*
     * Certain remotes (e.g., the 785) do not get a successful return from
     * HID_WriteReport even though the reset succeeds.  Ignore this.
     */
    if (err == -ENODEV) {
        debug("Ignoring error from reset command");
        err = 0;
    }
    return err;
}

/*
 * Send the GET_VERSION command to the remote, and read the response.
 *
 * Then populate our struct with all the relevant info.
 */
int CRemote::GetIdentity(TRemoteInfo &ri, THIDINFO &hid, lc_callback cb,
                         void *cb_arg, uint32_t cb_stage)
{
    int err = 0;
    uint32_t cb_count = 0;

    const uint8_t qid[64] = { COMMAND_GET_VERSION };

    if ((err = HID_WriteReport(qid))) {
        debug("Failed to write to remote");
        return 1;
    }

    uint8_t rsp[68];
    if ((err = HID_ReadReport(rsp))) {
        debug("Failed to read from remote");
        return 1;
    }

    /*
      * See specs/protocol.txt for format
      */
    const unsigned int rx_len = rsp[0] & 0x0F;

    if ((rsp[0] & 0xF0) != RESPONSE_VERSION_DATA ||
        (rx_len != 5 && rx_len != 7 && rx_len != 8)) {
        debug("Bogus ident response: %02X", rsp[1]);
        return LC_ERROR_INVALID_DATA_FROM_REMOTE;
    }

    ri.fw_ver_major = rsp[1] >> 4;
    ri.fw_ver_minor = rsp[1] & 0x0F;
    ri.hw_ver_major = rsp[2] >> 4;
    ri.hw_ver_minor = rsp[2] & 0x0F;
    ri.hw_ver_micro = 0; /* usbnet remotes have a non-zero micro version */
    ri.flash_id = rsp[3];
    ri.flash_mfg = rsp[4];
    ri.architecture = rx_len < 6 ? 2 : rsp[5] >> 4;
    ri.fw_type = rx_len < 6 ? 0 : rsp[5] & 0x0F;
    ri.skin = rx_len < 6 ? 2 : rsp[6];
    if (rx_len < 7) {
        ri.protocol = 0;
    } else if (rx_len < 8) {
        ri.protocol = rsp[7];
    } else {
        ri.protocol = ri.architecture;
    }

    setup_ri_pointers(ri);

    uint8_t rd[1024];
    if ((err=ReadFlash(ri.arch->config_base, 1024, rd, ri.protocol, false))) {
        debug("Error reading first k of config data");
        return LC_ERROR_READ;
    }
    if (cb) {
        cb(cb_stage, cb_count++, 1, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);
    }

    /*
     * Calculate cookie
     *   see specs/protocol.txt for more info
     */
    const uint32_t cookie = (ri.arch->cookie_size == 2)
            ? rd[0] | (rd[1] << 8)
            : rd[0] | (rd[1] << 8) | (rd[2] << 16) | (rd[3] << 24);

    ri.valid_config = (cookie == ri.arch->cookie);

    if (ri.valid_config) {
        ri.max_config_size = (ri.flash->size << 10)
            - (ri.arch->config_base - ri.arch->flash_base);
        const uint32_t end = rd[ri.arch->end_vector]
                | (rd[ri.arch->end_vector + 1] << 8)
                | (rd[ri.arch->end_vector + 2] << 16);
        ri.config_bytes_used =
            (end - (ri.arch->config_base - ri.arch->flash_base))
            + 4;
    } else {
        ri.config_bytes_used = 0;
        ri.max_config_size = 1;
    }

    // read serial (see specs/protocol.txt for details)
    switch (ri.arch->serial_location) {
    case SERIAL_LOCATION_EEPROM:
        err = ReadMiscByte(ri.arch->serial_address, SERIAL_SIZE,
                           COMMAND_MISC_EEPROM, rsp);
        break;
    case SERIAL_LOCATION_FLASH:
        err = ReadFlash(ri.arch->serial_address, SERIAL_SIZE, rsp, ri.protocol);
        break;
    default:
        debug("Invalid TArchInfo\n");
        return LC_ERROR_READ;
    }
    if (err) {
        debug("Couldn't read serial\n");
        return LC_ERROR_READ;
    }

    if (cb) {
        cb(cb_stage, cb_count++, 2, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);
    }

    make_serial(rsp, ri);

    return 0;
}

int CRemote::ReadFlash(uint32_t addr, const uint32_t len, uint8_t *rd,
                       unsigned int protocol, bool verify, lc_callback cb,
                       void *cb_arg, uint32_t cb_stage)
{
    uint32_t cb_count = 0;
    const unsigned int max_chunk_len = protocol == 0 ? 700 : 1022;

    /*
     * This is a mapping of the lower-half of the first command byte to
     * the size of the total command to be sent.
     *
     * See specs/protocol.txt for more * info.
     */
    // Protocol 0 (745, safe mode)
    static const unsigned int dl0[16] =
        { 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
    // All other protocols
    static const unsigned int dlx[16] =
        { 0, 0, 1, 2, 3, 4, 5, 6, 14, 30, 62, 0, 0, 0, 0, 0 };
    const unsigned int *rxlenmap = protocol ? dlx : dl0;

    uint8_t *pr = rd;
    const uint32_t end = addr+len;
    unsigned int bytes_read = 0;
    int err = 0;

    do {
        static uint8_t cmd[64] = {0};
        cmd[0] = COMMAND_READ_FLASH | 0x05;
        cmd[1] = (addr >> 16) & 0xFF;
        cmd[2] = (addr >> 8) & 0xFF;
        cmd[3] = addr & 0xFF;
        unsigned int chunk_len = end-addr;
        if (chunk_len > max_chunk_len)
            chunk_len = max_chunk_len;
        cmd[4] = (chunk_len >> 8) & 0xFF;
        cmd[5] = chunk_len & 0xFF;

        if ((err = HID_WriteReport(cmd)))
            break;

        uint8_t seq = 1;

        do {
            uint8_t rsp[68];
            if ((err = HID_ReadReport(rsp)))
                break;

            const uint8_t r = rsp[0] & COMMAND_MASK;

            if (r == RESPONSE_READ_FLASH_DATA) {
                if (seq != rsp[1]) {
                    err = LC_ERROR;
                    debug("Invalid sequence %02X %02x",
                        seq, rsp[1]);
                    break;
                }
                seq += 0x11;
                const unsigned int rxlen =
                    rxlenmap[rsp[0] & LENGTH_MASK];
                if (rxlen) {
                    if (verify) {
                        if (memcmp(pr, rsp+2, rxlen)) {
                            debug("Verify fail");
                            err = LC_ERROR_VERIFY;
                            break;
                        }
                    } else {
                        memcpy(pr, rsp+2, rxlen);
                    }
                    pr += rxlen;
                    addr += rxlen;
                    bytes_read += rxlen;
                }
            } else if (r == RESPONSE_DONE) {
                break;
            } else {
                debug("Invalid response [%02X]", rsp[0]);
                err = LC_ERROR;
            }
        } while (err == 0);

        if (cb) {
            cb(cb_stage, cb_count++, bytes_read, len, LC_CB_COUNTER_TYPE_BYTES,
               cb_arg, NULL);
        }
    } while (err == 0 && addr < end);

    return err;
}

int CRemote::InvalidateFlash(lc_callback cb, void *cb_arg, uint32_t lc_stage)
{
    const uint8_t ivf[64] = { COMMAND_WRITE_MISC | 0x01,
                              COMMAND_MISC_INVALIDATE_FLASH };
    int err;

    if (cb)
        cb(lc_stage, 0, 0, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);

    if ((err = HID_WriteReport(ivf)))
        return err;

    if (cb)
        cb(lc_stage, 1, 1, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);

    uint8_t rsp[68];
    if ((err = HID_ReadReport(rsp)))
        return err;

    if ((rsp[0] & COMMAND_MASK) != RESPONSE_DONE ||
        (rsp[1] & COMMAND_MASK) != COMMAND_WRITE_MISC) {
        return 1;
    }

    if (cb)
        cb(lc_stage, 2, 2, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);

    return 0;
}


int CRemote::EraseFlash(uint32_t addr, uint32_t len,  const TRemoteInfo &ri,
                        lc_callback cb, void *cb_arg, uint32_t cb_stage)
{
    const unsigned int *sectors = ri.flash->sectors;
    const unsigned int flash_base = ri.arch->flash_base;

    const uint32_t end = addr + len;

    int err = 0;
    uint32_t num_sectors = 0;

    uint32_t n = 0;

    // skip to where we need to start writing
    while (sectors[n] + flash_base < addr) {
        n++;
    }
    // start on the NEXT one
    n++;

    num_sectors = n;
    while (sectors[num_sectors] + flash_base < end) {
        num_sectors++;
    }
    num_sectors -= n - 1;

    uint32_t sector_begin = sectors[n-1] + flash_base;
    uint32_t sector_end = sectors[n] + flash_base;

    for (uint32_t i = 0; i < num_sectors; i++) {
        static uint8_t erase_cmd[64] = {0};
        erase_cmd[0] = COMMAND_ERASE_FLASH;
        erase_cmd[1] = (sector_begin >> 16) & 0xFF;
        erase_cmd[2] = (sector_begin >> 8) & 0xFF;
        erase_cmd[3] = sector_begin & 0xFF;

        if ((err = HID_WriteReport(erase_cmd)))
            break;

        uint8_t rsp[68];
        if ((err = HID_ReadReport(rsp, 5000)))
            break;

        if (cb)
            cb(cb_stage, i, i+1, num_sectors, LC_CB_COUNTER_TYPE_STEPS,
               cb_arg, NULL);
        debug("erase sector %2i: %06X - %06X", n, sector_begin, sector_end);
        sector_begin = sector_end;
        sector_end = sectors[++n] + flash_base;
    }

    return err;
}

int CRemote::PrepFirmware(const TRemoteInfo &ri, lc_callback cb, void *cb_arg,
                          uint32_t cb_stage)
{
    int err = 0;
    uint8_t data[1] = { 0x00 };

    if (cb)
        cb(cb_stage, 0, 0, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);

    if (ri.arch->firmware_update_base == ri.arch->firmware_base) {
        /*
         * The preperation for where the staging area IS the config
         * area.
         *    restart config
         *    write "1" to flash addr 200000
         */
        if ((err = WriteMiscByte(0x09, 1, COMMAND_MISC_RESTART_CONFIG, data)))
            return LC_ERROR;

        if (cb)
            cb(cb_stage, 1, 1, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);

        if ((err = WriteFlash(0x200000, 1, data, ri.protocol, NULL, NULL, 0)))
            return LC_ERROR;
    } else {
        /*
         * The preperation for where the staging area is distinct.
         *    write "1" to ram addr 0
         *    read it back
         */
        if ((err = WriteRam(0, 1, data)))
            return LC_ERROR_WRITE;

        if (cb)
            cb(cb_stage, 1, 1, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);

        if ((err = ReadRam(0, 1, data)))
            return LC_ERROR_WRITE;
        if (data[0] != 0)
            return LC_ERROR_VERIFY;
    }
    if (cb)
        cb(cb_stage, 2, 2, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);

    return 0;
}

int CRemote::FinishFirmware(const TRemoteInfo &ri, lc_callback cb, void *cb_arg,
                            uint32_t cb_stage)
{
    int err = 0;

    if (cb)
        cb(cb_stage, 0, 0, 3, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);

    uint8_t data[1];
    if (ri.arch->firmware_update_base == ri.arch->firmware_base) {
        data[0] = 0x02;
        if ((err = WriteFlash(0x200000, 1, data, ri.protocol, NULL, NULL, 0)))
            return LC_ERROR;
        if (cb)
            cb(cb_stage, 1, 1, 3, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);
    } else {
        data[0] = 0x02;
        if ((err = WriteRam(0, 1, data))) {
            debug("Failed to write 2 to RAM 0");
            return LC_ERROR_WRITE;
        }
        if (cb)
            cb(cb_stage, 1, 1, 3, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);
        if ((err = ReadRam(0, 1, data))) {
            debug("Failed to from RAM 0");
            return LC_ERROR_WRITE;
        }
        if (cb)
            cb(cb_stage, 2, 2, 3, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);
        if (data[0] != 2) {
            printf("byte is %d\n", data[0]);
            debug("Finalize byte didn't match");
            return LC_ERROR_VERIFY;
        }
    }
    if (cb)
        cb(cb_stage, 3, 3, 3, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);

    return 0;
}

int CRemote::PrepConfig(const TRemoteInfo &ri, lc_callback cb, void *cb_arg,
                        uint32_t cb_stage)
{
    int err;
    uint8_t data_zero[1] = { 0x00 };

    if (ri.architecture != 14) {
        if (cb) {
            cb(cb_stage, 0, 0, 1, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);
            cb(cb_stage, 1, 1, 1, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);
        }
        return 0;
    }

    if (cb)
        cb(cb_stage, 0, 0, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);

    if ((err = WriteMiscByte(0x02, 1, COMMAND_MISC_RESTART_CONFIG, data_zero))) {
        return err;
    }

    if (cb)
        cb(cb_stage, 1, 1, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);

    if ((err = WriteMiscByte(0x05, 1, COMMAND_MISC_RESTART_CONFIG, data_zero))) {
        return err;
    }

    if (cb)
        cb(cb_stage, 2, 2, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);

    return 0;
}

int CRemote::FinishConfig(const TRemoteInfo &ri, lc_callback cb, void *cb_arg,
                          uint32_t cb_stage)
{
    int err;
    uint8_t data_one[1]  = { 0x01 };
    uint8_t data_zero[1] = { 0x00 };

    if (ri.architecture != 14) {
        return 0;
    }

    if (cb)
        cb(cb_stage, 0, 0, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);

    if ((err = WriteMiscByte(0x03, 1, COMMAND_MISC_RESTART_CONFIG, data_one))) {
        return err;
    }

    if (cb)
        cb(cb_stage, 1, 1, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);

    if ((err = WriteMiscByte(0x06, 1, COMMAND_MISC_RESTART_CONFIG,
                             data_zero))) {
        return err;
    }

    if (cb)
        cb(cb_stage, 2, 2, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);

    return 0;
}

int CRemote::WriteRam(uint32_t addr, const uint32_t len, uint8_t *wr)
{
    return WriteMiscByte(addr, len, COMMAND_MISC_RAM, wr);
}

int CRemote::ReadRam(uint32_t addr, const uint32_t len, uint8_t *rd)
{
    return ReadMiscByte(addr, len, COMMAND_MISC_RAM, rd);
}

int CRemote::WriteFlash(uint32_t addr, const uint32_t len, const uint8_t *wr,
    unsigned int protocol, lc_callback cb, void *cb_arg, uint32_t cb_stage)
{
    uint32_t cb_count = 0;
    const unsigned int max_chunk_len = protocol == 0 ? 749 : 3150;

    /* mapping of lenghts - see specs/protocol.txt */
    static const unsigned int txlenmap0[] = { 0x07, 7, 6, 5, 4, 3, 2, 1 };
    static const unsigned int txlenmapx[] =
        { 0x0A, 63, 31, 15, 7, 6, 5, 4, 3, 2, 1 };
    const unsigned int *txlenmap = protocol ? txlenmapx : txlenmap0;

    const uint8_t *pw = wr;
    const uint32_t end = addr+len;
    unsigned int bytes_written = 0;
    int err = 0;

    do {
        static uint8_t write_setup_cmd[64] = {0};
        write_setup_cmd[0] = COMMAND_WRITE_FLASH | 0x05;
        write_setup_cmd[1] = (addr >> 16) & 0xFF;
        write_setup_cmd[2] = (addr >> 8) & 0xFF;
        write_setup_cmd[3] = addr & 0xFF;
        uint32_t chunk_len = end - addr;
        if (chunk_len > max_chunk_len)
            chunk_len = max_chunk_len;
        write_setup_cmd[4] = (chunk_len >> 8) & 0xFF;
        write_setup_cmd[5] = chunk_len & 0xFF;

        if ((err = HID_WriteReport(write_setup_cmd)))
            break;

        while (chunk_len) {
            unsigned int n = txlenmap[0];
            unsigned int i = 1;
            while (chunk_len < txlenmap[i]) {
                ++i;
                --n;
            }
            unsigned int block_len = txlenmap[i];
            uint8_t wd[64] = {0};
            wd[0] = COMMAND_WRITE_FLASH_DATA | n;
            memcpy(wd+1, pw, block_len);
            HID_WriteReport(wd);
            pw += block_len;
            addr += block_len;
            bytes_written += block_len;
            chunk_len -= block_len;
        }

        uint8_t end_cmd[64] = { COMMAND_DONE, COMMAND_WRITE_FLASH };
        HID_WriteReport(end_cmd);

        uint8_t rsp[68];
        if ((err = HID_ReadReport(rsp, 5000)))
            break;

        if (cb) {
            cb(cb_stage, cb_count++, bytes_written, len,
               LC_CB_COUNTER_TYPE_BYTES, cb_arg, NULL);
        }
    } while (addr < end);

    return err;
}

int CRemote::ReadMiscByte(uint8_t addr, uint32_t len, uint8_t kind, uint8_t *rd)
{
    uint8_t rmb[64] = { COMMAND_READ_MISC | 0x02, kind, 0 };

    while (len--) {
        rmb[2] = addr++;

        int err;
        if ((err = HID_WriteReport(rmb)))
            return err;

        uint8_t rsp[68];
        if ((err = HID_ReadReport(rsp)))
            return err;

        if (rsp[0] != (RESPONSE_READ_MISC_DATA | 0x02) ||
            rsp[1] != kind)
            return 1;

        *rd++ = rsp[2];
    }
    return 0;
}

int CRemote::ReadMiscWord(uint16_t addr, uint32_t len, uint8_t kind,
                          uint16_t *rd)
{
    uint8_t rmw[64] = { COMMAND_READ_MISC | 0x03, kind, 0, 0 };

    while (len--) {
        rmw[2] = addr >> 8;
        rmw[3] = addr & 0xFF;
        ++addr;

        int err;
        if ((err = HID_WriteReport(rmw)))
            return err;

        uint8_t rsp[68];
        if ((err = HID_ReadReport(rsp)))
            return err;

        // WARNING: The 880 responds with C2 rather than C3
        if ((rsp[0] & COMMAND_MASK) != RESPONSE_READ_MISC_DATA ||
            rsp[1] != kind) {
            return 1;
        }

        *rd++ = (rsp[2] << 8) | rsp[3];
    }
    return 0;
}

int CRemote::WriteMiscByte(uint8_t addr, uint32_t len, uint8_t kind,
                           uint8_t *wr)
{
    uint8_t wmb[64] = {0};
    wmb[0] = COMMAND_WRITE_MISC | 0x03;
    wmb[1] = kind;

    while (len--) {
        wmb[2] = addr++;
        wmb[3] = *wr++;

        int err;
        if ((err = HID_WriteReport(wmb)))
            return err;

        uint8_t rsp[68];
        if ((err = HID_ReadReport(rsp)))
            return err;
        if ((rsp[0] & COMMAND_MASK) != RESPONSE_DONE ||
            rsp[1] != COMMAND_WRITE_MISC) {
            return 1;
        }
    }
    return 0;
}

int CRemote::WriteMiscWord(uint16_t addr, uint32_t len, uint8_t kind,
                           uint16_t *wr)
{
    uint8_t wmw[64] = {0};
    wmw[0] = COMMAND_WRITE_MISC | 0x05;
    wmw[1] = kind;

    while (len--) {
        wmw[2] = addr >> 8;
        wmw[3] = addr & 0xFF;
        ++addr;
        wmw[4] = *wr >> 8;
        wmw[5] = *wr & 0xFF;
        ++wr;

        int err;
        if ((err = HID_WriteReport(wmw)))
            return err;

        uint8_t rsp[68];
        if ((err = HID_ReadReport(rsp)))
            return err;
        if ((rsp[0] & COMMAND_MASK) != RESPONSE_DONE ||
            rsp[1] != COMMAND_WRITE_MISC) {
            return 1;
        }
    }
    return 0;
}


int CRemote::GetTime(const TRemoteInfo &ri, THarmonyTime &ht)
{
    int err = 0;

    if (ri.architecture < 8) {
        uint8_t tsv[8];
        err = ReadMiscByte(0, 6, COMMAND_MISC_STATE, tsv);
        ht.second = tsv[0];
        ht.minute = tsv[1];
        ht.hour = tsv[2];
        ht.dow = 7;
        ht.day = 1 + tsv[3];
        ht.month = 1 + tsv[4];
        ht.year = 2000 + tsv[5];
    } else {
        uint16_t tsv[8];
        err = ReadMiscWord(0, 7, COMMAND_MISC_STATE, tsv);
        ht.second = tsv[0];
        ht.minute = tsv[1];
        ht.hour = tsv[2];
        ht.day = 1 + tsv[3];
        ht.dow = tsv[4] & 7;
        ht.month = 1 + tsv[5];
        ht.year = 2000 + tsv[6];
    }

    ht.utc_offset = 0;
    ht.timezone = "";

    return err;
}

int CRemote::SetTime(const TRemoteInfo &ri, const THarmonyTime &ht,
                     lc_callback cb, void *cb_arg, uint32_t cb_stage)
{
    int err = 0;
    uint8_t rsp[68];
    int cb_count = 0;

    if (cb)
        cb(cb_stage, cb_count++, 0, 2, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);

    if (ri.architecture < 8) {
        uint8_t tsv[8];
        tsv[0] = 0;
        tsv[1] = ht.minute;
        tsv[2] = ht.hour;
        tsv[3] = ht.day - 1;
        tsv[4] = ht.month  -1;
        tsv[5] = ht.year - 2000;
        if ((err = WriteMiscByte(0, 6, COMMAND_MISC_STATE, tsv)))
            return err;
        if (cb)
            cb(cb_stage, cb_count++, 1, 3, LC_CB_COUNTER_TYPE_STEPS, cb_arg,
               NULL);

        tsv[0] = ht.second;
        err = WriteMiscByte(0, 1, COMMAND_MISC_STATE, tsv);
    } else {
        uint16_t tsv[8];
        tsv[0] = 0;
        tsv[1] = ht.minute;
        tsv[2] = ht.hour;
        tsv[3] = ht.day-1;
        tsv[4] = ht.dow;
        tsv[5] = ht.month-1;
        tsv[6] = ht.year-2000;
        if ((err = WriteMiscWord(0, 7, COMMAND_MISC_STATE, tsv)))
            return err;
        if (cb)
            cb(cb_stage, cb_count++, 1, 3, LC_CB_COUNTER_TYPE_STEPS, cb_arg,
               NULL);
        tsv[0] = ht.second;
        if ((err = WriteMiscWord(0, 1, COMMAND_MISC_STATE, tsv)))
            return err;

        // Send Recalc Clock command for 880 only (not 360/520/550)
        if (ri.architecture == 8) {
            static const uint8_t rcc[64] = {
                COMMAND_WRITE_MISC | 0x01,
                COMMAND_MISC_CLOCK_RECALCULATE };
            err = HID_WriteReport(rcc);
        }
    }
    if (cb)
        cb(cb_stage, cb_count++, 2, 3, LC_CB_COUNTER_TYPE_STEPS, cb_arg,
            NULL);

    if (err != 0) {
        return err;
    }

    /*
     * For some models, they return a RESPONSE_DONE, and we have to read
     * it otherwise otherwise next command will fail.
     * However, other devices don't return this, in which case the read
     * failes.
     * So, if the read succeeds, we check the response, otherwise we just
     * move on with life.
     */
    err = HID_ReadReport(rsp);
    if (err == 0) {
        if ((rsp[0] & COMMAND_MASK) != RESPONSE_DONE) {
            err = 1;
        }
    } else {
        err = 0;
    }
    if (cb)
        cb(cb_stage, cb_count++, 3, 3, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);

    return err;
}

bool check_seq(int received_seq, uint8_t &expected_seq)
{
    if (received_seq == expected_seq) {
        return true;
    }

    if (received_seq == 0x1f && expected_seq == 0x10) {
        /*
         * Handle 880 SNAFU
         *
         * Does this indicate a bad learn???
         * Needs more testing
         */
        debug("sequence glitch!");
        expected_seq += 0x0F;
        return true;
    } else {
        debug("\nInvalid sequence %02X %02x", expected_seq,
            received_seq);
        return false;
    }
}

int _handle_ir_response(uint8_t rsp[64], uint32_t &ir_word, uint32_t &t_on,
                        uint32_t &t_off, uint32_t &t_total, uint32_t &ir_count,
                        uint32_t *&ir_signal, uint32_t &freq)
{
    const uint32_t len = rsp[63];
    if ((len & 1) != 0) {
        return 3;    // Invalid length
    }

    for (uint32_t u = 2; u < len; u += 2) {
        const uint32_t t = rsp[u] << 8 | rsp[1+u];
        if (ir_word > 2) {
            /*
             * For ODD words, t is the total time, we'll
             * update the total OFF time and be done.
             *
             * For EVEN words, t is just the ON time
             * -- IF we have any ON time, then we go ahead
             *    and record the off and on times we should
             *    have now gathered.
             *
             * Why do we differentiate between even/odd?
             * Perhaps just to make sure we've had two
             * cycles, and thus have off/on?
            */
            if (ir_word & 1) {
                // t == on + off time
                if (t_on) {
                    t_off = t - t_on;
                } else {
                    t_off += t;
                }
            } else {
                // t == on time
                t_on = t;
                if (t_on) {
                    debug("-%i\n", t_off);
                    if (ir_count < MAX_IR_SIGNAL_LENGTH) {
                        ir_signal[ir_count++] = t_off;
                    }
                    debug("+%i\n", t_on);
                    if (ir_count < MAX_IR_SIGNAL_LENGTH) {
                        ir_signal[ir_count++] = t_on;
                    }
                    t_total += t_off + t_on;
                }
            }
        } else {
            /*
             * For the first 3 words...
             *  the first one, we ignore, apparently
             *  the second one, we start keeping track of ON time
             *  the third one, we have enough data to calculate the 
             *    frequency and record the on time we've calculated
             */
            switch (ir_word) {
                case 0:    // ???
                    break;
                case 1:    // on time of first burst
                    t_on = t;
                    break;
                case 2:    // carrier cycle count of first burst
                    if (t_on) {
                        freq = static_cast<uint32_t>(
                            static_cast<uint64_t>(t)
                                *1000000/(t_on));
                        debug("%i Hz", freq);
                        debug("+%i", t_on);
                        ir_signal[ir_count++] = t_on;
                    }
                    break;
            }
        }
        ++ir_word;
    }
    return 0;
}

// This section of IR learning code is common between pure HID and MH remotes.
// 'seq' is the starting sequence number, which differs between HID and MH.
int LearnIRInnerLoop(uint32_t *freq, uint32_t **ir_signal,
                     uint32_t *ir_signal_length, uint8_t seq)
{
    int err = 0;
    uint8_t rsp[68];

    // Count of how man IR words we've received.
    uint32_t ir_word = 0;
    // Time button is on and off
    uint32_t t_on = 0;
    uint32_t t_off = 0;
    // total duration of received signal:
    // abort when > MAX_IR_SIGNAL_DURATION
    uint32_t t_total = 0;

    *ir_signal_length = 0;
    *ir_signal = new uint32_t[MAX_IR_SIGNAL_LENGTH];
    /*
     * Caller is responsible for deallocation of *ir_signal after use.
     *
     * Loop while we haven not:
     * - any error (including signal duration and buffer overflow)
     * - signal interrupted for IR_LEARN_DONE_TIMEOUT or longer
     */
    while ((err == 0) && (t_off < IR_LEARN_DONE_TIMEOUT * 1000)) {
        if ((err = HID_ReadReport(rsp, ir_word ?
            IR_LEARN_DONE_TIMEOUT : IR_LEARN_START_TIMEOUT))) {
            err = LC_ERROR_READ;
            break;
        }
        const uint8_t r = rsp[0] & COMMAND_MASK;
        if (r == RESPONSE_IRCAP_DATA) {
            if (!check_seq(rsp[1], seq)) {
                err = LC_ERROR;
                break;
            }
            seq += 0x10;
            /*
             * This will handle the IR response including updating
             * t_off so we can exit the loop if long enough time
             * goes by without action.
             */
            err = _handle_ir_response(rsp, ir_word, t_on, t_off,
                t_total, *ir_signal_length, *ir_signal,    *freq);
            if (err != 0) {
                break;
            }
        } else if (r == RESPONSE_DONE) {
            break;
        } else {
            debug("Invalid response [%02X]", rsp[1]);
            err = LC_ERROR;
        }
        /* check for overflow: */
        if ((t_total > MAX_IR_SIGNAL_DURATION * 1000)
            || (*ir_signal_length > MAX_IR_SIGNAL_LENGTH)) {
            err = LC_ERROR_IR_OVERFLOW;
        }
    }

    if ((err == 0) && (*ir_signal_length > 0)) {
        /* we have actually got some signal */
        if (t_off) {
            debug("-%i", t_off);
        }
        /* make sure we record a final off */
        if (*ir_signal_length < MAX_IR_SIGNAL_LENGTH) {
            (*ir_signal)[(*ir_signal_length)++] = t_off;
        }
    }

    return err;
}

int CRemote::LearnIR(uint32_t *freq, uint32_t **ir_signal,
                     uint32_t *ir_signal_length, lc_callback cb, void *cb_arg,
                     uint32_t cb_stage)
{
    int err = 0;
    uint8_t rsp[68];

    static const uint8_t start_ir_learn[64] = { COMMAND_START_IRCAP };
    static const uint8_t stop_ir_learn[64] = { COMMAND_STOP_IRCAP };

    if (cb) {
        cb(cb_stage, 0, 0, 1, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);
    }

    if (HID_WriteReport(start_ir_learn) != 0) {
        return LC_ERROR_WRITE;
    }

    err = LearnIRInnerLoop(freq, ir_signal, ir_signal_length, 0);

    if (HID_WriteReport(stop_ir_learn) != 0) {
        err = LC_ERROR_WRITE;
    }

    /* flush HID buffer until empty or RESPONSE_DONE: */
    do {
        if (HID_ReadReport(rsp, IR_LEARN_DONE_TIMEOUT) != 0) {
            err = LC_ERROR_READ;
            break;
        }
    } while ((rsp[0] & COMMAND_MASK) != RESPONSE_DONE);

    if (cb && !err) {
        cb(cb_stage, 1, 1, 1, LC_CB_COUNTER_TYPE_STEPS, cb_arg, NULL);
    }

    return err;
}

int CRemote::ReadFile(const char *filename, uint8_t *rd, const uint32_t rdlen,
                      uint32_t *data_read, uint8_t start_seq, lc_callback cb,
                      void *cb_arg, uint32_t cb_stage)
{
    return LC_ERROR_UNSUPP;
}

int CRemote::WriteFile(const char *filename, uint8_t *wr, const uint32_t wrlen)
{
    return LC_ERROR_UNSUPP;
}