+#include "scd_command.h"
+
+#include "cstring"
+#include "arpa/inet.h"
+
+
+scd_command::scd_command(): _type(0), _subtype(0), _msglen(0)
+{
+ _msg = NULL;
+}
+
+
+scd_command::scd_command(uint16_t type, uint16_t subtype):
+ _type(type), _subtype(subtype), _msglen(0)
+{
+ _msg = NULL;
+}
+
+
+scd_command::scd_command(uint16_t type, uint16_t subtype,
+ const std::string& msg):
+ _type(type), _subtype(subtype)
+{
+ if (msg.length() > 0)
+ {
+ _msglen = msg.length()+1;
+ _msg = new char[_msglen];
+ strncpy(_msg, msg.c_str(), _msglen);
+ _msg[_msglen-1] = 0;
+ }
+ else
+ {
+ _msglen = 0;
+ _msg = NULL;
+ }
+}
+
+
+scd_command::scd_command(uint16_t type, uint16_t subtype,
+ const sc_core::sc_time& time) : _type(type), _subtype(subtype)
+{
+ uint64_t value;
+ uint32_t hi, lo;
+
+ _msglen = 8;
+ _msg = new char[_msglen];
+
+ // obtain high and low words
+ value = time.value();
+ lo = value & 0xFFFFFFFF;
+ value >>= 32;
+ hi = value & 0xFFFFFFFF;
+
+ // do network conversion
+ hi = htonl(hi);
+ lo = htonl(lo);
+
+ // store to buffer
+ memcpy(_msg, &hi, 4);
+ memcpy(_msg + 4, &lo, 4);
+}
+
+
+
+scd_command::~scd_command()
+{
+ if (_msg != NULL)
+ delete _msg;
+}
+
+
+uint16_t scd_command::get_type() const { return _type; }
+
+
+uint16_t scd_command::get_subtype() const { return _subtype; }
+
+
+std::string scd_command::get_string() const
+{
+ if (_msglen <= 1)
+ return std::string();
+ else
+ return std::string(_msg, _msglen-1);
+}
+
+
+sc_core::sc_time scd_command::get_time() const
+{
+ if (_msglen != 8)
+ return sc_core::SC_ZERO_TIME;
+
+ uint64_t value;
+ uint32_t hi, lo;
+
+ // get value from buffer
+ memcpy(&hi, _msg, 4);
+ memcpy(&lo, _msg+4, 4);
+
+ // do network conversion
+ value = ntohl(hi);
+ value <<= 32;
+ value |= ntohl(lo);
+
+ return sc_core::sc_time(value, false);
+}