/*
 * logger_decoder.c
 *
 *  Created on: 26-May-2023
 *      Author: Invcuser52
 */


#include "logger.h"
#include <string.h>
#include <stdio.h>
#include "obc_tc_tm.h"
#include "exo_tctm_ipc.h"
#include "file_integrity_check.h"
#include "nvm.h"

extern uint8_t              log_backup_en_dis_cfg;

/*
 * Configuration debug level macros
 */
#define ERROR_LOGS_EN   			0x01
#define WARNING_LOGS_EN 			0x02
#define INFO_L1_LOGS_EN 			0x04
#define INFO_L2_LOGS_EN 			0x08
#define INFO_L3_LOGS_EN 			0x10
#define ALL_LOGS_DIS				0x00
#define ALL_LOGS_EN     			(ERROR_LOGS_EN | WARNING_LOGS_EN | INFO_L1_LOGS_EN | INFO_L2_LOGS_EN |INFO_L3_LOGS_EN)


/*
 * Initial debug level configuration
 * user can configure log level for each module
 *
	bit 0 -> LOG_ERROR,
	bit 1 -> LOG_WARNING,
	bit 2 -> LOG_INFO_L1,
	bit 3 -> LOG_INFO_L2,
	bit 4 -> LOG_INFO_L3,

	All bit zero - disabled all debug levels
	All bit ones - enabled all debug levels
 */
uint8_t log_level_cfg[LOG_MODULE_MAX + 1]=
{
		(ALL_LOGS_EN),										                    /// LOG_ADCS
		(ALL_LOGS_EN),  										                /// LOG_OTSU
		(ALL_LOGS_EN),   										                /// LOG_COMMS
		(ALL_LOGS_EN),                                                          /// LOG_FTM
		(ALL_LOGS_EN),                                                          /// LOG_OBC
		(ALL_LOGS_EN),   										                /// LOG_EPS
		(ALL_LOGS_EN),                                                          /// LOG_TCTM
		(ALL_LOGS_EN),                                                          /// LOG_SCHEDULER
		(ALL_LOGS_EN),                                                          /// LOG AHW
        (ALL_LOGS_EN),                                                          /// LOG_LOGGER
		(ALL_LOGS_EN),                                                          /// LOG_ERROR_HANDLER
		(ALL_LOGS_EN),                                                         /// LOG_HM
		(ALL_LOGS_EN),                                                         /// LOG_IPCC
};


typedef struct
{
        uint8_t log_dwn_cfg : 3;
        uint8_t log_lev_mod_sel : 5;

}__attribute__((__packed__))log_mod_ctr_data;

/*This API configure logger level for specific or all module based on the input given */
static void log_level_config(uint8_t config_mode, uint8_t module_id, uint8_t *config_lev_data);

static void log_level_config(uint8_t config_mode, uint8_t module_id, uint8_t *config_lev_data)
{
    uint32_t chksum;
	if(config_mode == ALL_LOG_SET)
	{
		 memcpy(log_level_cfg, config_lev_data, module_id);
		 log_level_cfg[LOG_MODULE_MAX] = 0x00;

	}
	else
	{
		 if(module_id < LOG_MODULE_MAX)
		 {
			 log_level_cfg[module_id] = config_lev_data[0];
		 }
		 else
		 {
			 /* do nothing */
		 }

	}
	 chksum = calc_checksum_bulkdata(log_level_cfg, (uint32_t)LOG_MODULE_MAX+1);
	 nvm_parameter_write(NVM_DATA,NVM_LOGGER_LOG_LEVEL_CFG,log_level_cfg);
	 nvm_parameter_write(NVM_DATA,NVM_LOGGER_LOG_LEVEL_CHKSUM,(uint8_t *) &chksum);

}

void log_lev_cfg_decode(uint8_t *tc_data)
{
    uint8_t total_module = 0, mode_selc = 0, module_cnt =0, cntr =0;
    obc_pck_data tm_pack;
    total_module = tc_data[1];
    mode_selc = tc_data[0];
    switch(mode_selc)
    {
		case CONFIG_ALL_MODULE:
		case ALL_LOG_ALL_MODULE:
		case ERROR_LOG_ALL_MODULE:
		case DEFAULT_LEVEL_ALL_MODULE:
		 {
			 if(LOG_MODULE_MAX==total_module)
			 {

				 log_level_config(ALL_LOG_SET, total_module, &tc_data[2]);

				 tm_pack.tx_data = os_malloc_eram(sizeof(log_level_cfg)+2);
				 tm_pack.tx_data_len = sizeof(log_level_cfg)+2;
				 memcpy(&tm_pack.tx_data[2],&log_level_cfg[0],sizeof(log_level_cfg));
				 tm_pack.tx_data[0] = mode_selc;
				 tm_pack.tx_data[1] = LOG_MODULE_MAX;
				 tm_pack.tm_rsp_id = TM_LOG_LEVEL_FILTER_CONFIG;
				 obc_pack_tm_data(&tm_pack);
			 }
			 else
			 {
			 }
		 }
		 break;

		case CONFIG_SPECIFIC_MODULE:
		 {
			 log_level_config(SPECIFIC_MODULE_SET, tc_data[1], &tc_data[2]);

			 tm_pack.tx_data = os_malloc_eram(3);
			 tm_pack.tx_data_len = 3;
			 tm_pack.tx_data[2] = log_level_cfg[tc_data[1]];
			 tm_pack.tx_data[0] = mode_selc;
			 tm_pack.tx_data[1] = tc_data[1];
			 tm_pack.tm_rsp_id = TM_LOG_LEVEL_FILTER_CONFIG;
			 obc_pack_tm_data(&tm_pack);
		 }
		 break;

		case MULTIPLE_MODULE_SET:
		{
			cntr = 2;
			for(module_cnt = 1;  module_cnt <= total_module; module_cnt++)
			{

				log_level_config(SPECIFIC_MODULE_SET, tc_data[cntr], &tc_data[1+cntr]);
				cntr= cntr+2;
			}
			 tm_pack.tx_data = os_malloc_eram(sizeof(log_level_cfg)+2);
			 tm_pack.tx_data_len = sizeof(log_level_cfg)+2;
			 memcpy(&tm_pack.tx_data[2],&log_level_cfg[0],sizeof(log_level_cfg));
			 tm_pack.tx_data[0] = mode_selc;
			 tm_pack.tx_data[1] = LOG_MODULE_MAX;
			 tm_pack.tm_rsp_id = TM_LOG_LEVEL_FILTER_CONFIG;
			 obc_pack_tm_data(&tm_pack);
		}

		default:
		 break;
    }
}


void read_log_info(uint8_t *tc_data)
{
	log_storage_handle info1;
	obc_pck_data tm_pack;
	switch(tc_data[0])
	{
	case READ_LOG_CONFIG:
	{
		tm_pack.tx_data = os_malloc_eram(sizeof(log_level_cfg)+2);
		tm_pack.tx_data_len = sizeof(log_level_cfg)+2;
		memcpy(&tm_pack.tx_data[2],&log_level_cfg[0],sizeof(log_level_cfg));
		tm_pack.tx_data[0] = tc_data[0];
		tm_pack.tx_data[1] = LOG_MODULE_MAX;
		tm_pack.tm_rsp_id = TM_READ_LOG_INFO;
		obc_pack_tm_data(&tm_pack);
	}
	break;

	case READ_LOG_HANDLE_INFO:
	 {
		 log_read_handler_info(&info1);
		 tm_pack.tx_data = os_malloc_eram(11);
		 tm_pack.tx_data_len = 11;
		 tm_pack.tx_data[0] = tc_data[0];
		 memcpy(&tm_pack.tx_data[1],&info1.current_block_id,sizeof(info1.current_block_id));
		 memcpy(&tm_pack.tx_data[3],&info1.start_block_id,sizeof(info1.start_block_id));
		 memcpy(&tm_pack.tx_data[5],&info1.end_block_id,sizeof(info1.end_block_id));
		 memcpy(&tm_pack.tx_data[7],&info1.bkup_erase_cycle,sizeof(info1.bkup_erase_cycle));
#ifndef LINUX_TEMP_PORT
		 tm_pack.tx_data[9] = log_backup_partition_no();
#else
		 tm_pack.tx_data[9] = 0;
#endif
		 tm_pack.tx_data[10] = log_backup_en_dis_cfg;
		 tm_pack.tm_rsp_id = TM_READ_LOG_INFO;
		 obc_pack_tm_data(&tm_pack);
	 }
	 break;

}
}

void log_backup_dwn_rsp(uint8_t *status)
{
	obc_pck_data tm_pack;
	tm_pack.tx_data = os_malloc_eram(1);
	tm_pack.tx_data_len = 1;
	tm_pack.tx_data[0] = RESPONSE_BACKUP_DATA_READ_FAIL;
	tm_pack.tm_rsp_id = TM_LOG_DOWNLOAD_BACKUP_BLOCKS;
	obc_pack_tm_data(&tm_pack);
}


