Example #1
0
/* 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);
}
Example #2
0
/* 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);
}
Example #3
0
int
getbytes_t(char *buf,int cnt,int timeout)
{
	int	i;
	struct elapsed_tmr tmr;
	volatile char *bp;
	char c;

	bp = (volatile char *)buf;

	for(i=0;i<cnt;i++) {
		if (!gotachar()) {
			startElapsedTimer(&tmr,timeout);
			while(!gotachar() && !msecElapsed(&tmr));
			if (!gotachar())
				break;
		}
		c = (char)getchar();
		*bp = c;
		if (*bp != c)
			break;
		bp++;
	}
	return(i);
}
Example #4
0
/* monDelay():
 * Delay for specified number of milliseconds.
 * Refer to msecElapsed() description for a discussion on the
 * accuracy of this delay.
 */
void
monDelay(int milliseconds)
{
	struct elapsed_tmr tmr;

	startElapsedTimer(&tmr,milliseconds);
	while(!msecElapsed(&tmr)) {
		WATCHDOG_MACRO;
		pollethernet();
	}
}
Example #5
0
/* dhcpStateCheck():
 *	Called by pollethernet() to monitor the progress of DHCPState.
 *	The retry rate is "almost" what is specified in the RFC...
 *	Refer to the RetransmitDelay() function for details.
 *
 *	Regarding timing...
 *	The DHCP startup may be running without an accurate measure of elapsed
 *	time.  The value of LoopsPerMillisecond is used as an approximation of
 *	the number of times this function must be called for one second to 
 *	pass (dependent on network traffic, etc...).  RetransmitDelay() is
 *	called to retrieve the number of seconds that must elapse prior to 
 *	retransmitting the last DHCP message.  The static variables in this
 *	function are used to keep track of that timeout.
 */
void
dhcpStateCheck(void)
{
	int delaysecs;

	/* If the DHCP command has been issued, it is assumed that the script
	 * is handling retries...
	 */
	if (BOOTPCommandIssued)
		return;

	/* Return, restart or fall through; depending on DHCPState... */
	switch(DHCPState) {
		case DHCPSTATE_NOTUSED:
		case BOOTPSTATE_COMPLETE:
			return;
		case BOOTPSTATE_RESTART:
			BOOTPStartup(0);
			return;
		case BOOTPSTATE_INITIALIZE:
			DHCPState = BOOTPSTATE_INITDELAY;
			return;
		case BOOTPSTATE_INITDELAY:
			BOOTPElapsedSecs = 0;
			startElapsedTimer(&dhcpTmr,
				RetransmitDelay(DELAY_INIT_DHCP)*1000);
			BOOTPStartup(0);
			return;
		default:
			break;
	}

	if (msecElapsed(&dhcpTmr)) {
		int lastdelay;
		
		lastdelay = RetransmitDelay(DELAY_RETURN);
		delaysecs = RetransmitDelay(DELAY_INCREMENT);
		
		if (delaysecs != RETRANSMISSION_TIMEOUT) {
			BOOTPElapsedSecs += delaysecs;
			startElapsedTimer(&dhcpTmr,delaysecs*1000);

			BOOTPStartup(BOOTPElapsedSecs);
			printf("  BOOTP retry (%d secs)\n",lastdelay);
		}
		else {
			printf("  BOOTP giving up\n");
		}
	}
}
Example #6
0
/* 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));
}