Artemis Teensy Flight Software
The software on the Teensy in the Artemis cubesat.
packetcomm.h
1 #ifndef PACKETCOMM_H
2 #define PACKETCOMM_H
3 
5 #include "math/crclib.h"
6 #include "support/sliplib.h"
7 #include "support/ax25class.h"
8 // #include "support/timelib.h"
9 
10 namespace Cosmos {
11  namespace Support {
12  class PacketComm
13  {
14  public:
15  struct __attribute__ ((packed)) CCSDS_Header {
16  uint8_t tf_version:2;
17  uint16_t spacecraft_id:10;
18  uint8_t virtual_channel_id:3;
19  uint8_t ocf_flag:1;
20  uint8_t master_frame_cnt;
21  uint8_t virtual_frame_cnt; // Start frame, will increment
22  uint16_t tf_data_field_status;
23  };
24 
25  PacketComm(uint16_t size=4);
26 
27  enum class TypeId : uint16_t {
28  Blank = 0,
29 
30  DataObc = 0x100,
31  DataObcBeacon = 0x101,
32  DataObcPong = 0x102,
33  DataObcResponse = 0x103,
34  DataObcTime = 0x104,
35  DataObcNop = 0x17f,
36  CommandObc = 0x180,
37  CommandObcReset = 0x181,
38  CommandObcReboot = 0x182,
39  CommandObcPing = 0x183,
40  CommandObcSetTime = 0x184,
41  CommandObcGetTimeHuman = 0x185,
42  CommandObcGetTimeBinary = 0x186,
43  CommandObcExternalCommand = 0x187,
44  CommandObcExternalTask = 0x188,
45  CommandObcInternalRequest = 0x189,
46  CommandObcSendBeacon = 0x18a,
47  CommandObcHalt = 0x18b,
48  CommandObcNop = 0x1ff,
49 
50  DataEps = 0x200,
51  DataEpsResponse = 0x201,
52  CommandEps = 0x280,
53  CommandEpsCommunicate = 0x281,
54  CommandEpsSwitchName = 0x282,
55  CommandEpsSwitchNumber = 0x283,
56  CommandEpsReset = 0x284,
57  CommandEpsState = 0x285,
58  CommandEpsWatchdog = 0x286,
59  CommandEpsSetTime = 0x287,
60  CommandEpsMinimumPower = 0x288,
61  CommandEpsSwitchNames = 0x289,
62  CommandEpsSwitchStatus = 0x28a,
63 
64  DataAdcs = 0x300,
65  DataAdcsResponse = 0x301,
66  CommandAdcs = 0x380,
67  CommandAdcsCommunicate = 0x381,
68  CommandAdcsState = 0x382,
69  CommandAdcsSetRunMode = 0x383,
70  CommandAdcsGetAdcsState = 0x384,
71  CommandAdcsOrbitParameters = 0x385,
72 
73  DataFile = 0x400,
74  DataFileCommand = 0x401,
75  DataFileMessage = 0x402,
76  DataFileQueue = 0x403,
77  DataFileCancel = 0x404,
78  DataFileComplete = 0x405,
79  DataFileReqMeta = 0x406,
80  DataFileReqData = 0x407,
81  DataFileMetaData = 0x408,
82  DataFileChunkData = 0x409,
83  DataFileReqComplete = 0x40a,
84  CommandFile = 0x480,
85  CommandFileListDirectory = 0x481,
86  CommandFileTransferFile = 0x482,
87  CommandFileTransferNode = 0x483,
88  CommandFileTransferRadio = 0x484,
89  CommandFileTransferList = 0x485,
90 
91  DataExec = 0x500,
92  CommandExec = 0x580,
93  CommandExecClearQueue = 0x581,
94  CommandExecSetOpsMode = 0x582,
95  CommandExecEnableChannel = 0x583,
96  CommandExecLoadCommand = 0x584,
97  CommandExecAddCommand = 0x585,
98 
99  DataRadio = 0x700,
100  DataRadioTest = 0x701,
101  DataRadioResponse = 0x702,
102  CommandRadio = 0x780,
103  CommandRadioTest = 0x781,
104  CommandRadioCommunicate = 0x782,
105  CommandRadioAstrodevCommunicate = 0x783,
106 
107  DataCamera = 0x800,
108  CommandCamera = 0x880,
109  CommandCameraOn = 0x881,
110  CommandCameraCapture = 0x882,
111  };
112 
113  std::map<TypeId, string> TypeString = {
114  {TypeId::DataObcBeacon, "Beacon"},
115  {TypeId::DataObcNop, "Nop"},
116  {TypeId::DataObcPong, "Pong"},
117  {TypeId::DataEpsResponse, "EpsResponse"},
118  {TypeId::DataRadioResponse, "RadioResponse"},
119  {TypeId::DataAdcsResponse, "AdcsResponse"},
120  {TypeId::DataObcResponse, "Response"},
121  {TypeId::DataRadioTest, "Test"},
122  {TypeId::DataObcTime, "Time"},
123  {TypeId::DataFileCommand, "FileCommand"},
124  {TypeId::DataFileMessage, "FileMessage"},
125  {TypeId::DataFileQueue, "FileQueue"},
126  {TypeId::DataFileCancel, "FileCancel"},
127  {TypeId::DataFileComplete, "FileComplete"},
128  {TypeId::DataFileReqMeta, "FileReqMeta"},
129  {TypeId::DataFileReqData, "FileReqData"},
130  {TypeId::DataFileMetaData, "FileMetaData"},
131  {TypeId::DataFileChunkData, "FileChunkData"},
132  {TypeId::DataFileReqComplete, "FileReqComplete"},
133  {TypeId::CommandObcReset, "Reset"},
134  {TypeId::CommandObcReboot, "Reboot"},
135  {TypeId::CommandObcSendBeacon, "SendBeacon"},
136  {TypeId::CommandExecClearQueue, "ClearQueue"},
137  {TypeId::CommandObcExternalCommand, "ExternalCommand"},
138  {TypeId::CommandObcExternalTask, "ExternalTask"},
139  {TypeId::CommandObcHalt, "Halt"},
140  {TypeId::CommandRadioTest, "TestRadio"},
141  {TypeId::CommandFileListDirectory, "ListDirectory"},
142  {TypeId::CommandFileTransferFile, "TransferFile"},
143  {TypeId::CommandFileTransferNode, "TransferNode"},
144  {TypeId::CommandFileTransferRadio, "TransferRadio"},
145  {TypeId::CommandFileTransferList, "TransferList"},
146  {TypeId::CommandObcInternalRequest, "InternalRequest"},
147  {TypeId::CommandObcPing, "Ping"},
148  {TypeId::CommandObcSetTime, "SetTime"},
149  {TypeId::CommandObcGetTimeHuman, "GetTimeHuman"},
150  {TypeId::CommandObcGetTimeBinary, "GetTimeBinary"},
151  {TypeId::CommandExecSetOpsMode, "SetOpsMode"},
152  {TypeId::CommandExecEnableChannel, "EnableChannel"},
153  {TypeId::CommandAdcsCommunicate, "AdcsCommunicate"},
154  {TypeId::CommandAdcsState, "AdcsState"},
155  {TypeId::CommandAdcsSetRunMode, "AdcsSetRunMode"},
156  {TypeId::CommandAdcsGetAdcsState, "AdcsGetAdcsState"},
157  {TypeId::CommandAdcsOrbitParameters, "AdcsOrbitParameters"},
158  {TypeId::CommandEpsCommunicate, "EpsCommunicate"},
159  {TypeId::CommandEpsSwitchName, "EpsSwitchName"},
160  {TypeId::CommandEpsSwitchNumber, "EpsSwitchNumber"},
161  {TypeId::CommandEpsReset, "EpsReset"},
162  {TypeId::CommandEpsState, "EpsState"},
163  {TypeId::CommandEpsWatchdog, "EpsWatchdog"},
164  {TypeId::CommandEpsSetTime, "EpsSetTime"},
165  {TypeId::CommandEpsMinimumPower, "EpsMinimumPower"},
166  {TypeId::CommandEpsSwitchNames, "EpsSwitchNames"},
167  {TypeId::CommandEpsSwitchStatus, "EpsSwitchStatus"},
168  {TypeId::CommandExecLoadCommand, "ExecLoadCommand"},
169  {TypeId::CommandExecAddCommand, "ExecAddCommand"},
170  {TypeId::CommandRadioCommunicate, "RadioCommunicate"},
171  {TypeId::CommandRadioAstrodevCommunicate, "RadioAstrodevCommunicate"},
172  {TypeId::CommandCameraOn, "CameraOn"},
173  {TypeId::CommandCameraCapture, "CameraCapture"},
174  };
175 
176  std::map<string, TypeId> StringType = {
177  {"Beacon", TypeId::DataObcBeacon},
178  {"Nop", TypeId::DataObcNop},
179  {"Pong", TypeId::DataObcPong},
180  {"EpsResponse", TypeId::DataEpsResponse},
181  {"RadioResponse", TypeId::DataRadioResponse},
182  {"AdcsResponse", TypeId::DataAdcsResponse},
183  {"Response", TypeId::DataObcResponse},
184  {"Test", TypeId::DataRadioTest},
185  {"Time", TypeId::DataObcTime},
186 
187  {"FileCommand", TypeId::DataFileCommand},
188  {"FileMessage", TypeId::DataFileMessage},
189  {"FileQueue", TypeId::DataFileQueue},
190  {"FileCancel", TypeId::DataFileCancel},
191  {"FileComplete", TypeId::DataFileComplete},
192  {"FileReqMeta", TypeId::DataFileReqMeta},
193  {"FileReqData", TypeId::DataFileReqData},
194  {"FileMetaData", TypeId::DataFileMetaData},
195  {"FileChunkData", TypeId::DataFileChunkData},
196  {"FileReqComplete", TypeId::DataFileReqComplete},
197 
198  {"Reset", TypeId::CommandObcReset},
199  {"Reboot", TypeId::CommandObcReboot},
200  {"SendBeacon", TypeId::CommandObcSendBeacon},
201  {"ClearQueue", TypeId::CommandExecClearQueue},
202  {"ExternalCommand", TypeId::CommandObcExternalCommand},
203  {"ExternalTask", TypeId::CommandObcExternalTask},
204  {"Halt", TypeId::CommandObcHalt},
205  {"TestRadio", TypeId::CommandRadioTest},
206  {"ListDirectory", TypeId::CommandFileListDirectory},
207  {"TransferFile", TypeId::CommandFileTransferFile},
208  {"TransferNode", TypeId::CommandFileTransferNode},
209  {"TransferRadio", TypeId::CommandFileTransferRadio},
210  {"TransferList", TypeId::CommandFileTransferList},
211  {"InternalRequest", TypeId::CommandObcInternalRequest},
212  {"Ping", TypeId::CommandObcPing},
213  {"SetTime", TypeId::CommandObcSetTime},
214  {"GetTimeHuman", TypeId::CommandObcGetTimeHuman},
215  {"GetTimeBinary", TypeId::CommandObcGetTimeBinary},
216  {"SetOpsMode", TypeId::CommandExecSetOpsMode},
217  {"EnableChannel", TypeId::CommandExecEnableChannel},
218  {"EpsCommunicate", TypeId::CommandEpsCommunicate},
219  {"EpsSwitchName", TypeId::CommandEpsSwitchName},
220  {"EpsSwitchNumber", TypeId::CommandEpsSwitchNumber},
221  {"EpsReset", TypeId::CommandEpsReset},
222  {"EpsState", TypeId::CommandEpsState},
223  {"EpsWatchdog", TypeId::CommandEpsWatchdog},
224  {"EpsSetTime", TypeId::CommandEpsSetTime},
225  {"EpsMinimumPower", TypeId::CommandEpsMinimumPower},
226  {"EpsSwitchNames", TypeId::CommandEpsSwitchNames},
227  {"EpsSwitchStatus", TypeId::CommandEpsSwitchStatus},
228  {"AdcsCommunicate", TypeId::CommandAdcsCommunicate},
229  {"AdcsState", TypeId::CommandAdcsState},
230  {"AdcsSetRunMode", TypeId::CommandAdcsSetRunMode},
231  {"AdcsGetAdcsState", TypeId::CommandAdcsGetAdcsState},
232  {"AdcsOrbitParameters", TypeId::CommandAdcsOrbitParameters},
233  {"ExecLoadCommand", TypeId::CommandExecLoadCommand},
234  {"ExecAddCommand", TypeId::CommandExecAddCommand},
235  {"RadioCommunicate", TypeId::CommandRadioCommunicate},
236  {"RadioAstrodevCommunicate", TypeId::CommandRadioAstrodevCommunicate},
237  {"CameraOn", TypeId::CommandCameraOn},
238  {"CameraCapture", TypeId::CommandCameraCapture},
239  };
240 
241  struct __attribute__ ((packed)) CommunicateHeader
242  {
243  uint8_t unit;
244  uint8_t command;
245  uint16_t responsecount;
246  };
247 
248  struct __attribute__ ((packed)) CommunicateResponseHeader
249  {
250  uint32_t deci;
251  uint8_t chunks;
252  uint8_t chunk_id;
253  uint8_t unit;
254  uint8_t command;
255  };
256 
257  // Structure is the same, but type aliases are provided for user convenience
261 
262  struct __attribute__ ((packed)) ResponseHeader
263  {
264  uint32_t deci;
265  uint32_t response_id;
266  uint8_t source_id;
267  uint8_t chunks;
268  uint8_t chunk_id;
269  };
270 
271  struct __attribute__ ((packed)) TestHeader
272  {
273  uint32_t test_id = 0;
274  uint32_t size = 0;
275  uint32_t packet_id = 0;
276  };
277 
278  struct __attribute__ ((packed)) Header
279  {
280  TypeId type = TypeId::Blank;
281  uint16_t data_size;
282  uint8_t nodeorig = 254; // refer to NodeData::NODEIDORIG;
283  uint8_t nodedest = 255; // refer to NodeData::NODEIDDEST;
284  uint8_t chanin = 0;
285  uint8_t chanout = 0;
286  } header;
287 
288  CCSDS_Header ccsds_header;
289  vector<uint8_t> packetized;
290  vector<uint8_t> wrapped;
292  vector<uint8_t> data;
293  uint16_t crc;
294  enum class PacketStyle : uint8_t
295  {
296  None,
297  Minimal,
298 // V1,
299  V2
300  };
301 
302  PacketStyle style = PacketStyle::V2;
303 
304  struct __attribute__ ((packed)) FileChunkData
305  {
306  uint16_t size;
307  uint32_t txid;
308  };
309 
310  vector<uint8_t> atsm = {0x1a, 0xcf, 0xfc, 0x1d};
311  vector<uint8_t> atsmr = {0x58, 0xf3, 0x3f, 0xb8};
312  vector<uint8_t> satsm = {0x35, 0x2e, 0xf8, 0x53};
313  CRC16 calc_crc;
314 
315  void Invert(vector<uint8_t>& data);
316  void CalcCRC();
317  bool CheckCRC();
318  int32_t Unwrap(bool checkcrc=true);
319  int32_t Unwrap(bool checkcrc, bool minimal_header);
320  int32_t RawUnPacketize(bool invert=false, bool checkcrc=true);
321  int32_t RawUnPacketize(bool invert, bool checkcrc, bool minimal_header);
322  bool ASMUnPacketize();
323  bool ASMUnPacketize(bool checkcrc, bool descramble);
324  bool SLIPUnPacketize(bool checkcrc=true);
325  bool HDLCUnPacketize(bool checkcrc=true);
326  bool AX25UnPacketize(bool checkcrc=true);
327  bool Wrap();
328  bool Wrap(bool calc_checksum);
329  bool RawPacketize();
330  bool ASMPacketize();
332  bool ASMPacketize(uint16_t packet_wrapped_size, bool scramble);
333  bool AX25Packetize(string dest_call="", string sour_call="", uint8_t flagcount=2, uint8_t dest_stat=0x60, uint8_t sour_stat=0x61, uint8_t cont=0x03, uint8_t prot=0xf0);
334  bool HDLCPacketize(uint8_t flagcount=10);
335  bool SLIPPacketize();
336 
337  private:
338 
339  };
340  }
341 }
342 
343 #endif // PACKETCOMM_H
Definition: crclib.h:43
Definition: packetcomm.h:13
bool Wrap()
Wrap up header and payload Merge Cosmos::Support::PacketComm::data and ::Cosmos::Support::PacketComm:...
Definition: packetcomm.cpp:233
vector< uint8_t > data
Data of interest.
Definition: packetcomm.h:292
Headers and definitions common to all COSMOS Kernel.
COSMOS SLIP support include file.
Definition: packetcomm.h:305
Definition: packetcomm.h:279
Definition: packetcomm.h:272