1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.bluetooth.btservice;
18 
19 import android.bluetooth.BluetoothDevice;
20 import android.bluetooth.IBluetoothSocketManager;
21 import android.os.Binder;
22 import android.os.ParcelFileDescriptor;
23 import android.os.ParcelUuid;
24 
25 import com.android.bluetooth.Utils;
26 
27 class BluetoothSocketManagerBinder extends IBluetoothSocketManager.Stub {
28     private static final String TAG = "BluetoothSocketManagerBinder";
29 
30     private static final int INVALID_FD = -1;
31 
32     private AdapterService mService;
33 
BluetoothSocketManagerBinder(AdapterService service)34     BluetoothSocketManagerBinder(AdapterService service) {
35         mService = service;
36     }
37 
cleanUp()38     void cleanUp() {
39         mService = null;
40     }
41 
42     @Override
connectSocket( BluetoothDevice device, int type, ParcelUuid uuid, int port, int flag)43     public ParcelFileDescriptor connectSocket(
44             BluetoothDevice device, int type, ParcelUuid uuid, int port, int flag) {
45 
46         enforceActiveUser();
47 
48         if (!Utils.checkConnectPermissionForPreflight(mService)) {
49             return null;
50         }
51 
52         return marshalFd(mService.connectSocketNative(
53             Utils.getBytesFromAddress(device.getAddress()),
54             type,
55             Utils.uuidToByteArray(uuid),
56             port,
57             flag,
58             Binder.getCallingUid()));
59     }
60 
61     @Override
createSocketChannel( int type, String serviceName, ParcelUuid uuid, int port, int flag)62     public ParcelFileDescriptor createSocketChannel(
63             int type, String serviceName, ParcelUuid uuid, int port, int flag) {
64 
65         enforceActiveUser();
66 
67         if (!Utils.checkConnectPermissionForPreflight(mService)) {
68             return null;
69         }
70 
71         return marshalFd(mService.createSocketChannelNative(
72             type,
73             serviceName,
74             Utils.uuidToByteArray(uuid),
75             port,
76             flag,
77             Binder.getCallingUid()));
78 
79     }
80 
81     @Override
requestMaximumTxDataLength(BluetoothDevice device)82     public void requestMaximumTxDataLength(BluetoothDevice device) {
83         enforceActiveUser();
84 
85         if (!Utils.checkConnectPermissionForPreflight(mService)) {
86             return;
87         }
88 
89         mService.requestMaximumTxDataLengthNative(Utils.getBytesFromAddress(device.getAddress()));
90     }
91 
enforceActiveUser()92     private void enforceActiveUser() {
93         if (!Utils.checkCallerIsSystemOrActiveOrManagedUser(mService, TAG)) {
94             throw new SecurityException("Not allowed for non-active user");
95         }
96     }
97 
marshalFd(int fd)98     private static ParcelFileDescriptor marshalFd(int fd) {
99         if (fd == INVALID_FD) {
100             return null;
101         }
102         return ParcelFileDescriptor.adoptFd(fd);
103     }
104 }
105