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::send(int fd)
+{
+ write(fd, data, len);
+ sent=true;
+}
-void Command::set_sent(bool s)
+ostream &operator<<(ostream &out, const Command &cmd)
{
- sent=s;
+ out<<cmd.cmd;
+ for(unsigned i=1; i<cmd.len; ++i)
+ out<<format(" %02X", static_cast<int>(cmd.data[i]));
+
+ return out;
}
} // namespace Marklin