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
5"""Configure cellular data emulation setup."""
6import time
7
8import common
9from autotest_lib.client.cros.cellular import base_station_8960
10from autotest_lib.client.cros.cellular import base_station_pxt
11from autotest_lib.client.cros.cellular import cellular_logging
12from autotest_lib.client.cros.cellular import ether_io_rf_switch
13from autotest_lib.client.cros.cellular import prologix_scpi_driver, scpi
14
15log = cellular_logging.SetupCellularLogging('emulator_config')
16
17
18class Error(Exception):
19    pass
20
21# TODO(byronk): Move this to the base_station_interface file or factory file
22def _BaseStationFactory(c, technology):
23    """Create a base station from a base station labconfig dictionary."""
24
25    adapter = c['gpib_adapter']
26    #TODO(byronk): get rid of the legacy single letter variable names
27    s = scpi.Scpi(
28        prologix_scpi_driver.PrologixScpiDriver(
29            hostname=adapter['address'],
30            port=adapter['ip_port'],
31            gpib_address=adapter['gpib_address']),
32        opc_on_stanza=True)
33    if technology == 'Technology:LTE':
34        return base_station_pxt.BaseStationPxt(s)
35    else:
36        return base_station_8960.BaseStation8960(s)
37
38
39# TODO(byronk): Make this a factory class, move to a better file
40def _CreateRfSwitch(config):
41    if 'rf_switch' not in config.cell:
42        return None
43    switch_config = config.cell['rf_switch']
44    if switch_config['type'] != 'ether_io':
45        raise KeyError('Could not configure switch of type %s' %
46                       switch_config['type'])
47    return ether_io_rf_switch.RfSwitch(switch_config['address'])
48
49
50def StartDefault(config, technology):
51    """Set up a base station and turn it on.  Return BS and verifier."""
52    # TODO(byronk): Stop using strings here. Config class? enum?
53    call_box_name_part = '8960'
54    if 'LTE' in technology:
55        call_box_name_part = 'pxt'
56
57    bs = None
58    # Find the first matching base station. Only a problem when we go to 3.
59    # TODO(byronk):This should be in the factory part
60    for cfg in config.cell['basestations']:
61        tp = cfg['type']
62        if call_box_name_part in tp:
63            bs_config = cfg
64            log.info('Using this call box: %s ' % cfg)
65            break
66    if bs_config is None:
67        raise Error(
68            'None of these base stations support %s:  %s' %
69            (technology, config.cell['basestations']))
70
71    # Match up to the legacy names. TODO(byronk) :fix this mess
72    #TODO(byronk): get rid of the legacy single letter variable names
73    c = cfg
74    bs = _BaseStationFactory(bs_config, technology)
75
76    rf_switch = _CreateRfSwitch(config)
77    if rf_switch:
78        port = config.get_rf_switch_port()
79        log.info(
80            'Changing switch port from %s to %s' % (rf_switch.Query(), port))
81        rf_switch.SelectPort(port)
82
83    with bs.checker_context:
84        bs.SetBsNetmaskV4(c['bs_netmask'])
85        bs.SetBsIpV4(*c['bs_addresses'])
86
87        bs.SetUeIpV4(*c['ue_rf_addresses'])
88        bs.SetUeDnsV4(*c['ue_dns_addresses'])
89
90        bs.SetTechnology(technology)
91        bs.SetPower(-40)
92        verifier = bs.GetAirStateVerifier()
93        bs.Start()
94
95    # TODO(rochberg):  Why does this seem to be necessary?
96    time.sleep(5)
97
98    return bs, verifier
99