summaryrefslogtreecommitdiff
path: root/digital/ucoolib/ucoolib/arch/host/mex/mex_msg.host.cc
blob: 93d40956925f0102e4077d84c4a648abb9aef12c (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
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
// ucoolib - Microcontroller object oriented library. {{{
//
// Copyright (C) 2012 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.
//
// }}}
#define __STDC_LIMIT_MACROS 1
#include "mex_msg.hh"

namespace ucoo {
namespace mex {

const int default_head_skip = 3;

/// Compute needed size to store given format.
static int
calcsize (const char *fmt)
{
    int size = 0;
    for (; *fmt; fmt++)
    {
        switch (*fmt)
        {
        case 'l':
        case 'L':
            size += 4;
            break;
        case 'h':
        case 'H':
            size += 2;
            break;
        case 'b':
        case 'B':
            size += 1;
            break;
        }
    }
    return size;
}

Msg::Msg (mtype_t mtype)
    : head_skip_ (default_head_skip),
      mtype_ (mtype)
{
    payload_.resize (head_skip_);
}

Msg::Msg (MsgReader &reader)
    : head_skip_ (default_head_skip),
      mtype_ (MTYPE_IDLE)
{
    int size = reader.size ();
    payload_.resize (head_skip_ - 1 + size);
    reader.read (&payload_.front () + head_skip_ - 1);
    mtype_ = static_cast<mtype_t> (payload_[head_skip_ - 1]);
}

void
Msg::write (MsgWriter &writer)
{
    assert (head_skip_ >= 1);
    payload_[head_skip_ - 1] = static_cast<int> (mtype_);
    writer.write (&payload_.front () + head_skip_ - 1,
                  payload_.size () - head_skip_ + 1);
}

MsgPusher
Msg::push (const char *fmt)
{
    int push_index = payload_.size ();
    payload_.resize (push_index + calcsize (fmt));
    return MsgPusher (payload_.begin () + push_index, fmt);
}

void
Msg::push (const char *buffer, int size)
{
    payload_.insert (payload_.end (), buffer, buffer + size);
}

MsgPoper
Msg::pop (const char *fmt)
{
    int pop_size = calcsize (fmt);
    assert (pop_size <= (int) payload_.size () - head_skip_);
    int pop_index = head_skip_;
    head_skip_ += pop_size;
    return MsgPoper (payload_.begin () + pop_index, fmt);
}

const char *
Msg::pop (int size)
{
    assert (size <= (int) payload_.size () - head_skip_);
    const char *r = &payload_.front () + head_skip_;
    head_skip_ += size;
    return r;
}

MsgPusher
Msg::encapsulate (mtype_t mtype, const char *fmt)
{
    int head_size = calcsize (fmt) + 1;
    assert (head_size <= head_skip_);
    payload_[head_skip_ - 1] = static_cast<int> (mtype_);
    mtype_ = mtype;
    head_skip_ -= head_size;
    return MsgPusher (payload_.begin () + head_skip_, fmt);
}

void
Msg::decapsulate ()
{
    assert (1 <= payload_.size () - head_skip_);
    mtype_ = static_cast<mtype_t> (payload_[head_skip_]);
    head_skip_++;
}

int
Msg::len () const
{
    return payload_.size () - head_skip_;
}

MsgPusher &
MsgPusher::operator<< (int32_t v)
{
    push (v);
    return *this;
}

MsgPusher &
MsgPusher::operator<< (uint32_t v)
{
    push (v);
    return *this;
}

MsgPusher::MsgPusher (std::vector<char>::iterator out, const char *fmt)
    : out_ (out),
      fmt_ (fmt)
{
}

void
MsgPusher::push (int64_t v)
{
    switch (*fmt_)
    {
    case 'l':
        assert (v >= INT32_MIN && v <= INT32_MAX);
        break;
    case 'L':
        assert (v >= 0 && v <= UINT32_MAX);
        break;
    case 'h':
        assert (v >= INT16_MIN && v <= INT16_MAX);
        break;
    case 'H':
        assert (v >= 0 && v <= UINT16_MAX);
        break;
    case 'b':
        assert (v >= INT8_MIN && v <= INT8_MAX);
        break;
    case 'B':
        assert (v >= 0 && v <= UINT8_MAX);
        break;
    default:
        assert (0);
    }
    switch (*fmt_)
    {
    case 'l':
    case 'L':
        *out_++ = v >> 24;
        *out_++ = v >> 16;
    case 'h':
    case 'H':
        *out_++ = v >> 8;
    case 'b':
    case 'B':
        *out_++ = v >> 0;
    }
    fmt_++;
}

MsgPoper &
MsgPoper::operator>> (int32_t &v)
{
    v = pop (31, true);
    return *this;
}

MsgPoper &
MsgPoper::operator>> (uint32_t &v)
{
    v = pop (32, false);
    return *this;
}

MsgPoper &
MsgPoper::operator>> (int16_t &v)
{
    v = pop (15, true);
    return *this;
}

MsgPoper &
MsgPoper::operator>> (uint16_t &v)
{
    v = pop (16, false);
    return *this;
}

MsgPoper &
MsgPoper::operator>> (int8_t &v)
{
    v = pop (7, true);
    return *this;
}

MsgPoper &
MsgPoper::operator>> (uint8_t &v)
{
    v = pop (8, false);
    return *this;
}

MsgPoper::MsgPoper (std::vector<char>::const_iterator in, const char *fmt)
    : in_ (in),
      fmt_ (fmt)
{
}

uint32_t
MsgPoper::pop_bytes (int bytes)
{
    uint32_t r = 0;
    while (bytes--)
        r = r << 8 | static_cast<unsigned char> (*in_++);
    return r;
}

int64_t
MsgPoper::pop (int bits, bool sign)
{
    switch (*fmt_++)
    {
    case 'l':
        assert (bits >= 31 && sign);
        return static_cast<int32_t> (pop_bytes (4));
    case 'L':
        assert (bits >= 32);
        return static_cast<uint32_t> (pop_bytes (4));
    case 'h':
        assert (bits >= 15 && sign);
        return static_cast<int16_t> (pop_bytes (2));
    case 'H':
        assert (bits >= 16);
        return static_cast<uint16_t> (pop_bytes (2));
    case 'b':
        assert (bits >= 7 && sign);
        return static_cast<int8_t> (pop_bytes (1));
    case 'B':
        assert (bits >= 8);
        return static_cast<uint8_t> (pop_bytes (1));
    default:
        assert_unreachable ();
    }
}

} // namespace mex
} // namespace ucoo