1#!/usr/bin/env python
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 enum
18
19from acts.controllers.relay_lib.errors import RelayConfigError
20from acts.controllers.relay_lib.generic_relay_device import GenericRelayDevice
21from acts.controllers.relay_lib.helpers import validate_key
22
23PAIRING_MODE_WAIT_TIME = 3
24WAIT_TIME = 0.1
25MISSING_RELAY_MSG = 'Relay config for i6s Headset "%s" missing relay "%s".'
26
27
28class Buttons(enum.Enum):
29    Power = "Power"
30    Answer_Call = "Answer"
31    Initiate_Call = "Call"
32    Next = 'Next'
33    Previous = "Previous"
34    Play_pause = 'Play_pause'
35    Pair = "Pair"
36    Volume_up = "Volume_up"
37    Volume_down = "Volume_down"
38
39
40class I6sHeadset(GenericRelayDevice):
41
42    def __init__(self, config, relay_rig):
43        GenericRelayDevice.__init__(self, config, relay_rig)
44        self.mac_address = validate_key('mac_address', config, str,
45                                        'I6sHeadset')
46        for button in Buttons:
47            self.ensure_config_contains_relay(button.value)
48
49    def setup(self):
50        GenericRelayDevice.setup(self)
51
52    def clean_up(self):
53        """Turns off headset."""
54        self.relays[Buttons.Pair.value].set_no_for(PAIRING_MODE_WAIT_TIME)
55
56    def ensure_config_contains_relay(self, relay_name):
57        """
58        Throws an error if the relay does not exist.
59
60        Args:
61            relay_name:relay_name to be checked.
62        """
63        if relay_name not in self.relays:
64            raise RelayConfigError(MISSING_RELAY_MSG % (self.name, relay_name))
65
66    def pairing_mode(self):
67        """Sets relay in paring mode."""
68        self.relays[Buttons.Pair.value].set_no_for(PAIRING_MODE_WAIT_TIME)
69
70    def power_on(self):
71        """Power on relay."""
72        self.relays[Buttons.Power.value].set_no_for(WAIT_TIME)
73
74    def play_pause(self):
75        """
76        Sets relay to
77            Play state : if there is no A2DP_streaming.
78            Pause state : if there is A2DP_streaming.
79        """
80        self.relays[Buttons.Play_pause.value].set_no_for(WAIT_TIME)
81
82    def skip_next(self):
83        """Skips to next song from relay_device."""
84        self.relays[Buttons.Next.value].set_no_for(WAIT_TIME)
85
86    def skip_previous(self):
87        """Skips to previous song from relay_device."""
88        self.relays[Buttons.Previous.value].set_no_for(WAIT_TIME)
89
90    def volume_up(self):
91        """Increases volume from relay_device."""
92        self.relays[Buttons.Volume_up.value].set_no_for(WAIT_TIME)
93
94    def volume_down(self):
95        """Decreases volume from relay_device."""
96        self.relays[Buttons.Volume_down.value].set_no_for(WAIT_TIME)
97
98    def initiate_call_from_hf(self):
99        """Initiate call from relay device."""
100        for i in range(0, 2):
101            self.relays[Buttons.Initiate_Call.value].set_no_for(WAIT_TIME)
102        return True
103
104    def accept_call(self):
105        """Accepts call from relay device."""
106        self.relays[Buttons.Answer_Call.value].set_no_for(WAIT_TIME)
107        return True
108