]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/command.cpp
Rewrite command/reply system
[r2c2.git] / source / libmarklin / command.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <cstring>
9 #include <msp/strings/formatter.h>
10 #include "command.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 namespace Marklin {
16
17 Command::Command(Cmd c, const unsigned char *d, unsigned l):
18         cmd(c),
19         len(1),
20         sent(false)
21 {
22         data[0]=cmd;
23         if(d)
24         {
25                 memcpy(data+1, d, min(l, 127U));
26                 len+=min(l, 127U);
27         }
28 }
29
30 void Command::send(int fd)
31 {
32         write(fd, data, len);
33         sent=true;
34 }
35
36 ostream &operator<<(ostream &out, const Command &cmd)
37 {
38         out<<cmd.cmd;
39         for(unsigned i=1; i<cmd.len; ++i)
40                 out<<format(" %02X", static_cast<int>(cmd.data[i]));
41
42         return out;
43 }
44
45 } // namespace Marklin