Artemis Teensy Flight Software
The software on the Teensy in the Artemis cubesat.
crclib.h
1 #ifndef _CRCLIB_H
2 #define _CRCLIB_H
3 
4 #include <cstdint>
5 #include <map>
6 #include <fstream>
7 using std::ifstream;
8 #include <string>
9 using std::string;
10 #include <vector>
11 using std::vector;
12 
13 #include "support/cosmos-errno.h"
14 #include "math/bytelib.h"
15 
16 
18 #define CRC16CCITT 0x1021
19 #define CRC16CCITTMSB 0x1021
20 #define CRC16CCITTMSBINIT 0xffff
22 #define CRC16CCITTR 0x8408
23 #define CRC16CCITTLSB 0x8408
24 #define CRC16CCITTLSBINIT 0x0000
26 #define CRC16CCITTRR 0x8810
27 
28 uint16_t calc_crc16ccitt(uint8_t *buf, int size, bool lsb=true);
29 uint16_t calc_crc16(uint8_t *buf, uint16_t size, uint16_t poly=CRC16CCITTMSB, uint16_t crc=CRC16CCITTMSBINIT, uint16_t xorout=0x0, bool lsbfirst=false);
30 uint16_t calc_crc16(vector<uint8_t> buf, uint16_t poly=CRC16CCITTMSB, uint16_t crc=CRC16CCITTMSBINIT, uint16_t xorout=0x0, bool lsbfirst=false);
31 uint16_t calc_crc16ccitt_lsb(string buf, uint16_t initialcrc=CRC16CCITTLSBINIT, uint16_t skip=0);
32 uint16_t calc_crc16ccitt_lsb(vector<uint8_t> &buf, uint16_t initialcrc=CRC16CCITTLSBINIT, uint16_t skip=0);
33 uint16_t calc_crc16ccitt_lsb(uint8_t *buf, uint16_t size, uint16_t initialcrc=CRC16CCITTLSBINIT);
34 uint16_t calc_crc16_lsb(uint8_t *buf, uint16_t size, uint16_t poly=CRC16CCITTLSB, uint16_t crc=CRC16CCITTLSBINIT, uint16_t xorout=0x0);
35 uint16_t calc_crc16_lsb(vector<uint8_t> &buf, uint16_t poly=CRC16CCITTLSB, uint16_t crc=CRC16CCITTLSBINIT, uint16_t xorout=0x0, uint16_t skip=0);
36 uint16_t calc_crc16ccitt_msb(string buf, uint16_t initialcrc=CRC16CCITTMSBINIT, uint16_t skip=0);
37 uint16_t calc_crc16ccitt_msb(vector<uint8_t> &buf, uint16_t initialcrc=CRC16CCITTMSBINIT, uint16_t skip=0);
38 uint16_t calc_crc16ccitt_msb(uint8_t *buf, uint16_t size, uint16_t initialcrc=CRC16CCITTMSBINIT);
39 uint16_t calc_crc16_msb(uint8_t *buf, uint16_t size, uint16_t poly=CRC16CCITTMSB, uint16_t crc=CRC16CCITTMSBINIT, uint16_t xorout=0x0);
40 uint16_t calc_crc16_msb(vector<uint8_t> &buf, uint16_t poly=CRC16CCITTMSB, uint16_t crc=CRC16CCITTMSBINIT, uint16_t xorout=0x0, uint16_t skip=0);
41 
42 class CRC16
43 {
44 
45 public:
46  uint16_t lookup[256];
47  struct crcset
48  {
49  bool lsbfirst;
50  uint16_t polynomial;
51  uint16_t initialcrc;
52  uint16_t xorout;
53  uint16_t test;
54  };
55 
56  std::map<string, crcset> types;
57 // CRC16(uint16_t polynomial=0x1021, uint16_t initial=0xffff, uint16_t xorout=0x0, bool lsbfirst=false);
58  CRC16();
59  uint16_t set(string type);
60  uint16_t set(uint16_t polynomial=0x1021, uint16_t initialcrc=0xffff, uint16_t xorout=0x0, bool lsbfirst=false);
61  uint16_t calc(vector<uint8_t> message);
62  uint16_t calc(vector<uint8_t> message, uint16_t size);
63  uint16_t calc(string message, uint16_t size);
64  uint16_t calc(string message);
65  uint16_t calc(uint8_t *message, uint16_t size);
66  int32_t calc_file(string file_path);
67 
68 private:
69  string type = "ccitt-false";
70  uint16_t test;
71  uint16_t initial;
72  uint16_t polynomial;
73  uint16_t xorout;
74  bool lsbfirst = true;
75 };
76 
77 #endif
Definition: crclib.h:43
CRC16()
Calculate CRC-16.
Definition: crclib.cpp:8
COSMOS Error Codes.
Definition: crclib.h:48