#include "LibItsCam_EncdecDeclarations.hh"

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

namespace LibItsCam__EncdecDeclarations {
  /****************************************************
   * @desc    External function to encode a CamReq type
   * @param   value to encode
   * @return  encoded value
   ****************************************************/
  BITSTRING fx__enc__CamReq(const LibItsCam__TestSystem::CamReq& p_camReq)
  {
    loggers::get_instance().log_msg(">>> fx__enc__CamReq: ", p_camReq);
    
    CAMCodec codec;
    OCTETSTRING os;
    if (codec.encode(p_camReq.msgOut(), os) == -1) {
      loggers::get_instance().warning("fx__enc__CamReq: -1 result code was returned");
      return int2bit(0, 1);
    }
    
    return oct2bit(os);
  }

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

    CAMCodec codec;
    CAM__PDU__Descriptions::CAM cam;
    OCTETSTRING is = bit2oct(b);

    if (codec.decode(is, cam) == -1) {
      loggers::get_instance().warning("fx__dec__CamReq: -1 result code was returned");
      return -1;
    }
    p.msgOut() = cam;
    
    return 0;
  }

  INTEGER fx__dec__CamInd(BITSTRING& b, LibItsCam__TestSystem::CamInd& p_camInd)
  {
    loggers::get_instance().log_msg(">>> fx__enc__CamInd: ", p_camInd);
    
    CAMCodec codec;
    CAM__PDU__Descriptions::CAM cam;
    OCTETSTRING is = bit2oct(b);

    // Calculate the size of the lower layers information
    int s = (
	     LibItsCam__TestSystem::CamInd_gnNextHeader_raw_.fieldlength +
	     LibItsCam__TestSystem::CamInd_gnHeaderType_raw_.fieldlength +
	     LibItsCam__TestSystem::CamInd_gnHeaderSubtype_raw_.fieldlength +
	     LibItsCam__TestSystem::CamInd_gnLifetime_raw_.fieldlength +
	     LibItsCam__TestSystem::CamInd_gnTrafficClass_raw_.fieldlength +
	     LibItsCam__TestSystem::CamInd_btpDestinationPort_raw_.fieldlength +
	     LibItsCam__TestSystem::CamInd_btpInfo_raw_.fieldlength +
	     LibItsCam__TestSystem::CamInd_ssp_raw_.fieldlength +
	     LibItsCam__TestSystem::CamInd_its__aid_raw_.fieldlength
	     ) / 8;
    if (codec.decode(OCTETSTRING(is.lengthof() - s, static_cast<const unsigned char *>(is)), cam) == -1) {
      loggers::get_instance().warning("fx__dec__CamInd: -1 result code was returned");
      return -1;
    }
    p_camInd.msgIn() = cam;
    TTCN_Buffer decoding_buffer(OCTETSTRING(is.lengthof() - s, static_cast<const unsigned char *>(is) + is.lengthof() - s));
    for (int i = 1; i < p_camInd.get_count(); i++) {
      p_camInd.get_at(i)->decode(*p_camInd.fld_descr(i), decoding_buffer, TTCN_EncDec::CT_RAW);      
    } // End of 'for' statement
    
    return 0;
  }

  BITSTRING fx__enc__CAM(const CAM__PDU__Descriptions::CAM& p_cam)
  {
    loggers::get_instance().log_msg(">>> fx__enc__CAM: ", p_cam);
    
    CAMCodec codec;
    OCTETSTRING os;
    codec.encode(p_cam, os);
    return oct2bit(os);
  }

  INTEGER fx__dec__CAM(BITSTRING& b, CAM__PDU__Descriptions::CAM& p)
  {
    loggers::get_instance().log(">>> fx__dec__CAM");
    CAMCodec codec;
    OCTETSTRING is = bit2oct(b);

    if (codec.decode(OCTETSTRING(is.lengthof(), static_cast<const unsigned char *>(is)), p) == -1) {
      loggers::get_instance().warning("fx__dec__CAM: -1 result code was returned");
      return -1;
    }
    
    return 0;
  }

} //end namespace

