File: fan_ctrl.c

package info (click to toggle)
nwutil 1.4-3
  • links: PTS
  • area: main
  • in suites: sarge, woody
  • size: 132 kB
  • ctags: 86
  • sloc: ansic: 1,176; makefile: 63
file content (127 lines) | stat: -rw-r--r-- 3,189 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
   fan_ctrl: a sample program to turn on/off Netwinder Rev5 fan
   Copyright 1998  woody@corelcomputer.com

   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.
 */

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

#include "therm.h"

int
main (int argc, char **argv)
{

  int fd = 0;
  int fan_new = 1;		//assume fan to be turned ON

  int fan_old = 0;
  char *basename;
  char *p;
  struct therm old_therm;
  struct therm kick_fan;

//      printf("\nargc=%d, argv:[0]=%s, [1]=%s.\n\n",
//          argc,argv[0],argv[1]);

  basename = (p = strrchr (argv[0], '/')) ? p + 1 : argv[0];

  if (argc < 2)
    {
      fprintf (stderr, "\nNetWinder Fan Control program, Corel Computer 1998.\nUsage: %s ON or OFF or START.\n\n",
	       basename);
    }
  else if ((strcasecmp (argv[1], "ON")) == 0)
    {
      fan_new = 1;
    }
  else if ((strcasecmp (argv[1], "OFF")) == 0)
    {
      fan_new = 0;
    }
  else if ((strcasecmp (argv[1], "START")) == 0)
    {
      fan_new = 3;
    }
  else
    {
      fprintf (stderr, "%s: unable to parse ON/OFF/START command (\"%s\")???.\n",
	       basename, argv[1]);
      return EXIT_FAILURE;
    }

  fd = open ("/dev/temperature", O_RDWR);
  if (fd < 0)
    {
      printf ("Error %d opening /dev/temperature\n", fd);
      return EXIT_FAILURE;
    }
//      else
//      {
//              printf("/dev/temperature open OK, rc = 0x%X.\n",fd);
//      }


  ioctl (fd, CMD_GET_FAN, &fan_old);
  if (!errno)
    printf ("Current fan setting: %s.\n", fan_old ? "ON" : "OFF");
  else
    {
      printf ("Error 0x%X getting fan setting.\n", errno);
      return EXIT_FAILURE;
    }

  if (argc >= 2)
    {
      ioctl (fd, CMD_SET_FAN, &fan_new);

      if (!errno)
	printf ("New fan setting: %s.\n", fan_new ? "ON" : "OFF");
      else
	printf ("Error 0x%X setting the fan.\n", errno);
    }

  //if turning fan ON or START
  if (((fan_old != fan_new) && fan_new) || fan_new == 3)
    {
      //remember the current setting
      ioctl (fd, CMD_GET_THERMOSTATE2, &old_therm);

      //temporary set therm at 0
      kick_fan.hi = 1;
      kick_fan.lo = 0;
      ioctl (fd, CMD_SET_THERMOSTATE2, &kick_fan);

      //give the fan a moment to start
      sleep (2);

      //and restore the old thermostat setting
      ioctl (fd, CMD_SET_THERMOSTATE2, &old_therm);
    }

  if (close (fd) != 0)
    printf ("Error closing /dev/temperature\n");

  return EXIT_SUCCESS;
}