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