/**
 * @file schedular.c
 *
 * @brief This file contains APIs for EPS controller.
 *
 * The functions provided in the file has eps control thread that monitors and controls eps battery mode,interrupt errors and output channels
 *
 * @copyright Copyright 2023 Antaris, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*********************************************************************/

#ifndef SCHEDULAR_H
#define SCHEDULAR_H

#include <stdio.h>
#include <stdint.h>
#include "exo_io_al_sos_timer.h"

#define SCH_PRINT_EN    //
#define EXT_LUT_RAM
//#define SCH_FIRST_TRIG_EN

#define SEC_TO_MSECS               1000
#define SECS_PER_MIN               1      //60  min value 1 max value 60
#define MAX_ORBIT_DAYS             2
#define TWODAYS_IN_SECS            1728000
#define SCH_GRP_ID_MAX_CNT         2
#define LUT_CTRL                   1
#define LUT_STATUS_CHECK           2
#define CONOPS_SCH_LUT_STS_IDLE     0xFF


#define CONOPS_SCH_MAX_EXEC_DAYS 2

#define SCH_ZES_TIMER   600*1000  //10 mins

#define SCH_INIT_SYNC_TMR 90*60*1000
#define EVNT_BFR_OFFSET 425
#define SCH_OBC_SYNC_EVNT_STRT 0xA5
#define SCH_OBC_SYNC_EVNT_STOP 0x5A
#define LUT_SIZE    6144

/**
 * @brief enum for LUT trigger request
 */

typedef enum
{
	CONOPS_SCH_LUT0_GRP_STS=0,
	CONOPS_SCH_LUT1_GRP_STS,
	CONOPS_SCH_CRNT_STS_INFO,
	CONOPS_SCH_CRNT_LUT_CNTR,
	CONOPS_SCH_CRNT_SEG_CNT,
	CONOPS_SCH_CRNT_EVNT_CNT
}conops_sch_trig_req;


/**
 * @brief state value of scheduler main thread conops_sch_proc
 */

typedef enum
{
	CONOPS_SCH_LUT_COPY=0,
	CONOPS_SCH_LUT_SAVE,
	CONOPS_SCH_LUT_STRT,
	CONOPS_SCH_LUT_PAUSE,
	CONOPS_SCH_LUT_STOP,
	CONOPS_SCH_LUT_ERASE,
	CONOPS_SCH_LUT_CMPLT,
	CONOPS_SCH_EVNT_START,
	CONOPS_SCH_EVNT_EXEC,
	CONOPS_SCH_EVNT_NXT_TMR,
	CONOPS_SCH_EVNT_ZES_EXEC,
	CONOPS_SCH_LUT_SAVE_STRT

}conops_sch_ipc_evnts_e;

/**
 * @brief success failure state of conops scheduler
 */

typedef enum
{
  CONOPS_SCH_SUCSS=0,
  CONOPS_INP_ERR=1,
  CONOPS_PARAM_ERR=1,
  CONOPS_MEM_ERR=2,
  CONOPS_SCH_ERR=3,
  CONOPS_RTC_ERR=4,
}conops_sch_sts_e;

/**
 * @brief event id present in scheduler LUT
 */

typedef enum
{
    TC_PLD_HEALTH_CHK = 100,
    TC_OGS_CONTACT_OGS = 101,
    TC_OGS_CONTACT_OGS1 = 103,
    TC_SW_UPDT = 104,

    NET_ENTER_GEOFENCE = 300,
    NET_EXIT_GEOFENCE = 301,
    NET_FIRM_UPDT = 302,
    NET_DOWNLINK_LOGS = 303,
    NET_PWR_CYCLE = 304,
    NET_PASS_THR_CMD = 305,

    ZES_DBG_MODE = 401,
    ZES_FLIGHT_MODE = 402,
    ZES_FIRM_UPDT = 402,
    ZES_APP_UPDT = 403,

    SAYA_SEQA = 500,
    SAYA_SEQB = 501,

    MAX_STAGED_TC_EVNT_ID

}staged_tc_event_id_e;


/**
 *
 * @brief ADCS sequence trigger
 *
 */

typedef enum
{
	SCH_ADCS_POINT_NADIR =1,
	SCH_ADCS_SUN_TRACK=2,
	SCH_ADCS_BFR_TRIG = 3,
	SCH_ADCS_FALL_BACK = 4
}conops_sch_adcs_evnt_e;


/**
 * @brief Event that controls in individual payload messages
 *
 */

typedef enum
{
	SCH_STAGED_TC =1,
	SCH_VM_ON =2,
	SCH_VM_OFF = 3
}conops_sch_sch_ps_evnt_e;

/**
 * @brief Event that controls OTSU boot mechanism
 */
typedef enum
{
    SCH_OSTU_SCH_BOOT = 1,
	SCH_OSTU_GS_SYNC_WAIT = 2,
} sch_otsu_evnt_e;


/**
 * @brief 
 */
typedef enum
{
   SCH_LUT_IDLE =0,
   SCH_LUT_EXEC_CMPLT,
   SCH_LUT_EXEC_STOP,
   SCH_LUT_EXEC_PAUSE,
   SCH_LUT_EXEC_PROG,
   SCH_LUT_RDY,
   SCH_LUT_RX_PROG
}conops_sch_lut_grp_sts_e;


typedef enum
{
    SCH_SRC_GS = 1,
    SCH_SRC_MAX
}conops_sch_src_id_e;


/**
 * @brief scheduler date information structure
 */
typedef struct sch_date_info_t
{
    uint8_t date;  //conops_sch_exec_day_s
    uint8_t month;  //conops_sch_exec_day_s
} __attribute__((__packed__))conops_sch_date_s;


/**
 * @brief scheduler event execution structure
 */
typedef struct sch_evnt_t
{
	uint8_t  src_id;
	uint8_t  dest_id;   //conops_sch_dest_id
	uint16_t id;        //id present in conops_sch_dest_id
	uint32_t arg1;
	uint32_t arg2;
}conops_sch_evnt_s;

/**
 * @brief scheduler event information structure for frame reception
 */
typedef struct sch_evnt_info_t
{
	uint32_t epoch;     //sys_time_s
	uint8_t  src_id;
	uint8_t  dest_id;   //conops_sch_dest_id
	uint16_t  id;        //id present in conops_sch_dest_id
	uint32_t arg1;
	uint32_t arg2;
}__attribute__((__packed__))conops_sch_evnt_info_s;

/**
 * @brief scheduler lut information structure for frame reception
 */
typedef struct
{
   uint8_t  grp_id;          //group id of overall schedular lut info table
   uint8_t  totl_cnt;        //total cnt present in this group
   uint8_t  crnt_num;        //crnt lut info count
   uint8_t  trig_type;    	 //conops_sch_trig_type_s
   uint8_t  evnt_info_cnt;   //number of conops_sch_evnt_info_s present
   conops_sch_evnt_info_s *evnt_buff;
}conops_sch_lut_info_rcd_s;   //information of recieved lut function
//__attribute__((__packed__))

/**
 * @brief  scheduler segment structure for frame reception
 */
typedef struct
{
  uint8_t  trig_type;    	 //conops_sch_trig_type_s
  uint8_t  evnt_info_cnt;
  conops_sch_evnt_info_s *evnt_buff;
}conops_sch_lut_seg_map_s;

/**
 * @brief scheduler lut buffer structure for processing
 */
typedef struct
{
   uint8_t  grp_sts;              //conops_sch_lut_grp_sts
   uint8_t  totl_seg;             //total cnt present in this group
   uint8_t  crnt_seg_cnt;         //conops_sch_lut_grp_sts
   uint8_t  crnt_evnt_cnt;        //conops_sch_lut_grp_sts
   uint64_t totl_seg_bin;          //conops_sch_lut_grp_sts
   uint64_t rcd_seg_bin;          //conops_sch_lut_grp_sts
   uint64_t cmpl_seg_bin;         //conops_sch_lut_grp_sts
   conops_sch_lut_seg_map_s *seg_map;
}conops_sch_lut_info_s;


void conops_sch_lut_info_tm_pack(uint8_t cmd, uint8_t **tm_frame, uint16_t *tm_frame_len);

/**
* @brief This API initialize the scheduler main FSM thread
*/

void conops_sch_init();

/**
 * @brief copy events received from ground station to scheduler local LUT buffer
 *
 * @param[in] data: pointer that data received from ground station
 *
 * @return uint16_t
 * @retval 0xFFFF - improper group id, other values gives information of received
 * 					and decoded event count of a group segments
 */

uint16_t conops_sch_lut_info_segmnt_copy(uint8_t *_data);

/**
 * @brief This API return latest information on total segment present / received in a group.
 *
 * @param[in] _group_id: group id information
 *
 * @return uint32_t
 * @retval 0  - group id not matched or no segment received
 *
 */

uint32_t conops_sch_lut_get_grp_rcd_seg_info(uint8_t _group_id);

/**
 * @brief This API return latest information on total segment executed in a group.
 *
 * @param[in] _group_id: group id information
 *
 * @return uint32_t
 * @retval 0  - group id not matched or no segment received
 *
 */

uint32_t conops_sch_lut_get_grp_exec_info(uint8_t _group_id);

/**
 * @brief This API triggers start stop of scheduler LUT execution of mentioned group
 *
 * @param[in] _data : pointer that gives information of group id and request information
 *
 * @return conops_sch_sts_e : success/ failure status of conversion
 * @retval  zero - success , greater than zero failure
 *
 */

conops_sch_sts_e conops_sch_lut_trig_req(uint8_t *_data);


conops_sch_sts_e conops_sch_send_ipc_msg_pld_buff(uint8_t dst_id, uint8_t pld_info, uint8_t msg_id,ptr_typ pld_addr, uint8_t pld_size);
#endif
