summaryrefslogtreecommitdiffstats
path: root/main/common/chario.c
blob: fb8fbaef2ecc59009fdc7aa2e9f6d814aebf09d4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/**************************************************************************
 *
 * Copyright (c) 2013 Alcatel-Lucent
 * 
 * Alcatel Lucent licenses this file to You under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License.  A copy of the License is contained the
 * file LICENSE at the top level of this repository.
 * You may also obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **************************************************************************
 *
 * chario.c:
 * This code supports some basic character io functionality.
 *
 * Original author:     Ed Sutter (ed.sutter@alcatel-lucent.com)
 *
 */
#include "config.h"
#include "cpuio.h"
#include "genlib.h"
#include "stddefs.h"
#include "timer.h"
#include "ether.h"
#include "fbi.h"

#define CTLC	0x03	/* control-c */

int rawmode, ConsoleBaudRate;

/* extWatchDog():
 * This function pointer is used by the application to override the
 * monitor's configured watchdog functionality.  Refer to config.h
 * in umon_ports/template for more information on the WATCHDOG macro.
 */
#ifdef WATCHDOG_ENABLED
int (*remoteWatchDog)(void);
#endif

/* remotexxxx() function pointers:
 * These function pointers provide the application with the ability to
 * override the monitor's default putchar/getchar/gotachar functions.
 * See comments above InitRemoteIO() function below for more info.
 */
int	(*remoterawon)(void);
int (*remoterawoff)(void);
int	(*remoteputchar)(int);
int (*remotegetchar)(void);
int (*remotegotachar)(void);

static char console_echo_off;

/* console_echo():
 * Enable/disable the output of the console...
 */
void
console_echo(int enable)
{
	if (enable)
		console_echo_off = 0;
	else
		console_echo_off = 1;
}

/* putchar():
 * 	Output a character to the stdout RS232 port.
 *  If console_echo_off is set, then we DON'T send the
 *  character to the console.
 */
int
putchar(char c)
{
	/* If the remoteputchar function pointer is non-zero, then we
	 * assume that the default putchar function has been overridden
	 * by some overlaying application using mon_com(CHARFUNC_PUTCHAR,),
	 * so we use that redefined function instead...
	 */
	if (remoteputchar) {
		remoteputchar(c);
		return((int)c);
	}

	if (rawmode) {
		if (!console_echo_off)
			target_putchar(c);
		fbi_putchar(c);
		return((int)c);
	}

	/* If redirection is active, the redirect the character to a file
	 * in TFS.  Note that if INCLUDE_REDIRECT is 0, then this function
	 * is NULL (see genlib.h).
	 */
	RedirectCharacter(c);

	/* Call the target-specific function that puts the character out on
	 * the console port (precede '\n' with '\r').
	 * For each call to target_putchar(), call SendIPMonChar so that if
	 * this output is generated by moncmd, it will be sent back to the
	 * moncmd client.  Note that if INCLUDE_ETHERNET is 0, then the
	 * function SendIPMonChar is NULL (see ether.h).
	 */
	if (c == '\n') {
		SendIPMonChar('\r',0);
		fbi_putchar('\r');
		if (!console_echo_off)
			target_putchar('\r');
	}
	SendIPMonChar(c,0);
	if (!console_echo_off)
		target_putchar(c);
	fbi_putchar(c);
	WATCHDOG_MACRO;
	return((int)c);
}

int 
getchar(void)
{
	/* If the remotegetchar function pointer is non-zero, then we
	 * assume that the default getchar function has been overridden
	 * by some overlaying application using mon_com(CHARFUNC_GETCHAR,),
	 * so we use that redefined function instead...
	 */
    if (remotegetchar)
        return(remotegetchar());
    
	while(!gotachar()) {
		/* While waiting for an incoming character, call pollethernet()
		 * to process any incoming packets.  Note that if INCLUDE_ETHERNET
		 * is 0 in config.h, then this function is NULL (see ether.h).
		 */
		WATCHDOG_MACRO;
    	pollethernet();
	}

	return(target_getchar());
}

int
gotachar(void)
{
	/* If the remotegotachar function pointer is non-zero, then we
	 * assume that the default gotachar function has been overridden
	 * by some overlaying application using mon_com(CHARFUNC_GOTACHAR,),
	 * so we use that redefined function instead...
	 */
    if (remotegotachar)
        return(remotegotachar());
    
	fbi_cursor();
	WATCHDOG_MACRO;
	return(target_gotachar());
}

/* flush_console_out():
 * Call this function to wait for the console UART to flush all outgoing
 * characters.  Note that this function has a timeout in the loop.
 */
void
flush_console_out(void)
{
	int timeout = 0;

    if (remoteputchar)
        return;

	while(timeout++ < 50000) {
		if (target_console_empty()) {
			monDelay(10);
			break;
		}
	}
}

/* flush_console_in(void):
 * While there is input queued up from the console, 
 * flush it to the bit bucket...
 */
void
flush_console_in(void)
{
	while(gotachar())
		getchar();
}


/* rawon() & rawoff():
 * Used primarily by xmodem.  When xmodem runs, it must be assured that
 * the interface is in RAW mode.  For the case of the monitor alone, it
 * will always be in a raw mode.  These functions are primarily for use
 * when an application has re-loaded the serial driver and may have put
 * it in a non-raw mode.  The mon_com() calls CHARFUNC_RAWMODEON and
 * CHARFUNC_RAWMODEOFF establish these pointers.
 */
void
rawon(void)
{
    if (remoterawon)
        remoterawon();
	rawmode = 1;
}

void
rawoff(void)
{
    if (remoterawoff)
        remoterawoff();
	rawmode = 0;
}

/* puts() & putstr():
 * Two string-output functions.  
 * puts: normal "puts" as is available in standard c libs.
 * putstr: same as "puts", but no ending newline.
 */

void
putstr(char *string)
{
	while(*string) {
		putchar(*string);
		string++;
	}
}

void
puts(char *string)
{
	putstr(string);
	putchar('\n');
}


/* 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 (getenv("CLI_HEXOUT"))
			printf("<%02x>",*buf);
		
		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;
				}
				puts("\r");
				crlf = *buf;
				*buf = 0;
				break;
			}
			if (*buf == '\b' || *buf == 0x7f) {
				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));
}

int
getline(char *buf, int max, int ledit)
{
	return(getfullline(buf,max,ledit,0,0,1));
}

int
getline_t(char *buf, int max, int timeout)
{
	return(getfullline(buf,max,0,timeout,0,1));
}

int
getline_p(char *buf, int max, int ledit, char *prefill)
{
	return(getfullline(buf,max,ledit,0,prefill,1));
}

/* getpass():
 */
#if INCLUDE_USRLVL
char *
getpass(char *prompt,char *buf,int max, int timeout)
{
	putstr(prompt);
	if (getfullline(buf,max,0,timeout*1000,0,0) == -1)
		putchar('\n');
	return(buf);
}
#endif

/* getbytes():
 *	Similar to gets() except that the caller specifies the number
 *	of characters and whether or not to block.
 *  If the copy to the buffer fails, abort and return the current
 *  count.
 */

int
getbytes(char *buf,int cnt,int block)
{
	int	i;
	volatile char *bp;
	char c;

	bp = (volatile char *)buf;

	for(i=0;i<cnt;i++) {
		if (!block && !gotachar())
			break;
		c = (char)getchar();
		*bp = c;
		if (*bp != c)
			break;
		bp++;
	}
	return(i);
}

/* getbytes_t():
 *	Similar to getbytes() except that the caller specifies the allowed
 *  timeout between two consecutive bytes received.
 *  If the copy to the buffer fails, or timeout occures, abort and return
 *	the current count.
 */

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);
}

int
putbytes(char *buf, int cnt)
{
	char *end;

	end = buf + cnt;

	while(buf < end) {
		putchar(*buf++);
	}
	return(cnt);
}

int
askuser(char *msg)
{
	int	yes, len;

#if INCLUDE_MONCMD
	/* If command was issued from UDP (i.e. moncmd), then
	 * immediately return 1...
	 */
	if (IPMonCmdActive)
		return(1);
#endif

	putstr(msg);
	len = strlen(msg);
	switch((char)getchar()) {
		case ' ':
		case 'y':
		case '\r':
		case '\n':
			yes = 1;
			break;
		default:
			yes = 0;
			break;
	}
	while(len) {
		putstr("\b \b");
		len--;
	}
	return(yes);
}

int
More(void)
{
	return(askuser((char *)"more?"));
}

int
hitakey(void)
{
	return(askuser((char *)"hit any key to continue..."));
}

/* RemoteIO functions:
 * The idea of "remote io" is to allow the monitor commands to still
 * run when the application has taken over the system.  The monitor's
 * connection to the serial port is a simple polled interface.  When 
 * the application comes up, it is very likely that it will overlay a
 * new driver onto the serial port.  If this happens, and the user at
 * the console interface of the application wants to execute a monitor
 * command, then the monitor's putchar/getchar/gotachar functions must
 * use functions that are part of the application.  These remote io
 * function pointers, if set, will point to those functions.
 */

void
InitRemoteIO(void)
{
	/* Null out the remote put/getchar functions. */
	remoterawon = 0;
	remoterawoff = 0;
	remoteputchar = 0;
	remotegetchar = 0;
	remotegotachar = 0;
}