00001 /* 00002 * Copyright (c) 2008 SCHUNK GmbH & Co. KG 00003 * Copyright (c) 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA) 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 //====================================================================== 00046 //====================================================================== 00047 00048 #ifndef CRC_h_ 00049 #define CRC_h_ 00050 00051 #include "sdhlibrary_settings.h" 00052 00053 //---------------------------------------------------------------------- 00054 // System Includes - include with <> 00055 //---------------------------------------------------------------------- 00056 00057 00058 //---------------------------------------------------------------------- 00059 // Project Includes - include with "" 00060 //---------------------------------------------------------------------- 00061 00062 #include "basisdef.h" 00063 00064 //---------------------------------------------------------------------- 00065 // Defines, enums, unions, structs 00066 //---------------------------------------------------------------------- 00067 00068 NAMESPACE_SDH_START 00069 00070 00071 typedef UInt16 tCRCValue; 00072 00073 //---------------------------------------------------------------------- 00074 // Global variables (declarations) 00075 //---------------------------------------------------------------------- 00076 00077 00078 //---------------------------------------------------------------------- 00079 // External functions (function declarations) 00080 //---------------------------------------------------------------------- 00081 00082 00083 //---------------------------------------------------------------------- 00084 // Function prototypes (function declarations) 00085 //---------------------------------------------------------------------- 00086 00087 00088 //---------------------------------------------------------------------- 00089 // Class declarations 00090 //---------------------------------------------------------------------- 00091 00099 class VCC_EXPORT cCRC 00100 { 00101 protected: 00103 tCRCValue current_crc; 00104 00106 tCRCValue initial_value; 00107 00109 tCRCValue const* crc_table; 00110 00111 public: 00113 cCRC(tCRCValue const* _crc_table, tCRCValue _initial_value) 00114 { 00115 crc_table = _crc_table; 00116 initial_value = _initial_value; 00117 current_crc = initial_value; 00118 } 00119 00121 tCRCValue AddByte(unsigned char byte) 00122 { 00123 current_crc = ((current_crc & 0xFF00) >> 8) ^ crc_table[(current_crc & 0x00FF) ^ (byte & 0x00FF)]; 00124 return current_crc; 00125 } 00126 00128 tCRCValue AddBytes(unsigned char* bytes, int nb_bytes) 00129 { 00130 for (int i = 0; i < nb_bytes; i++) 00131 current_crc = ((current_crc & 0xFF00) >> 8) ^ crc_table[(current_crc & 0x00FF) ^ (bytes[i] & 0x00FF)]; 00132 return current_crc; 00133 } 00134 00136 inline tCRCValue GetCRC() 00137 { 00138 return current_crc; 00139 } 00140 00142 inline UInt8 GetCRC_LB() 00143 { 00144 return current_crc & 0x00ff; 00145 } 00146 00148 inline UInt8 GetCRC_HB() 00149 { 00150 return (current_crc >> 8) & 0x00ff; 00151 } 00152 00154 inline tCRCValue Reset() 00155 { 00156 current_crc = initial_value; 00157 return current_crc; 00158 } 00159 }; 00160 //---------------------------------------------------------------------- 00161 //---------------------------------------------------------------------- 00162 00164 class VCC_EXPORT cCRC_DSACON32m : public cCRC 00165 { 00166 protected: 00168 static tCRCValue const crc_table_dsacon32m[256]; 00169 00170 public: 00172 inline cCRC_DSACON32m(void) 00173 : cCRC(crc_table_dsacon32m, 0xffff) 00174 { 00175 // nothing more to do 00176 } 00177 }; 00178 //---------------------------------------------------------------------- 00179 //---------------------------------------------------------------------- 00180 00187 class VCC_EXPORT cCRC_SDH : public cCRC_DSACON32m 00188 { 00189 public: 00191 inline cCRC_SDH(void) 00192 : cCRC_DSACON32m() 00193 { 00194 // nothing more to do 00195 } 00196 }; 00197 //---------------------------------------------------------------------- 00198 //---------------------------------------------------------------------- 00199 00200 NAMESPACE_SDH_END 00201 00202 #endif 00203 00204 00205 //====================================================================== 00206 /* 00207 Here are some settings for the emacs/xemacs editor (and can be safely ignored): 00208 (e.g. to explicitely set C++ mode for *.h header files) 00209 00210 Local Variables: 00211 mode:C 00212 mode:ELSE 00213 End: 00214 */ 00215 //======================================================================}