From: Zhenhua Zhang <[email protected]>

The status watches monitor the emulator activities. Other atoms like
gprs, netreg could register notification to watch emulator status
changes.
---
 include/emulator.h |    6 +++++
 src/emulator.c     |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/ofono.h        |   11 ++++++++++
 3 files changed, 71 insertions(+), 0 deletions(-)

diff --git a/include/emulator.h b/include/emulator.h
index 2691928..99ae218 100644
--- a/include/emulator.h
+++ b/include/emulator.h
@@ -30,6 +30,12 @@ extern "C" {
 
 struct ofono_emulator;
 
+enum ofono_emulator_status {
+       OFONO_EMULATOR_STATUS_IDLE = 0,
+       OFONO_EMULATOR_STATUS_CREATE,
+       OFONO_EMULATOR_STATUS_DESTROY,
+};
+
 struct ofono_emulator_driver {
        const char *name;
        enum ofono_atom_type type;
diff --git a/src/emulator.c b/src/emulator.c
index 1ede79a..c68dbf8 100644
--- a/src/emulator.c
+++ b/src/emulator.c
@@ -39,6 +39,8 @@ struct ofono_emulator {
        unsigned int id;
        GAtServer *server;
        struct ofono_emulator_driver *driver;
+       enum ofono_emulator_status status;
+       struct ofono_watchlist *status_watches;
 };
 
 static GSList *emulator_drivers = NULL;
@@ -80,6 +82,20 @@ static void ofono_emulator_release_id(int id)
        ofono_emulator_ids &= ~(0x1 << id);
 }
 
+static void notify_status_watches(struct ofono_emulator *e, void *data)
+{
+       struct ofono_watchlist_item *item;
+       ofono_emulator_status_notify_cb_t notify;
+       GSList *l;
+
+       for (l = e->status_watches->items; l; l = l->next) {
+               item = l->data;
+               notify = item->notify;
+
+               notify(e, e->status, data, item->notify_data);
+       }
+}
+
 int ofono_emulator_enable(struct ofono_emulator *e, GIOChannel *channel)
 {
        if (channel == NULL)
@@ -116,6 +132,11 @@ static void emulator_remove(struct ofono_atom *atom)
        if (e->server)
                emulator_disable(e);
 
+       e->status = OFONO_EMULATOR_STATUS_DESTROY;
+       notify_status_watches(e, NULL);
+
+       __ofono_watchlist_free(e->status_watches);
+
        ofono_emulator_release_id(e->id);
 
        if (e->driver->remove)
@@ -125,6 +146,37 @@ static void emulator_remove(struct ofono_atom *atom)
        e = NULL;
 }
 
+unsigned int __ofono_emulator_add_status_watch(struct ofono_emulator *e,
+                               ofono_emulator_status_notify_cb_t notify,
+                               void *data, ofono_destroy_func destroy)
+{
+       struct ofono_watchlist_item *item;
+
+       DBG("%p", e);
+
+       if (e == NULL)
+               return 0;
+
+       if (notify == NULL)
+               return 0;
+
+       item = g_new0(struct ofono_watchlist_item, 1);
+
+       item->notify = notify;
+       item->destroy = destroy;
+       item->notify_data = data;
+
+       return __ofono_watchlist_add_item(e->status_watches, item);
+}
+
+gboolean __ofono_emulator_remove_status_watch(struct ofono_emulator *e,
+                                               unsigned int id)
+{
+       DBG("%p", e);
+
+       return __ofono_watchlist_remove_item(e->status_watches, id);
+}
+
 struct ofono_emulator *ofono_emulator_create(struct ofono_modem *modem,
                                        struct ofono_emulator_driver *driver)
 {
@@ -148,6 +200,8 @@ struct ofono_emulator *ofono_emulator_create(struct 
ofono_modem *modem,
        }
 
        e->id = ofono_emulator_next_id();
+       e->status_watches = __ofono_watchlist_new(g_free);
+       e->status = OFONO_EMULATOR_STATUS_CREATE;
 
        return e;
 }
diff --git a/src/ofono.h b/src/ofono.h
index 64ea625..247152b 100644
--- a/src/ofono.h
+++ b/src/ofono.h
@@ -423,3 +423,14 @@ void __ofono_nettime_info_received(struct ofono_modem 
*modem,
 #include <ofono/emulator.h>
 
 void __ofono_emulator_probe_drivers(struct ofono_modem *modem);
+
+typedef void (*ofono_emulator_status_notify_cb_t)(struct ofono_emulator *e,
+                                       enum ofono_emulator_status status,
+                                       void *data, void *user_data);
+
+unsigned int __ofono_emulator_add_status_watch(struct ofono_emulator *e,
+                               ofono_emulator_status_notify_cb_t notify,
+                               void *data, ofono_destroy_func destroy);
+
+gboolean __ofono_emulator_remove_status_watch(struct ofono_emulator *e,
+                                               unsigned int id);
-- 
1.7.1

---------------------------------------------------------------------
Intel Corporation SAS (French simplified joint stock company)
Registered headquarters: "Les Montalets"- 2, rue de Paris, 
92196 Meudon Cedex, France
Registration Number:  302 456 199 R.C.S. NANTERRE
Capital: 4,572,000 Euros

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
_______________________________________________
ofono mailing list
[email protected]
http://lists.ofono.org/listinfo/ofono

Reply via email to