Commit f5888f02 authored by YannGarcia's avatar YannGarcia
Browse files

Re-organize ccsrc/Protocols/BTP; Bug fixed in Layers

parent e0bda735
Loading
Loading
Loading
Loading

ccsrc/Protocols/BTP/btp_layer.cc

deleted100644 → 0
+0 −114
Original line number Diff line number Diff line
#include "btp_types.hh"

#include "btp_layer_factory.hh"

#include "loggers.hh"

#include "converter.hh"

btp_layer::btp_layer(const std::string &p_type, const std::string &param)
  : t_layer<LibItsBtp__TestSystem::BtpPort>(p_type), _params(), _codec(), _device_mode{true} {
  loggers::get_instance().log(">>> btp_layer::btp_layer: %s, %s", to_string().c_str(), param.c_str());
  // Setup parameters
  params_its::convert(_params, param);

  // Sanity check
  params_its::const_iterator it = _params.find(params_its::btp_type);
  if (it == _params.cend()) {
    _params[params_its::btp_type] = std::string("btpB");
  }
  it = _params.find(params_its::btp_destination_port);
  if (it == _params.cend()) {
    _params[params_its::btp_destination_port] = std::to_string(2001);
  }
  it = _params.find(params_its::btp_info);
  if (it == _params.cend()) {
    _params[params_its::btp_info] = std::to_string(0);
  }
  it = _params.find(params_its::device_mode);
  if (it != _params.cend()) {
    _device_mode = (1 == converter::get_instance().string_to_int(it->second));
  }
}

void btp_layer::sendMsg(const LibItsBtp__TypesAndValues::BtpReq &p, params &p_params) {
  loggers::get_instance().log(">>> btp_layer::sendMsg");
  // params.log();

  // Encode BTP PDU
  OCTETSTRING data;
  _codec.encode(p.msgOut(), data);
  send_data(data, p_params);
}

void btp_layer::send_data(OCTETSTRING &data, params &p_params) {
  loggers::get_instance().log_msg(">>> btp_layer::send_data: ", data);
  //p_params.log();  // TODO To be removed
  //_params.log(); // TODO To be removed

  params_its &params = static_cast<params_its&>(p_params);

  if (_device_mode) {
    LibItsBtp__TypesAndValues::BtpHeader header;
    std::string                          btp_type;
    params_its::const_iterator               it = params.find(params_its::next_header);
    if (it != params.cend()) {
      btp_type = it->second;
    } else {
      btp_type = _params[params_its::btp_type];
    }
    loggers::get_instance().log("btp_layer::send_data: btp_type=%s", btp_type.c_str());
    int btp_destination_port = std::stoi(_params[params_its::btp_destination_port]); // Default value
    it                       = params.find(params_its::btp_destination_port);
    if (it != params.cend()) {
      btp_destination_port = std::stoi(params[params_its::btp_destination_port]); // Overwritting default value
    }
    loggers::get_instance().log("btp_layer::send_data: btp_destination_port=%d", btp_destination_port);
    if (btp_type.compare("btpA") == 0) {
      header.btpAHeader() = LibItsBtp__TypesAndValues::BtpAHeader(btp_destination_port, std::stoi(_params[params_its::btp_info]));
    } else {
      header.btpBHeader() = LibItsBtp__TypesAndValues::BtpBHeader(btp_destination_port, std::stoi(_params[params_its::btp_info]));
    }
    LibItsBtp__TypesAndValues::BtpPacket p(header, data);
    loggers::get_instance().log_msg("btp_layer::send_data: ", p);

    // Encode BTP PDU
    OCTETSTRING os;
    _codec.encode(p, os);
    data = os;
  }

  send_to_all_layers(data, params);
}

void btp_layer::receive_data(OCTETSTRING &data, params &p_params) {
  loggers::get_instance().log_msg(">>> btp_layer::receive_data: ", data);
  //p_params.log();

  // Decode the payload
  params_its &params = static_cast<params_its&>(p_params);
  LibItsBtp__TypesAndValues::BtpInd p;
  loggers::get_instance().log("btp_layer::receive_data: Looking for %s", params_its::gn_next_header.c_str());
  params_its::const_iterator it = params.find(params_its::gn_next_header);
  if (it != params.cend()) {
    loggers::get_instance().log("btp_layer::receive_data: Call set_btp_type with %s/%d", it->second.c_str(),
                                (it->second.compare("2") == 0) ? btp_codec::btpB : btp_codec::btpA);
    _codec.set_btp_type((it->second.compare("2") == 0) ? btp_codec::btpB : btp_codec::btpA);
  }
  _codec.decode(data, p.msgIn(), &params);

  // Pass the BTP raw payload to the upper layers if any
  it = params.find(params_its::btp_payload);
  if (it != params.cend()) {
    loggers::get_instance().log("btp_layer::receive_data: btp_payload=%s", it->second.c_str());
    OCTETSTRING os(str2oct(CHARSTRING(it->second.c_str())));
    receive_to_all_layers(os, params);
  } else {
    loggers::get_instance().warning("btp_layer::receive_data: No payload to pass to upper layers");
  }
  // Pass it to the ports if any
  // params.log();
  to_all_upper_ports(p, p_params);
}

btp_layer_factory btp_layer_factory::_f;

ccsrc/Protocols/BTP/btp_layer.hh

deleted100644 → 0
+0 −69
Original line number Diff line number Diff line
/*!
 * \file      btp_layer.hh
 * \brief     Header file for ITS BTP protocol layer.
 * \author    ETSI STF525
 * \copyright ETSI Copyright Notification
 *            No part may be reproduced except as authorized by written permission.
 *            The copyright and the foregoing restriction extend to reproduction in all media.
 *            All rights reserved.
 * \version   0.1
 */
#pragma once

#include "btp_codec.hh"
#include "t_layer.hh"

namespace LibItsBtp__TestSystem {
  class BtpPort; //! Forward declaration of TITAN class
  class BtpReq;  //! Forward declaration of TITAN class
  class BtpInd;  //! Forward declaration of TITAN class
} // namespace LibItsBtp__TestSystem

/*!
 * \class btp_layer
 * \brief  This class provides description of ITS BTP protocol layer
 */
class btp_layer : public t_layer<LibItsBtp__TestSystem::BtpPort> {
  params_its _params;      //! Layer parameters
  btp_codec _codec;       //! BTP codec
  bool      _device_mode; //! Set to true if the BTP layer shall encapsulate the upper layer PDU
public:                   //! \publicsection
  /*!
   * \brief Default constructor
   *        Create a new instance of the btp_layer class
   */
  explicit btp_layer() : t_layer<LibItsBtp__TestSystem::BtpPort>(), _params(), _codec(), _device_mode{false} {};
  /*!
   * \brief Specialised constructor
   *        Create a new instance of the btp_layer class
   * \param[in] p_type \todo
   * \param[in] p_param \todo
   */
  btp_layer(const std::string &p_type, const std::string &param);
  virtual ~btp_layer(){};

  /*!
   * \fn void sendMsg(const LibItsBtp__TestSystem::BtpReq& p_btp_req, params_its& p_param);
   * \brief Send BTP message to the lower layers
   * \param[in] p_btp_req The BTP message to be sent
   * \param[in] p_params Some parameters to overwrite default value of the lower layers parameters
   */
  void sendMsg(const LibItsBtp__TypesAndValues::BtpReq &p_btp_req, params &p_params);

  /*!
   * \virtual
   * \fn void send_data(OCTETSTRING& data, params& params);
   * \brief Send bytes formated data to the lower layers
   * \param[in] p_data The data to be sent
   * \param[in] p_params Some parameters to overwrite default value of the lower layers parameters
   */
  virtual void send_data(OCTETSTRING &data, params& p_params);
  /*!
   * \virtual
   * \fn void receive_data(OCTETSTRING& data, params& params);
   * \brief Receive bytes formated data from the lower layers
   * \param[in] p_data The bytes formated data received
   * \param[in] p_params Some lower layers parameters values when data was received
   */
  virtual void receive_data(OCTETSTRING &data, params& p_params);
}; // End of class btp_layer
+0 −41
Original line number Diff line number Diff line
/*!
 * \file      btp_layer_factory.hh
 * \brief     Header file for ITS BTP protocol layer factory.
 * \author    ETSI STF525
 * \copyright ETSI Copyright Notification
 *            No part may be reproduced except as authorized by written permission.
 *            The copyright and the foregoing restriction extend to reproduction in all media.
 *            All rights reserved.
 * \version   0.1
 */
#pragma once

#include "layer_stack_builder.hh"

#include "btp_layer.hh"

/*!
 * \class cam_layer_factory
 * \brief  This class provides a factory class to create a btp_layer class instance
 */
class btp_layer_factory : public layer_factory {
  static btp_layer_factory _f; //! Reference to the unique instance of this class
public:                        //! \publicsection
  /*!
   * \brief Default constructor
   *        Create a new instance of the btp_layer_factory class
   * \remark The BTP layer identifier is BTP
   */
  btp_layer_factory() {
    // Register factory
    layer_stack_builder::register_layer_factory("BTP", this);
  };
  /*!
   * \fn layer* create_layer(const std::string & type, const std::string & param);
   * \brief  Create the layers stack based on the provided layers stack description
   * \param[in] p_type The provided layers stack description
   * \param[in] p_params Optional parameters
   * \return 0 on success, -1 otherwise
   */
  inline virtual layer *create_layer(const std::string &p_type, const std::string &p_param) { return new btp_layer(p_type, p_param); };
}; // End of class btp_layer_factory
+2 −2
Original line number Diff line number Diff line
sources  := btp_codec.cc btp_layer.cc
sources := btp_codec.cc
includes := .
+2 −2
Original line number Diff line number Diff line
@@ -38,6 +38,6 @@ public:
  void sendMsg(const LibItsIvim__TypesAndValues::UtIvimUpdate &send_par, params_its& p_params);
  void sendMsg(const LibItsIvim__TypesAndValues::UtIvimTermination &send_par, params_its& p_params);

  virtual void send_data(OCTETSTRING &data, params_its& p_params);
  virtual void receive_data(OCTETSTRING &data, params_its& info);
  virtual void send_data(OCTETSTRING &data, params& p_params);
  virtual void receive_data(OCTETSTRING &data, params& info);
}; // End of class uppertester_ivim_layer