-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataParser.h
130 lines (120 loc) · 3.35 KB
/
DataParser.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include<Arduino.h>
#include "GlobalOptions.h"
#define MAGIC_CHAR '$'
#define MAX_PASSWORD_LEN 10
#define MAX_PHONE_LEN 15
/*
* Workflow
* --------
* 1) Text message to required data
*
* Give DataParser a chunk of text
* it will return an instance of Request_Message
* whose message_type field indicates
* whether the message failed to parse
* or if it succeed, what type of message it is
* See REQ_XXXX definitions for message types
*
* 2) Data to message
*
* Currently the parser can accept an instance of
* Status_Message_To_Phone and populate a given
* char array[160];
*/
/*
* These constants indicates what
* type of request an instance of
* Request_Message have
* See also private REQ_MSG_TYPE
*/
#define REQ_INVALID 255
#define REQ_STATUS 0
#define REQ_UPDATE_STATUS 1
#define REQ_CHANGE_PASSWORD 2
#define REQ_CHANGE_PHONE 3
/*
* these are tokens that are to
* be sent by user inorder to let
* the mcu indentify what type of request is this
* it is translated to corresponding REQ_XXX codes
* by the parser
*/
#define REQ_STATUS_TOKEN "STATUS"
#define REQ_UPDATE_STATUS_TOKEN "UPDATE"
#define REQ_CHANGE_PASSWORD_TOKEN "CPW"
#define REQ_CHANGE_PHONE_TOKEN "CPHONE"
#define CODE_STATUS_UPDATED 200
#define CODE_PASSWORD_CHANGED 201
#define CODE_PHONE_CHANGED 202
class DataParser {
public:
/*
* After parsing an incoming
* message, DataParser will populate
* given instance of this struct
*/
struct Status_Message_To_Phone {
byte device_on_off; //LSB -> device 1
byte temperature;
byte waterlevel_percent;
};
/*
* All the request messages will contain
* an instance of this struct
* The external authenticator is supposed
* to check its contents and discard rest
* of the message if failed
*/
struct Auth_Data {
char password[MAX_PASSWORD_LEN];
};
/*
* Holds the data for updating the status
* Currently,new device status only
*/
struct Status_Update_Request {
byte device_on_off; //first device is the right most
byte device_on_off_mask;
};
/*
* Change password request message
* contains new password
*/
struct Change_Password_Request {
char new_password[MAX_PASSWORD_LEN];
};
/*
* Change phoneno request message
* contains new phoneno
*/
struct Change_Phone_Request {
char new_phone[MAX_PHONE_LEN];
};
/*
* A struct which contains
* auth data
* a byte indicating what type of request do we have
* and a union of all the request messages
* the coder is supposed to check the byte first and read
* appropriate union-variable
*/
struct Request_Message {
byte message_type;
Auth_Data auth_data;
union {
Status_Update_Request status_update_request;
Change_Password_Request change_password_request;
Change_Phone_Request change_phone_request;
};
};
/*
* will try to parse the request message
* and populate the request_message variable
* passed as reference appropriately
* if parsing failed, the request_message's
* message_type will be set to invalid(see #define section)
*/
void parseRequest(char message[160],Request_Message& request_message);
void parseStatusReply(char message[160],Status_Message_To_Phone& status);
private:
};