def _connect_serial_modem(self): self.dev = serial.Serial( port = self.config["device"], baudrate = self.config["baudrate"], timeout = None, ) self.dev.flush() # ignore possible buffered data self.start_reading() read_modem_info_action = Command.create_with_read_file_action_system_file(UidFile()) read_modem_info_action.add_action( RegularAction( operation=ReadFileData( operand=DataRequest( offset=Offset(id=FirmwareVersionFile().id, offset=Length(0)), # TODO offset size length=FirmwareVersionFile().length ) ) ) ) resp_cmd = self.execute_command(read_modem_info_action, timeout_seconds=60) if len(resp_cmd) == 0: self.log.warning("Timed out reading node information") return False for action in resp_cmd[0].actions: if type(action) is RegularAction and type(action.operation) is ReturnFileData: if action.operand.offset.id == SystemFileIds.UID.value: self.uid = '{:x}'.format(struct.unpack(">Q", bytearray(action.operand.data))[0]) if action.operand.offset.id == SystemFileIds.FIRMWARE_VERSION.value: self.firmware_version = FirmwareVersionFile.parse(ConstBitStream(bytearray(action.operand.data))) if self.uid and self.firmware_version: return True return False
def test_simple_received_return_file_data_command(self): cmd = Command( generate_tag_request_action=False, actions=[ RegularAction(operation=ReturnFileData( operand=Data(data=list(bytearray("Hello world")), offset=Offset(id=0x51)))), StatusAction( status_operand_extension=StatusActionOperandExtensions. INTERFACE_STATUS, operation=InterfaceStatus(operand=InterfaceStatusOperand( interface_id=0xD7, interface_status=D7ASpStatus(channel_id=ChannelID( channel_header=ChannelHeader( channel_band=ChannelBand.BAND_433, channel_class=ChannelClass.LO_RATE, channel_coding=ChannelCoding.PN9), channel_index=16), rx_level=70, link_budget=80, target_rx_level=80, nls=False, missed=False, retry=False, unicast=False, fifo_token=200, seq_nr=0, response_to=CT(mant=20), addressee=Addressee())))) ]) expected = [ 0x62, # Interface Status action 0xD7, # D7ASP interface 32, # channel header 0, 16, # channel_id 70, # rxlevel (- dBm) 80, # link budget 80, # target rx level 0, # status 200, # fifo token 0, # seq 20, # response timeout 0x10, # addressee ctrl (NOID) 0, # access class 0x20, # action=32/ReturnFileData 0x51, # File ID 0x00, # offset 0x0b, # length 0x48, 0x65, 0x6c, 0x6c, 0x6f, # Hello 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64 # World ] bytes = bytearray(cmd) self.assertEqual(len(bytes), len(expected)) for i in xrange(len(expected)): self.assertEqual(bytes[i], expected[i])
def parse_alp_return_file_header_action(self, b7, b6, s): operand = FileHeaderOperand.parse(s) return RegularAction(group=b7, resp=b6, operation=ReturnFileHeader(operand=operand))
def parse_alp_return_file_data_action(self, b7, b6, s): operand = self.parse_alp_return_file_data_operand(s) return RegularAction(group=b7, resp=b6, operation=ReturnFileData(operand=operand))
def parse_alp_create_file_action(self, b7, b6, s): operand = FileHeaderOperand.parse(s) return RegularAction(group=b7, resp=b6, operation=CreateNewFile(operand=operand))
def parse_break_query_action(self, b7, b6, s): return RegularAction( group=b7, resp=b6, operation=BreakQuery(operand=QueryOperand.parse(s)))
def test_action_construction_switches(self): RegularAction(group=True, resp=True)
) ) ) if (config.factory_gaussian != 0xFF) or (config.factory_paramp != 0xFF): if (config.factory_gaussian != 0xFF) or (config.factory_paramp != 0xFF): fact = FactorySettingsFile(gaussian=config.factory_gaussian, paramp=config.factory_paramp) elif config.factory_gaussian != 0xFF: fact = FactorySettingsFile(gaussian=config.factory_gaussian) else: fact = FactorySettingsFile(paramp=config.factory_paramp) cmd.add_action( RegularAction( operation=WriteFileData( operand=Data( offset=Offset(id=fact.id), data=list(fact) ) ) ) ) cmd.add_action( RegularAction( operation=WriteFileData( operand=Data( offset=Offset(id=emFile.id), data=list(emFile) ) ) ) )
configure_default_logger(config.verbose) modem = Modem(config.device, config.rate, unsolicited_response_received_callback=received_command_callback) modem.connect() # command to be executed as an action: first do an arithmetic comparison of the sensor value with the supplied value... query_sensor_file_cmd = Command(generate_tag_request_action=False) query_sensor_file_cmd.add_action( RegularAction(operation=BreakQuery(operand=QueryOperand( type=QueryType.ARITH_COMP_WITH_VALUE, mask_present=False, params=ArithQueryParams(signed_data_type=False, comp_type=ArithComparisonType.GREATER_THAN), compare_length=Length(2), compare_value=[ ord(b) for b in struct.pack(">H", int(config.temperature * 10)) ], file_a_offset=Offset(id=0x40, offset=Length(0)))))) # ...if the query succeeds, read the file query_sensor_file_cmd.add_action( RegularAction(operation=ReadFileData( operand=DataRequest(offset=Offset(id=0x40), length=Length(2))))) # the interface config to send the result of above action to interface_config = InterfaceConfiguration( InterfaceType.D7ASP, Configuration(qos=QoS(resp_mod=ResponseMode.RESP_MODE_NO, retry_mod=RetryMode.RETRY_MODE_NO,
print(data) cmd = Command.create_with_write_file_action(file_id=199, data=data) answ = modem.execute_command(cmd) cmd = Command() f = open(config.file, "rb") total_length = os.path.getsize(config.file) amount_of_bytes = 239 length = 0 try: bytes = list(bytearray(f.read(amount_of_bytes))) while len(bytes) != 0: cmd.add_action( RegularAction(operation=WriteFileData(operand=Data( offset=Offset(id=200, offset=Length(length)), data=bytes)))) length += amount_of_bytes answ = modem.execute_command(cmd) worked = False for answer in answ: if answer.execution_completed and not answer.completed_with_error: worked = True if not worked: print("write failed") break cmd = Command() print( chr(27) + "[2J\n" + str(length) + "/" + str(total_length) + ": " + str(length * 100 / total_length) + "%")
def test_simple_received_return_file_data_command_with_tag_request(self): cmd = Command( tag_id=25, actions=[ RegularAction(operation=ReturnFileData( operand=Data(data=list(bytearray("Hello world")), offset=Offset(id=0x51)))), StatusAction( status_operand_extension=StatusActionOperandExtensions. INTERFACE_STATUS, operation=InterfaceStatus(operand=InterfaceStatusOperand( interface_id=0xD7, interface_status=D7ASpStatus(channel_header=0, channel_index=16, rx_level=70, link_budget=80, target_rx_level=80, nls=False, missed=False, retry=False, unicast=False, fifo_token=200, seq_nr=0, response_to=CT(mant=20), addressee=Addressee())))) ]) expected = [ 0xB4, # tag request with send response bit set 25, # tag ID 0x62, # Interface Status action 0xD7, # D7ASP interface 0, # channel header 16, 0, # channel_id 70, # rxlevel (- dBm) 80, # link budget 80, # target rx level 0, # status 200, # fifo token 0, # seq 20, # response timeout 16, # addressee ctrl (BCAST) 0x20, # action=32/ReturnFileData 0x51, # File ID 0x00, # offset 0x0b, # length 0x48, 0x65, 0x6c, 0x6c, 0x6f, # Hello 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64 # World ] bytes = bytearray(cmd) self.assertEqual(len(bytes), len(expected)) for i in xrange(len(expected)): self.assertEqual(bytes[i], expected[i])