1 /*
2  * Copyright (C) 2015 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 android.bluetooth.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 
23 import android.bluetooth.le.ScanRecord;
24 import android.os.ParcelUuid;
25 
26 import androidx.test.ext.junit.runners.AndroidJUnit4;
27 import androidx.test.filters.SmallTest;
28 import androidx.test.platform.app.InstrumentationRegistry;
29 
30 import com.android.compatibility.common.util.CddTest;
31 import com.android.internal.util.ArrayUtils;
32 
33 import org.junit.Assume;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 
37 /**
38  * Unit test cases for {@link ScanRecord}.
39  * <p>
40  * To run this test, use adb shell am instrument -e class 'android.bluetooth.ScanRecordTest' -w
41  * 'com.android.bluetooth.tests/android.bluetooth.BluetoothTestRunner'
42  */
43 @RunWith(AndroidJUnit4.class)
44 public class ScanRecordTest {
45 
46     @CddTest(requirements = {"7.4.3/C-2-1"})
47     @SmallTest
48     @Test
parser()49     public void parser() {
50         Assume.assumeTrue(TestUtils.isBleSupported(
51                 InstrumentationRegistry.getInstrumentation().getContext()));
52 
53         byte[] partialScanRecord = new byte[] {
54                 0x02, 0x01, 0x1a, // advertising flags
55                 0x05, 0x02, 0x0b, 0x11, 0x0a, 0x11, // 16 bit service uuids
56                 0x04, 0x09, 0x50, 0x65, 0x64, // name
57                 0x02, 0x0A, (byte) 0xec, // tx power level
58                 0x05, 0x16, 0x0b, 0x11, 0x50, 0x64, // service data
59                 0x05, (byte) 0xff, (byte) 0xe0, 0x00, 0x02, 0x15, // manufacturer specific data
60                 0x05, 0x14, 0x0c, 0x11, 0x0d, 0x11, // 16 bit service solicitation uuids
61                 0x03, 0x50, 0x01, 0x02, // an unknown data type won't cause trouble
62         };
63 
64         final byte[] tdsData = new byte[] {
65             ScanRecord.DATA_TYPE_TRANSPORT_DISCOVERY_DATA, 0x42, 0x43, 0x02 /* len */, 0x08, 0x09
66         };
67         final byte[] tdsDataLengh = new byte[] { (byte) tdsData.length };
68 
69         byte[] scanRecord = ArrayUtils.concat(partialScanRecord, tdsDataLengh, tdsData);
70 
71         ScanRecord data = TestUtils.parseScanRecord(scanRecord);
72         assertEquals(0x1a, data.getAdvertiseFlags());
73         ParcelUuid uuid1 = ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
74         ParcelUuid uuid2 = ParcelUuid.fromString("0000110B-0000-1000-8000-00805F9B34FB");
75         ParcelUuid uuid3 = ParcelUuid.fromString("0000110C-0000-1000-8000-00805F9B34FB");
76         ParcelUuid uuid4 = ParcelUuid.fromString("0000110D-0000-1000-8000-00805F9B34FB");
77         assertTrue(data.getServiceUuids().contains(uuid1));
78         assertTrue(data.getServiceUuids().contains(uuid2));
79         assertFalse(data.getServiceUuids().contains(uuid3));
80         assertFalse(data.getServiceUuids().contains(uuid4));
81         assertFalse(data.getServiceSolicitationUuids().contains(uuid1));
82         assertFalse(data.getServiceSolicitationUuids().contains(uuid2));
83         assertTrue(data.getServiceSolicitationUuids().contains(uuid3));
84         assertTrue(data.getServiceSolicitationUuids().contains(uuid4));
85 
86         TestUtils.assertArrayEquals(data.getTransportDiscoveryData().toByteArray(), tdsData);
87 
88         assertEquals("Ped", data.getDeviceName());
89         assertEquals(-20, data.getTxPowerLevel());
90 
91         assertTrue(data.getManufacturerSpecificData().get(0x00E0) != null);
92 
93         final byte[] manufacturerData = new byte[] {
94                 0x02, 0x15 };
95         TestUtils.assertArrayEquals(manufacturerData,
96                 data.getManufacturerSpecificData().get(0x00E0));
97         TestUtils.assertArrayEquals(manufacturerData, data.getManufacturerSpecificData(0x00E0));
98 
99         assertTrue(data.getServiceData().containsKey(uuid2));
100         final byte[] serviceData = new byte[] {
101                 0x50, 0x64 };
102         TestUtils.assertArrayEquals(serviceData, data.getServiceData().get(uuid2));
103         TestUtils.assertArrayEquals(serviceData, data.getServiceData(uuid2));
104 
105         final byte[] adData = new byte[] {0x01, 0x02};
106         TestUtils.assertArrayEquals(adData, data.getAdvertisingDataMap().get(0x50));
107     }
108 }
109