/**
 * @file fs_po.h
 * @brief Persistent object filesystem functionality declaration
 * @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.
 *
 * The file was taken from tzsl repo and modified for Multibuild project's
 * needs.
 */

#ifndef MB_FS_PO_H_
#define MB_FS_PO_H_

#include <stdint.h>
#include <protocol.h>

#define MB_FS_MAX_PATH_LEN 256

typedef enum MultibuildFsResultEnum {
  MB_FS_OK = 0,
  MB_FS_ERROR_NOT_FOUND = -1,
  MB_FS_ERROR_BAD_PARAMETERS = -2,
  MB_FS_ERROR_GENERIC = -3,
  MB_FS_ERROR_SHORT_BUFFER = -4,
} MultibuildFsResult;

/**
 * @brief Stores data to the persistent object specified by ID and owner's UUID
 *
 * @note The function must be preceded with FsPoInitUpdate() call and has no
 * effect until FsPoCommitUpdate() is called. This function is called in NWd
 * after the call to WriteToPO() from SWd
 *
 * @param[in]  owner_uuid       The UUID of the TA, which owns the persistent
 *                              object
 * @param[in]  owner_uuid_len   Length of owner's UUID in bytes
 * @param[in]  po_id            ID of persistent object the data must be stored
 *                              to
 * @param[in]  po_id_len        Length of persistent object's ID
 * @param[in]  data             A pointer to data buffer which must be stored to
 *                              the specified persistent object
 * @param[in]  data_len         Length of data to be stored in bytes
 * @param[in]  data_type        The type of data that should be written
 *
 * @retval MB_FS_OK                   The data is stored successfully
 * @retval MB_FS_ERROR_BAD_PARAMETERS Wrong parameters passed to the function
 * @retval MB_FS_ERROR_GENERIC        Any other error
 */
MultibuildFsResult FsPoStore(const uint8_t *owner_uuid,
                             uint32_t owner_uuid_len,
                             const uint8_t *po_id, uint32_t po_id_len,
                             const void *data, uint32_t data_len,
                             MultibuildFsStoreDataType data_type);

/**
 * @brief Reads data from the persistent object specified by ID and owner's UUID
 *
 * @note This function is called in NWd after the call to ReadFromPO() from SWd
 *
 * @param[in]      owner_uuid       The UUID of the TA, which owns the
 *                                  persistent object
 * @param[in]      owner_uuid_len   Length of owner's UUID in bytes
 * @param[in]      po_id            ID of persistent object the data must be
 *                                  stored to
 * @param[in]      po_id_len        Length of persistent object's ID
 * @param[in]      data             A pointer to data buffer where the data from
 *                                  the persistent object will be put
 * @param[in,out]  data_len         Before the call: size of data buffer in
 *                                  bytes. Output: length of read data in bytes
 * @param[in]      data_type        The type of data that should be read
 *
 * @retval MB_FS_OK                   The data is read successfully
 * @retval MB_FS_ERROR_BAD_PARAMETERS Wrong parameters passed to the function
 * @retval MB_FS_ERROR_NOT_FOUND      The specified object is not found
 * @retval MB_FS_ERROR_SHORT_BUFFER   Data buffer is too short to handle the
 *                                    object's data
 * @retval MB_FS_ERROR_GENERIC        Any other error
 */
MultibuildFsResult FsPoRead(const uint8_t *owner_uuid,
                            uint32_t owner_uuid_len,
                            const uint8_t *po_id, uint32_t po_id_len,
                            void *data, uint32_t *data_len,
                            MultibuildFsStoreDataType data_type);

/**
 * @brief Removes the persistent object specified by ID and owner's UUID from FS
 *
 * @note This function is called in NWd after the call to RemovePO() from SWd
 *
 * @param[in]      owner_uuid       The UUID of the TA, which owns the
 *                                  persistent object
 * @param[in]      owner_uuid_len   Length of owner's UUID in bytes
 * @param[in]      po_id            ID of persistent object to be removed
 * @param[in]      po_id_len        Length of persistent object's ID
 *
 * @retval MB_FS_OK                   Object removed successfully
 * @retval MB_FS_ERROR_BAD_PARAMETERS Wrong parameters passed to the function
 * @retval MB_FS_ERROR_NOT_FOUND      The specified object is not found
 * @retval MB_FS_ERROR_GENERIC        Any other error
 */
MultibuildFsResult FsPoRemove(const uint8_t *owner_uuid,
                              uint32_t owner_uuid_len,
                              const uint8_t *po_id, uint32_t po_id_len);

/**
 * @brief Inititalizes the object's update
 *
 * @note This function is called in NWd after the call to StartObjectUpdate()
 * from SWd. It must precede the calls to FsPoStore() and FsPoCommitUpdate().
 *
 * @param[in]      owner_uuid       The UUID of the TA, which owns the
 *                                  persistent object
 * @param[in]      owner_uuid_len   Length of owner's UUID in bytes
 * @param[in]      po_id            ID of persistent object to be updated
 * @param[in]      po_id_len        Length of persistent object's ID
 *
 * @retval MB_FS_OK                   Object update started successfully
 * @retval MB_FS_ERROR_BAD_PARAMETERS Wrong parameters passed to the function
 * @retval MB_FS_ERROR_NOT_FOUND      The specified object is not found
 * @retval MB_FS_ERROR_GENERIC        Any other error
 */
MultibuildFsResult FsPoInitUpdate(const uint8_t *owner_uuid,
                                  uint32_t owner_uuid_len,
                                  const uint8_t *po_id, uint32_t po_id_len);

/**
 * @brief Aborts the object's update
 *
 * @note This function is called in NWd after the call to AbortObjectUpdate()
 * from SWd. After this call all the data written to the object via FsPoStore()
 * will be skipped.
 *
 * @param[in]      owner_uuid       The UUID of the TA, which owns the
 *                                  persistent object
 * @param[in]      owner_uuid_len   Length of owner's UUID in bytes
 * @param[in]      po_id            ID of persistent object whose update must
 *                                  be terminated
 * @param[in]      po_id_len        Length of persistent object's ID
 *
 * @retval MB_FS_OK                   Object update aborted
 * @retval MB_FS_ERROR_BAD_PARAMETERS Wrong parameters passed to the function
 * @retval MB_FS_ERROR_NOT_FOUND      The specified object is not found
 * @retval MB_FS_ERROR_GENERIC        Any other error
 */
MultibuildFsResult FsPoAbortUpdate(const uint8_t *owner_uuid,
                                   uint32_t owner_uuid_len,
                                   const uint8_t *po_id, uint32_t po_id_len);

/**
 * @brief Finalizes the object's update
 *
 * @note This function is called in NWd after the call to CommitObjectUpdate()
 * from SWd. After this call all the data written to the object via FsPoStore()
 * will be stored in the object. This call must be preceded with corresponding
 * call to FsPoInitUpdate()
 *
 * @param[in]      owner_uuid       The UUID of the TA, which owns the
 *                                  persistent object
 * @param[in]      owner_uuid_len   Length of owner's UUID in bytes
 * @param[in]      po_id            ID of persistent object whose update must
 *                                  be finalized
 * @param[in]      po_id_len        Length of persistent object's ID
 *
 * @retval MB_FS_OK                   Object update complete
 * @retval MB_FS_ERROR_BAD_PARAMETERS Wrong parameters passed to the function
 * @retval MB_FS_ERROR_NOT_FOUND      The specified object is not found
 * @retval MB_FS_ERROR_GENERIC        Any other error
 */
MultibuildFsResult FsPoCommitUpdate(const uint8_t *owner_uuid,
                                    uint32_t owner_uuid_len,
                                    const uint8_t *po_id, uint32_t po_id_len);

/**
 * @brief Obtains persistent objects list for the given TA
 *
 * @note This function is called in NWd after the call to ListObjects()
 * from SWd. It obtains a blob with data regarding the list of persistent
 * objects which will be parsed in SWd.
 *
 * @param[in]      owner_uuid       The UUID of the TA, which owns the objects
 *                                  to be listed
 * @param[in]      owner_uuid_len   Length of owner's UUID in bytes
 * @param[out]     blob             A pointer to the buffer where the blob with
 *                                  the list of objects will be stored.
 * @param[in,out]  blob_len         In: the size of the buffer for storing the
 *                                  list blob; Out: The actual size of list blob
 *
 * @retval MB_FS_OK                   Object update complete
 * @retval MB_FS_ERROR_BAD_PARAMETERS Wrong parameters passed to the function
 * @retval MB_FS_ERROR_NOT_FOUND      The specified object is not found
 * @retval MB_FS_ERROR_SHORT_BUFFER   Data buffer is too short to store the data
 * @retval MB_FS_ERROR_GENERIC        Any other error
 */
MultibuildFsResult FsPoList(const uint8_t *owner_uuid,
                            uint32_t owner_uuid_len,
                            uint8_t *blob, uint32_t *blob_len);

/**
 * @brief Obtains REE time from NWD
 *
 * @param[out]     time               A pointer to structure where
 *                                    obtained time will be stored.
 * @retval MB_FS_OK                   Time obtained
 * @retval MB_FS_ERROR_BAD_PARAMETERS Wrong parameters passed to the function
*/
MultibuildFsResult ReeTimeGet(MB_Time *time);
#endif /* MB_FS_PO_H_ */
