aboutsummaryrefslogtreecommitdiff
path: root/chirp/drivers/bf-t1.py
blob: a0c4939e76170debd22dbf9791949c056d696f9f (plain) (blame)
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
# Copyright 2017 Pavel Milanes, CO7WT, <pavelmc@gmail.com>
#
# This driver is a community effort as I don't have the radio on my hands, so
# I was only the director of the orchestra, without the players this may never
# came true, so special thanks to the following hams for their contribution:
# - Henk van der Laan, PA3CQN
#       - Setting Discovery.
#       - Special channels for RELAY and EMERGENCY.
# - Harold Hankins
#       - Memory limits, testing & bug hunting.
# - Dmitry Milkov
#       - Testing & bug hunting.
# - Many others participants in the issue page on Chirp's site.
#
# 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 2 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/>.

from time import sleep
from chirp import chirp_common, directory, memmap
from chirp import bitwise, errors, util
from chirp.settings import RadioSetting, RadioSettingGroup, \
                RadioSettingValueBoolean, RadioSettingValueList, \
                RadioSettingValueInteger, RadioSettingValueString, \
                RadioSettingValueFloat, RadioSettings
from textwrap import dedent

import struct
import logging

LOG = logging.getLogger(__name__)

# A note about the memmory in these radios
#
# The '9100' OEM software only manipulates the lower 0x0180 bytes on read/write
# operations as we know, the file generated by the OEM software IS NOT an exact
# eeprom image, it's a crude text file with a pseudo csv format
#
# Later investigations by Harold Hankins found that the eeprom extend up to 2k
# consistent with a hardware chip K24C16 a 2k x 8 bit serial eeprom

MEM_SIZE = 0x0800  # 2048 bytes
WRITE_SIZE = 0x0180  # 384 bytes
BLOCK_SIZE = 0x10
ACK_CMD = "\x06"
MODES = ["NFM", "FM"]
SKIP_VALUES = ["S", ""]
TONES = chirp_common.TONES
DTCS = sorted(chirp_common.DTCS_CODES + [645])

# Special channels
SPECIALS = {
    "EMG": -2,
    "RLY": -1
    }

# Settings vars
TOT_LIST = ["Off"] + ["%s" % x for x in range(30, 210, 30)]
SCAN_TYPE_LIST = ["Time", "Carrier", "Search"]
LANGUAGE_LIST = ["Off", "English", "Chinese"]
TIMER_LIST = ["Off"] + ["%s h" % (x * 0.5) for x in range(1, 17)]
FM_RANGE_LIST = ["76-108", "65-76"]
RELAY_MODE_LIST = ["Off", "RX sync", "TX sync"]
BACKLIGHT_LIST = ["Off", "Key", "On"]
POWER_LIST = ["0.5 Watt", "1.0 Watt"]

# This is a general serial timeout for all serial read functions.
# Practice has show that about 0.07 sec will be enough to cover all radios.
STIMEOUT = 0.07

# this var controls the verbosity in the debug and by default it's low (False)
# make it True and you will to get a very verbose debug.log
debug = False

# #### ID strings #####################################################

# BF-T1 handheld
BFT1_magic = "\x05PROGRAM"
BFT1_ident = " BF9100S"


def _clean_buffer(radio):
    """Cleaning the read serial buffer, hard timeout to survive an infinite
    data stream"""

    dump = "1"
    datacount = 0

    try:
        while len(dump) > 0:
            dump = radio.pipe.read(100)
            datacount += len(dump)
            # hard limit to survive a infinite serial data stream
            # 5 times bigger than a normal rx block (20 bytes)
            if datacount > 101:
                seriale = "Please check your serial port selection."
                raise errors.RadioError(seriale)

    except Exception:
        raise errors.RadioError("Unknown error cleaning the serial buffer")


def _rawrecv(radio, amount=0):
    """Raw read from the radio device"""

    # var to hold the data to return
    data = ""

    try:
        if amount == 0:
            data = radio.pipe.read()
        else:
            data = radio.pipe.read(amount)

        # DEBUG
        if debug is True:
            LOG.debug("<== (%d) bytes:\n\n%s" %
                      (len(data), util.hexprint(data)))

        # fail if no data is received
        if len(data) == 0:
            raise errors.RadioError("No data received from radio")

    except:
        raise errors.RadioError("Error reading data from radio")

    return data


def _send(radio, data):
    """Send data to the radio device"""

    try:
        radio.pipe.write(data)

        # DEBUG
        if debug is True:
            LOG.debug("==> (%d) bytes:\n\n%s" %
                      (len(data), util.hexprint(data)))
    except:
        raise errors.RadioError("Error sending data to radio")


def _make_frame(cmd, addr, data=""):
    """Pack the info in the header format"""
    frame = struct.pack(">BHB", ord(cmd), addr, BLOCK_SIZE)

    # add the data if set
    if len(data) != 0:
        frame += data

    return frame


def _recv(radio, addr):
    """Get data from the radio"""

    # Get the full 20 bytes at a time
    # 4 bytes header + 16 bytes of data (BLOCK_SIZE)

    # get the whole block
    block = _rawrecv(radio, BLOCK_SIZE + 4)

    # short answer
    if len(block) < (BLOCK_SIZE + 4):
        raise errors.RadioError("Wrong block length (short) at 0x%04x" % addr)

    # long answer
    if len(block) > (BLOCK_SIZE + 4):
        raise errors.RadioError("Wrong block length (long) at 0x%04x" % addr)

    # header validation
    c, a, l = struct.unpack(">cHB", block[0:4])
    if c != "W" or a != addr or l != BLOCK_SIZE:
        LOG.debug("Invalid header for block 0x%04x:" % addr)
        LOG.debug("CMD: %s  ADDR: %04x  SIZE: %02x" % (c, a, l))
        raise errors.RadioError("Invalid header for block 0x%04x:" % addr)

    # return the data, 16 bytes of payload
    return block[4:]


def _start_clone_mode(radio, status):
    """Put the radio in clone mode, 3 tries"""

    # cleaning the serial buffer
    _clean_buffer(radio)

    # prep the data to show in the UI
    status.cur = 0
    status.msg = "Identifying the radio..."
    status.max = 3
    radio.status_fn(status)

    try:
        for a in range(0, status.max):
            # Update the UI
            status.cur = a + 1
            radio.status_fn(status)

            # send the magic word
            _send(radio, radio._magic)

            # Now you get a x06 of ACK if all goes well
            ack = _rawrecv(radio, 1)

            if ack == ACK_CMD:
                # DEBUG
                LOG.info("Magic ACK received")
                status.cur = status.max
                radio.status_fn(status)

                return True

        return False

    except errors.RadioError:
        raise
    except Exception as e:
        raise errors.RadioError("Error sending Magic to radio:\n%s" % e)


def _do_ident(radio, status):
    """Put the radio in PROGRAM mode & identify it"""
    #  set the serial discipline (default)
    radio.pipe.baudrate = 9600
    radio.pipe.parity = "N"
    radio.pipe.bytesize = 8
    radio.pipe.stopbits = 1
    radio.pipe.timeout = STIMEOUT

    # open the radio into program mode
    if _start_clone_mode(radio, status) is False:
        raise errors.RadioError("Radio did not enter clone mode, wrong model?")

    # Ok, poke it to get the ident string
    _send(radio, "\x02")
    ident = _rawrecv(radio, len(radio._id))

    # basic check for the ident
    if len(ident) != len(radio._id):
        raise errors.RadioError("Radio send a odd identification block.")

    # check if ident is OK
    if ident != radio._id:
        LOG.debug("Incorrect model ID, got this:\n\n" + util.hexprint(ident))
        raise errors.RadioError("Radio identification failed.")

    # handshake
    _send(radio, ACK_CMD)
    ack = _rawrecv(radio, 1)

    # checking handshake
    if len(ack) == 1 and ack == ACK_CMD:
        # DEBUG
        LOG.info("ID ACK received")
    else:
        LOG.debug("Radio handshake failed.")
        raise errors.RadioError("Radio handshake failed.")

    # DEBUG
    LOG.info("Positive ident, this is a %s %s" % (radio.VENDOR, radio.MODEL))

    return True


def _download(radio):
    """Get the memory map"""

    # UI progress
    status = chirp_common.Status()

    # put radio in program mode and identify it
    _do_ident(radio, status)

    # reset the progress bar in the UI
    status.max = MEM_SIZE / BLOCK_SIZE
    status.msg = "Cloning from radio..."
    status.cur = 0
    radio.status_fn(status)

    # cleaning the serial buffer
    _clean_buffer(radio)

    data = ""
    for addr in range(0, MEM_SIZE, BLOCK_SIZE):
        # sending the read request
        _send(radio, _make_frame("R", addr))

        # read
        d = _recv(radio, addr)

        # aggregate the data
        data += d

        # UI Update
        status.cur = addr / BLOCK_SIZE
        status.msg = "Cloning from radio..."
        radio.status_fn(status)

    # close comms with the radio
    _send(radio, "\x62")
    # DEBUG
    LOG.info("Close comms cmd sent, radio must reboot now.")

    return data


def _upload(radio):
    """Upload procedure, we only upload to the radio the Writable space"""

    # UI progress
    status = chirp_common.Status()

    # put radio in program mode and identify it
    _do_ident(radio, status)

    # get the data to upload to radio
    data = radio.get_mmap()

    # Reset the UI progress
    status.max = WRITE_SIZE / BLOCK_SIZE
    status.cur = 0
    status.msg = "Cloning to radio..."
    radio.status_fn(status)

    # cleaning the serial buffer
    _clean_buffer(radio)

    # the fun start here, we use WRITE_SIZE instead of the full MEM_SIZE
    for addr in range(0, WRITE_SIZE, BLOCK_SIZE):
        # getting the block of data to send
        d = data[addr:addr + BLOCK_SIZE]

        # build the frame to send
        frame = _make_frame("W", addr, d)

        # send the frame
        _send(radio, frame)

        # receiving the response
        ack = _rawrecv(radio, 1)

        # basic check
        if len(ack) != 1:
            raise errors.RadioError("No ACK when writing block 0x%04x" % addr)

        if ack != ACK_CMD:
            raise errors.RadioError("Bad ACK writing block 0x%04x:" % addr)

        # UI Update
        status.cur = addr / BLOCK_SIZE
        status.msg = "Cloning to radio..."
        radio.status_fn(status)

    # close comms with the radio
    _send(radio, "\x62")
    # DEBUG
    LOG.info("Close comms cmd sent, radio must reboot now.")


def _model_match(cls, data):
    """Match the opened/downloaded image to the correct version"""

    # a reliable fingerprint: the model name at
    rid = data[0x06f8:0x0700]

    if rid == BFT1_ident:
        return True

    return False


def _decode_ranges(low, high):
    """Unpack the data in the ranges zones in the memmap and return
    a tuple with the integer corresponding to the Mhz it means"""
    return (int(low) * 100000, int(high) * 100000)


MEM_FORMAT = """

struct channel {
  lbcd rxfreq[4];       // rx freq.
  u8 rxtone;            // x00 = none
                        // x01 - x32 = index of the analog tones
                        // x33 - x9b = index of Digital tones
                        // Digital tone polarity is handled below by
                        // ttondinv & ttondinv settings
  lbcd txoffset[4];     // the difference against RX, direction handled by
                        // offplus & offminus
  u8 txtone;            // Idem to rxtone
  u8 noskip:1,      // if true is included in the scan
     wide:1,        // 1 = Wide, 0 = Narrow
     ttondinv:1,    // if true TX tone is Digital & Inverted
     unA:1,         //
     rtondinv:1,    // if true RX tone is Digital & Inverted
     unB:1,         //
     offplus:1,     // TX = RX + offset
     offminus:1;    // TX = RX - offset
  u8 empty[5];
};

#seekto 0x0000;
struct channel emg;             // channel 0 is Emergent CH
#seekto 0x0010;
struct channel channels[20];    // normal 1-20 mem channels

#seekto 0x0150;     // Settings
struct {
  lbcd vhfl[2];     // VHF low limit
  lbcd vhfh[2];     // VHF high limit
  lbcd uhfl[2];     // UHF low limit
  lbcd uhfh[2];     // UHF high limit
  u8 unk0[8];
  u8 unk1[2];       // start of 0x0160 <=======
  u8 squelch;       // byte: 0-9
  u8 vox;           // byte: 0-9
  u8 timeout;       // tot, 0 off, then 30 sec increments up to 180
  u8 batsave:1,     // battery save 0 = off, 1 = on
     fm_funct:1,    // fm-radio 0=off, 1=on ( off disables fm button on set )
     ste:1,         // squelch tail 0 = off, 1 = on
     blo:1,         // busy lockout 0 = off, 1 = on
     beep:1,        // key beep 0 = off, 1 = on
     lock:1,        // keylock 0 = ff,  = on
     backlight:2;   // backlight 00 = off, 01 = key, 10 = on
  u8 scantype;      // scan type 0 = timed, 1 = carrier, 2 = stop
  u8 channel;       // active channel 1-20, setting it works on upload
  u8 fmrange;       // fm range 1 = low[65-76](ASIA), 0 = high[76-108](AMERICA)
  u8 alarm;         // alarm (count down timer)
                    //    d0 - d16 in half hour increments => off, 0.5 - 8.0 h
  u8 voice;         // voice prompt 0 = off, 1 = english, 2 = chinese
  u8 volume;        // volume 1-7 as per the radio steps
                    //    set to #FF by original software on upload
                    //    chirp uploads actual value and works.
  u16 fm_vfo;       // the frequency of the fm receiver.
                    //    resulting frequency is 65 + value * 0.1 MHz
                    //    0x145 is then 65 + 325*0.1 = 97.5 MHz
  u8 relaym;        // relay mode, d0 = off, d2 = re-tx, d1 = re-rx
                    //    still a mystery on how it works
  u8 tx_pwr;        // tx pwr 0 = low (0.5W), 1 = high(1.0W)
} settings;

#seekto 0x0170;     // Relay CH
struct channel rly;

"""


@directory.register
class BFT1(chirp_common.CloneModeRadio, chirp_common.ExperimentalRadio):
    """Baofeng BT-F1 radio & possibly alike radios"""
    VENDOR = "Baofeng"
    MODEL = "BF-T1"
    _vhf_range = (130000000, 174000000)
    _uhf_range = (400000000, 520000000)
    _upper = 20
    _magic = BFT1_magic
    _id = BFT1_ident

    @classmethod
    def get_prompts(cls):
        rp = chirp_common.RadioPrompts()
        rp.experimental = \
            ('This driver is experimental.\n'
             '\n'
             'Please keep a copy of your memories with the original software '
             'if you treasure them, this driver is new and may contain'
             ' bugs.\n'
             '\n'
             '"Emergent CH" & "Relay CH" are implemented via special channels,'
             'be sure to click on the button on the interface to access them.'
             )
        rp.pre_download = _(dedent("""\
            Follow these instructions to download your info:

            1 - Turn off your radio
            2 - Connect your interface cable
            3 - Turn on your radio
            4 - Do the download of your radio data

            """))
        rp.pre_upload = _(dedent("""\
            Follow these instructions to upload your info:

            1 - Turn off your radio
            2 - Connect your interface cable
            3 - Turn on your radio
            4 - Do the upload of your radio data

            """))
        return rp

    def get_features(self):
        """Get the radio's features"""

        rf = chirp_common.RadioFeatures()
        rf.valid_special_chans = list(SPECIALS.keys())
        rf.has_settings = True
        rf.has_bank = False
        rf.has_tuning_step = False
        rf.can_odd_split = True
        rf.has_name = False
        rf.has_offset = True
        rf.has_mode = True
        rf.valid_modes = MODES
        rf.has_dtcs = True
        rf.has_rx_dtcs = True
        rf.has_dtcs_polarity = True
        rf.has_ctone = True
        rf.has_cross = True
        rf.valid_duplexes = ["", "-", "+", "split"]
        rf.valid_tmodes = ['', 'Tone', 'TSQL', 'DTCS', 'Cross']
        rf.valid_cross_modes = [
            "Tone->Tone",
            "DTCS->",
            "->DTCS",
            "Tone->DTCS",
            "DTCS->Tone",
            "->Tone",
            "DTCS->DTCS"]
        rf.valid_skips = SKIP_VALUES
        rf.valid_dtcs_codes = DTCS
        rf.memory_bounds = (1, self._upper)
        rf.valid_tuning_steps = [2.5, 5., 6.25, 10., 12.5, 25.]

        # normal dual bands
        rf.valid_bands = [self._vhf_range, self._uhf_range]

        return rf

    def process_mmap(self):
        """Process the mem map into the mem object"""

        # Get it
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)

        # set the band limits as the memmap
        settings = self._memobj.settings
        self._vhf_range = _decode_ranges(settings.vhfl, settings.vhfh)
        self._uhf_range = _decode_ranges(settings.uhfl, settings.uhfh)

    def sync_in(self):
        """Download from radio"""
        data = _download(self)
        self._mmap = memmap.MemoryMap(data)
        self.process_mmap()

    def sync_out(self):
        """Upload to radio"""

        try:
            _upload(self)
        except errors.RadioError:
            raise
        except Exception as e:
            raise errors.RadioError("Error: %s" % e)

    def _decode_tone(self, val, inv):
        """Parse the tone data to decode from mem, it returns:
        Mode (''|DTCS|Tone), Value (None|###), Polarity (None,N,R)"""

        if val == 0:
            return '', None, None
        elif val < 51:  # analog tone
            return 'Tone', TONES[val - 1], None
        elif val > 50:  # digital tone
            pol = "N"
            # polarity?
            if inv == 1:
                pol = "R"

            return 'DTCS', DTCS[val - 51], pol

    def _encode_tone(self, memtone, meminv, mode, tone, pol):
        """Parse the tone data to encode from UI to mem"""

        if mode == '' or mode is None:
            memtone.set_value(0)
            meminv.set_value(0)
        elif mode == 'Tone':
            # caching errors for analog tones.
            try:
                memtone.set_value(TONES.index(tone) + 1)
                meminv.set_value(0)
            except:
                msg = "TCSS Tone '%d' is not supported" % tone
                LOG.error(msg)
                raise errors.RadioError(msg)

        elif mode == 'DTCS':
            # caching errors for digital tones.
            try:
                memtone.set_value(DTCS.index(tone) + 51)
                if pol == "R":
                    meminv.set_value(True)
                else:
                    meminv.set_value(False)
            except:
                msg = "Digital Tone '%d' is not supported" % tone
                LOG.error(msg)
                raise errors.RadioError(msg)
        else:
            msg = "Internal error: invalid mode '%s'" % mode
            LOG.error(msg)
            raise errors.InvalidDataError(msg)

    def get_raw_memory(self, number):
        return repr(self._memobj.memory[number])

    def _get_special(self, number):
        if isinstance(number, str):
            return (getattr(self._memobj, number.lower()))
        elif number < 0:
            for k, v in SPECIALS.items():
                if number == v:
                    return (getattr(self._memobj, k.lower()))
        else:
            return self._memobj.channels[number-1]

    def get_memory(self, number):
        """Get the mem representation from the radio image"""
        _mem = self._get_special(number)

        # Create a high-level memory object to return to the UI
        mem = chirp_common.Memory()

        # Check if special or normal
        if isinstance(number, str):
            mem.number = SPECIALS[number]
            mem.extd_number = number
        else:
            mem.number = number

        if _mem.get_raw()[0] == "\xFF":
            mem.empty = True
            return mem

        # Freq and offset
        mem.freq = int(_mem.rxfreq) * 10

        # TX freq (Stored as a difference)
        mem.offset = int(_mem.txoffset) * 10
        mem.duplex = ""

        # must work out the polarity
        if mem.offset != 0:
            if _mem.offminus == 1:
                mem.duplex = "-"
                #  tx below RX

            if _mem.offplus == 1:
                #  tx above RX
                mem.duplex = "+"

            # split RX/TX in different bands
            if mem.offset > 71000000:
                mem.duplex = "split"

                # show the actual value in the offset, depending on the shift
                if _mem.offminus == 1:
                    mem.offset = mem.freq - mem.offset
                if _mem.offplus == 1:
                    mem.offset = mem.freq + mem.offset

        # wide/narrow
        mem.mode = MODES[int(_mem.wide)]

        # skip
        mem.skip = SKIP_VALUES[_mem.noskip]

        # tone data
        rxtone = txtone = None
        txtone = self._decode_tone(_mem.txtone, _mem.ttondinv)
        rxtone = self._decode_tone(_mem.rxtone, _mem.rtondinv)
        chirp_common.split_tone_decode(mem, txtone, rxtone)

        return mem

    def set_memory(self, mem):
        """Set the memory data in the eeprom img from the UI"""
        # get the eprom representation of this channel
        _mem = self._get_special(mem.number)

        # if empty memmory
        if mem.empty:
            # the channel itself
            _mem.set_raw("\xFF" * 16)
            # return it
            return mem

        # frequency
        _mem.rxfreq = mem.freq / 10

        # duplex/ offset Offset is an absolute value
        _mem.txoffset = mem.offset / 10

        # must work out the polarity
        if mem.duplex == "":
            _mem.offplus = 0
            _mem.offminus = 0
        elif mem.duplex == "+":
            _mem.offplus = 1
            _mem.offminus = 0
        elif mem.duplex == "-":
            _mem.offplus = 0
            _mem.offminus = 1
        elif mem.duplex == "split":
            if mem.freq > mem.offset:
                _mem.offplus = 0
                _mem.offminus = 1
                _mem.txoffset = (mem.freq - mem.offset) / 10
            else:
                _mem.offplus = 1
                _mem.offminus = 0
                _mem.txoffset = (mem.offset - mem.freq) / 10

        # wide/narrow
        _mem.wide = MODES.index(mem.mode)

        # skip
        _mem.noskip = SKIP_VALUES.index(mem.skip)

        # tone data
        ((txmode, txtone, txpol), (rxmode, rxtone, rxpol)) = \
            chirp_common.split_tone_encode(mem)
        self._encode_tone(_mem.txtone, _mem.ttondinv, txmode, txtone, txpol)
        self._encode_tone(_mem.rxtone, _mem.rtondinv, rxmode, rxtone, rxpol)

        return mem

    def get_settings(self):
        _settings = self._memobj.settings
        basic = RadioSettingGroup("basic", "Basic Settings")
        fm = RadioSettingGroup("fm", "FM Radio")
        adv = RadioSettingGroup("adv", "Advanced Settings")
        group = RadioSettings(basic, fm, adv)

        # ## Basic Settings
        rs = RadioSetting("tx_pwr", "TX Power",
                          RadioSettingValueList(
                            POWER_LIST, POWER_LIST[_settings.tx_pwr]))
        basic.append(rs)

        rs = RadioSetting("channel", "Active Channel",
                          RadioSettingValueInteger(1, 20, _settings.channel))
        basic.append(rs)

        rs = RadioSetting("squelch", "Squelch Level",
                          RadioSettingValueInteger(0, 9, _settings.squelch))
        basic.append(rs)

        rs = RadioSetting("vox", "VOX Level",
                          RadioSettingValueInteger(0, 9, _settings.vox))
        basic.append(rs)

        # volume validation, as the OEM software set 0xFF on write
        _volume = _settings.volume
        if _volume > 7:
            _volume = 7
        rs = RadioSetting("volume", "Volume Level",
                          RadioSettingValueInteger(0, 7, _volume))
        basic.append(rs)

        rs = RadioSetting("scantype", "Scan Type",
                          RadioSettingValueList(SCAN_TYPE_LIST, SCAN_TYPE_LIST[
                              _settings.scantype]))
        basic.append(rs)

        rs = RadioSetting("timeout", "Time Out Timer (seconds)",
                          RadioSettingValueList(
                            TOT_LIST, TOT_LIST[_settings.timeout]))
        basic.append(rs)

        rs = RadioSetting("voice", "Voice Prompt",
                          RadioSettingValueList(
                            LANGUAGE_LIST, LANGUAGE_LIST[_settings.voice]))
        basic.append(rs)

        rs = RadioSetting("alarm", "Alarm Time",
                          RadioSettingValueList(
                            TIMER_LIST, TIMER_LIST[_settings.alarm]))
        basic.append(rs)

        rs = RadioSetting("backlight", "Backlight",
                          RadioSettingValueList(
                            BACKLIGHT_LIST,
                            BACKLIGHT_LIST[_settings.backlight]))
        basic.append(rs)

        rs = RadioSetting("blo", "Busy Lockout",
                          RadioSettingValueBoolean(_settings.blo))
        basic.append(rs)

        rs = RadioSetting("ste", "Squelch Tail Eliminate",
                          RadioSettingValueBoolean(_settings.ste))
        basic.append(rs)

        rs = RadioSetting("batsave", "Battery Save",
                          RadioSettingValueBoolean(_settings.batsave))
        basic.append(rs)

        rs = RadioSetting("lock", "Key Lock",
                          RadioSettingValueBoolean(_settings.lock))
        basic.append(rs)

        rs = RadioSetting("beep", "Key Beep",
                          RadioSettingValueBoolean(_settings.beep))
        basic.append(rs)

        # ## FM Settings
        rs = RadioSetting("fm_funct", "FM Function",
                          RadioSettingValueBoolean(_settings.fm_funct))
        fm.append(rs)

        rs = RadioSetting("fmrange", "FM Range",
                          RadioSettingValueList(
                            FM_RANGE_LIST, FM_RANGE_LIST[_settings.fmrange]))
        fm.append(rs)

        # callbacks for the FM VFO
        def apply_fm_freq(setting, obj):
            setattr(obj, setting.get_name(), int(setting.value.
                get_value() * 10) - 650)

        _fm_vfo = int(_settings.fm_vfo) * 0.1 + 65
        rs = RadioSetting("fm_vfo", "FM Station",
                          RadioSettingValueFloat(65, 108, _fm_vfo))
        rs.set_apply_callback(apply_fm_freq, _settings)
        fm.append(rs)

        # ## Advanced
        def apply_limit(setting, obj):
            setattr(obj, setting.get_name(), int(setting.value) * 10)

        rs = RadioSetting("vhfl", "VHF Low Limit",
                          RadioSettingValueInteger(130, 174, int(
                              _settings.vhfl) / 10))
        rs.set_apply_callback(apply_limit, _settings)
        adv.append(rs)

        rs = RadioSetting("vhfh", "VHF High Limit",
                          RadioSettingValueInteger(130, 174, int(
                              _settings.vhfh) / 10))
        rs.set_apply_callback(apply_limit, _settings)
        adv.append(rs)

        rs = RadioSetting("uhfl", "UHF Low Limit",
                          RadioSettingValueInteger(400, 520, int(
                              _settings.uhfl) / 10))
        rs.set_apply_callback(apply_limit, _settings)
        adv.append(rs)

        rs = RadioSetting("uhfh", "UHF High Limit",
                          RadioSettingValueInteger(400, 520, int(
                              _settings.uhfh) / 10))
        rs.set_apply_callback(apply_limit, _settings)
        adv.append(rs)

        rs = RadioSetting("relaym", "Relay Mode",
                          RadioSettingValueList(RELAY_MODE_LIST,
                              RELAY_MODE_LIST[_settings.relaym]))
        adv.append(rs)

        return group

    def set_settings(self, uisettings):
        _settings = self._memobj.settings

        for element in uisettings:
            if not isinstance(element, RadioSetting):
                self.set_settings(element)
                continue
            if not element.changed():
                continue

            try:
                name = element.get_name()
                value = element.value

                if element.has_apply_callback():
                    LOG.debug("Using apply callback")
                    element.run_apply_callback()
                else:
                    obj = getattr(_settings, name)
                    setattr(_settings, name, value)

                LOG.debug("Setting %s: %s" % (name, value))
            except Exception as e:
                LOG.debug(element.get_name())
                raise

    @classmethod
    def match_model(cls, filedata, filename):
        match_size = False
        match_model = False

        # testing the file data size
        if len(filedata) == MEM_SIZE:
            match_size = True

            # DEBUG
            if debug is True:
                LOG.debug("BF-T1 matched!")

        # testing the firmware model fingerprint
        match_model = _model_match(cls, filedata)

        return match_size and match_model