def probe_layout(self): start_addrs = [] tmp_format = '#seekto 0x0A00; ul16 zone_starts[128];' mem = bitwise.parse(tmp_format, self._mmap) zone_format = """struct zoneinfo { u8 number; u8 zonetype; u8 unknown1[2]; u8 count; char name[12]; u8 unknown2[15]; };""" zone_addresses = [] for i in range(0, 128): if mem.zone_starts[i] == 0xFFFF: break zone_addresses.append(mem.zone_starts[i]) zone_format += '#seekto 0x%x; struct zoneinfo zone%i;' % ( mem.zone_starts[i], i) zoneinfo = bitwise.parse(zone_format, self._mmap) zones = [] for i, addr in enumerate(zone_addresses): zone = getattr(zoneinfo, 'zone%i' % i) if zone.zonetype != 0x31: LOG.error('Zone %i is type 0x%02x; ' 'I only support 0x31 (conventional)') raise errors.RadioError( 'Unsupported non-conventional zone found in radio; ' 'Refusing to load to safeguard your data!') zones.append((addr, zone.count)) LOG.debug('Zones: %s' % zones) return zones
def test_char(self): data = memmap.MemoryMap("c") obj = bitwise.parse("char foo;", data) self.assertEqual(str(obj.foo), "c") self.assertEqual(obj.foo.size(), 8) obj.foo = "d" self.assertEqual(data.get_packed(), "d")
def test_struct_writes(self): data = memmap.MemoryMap("..") defn = "struct { u8 bar; u8 baz; } foo;" obj = bitwise.parse(defn, data) obj.foo.bar = 0x12 obj.foo.baz = 0x34 self.assertEqual(data.get_packed(), "\x12\x34")
def test_int_array(self): data = memmap.MemoryMap('\x00\x01\x02\x03') obj = bitwise.parse('u8 foo[4];', data) for i in range(4): self.assertEqual(i, obj.foo[i]) obj.foo[i] = i * 2 self.assertEqual('\x00\x02\x04\x06', data.get_packed())
def check_ver(ver_response, allowed_types): ''' Check the returned radio version is one we approve of ''' LOG.debug('ver_response = ') LOG.debug(util.hexprint(ver_response)) resp = bitwise.parse(VER_FORMAT, ver_response) verok = False if resp.hdr == 0x49 and resp.ack == 0x06: model, version = [ cstring_to_py_string(bitwise.get_string(s)).strip() for s in (resp.model, resp.version) ] LOG.debug('radio model: \'%s\' version: \'%s\'' % (model, version)) LOG.debug('allowed_types = %s' % allowed_types) if model in allowed_types: LOG.debug('model in allowed_types') if version in allowed_types[model]: LOG.debug('version in allowed_types[model]') verok = True else: raise errors.RadioError('Failed to parse version response') return verok, int(resp.bandlimit)
def process_mmap(self): if not self._memstart: return self._memobj = bitwise.parse(MEM_FORMAT_8800 % (self._memstart, self._bankstart), self._mmap)
def _download(radio): _ident(radio) memobj = None data = "" for start, end in radio._ranges: for addr in range(start, end, 0x10): if memobj is not None and not _should_send_addr(memobj, addr): block = "\xFF" * 0x10 else: block = _send(radio, 'R', addr, 0x10) data += block status = chirp_common.Status() status.cur = len(data) status.max = end status.msg = "Cloning from radio" radio.status_fn(status) if addr == 0x19F0: memobj = bitwise.parse(_mem_format, data) _finish(radio) return memmap.MemoryMap(data)
def expand_mmap(self, zone_sizes): """Remap memory into zones of the specified sizes, copying things around to keep the contents, as appropriate.""" old_zones = self._zones old_memobj = self._memobj self._mmap = memmap.MemoryMapBytes(bytes(self._mmap.get_packed())) new_format = HEADER_FORMAT addr = self._system_start self._zones = [] for index, count in enumerate(zone_sizes): new_format += SYSTEM_MEM_FORMAT % { 'addr': addr, 'count': max(count, 2), # bitwise bug 'index': index} self._zones.append((addr, count)) addr += 0x20 + (count * 0x30) self._memobj = bitwise.parse(new_format, self._mmap) # Set all known zone addresses and clear the rest for index in range(0, 128): try: self._memobj.zone_starts[index] = self._zones[index][0] except IndexError: self._memobj.zone_starts[index] = 0xFFFF for zone_number, count in enumerate(zone_sizes): dest_zone = getattr(self._memobj, 'zone%i' % zone_number) dest = dest_zone.memories dest_zoneinfo = dest_zone.zoneinfo if zone_number < len(old_zones): LOG.debug('Copying existing zone %i' % zone_number) _, old_count = old_zones[zone_number] source_zone = getattr(old_memobj, 'zone%i' % zone_number) source = source_zone.memories source_zoneinfo = source_zone.zoneinfo if old_count != count: LOG.debug('Zone %i going from %i to %i' % (zone_number, old_count, count)) # Copy the zone record from the source, but then update # the count dest_zoneinfo.set_raw(source_zoneinfo.get_raw()) dest_zoneinfo.count = count source_i = 0 for dest_i in range(0, min(count, old_count)): dest[dest_i].set_raw(source[dest_i].get_raw()) else: LOG.debug('New zone %i' % zone_number) dest_zone.zoneinfo.number = zone_number + 1 dest_zone.zoneinfo.zonetype = 0x31 dest_zone.zoneinfo.count = count dest_zone.zoneinfo.name = ( 'Zone %i' % (zone_number + 1)).ljust(12)
def test_string(self): data = memmap.MemoryMap("foobar") obj = bitwise.parse("char foo[6];", data) self.assertEqual(str(obj.foo), "foobar") self.assertEqual(obj.foo.size(), 8 * 6) obj.foo = "bazfoo" self.assertEqual(data.get_packed(), "bazfoo")
def process_mmap(self): self._memobj = bitwise.parse(DJG7EG_MEM_FORMAT, self._mmap) # We check all channels for corruption (see bug #5275) but we don't fix # it automatically because it would be unpolite to modify something on # a read operation. A log message is emitted though for the user to # take actions. for number in range(len(self._memobj.memory)): self._check_channel_consistency(number)
def test_bit_array(self): defn = "bit foo[24];" data = memmap.MemoryMap("\x00\x80\x01") obj = bitwise.parse(defn, data) for i, v in [(0, False), (8, True), (23, True)]: self.assertEqual(bool(obj.foo[i]), v) for i in range(0, 24): obj.foo[i] = i % 2 self.assertEqual(data.get_packed(), "\x55\x55\x55")
def test_string_with_various_input_types(self): data = memmap.MemoryMapBytes(bytes(b"foobar")) obj = bitwise.parse("char foo[6];", data) self.assertEqual('foobar', str(obj.foo)) self.assertEqual(6, len(b'barfoo')) obj.foo = b'barfoo' self.assertEqual('barfoo', str(obj.foo)) obj.foo = [ord(c) for c in 'fffbbb'] self.assertEqual('fffbbb', str(obj.foo))
def process_mmap(self): if not self._memstart: return mem_format = MEM_FORMAT.format(memories=self._memstart, skips=self._skipstart, enables=self._enablestart) self._memobj = bitwise.parse(mem_format, self._mmap)
def test_string_invalid_chars(self): data = memmap.MemoryMapBytes(bytes(b"\xFFoobar1")) obj = bitwise.parse("struct {char foo[7];} bar;", data) if six.PY3: expected = '\xffoobar1' else: expected = '\\xffoobar1' self.assertIn(expected, repr(obj.bar))
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 process_mmap(self): if not self._memstart: return mem_format = MEM_FORMAT.format(memories=self._memstart, skips=self._skipstart, enables=self._enablestart ) self._memobj = bitwise.parse(mem_format, self._mmap)
def test_bitfield_u8(self): defn = "u8 foo:4, bar:4;" data = memmap.MemoryMap("\x12") obj = bitwise.parse(defn, data) self.assertEqual(obj.foo, 1) self.assertEqual(obj.bar, 2) self.assertEqual(obj.foo.size(), 4) self.assertEqual(obj.bar.size(), 4) obj.foo = 0x8 obj.bar = 0x1 self.assertEqual(data.get_packed(), "\x81")
def test_byte_char_coherence(self): charmmap = memmap.MemoryMap('00') # This will to a get_byte_compatible() from chars obj = bitwise.parse('char foo[2];', charmmap) self.assertEqual('00', str(obj.foo)) obj.foo = '11' # The above assignment happens on the byte-compatible mmap, # make sure it is still visible in the charmmap we know about. # This confirms that get_byte_compatible() links the backing # store of the original mmap to the new one. self.assertEqual('11', charmmap.get_packed())
def get_raw_memory(self, number): self._lock.acquire() try: ic9x_ll.send_magic(self.pipe) mframe = ic9x_ll.get_memory_frame(self.pipe, self.vfo, number) except: self._lock.release() raise self._lock.release() return repr(bitwise.parse(ic9x_ll.MEMORY_FRAME_FORMAT, mframe))
def process_mmap(self): self._zones = self.probe_layout() mem_format = HEADER_FORMAT for index, (addr, count) in enumerate(self._zones): mem_format += '\n\n' + ( SYSTEM_MEM_FORMAT % { 'addr': addr, 'count': max(count, 2), # bitwise bug, one-element array 'index': index}) self._memobj = bitwise.parse(mem_format, self._mmap)
def _test_type(self, datatype, _data, value): data = memmap.MemoryMap(_data) obj = bitwise.parse("%s foo;" % datatype, data) self.assertEqual(int(obj.foo), value) self.assertEqual(obj.foo.size(), len(data) * 8) obj.foo = 0 self.assertEqual(int(obj.foo), 0) self.assertEqual(data.get_packed(), ("\x00" * (obj.size() / 8))) obj.foo = value self.assertEqual(int(obj.foo), value) self.assertEqual(data.get_packed(), _data)
def process_mmap(self): if len(self._mmap) == self._datsize: self._mmap = memmap.MemoryMap([ chr(int(self._mmap.get(i, 2), 16)) for i in range(0, self._datsize, 2) if self._mmap.get(i, 2) != "\r\n" ]) try: self._memobj = bitwise.parse(self.MEM_FORMAT % self._memstart, self._mmap) except AttributeError: # main variant have no _memstart attribute return
def process_mmap(self): if len(self._mmap) == self._datsize: self._mmap = memmap.MemoryMap([ chr(int(self._mmap.get(i, 2), 16)) for i in range(0, self._datsize, 2) if self._mmap.get(i, 2) != "\r\n" ]) try: self._memobj = bitwise.parse( self.MEM_FORMAT % self._memstart, self._mmap) except AttributeError: # main variant have no _memstart attribute return
def _test_def(self, definition, name, _data, value): data = memmap.MemoryMap(_data) obj = bitwise.parse(definition, data) self.assertEqual(int(getattr(obj, name)), value) self.assertEqual(getattr(obj, name).size(), len(_data) * 8) setattr(obj, name, 0) self.assertEqual(data.get_packed(), ("\x00" * len(_data))) setattr(obj, name, 42) if definition.startswith("b"): expected = (len(_data) == 2 and "\x00" or "") + "\x42" else: expected = "\x42" + (len(_data) == 2 and "\x00" or "") raw = data.get_packed() self.assertEqual(raw, expected)
def _test_bitfield_24(self, variant, data): defn = "u%s24 foo:12, bar:6, baz:6;" % variant data = memmap.MemoryMap(data) obj = bitwise.parse(defn, data) self.assertEqual(int(obj.foo), 4) self.assertEqual(int(obj.bar), 3) self.assertEqual(int(obj.baz), 2) self.assertEqual(obj.foo.size(), 12) self.assertEqual(obj.bar.size(), 6) self.assertEqual(obj.baz.size(), 6) obj.foo = 1 obj.bar = 2 obj.baz = 3 if variant == 'l': self.assertEqual(data.get_packed(), "\x83\x10\x00") else: self.assertEqual(data.get_packed(), "\x00\x10\x83")
def _test_bitfield_16(self, variant, data): defn = "u%s16 foo:4, bar:8, baz:4;" % variant data = memmap.MemoryMap(data) obj = bitwise.parse(defn, data) self.assertEqual(int(obj.foo), 1) self.assertEqual(int(obj.bar), 0x23) self.assertEqual(int(obj.baz), 4) self.assertEqual(obj.foo.size(), 4) self.assertEqual(obj.bar.size(), 8) self.assertEqual(obj.baz.size(), 4) obj.foo = 0x2 obj.bar = 0x11 obj.baz = 0x3 if variant == "l": self.assertEqual(data.get_packed(), "\x13\x21") else: self.assertEqual(data.get_packed(), "\x21\x13")
def get_memory(self): """Return a Memory object based on the contents of the frame""" _mem = bitwise.parse(MEMORY_FRAME_FORMAT, self).mem if MODES[_mem.mode] == "DV": mem = IC9xDVMemory() else: mem = IC9xMemory() mem.number = int(_mem.number) if self.get_iscall(): mem.number = -1 - mem.number mem.freq = int(_mem.freq) mem.offset = int(_mem.offset) mem.rtone = int(_mem.rtone) / 10.0 mem.ctone = int(_mem.ctone) / 10.0 mem.dtcs = int(_mem.dtcs) mem.mode = MODES[int(_mem.mode)] mem.tuning_step = TUNING_STEPS[int(_mem.tuning_step)] mem.duplex = DUPLEX[int(_mem.duplex)] mem.tmode = TMODES[int(_mem.tmode)] mem.dtcs_polarity = DTCS_POL[int(_mem.dtcs_polarity)] if int(_mem.bank) != 0: mem._bank = ord(str(_mem.bank)) - ord("A") mem._bank_index = int(_mem.bank_index) if _mem.skip: mem.skip = "S" elif _mem.pskip: mem.skip = "P" else: mem.skip = "" mem.name = str(_mem.name).rstrip() if mem.mode == "DV": mem.dv_urcall = str(_mem.urcall).rstrip() mem.dv_rpt1call = str(_mem.rpt1call).rstrip() mem.dv_rpt2call = str(_mem.rpt2call).rstrip() mem.dv_code = int(_mem.digital_code) return mem
def _test_type(self, datatype, _data, value): data = memmap.MemoryMapBytes(bytes(_data)) obj = bitwise.parse("%s foo;" % datatype, data) self.assertEqual(int(obj.foo), value) self.assertEqual(obj.foo.size(), len(data) * 8) obj.foo = 0 self.assertEqual(int(obj.foo), 0) self.assertEqual(data.get_packed(), (b"\x00" * (obj.size() // 8))) obj.foo = value self.assertEqual(int(obj.foo), value) self.assertEqual(data.get_packed(), _data) obj.foo = 7 # Compare against the equivalent real division so we get consistent # results on py2 and py3 self.assertEqual(7 // 2, obj.foo // 2) self.assertEqual(7 / 2, obj.foo / 2) self.assertEqual(7 / 2.0, obj.foo / 2.0)
def set_memory(self, mem): """Take Memory object @mem and configure the frame accordingly""" if mem.number < 0: self.set_iscall(True) mem.number = abs(mem.number) - 1 LOG.debug("Memory is %i (call %s)" % (mem.number, self.get_iscall())) _mem = bitwise.parse(MEMORY_FRAME_FORMAT, self).mem _mem.number = mem.number _mem.freq = mem.freq _mem.offset = mem.offset _mem.rtone = int(mem.rtone * 10) _mem.ctone = int(mem.ctone * 10) _mem.dtcs = int(mem.dtcs) _mem.mode = MODES.index(mem.mode) _mem.tuning_step = TUNING_STEPS.index(mem.tuning_step) _mem.duplex = DUPLEX.index(mem.duplex) _mem.tmode = TMODES.index(mem.tmode) _mem.dtcs_polarity = DTCS_POL.index(mem.dtcs_polarity) if mem._bank is not None: _mem.bank = chr(ord("A") + mem._bank) _mem.bank_index = mem._bank_index _mem.skip = mem.skip == "S" _mem.pskip = mem.skip == "P" _mem.name = mem.name.ljust(8)[:8] if mem.mode == "DV": _mem.urcall = mem.dv_urcall.upper().ljust(8)[:8] _mem.rpt1call = mem.dv_rpt1call.upper().ljust(8)[:8] _mem.rpt2call = mem.dv_rpt2call.upper().ljust(8)[:8] _mem.digital_code = mem.dv_code
def setUp(self): self.driver = H777Radio(None) self.testdata = bitwise.parse("lbcd foo[2];", memmap.MemoryMap("\x00\x00"))
def sync_in(self): self._mmap = do_download(self) self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
def process_mmap(self): self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
def process_mmap(self): """Process the mem map into the mem object""" self._memobj = bitwise.parse(self.MEM_FORMAT, self._mmap)
def process_mmap(self): if(len(self._mmap)==self._memsize): self._memobj = bitwise.parse(MEM_FORMAT, self._mmap) elif(len(self._mmap)==self._memsize+565): self._memobj = bitwise.parse(MEM_FORMAT, self._mmap[549:]) self.fix()
def process_mmap(self): """Process the mem map into the mem object""" self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)
def process_mmap(self): self._memobj = bitwise.parse(mem_format, self._mmap)
def process_mmap(self): """Process the mem map into the mem object""" self._memobj = bitwise.parse(MEM_FORMAT, self._mmap) # to set the vars on the class to the correct ones self.set_variant()
def process_mmap(self): self._memobj = bitwise.parse(MEM_FORMAT_8900, self._mmap)
def process_mmap(self): mem_format = ft1d.MEM_FORMAT + MEM_FORMAT self._memobj = bitwise.parse(mem_format % self._mem_params, self._mmap)
def process_mmap(self): self._memobj = bitwise.parse( TH9800_MEM_FORMAT % (self._mmap_offset, self._scanlimits_offset, self._settings_offset, self._chan_active_offset, self._info_offset), self._mmap)
def process_mmap(self): self._memobj = bitwise.parse(MEM_FORMAT % self._mem_params, self._mmap)
def process_mmap(self): self._memobj = bitwise.parse(self.MEM_FORMAT, self._mmap)
def get_obj(self): self._data = MemoryMap(str(self._data)) return bitwise.parse(mem_duptone_format, self._data)
def process_mmap(self): self._memobj = bitwise.parse( MEM_FORMAT % self._mem_formatter, self._mmap)
def process_mmap(self): fmt = MEM_FORMAT % self._mem_positions self._memobj = bitwise.parse(fmt, self._mmap)