/**
 * @file adcs_csw.h
 *
 * @brief This file contains types and function prototypes of
 * ADCS controller software
 *
 * @copyright Copyright 2023 Antaris, Inc.
 */

#ifndef ADCS_CSW_H
#define ADCS_CSW_H

#ifdef LINUX_TEMP_PORT
#include <stdio.h>
#endif
#include "exo_osal.h"
#include "exo_ral_cmn.h"
#include "exo_ral_mem_management.h"
#include "exo_types.h"

#ifdef __cplusplus
extern "C"
{
#endif

/// ADCS requests
#define RESET_REQUEST (1)           ///< ADCS Reset request
#define BOOT_INIT_REQUEST (2)       ///< ADCS boot request
#define CONFIG_UPDATE_REQUEST (3)   ///< ADCS config update request
#define SENSOR_INIT_REQUEST (4)     ///< ADCS sensor init request
#define ACTUATOR_INIT_REQUEST (5)   ///< ADCS actuator init request
#define DETUMBLE_REQUEST (6)        ///< ADCS detumble request
#define COARSE_MOMENTUM_REQUEST (7) ///< coarse momentum request
#define FINE_MOMENTUM_REQUEST (8)   ///< fine momentum request
#define CONTROL_REQUEST (10)        ///< control request
#define ADCS_BYPASS_RQST (11)       ///< Bypass external request
#define ADCS_SHUTDOWN_RQST (12)     ///< Shutdown request
#define ADCS_TEST_RQST (13)         ///< Test request

/// ADCS events
#define ADCS_DEPLOY_SOLAR (3)                ///< Deploy solar panel
#define ADCS_SENSOR_RESET (4)                ///< ADCS sensor reset
#define ADCS_SENSOR_INIT (5)                 ///< ADCS sensor init
#define ADCS_SENSOR_CALIB_TRIGR (6)          ///< ADCS sensor calib trigger
#define ADCS_ACTUATOR_RESET (7)              ///< ADCS actuator reset
#define ADCS_ACTUATOR_INIT (8)               ///< ADCS actuator init
#define ADCS_SET_SENSOR_PARAMS (9)           ///< Set ADCS sensor params
#define ADCS_SET_DETUMBLE_PARAMS (10)        ///< Set ADCS detumble params
#define ADCS_SET_COARSE_MOMENTUM_PARAMS (11) ///< Set coarse momentum params
#define ADCS_SET_FINE_MOMENTUM_PARAMS (12)   ///< Set fine momentum params
#define ADCS_SET_ACTUATOR_PARAMS (13)        ///< Set actuator params
#define ADCS_SET_CONFIG_PARAMS (14)          ///< Set configuration params
#define ADCS_SET_SPG4_ORBIT_PARAMS (15)      ///< Set SPG4 params
#define ADCS_SET_CONTROL_MODE (16)           ///< Set control mode
#define ADCS_SET_NVM (17)                    ///< Set NVM parameters
#define ADCS_GET_SENSOR_PARAMS (18)          ///< Get ADCS sensor params
#define ADCS_GET_DETUMBLE_PARAMS (19)        ///< Get ADCS detumble params
#define ADCS_GET_COARSE_MOMENTUM_PARAMS (20) ///< Get coarse momentum params
#define ADCS_GET_FINE_MOMENTUM_PARAMS (21)   ///< Get fine momentum params
#define ADCS_GET_ACTUATOR_PARAMS (22)        ///< Get actuator params
#define ADCS_GET_CONFIG_PARAMS (23)          ///< Get configuration params
#define ADCS_GET_SPG4_ORBIT_PARAMS (24)      ///< Get SPG4 params
#define ADCS_GET_CONTROL_MODE (25)           ///< Get control mode
#define ADCS_GET_NVM (26)                    ///< Get NVM parameters
#define ADCS_READ_SENSOR_MEASUREMENT (27)    ///< read sensor measurement
#define ADCS_READ_ACTUATOR_MEASUREMENT (28)  ///< read actuator measurement
#define ADCS_SENSOR_TEST (29)                ///< sensor test
#define ADCS_ACTUATOR_TEST (30)              ///< actuator test
#define ADCS_FIRMWARE_UPDATE_INIT (31)       ///< firmware update initilization
#define ADCS_FIRMWARE_DOWNLOAD_FILE (32)     ///< firmware download file
#define ADCS_FIRMWARE_READ_BACK (33)         ///< firmware read back
#define ADCS_FIRMWARE_CHECKSUM_TEST (34)     ///< firmware checksum test
#define ADCS_CHANGE_ATTITUDE (35)            ///< change attitude

///ADCS error codes
typedef enum
{   
    ADCS_IPC_ERR  = -2,                 ///< Error in IPC operation
    ADCS_NULL_PTR = -1,                 ///< Null pointer error
    ADCS_IPC_OK   = 2                   ///< No error
}e_adcs_csw_sts;

/// ADCS States
#define ADCS_RESET_STATE (0)           ///< Reset state
#define ADCS_BOOT_INIT_STATE (1)       ///< Boot initilization state
#define ADCS_CONFIG_UPDT_STATE (2)     ///< Configuration update state
#define ADCS_SENSOR_INIT_STATE (3)     ///< Sensor init state
#define ADCS_ACTUATOR_INIT_STATE (4)   ///< Actuator init state
#define ADCS_DETUMBLE_STATE (5)        ///< Detumble state
#define ADCS_COARSE_MOMENTUM_STATE (6) ///< Coarse momentum state
#define ADCS_FINE_MOMENTUM_STATE (7)   ///< Fine momentum state
#define ADCS_CONTROL_STATE (8)         ///< Control state
#define ADCS_MONITOR_STATE (9)         ///< Monitor state
#define ADCS_TELEMETRY_STATE (10)      ///< Telemetry state
#define ADCS_TEST_STATE (11)           ///< Test state
#define ADCS_FOTA_STATE (12)           ///< FOTA state
#define ADCS_SHUTDOWN_STATE (13)       ///< Shutdown state
#define ADCS_ERROR_STATE (14)          ///< Error state
#define ADCS_BYPASS_STATE (15)         ///< Bypass state

/**
 * @brief This API invokes a thread to handle the ADCS IPC
 * messages
 *
 * @return status of the thread creation
 * @retval ral_success -> Success / else -> failure.
 */
int32 adcs_csw_main(void);

/**
 * @brief This API is used to get the IPC receive handler of ADCS
 * controller software
 *
 * @param[out] hdlr  : Pointer to receive handler.
 *
 * @return status of the ADCS thread creation
 * @retval ADCS_IPC_OK -> Success / Other values ->failure.
 */
e_adcs_csw_sts adcs_get_thread_hdlr(os_thread_handle_ptr *hdlr);


/**
 * @brief This function deletes the ADCS queue and release its memory
 *
 * @return status of os thread suspend
 * @retval Success->ral_success, Error->ral_err_invld_arg, ral_error
 */
os_status_t adcs_module_suspend(void);

/**
 * @brief This functionalities resumes the ADCS thread
 *
 * @return status of os thread resume
 * @retval Success->ral_success, Error->ral_err_invld_arg, ral_error
 */
os_status_t adcs_module_resume(void);

/**
 * @brief This function configure ADCS FSM state based on previous state.
 *
 * @param[in] adcs_new_state  : ADCS new FSM state.
 *
 * @return status of the ADCS state configuration
 * @retval OPERATION_SUCCESS -> Success / Other values ->failure.
 */
int8 set_adcs_fsm_state(uint8 adcs_new_state);

#ifdef __cplusplus
}
#endif
#endif /*ADCS_CSW_H */
