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 for GATT longevity tests.
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_connection_priority
28from acts.test_utils.bt.bt_constants import gatt_characteristic_attr_length
29from acts.test_utils.bt.GattEnum import MtuSize
30from acts.test_utils.bt.bt_gatt_utils import setup_gatt_mtu
31
32
33class GattLongevityTest(GattConnectedBaseTest):
34    longevity_iterations = 1100000
35
36    @test_tracker_info(uuid='d7d378f4-89d8-4330-bb80-0054b92020bb')
37    def test_write_characteristic_no_resp_longevity(self):
38        """Longevity test write characteristic value
39
40        Longevity test to write characteristic value for
41        self.longevity_iteration times. This is to test the
42        integrity of written data and the robustness of central
43        and peripheral mode of the Android devices under test.
44
45        1. Central: write WRITABLE_CHAR_UUID characteristic with char_value
46           using write command.
47        2. Central: make sure write callback is called.
48        3. Peripheral: receive the written data.
49        4. Verify data written matches data received.
50        5. Repeat steps 1-4 self.longevity_iterations times.
51
52        Expected Result:
53        Verify that write command is properly delivered.
54
55        Returns:
56          Pass if True
57          Fail if False
58
59        TAGS: LE, GATT, Characteristic, Longevity
60        Priority: 0
61        """
62        self.cen_ad.droid.gattClientRequestConnectionPriority(
63            self.bluetooth_gatt, gatt_connection_priority['high'])
64
65        self.cen_ad.droid.gattClientCharacteristicSetWriteType(
66            self.bluetooth_gatt, self.discovered_services_index,
67            self.test_service_index, self.WRITABLE_CHAR_UUID,
68            gatt_characteristic['write_type_no_response'])
69
70        for i in range(self.longevity_iterations):
71            self.log.debug("Iteration {} started.".format(i + 1))
72            char_value = []
73            for j in range(i, i + self.mtu - 3):
74                char_value.append(j % 256)
75
76            self.cen_ad.droid.gattClientCharacteristicSetValue(
77                self.bluetooth_gatt, self.discovered_services_index,
78                self.test_service_index, self.WRITABLE_CHAR_UUID, char_value)
79
80            self.cen_ad.droid.gattClientWriteCharacteristic(
81                self.bluetooth_gatt, self.discovered_services_index,
82                self.test_service_index, self.WRITABLE_CHAR_UUID)
83
84            # client shall not wait for server, get complete event right away
85            event = self._client_wait(gatt_event['char_write'])
86            if event["data"]["Status"] != 0:
87                self.log.error("Write status should be 0")
88                return False
89
90            event = self._server_wait(gatt_event['char_write_req'])
91
92            self.log.info("{} event found: {}".format(gatt_cb_strings[
93                'char_write_req'].format(self.gatt_server_callback), event[
94                    'data']['value']))
95            request_id = event['data']['requestId']
96            found_value = event['data']['value']
97            if found_value != char_value:
98                self.log.info("Values didn't match. Found: {}, "
99                              "Expected: {}".format(found_value, char_value))
100                return False
101
102        return True
103