1 /*************************************************************************** 2 messagebuffer.cpp - Raw message containing type and data 3 ------------------- 4 begin : Thu Dec 5 2002 5 copyright : (C) 2002 by bones 6 email : boesemar@users.sourceforge.net 7 8 ***************************************************************************/ 9 10 /*************************************************************************** 11 * * 12 * This program is free software; you can redistribute it and/or modify * 13 * it under the terms of the GNU General Public License as published by * 14 * the Free Software Foundation; either version 2 of the License, or * 15 * (at your option) any later version. * 16 * * 17 ***************************************************************************/ 18 19 20 /* 21 --- 22 header 23 ----- 24 Uint8 type id 25 Uint8 Acknowledge flag 26 Uint16 counter 27 28 ---- 29 data 30 ---- 31 var length 32 33 34 header + data = buffer 35 36 */ 37 38 #include "messagebuffer.h" 39 40 CMessageBuffer()41CMessageBuffer::CMessageBuffer() 42 { 43 s_msg_header h; 44 h.ac = 0; 45 setHeader(h); 46 } 47 ~CMessageBuffer()48CMessageBuffer::~CMessageBuffer() 49 { 50 } 51 setType(const eMessageType t)52bool CMessageBuffer::setType(const eMessageType t) 53 { 54 s_msg_header m = getHeader(); 55 m.tid = (Uint8) t; 56 return setHeader(m); 57 } 58 setAC(const Uint8 acflag)59bool CMessageBuffer::setAC(const Uint8 acflag) 60 { 61 s_msg_header m = getHeader(); 62 m.ac = (Uint8) acflag; 63 return setHeader(m); 64 } 65 setCounter(const Uint16 counter)66bool CMessageBuffer::setCounter(const Uint16 counter) 67 { 68 s_msg_header m = getHeader(); 69 m.counter = counter; 70 return setHeader(m); 71 } 72 73 setData(const CBinBuffer & b)74bool CMessageBuffer::setData(const CBinBuffer & b) 75 { 76 resize(b.size() + getHeaderLength()); 77 for (unsigned int i=0;i<b.size();i++) 78 (*this)[getHeaderLength()+i] = b[i]; 79 80 return (true); 81 } 82 getData() const83CBinBuffer CMessageBuffer::getData() const 84 { 85 return substr(this->getHeaderLength()); 86 } 87 88 getHeader() const89CMessageBuffer::s_msg_header CMessageBuffer::getHeader() const 90 { 91 s_msg_header res; 92 res.ac = 255; 93 if (size() < sizeof(s_msg_header)) return res; 94 unsigned int pos = 0; 95 res.tid = getUint8(pos); 96 res.ac = getUint8(pos); 97 res.counter = getUint16(pos); 98 return res; 99 } 100 getType() const101CMessageBuffer::eMessageType CMessageBuffer::getType() const 102 { 103 s_msg_header m = getHeader(); 104 if (m.ac == 255) return badMessage; 105 return ((CMessageBuffer::eMessageType) m.tid); 106 } 107 getAC() const108Uint8 CMessageBuffer::getAC() const 109 { 110 s_msg_header m = getHeader(); 111 if (m.ac == 255) return badMessage; 112 return m.ac; 113 } 114 getCounter() const115Uint16 CMessageBuffer::getCounter() const 116 { 117 s_msg_header m = getHeader(); 118 if (m.ac == 255) return badMessage; 119 return m.counter; 120 } 121 122 123 setHeader(s_msg_header & h)124bool CMessageBuffer::setHeader(s_msg_header & h) 125 { 126 CBinBuffer tmp; 127 tmp.empty(); 128 tmp+=h.tid; 129 tmp+=h.ac; 130 tmp+=h.counter; 131 if (size() < tmp.size()) resize(tmp.size()); 132 for (unsigned int i=0;i<tmp.size();i++) 133 (*this)[i] = tmp[i]; 134 135 return true; 136 } 137 138 setBuffer(const CBinBuffer & b)139bool CMessageBuffer::setBuffer(const CBinBuffer & b) 140 { 141 if (size() < b.size()) 142 resize(b.size()); 143 144 for (unsigned int i=0;i<b.size();i++) 145 (*this)[i] = b[i]; 146 147 return true; 148 } 149 getBuffer()150CBinBuffer CMessageBuffer::getBuffer() 151 { 152 return (*this); 153 } 154 CMessageBuffer(const CMessageBuffer::eMessageType & t)155CMessageBuffer::CMessageBuffer(const CMessageBuffer::eMessageType & t) 156 { 157 s_msg_header h; 158 h.tid = (Uint8) t; 159 setHeader(h); 160 } 161 162 getHeaderLength() const163int CMessageBuffer::getHeaderLength() const 164 { 165 return sizeof(s_msg_header); 166 } 167