source: trunk/ETSI-Testsuites/ETSI_auto_IOT/adapter/src/Messages/TrafficCaptureMessage.cpp @ 36

Last change on this file since 36 was 22, checked in by rings, 14 years ago
  • Property svn:executable set to *
File size: 6.1 KB
Line 
1/**
2 * @file TrafficCaptureMessage.cpp
3 * @author Tomas Urban
4 * @version 0.2
5 * @date 23/07/2009
6 */
7#include "TrafficCaptureMessage.h"
8#include <string.h>
9
10TrafficCaptureMessage::TrafficCaptureMessage(void)
11{
12        m_pData = 0;
13        m_nDataLen = 0;
14}
15
16TrafficCaptureMessage::~TrafficCaptureMessage(void)
17{
18        DisposeEncodedData();
19}
20
21void TrafficCaptureMessage::DisposeEncodedData()
22{
23        delete m_pData;
24        m_pData = 0;
25        m_nDataLen = 0;
26}
27
28void TrafficCaptureMessage::EncodeData(const void * pData, size_t nSize, unsigned int & nOffset)
29{
30        if (!pData || !nSize)
31                return;
32        memcpy(&m_pData[nOffset], pData, nSize);
33        nOffset += nSize;
34}
35
36void TrafficCaptureMessage::EncodeUChar(unsigned char nValue, unsigned int & nOffset)
37{
38        EncodeData(&nValue, sizeof(unsigned char), nOffset);
39}
40
41void TrafficCaptureMessage::EncodeUShort(unsigned short nValue, unsigned int & nOffset)
42{
43        EncodeData(&nValue, sizeof(unsigned short), nOffset);
44}
45
46void TrafficCaptureMessage::EncodeInt(int nValue, unsigned int & nOffset)
47{
48        EncodeData(&nValue, sizeof(int), nOffset);
49}
50void TrafficCaptureMessage::EncodeUInt(unsigned int nValue, unsigned int & nOffset)
51{
52        EncodeData(&nValue, sizeof(unsigned int), nOffset);
53}
54
55void TrafficCaptureMessage::EncodeTime(struct timeval & timestamp, unsigned int & nOffset)
56{
57        unsigned long long nSec = timestamp.tv_sec;
58        EncodeData(&nSec, sizeof(unsigned long long), nOffset);
59        EncodeUInt(timestamp.tv_usec, nOffset);
60}
61
62void TrafficCaptureMessage::EncodeBool(bool bValue, unsigned int & nOffset)
63{
64        m_pData[nOffset++] = (char)bValue;
65}
66
67void TrafficCaptureMessage::EncodeString(const std::string & sValue, unsigned int & nOffset)
68{
69        unsigned int nLen = sValue.length();
70        EncodeUInt(nLen, nOffset);
71        EncodeData(sValue.c_str(), nLen, nOffset);
72}
73
74unsigned int TrafficCaptureMessage::CalculateDataLength()
75{
76        return
77                sizeof(unsigned short) // 2 byte id
78                + sizeof(m_nDataLen); // 4 byte length
79}
80
81unsigned int TrafficCaptureMessage::CalculateStringLength(const std::string & sValue)
82{
83        return sizeof(unsigned int) + sValue.length();
84}
85
86void TrafficCaptureMessage::EncodePayload(unsigned int & nOffset)
87{
88}
89
90void TrafficCaptureMessage::Encode()
91{
92        m_nDataLen = CalculateDataLength();
93        m_pData = new char[m_nDataLen];
94        unsigned int nOffset = 0;
95        EncodeUShort(GetId(), nOffset);
96        unsigned int nPayloadLen = m_nDataLen - sizeof(unsigned short) - sizeof (m_nDataLen);
97        EncodeUInt(nPayloadLen, nOffset);
98        EncodePayload(nOffset);
99}
100
101const char * TrafficCaptureMessage::GetEncodedMessage()
102{
103        if (!m_pData)
104                Encode();
105        return m_pData;
106}
107
108unsigned int TrafficCaptureMessage::GetEncodedDataLength()
109{
110        if (!m_pData)
111                Encode();
112        return m_nDataLen;
113}
114
115bool TrafficCaptureMessage::DecodeData(void * pValue, size_t nValueSize, const char * pData,
116                                                                           unsigned int nDataLen, unsigned int & nOffset)
117{
118        if (nOffset + nValueSize > nDataLen)
119                return false;
120        memcpy(pValue, &pData[nOffset], nValueSize);
121        nOffset += nValueSize;
122        return true;
123}
124
125bool TrafficCaptureMessage::DecodeUChar(unsigned char & nValue, const char * pData, unsigned int nDataLen,
126                                                                                 unsigned int & nOffset)
127{
128        return DecodeData(&nValue, sizeof(unsigned char), pData, nDataLen, nOffset);
129}
130
131bool TrafficCaptureMessage::DecodeUShort(unsigned short & nValue, const char * pData, unsigned int nDataLen,
132                                                                                 unsigned int & nOffset)
133{
134        return DecodeData(&nValue, sizeof(unsigned short), pData, nDataLen, nOffset);
135}
136bool TrafficCaptureMessage::DecodeInt(int & nValue, const char * pData, unsigned int nDataLen,
137                                                                          unsigned int & nOffset)
138{
139        return DecodeData(&nValue, sizeof(int), pData, nDataLen, nOffset);
140}
141bool TrafficCaptureMessage::DecodeUInt(unsigned int & nValue, const char * pData, unsigned int nDataLen,
142                                                                           unsigned int & nOffset)
143{
144        return DecodeData(&nValue, sizeof(unsigned int), pData, nDataLen, nOffset);
145}
146
147bool TrafficCaptureMessage::DecodeTime(struct timeval & timestamp, const char * pData, unsigned int nDataLen,
148                                                                           unsigned int & nOffset)
149{
150        unsigned long long nSec;
151        if (!DecodeData(&nSec, sizeof(unsigned long long), pData, nDataLen, nOffset))
152                return false;
153#ifdef WIN32
154        timestamp.tv_sec = static_cast<int>(nSec);
155#else
156        timestamp.tv_sec = nSec;
157#endif 
158        int nMicrosec;
159        if (!DecodeInt(nMicrosec, pData, nDataLen, nOffset))
160                return false;
161        timestamp.tv_usec = nMicrosec;
162        return true;
163}
164
165bool TrafficCaptureMessage::DecodeBool(bool & bValue, const char * pData, unsigned int nDataLen,
166                                                                           unsigned int & nOffset)
167{
168        if (nOffset + 1 > nDataLen)
169                return false;
170        bValue = pData[nOffset++] != 0;
171        return true;
172}
173
174bool TrafficCaptureMessage::DecodeString(std::string & sValue, const char * pData,
175                                                                                 unsigned int nDataLen, unsigned int & nOffset)
176{
177        unsigned int nLen;
178        if (!DecodeUInt(nLen, pData, nDataLen, nOffset))
179                return false;
180        char * pszTmp = new char [nLen + 1];
181        bool bRes = DecodeData(pszTmp, nLen, pData, nDataLen, nOffset);
182        if (bRes)
183        {
184                pszTmp[nLen] = '\0';
185                sValue = pszTmp;
186        }
187        delete pszTmp;
188        return bRes;
189}
190
191bool TrafficCaptureMessage::DecodePayload(const char * pPayload, unsigned int nPayloadLength,
192                                                                                  unsigned int & nOffset)
193{
194        return true;
195}
196
197bool TrafficCaptureMessage::DecodePayload(const char * pPayload, unsigned int nPayloadLength)
198{
199        unsigned int nOffset = 0;
200        bool bRes = DecodePayload(pPayload, nPayloadLength, nOffset);
201        bRes &= nPayloadLength == nOffset; // verify that there's no unprocessed data left
202        return bRes;
203}
204
205CommonReplyMessage::CommonReplyMessage() :
206        m_bResult(false)
207{
208}
209
210unsigned int CommonReplyMessage::CalculateDataLength()
211{
212        return TrafficCaptureMessage::CalculateDataLength() + 1; // bool is encoded into 1 byte
213}
214
215void CommonReplyMessage::EncodePayload(unsigned int & nOffset)
216{
217        TrafficCaptureMessage::EncodePayload(nOffset);
218        EncodeBool(m_bResult, nOffset);
219}
220
221bool CommonReplyMessage::DecodePayload(const char * pPayload, unsigned int nPayloadLength,
222                                                                           unsigned int & nOffset)
223{
224        if (!TrafficCaptureMessage::DecodePayload(pPayload, nPayloadLength, nOffset))
225                return false;
226        if (!DecodeBool(m_bResult, pPayload, nPayloadLength, nOffset))
227                return false;
228        return true;
229}
230
231void CommonReplyMessage::SetResult(bool bResult)
232{
233        DisposeEncodedData();
234        m_bResult = bResult;
235}
236
Note: See TracBrowser for help on using the repository browser.