|
| 1 | +from abc import ABC, abstractmethod |
| 2 | + |
| 3 | +from ._types import * |
| 4 | +from ._message import Message |
| 5 | +from ._device import Device |
| 6 | + |
| 7 | +class Command(Message): |
| 8 | + |
| 9 | + def __init__(self): |
| 10 | + self._address = None |
| 11 | + self._code = None |
| 12 | + |
| 13 | + @property |
| 14 | + @abstractmethod |
| 15 | + def command_code(self) -> int: |
| 16 | + pass |
| 17 | + |
| 18 | + @abstractmethod |
| 19 | + def security_control_block(self) -> bytes: |
| 20 | + pass |
| 21 | + |
| 22 | + @abstractmethod |
| 23 | + def custom_command_update(self, command_buffer: bytearray): |
| 24 | + pass |
| 25 | + |
| 26 | + def build_command(self, device: Device) -> bytes: |
| 27 | + command_buffer = bytearray([ |
| 28 | + self.SOM, |
| 29 | + self.address, |
| 30 | + 0x00, |
| 31 | + 0x00, |
| 32 | + device.message_control.control_byte |
| 33 | + ]) |
| 34 | + |
| 35 | + if device.message_control.has_security_control_block: |
| 36 | + command_buffer.extend(self.security_control_block()) |
| 37 | + |
| 38 | + command_buffer.append(self.command_code) |
| 39 | + |
| 40 | + if device.is_security_established: |
| 41 | + command_buffer.extend(self.encrypted_data(device)) |
| 42 | + |
| 43 | + # TODO: I don't think this needed |
| 44 | + # include mac and crc/checksum in length before generating mac |
| 45 | + # additional_length = 4 + (device.message_control.use_crc ? 2 : 1) |
| 46 | + # self.add_packet_length(command_buffer, additional_length) |
| 47 | + |
| 48 | + command_buffer.extend(device.generate_mac(bytes(command_buffer), True)[0:4]) |
| 49 | + else: |
| 50 | + command_buffer.extend(self.data()) |
| 51 | + |
| 52 | + command_buffer.append(0x00) |
| 53 | + if device.message_control.use_crc: |
| 54 | + command_buffer.append(0x00) |
| 55 | + |
| 56 | + self.add_packet_length(command_buffer) |
| 57 | + |
| 58 | + if device.message_control.use_crc: |
| 59 | + self.add_crc(command_buffer) |
| 60 | + else: |
| 61 | + self.add_checksum(command_buffer) |
| 62 | + |
| 63 | + custom_command_update(command_buffer) |
| 64 | + |
| 65 | + return bytes(command_buffer) |
| 66 | + |
| 67 | +class PollCommand(Command): |
| 68 | + |
| 69 | + def __init__(self, address: int): |
| 70 | + self.address = address |
| 71 | + |
| 72 | + def command_code(self) -> int: |
| 73 | + return 0x60 |
| 74 | + |
| 75 | + def security_control_block(self) -> bytes: |
| 76 | + return bytes([ 0x02, 0x15 ]) |
| 77 | + |
| 78 | + def data() -> bytes: |
| 79 | + return bytes([]) |
| 80 | + |
| 81 | + def custom_command_update(self, command_buffer: bytearray): |
| 82 | + pass |
| 83 | + |
| 84 | +class IdReportCommand(Command): |
| 85 | + |
| 86 | + def __init__(self, address: int): |
| 87 | + self.address = address |
| 88 | + |
| 89 | + def command_code(self) -> int: |
| 90 | + return 0x61 |
| 91 | + |
| 92 | + def security_control_block(self) -> bytes: |
| 93 | + return bytes([ 0x02, 0x17 ]) |
| 94 | + |
| 95 | + def data() -> bytes: |
| 96 | + return bytes([ 0x00 ]) |
| 97 | + |
| 98 | + def custom_command_update(self, command_buffer: bytearray): |
| 99 | + pass |
| 100 | + |
| 101 | +class DeviceCapabilitiesCommand(Command): |
| 102 | + |
| 103 | + def __init__(self, address: int): |
| 104 | + self.address = address |
| 105 | + |
| 106 | + def command_code(self) -> int: |
| 107 | + return 0x62 |
| 108 | + |
| 109 | + def security_control_block(self) -> bytes: |
| 110 | + return bytes([ 0x02, 0x17 ]) |
| 111 | + |
| 112 | + def data() -> bytes: |
| 113 | + return bytes([ 0x00 ]) |
| 114 | + |
| 115 | + def custom_command_update(self, command_buffer: bytearray): |
| 116 | + pass |
| 117 | + |
| 118 | +class LocalStatusReportCommand(Command): |
| 119 | + |
| 120 | + def __init__(self, address: int): |
| 121 | + self.address = address |
| 122 | + |
| 123 | + def command_code(self) -> int: |
| 124 | + return 0x64 |
| 125 | + |
| 126 | + def security_control_block(self) -> bytes: |
| 127 | + return bytes([ 0x02, 0x15 ]) |
| 128 | + |
| 129 | + def data() -> bytes: |
| 130 | + return bytes([]) |
| 131 | + |
| 132 | + def custom_command_update(self, command_buffer: bytearray): |
| 133 | + pass |
| 134 | + |
| 135 | +class InputStatusReportCommand(Command): |
| 136 | + |
| 137 | + def __init__(self, address: int): |
| 138 | + self.address = address |
| 139 | + |
| 140 | + def command_code(self) -> int: |
| 141 | + return 0x65 |
| 142 | + |
| 143 | + def security_control_block(self) -> bytes: |
| 144 | + return bytes([ 0x02, 0x15 ]) |
| 145 | + |
| 146 | + def data() -> bytes: |
| 147 | + return bytes([ ]) |
| 148 | + |
| 149 | + def custom_command_update(self, command_buffer: bytearray): |
| 150 | + pass |
| 151 | + |
| 152 | + |
| 153 | +class OutputStatusReportCommand(Command): |
| 154 | + |
| 155 | + def __init__(self, address: int): |
| 156 | + self.address = address |
| 157 | + |
| 158 | + def command_code(self) -> int: |
| 159 | + return 0x66 |
| 160 | + |
| 161 | + def security_control_block(self) -> bytes: |
| 162 | + return bytes([ 0x02, 0x15 ]) |
| 163 | + |
| 164 | + def data() -> bytes: |
| 165 | + return bytes([]) |
| 166 | + |
| 167 | + def custom_command_update(self, command_buffer: bytearray): |
| 168 | + pass |
| 169 | + |
| 170 | +class ReaderStatusReportCommand(Command): |
| 171 | + |
| 172 | + def __init__(self, address: int): |
| 173 | + self.address = address |
| 174 | + |
| 175 | + def command_code(self) -> int: |
| 176 | + return 0x67 |
| 177 | + |
| 178 | + def security_control_block(self) -> bytes: |
| 179 | + return bytes([ 0x02, 0x15 ]) |
| 180 | + |
| 181 | + def data() -> bytes: |
| 182 | + return bytes([]) |
| 183 | + |
| 184 | + def custom_command_update(self, command_buffer: bytearray): |
| 185 | + pass |
| 186 | + |
| 187 | + |
| 188 | +class OutputControlCommand(Command): |
| 189 | + |
| 190 | + def __init__(self, address: int, output_controls: OutputControls): |
| 191 | + self.address = address |
| 192 | + self.output_controls = output_controls |
| 193 | + |
| 194 | + def command_code(self) -> int: |
| 195 | + return 0x68 |
| 196 | + |
| 197 | + def security_control_block(self) -> bytes: |
| 198 | + return bytes([ 0x02, 0x17 ]) |
| 199 | + |
| 200 | + def data() -> bytes: |
| 201 | + return self.output_controls.build_data() |
| 202 | + |
| 203 | + def custom_command_update(self, command_buffer: bytearray): |
| 204 | + pass |
| 205 | + |
| 206 | + |
| 207 | +class ReaderLedControlCommand(Command): |
| 208 | + |
| 209 | + def __init__(self, address: int, reader_led_controls: ReaderLedControls): |
| 210 | + self.address = address |
| 211 | + self.reader_led_controls = reader_led_controls |
| 212 | + |
| 213 | + def command_code(self) -> int: |
| 214 | + return 0x69 |
| 215 | + |
| 216 | + def security_control_block(self) -> bytes: |
| 217 | + return bytes([ 0x02, 0x17 ]) |
| 218 | + |
| 219 | + def data() -> bytes: |
| 220 | + return self.reader_led_controls.build_data() |
| 221 | + |
| 222 | + def custom_command_update(self, command_buffer: bytearray): |
| 223 | + pass |
| 224 | + |
| 225 | +class SecurityInitializationRequestCommand(Command): |
| 226 | + |
| 227 | + def __init__(self, address: int, server_random_number: bytes): |
| 228 | + self.address = address |
| 229 | + self.server_random_number = server_random_number |
| 230 | + |
| 231 | + def command_code(self) -> int: |
| 232 | + return 0x76 |
| 233 | + |
| 234 | + def security_control_block(self) -> bytes: |
| 235 | + return bytes([ 0x03, 0x11, 0x00 ]) |
| 236 | + |
| 237 | + def data() -> bytes: |
| 238 | + return self.server_random_number |
| 239 | + |
| 240 | + def custom_command_update(self, command_buffer: bytearray): |
| 241 | + pass |
| 242 | + |
| 243 | + |
| 244 | +class ServerCryptogramCommand(Command): |
| 245 | + |
| 246 | + def __init__(self, address: int, server_cryptogram: bytes): |
| 247 | + self.address = address |
| 248 | + self.server_cryptogram = server_cryptogram |
| 249 | + |
| 250 | + def command_code(self) -> int: |
| 251 | + return 0x77 |
| 252 | + |
| 253 | + def security_control_block(self) -> bytes: |
| 254 | + return bytes([ 0x03, 0x13, 0x00 ]) |
| 255 | + |
| 256 | + def data() -> bytes: |
| 257 | + return self.server_cryptogram |
| 258 | + |
| 259 | + def custom_command_update(self, command_buffer: bytearray): |
| 260 | + pass |
| 261 | + |
0 commit comments