GeoNetworkingLayer.cc 4.3 KB
Newer Older
garciay's avatar
garciay committed
1
2
3
4
5
#include "GeoNetworkingLayer.hh"
#include "GeoNetworkingTypes.hh"

#include "loggers.hh"

garciay's avatar
garciay committed
6
GeoNetworkingLayer::GeoNetworkingLayer(const std::string & p_type, const std::string & param) : TLayer<LibItsGeoNetworking__TestSystem::GeoNetworkingPort>(p_type), _params(), _codec(), _thread(NULL), _running(FALSE) {
garciay's avatar
garciay committed
7
  loggers::get_instance().log(">>> GeoNetworkingLayer::GeoNetworkingLayer: %s, %s", to_string().c_str(), param.c_str());
garciay's avatar
garciay committed
8
9
  // Setup parameters
  Params::convert(_params, param);
garciay's avatar
garciay committed
10
11
12
13
  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"));
  }
14
15
}

garciay's avatar
garciay committed
16
17
18
19
20
21
22
23
24
GeoNetworkingLayer::~GeoNetworkingLayer() {
  if (_thread != NULL) {
    _running = FALSE;
    // Wait for the working thread to terminate
    _thread->join();
    loggers::get_instance().log("GeoNetworkingLayer::~GeoNetworkingLayer: Thread were stops");
  }
}
void GeoNetworkingLayer::sendMsg(const LibItsGeoNetworking__TestSystem::GeoNetworkingReq& p, Params& params) {
garciay's avatar
garciay committed
25
  loggers::get_instance().log(">>> GeoNetworkingLayer::sendMsg");
garciay's avatar
garciay committed
26
27

  // Encode GeoNetworking PDU
garciay's avatar
garciay committed
28
29
  OCTETSTRING data;
  _codec.encode(p.msgOut(), data);
garciay's avatar
garciay committed
30
  sendData(data, params);
garciay's avatar
garciay committed
31
32
}

garciay's avatar
garciay committed
33
void GeoNetworkingLayer::sendData(OCTETSTRING& data, Params& params) {
garciay's avatar
garciay committed
34
  loggers::get_instance().log_msg(">>> GeoNetworkingLayer::sendData: ", data);
garciay's avatar
garciay committed
35
  params.log();  
garciay's avatar
garciay committed
36
  sendToAllLayers(data, params);
garciay's avatar
garciay committed
37
38
}

garciay's avatar
garciay committed
39
void GeoNetworkingLayer::receiveData(OCTETSTRING& data, Params& params) { 
garciay's avatar
garciay committed
40
  loggers::get_instance().log_msg(">>> GeoNetworkingLayer::receiveData: ", data);
garciay's avatar
garciay committed
41
42
  // Decode the payload
  LibItsGeoNetworking__TestSystem::GeoNetworkingInd p;
43
  _codec.decode(data, p.msgIn(), &params);
garciay's avatar
garciay committed
44
  // Add lower layers parameters
garciay's avatar
garciay committed
45
  // 1. Destination MAC address
garciay's avatar
garciay committed
46
  Params::const_iterator it = params.find(Params::mac_dst);
garciay's avatar
garciay committed
47
  if (it != params.cend()) {
48
    loggers::get_instance().log("GeoNetworkingLayer::receiveData: dst=%s", it->second.c_str());
garciay's avatar
garciay committed
49
50
    p.macDestinationAddress() = str2oct(CHARSTRING(it->second.c_str()));
  } else {
garciay's avatar
garciay committed
51
    p.macDestinationAddress() = str2oct(CHARSTRING(_params["mac_bc"].c_str()));
garciay's avatar
garciay committed
52
53
54
55
  }
  // 2. ssp
  it = params.find(Params::ssp);
  if (it != params.cend()) {
56
    loggers::get_instance().log("GeoNetworkingLayer::receiveData: ssp=%s", it->second.c_str());
garciay's avatar
garciay committed
57
58
59
    p.ssp() = str2bit(CHARSTRING(it->second.c_str()));
  } else {
    p.ssp().set_to_omit();
garciay's avatar
garciay committed
60
  }
garciay's avatar
garciay committed
61
62
63
  // 3. its_aid
  it = params.find(Params::its_aid);
  if (it != params.cend()) {
64
    loggers::get_instance().log("GeoNetworkingLayer::receiveData: its_aid=%s", it->second.c_str());
garciay's avatar
garciay committed
65
66
67
68
69
    p.its__aid() = std::stoi(it->second.c_str());
  } else {
    p.its__aid().set_to_omit();
  }
  
70
71
72
73
74
75
76
77
78
79
  // 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
80
  toAllUpperPorts(p, params);
garciay's avatar
garciay committed
81
82
}

garciay's avatar
garciay committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
void GeoNetworkingLayer::start_beconning() {
  loggers::get_instance().log(">>> GeoNetworkingLayer::start_beaconing");

}

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
99
class GeoNetworkingFactory: public LayerFactory {
garciay's avatar
garciay committed
100
  static GeoNetworkingFactory _f;
garciay's avatar
garciay committed
101
public:
garciay's avatar
garciay committed
102
103
104
  GeoNetworkingFactory();
  virtual Layer * createLayer(const std::string & type,
			      const std::string & param);
garciay's avatar
garciay committed
105
106
107
};

GeoNetworkingFactory::GeoNetworkingFactory() {
garciay's avatar
garciay committed
108
109
110
  // Register factory
  loggers::get_instance().log(">>> GeoNetworkingFactory::GeoNetworkingFactory");
  LayerStackBuilder::RegisterLayerFactory("GN", this);
garciay's avatar
garciay committed
111
112
113
}

Layer * GeoNetworkingFactory::createLayer(const std::string & type, const std::string & param) {
garciay's avatar
garciay committed
114
  return new GeoNetworkingLayer(type, param);
garciay's avatar
garciay committed
115
116
117
}

GeoNetworkingFactory GeoNetworkingFactory::_f;