Commit dd4d8327 authored by Yann Garcia's avatar Yann Garcia
Browse files

Create project structure

parent 93af77e9
#include "LibItsHttp_MessageBodyTypes.hh"
#include "http_codec.hh"
#include "loggers.hh"
namespace LibItsHttp__EncdecDeclarations {
BITSTRING fx__enc__http__message(const LibItsHttp__TypesAndValues::HttpMessage& p) {
loggers::get_instance().log_msg(">>> fx__enc__http__message: ", (const Base_Type&)p);
OCTETSTRING os;
http_codec codec;
codec.encode(p, os);
return oct2bit(os);
}
INTEGER fx__dec__http__message(BITSTRING& pdu, LibItsHttp__TypesAndValues::HttpMessage& p) {
loggers::get_instance().log_msg(">>> fx__dec__http__message: ", pdu);
OCTETSTRING os = bit2oct(pdu);
http_codec codec;
codec.decode(os, p);
return 0;
}
} // End of namespace LibItsHttp__EncdecDeclarations
#include "LibMec_EncdecDeclarations.hh"
#include "loggers.hh"
namespace LibMec__EncdecDeclarations {
BITSTRING fx__enc__LocationRequest__UserInfo(const LibMec__SIPTypesAndValues::Request& p) {
loggers::get_instance().log_msg(">>> fx__enc__UserInfo: ", p);
float duration;
std::string tag("fx__enc__Request");
loggers::get_instance().set_start_time(tag);
OCTETSTRING os;
// sip_codec_request codec;
// if (codec.encode(p, os) == -1) {
// loggers::get_instance().warning("fx__enc__Request -1 result code was returned");
// return int2bit(0, 1);
// }
loggers::get_instance().set_stop_time(tag, duration);
return oct2bit(os);
}
INTEGER fx__dec__LocationRequest__UserInfo(BITSTRING& pdu, LibMec__SIPTypesAndValues::Request& p) {
loggers::get_instance().log_msg(">>> fx__dec__LocationRequest__UserInfo: ", pdu);
OCTETSTRING os = bit2oct(pdu);
// sip_codec_request codec;
// codec.decode(os, p);
return 0;
}
} // End of namespace LibMec__EncdecDeclarations
/*!
* \file base_time.hh
* \brief Header file for the control port base_time functionality.
* \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 <chrono>
/**
* \class base_time
* \brief This class provides time tools such as getting current time
*/
class base_time {
const unsigned long long its_base_time_ms = 1072915200000L; //! Base time 01/01/2004 12:00am in millseconds
static base_time* _instance;
private:
base_time() { }; //! Can not be created manually
public:
static inline base_time& get_instance();
virtual ~base_time() { if (_instance != nullptr) delete _instance; };
public:
inline const unsigned long long get_current_time_ms() const;
inline const unsigned long long get_its_base_time_ms() const;
inline const unsigned long long get_its_current_time_ms() const;
inline const unsigned long long get_its_current_time_us() const;
inline const unsigned long long get_its_current_time_mod_ms() const;
}; // End of class base_time
// static functions
base_time& base_time::get_instance() {
return (_instance != nullptr) ? *_instance : *(_instance = new base_time());
}
const unsigned long long base_time::get_current_time_ms() const {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
const unsigned long long base_time::get_its_base_time_ms() const {
return base_time::its_base_time_ms;
}
const unsigned long long base_time::get_its_current_time_ms() const {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count() - base_time::its_base_time_ms;
}
const unsigned long long base_time::get_its_current_time_us() const {
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count() - base_time::its_base_time_ms * 1000;
}
const unsigned long long base_time::get_its_current_time_mod_ms() const {
return (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count() - base_time::its_base_time_ms) % 65536;
}
/*!
* \file codec.hh
* \brief Header file for ITS abstract codec definition.
* \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 "params.hh"
class OCTETSTRING; //! Declare TITAN class
class CHARSTRING; //! Declare TITAN class
class BITSTRING; //! Declare TITAN class
/*!
* \class codec
* \brief This class provides the interface for all ITS codecs, include UT and AC codecs
* \abstract
*/
template<typename TPDUEnc, typename TPDUDec>
class codec {
protected:
params* _params; //! Reference to params stack
// \todo Use smart pointer std::unique_ptr<params>
public: //! \publicsection
/*!
* \fn codec();
* \brief Default constructor
* \todo Remove logs
*/
explicit codec() : _params(nullptr) { };
/*!
* \fn ~codec();
* \brief Default destructor
* \virtual
* \todo Remove logs
*/
virtual ~codec() { };
/*!
* \fn int encode(const TPDUEnc& msg, OCTETSTRING& data);
* \brief Encode typed message into an octet string
* \param[in] p_message The typed message to be encoded
* \param[out] p_data The encoding result
* \return 0 on success, -1 otherwise
* \pure
*/
virtual int encode(const TPDUEnc& p_message, OCTETSTRING& p_data) = 0;
/*!
* \fn int decode(const OCTETSTRING& p_, TPDUDec& p_message, params* p_params = NULL);
* \brief Encode typed message into an octet string format
* \param[in] p_data The message in its octet string
* \param[out] p_message The decoded typed message
* \return 0 on success, -1 otherwise
* \pure
*/
virtual int decode(const OCTETSTRING& p_, TPDUDec& p_message, params* p_params = NULL) = 0;
}; // End of class codec
/*!
* \file codec_factory.hh
* \brief Header file for ITS abstract protocol codec definition.
* \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 <string>
#include <map>
#include <vector>
#include <algorithm>
#include "codec.hh"
class Record_Type; //! TITAN forward declaration
/*!
* \class codec_factory
* \brief This class provides a factory class to create codec class instances
* \abstract
*/
class codec_factory {
public: //! \publicsection
/*!
* \fn codec();
* \brief Default constructor
*/
codec_factory() { };
/*!
* \fn codec* create_codec(const std::string & type, const std::string & param);
* \brief Create the codecs stack based on the provided codecs stack description (cf. remark)
* \param[in] p_type The provided codecs stack description
* \param[in] p_params Optional parameters
* \return 0 on success, -1 otherwise
* \remark The description below introduces codecs stack in case of ITS project:
* HTTP(codecs=xml:held_codec;html:html_codec,json:json_codec)/TCP(debug=1,server=httpbin.org,port=80,use_ssl=0)
* \pure
*/
virtual codec<Record_Type, Record_Type>* create_codec() = 0;
}; // End of class codec_factory
/*!
* \file codec_stack_builder.hh
* \brief Header file for ITS protocol stack builder.
* \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 "codec_factory.hh"
class Record_Type; //! TITAN forward declaration
/*!
* \class codec_stack_builder
* \brief This class provides a factory class to create Codec class instances
*/
class codec_stack_builder {
private: //! \privatesection
static codec_stack_builder* _instance; //! Smart pointer to the unique instance of the logger framework
std::map<std::string, codec_factory*> _codecs; //! The list of the registered \see t_codec factories
/*!
* \brief Default constructor
* Create a new instance of the codec_stack_builder class
* \private
*/
codec_stack_builder() { }; // can not be created manually
public: //! \publicsection
/*!
* \fn codec_stack_builder* get_instance();
* \brief Accessor for the unique instance of the logger framework
* \static
*/
static codec_stack_builder* get_instance() { return _instance ? _instance : _instance = new codec_stack_builder(); };
/*!
* \fn void register_codec_factory(const std::string & p_type, codec_factory<TPDUEnc, TPDUDec>* p_codec_factory);
* \brief Add a new codec factory
* \param[in] p_type The codec identifier (e.g. GN for the GeoNetworking codec...)
* \param[in] p_codec_factory A reference to the \see codec_factory
* \static
*/
static void register_codec_factory(const std::string & p_type, codec_factory* p_codec_factory) { codec_stack_builder::get_instance()->_register_codec_factory(p_type, p_codec_factory); };
private: //! \privatesection
/*!
* \fn void _register_codec_factory(const std::string & p_type, codec_factory<TPDUEnc, TPDUDec>* p_codec_factory);
* \brief Add a new codec factory
* \param[in] p_type The codec identifier (e.g. GN for the GeoNetworking codec...)
* \param[in] p_codec_factory A reference to the \see codec_factory
*/
void _register_codec_factory(const std::string & p_type, codec_factory* p_codec_factory) { _codecs[p_type] = p_codec_factory; };
public: //! \publicsection
/*!
* \fn codec* get_codec(const char* p_codec_name);
* \brief Retrieve the specified codec name from the list of the registered codecs
* \param[in] p_codec_name The codec indentifier
* \return The pointer to the codec object on success, nullptr otherwise
*/
inline codec<Record_Type, Record_Type>* get_codec(const char* p_codec_name) { // NOTE A virtual method cannot not be a template ==> polymorphism required here
typename std::map<std::string, codec_factory*>::const_iterator it = _codecs.find(p_codec_name);
if (it != _codecs.cend()) {
return it->second->create_codec();
}
return nullptr;
}
}; // End of class codec_stack_builder
/*!
* \file converter.hh
* \brief Helper class for types converter.
* \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 <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdint>
#include <cctype>
#include <climits> // LONG_MAX, LLONG_MAX
#include <ctime> // time_t, struct tm, difftime, time, mktime
/*!
* \class converter
* \brief This class provide a set of methods for types conversions
* \remark Singleton pattern
*/
class converter {
/*!
* \brief Unique static object reference of this class
*/
static converter * instance;
/*!
* \brief Default private ctor
*/
converter() {};
/*!
* \brief Default private dtor
*/
~converter() {
if (instance != NULL) {
delete instance;
instance = NULL;
}
};
public: /*! \publicsection */
/*!
* \brief Public accessor to the single object reference
*/
inline static converter & get_instance() {
if (instance == NULL) instance = new converter();
return *instance;
};
public:
/*!
* \enum endian_t
* \brief Endianess style
*/
typedef enum {
big_endian,
little_endian
} endian_t;
public:
/*!
* \brief Convert a Binary Coded Decimal value into a binary value
* \param[in] p_value The BDC value
* \return The binary value
* \inline
*/
inline uint8_t bcd_to_bin(const uint8_t p_value) {
return ((p_value / 16 * 10) + (p_value % 16));
};
/*!
* \brief Convert a binary value into a Binary Coded Decimal value
* \param[in] p_value The binary value
* \return The BCD value
* \inline
*/
inline uint8_t bin_to_bcd(const uint8_t p_value) {
return ((p_value / 10 * 16) + (p_value % 10));
};
/*!
* \brief Swap two bytes length value (e.g. 0xCAFE becomes 0xFECA)
* \param[in] p_value The value to swap
* \return The swapped value
* \inline
*/
uint16_t swap(const uint16_t p_value);
inline int16_t swap(const int16_t p_value) {
return static_cast<short>(swap(static_cast<uint16_t>(p_value)));
};
/*!
* \brief Swap four bytes length value (used for littel endian / big endian)
* \param[in] p_value The value to swap
* \return The swapped value
*/
uint32_t swap(const uint32_t p_value);
inline int32_t swap(const int32_t p_value) {
return static_cast<int>(swap(static_cast<uint32_t>(p_value)));
};
/*!
* \brief Convert a string into an hexadecimal string
* \param[in] p_value The string value
* \return The hexadecimal value
*/
std::string string_to_hexa(const std::string & p_value, const bool p_uppercase = false);
/*!
* \brief Convert a bytes array int32_t an hexadecimal string
* \param[in] p_value The bytes array value
* \return The hexadecimal value
*/
std::string bytes_to_hexa(const std::vector<uint8_t> & p_value, const bool p_uppercase = false);
/*!
* \brief Convert an hexadecimal string into a bytes array
* \param[in] p_value The hexadecimal value
* \return The bytes array value
*/
std::vector<uint8_t> hexa_to_bytes(const std::string & p_value);
/*!
* \brief Convert a time in time_t format into a string formated according to RFC 822, 1036, 1123, 2822
* \param[in] p_time The time to convert in time_t format
* \return The time string formated
* \see http://www.unixtimestamp.com/
* @code
* std::string result = time_to_string(1489755780);
* result.compare("Fri, 17 Mar 2017 13:03:00 +0000") == 0 // When time zone is set to UTC
* @endcode
* \remark Use commands 1) timedatectl to change your machine timezone (e.g. sudo timedatectl set-timezone UTC to change machine timezone to UTC, 2) timedatectl list-timezones to get the list of the timezones)
*/
std::string time_to_string(const time_t p_time);
/*!
* \brief Convert a time in struct tm format into a string formated according to RFC 822, 1036, 1123, 2822
* \param[in] p_time The time to convert in struct tm format
* \return The time string formated
* \see http://www.unixtimestamp.com/
*/
std::string time_to_string(const struct tm & p_time);
/*!
* \brief Convert a 16-bits integer (int16_t) into a bytes array
* \param[in] p_value The 16-bits integer value
* \param[in] p_endianess Endianess style. Default: big_endian
* \return The bytes array value
*/
inline std::vector<uint8_t> short_to_bytes(const int16_t p_value, const endian_t p_endianess = big_endian) const {
std::vector<uint8_t> result(sizeof(short), 0x00);
for (int i = sizeof(short) - 1; i >= 0; i--) {
int offset = (sizeof(short) - 1 - i) * 8;
result[i] = static_cast<uint8_t>((p_value >> offset) & 0xFF);
} // End of 'for' statement
return result;
}; // End of short_to_bytes
/*!
* \brief Convert a bytes array into a 16-bits integer (int16_t)
* \param[in] p_value The bytes array
* \param[in] p_endianess Endianess style. Default: big_endian
* \return The 16-bits integer on success, SHRT_MAX on error (wrong bytes array size)
*/
inline int16_t bytes_to_short(const std::vector<uint8_t> & p_value, const endian_t p_endianess = big_endian) const {
// Sanity check
if (p_value.size() > sizeof(short)) {
return SHRT_MAX;
}
int16_t value = 0;
for (size_t i = 0; i < p_value.size(); i++) {
value = (value << 8) + (p_value[i] & 0xff);
} // End of 'for' statement
return value;
}; // End of bytes_to_short
/*!
* \brief Convert a 32-bits integer (int32_t) into a bytes array
* \param[in] p_value The 32-bits integer value
* \param[in] p_endianess Endianess style. Default: big_endian
* \return The bytes array value
*/
inline std::vector<uint8_t> int_to_bytes(const int32_t p_value, const endian_t p_endianess = big_endian) const {
/*uint8_t bytes[sizeof(p_value)];
std::copy(
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)),
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)) + sizeof(p_value),
bytes
);
std::vector<uint8_t> result(bytes, bytes + sizeof(bytes) / sizeof(uint8_t));*/
std::vector<uint8_t> result(sizeof(int), 0x00);
for (int i = sizeof(int) - 1; i >= 0; i--) {
int offset = (sizeof(int) - 1 - i) * 8;
result[i] = static_cast<uint8_t>((p_value >> offset) & 0xFF);
} // End of 'for' statement
return result;
}; // End of int_to_bytes
/*!
* \brief Convert a bytes array into a 32-bits integer (int32_t)
* \param[in] p_value The bytes array
* \param[in] p_endianess Endianess style. Default: big_endian
* \return The 32-bits integer on success, LONG_MAX on error (wrong bytes array size)
*/
inline int32_t bytes_to_int(const std::vector<uint8_t> & p_value, const endian_t p_endianess = big_endian) const {
// Sanity check
if (p_value.size() > sizeof(int)) {
return INT_MAX;
}
int32_t value = 0;
for (size_t i = 0; i < p_value.size(); i++) {
value = (value << 8) + (p_value[i] & 0xff);
} // End of 'for' statement
return value;
// return *((int *)(&p_value[0]));
}; // End of bytes_to_int
/*!
* \brief Convert a 64-bits integer (int64_t) into a bytes array
* \param[in] p_value The 64-bits integer value
* \param[in] p_endianess Endianess style. Default: big_endian
* \return The bytes array value
*/
inline std::vector<uint8_t> long_to_bytes(const int64_t p_value, const endian_t p_endianess = big_endian) const {
/*uint8_t bytes[sizeof(p_value)];
std::copy(
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)),
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)) + sizeof(p_value),
bytes
);
std::vector<uint8_t> result(bytes, bytes + sizeof(bytes) / sizeof(uint8_t));*/
std::vector<uint8_t> result(sizeof(int64_t), 0x00);
for (int i = sizeof(int64_t) - 1; i >= 0; i--) {
int offset = (sizeof(int64_t) - 1 - i) * 8;
result[i] = static_cast<uint8_t>((p_value >> offset) & 0xFF);
} // End of 'for' statement
return result;
}; // End of long_to_bytes
/*!
* \brief Convert a bytes array into a 64-bits integer (int64_t)
* \param[in] p_value The bytes array
* \param[in] p_endianess Endianess style. Default: big_endian
* \return The 64-bits integer on success, LLONG_MAX on error (wrong bytes array size)
*/
inline int64_t bytes_to_long(const std::vector<uint8_t> & p_value, const endian_t p_endianess = big_endian) const {
// Sanity check
if (p_value.size() > sizeof(int64_t)) {
return LLONG_MAX;
}
int64_t value = 0;
for (size_t i = 0; i < p_value.size(); i++) {
value = (value << 8) + (p_value[i] & 0xff);
} // End of 'for' statement
return value;
// return *((long *)(&p_value[0]));
}; // End of bytes_to_long
/*!
* \brief Convert a float value into a bytes array
* \param[in] p_value The float value
* \return The bytes array value
*/
inline std::vector<uint8_t> float_to_bytes(const float p_value) const {
uint8_t bytes[sizeof(p_value)];
std::copy(
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)),
static_cast<const uint8_t *>(static_cast<const void *>(&p_value)) + sizeof(p_value),
bytes
);
std::vector<uint8_t> result(bytes, bytes + sizeof(bytes) / sizeof(uint8_t));
return result;
}; // End of float_to_long
/*!
* \brief Convert a bytes array into a float
* \param[in] p_value The bytes array
* \return The float value
*/