dol: initial dol commit
[jump.git] / dol / src / dol / visitor / hdsd / scd / scd_command.cpp
1 #include "scd_command.h"
2
3 #include "cstring"
4 #include "arpa/inet.h"
5
6
7 scd_command::scd_command(): _type(0), _subtype(0), _msglen(0)
8 {
9     _msg = NULL;
10 }
11
12
13 scd_command::scd_command(uint16_t type, uint16_t subtype):
14     _type(type), _subtype(subtype), _msglen(0)
15 {
16     _msg = NULL;
17 }
18
19
20 scd_command::scd_command(uint16_t type, uint16_t subtype, 
21         const std::string& msg):
22     _type(type), _subtype(subtype)
23 {
24     if (msg.length() > 0)
25     {
26         _msglen = msg.length()+1;
27         _msg = new char[_msglen];
28         strncpy(_msg, msg.c_str(), _msglen);
29         _msg[_msglen-1] = 0;
30     }
31     else
32     {
33         _msglen = 0;
34         _msg = NULL;
35     }
36 }
37
38
39 scd_command::scd_command(uint16_t type, uint16_t subtype,
40         const sc_core::sc_time& time) : _type(type), _subtype(subtype)
41 {
42     uint64_t value;
43     uint32_t hi, lo;
44
45     _msglen = 8;
46     _msg = new char[_msglen];
47
48     // obtain high and low words
49     value = time.value();
50     lo = value & 0xFFFFFFFF;
51     value >>= 32;
52     hi = value & 0xFFFFFFFF;
53
54     // do network conversion
55     hi = htonl(hi);
56     lo = htonl(lo);
57
58     // store to buffer
59     memcpy(_msg, &hi, 4);
60     memcpy(_msg + 4, &lo, 4);
61 }
62
63
64
65 scd_command::~scd_command()
66 {
67     if (_msg != NULL)
68         delete _msg;
69 }
70
71
72 uint16_t scd_command::get_type() const { return _type; }
73
74
75 uint16_t scd_command::get_subtype() const { return _subtype; }
76
77
78 std::string scd_command::get_string() const
79 {
80     if (_msglen <= 1)
81         return std::string();
82     else
83         return std::string(_msg, _msglen-1);
84 }
85
86
87 sc_core::sc_time scd_command::get_time() const
88 {
89     if (_msglen != 8)
90         return sc_core::SC_ZERO_TIME;
91
92     uint64_t value;
93     uint32_t hi, lo;
94     
95     // get value from buffer
96     memcpy(&hi, _msg, 4);
97     memcpy(&lo, _msg+4, 4);
98
99     // do network conversion
100     value = ntohl(hi);
101     value <<= 32;
102     value |= ntohl(lo);
103
104     return sc_core::sc_time(value, false);
105 }