• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#/usr/bin/env python3.4
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_utils.bt.BluetoothBaseTest import BluetoothBaseTest
21from acts.test_utils.bt.GattConnectedBaseTest import GattConnectedBaseTest
22from acts.test_utils.bt.GattEnum import GattCharacteristic
23from acts.test_utils.bt.GattEnum import GattDescriptor
24from acts.test_utils.bt.GattEnum import MtuSize
25from acts.test_utils.bt.GattEnum import GattEvent
26from acts.test_utils.bt.GattEnum import GattCbStrings
27from acts.test_utils.bt.GattEnum import GattConnectionPriority
28from math import ceil
29
30
31class GattReadTest(GattConnectedBaseTest):
32    @BluetoothBaseTest.bt_test_wrap
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(GattEvent.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(GattEvent.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    def test_read_long_char(self):
83        """Test read long characteristic value.
84
85        Test GATT read long characteristic value.
86
87        Steps:
88        1. Central: send read request.
89        2. Peripheral: receive read request .
90        3. Peripheral: send read response with status 0 (success), and
91           characteristic content.
92        5. Central: stack receives read response that was full, so stack sends
93           read blob request with increased offset.
94        6. Peripheral: receive read blob request, send read blob response with
95           next piece of characteristic value.
96        7. Central: stack receives read blob response, so stack sends read blob
97           request with increased offset. No Java callbacks are called here
98        8. Repeat steps 6 and 7 until whole characteristic is read.
99        9. Central: verify onCharacteristicRead callback is called with whole
100           characteristic content.
101
102        Expected Result:
103        Verify that read request/response is properly delivered, and that read
104        blob reqest/response is properly sent internally by the stack.
105
106        Returns:
107          Pass if True
108          Fail if False
109
110        TAGS: LE, GATT, Characteristic
111        Priority: 0
112        """
113        char_value = []
114        for i in range(512):
115            char_value.append(i % 256)
116
117        self.cen_ad.droid.gattClientReadCharacteristic(
118            self.bluetooth_gatt, self.discovered_services_index,
119            self.test_service_index, self.READABLE_CHAR_UUID)
120
121        # characteristic value is divided into packets, each contains MTU-1
122        # bytes. Compute number of packets we expect to receive.
123        num_packets = ceil((len(char_value) + 1) / (self.mtu - 1))
124
125        for i in range(num_packets):
126            startOffset = i * (self.mtu - 1)
127
128            event = self._server_wait(GattEvent.CHAR_READ_REQ)
129
130            request_id = event['data']['requestId']
131            self.assertEqual(startOffset, event['data']['offset'],
132                             "offset should be 0")
133
134            bt_device_id = 0
135            status = 0
136            offset = 0
137            self.per_ad.droid.gattServerSendResponse(
138                self.gatt_server, bt_device_id, request_id, status, offset,
139                char_value[startOffset:startOffset + self.mtu - 1])
140
141        event = self._client_wait(GattEvent.CHAR_READ)
142
143        self.assertEqual(status, event["data"]["Status"],
144                         "Write status should be 0")
145        self.assertEqual(char_value, event["data"]["CharacteristicValue"],
146                         "Read value shall be equal to value sent from server")
147
148        return True
149