plugins: implement mm_plugin_base_get_device_ids()

We'll need it in more than one place, so make it generic.
This commit is contained in:
Dan Williams
2009-06-20 18:18:08 -04:00
parent d5a43aa862
commit 05ab7b2a7c
3 changed files with 99 additions and 82 deletions

View File

@@ -47,61 +47,6 @@ mm_plugin_create (void)
/*****************************************************************************/ /*****************************************************************************/
/* From hostap, Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi> */
static int hex2num (char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}
static int hex2byte (const char *hex)
{
int a, b;
a = hex2num(*hex++);
if (a < 0)
return -1;
b = hex2num(*hex++);
if (b < 0)
return -1;
return (a << 4) | b;
}
/* End from hostap */
static gboolean
get_ids (GUdevDevice *device, guint32 *vendor, guint32 *product)
{
const char *vid, *pid;
g_return_val_if_fail (device != NULL, FALSE);
vid = g_udev_device_get_property (device, "ID_VENDOR_ID");
if (!vid || (strlen (vid) != 4))
return FALSE;
if (vendor) {
*vendor = (guint32) (hex2byte (vid + 2) & 0xFF);
*vendor |= (guint32) ((hex2byte (vid) & 0xFF) << 8);
}
pid = g_udev_device_get_property (device, "ID_MODEL_ID");
if (!pid || (strlen (pid) != 4))
return FALSE;
if (product) {
*product = (guint32) (hex2byte (pid + 2) & 0xFF);
*product |= (guint32) ((hex2byte (pid) & 0xFF) << 8);
}
return TRUE;
}
static char * static char *
get_driver_name (GUdevDevice *device) get_driver_name (GUdevDevice *device)
{ {
@@ -198,14 +143,14 @@ supports_port (MMPlugin *plugin,
MMPluginHuaweiPrivate *priv = MM_PLUGIN_HUAWEI_GET_PRIVATE (plugin); MMPluginHuaweiPrivate *priv = MM_PLUGIN_HUAWEI_GET_PRIVATE (plugin);
GUdevDevice *device, *physdev = NULL; GUdevDevice *device, *physdev = NULL;
guint32 level = 0; guint32 level = 0;
guint32 vendor = 0, product = 0; guint16 vendor = 0, product = 0;
device = get_device (priv->client, subsys, name, &physdev); device = get_device (priv->client, subsys, name, &physdev);
if (!device) if (!device)
goto out; goto out;
g_object_unref (physdev); g_object_unref (physdev);
if (!get_ids (device, &vendor, &product)) if (!mm_plugin_base_get_device_ids (MM_PLUGIN_BASE (plugin), subsys, name, &vendor, &product))
goto out; goto out;
if (vendor != 0x12d1) if (vendor != 0x12d1)
@@ -220,29 +165,6 @@ out:
return level; return level;
} }
#if 0
static char *
find_second_port (LibHalContext *ctx, const char *parent)
{
char **children;
char *second_port = NULL;
int num_children = 0;
int i;
if (!libhal_device_property_exists (ctx, parent, "usb.interface.number", NULL) ||
libhal_device_get_property_int (ctx, parent, "usb.interface.number", NULL) != 1)
return NULL;
children = libhal_manager_find_device_string_match (ctx, "info.parent", parent, &num_children, NULL);
for (i = 0; i < num_children && second_port == NULL; i++)
second_port = libhal_device_get_property_string (ctx, children[i], "serial.device", NULL);
libhal_free_string_array (children);
return second_port;
}
#endif
static MMModem * static MMModem *
grab_port (MMPlugin *plugin, grab_port (MMPlugin *plugin,
const char *subsys, const char *subsys,
@@ -255,7 +177,7 @@ grab_port (MMPlugin *plugin,
const char *devfile, *sysfs_path; const char *devfile, *sysfs_path;
char *driver = NULL; char *driver = NULL;
MMModem *modem = NULL; MMModem *modem = NULL;
guint32 product = 0; guint16 product = 0;
g_return_val_if_fail (subsys != NULL, NULL); g_return_val_if_fail (subsys != NULL, NULL);
g_return_val_if_fail (name != NULL, NULL); g_return_val_if_fail (name != NULL, NULL);
@@ -282,7 +204,7 @@ grab_port (MMPlugin *plugin,
goto out; goto out;
} }
if (!get_ids (device, NULL, &product)) { if (!mm_plugin_base_get_device_ids (MM_PLUGIN_BASE (plugin), subsys, name, NULL, &product)) {
g_set_error (error, 0, 0, "Could not get modem product ID."); g_set_error (error, 0, 0, "Could not get modem product ID.");
goto out; goto out;
} }

View File

@@ -19,6 +19,9 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#define G_UDEV_API_IS_SUBJECT_TO_CHANGE
#include <gudev/gudev.h>
#include "mm-plugin-base.h" #include "mm-plugin-base.h"
G_DEFINE_TYPE (MMPluginBase, mm_plugin_base, G_TYPE_OBJECT) G_DEFINE_TYPE (MMPluginBase, mm_plugin_base, G_TYPE_OBJECT)
@@ -95,6 +98,92 @@ mm_plugin_base_find_modem (MMPluginBase *self,
return g_hash_table_lookup (priv->modems, master_device); return g_hash_table_lookup (priv->modems, master_device);
} }
/* From hostap, Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi> */
static int hex2num (char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}
static int hex2byte (const char *hex)
{
int a, b;
a = hex2num(*hex++);
if (a < 0)
return -1;
b = hex2num(*hex++);
if (b < 0)
return -1;
return (a << 4) | b;
}
/* End from hostap */
gboolean
mm_plugin_base_get_device_ids (MMPluginBase *self,
const char *subsys,
const char *name,
guint16 *vendor,
guint16 *product)
{
GUdevClient *client;
GUdevDevice *device = NULL;
const char *tmp[] = { subsys, NULL };
const char *vid, *pid;
gboolean success = FALSE;
g_return_val_if_fail (self != NULL, FALSE);
g_return_val_if_fail (MM_IS_PLUGIN_BASE (self), FALSE);
g_return_val_if_fail (subsys != NULL, FALSE);
g_return_val_if_fail (name != NULL, FALSE);
if (vendor)
g_return_val_if_fail (*vendor == 0, FALSE);
if (product)
g_return_val_if_fail (*product == 0, FALSE);
client = g_udev_client_new (tmp);
if (!client)
return FALSE;
device = g_udev_client_query_by_subsystem_and_name (client, subsys, name);
if (!device)
goto out;
vid = g_udev_device_get_property (device, "ID_VENDOR_ID");
if (!vid || (strlen (vid) != 4))
goto out;
if (vendor) {
*vendor = (guint16) (hex2byte (vid + 2) & 0xFF);
*vendor |= (guint16) ((hex2byte (vid) & 0xFF) << 8);
}
pid = g_udev_device_get_property (device, "ID_MODEL_ID");
if (!pid || (strlen (pid) != 4)) {
*vendor = 0;
goto out;
}
if (product) {
*product = (guint16) (hex2byte (pid + 2) & 0xFF);
*product |= (guint16) ((hex2byte (pid) & 0xFF) << 8);
}
success = TRUE;
out:
if (device)
g_object_unref (device);
g_object_unref (client);
return success;
}
/*****************************************************************************/ /*****************************************************************************/
static void static void

View File

@@ -48,5 +48,11 @@ gboolean mm_plugin_base_add_modem (MMPluginBase *self,
MMModem *mm_plugin_base_find_modem (MMPluginBase *self, MMModem *mm_plugin_base_find_modem (MMPluginBase *self,
const char *master_device); const char *master_device);
gboolean mm_plugin_base_get_device_ids (MMPluginBase *self,
const char *subsys,
const char *name,
guint16 *vendor,
guint16 *product);
#endif /* MM_PLUGIN_BASE_H */ #endif /* MM_PLUGIN_BASE_H */