#include "xp_cds_status_bus.h"
//#include "xp_tools.h"


void clear_cur_stat_sbus(T_cds_status_serial_bus *v)
{
	v->cur_read_error = 0;
	v->cur_write_error = 0;
}

void clear_cur_stat_pbus(T_cds_status_parallel_bus *v)
{
	v->cur_read_error = 0;
	v->cur_write_error = 0;
}

void clear_cur_stat_hwpbus(T_cds_status_hwp_bus *v)
{
	v->cur_read_error = 0;
	v->cur_write_error = 0;
}


////////////////////////////////////////////
////////////////////////////////////////////


void clear_stat_sbus(T_cds_status_serial_bus *v)
{
	v->count_read_error = 0;
	v->count_write_error = 0;
	v->count_read_ok = 0;
	v->count_write_ok = 0;
	v->cur_read_error = 0;
	v->cur_write_error = 0;

	v->status      = 0;
}

void clear_stat_pbus(T_cds_status_parallel_bus *v)
{
	v->count_read_error = 0;
	v->count_write_error = 0;
	v->count_read_ok = 0;
	v->count_write_ok = 0;
	v->cur_read_error = 0;
	v->cur_write_error = 0;

	v->status      = 0;
}

void clear_stat_hwpbus(T_cds_status_hwp_bus *v)
{
	v->count_read_error = 0;
	v->count_write_error = 0;
	v->count_read_ok = 0;
	v->count_write_ok = 0;

	v->cur_read_error = 0;
	v->cur_write_error = 0;

	v->status      = 0;
}


int check_cds_ready_sbus(int err, int wr_rd, T_cds_status_serial_bus *v)
{

  if (wr_rd == ITS_WRITE_BUS) // write
  {
	if (err)
	{
		if (v->cur_write_error < v->max_write_error)
		   v->cur_write_error++;
	}
	else
	{
		if (v->cur_write_error > 0)
		   v->cur_write_error--;
	}
  }


  if (wr_rd == ITS_READ_BUS) // read
  {

	if (err)
	{
		if (v->cur_read_error < v->max_read_error)
		   v->cur_read_error++;
	}
	else
	{
		if (v->cur_read_error > 0)
		   v->cur_read_error--;
	}
  }


  if ( (v->cur_write_error >= v->max_write_error) || (  v->cur_read_error >= v->max_read_error) )
    return 1;


  return 0;
}



int check_cds_ready_pbus(int err, int wr_rd, T_cds_status_parallel_bus *v)
{

  if (wr_rd == ITS_WRITE_BUS) // write
  {
	if (err)
	{
		if (v->cur_write_error < v->max_write_error)
		   v->cur_write_error++;
	}
	else
	{
		if (v->cur_write_error > 0)
		   v->cur_write_error--;
	}
  }


  if (wr_rd == ITS_READ_BUS) // read
  {

	if (err)
	{
		if (v->cur_read_error < v->max_read_error)
		   v->cur_read_error++;
	}
	else
	{
		if (v->cur_read_error > 0)
		   v->cur_read_error--;
	}
  }


  if ( (v->cur_write_error >= v->max_write_error) || (  v->cur_read_error >= v->max_read_error) )
    return 1;


  return 0;
}



int check_cds_ready_hwpbus(int err, int wr_rd, T_cds_status_hwp_bus *v)
{

  if (wr_rd == ITS_WRITE_BUS) // write
  {
	if (err)
	{
	    v->count_write_error++;
		if (v->cur_write_error < v->max_write_error)
		   v->cur_write_error++;
	}
	else
	{
		if (v->cur_write_error > 0)
		   v->cur_write_error--;
        v->count_write_ok++;

	}
  }


  if (wr_rd == ITS_READ_BUS) // read
  {

	if (err)
	{
	    v->count_read_error++;
		if (v->cur_read_error < v->max_read_error)
		   v->cur_read_error++;
	}
	else
	{
		if (v->cur_read_error > 0)
		   v->cur_read_error--;
		v->count_read_ok++;
	}
  }


  if ( (v->cur_write_error >= v->max_write_error) || (  v->cur_read_error >= v->max_read_error) )
    return 1;


  return 0;
}




void set_status_cds(int err_ready, T_component_status *ss)
{

  if (err_ready == 0) // all ok
  {
	if ((*ss == component_NotReady) || (*ss ==  component_Started))
	  *ss = component_Ready;
  }

  if (err_ready == 1) // all !bad!
  {
	  *ss = component_ErrorSBus;//component_Error;
  }

}


