コード例 #1
0
ファイル: command.py プロジェクト: abkaya/pyd7a
    def create_with_read_file_action_system_file(
            file,
            interface_type=InterfaceType.HOST,
            interface_configuration=None):
        # default to host interface, when D7ASP interface is used prepend with Forward action
        cmd = Command()
        cmd.add_forward_action(interface_type, interface_configuration)
        cmd.add_action(
            RegularAction(operation=ReadFileData(operand=DataRequest(
                offset=Offset(id=file.id, offset=0),  # TODO offset size
                length=file.length))))

        return cmd
コード例 #2
0
    def connect(self):
        if self.connected:
            return

        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))))

        if self.skip_alp_parsing:
            self.log.info(
                "Running in skip_alp_parsing mode, not checking if we can receive the modem's UID"
            )
            self.connected = True
            self.execute_command_async(read_modem_info_action)
            return True

        resp_cmd = self.execute_command(read_modem_info_action,
                                        timeout_seconds=10)

        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",
                                      str(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:
            self.connected = True

        if self.connected:
            self.log.info(
                "connected to {}, node UID {} running D7AP v{}, application \"{}\" with git sha1 {}"
                .format(self.config["device"], self.uid,
                        self.firmware_version.d7ap_version,
                        self.firmware_version.application_name,
                        self.firmware_version.git_sha1))
            return True
        else:
            return False
コード例 #3
0
ファイル: modem.py プロジェクト: maximevince/pyd7a
    def _connect_serial_modem(self):
        self.dev = serial.Serial(
            port=self.config["device"],
            baudrate=self.config["baudrate"],
            timeout=None,
            parity=serial.PARITY_NONE,
            stopbits=serial.STOPBITS_ONE,
            bytesize=serial.EIGHTBITS,
            xonxoff=False,
            rtscts=False,
            dsrdtr=False,
            exclusive=True,
        )

        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
コード例 #4
0
    def _connect_serial_modem(self):
        self.dev = serial.Serial(
            port=self.config["device"],
            baudrate=self.config["baudrate"],
            timeout=0.5,
        )

        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=0),  # TODO offset size
                length=FirmwareVersionFile().length))))

        self.send_command(read_modem_info_action)

        # read thread not yet running here, read sync
        start_time = datetime.now()
        timeout = False
        while not timeout:
            commands, info = self.read()
            for command in commands:
                for action in command.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

            if (datetime.now() - start_time).total_seconds() > 2:
                timeout = True
                self.log("Timed out reading node information")

        return False
コード例 #5
0
 def parse_alp_file_data_request_operand(self, s):
     offset = self.parse_offset(s)
     length = Length.parse(s)
     return DataRequest(length=length.value, offset=offset)
コード例 #6
0
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,
                          stop_on_err=False,
                          record=False),
                  dorm_to=CT(),
                  addressee=Addressee(access_class=0x01,
                                      id_type=IdType.NOID,
                                      id=None,
                                      nls_method=NlsMethod.NONE)))

# create the command to write the action file and interface configuration file,
コード例 #7
0
 def parse_alp_file_data_request_operand(self, s):
     offset = self.parse_offset(s)
     length = s.read("uint:8")
     return DataRequest(length=length, offset=offset)