/* VectorNav HSI Calibration Library v1.0.0.2
 *
 * Copyright (c) 2018 VectorNav Technologies, LLC
 *
 * This source code is proprietary to and copyrighted by VectorNav Technologies, LLC and is
 * available solely under license from VectorNav. All rights reserved. If you do not have a
 * license to use this source code from VectorNav, any use hereof is strictly prohibited by
 * U.S. law and other applicable law. This source code is restricted for use solely with the
 * products of VectorNav and as otherwise set forth in any agreement with VectorNav. Any
 * disclosure of this source code to the public or to any third party is also prohibited.
 */
#include "vnerrdet.h"

uint8_t VnChecksum8_compute(char const *data, size_t length)
{
  uint8_t xorVal = 0;
  size_t i;

  for (i = 0; i < length; i++)
  {
    xorVal ^= data[i];
  }

  return xorVal;
}

uint16_t VnCrc16_compute(char const *data, size_t length)
{
  size_t i;
  uint16_t crc = 0;

  for (i = 0; i < length; i++)
  {
    crc = (uint16_t) (crc >> 8) | (crc << 8);

    crc ^= (uint8_t) data[i];
    crc ^= (uint16_t) (((uint8_t) (crc & 0xFF)) >> 4);
    crc ^= (uint16_t) ((crc << 8) << 4);
    crc ^= (uint16_t) (((crc & 0xFF) << 4) << 1);
  }

  return crc;
}
