static char rcsid[] = "$Id: dam.c,v 1.6 2000/11/23 23:45:27 root Exp $";

/*
 * $Log: dam.c,v $
 * Revision 1.6  2000/11/23 23:45:27  root
 * #
 *
 * Revision 1.5  2000/10/07 18:30:09  root
 * #
 *
 * Revision 1.4  2000/10/06 19:52:44  root
 * #
 *
 * Revision 1.3  2000/09/24 01:46:07  root
 * #
 *
 * Revision 1.2  2000/09/24 01:12:54  root
 * #
 *
 * Revision 1.1  2000/02/21 04:11:28  root
 * #
 *
 * Revision 1.6  2000/02/20 03:03:57  root
 * #
 *
 * Revision 1.5  2000/02/20 03:03:53  root
 * #
 *
 * Revision 1.4  2000/02/20 02:52:13  root
 * #
 *
 * Revision 1.3  2000/02/16 13:53:26  root
 * #
 *
 * Revision 1.2  2000/02/16 13:50:11  root
 * *** empty log message ***
 *
 * Revision 1.1  2000/02/14 04:08:33  root
 * #
 *
 * Revision 1.4  1999/10/26 17:46:46  root
 * #
 *
 * Revision 1.3  1999/10/26 17:46:41  root
 * #
 *
 * Revision 1.2  1999/09/21 23:01:38  root
 * #
 *
 * Revision 1.1  1999/09/21 22:56:11  root
 * Initial revision
 *
 */

#include "project.h"


extern void *msglib;

#include <linux/reboot.h>
//extern int reboot(unsigned int,unsigned int,unsigned int);
extern int reboot (unsigned int);

int bal_byte, left_byte, right_byte;

void
system_cmdst_message (Byte dunit, Word cmd, Byte * data, Word len)
{
  Word w;
  if ((dunit != systemunit) && (dunit != HIFI_UNIT_ALL))
    return;


  switch (cmd)
    {
    case HIFI_CMD_SYSTEM_OFF:

      //reboot(LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_POWER_OFF);
      //reboot (LINUX_REBOOT_CMD_POWER_OFF);

      sleep_turn_off ();

      break;
    case HIFI_CMD_SYSTEM_SLEEP:
      hifipkt_dword (data, &w);
      sleep_set (w);
      break;
    case HIFI_CMD_VOL:
      left_byte = data[0];
      right_byte = data[1];
      mixer_set_systemvol (left_byte, right_byte, bal_byte);
      break;
    case HIFI_CMD_SL_VOL:
      left_byte = data[0];
      right_byte = data[1];
      slide_mixer_set_systemvol (left_byte, right_byte, bal_byte);
      break;
    case HIFI_CMD_SYSTEM_PCM_VOL:
      printf ("Pcm Volm set to %d %d\n", data[0], data[1]);
      mixer_set_pcmvol (data[0], data[1]);
      break;
    case HIFI_CMD_SYSTEM_SPDIF:
      mixer_set_spdif (data[0]);
      break;
    case HIFI_CMD_BAL:
      bal_byte = data[0];
      mixer_set_systemvol (left_byte, right_byte, bal_byte);
      break;

    }

}

int
dam_query (Dam * d)
{
  FILE *f;
  char buf[1024];

  float div[2];
  int i;

  if (!d)
    return 1;

  f = fopen (DAM_PROC, "r");

  if (!f)
    return 1;

  fgets (buf, sizeof (buf), f);
  sscanf (buf, "%*[^:]:%f%*[^:]:%f", &d->fan[1], &div[1]);
  fgets (buf, sizeof (buf), f);
  sscanf (buf, "%*[^:]:%f%*[^:]:%f", &d->fan[0], &div[0]);
  fgets (buf, sizeof (buf), f);
  sscanf (buf, "%*[^:]:%f", &d->v[0]);
  fgets (buf, sizeof (buf), f);
  sscanf (buf, "%*[^:]:%f", &d->v[1]);
  fgets (buf, sizeof (buf), f);
  sscanf (buf, "%*[^:]:%f", &d->v[2]);
  fgets (buf, sizeof (buf), f);
  sscanf (buf, "%*[^:]:%f", &d->v[3]);
  fgets (buf, sizeof (buf), f);
  sscanf (buf, "%*[^:]:%f", &d->temp);

  fclose (f);

  for (i = 0; i < 2; ++i)
    {
      if (d->fan[i] > 0xfe)
        {
          d->fan[i] = 0;
        }
      else
        {
          d->fan[i] = (DAM_SAMP * DAM_CLK) / (d->fan[i] * div[i]);
        }
    }

  d->v[0] *= DAM_COR0;
  d->v[1] *= DAM_COR1;
  d->v[2] *= DAM_COR2;
  d->v[3] *= DAM_COR3;

  d->temp += DAM_TEMPO;
  d->temp *= DAM_TEMPS;

  if (d->temp > 60)
    {                           //Safety poweroff

      //reboot(LINUX_REBOOT_MAGIC1,LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_POWER_OFF);

      sleep_turn_off ();
      sleep (5);
      reboot (LINUX_REBOOT_CMD_POWER_OFF);

    }

  if (d->temp < 0)
    {
      d->temp = HUGE_VAL;
    }

  return (0);



}


void
dam_status (void)
{
  Dam d;
  Byte *buf = hifi_getbuf ();
  Byte *ptr = buf, *cptr;


  if (dam_query (&d))
    {
//hifi_freebuf(buf);
      //return ;

      d.temp = 37.0;
      d.fan[0] = 3024.0;

    }

  ptr = hifipkt_new_brd (ptr, HIFI_CLASS_SYSTEM, systemunit);
  ptr = hifipkt_est (cptr = ptr, HIFI_ST_SYSTEM_STATE);
  ptr +=
    sprintf (ptr, "Temp:%.0f C Sleep %d Speed:%.0f RPM", d.temp,
             sleep_left (), d.fan[0]);
  ptr++;                        //Trailing zero

  hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));


  {
    int l, r;


    mixer_get_systemvol (&left_byte, &right_byte, &bal_byte);
    ptr = hifipkt_est (cptr = ptr, HIFI_ST_VOL);
    ptr = hifipkt_ebyte (ptr, left_byte);
    ptr = hifipkt_ebyte (ptr, right_byte);
    hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));

    mixer_get_pcmvol (&l, &r);

    ptr = hifipkt_est (cptr = ptr, HIFI_ST_SYSTEM_PCM_VOL);
    ptr = hifipkt_ebyte (ptr, l);
    ptr = hifipkt_ebyte (ptr, r);
    hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));

    mixer_get_spdif (&l);
    ptr = hifipkt_est (cptr = ptr, HIFI_ST_SYSTEM_SPDIF);
    ptr = hifipkt_ebyte (ptr, r);
    hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));

    ptr = hifipkt_est (cptr = ptr, HIFI_ST_BAL);
    ptr = hifipkt_ebyte (ptr, bal_byte);
    hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));

  }

  ptr = hifipkt_eeof (ptr);

  hifi_send (msglib, buf, HIFI_PTR_DIFF (ptr, buf));
  hifi_freebuf (buf);

}
