1#!/usr/bin/env python3
2#
3# Copyright (C) 2019 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16"""
17GATT PTS Automation
18"""
19
20from acts import signals
21from acts.test_utils.abstract_devices.bluetooth_device import AndroidBluetoothDevice
22from acts.test_utils.abstract_devices.bluetooth_device import FuchsiaBluetoothDevice
23from acts.controllers.bluetooth_pts_device import VERDICT_STRINGS
24from acts.test_utils.bt.pts.pts_base_class import PtsBaseClass
25
26import acts.test_utils.bt.gatt_test_database as gatt_test_database
27import acts.test_utils.bt.pts.fuchsia_pts_ics_lib as f_ics_lib
28import acts.test_utils.bt.pts.fuchsia_pts_ixit_lib as f_ixit_lib
29
30
31class GattPtsTest(PtsBaseClass):
32    ble_advertise_interval = 100
33    pts_action_mapping = None
34
35    def setup_class(self):
36        super().setup_class()
37        self.dut_bluetooth_local_name = "fs_test"
38        self.dut.initialize_bluetooth_controller()
39        self.dut.set_bluetooth_local_name(self.dut_bluetooth_local_name)
40        local_dut_mac_address = self.dut.get_local_bluetooth_address()
41
42        ics = None
43        ixit = None
44        if isinstance(self.dut, FuchsiaBluetoothDevice):
45            fuchsia_ixit = f_ixit_lib.GATT_IXIT
46            fuchsia_ixit[b'TSPX_bd_addr_iut'] = (b'OCTETSTRING',
47                                                 local_dut_mac_address.replace(
48                                                     ':', '').encode())
49            fuchsia_ixit[
50                b'TSPX_iut_device_name_in_adv_packet_for_random_address'] = (
51                    b'IA5STRING', self.dut_bluetooth_local_name.encode())
52            ics = f_ics_lib.GATT_ICS
53            ixit = fuchsia_ixit
54        elif isinstance(self.dut, AndroidBluetoothDevice):
55            # TODO: Add ICS and IXIT values for Android. For now just default
56            # To Fuchsia as it's a subset of Android.
57            self.log.warn(
58                "ICS/IXIT values not set for Android, using Fuchsia as default."
59            )
60            fuchsia_ixit = f_ixit_lib.GATT_IXIT
61            fuchsia_ixit[b'TSPX_bd_addr_iut'] = (b'OCTETSTRING',
62                                                 local_dut_mac_address.replace(
63                                                     ':', '').encode())
64            fuchsia_ixit[
65                b'TSPX_iut_device_name_in_adv_packet_for_random_address'] = (
66                    b'IA5STRING', self.dut_bluetooth_local_name.encode())
67            ics = f_ics_lib.GATT_ICS
68            ixit = fuchsia_ixit
69        else:
70            raise ValueError(
71                "Unable to run PTS tests on unsupported hardare {}.".format(
72                    type(self.dut)))
73
74        ### PTS SETUP: Required after ICS, IXIT, and profile is setup ###
75        self.pts.set_profile_under_test("GATT")
76        self.pts.set_ics_and_ixit(ics, ixit)
77        self.pts.setup_pts()
78        ### End PTS Setup ###
79
80        self.dut.unbond_all_known_devices()
81        self.dut.start_pairing_helper()
82
83    def setup_test(self):
84        super(GattPtsTest, self).setup_test()
85        # Make sure there were no lingering answers due to a failed test.
86        self.pts.extra_answers = []
87
88    def teardown_test(self):
89        super(GattPtsTest, self).teardown_test()
90        self.dut.stop_le_advertisement()
91        self.dut.close_gatt_server()
92
93    def teardown_class(self):
94        super(GattPtsTest, self).teardown_class()
95        self.dut.unbond_device(self.peer_identifier)
96
97    # BEGIN GATT CLIENT TESTCASES #
98    @PtsBaseClass.pts_test_wrap
99    def test_gatt_cl_gad_bv_01_c(self):
100        return self.pts.execute_test("GATT/CL/GAD/BV-01-C")
101
102    @PtsBaseClass.pts_test_wrap
103    def test_gatt_cl_gad_bv_03_c(self):
104        return self.pts.execute_test("GATT/CL/GAD/BV-03-C")
105
106    @PtsBaseClass.pts_test_wrap
107    def test_gatt_cl_gad_bv_04_c(self):
108        return self.pts.execute_test("GATT/CL/GAD/BV-04-C")
109
110    @PtsBaseClass.pts_test_wrap
111    def test_gatt_cl_gad_bv_05_c(self):
112        return self.pts.execute_test("GATT/CL/GAD/BV-05-C")
113
114    @PtsBaseClass.pts_test_wrap
115    def test_gatt_cl_gad_bv_06_c(self):
116        return self.pts.execute_test("GATT/CL/GAD/BV-06-C")
117
118    @PtsBaseClass.pts_test_wrap
119    def test_gatt_cl_gad_bv_07_c(self):
120        return self.pts.execute_test("GATT/CL/GAD/BV-07-C")
121
122    @PtsBaseClass.pts_test_wrap
123    def test_gatt_cl_gad_bv_08_c(self):
124        return self.pts.execute_test("GATT/CL/GAD/BV-08-C")
125
126    @PtsBaseClass.pts_test_wrap
127    def test_gatt_cl_gar_bv_01_c(self):
128        return self.pts.execute_test("GATT/CL/GAR/BV-01-C")
129
130    @PtsBaseClass.pts_test_wrap
131    def test_gatt_cl_gaw_bv_01_c(self):
132        return self.pts.execute_test("GATT/CL/GAW/BV-01-C")
133
134    @PtsBaseClass.pts_test_wrap
135    def test_gatt_cl_gaw_bv_03_c(self):
136        return self.pts.execute_test("GATT/CL/GAW/BV-03-C")
137
138    @PtsBaseClass.pts_test_wrap
139    def test_gatt_cl_gaw_bv_05_c(self):
140        return self.pts.execute_test("GATT/CL/GAW/BV-05-C")
141
142    @PtsBaseClass.pts_test_wrap
143    def test_gatt_cl_gaw_bv_08_c(self):
144        return self.pts.execute_test("GATT/CL/GAW/BV-08-C")
145
146    @PtsBaseClass.pts_test_wrap
147    def test_gatt_cl_gaw_bv_09_c(self):
148        return self.pts.execute_test("GATT/CL/GAW/BV-09-C")
149
150    @PtsBaseClass.pts_test_wrap
151    def test_gatt_cl_gaw_bi_02_c(self):
152        return self.pts.execute_test("GATT/CL/GAW/BI-02-C")
153
154    @PtsBaseClass.pts_test_wrap
155    def test_gatt_cl_gaw_bi_03_c(self):
156        return self.pts.execute_test("GATT/CL/GAW/BI-03-C")
157
158    @PtsBaseClass.pts_test_wrap
159    def test_gatt_cl_gaw_bi_05_c(self):
160        return self.pts.execute_test("GATT/CL/GAW/BI-05-C")
161
162    @PtsBaseClass.pts_test_wrap
163    def test_gatt_cl_gaw_bi_06_c(self):
164        return self.pts.execute_test("GATT/CL/GAW/BI-06-C")
165
166    @PtsBaseClass.pts_test_wrap
167    def test_gatt_cl_gaw_bi_07_c(self):
168        return self.pts.execute_test("GATT/CL/GAW/BI-07-C")
169
170    @PtsBaseClass.pts_test_wrap
171    def test_gatt_cl_gaw_bi_08_c(self):
172        return self.pts.execute_test("GATT/CL/GAW/BI-08-C")
173
174    @PtsBaseClass.pts_test_wrap
175    def test_gatt_cl_gaw_bi_09_c(self):
176        return self.pts.execute_test("GATT/CL/GAW/BI-09-C")
177
178    @PtsBaseClass.pts_test_wrap
179    def test_gatt_cl_gaw_bi_33_c(self):
180        return self.pts.execute_test("GATT/CL/GAW/BI-33-C")
181
182    @PtsBaseClass.pts_test_wrap
183    def test_gatt_cl_gar_bv_01_c(self):
184        return self.pts.execute_test("GATT/CL/GAR/BV-01-C")
185
186    @PtsBaseClass.pts_test_wrap
187    def test_gatt_cl_gar_bv_03_c(self):
188        return self.pts.execute_test("GATT/CL/GAR/BV-03-C")
189
190    @PtsBaseClass.pts_test_wrap
191    def test_gatt_cl_gar_bv_04_c(self):
192        return self.pts.execute_test("GATT/CL/GAR/BV-04-C")
193
194    @PtsBaseClass.pts_test_wrap
195    def test_gatt_cl_gar_bv_06_c(self):
196        return self.pts.execute_test("GATT/CL/GAR/BV-06-C")
197
198    @PtsBaseClass.pts_test_wrap
199    def test_gatt_cl_gar_bv_07_c(self):
200        return self.pts.execute_test("GATT/CL/GAR/BV-07-C")
201
202    # END GATT CLIENT TESTCASES #
203    # BEGIN GATT SERVER TESTCASES #
204
205    @PtsBaseClass.pts_test_wrap
206    def test_gatt_sr_gad_bv_01_c(self):
207        self.dut.setup_gatt_server(
208            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
209        return self.pts.execute_test("GATT/SR/GAD/BV-01-C")
210
211    @PtsBaseClass.pts_test_wrap
212    def test_gatt_sr_gad_bv_02_c(self):
213        self.dut.setup_gatt_server(
214            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
215        return self.pts.execute_test("GATT/SR/GAD/BV-02-C")
216
217    @PtsBaseClass.pts_test_wrap
218    def test_gatt_sr_gad_bv_03_c(self):
219        self.dut.setup_gatt_server(
220            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
221        return self.pts.execute_test("GATT/SR/GAD/BV-03-C")
222
223    @PtsBaseClass.pts_test_wrap
224    def test_gatt_sr_gad_bv_04_c(self):
225        self.dut.setup_gatt_server(
226            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
227        return self.pts.execute_test("GATT/SR/GAD/BV-04-C")
228
229    @PtsBaseClass.pts_test_wrap
230    def test_gatt_sr_gad_bv_05_c(self):
231        self.dut.setup_gatt_server(
232            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
233        return self.pts.execute_test("GATT/SR/GAD/BV-05-C")
234
235    @PtsBaseClass.pts_test_wrap
236    def test_gatt_sr_gad_bv_06_c(self):
237        self.dut.setup_gatt_server(
238            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
239        return self.pts.execute_test("GATT/SR/GAD/BV-06-C")
240
241    @PtsBaseClass.pts_test_wrap
242    def test_gatt_sr_gad_bv_07_c(self):
243        self.dut.setup_gatt_server(
244            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
245        return self.pts.execute_test("GATT/SR/GAD/BV-07-C")
246
247    @PtsBaseClass.pts_test_wrap
248    def test_gatt_sr_gad_bv_08_c(self):
249        self.dut.setup_gatt_server(
250            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
251        return self.pts.execute_test("GATT/SR/GAD/BV-08-C")
252
253    @PtsBaseClass.pts_test_wrap
254    def test_gatt_sr_gar_bv_01_c(self):
255        self.dut.setup_gatt_server(
256            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
257        return self.pts.execute_test("GATT/SR/GAR/BV-01-C")
258
259    @PtsBaseClass.pts_test_wrap
260    def test_gatt_sr_gar_bi_01_c(self):
261        self.dut.setup_gatt_server(
262            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
263        return self.pts.execute_test("GATT/SR/GAR/BI-01-C")
264
265    @PtsBaseClass.pts_test_wrap
266    def test_gatt_sr_gar_bi_02_c(self):
267        if self.characteristic_read_invalid_handle is None:
268            raise signals.TestSkip(
269                "Required user params missing:\n{}\n{}".format(
270                    "characteristic_read_invalid_handle"))
271        self.dut.setup_gatt_server(
272            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
273        return self.pts.execute_test("GATT/SR/GAR/BI-02-C")
274
275    @PtsBaseClass.pts_test_wrap
276    def test_gatt_sr_gar_bi_05_c(self):
277        self.dut.setup_gatt_server(
278            gatt_test_database.GATT_SERVER_DB_MAPPING.get('TEST_DB_2'))
279        return self.pts.execute_test("GATT/SR/GAR/BI-05-C")
280
281    @PtsBaseClass.pts_test_wrap
282    def test_gatt_sr_gar_bv_03_c(self):
283        self.dut.setup_gatt_server(
284            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
285        return self.pts.execute_test("GATT/SR/GAR/BV-03-C")
286
287    @PtsBaseClass.pts_test_wrap
288    def test_gatt_sr_gar_bi_06_c(self):
289        if (self.characteristic_read_not_permitted_uuid is None
290                or self.characteristic_read_not_permitted_handle is None):
291            raise signals.TestSkip(
292                "Required user params missing:\n{}\n{}".format(
293                    "characteristic_read_not_permitted_uuid",
294                    "characteristic_read_not_permitted_handle"))
295        self.dut.setup_gatt_server(
296            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
297        return self.pts.execute_test("GATT/SR/GAR/BI-06-C")
298
299    @PtsBaseClass.pts_test_wrap
300    def test_gatt_sr_gar_bi_07_c(self):
301        if self.characteristic_attribute_not_found_uuid is None:
302            raise signals.TestSkip("Required user params missing:\n{}".format(
303                "characteristic_attribute_not_found_uuid"))
304        self.dut.setup_gatt_server(
305            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
306        return self.pts.execute_test("GATT/SR/GAR/BI-07-C")
307
308    @PtsBaseClass.pts_test_wrap
309    def test_gatt_sr_gar_bi_08_c(self):
310        self.dut.setup_gatt_server(
311            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_3'))
312        return self.pts.execute_test("GATT/SR/GAR/BI-08-C")
313
314    def test_gatt_sr_gar_bi_11_c(self):
315        self.dut.setup_gatt_server(
316            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
317        return self.pts.execute_test("GATT/SR/GAR/BI-11-C")
318
319    @PtsBaseClass.pts_test_wrap
320    def test_gatt_sr_gar_bv_04_c(self):
321        self.dut.setup_gatt_server(
322            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_3'))
323        return self.pts.execute_test("GATT/SR/GAR/BV-04-C")
324
325    @PtsBaseClass.pts_test_wrap
326    def test_gatt_sr_gar_bi_12_c(self):
327        if self.characteristic_read_not_permitted_handle is None:
328            raise signals.TestSkip("Required user params missing:\n{}".format(
329                "characteristic_read_not_permitted_handle"))
330        self.dut.setup_gatt_server(
331            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
332        return self.pts.execute_test("GATT/SR/GAR/BI-12-C")
333
334    @PtsBaseClass.pts_test_wrap
335    def test_gatt_sr_gar_bi_13_c(self):
336        self.dut.setup_gatt_server(
337            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
338        return self.pts.execute_test("GATT/SR/GAR/BI-13-C")
339
340    @PtsBaseClass.pts_test_wrap
341    def test_gatt_sr_gar_bi_14_c(self):
342        if self.characteristic_read_invalid_handle is None:
343            raise signals.TestSkip("Required user params missing:\n{}".format(
344                "characteristic_read_invalid_handle"))
345        self.dut.setup_gatt_server(
346            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
347        return self.pts.execute_test("GATT/SR/GAR/BI-14-C")
348
349    @PtsBaseClass.pts_test_wrap
350    def test_gatt_sr_gar_bi_16_c(self):
351        self.dut.setup_gatt_server(
352            gatt_test_database.GATT_SERVER_DB_MAPPING.get('TEST_DB_2'))
353        return self.pts.execute_test("GATT/SR/GAR/BI-16-C")
354
355    @PtsBaseClass.pts_test_wrap
356    def test_gatt_sr_gar_bv_06_c(self):
357        self.dut.setup_gatt_server(
358            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
359        return self.pts.execute_test("GATT/SR/GAR/BV-06-C")
360
361    @PtsBaseClass.pts_test_wrap
362    def test_gatt_sr_gar_bv_07_c(self):
363        self.dut.setup_gatt_server(
364            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
365        return self.pts.execute_test("GATT/SR/GAR/BV-07-C")
366
367    @PtsBaseClass.pts_test_wrap
368    def test_gatt_sr_gar_bv_08_c(self):
369        self.dut.setup_gatt_server(
370            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
371        return self.pts.execute_test("GATT/SR/GAR/BV-08-C")
372
373    @PtsBaseClass.pts_test_wrap
374    def test_gatt_sr_gaw_bv_01_c(self):
375        self.dut.setup_gatt_server(
376            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_3'))
377        return self.pts.execute_test("GATT/SR/GAW/BV-01-C")
378
379    @PtsBaseClass.pts_test_wrap
380    def test_gatt_sr_gaw_bv_03_c(self):
381        self.dut.setup_gatt_server(
382            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_3'))
383        return self.pts.execute_test("GATT/SR/GAW/BV-03-C")
384
385    @PtsBaseClass.pts_test_wrap
386    def test_gatt_sr_gaw_bi_03_c(self):
387        self.dut.setup_gatt_server(
388            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
389        return self.pts.execute_test("GATT/SR/GAW/BI-03-C")
390
391    @PtsBaseClass.pts_test_wrap
392    def test_gatt_sr_gaw_bi_07_c(self):
393        self.dut.setup_gatt_server(
394            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_3'))
395        return self.pts.execute_test("GATT/SR/GAW/BI-07-C")
396
397    @PtsBaseClass.pts_test_wrap
398    def test_gatt_sr_gaw_bi_08_c(self):
399        self.dut.setup_gatt_server(
400            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_3'))
401        return self.pts.execute_test("GATT/SR/GAW/BI-08-C")
402
403    @PtsBaseClass.pts_test_wrap
404    def test_gatt_sr_gaw_bi_12_c(self):
405        self.dut.setup_gatt_server(
406            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
407        return self.pts.execute_test("GATT/SR/GAW/BI-12-C")
408
409    @PtsBaseClass.pts_test_wrap
410    def test_gatt_sr_gaw_bi_13_c(self):
411
412        self.dut.setup_gatt_server(
413            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_3'))
414        return self.pts.execute_test("GATT/SR/GAW/BI-03-C")
415
416    @PtsBaseClass.pts_test_wrap
417    def test_gatt_sr_gaw_bv_05_c(self):
418        self.dut.setup_gatt_server(
419            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_3'))
420        return self.pts.execute_test("GATT/SR/GAW/BV-05-C")
421
422    @PtsBaseClass.pts_test_wrap
423    def test_gatt_sr_gaw_bi_09_c(self):
424        self.dut.setup_gatt_server(
425            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
426        return self.pts.execute_test("GATT/SR/GAW/BI-09-C")
427
428    @PtsBaseClass.pts_test_wrap
429    def test_gatt_sr_gaw_bv_06_c(self):
430        self.dut.setup_gatt_server(
431            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
432        return self.pts.execute_test("GATT/SR/GAW/BV-06-C")
433
434    @PtsBaseClass.pts_test_wrap
435    def test_gatt_sr_gaw_bv_07_c(self):
436        self.dut.setup_gatt_server(
437            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
438        return self.pts.execute_test("GATT/SR/GAW/BV-07-C")
439
440    @PtsBaseClass.pts_test_wrap
441    def test_gatt_sr_gaw_bv_09_c(self):
442        self.dut.setup_gatt_server(
443            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
444        return self.pts.execute_test("GATT/SR/GAW/BV-09-C")
445
446    @PtsBaseClass.pts_test_wrap
447    def test_gatt_sr_gaw_bv_10_c(self):
448        self.dut.setup_gatt_server(
449            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_1'))
450        return self.pts.execute_test("GATT/SR/GAW/BV-10-C")
451
452    @PtsBaseClass.pts_test_wrap
453    def test_gatt_sr_gaw_bi_32_c(self):
454        self.dut.setup_gatt_server(
455            gatt_test_database.GATT_SERVER_DB_MAPPING.get('DB_TEST'))
456        return self.pts.execute_test("GATT/SR/GAW/BI-32-C")
457
458    @PtsBaseClass.pts_test_wrap
459    def test_gatt_sr_gaw_bi_33_c(self):
460        self.dut.setup_gatt_server(
461            gatt_test_database.GATT_SERVER_DB_MAPPING.get('LARGE_DB_3'))
462        return self.pts.execute_test("GATT/SR/GAW/BI-33-C")
463
464    # END GATT SERVER TESTCASES #
465