////////////////////////////////////////////////////////////////// // Main loop // Program strategy: // * handle incoming xpl messages (respond on requests) // * send heartbeat // * monitor port pins for falling edges, debounce them and increment counter if required void main() { /* // Test code: set an initial ID in the EEPROM so that we don't have to configure the node eeprom_write(0x00, 'A'); eeprom_write(0x01, 'F'); eeprom_write(0x02, '\0'); */ // Hardware initialisation init(); // Init the xPL library xpl_init(); /* // DEBUG if (oo_get_devicecount()){ printf("Found %i devices\r\n", oo_get_devicecount()); oo_read_temperatures(); oo_print_device_info(0); } */ while (1){ // Call the xPL message handler xpl_handler(); } }
int c_engine( int argc, char *argv[] ) { off_t f_offset; int check4poll(); struct stat stat_buf; char spoolfile[100]; time_t time_now, time_prev; long tmin_now, tmin_prev, thour_now, thour_prev; mode_t oldumask; extern FILE *fdsout; extern FILE *fdserr; #ifdef HAVE_LIBXPL long xpl_poll = configp->engine_poll / 1000; Bool xpl_ready = FALSE; #endif sprintf( spoolfile, "%s/%s%s", SPOOLDIR, SPOOLFILE, configp->suffix); (void) signal(SIGINT, engine_quit /*iquit*/); (void) signal(SIGTERM, engine_quit); if (setjmp(mjb)) return(0); i_am_state = 1; i_am_monitor = 1; heyu_parent = D_CMDLINE; openlog( "heyu_engine", 0, LOG_DAEMON); syslog(LOG_ERR, "engine setting up-\n"); if ( read_x10state_file() != 0 ) x10state_init_all(); time_now = time_prev = time(NULL); tmin_now = tmin_prev = (long)time_now / 60L; thour_now = thour_prev = (long)time_now / 3600; engine_local_setup(E_START); fix_tznames(); f_offset = lseek(sptty, 0, SEEK_CUR); /* find current position */ oldumask = umask(configp->log_umask); fdsout = freopen(configp->logfile, "a", stdout); fdserr = freopen(configp->logfile, "a", stderr); umask(oldumask); #ifdef HAVE_LIBXPL /* xPLLib bug? */ if (xpl_poll == 1000) xpl_poll = 999; xpl_ready = xpl_init(); #endif fprintf(fdsout, "%s Engine started\n", datstrf()); fflush(fdsout); while (1) { if ( f_offset == lseek(sptty, 0, SEEK_END) ) { /* find end of file */ if( stat( spoolfile, &stat_buf ) < 0) engine_quit(SIGTERM) /* return(0) */; #ifdef HAVE_LIBXPL if (xpl_ready) xPL_processMessages(xpl_poll); else #endif /* this imposes a delay between the start of new output*/ /* It keeps the disk from being thrashed. */ microsleep(configp->engine_poll); } else { if ( fstat( sptty, &stat_buf ) < 0) return(0); lseek(sptty, f_offset, SEEK_SET); check4poll(1,1); f_offset = lseek(sptty, 0, SEEK_CUR); /* find current position */ } if ( (time_now = time(NULL)) != time_prev ) { if ( is_tomorrow(time_now) ) { /* Local Midnight. STD Time */ midnight_tick(time_now); set_global_tomorrow(time_now); } if ( (thour_now = (long)time_now / 3600L) != thour_prev ) { /* Once every hour */ hour_tick(time_now); thour_prev = thour_now; } if ( (tmin_now = (long)time_now / 60L) != tmin_prev ) { /* Once every minute */ minute_tick(time_now); tmin_prev = tmin_now; } /* Once every second */ /* Activate countdown timers */ second_tick(time_now, (long)(time_now - time_prev)); time_prev = time_now; } } /* return(0); */ }
int c_monitor( int argc, char *argv[] ) { off_t f_offset; int check4poll(); struct stat stat_buf; char spoolfile[100]; time_t time_now, time_prev; #ifdef HAVE_LIBXPL long xpl_poll = configp->engine_poll / 1000; Bool xpl_ready = FALSE; #endif sprintf( spoolfile, "%s/%s%s", SPOOLDIR, SPOOLFILE, configp->suffix); (void) signal(SIGCHLD, iquit); (void) signal(SIGINT, iquit); if (setjmp(mjb)) return(0); i_am_monitor = 1; if ( read_x10state_file() != 0 ) x10state_init_all(); if ( argc == 3 && strcmp(argv[2], "rfxmeter") == 0 ) { /* Disable display except for RFXMeter setup file pointers */ i_am_monitor = 0; i_am_rfxmeter = 1; fdsout = fopen("/dev/null", "w"); fdserr = fopen("/dev/null", "w"); fprfxo = stdout; fprfxe = stderr; } else { #ifdef HAVE_LIBXPL /* xPLLib bug? */ if (xpl_poll == 1000) xpl_poll = 999; xpl_ready = xpl_init(); #endif fprintf(stdout, "%s Monitor started\n", datstrf()); fflush(stdout); } f_offset = lseek(sptty, 0, SEEK_CUR); /* find current position */ time_now = time_prev = time(NULL); while (1) { if( f_offset == lseek(sptty, 0, SEEK_END) ) { /* find end of file */ if( stat( spoolfile, &stat_buf ) < 0) return(0); #ifdef HAVE_LIBXPL if (i_am_monitor && xpl_ready) xPL_processMessages(xpl_poll); else #endif /* this imposes a delay between the start of new output*/ /* It keeps the disk from being thrashed. */ microsleep(configp->engine_poll); } else { if ( fstat( sptty, &stat_buf ) < 0 ) return(0); lseek(sptty, f_offset, SEEK_SET); check4poll(1,1); f_offset = lseek(sptty, 0, SEEK_CUR); /* find current position */ } /* Activate countdown timers */ if ( (time_now = time(NULL)) != time_prev ) { second_tick(time_now, (long)(time_now - time_prev)); time_prev = time_now; } } /* return(0); */ }