Skip to content
GeoNetworkingLayer.cc 3.29 KiB
Newer Older
garciay's avatar
garciay committed
#include "GeoNetworkingLayer.hh"
#include "GeoNetworkingTypes.hh"

#include "loggers.hh"

garciay's avatar
garciay committed
GeoNetworkingLayer::GeoNetworkingLayer(const std::string & p_type, const std::string & param) : TLayer<LibItsGeoNetworking__TestSystem::GeoNetworkingPort>(p_type), _params(), _codec() {
garciay's avatar
garciay committed
  loggers::get_instance().log(">>> GeoNetworkingLayer::GeoNetworkingLayer: %s, %s", to_string().c_str(), param.c_str());
garciay's avatar
garciay committed
  // Setup parameters
  Params::convert(_params, param);
garciay's avatar
garciay committed
void GeoNetworkingLayer::sendMsg(const LibItsGeoNetworking__TestSystem::GeoNetworkingReq& p, const Params& params) {
garciay's avatar
garciay committed
  loggers::get_instance().log(">>> GeoNetworkingLayer::sendMsg");
garciay's avatar
garciay committed

  // Encode GeoNetworking PDU
garciay's avatar
garciay committed
  OCTETSTRING data;
  _codec.encode(p.msgOut(), data);
garciay's avatar
garciay committed
  // Update parameters
  Params par(params); // FIXME Review all const Param& in method declarations
  par.insert(std::pair<std::string, std::string>(Params::mac_dst, std::string(static_cast<const char *>(oct2str(p.macDestinationAddress())))));
  par.insert(std::pair<std::string, std::string>(Params::its_aid, std::string(static_cast<const char *>(int2str(p.its__aid())))));
garciay's avatar
garciay committed
  sendData(data, par);
garciay's avatar
garciay committed
void GeoNetworkingLayer::sendData(OCTETSTRING& data, Params& params) {
garciay's avatar
garciay committed
  loggers::get_instance().log_msg(">>> GeoNetworkingLayer::sendData: ", data);
garciay's avatar
garciay committed
  params.log();  
garciay's avatar
garciay committed
  sendToAllLayers(data, params);
garciay's avatar
garciay committed
void GeoNetworkingLayer::receiveData(OCTETSTRING& data, Params& params) { 
garciay's avatar
garciay committed
  loggers::get_instance().log_msg(">>> GeoNetworkingLayer::receiveData: ", data);
garciay's avatar
garciay committed
  // Decode the payload
  LibItsGeoNetworking__TestSystem::GeoNetworkingInd p;
  _codec.decode(data, p.msgIn());
  // Add lower layers parameters
garciay's avatar
garciay committed
  // 1. Destination MAC address
garciay's avatar
garciay committed
  std::map<std::string, std::string>::const_iterator it = params.find(Params::mac_dst);
garciay's avatar
garciay committed
  if (it != params.cend()) {
garciay's avatar
garciay committed
    loggers::get_instance().log("GeoNetworkingLayer::receiveData: dst=", it->second.c_str());
garciay's avatar
garciay committed
    p.macDestinationAddress() = str2oct(CHARSTRING(it->second.c_str()));
  } else {
garciay's avatar
garciay committed
    p.macDestinationAddress() = OCTETSTRING(Params::mac_address.size(), Params::mac_address.data());
  }
  // 2. ssp
  it = params.find(Params::ssp);
  if (it != params.cend()) {
    loggers::get_instance().log("GeoNetworkingLayer::receiveData: ssp=", it->second.c_str());
    p.ssp() = str2bit(CHARSTRING(it->second.c_str()));
  } else {
    p.ssp().set_to_omit();
garciay's avatar
garciay committed
  }
garciay's avatar
garciay committed
  // 3. its_aid
  it = params.find(Params::its_aid);
  if (it != params.cend()) {
    loggers::get_instance().log("GeoNetworkingLayer::receiveData: its_aid=", it->second.c_str());
    p.its__aid() = std::stoi(it->second.c_str());
  } else {
    p.its__aid().set_to_omit();
  }
  
garciay's avatar
garciay committed
  // Send it to the ports
garciay's avatar
garciay committed
  toAllUpperPorts(p, params);
garciay's avatar
garciay committed
}

class GeoNetworkingFactory: public LayerFactory {
garciay's avatar
garciay committed
  static GeoNetworkingFactory _f;
garciay's avatar
garciay committed
public:
garciay's avatar
garciay committed
  GeoNetworkingFactory();
  virtual Layer * createLayer(const std::string & type,
			      const std::string & param);
garciay's avatar
garciay committed
};

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

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

GeoNetworkingFactory GeoNetworkingFactory::_f;