summaryrefslogtreecommitdiff
path: root/n/avr/modules/proto/proto.c
blob: 5d54b36c48176aa9cfd98593b981da72e73ceb68 (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
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
/* proto.c */
/* avr.proto - Protocol AVR module. {{{
 *
 * Copyright (C) 2005 Nicolas Schodet
 *
 * Robot APB Team/Efrei 2006.
 *        Web: http://assos.efrei.fr/robot/
 *      Email: robot AT efrei DOT fr
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "common.h"
#include "proto.h"

#include <ctype.h>

/* +AutoDec */

/** Accept a digit to be used for args. */
static void
proto_accept_digit (uint8_t c);

#if AC_PROTO_QUOTE == 1
/** Accept a quoted char to be used for args. */
static void
proto_accept_char (uint8_t c);
#endif

/* Send a hex digit. */
inline static void
proto_hex (uint8_t h);

/* -AutoDec */

static uint8_t cmd;
static uint8_t size;
static uint8_t args[AC_PROTO_ARGS_MAX_SIZE];

/** Step of decoding:
 *  - 0: nothing received.
 *  - 1: bang received.
 *  - 2: command received.
 *  - 3: command received, and processing a number.
 *  - 4: quote received. */
static uint8_t step;

/** Accept a new character. */
void
proto_accept (uint8_t c)
{
    if (c == '!')
	step = 1;
    else
      {
	switch (step)
	  {
	  case 0:
	    /* Nothing received yet. */
	    break;
	  case 1:
	    /* Bang received yet. */
	    if (isalpha (c))
	      {
		cmd = c;
		size = 0;
		step = 2;
	      }
	    else
	      {
		AC_PROTO_CALLBACK ('?', 0, 0);
		step = 0;
	      }
	    break;
	  case 2:
	    /* Command received yet. */
	    if (c == '\r')
	      {
		AC_PROTO_CALLBACK (cmd, size, args);
		step = 0;
	      }
#if AC_PROTO_QUOTE == 1
	    else if (c == '\'')
		step = 4;
#endif
	    else
	      {
		step = 3;
		proto_accept_digit (c);
	      }
	    break;
	  case 3:
	    step--;
	    proto_accept_digit (c);
	    break;
#if AC_PROTO_QUOTE == 1
	  case 4:
	    step = 2;
	    proto_accept_char (c);
	    break;
#endif
	  }
      }
}

/** Accept a digit to be used for args. */
static void
proto_accept_digit (uint8_t c)
{
    /* Test for argument list overflow. */
    if (size >= AC_PROTO_ARGS_MAX_SIZE)
      {
	AC_PROTO_CALLBACK ('?', 0, 0);
	step = 0;
	return;
      }
    /* Convert from hexa. */
    if ('0' <= c && c <= '9')
	c -= '0';
    else if ('a' <= c && c <= 'f')
	c -= 'a' - 10;
    else if ('A' <= c && c <= 'F')
	c -= 'A' - 10;
    else
      {
	AC_PROTO_CALLBACK ('?', 0, 0);
	step = 0;
	return;
      }
    /* Add digit. */
    args[size] <<= 4;
    args[size] |= c;
    if (step == 2)
	size++;
}

#if AC_PROTO_QUOTE == 1
/** Accept a quoted char to be used for args. */
static void
proto_accept_char (uint8_t c)
{
    /* Test for argument list overflow. */
    if (size >= AC_PROTO_ARGS_MAX_SIZE)
      {
	AC_PROTO_CALLBACK ('?', 0, 0);
	step = 0;
	return;
      }
    /* Add char. */
    args[size] = c;
    size++;
}
#endif

/* Send a hex digit. */
inline static void
proto_hex (uint8_t h)
{
    AC_PROTO_PUTC (h >= 10 ? h - 10 + 'a' : h + '0');
}

/* Send a argument byte. */
void
proto_arg (uint8_t a)
{
    proto_hex ((a >> 4) & 0xf);
    proto_hex ((a >> 0) & 0xf);
}

/** Send a command, generic function. */
void
proto_send (uint8_t cmd, uint8_t size, uint8_t *args)
{
    AC_PROTO_PUTC ('!');
    AC_PROTO_PUTC (cmd);
    while (size--)
	proto_arg (*args++);
    AC_PROTO_PUTC ('\r');
}