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 """ 17 This test script exercises different GATT read procedures. 18 """ 19 20 from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest 21 from acts.test_utils.bt.GattConnectedBaseTest import GattConnectedBaseTest 22 from acts.test_utils.bt.GattEnum import GattCharacteristic 23 from acts.test_utils.bt.GattEnum import GattDescriptor 24 from acts.test_utils.bt.GattEnum import MtuSize 25 from acts.test_utils.bt.GattEnum import GattEvent 26 from acts.test_utils.bt.GattEnum import GattCbStrings 27 from math import ceil 28 29 30 class GattReadTest(GattConnectedBaseTest): 31 @BluetoothBaseTest.bt_test_wrap 32 def test_read_char(self): 33 """Test read characteristic value. 34 35 Test GATT read characteristic value. 36 37 Steps: 38 1. Central: send read request. 39 2. Peripheral: receive read request . 40 3. Peripheral: send read response with status 0 (success), and 41 characteristic value. 42 4. Central: receive read response, verify it's conent matches what was 43 sent 44 45 Expected Result: 46 Verify that read request/response is properly delivered. 47 48 Returns: 49 Pass if True 50 Fail if False 51 52 TAGS: LE, GATT, Characteristic 53 Priority: 0 54 """ 55 self.cen_ad.droid.gattClientReadCharacteristic( 56 self.bluetooth_gatt, self.discovered_services_index, 57 self.test_service_index, self.READABLE_CHAR_UUID) 58 59 event = self._server_wait(GattEvent.CHAR_READ_REQ) 60 61 request_id = event['data']['requestId'] 62 self.assertEqual(0, event['data']['offset'], "offset should be 0") 63 64 bt_device_id = 0 65 status = 0 66 char_value = [1, 2, 3, 4, 5, 6, 7, 20] 67 offset = 0 68 self.per_ad.droid.gattServerSendResponse(self.gatt_server, 69 bt_device_id, request_id, 70 status, offset, char_value) 71 72 event = self._client_wait(GattEvent.CHAR_READ) 73 self.assertEqual(status, event["data"]["Status"], 74 "Write status should be 0") 75 self.assertEqual(char_value, event["data"]["CharacteristicValue"], 76 "Read value shall be equal to value sent from server") 77 78 return True 79 80 @BluetoothBaseTest.bt_test_wrap 81 def test_read_long_char(self): 82 """Test read long characteristic value. 83 84 Test GATT read long characteristic value. 85 86 Steps: 87 1. Central: send read request. 88 2. Peripheral: receive read request . 89 3. Peripheral: send read response with status 0 (success), and 90 characteristic content. 91 5. Central: stack receives read response that was full, so stack sends 92 read blob request with increased offset. 93 6. Peripheral: receive read blob request, send read blob response with 94 next piece of characteristic value. 95 7. Central: stack receives read blob response, so stack sends read blob 96 request with increased offset. No Java callbacks are called here 97 8. Repeat steps 6 and 7 until whole characteristic is read. 98 9. Central: verify onCharacteristicRead callback is called with whole 99 characteristic content. 100 101 Expected Result: 102 Verify that read request/response is properly delivered, and that read 103 blob reqest/response is properly sent internally by the stack. 104 105 Returns: 106 Pass if True 107 Fail if False 108 109 TAGS: LE, GATT, Characteristic 110 Priority: 0 111 """ 112 char_value = [] 113 for i in range(512): 114 char_value.append(i % 256) 115 116 self.cen_ad.droid.gattClientReadCharacteristic( 117 self.bluetooth_gatt, self.discovered_services_index, 118 self.test_service_index, self.READABLE_CHAR_UUID) 119 120 # characteristic value is divided into packets, each contains MTU-1 121 # bytes. Compute number of packets we expect to receive. 122 num_packets = ceil((len(char_value) + 1) / (self.mtu - 1)) 123 124 for i in range(num_packets): 125 startOffset = i * (self.mtu - 1) 126 127 event = self._server_wait(GattEvent.CHAR_READ_REQ) 128 129 request_id = event['data']['requestId'] 130 self.assertEqual(startOffset, event['data']['offset'], 131 "offset should be 0") 132 133 bt_device_id = 0 134 status = 0 135 offset = 0 136 self.per_ad.droid.gattServerSendResponse( 137 self.gatt_server, bt_device_id, request_id, status, offset, 138 char_value[startOffset:startOffset + self.mtu - 1]) 139 140 event = self._client_wait(GattEvent.CHAR_READ) 141 142 self.assertEqual(status, event["data"]["Status"], 143 "Write status should be 0") 144 self.assertEqual(char_value, event["data"]["CharacteristicValue"], 145 "Read value shall be equal to value sent from server") 146 147 return True 148