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 GATT notify/indicate 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 GattNotifyTest(GattConnectedBaseTest): 32 @BluetoothBaseTest.bt_test_wrap 33 def test_notify_char(self): 34 """Test notify characteristic value. 35 36 Test GATT notify characteristic value. 37 38 Steps: 39 1. Central: write CCC - register for notifications. 40 2. Peripheral: receive CCC modification. 41 3. Peripheral: send characteristic notification. 42 4. Central: receive notification, verify it's conent matches what was 43 sent 44 45 Expected Result: 46 Verify that notification registration and delivery works. 47 48 Returns: 49 Pass if True 50 Fail if False 51 52 TAGS: LE, GATT, Characteristic 53 Priority: 0 54 """ 55 #write CCC descriptor to enable notifications 56 self.cen_ad.droid.gattClientDescriptorSetValue( 57 self.bluetooth_gatt, self.discovered_services_index, 58 self.test_service_index, self.NOTIFIABLE_CHAR_UUID, 59 self.CCC_DESC_UUID, GattDescriptor.ENABLE_NOTIFICATION_VALUE.value) 60 61 self.cen_ad.droid.gattClientWriteDescriptor( 62 self.bluetooth_gatt, self.discovered_services_index, 63 self.test_service_index, self.NOTIFIABLE_CHAR_UUID, 64 self.CCC_DESC_UUID) 65 66 #enable notifications in app 67 self.cen_ad.droid.gattClientSetCharacteristicNotification( 68 self.bluetooth_gatt, self.discovered_services_index, 69 self.test_service_index, self.NOTIFIABLE_CHAR_UUID, True) 70 71 event = self._server_wait(GattEvent.DESC_WRITE_REQ) 72 73 request_id = event['data']['requestId'] 74 bt_device_id = 0 75 status = 0 76 #confirm notification registration was successful 77 self.per_ad.droid.gattServerSendResponse( 78 self.gatt_server, bt_device_id, request_id, status, 0, []) 79 #wait for client to get response 80 event = self._client_wait(GattEvent.DESC_WRITE) 81 82 #set notified value 83 notified_value = [1,5,9,7,5,3,6,4,8,2] 84 self.per_ad.droid.gattServerCharacteristicSetValue( 85 self.notifiable_char_index, notified_value) 86 87 #send notification 88 self.per_ad.droid.gattServerNotifyCharacteristicChanged( 89 self.gatt_server, bt_device_id, 90 self.notifiable_char_index, False) 91 92 #wait for client to receive the notification 93 event = self._client_wait(GattEvent.CHAR_CHANGE) 94 self.assertEqual(notified_value, event["data"]["CharacteristicValue"]) 95 96 return True 97