summaryrefslogtreecommitdiff
path: root/avr/con-utils.c
blob: 4a96771040c536123388f92657a0d1e6fe54c982 (plain)
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
/*
 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include "common.h"
#include <string.h>
#include <avr/wdt.h>

#include "config.h"
#include "serial.h"
#include "background.h"
#include "con-utils.h"

uint_fast8_t tstc(void)
{
	bg_shed();
	return serial_tstc();
}

int my_getchar(uint_fast8_t waitforchar)
{
	int c;

	do {
		bg_shed();
		c = serial_getc();
	} while ((c < 0) && waitforchar);

#ifdef CONFIG_SYS_FBOOTSIG
	if (c < 0)
		return c;

	static const FLASH unsigned char bootsig[] = {CONFIG_SYS_FBOOTSIG};
	static uint8_t pb;
	unsigned char uc = c;


	if (bootsig[pb] == 0) {
		if (uc == 0xff) {
			wdt_enable(WDTO_15MS);
			for(;;)
				;
		} else
			pb = 0;

	} else {
		if (bootsig[pb] == uc)
			pb++;
		else
			pb = 0;
	}
#endif

	return c;
}


/* test if ctrl-c was pressed */

static uint_fast8_t ctrlc_disabled;	/* see disable_ctrl() */
static uint_fast8_t ctrlc_was_pressed;

uint_fast8_t ctrlc(void)
{
	bg_shed();
	if (!ctrlc_disabled) {
		switch (serial_getc()) {
		case 0x03:		/* ^C - Control C */
			ctrlc_was_pressed = 1;
			return 1;
		default:
			break;
		}
	}
	return 0;
}

/* Reads user's confirmation.
   Returns 1 if user's input is "y", "Y", "yes" or "YES"
*/
uint_fast8_t confirm_yesno(void)
{
	unsigned int i;
	char str_input[5];

	/* Flush input */
	while (serial_getc())
		;
	i = 0;
	while (i < sizeof(str_input)) {
		str_input[i] = my_getchar(1);
		putchar(str_input[i]);
		if (str_input[i] == '\r')
			break;
		i++;
	}
	putchar('\n');
	if (strncmp(str_input, "y\r", 2) == 0 ||
	    strncmp(str_input, "Y\r", 2) == 0 ||
	    strncmp(str_input, "yes\r", 4) == 0 ||
	    strncmp(str_input, "YES\r", 4) == 0)
		return 1;
	return 0;
}

/* pass 1 to disable ctrlc() checking, 0 to enable.
 * returns previous state
 */
uint_fast8_t disable_ctrlc(uint_fast8_t disable)
{
	uint_fast8_t prev = ctrlc_disabled;	/* save previous state */

	ctrlc_disabled = disable;
	return prev;
}

uint_fast8_t had_ctrlc (void)
{
	return ctrlc_was_pressed;
}

void clear_ctrlc(void)
{
	ctrlc_was_pressed = 0;
}