source: trunk/ETSI-Testsuites/ETSI_auto_IOT/adapter/src/LowerTestAdapter/LowerTestAdapter.cpp @ 27

Last change on this file since 27 was 22, checked in by rings, 14 years ago
  • Property svn:executable set to *
File size: 8.9 KB
Line 
1#include "LowerTestAdapter.h"
2
3#include <iostream>
4#include <sys/time.h>
5
6#include "Helper/Singleton.h"
7#include "Logger/Logger.h"
8#include "Messages/CommonTrafficCaptureMessages.h"
9#include "Messages/TrafficCaptureMessageFactory.h"
10#include "Messages/CapturedData.h"
11#include "Dispatcher/EthernetDispatcher.h"
12#include "Ports/MessagesIdentifiers.h"
13
14LowerTestAdapter::LowerTestAdapter()
15  :m_dataPort(0),
16   m_adapterConfigPort(0) {
17 
18  Logger::Instance().LogInfo("LowerTester creation");
19}
20
21LowerTestAdapter::~LowerTestAdapter() {
22}
23
24void LowerTestAdapter::RegisterDataPort(DataPort *pPort) {
25  m_dataPort = pPort;
26}
27
28void LowerTestAdapter::RegisterAdapterConfigPort(AdapterConfigPort *pPort) {
29  m_adapterConfigPort = pPort;
30}
31
32void LowerTestAdapter::RegisterFilter(const ComponentId *pComponent, const FilterSet & filter) {
33
34    m_filter.RegisterComponent(pComponent, filter);
35}
36
37void LowerTestAdapter::InitializeCapture(boost::shared_ptr<GeneralConfigurationParams> configParams)  throw (LowerTestAdapterException) {
38
39  clog << ">>> LowerTestAdapter::InitializeCapture" << endl;
40 
41  // Process OpenDevice message
42  OpenDeviceRequest rq;
43  // Set capture mode.
44  ECaptureMode ecMode;
45  clog << "LowerTestAdapter::InitializeCapture: Prepare mode:" << static_cast<int>(configParams.get()->Mode()) << endl;
46  switch (configParams.get()->Mode()) {
47    case 0x00:
48      ecMode = ECaptureMode_Live;
49      break;
50    case 0x01:
51      ecMode = ECaptureMode_Offline;
52      break;
53    default:
54      clog << "LowerTestAdapter::InitializeCapture: set live" << endl;
55      ecMode = ECaptureMode_Live;
56  }
57  clog << "LowerTestAdapter::InitializeCapture: Set mode:" << ecMode << endl;
58  rq.SetCaptureMode(ecMode);
59  // Set capture type.
60  rq.SetCaptureType(ECaptureType_PCAP);
61 
62  // Process merge operation first.
63  if ((ecMode == ECaptureMode_Offline) && (configParams.get()->MergerFileList().size() != 0))
64  {
65    clog << "LowerTestAdapter::InitializeCapture: process merge operation first" << endl;
66    MergePcapFilesRequest mergeReq;
67    // Populate Merge message.
68    mergeReq.SetMergeSource(configParams.get()->MergerFilePath(), configParams.get()->MergerFileList());
69    // Populate Merge tool path.
70    mergeReq.SetMergecapDirectory(configParams.get()->PcapMergeTool());
71    // Send parameters to the TrafficCapture process.
72    std::clog << "LowerTestAdapter::InitializeCapture: Merge tool path" << std::endl;
73    m_captureSocket->Send(mergeReq.GetEncodedMessage(), mergeReq.GetEncodedDataLength());
74    // TODO: Process the response !!!
75  }
76   // Continue with OpenDevice reauest processing
77  if (ecMode == ECaptureMode_Live)
78  {
79    std::vector<std::string> ifaces = configParams.get()->Ifaces();
80    // Sanity check.
81    if (ifaces.size() == 0)
82    {
83      std::cerr << "LowerTestAdapter::InitializeCapture: Ifaces are not well configured" << std::endl;
84      throw LowerTestAdapterException("LowerTestAdapter::InitializeCapture: Ifaces are not well configured");
85    }
86    // Set interfaces.
87    for(vector<string>::iterator it = ifaces.begin() ; it != ifaces.end(); ++it)
88    {
89      std::clog << "LowerTestAdapter::InitializeCapture: set iface: " << *it << std::endl;
90      rq.SetParameters(*it);
91    }
92  }
93  else // Off line mode
94  {
95    // Check file to play
96    if ((configParams.get()->MergerFileList().size() == 0) && (configParams.get()->FileToPlay().length() != 0))
97    {
98      rq.SetParameters(configParams.get()->FileToPlay());
99    }
100    else
101    {
102      // TODO: To be continued.
103      std::cerr << "LowerTestAdapter::InitializeCapture: Offline mode is not properly configure" << std::endl;
104    }
105    // Set timestamp offset if not 0
106    if ((configParams.get()->PcapTimeStampSeconds() != 0) || (configParams.get()->PcapTimeStampMicroseconds() != 0))
107    {
108      struct timeval timestamp = {
109        (time_t)(configParams.get()->PcapTimeStampSeconds()),
110        configParams.get()->PcapTimeStampMicroseconds() };
111      std::clog << "LowerTestAdapter::InitializeCapture: timestamp={" << timestamp.tv_sec << ", " << timestamp.tv_usec << "}" << std::endl;
112      rq.SetTimestamp(timestamp);
113    }
114  }
115  // Send parameters to the TrafficCapture process.
116  std::clog << "LowerTestAdapter::InitializeCapture: Open device" << std::endl;
117  SendMessage(&rq);
118}
119
120void LowerTestAdapter::SendMessage(TrafficCaptureMessage * pMsg)
121{
122  m_captureSocket->Send(pMsg->GetEncodedMessage(), pMsg->GetEncodedDataLength());
123}
124
125void LowerTestAdapter::StartCapture() {
126  std::stringstream ss;
127
128  // Set capture filter
129  SetFilterRequest filterReq;
130  filterReq.SetFilter(m_filter.GetPcapFilter());
131  ss << "Capture Filter: " << m_filter.GetPcapFilter();
132  Logger::Instance().LogDebug(ss.str());
133  SendMessage(&filterReq);
134
135  // Start capture
136  StartCaptureRequest rq;
137  std::clog << "LowerTestAdapter::StartCapture" << std::endl;
138  SendMessage(&rq);
139}
140
141void LowerTestAdapter::StopCapture() {
142
143  StopCaptureRequest rq;
144  std::clog << "LowerTestAdapter::StopCapture" << std::endl;
145  SendMessage(&rq);
146  Logger::Instance().LogDebug("Filter clean-up");
147  m_filter.Clear();
148}
149
150void LowerTestAdapter::ConnectToCaptureServer(boost::shared_ptr<GeneralConfigurationParams> configParams)  throw (LowerTestAdapterException) {
151
152  clog << ">>> LowerTestAdapter::ConnectToCaptureServer: " << configParams.get()->CaptureModuleAddress() << " - " << configParams.get()->CaptureModulePort() << endl;
153 
154  // Sanity checks.
155  if (configParams.get()->CaptureModuleAddress().length() == 0)
156  {
157    cerr << ">>> LowerTestAdapter::ConnectToCaptureServer: " << configParams.get()->CaptureModuleAddress() << " - " << configParams.get()->CaptureModulePort() << endl;
158    throw LowerTestAdapterException("LowerTestAdapter::ConnectToCaptureServer: information is missing");
159  }
160 
161  m_captureSocket = new SocketClient(configParams.get()->CaptureModuleAddress(), configParams.get()->CaptureModulePort());
162  m_captureThread = new boost::thread(&Run);
163
164  InitializeCapture(configParams);
165}
166
167void LowerTestAdapter::EnqueueMessage(const ComponentId * pComp, const unsigned char * pData, int nDataLen) {
168  t3devlib::Bitstring bsCapturedBits;
169  bsCapturedBits.Append(pData, nDataLen * 8);
170  // Enqueue captured packet to monitor ports
171  m_dataPort->EnqueueMsg(m_dataPort->GetConnectedPort(pComp), bsCapturedBits);
172}
173
174void LowerTestAdapter::ProcessCapturedData(const unsigned char * pCapturedData, unsigned int nDataLen) {
175  DispatchInfo msgInfo;
176  msgInfo.SetData(pCapturedData, nDataLen);
177  ComponentFilter filterCopy (m_filter);
178  bool bEnqueued = EthernetDispatcher::Instance().Dispatch(msgInfo, filterCopy);
179
180  if(!bEnqueued) {
181    std::stringstream ss;
182    ss << "Captured packet not selected by any Monitor port";
183    Logger::Instance().LogWarning(ss.str());
184  }
185}
186
187void LowerTestAdapter::ProcessResponse(const std::string& message, bool result) {
188  Logger::Instance().LogInfo("LowerTestAdapter::ProcessResponse");
189 
190  m_adapterConfigPort->ProcessResponse (message, result);
191}
192
193void LowerTestAdapter::Run() {
194  Logger::Instance().LogInfo("LowerTester dispatch thread created");
195
196  //Main loop
197  while(true) {
198
199    // Receive message header
200    TMessageHeader msgHeader;
201    int nb = Singleton<LowerTestAdapter>::Instance()
202      .m_captureSocket->Receive(sizeof(TMessageHeader),
203        reinterpret_cast<char*>(&msgHeader));
204
205    // Receive payload
206    char *psPayload = new char[msgHeader.nMsgLen];
207    nb = LowerTestAdapter::Instance()
208      .m_captureSocket->Receive(msgHeader.nMsgLen, psPayload);
209
210    // Analyze message
211    TrafficCaptureMessage *pMsg
212      = TrafficCaptureMessageFactory::Instance().CreateMessage(msgHeader.nMsgType, psPayload, msgHeader.nMsgLen);
213    if (pMsg == NULL)
214    {
215      Logger::Instance().LogError("LowerTestAdapter::Run: Create TrafficCaptureMessage failed");
216      continue;
217    }
218
219    clog << "LowerTestAdapter::Run: receive response=" << pMsg->GetId() << endl;
220    string message("");
221    switch (pMsg->GetId()) {
222    case CAPTURED_PACKET_IND:
223      {
224        CapturedData * pCapturedData = dynamic_cast<CapturedData*>(pMsg);
225        const unsigned char * pData = reinterpret_cast<const unsigned char *>(pCapturedData->GetData());   
226        LowerTestAdapter::Instance().ProcessCapturedData(pData, pCapturedData->GetDataLength());
227      }
228      break;
229    case START_CAPTURE_REP:
230      message.assign(MessagesIdentifiers::StartTrafficCaptureRsp);
231      break;
232    case STOP_CAPTURE_REP:
233      message.assign(MessagesIdentifiers::StopTrafficCaptureRsp);
234      break;
235    case OPEN_DEVICE_REP:
236      message.assign(MessagesIdentifiers::GeneralConfigurationRsp);
237      break;
238    case SET_FILTER_REP:
239      //message.assign(MessagesIdentifiers::SetFilterRsp);
240      break;
241    case MERGE_FILES_REP:
242      break;
243    default:
244      break;
245    }
246   
247    if (message.length() != 0)
248    {
249      clog << "LowerTestAdapter::Run: Call LowerTestAdapter::Instance().ProcessResponse: " << message << " - " << ((CommonReplyMessage *)pMsg)->GetResult() << endl;
250      LowerTestAdapter::Instance().ProcessResponse(message, ((CommonReplyMessage *)pMsg)->GetResult());
251    }
252    TrafficCaptureMessageFactory::Instance().DisposeMessage(pMsg);
253  }
254}
255
Note: See TracBrowser for help on using the repository browser.