Commit 99e11be1 authored by Yann Garcia's avatar Yann Garcia
Browse files

Rebuild full project

parent 80e9c7b6
Loading
Loading
Loading
Loading
+0 −73
Original line number Diff line number Diff line
/*!
 * \file      base_time.hh
 * \brief     Header file for 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

  unsigned long long leap_delay;

  static base_time *_instance;

private:
  base_time() : leap_delay{0} {}; //! 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;
  inline void                     set_leap_delay_us(const unsigned long long p_leap_delay);
  inline const unsigned long long get_leap_delay_us() 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 (leap_delay / 1000) + 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 (leap_delay / 1000) + 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 leap_delay + 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 ((leap_delay / 1000) + std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count() -
          base_time::its_base_time_ms) %
         65536;
}

void base_time::set_leap_delay_us(const unsigned long long p_leap_delay) { leap_delay = p_leap_delay; }

inline const unsigned long long base_time::get_leap_delay_us() const { return leap_delay; }
+0 −45
Original line number Diff line number Diff line
/*!
 * \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 <algorithm>
#include <map>
#include <string>
#include <vector>

#include "codec_gen.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_factory();
   * \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_gen<Record_Type, Record_Type> *create_codec() = 0;
}; // End of class codec_factory
+0 −75
Original line number Diff line number Diff line
/*!
 * \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_gen<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
+0 −396
Original line number Diff line number Diff line
/*!
 * \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 <iomanip>
#include <iostream>
#include <sstream>

#include <algorithm>
#include <string>
#include <vector>

#include <cctype>
#include <climits> // LONG_MAX, LLONG_MAX
#include <cstdint>
#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
   */
  inline float bytes_to_float(const std::vector<uint8_t> &p_value) const { return *((float *)(&p_value[0])); }; // End of bytes_to_float

  /*!
   * \brief Convert a string into a bytes array
   * \param[in] p_value The string value
   * \return The bytes array value
   */
  inline std::vector<uint8_t> string_to_bytes(const std::string& p_value) const {
    return std::vector<uint8_t>(p_value.begin(), p_value.end());
  }; // End of string_to_bytes

  /*!
   * \brief Convert a bytes array into a string
   * \param[in] p_value The bytes array value
   * \return The string value
   */
  inline std::string bytes_to_string(const std::vector<uint8_t> &p_value) const {
    return std::string(p_value.begin(), p_value.end());
  }; // End of bytes_to_string

public:
  /*!
   * \brief Convert a string into an integer
   * \param[in] p_value The string value
   * \return The integer value
   */
  inline int32_t string_to_int(const std::string& p_value) const {
    return std::stoi(p_value);
    // return atoi(p_value.c_str());
  }; // End of string_to_int

  /*!
   * \brief Convert an integer into a string
   * \param[in] p_value The integer value
   * \return The string value
   */
  inline std::string int_to_string(const int32_t &p_value) const {
    std::ostringstream ss;
    ss << p_value;
    return ss.str();
  }; // End of string_to_bytes

  /*!
   * \brief Convert a string in to lower case
   * \param[in/out] p_value The string value to convert
   */
  inline void to_lower(std::string& p_value) { std::transform(p_value.begin(), p_value.end(), p_value.begin(), ::tolower); }

  /*!
   * \brief Convert a string in to upper case
   * \param[in/out] p_value The string value to convert
   */
  inline void to_upper(std::string& p_value) { std::transform(p_value.begin(), p_value.end(), p_value.begin(), ::toupper); }

public:
  /*!
   * \brief Returns a copy of the string, with leading and trailing special characters omitted
   * \param[in] p_value The string value
   * \param[in] p_trim_chars The special characters to be omitted. Default: ' ' and TAB
   * \return The new string value
   */
  std::string trim(const std::string& p_value, const std::string& p_trim_chars = " \t");

  /*!
   * \brief Convert the provided string into a list of arguments
   * \param[in] p_value The string value
   * \param[in] p_separator The separator sequence to use for the spliting process
   * \return The item list
   * \code{.cc}
   *     std::string str = "This is a test for spliting a string with a white spave";
   *     std::vector<std::string> tokens = converter::get_instance().split(str, " ");
   *     std::clog << "Tokens: " << std::endl;
   *     for (auto it = tokens.begin(); it != tokens.end(); ++it) {
   *       std::clog << "   " << *it << std::endl;
   *     }
   * \endcode
   */
  std::vector<std::string> split(const std::string& p_value, const std::string& p_separator);

  /*!
   * \brief Convert the provided string into a list of arguments
   * \param[in] p_value The string value
   * \return The arguments list
   * \code{.cc}
   *     std::string str = "--host localhost --port 12345 --duration -1";
   *     std::vector<std::string> tokens = converter::get_instance().split_arguments_line(str);
   *     std::clog << "Tokens: " << std::endl;
   *     for (auto it = tokens.begin(); it != tokens.end(); ++it) {
   *       std::clog << "   " << *it << std::endl;
   *     }
   * \endcode
   */
  std::vector<std::string> split_arguments_line(const std::string& p_value);

  /*!
   * \brief Replace string or character into the the provided string
   * \param[in] p_value The original string value
   * \param[in] p_from The pattern to be replaced
   * \param[in] p_to The new pattern
   * \return The modified string
   */
  std::string replace(const std::string& p_value, const std::string& p_from, const std::string& p_to);

  /*!
   * \brief Convert the provided buffer into a Base64
   * \param[in] p_value The buffer value
   * \return The Base64 encoded buffert
   */
  std::vector<unsigned char> buffer_to_base64(const std::vector<unsigned char> &p_value, const bool p_is_url = false);

  /*!
   * \brief Convert the provided Base64 buffer
   * \param[in] p_value The buffer value
   * \return The Base64 encoded buffert
   */
  std::vector<unsigned char> base64_to_buffer(const std::vector<unsigned char> &p_value, const bool p_remove_crlf = true);

  static const std::string lut_u;
  static const std::string lut_l;
  static const std::string base64_enc_map[2];

}; // End of class converter

ccsrc/Framework/include/layer.hh

deleted100644 → 0
+0 −146

File deleted.

Preview size limit exceeded, changes collapsed.

Loading