xmlrpc_datatypes.py revision d1d96b886f5eebdbb2a13047638dd1f21472d0f9
1# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5from autotest_lib.client.common_lib.cros import xmlrpc_datatypes
6
7class AssociationParameters(xmlrpc_datatypes.XmlRpcStruct):
8    """Describes parameters used in WiFi connection attempts."""
9
10    DEFAULT_SECURITY = 'none'
11    DEFAULT_PSK = ''
12    DEFAULT_DISCOVERY_TIMEOUT = 15
13    DEFAULT_ASSOCIATION_TIMEOUT = 15
14    DEFAULT_CONFIGURATION_TIMEOUT = 15
15
16    def __init__(self, serialized=None):
17        """Construct an AssociationParameters.
18
19        @param serialized dict passed over the wire from XMLRPC server.
20
21        """
22        super(AssociationParameters, self).__init__()
23        if serialized is None:
24            serialized = {}
25        # The network to connect to (e.g. 'GoogleGuest').
26        self.ssid = serialized.get('ssid', None)
27        # Which encryption to use (e.g. 'wpa').
28        self.security = serialized.get('security', self.DEFAULT_SECURITY)
29        # Passphrase for this network (e.g. 'password123').
30        self.psk = serialized.get('psk', self.DEFAULT_PSK)
31        # Max delta in seconds between XMLRPC call to connect in the proxy
32        # and when shill finds the service.  Presumably callers call connect
33        # quickly after configuring the AP so that this is an approximation
34        # of how long takes shill to scan and discover new services.
35        self.discovery_timeout = serialized.get('discovery_timeout',
36                                                self.DEFAULT_DISCOVERY_TIMEOUT)
37        # Max delta in seconds between service creation and the transition to
38        # an associated state.
39        self.association_timeout = serialized.get(
40                'association_timeout',
41                self.DEFAULT_ASSOCIATION_TIMEOUT)
42        # Max delta in seconds between service association success and the
43        # transition to online.
44        self.configuration_timeout = serialized.get(
45                'configuration_timeout',
46                self.DEFAULT_CONFIGURATION_TIMEOUT)
47        # True iff this is a hidden network.
48        self.is_hidden = serialized.get('is_hidden', False)
49        # Passing false tells shill not to remember the configured service.
50        self.save_credentials = serialized.get('save_credentials', False)
51
52
53class AssociationResult(xmlrpc_datatypes.XmlRpcStruct):
54    """Describes the result of an association attempt."""
55
56    def __init__(self, serialized=None):
57        """Construct an AssociationResult.
58
59        @param serialized dict passed over the wire from XMLRPC server.
60
61        """
62        super(AssociationResult, self).__init__()
63        if serialized is None:
64            serialized = {}
65        # True iff we were successful in connecting to this WiFi network.
66        self.success = serialized.get('success', False)
67        # Describes how long it took to find and call connect on a network
68        # From the time we proxy is told to connect.  This includes scanning
69        # time.
70        self.discovery_time = serialized.get('discovery_time', -1.0)
71        # Describes how long it takes from the moment that we call connect to
72        # the moment we're fully associated with the BSS.  This includes wpa
73        # handshakes.
74        self.association_time = serialized.get('association_time', -1.0)
75        # Describes how long it takes from association till we have an IP
76        # address and mark the network as being either online or portalled.
77        self.configuration_time = serialized.get('configuration_time', -1.0)
78        # Holds a descriptive reason for why the negotiation failed when
79        # |successs| is False.  Undefined otherwise.
80        self.failure_reason = serialized.get('failure_reason', 'unknown')
81
82
83    @staticmethod
84    def from_dbus_proxy_output(raw):
85        """Factory for AssociationResult.
86
87        The object which knows how to talk over DBus to shill is not part of
88        autotest and as a result can't return a AssociationResult.  Instead,
89        it returns a similar looing tuple, which we'll parse.
90
91        @param raw tuple from ShillProxy.
92        @return AssociationResult parsed output from ShillProxy.
93
94        """
95        result = AssociationResult()
96        result.success = raw[0]
97        result.discovery_time = raw[1]
98        result.association_time = raw[2]
99        result.configuration_time = raw[3]
100        result.failure_reason = raw[4]
101        return result
102
103
104class BgscanConfiguration(xmlrpc_datatypes.XmlRpcStruct):
105    """Describes how to configure wpa_supplicant on a DUT."""
106
107    RESET_VALUE = 'default'
108
109    def __init__(self, serialized=None):
110        """Construct a BgscanConfiguration.
111
112        @param serialized dict passed over the wire from the autotest server.
113
114        """
115        super(BgscanConfiguration, self).__init__()
116        if serialized is None:
117            serialized = {}
118        self.interface = serialized.get('interface', None)
119        self.signal = serialized.get('signal', None)
120        self.short_interval = serialized.get('short_interval', None)
121        self.long_interval = serialized.get('long_interval', None)
122        self.method = serialized.get('method', None)
123
124
125    def set_auto_signal(signal_average, signal_offset=None, signal_noise=None):
126        """Set the signal threshold automatically from observed parameters.
127
128        @param signal_average int average signal level.
129        @param signal_offset int amount to adjust the average by.
130        @param signal_noise int amount of background noise observed.
131
132        """
133        signal = signal_average
134        if signal_offset:
135            signal += signal_offset
136        if signal_noise:
137            # Compensate for real noise vs standard estimate
138            signal -= 95 + signal_noise
139        logging.debug('Setting signal via auto configuration: '
140                      'avg=%d, offset=%r, noise=%r => signal=%d.',
141                      signal_average, signal_offset, signal_noise, signal)
142        self.signal = signal
143