Skip to content

Commit ec10a38

Browse files
committed
Restructure the source code, fixed syntax errors
1 parent 55c59fb commit ec10a38

40 files changed

Lines changed: 885 additions & 840 deletions

osdp/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
#!/usr/bin/env python
3+
# -*- coding: utf-8 -*-
4+
5+
# Python OSDP Module.
6+
7+
"""
8+
Python OSDP Module.
9+
~~~~
10+
:author: Ryan Hu<huzhiren@gmail.com>
11+
:copyright: Copyright 2019 Ryan Hu and Contributors
12+
:license: Apache License, Version 2.0
13+
:source: <https://github.com/ryanhz/osdp-python>
14+
"""
15+
16+
17+
from ._types import *
18+
from ._connection import OsdpConnection, SerialPortOsdpConnection, TcpClientOsdpConnection, TcpServerOsdpConnection
19+
from ._device import Device
20+
from ._message import Message
21+
from ._command import *
22+
from ._reply import *
23+
from ._secure_channel import SecureChannel
24+
from ._bus import Bus
25+
from ._control_panel import ControlPanel
26+
27+
28+
29+
__author__ = 'Ryan Hu<huzhiren@gmail.com>'
30+
__copyright__ = 'Copyright 2019 Ryan Hu and Contributors'
31+
__license__ = 'Apache License, Version 2.0'

osdp/bus.py renamed to osdp/_bus.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
from threading import Lock
66
from uuid import UUID, uuid4
77

8+
from ._connection import OsdpConnection
9+
from ._device import Device
10+
from ._command import Command
11+
from ._reply import Reply
12+
813
log = logging.getLogger('osdp')
914

1015
'''
@@ -112,7 +117,7 @@ def process_reply(self, reply: Reply, device: Device):
112117

113118
extract_reply_data = reply.extract_reply_data
114119
error_code = ErrorCode(extract_reply_data[0])
115-
if reply.type == ReplyType.Nak and (error_code==ErrorCode.DoesNotSupportSecurityBlock || error_code==ErrorCode.DoesNotSupportSecurityBlock):
120+
if reply.type == ReplyType.Nak and (error_code==ErrorCode.DoesNotSupportSecurityBlock or error_code==ErrorCode.DoesNotSupportSecurityBlock):
116121
device.reset_security()
117122

118123
if reply.type==ReplyType.CrypticData:

osdp/_command.py

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
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

Comments
 (0)