Commit 53689860 authored by garciay's avatar garciay
Browse files

UpperTester/CAM validation

parent 2eff1f09
Loading
Loading
Loading
Loading
+71 −0
Original line number Diff line number Diff line
#include "LibItsCam_EncdecDeclarations.hh"

#include "CAMCodec.hh"
#include "UpperTesterCamCodec.hh"
#include "loggers.hh"

namespace LibItsCam__EncdecDeclarations {
@@ -102,5 +103,75 @@ namespace LibItsCam__EncdecDeclarations {
    return 0;
  }

  BITSTRING fx__enc__UtCamInitialize (const LibItsCam__TypesAndValues::UtCamInitialize& p_utCamInitialize)
  {
    loggers::get_instance().log_msg(">>> fx__enc__UtCamInitialize: ", p_utCamInitialize);
    
    UpperTesterCamCodec codec;
    OCTETSTRING os;
    codec.encode(p_utCamInitialize, os);
    return oct2bit(os);
  }

  BITSTRING fx__enc__UtCamChangePosition (const LibItsCam__TypesAndValues::UtCamChangePosition& p_utCamChangePosition)
  {
    loggers::get_instance().log_msg(">>> fx__enc__UtCamChangePosition: ", p_utCamChangePosition);
    
    UpperTesterCamCodec codec;
    OCTETSTRING os;
    codec.encode(p_utCamChangePosition, os);
    return oct2bit(os);
  }

  BITSTRING fx__enc__UtCamTrigger (const LibItsCam__TypesAndValues::UtCamTrigger& p_utCamTrigger)
  {
    loggers::get_instance().log_msg(">>> fx__enc__UtCamTrigger: ", p_utCamTrigger);
    
    UpperTesterCamCodec codec;
    OCTETSTRING os;
    codec.encode(p_utCamTrigger, os);
    return oct2bit(os);
  }

  /****************************************************
   * @desc    External function to decode a UtCamResults type
   * @param   value to encode
   * @return  encoded value
   ****************************************************/
  INTEGER fx__dec__UtCamResults(BITSTRING& b, LibItsCam__TypesAndValues::UtCamResults& p)
  {
    loggers::get_instance().log(">>> fx__dec__UtCamResults");

    UpperTesterCamCodec codec;
    OCTETSTRING is = bit2oct(b);
    std::unique_ptr<Base_Type> ptr = codec.decode(is);
    if (ptr == nullptr) {
      loggers::get_instance().warning("fx__dec__UtCamResults: -1 result code was returned");
      return -1;
    }
    p = static_cast<LibItsCam__TypesAndValues::UtCamResults&>(*ptr);
    return 0;
  }

  /****************************************************
   * @desc    External function to decode a UtCamEventInd type
   * @param   value to encode
   * @return  encoded value
   ****************************************************/
  INTEGER fx__dec__UtCamEventInd(BITSTRING& b, LibItsCam__TypesAndValues::UtCamEventInd& p)
  {
    loggers::get_instance().log(">>> fx__dec__UtCamEventInd");

    UpperTesterCamCodec codec;
    OCTETSTRING is = bit2oct(b);
    std::unique_ptr<Base_Type> ptr = codec.decode(is);
    if (ptr == nullptr) {
      loggers::get_instance().warning("fx__dec__UtCamEventInd: -1 result code was returned");
      return -1;
    }
    p = static_cast<LibItsCam__TypesAndValues::UtCamEventInd&>(*ptr);
    return 0;
  }

} //end namespace
+237 −0
Original line number Diff line number Diff line
#include <string>
//#include <typeinfo>

#include "UpperTesterCamCodec.hh"
#include "loggers.hh"

#include "UpperTesterTypes.hh"

#include "CAMCodec.hh"

template <typename T_type> class OPTIONAL;
class TTCN_EncDec;

int UpperTesterCamCodec::encode (const Record_Type& msg, OCTETSTRING& data)
{
  loggers::get_instance().log_msg(">>> UpperTesterCamCodec::encode: ", (const Record_Type&)msg);
  
  TTCN_EncDec::clear_error();
  TTCN_Buffer encoding_buffer;
  if (std::string(msg.get_descriptor()->name).compare("@LibItsCam_TypesAndValues.UtCamInitialize") == 0) {
    encoding_buffer.put_c(static_cast<unsigned char>(UpperTesterCamCodec::c_utCamInitialize));
  } else if (std::string(msg.get_descriptor()->name).compare("@LibItsCam_TypesAndValues.UtCamChangePosition") == 0) {
    encoding_buffer.put_c(static_cast<unsigned char>(UpperTesterCamCodec::c_utCamChangePosition));
  } else { // Error
    data = OCTETSTRING();
    loggers::get_instance().log("<<< UpperTesterCamCodec::encode: Return -1");
    return -1;
  }
  encode_(msg, *msg.get_descriptor(), encoding_buffer);
  data = OCTETSTRING(encoding_buffer.get_len(), encoding_buffer.get_data());
  loggers::get_instance().log_msg("<<< UpperTesterCamCodec::encode: data=", data);
  return 0;
}

int UpperTesterCamCodec::encode (const LibItsCam__TypesAndValues::UtCamTrigger& msg, OCTETSTRING& data)
{
  loggers::get_instance().log_msg(">>> UpperTesterCamCodec::encode: ", (const Record_Type&)msg);
  
  TTCN_EncDec::clear_error();
  TTCN_Buffer encoding_buffer;
  const Record_Type *type;
  const LibItsCam__TypesAndValues::UtCamTrigger &u = static_cast<const LibItsCam__TypesAndValues::UtCamTrigger &>(msg);
  if (u.changeCurvature().is_present()) {
    encoding_buffer.put_c(static_cast<unsigned char>(UpperTesterCamCodec::c_utCamChangeCurvature));
    encoding_buffer.put_string(int2oct(u.changeCurvature(), 2));
  } else if (u.changeSpeed().is_present()) {
    encoding_buffer.put_c(static_cast<unsigned char>(UpperTesterCamCodec::c_utCamChangeSpeed));
    encoding_buffer.put_string(int2oct(u.changeSpeed(), 2));
  } else if (u.changeHeading().is_present()) {
    encoding_buffer.put_c(static_cast<unsigned char>(UpperTesterCamCodec::c_utCamChangeHeading));
    encoding_buffer.put_string(int2oct(u.changeHeading(), 2));
  } else if (u.setDriveDirection().is_present()) {
    encoding_buffer.put_c(static_cast<unsigned char>(UpperTesterCamCodec::c_utCamSetDriveDirection));
    encoding_buffer.put_c((unsigned char)static_cast<const Enum_Type&>(u.setDriveDirection()).as_int());
  } else if (u.changeYawRate().is_present()) {
    encoding_buffer.put_c(static_cast<unsigned char>(UpperTesterCamCodec::c_utCamChangeYawRate));
    encoding_buffer.put_string(int2oct(u.changeYawRate(), 2));
  }
    // TODO To be continued
  else { // Error
    data = OCTETSTRING();
    loggers::get_instance().log("<<< UpperTesterCamCodec::encode: Return -1");
    return -1;
  }
  //encode_(*type, *type->get_descriptor(), encoding_buffer);
  data = OCTETSTRING(encoding_buffer.get_len(), encoding_buffer.get_data());
  loggers::get_instance().log_msg("<<< UpperTesterCamCodec::encode: data=", data);
  return 0;
}

int UpperTesterCamCodec::encode_ (const Base_Type& type, const TTCN_Typedescriptor_t& field_descriptor, TTCN_Buffer& encoding_buffer)
{
  loggers::get_instance().log(">>> UpperTesterCamCodec::encode_: processing %s/%s/%p", type.get_descriptor()->name, field_descriptor.name, dynamic_cast<const Record_Type*>(&type));
  loggers::get_instance().log_msg(">>> UpperTesterCamCodec::encode_: ", type);
  
  if (dynamic_cast<const Record_Type*>(&type) != NULL) {
    const Record_Type& r = (const Record_Type &)type;
    loggers::get_instance().log("UpperTesterCamCodec::encode_: processing Record_Type %s", r.get_descriptor()->name);
    for(int i = 0; i < r.get_count(); i++) {
      loggers::get_instance().log("UpperTesterCamCodec::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("UpperTesterCamCodec::encode_: -1 result code returned");
          return -1;
        }
      } else if (std::string(r.fld_name(i)).compare("alacarte") == 0) {
        // Add empty field length
        loggers::get_instance().log("UpperTesterCamCodec::encode_: alacarte is missing, add 0x00");
        encoding_buffer.put_c(0x00);
      }
    } // End of 'for' statement
  } else {
    std::string s(field_descriptor.name);
    loggers::get_instance().log("UpperTesterCamCodec::encode_: field to process %s", s.c_str());
    if (
        (s.rfind(".shape") != string::npos) ||
        (s.rfind(".relevanceDistance") != string::npos) ||
        (s.rfind(".relevanceTrafficDirection") != string::npos)
        ) {
      encoding_buffer.put_c((unsigned char)static_cast<const Enum_Type&>(type).as_int());
    } else if (s.rfind(".payload") != string::npos) {
      const OCTETSTRING& os = static_cast<const OCTETSTRING&>(type);
      const unsigned char s[] = { (unsigned char)((os.lengthof() & 0x0000FF00) >> 8), (unsigned char)os.lengthof() };
      encoding_buffer.put_s(2, s);
      if (os.lengthof() != 0) {
        encoding_buffer.put_string(os);
      }
    } else if (s.rfind(".detectionTime") != string::npos) {
      unsigned long long llu = static_cast<const INTEGER&>(type).get_long_long_val();
      loggers::get_instance().log("UpperTesterCamCodec::encode_ : detectionTime=%llu", llu);
      std::vector<unsigned char> v;
      for (int i = 0; i < 6; i++) {
        v.insert(v.begin(), static_cast<unsigned char>(llu));
        llu >>= 8;
      } // End of 'for' statement
      OCTETSTRING os(v.size(), v.data());
      loggers::get_instance().log_msg("UpperTesterCamCodec::encode_: timeDetection=", os);
      encoding_buffer.put_string(os);
    } else if (
               (s.rfind(".validityDuration") != string::npos) ||
               (s.rfind(".repetitionDuration") != string::npos)
               ) {
      if (type.is_present()) {
        const OPTIONAL<INTEGER> &o = dynamic_cast<const OPTIONAL<INTEGER> &>(type);
        const INTEGER& i = static_cast<const INTEGER&>(*o.get_opt_value());
        loggers::get_instance().log_msg("UpperTesterCamCodec::encode_: i=", i);
        encoding_buffer.put_string(int2oct(i, 3));
      }
    } else if (
               (s.rfind(".informationQuality") != string::npos) ||
               (s.rfind(".causeCode") != string::npos) ||
               (s.rfind(".subCauseCode") != string::npos)
               ) {
      const INTEGER& i = static_cast<const INTEGER&>(type);
      encoding_buffer.put_string(int2oct(i, 1));
    } else if (
               (s.rfind(".linkedCause") != string::npos) ||
               (s.rfind(".eventHistory") != string::npos)
               ) {
      // Skip them
    } else if (
               (s.rfind(".transmissionInterval") != string::npos) ||
               (s.rfind(".repetitionInterval") != string::npos)
               ) {
      if (type.is_present()) {
        const OPTIONAL<INTEGER> &o = dynamic_cast<const OPTIONAL<INTEGER> &>(type);
        const INTEGER& i = static_cast<const INTEGER&>(*o.get_opt_value());
        encoding_buffer.put_string(int2oct(i, 2));
      }
    } else if (s.rfind(".originatingStationID") != string::npos) {
      const INTEGER& i = static_cast<const INTEGER&>(type);
      encoding_buffer.put_string(int2oct(i, 4));
    } else if (s.rfind(".sequenceNumber") != string::npos) {
      const INTEGER& i = static_cast<const INTEGER&>(type);
      encoding_buffer.put_string(int2oct(i, 2));
    } else if (s.rfind(".AlacarteContainer") != string::npos) {
      const OPTIONAL<OCTETSTRING> &o = dynamic_cast<const OPTIONAL<OCTETSTRING> &>(type);
      const OCTETSTRING& os = static_cast<const OCTETSTRING&>(*o.get_opt_value());
      encoding_buffer.put_string(int2oct(os.lengthof(), 2));
      encoding_buffer.put_string(os);
    } else {
      loggers::get_instance().log("UpperTesterCamCodec::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("<<<UpperTesterCamCodec::encode_: encoding_buffer=", encoding_buffer);
  return 0;
}

int UpperTesterCamCodec::decode (const OCTETSTRING& data, Record_Type& msg, Params* params)
{
  TTCN_EncDec::clear_error();
  TTCN_Buffer decoding_buffer(data);
  _params = params;
  
  loggers::get_instance().log_to_hexa(">>> UpperTesterCamCodec::decode: decoding_buffer=", decoding_buffer);
  //decode_(msg, *msg.get_descriptor(), decoding_buffer);
  
  loggers::get_instance().log_msg("<<< UpperTesterCamCodec::decode: ", (const Record_Type&)msg);
  return 0;
}

std::unique_ptr<Base_Type> UpperTesterCamCodec::decode (const OCTETSTRING& data, Params* params) {

  std::unique_ptr<Base_Type> result;
  
  const unsigned char* ptr = static_cast<const unsigned char*>(data);
  if (*ptr != UpperTesterCamCodec::c_utCamEventInd) {
    LibItsCam__TypesAndValues::UtCamResults res;
    decode(data, res, params);
    result.reset((Base_Type *)res.clone());
  } else {
    LibItsCam__TypesAndValues::UtCamEventInd ind;
    decode(data, ind, params);
    result.reset((Base_Type *)ind.clone());
  }

  return result;
}

int UpperTesterCamCodec::decode (const OCTETSTRING& data, LibItsCam__TypesAndValues::UtCamResults& msg, Params* params)
{
  loggers::get_instance().log_to_hexa(">>> UpperTesterCamCodec::decode: decoding_buffer=", data);

  const unsigned char* ptr = static_cast<const unsigned char*>(data);
  if (*ptr == UpperTesterCamCodec::c_utCamInitializeResult) {
    msg.utCamInitializeResult() = BOOLEAN(*(ptr + 1) == 0x01);
  } else if (*ptr == UpperTesterCamCodec::c_utCamChangePositionResult) {
    msg.utCamChangePositionResult() = BOOLEAN(*(ptr + 1) == 0x01);
  } else if (*ptr == UpperTesterCamCodec::c_utCamTriggerResult) {
    msg.utCamTriggerResult() = BOOLEAN(*(ptr + 1) == 0x01);
  } else {
    TTCN_warning("UpperTesterCamCodec::decode: Unsupported result");
  }
  
  loggers::get_instance().log_msg("<<< UpperTesterCamCodec::decode", msg);
  return 0;
}

int UpperTesterCamCodec::decode (const OCTETSTRING& data, LibItsCam__TypesAndValues::UtCamEventInd& msg, Params* params)
{
  loggers::get_instance().log_to_hexa(">>> UpperTesterCamCodec::decode: decoding_buffer=", data);

  const unsigned char* ptr = static_cast<const unsigned char*>(data) + 1;
  OCTETSTRING os(2, ptr);
  ptr += 2;
  unsigned int length = oct2int(os);
  loggers::get_instance().log("UpperTesterCamCodec::decode: CAM message length=%d", length);
  os = OCTETSTRING(length, ptr);
  loggers::get_instance().log_to_hexa("UpperTesterCamCodec::decode: CAM message=", os);
  CAMCodec codec;
  codec.decode(os, msg.camMsg(), params);
  
  loggers::get_instance().log_msg("<<< UpperTesterCamCodec::decode", msg);
  return 0;
}
+52 −0
Original line number Diff line number Diff line
#ifndef UpperTesterCamCODEC_H
#define UpperTesterCamCODEC_H

#include "Codec.hh"
#include "loggers.hh" // TODO To be removed
#include "Params.hh"

class Base_Type;
class Record_Type;
class TTCN_Typedescriptor_t;
class TTCN_Buffer;

namespace LibItsCam__TypesAndValues {
  class UtCamTrigger;
  class UtCamResults;
  class UtCamEventInd;
}

class UpperTesterCamCodec: public Codec <
  Record_Type,
  Record_Type>
{
  const unsigned char c_utCamInitialize = 0x00;
  const unsigned char c_utCamInitializeResult = 0x01;
  const unsigned char c_utCamChangePosition = 0x02;
  const unsigned char c_utCamChangePositionResult = 0x03;
  const unsigned char c_utCamChangeCurvature = 0x30;
  const unsigned char c_utCamChangeSpeed = 0x31;
  const unsigned char c_utCamSetAccelerationStatus = 0x32;
  const unsigned char c_utCamSetExteriorLightsStatus = 0x33;
  const unsigned char c_utCamChangeHeading = 0x34;
  const unsigned char c_utCamSetDriveDirection = 0x35;
  const unsigned char c_utCamChangeYawRate = 0x36;
  const unsigned char c_utCamTriggerResult = 0x21;
  const unsigned char c_utCamEventInd = 0x23;

  int encode_ (const Base_Type& type, const TTCN_Typedescriptor_t& field_descriptor, TTCN_Buffer& encoding_buffer);

public:
  UpperTesterCamCodec() : Codec<Record_Type, Record_Type>() { loggers::get_instance().log("UpperTesterCamCodec::UpperTesterCamCodec(D)"); };
  virtual ~UpperTesterCamCodec() { loggers::get_instance().log("UpperTesterCamCodec::~UpperTesterCamCodec"); };

  virtual int encode (const Record_Type&, OCTETSTRING& data);
  int encode (const LibItsCam__TypesAndValues::UtCamTrigger&, OCTETSTRING& data);
  virtual int decode (const OCTETSTRING& data, Record_Type&, Params* params = NULL);
  std::unique_ptr<Base_Type> decode (const OCTETSTRING& data, Params* params = NULL);
  
private:
  int decode (const OCTETSTRING& data, LibItsCam__TypesAndValues::UtCamResults& msg, Params* params = NULL);
  int decode (const OCTETSTRING& data, LibItsCam__TypesAndValues::UtCamEventInd& msg, Params* params = NULL);
};
#endif
+1 −1
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ public:

  virtual int encode (const Record_Type&, OCTETSTRING& data);
  virtual int decode (const OCTETSTRING& data, Record_Type&, Params* params = NULL);
  virtual std::unique_ptr<Base_Type> decode (const OCTETSTRING& data, Params* params = NULL);
  std::unique_ptr<Base_Type> decode (const OCTETSTRING& data, Params* params = NULL);
  
private:
  int decode (const OCTETSTRING& data, LibItsDenm__TypesAndValues::UtDenmResults& msg, Params* params = NULL);