3 import java.security.InvalidParameterException;
5 public class LifxHeader {
9 private boolean tagged;
10 private boolean addressable;
14 //Frame Adress Variables
15 private byte[] macAddress = new byte[8];
16 private boolean ack_required;
17 private boolean res_required;
24 // needed values as per spec
30 public void setSize(int _size) {
32 throw new InvalidParameterException("Header: size cannot be less than 0");
33 } else if (_size > 65535) {
34 throw new InvalidParameterException("Header: size to large");
39 public void setOrigin(int _origin) {
41 throw new InvalidParameterException("Header: origin cannot be less than 0");
42 } else if (_origin > 3) {
43 throw new InvalidParameterException("Header: origin to large");
49 public void setTagged(boolean _tagged) {
53 public void setAddressable(boolean _addressable) {
54 addressable = _addressable;
57 public void setProtocol(int _protocol) {
59 throw new InvalidParameterException("Header: protocol cannot be less than 0");
60 } else if (_protocol > 4095) {
61 throw new InvalidParameterException("Header: protocol to large");
67 public void setSource(long _source) {
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");
76 public void setSequence(int _sequence) {
78 throw new InvalidParameterException("Header: sequence cannot be less than 0");
79 } else if (_sequence > 255) {
80 throw new InvalidParameterException("Header: sequence to large");
85 public void setType(int _type) {
87 throw new InvalidParameterException("Header: type cannot be less than 0");
88 } else if (_type > 65535) {
89 throw new InvalidParameterException("Header: type to large");
94 public void setAck_required(boolean _ack_required) {
95 ack_required = _ack_required;
98 public void setRes_required(boolean _res_required) {
99 res_required = _res_required;
102 public void setMacAddress(byte[] _macAddress) {
103 macAddress = _macAddress;
106 public int getSize() {
110 public int getOrigin() {
114 public boolean getTagged() {
118 public boolean getAddressable() {
122 public int getProtocol() {
126 public long getSource() {
130 public int getSequence() {
134 public int getType() {
138 public byte[] getMacAddress() {
142 public boolean getAck_required() {
146 public boolean getRes_required() {
150 public byte[] getHeaderBytes() {
151 byte[] headerBytes = new byte[36];
152 headerBytes[0] = (byte)(size & 0xFF);
153 headerBytes[1] = (byte)((size >> 8) & 0xFF);
156 headerBytes[2] = (byte)(protocol & 0xFF);
157 headerBytes[3] = (byte)((protocol >> 8) & 0x0F);
159 headerBytes[3] |= (byte)((origin & 0x03) << 6);
162 headerBytes[3] |= (1 << 5);
166 headerBytes[3] |= (1 << 4);
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);
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];
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;
194 headerBytes[22] = (1 << 1);
198 headerBytes[22] |= (1);
201 headerBytes[23] = (byte)(sequence & 0xFF);
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;
213 headerBytes[32] = (byte)((type >> 0) & 0xFF);
214 headerBytes[33] = (byte)((type >> 8) & 0xFF);
216 // Reserved and set to 0
217 //headerBytes[34] = 0;
218 //headerBytes[35] = 0;
223 public void setFromBytes(byte[] dataBytes) {
224 if (dataBytes.length != 36) {
225 throw new InvalidParameterException("Header: invalid number of bytes");
228 size = dataBytes[0] & 0xFF;
229 size |= ((dataBytes[1] & 0xFF) << 8);
232 origin = (dataBytes[3] >> 6) & 0x03;
233 tagged = ((dataBytes[3] >> 5) & 0x01) == 1;
234 addressable = ((dataBytes[3] >> 4) & 0x01) == 1;
237 protocol = (dataBytes[3] & 0x0F) << 8;
238 protocol |= dataBytes[2];
241 source = (dataBytes[7] & 0xFFl) << 24;
242 source |= ((dataBytes[6] & 0xFFl) << 16);
243 source |= ((dataBytes[5] & 0xFFl) << 8);
244 source |= ((dataBytes[4] & 0xFFl));
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];
255 ack_required = (dataBytes[22] & 0x02) == 0x02;
256 res_required = (dataBytes[22] & 0x01) == 0x01;
258 sequence = (dataBytes[23] & 0xFF);
260 type = ((dataBytes[33] & 0xFF) << 8);
261 type |= (dataBytes[32] & 0xFF);