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.io.j2me.btl2cap;
028:
029: import java.io.IOException;
030: import javax.microedition.io.Connection;
031: import javax.bluetooth.L2CAPConnection;
032: import javax.bluetooth.BluetoothConnectionException;
033:
034: import com.sun.midp.io.BluetoothUrl;
035: import com.sun.midp.io.BluetoothProtocol;
036: import com.sun.midp.security.Permissions;
037: import com.sun.midp.security.SecurityToken;
038: import com.sun.midp.main.Configuration;
039:
040: /**
041: * Provides "btl2cap" protocol implementation
042: */
043: public class Protocol extends BluetoothProtocol {
044: /** Keeps maximum MTU supported by the BT stack. */
045: static final int MAX_STACK_MTU;
046:
047: static {
048: int maxReceiveMTU;
049: try {
050: maxReceiveMTU = Integer.parseInt(System
051: .getProperty("bluetooth.l2cap.receiveMTU.max"));
052: } catch (NumberFormatException e) {
053: maxReceiveMTU = L2CAPConnection.DEFAULT_MTU;
054: }
055: MAX_STACK_MTU = maxReceiveMTU;
056: }
057:
058: /**
059: * Constructs an instance.
060: */
061: public Protocol() {
062: super (BluetoothUrl.L2CAP);
063: }
064:
065: /**
066: * Cheks permissions and opens requested connection.
067: *
068: * @param token security token passed by calling class
069: * @param url <code>BluetoothUrl</code> instance that defines required
070: * connection stringname the URL without protocol name and colon
071: * @param mode Connector.READ_WRITE or Connector.READ or Connector.WRITE
072: *
073: * @return a notifier in case of server connection string, open connection
074: * in case of client one.
075: *
076: * @exception IOException if opening connection fails.
077: */
078: public Connection openPrim(Object token, BluetoothUrl url, int mode)
079: throws IOException {
080: return openPrimImpl(token, url, mode);
081: }
082:
083: /**
084: * Ensures URL parameters have valid values. Sets receiveMTU if undefined.
085: * @param url URL to check
086: * @exception IllegalArgumentException if invalid url parameters found
087: * @exception BluetoothConnectionException if url parameters are not
088: * acceptable due to Bluetooth stack limitations
089: */
090: protected void checkUrl(BluetoothUrl url)
091: throws IllegalArgumentException,
092: BluetoothConnectionException {
093:
094: if (url.receiveMTU == -1) {
095: url.receiveMTU = L2CAPConnection.DEFAULT_MTU;
096: }
097:
098: if (url.isSystem()) {
099: return;
100: }
101:
102: super .checkUrl(url);
103:
104: if (!url.isServer
105: && (url.port <= 0x1000 || url.port >= 0xffff
106: || ((url.port & 1) != 1) || ((url.port & 0x100) != 0))) {
107: throw new IllegalArgumentException("Invalid PSM: "
108: + url.port);
109: }
110:
111: // IMPL_NOTE BluetoothConnectionException should be thrown here
112: // It is temporary substituted by IllegalArgumentException
113: // to pass TCK succesfully. To be changed back when fixed
114: // TCK arrives. The erroneous TCK test is
115: // javasoft.sqe.tests.api.javax.bluetooth.Connector.L2Cap.
116: // openClientTests.L2Cap1014()
117: //
118: // Correct code here is
119: // throw new BluetoothConnectionException(
120: // BluetoothConnectionException.UNACCEPTABLE_PARAMS,
121: // <message>);
122: if (url.receiveMTU < L2CAPConnection.MINIMUM_MTU) {
123: throw new IllegalArgumentException(
124: "Receive MTU is too small");
125: }
126:
127: if (url.receiveMTU > MAX_STACK_MTU) {
128: throw new IllegalArgumentException(
129: "Receive MTU is too large");
130: }
131:
132: if (url.transmitMTU != -1 && url.transmitMTU > MAX_STACK_MTU) {
133: throw new BluetoothConnectionException(
134: BluetoothConnectionException.UNACCEPTABLE_PARAMS,
135: "Transmit MTU is too large");
136: }
137: }
138:
139: /**
140: * Ensures that permissions are proper and creates client side connection.
141: * @param token security token if passed by caller, or <code>null</code>
142: * @param mode I/O access mode
143: * @return proper <code>L2CAPConnectionImpl</code> instance
144: * @exception IOException if openning connection fails.
145: */
146: protected Connection clientConnection(SecurityToken token, int mode)
147: throws IOException {
148: checkForPermission(token, Permissions.BLUETOOTH_CLIENT);
149: return new L2CAPConnectionImpl(url, mode);
150: }
151:
152: /**
153: * Ensures that permissions are proper and creates required notifier at
154: * server side.
155: * @param token security token if passed by caller, or <code>null</code>
156: * @param mode I/O access mode
157: * @return proper <code>L2CAPNotifierImpl</code> instance
158: * @exception IOException if openning connection fails
159: */
160: protected Connection serverConnection(SecurityToken token, int mode)
161: throws IOException {
162: checkForPermission(token, Permissions.BLUETOOTH_SERVER);
163: return new L2CAPNotifierImpl(url, mode);
164: }
165: }
|