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.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 acts.test_utils.bt.bt_constants import gatt_char_desc_uuids
28from math import ceil
29
30
31class GattNotifyTest(GattConnectedBaseTest):
32    @BluetoothBaseTest.bt_test_wrap
33    @test_tracker_info(uuid='e0ba60af-c1f2-4516-a5d5-89e2def0c757')
34    def test_notify_char(self):
35        """Test notify characteristic value.
36
37        Test GATT notify characteristic value.
38
39        Steps:
40        1. Central: write CCC - register for notifications.
41        2. Peripheral: receive CCC modification.
42        3. Peripheral: send characteristic notification.
43        4. Central: receive notification, verify it's conent matches what was
44           sent
45
46        Expected Result:
47        Verify that notification registration and delivery works.
48
49        Returns:
50          Pass if True
51          Fail if False
52
53        TAGS: LE, GATT, Characteristic
54        Priority: 0
55        """
56        #write CCC descriptor to enable notifications
57        self.cen_ad.droid.gattClientDescriptorSetValue(
58            self.bluetooth_gatt, self.discovered_services_index,
59            self.test_service_index, self.NOTIFIABLE_CHAR_UUID,
60            gatt_char_desc_uuids['client_char_cfg'],
61            gatt_descriptor['enable_notification_value'])
62
63        self.cen_ad.droid.gattClientWriteDescriptor(
64            self.bluetooth_gatt, self.discovered_services_index,
65            self.test_service_index, self.NOTIFIABLE_CHAR_UUID,
66            gatt_char_desc_uuids['client_char_cfg'])
67
68        #enable notifications in app
69        self.cen_ad.droid.gattClientSetCharacteristicNotification(
70            self.bluetooth_gatt, self.discovered_services_index,
71            self.test_service_index, self.NOTIFIABLE_CHAR_UUID, True)
72
73        event = self._server_wait(gatt_event['desc_write_req'])
74
75        request_id = event['data']['requestId']
76        bt_device_id = 0
77        status = 0
78        #confirm notification registration was successful
79        self.per_ad.droid.gattServerSendResponse(
80            self.gatt_server, bt_device_id, request_id, 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