/* pollConsole(): * Common function used to query the console port with a message. * If after about a 2-second period (or the time specified by POLLTIMEOUT), * there is no response from the console, then return 0; else return * the character that was received. */ int pollConsole(char *msg) { char *env; int pollval, msec; struct elapsed_tmr tmr; env = getenv("POLLTIMEOUT"); if (env) msec = strtol(env,0,0); else msec = 2000; pollval = 0; printf("%s",msg); startElapsedTimer(&tmr,msec); while(!msecElapsed(&tmr)) { if (gotachar()) { while(gotachar()) pollval = getchar(); break; } pollethernet(); } putstr("\r\n"); if (ELAPSED_TIMEOUT(&tmr)) return(0); return(pollval); }
/* monTimer(): * Provide the API with the ability to start a millisecond-granularity * timer with some countdown value, and poll it waiting for completion. */ int monTimer(int cmd, void *arg) { int rc = 0; struct elapsed_tmr *tmr = (struct elapsed_tmr *)arg; switch(cmd) { case TIMER_START: startElapsedTimer(tmr,tmr->start); break; case TIMER_ELAPSED: msecElapsed(tmr); if (ELAPSED_TIMEOUT(tmr)) rc = 1; break; case TIMER_QUERY: #if INCLUDE_HWTMR tmr->tmrflags = HWTMR_ENABLED; tmr->tpm = (unsigned long)TIMER_TICKS_PER_MSEC; tmr->currenttmrval = target_timer(); #else tmr->tmrflags = 0; tmr->tpm = (unsigned long)LoopsPerMillisecond; tmr->currenttmrval = 0; #endif break; default: rc = -1; break; } return(rc); }
int msecElapsed(struct elapsed_tmr *tmr) { ulong new_elapsed_low, new_tmrval, elapsed; /* If timeout has already occurred, then we can assume that this * function being called without a matching startElapsedTimer() call. */ if (ELAPSED_TIMEOUT(tmr)) return(1); #if INCLUDE_HWTMR new_tmrval = target_timer(); #else new_tmrval = tmr->tmrval + 1; #endif /* Record how many ticks elapsed since the last call to msecElapsed * and add that value to the total number of ticks that have elapsed. */ elapsed = new_tmrval - tmr->tmrval; new_elapsed_low = tmr->elapsed_low + elapsed; if (new_elapsed_low < tmr->elapsed_low) tmr->elapsed_high++; /* If the total elapsed number of ticks exceeds the timeout number * of ticks, then we can return 1 to indicate that the requested * amount of time has elapsed. Otherwise, we record the values and * return 0. */ if ((tmr->elapsed_high >= tmr->timeout_high) && (new_elapsed_low >= tmr->timeout_low)) { tmr->tmrflags |= TIMEOUT_OCCURRED; return(1); } tmr->tmrval = new_tmrval; tmr->elapsed_low = new_elapsed_low; return(0); }
/* msecRemainging(): * Used to query how many milliseconds were left (if any) in the timeout. */ ulong msecRemaining(struct elapsed_tmr *tmr) { ulong high, low, msectot, leftover, divisor; if (ELAPSED_TIMEOUT(tmr)) return(0); high = tmr->timeout_high - tmr->elapsed_high; low = tmr->timeout_low - tmr->elapsed_low; msectot = leftover = 0; #if INCLUDE_HWTMR divisor = (ulong)TIMER_TICKS_PER_MSEC; #else divisor = (ulong)LoopsPerMillisecond; #endif while(1) { while (low > divisor) { msectot++; low -= divisor; } leftover += low; if (high == 0) break; else { high--; low = 0xffffffff; } } while(leftover > divisor) { msectot++; low -= divisor; } return(msectot); }
/* getfullline(): * Basic line retrieval; but with a few options... * This function is accessed through the getline_xx functions * below. * Args... * buf: pointer to buffer to be used to place the incoming characters. * max: size of the buffer. * ledit: if set, then allow the line-editor to be used if ESC is input. * timeout: if positive, then after 'timeout' number of seconds waiting * per character, giveup. * if negative, then after 'timeout' number of seconds waiting * total, giveup. * if zero, then wait forever. * prefill: if set, prefill the buffer with that string and show the user. * echo: if set, characters are echoed as they are entered. */ int getfullline(char *buf,int max,int ledit, int timeout,char *prefill, int echo) { char *base; struct elapsed_tmr tmr; static unsigned char crlf; int tot, idx, cumulativetimeout; cumulativetimeout = 0; tot = idx = 0; base = buf; max -= 1; /* Make sure there is space for the null terminator. */ if (prefill) { strcpy(base,prefill); tot = strlen(prefill); putstr(prefill); buf += tot; idx = tot; } /* If the timeout parameter is negative, then assume that this is * to be run with a cumulative timeout rather than a timeout that * is re-started after each character... */ if (timeout < 0) { cumulativetimeout = 1; timeout = abs(timeout); } for(;idx<max;idx++) { if (timeout > 0) { startElapsedTimer(&tmr,timeout); while(!msecElapsed(&tmr)) { if (gotachar()) break; pollethernet(); } if (cumulativetimeout) timeout = msecRemaining(&tmr); if (ELAPSED_TIMEOUT(&tmr)) { *buf = 0; return(-1); /* Return negative to indicate timeout */ } } if (cumulativetimeout && timeout == 0) { *buf = 0; return(-1); } *buf = (char)getchar(); if (!*buf) { idx--; continue; } #if INCLUDE_LINEEDIT if ((*buf == 0x1b) && (ledit)) { (void)line_edit(base); break; } else #endif { if ((*buf == '\r') || (*buf == '\n')) { if ((crlf) && (*buf != crlf)) { crlf = 0; continue; } #if UMON_TARGET == UMON_TARGET_XT || UMON_TARGET == UMON_TARGET_ARM // use CR/LF puts("\r"); #else putchar('\n'); #endif crlf = *buf; *buf = 0; break; } if (*buf == '\b') { if (tot) { idx -= 2; buf--; tot--; if (echo) putstr("\b \b"); } } else if (*buf == CTLC) { puts("^C"); *base = 0; return(0); } else { if (echo) putchar(*buf); tot++; buf++; } crlf = 0; } } if (idx == max) { printf((char *)"\007\nInput too long (exceeds %d bytes).\n",max); *buf = 0; return(0); } #if INCLUDE_LINEEDIT if (ledit) historylog(base); #endif return(strlen(base)); }