OpenECHO
 All Classes Namespaces Files Functions Variables
EchoUtils.java
Go to the documentation of this file.
1 /*
2  * Copyright 2012 Sony Computer Science Laboratories, Inc. <info@kadecot.net>
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.sonycsl.echo;
17 
18 import java.net.Inet4Address;
19 import java.net.InetAddress;
20 import java.net.NetworkInterface;
21 import java.net.SocketException;
22 import java.nio.ByteBuffer;
23 import java.nio.ByteOrder;
24 import java.util.ArrayList;
25 import java.util.Enumeration;
26 import java.util.HashMap;
27 import java.util.List;
28 
31 
32 
33 
34 public final class EchoUtils {
35 
36 
37  public static final byte[] classToByteArray(EchoObject eoj) {
38  return new byte[]{eoj.getClassGroupCode(), eoj.getClassCode()};
39  }
40 
41  public static final byte[] instanceToByteArray(EchoObject eoj) {
42  return new byte[]{eoj.getClassGroupCode(), eoj.getClassCode(), eoj.getInstanceCode()};
43  }
44 
45  public static byte[] devicesToByteArray(DeviceObject[] devices, int index) {
46  // index=0 : 1~84
47  // index=1 : 85~168
48  // index=2 : 169~252
49  // index=3 : 253~255
50  int length = devices.length;
51  byte[] ret = null;
52  int num = 0;
53  switch(index) {
54  case 0:
55  if(length > 84) num = 84;
56  else num = length;
57  break;
58  case 1:
59  if(length < 85) return null;
60  if(length > 168) num = 84;
61  else num = length - 84;
62  break;
63  case 2:
64  if(length < 169) return null;
65  if(length > 252) num = 84;
66  else num = length - 168;
67  break;
68  case 3:
69  if(length < 253) return null;
70  if(length > 255) return null;
71  num = length - 252;
72  break;
73  default:
74  return null;
75  }
76 
77  ret = new byte[num * 3 + 1];
78  ret[0] = (byte)num;
79  for(int i = 0; i < num; i++) {
80  byte[] b = instanceToByteArray(devices[84 * index + i]);
81  ret[i*3+1] = b[0];
82  ret[i*3+2] = b[1];
83  ret[i*3+3] = b[2];
84  }
85  return ret;
86  }
87 
88  public static byte[] devicesToByteArray(DeviceObject[] devices) {
89  int length = devices.length;
90  byte[] ret = new byte[length * 3 + 1];
91  ret[0] = (byte)length;
92  for(int i = 0; i < length; i++) {
93  byte[] b = instanceToByteArray(devices[i]);
94  ret[i*3+1] = b[0];
95  ret[i*3+2] = b[1];
96  ret[i*3+3] = b[2];
97  }
98  return ret;
99  }
100 
101  public static short getEchoClassCode(byte classGroupCode, byte classCode) {
102  ByteBuffer buffer = ByteBuffer.allocate(2);
103  buffer.order(ByteOrder.BIG_ENDIAN);
104  buffer.put(classGroupCode);
105  buffer.put(classCode);
106  return buffer.getShort(0);
107 
108  }
109 
110  public static int getEchoObjectCode(byte classGroupCode, byte classCode, byte instanceCode) {
111  ByteBuffer buffer = ByteBuffer.allocate(4);
112  buffer.order(ByteOrder.BIG_ENDIAN);
113  buffer.put((byte)0);
114  buffer.put(classGroupCode);
115  buffer.put(classCode);
116  buffer.put(instanceCode);
117  return buffer.getInt(0);
118  }
119 
120  public static int getEchoObjectCode(short echoClassCode, byte instanceCode) {
121  ByteBuffer buffer = ByteBuffer.allocate(4);
122  buffer.order(ByteOrder.BIG_ENDIAN);
123  buffer.put((byte)0);
124  buffer.putShort(echoClassCode);
125  buffer.put(instanceCode);
126  return buffer.getInt(0);
127  }
128 
129  public static short getEchoClassCodeFromObjectCode(int objectCode) {
130  return (short)((objectCode >> 8) & 0xFFFF);
131  }
132  public static byte getInstanceCodeFromObjectCode(int objectCode) {
133  return (byte)(objectCode & 0xFF);
134  }
135 
136  public static InetAddress getLocalIpAddress() throws SocketException {
137  Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
138  while(en.hasMoreElements()) {
139  NetworkInterface intf = en.nextElement();
140  Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
141  while(enumIpAddr.hasMoreElements()) {
142  InetAddress inetAddress = enumIpAddr.nextElement();
143  if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
144  return inetAddress;
145  }
146  }
147  }
148  return null;
149  }
150 
151  public static NetworkInterface getNetworkInterface() throws SocketException {
152 
153  Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
154  while(en.hasMoreElements()) {
155  NetworkInterface intf = en.nextElement();
156  Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
157  while(enumIpAddr.hasMoreElements()) {
158  InetAddress inetAddress = enumIpAddr.nextElement();
159  if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
160  return intf;
161  }
162  }
163  }
164  return null;
165  }
166 
167  public static String toHexString(byte[] data) {
168  return toHexString(data," ") ;
169  }
170  public static String toHexString(byte[] data,String sep) {
171  if(data == null) return "";
172  StringBuilder sb = new StringBuilder();
173  for(byte b : data) {
174  sb.append(String.format("%02x"+sep, b));
175  }
176 
177  return new String(sb);
178  }
179 
180  public static String toHexString(byte b) {
181  return String.format("%02x", b);
182  }
183 
184  public static String toHexString(short s) {
185  return String.format("%04x", s);
186  }
187 
188  public static byte[] propertiesToPropertyMap(byte[] properties) {
189  byte[] ret;
190  int length = properties.length;
191  if(length < 16) {
192  ret = new byte[length + 1];
193  ret[0] = (byte)length;
194  for(int i = 0; i < length; i++) {
195  ret[i+1] = properties[i];
196  }
197  } else {
198  ret = new byte[17];
199  ret[0] = (byte)length;
200  //for(int i = 0; i < length; i++) {
201  // ret[i+1] = (byte)0;
202  //}
203  for(int i = 1; i < ret.length; i++) {
204  ret[i] = (byte)0;
205  }
206  for(byte p : properties) {
207  int high = (int)((p >> 4) & 0x0F);
208  if (high < 0x08) {
209  continue;
210  }
211  int low = (int)(p & 0x0F);
212  //ret[low+1] = (byte)((ret[low+1] & 0xFF) | (1 << (0x0F - high)));
213  ret[low+1] = (byte)((ret[low+1] & 0xFF) | (1 << (high - 0x08)));
214  }
215  }
216  return ret;
217  }
218 
219  public static byte[] propertyMapToProperties(byte[] map) {
220  if(map == null || map.length == 0) return new byte[]{};
221  byte[] ret = new byte[(int)(map[0] & 0xFF)];
222  if(ret.length < 16) {
223  for(int i = 0; i < ret.length; i++) {
224  ret[i] = map[i+1];
225  }
226  } else {
227  int n = 0;
228  for(int low = 0; low < 16; low++) {
229  byte tmp = map[low + 1];
230  for(int high = 0; high < 8; high++) {
231  if((tmp & 0x01) == 0x01) {
232  //ret[n] = (byte)(i | ((0x0F - j) << 4));
233  ret[n] = (byte)(low | ((high + 0x08) << 4));
234  n++;
235  }
236  tmp = (byte)(tmp >> 1);
237  }
238  }
239  }
240 
241  return ret;
242  }
243 
244  public static byte[] toByteArray(byte arg, int length) {
245  byte[] ret = new byte[length];
246  byte num = arg;
247  for(int i = length - 1; i >= 0; i--) {
248  ret[i] = (byte)(num & 0xFF);
249  num >>= 8;
250  }
251  return ret;
252  }
253 
254  public static byte[] toByteArray(byte arg) {
255  return toByteArray(arg, 1);
256  }
257 
258  public static byte[] toByteArray(short arg, int length) {
259  byte[] ret = new byte[length];
260  short num = arg;
261  for(int i = length - 1; i >= 0; i--) {
262  ret[i] = (byte)(num & 0xFF);
263  num >>= 8;
264  }
265  return ret;
266  }
267 
268  public static byte[] toByteArray(short arg) {
269  return toByteArray(arg, 2);
270  }
271 
272  public static byte[] toByteArray(int arg, int length) {
273  byte[] ret = new byte[length];
274  int num = arg;
275  for(int i = length - 1; i >= 0; i--) {
276  ret[i] = (byte)(num & 0xFF);
277  num >>= 8;
278  }
279  return ret;
280  }
281 
282  public static byte[] toByteArray(int arg) {
283  return toByteArray(arg, 4);
284  }
285 
286  public static byte[] toByteArray(long arg, int length) {
287  byte[] ret = new byte[length];
288  long num = arg;
289  for(int i = length - 1; i >= 0; i--) {
290  ret[i] = (byte)(num & 0xFF);
291  num >>= 8;
292  }
293  return ret;
294  }
295 
296  public static byte[] toByteArray(long arg) {
297  return toByteArray(arg, 8);
298  }
299 
300  private static HashMap<Short, Byte> sAllocatedSelfDeviceInstanceCode = new HashMap<Short, Byte>();
301 
302  public static byte allocateSelfDeviceInstanceCode(short echoClassCode) {
303 
304  if (sAllocatedSelfDeviceInstanceCode.containsKey(echoClassCode)) {
305 
306  byte code = sAllocatedSelfDeviceInstanceCode.get(echoClassCode);
307  code += 1;
308  sAllocatedSelfDeviceInstanceCode.put(echoClassCode, code);
309  return code;
310  } else {
311  sAllocatedSelfDeviceInstanceCode.put(echoClassCode, (byte)1);
312  return 1;
313 
314  }
315  }
316 }
317 
static byte[] propertiesToPropertyMap(byte[] properties)
Definition: EchoUtils.java:188
static byte[] toByteArray(long arg, int length)
Definition: EchoUtils.java:286
static String toHexString(byte[] data, String sep)
Definition: EchoUtils.java:170
static byte[] devicesToByteArray(DeviceObject[] devices, int index)
Definition: EchoUtils.java:45
static final byte[] instanceToByteArray(EchoObject eoj)
Definition: EchoUtils.java:41
static int getEchoObjectCode(short echoClassCode, byte instanceCode)
Definition: EchoUtils.java:120
static byte[] devicesToByteArray(DeviceObject[] devices)
Definition: EchoUtils.java:88
static String toHexString(byte[] data)
Definition: EchoUtils.java:167
static String toHexString(short s)
Definition: EchoUtils.java:184
static byte[] toByteArray(byte arg, int length)
Definition: EchoUtils.java:244
static byte[] toByteArray(int arg)
Definition: EchoUtils.java:282
static String toHexString(byte b)
Definition: EchoUtils.java:180
static InetAddress getLocalIpAddress()
Definition: EchoUtils.java:136
static byte[] toByteArray(short arg, int length)
Definition: EchoUtils.java:258
static int getEchoObjectCode(byte classGroupCode, byte classCode, byte instanceCode)
Definition: EchoUtils.java:110
static byte[] toByteArray(short arg)
Definition: EchoUtils.java:268
static final byte[] classToByteArray(EchoObject eoj)
Definition: EchoUtils.java:37
static byte[] toByteArray(long arg)
Definition: EchoUtils.java:296
static short getEchoClassCode(byte classGroupCode, byte classCode)
Definition: EchoUtils.java:101
static short getEchoClassCodeFromObjectCode(int objectCode)
Definition: EchoUtils.java:129
static byte allocateSelfDeviceInstanceCode(short echoClassCode)
Definition: EchoUtils.java:302
static NetworkInterface getNetworkInterface()
Definition: EchoUtils.java:151
static byte[] toByteArray(int arg, int length)
Definition: EchoUtils.java:272
static byte getInstanceCodeFromObjectCode(int objectCode)
Definition: EchoUtils.java:132
static byte[] toByteArray(byte arg)
Definition: EchoUtils.java:254
static byte[] propertyMapToProperties(byte[] map)
Definition: EchoUtils.java:219