---
 plugins/hsp_ag.c |   76 +++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 67 insertions(+), 9 deletions(-)

diff --git a/plugins/hsp_ag.c b/plugins/hsp_ag.c
index a8e6f40..5a60269 100644
--- a/plugins/hsp_ag.c
+++ b/plugins/hsp_ag.c
@@ -47,7 +47,9 @@
 
 #define HSP_RFCOMM_CHAN 17
 
-static struct server *hsp;
+static struct server *server;
+static guint modemwatch_id;
+static GList *modems;
 
 static const gchar *hsp_record =
 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
@@ -90,25 +92,81 @@ static const gchar *hsp_record =
 " </attribute>\n"
 "</record>\n";
 
-static void connect_cb(GIOChannel *channel, GError *err, gpointer user_data)
+static void connect_cb(GIOChannel *channel, GError *err, gpointer data)
+{      int fd;
+       struct ofono_emulator *em;
+       struct ofono_modem *modem;
+
+       /* Pick the first powered modem */
+       modem = modems->data;
+       DBG("Picked modem %p for emulator", modem);
+
+       em = ofono_emulator_create(modem, OFONO_EMULATOR_TYPE_HSP);
+       if (em == NULL)
+               return;
+
+       fd = g_io_channel_unix_get_fd(channel);
+
+       ofono_emulator_register(em, fd);
+
+       g_io_channel_set_close_on_unref(channel, FALSE);
+}
+
+static void voicecall_watch(struct ofono_atom *atom,
+                               enum ofono_atom_watch_condition cond,
+                               void *data)
 {
+       struct ofono_modem *modem = data;
+
+       if (cond == OFONO_ATOM_WATCH_CONDITION_REGISTERED) {
+               modems = g_list_append(modems, modem);
+
+               if (modems->next == NULL)
+                       server = bluetooth_register_server(HSP_RFCOMM_CHAN,
+                                                       hsp_record,
+                                                       connect_cb,
+                                                       NULL);
+       } else {
+               modems = g_list_remove(modems, modem);
+               if (modems == NULL &&  server != NULL) {
+                       bluetooth_unregister_server(server);
+                       server = NULL;
+               }
+       }
 }
 
-static int hsp_ag_init(void)
+static void modem_watch(struct ofono_modem *modem, gboolean added, void *user)
 {
-       hsp = bluetooth_register_server(HSP_RFCOMM_CHAN, hsp_record, 
connect_cb, NULL);
+       DBG("modem: %p, added: %d", modem, added);
 
-       if(!hsp)
-               return -1;
+       if (added == FALSE)
+               return;
+
+       __ofono_modem_add_atom_watch(modem, OFONO_ATOM_TYPE_VOICECALL,
+                                       voicecall_watch, modem, NULL);
+}
+
+static void call_modemwatch(struct ofono_modem *modem, void *user)
+{
+       modem_watch(modem, TRUE, user);
+}
+
+static int hsp_ag_init(void)
+{
+       modemwatch_id = __ofono_modemwatch_add(modem_watch, NULL, NULL);
+       __ofono_modem_foreach(call_modemwatch, NULL);
 
-       DBG("HSP Gateway profile starting");
        return 0;
 }
 
 static void hsp_ag_exit(void)
 {
-       if(hsp)
-               bluetooth_unregister_server(hsp);
+       __ofono_modemwatch_remove(modemwatch_id);
+
+       if (server) {
+               bluetooth_unregister_server(server);
+               server = NULL;
+       }
 }
 
 OFONO_PLUGIN_DEFINE(hsp_ag, "Headset Gateway Profile", VERSION,
-- 
1.7.1

_______________________________________________
ofono mailing list
[email protected]
http://lists.ofono.org/listinfo/ofono

Reply via email to