1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
"""ModemManager mock template
This creates the expected methods and properties of the main
ModemManager object, but no devices. You can specify any property
such as DaemonVersion in "parameters".
"""
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.
__author__ = "Guido Günther"
__copyright__ = "2024 The Phosh Developers"
import dbus
from dbusmock import MOCK_IFACE, OBJECT_MANAGER_IFACE, mockobject
BUS_NAME = "org.freedesktop.ModemManager1"
MAIN_OBJ = "/org/freedesktop/ModemManager1"
MAIN_IFACE = "org.freedesktop.ModemManager1"
SYSTEM_BUS = True
IS_OBJECT_MANAGER = False
MODEM_IFACE = "org.freedesktop.ModemManager1.Modem"
MODEM_3GPP_IFACE = "org.freedesktop.ModemManager1.Modem.Modem3gpp"
MODEM_CELL_BROADCAST_IFACE = "org.freedesktop.ModemManager1.Modem.CellBroadcast"
MODEM_VOICE_IFACE = "org.freedesktop.ModemManager1.Modem.Voice"
SIM_IFACE = "org.freedesktop.ModemManager1.Sim"
CBM_IFACE = "org.freedesktop.ModemManager1.Cbm"
SIMPLE_MODEM_PATH = "/org/freedesktop/ModemManager1/Modems/8"
class MMModemMode:
"""
See
https://www.freedesktop.org/software/ModemManager/doc/latest/ModemManager/ModemManager-Flags-and-Enumerations.html#MMModemMode
"""
MODE_NONE = 0
MODE_CS = 1 << 0
MODE_2G = 1 << 1
MODE_3G = 1 << 2
MODE_4G = 1 << 3
MODE_5G = 1 << 4
class MMModemState:
"""
See
https://www.freedesktop.org/software/ModemManager/doc/latest/ModemManager/ModemManager-Flags-and-Enumerations.html#MMModemState
"""
STATE_FAILED = -1
STATE_UNKNOWN = 0
STATE_INITIALIZING = 1
STATE_LOCKED = 2
STATE_DISABLED = 3
STATE_DISABLING = 4
STATE_ENABLING = 5
STATE_ENABLED = 6
STATE_SEARCHING = 7
STATE_REGISTERED = 8
STATE_DISCONNECTING = 9
STATE_CONNECTING = 10
STATE_CONNECTED = 11
class MMModemPowerState:
"""
See
https://www.freedesktop.org/software/ModemManager/doc/latest/ModemManager/ModemManager-Flags-and-Enumerations.html#MMModemPowerState
"""
POWER_STATE_UNKNOWN = 0
POWER_STATE_OFF = 1
POWER_STATE_LOW = 2
POWER_STATE_ON = 3
class MMModemAccesssTechnology:
"""
See
https://www.freedesktop.org/software/ModemManager/doc/latest/ModemManager/ModemManager-Flags-and-Enumerations.html#MMModemAccessTechnology
"""
ACCESS_TECHNOLOGY_UNKNOWN = 0
ACCESS_TECHNOLOGY_POTS = 1 << 0
ACCESS_TECHNOLOGY_GSM = 1 << 1
ACCESS_TECHNOLOGY_GSM_COMPACT = 1 << 2
ACCESS_TECHNOLOGY_GPRS = 1 << 3
ACCESS_TECHNOLOGY_EDGE = 1 << 4
ACCESS_TECHNOLOGY_UMTS = 1 << 5
ACCESS_TECHNOLOGY_HSDPA = 1 << 6
ACCESS_TECHNOLOGY_HSUPA = 1 << 7
ACCESS_TECHNOLOGY_HSPA = 1 << 8
ACCESS_TECHNOLOGY_HSPA_PLUS = 1 << 9
ACCESS_TECHNOLOGY_1XRTT = 1 << 10
ACCESS_TECHNOLOGY_EVDO0 = 1 << 11
ACCESS_TECHNOLOGY_EVDOA = 1 << 12
ACCESS_TECHNOLOGY_EVDOB = 1 << 13
ACCESS_TECHNOLOGY_LTE = 1 << 14
ACCESS_TECHNOLOGY_5GNR = 1 << 15
ACCESS_TECHNOLOGY_LTE_CAT_M = 1 << 16
ACCESS_TECHNOLOGY_LTE_NB_IOT = 1 << 17
def load(mock, parameters):
methods = [
("ScanDevices", "", "", ""),
]
props = dbus.Dictionary(
{
"Version": parameters.get("DaemonVersion", "1.22"),
},
signature="sv",
)
mock.AddMethods(MAIN_IFACE, methods)
mock.AddProperties(MAIN_IFACE, props)
cond = "k != '/'" if mock.path == "/" else f"k.startswith('{mock.path}/Modems/')"
code = f"ret = {{dbus.ObjectPath(k): objects[k].props for k in objects.keys() if {cond} }}"
mock.AddMethod(OBJECT_MANAGER_IFACE, "GetManagedObjects", "", "a{oa{sa{sv}}}", code)
def listCbm(_):
paths = []
for path in mockobject.objects:
if path.startswith("/org/freedesktop/ModemManager1/Cbm/"):
paths.append(dbus.ObjectPath(path))
return paths
def deleteCbm(self, cbm_path):
obj = mockobject.objects.get(cbm_path)
if obj is None:
return
modem_obj = mockobject.objects[SIMPLE_MODEM_PATH]
self.RemoveObject(cbm_path)
modem_obj.EmitSignal(
MODEM_CELL_BROADCAST_IFACE,
"Deleted",
"o",
[
cbm_path,
],
)
def setChannels(_self, channels):
modem_obj = mockobject.objects[SIMPLE_MODEM_PATH]
modem_obj.UpdateProperties(
MODEM_CELL_BROADCAST_IFACE,
{
"Channels": dbus.Array(channels),
},
)
@dbus.service.method(MOCK_IFACE, in_signature="", out_signature="ss")
def AddSimpleModem(self):
"""Convenience method to add a simple Modem object
Please note that this does not set any global properties.
Returns the new object path.
"""
modem_path = SIMPLE_MODEM_PATH
sim_path = "/org/freedesktop/ModemManager1/SIM/2"
manager = mockobject.objects[MAIN_OBJ]
modem_props = {
"State": dbus.Int32(MMModemState.STATE_ENABLED),
"Model": dbus.String("E1750"),
"Revision": dbus.String("11.126.08.01.00"),
"AccessTechnologies": dbus.UInt32(MMModemAccesssTechnology.ACCESS_TECHNOLOGY_LTE),
"PowerState": dbus.UInt32(MMModemPowerState.POWER_STATE_ON),
"UnlockRequired": dbus.UInt32(0),
"UnlockRetries": dbus.Dictionary([], signature="uu"),
"CurrentModes": dbus.Struct(
(dbus.UInt32(MMModemMode.MODE_4G), dbus.UInt32(MMModemMode.MODE_4G)), signature="(uu)"
),
"SignalQuality": dbus.Struct(
(dbus.UInt32(70), dbus.Boolean(True)),
),
"Sim": dbus.ObjectPath(sim_path),
"SupportedModes": [
(dbus.UInt32(MMModemMode.MODE_4G), dbus.UInt32(MMModemMode.MODE_4G)),
(dbus.UInt32(MMModemMode.MODE_3G | MMModemMode.MODE_2G), dbus.UInt32(MMModemMode.MODE_3G)),
],
"SupportedBands": [dbus.UInt32(0)],
}
self.AddObject(modem_path, MODEM_IFACE, modem_props, [])
modem_3gpp_props = {
"Imei": dbus.String("doesnotmatter"),
"OperatorName": dbus.String("TheOperator"),
"OperatorCode": dbus.String("00101"),
"Pco": dbus.Array([], signature="(ubay)"),
}
modem = mockobject.objects[modem_path]
modem.AddProperties(MODEM_3GPP_IFACE, modem_3gpp_props)
modem_cell_broadcast_props = {
"Channels": dbus.Array([], signature="(uu)"),
"CellBroadcasts": dbus.Array([], signature="o"),
}
modem_cell_broadcast_methods = [
("List", "", "ao", listCbm),
("Delete", "o", "", deleteCbm),
("SetChannels", "a(uu)", "", setChannels),
]
modem.AddProperties(MODEM_CELL_BROADCAST_IFACE, modem_cell_broadcast_props)
modem.AddMethods(MODEM_CELL_BROADCAST_IFACE, modem_cell_broadcast_methods)
modem_voice_props = {
"Calls": dbus.Array([], signature="o"),
"EmergencyOnly": False,
}
modem.call_waiting = False
modem_voice_methods = [
("CallWaitingQuery", "", "b", "ret = self.call_waiting"),
("CallWaitingSetup", "b", "", "self.call_waiting = args[0]"),
]
modem.AddProperties(MODEM_VOICE_IFACE, modem_voice_props)
modem.AddMethods(MODEM_VOICE_IFACE, modem_voice_methods)
manager.EmitSignal(
OBJECT_MANAGER_IFACE,
"InterfacesAdded",
"oa{sa{sv}}",
[
dbus.ObjectPath(modem_path),
{
MODEM_IFACE: modem_props,
MODEM_3GPP_IFACE: modem_3gpp_props,
MODEM_CELL_BROADCAST_IFACE: modem_cell_broadcast_props,
MODEM_VOICE_IFACE: modem_voice_props,
},
],
)
sim_props = {
"Active": dbus.Boolean(True),
"Imsi": dbus.String("doesnotmatter"),
"PreferredNetworks": dbus.Array([], signature="(su)"),
}
self.AddObject(sim_path, SIM_IFACE, sim_props, [])
return (modem_path, sim_path)
@dbus.service.method(MOCK_IFACE, in_signature="uus", out_signature="s")
def AddCbm(self, state, channel, text):
"""Convenience method to add a cell broadcast message
Returns the new object path.
"""
n = 1
while mockobject.objects.get(f"/org/freedesktop/ModemManager1/Cbm/{n}") is not None:
n += 1
cbm_path = f"/org/freedesktop/ModemManager1/Cbm/{n}"
cbm_props = {
"State": dbus.UInt32(state),
"Channel": dbus.UInt32(channel),
"Text": dbus.String(text),
"MessageCode": dbus.UInt32(0),
"Update": dbus.UInt32(0),
}
self.AddObject(cbm_path, CBM_IFACE, cbm_props, [])
modem_obj = mockobject.objects[SIMPLE_MODEM_PATH]
paths = listCbm(self)
modem_obj.UpdateProperties(
MODEM_CELL_BROADCAST_IFACE,
{
"CellBroadcasts": dbus.Array(paths),
},
)
modem_obj.EmitSignal(
MODEM_CELL_BROADCAST_IFACE,
"Added",
"o",
[
dbus.ObjectPath(cbm_path),
],
)
return cbm_path
|