BTPCodec.cc 7.96 KB
Newer Older
garciay's avatar
garciay committed
#include <string>
#include <typeinfo>

#include "BTPCodec.hh"
garciay's avatar
garciay committed
#include "loggers.hh"

#include "LibItsBtp_TypesAndValues.hh"
filatov's avatar
filatov committed

garciay's avatar
garciay committed
template <typename T_type> class OPTIONAL;
class TTCN_EncDec;
garciay's avatar
garciay committed
int BTPCodec::encode (const LibItsBtp__TypesAndValues::BtpPacket& msg, OCTETSTRING& data)
filatov's avatar
filatov committed
{
garciay's avatar
garciay committed
  loggers::get_instance().log_msg(">>> BTPCodec::encode: ", (const Base_Type&)msg);
  
  TTCN_EncDec::clear_error();
  TTCN_Buffer encoding_buffer;
  encode_(msg, LibItsBtp__TypesAndValues::BtpPacket_descr_, encoding_buffer);
  data = OCTETSTRING(encoding_buffer.get_len(), encoding_buffer.get_data());
garciay's avatar
garciay committed
  loggers::get_instance().log_msg("<<< BTPCodec::encode: data=", data);
  return 0;
}

int BTPCodec::encode_ (const Base_Type& type, const TTCN_Typedescriptor_t& field_descriptor, TTCN_Buffer& encoding_buffer)
{
  loggers::get_instance().log(">>> BTPCodec::encode_: processing %s/%s", type.get_descriptor()->name, field_descriptor.name);
  loggers::get_instance().log_msg(">>> BTPCodec::encode_: ", type);

  if (dynamic_cast<const Record_Type *>(&type) != NULL) {
    const Record_Type& r = (const Record_Type &)type;
    loggers::get_instance().log("BTPCodec::encode_: processing Record_Type %s", r.get_descriptor()->name);
    for(int i = 0; i < r.get_count(); i++) {
      loggers::get_instance().log("BTPCodec::encode_: processing %s/%s/%s - %d (1 ==> use dynamic_cast<const OPTIONAL<...>) - %d", r.fld_name(i), r.fld_descr(i)->name, r.get_at(i)->get_descriptor()->name, r.get_at(i)->is_optional(), r.get_at(i)->is_present());
      if (r.get_at(i)->is_present()) {
	if (encode_(*r.get_at(i), *r.fld_descr(i), encoding_buffer) == -1) {
	  loggers::get_instance().warning("BTPCodec::encode_: -1 result code returned");
	  return -1;
	}
      }
    } // End of 'for' statement
  } else if (std::string(type.get_descriptor()->name).compare("@LibItsBtp_TypesAndValues.BtpPayload") == 0) {
    const OPTIONAL<LibItsBtp__TypesAndValues::BtpPayload> &v = dynamic_cast<const OPTIONAL<LibItsBtp__TypesAndValues::BtpPayload> &>(type);
    loggers::get_instance().log("BTPCodec::encode_: Payload present: %d", v.is_present());
    if (v.is_present()) {
      if (encode_(static_cast<const LibItsBtp__TypesAndValues::BtpPayload &>(*v.get_opt_value()), LibItsBtp__TypesAndValues::BtpPayload_descr_, encoding_buffer) == -1) {
	loggers::get_instance().warning("BTPCodec::encode_: -1 result code returned");
	return -1;
      }
    }
  } else if (std::string(type.get_descriptor()->name).compare("@LibItsBtp_TypesAndValues.DecodedBtpPayload") == 0) {
    const OPTIONAL<LibItsBtp__TypesAndValues::DecodedBtpPayload> &v = dynamic_cast<const OPTIONAL<LibItsBtp__TypesAndValues::DecodedBtpPayload> &>(type);
    loggers::get_instance().log("BTPCodec::encode_: DecodedBtpPayload present: %d", v.is_present());
    if (v.is_present()) {
      loggers::get_instance().error("BTPCodec::encode_ DecodedBtpPayload shall not be present");
    }
  } else {
    loggers::get_instance().log("BTPCodec::encode_ (else): processing type %s/%s", type.get_descriptor()->name, field_descriptor.name);
    type.encode(field_descriptor, encoding_buffer, TTCN_EncDec::CT_RAW);
  }

  loggers::get_instance().log_to_hexa("<<<BTPCodec::encode_: encoding_buffer=", encoding_buffer);
  return 0;
filatov's avatar
filatov committed
}
garciay's avatar
garciay committed
int BTPCodec::decode (const OCTETSTRING& data, LibItsBtp__TypesAndValues::BtpPacket& msg, Params* params)
filatov's avatar
filatov committed
{
garciay's avatar
garciay committed
  TTCN_EncDec::clear_error();
  TTCN_Buffer decoding_buffer(data);
garciay's avatar
garciay committed
  _params = params;
garciay's avatar
garciay committed
  loggers::get_instance().log_to_hexa(">>> BTPCodec::decode: decoding_buffer=", decoding_buffer);
  decode_(msg, LibItsBtp__TypesAndValues::BtpPacket_descr_, decoding_buffer);
garciay's avatar
garciay committed
  loggers::get_instance().log_msg("<<< BTPCodec::decode: ", (const Base_Type&)msg);
  return 0;
filatov's avatar
filatov committed
}
garciay's avatar
garciay committed

int BTPCodec::decode_ (Base_Type& type, const TTCN_Typedescriptor_t& field_descriptor, TTCN_Buffer& decoding_buffer)
{
  loggers::get_instance().log(">>> BTPCodec::decode_: processing %s/%s (%d,%d,%p)", type.get_descriptor()->name, field_descriptor.name, decoding_buffer.get_len(), decoding_buffer.get_pos(), decoding_buffer.get_read_data());

  if (dynamic_cast<Record_Type *>(&type) != NULL) {
    Record_Type& r = (Record_Type &)type;
    loggers::get_instance().log("BTPCodec::decode_: processing Record_Type %s", r.get_descriptor()->name);
    for(int i = 0; i < r.get_count(); i++) {
      loggers::get_instance().log("BTPCodec::decode_: processing %s/%s/%s", r.fld_name(i), r.fld_descr(i)->name, r.get_at(i)->get_descriptor()->name);
      if (decode_(*dynamic_cast<Base_Type *>(r.get_at(i)), *(r.fld_descr(i)), decoding_buffer) == -1) {
	return -1;
      }
    } // End of 'for' statement
  } else {
    loggers::get_instance().log("BTPCodec::decode_ (else): processing type %s/%s - optional:%d", type.get_descriptor()->name, field_descriptor.name, type.is_optional());
    if (std::string(field_descriptor.name).compare("@LibItsBtp_TypesAndValues.BtpPacket.payload") == 0) {
      if (decoding_buffer.get_len() == decoding_buffer.get_pos()) { // No payload, set it to omit
	loggers::get_instance().log("BTPCodec::decode_: Set OPTIONAL<BtpPayload> to omit");
garciay's avatar
garciay committed
	dynamic_cast<OPTIONAL<LibItsBtp__TypesAndValues::BtpPayload> &>(type).set_to_omit();
      } else {
	LibItsBtp__TypesAndValues::BtpPayload p;
	decode_(p, LibItsBtp__TypesAndValues::BtpPayload_descr_, decoding_buffer);
	loggers::get_instance().log_msg("BTPCodec::decode_: payload= ", p);
	if (p.is_bound()) {
	  dynamic_cast<OPTIONAL<LibItsBtp__TypesAndValues::BtpPayload> &>(type) = OPTIONAL<LibItsBtp__TypesAndValues::BtpPayload>(p);
	  loggers::get_instance().log_msg("BTPCodec::decode_: Set OPTIONAL to ", type);
	} else {
	  loggers::get_instance().log("BTPCodec::decode_: Set OPTIONAL to omit");
	  dynamic_cast<OPTIONAL<LibItsBtp__TypesAndValues::BtpPayload> &>(type).set_to_omit();
	}
garciay's avatar
garciay committed
      }
    } else if (std::string(field_descriptor.name).compare("@LibItsBtp_TypesAndValues.BtpPacket.header") == 0) {
      loggers::get_instance().log("BTPCodec::decode_: _type = %d", _type);
      LibItsBtp__TypesAndValues::BtpHeader& b = static_cast<LibItsBtp__TypesAndValues::BtpHeader&>(type);
      if (_type == BTPCodec::btpA) {
	decode_(b.btpAHeader(), *b.btpAHeader().get_descriptor(), decoding_buffer);
garciay's avatar
garciay committed
	if (_params != NULL) {
	  (*_params)[Params::btp_type] = std::string("btpA");
	  (*_params)[Params::btp_destination_port] = static_cast<int>(b.btpAHeader().destinationPort());
	}
garciay's avatar
garciay committed
      } else if (_type == BTPCodec::btpB) {
	decode_(b.btpBHeader(), *b.btpBHeader().get_descriptor(), decoding_buffer);
garciay's avatar
garciay committed
	if (_params != NULL) {
	  (*_params)[Params::btp_type] = std::string("btpB");
	  (*_params)[Params::btp_destination_port] = static_cast<int>(b.btpBHeader().destinationPort());
	  (*_params)[Params::btp_info] = static_cast<int>(b.btpBHeader().destinationPortInfo());
	}
garciay's avatar
garciay committed
      } else {
	loggers::get_instance().error("BTPCodec::decode_: Invalid BTP type"); // Cannot be reached
      }
      loggers::get_instance().log_msg("BTPCodec::decode_: BtpHeader: ", type);
    } else if (std::string(field_descriptor.name).compare("@LibItsBtp_TypesAndValues.BtpPayload.decodedPayload") == 0) {
      dynamic_cast<OPTIONAL<LibItsBtp__TypesAndValues::DecodedBtpPayload> &>(type).set_to_omit();
    } else if (std::string(field_descriptor.name).compare("@LibItsBtp_TypesAndValues.BtpPayload.rawPayload") == 0) {
      // Decode BtpRawPayload data structure
      OCTETSTRING os(decoding_buffer.get_len() - decoding_buffer.get_pos(), decoding_buffer.get_data() + decoding_buffer.get_pos());
      loggers::get_instance().log_msg("BTPCodec::decode_: s=", os);
garciay's avatar
garciay committed
      if (_params != NULL) {
	(*_params)[Params::btp_payload] = std::string(static_cast<const char *>(oct2str(os)));
garciay's avatar
garciay committed
      }
      dynamic_cast<OCTETSTRING &>(type) = os;
garciay's avatar
garciay committed
    } else {
      type.decode(field_descriptor, decoding_buffer, TTCN_EncDec::CT_RAW);
    }
  }

  loggers::get_instance().log_msg("<<< BTPCodec::decode_: ", type);
  loggers::get_instance().log_to_hexa("<<< BTPCodec::decode_: decoding_buffer=", decoding_buffer);

  return 0;
}