GNU Radio's SATNOGS Package
tc_tm.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module
4  *
5  * Copyright (C) 2016, Libre Space Foundation <http://librespacefoundation.org/>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef INCLUDED_SATNOGS_TC_TM_H
22 #define INCLUDED_SATNOGS_TC_TM_H
23 
24 #include <satnogs/api.h>
25 #include <stdint.h>
26 
27 /* TM TC services*/
28 #define ECSS_VER_NUMBER 0
29 #define ECSS_DATA_FIELD_HDR_FLG 1
30 #define TC_TM_SER_TC_VER 1
31 
32 #define ECSS_PUS_VER 1
33 #define ECSS_SEC_HDR_FIELD_FLG 0
34 /* Minimal */
35 #define TC_TM_SER_TC_VER_ACC_SUCC 1
36 #define TC_TM_SER_TC_VER_ACC_FAIL 2
37 /* Additional */
38 #define TC_TM_SER_TC_VER_EXEC_START_SUCC 3
39 #define TC_TM_SER_TC_VER_EXEC_START_FAIL 4
40 #define TC_TM_SER_TC_VER_EXEC_PROG_SUCC 5
41 #define TC_TM_SER_TC_VER_EXEC_PROG_FAIL 6
42 #define TC_TM_SER_TC_VER_EXEC_COMP_SUCC 7
43 #define TC_TM_SER_TC_VER_EXEC_COMP_FAIL 8
44 
45 #define TC_TM_SER_DIST 2
46 #define TC_TM_SER_HK 3
47 
48 /* Minimal */
49 #define TC_TM_SER_HK_PAR 25
50 /* Additional */
51 
52 #define TC_TM_SER_STAT 4
53 #define TC_TM_SER_EVENT 5
54 #define TC_TM_SER_MEM 6
55 #define TC_TM_SER_NC1 7
56 #define TC_TM_SER_FMAG 8
57 #define TC_TM_SER_TMAG 9
58 #define TC_TM_SER_NC2 10
59 
60 #define TC_TM_SER_SCH 11
61 #define TC_TM_SER_MON 12
62 #define TC_TM_SER_LDATA 13
63 #define TC_TM_SER_PCKT_FWD 14
64 #define TC_TM_SER_STORAGE 15
65 #define TC_TM_SER_NC3 16
66 #define TC_TM_SER_TEST 17
67 #define TC_TM_SER_PROC 18
68 #define TC_TM_SER_EV_ACT 19
69 
70 /*sequence definitions*/
71 #define TC_TM_SEQ_FPACKET 0x01
72 #define TC_TM_SEQ_CPACKET 0x00
73 #define TC_TM_SEQ_LPACKET 0x02
74 #define TC_TM_SEQ_SPACKET 0x03
75 
76 /*services ack req*/
77 /*should confirm endianess*/
78 #define TC_ACK_NO 0x00
79 #define TC_ACK_ACC 0x01
80 #define TC_ACK_EXE_START 0x02
81 #define TC_ACK_EXE_PROG 0x04
82 #define TC_ACK_EXE_COMP 0x08
83 #define TC_ACK_ALL 0x0F
84 
85 #define TC_TM_SER_LDATA_MAX_DATA_PKT 512
86 
87 #define VER_NUMBER
88 #define TC_TM_TYPE
89 #define DATA_FIELD_HDR_FLG
90 #define APP_ID
91 #define SEQ_FLG
92 
93 #define MAX_PKT_DATA 20
94 #define MAX_EX_PKT_DATA 512
95 #define MAX_APP_ID 20
96 #define MAX_SERVICES 20
97 #define MAX_SUBTYPES 26
98 
99 #define TC 1
100 #define TM 0
101 
102 #define TC_VERIFICATION_SERVICE 1
103 #define TC_HOUSEKEEPING_SERVICE 3
104 #define TC_FUNCTION_MANAGEMENT_SERVICE 8
105 #define TC_LARGE_DATA_SERVICE 13
106 #define TC_MASS_STORAGE_SERVICE 15
107 #define TC_TEST_SERVICE 17
108 
109 #define OBC 1
110 #define EPS 2
111 #define ADCS 3
112 #define COMMS 4
113 #define IAC 5
114 #define GND 6
115 
116 #define SD1 1
117 
118 #define TURN_OFF 0
119 #define TURN_ON 1
120 #define RESET 2
121 
122 namespace gr {
123 namespace satnogs {
124 /*!
125  * \brief Telemetry and telecommands packet methods
126  * \ingroup satnogs
127  */
128 
129 /*!
130  * Return status codes
131  */
132 typedef enum {
133  R_OBC_PKT_ILLEGAL_APPID = 0, //!< R_OBC_PKT_ILLEGAL_APPID illegal application ID
134  R_OBC_PKT_INV_LEN = 1, //!< R_OBC_PKT_INV_LEN invalid length
135  R_OBC_PKT_INC_CRC = 2, //!< R_OBC_PKT_INC_CRC incorrect CRC
136  R_OBC_PKT_ILLEGAL_PKT_TP = 3, //!< R_OBC_PKT_ILLEGAL_PKT_TP
137  R_OBC_PKT_ILLEGAL_PKT_STP = 4, //!< R_OBC_PKT_ILLEGAL_PKT_STP
138  R_OBC_PKT_ILLEGAL_APP_DATA = 5, //!< R_OBC_PKT_ILLEGAL_APP_DATA
139  R_OBC_OK = 6, //!< R_OBC_OK All ok
140  R_OBC_ERROR = 7, //!< R_OBC_ERROR an error occured
141  R_OBC_EOT = 8, //!< R_OBC_EOT End-of-transfer
143 
144 union _cnv {
145  uint32_t cnv32;
146  uint16_t cnv16[2];
147  uint8_t cnv8[4];
148 };
149 
150 typedef struct {
151  /* packet id */
152  uint8_t ver; /* 3 bits, should be equal to 0 */
153  uint8_t data_field_hdr; /* 1 bit, data_field_hdr exists in data = 1 */
154  uint16_t app_id; /* TM: app id = 0 for time packets, = 0xff for idle packets. */
155  uint8_t type; /* 1 bit, tm = 0, tc = 1 */
156 
157  /* packet sequence control */
158  uint8_t seq_flags; /* 3 bits, definition in TC_SEQ_xPACKET */
159  uint16_t seq_count; /* 14 bits, packet counter, should be unique for each app id */
160 
161  uint16_t len; /* 16 bits, C = (Number of octets in packet data field) - 1 */
162 
163  uint8_t ack; /* 4 bits, definition in TC_ACK_xxxx 0 if its a TM */
164  uint8_t ser_type; /* 8 bit, service type */
165  uint8_t ser_subtype; /* 8 bit, service subtype */
166 
167  /*optional*/
168  uint8_t pckt_sub_cnt; /* 8 bits*/
169  uint16_t dest_id;
170 
171  uint8_t *data; /* variable data, this should be fixed array */
172  uint8_t padding; /* x bits, padding for word alligment */
173 
174  uint16_t crc; /* CRC or checksum, mission specific*/
175 } tc_tm_pkt;
176 
177 
181 
182 extern OBC_ret_state_t
183 verification_pack_pkt_api(uint8_t *buf, tc_tm_pkt *pkt,
184  uint16_t *buf_pointer);
185 extern OBC_ret_state_t
186 hk_pack_pkt_api(uint8_t *buf, tc_tm_pkt *pkt, uint16_t *buf_pointer);
187 
188 
189 static inline uint8_t
190 ecss_tm_checksum(const uint8_t *data, uint16_t size)
191 {
192  uint8_t CRC = 0;
193  for (int i = 0; i <= size; i++) {
194  CRC = CRC ^ data[i];
195  }
196  return CRC;
197 }
198 
199 /*Must check for endianess*/
200 static inline OBC_ret_state_t
201 ecss_tm_unpack_pkt(const uint8_t *buf, tc_tm_pkt *pkt, const uint16_t size)
202 {
203  union _cnv cnv;
204  uint8_t tmp_crc[2];
205 
206  uint8_t ver, dfield_hdr, ccsds_sec_hdr, tc_pus;
207 
208  tmp_crc[0] = buf[size - 1];
209  tmp_crc[1] = ecss_tm_checksum(buf, size - 2);
210 
211  ver = buf[0] >> 5;
212 
213  pkt->type = (buf[0] >> 4) & 0x01;
214  dfield_hdr = (buf[0] >> 3) & 0x01;
215 
216  cnv.cnv8[0] = buf[1];
217  cnv.cnv8[1] = 0x07 & buf[0];
218  pkt->app_id = cnv.cnv16[0];
219 
220  pkt->seq_flags = buf[2] >> 6;
221 
222  cnv.cnv8[0] = buf[3];
223  cnv.cnv8[1] = buf[2] & 0x3F;
224  pkt->seq_count = cnv.cnv16[0];
225 
226  cnv.cnv8[0] = buf[4];
227  cnv.cnv8[1] = buf[5];
228  pkt->len = cnv.cnv16[0];
229 
230  ccsds_sec_hdr = buf[6] >> 7;
231 
232  tc_pus = buf[6] >> 4;
233 
234  pkt->ack = 0x04 & buf[6];
235 
236  pkt->ser_type = buf[7];
237  pkt->ser_subtype = buf[8];
238  pkt->dest_id = buf[9];
239 
240  if (app_id_verification[pkt->app_id] != 1) {
242  }
243 
244  if (pkt->len != size - 7) {
245  return R_OBC_PKT_INV_LEN;
246  }
247 
248  if (tmp_crc[0] != tmp_crc[1]) {
249  return R_OBC_PKT_INC_CRC;
250  }
251 
252  if (services_verification_TC_TM[pkt->ser_type][pkt->ser_subtype][pkt->type]
253  != 1) {
255  }
256 
257  if (ver != 0) {
258  return R_OBC_ERROR;
259  }
260 
261  if (tc_pus != 1) {
262  return R_OBC_ERROR;
263  }
264 
265  if (ccsds_sec_hdr != 0) {
266  return R_OBC_ERROR;
267  }
268 
269  if (pkt->type != TC && pkt->type != TM) {
270  return R_OBC_ERROR;
271  }
272 
273  if (dfield_hdr != 1) {
274  return R_OBC_ERROR;
275  }
276 
277  if (pkt->ack != TC_ACK_NO || pkt->ack != TC_ACK_ACC
278  || pkt->ack != TC_ACK_EXE_COMP) {
279  return R_OBC_ERROR;
280  }
281 
282  for (int i = 0; i < pkt->len - 4; i++) {
283  pkt->data[i] = buf[10 + i];
284  }
285 
286  return R_OBC_OK;
287 }
288 
289 /**
290  * Packs a TC packet into a byte buffer
291  * @param buf buffer to store the data to be sent
292  * @param pkt the data to be stored in the buffer
293  * @param size size of the array
294  * @return appropriate error code or R_OBC_OK if all operation succeed
295  */
296 static inline OBC_ret_state_t
297 ecss_tm_pack_pkt(uint8_t *buf, tc_tm_pkt *pkt, uint16_t *size)
298 {
299 
300  union _cnv cnv;
301  uint8_t buf_pointer;
302 
303  cnv.cnv16[0] = pkt->app_id;
304 
305  buf[0] = (ECSS_VER_NUMBER << 5 | pkt->type << 4
306  | ECSS_DATA_FIELD_HDR_FLG << 3 | cnv.cnv8[1]);
307  buf[1] = cnv.cnv8[0];
308 
309  cnv.cnv16[0] = pkt->seq_count;
310  buf[2] = (pkt->seq_flags << 6 | cnv.cnv8[1]);
311  buf[3] = cnv.cnv8[0];
312 
313  /* TYPE = 0 TM, TYPE = 1 TC*/
314  if (pkt->type == TM) {
315  buf[6] = ECSS_PUS_VER << 4;
316  }
317  else if (pkt->type == TC) {
318  buf[6] = (ECSS_SEC_HDR_FIELD_FLG << 7 | ECSS_PUS_VER << 4 | pkt->ack);
319  }
320  else {
321  return R_OBC_ERROR;
322  }
323 
324  buf[7] = pkt->ser_type;
325  buf[8] = pkt->ser_subtype;
326  buf[9] = pkt->dest_id; /*source or destination*/
327 
328  buf_pointer = 10;
329 
330  if (pkt->ser_type == TC_VERIFICATION_SERVICE) {
331  //cnv.cnv16[0] = tc_pkt_id;
332  //cnv.cnv16[1] = tc_pkt_seq_ctrl;
333 
334  /*verification_pack_pkt_api (buf, pkt, &buf_pointer);*/
335 
336  }
337  else if (pkt->ser_type == TC_HOUSEKEEPING_SERVICE) {
338 
339  /*hk_pack_pkt_api (buf, pkt, &buf_pointer);*/
340 
341  }
343  && pkt->ser_subtype == 1) {
344 
345  buf[10] = pkt->data[0];
346 
347  buf[11] = pkt->data[1];
348  buf[12] = pkt->data[2];
349  buf[13] = pkt->data[3];
350  buf[14] = pkt->data[4];
351 
352  buf_pointer += 5;
353 
354  }
355  else {
356  return R_OBC_ERROR;
357  }
358 
359  /*check if this is correct*/
360  cnv.cnv16[0] = buf_pointer - 6;
361  buf[4] = cnv.cnv8[0];
362  buf[5] = cnv.cnv8[1];
363 
364  buf[buf_pointer] = ecss_tm_checksum(buf, buf_pointer - 1);
365  *size = buf_pointer;
366  return R_OBC_OK;
367 }
368 
369 static inline OBC_ret_state_t
370 ecss_tm_crt_pkt(tc_tm_pkt *pkt, uint16_t app_id, uint8_t type, uint8_t ack,
371  uint8_t ser_type, uint8_t ser_subtype, uint16_t dest_id)
372 {
373 
374  pkt->type = type;
375  pkt->app_id = app_id;
376  pkt->dest_id = dest_id;
377 
378  pkt->ser_type = ser_type;
379  pkt->ser_subtype = ser_subtype;
380 
381  return R_OBC_OK;
382 }
383 
384 } // namespace satnogs
385 } // namespace gr
386 
387 #endif /* INCLUDED_SATNOGS_TC_TM_H */
388 
uint8_t cnv8[4]
Definition: tc_tm.h:147
int i
Definition: decode_rs.h:71
uint8_t seq_flags
Definition: tc_tm.h:158
static OBC_ret_state_t ecss_tm_pack_pkt(uint8_t *buf, tc_tm_pkt *pkt, uint16_t *size)
Definition: tc_tm.h:297
OBC_ret_state_t verification_pack_pkt_api(uint8_t *buf, tc_tm_pkt *pkt, uint16_t *buf_pointer)
uint8_t pckt_sub_cnt
Definition: tc_tm.h:168
#define TC_VERIFICATION_SERVICE
Definition: tc_tm.h:102
Definition: tc_tm.h:144
uint16_t crc
Definition: tc_tm.h:174
const uint8_t services_verification_OBC_TC[MAX_SERVICES][MAX_SUBTYPES]
Definition: tc_tm.h:180
#define MAX_SUBTYPES
Definition: tc_tm.h:97
#define ECSS_PUS_VER
Definition: tc_tm.h:32
const uint8_t app_id_verification[MAX_APP_ID]
Definition: tc_tm.h:179
R_OBC_PKT_INV_LEN invalid length.
Definition: tc_tm.h:134
#define TC_ACK_ACC
Definition: tc_tm.h:79
#define TC_ACK_NO
Definition: tc_tm.h:78
static OBC_ret_state_t ecss_tm_crt_pkt(tc_tm_pkt *pkt, uint16_t app_id, uint8_t type, uint8_t ack, uint8_t ser_type, uint8_t ser_subtype, uint16_t dest_id)
Definition: tc_tm.h:370
static OBC_ret_state_t ecss_tm_unpack_pkt(const uint8_t *buf, tc_tm_pkt *pkt, const uint16_t size)
Definition: tc_tm.h:201
uint8_t ver
Definition: tc_tm.h:152
R_OBC_EOT End-of-transfer.
Definition: tc_tm.h:141
#define ECSS_SEC_HDR_FIELD_FLG
Definition: tc_tm.h:33
uint16_t dest_id
Definition: tc_tm.h:169
#define MAX_APP_ID
Definition: tc_tm.h:95
uint16_t cnv16[2]
Definition: tc_tm.h:146
const uint8_t services_verification_TC_TM[MAX_SERVICES][MAX_SUBTYPES][2]
Definition: tc_tm.h:178
Definition: tc_tm.h:150
uint16_t seq_count
Definition: tc_tm.h:159
R_OBC_PKT_INC_CRC incorrect CRC.
Definition: tc_tm.h:135
Definition: amsat_duv_decoder.h:29
R_OBC_PKT_ILLEGAL_APPID illegal application ID.
Definition: tc_tm.h:133
#define TC_HOUSEKEEPING_SERVICE
Definition: tc_tm.h:103
uint8_t padding
Definition: tc_tm.h:172
uint8_t * data
Definition: tc_tm.h:171
uint8_t ack
Definition: tc_tm.h:163
static uint8_t ecss_tm_checksum(const uint8_t *data, uint16_t size)
Definition: tc_tm.h:190
#define TC
Definition: tc_tm.h:99
#define ECSS_DATA_FIELD_HDR_FLG
Definition: tc_tm.h:29
OBC_ret_state_t
Telemetry and telecommands packet methods.
Definition: tc_tm.h:132
R_OBC_ERROR an error occured.
Definition: tc_tm.h:140
#define TC_ACK_EXE_COMP
Definition: tc_tm.h:82
#define MAX_SERVICES
Definition: tc_tm.h:96
uint8_t type
Definition: tc_tm.h:155
uint16_t app_id
Definition: tc_tm.h:154
R_OBC_PKT_ILLEGAL_APP_DATA.
Definition: tc_tm.h:138
uint16_t len
Definition: tc_tm.h:161
uint8_t ser_type
Definition: tc_tm.h:164
R_OBC_PKT_ILLEGAL_PKT_STP.
Definition: tc_tm.h:137
R_OBC_PKT_ILLEGAL_PKT_TP.
Definition: tc_tm.h:136
R_OBC_OK All ok.
Definition: tc_tm.h:139
uint8_t ser_subtype
Definition: tc_tm.h:165
#define TC_FUNCTION_MANAGEMENT_SERVICE
Definition: tc_tm.h:104
#define TM
Definition: tc_tm.h:100
uint8_t data_field_hdr
Definition: tc_tm.h:153
#define ECSS_VER_NUMBER
Definition: tc_tm.h:28
OBC_ret_state_t hk_pack_pkt_api(uint8_t *buf, tc_tm_pkt *pkt, uint16_t *buf_pointer)
uint32_t cnv32
Definition: tc_tm.h:145