def nfc_cmd(cmd): return Struct( 'nfc_msg', OneOf(Byte('start'), (NFC_STX, )), OneOf(Byte('device'), NFC_RESPONSE_SET), OneOf(Byte('command'), NFC_COMMAND_SET), UBInt16('len'), String('data', lambda ctx: ctx['len']) if cmd is None else cmd, OneOf(Byte('stop'), (NFC_ETX, )), Embed(crc))
class TestOneOf(unittest.TestCase): def setUp(self): self.o = OneOf(UBInt8("foo"), [4, 5, 6, 7]) def test_trivial(self): pass def test_parse(self): self.assertEqual(self.o.parse(six.b("\x05")), 5) def test_parse_invalid(self): self.assertRaises(ValidationError, self.o.parse, six.b("\x08")) def test_build(self): self.assertEqual(self.o.build(5), six.b("\x05")) def test_build_invalid(self): self.assertRaises(ValidationError, self.o.build, 9)
class TestOneOf(unittest.TestCase): def setUp(self): self.o = OneOf(UBInt8("foo"), [4, 5, 6, 7]) def test_trivial(self): pass def test_parse(self): self.assertEqual(self.o.parse("\x05"), 5) def test_parse_invalid(self): self.assertRaises(ValidationError, self.o.parse, "\x08") def test_build(self): self.assertEqual(self.o.build(5), "\x05") def test_build_invalid(self): self.assertRaises(ValidationError, self.o.build, 9)
def setUp(self): self.o = OneOf(UBInt8("foo"), [4, 5, 6, 7])
from construct import Struct, Int8ul, Int16ul, Int32ul, OneOf, IfThenElse, Tell, this from .piostring import OffsetPioString ARTIST_ENTRY_MAGIC = 0x60 LONG_ARTIST_ENTRY_MAGIC = 0x64 Artist = Struct( "entry_start" / Tell, "magic" / OneOf(Int16ul, [ARTIST_ENTRY_MAGIC, LONG_ARTIST_ENTRY_MAGIC]), "index_shift" / Int16ul, "id" / Int32ul, "unknown" / IfThenElse(this.magic == LONG_ARTIST_ENTRY_MAGIC, Int16ul, Int8ul), # always 0x03, maybe an unindexed empty string "name_idx" / IfThenElse(this.magic == LONG_ARTIST_ENTRY_MAGIC, Int16ul, Int8ul), "name" / OffsetPioString(this.name_idx))
Int16ul, Int16ub, Int32ub, GreedyString, GreedyRange from ..const import EDDYSTONE_UUID, EDDYSTONE_URL_SCHEMES, EDDYSTONE_TLM_UNENCRYPTED, \ EDDYSTONE_TLM_ENCRYPTED, EDDYSTONE_UID_FRAME, EDDYSTONE_URL_FRAME, \ EDDYSTONE_TLM_FRAME, EDDYSTONE_EID_FRAME, FLAGS_DATA_TYPE, \ SERVICE_DATA_TYPE, SERVICE_UUIDS_DATA_TYPE # pylint: disable=invalid-name EddystoneUIDFrame = Struct("tx_power" / Int8sl, "namespace" / Array(10, Byte), "instance" / Array(6, Byte), "rfu" / Const(b"\x00\x00")) EddystoneURLFrame = Struct( "tx_power" / Int8sl, "url_scheme" / OneOf(Byte, list(EDDYSTONE_URL_SCHEMES)), "url" / GreedyString(encoding="ascii")) UnencryptedTLMFrame = Struct( "voltage" / Int16ub, "temperature" / Int16ul, "advertising_count" / Int32ub, "seconds_since_boot" / Int32ub, ) EncryptedTLMFrame = Struct("encrypted_data" / Array(12, Byte), "salt" / Int16ul, "mic" / Int16ul) EddystoneTLMFrame = Struct( "tlm_version" / Byte, "data" / Switch( lambda ctx: ctx.tlm_version, {
from ..const import EDDYSTONE_UUID, EDDYSTONE_URL_SCHEMES, EDDYSTONE_TLM_UNENCRYPTED, \ EDDYSTONE_TLM_ENCRYPTED, EDDYSTONE_UID_FRAME, EDDYSTONE_URL_FRAME, \ EDDYSTONE_TLM_FRAME, EDDYSTONE_EID_FRAME, FLAGS_DATA_TYPE, \ SERVICE_DATA_TYPE, SERVICE_UUIDS_DATA_TYPE, ESTIMOTE_UUID, \ ESTIMOTE_TELEMETRY_FRAME from .estimote import EstimoteTelemetryFrame # pylint: disable=invalid-name EddystoneUIDFrame = Struct("tx_power" / Int8sl, "namespace" / Array(10, Byte), "instance" / Array(6, Byte), "rfu" / Array(2, Byte)) EddystoneURLFrame = Struct( "tx_power" / Int8sl, "url_scheme" / OneOf(Byte, list(EDDYSTONE_URL_SCHEMES)), "url" / GreedyString(encoding="ascii")) UnencryptedTLMFrame = Struct( "voltage" / Int16ub, "temperature" / Int16ul, "advertising_count" / Int32ub, "seconds_since_boot" / Int32ub, ) EncryptedTLMFrame = Struct("encrypted_data" / Array(12, Byte), "salt" / Int16ul, "mic" / Int16ul) EddystoneTLMFrame = Struct( "tlm_version" / Byte, "data" / Switch( lambda ctx: ctx.tlm_version, {
from construct import Array, BitsInteger, BitStruct, Enum, GreedyRange, Struct, Int8ub, Int32ub, Int32ul, Int32sb, Int32sl, OneOf, Nibble, Octet, If, IfThenElse, ByteSwapped, this, Computed, Switch, Pointer, Check, Terminated from ironman.globals import IPBUS_VERSION PacketHeaderStruct = BitStruct( "protocol_version" / OneOf(Nibble, [IPBUS_VERSION]), "reserved" / OneOf(Nibble, [0x0]), "id" / BitsInteger(16), "byteorder" / OneOf(Nibble, [0xf]), "type_id" / Enum(Nibble, CONTROL=0x0, STATUS=0x1, RESEND=0x2)) """ Struct detailing the Packet Header logic byteorder is `0xf` if big-endian and `0x0` if little-endian """ ControlHeaderStruct = BitStruct( "protocol_version" / OneOf(Nibble, [IPBUS_VERSION]), "id" / BitsInteger(12), "words" / Octet, "type_id" / Enum(Nibble, READ=0x0, NOINCREAD=0x2, WRITE=0x1, NOINCWRITE=0x3, RMWBITS=0x4, RMWSUM=0x5, RCONFIG=0x6, WCONFIG=0x7), "info_code" / Enum(Nibble, SUCCESS=0x0, BADHEADER=0x1, RBUSERROR=0x4, WBUSERROR=0x5, RBUSTIMEOUT=0x6,
from __future__ import absolute_import, division, print_function, unicode_literals from construct import this, If, Switch, OneOf from construct import Struct from construct import Byte, Int8ub from construct import PascalString AuthRequest = Struct("version" / OneOf(Int8ub, [1]), "username" / PascalString(Byte), "password" / PascalString(Byte)) AuthResponse = Struct("version" / OneOf(Int8ub, [1]), "status" / Byte)
# construtc imports from construct import Struct, OneOf, Byte, String, Embed # card reader imports from crc import crc # magnetic stripe command markers MS_STX = 0x02 # start of command MS_ETX = 0x03 # end of command # operation modes MS_MODES = (0x31, ) # response codes MS_RESPONSE_STATUS = (0x06, 0x15) # message structure ms_cmd = Struct('ms_msg', OneOf(Byte('start'), (MS_STX, )), OneOf(Byte('mode'), MS_MODES), Byte('track1_id'), Byte('track1_len'), OneOf(Byte('track1_status'), MS_RESPONSE_STATUS), String('track1', lambda ctx: ctx['track1_len']), Byte('track2_id'), Byte('track2_len'), OneOf(Byte('track2_status'), MS_RESPONSE_STATUS), String('track2', lambda ctx: ctx['track2_len']), Byte('track3_id'), Byte('track3_len'), OneOf(Byte('track3_status'), MS_RESPONSE_STATUS), String('track3', lambda ctx: ctx['track3_len']), OneOf(Byte('stop'), (MS_ETX, )), Embed(crc))
WIP_TAG_INT32=Array(this.data_size // 4, Int32sl), WIP_TAG_UINT32=Array(this.data_size // 4, Int32ul), WIP_TAG_CHAR=Array(this.data_size, Int8ul), WIP_TAG_BOOL=Int8ul, WIP_TAG_STRING=WIPString, ))), 'end' / Tell, # pad to get to data_end Padding(this.data_end - this.end), ) WIPFile = Struct( Const(b'WIT_PR06'), # alternately, "WIT_PRCT" 'root' / WIPTag, # Validate the root name OneOf(Computed(this.root.name), ['WITec Project'])) def parse_tag(tag, shallow=False): data = tag.data if data is None: return tag.name, data if tag.type in ('WIP_TAG_INT32', 'WIP_TAG_UINT32', 'WIP_TAG_CHAR'): if len(data) == 1: data = data[0] else: data = np.array(data) elif shallow and tag.type == 'WIP_TAG_LIST': data = None return tag.name, data
CJ_MANUFACTURER_ID, FLAGS_DATA_TYPE, IBEACON_MANUFACTURER_ID, \ EXPOSURE_NOTIFICATION_UUID # pylint: disable=invalid-name AdvertisingFlags = BitStruct( "reserved" / BitsInteger(3), "le_br_edr_support_host" / Flag, "le_br_edr_support_controller" / Flag, "br_edr_not_supported" / Flag, "le_general_discoverable_mode" / Flag, "le_limited_discoverable_mode" / Flag, ) ServiceData = Struct( "service_identifier" / OneOf( Bytes(2), [EDDYSTONE_UUID, ESTIMOTE_UUID, EXPOSURE_NOTIFICATION_UUID]), "service_data" / Switch( lambda ctx: ctx.service_identifier, { EXPOSURE_NOTIFICATION_UUID: ExposureNotificationFrame, EDDYSTONE_UUID: Struct( "frame_type" / Byte, "frame" / Switch( lambda ctx: ctx.frame_type, { EDDYSTONE_UID_FRAME: EddystoneUIDFrame, EDDYSTONE_URL_FRAME: EddystoneURLFrame, EDDYSTONE_TLM_FRAME: EddystoneTLMFrame, EDDYSTONE_EID_FRAME: EddystoneEIDFrame, })), ESTIMOTE_UUID: Struct(
from construct.core import Struct from construct.macros import * from construct import RepeatUntil, OneOf from util import hexdump SCFGItem = Struct("SCFGItem", String("tag", 4), String("data", 16, padchar="\x00") ) SCFG = Struct("SCFG", OneOf(String("magic", 4), ["gfCS"]), ULInt32("length"), ULInt32("unk1"), ULInt32("unk2"), ULInt32("unk3"), ULInt32("unk4") ) def parse_SCFG(data): res = {} scfg = SCFG.parse(data) assert scfg.length > 0x18 for i in Array((scfg.length - 0x18) / 20, SCFGItem).parse(data[0x18:scfg.length]): if i.tag != "\xFF\xFF\xFF\xFF": res[str(i.tag)[::-1]] = str(i.data) return res
ADDRESS_TYPE_NOT_SUPPORTED=0x08) AddrType = Enum(Byte, IPV4=0x01, DOMAINNAME=0x03, IPV6=0x04) Requestv4 = Struct("cmd" / Byte, "port" / Int16ub, "addr" / BytesInteger(4), "name" / CString(), "domainname" / If(this.addr == 1, CString())) Responsev4 = Struct( "status" / Byte, "port" / Int16ub, "addr" / BytesInteger(4), ) GreetingRequest = Struct( "version" / OneOf(Int8ub, [4, 5]), Embedded( Switch( this.version, { 0x4: Requestv4, 0x5: Struct("nmethod" / Int8ub, "methods" / Array(this.nmethod, Byte)) }))) GreetingResponse = Struct( "version" / OneOf(Int8ub, [0, 5]), Embedded( Switch(this.version, { 0x0: Responsev4,