-
Notifications
You must be signed in to change notification settings - Fork 0
/
Message.h
92 lines (79 loc) · 1.79 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
#pragma once
#include "MessageBuilder.h"
class Message{
private:
String m_message;
char m_flags;
long m_time_start;//millis
long m_timeout;//millis
char m_resend_attempts;
bool m_null;
public:
bool isTimeout(long current_time){
long delta = (current_time - m_time_start);
if (delta < 0){
delta += 0xffff;
}
if (delta >= m_timeout){
//Serial.println("timeout");
return true;
}
return false;
}
char getResendAttempts(){
return m_resend_attempts;
}
void attemptResend(long time_start){
m_time_start = time_start;
++m_resend_attempts;
}
void setMessage(String message, long time_start, long timeout){
m_message = message;
m_time_start = time_start;
m_timeout = timeout;
m_flags = MessageBuilder::decodeFlags(message);
m_null = false;
}
bool validate(String m){
char flags;
String address_from;
String address_to;
char type;
char sub_type;
String data;
MessageBuilder::decodeMessageFields(m,flags,address_from,address_to,type,sub_type,data);
String m_address_from;
String m_address_to;
char m_type;
char m_sub_type;
String m_data;
MessageBuilder::decodeMessageFields(m_message,flags,m_address_from,m_address_to,m_type,m_sub_type,m_data);
if (address_from == m_address_to &&
address_to == m_address_from &&
type == m_type &&
sub_type == m_sub_type &&
data == m_data)
return true;
return false;
}
void eraseMessage(){
m_null = true;
m_resend_attempts = 0;
m_flags = 0;
}
bool isNull(){
return m_null;
}
bool hasFlag(char flag){
if (m_flags & flag == 1)
return true;
return false;
}
String getMessage(){
return m_message;
}
Message(void): m_null(true),m_flags(0),
m_resend_attempts(0){
}
~Message(void){}
};