-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.h
113 lines (96 loc) · 2.53 KB
/
message.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#ifndef MESSAGE_H
#define MESSAGE_H
#include <stdint.h>
#include <bits/stdint-uintn.h>
#include <string>
#include <map>
#include "AddressMap.h"
#include "hexdump.hpp"
typedef enum {
MT_RINF = 0x1,
MT_SINF = 0x2,
MT_SET = 0x3,
MT_ACK = 0x4,
MT_NACK = 0x5,
MT_QUERY = 0x6,
MT_RESPONSE = 0x7,
MT_ERROR = 0x8
} MType;
static const std::map<int,std::string> messagetypestringtable = {
{ MT_RINF, "Request info" },
{ MT_SINF, "Send info" },
{ MT_SET, "Set" },
{ MT_ACK, "Ack" },
{ MT_NACK, "Nack" },
{ MT_QUERY, "Query" },
{ MT_RESPONSE, "Response" },
{ MT_ERROR, "Error" }
};
class Message {
std::string message;
public:
Message(std::string message) : message(message) {}
// DC 8A 00 0B 06 3D 0D 05 19 4F 8C
uint32_t address(void ) const {
uint32_t addr=0;
for(int i=0;i<=3;i++) {
addr<<=8;
addr|=message.at(i+5);
}
// Query Swaps the first 2 bytes
if (needs_swap()) {
addr=((addr&0xff000000)>>8)|((addr&0x00ff0000)<<8)|(addr&0x0000ffff);
}
return addr;
}
bool needs_swap(void ) const {
if (messagetype() == MT_QUERY)
return true;
if (messagetype() == MT_SET)
return true;
return false;
}
unsigned int messagetype(void ) const {
return (uint8_t) message.at(4);
}
std::string messagetypestring(void ) const {
try {
return messagetypestringtable.at(messagetype());
} catch(const std::out_of_range& e) {
return "unknown";
}
}
std::string value(void ) const {
std::string value=message.substr(9,message.size()-11);
return value;
}
bool hasvalue(void ) const {
if (messagetype() == MT_RESPONSE
|| messagetype() == MT_SINF)
return true;
return false;
}
unsigned int to(void ) const {
return (uint8_t) message.at(2);
}
unsigned int from(void ) const {
return (uint8_t) message.at(1);
}
friend std::ostream& operator<<(std::ostream& os, const Message& m) {
Address *aobj=addressmap.find(m.address());
os << "From: " << std::hex << m.from() << std::endl;
os << "To: " << std::hex << m.to() << std::endl;
os << "Type: " << std::hex << m.messagetypestring() << "(" << m.messagetype() << ")" << std::endl;
if (aobj == nullptr) {
os << "Address: " << "Unknown" << "(" << std::hex << m.address() << ")" << std::endl;
} else {
os << "Address: " << aobj->name << "(" << std::hex << m.address() << ")" << std::endl;
if (m.hasvalue()) {
os << "Value: " << aobj->vtype->decode(m.value()) << std::endl;
}
}
hex::dump(m.message.c_str(), m.message.size(), os);
return os;
}
};
#endif