// iisalive.cpp #include #include #include #include // for watchdog timer #include // needed only if close() is used to close watchdog timer #include // for watchdog timer #include // for watchdog timer #include int main(int argc, char*argv[]) { FILE *fdf, *fdl; int valor,millamperes; char buffer[100]; char name[50]; char *nom; int i; struct watchdog_info ident; int fd, ret, fw; int timeout = 0; int timeslice=10; int retfinal; /* 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"); } nom=getenv("HOSTNAME"); while (1) { 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(%s): %d-%02d-%02d %02d:%02d:%02d Temperaturas:",nom, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); fprintf(fdl, "alivei(%s): %d-%02d-%02d %02d:%02d:%02d \n",nom, 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); fclose(fdf); } 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 fclose(fdl); close(fd); if (ret<0) { fprintf(stderr, "Failed to close watchdog device."); return -1; } printf ("+1\n"); return 0; }