source: trunk/ETSI-Testsuites/ETSI_auto_IOT/adapter/src/Messages/CapturedData.cpp @ 55

Last change on this file since 55 was 22, checked in by rings, 14 years ago
  • Property svn:executable set to *
File size: 2.8 KB
Line 
1/**
2 * @file CapturedData.cpp
3 * @author Tomas Urban
4 * @version 0.1
5 * @date 16/07/2009
6 */
7#include "CapturedData.h"
8#include <sstream>
9#include <iomanip>
10#include <ctime>
11
12CapturedData::CapturedData(void) :
13        m_nCapturedLen(0),
14        m_pCapturedData(0),
15        m_captureType(ECaptureType_PCAP)
16{
17        m_timestamp.tv_sec = 0;
18        m_timestamp.tv_usec = 0;
19}
20
21CapturedData::~CapturedData(void)
22{
23        delete m_pCapturedData;
24}
25
26void CapturedData::SetData (int nLen, const char * pData)
27{
28        DisposeEncodedData();
29        delete m_pCapturedData;
30        if (nLen > 0)
31        {
32                m_nCapturedLen = nLen;
33                m_pCapturedData = new char[nLen];
34                memcpy(m_pCapturedData, pData, nLen);
35        }
36        else
37        {
38                m_nCapturedLen = 0;
39                m_pCapturedData = NULL;
40        }
41}
42
43void CapturedData::SetTimestamp(timeval timestamp)
44{
45        DisposeEncodedData();
46        m_timestamp = timestamp;
47}
48void CapturedData::SetCaptureType(ECaptureType captureType)
49{
50        DisposeEncodedData();
51        m_captureType = captureType;
52}
53
54unsigned int CapturedData::CalculateDataLength()
55{
56        return TrafficCaptureMessage::CalculateDataLength() +
57                1 + // capture type
58                TIME_SIZE + // timestamp
59                sizeof(m_nCapturedLen) + m_nCapturedLen; // captured packet
60}
61
62void CapturedData::EncodePayload(unsigned int & nOffset)
63{
64        TrafficCaptureMessage::EncodePayload(nOffset);
65        EncodeUChar(static_cast<unsigned char>(m_captureType), nOffset);
66        EncodeTime(m_timestamp, nOffset);
67        EncodeUInt(m_nCapturedLen, nOffset);
68        EncodeData(m_pCapturedData, m_nCapturedLen, nOffset);
69}
70
71bool CapturedData::DecodePayload(const char * pPayload, unsigned int nPayloadLength, unsigned int & nOffset)
72{
73        delete m_pCapturedData;
74        m_pCapturedData = NULL;
75        if (!TrafficCaptureMessage::DecodePayload(pPayload, nPayloadLength, nOffset))
76                return false;
77        unsigned char c;
78        if (!DecodeUChar(c, pPayload, nPayloadLength, nOffset))
79                return false;
80        m_captureType = static_cast<ECaptureType>(c);
81        if (!DecodeTime(m_timestamp, pPayload, nPayloadLength, nOffset))
82                return false;
83        if (!DecodeUInt(m_nCapturedLen, pPayload, nPayloadLength, nOffset))
84        {
85                m_nCapturedLen = 0;
86                return false;
87        }
88        if (m_nCapturedLen)
89        {
90                m_pCapturedData = new char [m_nCapturedLen];
91                if (!DecodeData(m_pCapturedData, m_nCapturedLen, pPayload, nPayloadLength, nOffset))
92                        return false;
93        }
94        return true;
95}
96std::string CapturedData::ToString()
97{
98        std::stringstream ss;
99        ss << "Data captured\n";
100        time_t t = m_timestamp.tv_sec;
101        struct tm * ts;
102    char szBuf[80];
103        ts = localtime(&t);
104        strftime(szBuf, sizeof(szBuf), "%Y-%m-%d %H:%M:%S", ts);
105        ss << "Timestamp: " << szBuf << "." << m_timestamp.tv_usec << "\n";
106        ss << "Length: " << m_nCapturedLen << "\n";
107        for (unsigned int i = 0; i < m_nCapturedLen; i++)
108        {
109                if (i > 0)                     
110                        ss << (i % 16 == 0 ? "\n" : " ");
111                ss << std::hex << std::setfill('0') << std::setw(2) <<
112                        (short)(unsigned char)m_pCapturedData[i];       
113        }
114        ss << "\n\n";
115        return ss.str();
116}
117
Note: See TracBrowser for help on using the repository browser.