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
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed 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.
*/
package android.server;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* Local cache of bonding state.
* We keep our own state to track the intermediate state BONDING, which
* bluez does not track.
* All addresses must be passed in upper case.
*/
class BluetoothBondState {
private static final String TAG = "BluetoothBondState";
private static final boolean DBG = true;
private final HashMap<String, Integer> mState = new HashMap<String, Integer>();
private final HashMap<String, Integer> mPinAttempt = new HashMap<String, Integer>();
private static final String AUTO_PAIRING_BLACKLIST =
"/etc/bluetooth/auto_pairing.conf";
private static final String DYNAMIC_AUTO_PAIRING_BLACKLIST =
"/data/misc/bluetooth/dynamic_auto_pairing.conf";
private ArrayList<String> mAutoPairingAddressBlacklist;
private ArrayList<String> mAutoPairingExactNameBlacklist;
private ArrayList<String> mAutoPairingPartialNameBlacklist;
private ArrayList<String> mAutoPairingFixedPinZerosKeyboardList;
// Addresses added to blacklist dynamically based on usage.
private ArrayList<String> mAutoPairingDynamicAddressBlacklist;
// If this is an outgoing connection, store the address.
// There can be only 1 pending outgoing connection at a time,
private String mPendingOutgoingBonding;
private final Context mContext;
private final BluetoothService mService;
private final BluetoothInputProfileHandler mBluetoothInputProfileHandler;
BluetoothBondState(Context context, BluetoothService service) {
mContext = context;
mService = service;
mBluetoothInputProfileHandler =
BluetoothInputProfileHandler.getInstance(mContext, mService);
}
synchronized void setPendingOutgoingBonding(String address) {
mPendingOutgoingBonding = address;
}
public synchronized String getPendingOutgoingBonding() {
return mPendingOutgoingBonding;
}
public synchronized void loadBondState() {
if (mService.getBluetoothStateInternal() !=
BluetoothAdapter.STATE_TURNING_ON) {
return;
}
String val = mService.getAdapterProperties().getProperty("Devices");
if (val == null) {
return;
}
String[] bonds = val.split(",");
if (bonds == null) {
return;
}
mState.clear();
if (DBG) Log.d(TAG, "found " + bonds.length + " bonded devices");
for (String device : bonds) {
mState.put(mService.getAddressFromObjectPath(device).toUpperCase(),
BluetoothDevice.BOND_BONDED);
}
}
public synchronized void setBondState(String address, int state) {
setBondState(address, state, 0);
}
/** reason is ignored unless state == BOND_NOT_BONDED */
public synchronized void setBondState(String address, int state, int reason) {
int oldState = getBondState(address);
if (oldState == state) {
return;
}
// Check if this was an pending outgoing bonding.
// If yes, reset the state.
if (oldState == BluetoothDevice.BOND_BONDING) {
if (address.equals(mPendingOutgoingBonding)) {
mPendingOutgoingBonding = null;
}
}
if (state == BluetoothDevice.BOND_BONDED) {
mService.addProfileState(address);
} else if (state == BluetoothDevice.BOND_NONE) {
mService.removeProfileState(address);
}
// HID is handled by BluetoothService, other profiles
// will be handled by their respective services.
mBluetoothInputProfileHandler.setInitialInputDevicePriority(
mService.getRemoteDevice(address), state);
if (DBG) {
Log.d(TAG, address + " bond state " + oldState + " -> " + state
+ " (" + reason + ")");
}
Intent intent = new Intent(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mService.getRemoteDevice(address));
intent.putExtra(BluetoothDevice.EXTRA_BOND_STATE, state);
intent.putExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, oldState);
if (state == BluetoothDevice.BOND_NONE) {
if (reason <= 0) {
Log.w(TAG, "setBondState() called to unbond device, but reason code is " +
"invalid. Overriding reason code with BOND_RESULT_REMOVED");
reason = BluetoothDevice.UNBOND_REASON_REMOVED;
}
intent.putExtra(BluetoothDevice.EXTRA_REASON, reason);
mState.remove(address);
} else {
mState.put(address, state);
}
mContext.sendBroadcast(intent, BluetoothService.BLUETOOTH_PERM);
}
public boolean isAutoPairingBlacklisted(String address) {
if (mAutoPairingAddressBlacklist != null) {
for (String blacklistAddress : mAutoPairingAddressBlacklist) {
if (address.startsWith(blacklistAddress)) return true;
}
}
if (mAutoPairingDynamicAddressBlacklist != null) {
for (String blacklistAddress: mAutoPairingDynamicAddressBlacklist) {
if (address.equals(blacklistAddress)) return true;
}
}
String name = mService.getRemoteName(address);
if (name != null) {
if (mAutoPairingExactNameBlacklist != null) {
for (String blacklistName : mAutoPairingExactNameBlacklist) {
if (name.equals(blacklistName)) return true;
}
}
if (mAutoPairingPartialNameBlacklist != null) {
for (String blacklistName : mAutoPairingPartialNameBlacklist) {
if (name.startsWith(blacklistName)) return true;
}
}
}
return false;
}
public boolean isFixedPinZerosAutoPairKeyboard(String address) {
// Note: the meaning of blacklist is reversed in this case.
// If its in the list, we can go ahead and auto pair since
// by default keyboard should have a variable PIN that we don't
// auto pair using 0000.
if (mAutoPairingFixedPinZerosKeyboardList != null) {
for (String blacklistAddress : mAutoPairingFixedPinZerosKeyboardList) {
if (address.startsWith(blacklistAddress)) return true;
}
}
return false;
}
public synchronized int getBondState(String address) {
Integer state = mState.get(address);
if (state == null) {
return BluetoothDevice.BOND_NONE;
}
return state.intValue();
}
/*package*/ synchronized String[] listInState(int state) {
ArrayList<String> result = new ArrayList<String>(mState.size());
for (Map.Entry<String, Integer> e : mState.entrySet()) {
if (e.getValue().intValue() == state) {
result.add(e.getKey());
}
}
return result.toArray(new String[result.size()]);
}
public synchronized void addAutoPairingFailure(String address) {
if (mAutoPairingDynamicAddressBlacklist == null) {
mAutoPairingDynamicAddressBlacklist = new ArrayList<String>();
}
updateAutoPairingData(address);
mAutoPairingDynamicAddressBlacklist.add(address);
}
public synchronized boolean isAutoPairingAttemptsInProgress(String address) {
return getAttempt(address) != 0;
}
public synchronized void clearPinAttempts(String address) {
mPinAttempt.remove(address);
}
public synchronized boolean hasAutoPairingFailed(String address) {
if (mAutoPairingDynamicAddressBlacklist == null) return false;
return mAutoPairingDynamicAddressBlacklist.contains(address);
}
public synchronized int getAttempt(String address) {
Integer attempt = mPinAttempt.get(address);
if (attempt == null) {
return 0;
}
return attempt.intValue();
}
public synchronized void attempt(String address) {
Integer attempt = mPinAttempt.get(address);
int newAttempt;
if (attempt == null) {
newAttempt = 1;
} else {
newAttempt = attempt.intValue() + 1;
}
mPinAttempt.put(address, new Integer(newAttempt));
}
private void copyAutoPairingData() {
FileInputStream in = null;
FileOutputStream out = null;
try {
File file = new File(DYNAMIC_AUTO_PAIRING_BLACKLIST);
if (file.exists()) return;
in = new FileInputStream(AUTO_PAIRING_BLACKLIST);
out= new FileOutputStream(DYNAMIC_AUTO_PAIRING_BLACKLIST);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
Log.e(TAG, "FileNotFoundException: copyAutoPairingData " + e);
} catch (IOException e) {
Log.e(TAG, "IOException: copyAutoPairingData " + e);
} finally {
try {
if (in != null) in.close();
if (out != null) out.close();
} catch (IOException e) {}
}
}
synchronized public void readAutoPairingData() {
if (mAutoPairingAddressBlacklist != null) return;
copyAutoPairingData();
FileInputStream fstream = null;
try {
fstream = new FileInputStream(DYNAMIC_AUTO_PAIRING_BLACKLIST);
DataInputStream in = new DataInputStream(fstream);
BufferedReader file = new BufferedReader(new InputStreamReader(in));
String line;
while((line = file.readLine()) != null) {
line = line.trim();
if (line.length() == 0 || line.startsWith("//")) continue;
String[] value = line.split("=");
if (value != null && value.length == 2) {
String[] val = value[1].split(",");
if (value[0].equalsIgnoreCase("AddressBlacklist")) {
mAutoPairingAddressBlacklist =
new ArrayList<String>(Arrays.asList(val));
} else if (value[0].equalsIgnoreCase("ExactNameBlacklist")) {
mAutoPairingExactNameBlacklist =
new ArrayList<String>(Arrays.asList(val));
} else if (value[0].equalsIgnoreCase("PartialNameBlacklist")) {
mAutoPairingPartialNameBlacklist =
new ArrayList<String>(Arrays.asList(val));
} else if (value[0].equalsIgnoreCase("FixedPinZerosKeyboardBlacklist")) {
mAutoPairingFixedPinZerosKeyboardList =
new ArrayList<String>(Arrays.asList(val));
} else if (value[0].equalsIgnoreCase("DynamicAddressBlacklist")) {
mAutoPairingDynamicAddressBlacklist =
new ArrayList<String>(Arrays.asList(val));
} else {
Log.e(TAG, "Error parsing Auto pairing blacklist file");
}
}
}
} catch (FileNotFoundException e) {
Log.e(TAG, "FileNotFoundException: readAutoPairingData " + e);
} catch (IOException e) {
Log.e(TAG, "IOException: readAutoPairingData " + e);
} finally {
if (fstream != null) {
try {
fstream.close();
} catch (IOException e) {
// Ignore
}
}
}
}
// This function adds a bluetooth address to the auto pairing blacklist
// file. These addresses are added to DynamicAddressBlacklistSection
private void updateAutoPairingData(String address) {
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(DYNAMIC_AUTO_PAIRING_BLACKLIST, true));
StringBuilder str = new StringBuilder();
if (mAutoPairingDynamicAddressBlacklist.size() == 0) {
str.append("DynamicAddressBlacklist=");
}
str.append(address);
str.append(",");
out.write(str.toString());
} catch (FileNotFoundException e) {
Log.e(TAG, "FileNotFoundException: updateAutoPairingData " + e);
} catch (IOException e) {
Log.e(TAG, "IOException: updateAutoPairingData " + e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// Ignore
}
}
}
}
}
|