001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.jsr082.bluetooth;
028:
029: import com.sun.midp.jsr082.BluetoothUtils;
030:
031: /**
032: * BluetoothStack implementation which relies on HCI command/event flow.
033: */
034: public class GenericBluetoothStack extends BluetoothStack {
035:
036: /** Maximum HCI packet size as defined by Bluetooth 1.1 specification. */
037: private final int MAX_HCI_PACKET_SIZE = 257;
038: /** HCI Inquiry Complete event code. */
039: private final int HCI_INQUIRY_COMPLETE = 0x01;
040: /** HCI Inquiry Result event code. */
041: private final int HCI_INQUIRY_RESULT = 0x02;
042: /** HCI Authentication Complete event code. */
043: private final int HCI_AUTH_COMPLETE = 0x06;
044: /** HCI Remote Name Request Complete event code. */
045: private final int HCI_NAME_COMPLETE = 0x07;
046: /** HCI Encrypt Change event code. */
047: private final int HCI_ENCRYPT_CHANGE = 0x08;
048:
049: /** Internal byte array for storing incoming HCI events. */
050: private byte[] buffer = new byte[MAX_HCI_PACKET_SIZE];
051:
052: /**
053: * Extracts Bluetooth address from the internal buffer.
054: *
055: * @param offset offset in the internal buffer
056: * @return Bluetooth address consisting of 12 hexadecimal characters
057: */
058: protected String extractAddress(int offset) {
059: byte[] addr = new byte[BluetoothUtils.BTADDR_SIZE];
060: System.arraycopy(buffer, offset, addr, 0,
061: BluetoothUtils.BTADDR_SIZE);
062: return BluetoothUtils.getAddressString(addr);
063: }
064:
065: /**
066: * Extracts string (e.g. friendly name) from the internal buffer. The
067: * string is stored in UTF-8 format and is terminated by NULL character.
068: *
069: * @param offset offset in the internal buffer
070: * @return Java string retrieved from binary event data
071: */
072: protected String extractString(int offset) {
073: int length = 0;
074: while (offset + length < MAX_HCI_PACKET_SIZE
075: && buffer[offset + length] != 0) {
076: length++;
077: }
078: // IMPL_NOTE: UTF-8 encoding support for Java strings?
079: // return new String(buffer, offset, length, "UTF-8");
080: return stringUTF8(buffer, offset, length);
081: }
082:
083: /**
084: * Extracts 2 octets from the internal buffer.
085: *
086: * @param offset offset in the internal buffer
087: * @return 16 bits of data from the given offset
088: */
089: protected int extractShort(int offset) {
090: return ((int) buffer[offset] & 0xff)
091: | (((int) buffer[offset + 1] & 0xff) << 8);
092: }
093:
094: /**
095: * Retrieves Bluetooth event from HCI event data containing in the
096: * internal buffer.
097: *
098: * @return BluetoothEvent subclass instance
099: */
100: protected BluetoothEvent retrieveEvent() {
101: readData(buffer);
102: int code = buffer[0];
103: int len = buffer[1];
104: switch (code) {
105: case HCI_INQUIRY_COMPLETE:
106: return new InquiryCompleteEvent(buffer[2] == 0);
107: case HCI_INQUIRY_RESULT:
108: int num = buffer[2];
109: int offset = 3;
110: InquiryResult[] results = new InquiryResult[num];
111: for (int i = 0; i < num; i++) {
112: String addr = extractAddress(offset);
113: int cod = 0;
114: for (int j = 0; j < 3; j++) {
115: cod |= ((int) buffer[offset + 9 + j] & 0xff) << (16 - j * 8);
116: }
117: results[i] = new InquiryResult(addr, cod);
118: offset += 14; // 14 is the size of the response structure
119: }
120: return new InquiryResultEvent(results);
121: case HCI_AUTH_COMPLETE:
122: return new AuthenticationCompleteEvent(extractShort(3),
123: buffer[2] == 0);
124: case HCI_NAME_COMPLETE:
125: return new NameResultEvent(extractAddress(3),
126: extractString(9));
127: case HCI_ENCRYPT_CHANGE:
128: return new EncryptionChangeEvent(extractShort(3),
129: buffer[2] == 0, buffer[5] == 1);
130: default:
131: return null;
132: }
133: }
134:
135: }
|