// This Test Port skeleton source file was generated by the
// TTCN-3 Compiler of the TTCN-3 Test Executor version CRL 113 200/5 R3A
// for U-ERICSSON\ethgry (ethgry@HU00078339) on Sat Aug 15 22:33:04 2015

// Copyright Ericsson Telecom AB 2000-2014

// You may modify this file. Complete the body of empty functions and
// add your member functions here.

#include "GeoNetworkingPort.hh"
#include "GeoNetworkingLayer.hh"
#include "loggers.hh"

namespace LibItsGeoNetworking__TestSystem {

  GeoNetworkingPort::GeoNetworkingPort(const char *par_port_name)
    : GeoNetworkingPort_BASE(par_port_name), _cfg_params(), _layer_params(), _layer(NULL), _time_key("GeoNetworkingPort::outgoing_send")
  {

  }

  GeoNetworkingPort::~GeoNetworkingPort()
  {
    if (_layer != NULL) {
      delete _layer;
    }
  }

  void GeoNetworkingPort::set_parameter(const char * parameter_name, const char * parameter_value)
  {
    loggers::get_instance().log("GeoNetworkingPort::set_parameter: %s=%s", parameter_name, parameter_value);
    _cfg_params.insert(std::pair<std::string, std::string>(std::string(parameter_name), std::string(parameter_value)));
  }

  /*void GeoNetworkingPort::Handle_Fd_Event(int fd, boolean is_readable,
    boolean is_writable, boolean is_error) {}*/

  void GeoNetworkingPort::Handle_Fd_Event_Error(int /*fd*/)
  {

  }

  void GeoNetworkingPort::Handle_Fd_Event_Writable(int /*fd*/)
  {

  }

  void GeoNetworkingPort::Handle_Fd_Event_Readable(int /*fd*/)
  {

  }

  /*void GeoNetworkingPort::Handle_Timeout(double time_since_last_call) {}*/

  void GeoNetworkingPort::user_map(const char * system_port)
  {
    loggers::get_instance().log(">>> GeoNetworkingPort::user_map: %s", system_port);
    // Build layer stack
    Params::iterator it = _cfg_params.find(std::string("params"));
    if (it != _cfg_params.end()) {
      //loggers::get_instance().log("GeoNetworkingPort::user_map: %s", it->second.c_str());
      _layer = LayerStackBuilder::GetInstance()->createLayerStack(it->second.c_str());
    if (static_cast<GeoNetworkingLayer *>(_layer) == NULL) {
      loggers::get_instance().error("GeoNetworkingPort::user_map: Invalid stack configuration: %s", it->second.c_str());
    }
      static_cast<GeoNetworkingLayer *>(_layer)->addUpperPort(this);
    } else {
      loggers::get_instance().error("GeoNetworkingPort::user_map: No layers defined in configuration file");
    }

    //static_cast<GeoNetworkingLayer *>(_layer)->start_beaconing();
  }

  void GeoNetworkingPort::user_unmap(const char * system_port)
  {
    loggers::get_instance().log(">>> GeoNetworkingPort::user_unmap: %s", system_port);
    if (_layer != NULL) {
      delete _layer;
      _layer = NULL;
    }
  }

  void GeoNetworkingPort::user_start()
  {

  }

  void GeoNetworkingPort::user_stop()
  {

  }

  void GeoNetworkingPort::outgoing_send(const GeoNetworkingReq& send_par)
  {
    loggers::get_instance().log_msg(">>> GeoNetworkingPort::outgoing_send: payload=", send_par);
    
    float duration;
    loggers::get_instance().set_start_time(_time_key);
    static_cast<GeoNetworkingLayer *>(_layer)->sendMsg(send_par, _layer_params);
    loggers::get_instance().set_stop_time(_time_key, duration);
  }

  void GeoNetworkingPort::receiveMsg (const LibItsGeoNetworking__TestSystem::GeoNetworkingInd& p_ind, const Params& p_params) {
    loggers::get_instance().log_msg(">>> GeoNetworkingPort::receive_msg: ", p_ind);
    // Sanity check
    if (!p_ind.is_bound()) {
      return;
    }

    // Update location table
    if (p_ind.msgIn().basicHeader().version() == 0) { // Non secured mode
      const LibItsGeoNetworking__TypesAndValues::GnNonSecuredPacket& p = p_ind.msgIn().gnPacket().packet();
      const LibItsGeoNetworking__TypesAndValues::HeaderTST& htst = p.commonHeader().headerTST();
      if (htst.ischosen(LibItsGeoNetworking__TypesAndValues::HeaderTST::ALT_lsHdr)) { // Location service
	const LibItsGeoNetworking__TypesAndValues::LsHeaderType& ls = htst.lsHdr();
	if (ls.headerSubType() == LibItsGeoNetworking__TypesAndValues::HeaderSubTypeLs::e__lsReply) { // Update the Test System Location Table
	  const LibItsGeoNetworking__TypesAndValues::ExtendedHeader& ex = p.extendedHeader();
	  if (ex.ischosen(LibItsGeoNetworking__TypesAndValues::ExtendedHeader::ALT_lsReplyHeader)) {
	    const LibItsGeoNetworking__TypesAndValues::LongPosVector& sopv = ex.lsReplyHeader().srcPosVector();
	    loggers::get_instance().user_msg("GeoNetworkingPort::receive_msg: Update LocationTable: ", sopv);	    
	    // TODO Add a LocationTable class to manage it
	  } // else, discard packet
	} // else, discard packet
      } // else, discard packet
    } // TODO else security mode

    // TODO Add beacon filter for StartPassBeaconing/Stop
    
    incoming_message(p_ind);
  }

} /* end of namespace */

