# HG changeset patch
# User Jim Unroe <rock.unroe@gmail.com>
# Date 1644798278 18000
#      Sun Feb 13 19:24:38 2022 -0500
# Node ID 2c177d4e54b0807edbe728633af3fb856c20e2ac
# Parent  6fc2b2336b5037c8cd939810fe219658f3c75df6
[RB17P] Retevis RB17P

This patch adds support for the Retevis RB17P GMRS radio.

Addresses #9752

diff -r 6fc2b2336b50 -r 2c177d4e54b0 chirp/drivers/retevis_rb17p.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/chirp/drivers/retevis_rb17p.py	Sun Feb 13 19:24:38 2022 -0500
@@ -0,0 +1,608 @@
+# Copyright 2022 Jim Unroe <rock.unroe@gmail.com>
+#
+# 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/>.
+
+import time
+import os
+import struct
+import unittest
+import logging
+
+from chirp import chirp_common, directory, memmap
+from chirp import bitwise, errors, util
+from chirp.settings import RadioSetting, RadioSettingGroup, \
+    RadioSettingValueInteger, RadioSettingValueList, \
+    RadioSettingValueBoolean, RadioSettings
+
+LOG = logging.getLogger(__name__)
+
+MEM_FORMAT = """
+#seekto 0x1000;
+struct {
+    lbcd rxfreq[4];
+    lbcd txfreq[4];
+    lbcd rxtone[2];
+    lbcd txtone[2];
+    u8 unknown1:4,
+       compander:1,
+       unknown2:1,
+       highpower:1,
+       unknown3:1;
+    u8 unknown4:3,
+       wide:1,
+       scan:1,
+       unknown4:1,
+       bcl:2;
+    u8 unknown6[2];
+} memory[30];
+#seekto 0x0810;
+struct {
+    u8 unknown01:5,
+       dualwatch:1,
+       workmode:2;
+    u8 scanmode:2,
+       unknown02:3,
+       autokeylock:1,
+       unknown03:2;
+    u8 unknown04:1,
+       beep:1,
+       unknown05:1,
+       voice:1,
+       unknown06:1,
+       noaa:1,
+       batterysave:2;
+    u8 squelchlevel;
+    u8 unknown07:5,
+       timeouttimer:2,
+       unknown08:1;
+    u8 alarmtype:1,
+       unknown09:3
+       voxlevel:4;
+    u8 unknown10:2,
+       backlight:2,
+       unknown11:4;
+    u8 unknown12[3];
+    u8 unknown13:3,
+       rogerbeep:1,
+       unknown14:1,
+       sidekey:3;
+} settings;
+"""
+
+CMD_ACK = "\x06"
+
+ALARMTYPE_LIST = ["Local & Remote", "Local"]
+BACKLIGHT_LIST = ["5 seconds", "10 seconds", "30 seconds", "Always"]
+BATTERYSAVE_LIST = ["Off", "1:4", "1:8"]
+SCANMODE_LIST = ["Time Operated", "Carrier Operated", "Search"]
+SIDEKEY_LIST = ["Off", "Scan", "VOX", "2nd PTT", "NOAA", "Monitor"]
+TIMEOUTTIMER_LIST = ["60 seconds", "120 seconds", "180 seconds"]
+VOXLEVEL_LIST = ["Off", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
+WORKMODE_LIST = ["Frequencies", "Channel Numbers"]
+
+SETTING_LISTS = {
+    "alarmtype": ALARMTYPE_LIST,
+    "backlight": BACKLIGHT_LIST,
+    "batterysave": BATTERYSAVE_LIST,
+    "scanmode": SCANMODE_LIST,
+    "sidekey": SIDEKEY_LIST,
+    "timeouttimer": TIMEOUTTIMER_LIST,
+    "voxlevel": VOXLEVEL_LIST,
+    "workmode": WORKMODE_LIST
+}
+
+BCL = ["Off", "Carrier", "QT/DCS"]
+
+GMRS_FREQS1 = [462.5625, 462.5875, 462.6125, 462.6375, 462.6625,
+               462.6875, 462.7125]
+GMRS_FREQS2 = [467.5625, 467.5875, 467.6125, 467.6375, 467.6625,
+               467.6875, 467.7125]
+GMRS_FREQS3 = [462.5500, 462.5750, 462.6000, 462.6250, 462.6500,
+               462.6750, 462.7000, 462.7250]
+GMRS_FREQS = GMRS_FREQS1 + GMRS_FREQS2 + GMRS_FREQS3 * 2
+
+
+def _enter_programming_mode(radio):
+    serial = radio.pipe
+
+    try:
+        serial.write("\x02")
+        time.sleep(0.01)
+        serial.write(radio._magic)
+        ack = serial.read(1)
+    except:
+        raise errors.RadioError("Error communicating with radio")
+
+    if not ack:
+        raise errors.RadioError("No response from radio")
+    elif ack != CMD_ACK:
+        raise errors.RadioError("Radio refused to enter programming mode")
+
+    try:
+        serial.write("M" + "\x02")
+        ident = serial.read(8)
+    except:
+        raise errors.RadioError("Error communicating with radio")
+
+    if not ident.startswith(radio._fingerprint):
+        LOG.debug(util.hexprint(ident))
+        raise errors.RadioError("Radio returned unknown identification string")
+
+    try:
+        serial.write(CMD_ACK)
+        ack = serial.read(1)
+    except:
+        raise errors.RadioError("Error communicating with radio")
+
+    if ack != CMD_ACK:
+        raise errors.RadioError("Radio refused to enter programming mode")
+
+
+def _read_block(radio, block_addr, block_size):
+    serial = radio.pipe
+
+    cmd = struct.pack(">cHb", 'R', block_addr, block_size)
+    expectedresponse = "W" + cmd[1:]
+    LOG.debug("Reading block %04x..." % (block_addr))
+
+    try:
+        serial.write(cmd)
+        response = serial.read(4 + block_size)
+        if response[:4] != expectedresponse:
+            raise Exception("Error reading block %04x." % (block_addr))
+
+        block_data = response[4:]
+    except:
+        raise errors.RadioError("Failed to read block at %04x" % block_addr)
+
+    return block_data
+
+
+def _write_block(radio, block_addr, block_size):
+    serial = radio.pipe
+
+    cmd = struct.pack(">cHb", 'W', block_addr, block_size)
+    data = radio.get_mmap()[block_addr:block_addr + block_size]
+
+    LOG.debug("Writing Data:")
+    LOG.debug(util.hexprint(cmd + data))
+
+    try:
+        serial.write(cmd + data)
+        if serial.read(1) != CMD_ACK:
+            raise Exception("No ACK")
+    except:
+        raise errors.RadioError("Failed to send block "
+                                "to radio at %04x" % block_addr)
+
+
+def do_download(radio):
+    LOG.debug("download")
+    _enter_programming_mode(radio)
+
+    data = ""
+
+    status = chirp_common.Status()
+    status.msg = "Cloning from radio"
+
+    status.cur = 0
+    status.max = radio._memsize
+
+    for addr in range(0, radio._memsize, radio.BLOCK_SIZE):
+        status.cur = addr + radio.BLOCK_SIZE
+        radio.status_fn(status)
+
+        block = _read_block(radio, addr, radio.BLOCK_SIZE)
+        data += block
+
+        LOG.debug("Address: %04x" % addr)
+        LOG.debug(util.hexprint(block))
+
+    return memmap.MemoryMap(data)
+
+
+def do_upload(radio):
+    status = chirp_common.Status()
+    status.msg = "Uploading to radio"
+
+    _enter_programming_mode(radio)
+
+    status.cur = 0
+    status.max = radio._memsize
+
+    for start_addr, end_addr, block_size in radio._ranges:
+        for addr in range(start_addr, end_addr, block_size):
+            status.cur = addr + block_size
+            radio.status_fn(status)
+            _write_block(radio, addr, block_size)
+
+
+class RB17P_Base(chirp_common.CloneModeRadio):
+    """Base class for Retevis RB17P"""
+    VENDOR = "Retevis"
+    MODEL = "RB17P Base"
+    BAUD_RATE = 9600
+    BLOCK_SIZE = 0x40
+
+    VALID_BANDS = [(400000000, 470000000)]
+    POWER_LEVELS = [chirp_common.PowerLevel("Low", watts=0.50),
+                    chirp_common.PowerLevel("High", watts=5.00)]
+
+    _magic = "PGA588"
+    _fingerprint = "\xFF" * 8
+    _upper = 128
+    _mem_params = (_upper  # number of channels
+                   )
+    _gmrs = False
+    _ranges = [(0x0000, 0x1800, 0x40),
+               ]
+    _memsize = 0x1800
+
+    def get_features(self):
+        rf = chirp_common.RadioFeatures()
+        rf.has_settings = True
+        rf.valid_modes = ["NFM", "FM"]  # 12.5 KHz, 25 kHz.
+        rf.valid_skips = ["", "S"]
+        rf.valid_tmodes = ["", "Tone", "TSQL", "DTCS", "Cross"]
+        rf.valid_duplexes = ["", "-", "+", "split", "off"]
+        rf.valid_power_levels = self.POWER_LEVELS
+        rf.can_odd_split = True
+        rf.has_rx_dtcs = True
+        rf.has_ctone = True
+        rf.has_cross = True
+        rf.valid_cross_modes = [
+            "Tone->Tone",
+            "DTCS->",
+            "->DTCS",
+            "Tone->DTCS",
+            "DTCS->Tone",
+            "->Tone",
+            "DTCS->DTCS"]
+        rf.has_tuning_step = False
+        rf.has_bank = False
+        rf.has_name = False
+        rf.memory_bounds = (1, 30)
+        rf.valid_bands = self.VALID_BANDS
+        rf.valid_tuning_steps = chirp_common.TUNING_STEPS
+
+        return rf
+
+    def process_mmap(self):
+        self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
+
+    def validate_memory(self, mem):
+        msgs = ""
+        msgs = chirp_common.CloneModeRadio.validate_memory(self, mem)
+
+        _msg_freq = 'Memory location cannot change frequency'
+        _msg_simplex = 'Memory location only supports Duplex:(None)'
+        _msg_duplex = 'Memory location only supports Duplex: +'
+        _msg_nfm = 'Memory location only supports Mode: NFM'
+        _msg_txp = 'Memory location only supports Power: Low'
+
+        # GMRS only models
+        if self._gmrs:
+            # range of memories with values set by FCC rules
+            if mem.freq != int(GMRS_FREQS[mem.number - 1] * 1000000):
+                # warn user can't change frequency
+                msgs.append(chirp_common.ValidationError(_msg_freq))
+            if mem.number >= 8 and mem.number <= 14:
+                if str(mem.power) != "Low":
+                    # warn user can't change power
+                    msgs.append(chirp_common.ValidationError(_msg_txp))
+
+                if str(mem.mode) != "NFM":
+                    # warn user can't change mode
+                    msgs.append(chirp_common.ValidationError(_msg_nfm))
+
+            if mem.number >= 1 and mem.number <= 22:
+                # channels 1 - 22 are simplex only
+                if str(mem.duplex) != "":
+                    # warn user can't change duplex
+                    msgs.append(chirp_common.ValidationError(_msg_simplex))
+
+            if mem.number >= 23 and mem.number <= 30:
+                # channels 23 - 30 are duplex + only
+                if str(mem.duplex) != "+":
+                    # warn user can't change duplex
+                    msgs.append(chirp_common.ValidationError(_msg_duplex))
+
+        return msgs
+
+    def sync_in(self):
+        self._mmap = do_download(self)
+        self.process_mmap()
+
+    def sync_out(self):
+        do_upload(self)
+
+    def get_raw_memory(self, number):
+        return repr(self._memobj.memory[number - 1])
+
+    def _decode_tone(self, val):
+        val = int(val)
+        if val == 16665:
+            return '', None, None
+        elif val >= 12000:
+            return 'DTCS', val - 12000, 'R'
+        elif val >= 8000:
+            return 'DTCS', val - 8000, 'N'
+        else:
+            return 'Tone', val / 10.0, None
+
+    def _encode_tone(self, memval, mode, value, pol):
+        if mode == '':
+            memval[0].set_raw(0xFF)
+            memval[1].set_raw(0xFF)
+        elif mode == 'Tone':
+            memval.set_value(int(value * 10))
+        elif mode == 'DTCS':
+            flag = 0x80 if pol == 'N' else 0xC0
+            memval.set_value(value)
+            memval[1].set_bits(flag)
+        else:
+            raise Exception("Internal error: invalid mode `%s'" % mode)
+
+    def get_memory(self, number):
+        _mem = self._memobj.memory[number - 1]
+
+        mem = chirp_common.Memory()
+
+        mem.number = number
+        mem.freq = int(_mem.rxfreq) * 10
+
+        # We'll consider any blank (i.e. 0MHz frequency) to be empty
+        if mem.freq == 0:
+            mem.empty = True
+            return mem
+
+        if _mem.rxfreq.get_raw() == "\xFF\xFF\xFF\xFF":
+            mem.freq = 0
+            mem.empty = True
+            return mem
+
+        if _mem.txfreq.get_raw() == "\xFF\xFF\xFF\xFF":
+            mem.duplex = "off"
+            mem.offset = 0
+        elif int(_mem.rxfreq) == int(_mem.txfreq):
+            mem.duplex = ""
+            mem.offset = 0
+        else:
+            mem.duplex = int(_mem.rxfreq) > int(_mem.txfreq) and "-" or "+"
+            mem.offset = abs(int(_mem.rxfreq) - int(_mem.txfreq)) * 10
+
+        mem.mode = _mem.wide and "FM" or "NFM"
+
+        mem.skip = not _mem.scan and "S" or ""
+
+        txtone = self._decode_tone(_mem.txtone)
+        rxtone = self._decode_tone(_mem.rxtone)
+        chirp_common.split_tone_decode(mem, txtone, rxtone)
+
+        mem.power = self.POWER_LEVELS[_mem.highpower]
+
+        if self._gmrs:
+            GMRS_IMMUTABLE = ["freq", "duplex", "offset"]
+            if mem.number >= 8 and mem.number <= 14:
+                GMRS_IMMUTABLE = GMRS_IMMUTABLE + ["mode", "power"]
+
+            mem.immutable = GMRS_IMMUTABLE
+
+        mem.extra = RadioSettingGroup("Extra", "extra")
+        rs = RadioSetting("bcl", "Busy Channel Lockout",
+                          RadioSettingValueList(BCL, BCL[_mem.bcl]))
+        mem.extra.append(rs)
+        rs = RadioSetting("compander", "Compander",
+                          RadioSettingValueBoolean(_mem.compander))
+        mem.extra.append(rs)
+
+        return mem
+
+    def set_memory(self, mem):
+        # Get a low-level memory object mapped to the image
+        _mem = self._memobj.memory[mem.number - 1]
+
+        if mem.empty:
+            if self._frs:
+                _mem.set_raw("\xFF" * 12 + "\x6A\x08\xFF\xDC")
+                if self._upper == 22:
+                    FRS_FREQ = int(FRS_FREQS[mem.number - 1] * 100000)
+                else:
+                    FRS_FREQ = int(FRS16_FREQS[mem.number - 1] * 100000)
+                _mem.rxfreq = _mem.txfreq = FRS_FREQ
+                _mem.narrow = True
+                _mem.highpower = True
+                if self._upper == 22:
+                    if mem.number >= 8 and mem.number <= 14:
+                        _mem.highpower = False
+            else:
+                _mem.set_raw("\xFF" * (_mem.size() / 8))
+
+            return
+
+        _mem.rxfreq = mem.freq / 10
+
+        if mem.duplex == "off":
+            for i in range(0, 4):
+                _mem.txfreq[i].set_raw("\xFF")
+        elif mem.duplex == "split":
+            _mem.txfreq = mem.offset / 10
+        elif mem.duplex == "+":
+            _mem.txfreq = (mem.freq + mem.offset) / 10
+        elif mem.duplex == "-":
+            _mem.txfreq = (mem.freq - mem.offset) / 10
+        else:
+            _mem.txfreq = mem.freq / 10
+
+        txtone, rxtone = chirp_common.split_tone_encode(mem)
+        self._encode_tone(_mem.txtone, *txtone)
+        self._encode_tone(_mem.rxtone, *rxtone)
+
+        if self.MODEL != "T18" and self.MODEL != "RB18":
+            _mem.highpower = mem.power == self.POWER_LEVELS[1]
+
+        _mem.wide = mem.mode == "FM"
+        _mem.scan = mem.skip == ""
+
+        for setting in mem.extra:
+            setattr(_mem, setting.get_name(), int(setting.value))
+
+    def get_settings(self):
+        _settings = self._memobj.settings
+        basic = RadioSettingGroup("basic", "Basic Settings")
+        top = RadioSettings(basic)
+
+        # Menu 01 - Work Mode
+        rs = RadioSetting("workmode", "Work Mode",
+                          RadioSettingValueList(
+                              WORKMODE_LIST,
+                              WORKMODE_LIST[(_settings.workmode) - 1]))
+        basic.append(rs)
+
+        # Menu 04 - Squelch
+        rs = RadioSetting("squelchlevel", "Squelch level",
+                          RadioSettingValueInteger(
+                              0, 9, _settings.squelchlevel))
+        basic.append(rs)
+
+        # Menu 05 - Battery Save
+        rs = RadioSetting("batterysave", "Batery Save",
+                          RadioSettingValueList(
+                              BATTERYSAVE_LIST,
+                              BATTERYSAVE_LIST[_settings.batterysave]))
+        basic.append(rs)
+
+        # Menu 06 - Dual Watch
+        rs = RadioSetting("dualwatch", "Dual Watch",
+                          RadioSettingValueBoolean(_settings.dualwatch))
+        basic.append(rs)
+
+        # Menu 07 - Backlight Duration[s]
+        rs = RadioSetting("backlight", "Backlight Duration",
+                          RadioSettingValueList(
+                              BACKLIGHT_LIST,
+                              BACKLIGHT_LIST[_settings.backlight]))
+        basic.append(rs)
+
+        # Menu 09 - Beep Tone
+        rs = RadioSetting("beep", "Key Beep",
+                          RadioSettingValueBoolean(_settings.beep))
+        basic.append(rs)
+
+        # Menu 10 - Voice Annunciation
+        rs = RadioSetting("voice", "Voice prompts",
+                          RadioSettingValueBoolean(_settings.voice))
+        basic.append(rs)
+
+        # Menu 11 - Time out Timer[s]
+        rs = RadioSetting("timeouttimer", "Timeout timer",
+                          RadioSettingValueList(
+                              TIMEOUTTIMER_LIST,
+                              TIMEOUTTIMER_LIST[
+                                  (_settings.timeouttimer) - 1]))
+        basic.append(rs)
+
+        # Menu 12 - Roger Tone
+        rs = RadioSetting("rogerbeep", "Roger beep",
+                          RadioSettingValueBoolean(_settings.rogerbeep))
+        basic.append(rs)
+
+        # Menu 13 - SideKey
+        rs = RadioSetting("sidekey", "Side Key",
+                          RadioSettingValueList(
+                              SIDEKEY_LIST,
+                              SIDEKEY_LIST[_settings.sidekey]))
+        basic.append(rs)
+
+        # Menu 14 - Auto Key Lock
+        rs = RadioSetting("autokeylock", "Auto Key Lock",
+                          RadioSettingValueBoolean(_settings.autokeylock))
+        basic.append(rs)
+
+        # Menu 17 - NOAA
+        rs = RadioSetting("noaa", "NOAA",
+                          RadioSettingValueBoolean(_settings.noaa))
+        basic.append(rs)
+
+        # Scan Mode
+        rs = RadioSetting("scanmode", "Scan mode",
+                          RadioSettingValueList(
+                              SCANMODE_LIST,
+                              SCANMODE_LIST[_settings.scanmode]))
+        basic.append(rs)
+
+        # Alarm Type
+        rs = RadioSetting("alarmtype", "Alarm Type",
+                          RadioSettingValueList(
+                              ALARMTYPE_LIST,
+                              ALARMTYPE_LIST[_settings.alarmtype]))
+        basic.append(rs)
+
+        # VOX Level
+        rs = RadioSetting("voxlevel", "Vox level",
+                          RadioSettingValueList(
+                              VOXLEVEL_LIST,
+                              VOXLEVEL_LIST[_settings.voxlevel]))
+        basic.append(rs)
+
+        return top
+
+    def set_settings(self, settings):
+        for element in settings:
+            if not isinstance(element, RadioSetting):
+                self.set_settings(element)
+                continue
+            else:
+                try:
+                    if "." in element.get_name():
+                        bits = element.get_name().split(".")
+                        obj = self._memobj
+                        for bit in bits[:-1]:
+                            obj = getattr(obj, bit)
+                        setting = bits[-1]
+                    else:
+                        obj = self._memobj.settings
+                        setting = element.get_name()
+
+                    if element.has_apply_callback():
+                        LOG.debug("Using apply callback")
+                        element.run_apply_callback()
+                    elif setting == "timeouttimer":
+                        setattr(obj, setting, int(element.value) + 1)
+                    elif setting == "workmode":
+                        setattr(obj, setting, int(element.value) + 1)
+                    else:
+                        LOG.debug("Setting %s = %s" % (setting, element.value))
+                        setattr(obj, setting, element.value)
+                except Exception, e:
+                    LOG.debug(element.get_name())
+                    raise
+
+    @classmethod
+    def match_model(cls, filedata, filename):
+        # Radios that have always been post-metadata, so never do
+        # old-school detection
+        return False
+
+
+@directory.register
+class RB17PRadio(RB17P_Base):
+    """Retevis RB17P"""
+    VENDOR = "Retevis"
+    MODEL = "RB17P"
+    _gmrs = True
+    _ranges = [(0x0010, 0x0510, 0x40),
+               (0x0810, 0x0830, 0x30),
+               (0x1000, 0x1800, 0x40)
+               ]
diff -r 6fc2b2336b50 -r 2c177d4e54b0 tools/cpep8.manifest
--- a/tools/cpep8.manifest	Sun Feb 13 13:45:01 2022 -0800
+++ b/tools/cpep8.manifest	Sun Feb 13 19:24:38 2022 -0500
@@ -78,6 +78,7 @@
 ./chirp/drivers/puxing.py
 ./chirp/drivers/radioddity_r2.py
 ./chirp/drivers/radtel_t18.py
+./chirp/drivers/retevis_rb17p.py
 ./chirp/drivers/retevis_rt1.py
 ./chirp/drivers/retevis_rt21.py
 ./chirp/drivers/retevis_rt22.py
