xref: /btstack/platform/daemon/binding/java/src/com/bluekitchen/btstack/Util.java (revision a32d980d486a58fbb76d7997fc39bd072089d517)
1 package com.bluekitchen.btstack;
2 
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.UnsupportedEncodingException;
6 import java.util.Arrays;
7 
8 public class Util {
opcode(int ogf, int ocf)9 	public static int opcode(int ogf, int ocf){
10 		return ocf | (ogf << 10);
11 	}
12 
readByte(byte[] buffer, int offset)13 	public static int readByte(byte[] buffer, int offset){
14 		int data = buffer[offset];
15 		if (data >= 0) return data;
16 		return data + 256;
17 	}
18 
readBt16(byte[] buffer, int offset)19 	public static int readBt16(byte[] buffer, int offset){
20 		return readByte(buffer, offset) | (readByte(buffer, offset + 1) << 8);
21 	}
22 
readBt24(byte[] buffer, int offset)23 	public static int readBt24(byte[] buffer, int offset){
24 		return readByte(buffer, offset) | (readByte(buffer, offset + 1) << 8) | (readByte(buffer, offset + 2) << 16) ;
25 	}
26 
readBt32(byte[] buffer, int offset)27 	public static long readBt32(byte[] buffer, int offset){
28 		return (((long) readByte(buffer, offset+3)) << 24) | readByte(buffer, offset) | (readByte(buffer, offset + 1) << 8) | (readByte(buffer, offset + 2) << 16) ;
29 	}
30 
readBdAddr(byte[] buffer, int offset)31 	public static BD_ADDR readBdAddr(byte[] buffer, int offset){
32 		return new BD_ADDR(Arrays.copyOfRange(buffer, offset, offset + BD_ADDR.LEN));
33 	}
34 
readGattService(byte[] buffer, int offset)35 	public static GATTService readGattService(byte[] buffer, int offset){
36 		return new GATTService(Arrays.copyOfRange(buffer, offset, offset + GATTService.LEN));
37 	}
38 
readGattCharacteristic(byte[] buffer, int offset)39 	public static GATTCharacteristic readGattCharacteristic(byte[] buffer, int offset){
40 		return new GATTCharacteristic(Arrays.copyOfRange(buffer, offset, offset + GATTCharacteristic.LEN));
41 	}
42 
readGattCharacteristicDescriptor(byte[] buffer, int offset)43 	public static GATTCharacteristicDescriptor readGattCharacteristicDescriptor(byte[] buffer, int offset){
44 		return new GATTCharacteristicDescriptor(Arrays.copyOfRange(buffer, offset, offset + GATTCharacteristicDescriptor.LEN));
45 	}
46 
readExactly(InputStream in, byte[] buffer, int offset, int len)47 	public static int readExactly(InputStream in, byte[] buffer, int offset, int len){
48 		int readTotal = 0;
49 		try {
50 			while (len > 0){
51 				int read;
52 					read = in.read(buffer, offset, len);
53 				if (read < 0) break;
54 				len -= read;
55 				offset += read;
56 				readTotal += read;
57 			}
58 		} catch (IOException e) {
59 		}
60 		return readTotal;
61 	}
62 
storeByte(byte[] buffer, int offset, int value)63 	public static void storeByte(byte[] buffer, int offset, int value){
64 		buffer[offset] = (byte) value;
65 	}
66 
storeBt16(byte[] buffer, int offset, int value)67 	public static void storeBt16(byte[] buffer, int offset, int value){
68 		storeByte(buffer, offset, value & 0xff);
69 		storeByte(buffer, offset+1, value >> 8);
70 	}
71 
storeBt24(byte[] buffer, int offset, int value)72 	public static void storeBt24(byte[] buffer, int offset, int value){
73 		storeByte(buffer, offset, value & 0xff);
74 		storeByte(buffer, offset+1, value >> 8);
75 		storeByte(buffer, offset+2, value >> 16);
76 	}
77 
storeBt32(byte[] buffer, int offset, long value)78 	public static void storeBt32(byte[] buffer, int offset, long value){
79 		storeByte(buffer, offset, (int) (value & 0xff));
80 		storeByte(buffer, offset+1, (int) (value >> 8));
81 		storeByte(buffer, offset+2, (int) (value >> 16));
82 		storeByte(buffer, offset+3, (int) (value >> 24));
83 	}
84 
storeBytes(byte[] buffer, int offset, byte[] value)85 	public static void storeBytes(byte[] buffer, int offset, byte[] value) {
86 		System.arraycopy(value, 0, buffer, offset, value.length);
87 	}
88 
storeBytes(byte[] buffer, int offset, byte[] value, int len)89 	public static void storeBytes(byte[] buffer, int offset, byte[] value, int len) {
90 		int bytes_to_copy = Math.min(value.length, len);
91 		System.arraycopy(value, 0, buffer, offset, bytes_to_copy);
92 		for (int i = bytes_to_copy; i < len ; i++){
93 			buffer[offset + i] = 0;
94 		}
95 	}
96 
storeString(byte[] buffer, int offset, String value, int len)97 	public static void storeString(byte[] buffer, int offset, String value, int len) {
98 		byte data[] = new byte[0];
99 		try {
100 			data = value.getBytes("UTF-8");
101 		} catch (UnsupportedEncodingException e) {
102 			e.printStackTrace();
103 		}
104 		storeBytes(buffer, offset, data, len);
105 	}
106 
asHexdump(byte[] buffer, int len)107 	public static String asHexdump(byte[] buffer, int len){
108 		StringBuffer t = new StringBuffer();
109 		for (int i = 0; i < len ; i++){
110 			t.append(String.format("0x%02x, ", readByte(buffer, i)));
111 		}
112 		return t.toString();
113 	}
114 
asHexdump(byte[] buffer)115 	public static String asHexdump(byte[] buffer){
116 		return asHexdump(buffer, buffer.length);
117 	}
118 
hexdump(byte[] buffer, int len)119 	public static void hexdump(byte[] buffer, int len){
120 		System.out.println(asHexdump(buffer, len));
121 		System.out.println();
122 	}
123 
hexdump(byte[] buffer)124 	public static void hexdump(byte[] buffer){
125 		hexdump(buffer, buffer.length);
126 	}
127 
flipX(byte src[], byte dst[])128 	public static void flipX(byte src[], byte dst[]){
129 		int len = src.length;
130 		if (len != dst.length) return;
131 	    for (int i = 0; i < len ; i++) {
132 	        dst[len - 1 - i] = src[i];
133 		}
134 	}
135 
storeNet32(byte[] buffer, int offset, long value)136 	public static void storeNet32(byte[] buffer, int offset, long value) {
137 			storeByte(buffer, offset+3, (int) (value & 0xff));
138 			storeByte(buffer, offset+2, (int) (value >> 8));
139 			storeByte(buffer, offset+1, (int) (value >> 16));
140 			storeByte(buffer, offset,   (int) (value >> 24));
141 	}
142 
readNet32(byte[] buffer, int offset)143 	public static long readNet32(byte[] buffer, int offset) {
144 		return (((long) readByte(buffer, offset)) << 24) | (readByte(buffer, offset + 1) << 16) | (readByte(buffer, offset + 2) << 8)  | readByte(buffer, offset + 3);
145 	}
146 
serviceSearchPatternForUUID16(int uuid)147 	public static byte[] serviceSearchPatternForUUID16(int uuid){
148 		return new byte[] {(byte)0x35, (byte)0x03, (byte)0x19, (byte) (uuid >> 8), (byte) (uuid & 0xff)};
149 	}
150 
getBytes(byte[] buffer, int offset, int length)151 	public static byte[] getBytes(byte[] buffer, int offset, int length){
152 		return Arrays.copyOfRange(buffer, offset, offset + length);
153 	}
154 
getText(byte[] buffer, int offset, int length)155 	public static String getText(byte[] buffer, int offset, int length){
156 		byte [] byteData = getBytes(buffer, offset, length);
157 		try {
158 			return new String(byteData, "UTF-8");
159 		} catch (UnsupportedEncodingException e) {
160 			e.printStackTrace();
161 		}
162 		return "";
163 	}
164 }
165