/**
 * @file obc_main.h
 *
 * @brief This file contains function prototypes and data definitions that helps
 * to create a OBC application
 */

#ifndef OBC_SAMP_APP_MAIN_H_
#define OBC_SAMP_APP_MAIN_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "exo_osal.h"
#include "obc_tc_tm.h"
//#include <pthread.h>

#ifdef __cplusplus
extern "C" {
#endif
    /**@}*/
/**\name C standard macros */
#ifndef NULL
#ifdef __cplusplus
#define NULL                                      0
#else
#define NULL                            ((void *) 0)
#endif
#endif

/* One orbit duration = 90minutes 
 * Every minute enqueing 1instance = 90 instance for one orbit
 * For 3 orbit 90*3 instance */
#define OBC_MAX_BUFF_SIZE         90*3   

typedef struct ft_tctm_pld_tx
{
    uint8_t *payload_ptr;
    uint16_t payload_len;
    uint16_t tc_tm_id;
    uint8_t send_request;
}ft_tctm_payload_tx;
/**
 * @brief OBC health metric buffer info
 * Periodicity every 1min
 * Buffer size = sizeof(s_obc_health_info)*90*3
 *
 */
typedef struct
{
    /** Timestamp*/
    uint64_t timestamp;

    /** FSM current state */
    uint8_t fsm_state;

    /** No of OBC resets */
    uint8_t num_resets;

    /** IO errors */
    uint16_t io_err;

    /** System errors */
    uint8_t sys_err;

    /** CPU Utilization*/
    float cpuUtil;

    /** Remaining iram memory info*/
    uint32_t iram_rem_heap;


    /** Remaining eram memory info*/
    uint32_t eram_rem_heap;

    /** OBC uptime */
    uint32_t uptime;

    /** System reset cause */
    uint8_t reset_cause;

    /** IPC  fail counter **/
    uint16_t ipc_fail_cntr[OS_TASK_MAX];

}__attribute__((__packed__))s_obc_health_info;

/**
 * @brief Sensor information queue id
 */
typedef enum
{
        OBC_DATA_QUEUE_ID,
        OBC_QUEUE_ID_MAX
}obc_queue_id;


int32_t obc_main_init(void);
void obc_evnt_trig_thread(void *params);
int32_t obc_tc_hdlr(uint16_t tc_id, uint16_t tc_data_len, uint8_t *tc_data);
uint8_t ft_payload_transmitter(uint16_t tc_tm_id, uint8_t *payload_ptr, uint16_t payload_len);
uint8_t otsu_payload_transmitter_to_obc(uint16_t tc_tm_id, uint8_t *payload_ptr, uint16_t payload_len);
void obc_proc_sband_ground_sync_ind(void);
void obc_sband_nvm_upd_first_ground_contact(void);
void obc_sband_nvm_upd_otsu_ground_contact(void);
void obc_proc_sband_health_check_req(void);

void obc_timer_exp_send_health_info();
void obc_health_buff_timer_exp_cb();
void obc_health_data_send_tmr_strt();
void tc_request_health_data(uint8* tc_data);
void tc_chng_queue_priority(uint8* tc_data);
void tc_chng_module_priority(uint8* tc_data);
#ifndef LINUX_TEMP_PORT
void obc_get_cpu_load(obc_pck_data *tm_data_info); 
#endif
float linux_get_cpuload();
void linux_get_memusage(iram_heap_status *iram_lnx_sts);
uint64_t linux_get_uptime();
void ft_send_tc_to_ftm(uint16 tc_id, uint8 *tc_data, uint16 tc_data_len, uint16_t app_id);
/**
 * @brief This function to check socket connection status
 */
int check_socket_disconnected(int sockfd_info);

#ifdef __cplusplus
}
#endif
#endif /*OBC_SAMP_APP_MAIN_H_*/
