/**
 * @file persistent_object.h
 * @brief Multibuild's internal persistent object functions declarations
 * @author Iaroslav Makarchuk (i.makarchuk@samsung.com)
 * @date Created Oct 3, 2016
 * @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 2015. All rights reserved.
 *
 * This software is proprietary of Samsung Electronics.
 * No part of this software, either material or conceptual may be copied
 * or distributed, transmitted, transcribed, stored in a retrieval system
 * or translated into any human or computer language in any form by any means,
 * electronic, mechanical, manual or otherwise, or disclosed to third parties
 * without the express written permission of Samsung Electronics.
 */

#ifndef MB_TEE_PO_INTERNAL_H_
#define MB_TEE_PO_INTERNAL_H_

#include <tee_internal_api.h>
#include <protocol.h>

/**
 * @brief Reads data from persistent object with specified id
 *
 * @param[in]   id        A pointer to persistent object ID, where data should
 *                        be read from
 * @param[in]   id_len    Length in bytes of persistent object's ID
 * @param[out]  data      A data buffer to store read data
 * @param[out]  data_len  Size of store data in bytes
 * @param[in]   data_type The type of data that should be read
 *
 * @retval TEE_SUCCESS              Data read successfully
 * @retval TEE_ERROR_BAD_PARAMETERS Bad parameters passed to the function
 * @retval TEE_ERROR_ITEM_NOT_FOUND Requested peristent object doesn't exist
 * @retval TEE_ERROR_GENERIC        Other error
 */
TEE_Result ReadFromPO(const uint8_t *id, uint32_t id_len,
                      uint8_t *data, uint32_t *data_len,
                      MultibuildFsStoreDataType data_type);

/**
 * @brief Writes data to persistent object with specified id
 *
 * @param[in]   id        A pointer to persistent object ID, where data should
 *                        be written to
 * @param[in]   id_len    Length in bytes of persistent object's ID
 * @param[in]   data      A data buffer which should be written to the object
 * @param[in]   data_len  Size in bytes of data to be written
 * @param[in]   data_type The type of data that should be written
 *
 * @retval TEE_SUCCESS              Data written successfully
 * @retval TEE_ERROR_BAD_PARAMETERS Bad parameters passed to the function
 * @retval TEE_ERROR_GENERIC        Other error
 */
TEE_Result WriteToPO(const uint8_t *id, uint32_t id_len,
                     const uint8_t *data, uint32_t data_len,
                     MultibuildFsStoreDataType data_type);

/**
 * @brief Reads data from persistent object with specified id
 *
 * @param[in]   id       A pointer to persistent object ID, which will be
 *                       removed
 * @param[in]   id_len   Length in bytes of persistent object's ID
 *
 * @retval TEE_SUCCESS              Persistent object removed succesfully
 * @retval TEE_ERROR_BAD_PARAMETERS Bad parameters passed to the function
 * @retval TEE_ERROR_ITEM_NOT_FOUND Requested peristent object doesn't exist
 * @retval TEE_ERROR_GENERIC        Other error
 */
TEE_Result RemovePO(const uint8_t *id, uint32_t id_len);

/**
 * @brief Starts object data update
 *
 * @note After this call writing data to object is possible. After the data is
 * written, CommitObjectUpdate() should be called. Until CommitObjectUpdate()
 * is called, the written data is not saved to the object.
 *
 * @param[in]   id       A pointer to persistent object ID, which will be
 *                       updated
 * @param[in]   id_len   Length in bytes of persistent object's ID
 *
 * @retval TEE_SUCCESS              Persistent object update started
 * @retval TEE_ERROR_BAD_PARAMETERS Bad parameters passed to the function
 * @retval TEE_ERROR_GENERIC        Other error
 */
TEE_Result StartObjectUpdate(const void *id, uint32_t id_len);

/**
 * @brief Finalizes object data update
 *
 * @note After this call the data written before will be actually stored into
 * the specified object. This call must be preceded with StartObjectUpdate()
 * for the same object ID.
 *
 * @param[in]   id       A pointer to persistent object ID, which will be
 *                       updated
 * @param[in]   id_len   Length in bytes of persistent object's ID
 *
 * @retval TEE_SUCCESS              Persistent object update finished
 * @retval TEE_ERROR_BAD_PARAMETERS Bad parameters passed to the function
 * @retval TEE_ERROR_GENERIC        Other error
 */
TEE_Result CommitObjectUpdate(const void *id, uint32_t id_len);

/**
 * @brief Aborts object data update
 *
 * @note Cancels object data update. All the data written to the object after
 * the corresponding StartObjectUpdate() call will be lost.
 *
 * @param[in]   id       A pointer to persistent object ID, whose update must be
 *                       cancelled
 * @param[in]   id_len   Length in bytes of persistent object's ID
 */
void AbortObjectUpdate(const void *id, uint32_t id_len);

/**
 * @brief Lists the persistent objects for the current session
 *
 * @note This call returns the blob containing the data about available
 * persistent objects.
 *
 * @param[out]   data       A pointer to buffer where the object list blob
 *                          should be stored
 * @param[out]   data_len   The length of the obtained object list blob in bytes
 *
 * @retval TEE_SUCCESS              Objcet list blob obtained
 * @retval TEE_ERROR_BAD_PARAMETERS Bad parameters passed to the function
 * @retval TEE_ERROR_GENERIC        Other error
 */
TEE_Result ListObjects(uint8_t *data, uint32_t *data_len);

#endif /* MB_TEE_PO_INTERNAL_H_ */
