My favorites | Sign in
Logo
                
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2007-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.obex;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Vector;

import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.microedition.io.Connection;
import javax.microedition.io.StreamConnection;
import javax.obex.Authenticator;
import javax.obex.HeaderSet;
import javax.obex.ServerRequestHandler;

import com.intel.bluetooth.BluetoothConnectionAccess;
import com.intel.bluetooth.BluetoothStack;
import com.intel.bluetooth.DebugLog;
import com.intel.bluetooth.obex.OBEXAuthentication.Challenge;

/**
* Base for Client and Server implementations. See <a
* href="http://bluetooth.com/Bluetooth/Learn/Technology/Specifications/" >Bluetooth Specification Documents</A> for
* details.
*
*/
abstract class OBEXSessionBase implements Connection, BluetoothConnectionAccess {

protected boolean isConnected;

private StreamConnection conn;

private InputStream is;

private OutputStream os;

protected long connectionID;

protected int mtu = OBEXOperationCodes.OBEX_DEFAULT_MTU;

protected Authenticator authenticator;

protected OBEXConnectionParams obexConnectionParams;

protected int packetsCountWrite;

protected int packetsCountRead;

private Vector authChallengesSent;

/**
* Each request packet flowed by response. This flag is from Client point of view
*/
protected boolean requestSent;

public OBEXSessionBase(StreamConnection conn, OBEXConnectionParams obexConnectionParams) throws IOException {
if (obexConnectionParams == null) {
throw new NullPointerException("obexConnectionParams is null");
}
this.isConnected = false;
this.conn = conn;
this.obexConnectionParams = obexConnectionParams;
this.mtu = obexConnectionParams.mtu;
this.connectionID = -1;
this.packetsCountWrite = 0;
this.packetsCountRead = 0;
boolean initOK = false;
try {
this.os = conn.openOutputStream();
this.is = conn.openInputStream();
initOK = true;
} finally {
if (!initOK) {
try {
this.close();
} catch (IOException e) {
DebugLog.error("close error", e);
}
}
}
}

public void close() throws IOException {
StreamConnection c = this.conn;
this.conn = null;
try {
try {
if (this.is != null) {
this.is.close();
this.is = null;
}
} finally {
// Close output even if input can't be closed.
if (this.os != null) {
this.os.close();
this.os = null;
}
}
} finally {
// Close connection even if streams can't be closed.
if (c != null) {
c.close();
}
}

}

static OBEXHeaderSetImpl createOBEXHeaderSetImpl() {
return new OBEXHeaderSetImpl();
}

public static HeaderSet createOBEXHeaderSet() {
return createOBEXHeaderSetImpl();
}

static void validateCreatedHeaderSet(HeaderSet headers) {
OBEXHeaderSetImpl.validateCreatedHeaderSet(headers);
}

protected void writePacket(int commId, OBEXHeaderSetImpl headers) throws IOException {
writePacketWithFlags(commId, null, headers);
}

protected synchronized void writePacketWithFlags(int commId, byte[] headerFlagsData, OBEXHeaderSetImpl headers)
throws IOException {
if (this.requestSent) {
throw new IOException("Write packet out of order");
}
this.requestSent = true;
int len = 3;
if (this.connectionID != -1) {
len += 5;
}
if (headerFlagsData != null) {
len += headerFlagsData.length;
}
byte[] data = null;
if (headers != null) {
data = OBEXHeaderSetImpl.toByteArray(headers);
len += data.length;
}
if (len > mtu) {
throw new IOException("Can't sent more data than in MTU, len=" + len + ", mtu=" + mtu);
}
this.packetsCountWrite++;
ByteArrayOutputStream buf = new ByteArrayOutputStream();
OBEXHeaderSetImpl.writeObexLen(buf, commId, len);
if (headerFlagsData != null) {
buf.write(headerFlagsData);
}
if (this.connectionID != -1) {
OBEXHeaderSetImpl.writeObexInt(buf, OBEXHeaderSetImpl.OBEX_HDR_CONNECTION, this.connectionID);
}
if (data != null) {
buf.write(data);
}
DebugLog.debug0x("obex send (" + this.packetsCountWrite + ")", OBEXUtils.toStringObexResponseCodes(commId),
commId);
os.write(buf.toByteArray());
os.flush();
DebugLog.debug("obex sent (" + this.packetsCountWrite + ") len", len);

if ((headers != null) && (headers.hasAuthenticationChallenge())) {
if (authChallengesSent == null) {
authChallengesSent = new Vector();
}
for (Enumeration iter = headers.getAuthenticationChallenges(); iter.hasMoreElements();) {
byte[] authChallenge = (byte[]) iter.nextElement();
Challenge challenge = new Challenge(authChallenge);
authChallengesSent.addElement(challenge);
}
}
}

protected synchronized byte[] readPacket() throws IOException {
if (!this.requestSent) {
throw new IOException("Read packet out of order");
}
this.requestSent = false;
byte[] header = new byte[3];
OBEXUtils.readFully(is, obexConnectionParams, header);
this.packetsCountRead++;
DebugLog.debug0x("obex received (" + this.packetsCountRead + ")", OBEXUtils
.toStringObexResponseCodes(header[0]), header[0] & 0xFF);
int lenght = OBEXUtils.bytesToShort(header[1], header[2]);
if (lenght == 3) {
return header;
}
if ((lenght < 3) || (lenght > OBEXOperationCodes.OBEX_MAX_PACKET_LEN)) {
throw new IOException("Invalid packet length " + lenght);
}
byte[] data = new byte[lenght];
System.arraycopy(header, 0, data, 0, header.length);
OBEXUtils.readFully(is, obexConnectionParams, data, header.length, lenght - header.length);
if (is.available() > 0) {
DebugLog.debug("has more data after read", is.available());
}
return data;
}

private void validateBluetoothConnection() {
if ((conn != null) && !(conn instanceof BluetoothConnectionAccess)) {
throw new IllegalArgumentException("Not a Bluetooth connection " + conn.getClass().getName());
}
}

void validateAuthenticationResponse(OBEXHeaderSetImpl requestHeaders, OBEXHeaderSetImpl incomingHeaders)
throws IOException {
if ((requestHeaders != null) && requestHeaders.hasAuthenticationChallenge()
&& (!incomingHeaders.hasAuthenticationResponses())) {
// TODO verify that this appropriate Exception
throw new IOException("Authentication response is missing");
}
handleAuthenticationResponse(incomingHeaders, null);
}

boolean handleAuthenticationResponse(OBEXHeaderSetImpl incomingHeaders, ServerRequestHandler serverHandler)
throws IOException {
if (incomingHeaders.hasAuthenticationResponses()) {
if (authenticator == null) {
throw new IOException("Authenticator required for authentication");
}
if ((authChallengesSent == null) && (authChallengesSent.size() == 0)) {
throw new IOException("Authentication challenges had not been sent");
}
boolean authenticated = false;
try {
authenticated = OBEXAuthentication.handleAuthenticationResponse(incomingHeaders, authenticator,
serverHandler, authChallengesSent);
} finally {
if ((authenticated) && (authChallengesSent != null)) {
authChallengesSent.removeAllElements();
}
}
return authenticated;
} else {
if ((authChallengesSent != null) && (authChallengesSent.size() > 0)) {
throw new IOException("Authentication response is missing");
}
return true;
}
}

void handleAuthenticationChallenge(OBEXHeaderSetImpl incomingHeaders, OBEXHeaderSetImpl replyHeaders)
throws IOException {
if (incomingHeaders.hasAuthenticationChallenge()) {
if (authenticator == null) {
throw new IOException("Authenticator required for authentication");
}
OBEXAuthentication.handleAuthenticationChallenge(incomingHeaders, replyHeaders, authenticator);
}
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothConnectionAccess#getRemoteAddress()
*/
public long getRemoteAddress() throws IOException {
validateBluetoothConnection();
if (conn == null) {
throw new IOException("Connection closed");
}
return ((BluetoothConnectionAccess) conn).getRemoteAddress();
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothConnectionAccess#getRemoteDevice()
*/
public RemoteDevice getRemoteDevice() {
validateBluetoothConnection();
if (conn == null) {
return null;
}
return ((BluetoothConnectionAccess) conn).getRemoteDevice();
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothConnectionAccess#isClosed()
*/
public boolean isClosed() {
if (conn == null) {
return true;
}
if (this.conn instanceof BluetoothConnectionAccess) {
return ((BluetoothConnectionAccess) conn).isClosed();
} else {
return false;
}
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothConnectionAccess#shutdown()
*/
public void shutdown() throws IOException {
if (this.conn instanceof BluetoothConnectionAccess) {
((BluetoothConnectionAccess) conn).shutdown();
}
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothConnectionAccess#markAuthenticated()
*/
public void markAuthenticated() {
validateBluetoothConnection();
if (conn != null) {
((BluetoothConnectionAccess) conn).markAuthenticated();
}
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothConnectionAccess#getSecurityOpt()
*/
public int getSecurityOpt() {
validateBluetoothConnection();
if (conn == null) {
return ServiceRecord.NOAUTHENTICATE_NOENCRYPT;
} else {
return ((BluetoothConnectionAccess) conn).getSecurityOpt();
}
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothConnectionAccess#encrypt(boolean)
*/
public boolean encrypt(long address, boolean on) throws IOException {
validateBluetoothConnection();
if (conn == null) {
throw new IOException("Connection closed");
}
return ((BluetoothConnectionAccess) conn).encrypt(address, on);
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothConnectionAccess#setRemoteDevice(javax.bluetooth .RemoteDevice)
*/
public void setRemoteDevice(RemoteDevice remoteDevice) {
validateBluetoothConnection();
if (conn != null) {
((BluetoothConnectionAccess) conn).setRemoteDevice(remoteDevice);
}
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothConnectionAccess#getBluetoothStack()
*/
public BluetoothStack getBluetoothStack() {
validateBluetoothConnection();
if (conn == null) {
return null;
}
return ((BluetoothConnectionAccess) conn).getBluetoothStack();
}

/**
* Function used in unit tests.
*
* @return the packetsCountWrite
*/
int getPacketsCountWrite() {
return this.packetsCountWrite;
}

/**
* Function used in unit tests.
*
* @return the packetsCountRead
*/
int getPacketsCountRead() {
return this.packetsCountRead;
}

/**
* Function used in unit tests.
*
* @return the mtu
*/
int getPacketSize() {
return this.mtu;
}

/**
* Function used to change the connection mtu
*
* @param mtu
* @throws IOException
*/
void setPacketSize(int mtu) throws IOException {
if (isConnected) {
throw new IOException("Session already connected");
}
obexConnectionParams.mtu = mtu;
}
}
Show details Hide details

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

r2763 by skarzhevskyy on Feb 18, 2009   Diff
OBEX Close connection even if
operation can't be closed.
r2641 by skarzhevskyy on Dec 22, 2008   Diff
Fixed client side handling of
Authentication Challenge, will retry
sending headers
r2640 by skarzhevskyy on Dec 22, 2008   Diff
change internal writePacket function
to accept headers
All revisions of this file

File info

Size: 12296 bytes, 436 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Hosted by Google Code