#include "system_defines.h"
#include "cyclone_device.h"
#include "pmbus_commands.h"
#include "pmbus.h"
#include "variables.h"
#include "function_definitions.h"
#include "software_interrupts.h"

int32 pmbus_read_one_byte_handler(Uint8 value) 
{
	pmbus_number_of_bytes = 1;
	pmbus_buffer[0] = value;  
	return 0;
}

int32 pmbus_read_two_byte_handler(Uint16 value) 
{
	pmbus_number_of_bytes = 2;
	pmbus_buffer[1] = value >> 8;
	pmbus_buffer[0] = value & 0xff;
	return 0;
}

// look at command byte from a write perspective
int32 pmbus_write_message(void)
{
	switch (pmbus_buffer[0]) 			
	{
		case PMBUS_CMD_ROM_MODE: 
			return pmbus_write_rom_mode();
        case PMBUS_CMD_MFR_PARM_INFO: 
        	return pmbus_write_parm_info();
		case PMBUS_CMD_MFR_PARM_VALUE: 
			return pmbus_write_parm_value();	
		default:
			break;
	}
	return 0; 
}

// look at command byte from a read perspective
int32 pmbus_read_message(void)
{
	switch (pmbus_buffer[0]) 			
	{

        case PMBUS_CMD_MFR_CMDS_DCDC_PAGED: 
        	return pmbus_read_cmd_dcdc_paged();
        case PMBUS_CMD_MFR_CMDS_DCDC_NONPAGED: 
        	return pmbus_read_cmd_dcdc_nonpaged();
		case PMBUS_CMD_MFR_SETUP_ID: 
			return pmbus_read_setup_id();
		case PMBUS_CMD_MFR_DEVICE_ID: 
			return pmbus_read_device_id();	
        case PMBUS_CMD_PMBUS_REVISION: 
        	return pmbus_read_one_byte_handler(0x42); //version 1.2 for both		
		case PMBUS_CMD_MFR_ID: 
			return pmbus_mfr_id_handler(); 
		case PMBUS_CMD_MFR_MODEL: 
			return pmbus_mfr_model_handler(); 
		case PMBUS_CMD_MFR_REVISION: 
			return pmbus_mfr_revision_handler(); 
		case PMBUS_CMD_MFR_LOCATION: 
			return pmbus_mfr_location_handler();
		case PMBUS_CMD_MFR_DATE: 
			return pmbus_mfr_date_handler();
		case PMBUS_CMD_MFR_SERIAL: 
			return pmbus_mfr_serial_handler();
		case PMBUS_CMD_MFR_PARM_INFO: 
			return pmbus_read_parm_info();
		case PMBUS_CMD_MFR_PARM_VALUE: 
			return pmbus_read_parm_value();
		default:
			pmbus_number_of_bytes = 16;
			pmbus_buffer[0] = 0x0f;
			pmbus_buffer[1] = 0x22;
			pmbus_buffer[2] = 0x33;
			pmbus_buffer[3] = 0x44;
			pmbus_buffer[4] = 0x55;
			pmbus_buffer[5] = 0x66;
			pmbus_buffer[6] = 0x77;
			pmbus_buffer[7] = 0x88;
			pmbus_buffer[8] = 0x99;
			pmbus_buffer[9] = 0xaa;
			pmbus_buffer[10] = 0xbb;
			pmbus_buffer[11] = 0xcc;
			pmbus_buffer[12] = 0xdd;
			pmbus_buffer[13] = 0xee;
			pmbus_buffer[14] = 0xff;
			pmbus_buffer[15] = 0x01;
			return 0;
	}
}

