View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2008 Michael Lifshits
4    *  Copyright (C) 2008 Vlad Skarzhevskyy
5    *
6    *  Licensed to the Apache Software Foundation (ASF) under one
7    *  or more contributor license agreements.  See the NOTICE file
8    *  distributed with this work for additional information
9    *  regarding copyright ownership.  The ASF licenses this file
10   *  to you under the Apache License, Version 2.0 (the
11   *  "License"); you may not use this file except in compliance
12   *  with the License.  You may obtain a copy of the License at
13   *
14   *    http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing,
17   *  software distributed under the License is distributed on an
18   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   *  KIND, either express or implied.  See the License for the
20   *  specific language governing permissions and limitations
21   *  under the License.
22   *
23   *  @author vlads
24   *  @version $Id: EmulatorL2CAPClient.java 2476 2008-12-01 17:41:59Z skarzhevskyy $
25   */
26  package com.intel.bluetooth;
27  
28  import java.io.IOException;
29  
30  /**
31   *
32   *
33   */
34  class EmulatorL2CAPClient extends EmulatorLinkedConnection {
35  
36  	private int receiveMTU;
37  
38  	private int transmitMTU;
39  
40  	EmulatorL2CAPClient(EmulatorLocalDevice localDevice, long handle) {
41  		super(localDevice, handle);
42  	}
43  
44  	void connect(BluetoothConnectionParams params, int receiveMTU, int transmitMTU) throws IOException {
45  		connectVerify(params);
46  		this.connectionHandle = localDevice.getDeviceManagerService().l2Connect(localDevice.getAddress(),
47  				params.address, params.channel, params.authenticate, params.encrypt, receiveMTU, params.timeout);
48  		this.remoteAddress = params.address;
49  		this.receiveMTU = receiveMTU;
50  		this.transmitMTU = transmitMTU;
51  		int remoteDeviceReceiveMTU = localDevice.getDeviceManagerService().l2RemoteDeviceReceiveMTU(
52  				localDevice.getAddress(), this.connectionHandle);
53  		if (this.transmitMTU == -1) {
54  			this.transmitMTU = remoteDeviceReceiveMTU;
55  		} else if (this.transmitMTU < remoteDeviceReceiveMTU) {
56  			// Ok use selected
57  		} else {
58  			this.transmitMTU = remoteDeviceReceiveMTU;
59  		}
60  	}
61  
62  	void connect(long remoteAddress, long connectionHandle, int receiveMTU, int transmitMTU) throws IOException {
63  		super.connect(remoteAddress, connectionHandle);
64  		this.receiveMTU = receiveMTU;
65  		this.transmitMTU = transmitMTU;
66  	}
67  
68  	int getReceiveMTU() throws IOException {
69  		return receiveMTU;
70  	}
71  
72  	int getTransmitMTU() throws IOException {
73  		return transmitMTU;
74  	}
75  
76  	boolean ready() throws IOException {
77  		return localDevice.getDeviceManagerService().l2Ready(localDevice.getAddress(), this.connectionHandle);
78  	}
79  
80  	int receive(byte[] inBuf) throws IOException {
81  		byte[] packetData = localDevice.getDeviceManagerService().l2Receive(localDevice.getAddress(),
82  				this.connectionHandle, this.receiveMTU);
83  		int length = packetData.length;
84  		if (length > inBuf.length) {
85  			length = inBuf.length;
86  		}
87  		System.arraycopy(packetData, 0, inBuf, 0, length);
88  		return length;
89  	}
90  
91  	void send(byte[] data) throws IOException {
92  		if (data.length > transmitMTU) {
93  			byte[] b = new byte[transmitMTU];
94  			System.arraycopy(data, 0, b, 0, transmitMTU);
95  			data = b;
96  		}
97  		localDevice.getDeviceManagerService().l2Send(localDevice.getAddress(), this.connectionHandle, data);
98  	}
99  
100 }