aboutsummaryrefslogtreecommitdiff
path: root/cli/mmcli-common.c
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@aleksander.es>2016-03-27 14:41:35 +0200
committerAleksander Morgado <aleksander@aleksander.es>2016-09-29 15:41:21 +0200
commit1f813c4e9691f22017802278ab6f5b1475185113 (patch)
tree8c354cd3053d7837bc575e6f1c793fda0c2ba3e1 /cli/mmcli-common.c
parente5fa0233bb73a8374cf35e9170c66c580255815a (diff)
core: allow identifying devices by a user-provided 'uid'
All ports of the same modem reported by the kernel will all be associated with a common 'uid' (unique id), which uniquely identifies the physical device. This logic was already in place, what we do now is avoid calling it the 'sysfs path' of the physical device, because we may not want to use that to identify a device. This logic now also enables the possibility of "naming" the modems in a unique way by setting the "ID_MM_PHYSDEV_UID" property in the "usb_device" that owns all the ports. E.g. a custom device has 4 modems in 4 different USB ports. The device path of each USB device will always be the same, so the naming rules could go like this: $ vim /usr/lib/udev/rules.d/78-mm-naming.rules ACTION!="add|change|move", GOTO="mm_naming_rules_end" DEVPATH=="/devices/pci0000:00/0000:00:1d.0/usb4/4-1/4-1.5/4-1.5.1", ENV{ID_MM_PHYSDEV_UID}="USB-MODEM-1" DEVPATH=="/devices/pci0000:00/0000:00:1d.0/usb4/4-1/4-1.5/4-1.5.2", ENV{ID_MM_PHYSDEV_UID}="USB-MODEM-2" DEVPATH=="/devices/pci0000:00/0000:00:1d.0/usb4/4-1/4-1.5/4-1.5.3", ENV{ID_MM_PHYSDEV_UID}="USB-MODEM-3" DEVPATH=="/devices/pci0000:00/0000:00:1d.0/usb4/4-1/4-1.5/4-1.5.4", ENV{ID_MM_PHYSDEV_UID}="USB-MODEM-4" LABEL="mm_naming_rules_end" Each of the modems found will have a unique UID retrieved from the previous list of rules. Then, "mmcli" has also been updated to allow using the UID instead of the modem DBus path or index, e.g.: $ sudo mmcli -m USB-MODEM-1 /org/freedesktop/ModemManager1/Modem/0 (device id '988d83252c0598f670c2d69d5f41e077204a92fd') ------------------------- Hardware | manufacturer: 'ZTE CORPORATION' | model: 'MF637' | revision: 'BD_W7P673A3F3V1.0.0B04' | supported: 'gsm-umts' | current: 'gsm-umts' | equipment id: '356516027657837' ------------------------- System | device: 'USB-MODEM-1' | drivers: 'option' | plugin: 'ZTE' | primary port: 'ttyUSB5' | ports: 'ttyUSB5 (at)' ... $ sudo mmcli -m USB-MODEM-1 --enable ...
Diffstat (limited to 'cli/mmcli-common.c')
-rw-r--r--cli/mmcli-common.c98
1 files changed, 70 insertions, 28 deletions
diff --git a/cli/mmcli-common.c b/cli/mmcli-common.c
index f9908e94..f48dd9c2 100644
--- a/cli/mmcli-common.c
+++ b/cli/mmcli-common.c
@@ -114,25 +114,41 @@ mmcli_get_manager_sync (GDBusConnection *connection)
static MMObject *
find_modem (MMManager *manager,
- const gchar *modem_path)
+ const gchar *modem_path,
+ const gchar *modem_uid)
{
GList *modems;
GList *l;
MMObject *found = NULL;
+ g_assert (modem_path || modem_uid);
+ g_assert (!(modem_path && modem_uid));
+
modems = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (manager));
for (l = modems; l; l = g_list_next (l)) {
- MMObject *modem = MM_OBJECT (l->data);
+ MMObject *obj;
+ MMModem *modem;
+
+ obj = MM_OBJECT (l->data);
+ modem = MM_MODEM (mm_object_get_modem (obj));
+
+ if (modem_path && g_str_equal (mm_object_get_path (obj), modem_path)) {
+ found = g_object_ref (obj);
+ break;
+ }
- if (g_str_equal (mm_object_get_path (modem), modem_path)) {
- found = g_object_ref (modem);
+ if (modem_uid && g_str_equal (mm_modem_get_device (modem), modem_uid)) {
+ found = g_object_ref (obj);
break;
}
}
g_list_free_full (modems, (GDestroyNotify) g_object_unref);
if (!found) {
- g_printerr ("error: couldn't find modem at '%s'\n", modem_path);
+ if (modem_path)
+ g_printerr ("error: couldn't find modem at '%s'\n", modem_path);
+ else if (modem_uid)
+ g_printerr ("error: couldn't find modem identified by uid '%s'\n", modem_uid);
exit (EXIT_FAILURE);
}
@@ -145,6 +161,7 @@ typedef struct {
GSimpleAsyncResult *result;
GCancellable *cancellable;
gchar *modem_path;
+ gchar *modem_uid;
} GetModemContext;
typedef struct {
@@ -168,6 +185,7 @@ get_modem_context_complete_and_free (GetModemContext *ctx)
if (ctx->cancellable)
g_object_unref (ctx->cancellable);
g_free (ctx->modem_path);
+ g_free (ctx->modem_uid);
g_free (ctx);
}
@@ -193,7 +211,7 @@ get_manager_ready (GDBusConnection *connection,
results = g_new (GetModemResults, 1);
results->manager = mmcli_get_manager_finish (res);
- results->object = find_modem (results->manager, ctx->modem_path);
+ results->object = find_modem (results->manager, ctx->modem_path, ctx->modem_uid);
/* Set operation results */
g_simple_async_result_set_op_res_gpointer (
@@ -204,37 +222,57 @@ get_manager_ready (GDBusConnection *connection,
get_modem_context_complete_and_free (ctx);
}
-static gchar *
-get_modem_path (const gchar *path_or_index)
+static void
+get_modem_path_or_uid (const gchar *str,
+ gchar **modem_path,
+ gchar **modem_uid)
{
- gchar *modem_path;
+ gboolean all_numeric;
+ guint i;
/* We must have a given modem specified */
- if (!path_or_index) {
+ if (!str || !str[0]) {
g_printerr ("error: no modem was specified\n");
exit (EXIT_FAILURE);
}
- /* Modem path may come in two ways: full DBus path or just modem index.
- * If it is a modem index, we'll need to generate the DBus path ourselves */
- if (g_str_has_prefix (path_or_index, MM_DBUS_MODEM_PREFIX)) {
- g_debug ("Assuming '%s' is the full modem path", path_or_index);
- modem_path = g_strdup (path_or_index);
- } else if (g_ascii_isdigit (path_or_index[0])) {
- g_debug ("Assuming '%s' is the modem index", path_or_index);
- modem_path = g_strdup_printf (MM_DBUS_MODEM_PREFIX "/%s", path_or_index);
- } else {
- g_printerr ("error: invalid path or index string specified: '%s'\n",
- path_or_index);
- exit (EXIT_FAILURE);
+ /* Modem path may come in three ways:
+ * a) full DBus path
+ * b) modem index
+ * c) uid
+ */
+
+ *modem_path = NULL;
+ *modem_uid = NULL;
+
+ /* If we have DBus prefix, we have the modem DBus path */
+ if (g_str_has_prefix (str, MM_DBUS_MODEM_PREFIX)) {
+ g_debug ("Assuming '%s' is the full modem path", str);
+ *modem_path = g_strdup (str);
+ return;
+ }
+
+ /* If all numeric, we have the modem index */
+ all_numeric = TRUE;
+ for (i = 0; str[i]; i++) {
+ if (!g_ascii_isdigit (str[i])) {
+ all_numeric = FALSE;
+ break;
+ }
+ }
+ if (all_numeric) {
+ g_debug ("Assuming '%s' is the modem index", str);
+ *modem_path = g_strdup_printf (MM_DBUS_MODEM_PREFIX "/%s", str);
+ return;
}
- return modem_path;
+ /* Otherwise we have the UID */
+ *modem_uid = g_strdup (str);
}
void
mmcli_get_modem (GDBusConnection *connection,
- const gchar *path_or_index,
+ const gchar *modem_str,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
@@ -242,7 +280,8 @@ mmcli_get_modem (GDBusConnection *connection,
GetModemContext *ctx;
ctx = g_new0 (GetModemContext, 1);
- ctx->modem_path = get_modem_path (path_or_index);
+ get_modem_path_or_uid (modem_str, &ctx->modem_path, &ctx->modem_uid);
+ g_assert (ctx->modem_path || ctx->modem_uid);
ctx->result = g_simple_async_result_new (G_OBJECT (connection),
callback,
user_data,
@@ -261,17 +300,20 @@ mmcli_get_modem_sync (GDBusConnection *connection,
{
MMManager *manager;
MMObject *found;
- gchar *modem_path;
+ gchar *modem_path = NULL;
+ gchar *modem_uid = NULL;
manager = mmcli_get_manager_sync (connection);
- modem_path = get_modem_path (modem_str);
- found = find_modem (manager, modem_path);
+ get_modem_path_or_uid (modem_str, &modem_path, &modem_uid);
+ g_assert (modem_path || modem_uid);
+ found = find_modem (manager, modem_path, modem_uid);
if (o_manager)
*o_manager = manager;
else
g_object_unref (manager);
g_free (modem_path);
+ g_free (modem_uid);
return found;
}