/**
 * @file       serialize.h
 * @brief      Process authenticator API for serialization ASN1 format
 *
 * Main purpose of this module is providing interface to serialize/deserialize
 * in ASN1 format.
 * @author     Viacheslav Vovchenko (v.vovchenko@samsung.com)
 * @version    1.0
 * @date       Created May 30, 2016
 * @copyright  In Samsung Ukraine R&D Center (SURC) under a contract between
 * @copyright  LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine) and
 * @copyright  "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
 * @copyright  Copyright: (c) Samsung Electronics Co, Ltd 2016. All rights reserved.
**/

#ifndef PA_ASN1_LIB_SRC_SERIALIZE_H_
#define PA_ASN1_LIB_SRC_SERIALIZE_H_

#include "PaDriverCommand.h"
#include "PaDriverCommandResponse.h"
#include "PaCertificate.h"

enum {
  kSerializedDataMaxSize = 4096
};

/**
 * @brief Encode ASN1 command to out buffer.
 * @param [in]  command Pointer of command structure
 * @param [out] memory Output memory buffer for encoded command
 * @param [in, out] size_memory Maximal size of output memory buffer for encoded command.
 * Set number of encoded bytes
 * @return ::0,  if the encoder is completed successful,
 *         ::-1, if the encoder is failed,
 */
int PaEncodeDriverCommand(const PaDriverCommand_t *command, void *memory,
                           uint32_t *size_memory);

/**
 * @brief Decode ASN1 command that passed to driver
 * @param [in] memory,size_buffer Memory with encoded ASN1 command
 * @param [out] command Pointer to command structure (MUST be freed by caller)
 * @return ::0,  if the dencoder is completed successful,
 *         ::-1, if the dencoder is failed,
 */
int PaDecodeDriverCommand(const void *memory, const uint32_t size_buffer,
                          PaDriverCommand_t** command);

/**
 * @brief Encode ASN1 command response that passed from driver
 * @param [in] command_response Pointer of command response structure
 * @param [out] memory Output memory buffer for encoded command
 * @param [in,out] buffer_size Max output buffer size and real encoded count of bytes
 * @return ::0,  if the encoder is completed successful,
 *         ::-1, if the encoder is failed,
 */
int PaEncodeDriverCommandResponse(
    const PaDriverCommandResponse_t *command_response, void *memory,
    uint32_t *buffer_size);

/**
 * @brief Decode ASN1 response command.
 * @param [in] memory,size_buffer Memory with encoded ASN1 command
 * @param [out] command_response Pointer of command structure
 * (must be to free in own code)
 * @return ::0,  if the decoder is completed successful,
 *         ::-1, if the decoder is failed,
 */
int PaDecodeDriverCommandResponse(
    const void *memory, const uint32_t size_buffer,
    PaDriverCommandResponse_t** command_response);

/**
 * @brief Free ASN1 resources for response command.
 * @param [in] command_response Pointer of command structure
 */
void PaFreeDriverCommandResponse(PaDriverCommandResponse_t *command_response);

/**
 * @brief Encode ASN1 data for sub-structure of certificate data
 * @param [in] command Pointer of sub-structure of certificate data
 * @param [out] memory,size_buffer Output buffer for encoded response
 * @return 0, if the encoder is completed successful,
 *         -1, if the encoder is failed,
 */
int PaEncodePaData(const PaData_t *command, void *memory,
                    uint32_t *size_buffer);

/**
 * @brief Encode ASN1 certificate data.
 * @param [in] certificate Pointer to certificate data structure
 * @param [out] memory,size Memory with encoded ASN1 command
 * @return ::0,  if the encoder is completed successful,
 *         ::-1, if the encoder is failed,
 */
int PaEncodeCertificate(const PaCertificate_t *certificate,
                        void *memory, uint32_t *size);

/**
 * @brief Decode plain ASN1 certificate data to certificate data structure.
 * @param [in] memory,memory_size Memory with encoded ASN1 certificate data
 * @param [out] certificate Pointer of certificate data structure
 * (must be to free in own code)
 * @return ::0,  if the decoder is completed successful,
 *         ::-1, if the decoder is failed,
 */
int PaDecodeCertificate(const void *memory, const uint32_t memory_size,
                        PaCertificate_t **certificate);

/**
 * @brief Free certificate structure memory allocated by PaDecodeCertificate.
 * @param [in] certificate Pointer of certificate structure
 */
void PaFreeCertificate(PaCertificate_t *certificate);

#endif  // PA_ASN1_LIB_SRC_SERIALIZE_H_
