/*
   therm_alert.c: a thermal watchdog monitor for Netwinder
   using the hardware Watchdog and DS1620 therm device.
   woody@rebel.com 1999

   Based on fand * Jamie Guinan, December 1998 *

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


/*
 * Jamie Guinan, December 1998
 *
 * NetWinder fan daemon.  Turns the fan off, then waits for the temperature
 * to rise above the high thermostat setting and turns it on,
 * or waits for it to fall below the low thermostat setting
 * and turns it off.
 *
 * This seems to work even when run as non-root.
 *
 * Note that you need at least therm.h from the kernel sources to
 * compile this.
 *
 * Copyright:  Free for redistribution, modifications welcome, as 
 *    long as credits are maintained.
 *
 * Warranty: None whatsoever.  Use entirely at your own risk.
 */
#define USE_SYSLOG 1
#define	RESET_ON_ERROR	1
#define SHUTDOWN_TEMPERATURE	52	//start shutdown at 55

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>

#if USE_SYSLOG
#include <syslog.h>
#endif

#if defined (LINUX_VERSION_CODE) && LINUX_VERSION_CODE < 0x20100
#include "/usr/src/linux/arch/arm/drivers/char/therm.h"
#else
#include "ds1620.h"
#endif
#include <time.h>

#define THERMDEV "/dev/temperature"
#define SLEEPSECONDS 30

const char beep_snd[] = {
  0x2e, 0x73, 0x6e, 0x64, 0x00, 0x00, 0x00, 0x20, 
  0x00, 0x00, 0x0d, 0xf0, 0x00, 0x00, 0x00, 0x01, 
  0x00, 0x00, 0x1f, 0x40, 0x00, 0x00, 0x00, 0x01, 
  0x00, 0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x6d, 0x65, 0x5f, 0x65, 
  0x7b, 0x6d, 0x5d, 0x61, 0x70, 0xe8, 0xd2, 0xe4, 
  0xe5, 0xde, 0xe6, 0xe3, 0x78, 0x77, 0x67, 0xfb, 
  0xf1, 0x6c, 0x5f, 0x59, 0xf5, 0xec, 0x78, 0x6e, 
  0x5c, 0x56, 0x6b, 0x6f, 0x6e, 0xee, 0xf0, 0xda, 
  0xdf, 0xf7, 0xe8, 0xe6, 0xe6, 0x79, 0x5f, 0x57, 
  0x4c, 0x53, 0xfa, 0xe5, 0xe9, 0xed, 0xe8, 0xdd, 
  0xf6, 0x6c, 0x77, 0x7d, 0xdb, 0xdd, 0xea, 0xfa, 
  0x70, 0xed, 0xdd, 0xd7, 0xed, 0xeb, 0x7d, 0x72, 
  0xeb, 0x6b, 0x58, 0x6b, 0x67, 0x60, 0x78, 0x63, 
  0x6d, 0xe8, 0xdf, 0xf1, 0xf1, 0xf5, 0xeb, 0x7a, 
  0x61, 0x63, 0x5d, 0x57, 0x56, 0x67, 0xf7, 0xec, 
  0xe3, 0xdb, 0xd9, 0xd7, 0xd8, 0xf5, 0x70, 0xef, 
  0xe3, 0xe8, 0x71, 0x66, 0x72, 0x6f, 0xfd, 0x69
  
};


int temperature, last_temperature;
#if defined (LINUX_VERSION_CODE) && LINUX_VERSION_CODE < 0x20100
struct THERM_SETTING ts;
#else
struct therm ts;
#endif

int get_temperature()
{
  int therm_fd;
  therm_fd = open(THERMDEV, O_RDONLY);
  if (therm_fd < 0)
    {
      return -1;
    }
  ioctl(therm_fd, CMD_GET_TEMPERATURE, &temperature);
  close(therm_fd);
  return 0;
}

int get_thermostates()
{
  int therm_fd;
  therm_fd = open(THERMDEV, O_RDONLY);
  if (therm_fd < 0)
    {
      return -1;
    }
  ioctl(therm_fd, CMD_GET_THERMOSTATE2, &ts);
  close(therm_fd);
  return 0;
}

int set_thermostates()
{
  int therm_fd;
  therm_fd = open(THERMDEV, O_RDONLY);
  if (therm_fd < 0)
    {
      return -1;
    }
  ioctl(therm_fd, CMD_SET_THERMOSTATE2, &ts);
  close(therm_fd);
  return 0;
}

int set_fan(int cntrl)
{
  int therm_fd;
  therm_fd = open(THERMDEV, O_RDONLY);
  if (therm_fd < 0)
    {
      return -1;
    }
  ioctl(therm_fd, CMD_SET_FAN, &cntrl);
  close(therm_fd);
  return 0;
}

void err_quit()
{
  /* On error, turn fan on and alert before exit. */
    set_fan(1);

#if USE_SYSLOG
    syslog(LOG_ERR, "Error reading temperature. Aborting.");
#else				/* USE_SYSLOG */
    fprintf(stderr, "Error reading temperature. Aborting.\n");
#endif				/* USE_SYSLOG */

#if REBOOT_ON_ERROR
//    system("shutdown -r now");
    system("reboot");
#endif

    exit(-1);
}


void quit()
{
  /* On signal, turn fan on before exit. */
    set_fan(1);
    exit(-1);
}


void sig_handle(int n)
{
  quit();
}

void usage(char* name)
{
    printf("Usage: %s\n", name);
}

int alarm_beep(int beeps)
{
int devaudio;
int i;
int beep_size;
int hdrSize;

    beep_size = sizeof(beep_snd);
    hdrSize = *(beep_snd+7);	//header size

    devaudio = open("/dev/audio",O_WRONLY);
    if(devaudio != -1)
    {
	for (i=0; i<beeps; i++)
	    write(devaudio,beep_snd+hdrSize,beep_size);

	close(devaudio);
    }

}
/*---------------------------------------------------------------*/

int main(int argc, char *argv[])
{
int	fd;
time_t	tm;
int	alarm_flag = 0;
int 	devaudio;

    if (argc != 1)
    {
      usage(argv[0]);
      return -1;
    }

    fd = open ("/dev/watchdog", O_WRONLY);
    if (fd == -1)
    {
	printf ("watchdog not ready: add device 'mknod -m666 /dev/watchdog c 10 130'!\n");
	exit (1);
    }
    write (fd, "\5", 1);

//printf("wms: opened watchdog as file %X.\n",fd);

    devaudio = open("/dev/audio",O_WRONLY);
    if(devaudio != -1)
    {
	close(devaudio);
    }
    else
    {
	printf("%s: Can not use /dev/audio for alarm. Aborting...\n",
	    argv[0]);
	exit (2);
    }

    /* get the current thermostate HI setting. Make sure it points below 55C */
    if (get_thermostates() != 0)
    {
	printf("%s: Error reading the thermostate setting from DS1620. Aborting...\n",
	    argv[0]);
	exit (3);
    }
    
    if (ts.hi > 55)
    {
	printf("%s: Thermostat set dangerously high. Resetting to 55C...\n");

	ts.hi = 55;

        if (set_thermostates() != 0)
        {
	    printf("%s: Error resetting the DS1620 thermostate to 55C. Aborting...\n",
		argv[0]);
        }
    }

    /* Background thyself. */
    switch (fork())
    {
    case 0:
      /* Child. */
      break;
    case -1:
      /* Error. */
      perror("fork");
      exit(-1);
      break;
    default:
      /* Parent. */
      exit(0);
    }

    /* Handle a few common signals. */
    signal(SIGINT, sig_handle);
    signal(SIGQUIT, sig_handle);
    signal(SIGHUP, sig_handle);
    signal(SIGABRT, sig_handle);
    signal(SIGILL, sig_handle);
    signal(SIGTERM, sig_handle);

    last_temperature = 0;

    /* No controlling terminal. */
    setsid();

    while (1)
    {
	if (get_temperature() != 0)
	{
	  err_quit();
	}
	
        if (get_thermostates() != 0)
	{
	  err_quit();
	}
	  
//	printf("wms: temperature=%d, ts.hi=%d ts.lo=%d.\n", temperature, ts.hi, ts.lo);
      
	if (last_temperature == 0)
	{
	  set_fan(0);
	}
      
	if (temperature != last_temperature)
	{
	    if (temperature >= ts.hi)
	    {
		if (set_fan(1) != 0)
		{
		  err_quit();
		}
	    }
	    else if (temperature <= ts.lo)
	    {
		if (set_fan(0) != 0)
		{
		  err_quit();
		}
	    }
	    last_temperature = temperature;
	}

	if ((temperature > ts.hi) && (temperature <= SHUTDOWN_TEMPERATURE))
	{
	    /* once we exceed the HI level, issue a warning */
	    syslog(LOG_WARNING, "System temperature increased to %d'C.", temperature);
	}

	if (temperature > SHUTDOWN_TEMPERATURE)		//55!!!
	{
	    alarm_flag ++;
	}
	else
	{
	    alarm_flag = 0;
	}

	if (alarm_flag)
	{
	    alarm_beep(alarm_flag);
#if USE_SYSLOG
	    syslog(LOG_ERR, "System overheating! (temperature %d'C)", temperature);
#else				/* USE_SYSLOG */
	    fprintf(stderr, "%s: System overheating! (temperature %d'C).\n", argv[0], temperature);
#endif				/* USE_SYSLOG */
	}


	if (alarm_flag < 10)
	{
//tm = time(NULL);
//printf("wms: Watchdog kick: alarm_flag = %d, time = %s",alarm_flag,ctime(&tm));

	    write (fd, "\5", 1);
	}
	else if (alarm_flag == 10)
	{
#if USE_SYSLOG
	    syslog(LOG_ERR,"...ATTENTION: System overheating (%d'C) - shutting down NOW!...",
		temperature);
#else				/* USE_SYSLOG */
	    fprintf(stderr, "%s: ...ATTENTION: System overheating (%d'C) - shutting down NOW!...\n",
		argv[0], temperature);
#endif				/* USE_SYSLOG */
	    write (fd, "\5", 1);	//last kick to give halt 1 min
	    system("halt");
	}

	sleep (SLEEPSECONDS);	//kick WD every 30 seconds
    }

    return 0;
}


