/**
 *****************************************************************************************
 *	@file etn_mesh_fota_blb_srv.h
 *
 *	@brief eaton ble mesh fota blob transfer server model header file
 *
 *	@details this custom model from eaton, used to transfer binary large object from client to server. 
 *
 *	@version C Style Guide Version 1.0
 *
 *	@copyright 2022 Eaton Corporation. All Rights Reserved.
 *
 *****************************************************************************************
 */
#ifndef ETN_MESH_FOTA_BLB_SRV_H
#define ETN_MESH_FOTA_BLB_SRV_H

/*
 *****************************************************************************************
 *		Headers
 *****************************************************************************************
 */
#include <bluetooth/mesh/model_types.h>
#include <etn_mesh_fota.h>
#include <etn_mesh_fota_blb.h>

#ifdef __cplusplus
extern "C" {
#endif

/*
 *****************************************************************************************
 *		Defines
 *****************************************************************************************
 */

/*
 *****************************************************************************************
 *		Constants
 *****************************************************************************************
 */

/*
 *****************************************************************************************
 *		Variables
 *****************************************************************************************PLAT
 */

/*
 *****************************************************************************************
 *		Structures
 *****************************************************************************************
 */

struct bt_mesh_etn_fota_blb_srv;

extern const struct bt_mesh_model_op _bt_mesh_etn_fota_blb_srv_op[];
extern const struct bt_mesh_model_cb _bt_mesh_etn_fota_blb_srv_cb;

/** @def BT_MESH_MODEL_ETN_FOTA_BLB_SRV
 *
 * @brief eaton mesh fota blob transfer server model composition data entry.
 *
 * @param[in] _blbsrv Pointer to a @ref bt_mesh_etn_fota_blb_srv instance.
 */
#define BT_MESH_MODEL_ETN_FOTA_BLB_SRV(_blbsrv)                                \
	BT_MESH_MODEL_VND_CB(BT_MESH_COMPANY_ID_EATON,                       \
			     BT_MESH_MODEL_ID_ETN_FOTA_BLB_SRV,             \
			     _bt_mesh_etn_fota_blb_srv_op, &(_blbsrv)->pub,    \
			     BT_MESH_MODEL_USER_DATA(                          \
				struct bt_mesh_etn_fota_blb_srv, _blbsrv),     \
			     &_bt_mesh_etn_fota_blb_srv_cb)


/** Bluetooth Mesh eaton fota blob server state access handlers. */
struct bt_mesh_etn_fota_blb_srv_handlers {
	
	/** @brief Called after the node has been provisioned, or after all
	 * mesh data has been loaded from persistent storage.
	 *
	 * @param[in] blbsrv blob transfer server instance that has been started.
	 */
	void (*const start)(struct bt_mesh_etn_fota_blb_srv *blbsrv);

	/** @brief state handler for transfer start
	 *
	 * @param[in] blbsrv blob server instance
	 * @param[in] ctx Context of the incoming message.
	 * @param[in] fota_blb_trans_start Pointer to structure fota_blb_trans_start
	 */
	void ( *const blob_transfer_get )( struct bt_mesh_etn_fota_blb_srv *blbsrv, struct bt_mesh_msg_ctx *ctx);

	/** @brief state handler for transfer start
	 *
	 * @param[in] blbsrv blob server instance
	 * @param[in] ctx Context of the incoming message.
	 * @param[in] fota_blb_trans_start Pointer to a blb trans start
	 */
	void ( *const blob_transfer_start )( struct bt_mesh_etn_fota_blb_srv *blbsrv, struct bt_mesh_msg_ctx *ctx, const struct fota_blb_trans_start *trans_start );

	/** @brief state handler for transfer cancel
	 *
	 * @param[in] blbsrv blob server instance
	 * @param[in] ctx Context of the incoming message.
	 */
	void ( *const blob_transfer_cancel )( struct bt_mesh_etn_fota_blb_srv *blbsrv, struct bt_mesh_msg_ctx *ctx, struct fota_blb_current_values *curr_values );

	/** @brief state handler for block get
	 *
	 * @param[in] blbsrv blob server instance
	 * @param[in] ctx Context of the incoming message.
	 */
	void ( *const blob_block_get )( struct bt_mesh_etn_fota_blb_srv *blbsrv, struct bt_mesh_msg_ctx *ctx );

	/** @brief state handler for block start
	 *
	 * @param[in] blbsrv blob server instance
	 * @param[in] ctx Context of the incoming message.
	 */
	void ( *const blob_block_start )( struct bt_mesh_etn_fota_blb_srv *blbsrv, struct bt_mesh_msg_ctx *ctx, struct fota_blb_block_start *block_start );

	/** @brief state handler for block chunck transfer
	 *
	 * @param[in] blbsrv blob server instance that received the test message.
	 * @param[in] ctx Context of the incoming message.
	 * @param[in] msg Pointer to a received text message terminated with
	 * a null character, '\0'.
	 */
	void ( *const block_chunck_transfer )( struct bt_mesh_etn_fota_blb_srv *blbsrv, struct bt_mesh_msg_ctx *ctx, const struct fota_blb_data_flash *flash_data);

	/** @brief state handler for blob info get
	 *
	 * @param[in] blbsrv blob server instance that received the test message.
	 * @param[in] ctx Context of the incoming message.
	 */
	void ( *const blb_info_get)(struct bt_mesh_etn_fota_blb_srv *blbsrv, struct bt_mesh_msg_ctx *ctx);

	/* error reporting call back */
	void ( *const error_reporting )( struct bt_mesh_etn_fota_blb_srv *blbsrv, struct bt_mesh_msg_ctx *ctx, struct etn_fota_error *err_report );

	void (*const download_completed)(struct bt_mesh_etn_fota_blb_srv *blbsrv,struct fota_blb_current_values *curr_values);
};

/**
 * Bluetooth Mesh eaton fota blob server model context.
 */
struct bt_mesh_etn_fota_blb_srv 
{
	/** Access model pointer. */
	struct bt_mesh_model *model;
	/** Publish parameters. */
	struct bt_mesh_model_pub pub;
	/** Publication message. */
	struct net_buf_simple pub_msg;
	/** Publication message buffer. */
	uint8_t buf[BT_MESH_MODEL_BUF_LEN(BT_MESH_ETN_FOTA_BLB_CHUNK_TRANS_OP, BT_MESH_ETN_FOTA_BLB_CHUNK_TRANS_MSG_MINLEN )];
	/** Handler function structure. */
	const struct bt_mesh_etn_fota_blb_srv_handlers *handlers;
};

/** @brief sends blob transfer status to mentioned server
 *
 * @param[in] blbsrv blob server model instance to send chunk data
 * @param[in] addr Address of the blob server to send chunk data
 *
 * @retval 0 Successfully sent get opcode
 * @retval -EINVAL The model is not bound to an application key.
 * @retval -EAGAIN The device has not been provisioned.
 */
int bt_mesh_etn_fota_blb_srv_blob_transfer_status_send(struct bt_mesh_etn_fota_blb_srv *blbsrv, struct bt_mesh_msg_ctx *ctx, const struct fota_blb_trans_status *trans_status);


/** @brief sends blob block status to mentioned server
 *
 * @param[in] blbsrv blob server model instance to send block status
 * @param[in] addr Address of the blob server to send block status 
 *
 * @retval 0 Successfully sent get opcode
 * @retval -EINVAL The model is not bound to an application key.
 * @retval -EAGAIN The device has not been provisioned.
 */
int bt_mesh_etn_fota_blb_srv_blob_block_status_send(struct bt_mesh_etn_fota_blb_srv *blbsrv, struct bt_mesh_msg_ctx *ctx, const struct fota_blb_block_status *block_status);

/** @brief sends blob partial report status to mentioned server
 *
 * @param[in] blbsrv blob server model instance to send block status
 * @param[in] addr Address of the blob server to send block status 
 *
 * @retval 0 Successfully sent get opcode
 * @retval -EINVAL The model is not bound to an application key.
 * @retval -EAGAIN The device has not been provisioned.
 */
int bt_mesh_etn_fota_blb_srv_blob_block_partial_report_send(struct bt_mesh_etn_fota_blb_srv *blbsrv, struct bt_mesh_msg_ctx *ctx, const struct fota_blb_block_prtl_rpt *partl_rpt);

/** @brief sends blob info status to mentioned server
 *
 * @param[in] blbsrv blob server model instance to send block info status
 * @param[in] addr Address of the blob server to send block info status 
 *
 * @retval 0 Successfully sent get opcode
 * @retval -EINVAL The model is not bound to an application key.
 * @retval -EAGAIN The device has not been provisioned.
 */
int bt_mesh_etn_fota_blb_srv_blob_info_status_send(struct bt_mesh_etn_fota_blb_srv *blbsrv, struct bt_mesh_msg_ctx *ctx, const struct fota_blb_info_status *info_status);

void bt_mesh_etn_fota_blb_srv_print_data( void );


/** @brief print blob server opcodes 
 *
 * @retval [none].
 */
void bt_mesh_etn_fota_blb_srv_print_opcode_value( void );

void bt_mesh_etn_fota_blb_srv_update_metadata(struct fota_dis_metadata *metadata);

#ifdef __cplusplus
}
#endif

#endif