iamalive_ini.cpp 4.17 KB
Newer Older
German Leon's avatar
German Leon committed
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

// iisalive.cpp
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h> // for watchdog timer
#include <unistd.h> // needed only if close() is used to close watchdog timer
#include <sys/ioctl.h> // for watchdog timer
#include <linux/watchdog.h> // for watchdog timer
#include <time.h>

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

  FILE *fdf, *fdl;
  int valor,millamperes;
  char buffer[100];
   char name[50];
  int i;
  struct watchdog_info ident;
  int fd, ret, fw;
  int timeout = 0;
  int timeslice=10;
  int retfinal;

   char *nom;
  /* open WDT0 device (WDT0 enables itself automatically) */
  fd = open("/dev/watchdog0", O_RDWR);
  if (fd<0) {
    fprintf(stderr, "Open watchdog device failed!\r\n");
  return -1;
  }


  /* Enviorment monitor*/
  ret = ioctl(fd, WDIOC_GETSUPPORT, &ident);
  if (ret){
    fprintf(stderr, "Kick watchdog failed!\n");
  }

  //printf ("Identidad %s Version %d Codigo %8x \n", ident.identity,ident.firmware_version,ident.options );
  timeout = 60;
  if (argc > 1) timeout=atoi(argv[1]);
  if (argc > 2) timeslice=atoi(argv[2]);

  /* WDT0 is counting now,check the default timeout value */
  ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
  if(ret) {
    fprintf(stderr, "Set watchdog timeout value failed!\r\n");
    return -1;
  }
  fprintf(stdout, "Initial Watchdog timeout value: %d\r\n", timeout);

  /* set new timeout value 60s */
  /* Note the value should be within [5, 1000] */

  /* Enviorment monitor*/
  ret = ioctl(fd, WDIOC_GETSUPPORT, &ident);
  if (ret){
    fprintf(stderr, "Kick watchdog failed!\r\n");
  }

  //tdout, "/printf ("Identidad %s Version %d Codigo %d \n", ident.identity,ident.firmware_version,ident.options );

  retfinal=ident.options & 0xff;
  //printf ("retfinal: %d-%8x \n", retfinal,ident.options);
  fprintf(stdout, "Last error ?");

  switch (retfinal)
  {
    case	WDIOF_OVERHEAT:		fprintf(stdout," Reset due to CPU overheat \r\n");break;
    case	WDIOF_FANFAULT:		fprintf(stdout, " Fan failed \r\n");break;
    case	WDIOF_EXTERN1:		fprintf(stdout, " External relay 1 \r\n");break;
    case	WDIOF_EXTERN2:		fprintf(stdout, " External relay 2 \r\n");break;
    case	WDIOF_POWERUNDER:	fprintf(stdout, " Power bad/power fault \r\n");break;
    case	WDIOF_CARDRESET:	fprintf(stdout, " Card previously reset the CPU \r\n");break;
    case	WDIOF_POWEROVER:	fprintf(stdout, " Power over voltage \r\n");break;
   case	WDIOF_SETTIMEOUT:	fprintf(stdout, " Set timeout (in seconds) \r\n");break;
   default: 
	fprintf(stdout,"Code: %d \r\n", ident.options);
  }

    nom=getenv("MYHOME");
    if (nom==NULL)
        sprintf(buffer,"./watchdog.log");
    else
        sprintf(buffer,"%s/watchdog.log",nom);
    if ( (fdl = fopen(buffer, "a") ) == NULL ) {
      printf("Error! opening watchog.log file\n");
    }
German Leon's avatar
German Leon committed
90
  while (1) {
German Leon's avatar
German Leon committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

    sleep (timeslice);
 
    ret = ioctl(fd, WDIOC_KEEPALIVE, &ident);
    if (ret<0) {
     fprintf(stderr, "Kick watchdog failed!\r\n");
     return -1;
    }
   
    time_t t = time(NULL);
    struct tm tm = *localtime(&t);
    fprintf(stdout, "alive: %d-%02d-%02d %02d:%02d:%02d  Temperaturas:", 
        tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

    fprintf(fdl, "alive: %d-%02d-%02d %02d:%02d:%02d \n", 
        tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
    fflush(fdl);
    for(i=0; i<6;i++) {
      sprintf(buffer,"/sys/devices/virtual/thermal/thermal_zone%d/temp",i);
      fdf=fopen(buffer,"r");
      fscanf (fdf,"%d",&valor);
      fprintf(stdout,"%.1f,",valor/1000.0);
German Leon's avatar
German Leon committed
113
      fclose(fdf);
German Leon's avatar
German Leon committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    }
    for(i=0; i<3;i++) {
      sprintf(buffer,"/sys/bus/i2c/drivers/ina3221x/6-0040/iio:device0/rail_name_%d",i);
     
      fdf=fopen(buffer,"r");
      fscanf (fdf,"%s",name);
      fclose(fdf);

      sprintf(buffer,"/sys/bus/i2c/drivers/ina3221x/6-0040/iio:device0/in_power%d_input",i);
      
      fdf=fopen(buffer,"r");
      fscanf (fdf,"%d",&millamperes);
      fclose(fdf);
    
  
      fprintf(stdout,"%s:%.3fW ",name+7,((float)millamperes)/1.0e3);
    }
    fprintf (stdout,"\r\n");


  } // end while

German Leon's avatar
German Leon committed
136
    fclose(fdl);
German Leon's avatar
German Leon committed
137
138
139
140
141
142
143
144
145
146
  close(fd);
  if (ret<0) {
    fprintf(stderr, "Failed to close watchdog device.");
    return -1;
  }
 printf ("+1\n");

  return 0;

}