
#include "exo_osal.h"
#include "exo_common.h"
#include "obc_main.h"
#include "obc_ipc.h"

extern exo_sw_dbg_cb exo_sw_dbg;

int obc_ipc_sts = 0;
os_itc_msg_handle_t obc_send_ipc;
//uint8 obc_ipc_snd_buff[1024] = {0};

//Prepare tm data such that -> seq_no + tm_ctrl + tm_data
uint16 obc_prepare_ipc_msg_seq_no = 0;

uint16_t sequence_number = 0;
uint8_t radio_module_id = 0;

int obc_prepare_ipc_msg(uint8 *tm_data, uint16 tm_data_len, uint16 tm_id, uint16 src_id)
{
    //uint8 *obc_ipc_snd_buff = os_malloc_eram(tm_data_len + sizeof(obc_prepare_ipc_msg_seq_no) + 2);
    /* Using eram allocation caused code to crash when 1KB of data is pushed from PS to OBC Socket while using TTY DBG TC */
    /* Need to be debugged further */
    uint8 *obc_ipc_snd_buff = os_malloc(tm_data_len + sizeof(obc_prepare_ipc_msg_seq_no) + 2);

    if(NULL == obc_ipc_snd_buff)
    {
        obc_ipc_sts = -1;
    }
    else
    {
        //    os_memcpy(obc_ipc_snd_buff, &obc_prepare_ipc_msg_seq_no, sizeof(obc_prepare_ipc_msg_seq_no));
        //
        obc_ipc_snd_buff[2] = 0x70;
        //    obc_ipc_snd_buff[3] = 1;
        get_seq_no_radio_id(obc_ipc_snd_buff);
        os_memcpy(&obc_ipc_snd_buff[TM_PLD_IDX], tm_data, tm_data_len);
        //TODO tm_data can be os_freed here since tm_data is memcopied to another buffer
        //os_free(tm_data) - check if there are any other tm_data free

        // Prepare IPC data
        obc_send_ipc.src_entity = src_id;
        //TODO - Update value with macro - This should be taken care once header file is available with macro when porting is done
        obc_send_ipc.Msg_id = tm_id;
        obc_send_ipc.Msg_len = tm_data_len;
        obc_send_ipc.pld.pld_ptr = obc_ipc_snd_buff;

        obc_ipc_sts = os_itc_msg_send(&obc_send_ipc,
                TM_STORAGE,
                os_send_itc_wait);

        if(ral_success == obc_ipc_sts)
        {

        }
        else
        {
            //DEBUG_CPRINT(("OBC APP ITC Message send fail\n"));
        }
    }
    return obc_ipc_sts;
}

int obc_send_ipc_msg(uint16_t msg_id, uint16 dst_id)
{
    // Prepare IPC data
    obc_send_ipc.src_entity = OBC_MAIN;

    //TODO - Update value with macro - This should be taken care once header file is available with macro when porting is done
    obc_send_ipc.Msg_id = msg_id;
    obc_send_ipc.Msg_len = 0;
    obc_send_ipc.pld.pld_ptr = NULL;

    obc_ipc_sts = os_itc_msg_send(&obc_send_ipc,dst_id,os_send_itc_wait);

    if(ral_success == obc_ipc_sts)
    {

    }
    else
    {
        //DEBUG_CPRINT(("OBC APP ITC Message send fail\n"));
    }
    return obc_ipc_sts;
}

int obc_send_ipc_msg_pld(uint16_t msg_id, uint8_t dst_id, uint8_t *pld, uint16_t pld_size)
{

    // Prepare IPC data
    obc_send_ipc.src_entity = OBC_MAIN;

    //TODO - Update value with macro - This should be taken care once header file is available with macro when porting is done
    obc_send_ipc.Msg_id = msg_id;
    obc_send_ipc.Msg_len = pld_size;
    obc_send_ipc.pld.pld_ptr = os_malloc_eram(pld_size);
    os_memcpy(obc_send_ipc.pld.pld_ptr,pld,pld_size);

    obc_ipc_sts = os_itc_msg_send(&obc_send_ipc,dst_id,os_send_itc_wait);

    if(ral_success == obc_ipc_sts)
    {

    }
    else
    {
    	os_free(obc_send_ipc.pld.pld_ptr);
        DEBUG_CPRINT(("OBC APP ITC Message send fail\n"));
    }
    return obc_ipc_sts;
}

int obc_send_ipc_msg_pld_eps(uint8_t dst_id, uint8_t pld_info, uint8_t msg_id, uint8_t *pld, uint8_t pld_size)
{
	os_itc_msg_handle_t obc_send_ipc;
	obc_send_ipc.src_entity = OBC_MAIN;

	obc_send_ipc.Msg_id 	  = msg_id;
	obc_send_ipc.pld_info 	  = pld_info;
	obc_send_ipc.Msg_len	  = pld_size;
	//obc_send_ipc.pld.pld_data = pld_data;

	if(pld_size)
	{
		obc_send_ipc.pld.pld_ptr = os_malloc_eram(pld_size);
		os_memcpy(obc_send_ipc.pld.pld_ptr,pld,pld_size);
	}
	else
		obc_send_ipc.pld.pld_ptr = NULL;

	if(os_itc_msg_send(&obc_send_ipc,dst_id,os_send_itc_wait) != ral_success)
	{
		os_free(obc_send_ipc.pld.pld_ptr);
		obc_send_ipc.pld.pld_ptr = NULL;
		return -1;
	}
	else
            return obc_ipc_sts;
}

void set_seq_no_radio_id(uint8_t* data)
{
    os_memcpy(&sequence_number,data,sizeof(sequence_number));
    os_memcpy(&radio_module_id,&data[TC_RADIO_MOD_IDX],sizeof(radio_module_id));
}

void get_seq_no_radio_id(uint8_t* data)
{
    os_memcpy(data,&sequence_number,sizeof(sequence_number));
    data[TM_CTRL_DATA_IDX] = TM_DEFAULT_CTRL_DATA;
    os_memcpy(&data[TM_RADIO_MOD_IDX],&radio_module_id,sizeof(radio_module_id));
}
uint8 get_radio_module_id()
{
    return radio_module_id;
}
void set_radio_module_id(uint8_t radio_id)
{
    radio_module_id = radio_id;
}
