Adding more directory structure to generated files by compiler, also script to copy...
[iot2.git] / others / tutorial / benchmarks / drivers / SmartLightBulb / LifxHeader.java
1 package iotcode.LifxLightBulb;
2
3 import java.security.InvalidParameterException;
4
5 public class LifxHeader {
6         // Frame Variables
7         private int size;
8         private int origin;
9         private boolean tagged;
10         private boolean addressable;
11         private int protocol;
12         private long source;
13
14         //Frame Adress Variables
15         private byte[] macAddress = new byte[8];
16         private boolean ack_required;
17         private boolean res_required;
18         private int sequence;
19
20         //Protocol Header
21         private int type;
22
23         public LifxHeader() {
24                 // needed values as per spec
25                 origin = 0;
26                 addressable = true;
27                 protocol = 1024;
28         }
29
30         public void setSize(int _size) {
31                 if (_size < 0) {
32                         throw new InvalidParameterException("Header: size cannot be less than 0");
33                 } else if (_size > 65535) {
34                         throw new InvalidParameterException("Header: size to large");
35                 }
36                 size = _size;
37         }
38
39         public void setOrigin(int _origin) {
40                 if (_origin < 0) {
41                         throw new InvalidParameterException("Header: origin cannot be less than 0");
42                 } else if (_origin > 3) {
43                         throw new InvalidParameterException("Header: origin to large");
44                 }
45
46                 origin = _origin;
47         }
48
49         public void setTagged(boolean _tagged) {
50                 tagged = _tagged;
51         }
52
53         public void setAddressable(boolean _addressable) {
54                 addressable = _addressable;
55         }
56
57         public void setProtocol(int _protocol) {
58                 if (_protocol < 0) {
59                         throw new InvalidParameterException("Header: protocol cannot be less than 0");
60                 } else if (_protocol > 4095) {
61                         throw new InvalidParameterException("Header: protocol to large");
62                 }
63
64                 protocol = _protocol;
65         }
66
67         public void setSource(long _source) {
68                 if (_source < 0) {
69                         throw new InvalidParameterException("Header: source cannot be less than 0");
70                 } else if (_source > (long)4294967295l) {
71                         throw new InvalidParameterException("Header: source to large");
72                 }
73                 source = _source;
74         }
75
76         public void setSequence(int _sequence) {
77                 if (_sequence < 0) {
78                         throw new InvalidParameterException("Header: sequence cannot be less than 0");
79                 } else if (_sequence > 255) {
80                         throw new InvalidParameterException("Header: sequence to large");
81                 }
82                 sequence = _sequence;
83         }
84
85         public void setType(int _type) {
86                 if (_type < 0) {
87                         throw new InvalidParameterException("Header: type cannot be less than 0");
88                 } else if (_type > 65535) {
89                         throw new InvalidParameterException("Header: type to large");
90                 }
91                 type = _type;
92         }
93
94         public void setAck_required(boolean _ack_required) {
95                 ack_required = _ack_required;
96         }
97
98         public void setRes_required(boolean _res_required) {
99                 res_required = _res_required;
100         }
101
102         public void setMacAddress(byte[] _macAddress) {
103                 macAddress = _macAddress;
104         }
105
106         public int getSize() {
107                 return size;
108         }
109
110         public int getOrigin() {
111                 return origin;
112         }
113
114         public boolean getTagged() {
115                 return tagged;
116         }
117
118         public boolean getAddressable() {
119                 return addressable;
120         }
121
122         public int getProtocol() {
123                 return protocol;
124         }
125
126         public long getSource() {
127                 return source;
128         }
129
130         public int getSequence() {
131                 return sequence;
132         }
133
134         public int getType() {
135                 return type;
136         }
137
138         public byte[] getMacAddress() {
139                 return macAddress;
140         }
141
142         public boolean getAck_required() {
143                 return ack_required;
144         }
145
146         public boolean getRes_required() {
147                 return res_required;
148         }
149
150         public byte[] getHeaderBytes() {
151                 byte[] headerBytes = new byte[36];
152                 headerBytes[0] = (byte)(size & 0xFF);
153                 headerBytes[1] = (byte)((size >> 8) & 0xFF);
154
155
156                 headerBytes[2] = (byte)(protocol & 0xFF);
157                 headerBytes[3] = (byte)((protocol >> 8) & 0x0F);
158
159                 headerBytes[3] |= (byte)((origin & 0x03) << 6);
160
161                 if (tagged) {
162                         headerBytes[3] |= (1 << 5);
163                 }
164
165                 if (addressable) {
166                         headerBytes[3] |= (1 << 4);
167                 }
168
169                 headerBytes[4] = (byte)((source >> 0) & 0xFF);
170                 headerBytes[5] = (byte)((source >> 8) & 0xFF);
171                 headerBytes[6] = (byte)((source >> 16) & 0xFF);
172                 headerBytes[7] = (byte)((source >> 24) & 0xFF);
173
174
175                 // fix in a bit
176                 headerBytes[8] = macAddress[0];
177                 headerBytes[9] = macAddress[1];
178                 headerBytes[10] = macAddress[2];
179                 headerBytes[11] = macAddress[3];
180                 headerBytes[12] = macAddress[4];
181                 headerBytes[13] = macAddress[5];
182                 headerBytes[14] = macAddress[6];
183                 headerBytes[15] = macAddress[7];
184
185                 // Reserved and set to 0
186                 // headerBytes[16] = 0;
187                 // headerBytes[17] = 0;
188                 // headerBytes[18] = 0;
189                 // headerBytes[19] = 0;
190                 // headerBytes[20] = 0;
191                 // headerBytes[21] = 0;
192
193                 if (ack_required) {
194                         headerBytes[22] = (1 << 1);
195                 }
196
197                 if (res_required) {
198                         headerBytes[22] |= (1);
199                 }
200
201                 headerBytes[23] = (byte)(sequence & 0xFF);
202
203                 // Reserved and set to 0
204                 //headerBytes[24] = 0;
205                 //headerBytes[25] = 0;
206                 //headerBytes[26] = 0;
207                 //headerBytes[27] = 0;
208                 //headerBytes[28] = 0;
209                 //headerBytes[29] = 0;
210                 //headerBytes[30] = 0;
211                 //headerBytes[31] = 0;
212
213                 headerBytes[32] = (byte)((type >> 0) & 0xFF);
214                 headerBytes[33] = (byte)((type >> 8) & 0xFF);
215
216                 // Reserved and set to 0
217                 //headerBytes[34] = 0;
218                 //headerBytes[35] = 0;
219
220                 return headerBytes;
221         }
222
223         public void setFromBytes(byte[] dataBytes) {
224                 if (dataBytes.length != 36) {
225                         throw new InvalidParameterException("Header: invalid number of bytes");
226                 }
227
228                 size = dataBytes[0] & 0xFF;
229                 size |= ((dataBytes[1] & 0xFF) << 8);
230                 size &= 0xFFFF;
231
232                 origin = (dataBytes[3] >> 6) & 0x03;
233                 tagged = ((dataBytes[3] >> 5) & 0x01) == 1;
234                 addressable = ((dataBytes[3] >> 4) & 0x01) == 1;
235
236
237                 protocol = (dataBytes[3] & 0x0F) << 8;
238                 protocol |= dataBytes[2];
239                 protocol &= 0x0FFF;
240
241                 source = (dataBytes[7] & 0xFFl) << 24;
242                 source |= ((dataBytes[6] & 0xFFl) << 16);
243                 source |= ((dataBytes[5] & 0xFFl) << 8);
244                 source |= ((dataBytes[4] & 0xFFl));
245
246                 macAddress[0] = dataBytes[8];
247                 macAddress[1] = dataBytes[9];
248                 macAddress[2] = dataBytes[10];
249                 macAddress[3] = dataBytes[11];
250                 macAddress[4] = dataBytes[12];
251                 macAddress[5] = dataBytes[13];
252                 macAddress[6] = dataBytes[14];
253                 macAddress[7] = dataBytes[15];
254
255                 ack_required = (dataBytes[22] & 0x02) == 0x02;
256                 res_required = (dataBytes[22] & 0x01) == 0x01;
257
258                 sequence = (dataBytes[23] & 0xFF);
259
260                 type = ((dataBytes[33] & 0xFF) << 8);
261                 type |= (dataBytes[32] & 0xFF);
262         }
263 }