Exemple #1
0
    def write_sensor_register(self, address: Union[int, List[int]],
                              value: Union[bytes, List[bytes]]) -> None:
        print(f"write_sensor_register({address}, {value})")
        if isinstance(address, int):
            if not isinstance(value, bytes):
                raise ValueError("Invalid value")

            message = b"\x00" + encode("<H", address) + value

        else:
            if isinstance(value, bytes):
                raise ValueError("Invalid value")

            length = len(address)
            if len(value) != length:
                raise ValueError("Invalid value")

            message = b""
            message += b"\x01"
            for i in length:
                if len(value[i]) != 2:
                    raise ValueError("Invalid value")

                message += encode("<H", address[i])
                message += value[i]

        self.write(
            encode_message_pack(
                encode_message_protocol(message,
                                        COMMAND_WRITE_SENSOR_REGISTER)))

        check_ack(
            check_message_protocol(check_message_pack(self.read()),
                                   COMMAND_ACK), COMMAND_WRITE_SENSOR_REGISTER)
Exemple #2
0
    def preset_psk_read_r(self, address: int, length: int = 0) -> bytes:
        print(f"preset_psk_read_r({address}, {length})")

        self.write(
            encode_message_pack(
                encode_message_protocol(
                    encode("<I", address) + encode("<I", length),
                    COMMAND_PRESET_PSK_READ_R)))

        check_ack(
            check_message_protocol(check_message_pack(self.read()),
                                   COMMAND_ACK), COMMAND_PRESET_PSK_READ_R)

        message = check_message_protocol(check_message_pack(self.read()),
                                         COMMAND_PRESET_PSK_READ_R)

        length = len(message)
        if length < 9:
            raise SystemError("Invalid response length")

        psk_length = decode("<I", message[5:9])[0]
        if length - 9 < psk_length:
            raise SystemError("Invalid response length")

        if message[0] != 0x00 or decode("<I", message[1:5])[0] != address:
            raise SystemError("Invalid response")

        return message[9:9 + psk_length]
Exemple #3
0
    def check_firmware(self,
                       offset: int,
                       length: int,
                       checksum: int,
                       payload: Optional[bytes] = None) -> None:
        print(f"update_firmware({offset}, {length}, {checksum}, {payload})")

        if payload is None:
            payload = b""

        self.write(
            encode_message_pack(
                encode_message_protocol(
                    encode("<I", offset) + encode("<I", length) +
                    encode("<I", checksum) + payload, COMMAND_CHECK_FIRMWARE)))

        check_ack(
            check_message_protocol(check_message_pack(self.read()),
                                   COMMAND_ACK), COMMAND_CHECK_FIRMWARE)

        message = check_message_protocol(check_message_pack(self.read()),
                                         COMMAND_CHECK_FIRMWARE)

        if len(message) != 2:
            raise SystemError("Invalid response length")

        if message[0] != 0x01:
            raise SystemError("Invalid response")
Exemple #4
0
    def reset(self,
              reset_sensor: bool = True,
              soft_reset_mcu: bool = False,
              sleep_time: int = 20) -> Optional[int]:
        print(f"reset({reset_sensor}, {soft_reset_mcu}, {sleep_time})")

        self.write(
            encode_message_pack(
                encode_message_protocol(
                    encode("<B", (0x1 if reset_sensor else 0x0) |
                           (0x1 if soft_reset_mcu else 0x0) << 1) +
                    encode("<B", sleep_time), COMMAND_RESET)))

        check_ack(
            check_message_protocol(check_message_pack(self.read()),
                                   COMMAND_ACK), COMMAND_RESET)

        if soft_reset_mcu:
            return None

        message = check_message_protocol(check_message_pack(self.read()),
                                         COMMAND_RESET)

        if len(message) != 3:
            raise SystemError("Invalid response length")

        if message[0] != 0x01:
            raise SystemError("Invalid response")

        return decode("<H", message[1:3])[0]
Exemple #5
0
def encode_message_pack(payload: bytes,
                        flags: int = FLAGS_MESSAGE_PROTOCOL,
                        length: Optional[int] = None) -> bytes:
    if length is None:
        length = len(payload)

    data = b""
    data += encode("<B", flags)
    data += encode("<H", length)
    data += encode("<B", sum(data) & 0xff)
    data += payload

    return data
Exemple #6
0
def encode_message_protocol(payload: bytes,
                            command: int,
                            length: Optional[int] = None,
                            checksum: bool = True) -> bytes:
    if length is None:
        length = len(payload)

    data = b""
    data += encode("<B", command)
    data += encode("<H", length + 1)
    data += payload
    data += encode("<B", 0xaa - sum(data) & 0xff if checksum else 0x88)

    return data
 def deleteURI(self, uri):
     buff = struct.pack('!B', self.DELETE_URI)
     uri16 = uri.encode('utf-16-be')
     buff += struct.pack('!L', len(uri16))
     buff += uri16
     buff += self.origin.encode('utf-16-be')
     #Send the message with its length prepended to the front
     self.socket.send(struct.encode('!L', len(buff)) + buff)
 def expireURI(self, uri, expiration_time):
     buff = struct.pack('!B', self.EXPIRE_URI)
     uri16 = uri.encode('utf-16-be')
     buff += struct.pack('!L', len(uri16))
     buff += uri16
     buff += struct.pack('!Q', expiration_time)
     buff += self.origin.encode('utf-16-be')
     #Send the message with its length prepended to the front
     self.socket.send(struct.encode('!L', len(buff)) + buff)
Exemple #9
0
    def read_sensor_register(self,
                             address: Union[int, List[int]],
                             length: int = 2) -> Union[bytes, List[bytes]]:
        print(f"read_sensor_register({address}, {length})")

        if isinstance(address, int):
            message = b"\x00" + encode("<H", address) + encode("<B", length)

        else:
            if length != 2:
                raise ValueError("Invalid length")

            message = b""
            message += b"\x01"
            for value in address:
                message += encode("<H", value)
            message += encode("<B", length)

        self.write(
            encode_message_pack(
                encode_message_protocol(message, COMMAND_READ_SENSOR_REGISTER)))

        check_ack(
            check_message_protocol(check_message_pack(self.read()),
                                   COMMAND_ACK), COMMAND_READ_SENSOR_REGISTER)

        message = check_message_protocol(check_message_pack(self.read()),
                                         COMMAND_READ_SENSOR_REGISTER)

        if isinstance(address, int):
            if len(message) != length:
                raise SystemError("Invalid response length")

            return message

        length = len(message) - 1
        if length != len(address) * 2:
            raise SystemError("Invalid response length")

        value = []
        for i in range(0, length, 2):
            value.append(message[i:i + 2])

        return value
Exemple #10
0
    def read_firmware(self, offset: int, length: int) -> bytes:
        print(f"read_firmware({offset}, {length})")

        self.write(
            encode_message_pack(
                encode_message_protocol(
                    encode("<I", offset) + encode("<I", length),
                    COMMAND_READ_FIRMWARE)))

        check_ack(
            check_message_protocol(check_message_pack(self.read()),
                                   COMMAND_ACK), COMMAND_READ_FIRMWARE)

        message = check_message_protocol(check_message_pack(self.read()),
                                         COMMAND_READ_FIRMWARE)
        if len(message) != length:
            raise SystemError("Invalid response length")

        return message
    def setup(self):
        data = self._kwargs.get('data', {})
        columns = self._kwargs.get('columns', {})

        self.value = [b'\x00']
        nulls = [0]
        for i, el in enumerate(data):
            if i > 0 and (i + 2) % 8 == 0:
                nulls.append(0)
            if el is None:
                if i < 6:
                    nulls[-1] = nulls[-1] + (1 << ((i + 2) % 8))
                else:
                    nulls[-1] = nulls[-1] + (1 << ((i - 6) % 8))
        self.value.append(bytes(nulls))

        for i, col in enumerate(columns):
            # NOTE at this moment all types sends as strings, and it works
            if data[i] is None:
                continue

            enc = ''
            col_type = col['type']
            if col_type == TYPES.MYSQL_TYPE_DOUBLE:
                enc = '<d'
            elif col_type == TYPES.MYSQL_TYPE_LONGLONG:
                enc = '<q'
            elif col_type == TYPES.MYSQL_TYPE_LONG:
                enc = '<l'
            elif col_type == TYPES.MYSQL_TYPE_FLOAT:
                enc = '<f'
            elif col_type == TYPES.MYSQL_TYPE_YEAR:
                enc = '<h'
            elif col_type == TYPES.MYSQL_TYPE_DATE:
                enc = ''
            elif col_type == TYPES.MYSQL_TYPE_TIMESTAMP:
                enc = ''
            elif col_type == TYPES.MYSQL_TYPE_DATETIME:
                enc = ''
            elif col_type == TYPES.MYSQL_TYPE_TIME:
                enc = ''
            elif col_type == TYPES.MYSQL_TYPE_NEWDECIMAL:
                enc = ''
            else:
                enc = 'string'

            if enc == '':
                raise Exception(
                    f'Column with type {col_type} cant be encripted')

            if enc == 'string':
                self.value.append(
                    Datum('string<lenenc>', str(data[i])).toStringPacket())
            else:
                self.value.append(struct.encode(enc, data[i]))[0]
Exemple #12
0
    def mcu_erase_app(self, sleep_time: int = 0) -> None:
        print(f"mcu_erase_app({sleep_time})")

        self.write(
            encode_message_pack(
                encode_message_protocol(b"\x00" + encode("<B", sleep_time),
                                        COMMAND_MCU_ERASE_APP)))

        check_ack(
            check_message_protocol(check_message_pack(self.read()),
                                   COMMAND_ACK), COMMAND_MCU_ERASE_APP)
Exemple #13
0
    def write_firmware(self, offset: int, payload: bytes) -> None:
        print(f"write_firmware({offset}, {payload})")

        self.write(
            encode_message_pack(
                encode_message_protocol(
                    encode("<I", offset) + encode("<I", len(payload)) + payload,
                    COMMAND_WRITE_FIRMWARE)))

        check_ack(
            check_message_protocol(check_message_pack(self.read()),
                                   COMMAND_ACK), COMMAND_WRITE_FIRMWARE)

        message = check_message_protocol(check_message_pack(self.read()),
                                         COMMAND_WRITE_FIRMWARE)

        if len(message) != 2:
            raise SystemError("Invalid response length")

        if message[0] != 0x01:
            raise SystemError("Invalid response")
Exemple #14
0
    def enable_chip(self, enable: bool = True) -> None:
        print(f"enable_chip({enable})")

        self.write(
            encode_message_pack(
                encode_message_protocol(
                    encode("<B", 0x1 if enable else 0x0) + b"\x00",
                    COMMAND_ENABLE_CHIP)))

        check_ack(
            check_message_protocol(check_message_pack(self.read()),
                                   COMMAND_ACK), COMMAND_ENABLE_CHIP)
Exemple #15
0
    def mcu_switch_to_idle_mode(self, sleep_time: int = 20) -> None:
        print(f"mcu_switch_to_idle_mode({sleep_time})")

        self.write(
            encode_message_pack(
                encode_message_protocol(
                    encode("<B", sleep_time) + b"\x00",
                    COMMAND_MCU_SWITCH_TO_IDLE_MODE)))

        check_ack(
            check_message_protocol(check_message_pack(self.read()),
                                   COMMAND_ACK),
            COMMAND_MCU_SWITCH_TO_IDLE_MODE)
Exemple #16
0
    def preset_psk_write_r(self, address: int, length: int,
                           payload: bytes) -> None:
        print(f"preset_psk_write_r({address}, {length}, {payload})")

        self.write(
            encode_message_pack(
                encode_message_protocol(
                    encode("<I", address) + encode("<I", length) + payload,
                    COMMAND_PRESET_PSK_WRITE_R)))

        check_ack(
            check_message_protocol(check_message_pack(self.read()),
                                   COMMAND_ACK), COMMAND_PRESET_PSK_WRITE_R)

        message = check_message_protocol(check_message_pack(self.read()),
                                         COMMAND_PRESET_PSK_WRITE_R)

        if len(message) > 2:
            raise SystemError("Invalid response length")

        if message[0] != 0x00:
            raise SystemError("Invalid response")
Exemple #17
0
    def set_powerdown_scan_frequency(self,
                                     powerdown_scan_frequency: int = 100
                                    ) -> None:
        print(f"set_powerdown_scan_frequency({powerdown_scan_frequency})")

        self.write(
            encode_message_pack(
                encode_message_protocol(encode("<H", powerdown_scan_frequency),
                                        COMMAND_SET_POWERDOWN_SCAN_FREQUENCY)))

        check_ack(
            check_message_protocol(check_message_pack(self.read()),
                                   COMMAND_ACK),
            COMMAND_SET_POWERDOWN_SCAN_FREQUENCY)

        message = check_message_protocol(check_message_pack(self.read()),
                                         COMMAND_SET_POWERDOWN_SCAN_FREQUENCY)

        if len(message) != 2:
            raise SystemError("Invalid response length")

        if message[0] != 0x01:
            raise SystemError("Invalid response")