plugin: add a base class to handle modem tracking

This commit is contained in:
Dan Williams
2009-06-19 11:55:31 -04:00
parent 018e9e5831
commit 88bdb5d29a
7 changed files with 203 additions and 87 deletions

View File

@@ -34,7 +34,7 @@
static void plugin_init (MMPlugin *plugin_class);
G_DEFINE_TYPE_EXTENDED (MMPluginGeneric, mm_plugin_generic, G_TYPE_OBJECT,
G_DEFINE_TYPE_EXTENDED (MMPluginGeneric, mm_plugin_generic, MM_TYPE_PLUGIN_BASE,
0, G_IMPLEMENT_INTERFACE (MM_TYPE_PLUGIN, plugin_init))
int mm_plugin_major_version = MM_PLUGIN_MAJOR_VERSION;
@@ -44,7 +44,6 @@ int mm_plugin_minor_version = MM_PLUGIN_MINOR_VERSION;
typedef struct {
GUdevClient *client;
GHashTable *modems;
} MMPluginGenericPrivate;
@@ -180,33 +179,6 @@ out:
return level;
}
typedef struct {
char *key;
gpointer modem;
} FindInfo;
static void
find_modem (gpointer key, gpointer data, gpointer user_data)
{
FindInfo *info = user_data;
if (!info->key && data == info->modem)
info->key = g_strdup ((const char *) key);
}
static void
modem_destroyed (gpointer data, GObject *modem)
{
MMPluginGeneric *self = MM_PLUGIN_GENERIC (data);
MMPluginGenericPrivate *priv = MM_PLUGIN_GENERIC_GET_PRIVATE (self);
FindInfo info = { NULL, modem };
g_hash_table_foreach (priv->modems, find_modem, &info);
if (info.key)
g_hash_table_remove (priv->modems, info.key);
g_free (info.key);
}
static MMModem *
grab_port (MMPlugin *plugin,
const char *subsys,
@@ -214,7 +186,6 @@ grab_port (MMPlugin *plugin,
GError **error)
{
MMPluginGeneric *self = MM_PLUGIN_GENERIC (plugin);
MMPluginGenericPrivate *priv = MM_PLUGIN_GENERIC_GET_PRIVATE (plugin);
GUdevDevice *device = NULL, *physdev = NULL;
const char *devfile, *sysfs_path;
char *driver = NULL;
@@ -263,7 +234,7 @@ grab_port (MMPlugin *plugin,
goto out;
}
modem = g_hash_table_lookup (priv->modems, sysfs_path);
modem = mm_plugin_base_find_modem (MM_PLUGIN_BASE (self), sysfs_path);
if (!modem) {
if (gsm) {
modem = mm_generic_gsm_new (sysfs_path,
@@ -282,10 +253,8 @@ grab_port (MMPlugin *plugin,
}
}
if (modem) {
g_object_weak_ref (G_OBJECT (modem), modem_destroyed, self);
g_hash_table_insert (priv->modems, g_strdup (sysfs_path), modem);
}
if (modem)
mm_plugin_base_add_modem (MM_PLUGIN_BASE (self), modem);
} else {
if (gsm || cdma) {
if (!mm_modem_grab_port (modem, subsys, name, error))
@@ -323,8 +292,6 @@ mm_plugin_generic_init (MMPluginGeneric *self)
MMPluginGenericPrivate *priv = MM_PLUGIN_GENERIC_GET_PRIVATE (self);
const char *subsys[2] = { "tty", NULL };
priv->modems = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
priv->client = g_udev_client_new (subsys);
}
@@ -333,7 +300,6 @@ dispose (GObject *object)
{
MMPluginGenericPrivate *priv = MM_PLUGIN_GENERIC_GET_PRIVATE (object);
g_hash_table_destroy (priv->modems);
g_object_unref (priv->client);
}

View File

@@ -4,6 +4,7 @@
#define MM_PLUGIN_GENERIC_H
#include "mm-plugin.h"
#include "mm-plugin-base.h"
#define MM_TYPE_PLUGIN_GENERIC (mm_plugin_generic_get_type ())
#define MM_PLUGIN_GENERIC(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MM_TYPE_PLUGIN_GENERIC, MMPluginGeneric))
@@ -13,11 +14,11 @@
#define MM_PLUGIN_GENERIC_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MM_TYPE_PLUGIN_GENERIC, MMPluginGenericClass))
typedef struct {
GObject parent;
MMPluginBase parent;
} MMPluginGeneric;
typedef struct {
GObjectClass parent;
MMPluginBaseClass parent;
} MMPluginGenericClass;
GType mm_plugin_generic_get_type (void);

View File

@@ -25,7 +25,7 @@
static void plugin_init (MMPlugin *plugin_class);
G_DEFINE_TYPE_EXTENDED (MMPluginGobi, mm_plugin_gobi, G_TYPE_OBJECT,
G_DEFINE_TYPE_EXTENDED (MMPluginGobi, mm_plugin_gobi, MM_TYPE_PLUGIN_BASE,
0, G_IMPLEMENT_INTERFACE (MM_TYPE_PLUGIN, plugin_init))
int mm_plugin_major_version = MM_PLUGIN_MAJOR_VERSION;
@@ -35,7 +35,6 @@ int mm_plugin_minor_version = MM_PLUGIN_MINOR_VERSION;
typedef struct {
GUdevClient *client;
GHashTable *modems;
} MMPluginGobiPrivate;
@@ -175,33 +174,6 @@ out:
return level;
}
typedef struct {
char *key;
gpointer modem;
} FindInfo;
static void
find_modem (gpointer key, gpointer data, gpointer user_data)
{
FindInfo *info = user_data;
if (!info->key && data == info->modem)
info->key = g_strdup ((const char *) key);
}
static void
modem_destroyed (gpointer data, GObject *modem)
{
MMPluginGobi *self = MM_PLUGIN_GOBI (data);
MMPluginGobiPrivate *priv = MM_PLUGIN_GOBI_GET_PRIVATE (self);
FindInfo info = { NULL, modem };
g_hash_table_foreach (priv->modems, find_modem, &info);
if (info.key)
g_hash_table_remove (priv->modems, info.key);
g_free (info.key);
}
static MMModem *
grab_port (MMPlugin *plugin,
const char *subsys,
@@ -209,7 +181,6 @@ grab_port (MMPlugin *plugin,
GError **error)
{
MMPluginGobi *self = MM_PLUGIN_GOBI (plugin);
MMPluginGobiPrivate *priv = MM_PLUGIN_GOBI_GET_PRIVATE (plugin);
GUdevDevice *device = NULL, *physdev = NULL;
const char *devfile, *sysfs_path;
char *driver = NULL;
@@ -258,7 +229,7 @@ grab_port (MMPlugin *plugin,
goto out;
}
modem = g_hash_table_lookup (priv->modems, sysfs_path);
modem = mm_plugin_base_find_modem (MM_PLUGIN_BASE (self), sysfs_path);
if (!modem) {
if (gsm) {
modem = mm_modem_gobi_gsm_new (sysfs_path,
@@ -277,10 +248,8 @@ grab_port (MMPlugin *plugin,
}
}
if (modem) {
g_object_weak_ref (G_OBJECT (modem), modem_destroyed, self);
g_hash_table_insert (priv->modems, g_strdup (sysfs_path), modem);
}
if (modem)
mm_plugin_base_add_modem (MM_PLUGIN_BASE (self), modem);
} else {
if (gsm || cdma) {
if (!mm_modem_grab_port (modem, subsys, name, error))
@@ -318,8 +287,6 @@ mm_plugin_gobi_init (MMPluginGobi *self)
MMPluginGobiPrivate *priv = MM_PLUGIN_GOBI_GET_PRIVATE (self);
const char *subsys[2] = { "tty", NULL };
priv->modems = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
priv->client = g_udev_client_new (subsys);
}
@@ -328,7 +295,6 @@ dispose (GObject *object)
{
MMPluginGobiPrivate *priv = MM_PLUGIN_GOBI_GET_PRIVATE (object);
g_hash_table_destroy (priv->modems);
g_object_unref (priv->client);
}

View File

@@ -18,6 +18,7 @@
#define MM_PLUGIN_GOBI_H
#include "mm-plugin.h"
#include "mm-plugin-base.h"
#include "mm-generic-gsm.h"
#define MM_TYPE_PLUGIN_GOBI (mm_plugin_gobi_get_type ())
@@ -28,11 +29,11 @@
#define MM_PLUGIN_GOBI_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MM_TYPE_PLUGIN_GOBI, MMPluginGobiClass))
typedef struct {
GObject parent;
MMPluginBase parent;
} MMPluginGobi;
typedef struct {
GObjectClass parent;
MMPluginBaseClass parent;
} MMPluginGobiClass;
GType mm_plugin_gobi_get_type (void);
@@ -40,3 +41,4 @@ GType mm_plugin_gobi_get_type (void);
G_MODULE_EXPORT MMPlugin *mm_plugin_create (void);
#endif /* MM_PLUGIN_GOBI_H */

View File

@@ -44,7 +44,9 @@ modem_manager_SOURCES = \
mm-options.c \
mm-options.h \
mm-plugin.c \
mm-plugin.h
mm-plugin.h \
mm-plugin-base.c \
mm-plugin-base.h
mm-manager-glue.h: $(top_srcdir)/introspection/mm-manager.xml
dbus-binding-tool --prefix=mm_manager --mode=glib-server --output=$@ $<

127
src/mm-plugin-base.c Normal file
View File

@@ -0,0 +1,127 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* Copyright (C) 2008 - 2009 Novell, Inc.
* Copyright (C) 2009 Red Hat, Inc.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "mm-plugin-base.h"
G_DEFINE_TYPE (MMPluginBase, mm_plugin_base, G_TYPE_OBJECT)
#define MM_PLUGIN_BASE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_PLUGIN_BASE, MMPluginBasePrivate))
typedef struct {
GHashTable *modems;
} MMPluginBasePrivate;
typedef struct {
char *key;
gpointer modem;
} FindInfo;
static void
find_modem (gpointer key, gpointer data, gpointer user_data)
{
FindInfo *info = user_data;
if (!info->key && data == info->modem)
info->key = g_strdup ((const char *) key);
}
static void
modem_destroyed (gpointer data, GObject *modem)
{
MMPluginBase *self = MM_PLUGIN_BASE (data);
MMPluginBasePrivate *priv = MM_PLUGIN_BASE_GET_PRIVATE (self);
FindInfo info = { NULL, modem };
g_hash_table_foreach (priv->modems, find_modem, &info);
if (info.key)
g_hash_table_remove (priv->modems, info.key);
g_free (info.key);
}
gboolean
mm_plugin_base_add_modem (MMPluginBase *self,
MMModem *modem)
{
MMPluginBasePrivate *priv;
const char *device;
g_return_val_if_fail (self != NULL, FALSE);
g_return_val_if_fail (MM_IS_PLUGIN_BASE (self), FALSE);
g_return_val_if_fail (modem != NULL, FALSE);
g_return_val_if_fail (MM_IS_MODEM (modem), FALSE);
priv = MM_PLUGIN_BASE_GET_PRIVATE (self);
device = mm_modem_get_device (modem);
if (g_hash_table_lookup (priv->modems, device))
return FALSE;
g_object_weak_ref (G_OBJECT (modem), modem_destroyed, self);
g_hash_table_insert (priv->modems, g_strdup (device), modem);
return TRUE;
}
MMModem *
mm_plugin_base_find_modem (MMPluginBase *self,
const char *master_device)
{
MMPluginBasePrivate *priv;
g_return_val_if_fail (self != NULL, NULL);
g_return_val_if_fail (MM_IS_PLUGIN_BASE (self), NULL);
g_return_val_if_fail (master_device != NULL, NULL);
g_return_val_if_fail (strlen (master_device) > 0, NULL);
priv = MM_PLUGIN_BASE_GET_PRIVATE (self);
return g_hash_table_lookup (priv->modems, master_device);
}
/*****************************************************************************/
static void
mm_plugin_base_init (MMPluginBase *self)
{
MMPluginBasePrivate *priv = MM_PLUGIN_BASE_GET_PRIVATE (self);
priv->modems = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
}
static void
finalize (GObject *object)
{
MMPluginBasePrivate *priv = MM_PLUGIN_BASE_GET_PRIVATE (object);
g_hash_table_destroy (priv->modems);
G_OBJECT_CLASS (mm_plugin_base_parent_class)->finalize (object);
}
static void
mm_plugin_base_class_init (MMPluginBaseClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (object_class, sizeof (MMPluginBasePrivate));
/* Virtual methods */
object_class->finalize = finalize;
}

52
src/mm-plugin-base.h Normal file
View File

@@ -0,0 +1,52 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* Copyright (C) 2009 Red Hat, Inc.
*/
#ifndef MM_PLUGIN_BASE_H
#define MM_PLUGIN_BASE_H
#include <glib.h>
#include <glib/gtypes.h>
#include <glib-object.h>
#include "mm-modem.h"
#define MM_TYPE_PLUGIN_BASE (mm_plugin_base_get_type ())
#define MM_PLUGIN_BASE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MM_TYPE_PLUGIN_BASE, MMPluginBase))
#define MM_PLUGIN_BASE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MM_TYPE_PLUGIN_BASE, MMPluginBaseClass))
#define MM_IS_PLUGIN_BASE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MM_TYPE_PLUGIN_BASE))
#define MM_IS_PLUBIN_BASE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MM_TYPE_PLUGIN_BASE))
#define MM_PLUGIN_BASE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MM_TYPE_PLUGIN_BASE, MMPluginBaseClass))
typedef struct _MMPluginBase MMPluginBase;
typedef struct _MMPluginBaseClass MMPluginBaseClass;
struct _MMPluginBase {
GObject parent;
};
struct _MMPluginBaseClass {
GObjectClass parent;
};
GType mm_plugin_base_get_type (void);
gboolean mm_plugin_base_add_modem (MMPluginBase *self,
MMModem *modem);
MMModem *mm_plugin_base_find_modem (MMPluginBase *self,
const char *master_device);
#endif /* MM_PLUGIN_BASE_H */