int main(){ printf("goodnight, Irene?\n"); signal(SIGUSR1, sig1); signal(SIGUSR2, sig2); deepsleep(); printf("oops.. wokeup!\n"); while(1){ sleep(10); } }
int main() { #if defined(TARGET_LPC11U68) wkp.mode(PullUp); #endif wkp.rise(&flip); while (true) { // sleep(); deepsleep(); } }
void dnszones_finish(struct dnszones *dnszones) { long long i; long long s[5] = {SIGTERM, SIGALRM, SIGTERM, SIGTERM, SIGKILL}; for(i = 0; i < 5; ++i) { dnszones_collect_zombies(dnszones); if (!dnszones_haschilds(dnszones)) break; dnszones_killall(dnszones, s[i]); deepsleep(1); } }
void trystart(void) { int f; switch(f = fork()) { case -1: strerr_warn4(WARNING,"unable to fork for ",dir,", sleeping 60 seconds: ",&strerr_sys); deepsleep(60); trigger(); return; case 0: sig_uncatch(sig_child); sig_unblock(sig_child); execve(*run,run,environ); strerr_die4sys(111,FATAL,"unable to start ",dir,"/run: "); } flagpaused = 0; pid = f; pidchange(); announce(); deepsleep(1); }
static void trystart() { int f; switch(f = fork()) { case -1: write_log(fdlogwf, WARNING, "unable to fork for ", service, ", sleeping 60 seconds\n"); deepsleep(60); trigger(); return; case 0: sig_uncatch(sig_child); sig_unblock(sig_child); execvp(cmd[0], cmdp); write_log(fdlogwf, FATAL, "unable to start ", cmd[0], "\n"); _exit(1); } flagpaused = 0; pid = f; pidchange(); announce(); deepsleep(1); }
static void suspendtimer(void) { #ifdef notdef uvlong now; if (suspendtime > 0) suspendtime--; if (suspendtime == 0){ now = seconds(); if (now < lastsuspend + 10){ resetsuspendtimer(); return; } lastsuspend = seconds(); deepsleep(); lastsuspend = seconds(); return; } #endif /* notdef */ }
void powerkproc(void*) { ulong xlink, xlink1; while(waserror()) ; for(;;){ while(powerflag == 0) sleep(&powerr, powerdown, 0); xlink = getcallerpc(&xlink); // iprint("call deepsleep, pc = 0x%lux, sp = 0x%lux\n", xlink, &xlink); deepsleep(); xlink1 = getcallerpc(&xlink1); delay(2000); // iprint("deepsleep returned, pc = 0x%lux, sp = 0x%lux\n", xlink1, &xlink); powerflag = 0; } }
int main(){ printf("goodnight, Irene?\n"); signal(SIGUSR1, happy); deepsleep(); printf("oops.. wokeup!\n"); }
void pause5(const char *s1,const char *s2,const char *s3,const char *s4,const char *s5) { strerr_warn6(WARNING,s1,s2,s3,s4,s5,&strerr_sys); deepsleep(5); }
void pause3(const char *s1,const char *s2,const char *s3) { strerr_warn4(WARNING,s1,s2,s3,&strerr_sys); deepsleep(5); }
void fullcurrent(struct cyclog *d) { int fd; int pid; int wstat; while (fchdir(d->fddir) == -1) pause3("unable to switch to ",d->dir,", pausing: "); while (fsync(d->fdcurrent) == -1) pause3("unable to write ",d->dir,"/current to disk, pausing: "); close(d->fdcurrent); while (rename("current","previous") == -1) pause3("unable to rename current to previous in directory ",d->dir,", pausing: "); while ((d->fdcurrent = open_append("current")) == -1) pause3("unable to create ",d->dir,"/current, pausing: "); coe(d->fdcurrent); d->bytes = 0; while (fchmod(d->fdcurrent,0644) == -1) pause3("unable to set mode of ",d->dir,"/current, pausing: "); while (chmod("previous",0744) == -1) pause3("unable to set mode of ",d->dir,"/previous, pausing: "); if (!d->processor) finish(d,"previous","s"); else { for (;;) { while ((pid = fork()) == -1) pause3("unable to fork for processor in ",d->dir,", pausing: "); if (!pid) { startprocessor(d); strerr_die4sys(111,FATAL,"unable to run ",d->processor,": "); } if (wait_pid(&wstat,pid) == -1) pause3("wait failed for processor in ",d->dir,", pausing: "); else if (wait_crashed(wstat)) pause3("processor crashed in ",d->dir,", pausing: "); else if (!wait_exitcode(wstat)) break; strerr_warn4(WARNING,"processor failed in ",d->dir,", pausing",0); deepsleep(5); } while ((fd = open_append("processed")) == -1) pause3("unable to create ",d->dir,"/processed, pausing: "); while (fsync(fd) == -1) pause3("unable to write ",d->dir,"/processed to disk, pausing: "); while (fchmod(fd,0744) == -1) pause3("unable to set mode of ",d->dir,"/processed, pausing: "); close(fd); while ((fd = open_append("newstate")) == -1) pause3("unable to create ",d->dir,"/newstate, pausing: "); while (fsync(fd) == -1) pause3("unable to write ",d->dir,"/newstate to disk, pausing: "); close(fd); while (unlink("previous") == -1) pause3("unable to remove ",d->dir,"/previous, pausing: "); while (rename("newstate","state") == -1) pause3("unable to rename newstate to state in directory ",d->dir,", pausing: "); finish(d,"processed","s"); } }
int main(int argc, char **argv) { unsigned char reg; unsigned int i; unsigned int mode; unsigned char snoopctl, snoopctl_orig; unsigned char edid[256]; unsigned char cached_edid[256]; unsigned char netv_edid[256]; unsigned char *hdmi; unsigned char *new_edid; FILE *edid_file = NULL; FILE *test_file = NULL; char *edid_fname = NULL; char *test_fname = NULL; unsigned int cache_valid = 0; unsigned char buffer; unsigned char compctl; unsigned char cea_list[128]; char c; int cx; int temp; int num_modes = 0; cx = getopt(argc, argv, "hf:T:"); while(cx != -1 ) { printf( "%02x", cx ); c = (char) cx; switch(c) { case 'f': edid_fname = malloc(strlen(optarg) * sizeof(char)); if( edid_fname != NULL ) { // safe to use strcpy because buffer is allocated dynamically // and matched to length of optarg strcpy(edid_fname, optarg); } break; case 'T': test_fname = malloc(strlen(optarg) * sizeof(char)); if( test_fname != NULL ) { strcpy(test_fname, optarg); } break; case 'h': default: fprintf(stderr, "Usage: %s [-h] [-f path-to-edid-cache] [-T file-for-test]\n", argv[0]); exit(0); break; } // switch cx = getopt(argc, argv, "hf:T:"); } // while bzero(cached_edid, sizeof(cached_edid) ); if( edid_fname == NULL ) edid_fname = default_edidname; edid_file = fopen(edid_fname, "rw+"); if( edid_file != NULL ) { for( i = 0; !feof(edid_file) && (i < 256); i ++ ) { fscanf(edid_file, "%x%*[ \n\t]", &temp ); cached_edid[i] = temp & 0xFF; } if( parse_edid( cached_edid ) ) { cache_valid = 0; } else { cache_valid = 1; } rewind( edid_file ); } else { cache_valid = 0; edid_file = fopen(edid_fname, "w+"); if( edid_file == NULL ) { fprintf(stderr, "Can't open/create %s for writing.\n", edid_fname ); exit(1); } } if( cache_valid ) fprintf(stderr,"Cached EDID has a valid structure.\n" ); else fprintf(stderr,"Cached EDID has an invalid structure.\n" ); for( i = 0; i< 256; i++ ) { edid[i] = 0xff; netv_edid[i] = 0xff; } if( test_fname == NULL ) { // first check and see if a TV is present if( sink_status() == SINK_DISCONNECTED ) { if( cache_valid ) { fprintf(stderr, "No sink detected, but cache is valid. Cowardly quitting and doing nothing. User should be instructed to plug in the TV before boot.\n" ); // need to commit the cached modeline though, that's a requirement of this program... // all non-test exit paths should leave with at least a valid modeline written in write_modeline(cached_edid); exit(1); } else { // put in a bogus 720p-only record for( i = 0; i < 128; i++ ) { cea_list[i] = 0; } cea_list[4] = 1; // set 720p support // now build a new EDID based upon the supported modes from the monitor new_edid = make_edid(cea_list); memcpy( netv_edid, new_edid, sizeof(unsigned char) * 128); free(new_edid); hdmi = make_hdmi(cea_list); memcpy( &(netv_edid[128]), hdmi, sizeof(unsigned char) * 128); free(hdmi); // HDMI is calloc'd inside make_hdmi fprintf( stderr, "No sink, but EDID cache is invalid. Committing a 'safe' 720p-only EDID to disk.\n" ); // if cache is invalid... // write the EDID to a file for( i = 0; i < 256; i++ ) { if( (i % 16 == 0) && (i != 0) ) { fprintf(edid_file, "\n" ); } fprintf(edid_file, "%02x ", netv_edid[i] ); } fprintf(edid_file, "\n" ); fclose( edid_file ); system("sync"); // force it to commit to flash write_modeline(netv_edid); exit(1); } } snoopctl = read_byte(0x0); snoopctl_orig = snoopctl; compctl = read_byte(0x3); fprintf(stderr, "Checking semaphore...\n" ); if( (compctl & 0x80) ) { // semaphore is set fprintf(stderr, "Semaphore set, someone else is running: exiting.\n" ); return 1; } compctl |= 0x80; // set the semaphore write_byte( 0x3, compctl ); fprintf(stderr, "Grabbed semaphore!\n" ); // turn off EDID squashing buffer = read_byte( FPGA_SNOOP_CTL_ADR ) ; buffer &= 0xFB; write_byte( FPGA_SNOOP_CTL_ADR, buffer ); snoopctl = read_byte(0x0); snoopctl &= 0xFE; snoopctl |= 0x08; // force HPD, leave all other bits intact write_byte( 0x0, snoopctl ); // hpd is now forcing deepsleep(0, 150); // 150 ms sleep snoopctl &= 0xF7; write_byte( 0x0, snoopctl ); // hpd is now releasing deepsleep(1, 0); // 1 s sleep to get the EDIDs transferred for( i = 0; i < 0x100; i++ ) { write_byte( 0x1, (unsigned char) i & 0xFF ); write_byte( 0x0, snoopctl | 0x2 ); write_byte( 0x0, snoopctl ); if( (i % 16) == 0 ) { fprintf(stderr, "\n%02x: ", i ); } edid[i] = read_byte(0x2); fprintf(stderr, "%02x ", edid[i]); } fprintf(stderr, "\n" ); // turn edid squashing back on buffer = read_byte( FPGA_SNOOP_CTL_ADR ) ; buffer |= 0x04; write_byte( FPGA_SNOOP_CTL_ADR, buffer ); } else { test_file = fopen( test_fname, "r" ); if( test_file != NULL ) { for( i = 0; i < 256; i++ ) { fscanf(test_file, "%x%*[ \n\t]", &temp ); edid[i] = temp & 0xFF; if( i % 16 == 0 ) fprintf(stderr, "\n" ); fprintf(stderr, "%02x ", edid[i] ); } } else { fprintf(stderr, "Can't open %s, aborting.\n", test_fname ); exit(0); } } if( parse_edid( edid ) ) { fprintf(stderr, "invalid EDID from monitor, big problems\n" ); } // alright, at this point, i need to extract a mode list // in a form that's usable so i can process it // 0x80 is a magic number that is the offset to // HDMI_EXTENSION. this code is in parse-edid.c and // should probably be broken into a header parse_hdmi_extension_return(edid + 0x80, cea_list); fprintf(stderr,"modes supported: "); num_modes = 0; for( i = 0; i < 128; i++ ) { if( cea_list[i] != 0 ) { fprintf(stderr, "%d ", i ); num_modes ++; } } fprintf(stderr,"\n" ); printf( "\n" ); // compatibility with bug in check_edid.c if( num_modes == 0 ) { fprintf(stderr, "No overlapping modes supported between NeTV and monitor. Forcing 720p and praying...\n" ); cea_list[4] = 1; } // now build a new EDID based upon the supported modes from the monitor new_edid = make_edid(cea_list); memcpy( netv_edid, new_edid, sizeof(unsigned char) * 128); free(new_edid); hdmi = make_hdmi(cea_list); memcpy( &(netv_edid[128]), hdmi, sizeof(unsigned char) * 128); free(hdmi); // HDMI is calloc'd inside make_hdmi if( cache_valid ) { for( i = 0; i < 256; i++ ) { if( netv_edid[i] != cached_edid[i] ) cache_valid = 0; } } if( !cache_valid ) { fprintf( stderr, "EDID cache is invalid, committing to disk.\n" ); // if cache is invalid... // write the EDID to a file for( i = 0; i < 256; i++ ) { if( (i % 16 == 0) && (i != 0) ) { fprintf(edid_file, "\n" ); } fprintf(edid_file, "%02x ", netv_edid[i] ); } fprintf(edid_file, "\n" ); fclose( edid_file ); system("sync"); // force it to commit to flash } else { fprintf( stderr, "EDID cache was valid, not touching the disk.\n" ); fclose( edid_file ); } if( test_fname == NULL ) { fprintf(stderr, "Pulling HPD to re-load the spoofed NeTV EDID.\n"); // edid squashing was turned off way above // load data into the modeline ROM write_modeline(netv_edid); // initiate a quick HPD to re-load the new EDID to the source snoopctl = read_byte(0x0); snoopctl |= (0x08 | 0x04); // force HPD, edid squash, leave all other bits intact write_byte( 0x0, snoopctl ); // hpd is now forcing deepsleep(0, 150); // 150 ms sleep snoopctl &= 0xF7; snoopctl |= 0x04; // make sure edid squash stays on write_byte( 0x0, snoopctl ); // hpd is now releasing } if( test_fname == NULL ) { fprintf(stderr, "\nDone. Releasing semaphore.\n" ); // then release semaphore compctl = read_byte(0x3); // refresh compctl in case other bits changed compctl &= 0x7F; // release semaphore write_byte( 0x3, compctl ); } return 0; }
int main( void ) { HAL_EnableDBGStopMode(); LowPowerConfiguration(); pc.baud(115200); // print banner pc.printf("\r\n============== DEBUG STARTED ==============\r\n"); /** User button triggers the ultrasonic sensor */ //PinDetect button(PC_13,PullUp); //button.attach_asserted(&sensor, &UltraSonic::triggerSample); // callback routine to trigger usonic //button.setSampleFrequency(); LoRaMacPrimitives_t LoRaMacPrimitives; LoRaMacCallback_t LoRaMacCallbacks; MibRequestConfirm_t mibReq; //BoardInitMcu( ); //BoardInitPeriph( ); BoardInit( ); TimerInit( &mainLoopTimeout, onMainLoopTimeoutEvent ); DeviceState = DEVICE_STATE_INIT; while( 1 ) { wait(0.5); // This is a kind of watchdog on the main loop, if the timer isn't cancelled in x seconds then loop will enter INIT state TimerSetValue( &mainLoopTimeout, 3000000 ); TimerStart( &mainLoopTimeout ); switch( DeviceState ) { case DEVICE_STATE_INIT: { pc.printf("DEVICE_STATE_INIT\r\n"); LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm; LoRaMacPrimitives.MacMcpsIndication = McpsIndication; LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm; LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel; LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks ); TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent ); TimerInit( &ultrasonicTimer, OnUltrasonicTimeout ); mibReq.Type = MIB_ADR; mibReq.Param.AdrEnable = LORAWAN_ADR_ON; LoRaMacMibSetRequestConfirm( &mibReq ); // MAC information base service to set attributes of LoRaMac layer mibReq.Type = MIB_PUBLIC_NETWORK; mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK; LoRaMacMibSetRequestConfirm( &mibReq ); sensor.distanceAvailable = false; // Ensure initialised to false after power up DeviceState = DEVICE_STATE_JOIN; break; } case DEVICE_STATE_JOIN: { pc.printf("DEVICE_STATE_JOIN\r\n"); #if( OVER_THE_AIR_ACTIVATION != 0 ) pc.printf("OTA\r\n"); MlmeReq_t mlmeReq; // Initialize LoRaMac device unique ID //BoardGetUniqueId( DevEui ); mlmeReq.Type = MLME_JOIN; // MAC management service types are MLME_JOIN or MLME_LINK_CHECK mlmeReq.Req.Join.DevEui = DevEui; mlmeReq.Req.Join.AppEui = AppEui; mlmeReq.Req.Join.AppKey = AppKey; if( NextTx == true ) { LoRaMacMlmeRequest( &mlmeReq ); // Sends a join request } // Schedule next packet transmission //TxDutyCycleTime = OVER_THE_AIR_ACTIVATION_DUTYCYCLE; //DeviceState = DEVICE_STATE_CYCLE; DeviceState = DEVICE_STATE_CYCLE; #else pc.printf("ABP\r\n"); // Choose a random device address if not already defined in Comissioning.h if( DevAddr == 0 ) { // Random seed initialization // srand1( BoardGetRandomSeed( ) ); // Choose a random device address DevAddr = randr( 0, 0x01FFFFFF ); } mibReq.Type = MIB_NET_ID; mibReq.Param.NetID = LORAWAN_NETWORK_ID; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_DEV_ADDR; mibReq.Param.DevAddr = DevAddr; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_NWK_SKEY; mibReq.Param.NwkSKey = NwkSKey; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_APP_SKEY; mibReq.Param.AppSKey = AppSKey; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_NETWORK_JOINED; mibReq.Param.IsNetworkJoined = true; LoRaMacMibSetRequestConfirm( &mibReq ); DeviceState = DEVICE_STATE_SEND; #endif break; } case DEVICE_STATE_SEND: { pc.printf("DEVICE_STATE_SEND\r\n"); if( NextTx == true ) { pc.printf("Sending\r\n"); PrepareTxFrame( AppPort ); NextTx = SendFrame( ); } } case DEVICE_STATE_CYCLE: { pc.printf("DEVICE_STATE_CYCLE\r\n"); pc.printf("SensorState = %d, distanceAvailable = ",SensorState); if (sensor.distanceAvailable) pc.printf("true\r\n"); else pc.printf("false\r\n"); pc.printf("LoRaMacState = %d\r\n",GetMacStatus()); if ((SensorState == SENT) || (SensorState == INIT)) { /** Range and Temperature readings have been sent so we can sleep */ TimerStop( &TxNextPacketTimer ); // Wait for MAC state to become idle before deep sleeping if (GetMacStatus() == 0) { // Ensure MAC state is idle before sleeping pc.printf("DeepSleep ..zzz.\r\n"); TimerStop(&mainLoopTimeout); // cancel main loop guard timeout WakeUp::set(DEEPSLEEP_SECONDS); // Set RTC alarm to wake up from deep sleep LowPowerPrep(); deepsleep(); // Deep sleep until wake up alarm from RTC LowPowerRestore(); SensorState = TRIGGERED; /* Trigger ultrasonic sensor and start sensor read failure timer */ sensor.triggerSample(); // Get Ultrasonic reading TimerSetValue( &ultrasonicTimer, 2000000 ); // 2s timeout TimerStart( &ultrasonicTimer ); DeviceState = DEVICE_STATE_SLEEP; // Cycle in sleep until sensor readings ready } else { // Cycle around until MAC state is idle DeviceState = DEVICE_STATE_CYCLE; } } else { // Error shouldn't get here pc.printf("Error State!!\r\n"); //TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime ); //TimerStart( &TxNextPacketTimer ); SensorState = INIT; DeviceState = DEVICE_STATE_SLEEP; } break; } case DEVICE_STATE_SLEEP: { pc.printf("DEVICE_STATE_SLEEP\r\n"); // Loop until sensor ready if ((SensorState == TRIGGERED) && (sensor.distanceAvailable)) { pc.printf("Xmit Reading..!\r\n"); TimerStop( &ultrasonicTimer ); // stop the sensor failure timer SensorState = SENDING; TimerSetValue( &TxNextPacketTimer, 10 ); // Schedule immediate transmission TimerStart( &TxNextPacketTimer ); } break; } default: { DeviceState = DEVICE_STATE_INIT; break; } } } }