• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright (C) 2016 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"""
17This test script exercises different GATT read procedures.
18"""
19
20from acts.test_decorators import test_tracker_info
21from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
22from acts.test_utils.bt.GattConnectedBaseTest import GattConnectedBaseTest
23from acts.test_utils.bt.bt_constants import gatt_characteristic
24from acts.test_utils.bt.bt_constants import gatt_descriptor
25from acts.test_utils.bt.bt_constants import gatt_event
26from acts.test_utils.bt.bt_constants import gatt_cb_strings
27from math import ceil
28
29
30class GattReadTest(GattConnectedBaseTest):
31    @BluetoothBaseTest.bt_test_wrap
32    @test_tracker_info(uuid='ed8523f4-0eb8-4d14-b558-d3c28902f8bb')
33    def test_read_char(self):
34        """Test read characteristic value.
35
36        Test GATT read characteristic value.
37
38        Steps:
39        1. Central: send read request.
40        2. Peripheral: receive read request .
41        3. Peripheral: send read response with status 0 (success), and
42           characteristic value.
43        4. Central: receive read response, verify it's conent matches what was
44           sent
45
46        Expected Result:
47        Verify that read request/response is properly delivered.
48
49        Returns:
50          Pass if True
51          Fail if False
52
53        TAGS: LE, GATT, Characteristic
54        Priority: 0
55        """
56        self.cen_ad.droid.gattClientReadCharacteristic(
57            self.bluetooth_gatt, self.discovered_services_index,
58            self.test_service_index, self.READABLE_CHAR_UUID)
59
60        event = self._server_wait(gatt_event['char_read_req'])
61
62        request_id = event['data']['requestId']
63        self.assertEqual(0, event['data']['offset'], "offset should be 0")
64
65        bt_device_id = 0
66        status = 0
67        char_value = [1, 2, 3, 4, 5, 6, 7, 20]
68        offset = 0
69        self.per_ad.droid.gattServerSendResponse(self.gatt_server,
70                                                 bt_device_id, request_id,
71                                                 status, offset, char_value)
72
73        event = self._client_wait(gatt_event['char_read'])
74        self.assertEqual(status, event["data"]["Status"],
75                         "Write status should be 0")
76        self.assertEqual(char_value, event["data"]["CharacteristicValue"],
77                         "Read value shall be equal to value sent from server")
78
79        return True
80
81    @BluetoothBaseTest.bt_test_wrap
82    @test_tracker_info(uuid='5916a78d-3db8-4df2-9b96-b25e99096e0d')
83    def test_read_long_char(self):
84        """Test read long characteristic value.
85
86        Test GATT read long characteristic value.
87
88        Steps:
89        1. Central: send read request.
90        2. Peripheral: receive read request .
91        3. Peripheral: send read response with status 0 (success), and
92           characteristic content.
93        5. Central: stack receives read response that was full, so stack sends
94           read blob request with increased offset.
95        6. Peripheral: receive read blob request, send read blob response with
96           next piece of characteristic value.
97        7. Central: stack receives read blob response, so stack sends read blob
98           request with increased offset. No Java callbacks are called here
99        8. Repeat steps 6 and 7 until whole characteristic is read.
100        9. Central: verify onCharacteristicRead callback is called with whole
101           characteristic content.
102
103        Expected Result:
104        Verify that read request/response is properly delivered, and that read
105        blob reqest/response is properly sent internally by the stack.
106
107        Returns:
108          Pass if True
109          Fail if False
110
111        TAGS: LE, GATT, Characteristic
112        Priority: 0
113        """
114        char_value = []
115        for i in range(512):
116            char_value.append(i % 256)
117
118        self.cen_ad.droid.gattClientReadCharacteristic(
119            self.bluetooth_gatt, self.discovered_services_index,
120            self.test_service_index, self.READABLE_CHAR_UUID)
121
122        # characteristic value is divided into packets, each contains MTU-1
123        # bytes. Compute number of packets we expect to receive.
124        num_packets = ceil((len(char_value) + 1) / (self.mtu - 1))
125
126        for i in range(num_packets):
127            startOffset = i * (self.mtu - 1)
128
129            event = self._server_wait(gatt_event['char_read_req'])
130
131            request_id = event['data']['requestId']
132            self.assertEqual(startOffset, event['data']['offset'],
133                             "offset should be 0")
134
135            bt_device_id = 0
136            status = 0
137            offset = 0
138            self.per_ad.droid.gattServerSendResponse(
139                self.gatt_server, bt_device_id, request_id, status, offset,
140                char_value[startOffset:startOffset + self.mtu - 1])
141
142        event = self._client_wait(gatt_event['char_read'])
143
144        self.assertEqual(status, event["data"]["Status"],
145                         "Write status should be 0")
146        self.assertEqual(char_value, event["data"]["CharacteristicValue"],
147                         "Read value shall be equal to value sent from server")
148
149        return True
150
151    @BluetoothBaseTest.bt_test_wrap
152    @test_tracker_info(uuid='12498522-cac5-478b-a0cd-faa542832fa8')
153    def test_read_using_char_uuid(self):
154        """Test read using characteristic UUID.
155
156        Test GATT read value using characteristic UUID.
157
158        Steps:
159        1. Central: send read by UUID request.
160        2. Peripheral: receive read request .
161        3. Peripheral: send read response with status 0 (success), and
162           characteristic value.
163        4. Central: receive read response, verify it's conent matches what was
164           sent
165
166        Expected Result:
167        Verify that read request/response is properly delivered.
168
169        Returns:
170          Pass if True
171          Fail if False
172
173        TAGS: LE, GATT, Characteristic
174        Priority: 0
175        """
176        self.cen_ad.droid.gattClientReadUsingCharacteristicUuid(
177            self.bluetooth_gatt, self.READABLE_CHAR_UUID, 0x0001, 0xFFFF)
178
179        event = self._server_wait(gatt_event['char_read_req'])
180
181        request_id = event['data']['requestId']
182        self.assertEqual(0, event['data']['offset'], "offset should be 0")
183
184        bt_device_id = 0
185        status = 0
186        char_value = [1, 2, 3, 4, 5, 6, 7, 20]
187        offset = 0
188        self.per_ad.droid.gattServerSendResponse(self.gatt_server,
189                                                 bt_device_id, request_id,
190                                                 status, offset, char_value)
191
192        event = self._client_wait(gatt_event['char_read'])
193        self.assertEqual(status, event["data"]["Status"],
194                         "Write status should be 0")
195        self.assertEqual(char_value, event["data"]["CharacteristicValue"],
196                         "Read value shall be equal to value sent from server")
197
198        return True
199