/**
 *****************************************************************************************
 *	@file model_handler_cli.h
 *
 *	@brief eaton ble mesh fota client model handler
 *
 *	@details this custom model handler from eaton, used for handling client models
 *
 *	@version C Style Guide Version 1.0
 *
 *	@copyright 2022 Eaton Corporation. All Rights Reserved.
 *
 *****************************************************************************************
 */

#ifndef MODEL_HANDLER_CLI_H__
#define MODEL_HANDLER_CLI_H__

#include <bluetooth/mesh.h>

#define BLB_CLI_SEND_OPCODE_WORK_DELAY     1500

#define CLI_WAIT_TIMEOUT    BLB_CLI_SEND_OPCODE_WORK_DELAY * 5

enum blbcli_firmware_states{
    FW_STATE_NONE,
    FW_STATE_DFU_CMD_SEND,
    FW_STATE_DFU_CMD_WAIT,
    FW_STATE_DFU_CMD_DONE,
    FW_STATE_METADATA_SEND,
    FW_STATE_METADATA_WAIT,
    FW_STATE_METADATA_DONE,
    FW_STATE_PREPARE_DATA,
    FW_STATE_BLOB_TRANS_START_SEND,
    FW_STATE_BLOB_TRANS_START_WAIT,
    FW_STATE_BLOB_TRANS_START_DONE,
    FW_STATE_BLOB_TRANS_GET_SEND,
    FW_STATE_BLOB_TRANS_GET_WAIT,
    FW_STATE_BLOB_TRANS_GET_DONE,
    FW_STATE_BLOB_TRANS_CANCEL_SEND,
    FW_STATE_BLOB_TRANS_CANCEL_WAIT,
    FW_STATE_BLOB_TRANS_CANCEL_DONE,
    FW_STATE_BLOCK_TRANS_START_SEND,
    FW_STATE_BLOCK_TRANS_START_WAIT,
    FW_STATE_BLOCK_TRANS_START_DONE,
    FW_STATE_BLOCK_TRANS_GET_SEND,
    FW_STATE_BLOCK_TRANS_GET_WAIT,
    FW_STATE_BLOCK_TRANS_GET_DONE,
    FW_STATE_BLOCK_CHUNK_TRANS_SEND,
    FW_STATE_BLOCK_CHUNK_TRANS_WAIT,
    FW_STATE_BLOCK_CHUNK_TRANS_DONE,
    FW_STATE_CHUNK_TRANS_MISS_SEND,
    FW_STATE_CHUNK_TRANS_MISS_DONE,
    FW_STATE_BLOCK_TRANS_COMPLETE,
    FW_STATE_BLOB_TRANS_COMPLETE,
    FW_STATE_BLOB_INFO_SEND,
    FW_STATE_BLOB_INFO_WAIT,
    FW_STATE_BLOB_INFO_DONE,
    FW_STATE_FIRMWARE_TRANS_COMPLETED,
    FW_STATE_PROGRESS_REQUEST,
    FW_STATE_PROGRESS_WAIT_RES,
    FW_STATE_PROGRESS_GET_DONE,
    FW_STATE_REBOOT_SEND,
    FW_STATE_HOLD_FOR_DEBUG,
};

/*enum blbcli_firmware_transfer_states{
    FOTA_STATE_NONE=0,
    FOTA_STATE_PREPARE_DATA,
    FOTA_STATE_
};*/

struct blbcli_firmware_values{
    uint16_t server_id;
    uint16_t miss_these_chunks;
    uint32_t file_size_in_bytes;
    uint32_t transferd_bytes;
    uint8_t num_of_blobs;
    uint8_t num_of_blocks_remaining;
    uint8_t num_of_chunks_remaining;
    uint8_t num_of_byte_remaining;
    bool blob_main_comp_flag;
    bool block_main_comp_flag;
    bool chunk_main_comp_flag;
    uint8_t blob_counter;
    uint16_t block_counter;   
    uint8_t block_to_send_in_blob;
    uint8_t chunk_to_send_in_block;
    uint16_t curr_block_size;
    uint8_t block_max_counter;
    uint8_t chunk_max_counter;
    uint16_t send_chunks_bits;
    uint16_t send_chunk_shift;
    uint16_t chunk_number;
    uint32_t flash_addr;
    uint32_t flash_length;
    uint16_t chunk_miss_mask;
    uint16_t remain_chunk_mask_max;
    uint16_t remain_chunk_mask_index;
    uint16_t missing_chunks;
    uint8_t percent_complete;
    uint8_t firmware_index;
}__packed;

#ifdef __cplusplus
extern "C" {
#endif

const struct bt_mesh_comp *model_handler_cli_init(void);

void start_firmware_transfer_state_machine_in_handler(void);
uint16_t cal_crc16( uint8_t const* data, uint32_t length, uint16_t init_crc );

#ifdef __cplusplus
}
#endif

#endif /* MODEL_HANDLER_CLI_H__ */
