cip.c 8.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
// Copyright  2011 Moritz Hanke

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#include "bit_macros.h"
#include "cip.h"

// decode Packet Primary Header
void * decode_pph(void * buffer, struct cip * cip);

// decode Secondary Header
void * decode_sh(void * buffer, struct cip * cip);

// decode Source Data Field
void * decode_sdf(void * buffer, struct cip * cip);

// decode Preprocessor Parameters
void * decode_preprocessor(void * buffer, struct cip * cip);

// decode Entopy Coder Parameters
void * decode_entropy_coder(void * buffer, struct cip * cip);

// decode Instrument Configuration
void * decode_instrument_configuration(void * buffer, struct cip * cip);

void * decode_cip(void * buffer, struct cip * cip) {

   void * pdf; // Packet Data Field
   void * sdf; // Source Data Field
   void * cds; // Coded data sets

   pdf = decode_pph(buffer, cip);

   if (cip->header.secondary_header_available)
      sdf = decode_sh(pdf, cip);
   else
      sdf = pdf;

   cds = decode_sdf(sdf, cip);

   if ((char*)cds > (char*)pdf + cip->header.data_length + 1) {

      fprintf(stderr, "Packet Data Field is bigger than specified in CIP Packet Primary Header\n");
      exit(EXIT_FAILURE);
   }

   return (char*)pdf + cip->header.data_length + 1;
}

// decode Primary Header
void * decode_pph(void * buffer, struct cip * cip) {

   char * cip_buf = buffer;

   // Packet Version Number

   cip->header.packet_version_number = (cip_buf[0] >> 5) & B3 ;

   if (cip->header.packet_version_number != 0) {

      fprintf(stderr, "Undefined Packet Version Number\n");
      exit(EXIT_FAILURE);
   }

   // Packet Identification
   {
      // Packet Type

      cip->header.type = ((cip_buf[0] >> 4) & B1)?TELECOMMAND:TELEMETRY;

      // is a Secondary Header available

      cip->header.secondary_header_available = (cip_buf[0] >> 3) & B1;

      // Application Process Identifer

      cip->header.apid = ((unsigned)(cip_buf[0] & B3) << 8) | cip_buf[1];
   }

   // Packet Sequence Control
   {
      // Sequence Flags

      cip->header.sequence_flag = (cip_buf[2] >> 6) & B2;

      // Packet Sequence Count or Packet Name

      assert(sizeof(unsigned) > 2);

      cip->header.sequence_count = ((unsigned)(cip_buf[2] & B6) << 8) |
                                   cip_buf[3];
   }

   // Packet Data Length

   cip->header.data_length = ((unsigned)cip_buf[4] << 8) | cip_buf[5];

   return (char*)cip + 6; // the Packet Primary Header has a size of 48
                          // bits 
}

void * decode_sh(void * buffer, struct cip * cip) {

   // The Secondary Header contains information like time and position
   // or attitude of a spacecraft.
   // The handling of a Secondary Header is currently not implemented.

   fprintf(stderr, "Compression Identification Packet contains a Secondary Header\n");
   exit(EXIT_FAILURE);

   return buffer;
}

void * decode_sdf(void * buffer, struct cip * cip) {

   char * sdf = buffer;

   // Grouping Data Length (number of packets containing compressed data)

   cip->sdf.grouping_data_length = (((unsigned)(sdf[0] & B4) << 8) |
                                    sdf[1]) + 1;

   // check whether a compression is used for the current group

   if (sdf[2] > 1) {

      fprintf(stderr, "Undefined compression technique\n");
      exit(EXIT_FAILURE);
   }

   cip->sdf.compression_technique = sdf[2];

   // Reference Sample Interval

   cip->sdf.reference_sample_interval = sdf[3]+1;


   if (cip->sdf.compression_technique == LOSSLESS) {

      // decode Preprocessor Parameters
      sdf = decode_preprocessor(sdf, cip);
   }

   // decode Entropy Coder Parameters
   sdf = decode_entropy_coder(sdf, cip);

   // decode Instrument Configuration
   sdf = decode_instrument_configuration(sdf, cip);

   return sdf;
}

void * decode_preprocessor(void * buffer, struct cip * cip) {

   char * sdf = buffer;

   unsigned header;

   // check for correct header
   header = (sdf[0] >> 6) & B2;
   if (header != 0) {

      fprintf(stderr, "Wrong header for preprocessor field\n");
      exit(EXIT_FAILURE);
   }

   // get preprocessor presents
   cip->sdf.preprocessor_config.preprocessor_present = (sdf[0] >> 5) & B1;

   // if a preprocessor is present
   if (cip->sdf.preprocessor_config.preprocessor_present) {

      // Predictor type
      switch ((sdf[0] >> 2) & B3) {

         case (0):
            cip->sdf.preprocessor_config.predictor_type = BYPASS_P;
            break;
         case (1):
            cip->sdf.preprocessor_config.predictor_type = UNIT_DELAY_P;
            break;
         case (7):
            cip->sdf.preprocessor_config.predictor_type = APP_SPECIFIC_P;
            break;
         default:
            fprintf(stderr, "Invalid predictor type\n");
            exit(EXIT_FAILURE);
      };

      // Mapper type
      switch (sdf[0] & B2) {

         case (0):
            cip->sdf.preprocessor_config.mapper_type = STANDARD_M;
            break;
         case (3):
            cip->sdf.preprocessor_config.mapper_type = APP_SPECIFIC_M;
            break;
         default:
            fprintf(stderr, "Invalid mapper type\n");
            exit(EXIT_FAILURE);
      };
   }

   // Block size (J)
   switch ((sdf[1] >> 6) & B2) {

      case (0):
         cip->sdf.preprocessor_config.block_size = 8;
         break;
      case (1):
         cip->sdf.preprocessor_config.block_size = 16;
         break;
      case(3):
         fprintf(stderr, "Missing implementation for application-specific block size\n");
         exit(EXIT_FAILURE);
      default:
         fprintf(stderr, "Invalid block size\n");
         exit(EXIT_FAILURE);
   };

   // Data sense
   cip->sdf.preprocessor_config.data_sense = (sdf[1] >> 5) & 1;

   // check whether preprocessor presents and data sense match
   if (!cip->sdf.preprocessor_config.preprocessor_present &&
       cip->sdf.preprocessor_config.data_sense != POSITIVE) {

      fprintf(stderr, "If no preprocessor is present, the data sense has to be positive.\n");
      exit(EXIT_FAILURE);
   }

   // Input data sample resolution
   cip->sdf.preprocessor_config.resolution = (sdf[1] & B5) + 1;

   return (char*)buffer + 2; // the preprocessor field has a size of 2 byte
}

void * decode_entropy_coder(void * buffer, struct cip * cip) {

   char * sdf = buffer;

   unsigned header;

   // check for correct header
   header = (sdf[0] >> 6) & B2;
   if (header != 1) {

      fprintf(stderr, "Wrong header for entropy coder field\n");
      exit(EXIT_FAILURE);
   }

   // Entropy Coder option
   switch ((sdf[0] >> 4) & B2) {

      case (1):
         if (cip->sdf.preprocessor_config.resolution > 8) {

            fprintf(stderr, "Entropy coder option does not match input data sample resolution\n");
            exit(EXIT_FAILURE);
         }
         cip->sdf.entropy_coder_config.option = OPTION_S;
         break;
      case (2):
         if ((cip->sdf.preprocessor_config.resolution <= 8) ||
             (cip->sdf.preprocessor_config.resolution > 16)) {

            fprintf(stderr, "Entropy coder option does not match input data sample resolution\n");
            exit(EXIT_FAILURE);
         }
         cip->sdf.entropy_coder_config.option = OPTION_M;
         break;
      case (3):
         if ((cip->sdf.preprocessor_config.resolution <= 16) ||
             (cip->sdf.preprocessor_config.resolution > 32)) {

            fprintf(stderr, "Entropy coder option does not match input data sample resolution\n");
            exit(EXIT_FAILURE);
         }
         cip->sdf.entropy_coder_config.option = OPTION_L;
         break;
      default:
         fprintf(stderr, "Invalid entropy coder option\n");
         exit(EXIT_FAILURE);
   };

   assert(sizeof(unsigned) > 2);

   // Number of coded data sets per packet
   cip->sdf.entropy_coder_config.num_cds_per_packet =
      (((unsigned)(sdf[0] & B4) << 8) | sdf[1]) + 1;

   return (char*)buffer + 2; // entropy coder field has a size of 2 byte
}

void * decode_instrument_configuration(void * buffer, struct cip * cip) {

   // Instrument Configuration is mission specific.
   // This implementation assumes that there is no.
   // In an one is added later, the first 2 bits of buffer have to 10.

   return buffer;
}