| | 130 | // |
| | 131 | // Test extracting a packet with bit inversion |
| | 132 | // |
| | 133 | void test_ExtractPacketHeader_02() { |
| | 134 | // generate input buffer |
| | 135 | char * input; |
| | 136 | input = (char*) calloc( 400, sizeof(char) ); |
| | 137 | input[0] = 1; |
| | 138 | input[1] = 0; |
| | 139 | input[2] = 1; |
| | 140 | |
| | 141 | // generate packet buffer |
| | 142 | char * packet; |
| | 143 | packet = (char*) calloc( 1024, sizeof(char) ); |
| | 144 | memset( packet, 0x01, 1024*sizeof(char) ); |
| | 145 | |
| | 146 | // generate output buffer |
| | 147 | char * output; |
| | 148 | output = (char*) calloc( 1024, sizeof(char) ); |
| | 149 | memset( output, 0x01, 1024*sizeof(char) ); |
| | 150 | |
| | 151 | bool found_header; |
| | 152 | bool packet_type_extracted; |
| | 153 | bool packet_subheader_extracted; |
| | 154 | bool packet_data_extracted; |
| | 155 | unsigned int n; |
| | 156 | |
| | 157 | // assemble packet |
| | 158 | unsigned int packet_length(1024); |
| | 159 | AssemblePacket( input, 400, packet, packet_length, PACKET_RAW_400 ); |
| | 160 | |
| | 161 | // flip all the bits |
| | 162 | for (unsigned int i=0; i<1024; i++) |
| | 163 | packet[i] = 1-packet[i]; |
| | 164 | |
| | 165 | // try to extract packet header |
| | 166 | unsigned int N(0); // total bits read from input |
| | 167 | found_header = ExtractPacketHeader( packet+N, 1024, n ); |
| | 168 | N += n; |
| | 169 | TS_ASSERT_EQUALS(found_header, true); // should have found header |
| | 170 | TS_ASSERT_EQUALS(n, PN_SYNC_CODE_LENGTH); // should have only read 63 values |
| | 171 | TS_ASSERT_EQUALS(rxy, -PN_SYNC_CODE_LENGTH);// correlator should be negative |
| | 172 | |
| | 173 | // try to extract packet type |
| | 174 | packet_type_extracted = ExtractPacketType( packet+N, 500, n ); |
| | 175 | N += n; |
| | 176 | TS_ASSERT_EQUALS(packet_type_extracted, true); |
| | 177 | TS_ASSERT_EQUALS(n, NUM_CONTROL_CODES*PN_CONTROL_CODE_LENGTH); // should have read control codes |
| | 178 | TS_ASSERT_EQUALS( N, PN_SYNC_CODE_LENGTH + NUM_CONTROL_CODES*PN_CONTROL_CODE_LENGTH); |
| | 179 | |
| | 180 | TS_ASSERT_EQUALS(new_packet_type, 0); |
| | 181 | |
| | 182 | // try to extract the subheader |
| | 183 | packet_subheader_extracted = ExtractPacketSubheader( packet+N, 500, n ); |
| | 184 | N += n; |
| | 185 | TS_ASSERT_EQUALS(packet_subheader_extracted, true); |
| | 186 | TS_ASSERT_EQUALS(n, 21); |
| | 187 | TS_ASSERT_EQUALS(N, 112); |
| | 188 | |
| | 189 | // try to extract packet data |
| | 190 | n = 1024; |
| | 191 | packet_data_extracted = ExtractPacketData( packet+N, 400, output, n ); |
| | 192 | N += n; |
| | 193 | TS_ASSERT_EQUALS( packet_data_extracted, true ); |
| | 194 | TS_ASSERT_EQUALS( n, 400 ); |
| | 195 | |
| | 196 | // output data should be the same, even though the packet was decoded with bit inversions; |
| | 197 | // the P/N sync correlator should have detected this and corrected for it appropriately |
| | 198 | TS_ASSERT_SAME_DATA( input, output, 400 ); |
| | 199 | |
| | 200 | free(input); |
| | 201 | free(packet); |
| | 202 | free(output); |
| | 203 | } |
| | 204 | |