1#
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17from host_controller.tfc import api_message
18from vts.utils.python.controllers import android_device
19
20
21class DeviceInfo(api_message.ApiMessage):
22    """The device information defined by TFC API.
23
24    Attributes:
25        _DEVICE_SNAPSHOT: The parameters of host_events.
26        _LEASE_HOST_TASKS: The parameters of leasehosttasks.
27        _OTHER: The data retrieved from TradeFed host but not used by the API.
28        _ALL_KEYS: Union of above.
29    """
30    _DEVICE_SNAPSHOT = {
31            "battery_level",
32            "build_id",
33            "device_serial",
34            "group_name",
35            "mac_address",
36            "product",
37            "product_variant",
38            "run_target",
39            "sim_operator",
40            "sim_state",
41            "sdk_version",
42            "state"}
43    _LEASE_HOST_TASKS = {
44            "device_serial",
45            "group_name",
46            "run_target",
47            "state"}
48    _OTHER = {"stub"}
49    _ALL_KEYS = (_DEVICE_SNAPSHOT | _LEASE_HOST_TASKS | _OTHER)
50
51    def __init__(self, device_serial, **kwargs):
52        """Initializes the attributes.
53
54        Args:
55            device_serial: The serial number of the device.
56            **kwargs: The optional attributes.
57        """
58        super(DeviceInfo, self).__init__(self._ALL_KEYS,
59                                         device_serial=device_serial, **kwargs)
60
61    def IsAvailable(self):
62        """Returns whether the device is available for running commands.
63
64        Returns:
65            A boolean.
66        """
67        return getattr(self, "state", None) == "Available"
68
69    def IsStub(self):
70        """Returns whether the device is a stub.
71
72        Returns:
73            A boolean.
74        """
75        return getattr(self, "stub", False)
76
77    def ToDeviceSnapshotJson(self):
78        """Converts to the parameter of host_events.
79
80        Returns:
81            A JSON object.
82        """
83        return self.ToJson(self._DEVICE_SNAPSHOT)
84
85    def ToLeaseHostTasksJson(self):
86        """Converts to the parameter of leasehosttasks.
87
88        Returns:
89            A JSON object.
90        """
91        return self.ToJson(self._LEASE_HOST_TASKS)
92
93    def Extend(self, properties):
94        """Adds properties to a DeviceInfo object, using AndroidDevice
95
96        Args:
97            properties: list of strings, list of properties to update
98        """
99        serial = getattr(self, "device_serial", None)
100        ad = android_device.AndroidDevice(serial)
101        for prop in properties:
102            val = getattr(ad, prop, None)
103            if val is None:
104                continue
105            setattr(self, prop, val)
106