| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
/* |
| 4 |
* 1. ModemManager (MM) integration consists of two big parts: things |
| 5 |
* we do on the networkd start and what we do during the run-time. |
| 6 |
* |
| 7 |
* 2. Initialization phase |
| 8 |
* 2.1. Wait for networkd to connect to D-Bus |
| 9 |
* 2.2. Setup D-Bus handlers for the essential signals: |
| 10 |
* - /org/freedesktop/DBus, org.freedesktop.DBus, NameOwnerChanged - to track MM service availability |
| 11 |
* - /org/freedesktop/ModemManager1, org.freedesktop.DBus.ObjectManager Interfaces{Added|Removed} - |
| 12 |
* to track modem plug/unplug |
| 13 |
* - /org/freedesktop/ModemManager1/Bearer org.freedesktop.DBus.Properties PropertiesChanged |
| 14 |
* to track bearers |
| 15 |
* 2.3. Check if MM service is yet available: for that call /org/freedesktop/DBus, org.freedesktop.DBus |
| 16 |
* ListNames method and see if MM is available. If it is not, then wait for the NameOwnerChanged |
| 17 |
* signal and see when it is; finish initialization phase. |
| 18 |
* 2.4. If MM is available - enumerate modems, see p.4. |
| 19 |
* 2.5. Finish initialization phase. |
| 20 |
* |
| 21 |
* 3. Run-time |
| 22 |
* 3.1. During the run-time we track MM service availability. When it is gone we remove all the modems |
| 23 |
* and bearers. |
| 24 |
* 3.2. When MM is connected we do modem enumeration to get in sync with their current state. |
| 25 |
* 3.3. If a modem was removed we also remove all its bearers. |
| 26 |
* 3.4. If a modem was added we try to start a simple connect. |
| 27 |
* 3.5. If connection was interrupted, e.g. modem changed its network connection from connected state |
| 28 |
* we start an automatic reconnect. |
| 29 |
* |
| 30 |
* 4. Modem enumeration |
| 31 |
* 4.1. Modem enumeration is done by calling GetManagedObjects. |
| 32 |
* 4.2. By receiving managed objects we try to instantiate all new modems found. |
| 33 |
* 4.3. For that we inspect all bearers available for that modem and add all new bearers found. |
| 34 |
* 4.4. We also read modem ports to detect WWAN interface name assigned to this modem, e.g. "wwan0" etc. |
| 35 |
* N.B. As we only get the interface name known that late and the corresponding .network file was |
| 36 |
* already used by the networkd to match interfaces etc. it is not possible |
| 37 |
* to do things like matching APN to .network and so on. |
| 38 |
* |
| 39 |
* 5. Simple (re)connect |
| 40 |
* 5.1. Connection is done by calling org.freedesktop.ModemManager1.Modem.Simple Connect method for |
| 41 |
* the relevant modem. |
| 42 |
* 5.2. It is possible that at the time of connect the operation may fail. For that reason and to ensure |
| 43 |
* we are always connected we employ a periodic timer which will re-try connection hoping it will |
| 44 |
* be successful this time or when modem has recovered after an error state and so on. |
| 45 |
* 5.3. networkd will automatically start reconnection if any external entity disconnects modem from |
| 46 |
* the network. |
| 47 |
*/ |
| 48 |
|
| 49 |
#include "af-list.h" |
| 50 |
#include "alloc-util.h" |
| 51 |
#include "bus-error.h" |
| 52 |
#include "bus-internal.h" |
| 53 |
#include "bus-map-properties.h" |
| 54 |
#include "bus-message.h" |
| 55 |
#include "bus-util.h" |
| 56 |
#include "event-util.h" |
| 57 |
#include "hashmap.h" |
| 58 |
#include "networkd-link.h" |
| 59 |
#include "networkd-manager.h" |
| 60 |
#include "networkd-wwan.h" |
| 61 |
#include "networkd-wwan-bus.h" |
| 62 |
#include "string-util.h" |
| 63 |
#include "strv.h" |
| 64 |
|
| 65 |
#define RECONNECT_TIMEOUT_USEC (30 * USEC_PER_SEC) |
| 66 |
|
| 67 |
static const char * const modem_state_failed_reason_str[__MM_MODEM_STATE_FAILED_REASON_MAX] = { |
| 68 |
[MM_MODEM_STATE_FAILED_REASON_NONE] = "No error", |
| 69 |
[MM_MODEM_STATE_FAILED_REASON_UNKNOWN] = "Unknown error", |
| 70 |
[MM_MODEM_STATE_FAILED_REASON_SIM_MISSING] = "SIM is required, but missing", |
| 71 |
[MM_MODEM_STATE_FAILED_REASON_SIM_ERROR] = "SIM is available, but unusable", |
| 72 |
[MM_MODEM_STATE_FAILED_REASON_UNKNOWN_CAPABILITIES] = "Unknown modem capabilities", |
| 73 |
[MM_MODEM_STATE_FAILED_REASON_ESIM_WITHOUT_PROFILES] = "eSIM is not initialized", |
| 74 |
}; |
| 75 |
|
| 76 |
static int map_name( |
| 77 |
sd_bus *bus, |
| 78 |
const char *member, |
| 79 |
sd_bus_message *m, |
| 80 |
sd_bus_error *error, |
| 81 |
void *userdata) { |
| 82 |
|
| 83 |
Bearer *b = ASSERT_PTR(userdata); |
| 84 |
const char *s; |
| 85 |
int r; |
| 86 |
|
| 87 |
assert(m); |
| 88 |
|
| 89 |
/* |
| 90 |
* If name is already set - do not wipe it on disconnect, so we can work with link and other code which |
| 91 |
* relies on the interface name. |
| 92 |
*/ |
| 93 |
r = sd_bus_message_read_basic(m, 's', &s); |
| 94 |
if (r < 0) |
| 95 |
return r; |
| 96 |
|
| 97 |
if (!isempty(b->name)) |
| 98 |
return 0; |
| 99 |
|
| 100 |
return bearer_set_name(b, s); |
| 101 |
} |
| 102 |
|
| 103 |
static int map_dns( |
| 104 |
sd_bus *bus, |
| 105 |
const char *member, |
| 106 |
sd_bus_message *m, |
| 107 |
sd_bus_error *error, |
| 108 |
void *userdata) { |
| 109 |
|
| 110 |
Bearer *b = ASSERT_PTR(userdata); |
| 111 |
int r; |
| 112 |
|
| 113 |
assert(m); |
| 114 |
|
| 115 |
const char *s; |
| 116 |
r = sd_bus_message_read_basic(m, 's', &s); |
| 117 |
if (r < 0) |
| 118 |
return r; |
| 119 |
|
| 120 |
_cleanup_free_ struct in_addr_full *a = NULL; |
| 121 |
r = in_addr_full_new_from_string(s, &a); |
| 122 |
if (r < 0) |
| 123 |
return r; |
| 124 |
|
| 125 |
if (!GREEDY_REALLOC(b->dns, b->n_dns + 1)) |
| 126 |
return -ENOMEM; |
| 127 |
|
| 128 |
b->dns[b->n_dns++] = TAKE_PTR(a); |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
static int map_in_addr( |
| 133 |
sd_bus *bus, |
| 134 |
const char *member, |
| 135 |
sd_bus_message *m, |
| 136 |
sd_bus_error *error, |
| 137 |
void *userdata, |
| 138 |
int family) { |
| 139 |
|
| 140 |
union in_addr_union *addr = ASSERT_PTR(userdata); |
| 141 |
const char *s; |
| 142 |
int r; |
| 143 |
|
| 144 |
assert(m); |
| 145 |
assert(IN_SET(family, AF_INET, AF_INET6)); |
| 146 |
|
| 147 |
r = sd_bus_message_read_basic(m, 's', &s); |
| 148 |
if (r < 0) |
| 149 |
return r; |
| 150 |
|
| 151 |
return in_addr_from_string(family, s, addr); |
| 152 |
} |
| 153 |
|
| 154 |
static int map_in4( |
| 155 |
sd_bus *bus, |
| 156 |
const char *member, |
| 157 |
sd_bus_message *m, |
| 158 |
sd_bus_error *error, |
| 159 |
void *userdata) { |
| 160 |
|
| 161 |
return map_in_addr(bus, member, m, error, userdata, AF_INET); |
| 162 |
} |
| 163 |
|
| 164 |
static int map_in6( |
| 165 |
sd_bus *bus, |
| 166 |
const char *member, |
| 167 |
sd_bus_message *m, |
| 168 |
sd_bus_error *error, |
| 169 |
void *userdata) { |
| 170 |
|
| 171 |
return map_in_addr(bus, member, m, error, userdata, AF_INET6); |
| 172 |
} |
| 173 |
|
| 174 |
static int map_prefixlen( |
| 175 |
sd_bus *bus, |
| 176 |
const char *member, |
| 177 |
sd_bus_message *m, |
| 178 |
sd_bus_error *error, |
| 179 |
void *userdata, |
| 180 |
int family) { |
| 181 |
|
| 182 |
unsigned *prefixlen = ASSERT_PTR(userdata); |
| 183 |
unsigned p; |
| 184 |
int r; |
| 185 |
|
| 186 |
assert(m); |
| 187 |
|
| 188 |
r = sd_bus_message_read_basic(m, 'u', &p); |
| 189 |
if (r < 0) |
| 190 |
return r; |
| 191 |
|
| 192 |
if (p > FAMILY_ADDRESS_SIZE(family) * 8) |
| 193 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), |
| 194 |
"Bearer has invalid prefix length %u for %s address, ignoring.", |
| 195 |
p, af_to_ipv4_ipv6(family)); |
| 196 |
|
| 197 |
*prefixlen = p; |
| 198 |
|
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
static int map_prefixlen4( |
| 203 |
sd_bus *bus, |
| 204 |
const char *member, |
| 205 |
sd_bus_message *m, |
| 206 |
sd_bus_error *error, |
| 207 |
void *userdata) { |
| 208 |
|
| 209 |
return map_prefixlen(bus, member, m, error, userdata, AF_INET); |
| 210 |
} |
| 211 |
|
| 212 |
static int map_prefixlen6( |
| 213 |
sd_bus *bus, |
| 214 |
const char *member, |
| 215 |
sd_bus_message *m, |
| 216 |
sd_bus_error *error, |
| 217 |
void *userdata) { |
| 218 |
|
| 219 |
return map_prefixlen(bus, member, m, error, userdata, AF_INET6); |
| 220 |
} |
| 221 |
|
| 222 |
static int map_ip4_config( |
| 223 |
sd_bus *bus, |
| 224 |
const char *member, |
| 225 |
sd_bus_message *m, |
| 226 |
sd_bus_error *error, |
| 227 |
void *userdata) { |
| 228 |
|
| 229 |
static const struct bus_properties_map map[] = { |
| 230 |
{ "method", "u", NULL, offsetof(Bearer, ip4_method) }, |
| 231 |
{ "address", "s", map_in4, offsetof(Bearer, ip4_address) }, |
| 232 |
{ "prefix", "u", map_prefixlen4, offsetof(Bearer, ip4_prefixlen) }, |
| 233 |
{ "dns1", "s", map_dns, 0, }, |
| 234 |
{ "dns2", "s", map_dns, 0, }, |
| 235 |
{ "dns3", "s", map_dns, 0, }, |
| 236 |
{ "gateway", "s", map_in4, offsetof(Bearer, ip4_gateway) }, |
| 237 |
{ "mtu", "u", NULL, offsetof(Bearer, ip4_mtu) }, |
| 238 |
{} |
| 239 |
}; |
| 240 |
Bearer *b = ASSERT_PTR(userdata); |
| 241 |
|
| 242 |
/* |
| 243 |
* The "Ip4Config" property: if the bearer was configured for IPv4 addressing, upon activation this |
| 244 |
* property contains the addressing details for assignment to the data interface. |
| 245 |
* We may have both IPv4 and IPv6 configured. |
| 246 |
*/ |
| 247 |
b->ip_type |= ADDRESS_FAMILY_IPV4; |
| 248 |
|
| 249 |
return bus_message_map_all_properties(m, map, /* flags= */ 0, error, userdata); |
| 250 |
} |
| 251 |
|
| 252 |
static int map_ip6_config( |
| 253 |
sd_bus *bus, |
| 254 |
const char *member, |
| 255 |
sd_bus_message *m, |
| 256 |
sd_bus_error *error, |
| 257 |
void *userdata) { |
| 258 |
|
| 259 |
static const struct bus_properties_map map[] = { |
| 260 |
{ "method", "u", NULL, offsetof(Bearer, ip6_method) }, |
| 261 |
{ "address", "s", map_in6, offsetof(Bearer, ip6_address) }, |
| 262 |
{ "prefix", "u", map_prefixlen6, offsetof(Bearer, ip6_prefixlen) }, |
| 263 |
{ "dns1", "s", map_dns, 0, }, |
| 264 |
{ "dns2", "s", map_dns, 0, }, |
| 265 |
{ "dns3", "s", map_dns, 0, }, |
| 266 |
{ "gateway", "s", map_in6, offsetof(Bearer, ip6_gateway) }, |
| 267 |
{ "mtu", "u", NULL, offsetof(Bearer, ip6_mtu) }, |
| 268 |
{} |
| 269 |
}; |
| 270 |
Bearer *b = ASSERT_PTR(userdata); |
| 271 |
|
| 272 |
/* |
| 273 |
* The "Ip6Config" property: if the bearer was configured for IPv6 addressing, upon activation this |
| 274 |
* property contains the addressing details for assignment to the data interface. |
| 275 |
* We may have both IPv4 and IPv6 configured. |
| 276 |
*/ |
| 277 |
b->ip_type |= ADDRESS_FAMILY_IPV6; |
| 278 |
|
| 279 |
return bus_message_map_all_properties(m, map, /* flags= */ 0, error, userdata); |
| 280 |
} |
| 281 |
|
| 282 |
static int map_properties( |
| 283 |
sd_bus *bus, |
| 284 |
const char *member, |
| 285 |
sd_bus_message *m, |
| 286 |
sd_bus_error *error, |
| 287 |
void *userdata) { |
| 288 |
|
| 289 |
static const struct bus_properties_map map[] = { |
| 290 |
{ "apn", "s", NULL, offsetof(Bearer, apn) }, |
| 291 |
{} |
| 292 |
}; |
| 293 |
|
| 294 |
return bus_message_map_all_properties(m, map, BUS_MAP_STRDUP, error, userdata); |
| 295 |
} |
| 296 |
|
| 297 |
static int bus_message_contains_properties( |
| 298 |
sd_bus_message *m, |
| 299 |
const struct bus_properties_map *map, |
| 300 |
sd_bus_error *error) { |
| 301 |
|
| 302 |
unsigned found_cnt; |
| 303 |
int r; |
| 304 |
|
| 305 |
assert(m); |
| 306 |
assert(map); |
| 307 |
|
| 308 |
found_cnt = 0; |
| 309 |
|
| 310 |
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}"); |
| 311 |
if (r < 0) |
| 312 |
return bus_log_parse_error_debug(r); |
| 313 |
|
| 314 |
while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) { |
| 315 |
const struct bus_properties_map *prop = NULL; |
| 316 |
const char *member; |
| 317 |
|
| 318 |
r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member); |
| 319 |
if (r < 0) |
| 320 |
return bus_log_parse_error_debug(r); |
| 321 |
|
| 322 |
for (unsigned i = 0; map[i].member; i++) |
| 323 |
if (streq(map[i].member, member)) { |
| 324 |
prop = &map[i]; |
| 325 |
break; |
| 326 |
} |
| 327 |
|
| 328 |
r = sd_bus_message_skip(m, "v"); |
| 329 |
if (r < 0) |
| 330 |
return bus_log_parse_error_debug(r); |
| 331 |
if (prop) |
| 332 |
found_cnt++; |
| 333 |
|
| 334 |
r = sd_bus_message_exit_container(m); |
| 335 |
if (r < 0) |
| 336 |
return bus_log_parse_error_debug(r); |
| 337 |
} |
| 338 |
if (r < 0) |
| 339 |
return bus_log_parse_error_debug(r); |
| 340 |
|
| 341 |
r = sd_bus_message_exit_container(m); |
| 342 |
if (r < 0) |
| 343 |
return bus_log_parse_error_debug(r); |
| 344 |
|
| 345 |
return found_cnt; |
| 346 |
} |
| 347 |
|
| 348 |
static int bearer_get_all_handler(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
| 349 |
static const struct bus_properties_map map[] = { |
| 350 |
{ "Interface", "s", map_name, 0, }, |
| 351 |
{ "Connected", "b", NULL, offsetof(Bearer, connected) }, |
| 352 |
{ "Ip4Config", "a{sv}", map_ip4_config, 0, }, |
| 353 |
{ "Ip6Config", "a{sv}", map_ip6_config, 0, }, |
| 354 |
{ "Properties", "a{sv}", map_properties, 0, }, |
| 355 |
{} |
| 356 |
}; |
| 357 |
|
| 358 |
Bearer *b = ASSERT_PTR(userdata); |
| 359 |
const sd_bus_error *e; |
| 360 |
int r; |
| 361 |
|
| 362 |
assert(message); |
| 363 |
|
| 364 |
b->slot_getall = sd_bus_slot_unref(b->slot_getall); |
| 365 |
|
| 366 |
e = sd_bus_message_get_error(message); |
| 367 |
if (e) { |
| 368 |
bool removed = false; |
| 369 |
|
| 370 |
if (sd_bus_error_has_name(e, SD_BUS_ERROR_UNKNOWN_METHOD)) |
| 371 |
/* The path is already removed? */ |
| 372 |
removed = true; |
| 373 |
|
| 374 |
r = sd_bus_error_get_errno(e); |
| 375 |
log_full_errno(removed ? LOG_DEBUG : LOG_WARNING, r, |
| 376 |
"Could not get properties of bearer \"%s\": %s", |
| 377 |
b->path, bus_error_message(e, r)); |
| 378 |
|
| 379 |
bearer_drop(b); |
| 380 |
return 0; |
| 381 |
} |
| 382 |
|
| 383 |
/* skip name: string "org.freedesktop.ModemManager1.Bearer" */ |
| 384 |
r = sd_bus_message_skip(message, "s"); |
| 385 |
if (r < 0) |
| 386 |
return log_warning_errno(r, "Failed while parsing properties of bearer \"%s\": %s", |
| 387 |
b->path, bus_error_message(ret_error, r)); |
| 388 |
|
| 389 |
r = bus_message_contains_properties(message, map, ret_error); |
| 390 |
if (r < 0) |
| 391 |
return log_warning_errno(r, "Failed to check properties of bearer \"%s\": %s", |
| 392 |
b->path, bus_error_message(ret_error, r)); |
| 393 |
|
| 394 |
/* |
| 395 |
* We do not want to update link status on properties change which come more or less frequently |
| 396 |
* and do not involve link state change, e.g. we do not want to bearer_update_link on Rx/Tx counters |
| 397 |
* change. So, see if this callback was called with the changes we want to track. |
| 398 |
*/ |
| 399 |
if (r == 0) |
| 400 |
return 0; |
| 401 |
|
| 402 |
r = sd_bus_message_rewind(message, true); |
| 403 |
if (r < 0) |
| 404 |
return log_warning_errno(r, "Failed to rewind properties of bearer \"%s\"", b->path); |
| 405 |
/* skip name: string "org.freedesktop.ModemManager1.Bearer" */ |
| 406 |
r = sd_bus_message_skip(message, "s"); |
| 407 |
if (r < 0) |
| 408 |
return log_warning_errno(r, "Failed while parsing properties of bearer \"%s\": %s", |
| 409 |
b->path, bus_error_message(ret_error, r)); |
| 410 |
|
| 411 |
r = bus_message_map_all_properties(message, map, BUS_MAP_BOOLEAN_AS_BOOL, ret_error, b); |
| 412 |
if (r < 0) |
| 413 |
return log_warning_errno(r, "Failed to parse properties of bearer \"%s\": %s", |
| 414 |
b->path, bus_error_message(ret_error, r)); |
| 415 |
|
| 416 |
if (b->name) |
| 417 |
log_info("%s: ModemManager announces %s %s is%s connected.", |
| 418 |
b->name, b->modem->manufacturer, b->modem->model, |
| 419 |
b->connected ? "" : " not"); |
| 420 |
|
| 421 |
if (b->connected) |
| 422 |
b->modem->reconnect_state = MODEM_RECONNECT_DONE; |
| 423 |
|
| 424 |
return bearer_update_link(b); |
| 425 |
} |
| 426 |
|
| 427 |
static int bearer_initialize(Bearer *b) { |
| 428 |
int r; |
| 429 |
|
| 430 |
assert(b); |
| 431 |
assert(b->modem); |
| 432 |
assert(b->modem->manager); |
| 433 |
assert(sd_bus_is_ready(b->modem->manager->bus) > 0); |
| 434 |
assert(b->path); |
| 435 |
|
| 436 |
b->slot_getall = sd_bus_slot_unref(b->slot_getall); |
| 437 |
|
| 438 |
r = sd_bus_call_method_async( |
| 439 |
b->modem->manager->bus, |
| 440 |
&b->slot_getall, |
| 441 |
"org.freedesktop.ModemManager1", |
| 442 |
b->path, |
| 443 |
"org.freedesktop.DBus.Properties", |
| 444 |
"GetAll", |
| 445 |
bearer_get_all_handler, |
| 446 |
b, "s", "org.freedesktop.ModemManager1.Bearer"); |
| 447 |
if (r < 0) |
| 448 |
return log_warning_errno(r, "Could not get properties of bearer \"%s\": %m", b->path); |
| 449 |
|
| 450 |
return 0; |
| 451 |
} |
| 452 |
|
| 453 |
static int bearer_new_and_initialize(Modem *modem, const char *path) { |
| 454 |
_cleanup_(bearer_freep) Bearer *b = NULL; |
| 455 |
int r; |
| 456 |
|
| 457 |
assert(modem); |
| 458 |
assert(modem->manager); |
| 459 |
assert(path); |
| 460 |
|
| 461 |
r = bearer_new(modem, path, &b); |
| 462 |
if (r < 0) { |
| 463 |
if (r == -EEXIST) |
| 464 |
return 0; |
| 465 |
return log_warning_errno(r, "Failed to allocate new bearer \"%s\": %m", path); |
| 466 |
} |
| 467 |
|
| 468 |
r = bearer_initialize(b); |
| 469 |
if (r < 0) |
| 470 |
return r; |
| 471 |
|
| 472 |
TAKE_PTR(b); |
| 473 |
return 0; |
| 474 |
} |
| 475 |
|
| 476 |
static int modem_connect_handler(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
| 477 |
Modem *modem = ASSERT_PTR(userdata); |
| 478 |
int r; |
| 479 |
|
| 480 |
assert(message); |
| 481 |
|
| 482 |
modem->slot_connect = sd_bus_slot_unref(modem->slot_connect); |
| 483 |
|
| 484 |
const sd_bus_error *e = sd_bus_message_get_error(message); |
| 485 |
if (e) { |
| 486 |
r = sd_bus_error_get_errno(e); |
| 487 |
log_error_errno(r, |
| 488 |
"Could not connect modem %s %s: %s", |
| 489 |
modem->manufacturer, modem->model, |
| 490 |
bus_error_message(e, r)); |
| 491 |
|
| 492 |
modem->reconnect_state = MODEM_RECONNECT_WAITING; |
| 493 |
return 0; |
| 494 |
} |
| 495 |
|
| 496 |
const char *new_bearer; |
| 497 |
r = sd_bus_message_read(message, "o", &new_bearer); |
| 498 |
if (r < 0) { |
| 499 |
log_debug_errno(r, "Received connection of %s %s from ModemManager, but bearer not found, ignoring: %m", |
| 500 |
modem->manufacturer, modem->model); |
| 501 |
return 0; |
| 502 |
} |
| 503 |
|
| 504 |
log_debug("ModemManager: %s %s connected, bearer is at %s", |
| 505 |
modem->manufacturer, modem->model, new_bearer); |
| 506 |
return 0; |
| 507 |
} |
| 508 |
|
| 509 |
static int bus_call_method_async_props( |
| 510 |
sd_bus *bus, |
| 511 |
sd_bus_slot **slot, |
| 512 |
const char *destination, |
| 513 |
const char *path, |
| 514 |
const char *interface, |
| 515 |
const char *member, |
| 516 |
sd_bus_message_handler_t callback, |
| 517 |
void *userdata, |
| 518 |
Link *link) { |
| 519 |
|
| 520 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 521 |
Network *network = ASSERT_PTR(ASSERT_PTR(link)->network); |
| 522 |
int r; |
| 523 |
|
| 524 |
assert(bus); |
| 525 |
|
| 526 |
r = sd_bus_message_new_method_call(bus, &m, destination, path, interface, member); |
| 527 |
if (r < 0) |
| 528 |
return bus_log_create_error(r); |
| 529 |
|
| 530 |
r = sd_bus_message_open_container(m, 'a', "{sv}"); |
| 531 |
if (r < 0) |
| 532 |
return bus_log_create_error(r); |
| 533 |
|
| 534 |
if (network->mm_apn) { |
| 535 |
r = sd_bus_message_append(m, "{sv}", "apn", "s", network->mm_apn); |
| 536 |
if (r < 0) |
| 537 |
return bus_log_create_error(r); |
| 538 |
} |
| 539 |
|
| 540 |
r = sd_bus_message_append(m, "{sv}", "allow-roaming", "b", network->mm_allow_roaming); |
| 541 |
if (r < 0) |
| 542 |
return bus_log_create_error(r); |
| 543 |
|
| 544 |
if (network->mm_allowed_auth != MM_BEARER_ALLOWED_AUTH_UNKNOWN) { |
| 545 |
r = sd_bus_message_append(m, "{sv}", "allowed-auth", "u", (uint32_t) network->mm_allowed_auth); |
| 546 |
if (r < 0) |
| 547 |
return bus_log_create_error(r); |
| 548 |
} |
| 549 |
|
| 550 |
if (network->mm_ip_family != MM_BEARER_IP_FAMILY_NONE) { |
| 551 |
r = sd_bus_message_append(m, "{sv}", "ip-type", "u", (uint32_t) network->mm_ip_family); |
| 552 |
if (r < 0) |
| 553 |
return bus_log_create_error(r); |
| 554 |
} |
| 555 |
|
| 556 |
if (network->mm_operator_id) { |
| 557 |
r = sd_bus_message_append(m, "{sv}", "operator-id", "s", network->mm_operator_id); |
| 558 |
if (r < 0) |
| 559 |
return bus_log_create_error(r); |
| 560 |
} |
| 561 |
|
| 562 |
if (network->mm_user) { |
| 563 |
r = sd_bus_message_append(m, "{sv}", "user", "s", network->mm_user); |
| 564 |
if (r < 0) |
| 565 |
return bus_log_create_error(r); |
| 566 |
} |
| 567 |
|
| 568 |
if (network->mm_password) { |
| 569 |
r = sd_bus_message_append(m, "{sv}", "password", "s", network->mm_password); |
| 570 |
if (r < 0) |
| 571 |
return bus_log_create_error(r); |
| 572 |
} |
| 573 |
|
| 574 |
if (network->mm_pin) { |
| 575 |
r = sd_bus_message_append(m, "{sv}", "pin", "s", network->mm_pin); |
| 576 |
if (r < 0) |
| 577 |
return bus_log_create_error(r); |
| 578 |
} |
| 579 |
|
| 580 |
r = sd_bus_message_close_container(m); |
| 581 |
if (r < 0) |
| 582 |
return bus_log_create_error(r); |
| 583 |
|
| 584 |
return sd_bus_call_async(bus, slot, m, callback, userdata, 0); |
| 585 |
} |
| 586 |
|
| 587 |
static void modem_simple_connect(Modem *modem) { |
| 588 |
Link *link; |
| 589 |
int r; |
| 590 |
|
| 591 |
assert(modem); |
| 592 |
|
| 593 |
/* Already have simple connect in progress? */ |
| 594 |
if (modem->slot_connect) |
| 595 |
return; |
| 596 |
|
| 597 |
if (modem->reconnect_state != MODEM_RECONNECT_SCHEDULED) |
| 598 |
return; |
| 599 |
|
| 600 |
/* |
| 601 |
* If port name is not known yet then wait for the reconnect |
| 602 |
* timer to trigger reconnection later on. |
| 603 |
*/ |
| 604 |
if (!modem->port_name) |
| 605 |
return; |
| 606 |
|
| 607 |
r = link_get_by_name(modem->manager, modem->port_name, &link); |
| 608 |
if (r < 0) |
| 609 |
return (void) log_debug_errno(r, "ModemManager: cannot find link for %s: %m", modem->port_name); |
| 610 |
|
| 611 |
/* Check if .network file found at all */ |
| 612 |
if (!link->network) |
| 613 |
return (void) log_debug("ModemManager: no .network file provided for %s", |
| 614 |
modem->port_name); |
| 615 |
|
| 616 |
/* Check if we are provided with at least APN which is required. */ |
| 617 |
if (!link->network->mm_apn) |
| 618 |
return (void) log_debug("ModemManager: not enough simple connect properties provided for %s", |
| 619 |
modem->port_name); |
| 620 |
|
| 621 |
log_info("ModemManager: starting simple connect on %s %s interface %s", |
| 622 |
modem->manufacturer, modem->model, modem->port_name); |
| 623 |
r = bus_call_method_async_props( |
| 624 |
modem->manager->bus, |
| 625 |
&modem->slot_connect, |
| 626 |
"org.freedesktop.ModemManager1", |
| 627 |
modem->path, |
| 628 |
"org.freedesktop.ModemManager1.Modem.Simple", |
| 629 |
"Connect", |
| 630 |
modem_connect_handler, modem, link); |
| 631 |
/* |
| 632 |
* If we failed to (re)start the connection now then rely on the periodic |
| 633 |
* timer and wait when it retries the connection attempt. |
| 634 |
*/ |
| 635 |
if (r < 0) |
| 636 |
log_warning_errno(r, "Could not start modem connection %s %s, will retry: %m", |
| 637 |
modem->manufacturer, modem->model); |
| 638 |
} |
| 639 |
|
| 640 |
static void modem_simple_disconnect(Modem *modem) { |
| 641 |
int r; |
| 642 |
|
| 643 |
assert(modem); |
| 644 |
|
| 645 |
r = sd_bus_call_method_async( |
| 646 |
modem->manager->bus, |
| 647 |
/* ret_slot= */ NULL, |
| 648 |
"org.freedesktop.ModemManager1", |
| 649 |
modem->path, |
| 650 |
"org.freedesktop.ModemManager1.Modem.Simple", |
| 651 |
"Disconnect", |
| 652 |
/* callback= */ NULL, |
| 653 |
/* userdata= */ NULL, |
| 654 |
"o", "/"); |
| 655 |
if (r < 0) |
| 656 |
log_warning_errno(r, "Could not disconnect modem %s %s: %m", |
| 657 |
modem->manufacturer, modem->model); |
| 658 |
} |
| 659 |
|
| 660 |
static int reset_timer(Manager *m, sd_event *e, sd_event_source **s); |
| 661 |
|
| 662 |
static int on_periodic_timer(sd_event_source *s, uint64_t usec, void *userdata) { |
| 663 |
Manager *manager = ASSERT_PTR(userdata); |
| 664 |
Modem *modem; |
| 665 |
sd_event *e; |
| 666 |
int r; |
| 667 |
|
| 668 |
assert(s); |
| 669 |
|
| 670 |
e = sd_event_source_get_event(s); |
| 671 |
|
| 672 |
HASHMAP_FOREACH(modem, manager->modems_by_path) { |
| 673 |
/* |
| 674 |
* We might be rate limiting the reconnection, e.g. if wrong simple connect options are |
| 675 |
* provided modem manager might try to connect (registered->connecting) and fail soon |
| 676 |
* (connecting->registered). To rate limit such a case we set MODEM_RECONNECT_WAITING state, |
| 677 |
* so using this timer we can limit the requests and wait, for example, for network |
| 678 |
* reconfigure wwanX. Still do not try to reconnect modems in failed state yet. |
| 679 |
*/ |
| 680 |
if (modem->reconnect_state == MODEM_RECONNECT_WAITING) { |
| 681 |
if (modem->state == MM_MODEM_STATE_LOCKED) |
| 682 |
/* If SIM is locked do not try to make it worse with applying wrong configuration again. */ |
| 683 |
continue; |
| 684 |
if (modem->state_fail_reason == MM_MODEM_STATE_FAILED_REASON_NONE) |
| 685 |
modem->reconnect_state = MODEM_RECONNECT_SCHEDULED; |
| 686 |
} |
| 687 |
modem_simple_connect(modem); |
| 688 |
} |
| 689 |
|
| 690 |
r = reset_timer(manager, e, &s); |
| 691 |
if (r < 0) |
| 692 |
log_warning_errno(r, "ModemManager: Failed to reset periodic timer event source, ignoring: %m"); |
| 693 |
|
| 694 |
return 0; |
| 695 |
} |
| 696 |
|
| 697 |
static int reset_timer(Manager *m, sd_event *e, sd_event_source **s) { |
| 698 |
return event_reset_time_relative( |
| 699 |
e, |
| 700 |
s, |
| 701 |
CLOCK_MONOTONIC, |
| 702 |
RECONNECT_TIMEOUT_USEC, |
| 703 |
/* accuracy= */ 0, |
| 704 |
on_periodic_timer, |
| 705 |
m, |
| 706 |
/* priority= */ 0, |
| 707 |
"modem-periodic-timer-event-source", |
| 708 |
/* force_reset= */ false); |
| 709 |
} |
| 710 |
|
| 711 |
static int setup_periodic_timer(Manager *m, sd_event *event) { |
| 712 |
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; |
| 713 |
int r; |
| 714 |
|
| 715 |
assert(event); |
| 716 |
assert(m); |
| 717 |
|
| 718 |
r = reset_timer(m, event, &s); |
| 719 |
if (r < 0) |
| 720 |
return r; |
| 721 |
|
| 722 |
return sd_event_source_set_floating(s, true); |
| 723 |
} |
| 724 |
|
| 725 |
int link_modem_reconfigure(Link *link) { |
| 726 |
Modem *modem; |
| 727 |
int r; |
| 728 |
|
| 729 |
assert(link); |
| 730 |
|
| 731 |
if (link_get_modem(link, &modem) >= 0) { |
| 732 |
modem_simple_disconnect(modem); |
| 733 |
/* .network has changed: start (re)connect if failed before. */ |
| 734 |
if (modem->reconnect_state == MODEM_RECONNECT_WAITING && |
| 735 |
modem->state_fail_reason == MM_MODEM_STATE_FAILED_REASON_NONE) { |
| 736 |
modem->reconnect_state = MODEM_RECONNECT_SCHEDULED; |
| 737 |
modem_simple_connect(modem); |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
r = link_apply_bearer(link); |
| 742 |
if (r < 0) |
| 743 |
return r; |
| 744 |
|
| 745 |
return 0; |
| 746 |
} |
| 747 |
|
| 748 |
static int modem_on_state_change( |
| 749 |
Modem *modem, |
| 750 |
MMModemState old_state, |
| 751 |
MMModemStateFailedReason old_fail_reason) { |
| 752 |
|
| 753 |
assert(modem); |
| 754 |
|
| 755 |
if (IN_SET(modem->state, MM_MODEM_STATE_CONNECTING, MM_MODEM_STATE_CONNECTED)) |
| 756 |
/* |
| 757 |
* Connection is ok or reconnect is already in progress: either initiataed by us or an |
| 758 |
* external entity. Make sure we do not try to start reconnection logic and wait for th |
| 759 |
* modem state change signal and then decide if need be. |
| 760 |
* We assume that it is not possible to be in the above modem states e.g. |
| 761 |
* connecting|connected if failed reason is not NONE, e.g. modem is all good. |
| 762 |
*/ |
| 763 |
return 0; |
| 764 |
|
| 765 |
/* Check if modem is still in failed state. */ |
| 766 |
if (modem->state_fail_reason != MM_MODEM_STATE_FAILED_REASON_NONE) { |
| 767 |
if (modem->state_fail_reason != old_fail_reason) { |
| 768 |
log_error("ModemManager: cannot schedule reconnect for %s %s, modem is in failed state: %s", |
| 769 |
modem->manufacturer, modem->model, |
| 770 |
modem->state_fail_reason < __MM_MODEM_STATE_FAILED_REASON_MAX ? |
| 771 |
modem_state_failed_reason_str[modem->state_fail_reason] : |
| 772 |
"unknown reason"); |
| 773 |
|
| 774 |
/* Do not try to reconnect until modem has recovered. */ |
| 775 |
modem->reconnect_state = MODEM_RECONNECT_WAITING; |
| 776 |
} |
| 777 |
return 0; |
| 778 |
} |
| 779 |
|
| 780 |
if (modem->reconnect_state == MODEM_RECONNECT_SCHEDULED) |
| 781 |
/* We are reconnecting now. */ |
| 782 |
return 0; |
| 783 |
|
| 784 |
/* |
| 785 |
* Modem is not in failed state and is not connected: try now. It is ok to fail and re-try to |
| 786 |
* connect with periodic timer later on. |
| 787 |
*/ |
| 788 |
modem->reconnect_state = MODEM_RECONNECT_SCHEDULED; |
| 789 |
modem_simple_connect(modem); |
| 790 |
|
| 791 |
return 0; |
| 792 |
} |
| 793 |
|
| 794 |
static int bearer_properties_changed_handler( |
| 795 |
sd_bus_message *message, |
| 796 |
void *userdata, |
| 797 |
sd_bus_error *error) { |
| 798 |
|
| 799 |
Manager *manager = ASSERT_PTR(userdata); |
| 800 |
const char *path; |
| 801 |
Modem *modem; |
| 802 |
Bearer *b; |
| 803 |
|
| 804 |
assert(message); |
| 805 |
|
| 806 |
path = sd_bus_message_get_path(message); |
| 807 |
if (!path) |
| 808 |
return 0; |
| 809 |
|
| 810 |
if (bearer_get_by_path(manager, path, &modem, &b) < 0) |
| 811 |
/* Unknown bearer, nothing to do. Modem-bearer association is handled |
| 812 |
* by modem_map_bearers() during modem property initialization. */ |
| 813 |
return 0; |
| 814 |
|
| 815 |
if (b->slot_getall) { |
| 816 |
/* Not initialized yet. Re-initialize it. */ |
| 817 |
(void) bearer_initialize(b); |
| 818 |
return 0; |
| 819 |
} |
| 820 |
|
| 821 |
(void) bearer_get_all_handler(message, b, error); |
| 822 |
return 0; |
| 823 |
} |
| 824 |
|
| 825 |
static int modem_map_bearers( |
| 826 |
sd_bus *bus, |
| 827 |
const char *member, |
| 828 |
sd_bus_message *m, |
| 829 |
sd_bus_error *error, |
| 830 |
void *userdata) { |
| 831 |
|
| 832 |
Modem *modem = ASSERT_PTR(userdata); |
| 833 |
_cleanup_strv_free_ char **paths = NULL; |
| 834 |
int r; |
| 835 |
|
| 836 |
r = sd_bus_message_read_strv(m, &paths); |
| 837 |
if (r < 0) |
| 838 |
return bus_log_parse_error(r); |
| 839 |
|
| 840 |
STRV_FOREACH(path, paths) { |
| 841 |
log_info("ModemManager: bearer found at path %s", *path); |
| 842 |
(void) bearer_new_and_initialize(modem, *path); |
| 843 |
} |
| 844 |
|
| 845 |
return 0; |
| 846 |
} |
| 847 |
|
| 848 |
static int modem_map_ports( |
| 849 |
sd_bus *bus, |
| 850 |
const char *member, |
| 851 |
sd_bus_message *m, |
| 852 |
sd_bus_error *error, |
| 853 |
void *userdata) { |
| 854 |
|
| 855 |
Modem *modem = ASSERT_PTR(userdata); |
| 856 |
const char *port_name; |
| 857 |
uint32_t port_type; |
| 858 |
int r; |
| 859 |
|
| 860 |
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, NULL); |
| 861 |
if (r < 0) |
| 862 |
return bus_log_parse_error_debug(r); |
| 863 |
|
| 864 |
while ((r = sd_bus_message_read(m, "(su)", &port_name, &port_type)) > 0) |
| 865 |
if (port_type == MM_MODEM_PORT_TYPE_NET) { |
| 866 |
r = free_and_strdup_warn(&modem->port_name, port_name); |
| 867 |
if (r < 0) |
| 868 |
return r; |
| 869 |
break; |
| 870 |
} |
| 871 |
|
| 872 |
r = sd_bus_message_exit_container(m); |
| 873 |
if (r < 0) |
| 874 |
return bus_log_parse_error(r); |
| 875 |
|
| 876 |
return 0; |
| 877 |
} |
| 878 |
|
| 879 |
static int modem_properties_changed_signal( |
| 880 |
sd_bus_message *message, |
| 881 |
void *userdata, |
| 882 |
sd_bus_error *ret_error) { |
| 883 |
|
| 884 |
static const struct bus_properties_map map[] = { |
| 885 |
{ "Bearers", "ao", modem_map_bearers, 0, }, |
| 886 |
{ "State", "i", NULL, offsetof(Modem, state) }, |
| 887 |
{ "StateFailedReason", "u", NULL, offsetof(Modem, state_fail_reason) }, |
| 888 |
{ "Manufacturer", "s", NULL, offsetof(Modem, manufacturer) }, |
| 889 |
{ "Model", "s", NULL, offsetof(Modem, model) }, |
| 890 |
{ "Ports", "a(su)", modem_map_ports, 0, }, |
| 891 |
{} |
| 892 |
}; |
| 893 |
Modem *modem = ASSERT_PTR(userdata); |
| 894 |
MMModemState old_state; |
| 895 |
MMModemStateFailedReason old_fail_reason; |
| 896 |
int r; |
| 897 |
|
| 898 |
/* skip name: string "org.freedesktop.ModemManager1.Modem" */ |
| 899 |
r = sd_bus_message_skip(message, "s"); |
| 900 |
if (r < 0) |
| 901 |
return log_warning_errno(r, "Failed while parsing properties of modem %s: %s", |
| 902 |
modem->path, bus_error_message(ret_error, r)); |
| 903 |
|
| 904 |
r = bus_message_contains_properties(message, map, ret_error); |
| 905 |
if (r < 0) |
| 906 |
return log_warning_errno(r, "Failed to check changed properties of modem %s: %s", |
| 907 |
modem->path, bus_error_message(ret_error, r)); |
| 908 |
|
| 909 |
if (r == 0) |
| 910 |
return 0; |
| 911 |
|
| 912 |
r = sd_bus_message_rewind(message, true); |
| 913 |
if (r < 0) |
| 914 |
return log_warning_errno(r, "Failed to rewind properties of modem %s", modem->path); |
| 915 |
old_state = modem->state; |
| 916 |
old_fail_reason = modem->state_fail_reason; |
| 917 |
|
| 918 |
/* skip name: string "org.freedesktop.ModemManager1.Bearer" */ |
| 919 |
r = sd_bus_message_skip(message, "s"); |
| 920 |
if (r < 0) |
| 921 |
return log_warning_errno(r, "Failed while parsing properties of modem %s: %s", |
| 922 |
modem->path, bus_error_message(ret_error, r)); |
| 923 |
|
| 924 |
r = bus_message_map_all_properties(message, map, |
| 925 |
BUS_MAP_BOOLEAN_AS_BOOL | BUS_MAP_STRDUP, |
| 926 |
ret_error, modem); |
| 927 |
if (r < 0) |
| 928 |
return log_warning_errno(r, "Failed to parse properties of modem %s: %s", |
| 929 |
modem->path, bus_error_message(ret_error, r)); |
| 930 |
|
| 931 |
return modem_on_state_change(modem, old_state, old_fail_reason); |
| 932 |
} |
| 933 |
|
| 934 |
static int modem_properties_changed_installed( |
| 935 |
sd_bus_message *message, |
| 936 |
void *userdata, |
| 937 |
sd_bus_error *ret_error) { |
| 938 |
|
| 939 |
Modem *modem = ASSERT_PTR(userdata); |
| 940 |
|
| 941 |
/* |
| 942 |
* As soon as the signal handler installed we can start reconnect |
| 943 |
* so we don't miss any property changed. |
| 944 |
*/ |
| 945 |
return modem_on_state_change(modem, MM_MODEM_STATE_UNKNOWN, MM_MODEM_STATE_FAILED_REASON_UNKNOWN); |
| 946 |
} |
| 947 |
|
| 948 |
static int modem_match_properties_changed(Modem *modem, const char *path) { |
| 949 |
int r; |
| 950 |
|
| 951 |
assert(modem); |
| 952 |
assert(modem->manager); |
| 953 |
assert(modem->manager->bus); |
| 954 |
|
| 955 |
r = sd_bus_match_signal_async(modem->manager->bus, &modem->slot_propertieschanged, |
| 956 |
"org.freedesktop.ModemManager1", path, |
| 957 |
"org.freedesktop.DBus.Properties", "PropertiesChanged", |
| 958 |
modem_properties_changed_signal, modem_properties_changed_installed, modem); |
| 959 |
if (r < 0) |
| 960 |
return log_error_errno(r, "Failed to request match for PropertiesChanged for modem %s: %m", path); |
| 961 |
|
| 962 |
return 0; |
| 963 |
} |
| 964 |
|
| 965 |
static int modem_add(Manager *m, const char *path, sd_bus_message *message, sd_bus_error *ret_error) { |
| 966 |
static const struct bus_properties_map map[] = { |
| 967 |
{ "Bearers", "ao", modem_map_bearers, 0, }, |
| 968 |
{ "State", "i", NULL, offsetof(Modem, state) }, |
| 969 |
{ "StateFailedReason", "u", NULL, offsetof(Modem, state_fail_reason) }, |
| 970 |
{ "Manufacturer", "s", NULL, offsetof(Modem, manufacturer) }, |
| 971 |
{ "Model", "s", NULL, offsetof(Modem, model) }, |
| 972 |
{ "Ports", "a(su)", modem_map_ports, 0, }, |
| 973 |
{} |
| 974 |
}; |
| 975 |
Modem *modem; |
| 976 |
int r; |
| 977 |
|
| 978 |
r = modem_get_by_path(m, path, &modem); |
| 979 |
if (r != -ENOENT) |
| 980 |
return sd_bus_message_skip(message, "a{sv}"); |
| 981 |
|
| 982 |
log_info("ModemManager: modem found at %s\n", path); |
| 983 |
|
| 984 |
r = modem_new(m, path, &modem); |
| 985 |
if (r < 0) |
| 986 |
return log_warning_errno(r, "Failed to initialize modem at %s, ignoring", path); |
| 987 |
|
| 988 |
r = modem_match_properties_changed(modem, path); |
| 989 |
if (r < 0) |
| 990 |
return log_warning_errno(r, "Failed to match on properties changed at %s, ignoring", path); |
| 991 |
|
| 992 |
r = bus_message_map_all_properties(message, map, BUS_MAP_STRDUP, ret_error, modem); |
| 993 |
if (r < 0) |
| 994 |
return log_warning_errno(r, "Failed to map properties at %s, ignoring", path); |
| 995 |
|
| 996 |
return 0; |
| 997 |
} |
| 998 |
|
| 999 |
static void modem_remove(Manager *m, const char *path) { |
| 1000 |
Modem *modem; |
| 1001 |
int r; |
| 1002 |
|
| 1003 |
r = modem_get_by_path(m, path, &modem); |
| 1004 |
if (r < 0) |
| 1005 |
return; |
| 1006 |
|
| 1007 |
log_error("ModemManager: %s %s %s removed", modem->manufacturer, modem->model, modem->port_name); |
| 1008 |
modem_free(modem); |
| 1009 |
} |
| 1010 |
|
| 1011 |
static int enumerate_modems_handler(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
| 1012 |
Manager *manager = ASSERT_PTR(userdata); |
| 1013 |
const sd_bus_error *e; |
| 1014 |
const char *modem_path; |
| 1015 |
int r; |
| 1016 |
|
| 1017 |
assert(message); |
| 1018 |
|
| 1019 |
e = sd_bus_message_get_error(message); |
| 1020 |
if (e) { |
| 1021 |
r = sd_bus_error_get_errno(e); |
| 1022 |
log_warning_errno(r, "Could not get managed objects: %s", bus_error_message(e, r)); |
| 1023 |
return 0; |
| 1024 |
} |
| 1025 |
|
| 1026 |
r = sd_bus_message_enter_container(message, SD_BUS_TYPE_ARRAY, "{oa{sa{sv}}}"); |
| 1027 |
if (r < 0) |
| 1028 |
return bus_log_parse_error_debug(r); |
| 1029 |
|
| 1030 |
while ((r = sd_bus_message_enter_container(message, SD_BUS_TYPE_DICT_ENTRY, "oa{sa{sv}}")) > 0) { |
| 1031 |
r = sd_bus_message_read_basic(message, SD_BUS_TYPE_OBJECT_PATH, &modem_path); |
| 1032 |
if (r < 0) |
| 1033 |
return bus_log_parse_error(r); |
| 1034 |
|
| 1035 |
r = sd_bus_message_enter_container(message, SD_BUS_TYPE_ARRAY, "{sa{sv}}"); |
| 1036 |
if (r < 0) |
| 1037 |
return bus_log_parse_error(r); |
| 1038 |
|
| 1039 |
while ((r = sd_bus_message_enter_container(message, SD_BUS_TYPE_DICT_ENTRY, "sa{sv}")) > 0) { |
| 1040 |
const char *interface_name = NULL; |
| 1041 |
|
| 1042 |
r = sd_bus_message_read_basic(message, 's', &interface_name); |
| 1043 |
if (r < 0) |
| 1044 |
return bus_log_parse_error(r); |
| 1045 |
|
| 1046 |
if (streq("org.freedesktop.ModemManager1.Modem", interface_name)) { |
| 1047 |
r = modem_add(manager, modem_path, message, ret_error); |
| 1048 |
if (r < 0) |
| 1049 |
return log_error_errno(r, "Failed to add modem at %s: %m", modem_path); |
| 1050 |
} else { |
| 1051 |
r = sd_bus_message_skip(message, "a{sv}"); |
| 1052 |
if (r < 0) |
| 1053 |
return bus_log_parse_error(r); |
| 1054 |
} |
| 1055 |
|
| 1056 |
r = sd_bus_message_exit_container(message); |
| 1057 |
if (r < 0) |
| 1058 |
return bus_log_parse_error(r); |
| 1059 |
} |
| 1060 |
|
| 1061 |
r = sd_bus_message_exit_container(message); |
| 1062 |
if (r < 0) |
| 1063 |
return bus_log_parse_error(r); |
| 1064 |
|
| 1065 |
r = sd_bus_message_exit_container(message); |
| 1066 |
if (r < 0) |
| 1067 |
return bus_log_parse_error(r); |
| 1068 |
} |
| 1069 |
|
| 1070 |
r = sd_bus_message_exit_container(message); |
| 1071 |
if (r < 0) |
| 1072 |
return bus_log_parse_error(r); |
| 1073 |
|
| 1074 |
return 0; |
| 1075 |
} |
| 1076 |
|
| 1077 |
static int enumerate_modems(Manager *manager) { |
| 1078 |
int r; |
| 1079 |
|
| 1080 |
log_debug("ModemManager: enumerate modems"); |
| 1081 |
/* Enumerate all modems and add new and drop removed. */ |
| 1082 |
|
| 1083 |
assert(manager); |
| 1084 |
assert(sd_bus_is_ready(manager->bus) > 0); |
| 1085 |
|
| 1086 |
r = sd_bus_call_method_async( |
| 1087 |
manager->bus, |
| 1088 |
/* ret_slot= */ NULL, |
| 1089 |
"org.freedesktop.ModemManager1", |
| 1090 |
"/org/freedesktop/ModemManager1", |
| 1091 |
"org.freedesktop.DBus.ObjectManager", |
| 1092 |
"GetManagedObjects", |
| 1093 |
enumerate_modems_handler, |
| 1094 |
manager, |
| 1095 |
/* types= */ NULL); |
| 1096 |
if (r < 0) |
| 1097 |
return log_error_errno(r, "Could not get managed objects: %m"); |
| 1098 |
|
| 1099 |
return 0; |
| 1100 |
} |
| 1101 |
|
| 1102 |
static int interface_add_remove_signal(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1103 |
Manager *manager = ASSERT_PTR(userdata); |
| 1104 |
int r; |
| 1105 |
|
| 1106 |
assert(manager); |
| 1107 |
assert(message); |
| 1108 |
|
| 1109 |
manager->slot_mm = sd_bus_slot_unref(manager->slot_mm); |
| 1110 |
|
| 1111 |
if (streq(message->member, "InterfacesAdded")) |
| 1112 |
log_info("ModemManager: modem(s) added"); |
| 1113 |
else { |
| 1114 |
const char *path; |
| 1115 |
|
| 1116 |
r = sd_bus_message_read_basic(message, 'o', &path); |
| 1117 |
if (r < 0) |
| 1118 |
return r; |
| 1119 |
|
| 1120 |
modem_remove(manager, path); |
| 1121 |
return 0; |
| 1122 |
} |
| 1123 |
|
| 1124 |
return enumerate_modems(manager); |
| 1125 |
} |
| 1126 |
|
| 1127 |
static int name_owner_changed_signal(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 1128 |
Manager *manager = ASSERT_PTR(userdata); |
| 1129 |
const char *name; |
| 1130 |
const char *new_owner; |
| 1131 |
int r; |
| 1132 |
|
| 1133 |
assert(manager); |
| 1134 |
assert(message); |
| 1135 |
|
| 1136 |
r = sd_bus_message_read(message, "sss", &name, NULL, &new_owner); |
| 1137 |
if (r < 0) { |
| 1138 |
bus_log_parse_error(r); |
| 1139 |
return 0; |
| 1140 |
} |
| 1141 |
|
| 1142 |
if (!streq(name, "org.freedesktop.ModemManager1")) |
| 1143 |
return 0; |
| 1144 |
|
| 1145 |
if (!isempty(new_owner)) |
| 1146 |
log_info("ModemManager: service is available"); |
| 1147 |
else { |
| 1148 |
log_info("ModemManager: service is not available"); |
| 1149 |
hashmap_clear(manager->modems_by_path); |
| 1150 |
return 0; |
| 1151 |
} |
| 1152 |
return enumerate_modems(manager); |
| 1153 |
} |
| 1154 |
|
| 1155 |
int manager_match_mm_signals(Manager *manager) { |
| 1156 |
int r; |
| 1157 |
|
| 1158 |
assert(manager); |
| 1159 |
assert(manager->bus); |
| 1160 |
|
| 1161 |
r = sd_bus_match_signal_async( |
| 1162 |
manager->bus, |
| 1163 |
/* ret= */ NULL, |
| 1164 |
"org.freedesktop.DBus", |
| 1165 |
"/org/freedesktop/DBus", |
| 1166 |
"org.freedesktop.DBus", |
| 1167 |
"NameOwnerChanged", |
| 1168 |
name_owner_changed_signal, |
| 1169 |
/* install_callback= */ NULL, |
| 1170 |
manager); |
| 1171 |
if (r < 0) |
| 1172 |
return log_error_errno(r, "Failed to request signal for NameOwnerChanged"); |
| 1173 |
|
| 1174 |
r = sd_bus_match_signal_async( |
| 1175 |
manager->bus, |
| 1176 |
/* ret= */ NULL, |
| 1177 |
"org.freedesktop.ModemManager1", |
| 1178 |
"/org/freedesktop/ModemManager1", |
| 1179 |
"org.freedesktop.DBus.ObjectManager", |
| 1180 |
"InterfacesAdded", |
| 1181 |
interface_add_remove_signal, |
| 1182 |
/* install_callback= */ NULL, |
| 1183 |
manager); |
| 1184 |
if (r < 0) |
| 1185 |
return log_error_errno(r, "Failed to request signal for InterfaceAdded"); |
| 1186 |
|
| 1187 |
r = sd_bus_match_signal_async( |
| 1188 |
manager->bus, |
| 1189 |
/* ret= */ NULL, |
| 1190 |
"org.freedesktop.ModemManager1", |
| 1191 |
"/org/freedesktop/ModemManager1", |
| 1192 |
"org.freedesktop.DBus.ObjectManager", |
| 1193 |
"InterfacesRemoved", |
| 1194 |
interface_add_remove_signal, |
| 1195 |
/* install_callback= */ NULL, |
| 1196 |
manager); |
| 1197 |
if (r < 0) |
| 1198 |
return log_error_errno(r, "Failed to request signal for InterfaceRemoved"); |
| 1199 |
|
| 1200 |
/* N.B. We need "path_namespace" for bearers, not "path", */ |
| 1201 |
r = sd_bus_add_match_async( |
| 1202 |
manager->bus, |
| 1203 |
/* ret_slot= */ NULL, |
| 1204 |
"type='signal'," |
| 1205 |
"sender='org.freedesktop.ModemManager1'," |
| 1206 |
"path_namespace='/org/freedesktop/ModemManager1/Bearer'," |
| 1207 |
"interface='org.freedesktop.DBus.Properties'," |
| 1208 |
"member='PropertiesChanged'", |
| 1209 |
bearer_properties_changed_handler, |
| 1210 |
/* install_callback= */ NULL, |
| 1211 |
manager); |
| 1212 |
if (r < 0) |
| 1213 |
return log_error_errno(r, "Failed to request signal for PropertiesChanged in ModemManager bearers"); |
| 1214 |
|
| 1215 |
return 0; |
| 1216 |
} |
| 1217 |
|
| 1218 |
static int list_names_handler(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
| 1219 |
Manager *manager = ASSERT_PTR(userdata); |
| 1220 |
_cleanup_strv_free_ char **names = NULL; |
| 1221 |
int r; |
| 1222 |
|
| 1223 |
assert(manager); |
| 1224 |
assert(message); |
| 1225 |
|
| 1226 |
manager->slot_mm = sd_bus_slot_unref(manager->slot_mm); |
| 1227 |
|
| 1228 |
/* Read the list of available services. */ |
| 1229 |
r = sd_bus_message_read_strv(message, &names); |
| 1230 |
if (r < 0) |
| 1231 |
return bus_log_parse_error(r); |
| 1232 |
|
| 1233 |
if (!strv_contains(names, "org.freedesktop.ModemManager1")) |
| 1234 |
/* If not found yet then wait for NameOwnerChanged signal. */ |
| 1235 |
return 0; |
| 1236 |
|
| 1237 |
log_info("ModemManager: service available"); |
| 1238 |
return enumerate_modems(manager); |
| 1239 |
} |
| 1240 |
|
| 1241 |
int manager_notify_mm_bus_connected(Manager *m) { |
| 1242 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 1243 |
int r; |
| 1244 |
|
| 1245 |
/* |
| 1246 |
* Called on D-Bus connected. |
| 1247 |
* Check if ModemManager is available. If it is then initialize. |
| 1248 |
* If not then wait for the service to be available. |
| 1249 |
*/ |
| 1250 |
assert(m); |
| 1251 |
assert(sd_bus_is_ready(m->bus) > 0); |
| 1252 |
|
| 1253 |
m->slot_mm = sd_bus_slot_unref(m->slot_mm); |
| 1254 |
|
| 1255 |
r = sd_bus_call_method_async( |
| 1256 |
m->bus, |
| 1257 |
&m->slot_mm, |
| 1258 |
"org.freedesktop.DBus", |
| 1259 |
"/org/freedesktop/DBus", |
| 1260 |
"org.freedesktop.DBus", |
| 1261 |
"ListNames", |
| 1262 |
list_names_handler, |
| 1263 |
m, |
| 1264 |
/* types= */ NULL); |
| 1265 |
if (r < 0) |
| 1266 |
return log_warning_errno(r, "Could not LsitNames: %m"); |
| 1267 |
|
| 1268 |
r = sd_event_default(&event); |
| 1269 |
if (r < 0) |
| 1270 |
return log_error_errno(r, "Failed to initialize sd-event: %m"); |
| 1271 |
|
| 1272 |
r = setup_periodic_timer(m, event); |
| 1273 |
if (r < 0) |
| 1274 |
return log_error_errno(r, "Failed to set up periodic timer: %m"); |
| 1275 |
|
| 1276 |
return 0; |
| 1277 |
} |