xref: /aosp_15_r20/external/autotest/client/cros/cellular/pseudomodem/bearer.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Lint as: python2, python3
2# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import dbus
7
8import common
9from autotest_lib.client.cros.cellular.pseudomodem import dbus_std_ifaces
10from autotest_lib.client.cros.cellular.pseudomodem import utils
11
12from autotest_lib.client.cros.cellular import mm1_constants
13from autotest_lib.client.cros.cellular import net_interface
14
15class Bearer(dbus_std_ifaces.DBusProperties):
16    """
17    Fake implementation of the org.freedesktop.ModemManager1.Bearer
18    interface. Bearer objects are owned and managed by specific Modem objects.
19    A single Modem may expose one or more Bearer objects, which can then be
20    used to get the modem into connected state.
21
22    """
23
24    count = 0
25
26    def __init__(self, bus, properties, config=None):
27        self._active = False
28        self._bearer_props = properties
29        path = '%s/Bearer/%d' % (mm1_constants.MM1, Bearer.count)
30        Bearer.count += 1
31        dbus_std_ifaces.DBusProperties.__init__(self, path, bus, config)
32
33
34    def _InitializeProperties(self):
35        props = {
36            'Interface': net_interface.PseudoNetInterface.IFACE_NAME,
37            'Connected': dbus.types.Boolean(False),
38            'Suspended': dbus.types.Boolean(False),
39            'Properties': self._bearer_props
40        }
41        return { mm1_constants.I_BEARER: props }
42
43
44    def _AddProperty(self, property_key):
45        self._properties[mm1_constants.I_BEARER][property_key] = None
46
47
48    def _RemoveProperty(self, property_key):
49        try:
50            self._properties[mm1_constants.I_BEARER].pop(property_key)
51        except KeyError:
52            pass
53
54
55    def IsActive(self):
56        """
57        @returns: True, if the bearer is currently active.
58
59        """
60        return self._active
61
62
63    @property
64    def bearer_properties(self):
65        """
66        @returns: The current bearer properties that were set during a call to
67                org.freedesktop.ModemManager1.Modem.Simple.Connect.
68
69        """
70        return self._bearer_props
71
72
73    @utils.log_dbus_method()
74    @dbus.service.method(mm1_constants.I_BEARER)
75    def Connect(self):
76        """
77        Requests activation of a packet data connection with the network using
78        this bearer's properties. Upon successful activation, the modem can
79        send and receive packet data and, depending on the addressing
80        capability of the modem, a connection manager may need to start PPP,
81        perform DHCP, or assign the IP address returned by the modem to the
82        data interface. Upon successful return, the "Ip4Config" and/or
83        "Ip6Config" properties become valid and may contain IP configuration
84        information for the data interface associated with this bearer.
85
86        Since this is a mock implementation, this bearer will not establish
87        a real connection with the outside world. Since shill does not specify
88        IP addressing information to the bearer, we do not need to populate
89        these properties.
90
91        """
92        # Set the ip config property
93        ip_family = self._bearer_props.get('ip-type', None)
94        if ip_family and ip_family >= mm1_constants.MM_BEARER_IP_FAMILY_IPV6:
95            config_prop = 'Ip6Config'
96        else:
97            config_prop = 'Ip4Config'
98
99        self._AddProperty('Ip4Config')
100        self.Set(mm1_constants.I_BEARER, config_prop, {
101            'method': dbus.types.UInt32(mm1_constants.MM_BEARER_IP_METHOD_DHCP,
102                                        variant_level=1)
103        })
104        self._active = True
105        self.Set(mm1_constants.I_BEARER, 'Connected', dbus.types.Boolean(True))
106
107
108    @utils.log_dbus_method()
109    @dbus.service.method(mm1_constants.I_BEARER)
110    def Disconnect(self):
111        """
112        Disconnect and deactivate this packet data connection. In a real bearer,
113        any ongoing data session would be terminated and IP addresses would
114        become invalid when this method is called, however, the fake
115        implementation doesn't set the IP properties.
116
117        """
118        self._RemoveProperty('Ip4Config')
119        self._RemoveProperty('Ip6Config')
120        self._active = False
121        self.Set(mm1_constants.I_BEARER, 'Connected',
122                 dbus.types.Boolean(False))
123