GpCoreTools/MessageRawHeader.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of GpCoreTools.
00004 **
00005 **  This file may be distributed and/or modified under the terms of the
00006 **  GNU General Public License version 2 or 3 as published by the Free
00007 **  Software Foundation and appearing in the file LICENSE.GPL included
00008 **  in the packaging of this file.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
00013 **  more details.
00014 **
00015 **  You should have received a copy of the GNU General Public License
00016 **  along with this program. If not, see <http://www.gnu.org/licenses/>.
00017 **
00018 **  See http://www.geopsy.org for more information.
00019 **
00020 **  Created: 2011-06-20
00021 **  Authors:
00022 **    Marc Wathelet (ISTerre, Grenoble, France)
00023 **
00024 ***************************************************************************/
00025 
00026 #ifndef MESSAGERAWHEADER_H
00027 #define MESSAGERAWHEADER_H
00028 
00029 #include "GpCoreToolsDLLExport.h"
00030 #include "FletcherChecksum.h"
00031 #include "ByteOrder.h"
00032 
00033 namespace GpCoreTools {
00034 
00035 #pragma pack(1)     /* set alignment to 1 byte boundary */
00036 
00037   class GPCORETOOLS_EXPORT MessageRawHeader
00038   {
00039   public:
00040     MessageRawHeader() {}
00041     inline MessageRawHeader(unsigned int length);
00042     inline MessageRawHeader(const MessageRawHeader& o);
00043 
00044     enum {SynchronizationSize=2, HeaderSize=2, CheckSumSize=2};
00045 
00046     bool isValid() const {return _sync[0]==0xB5 && _sync[1]==0x62;}
00047     inline bool isValid(const FletcherChecksum& cksum, const char * payload=0,
00048                         unsigned short payloadLength=0) const;
00049 
00050     inline void littleEndianValues();
00051     inline void bigEndianValues();
00052 
00053     const char * header() const {return _bytes;}
00054 
00055     unsigned int lengthLittleEndian() const {return ByteOrder::littleEndianToNative(_values.length);}
00056     unsigned int lengthBigEndian() const {return ByteOrder::bigEndianToNative(_values.length);}
00057   protected:
00058     unsigned char _sync[2];
00059     union {
00060       struct {
00061         unsigned short length;
00062       } _values;
00063       char _bytes[HeaderSize];
00064     };
00065   };
00066 
00067   inline MessageRawHeader::MessageRawHeader(unsigned int length)
00068   {
00069     _sync[0]=0xB5;
00070     _sync[1]=0x62;
00071     _values.length=length;
00072   }
00073 
00074   inline MessageRawHeader::MessageRawHeader(const MessageRawHeader& o)
00075   {
00076     _sync[0]=o._sync[0];
00077     _sync[1]=o._sync[1];
00078     _values.length=o._values.length;
00079   }
00080 
00081   inline void MessageRawHeader::bigEndianValues()
00082   {
00083 #if BYTE_ORDER==LITTLE_ENDIAN
00084     ByteOrder::swap(_bytes[0], _bytes[1]);
00085 #endif
00086   }
00087 
00088   inline void MessageRawHeader::littleEndianValues()
00089   {
00090 #if BYTE_ORDER==BIG_ENDIAN
00091     ByteOrder::swap(_bytes[0], _bytes[1]);
00092 #endif
00093   }
00094 
00095   inline bool MessageRawHeader::isValid(const FletcherChecksum& cksum, const char * payload,
00096                                         unsigned short payloadLength) const
00097   {
00098     FletcherChecksum mycs;
00099     mycs.add(reinterpret_cast<const char *>(header()), HeaderSize);
00100     if(payload) {
00101       mycs.add(payload, payloadLength);
00102     }
00103     return cksum==mycs;
00104   }
00105 
00106 #pragma pack()   // restore original alignment
00107 
00108 } // namespace GpCoreTools
00109 
00110 #endif // MESSAGERAWHEADER_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines