-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageBuilder.cpp
162 lines (135 loc) · 4.15 KB
/
MessageBuilder.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "MessageBuilder.h"
MessageBuilder::MessageBuilder(void):
m_flags('-'),m_message_type('-'),m_message_sub_type('-'){}
MessageBuilder::~MessageBuilder(void){
}
void MessageBuilder::setFlags(char codes){
m_flags = intToBase36(codes).charAt(0);
}
void MessageBuilder::setAddressFrom(String address){
m_address_from = address;
}
void MessageBuilder::setAddressTo(String address){
m_address_to = address;
}
void MessageBuilder::setMessageType(char type){
m_message_type = type;
}
void MessageBuilder::setMessageSubType(char sub_type){
m_message_sub_type = sub_type;
}
void MessageBuilder::setData(String data){
m_data = data;
}
void MessageBuilder::setFlags(String& message, char codes){
char flags = intToBase36(codes).charAt(0);
message.setCharAt(1,flags);
}
void MessageBuilder::invertAddress(String& message){
String address_from = decodeAddressFrom(message);
String address_to = decodeAddressTo(message);
setAddressFrom(message,address_to);
setAddressTo(message,address_from);
}
void MessageBuilder::setAddressFrom(String& message, String address){
message.setCharAt(2,address.charAt(0));
message.setCharAt(3,address.charAt(1));
message.setCharAt(4,address.charAt(2));
message.setCharAt(5,address.charAt(3));
}
void MessageBuilder::setAddressTo(String& message, String address){
message.setCharAt(6,address.charAt(0));
message.setCharAt(7,address.charAt(1));
message.setCharAt(8,address.charAt(2));
message.setCharAt(9,address.charAt(3));
}
String MessageBuilder::getMessage(){
String output = String('>') + m_flags +
m_address_from + m_address_to +
m_message_type + m_message_sub_type +
m_data + "00;";
setChecksum(output);
return output;
}
String MessageBuilder::decodeAddressTo(String message){
return message.substring(6,10);
}
String MessageBuilder::decodeAddressFrom(String message){
return message.substring(2,6);
}
void MessageBuilder::decodeMessageFields(String message,
char& flags, String& address_from, String& address_to,
char& type,char& sub_type,String& data){
flags = decodeFlags(message);
address_from =decodeAddressFrom(message);
address_to =decodeAddressTo(message);
type = decodeType(message);
sub_type = decodeSubType(message);
data = decodeData(message);
}
String MessageBuilder::decodeData(String message){
return message.substring(12,message.length() - 3);
}
char MessageBuilder::decodeType(String message){
return message.charAt(10);
}
char MessageBuilder::decodeSubType(String message){
return message.charAt(11);
}
void MessageBuilder::setChecksum(String& message){
String checksum = computeChecksum(message);
message.setCharAt(message.length() - 3 , checksum.charAt(checksum.length() - 2));
message.setCharAt(message.length() - 2 , checksum.charAt(checksum.length() - 1));
}
char MessageBuilder::decodeFlags(String message){
return base36ToInt(String(message.charAt(1)));
}
String MessageBuilder::intToBase36(int i) {
String output("");
if(i == 0) {
return "0";
}
while (i >= 1) {
int mod = i % 36;
if (mod > 9) {
//output += (char) ((mod - 10) + 'a');
output = String((char) ((mod - 10) + 'a')) + output;
} else {
//output += (char) (mod + '0');
output = String((char) (mod + '0')) + output;
}
i = i / 36;
}
return output;
}
int MessageBuilder::base36ToInt(String str) {
int r = 0;
int power = 1;
for (int i = str.length() - 1; i >= 0; i--) {
char c = str.charAt(i);
if (c >= '0' && c <= '9') {
r += (c - '0') * power;
} else {
r += ((c - 'a') + 10) * power;
}
power *= 36;
}
return r;
}
String MessageBuilder::computeChecksum(String message) {
int sum = 0;
for (int i = 1; i < message.length() - 3; i++) {
sum += message.charAt(i);
}
return intToBase36(sum);
}
char MessageBuilder::validate(String message){
if (message.length() < 15)//fields missing
return 2;
String chechsum = computeChecksum(message);
if (chechsum.charAt(chechsum.length() - 2) != message.charAt(message.length() - 3))
return 1;
if (chechsum.charAt(chechsum.length() - 1) != message.charAt(message.length() - 2))
return 1;
return 0;
}