All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gustavo F. Padovan <padovan@profusion.mobi>
To: ofono@ofono.org
Subject: [PATCH 5/8] emulator: Add emulator atom in oFono
Date: Mon, 31 Jan 2011 18:51:59 -0200	[thread overview]
Message-ID: <1296507122-10936-5-git-send-email-padovan@profusion.mobi> (raw)
In-Reply-To: <1296507122-10936-4-git-send-email-padovan@profusion.mobi>

[-- Attachment #1: Type: text/plain, Size: 9132 bytes --]

Create emulator atom when modem state changes to online. The emulator
driver probes each driver to create specific emulator like DUN, HFP AG,
etc. Once get client connection request, create GAtServer to talk AT
commands with client side.

Based on a patch from Zhenhua Zhang <zhenhua.zhang@intel.com>
---
 Makefile.am        |    5 +-
 include/emulator.h |    5 +-
 src/emulator.c     |  281 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/ofono.h        |    5 +
 4 files changed, 291 insertions(+), 5 deletions(-)
 create mode 100644 src/emulator.c

diff --git a/Makefile.am b/Makefile.am
index 77b1453..1ca08e9 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -14,7 +14,7 @@ pkginclude_HEADERS = include/log.h include/plugin.h include/history.h \
 			include/audio-settings.h include/nettime.h \
 			include/ctm.h include/cdma-voicecall.h \
 			include/cdma-sms.h include/sim-auth.h \
-			include/gprs-provision.h
+			include/gprs-provision.h include/emulator.h
 
 nodist_pkginclude_HEADERS = include/version.h
 
@@ -363,7 +363,8 @@ src_ofonod_SOURCES = $(gdbus_sources) $(builtin_sources) src/ofono.ver \
 			src/simfs.c src/simfs.h src/audio-settings.c \
 			src/smsagent.c src/smsagent.h src/ctm.c \
 			src/cdma-voicecall.c src/sim-auth.c \
-			src/message.h src/message.c src/gprs-provision.c
+			src/message.h src/message.c src/gprs-provision.c \
+			src/emulator.c
 
 src_ofonod_LDADD = $(builtin_libadd) @GLIB_LIBS@ @DBUS_LIBS@ @CAPNG_LIBS@ -ldl
 
diff --git a/include/emulator.h b/include/emulator.h
index 1287b47..bc89071 100644
--- a/include/emulator.h
+++ b/include/emulator.h
@@ -33,9 +33,8 @@ struct ofono_emulator;
 struct ofono_emulator_driver {
 	const char *name;
 	enum ofono_atom_type type;
-	int (*probe)(struct ofono_emulator *emulator,
-			struct ofono_modem *modem);
-	void (*remove)();
+	int (*probe)(struct ofono_emulator *emulator);
+	void (*remove)(void);
 };
 
 int ofono_emulator_enable(struct ofono_emulator *emulator, int fd);
diff --git a/src/emulator.c b/src/emulator.c
new file mode 100644
index 0000000..aa71e21
--- /dev/null
+++ b/src/emulator.c
@@ -0,0 +1,281 @@
+/*
+ *
+ *  oFono - Open Source Telephony
+ *
+ *  Copyright (C) 2010  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  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.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <glib.h>
+#include <gdbus.h>
+
+#include "ofono.h"
+#include "common.h"
+#include "gatserver.h"
+
+struct ofono_emulator {
+	struct ofono_modem *modem;
+	struct ofono_atom *atom;
+	GAtServer *server;
+	struct ofono_emulator_driver *driver;
+};
+
+static GSList *emulator_drivers = NULL;
+
+static void ofono_emulator_debug(const char *str, void *data)
+{
+	g_print("%s: %s\n", (char *)data, str);
+}
+
+void ofono_emulator_remove(struct ofono_emulator *emulator)
+{
+	if (emulator == NULL)
+		return;
+
+	__ofono_atom_free(emulator->atom);
+}
+
+static void ppp_connect(const char *iface, const char *local,
+			const char *remote,
+			const char *dns1, const char *dns2,
+			gpointer user_data)
+{
+	DBG("Network Device: %s\n", iface);
+	DBG("IP Address: %s\n", local);
+	DBG("Remote IP Address: %s\n", remote);
+	DBG("Primary DNS Server: %s\n", dns1);
+	DBG("Secondary DNS Server: %s\n", dns2);
+}
+
+static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data)
+{
+	struct ofono_emulator *e = user_data;
+
+	DBG("");
+
+	g_at_ppp_unref(e->ppp);
+	e->ppp = NULL;
+
+	if (e->server == NULL)
+		return;
+
+	g_at_server_resume(e->server);
+
+	g_at_server_send_final(e->server, G_AT_SERVER_RESULT_NO_CARRIER);
+}
+
+static gboolean setup_ppp(gpointer user_data)
+{
+	struct ofono_emulator *e = user_data;
+	GAtServer *server = e->server;
+	GAtIO *io;
+
+	DBG("");
+
+	io = g_at_server_get_io(server);
+
+	g_at_server_suspend(server);
+
+	e->ppp = g_at_ppp_server_new_from_io(io, DUN_SERVER_ADDRESS);
+	if (e->ppp == NULL) {
+		g_at_server_resume(server);
+		return FALSE;
+	}
+
+	g_at_ppp_set_server_info(e->ppp, DUN_PEER_ADDRESS,
+					DUN_DNS_SERVER_1, DUN_DNS_SERVER_2);
+
+	g_at_ppp_set_credentials(e->ppp, "", "");
+	g_at_ppp_set_debug(e->ppp, ofono_emulator_debug, "PPP");
+
+	g_at_ppp_set_connect_function(e->ppp, ppp_connect, e);
+	g_at_ppp_set_disconnect_function(e->ppp, ppp_disconnect, e);
+
+	return FALSE;
+}
+
+static gboolean dial_call(struct ofono_emulator *e, const char *dial_str)
+{
+	char c = *dial_str;
+
+	DBG("dial call %s", dial_str);
+
+	if (c == '*' || c == '#' || c == 'T' || c == 't') {
+
+		g_at_server_send_intermediate(e->server, "CONNECT");
+		g_idle_add(setup_ppp, e);
+	}
+
+	return TRUE;
+}
+
+static void dial_cb(GAtServerRequestType type, GAtResult *result,
+					gpointer user_data)
+{
+	struct ofono_emulator *e = user_data;
+	GAtServer *server = e->server;
+	GAtResultIter iter;
+	const char *dial_str;
+
+	DBG("");
+
+	if (type != G_AT_SERVER_REQUEST_TYPE_SET)
+		goto error;
+
+	g_at_result_iter_init(&iter, result);
+
+	if (!g_at_result_iter_next(&iter, "D"))
+		goto error;
+
+	dial_str = g_at_result_iter_raw_line(&iter);
+	if (!dial_str)
+		goto error;
+
+	if (e->ppp)
+		goto error;
+
+	if (!dial_call(e, dial_str))
+		goto error;
+
+	return;
+
+error:
+	g_at_server_send_final(server, G_AT_SERVER_RESULT_ERROR);
+}
+
+void ofono_emulator_disable(struct ofono_emulator *e)
+{
+	DBG("");
+
+	g_at_server_shutdown(e->server);
+	g_at_server_unref(e->server);
+	e->server = NULL;
+}
+
+static void emulator_disconnect_cb(gpointer user_data)
+{
+	struct ofono_emulator *e = user_data;
+
+	if (e == NULL)
+		return;
+
+	ofono_emulator_disable(e);
+}
+
+int ofono_emulator_enable(struct ofono_emulator *e, int fd)
+{
+	GIOChannel *io;
+
+	if (fd < 0)
+		return -EIO;
+
+	io = g_io_channel_unix_new(fd);
+
+	e->server = g_at_server_new(io);
+	if (!e->server) {
+		g_free(e);
+		return -ENOMEM;
+	}
+
+	g_at_server_set_debug(e->server, ofono_emulator_debug, "Server");
+	g_at_server_set_disconnect_function(e->server,
+						emulator_disconnect_cb, e);
+
+	return 0;
+}
+
+static void emulator_remove(struct ofono_atom *atom)
+{
+	struct ofono_emulator *e =  __ofono_atom_get_data(atom);
+
+	DBG("");
+
+	if (e->server)
+		ofono_emulator_disable(e);
+
+	if (e->driver->remove)
+		e->driver->remove();
+
+	g_free(e);
+}
+
+struct ofono_emulator *ofono_emulator_create(struct ofono_modem *modem,
+					struct ofono_emulator_driver *driver)
+{
+	struct ofono_emulator *e;
+
+	DBG("");
+
+	if (driver->probe == NULL)
+		return NULL;
+
+	e = g_try_new0(struct ofono_emulator, 1);
+	if (!e)
+		return NULL;
+
+	e->modem = modem;
+	e->driver = driver;
+
+	if (driver->probe(e) < 0) {
+		g_free(e);
+		return NULL;
+	}
+
+	return e;
+}
+
+void __ofono_emulator_probe_drivers(struct ofono_modem *modem)
+{
+	struct ofono_emulator_driver *driver;
+	GSList *l;
+
+	for (l = emulator_drivers; l; l = l->next) {
+		struct ofono_emulator *e;
+
+		driver = l->data;
+
+		e = ofono_emulator_create(modem, driver);
+		if (!e)
+			continue;
+
+		e->atom = __ofono_modem_add_atom(modem, driver->type,
+							emulator_remove, e);
+	}
+}
+
+int ofono_emulator_driver_register(const struct ofono_emulator_driver *driver)
+{
+	DBG("driver: %p name: %s", driver, driver->name);
+
+	emulator_drivers = g_slist_prepend(emulator_drivers, (void *)driver);
+
+	return 0;
+}
+
+void ofono_emulator_driver_unregister(
+				const struct ofono_emulator_driver *driver)
+{
+	DBG("driver: %p name: %s", driver, driver->name);
+
+	emulator_drivers = g_slist_remove(emulator_drivers, driver);
+}
diff --git a/src/ofono.h b/src/ofono.h
index 6ba0187..64d13e7 100644
--- a/src/ofono.h
+++ b/src/ofono.h
@@ -128,6 +128,7 @@ enum ofono_atom_type {
 	OFONO_ATOM_TYPE_CTM,
 	OFONO_ATOM_TYPE_CDMA_VOICECALL_MANAGER,
 	OFONO_ATOM_TYPE_SIM_AUTH,
+	OFONO_ATOM_TYPE_EMULATOR_DUN,
 };
 
 enum ofono_atom_watch_condition {
@@ -430,3 +431,7 @@ ofono_bool_t __ofono_gprs_provision_get_settings(const char *mcc,
 void __ofono_gprs_provision_free_settings(
 				struct ofono_gprs_provision_data *settings,
 				int count);
+
+#include <ofono/emulator.h>
+
+void __ofono_emulator_probe_drivers(struct ofono_modem *modem);
-- 
1.7.4.rc3


  reply	other threads:[~2011-01-31 20:51 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-31 20:51 [PATCH 1/8] bluetooth: add server support Gustavo F. Padovan
2011-01-31 20:51 ` [PATCH 2/8] bluetooth: add support to register Bluetooth Service Gustavo F. Padovan
2011-01-31 20:51   ` [PATCH 3/8] bluetooth: add Request auth code for new connections Gustavo F. Padovan
2011-01-31 20:51     ` [PATCH 4/8] include: add public headed to emulator atom Gustavo F. Padovan
2011-01-31 20:51       ` Gustavo F. Padovan [this message]
2011-01-31 20:52         ` [PATCH 6/8] dun_gw: Add DUN server plugin for oFono Gustavo F. Padovan
2011-01-31 20:52           ` [PATCH 7/8] emulator: Implement dialing up for DUN Gustavo F. Padovan
2011-01-31 20:52             ` [PATCH 8/8] gsmdial: add option for Bluetooth DUN dialing Gustavo F. Padovan
2011-02-01 14:25   ` [PATCH 2/8] bluetooth: add support to register Bluetooth Service Frederic Danis
2011-02-01 14:17 ` [PATCH 1/8] bluetooth: add server support Frederic Danis
2011-02-02 18:21   ` Gustavo F. Padovan
2011-02-03 15:56     ` Frederic Danis
2011-02-03 16:30       ` Gustavo F. Padovan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1296507122-10936-5-git-send-email-padovan@profusion.mobi \
    --to=padovan@profusion.mobi \
    --cc=ofono@ofono.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.