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 GATT notify/indicate procedures.
18"""
19
20from acts.test_decorators import test_tracker_info
21from acts_contrib.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
22from acts_contrib.test_utils.bt.GattConnectedBaseTest import GattConnectedBaseTest
23from acts_contrib.test_utils.bt.bt_constants import gatt_characteristic
24from acts_contrib.test_utils.bt.bt_constants import gatt_descriptor
25from acts_contrib.test_utils.bt.bt_constants import gatt_event
26from acts_contrib.test_utils.bt.bt_constants import gatt_cb_strings
27from acts_contrib.test_utils.bt.bt_constants import gatt_char_desc_uuids
28
29
30class GattNotifyTest(GattConnectedBaseTest):
31    @BluetoothBaseTest.bt_test_wrap
32    @test_tracker_info(uuid='e0ba60af-c1f2-4516-a5d5-89e2def0c757')
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            gatt_char_desc_uuids['client_char_cfg'],
60            gatt_descriptor['enable_notification_value'])
61
62        self.cen_ad.droid.gattClientWriteDescriptor(
63            self.bluetooth_gatt, self.discovered_services_index,
64            self.test_service_index, self.NOTIFIABLE_CHAR_UUID,
65            gatt_char_desc_uuids['client_char_cfg'])
66
67        #enable notifications in app
68        self.cen_ad.droid.gattClientSetCharacteristicNotification(
69            self.bluetooth_gatt, self.discovered_services_index,
70            self.test_service_index, self.NOTIFIABLE_CHAR_UUID, True)
71
72        event = self._server_wait(gatt_event['desc_write_req'])
73
74        request_id = event['data']['requestId']
75        bt_device_id = 0
76        status = 0
77        #confirm notification registration was successful
78        self.per_ad.droid.gattServerSendResponse(self.gatt_server,
79                                                 bt_device_id, request_id,
80                                                 status, 0, [])
81        #wait for client to get response
82        event = self._client_wait(gatt_event['desc_write'])
83
84        #set notified value
85        notified_value = [1, 5, 9, 7, 5, 3, 6, 4, 8, 2]
86        self.per_ad.droid.gattServerCharacteristicSetValue(
87            self.notifiable_char_index, notified_value)
88
89        #send notification
90        self.per_ad.droid.gattServerNotifyCharacteristicChanged(
91            self.gatt_server, bt_device_id, self.notifiable_char_index, False)
92
93        #wait for client to receive the notification
94        event = self._client_wait(gatt_event['char_change'])
95        self.assertEqual(notified_value, event["data"]["CharacteristicValue"])
96
97        return True
98