+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#include <cstring>
+#include <msp/strings/formatter.h>
#include "command.h"
using namespace std;
+using namespace Msp;
namespace Marklin {
-Command::Command(const string &c):
+Command::Command(Cmd c, const unsigned char *d, unsigned l):
cmd(c),
+ len(1),
sent(false)
-{ }
+{
+ data[0]=cmd;
+ if(d)
+ {
+ memcpy(data+1, d, min(l, 127U));
+ len+=min(l, 127U);
+ }
+}
-void Command::set_sent(bool s)
+void Command::send(int fd)
{
- sent=s;
+ write(fd, data, len);
+ sent=true;
+}
+
+ostream &operator<<(ostream &out, const Command &cmd)
+{
+ out<<cmd.cmd;
+ for(unsigned i=1; i<cmd.len; ++i)
+ out<<format(" %02X", static_cast<int>(cmd.data[i]));
+
+ return out;
}
} // namespace Marklin