GeoNetworkingLayer.cc 8.01 KB
Newer Older
garciay's avatar
garciay committed
1
2
#include <chrono>

garciay's avatar
garciay committed
3
4
5
6
7
#include "GeoNetworkingLayer.hh"
#include "GeoNetworkingTypes.hh"

#include "loggers.hh"

garciay's avatar
garciay committed
8
9
10
using namespace LibItsGeoNetworking__TypesAndValues;

GeoNetworkingLayer::GeoNetworkingLayer(const std::string & p_type, const std::string & param) : TLayer<LibItsGeoNetworking__TestSystem::GeoNetworkingPort>(p_type), _params(), _codec(), _beacon(NULL), _thread(NULL), _running(FALSE) {
garciay's avatar
garciay committed
11
  loggers::get_instance().log(">>> GeoNetworkingLayer::GeoNetworkingLayer: %s, %s", to_string().c_str(), param.c_str());
garciay's avatar
garciay committed
12
13
  // Setup parameters
  Params::convert(_params, param);
garciay's avatar
garciay committed
14
15
16
17
  Params::const_iterator it = _params.find("mac_bc");
  if (it == _params.cend()) {
    _params.insert(std::pair<std::string, std::string>(std::string("mac_bc"), "FFFFFFFFFFFF"));
  }
garciay's avatar
garciay committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  // Prepare the static part of the TestSystem beacon
  // TODO Add a method
  _beacon = new GeoNetworkingPdu();
  HeaderTST h;
  h.beaconHdr() = BeaconHeaderType(
				   HeaderType(HeaderType::e__beacon),
				   0
				   );
  ExtendedHeader eh;
  eh.beaconHeader() = BeaconHeader(
				   LongPosVector(
						 GN__Address(
							     TypeOfAddress(TypeOfAddress::e__manual),
							     StationType(StationType::e__passengerCar),
							     33,
							     int2oct(0, 6)
							     ),
						 0,
						 0,
						 0,
						 int2bit(0, 1),
						 0,
						 0
						 )
				   );
  _beacon->basicHeader() = BasicHeader(
				       0,
				       BasicNextHeader(
						       BasicNextHeader::e__commonHeader
						       ),
				       0,
				       Lifetime(
						4,
						LtBase(LtBase::e__50ms)
						),
				       5
				       );
  _beacon->gnPacket().packet() = GnNonSecuredPacket(
						    CommonHeader(
								 NextHeader(
									    NextHeader::e__any
									    ),
								 0,
								 h,
								 TrafficClass(
									      SCF(SCF::e__scfDisabled),
									      ChannelOffload(ChannelOffload::e__choffDisabled),
									     0
									     ),
								 int2bit(0, 8),
								 0,
								 1,
								 0
								 ),
						    OPTIONAL<ExtendedHeader>(eh),
						    OPTIONAL<Payload>()
						    );
  _beacon->gnPacket().packet().payload().set_to_omit();
  _beacon->gnPacket().securedMsg().set_to_omit();
  //  loggers::get_instance().log_msg("GeoNetworkingLayer::GeoNetworkingLayer: beacon value: ", *p._beacon);
78
79
}

garciay's avatar
garciay committed
80
GeoNetworkingLayer::~GeoNetworkingLayer() {
garciay's avatar
garciay committed
81
82
  stop_beaconing();
  delete _beacon;
garciay's avatar
garciay committed
83
84
}
void GeoNetworkingLayer::sendMsg(const LibItsGeoNetworking__TestSystem::GeoNetworkingReq& p, Params& params) {
garciay's avatar
garciay committed
85
  loggers::get_instance().log(">>> GeoNetworkingLayer::sendMsg");
garciay's avatar
garciay committed
86
87

  // Encode GeoNetworking PDU
garciay's avatar
garciay committed
88
89
  OCTETSTRING data;
  _codec.encode(p.msgOut(), data);
garciay's avatar
garciay committed
90
  sendData(data, params);
garciay's avatar
garciay committed
91
92
}

garciay's avatar
garciay committed
93
void GeoNetworkingLayer::sendData(OCTETSTRING& data, Params& params) {
garciay's avatar
garciay committed
94
  loggers::get_instance().log_msg(">>> GeoNetworkingLayer::sendData: ", data);
garciay's avatar
garciay committed
95
  params.log();  
garciay's avatar
garciay committed
96
  sendToAllLayers(data, params);
garciay's avatar
garciay committed
97
98
}

garciay's avatar
garciay committed
99
void GeoNetworkingLayer::receiveData(OCTETSTRING& data, Params& params) { 
garciay's avatar
garciay committed
100
  loggers::get_instance().log_msg(">>> GeoNetworkingLayer::receiveData: ", data);
garciay's avatar
garciay committed
101
102
  // Decode the payload
  LibItsGeoNetworking__TestSystem::GeoNetworkingInd p;
103
  _codec.decode(data, p.msgIn(), &params);
garciay's avatar
garciay committed
104
  // Add lower layers parameters
garciay's avatar
garciay committed
105
  // 1. Destination MAC address
garciay's avatar
garciay committed
106
  Params::const_iterator it = params.find(Params::mac_dst);
garciay's avatar
garciay committed
107
  if (it != params.cend()) {
108
    loggers::get_instance().log("GeoNetworkingLayer::receiveData: dst=%s", it->second.c_str());
garciay's avatar
garciay committed
109
110
    p.macDestinationAddress() = str2oct(CHARSTRING(it->second.c_str()));
  } else {
garciay's avatar
garciay committed
111
    p.macDestinationAddress() = str2oct(CHARSTRING(_params["mac_bc"].c_str()));
garciay's avatar
garciay committed
112
113
114
115
  }
  // 2. ssp
  it = params.find(Params::ssp);
  if (it != params.cend()) {
116
    loggers::get_instance().log("GeoNetworkingLayer::receiveData: ssp=%s", it->second.c_str());
garciay's avatar
garciay committed
117
118
119
    p.ssp() = str2bit(CHARSTRING(it->second.c_str()));
  } else {
    p.ssp().set_to_omit();
garciay's avatar
garciay committed
120
  }
garciay's avatar
garciay committed
121
122
123
  // 3. its_aid
  it = params.find(Params::its_aid);
  if (it != params.cend()) {
124
    loggers::get_instance().log("GeoNetworkingLayer::receiveData: its_aid=%s", it->second.c_str());
garciay's avatar
garciay committed
125
126
127
128
129
    p.its__aid() = std::stoi(it->second.c_str());
  } else {
    p.its__aid().set_to_omit();
  }
  
130
131
132
133
134
135
136
137
138
139
  // Pass the GeoNetworking raw payload to the upper layers if any
  it = params.find(Params::gn_payload);
  if (it != params.cend()) {
    loggers::get_instance().log("GeoNetworkingLayer::receiveData: gn_payload=%s", it->second.c_str());
    OCTETSTRING os(str2oct(CHARSTRING(it->second.c_str())));
    receiveToAllLayers(os, params);
  } else {
    loggers::get_instance().warning("GeoNetworkingLayer::receiveData: No payload to pass to upper layers");
  }
  // Pass it to the ports
garciay's avatar
garciay committed
140
  toAllUpperPorts(p, params);
garciay's avatar
garciay committed
141
142
}

garciay's avatar
garciay committed
143
void GeoNetworkingLayer::start_beaconing() {
garciay's avatar
garciay committed
144
145
  loggers::get_instance().log(">>> GeoNetworkingLayer::start_beaconing");

garciay's avatar
garciay committed
146
147
148
149
150
151
152
153
154
155
156
  // Create the offline reader thread
  _thread = new std::thread(&GeoNetworkingLayer::run_beaconing, (void *)this);
  if (_thread == NULL) {
    loggers::get_instance().error("GeoNetworkingLayer::start_beaconing: Failed to start beaconing thread");
  }
  // Wait for the thread
  while (_running == FALSE) {
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
  }
  // Thread was started
  loggers::get_instance().log("<<< PcapLayer::PcapLayer");    
garciay's avatar
garciay committed
157
158
159
160
161
162
163
164
165
166
167
168
169
}

void GeoNetworkingLayer::stop_beaconing() {
  loggers::get_instance().log(">>> GeoNetworkingLayer::stop_beaconing");
  if (_thread != NULL) {
    _running = FALSE;
    // Wait for the working thread to terminate
    _thread->join();
    loggers::get_instance().log("GeoNetworkingLayer::stop_beaconing: Thread were stops");
    _thread = NULL;
  }
}

garciay's avatar
garciay committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
void* GeoNetworkingLayer::run_beaconing(void* p_this) {
  loggers::get_instance().log(">>> GeoNetworkingLayer::run_beaconing");

  // Pointer the main object
  GeoNetworkingLayer& p = *static_cast<GeoNetworkingLayer *>(p_this);
  Params params(p._params);
  unsigned int expiry = 1000;
  Params::const_iterator it = params.find("TsBeaconInterval");
  if (it != params.cend()) {
    expiry = std::stoul(it->second);
  }
  ExtendedHeader* eh = static_cast<ExtendedHeader *>(p._beacon->gnPacket().packet().extendedHeader().get_opt_value());
  if (eh == NULL) {
    loggers::get_instance().error("GeoNetworkingLayer::run_beaconing: Wrong cast");
  }
  GeoNetworkingCodec codec;
  // Create the beacon
  p._running = TRUE;
  while (p._running) { // Loop while _running flag is up
    // Update timestamp
    loggers::get_instance().log("GeoNetworkingLayer::run_beaconing: Update timestamp");
    unsigned long ms = std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
    eh->beaconHeader().srcPosVector().timestamp__() = ms;
    // Encode message
    OCTETSTRING data;
    codec.encode(*p._beacon, data);
    // Use TITAN codec because of no payload!!
    /*TTCN_Buffer encoding_buffer;
      p._beacon->encode(*p._beacon->get_descriptor(), encoding_buffer, TTCN_EncDec::CT_RAW);*/
    // Send it
    p.sendData(data, params);
    /*OCTETSTRING data;
    data = OCTETSTRING(encoding_buffer.get_len(), encoding_buffer.get_data());
    p.sendData(data, params);*/
    // Timer
    loggers::get_instance().log("GeoNetworkingLayer::run_beaconing: Wait for expiry");
    std::this_thread::sleep_for(std::chrono::milliseconds(expiry));
  } // End of 'while' statement
  
  loggers::get_instance().log("<<< GeoNetworkingLayer::run_beaconing");
  return NULL;
}

garciay's avatar
garciay committed
213
class GeoNetworkingFactory: public LayerFactory {
garciay's avatar
garciay committed
214
  static GeoNetworkingFactory _f;
garciay's avatar
garciay committed
215
public:
garciay's avatar
garciay committed
216
217
218
  GeoNetworkingFactory();
  virtual Layer * createLayer(const std::string & type,
			      const std::string & param);
garciay's avatar
garciay committed
219
220
221
};

GeoNetworkingFactory::GeoNetworkingFactory() {
garciay's avatar
garciay committed
222
223
224
  // Register factory
  loggers::get_instance().log(">>> GeoNetworkingFactory::GeoNetworkingFactory");
  LayerStackBuilder::RegisterLayerFactory("GN", this);
garciay's avatar
garciay committed
225
226
227
}

Layer * GeoNetworkingFactory::createLayer(const std::string & type, const std::string & param) {
garciay's avatar
garciay committed
228
  return new GeoNetworkingLayer(type, param);
garciay's avatar
garciay committed
229
230
231
}

GeoNetworkingFactory GeoNetworkingFactory::_f;