Moving RPi folder.
[iotcloud.git] / version2 / src / others / RPi / LifxHeader.h
1 #ifndef _LIFXHEADER_H__
2 #define _LIFXHEADER_H__
3 #include <iostream>
4
5 class LifxHeader {
6
7         private:
8                 // Frame variables
9                 int size;
10                 int origin;
11                 bool tagged;
12                 bool addressable;
13                 int protocol;
14                 int64_t source;
15
16                 // Frame address variables
17                 char macAddress[8];
18                 bool ack_required;
19                 bool res_required;
20                 int sequence;
21
22                 // Protocol header
23                 int type;
24
25         public:
26
27                 LifxHeader() {
28                         origin = 0;
29                         addressable = true;
30                         protocol =1024;
31                 }
32
33
34                 ~LifxHeader() {
35                 }
36
37
38                 void setSize(int _size) {
39                         if (_size < 0) {
40                                 cerr << "Header: size cannot be less than 0" << endl;
41                                 exit(1);
42                         } else if (_size > 65535) {
43                                 cerr << "Header: size too large" << endl;
44                                 exit(1);
45                         }
46                         size = _size;
47                 }
48
49
50                 void setOrigin(int _origin) {
51                         if (_origin < 0) {
52                                 cerr << "Header: origin cannot be less than 0" << endl;
53                                 exit(1);
54                         } else if (_origin > 3) {
55                                 cerr << "Header: origin too large" << endl;
56                                 exit(1);
57                         }
58                         origin = _origin;
59                 }
60
61
62                 void setTagged(bool _tagged) {
63                         tagged = _tagged;
64                 }
65
66
67                 void setAddressable(bool _addressable) {
68                         addressable = _addressable;
69                 }
70
71
72                 void setProtocol(int _protocol) {
73                         if (_protocol < 0) {
74                                 cerr << "Header: protocol cannot be less than 0" << endl;
75                                 exit(1);
76                         } else if (_protocol > 4095) {
77                                 cerr << "Header: protocol too large" << endl;
78                                 exit(1);
79                         }
80                         protocol = _protocol;
81                 }
82
83
84                 void setSource(int64_t _source) {
85                         if (_source < 0) {
86                                 cerr << "Header: source cannot be less than 0" << endl;
87                                 exit(1);
88                         } else if (_source > 4294967295) {
89                                 cerr << "Header: source too large" << endl;
90                                 exit(1);
91                         }
92                         source = _source;
93                 }
94
95
96                 void setSequence(int _sequence) {
97                         if (_sequence < 0) {
98                                 cerr << "Header: sequence cannot be less than 0" << endl;
99                                 exit(1);
100                         } else if (_sequence > 255) {
101                                 cerr << "Header: sequence too large" << endl;
102                                 exit(1);
103                         }
104                         sequence = _sequence;
105                 }
106
107
108                 void setType(int _type) {
109                         if (_type < 0) {
110                                 cerr << "Header: type cannot be less than 0" << endl;
111                                 exit(1);
112                         } else if (_type > 65535) {
113                                 cerr << "Header: type too large" << endl;
114                                 exit(1);
115                         }
116                         type = _type;
117                 }
118
119
120                 void setAck_required(bool _ack_required) {
121                         ack_required = _ack_required;
122                 }
123
124
125                 void setRes_required(bool _res_required) {
126                         res_required = _res_required;
127                 }
128
129
130                 void setMacAddress(char _macAddress[8]) {
131                         strcpy(macAddress, _macAddress);
132                 }
133
134
135                 int getSize() {
136                         return size;
137                 }
138
139
140                 int getOrigin() {
141                         return origin;
142                 }
143
144
145                 bool getTagged() {
146                         return tagged;
147                 }
148
149
150                 bool getAddressable() {
151                         return addressable;
152                 }
153
154
155                 int getProtocol() {
156                         return protocol;
157                 }
158
159
160                 int64_t getSource() {
161                         return source;
162                 }
163
164
165                 int64_t getSequence() {
166                         return sequence;
167                 }
168
169
170                 int getType() {
171                         return type;
172                 }
173
174
175                 char* getHeaderBytes(char headerBytes[36]) {
176
177                         //char headerBytes[36];
178                         headerBytes[0] = (char)(size & 0xFF);
179                         headerBytes[1] = (char)((size >> 8) & 0xFF);
180
181
182                         headerBytes[2] = (char)(protocol & 0xFF);
183                         headerBytes[3] = (char)((protocol >> 8) & 0x0F);
184
185                         headerBytes[3] |= (char)((origin & 0x03) << 6);
186
187                         if (tagged) {
188                                 headerBytes[3] |= (1 << 5);
189                         }
190
191                         if (addressable) {
192                                 headerBytes[3] |= (1 << 4);
193                         }
194
195                         headerBytes[4] = (char)((source >> 0) & 0xFF);
196                         headerBytes[5] = (char)((source >> 8) & 0xFF);
197                         headerBytes[6] = (char)((source >> 16) & 0xFF);
198                         headerBytes[7] = (char)((source >> 24) & 0xFF);
199
200                         // fix in a bit
201                         headerBytes[8] = macAddress[0];
202                         headerBytes[9] = macAddress[1];
203                         headerBytes[10] = macAddress[2];
204                         headerBytes[11] = macAddress[3];
205                         headerBytes[12] = macAddress[4];
206                         headerBytes[13] = macAddress[5];
207                         headerBytes[14] = macAddress[6];
208                         headerBytes[15] = macAddress[7];
209
210                         // Reserved and set to 0
211                         headerBytes[16] = 0;
212                         headerBytes[17] = 0;
213                         headerBytes[18] = 0;
214                         headerBytes[19] = 0;
215                         headerBytes[20] = 0;
216                         headerBytes[21] = 0;
217
218                         if (ack_required) {
219                                 headerBytes[22] = (1 << 1);
220                         }
221
222                         if (res_required) {
223                                 headerBytes[22] |= (1);
224                         }
225
226                         headerBytes[23] = (char)(sequence & 0xFF);
227
228                         // Reserved and set to 0
229                         headerBytes[24] = 0;
230                         headerBytes[25] = 0;
231                         headerBytes[26] = 0;
232                         headerBytes[27] = 0;
233                         headerBytes[28] = 0;
234                         headerBytes[29] = 0;
235                         headerBytes[30] = 0;
236                         headerBytes[31] = 0;
237
238                         headerBytes[32] = (char)((type >> 0) & 0xFF);
239                         headerBytes[33] = (char)((type >> 8) & 0xFF);
240
241                         // Reserved and set to 0
242                         headerBytes[34] = 0;
243                         headerBytes[35] = 0;
244
245                         return headerBytes;
246                 }
247
248
249         void setFromBytes(char dataBytes[36]) {
250
251                 size = dataBytes[0] & 0xFF;
252                 size |= ((dataBytes[1] & 0xFF) << 8);
253                 size &= 0xFFFF;
254
255                 origin = (dataBytes[3] >> 6) & 0x03;
256                 tagged = ((dataBytes[3] >> 5) & 0x01) == 1;
257                 addressable = ((dataBytes[3] >> 4) & 0x01) == 1;
258
259
260                 protocol = (dataBytes[3] & 0x0F) << 8;
261                 protocol |= dataBytes[2];
262                 protocol &= 0x0FFF;
263
264                 source = (dataBytes[7] & 0xFFl) << 24;
265                 source |= ((dataBytes[6] & 0xFFl) << 16);
266                 source |= ((dataBytes[5] & 0xFFl) << 8);
267                 source |= ((dataBytes[4] & 0xFFl));
268
269                 macAddress[0] = dataBytes[8];
270                 macAddress[1] = dataBytes[9];
271                 macAddress[2] = dataBytes[10];
272                 macAddress[3] = dataBytes[11];
273                 macAddress[4] = dataBytes[12];
274                 macAddress[5] = dataBytes[13];
275                 macAddress[6] = dataBytes[14];
276                 macAddress[7] = dataBytes[15];
277
278                 ack_required = (dataBytes[22] & 0x02) == 0x02;
279                 res_required = (dataBytes[22] & 0x01) == 0x01;
280
281                 sequence = (dataBytes[23] & 0xFF);
282
283                 type = ((dataBytes[33] & 0xFF) << 8);
284                 type |= (dataBytes[32] & 0xFF); 
285         }
286 };
287 #endif