Project

General

Profile

Bug #8245 » tk760.py

test driver module to use while waiting for approval - Jim Unroe, 02/23/2021 06:55 PM

 
1
# Copyright 2016 Pavel Milanes CO7WT, <co7wt@frcuba.co.cu> <pavelmc@gmail.com>
2
#
3
# This program is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation, either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15

    
16
from chirp import chirp_common, directory, memmap
17
from chirp import bitwise, errors, util
18
from chirp.settings import RadioSettingGroup, RadioSetting, \
19
    RadioSettingValueBoolean, RadioSettingValueList, \
20
    RadioSettingValueString, RadioSettingValueInteger, \
21
    RadioSettings
22
from textwrap import dedent
23

    
24
import time
25
import struct
26
import logging
27

    
28
LOG = logging.getLogger(__name__)
29

    
30
MEM_FORMAT = """
31
#seekto 0x0000;
32
struct {
33
  lbcd rxfreq[4];
34
  lbcd txfreq[4];
35
} memory[32];
36

    
37
#seekto 0x0100;
38
struct {
39
  lbcd rx_tone[2];
40
  lbcd tx_tone[2];
41
} tone[32];
42

    
43
#seekto 0x0180;
44
struct {
45
  u8 unknown0:1,
46
     unknown1:1,
47
     wide:1,            // wide: 1 = wide, 0 = narrow
48
     power:1,           // power: 1 = high, 0 = low
49
     busy_lock:1,       // busy lock:  1 = off, 0 = on
50
     pttid:1,           // ptt id:  1 = off, 0 = on
51
     dtmf:1,            // dtmf signaling:  1 = off, 0 = on
52
     twotone:1;         // 2-tone signaling:  1 = off, 0 = on
53
} ch_settings[32];
54

    
55
#seekto 0x02B0;
56
struct {
57
    u8 unknown10[16];      // x02b0
58
    u8 unknown11[16];      // x02c0
59
    u8 active[4];            // x02d0
60
    u8 scan[4];              // x02d4
61
    u8 unknown12[8];         // x02d8
62
    u8 unknown13;          // x02e0
63
    u8 kMON;                 // 0x02d1 MON Key
64
    u8 kA;                   // 0x02d2 A Key
65
    u8 kSCN;                 // 0x02d3 SCN Key
66
    u8 kDA;                  // 0x02d4 D/A Key
67
    u8 unknown14;            // x02e5
68
    u8 min_vol;              // x02e6 byte 0-31 0 = off
69
    u8 poweron_tone;         // x02e7 power on tone 0 = off, 1 = on
70
    u8 tot;                  // x02e8 Time out Timer 0 = off, 1 = 30s (max 300)
71
    u8 unknown15[3];         // x02e9-x02eb
72
    u8 dealer_tuning;        // x02ec ? bit 0? 0 = off, 1 = on
73
    u8 clone;                // x02ed ? bit 0? 0 = off, 1 = on
74
    u8 unknown16[2];         // x02ee-x2ef
75
    u8 unknown17[16];      // x02f0
76
    u8 unknown18[5];       // x0300
77
    u8 clear2transpond;      // x0305 byte 0 = off, 1 = on
78
    u8 off_hook_decode;      // x0306 byte 0 = off, 1 = on
79
    u8 off_hook_hornalert;   // x0307 byte 0 = off, 1 = on
80
    u8 unknown19[8];         // x0308-x030f
81
    u8 unknown20[16];      // x0310
82
} settings;
83
"""
84

    
85
KEYS = {
86
    0x00: "Disabled",
87
    0x01: "Monitor",
88
    0x02: "Talk Around",
89
    0x03: "Horn Alert",
90
    0x04: "Public Adress",
91
    0x05: "Auxiliary",
92
    0x06: "Scan",
93
    0x07: "Scan Del/Add",
94
    0x08: "Home Channel",
95
    0x09: "Operator Selectable Tone",
96
    0x0C: "Unknown"
97
}
98

    
99
MEM_SIZE = 0x400
100
BLOCK_SIZE = 8
101
MEM_BLOCKS = range(0, (MEM_SIZE / BLOCK_SIZE))
102
ACK_CMD = "\x06"
103
# from 0.03 up it' s safe
104
# I have to turn it up, some users reported problems with this, was 0.05
105
TIMEOUT = 0.1
106

    
107
POWER_LEVELS = [chirp_common.PowerLevel("Low", watts=1),
108
                chirp_common.PowerLevel("High", watts=5)]
109
MODES = ["NFM", "FM"]
110
SKIP_VALUES = ["", "S"]
111
TONES = chirp_common.TONES
112
# TONES.remove(254.1)
113
DTCS_CODES = chirp_common.DTCS_CODES
114

    
115
TOT = ["off"] + ["%s" % x for x in range(30, 330, 30)]
116
VOL = ["off"] + ["%s" % x for x in range(1, 32)]
117

    
118

    
119
def rawrecv(radio, amount):
120
    """Raw read from the radio device"""
121
    data = ""
122
    try:
123
        data = radio.pipe.read(amount)
124
        # print("<= %02i: %s" % (len(data), util.hexprint(data)))
125
    except:
126
        raise errors.RadioError("Error reading data from radio")
127

    
128
    return data
129

    
130

    
131
def rawsend(radio, data):
132
    """Raw send to the radio device"""
133
    try:
134
        radio.pipe.write(data)
135
        # print("=> %02i: %s" % (len(data), util.hexprint(data)))
136
    except:
137
        raise errors.RadioError("Error sending data from radio")
138

    
139

    
140
def send(radio, frame):
141
    """Generic send data to the radio"""
142
    rawsend(radio, frame)
143

    
144

    
145
def make_frame(cmd, addr, data=""):
146
    """Pack the info in the format it likes"""
147
    ts = struct.pack(">BHB", ord(cmd), addr, 8)
148
    if data == "":
149
        return ts
150
    else:
151
        if len(data) == 8:
152
            return ts + data
153
        else:
154
            raise errors.InvalidValueError("Data length of unexpected length")
155

    
156

    
157
def handshake(radio, msg="", full=False):
158
    """Make a full handshake, if not full just hals"""
159
    # send ACK if commandes
160
    if full is True:
161
        rawsend(radio, ACK_CMD)
162
    # receive ACK
163
    ack = rawrecv(radio, 1)
164
    # check ACK
165
    if ack != ACK_CMD:
166
        # close_radio(radio)
167
        mesg = "Handshake failed: " + msg
168
        raise errors.RadioError(mesg)
169

    
170

    
171
def recv(radio):
172
    """Receive data from the radio, 12 bytes, 4 in the header, 8 as data"""
173
    rxdata = rawrecv(radio, 12)
174

    
175
    if len(rxdata) != 12:
176
        raise errors.RadioError(
177
            "Received a length of data that is not possible")
178
        return
179

    
180
    cmd, addr, length = struct.unpack(">BHB", rxdata[0:4])
181
    data = ""
182
    if length == 8:
183
        data = rxdata[4:]
184

    
185
    return data
186

    
187

    
188
def open_radio(radio):
189
    """Open the radio into program mode and check if it's the correct model"""
190
    # Set serial discipline
191
    try:
192
        radio.pipe.parity = "N"
193
        radio.pipe.timeout = TIMEOUT
194
        radio.pipe.flush()
195
        LOG.debug("Serial port open successful")
196
    except:
197
        msg = "Serial error: Can't set serial line discipline"
198
        raise errors.RadioError(msg)
199

    
200
    magic = "PROGRAM"
201
    LOG.debug("Sending MAGIC")
202
    exito = False
203

    
204
    # it appears that some buggy interfaces/serial devices keep sending
205
    # data in the RX line, we will try to catch this garbage here
206
    devnull = rawrecv(radio, 256)
207

    
208
    for i in range(0, 5):
209
        LOG.debug("Try %i" % i)
210
        for i in range(0, len(magic)):
211
            ack = rawrecv(radio, 1)
212
            time.sleep(0.05)
213
            send(radio, magic[i])
214

    
215
        try:
216
            handshake(radio, "Radio not entering Program mode")
217
            LOG.debug("Radio opened for programming")
218
            exito = True
219
            break
220
        except:
221
            LOG.debug("No go, next try")
222
            pass
223

    
224
    # validate the success
225
    if exito is False:
226
        msg = "Radio refuse to enter into program mode after a few tries"
227
        raise errors.RadioError(msg)
228

    
229
    rawsend(radio, "\x02")
230
    ident = rawrecv(radio, 8)
231

    
232
    # validate the input
233
    if len(ident) != 8:
234
        LOG.debug("Wrong ID, get only %s bytes, we expect 8" % len(ident))
235
        LOG.debug(hexprint(ident))
236
        msg = "Bad ID received, just %s bytes, we want 8" % len(ident)
237
        raise errors.RadioError(msg)
238

    
239
    handshake(radio, "Comm error after ident", True)
240
    LOG.debug("Correct get ident and hanshake")
241

    
242
    if not (radio.TYPE in ident):
243
        LOG.debug("Incorrect model ID:")
244
        LOG.debug(util.hexprint(ident))
245
        msg = "Incorrect model ID, got %s, it not contains %s" % \
246
            (ident[0:5], radio.TYPE)
247
        raise errors.RadioError(msg)
248

    
249
    LOG.debug("Full ident string is:")
250
    LOG.debug(util.hexprint(ident))
251

    
252

    
253
def do_download(radio):
254
    """This is your download function"""
255
    open_radio(radio)
256

    
257
    # UI progress
258
    status = chirp_common.Status()
259
    status.cur = 0
260
    status.max = MEM_SIZE / BLOCK_SIZE
261
    status.msg = "Cloning from radio..."
262
    radio.status_fn(status)
263

    
264
    data = ""
265
    LOG.debug("Starting the downolad")
266
    for addr in MEM_BLOCKS:
267
        send(radio, make_frame("R", addr * BLOCK_SIZE))
268
        data += recv(radio)
269
        handshake(radio, "Rx error in block %03i" % addr, True)
270
        LOG.debug("Block: %04x, Pos: %06x" % (addr, addr * BLOCK_SIZE))
271

    
272
        # UI Update
273
        status.cur = addr
274
        status.msg = "Cloning from radio..."
275
        radio.status_fn(status)
276

    
277
    return memmap.MemoryMap(data)
278

    
279

    
280
def do_upload(radio):
281
    """Upload info to radio"""
282
    open_radio(radio)
283

    
284
    # UI progress
285
    status = chirp_common.Status()
286
    status.cur = 0
287
    status.max = MEM_SIZE / BLOCK_SIZE
288
    status.msg = "Cloning to radio..."
289
    radio.status_fn(status)
290
    count = 0
291

    
292
    for addr in MEM_BLOCKS:
293
        # UI Update
294
        status.cur = addr
295
        status.msg = "Cloning to radio..."
296
        radio.status_fn(status)
297

    
298
        pos = addr * BLOCK_SIZE
299
        if pos > 0x0378:
300
            # it seems that from this point forward is read only !?!?!?
301
            continue
302

    
303
        data = radio.get_mmap()[pos:pos + BLOCK_SIZE]
304
        send(radio, make_frame("W", pos, data))
305
        LOG.debug("Block: %04x, Pos: %06x" % (addr, pos))
306

    
307
        time.sleep(0.1)
308
        handshake(radio, "Rx error in block %04x" % addr)
309

    
310

    
311
def get_rid(data):
312
    """Extract the radio identification from the firmware"""
313
    rid = data[0x0378:0x0380]
314
    # we have to invert rid
315
    nrid = ""
316
    for i in range(1, len(rid) + 1):
317
        nrid += rid[-i]
318
    rid = nrid
319

    
320
    return rid
321

    
322

    
323
def model_match(cls, data):
324
    """Match the opened/downloaded image to the correct version"""
325
    rid = get_rid(data)
326

    
327
    # DEBUG
328
    # print("Full ident string is %s" % util.hexprint(rid))
329

    
330
    if (rid in cls.VARIANTS):
331
        # correct model
332
        return True
333
    else:
334
        return False
335

    
336

    
337
class Kenwood_M60_Radio(chirp_common.CloneModeRadio,
338
                        chirp_common.ExperimentalRadio):
339
    """Kenwood Mobile Family 60 Radios"""
340
    VENDOR = "Kenwood"
341
    _range = [136000000, 500000000]  # don't mind, it will be overwritten
342
    _upper = 32
343
    VARIANT = ""
344
    MODEL = ""
345

    
346
    @classmethod
347
    def get_prompts(cls):
348
        rp = chirp_common.RadioPrompts()
349
        rp.experimental = \
350
            ('This driver is experimental; not all features have been '
351
             'implemented, but it has those features most used by hams.\n'
352
             '\n'
353
             'This radios are able to work slightly outside the OEM '
354
             'frequency limits. After testing, the limit in Chirp has '
355
             'been set 4% outside the OEM limit. This allows you to use '
356
             'some models on the ham bands.\n'
357
             '\n'
358
             'Nevertheless, each radio has its own hardware limits and '
359
             'your mileage may vary.\n'
360
             )
361
        rp.pre_download = _(dedent("""\
362
            Follow this instructions to read your radio:
363
            1 - Turn off your radio
364
            2 - Connect your interface cable
365
            3 - Turn on your radio
366
            4 - Do the download of your radio data
367
            """))
368
        rp.pre_upload = _(dedent("""\
369
            Follow this instructions to write your radio:
370
            1 - Turn off your radio
371
            2 - Connect your interface cable
372
            3 - Turn on your radio
373
            4 - Do the upload of your radio data
374
            """))
375
        return rp
376

    
377
    def get_features(self):
378
        rf = chirp_common.RadioFeatures()
379
        rf.has_settings = True
380
        rf.has_bank = False
381
        rf.has_tuning_step = False
382
        rf.has_name = False
383
        rf.has_offset = True
384
        rf.has_mode = True
385
        rf.has_dtcs = True
386
        rf.has_rx_dtcs = True
387
        rf.has_dtcs_polarity = True
388
        rf.has_ctone = True
389
        rf.has_cross = True
390
        rf.valid_modes = MODES
391
        rf.valid_duplexes = ["", "-", "+", "off"]
392
        rf.valid_tmodes = ['', 'Tone', 'TSQL', 'DTCS', 'Cross']
393
        rf.valid_cross_modes = [
394
            "Tone->Tone",
395
            "DTCS->",
396
            "->DTCS",
397
            "Tone->DTCS",
398
            "DTCS->Tone",
399
            "->Tone",
400
            "DTCS->DTCS"]
401
        rf.valid_power_levels = POWER_LEVELS
402
        rf.valid_skips = SKIP_VALUES
403
        rf.valid_dtcs_codes = DTCS_CODES
404
        rf.valid_bands = [self._range]
405
        rf.memory_bounds = (1, self._upper)
406
        rf.valid_tuning_steps = [5., 6.25, 10., 12.5]
407
        return rf
408

    
409
    def sync_in(self):
410
        """Download from radio"""
411
        self._mmap = do_download(self)
412
        self.process_mmap()
413

    
414
    def sync_out(self):
415
        """Upload to radio"""
416
        # Get the data ready for upload
417
        try:
418
            self._prep_data()
419
        except:
420
            raise errors.RadioError("Error processing the radio data")
421

    
422
        # do the upload
423
        try:
424
            do_upload(self)
425
        except:
426
            raise errors.RadioError("Error uploading data to radio")
427

    
428
    def set_variant(self):
429
        """Select and set the correct variables for the class acording
430
        to the correct variant of the radio"""
431
        rid = get_rid(self._mmap)
432

    
433
        # indentify the radio variant and set the enviroment to it's values
434
        try:
435
            self._upper, low, high, self._kind = self.VARIANTS[rid]
436

    
437
            # Frequency ranges: some model/variants are able to work the near
438
            # ham bands, even if they are outside the OEM ranges.
439
            # By experimentation we found that a +/- 4% at the edges is in most
440
            # cases safe and will cover the near ham band in full
441
            self._range = [low * 1000000 * 0.96, high * 1000000 * 1.04]
442

    
443
            # put the VARIANT in the class, clean the model / CHs / Type
444
            # in the same layout as the KPG program
445
            self._VARIANT = self.MODEL + " [" + str(self._upper) + "CH]: "
446
            # In the OEM string we show the real OEM ranges
447
            self._VARIANT += self._kind + ", %d - %d MHz" % (low, high)
448

    
449
        except KeyError:
450
            LOG.debug("Wrong Kenwood radio, ID or unknown variant")
451
            LOG.debug(util.hexprint(rid))
452
            raise errors.RadioError(
453
                "Wrong Kenwood radio, ID or unknown variant, see LOG output.")
454

    
455
    def _prep_data(self):
456
        """Prepare the areas in the memmap to do a consistend write
457
        it has to make an update on the x200 flag data"""
458
        achs = 0
459

    
460
        for i in range(0, self._upper):
461
            if self.get_active(i) is True:
462
                achs += 1
463

    
464
        # The x0200 area has the settings for the DTMF/2-Tone per channel,
465
        # as by default any of this radios has the DTMF IC installed;
466
        # we clean this areas
467
        fldata = "\x00\xf0\xff\xff\xff" * achs + \
468
            "\xff" * (5 * (self._upper - achs))
469
        self._fill(0x0200, fldata)
470

    
471
    def _fill(self, offset, data):
472
        """Fill an specified area of the memmap with the passed data"""
473
        for addr in range(0, len(data)):
474
            self._mmap[offset + addr] = data[addr]
475

    
476
    def process_mmap(self):
477
        """Process the mem map into the mem object"""
478
        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
479
        # to set the vars on the class to the correct ones
480
        self.set_variant()
481

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

    
485
    def decode_tone(self, val):
486
        """Parse the tone data to decode from mem, it returns:
487
        Mode (''|DTCS|Tone), Value (None|###), Polarity (None,N,R)"""
488
        if val.get_raw() == "\xFF\xFF":
489
            return '', None, None
490

    
491
        val = int(val)
492
        if val >= 12000:
493
            a = val - 12000
494
            return 'DTCS', a, 'R'
495
        elif val >= 8000:
496
            a = val - 8000
497
            return 'DTCS', a, 'N'
498
        else:
499
            a = val / 10.0
500
            return 'Tone', a, None
501

    
502
    def encode_tone(self, memval, mode, value, pol):
503
        """Parse the tone data to encode from UI to mem"""
504
        if mode == '':
505
            memval[0].set_raw(0xFF)
506
            memval[1].set_raw(0xFF)
507
        elif mode == 'Tone':
508
            memval.set_value(int(value * 10))
509
        elif mode == 'DTCS':
510
            flag = 0x80 if pol == 'N' else 0xC0
511
            memval.set_value(value)
512
            memval[1].set_bits(flag)
513
        else:
514
            raise Exception("Internal error: invalid mode `%s'" % mode)
515

    
516
    def get_scan(self, chan):
517
        """Get the channel scan status from the 4 bytes array on the eeprom
518
        then from the bits on the byte, return '' or 'S' as needed"""
519
        result = "S"
520
        byte = int(chan/8)
521
        bit = chan % 8
522
        res = self._memobj.settings.scan[byte] & (pow(2, bit))
523
        if res > 0:
524
            result = ""
525

    
526
        return result
527

    
528
    def set_scan(self, chan, value):
529
        """Set the channel scan status from UI to the mem_map"""
530
        byte = int(chan/8)
531
        bit = chan % 8
532

    
533
        # get the actual value to see if I need to change anything
534
        actual = self.get_scan(chan)
535
        if actual != value:
536
            # I have to flip the value
537
            rbyte = self._memobj.settings.scan[byte]
538
            rbyte = rbyte ^ pow(2, bit)
539
            self._memobj.settings.scan[byte] = rbyte
540

    
541
    def get_active(self, chan):
542
        """Get the channel active status from the 4 bytes array on the eeprom
543
        then from the bits on the byte, return True/False"""
544
        byte = int(chan/8)
545
        bit = chan % 8
546
        res = self._memobj.settings.active[byte] & (pow(2, bit))
547
        return bool(res)
548

    
549
    def set_active(self, chan, value=True):
550
        """Set the channel active status from UI to the mem_map"""
551
        byte = int(chan/8)
552
        bit = chan % 8
553

    
554
        # get the actual value to see if I need to change anything
555
        actual = self.get_active(chan)
556
        if actual != bool(value):
557
            # I have to flip the value
558
            rbyte = self._memobj.settings.active[byte]
559
            rbyte = rbyte ^ pow(2, bit)
560
            self._memobj.settings.active[byte] = rbyte
561

    
562
    def get_memory(self, number):
563
        """Get the mem representation from the radio image"""
564
        _mem = self._memobj.memory[number - 1]
565
        _tone = self._memobj.tone[number - 1]
566
        _ch = self._memobj.ch_settings[number - 1]
567

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

    
571
        # Memory number
572
        mem.number = number
573

    
574
        if _mem.get_raw()[0] == "\xFF" or not self.get_active(number - 1):
575
            mem.empty = True
576
            # but is not enough, you have to crear the memory in the mmap
577
            # to get it ready for the sync_out process
578
            _mem.set_raw("\xFF" * 8)
579
            return mem
580

    
581
        # Freq and offset
582
        mem.freq = int(_mem.rxfreq) * 10
583
        # tx freq can be blank
584
        if _mem.get_raw()[4] == "\xFF":
585
            # TX freq not set
586
            mem.offset = 0
587
            mem.duplex = "off"
588
        else:
589
            # TX feq set
590
            offset = (int(_mem.txfreq) * 10) - mem.freq
591
            if offset < 0:
592
                mem.offset = abs(offset)
593
                mem.duplex = "-"
594
            elif offset > 0:
595
                mem.offset = offset
596
                mem.duplex = "+"
597
            else:
598
                mem.offset = 0
599

    
600
        # power
601
        mem.power = POWER_LEVELS[_ch.power]
602

    
603
        # wide/marrow
604
        mem.mode = MODES[_ch.wide]
605

    
606
        # skip
607
        mem.skip = self.get_scan(number - 1)
608

    
609
        # tone data
610
        rxtone = txtone = None
611
        txtone = self.decode_tone(_tone.tx_tone)
612
        rxtone = self.decode_tone(_tone.rx_tone)
613
        chirp_common.split_tone_decode(mem, txtone, rxtone)
614

    
615
        # Extra
616
        # bank and number in the channel
617
        mem.extra = RadioSettingGroup("extra", "Extra")
618

    
619
        bl = RadioSetting("busy_lock", "Busy Channel lock",
620
                          RadioSettingValueBoolean(
621
                              not bool(_ch.busy_lock)))
622
        mem.extra.append(bl)
623

    
624
        return mem
625

    
626
    def set_memory(self, mem):
627
        """Set the memory data in the eeprom img from the UI
628
        not ready yet, so it will return as is"""
629

    
630
        # Get a low-level memory object mapped to the image
631
        _mem = self._memobj.memory[mem.number - 1]
632
        _tone = self._memobj.tone[mem.number - 1]
633
        _ch = self._memobj.ch_settings[mem.number - 1]
634

    
635
        # Empty memory
636
        if mem.empty:
637
            _mem.set_raw("\xFF" * 8)
638
            # empty the active bit
639
            self.set_active(mem.number - 1, False)
640
            return
641

    
642
        # freq rx
643
        _mem.rxfreq = mem.freq / 10
644

    
645
        # freq tx
646
        if mem.duplex == "+":
647
            _mem.txfreq = (mem.freq + mem.offset) / 10
648
        elif mem.duplex == "-":
649
            _mem.txfreq = (mem.freq - mem.offset) / 10
650
        elif mem.duplex == "off":
651
            for byte in _mem.txfreq:
652
                byte.set_raw("\xFF")
653
        else:
654
            _mem.txfreq = mem.freq / 10
655

    
656
        # tone data
657
        ((txmode, txtone, txpol), (rxmode, rxtone, rxpol)) = \
658
            chirp_common.split_tone_encode(mem)
659
        self.encode_tone(_tone.tx_tone, txmode, txtone, txpol)
660
        self.encode_tone(_tone.rx_tone, rxmode, rxtone, rxpol)
661

    
662
        # power, default power is low
663
        if mem.power is None:
664
            mem.power = POWER_LEVELS[0]
665

    
666
        _ch.power = POWER_LEVELS.index(mem.power)
667

    
668
        # wide/marrow
669
        _ch.wide = MODES.index(mem.mode)
670

    
671
        # skip
672
        self.set_scan(mem.number - 1, mem.skip)
673

    
674
        # extra settings
675
        for setting in mem.extra:
676
            setattr(_mem, setting.get_name(), setting.value)
677

    
678
        # set the mem a active in the _memmap
679
        self.set_active(mem.number - 1)
680

    
681
        return mem
682

    
683
    @classmethod
684
    def match_model(cls, filedata, filename):
685
        match_size = False
686
        match_model = False
687

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

    
692
        # testing the firmware model fingerprint
693
        match_model = model_match(cls, filedata)
694

    
695
        if match_size and match_model:
696
            return True
697
        else:
698
            return False
699

    
700
    def get_settings(self):
701
        """Translate the bit in the mem_struct into settings in the UI"""
702
        sett = self._memobj.settings
703

    
704
        # basic features of the radio
705
        basic = RadioSettingGroup("basic", "Basic Settings")
706
        # buttons
707
        fkeys = RadioSettingGroup("keys", "Front keys config")
708

    
709
        top = RadioSettings(basic, fkeys)
710

    
711
        # Basic
712
        val = RadioSettingValueString(0, 35, self._VARIANT)
713
        val.set_mutable(False)
714
        mod = RadioSetting("not.mod", "Radio version", val)
715
        basic.append(mod)
716

    
717
        tot = RadioSetting("settings.tot", "Time Out Timer (TOT)",
718
                           RadioSettingValueList(TOT, TOT[int(sett.tot)]))
719
        basic.append(tot)
720

    
721
        minvol = RadioSetting("settings.min_vol", "Minimum volume",
722
                              RadioSettingValueList(VOL,
723
                                                    VOL[int(sett.min_vol)]))
724
        basic.append(minvol)
725

    
726
        ptone = RadioSetting("settings.poweron_tone", "Power On tone",
727
                             RadioSettingValueBoolean(
728
                                 bool(sett.poweron_tone)))
729
        basic.append(ptone)
730

    
731
        sprog = RadioSetting("settings.dealer_tuning", "Dealer Tuning",
732
                             RadioSettingValueBoolean(
733
                                 bool(sett.dealer_tuning)))
734
        basic.append(sprog)
735

    
736
        clone = RadioSetting("settings.clone", "Allow clone",
737
                             RadioSettingValueBoolean(
738
                                 bool(sett.clone)))
739
        basic.append(clone)
740

    
741
        # front keys
742
        rs = RadioSettingValueList(KEYS.values(),
743
                                   KEYS.values()[KEYS.keys().index(
744
                                       int(sett.kMON))])
745
        mon = RadioSetting("settings.kMON", "MON", rs)
746
        fkeys.append(mon)
747

    
748
        rs = RadioSettingValueList(KEYS.values(),
749
                                   KEYS.values()[KEYS.keys().index(
750
                                       int(sett.kA))])
751
        a = RadioSetting("settings.kA", "A", rs)
752
        fkeys.append(a)
753

    
754
        rs = RadioSettingValueList(KEYS.values(),
755
                                   KEYS.values()[KEYS.keys().index(
756
                                       int(sett.kSCN))])
757
        scn = RadioSetting("settings.kSCN", "SCN", rs)
758
        fkeys.append(scn)
759

    
760
        rs = RadioSettingValueList(KEYS.values(),
761
                                   KEYS.values()[KEYS.keys().index(
762
                                       int(sett.kDA))])
763
        da = RadioSetting("settings.kDA", "D/A", rs)
764
        fkeys.append(da)
765

    
766
        return top
767

    
768
    def set_settings(self, settings):
769
        """Translate the settings in the UI into bit in the mem_struct
770
        I don't understand well the method used in many drivers
771
        so, I used mine, ugly but works ok"""
772

    
773
        mobj = self._memobj
774

    
775
        for element in settings:
776
            if not isinstance(element, RadioSetting):
777
                self.set_settings(element)
778
                continue
779

    
780
            # Let's roll the ball
781
            if "." in element.get_name():
782
                inter, setting = element.get_name().split(".")
783
                # you must ignore the settings with "not"
784
                # this are READ ONLY attributes
785
                if inter == "not":
786
                    continue
787

    
788
                obj = getattr(mobj, inter)
789
                value = element.value
790

    
791
                # case keys, with special config
792
                if setting[0] == "k":
793
                    value = KEYS.keys()[KEYS.values().index(str(value))]
794

    
795
                # integers case + special case
796
                if setting in ["tot", "min_vol"]:
797
                    # catching the "off" values as zero
798
                    try:
799
                        value = int(value)
800
                    except:
801
                        value = 0
802

    
803
                # Bool types + inverted
804
                if setting in ["poweron_tone", "dealer_tuning", "clone"]:
805
                    value = bool(value)
806

    
807
            # Apply al configs done
808
            # DEBUG
809
            # print("%s: %s" % (setting, value))
810
            setattr(obj, setting, value)
811

    
812

    
813
# This are the oldest family 60 models (Black keys), just mobiles support here
814

    
815
@directory.register
816
class TK760_Radio(Kenwood_M60_Radio):
817
    """Kenwood TK-760 Radios"""
818
    MODEL = "TK-760"
819
    TYPE = "M0760"
820
    VARIANTS = {
821
        "M0760\x01\x00\x00": (32, 136, 156, "K2"),
822
        "M0760\x00\x00\x00": (32, 148, 174, "K")
823
        }
824

    
825

    
826
@directory.register
827
class TK762_Radio(Kenwood_M60_Radio):
828
    """Kenwood TK-762 Radios"""
829
    MODEL = "TK-762"
830
    TYPE = "M0762"
831
    VARIANTS = {
832
        "M0762\x01\x00\x00": (2, 136, 156, "K2"),
833
        "M0762\x00\x00\x00": (2, 148, 174, "K")
834
        }
835

    
836

    
837
@directory.register
838
class TK768_Radio(Kenwood_M60_Radio):
839
    """Kenwood TK-768 Radios"""
840
    MODEL = "TK-768"
841
    TYPE = "M0768"
842
    VARIANTS = {
843
        "M0768\x21\x00\x00": (32, 136, 156, "K2"),
844
        "M0768\x20\x00\x00": (32, 148, 174, "K")
845
        }
846

    
847

    
848
@directory.register
849
class TK860_Radio(Kenwood_M60_Radio):
850
    """Kenwood TK-860 Radios"""
851
    MODEL = "TK-860"
852
    TYPE = "M0860"
853
    VARIANTS = {
854
        "M0860\x05\x00\x00": (32, 406, 430, "F4"),
855
        "M0860\x04\x00\x00": (32, 488, 512, "F3"),
856
        "M0860\x03\x00\x00": (32, 470, 496, "F2"),
857
        "M0860\x02\x00\x00": (32, 450, 476, "F1")
858
        }
859

    
860

    
861
@directory.register
862
class TK862_Radio(Kenwood_M60_Radio):
863
    """Kenwood TK-862 Radios"""
864
    MODEL = "TK-862"
865
    TYPE = "M0862"
866
    VARIANTS = {
867
        "M0862\x05\x00\x00": (2, 406, 430, "F4"),
868
        "M0862\x04\x00\x00": (2, 488, 512, "F3"),
869
        "M0862\x03\x00\x00": (2, 470, 496, "F2"),
870
        "M0862\x02\x00\x00": (2, 450, 476, "F1")
871
        }
872

    
873

    
874
@directory.register
875
class TK868_Radio(Kenwood_M60_Radio):
876
    """Kenwood TK-868 Radios"""
877
    MODEL = "TK-868"
878
    TYPE = "M0868"
879
    VARIANTS = {
880
        "M0868\x25\x00\x00": (32, 406, 430, "F4"),
881
        "M0868\x24\x00\x00": (32, 488, 512, "F3"),
882
        "M0868\x23\x00\x00": (32, 470, 496, "F2"),
883
        "M0868\x22\x00\x00": (32, 450, 476, "F1")
884
        }
(3-3/3)