summaryrefslogtreecommitdiffhomepage
path: root/digital/ucoolib/ucoolib/base/proto/proto.cc
blob: 2e41a17fcd4288b6c81144aa8691682a0da3aa7f (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
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
// ucoolib - Microcontroller object oriented library. {{{
//
// Copyright (C) 2013 Nicolas Schodet
//
// APBTeam:
//        Web: http://apbteam.org/
//      Email: team AT apbteam DOT org
//
// 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 "proto.hh"

#include <cctype>

namespace ucoo {

Proto::Proto (Handler &handler, Stream &stream)
    : handler_ (handler), stream_ (stream), step_ (IDLE)
{
}

void
Proto::accept ()
{
    int c;
    while ((c = stream_.getc ()) != -1)
    {
        if (c == '!')
            step_ = BANG;
        else
        {
            switch (step_)
            {
            case IDLE:
                // Nothing received yet.
                break;
            case BANG:
                // Bang received yet.
                if (std::isalpha (c))
                {
                    cmd_ = c;
                    size_ = 0;
                    step_ = COMMAND;
                }
                else
                {
                    handler_.proto_handle (*this, '?', 0, 0);
                    step_ = IDLE;
                }
                break;
            case COMMAND:
                // Command received yet.
                if (c == '\r' || c == '\n')
                {
                    handler_.proto_handle (*this, cmd_, args_, size_);
                    step_ = IDLE;
                }
                else if (c == '\'')
                    step_ = ARG_CHAR;
                else if (c == '"')
                    step_ = ARG_STRING;
                else
                {
                    step_ = ARG_DIGIT;
                    accept_digit (c);
                }
                break;
            case ARG_DIGIT:
                step_ = COMMAND;
                accept_digit (c);
                break;
            case ARG_CHAR:
                step_ = COMMAND;
                accept_char (c);
                break;
            case ARG_STRING:
                if (c == '\r' || c == '\n')
                {
                    handler_.proto_handle (*this, cmd_, args_, size_);
                    step_ = IDLE;
                }
                else
                {
                    accept_char (c);
                }
                break;
            }
        }
    }
}

void
Proto::send (char cmd)
{
    char buf[3];
    char *p = buf;
    *p++ = '!';
    *p++ = cmd;
    *p++ = '\n';
    stream_.write (buf, p - buf);
}

void
Proto::send (char cmd, const char *fmt, int a0)
{
    send (cmd, fmt, a0, 0, 0, 0);
}

void
Proto::send (char cmd, const char *fmt, int a0, int a1)
{
    send (cmd, fmt, a0, a1, 0, 0);
}

void
Proto::send (char cmd, const char *fmt, int a0, int a1, int a2)
{
    send (cmd, fmt, a0, a1, a2, 0);
}

static inline void
send_byte (char *buf, uint8_t b)
{
    static const char hexchars[] = "0123456789abcdef";
    buf[0] = hexchars[b >> 4];
    buf[1] = hexchars[b & 0xf];
}

static char *
send_arg (char *buf, char fmt, int v)
{
    switch (fmt)
    {
    case 'L':
    case 'l':
        send_byte (buf, (v >> 24) & 0xff);
        buf += 2;
        send_byte (buf, (v >> 16) & 0xff);
        buf += 2;
    case 'H':
    case 'h':
        send_byte (buf, (v >> 8) & 0xff);
        buf += 2;
    case 'B':
    case 'b':
        send_byte (buf, v & 0xff);
        buf += 2;
        break;
    default:
        assert_unreachable ();
    }
    return buf;
}

void
Proto::send (char cmd, const char *fmt, int a0, int a1, int a2, int a3)
{
    char buf[3 + 4 * 4 * 2]; // Large enough for the largest frame.
    char *p = buf;
    *p++ = '!';
    *p++ = cmd;
    if (*fmt)
    {
        p = send_arg (p, *fmt++, a0);
        if (*fmt)
        {
            p = send_arg (p, *fmt++, a1);
            if (*fmt)
            {
                p = send_arg (p, *fmt++, a2);
                if (*fmt)
                {
                    p = send_arg (p, *fmt++, a3);
                }
            }
        }
    }
    *p++ = '\n';
    stream_.write (buf, p - buf);
}

void
Proto::send_buf (char cmd, const uint8_t *args, int size)
{
    int i;
    char buf[3 + 2 * size];
    char *p = buf;
    *p++ = '!';
    *p++ = cmd;
    for (i = 0; i < size; i++)
    {
        send_byte (p, args[i]);
        p += 2;
    }
    *p++ = '\n';
    stream_.write (buf, p - buf);
}

void
Proto::accept_digit (int c)
{
    // Test for argument list overflow.
    if (size_ >= UCOO_CONFIG_BASE_PROTO_ARGS_MAX_SIZE)
      {
	handler_.proto_handle (*this, '?', 0, 0);
	step_ = IDLE;
	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
      {
	handler_.proto_handle (*this, '?', 0, 0);
	step_ = IDLE;
	return;
      }
    // Add digit.
    args_[size_] <<= 4;
    args_[size_] |= c;
    if (step_ == COMMAND)
	size_++;
}

void
Proto::accept_char (int c)
{
    // Test for argument list overflow or unwanted char.
    if (size_ >= UCOO_CONFIG_BASE_PROTO_ARGS_MAX_SIZE || !std::isprint (c))
      {
	handler_.proto_handle (*this, '?', 0, 0);
	step_ = IDLE;
	return;
      }
    // Add char.
    args_[size_] = c;
    size_++;
}

} // namespace ucoo