1#!/usr/bin/env python3.4
2#
3#   Copyright 2017 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of 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,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17import time
18
19from acts.base_test import BaseTestClass
20from acts.test_decorators import test_tracker_info
21
22
23class NfcBasicFunctionalityTest(BaseTestClass):
24    nfc_on_event = "NfcStateOn"
25    nfc_off_event = "NfcStateOff"
26    timeout = 5
27
28    def setup_class(self):
29        self.dut = self.android_devices[0]
30        self._ensure_nfc_enabled(self.dut)
31        self.dut.droid.nfcStartTrackingStateChange()
32        self.dut.adb.shell("setprop nfc.app_log_level 255")
33        self.dut.adb.shell("setprop nfc.enable_protocol_log 255")
34        self.dut.adb.shell("setprop nfc.nxp_log_level_global 5")
35        self.dut.adb.shell("setprop nfc.nxp_log_level_extns 5")
36        self.dut.adb.shell("setprop nfc.nxp_log_level_hal 5")
37        self.dut.adb.shell("setprop nfc.nxp_log_level_nci 5")
38        self.dut.adb.shell("setprop nfc.nxp_log_level_tml 5")
39        self.dut.adb.shell("setprop nfc.nxp_log_level_dnld 5")
40        self._ensure_nfc_disabled(self.dut)
41        return True
42
43    def _ensure_nfc_enabled(self, dut):
44        end_time = time.time() + 10
45        while end_time > time.time():
46            try:
47                dut.ed.pop_event(self.nfc_on_event, self.timeout)
48                self.log.info("Event {} found".format(self.nfc_on_event))
49                return True
50            except Exception as err:
51                self.log.debug(
52                    "Event {} not yet found".format(self.nfc_on_event))
53        return False
54
55    def _ensure_nfc_disabled(self, dut):
56        end_time = time.time() + 10
57        while end_time > time.time():
58            try:
59                dut.ed.pop_event(self.nfc_off_event, self.timeout)
60                self.log.info("Event {} found".format(self.nfc_off_event))
61                return True
62            except Exception as err:
63                self.log.debug(
64                    "Event {} not yet found".format(self.nfc_off_event))
65        return False
66
67    def setup_test(self):
68        # Every test starts with the assumption that NFC is enabled
69        if not self.dut.droid.nfcIsEnabled():
70            self.dut.droid.nfcEnable()
71        else:
72            return True
73        if not self._ensure_nfc_enabled(self.dut):
74            self.log.error("Failed to toggle NFC on")
75            return False
76        return True
77
78    def on_fail(self, test_name, begin_time):
79        self.dut.take_bug_report(test_name, begin_time)
80
81    @test_tracker_info(uuid='d57fcdd8-c56c-4ab0-81fb-e2218b100de9')
82    def test_nfc_toggle_state_100_iterations(self):
83        """Test toggling NFC state 100 times.
84
85        Verify that NFC toggling works. Test assums NFC is on.
86
87        Steps:
88        1. Toggle NFC off
89        2. Toggle NFC on
90        3. Repeat steps 1-2 100 times.
91
92        Expected Result:
93        RFCOMM connection is established then disconnected succcessfully.
94
95        Returns:
96          Pass if True
97          Fail if False
98
99        TAGS: NFC
100        Priority: 1
101        """
102        iterations = 100
103        for i in range(iterations):
104            self.log.info("Starting iteration {}".format(i + 1))
105            self.dut.droid.nfcDisable()
106            if not self._ensure_nfc_disabled(self.dut):
107                return False
108            self.dut.droid.nfcEnable()
109            if not self._ensure_nfc_enabled(self.dut):
110                return False
111        return True
112