Mercurial > public > ostc_companion
annotate crcmodel.h @ 12:ac837fe1d590 default tip
Switch implementation for reqex class and added RFCOMM as label for Bluetooth based connection by Linux
| author | Ideenmodellierer |
|---|---|
| date | Mon, 12 Jan 2026 13:58:41 +0000 |
| parents | 6fba58c4964b |
| children |
| rev | line source |
|---|---|
| 5 | 1 /////////////////////////////////////////////////////////////////////////////// |
| 2 /// -*- coding: UTF-8 -*- | |
| 3 /// | |
| 4 /// \file Discovery/Inc/crcmodel.h | |
| 5 /// \brief Rocksoft CRC Model Algorithm | |
| 6 /// \author Ross Williams (ross@guest.adelaide.edu.au.) and Heinrichs Weikamp | |
| 7 /// \date 3 June 1993 | |
| 8 /// | |
| 9 /// \details | |
| 10 /// | |
| 11 /// This is the header (.h) file for the reference | |
| 12 /// implementation of the Rocksoft^tm Model CRC Algorithm. For more | |
| 13 /// information on the Rocksoft^tm Model CRC Algorithm, see the document | |
| 14 /// titled "A Painless Guide to CRC Error Detection Algorithms" by Ross | |
| 15 /// Williams (ross@guest.adelaide.edu.au.). This document is likely to be in | |
| 16 /// "ftp.adelaide.edu.au/pub/rocksoft". | |
| 17 /// | |
| 18 /// Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia. | |
| 19 /// | |
| 20 /// $Id$ | |
| 21 /////////////////////////////////////////////////////////////////////////////// | |
| 22 /// \par Copyright (c) 2014-2018 Heinrichs Weikamp gmbh | |
| 23 /// | |
| 24 /// This program is free software: you can redistribute it and/or modify | |
| 25 /// it under the terms of the GNU General Public License as published by | |
| 26 /// the Free Software Foundation, either version 3 of the License, or | |
| 27 /// (at your option) any later version. | |
| 28 /// | |
| 29 /// This program is distributed in the hope that it will be useful, | |
| 30 /// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 31 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 32 /// GNU General Public License for more details. | |
| 33 /// | |
| 34 /// You should have received a copy of the GNU General Public License | |
| 35 /// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 36 ////////////////////////////////////////////////////////////////////////////// | |
| 37 | |
| 38 /******************************************************************************/ | |
| 39 /* Start of crcmodel.h */ | |
| 40 /******************************************************************************/ | |
| 41 /* */ | |
| 42 /* Author : Ross Williams (ross@guest.adelaide.edu.au.). */ | |
| 43 /* Date : 3 June 1993. */ | |
| 44 /* Status : Public domain. */ | |
| 45 /* */ | |
| 46 /* Description : This is the header (.h) file for the reference */ | |
| 47 /* implementation of the Rocksoft^tm Model CRC Algorithm. For more */ | |
| 48 /* information on the Rocksoft^tm Model CRC Algorithm, see the document */ | |
| 49 /* titled "A Painless Guide to CRC Error Detection Algorithms" by Ross */ | |
| 50 /* Williams (ross@guest.adelaide.edu.au.). This document is likely to be in */ | |
| 51 /* "ftp.adelaide.edu.au/pub/rocksoft". */ | |
| 52 /* */ | |
| 53 /* Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia. */ | |
| 54 /* */ | |
| 55 /******************************************************************************/ | |
| 56 /* */ | |
| 57 /* How to Use This Package */ | |
| 58 /* ----------------------- */ | |
| 59 /* Step 1: Declare a variable of type cm_t. Declare another variable */ | |
| 60 /* (p_cm say) of type p_cm_t and initialize it to point to the first */ | |
| 61 /* variable (e.g. p_cm_t p_cm = &cm_t). */ | |
| 62 /* */ | |
| 63 /* Step 2: Assign values to the parameter fields of the structure. */ | |
| 64 /* If you don't know what to assign, see the document cited earlier. */ | |
| 65 /* For example: */ | |
| 66 /* p_cm->cm_width = 16; */ | |
| 67 /* p_cm->cm_poly = 0x8005L; */ | |
| 68 /* p_cm->cm_init = 0L; */ | |
| 69 /* p_cm->cm_refin = TRUE; */ | |
| 70 /* p_cm->cm_refot = TRUE; */ | |
| 71 /* p_cm->cm_xorot = 0L; */ | |
| 72 /* Note: Poly is specified without its top bit (18005 becomes 8005). */ | |
| 73 /* Note: Width is one bit less than the raw poly width. */ | |
| 74 /* */ | |
| 75 /* Step 3: Initialize the instance with a call cm_ini(p_cm); */ | |
| 76 /* */ | |
| 77 /* Step 4: Process zero or more message bytes by placing zero or more */ | |
| 78 /* successive calls to cm_nxt. Example: cm_nxt(p_cm,ch); */ | |
| 79 /* */ | |
| 80 /* Step 5: Extract the CRC value at any time by calling crc = cm_crc(p_cm); */ | |
| 81 /* If the CRC is a 16-bit value, it will be in the bottom 16 bits. */ | |
| 82 /* */ | |
| 83 /******************************************************************************/ | |
| 84 /* */ | |
| 85 /* Design Notes */ | |
| 86 /* ------------ */ | |
| 87 /* PORTABILITY: This package has been coded very conservatively so that */ | |
| 88 /* it will run on as many machines as possible. For example, all external */ | |
| 89 /* identifiers have been restricted to 6 characters and all internal ones to */ | |
| 90 /* 8 characters. The prefix cm (for Crc Model) is used as an attempt to avoid */ | |
| 91 /* namespace collisions. This package is endian independent. */ | |
| 92 /* */ | |
| 93 /* EFFICIENCY: This package (and its interface) is not designed for */ | |
| 94 /* speed. The purpose of this package is to act as a well-defined reference */ | |
| 95 /* model for the specification of CRC algorithms. If you want speed, cook up */ | |
| 96 /* a specific table-driven implementation as described in the document cited */ | |
| 97 /* above. This package is designed for validation only; if you have found or */ | |
| 98 /* implemented a CRC algorithm and wish to describe it as a set of parameters */ | |
| 99 /* to the Rocksoft^tm Model CRC Algorithm, your CRC algorithm implementation */ | |
| 100 /* should behave identically to this package under those parameters. */ | |
| 101 /* */ | |
| 102 /******************************************************************************/ | |
| 103 | |
| 104 #ifndef CRC_MODEL_H | |
| 105 #define CRC_MODEL_H | |
| 106 | |
| 107 #ifdef __cplusplus | |
| 108 extern "C" { | |
| 109 #endif | |
| 110 /******************************************************************************/ | |
| 111 | |
| 112 /* The following definitions are extracted from my style header file which */ | |
| 113 /* would be cumbersome to distribute with this package. The DONE_STYLE is the */ | |
| 114 /* idempotence symbol used in my style header file. */ | |
| 115 | |
| 116 #ifndef DONE_STYLE | |
| 117 | |
| 118 #include <stdbool.h> | |
| 119 | |
|
11
6fba58c4964b
Minor changes done by automatic style checker
Ideenmodellierer
parents:
5
diff
changeset
|
120 typedef unsigned long ulong; |
|
6fba58c4964b
Minor changes done by automatic style checker
Ideenmodellierer
parents:
5
diff
changeset
|
121 typedef unsigned char *p_ubyte_; |
| 5 | 122 |
| 123 #ifndef TRUE | |
| 124 #define FALSE 0 | |
|
11
6fba58c4964b
Minor changes done by automatic style checker
Ideenmodellierer
parents:
5
diff
changeset
|
125 #define TRUE 1 |
| 5 | 126 #endif |
| 127 | |
| 128 /* Change to the second definition if you don't have prototypes. */ | |
| 129 #define P_(A) A | |
| 130 /* #define P_(A) () */ | |
| 131 | |
| 132 /* Uncomment this definition if you don't have void. */ | |
| 133 /* typedef int void; */ | |
| 134 | |
| 135 #endif | |
| 136 | |
| 137 /******************************************************************************/ | |
| 138 | |
| 139 /* CRC Model Abstract Type */ | |
| 140 /* ----------------------- */ | |
| 141 /* The following type stores the context of an executing instance of the */ | |
| 142 /* model algorithm. Most of the fields are model parameters which must be */ | |
| 143 /* set before the first initializing call to cm_ini. */ | |
| 144 typedef struct | |
|
11
6fba58c4964b
Minor changes done by automatic style checker
Ideenmodellierer
parents:
5
diff
changeset
|
145 { |
|
6fba58c4964b
Minor changes done by automatic style checker
Ideenmodellierer
parents:
5
diff
changeset
|
146 int cm_width; /* Parameter: Width in bits [8,32]. */ |
|
6fba58c4964b
Minor changes done by automatic style checker
Ideenmodellierer
parents:
5
diff
changeset
|
147 ulong cm_poly; /* Parameter: The algorithm's polynomial. */ |
|
6fba58c4964b
Minor changes done by automatic style checker
Ideenmodellierer
parents:
5
diff
changeset
|
148 ulong cm_init; /* Parameter: Initial register value. */ |
|
6fba58c4964b
Minor changes done by automatic style checker
Ideenmodellierer
parents:
5
diff
changeset
|
149 bool cm_refin; /* Parameter: Reflect input bytes? */ |
|
6fba58c4964b
Minor changes done by automatic style checker
Ideenmodellierer
parents:
5
diff
changeset
|
150 bool cm_refot; /* Parameter: Reflect output CRC? */ |
|
6fba58c4964b
Minor changes done by automatic style checker
Ideenmodellierer
parents:
5
diff
changeset
|
151 ulong cm_xorot; /* Parameter: XOR this to output CRC. */ |
| 5 | 152 |
|
11
6fba58c4964b
Minor changes done by automatic style checker
Ideenmodellierer
parents:
5
diff
changeset
|
153 ulong cm_reg; /* Context: Context during execution. */ |
|
6fba58c4964b
Minor changes done by automatic style checker
Ideenmodellierer
parents:
5
diff
changeset
|
154 } cm_t; |
| 5 | 155 typedef cm_t *p_cm_t; |
| 156 | |
| 157 /******************************************************************************/ | |
| 158 | |
| 159 /* Functions That Implement The Model */ | |
| 160 /* ---------------------------------- */ | |
| 161 /* The following functions animate the cm_t abstraction. */ | |
| 162 | |
| 163 void cm_ini P_((p_cm_t p_cm)); | |
| 164 /* Initializes the argument CRC model instance. */ | |
| 165 /* All parameter fields must be set before calling this. */ | |
| 166 | |
|
11
6fba58c4964b
Minor changes done by automatic style checker
Ideenmodellierer
parents:
5
diff
changeset
|
167 void cm_nxt P_((p_cm_t p_cm, int ch)); |
| 5 | 168 /* Processes a single message byte [0,255]. */ |
| 169 | |
|
11
6fba58c4964b
Minor changes done by automatic style checker
Ideenmodellierer
parents:
5
diff
changeset
|
170 void cm_blk P_((p_cm_t p_cm, p_ubyte_ blk_adr, ulong blk_len)); |
| 5 | 171 /* Processes a block of message bytes. */ |
| 172 | |
| 173 ulong cm_crc P_((p_cm_t p_cm)); | |
| 174 /* Returns the CRC value for the message bytes processed so far. */ | |
| 175 | |
| 176 /******************************************************************************/ | |
| 177 | |
| 178 /* Functions For Table Calculation */ | |
| 179 /* ------------------------------- */ | |
| 180 /* The following function can be used to calculate a CRC lookup table. */ | |
| 181 /* It can also be used at run-time to create or check static tables. */ | |
| 182 | |
|
11
6fba58c4964b
Minor changes done by automatic style checker
Ideenmodellierer
parents:
5
diff
changeset
|
183 ulong cm_tab P_((p_cm_t p_cm, int index)); |
| 5 | 184 /* Returns the i'th entry for the lookup table for the specified algorithm. */ |
| 185 /* The function examines the fields cm_width, cm_poly, cm_refin, and the */ | |
| 186 /* argument table index in the range [0,255] and returns the table entry in */ | |
| 187 /* the bottom cm_width bytes of the return value. */ | |
| 188 | |
| 189 /******************************************************************************/ | |
| 190 | |
| 191 #ifdef __cplusplus | |
| 192 } | |
| 193 #endif | |
| 194 #endif // CRC_MODEL_H |
