My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2006-2009 Vlad Skarzhevskyy
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @author vlads
* @version $Id$
*/
package com.intel.bluetooth;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Enumeration;

import javax.bluetooth.DataElement;
import javax.bluetooth.UUID;

import junit.framework.TestCase;

/**
*
*
*/
public class BluetoothStackWIDCOMMSDPInputStreamTest extends TestCase {

protected void setUp() throws Exception {
super.setUp();
System.getProperties().put("bluecove.debug", "true");
}

public static boolean equals(DataElement de1, DataElement de2) {
if ((de1 == null) || (de2 == null)) {
return false;
}
try {
if (de1.getDataType() != de2.getDataType()) {
return false;
}
switch (de1.getDataType()) {
case DataElement.U_INT_1:
case DataElement.U_INT_2:
case DataElement.U_INT_4:
case DataElement.INT_1:
case DataElement.INT_2:
case DataElement.INT_4:
case DataElement.INT_8:
return (de1.getLong() == de2.getLong());
case DataElement.URL:
case DataElement.STRING:
case DataElement.UUID:
return de1.getValue().equals(de2.getValue());
case DataElement.INT_16:
case DataElement.U_INT_8:
case DataElement.U_INT_16:
byte[] byteAray1 = (byte[]) de1.getValue();
byte[] byteAray2 = (byte[]) de2.getValue();
if (byteAray1.length != byteAray2.length) {
return false;
}
for (int k = 0; k < byteAray1.length; k++) {
if (byteAray1[k] != byteAray2[k]) {
return false;
}
}
return true;
case DataElement.NULL:
return true;
case DataElement.BOOL:
return (de1.getBoolean() == de2.getBoolean());
case DataElement.DATSEQ:
case DataElement.DATALT:
Enumeration en1 = (Enumeration) de1.getValue();
Enumeration en2 = (Enumeration) de2.getValue();
for (; en1.hasMoreElements() && en2.hasMoreElements();) {
DataElement d1 = (DataElement) en1.nextElement();
DataElement d2 = (DataElement) en2.nextElement();
if (!equals(d1, d2)) {
return false;
}
}
if (en1.hasMoreElements() || en2.hasMoreElements()) {
return false;
}
return true;
default:
return false;
}
} catch (Throwable e) {
e.printStackTrace();
return false;
}
}

static public void assertEquals(String message, DataElement expected, DataElement actual) {
if (equals(expected, actual)) {
return;
}
fail(message + " expected:[" + expected + "] actual:" + actual + "]");
}

static class BluetoothStackWIDCOMMSDPOutputStream extends ByteArrayOutputStream {

/*
* private long readLong(int size) throws IOException { long result = 0; for (int i = 0; i < size; i++) { result +=
* ((long)read()) << (8 * i); } return result; }
*/

public void writeLong(long value, int size) throws IOException {
long data = value;
byte[] b = new byte[size];
for (int i = 0; i < size; i++) {
b[i] = (byte) ((data) & 0xFF);
data >>>= 8;
}
write(b);
}

public void writeInt(int i) throws IOException {
writeLong(i, 4);
}

public void write0(int size) throws IOException {
for (int i = 0; i < size; i++) {
write(0);
}
}

}

public void testUUID() {
UUID uuid = new UUID("E10C0FE1121111A11111161911110003", false);
byte[] bytes = BluetoothStackWIDCOMMSDPInputStream.getUUIDHexBytes(uuid);
UUID uuid2 = new UUID(BluetoothStackWIDCOMMSDPInputStream.hexString(bytes), false);
assertEquals("UUID", uuid, uuid2);
}

public void testServiceClassIDList() throws IOException {
/*
* DATSEQ { UUID 0000110100001000800000805f9b34fb (SERIAL_PORT) }
*/
DataElement expect = new DataElement(DataElement.DATSEQ);
expect.addElement(new DataElement(DataElement.UUID, new UUID("1101", true)));

BluetoothStackWIDCOMMSDPOutputStream bos = new BluetoothStackWIDCOMMSDPOutputStream();
int valueSize = BluetoothStackWIDCOMMSDPInputStream.MAX_ATTR_LEN_OLD;
bos.writeInt(valueSize);

bos.writeInt(1); // num_elem
bos.writeInt(BluetoothStackWIDCOMMSDPInputStream.ATTR_TYPE_UUID); // type
bos.writeInt(2); // len
bos.writeInt(1); // start_of_seq
bos.writeLong(0x1101, 2);
bos.flush();

DataElement element = (new BluetoothStackWIDCOMMSDPInputStream(new ByteArrayInputStream(bos.toByteArray())))
.readElement();

assertEquals("Element stream 1 item", expect, element);

/*
* DATSEQ { UUID 0000110100001000800000805f9b34fb (SERIAL_PORT) UUID 0000120300001000800000805f9b34fb }
*/
expect.addElement(new DataElement(DataElement.UUID, new UUID("1203", true)));

bos = new BluetoothStackWIDCOMMSDPOutputStream();
bos.writeInt(valueSize);

bos.writeInt(2); // num_elem
bos.writeInt(BluetoothStackWIDCOMMSDPInputStream.ATTR_TYPE_UUID); // type
bos.writeInt(2); // len
bos.writeInt(1); // start_of_seq
bos.writeLong(0x1101, 2);
bos.write0(valueSize - 2);
bos.writeInt(BluetoothStackWIDCOMMSDPInputStream.ATTR_TYPE_UUID); // type
bos.writeInt(2); // len
bos.writeInt(0); // start_of_seq
bos.writeLong(0x1203, 2);
bos.flush();

element = (new BluetoothStackWIDCOMMSDPInputStream(new ByteArrayInputStream(bos.toByteArray()))).readElement();

assertEquals("Element stream 2 items", expect, element);
}

public void testProtocolDescriptorList() throws IOException {
/*
* DATSEQ { DATSEQ { UUID 0000010000001000800000805f9b34fb (L2CAP) } DATSEQ { UUID
* 0000000300001000800000805f9b34fb (RFCOMM) U_INT_1 0x9 } DATSEQ { UUID 0000000800001000800000805f9b34fb (OBEX) } }
*/

DataElement expect = new DataElement(DataElement.DATSEQ);
DataElement e1 = new DataElement(DataElement.DATSEQ);
e1.addElement(new DataElement(DataElement.UUID, new UUID("0100", true)));
DataElement e2 = new DataElement(DataElement.DATSEQ);
e2.addElement(new DataElement(DataElement.UUID, new UUID("0003", true)));
e2.addElement(new DataElement(DataElement.U_INT_1, 9));
DataElement e3 = new DataElement(DataElement.DATSEQ);
e3.addElement(new DataElement(DataElement.UUID, new UUID("0008", true)));
expect.addElement(e1);
expect.addElement(e2);
expect.addElement(e3);

BluetoothStackWIDCOMMSDPOutputStream bos = new BluetoothStackWIDCOMMSDPOutputStream();

int valueSize = BluetoothStackWIDCOMMSDPInputStream.MAX_ATTR_LEN_OLD;
bos.writeInt(valueSize);

bos.writeInt(4); // num_elem

bos.writeInt(BluetoothStackWIDCOMMSDPInputStream.ATTR_TYPE_UUID); // type
bos.writeInt(2); // len
bos.writeInt(1); // start_of_seq
bos.writeLong(0x0100, 2);
bos.write0(valueSize - 2);

bos.writeInt(BluetoothStackWIDCOMMSDPInputStream.ATTR_TYPE_UUID); // type
bos.writeInt(2); // len
bos.writeInt(1); // start_of_seq
bos.writeLong(0x0003, 2);
bos.write0(valueSize - 2);

bos.writeInt(BluetoothStackWIDCOMMSDPInputStream.ATTR_TYPE_INT); // type
bos.writeInt(1); // len
bos.writeInt(0); // start_of_seq
bos.writeLong(9, 1);
bos.write0(valueSize - 1);

bos.writeInt(BluetoothStackWIDCOMMSDPInputStream.ATTR_TYPE_UUID); // type
bos.writeInt(2); // len
bos.writeInt(1); // start_of_seq
bos.writeLong(0x0008, 2);
bos.write0(valueSize - 2);

bos.flush();

DataElement element = (new BluetoothStackWIDCOMMSDPInputStream(new ByteArrayInputStream(bos.toByteArray())))
.readElement();

assertEquals("Element stream", expect, element);
}
}

Change log

r2915 by skarzhevskyy on Mar 13, 2009   Diff
Updated javadocs and Copyright
Go to: 
Project members, sign in to write a code review

Older revisions

r2476 by skarzhevskyy on Dec 1, 2008   Diff
Correction to headers
r2471 by skarzhevskyy on Nov 30, 2008   Diff
Change license to Apache License,
Version 2.0, Update headers
r2408 by skarzhevskyy on Oct 9, 2008   Diff
organize product to modules
All revisions of this file

File info

Size: 9565 bytes, 258 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Powered by Google Project Hosting