/**
 * \file TLV.h
 * \brief Helper for creating TLV structures.
 * \author Roman Pasechnik (r.pasechnik@samsung.com)
 * \version 0.1
 * \date Created Dec 23, 2013
 * \par In Samsung Ukraine R&D Center (SURC) under a contract between
 * \par LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine) and
 * \par "Samsung Elecrtronics Co", Ltd (Seoul, Republic of Korea)
 * \par Copyright: (c) Samsung Electronics Co, Ltd 2012. All rights reserved.
 */

#ifndef __TLV_H_INCLUDED__
#define __TLV_H_INCLUDED__

#include <stdint.h>

/** tlvInit
 *
 * Initialize TLV buffer. Must be called before tlvAdd()
 *
 * tlv: buffer to initialize
 * tlvLen: size of allocated tlv buffer
 *
 * return: NO_ERROR on success or error otherwise
 */
int tlvInit(uint8_t *tlv, uint32_t tlvLen);

/** tlvAdd
 *
 * Add new attribute to TLV buffer
 *
 * tlv: initialized tlv buffer
 * tlvLen: size of allocated tlv buffer (maximum size)
 * tag: identifier of attribute
 * value: pointer to value of the attribute. Representation depends on
 *        particular attribute
 * valueLen: size of value buffer
 *
 * return: NO_ERROR on success or error otherwise
 */
int tlvAdd(uint8_t *tlv, uint32_t tlvLen, TlvTag_t tag,
		const void *value, uint32_t valueLen);

/** tlvSize
 *
 * Return actual size of TLV buffer. This size should be passed to
 * generateServiceKeyEx.
 *
 * tlv: buffer initialized by tlvInit and constructed by tlvAdd
 * tlvLen: size of allocated tlv buffer (maximum size)
 *
 * return: actual size of TLV buffer on success or negative error code
 *         otherwise
 */
int tlvSize(const uint8_t *tlv, uint32_t tlvLen);

/** tlvSize
 *
 * Get (tag, length, value) in tlv buffer of custom tlv element
 * specified by tlvNo
 *
 * tlv: buffer initialized by tlvInit and constructed by tlvAdd
 * tlvLen: size of allocated tlv buffer
 * tlvTag: desired tag to get
 * tag: pointer to tag, which will be fetched
 * length: pointer to length, which will be fetched
 * value: pointer to value, which will be fetched
 *
 * return: NO_ERROR on success or negative error code
 */
int tlvGet(const uint8_t *tlv, uint32_t tlvLen, TlvTag_t tag,
		uint32_t* length, uint8_t** value);

#endif
