-
Notifications
You must be signed in to change notification settings - Fork 2
/
packet_spec.py
190 lines (161 loc) · 5.55 KB
/
packet_spec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
"""
packet_spec.py
This file contains dataclass definitions of the packet header and packets from the packet specification, and methods to parse
byte streams into their respective classes
"""
from dataclasses import dataclass
from abc import ABC
from enum import Enum
import numpy as np
import struct
class PacketType(Enum):
CONTROL = 0
TELEMETRY = 1
class TelemetryPacketSubType(Enum):
TEMPERATURE = 0
PRESSURE = 1
MASS = 2
ARMING_STATE = 3
ACT_STATE = 4
WARNING = 5
ACT_REQ = 6
ACT_ACK = 7
ARM_REQ = 8
ARM_ACK = 9
class ActuationRequestStatus(Enum):
ACT_OK = 0
ACT_DENIED = 1
ACT_DNE = 2
ACT_INV = 3
class ArmingState(Enum):
ARMED_PAD = 0
ARMED_VALVES = 1
ARMED_IGNITION = 2
ARMED_DISCONNECTED = 3
ARMED_LAUNCH = 4
class Warning(Enum):
HIGH_PRESSURE = 0
HIGH_TEMP = 1
class ActuatorState(Enum):
OFF = 0
ON = 1
class AcknowledgementStatus(Enum):
ARM_OK = 0
ARM_DENIED = 1
ARM_INV = 2
@dataclass
class PacketHeader:
type: PacketType
sub_type: TelemetryPacketSubType
@dataclass
class PacketMessage(ABC):
time_since_power: int
@dataclass
class ActuationRequest():
id: int
state: ActuatorState
@dataclass
class ActuationAcknowledgement():
id: int
status: ActuationRequestStatus
@dataclass
class ArmingRequest():
level: ArmingState
@dataclass
class ArmingAcknowledgement():
status: AcknowledgementStatus
@dataclass
class TemperaturePacket(PacketMessage):
temperature: int
id: int
@dataclass
class PressurePacket(PacketMessage):
pressure: int
id: int
@dataclass
class MassPacket(PacketMessage):
mass: int
id: int
@dataclass
class ArmingStatePacket(PacketMessage):
state: ArmingState
@dataclass
class ActuatorStatePacket(PacketMessage):
id: int
state: ActuatorState
@dataclass
class WarningPacket(PacketMessage):
type: Warning
def parse_packet_header(header_bytes: bytes) -> PacketHeader:
packet_type: int
packet_sub_type: int
packet_type, packet_sub_type = struct.unpack("<BB", header_bytes)
return PacketHeader(PacketType(packet_type), TelemetryPacketSubType(packet_sub_type))
def packet_message_bytes_length(header: PacketHeader) -> int:
match header.type:
case PacketType.TELEMETRY:
match header.sub_type:
case TelemetryPacketSubType.TEMPERATURE | TelemetryPacketSubType.PRESSURE | TelemetryPacketSubType.MASS:
return 9
case TelemetryPacketSubType.ACT_STATE:
return 6
case TelemetryPacketSubType.ARMING_STATE | TelemetryPacketSubType.WARNING:
return 5
def parse_packet_message(header: PacketHeader, message_bytes: bytes) -> PacketMessage:
match header.type:
case PacketType.CONTROL:
match header.sub_type:
case TelemetryPacketSubType.ACT_REQ:
id: int
state: int
id, state = struct.unpack("<BB". message_bytes)
return ActuationRequest(id=id, state=state)
case TelemetryPacketSubType.ACT_ACK:
id: int
status: int
id, status = struct.unpack("<BB", message_bytes)
return ActuationAcknowledgement(id=id, status=status)
case TelemetryPacketSubType.ARM_REQ:
level:int
level = struct.unpack("<B", message_bytes)
return ArmingRequest(level=level)
case TelemetryPacketSubType.ARM_ACK:
status:int
status = struct.unpack("<B", message_bytes)
return ArmingAcknowledgement(status=status)
case PacketType.TELEMETRY:
match header.sub_type:
case TelemetryPacketSubType.TEMPERATURE:
temperature: int
id: int
time: int
time, temperature, id = struct.unpack("<IIB", message_bytes)
return TemperaturePacket(temperature=temperature, id=id, time_since_power=time)
case TelemetryPacketSubType.PRESSURE:
pressure: int
id: int
time: int
time, pressure, id = struct.unpack("<IIB", message_bytes)
return PressurePacket(pressure=pressure, id=id, time_since_power=time)
case TelemetryPacketSubType.MASS:
time: int
mass: int
id: int
time, mass, id = struct.unpack("<IIB", message_bytes)
return MassPacket(mass=mass, id=id, time_since_power=time)
case TelemetryPacketSubType.ARMING_STATE:
time:int
state:int
time, state = struct.unpack("<IB", message_bytes)
return ArmingStatePacket(state=ArmingState(state), time_since_power=time)
case TelemetryPacketSubType.ACT_STATE:
time:int
state:int
id:int
time, id, state = struct.unpack("<IBB", message_bytes)
return ActuatorStatePacket(time_since_power=time, id=id, state=ActuatorState(state))
case TelemetryPacketSubType.WARNING:
time:int
type:int
time, type = struct.unpack("<IB", message_bytes)
return WarningPacket(type=Warning(type), time_since_power=time)