UoS³ Flight Computer Firmware
 All Data Structures Files Functions Groups Pages
packet_transmission.c
1 #include "../../firmware.h"
2 #include "../../test.h"
3 
4 bool test_packet_transmission_telecommand(void)
5 {
6  uint32_t i;
7  uint16_t spacecraft_id = 0x4242;
8  uint8_t tc_sign_key[16] = { 0x45, 0xDA, 0x45, 0x92, 0xF7, 0x44, 0xD4, 0xEB,
9  0x6C, 0x81, 0x34, 0x3F, 0x9C, 0xB6, 0xE0, 0xCE };
10 
11  packet_telecommand_512 reference_packet;
12  packet_telecommand_512 test_tx_packet;
13  packet_telecommand_512 test_rx_packet;
14 
15  uint8_t transmission_buffer[128]; // 1024b
16 
17  /* Construct sample packet data */
18  reference_packet.spacecraft = spacecraft_id;
19  for(i=0; i<sizeof(reference_packet.data); i++)
20  {
21  reference_packet.data[i] = (uint8_t)(Random(255));
22  }
23 
24  if(TEST_VERBOSE)
25  {
26  Debug_print("Original message\r\n");
27  buffer_print_hex((uint8_t *)&reference_packet, sizeof(reference_packet));
28  }
29 
30  memcpy(&test_tx_packet, &reference_packet, sizeof(test_tx_packet));
31 
32  Packet_telecommand_512_encode(&test_tx_packet, transmission_buffer, spacecraft_id, tc_sign_key, sizeof(tc_sign_key));
33 
34  if(TEST_VERBOSE)
35  {
36  Debug_print("Coded Message\r\n");
37  buffer_print_hex(transmission_buffer, sizeof(transmission_buffer));
38  }
39 
40  /* Add random noise (simulate transmission channel) */
41  #define BIT_FLIPS 10
42  uint32_t bit_index;
43  for(i=0; i<BIT_FLIPS; i++)
44  {
45  bit_index = (uint8_t)(Random(sizeof(transmission_buffer)));
46  transmission_buffer[bit_index >> 3] = (uint8_t)(transmission_buffer[bit_index >> 3] ^ (0x80 >> (bit_index & 0x07)));
47  }
48 
49  /* Attempt decode */
50  if(!Packet_telecommand_512_decode(transmission_buffer, &test_rx_packet, spacecraft_id, tc_sign_key, sizeof(tc_sign_key)))
51  {
52  if(1)
53  {
54  Debug_print("Decode Failed\r\n");
55  Debug_print("reference\r\n");
56  buffer_print_hex((uint8_t *)&reference_packet, sizeof(reference_packet));
57  Debug_print("transmitted\r\n");
58  buffer_print_hex((uint8_t *)&test_rx_packet, sizeof(test_rx_packet));
59  }
60  return false;
61  }
62 
63  /* Compare data with ref packet */
64  if(memcmp(test_rx_packet.data, reference_packet.data, sizeof(reference_packet.data)) == 0)
65  {
66  return true;
67  }
68  else
69  {
70  return false;
71  }
72 }
73 
74 bool test_packet_transmission_telemetry(void)
75 {
76 
77  uint32_t i;
78  uint16_t spacecraft_id = 0x4242;
79  uint8_t tm_sign_key[16] = { 0x45, 0xDA, 0x82, 0xBB, 0xF7, 0x44, 0xD4, 0x98,
80  0x6C, 0xA3, 0x33, 0x3E, 0x9B, 0xB6, 0xEB, 0xCE };
81 
82  packet_telemetry_1024 reference_packet;
83  packet_telemetry_1024 test_tx_packet;
84  packet_telemetry_1024 test_rx_packet;
85 
86  uint8_t transmission_buffer[384]; // 3072b
87 
88  /* Construct sample packet data */
89  reference_packet.spacecraft = spacecraft_id;
90  for(i=0; i<sizeof(reference_packet.data); i++)
91  {
92  reference_packet.data[i] = (uint8_t)(Random(255));
93  }
94 
95  if(TEST_VERBOSE)
96  {
97  Debug_print("Original message\r\n");
98  buffer_print_hex((uint8_t *)&reference_packet, sizeof(reference_packet));
99  }
100 
101  memcpy(&test_tx_packet, &reference_packet, sizeof(test_tx_packet));
102 
103  Packet_telemetry_1024_encode(&test_tx_packet, transmission_buffer, spacecraft_id, tm_sign_key, sizeof(tm_sign_key));
104 
105  if(TEST_VERBOSE)
106  {
107  Debug_print("Coded Message\r\n");
108  buffer_print_hex(transmission_buffer, sizeof(transmission_buffer));
109  }
110 
111  /* Add random noise (simulate transmission channel) */
112  /* TODO: Add back in when we have FEC
113  #define BIT_FLIPS 10
114  uint32_t bit_index;
115  for(i=0; i<BIT_FLIPS; i++)
116  {
117  bit_index = (uint8_t)(Random(sizeof(transmission_buffer)));
118  transmission_buffer[bit_index >> 3] = (uint8_t)(transmission_buffer[bit_index >> 3] ^ (0x80 >> (bit_index & 0x07)));
119  }
120  */
121 
122  /* Attempt decode */
123  if(!Packet_telemetry_1024_decode(transmission_buffer, &test_rx_packet, spacecraft_id, tm_sign_key, sizeof(tm_sign_key)))
124  {
125  if(TEST_VERBOSE)
126  {
127  Debug_print("Decode Failed\r\n");
128  Debug_print("reference\r\n");
129  buffer_print_hex((uint8_t *)&reference_packet, sizeof(reference_packet));
130  Debug_print("transmitted\r\n");
131  buffer_print_hex((uint8_t *)&test_rx_packet, sizeof(test_rx_packet));
132  }
133  return false;
134  }
135 
136  /* Compare data with ref packet */
137  if(memcmp(test_rx_packet.data, reference_packet.data, sizeof(reference_packet.data)) == 0)
138  {
139  return true;
140  }
141  else
142  {
143  return false;
144  }
145 }
uint32_t Random(uint32_t max)
Definition: random.c:21
void Packet_telemetry_1024_encode(packet_telemetry_1024 *input_packet, uint8_t *output_buffer, const uint16_t origin, const uint8_t *key, uint32_t key_length)
Definition: packet.c:104
void Debug_print(char *fmt,...)
Definition: debug.c:16
void buffer_print_hex(uint8_t *buffer, uint32_t length)
Definition: test.c:53