#include <stdio.h>
#include <stdint.h>
#include <aspoutil.h>

#include "feig.h"
LOG_BUFFER(200)
/*
  All section and page referenses are for H20800-18e-ID-B.pdf,
  ask a Feig represenative for the file.
*/

int main(int argc, char *argv[]) {
  uint16_t crc;
  int ix;

  /* test data from p.167 */
  uint8_t t1[] = {
    0x1e, 0xff, 0xb0, 0x24, 0x01, 0xe0, 0x07, 0x00,
    0x00, 0x01, 0x47, 0x67, 0x7e, 0x00, 0x03, 0x04,
    0x04, 0x03, 0x02, 0x01,
    0x14, 0x13, 0x12, 0x11,
    0x24, 0x23, 0x22, 0x21,
    0x7c, 0x34
  };
  uint8_t t2[] = {
    0x17, 0x00, 0xb0, 0x00, 0x03, 0x04, 0x00,
    0x04, 0x03, 0x02, 0x01, 0x00,
    0x14, 0x13, 0x12, 0x11, 0x00,
    0x24, 0x23, 0x22, 0x21,
    0xb4, 0x5b,
  };
  uint8_t t3[] = {
    0x2a, 0xff, 0xb0, 0x24, 0x01, 0x60, 0x05, 0x00,
    0x00, 0x02, 0x11, 0x25, 0x04, 0x03, 0x03, 0x08,
    0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
    0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11,
    0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21,
    0xe6, 0x25,
  };
  uint8_t t4[] = {
    0x23, 0x00, 0xb0, 0x00, 0x03, 0x08, 0x00,
    0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
    0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x00,
    0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21,
    0x99, 0x65,
  };

  (void) argc;
  (void) argv;

  crc = FEIG_CRC_PRESET;
  for (ix = 0; ix < (int) sizeof(t1); ++ix) {
    if (ix && ix%5 == 0) printf("\n");
    printf(" %04x", crc);
    crc = feig_crc_seed(t1+ix, 1, crc);
  }
  printf("\n%04x ", crc);

  crc = feig_crc(t1, sizeof(t1));
  printf("%04x\n\n", crc);

  printf("%04x ",  feig_crc(t2, sizeof(t2)-2));
  printf("%04x\n", feig_crc(t2, sizeof(t2)));

  printf("%04x ",  feig_crc(t3, sizeof(t3)-2));
  printf("%04x\n", feig_crc(t3, sizeof(t3)));

  printf("%04x ",  feig_crc(t4, sizeof(t4)-2));
  printf("%04x\n", feig_crc(t4, sizeof(t4)));

  return 0;
}
