| 1 | /**************************************************************************** |
|---|
| 2 | |
|---|
| 3 | Copyright 2007 Virginia Polytechnic Institute and State University |
|---|
| 4 | |
|---|
| 5 | This file is part of the OSSIE Packetizer. |
|---|
| 6 | |
|---|
| 7 | OSSIE Packetizer 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 2 of the License, or |
|---|
| 10 | (at your option) any later version. |
|---|
| 11 | |
|---|
| 12 | OSSIE Packetizer 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 OSSIE Packetizer; if not, write to the Free Software |
|---|
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | |
|---|
| 21 | ****************************************************************************/ |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | #ifndef PACKETIZERDSP_IMPL_H |
|---|
| 25 | #define PACKETIZERDSP_IMPL_H |
|---|
| 26 | |
|---|
| 27 | #include <stdlib.h> |
|---|
| 28 | #include "PNCodes.h" |
|---|
| 29 | #include "sigproc/SigProc.h" |
|---|
| 30 | |
|---|
| 31 | #define PN_SYNC_CODE_LENGTH 63 |
|---|
| 32 | #define PN_CONTROL_CODE_LENGTH 7 |
|---|
| 33 | #define NUM_CONTROL_CODES 4 |
|---|
| 34 | #define MAX_PACKET_SIZE 4096 |
|---|
| 35 | |
|---|
| 36 | #define DEBUG_LEVEL 0 |
|---|
| 37 | |
|---|
| 38 | #define DEBUG_PRINTF(level, msg) \ |
|---|
| 39 | if (DEBUG_LEVEL > level) \ |
|---|
| 40 | printf(msg); |
|---|
| 41 | |
|---|
| 42 | #define PACKETIZER_DEFINE_EXTRACT_API(X) \ |
|---|
| 43 | bool X( \ |
|---|
| 44 | char* input, \ |
|---|
| 45 | unsigned int input_length, \ |
|---|
| 46 | char* output, \ |
|---|
| 47 | unsigned int output_length, \ |
|---|
| 48 | unsigned int &num_written); |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | extern "C" { |
|---|
| 52 | #include "binary_sequence.h" |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | /// Forward error correction scheme |
|---|
| 56 | enum FECScheme { |
|---|
| 57 | REPEAT_1, ///< Repeat code, rate 1/3 |
|---|
| 58 | RS_1, ///< Reed solomon, rate... |
|---|
| 59 | CONV_1 ///< Convolutional, rate..., K=... |
|---|
| 60 | }; |
|---|
| 61 | |
|---|
| 62 | /// Interleaving scheme |
|---|
| 63 | enum InterleavingScheme { |
|---|
| 64 | INT_1, ///< Interleaving depth... |
|---|
| 65 | INT_2 ///< Interleaving depth... |
|---|
| 66 | }; |
|---|
| 67 | |
|---|
| 68 | /// Cyclic redundancy check scheme |
|---|
| 69 | enum CRCScheme { |
|---|
| 70 | CRC_1, |
|---|
| 71 | CRC_2 |
|---|
| 72 | }; |
|---|
| 73 | |
|---|
| 74 | /// Packet type |
|---|
| 75 | enum PacketType { |
|---|
| 76 | ///\brief raw data packet with 400 bits input |
|---|
| 77 | // pn_sync 63 |
|---|
| 78 | // control 4x7 |
|---|
| 79 | // p_id 7x3 |
|---|
| 80 | // data 400 |
|---|
| 81 | // ------------- |
|---|
| 82 | // total: 512 |
|---|
| 83 | PACKET_RAW_400=0, |
|---|
| 84 | |
|---|
| 85 | ///\brief Simplified packet with low overhead |
|---|
| 86 | PACKET_LOW_OVERHEAD, |
|---|
| 87 | |
|---|
| 88 | ///\brief Fully-descriptive packet |
|---|
| 89 | PACKET_RELIABLE_DATA |
|---|
| 90 | }; |
|---|
| 91 | |
|---|
| 92 | /** \brief Basic packet functionality |
|---|
| 93 | |
|---|
| 94 | */ |
|---|
| 95 | class PacketizerDSP |
|---|
| 96 | { |
|---|
| 97 | public: |
|---|
| 98 | /// Default constructor |
|---|
| 99 | PacketizerDSP(); |
|---|
| 100 | |
|---|
| 101 | /// Destructor |
|---|
| 102 | ~PacketizerDSP(); |
|---|
| 103 | |
|---|
| 104 | ///\todo implement this method; for now assume packet can be configured by |
|---|
| 105 | /// setting properties directly because SCA component wrapper inherits |
|---|
| 106 | /// from the PacketizerDSP class |
|---|
| 107 | void ConfigurePacket( |
|---|
| 108 | PacketType _type, |
|---|
| 109 | FECScheme _fec_inner, |
|---|
| 110 | FECScheme _fec_outer, |
|---|
| 111 | InterleavingScheme _int, |
|---|
| 112 | CRCScheme _crc); |
|---|
| 113 | |
|---|
| 114 | /// Returns the necessary buffer size for processing given the current |
|---|
| 115 | /// packet configuration |
|---|
| 116 | unsigned int RequiredBufferSize(unsigned int input_length); |
|---|
| 117 | |
|---|
| 118 | /// \brief Completely constructs and encodes a packet from raw data bits |
|---|
| 119 | /// |
|---|
| 120 | /// \param[in] input raw un-encoded data bits |
|---|
| 121 | /// \param[in] input_length length of input |
|---|
| 122 | /// \param[in] output output buffer |
|---|
| 123 | /// \param[in,out] output_length number of (maximum) output bits written |
|---|
| 124 | /// \param[in] type type of packet to use |
|---|
| 125 | void CreatePacket( |
|---|
| 126 | PacketType type, |
|---|
| 127 | char* input, |
|---|
| 128 | unsigned int input_length, |
|---|
| 129 | char* output, |
|---|
| 130 | unsigned int output_length, |
|---|
| 131 | unsigned int &num_written); |
|---|
| 132 | |
|---|
| 133 | /// \brief Completely finds and decodes a packet from raw data |
|---|
| 134 | /// |
|---|
| 135 | /// \param[in] input raw input |
|---|
| 136 | /// \param[in] input_length length of input |
|---|
| 137 | /// \param[in] output output buffer |
|---|
| 138 | /// \param[in] output_length number of output bits available in buffer |
|---|
| 139 | /// \param[out] num_written number of samples written to output buffer |
|---|
| 140 | PACKETIZER_DEFINE_EXTRACT_API(ExtractPacket) |
|---|
| 141 | |
|---|
| 142 | /// \brief Assembles a packet from pre-encoded data bits |
|---|
| 143 | /// |
|---|
| 144 | /// \param[in] input buffer of pre-encoded data bits |
|---|
| 145 | /// \param[in] input_length length of input buffer |
|---|
| 146 | /// \param[in] output output buffer |
|---|
| 147 | /// \param[in] output_length number of output bits available in buffer |
|---|
| 148 | /// \param[out] num_written number of samples written to output buffer |
|---|
| 149 | void AssemblePacket( |
|---|
| 150 | char* input, |
|---|
| 151 | unsigned int input_length, |
|---|
| 152 | char* output, |
|---|
| 153 | unsigned int output_length, |
|---|
| 154 | unsigned int &num_written); |
|---|
| 155 | |
|---|
| 156 | /// |
|---|
| 157 | void EncodePacketData( |
|---|
| 158 | char * input, |
|---|
| 159 | unsigned int input_length, |
|---|
| 160 | char * output, |
|---|
| 161 | unsigned int output_length, |
|---|
| 162 | unsigned int &num_written); |
|---|
| 163 | |
|---|
| 164 | /// Finds the packet header by correlating with the P/N sync code |
|---|
| 165 | bool FindHeader( |
|---|
| 166 | char * input, |
|---|
| 167 | unsigned int input_length, |
|---|
| 168 | unsigned int &num_bits_read); |
|---|
| 169 | |
|---|
| 170 | /// \brief Extracts header from raw input |
|---|
| 171 | /// |
|---|
| 172 | /// \param[in] input raw input |
|---|
| 173 | /// \param[in] input_length length of input |
|---|
| 174 | /// \param[in] output output buffer |
|---|
| 175 | /// \param[in] output_length number of output bits available in buffer |
|---|
| 176 | /// \param[out] num_written number of samples written to output buffer |
|---|
| 177 | PACKETIZER_DEFINE_EXTRACT_API(ExtractHeader) |
|---|
| 178 | |
|---|
| 179 | /// Extracts the basic packet type from the header |
|---|
| 180 | bool DecodeHeader( |
|---|
| 181 | char * input, |
|---|
| 182 | unsigned int input_length); |
|---|
| 183 | |
|---|
| 184 | /// \brief Extracts sub header from raw input |
|---|
| 185 | /// |
|---|
| 186 | /// \param[in] input raw input |
|---|
| 187 | /// \param[in] input_length length of input |
|---|
| 188 | /// \param[in] output output buffer |
|---|
| 189 | /// \param[in] output_length number of output bits available in buffer |
|---|
| 190 | /// \param[out] num_written number of samples written to output buffer |
|---|
| 191 | PACKETIZER_DEFINE_EXTRACT_API(ExtractSubheader) |
|---|
| 192 | |
|---|
| 193 | /// |
|---|
| 194 | bool DecodeSubheader( |
|---|
| 195 | char * input, |
|---|
| 196 | unsigned int input_length); |
|---|
| 197 | |
|---|
| 198 | /// Extracts the remainder of the packet's raw data |
|---|
| 199 | PACKETIZER_DEFINE_EXTRACT_API(ExtractPacketData) |
|---|
| 200 | |
|---|
| 201 | /// |
|---|
| 202 | bool DecodePacketData( |
|---|
| 203 | char * input, |
|---|
| 204 | unsigned int input_length, |
|---|
| 205 | char * output, |
|---|
| 206 | unsigned int output_length, |
|---|
| 207 | unsigned int &num_written); |
|---|
| 208 | |
|---|
| 209 | protected: |
|---|
| 210 | PacketType packet_type; ///< packet type identifier |
|---|
| 211 | |
|---|
| 212 | bool dynamic_packet_size; |
|---|
| 213 | unsigned int packet_header_length; |
|---|
| 214 | unsigned int packet_subheader_length; |
|---|
| 215 | unsigned int packet_data_length; |
|---|
| 216 | |
|---|
| 217 | // Physical layer definitions |
|---|
| 218 | char * pnSyncCode; ///< packet synchronization code |
|---|
| 219 | binary_sequence_t * pn_sync_code; ///< packet synchronization code shift register |
|---|
| 220 | |
|---|
| 221 | char * pnControlCode; ///< packet control code |
|---|
| 222 | binary_sequence_t * pn_control_code;///< packet control code shift register |
|---|
| 223 | |
|---|
| 224 | char * pnEOMCode; ///< packet EOM code |
|---|
| 225 | binary_sequence_t * pn_eom_code; ///< packet EOM code shift register |
|---|
| 226 | |
|---|
| 227 | // Media Access Control layer definitions |
|---|
| 228 | FECScheme fec_inner; |
|---|
| 229 | FECScheme fec_outer; |
|---|
| 230 | InterleavingScheme interleaving; |
|---|
| 231 | CRCScheme crc; |
|---|
| 232 | |
|---|
| 233 | // Network layer definitions |
|---|
| 234 | unsigned long src_id; ///< source identifier |
|---|
| 235 | unsigned long dst_id; ///< destination identifier |
|---|
| 236 | |
|---|
| 237 | // Transport layer definitions |
|---|
| 238 | unsigned long packet_id; ///< packet identifier |
|---|
| 239 | unsigned long port_id; ///< port identifier |
|---|
| 240 | |
|---|
| 241 | // Application layer definitions |
|---|
| 242 | // ... |
|---|
| 243 | |
|---|
| 244 | /// Array of control code pointers |
|---|
| 245 | char ** controlBlock; |
|---|
| 246 | |
|---|
| 247 | /// Configures control codes and other data based on packet type |
|---|
| 248 | void ConfigurePacketType( PacketType type ); |
|---|
| 249 | |
|---|
| 250 | // ------ Packetizer definitions ------ |
|---|
| 251 | |
|---|
| 252 | /// Sets pointers in controlBlock to appropriate control codes |
|---|
| 253 | void ConfigureControl( unsigned char id ); |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | // ------ Depacketizer definitions ------ |
|---|
| 257 | |
|---|
| 258 | /// |
|---|
| 259 | PACKETIZER_DEFINE_EXTRACT_API(ExtractPacketDataDynamicLength) |
|---|
| 260 | |
|---|
| 261 | /// |
|---|
| 262 | PACKETIZER_DEFINE_EXTRACT_API(ExtractPacketDataFixedLength) |
|---|
| 263 | |
|---|
| 264 | binary_sequence_t * pn_sync_buffer; ///< |
|---|
| 265 | binary_sequence_t * pn_control_buffer; ///< |
|---|
| 266 | binary_sequence_t * pn_eom_buffer; ///< |
|---|
| 267 | int rxy; ///< cross-correlator output |
|---|
| 268 | |
|---|
| 269 | unsigned int input_index; |
|---|
| 270 | unsigned int output_index; |
|---|
| 271 | |
|---|
| 272 | enum { |
|---|
| 273 | FIND_HEADER=0, |
|---|
| 274 | EXTRACT_HEADER, |
|---|
| 275 | DECODE_HEADER, |
|---|
| 276 | EXTRACT_SUBHEADER, |
|---|
| 277 | DECODE_SUBHEADER, |
|---|
| 278 | EXTRACT_PACKET_DATA, |
|---|
| 279 | DECODE_PACKET_DATA |
|---|
| 280 | } op_mode; |
|---|
| 281 | |
|---|
| 282 | // |
|---|
| 283 | unsigned int num_header_bits_read; |
|---|
| 284 | unsigned int num_control_codes_extracted; |
|---|
| 285 | unsigned int num_control_bits_read; |
|---|
| 286 | unsigned int new_packet_type; |
|---|
| 287 | |
|---|
| 288 | // |
|---|
| 289 | unsigned int num_subheader_bits_read; |
|---|
| 290 | |
|---|
| 291 | // |
|---|
| 292 | unsigned int num_packet_bits_read; |
|---|
| 293 | |
|---|
| 294 | bool invert_bits; |
|---|
| 295 | |
|---|
| 296 | // buffer pointers for signal processing; memory allocated externally |
|---|
| 297 | char *buf1; |
|---|
| 298 | char *buf2; |
|---|
| 299 | unsigned int buf_len; |
|---|
| 300 | void SetBuffer(char *_b1, char *_b2, unsigned int _len); |
|---|
| 301 | |
|---|
| 302 | private: |
|---|
| 303 | /// Disallow copy constructor |
|---|
| 304 | PacketizerDSP(PacketizerDSP&); |
|---|
| 305 | |
|---|
| 306 | }; |
|---|
| 307 | |
|---|
| 308 | #endif |
|---|
| 309 | |
|---|