Skip to contentSkip to conversation

Add DUN server for oFono

PATCH New Zhenhua Zhang 1 participant 22 messages Sep 29, 2010 Kbuild

lore
Share & tools
lore
#0 lore raw

cover Add DUN server for oFono

Hi,

I rebase my DUN patches with latest git tree and combine them here. For some reason, I cann't continue to work on this feature. I hope someone interested about it could pick up these patches and continue this work.

Please feel free to give your comments.

Thanks,
Zhenhua

#1 lore raw

PATCH 1/21 bluetooth: Add reference count for bluetooth utils

Add bluetooth_ref()/bluetooth_unref() to support reference count in bluetooth utils.

---
 plugins/bluetooth.c |   62 +++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 45 insertions(+), 17 deletions(-)
plugins/bluetooth.c +45 -17
index 5a85eaa..10cc49d 100644
@@ -39,6 +39,7 @@
 static DBusConnection *connection;
 static GHashTable *uuid_hash = NULL;
 static GHashTable *adapter_address_hash = NULL;
+static gint ref_count;
 
 void bluetooth_create_path(const char *dev_addr, const char *adapter_addr,
 				char *buf, int size)
@@ -503,13 +504,10 @@ static guint adapter_added_watch;
 static guint adapter_removed_watch;
 static guint property_watch;
 
-int bluetooth_register_uuid(const char *uuid, struct bluetooth_profile *profile)
+static int bluetooth_init()
 {
 	int err;
 
-	if (uuid_hash)
-		goto done;
-
 	connection = ofono_dbus_get_connection();
 
 	bluetooth_watch = g_dbus_add_service_watch(connection, BLUEZ_SERVICE,
@@ -542,13 +540,6 @@ int bluetooth_register_uuid(const char *uuid, struct bluetooth_profile *profile)
 	adapter_address_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
 						g_free, g_free);
 
-done:
-	g_hash_table_insert(uuid_hash, g_strdup(uuid), profile);
-
-	bluetooth_send_with_reply("/", BLUEZ_MANAGER_INTERFACE, "GetProperties",
-				manager_properties_cb, NULL, NULL, -1,
-				DBUS_TYPE_INVALID);
-
 	return 0;
 
 remove:
@@ -556,14 +547,27 @@ remove:
 	g_dbus_remove_watch(connection, adapter_added_watch);
 	g_dbus_remove_watch(connection, adapter_removed_watch);
 	g_dbus_remove_watch(connection, property_watch);
+
 	return err;
 }
 
-void bluetooth_unregister_uuid(const char *uuid)
+static int bluetooth_ref()
 {
-	g_hash_table_remove(uuid_hash, uuid);
+	g_atomic_int_inc(&ref_count);
+
+	if (ref_count > 1)
+		return 0;
+
+	return bluetooth_init();
+}
+
+static void bluetooth_unref()
+{
+	gboolean is_zero;
+
+	is_zero = g_atomic_int_dec_and_test(&ref_count);
 
-	if (g_hash_table_size(uuid_hash))
+	if (is_zero == FALSE)
 		return;
 
 	g_dbus_remove_watch(connection, bluetooth_watch);
@@ -571,9 +575,33 @@ void bluetooth_unregister_uuid(const char *uuid)
 	g_dbus_remove_watch(connection, adapter_removed_watch);
 	g_dbus_remove_watch(connection, property_watch);
 
-	g_hash_table_destroy(uuid_hash);
-	g_hash_table_destroy(adapter_address_hash);
-	uuid_hash = NULL;
+	if (uuid_hash)
+		g_hash_table_destroy(uuid_hash);
+
+	if (adapter_address_hash)
+		g_hash_table_destroy(adapter_address_hash);
+}
+
+int bluetooth_register_uuid(const char *uuid, struct bluetooth_profile *profile)
+{
+	int err = bluetooth_ref();
+
+	if (err != 0)
+		return err;
+
+	g_hash_table_insert(uuid_hash, g_strdup(uuid), profile);
+
+	bluetooth_send_with_reply("/", BLUEZ_MANAGER_INTERFACE, "GetProperties",
+				manager_properties_cb, NULL, NULL, -1,
+				DBUS_TYPE_INVALID);
+	return 0;
+}
+
+void bluetooth_unregister_uuid(const char *uuid)
+{
+	g_hash_table_remove(uuid_hash, uuid);
+
+	bluetooth_unref();
 }
 
 OFONO_PLUGIN_DEFINE(bluetooth, "Bluetooth Utils Plugins", VERSION,
Signature · 1 line
1.7.0.4
#2 lore raw

PATCH 2/21 bluetooth: Add Btio library for DUN

In reply to Zhenhua Zhang

Btio library is the low level socket API for BT RFCOMM connection. We share the same library among BlueZ, Obex and oFono. So make sure you synchronize to other two projects when you make changes to btio.[ch].

---
 Makefile.am  |    8 +-
 btio/btio.c  | 1299 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 btio/btio.h  |   97 +++++
 configure.ac |    5 +
 4 files changed, 1407 insertions(+), 2 deletions(-)
 create mode 100644 btio/btio.c
 create mode 100644 btio/btio.h
Makefile.am +6 -2
index 957dd82..d4542e8 100644
@@ -72,6 +72,8 @@ gatchat_sources = gatchat/gatchat.h gatchat/gatchat.c \
 				gatchat/ppp_auth.c gatchat/ppp_net.c \
 				gatchat/ppp_ipcp.c
 
+btio_sources = btio/btio.h btio/btio.c
+
 udev_files = plugins/ofono.rules
 
 if UDEV
@@ -298,7 +300,8 @@ src_ofonod_SOURCES = $(gdbus_sources) $(builtin_sources) src/ofono.ver \
 			src/nettime.c src/stkagent.c src/stkagent.h \
 			src/simfs.c src/simfs.h
 
-src_ofonod_LDADD = $(builtin_libadd) @GLIB_LIBS@ @DBUS_LIBS@ @CAPNG_LIBS@ -ldl
+src_ofonod_LDADD = $(builtin_libadd) @GLIB_LIBS@ @DBUS_LIBS@ @CAPNG_LIBS@ \
+			@BLUEZ_LIBS@ -ldl
 
 src_ofonod_LDFLAGS = -Wl,--export-dynamic \
 				-Wl,--version-script=$(srcdir)/src/ofono.ver
@@ -318,7 +321,8 @@ AM_CFLAGS = @DBUS_CFLAGS@ @GLIB_CFLAGS@ @CAPNG_CFLAGS@ $(builtin_cflags) \
 					-DPLUGINDIR=\""$(build_plugindir)"\"
 
 INCLUDES = -I$(builddir)/include -I$(builddir)/src -I$(srcdir)/src \
-			-I$(srcdir)/gdbus -I$(srcdir)/gisi -I$(srcdir)/gatchat
+			-I$(srcdir)/gdbus -I$(srcdir)/gisi -I$(srcdir)/gatchat \
+			-I$(srcdir)/btio
 
 doc_files = doc/overview.txt doc/ofono-paper.txt \
 		doc/manager-api.txt doc/modem-api.txt doc/network-api.txt \
btio/btio.c new file +1299 -0

1,299 lines not on this page · raw message

btio/btio.h new file +97 -0

97 lines not on this page · raw message

configure.ac +5 -0
index 74c8fc2..c460636 100644
@@ -102,6 +102,11 @@ else
 fi
 AC_SUBST(DBUS_CONFDIR)
 
+PKG_CHECK_MODULES(BLUEZ, bluez, dummy=yes,
+				AC_MSG_ERROR(libbluetooth is required))
+AC_SUBST(BLUEZ_CFLAGS)
+AC_SUBST(BLUEZ_LIBS)
+
 AC_ARG_ENABLE(udev, AC_HELP_STRING([--disable-udev],
 			[don't use udev support even if available]),
 						[enable_udev=${enableval}])
Signature · 1 line
1.7.0.4
#3 lore raw

PATCH 3/21 bluetooth: Add bluetooth server support for DUN

In reply to Zhenhua Zhang

It watches Bluetooth adapter property changes and addes DUN record to listen DUN client connection request.

---
 plugins/bluetooth.c |  379 +++++++++++++++++++++++++++++++++++++++++++++++++++
 plugins/bluetooth.h |   14 ++
 2 files changed, 393 insertions(+), 0 deletions(-)
plugins/bluetooth.c +379 -0
index 10cc49d..0577747 100644
@@ -34,13 +34,72 @@
 
 #include <ofono/dbus.h>
 
+#include <btio.h>
 #include "bluetooth.h"
 
 static DBusConnection *connection;
 static GHashTable *uuid_hash = NULL;
 static GHashTable *adapter_address_hash = NULL;
+static GSList *server_list = NULL;
 static gint ref_count;
 
+static const gchar *dun_record = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>	\
+<record>									\
+  <attribute id=\"0x0001\">							\
+    <sequence>									\
+      <uuid value=\"0x1103\"/>							\
+    </sequence>									\
+  </attribute>									\
+										\
+  <attribute id=\"0x0004\">							\
+    <sequence>									\
+      <sequence>								\
+        <uuid value=\"0x0100\"/>						\
+      </sequence>								\
+      <sequence>								\
+        <uuid value=\"0x0003\"/>						\
+        <uint8 value=\"%u\" name=\"channel\"/>					\
+      </sequence>								\
+      <sequence>								\
+        <uuid value=\"0x0008\"/>						\
+      </sequence>								\
+    </sequence>									\
+  </attribute>									\
+										\
+  <attribute id=\"0x0009\">							\
+    <sequence>									\
+      <sequence>								\
+        <uuid value=\"0x1103\"/>						\
+        <uint16 value=\"0x0100\" name=\"version\"/>				\
+      </sequence>								\
+    </sequence>									\
+  </attribute>									\
+										\
+  <attribute id=\"0x0100\">							\
+    <text value=\"%s\" name=\"name\"/>						\
+  </attribute>									\
+</record>";
+
+#define TIMEOUT (60*1000) /* Timeout for user response (miliseconds) */
+
+struct client {
+	GIOChannel	*io;
+	guint		watch;
+};
+
+struct server {
+	guint16		service;
+	gchar		*name;
+	guint8		channel;
+	GIOChannel	*io;
+	gchar		*adapter;
+	guint		handle;
+	ConnectFunc	connect_cb;
+	gpointer	user_data;
+
+	struct client	client;
+};
+
 void bluetooth_create_path(const char *dev_addr, const char *adapter_addr,
 				char *buf, int size)
 {
@@ -370,6 +429,253 @@ static gboolean property_changed(DBusConnection *connection, DBusMessage *msg,
 	return TRUE;
 }
 
+static void disconnect(struct server *server)
+{
+	struct client *client = &server->client;
+
+	if (!client->io)
+		return;
+
+	g_io_channel_unref(client->io);
+	client->io = NULL;
+
+	if (client->watch > 0) {
+		g_source_remove(client->watch);
+		client->watch = 0;
+	}
+
+	return;
+}
+
+static void server_stop(gpointer data, gpointer user_data)
+{
+	struct server *server = data;
+
+	disconnect(server);
+
+	if (server->handle) {
+		DBusMessage *msg;
+
+		msg = dbus_message_new_method_call(BLUEZ_SERVICE,
+						server->adapter,
+						BLUEZ_SERVICE_INTERFACE,
+						"RemoveRecord");
+		dbus_message_append_args(msg, DBUS_TYPE_UINT32, &server->handle,
+						DBUS_TYPE_INVALID);
+		g_dbus_send_message(connection, msg);
+
+		server->handle = 0;
+	}
+
+	if (server->io) {
+		g_io_channel_shutdown(server->io, TRUE, NULL);
+		g_io_channel_unref(server->io);
+		server->io = NULL;
+	}
+
+	g_free(server->adapter);
+	server->adapter = NULL;
+}
+
+static gboolean client_event(GIOChannel *chan, GIOCondition cond, gpointer data)
+{
+	struct server *server = data;
+
+	disconnect(server);
+
+	return FALSE;
+}
+
+static void cancel_authorization(struct server *server)
+{
+	DBusMessage *msg;
+
+	if (!server->adapter)
+		return;
+
+	msg = dbus_message_new_method_call(BLUEZ_SERVICE, server->adapter,
+						BLUEZ_SERVICE_INTERFACE,
+						"CancelAuthorization");
+
+	g_dbus_send_message(connection, msg);
+}
+
+static void auth_cb(DBusPendingCall *call, gpointer user_data)
+{
+	struct server *server = user_data;
+	struct client *client = &server->client;
+	GIOChannel *io = client->io;
+	DBusMessage *reply = dbus_pending_call_steal_reply(call);
+	DBusError derr;
+	GError *err = NULL;
+
+	dbus_error_init(&derr);
+
+	if (dbus_set_error_from_message(&derr, reply)) {
+		ofono_error("RequestAuthorization error: %s, %s",
+				derr.name, derr.message);
+
+		if (dbus_error_has_name(&derr, DBUS_ERROR_NO_REPLY))
+			cancel_authorization(server);
+
+		dbus_error_free(&derr);
+		goto failed;
+	}
+
+	ofono_info("RequestAuthorization succeeded");
+
+	if (!bt_io_accept(io, server->connect_cb, server->user_data,
+						NULL, &err)) {
+		ofono_error("%s", err->message);
+		g_error_free(err);
+		goto failed;
+	}
+
+	g_source_remove(client->watch);
+
+	client->watch = g_io_add_watch(client->io,
+					G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+					client_event, server);
+
+	dbus_message_unref(reply);
+
+	return;
+
+failed:
+	dbus_message_unref(reply);
+	disconnect(server);
+}
+
+static gboolean auth_watch(GIOChannel *io, GIOCondition cond,
+						gpointer user_data)
+{
+	struct server *server = user_data;
+
+	cancel_authorization(server);
+	disconnect(server);
+
+	return FALSE;
+}
+
+static void confirm_event(GIOChannel *io, gpointer user_data)
+{
+	struct server *server = user_data;
+	struct client *client = &server->client;
+	GError *err = NULL;
+	char address[18];
+	const char *addr;
+	guint8 channel;
+	int ret;
+
+	if (client->io) {
+		ofono_error("Rejecting connection since one client already \
+				connected");
+		return;
+	}
+
+	bt_io_get(io, BT_IO_RFCOMM, &err, BT_IO_OPT_DEST, address,
+					BT_IO_OPT_CHANNEL, &channel,
+					BT_IO_OPT_INVALID);
+	if (err) {
+		ofono_error("%s", err->message);
+		g_error_free(err);
+		return;
+	}
+
+	ofono_info("New connection from: %s, channel %u", address, channel);
+
+	addr = address;
+	ret = bluetooth_send_with_reply(server->adapter,
+					BLUEZ_SERVICE_INTERFACE,
+					"RequestAuthorization",
+					auth_cb, server, NULL, TIMEOUT,
+					DBUS_TYPE_STRING, &addr,
+					DBUS_TYPE_UINT32, &server->handle,
+					DBUS_TYPE_INVALID);
+	if (ret < 0) {
+		ofono_error("Request Bluetooth authorization failed");
+		return;
+	}
+
+	ofono_info("RequestAuthorization(%s, 0x%x)", address, server->handle);
+
+	client->io = g_io_channel_ref(io);
+	client->watch = g_io_add_watch(io, G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+					auth_watch, server);
+
+	return;
+}
+
+static void add_record_cb(DBusPendingCall *call, gpointer user_data)
+{
+	struct server *server = user_data;
+	DBusMessage *reply = dbus_pending_call_steal_reply(call);
+	DBusError derr;
+	guint32 handle;
+
+	dbus_error_init(&derr);
+
+	if (dbus_set_error_from_message(&derr, reply)) {
+		ofono_error("Replied with an error: %s, %s",
+					derr.name, derr.message);
+		dbus_error_free(&derr);
+		server_stop(server, NULL);
+		goto done;
+	}
+
+	dbus_message_get_args(reply, NULL, DBUS_TYPE_UINT32, &handle,
+					DBUS_TYPE_INVALID);
+	server->handle = handle;
+
+	ofono_info("Registered: %s, handle: 0x%x", server->name, handle);
+
+done:
+	dbus_message_unref(reply);
+}
+
+static void server_start(gpointer data, gpointer user_data)
+{
+	struct server *server = data;
+	gchar *path = user_data;
+	GError *err = NULL;
+	gchar *xml;
+
+	if (server->handle != 0)
+		return;
+
+	if (server->service != DUN_GW)
+		goto failed;
+
+	server->io = bt_io_listen(BT_IO_RFCOMM, NULL, confirm_event,
+					server, NULL, &err,
+					BT_IO_OPT_CHANNEL, server->channel,
+					BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
+					BT_IO_OPT_INVALID);
+	if (!server->io) {
+		ofono_error("Bluetooth %s register failed: %s",
+					server->name, err->message);
+		g_error_free(err);
+		goto failed;
+	}
+
+	server->adapter = g_strdup(path);
+
+	xml = g_markup_printf_escaped(dun_record, server->channel,
+					server->name);
+
+	bluetooth_send_with_reply(path, BLUEZ_SERVICE_INTERFACE, "AddRecord",
+					add_record_cb, server, NULL, -1,
+					DBUS_TYPE_STRING, &xml,
+					DBUS_TYPE_INVALID);
+
+	g_free(xml);
+
+	return;
+
+failed:
+	server_stop(server, NULL);
+}
+
 static void adapter_properties_cb(DBusPendingCall *call, gpointer user_data)
 {
 	const char *path = user_data;
@@ -394,6 +700,9 @@ static void adapter_properties_cb(DBusPendingCall *call, gpointer user_data)
 	g_hash_table_insert(adapter_address_hash,
 				g_strdup(path), g_strdup(addr));
 
+	if (server_list)
+		g_slist_foreach(server_list, server_start, (gpointer)path);
+
 	for (l = device_list; l; l = l->next) {
 		const char *device = l->data;
 
@@ -428,11 +737,26 @@ static gboolean adapter_removed(DBusConnection *connection,
 				DBusMessage *message, void *user_data)
 {
 	const char *path;
+	GSList *l;
 
 	if (dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
 				DBUS_TYPE_INVALID) == TRUE)
 		g_hash_table_remove(adapter_address_hash, path);
 
+	for (l = server_list; l; l = l->next) {
+		struct server *server = l->data;
+
+		if (!server->adapter)
+			continue;
+
+		if (!g_str_equal(path, server->adapter))
+			continue;
+
+		/* Don't remove handle if the adapter has been removed */
+		server->handle = 0;
+		server_stop(server, NULL);
+	}
+
 	return TRUE;
 }
 
@@ -564,6 +888,7 @@ static int bluetooth_ref()
 static void bluetooth_unref()
 {
 	gboolean is_zero;
+	GSList *l;
 
 	is_zero = g_atomic_int_dec_and_test(&ref_count);
 
@@ -580,6 +905,20 @@ static void bluetooth_unref()
 
 	if (adapter_address_hash)
 		g_hash_table_destroy(adapter_address_hash);
+
+	if (server_list == NULL)
+		return;
+
+	for (l = server_list; l; l = l->next) {
+		struct server *server = l->data;
+
+		server_stop(server, NULL);
+		g_free(server->name);
+		g_free(server);
+	}
+
+	g_slist_free(server_list);
+	server_list = NULL;
 }
 
 int bluetooth_register_uuid(const char *uuid, struct bluetooth_profile *profile)
@@ -604,5 +943,45 @@ void bluetooth_unregister_uuid(const char *uuid)
 	bluetooth_unref();
 }
 
+struct server *bluetooth_register_server(guint16 service, char *name,
+						guint8 channel, ConnectFunc cb,
+						gpointer user_data)
+{
+	struct server *server;
+	int err = bluetooth_ref();
+
+	if (err != 0)
+		return NULL;
+
+	server = g_try_new0(struct server, 1);
+	if (!server)
+		return NULL;
+
+	server->service = service;
+	server->name = g_strdup(name);
+	server->channel = channel;
+	server->connect_cb = cb;
+	server->user_data = user_data;
+
+	server_list = g_slist_prepend(server_list, server);
+
+	bluetooth_send_with_reply("/", BLUEZ_MANAGER_INTERFACE, "GetProperties",
+					manager_properties_cb, NULL, NULL, -1,
+					DBUS_TYPE_INVALID);
+
+	return server;
+}
+
+void bluetooth_unregister_server(struct server *server)
+{
+	server_list = g_slist_remove(server_list, server);
+
+	server_stop(server, NULL);
+	g_free(server->name);
+	g_free(server);
+
+	bluetooth_unref();
+}
+
 OFONO_PLUGIN_DEFINE(bluetooth, "Bluetooth Utils Plugins", VERSION,
 			OFONO_PLUGIN_PRIORITY_DEFAULT, NULL, NULL)
plugins/bluetooth.h +14 -0
index b70bb0c..1780e85 100644
@@ -2,6 +2,7 @@
  *  oFono - Open Source Telephony
  *
  *  Copyright (C) 2010 Gustavo F. Padovan <[email protected]>
+ *  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
@@ -22,6 +23,7 @@
 #define	BLUEZ_MANAGER_INTERFACE		BLUEZ_SERVICE ".Manager"
 #define	BLUEZ_ADAPTER_INTERFACE		BLUEZ_SERVICE ".Adapter"
 #define	BLUEZ_DEVICE_INTERFACE		BLUEZ_SERVICE ".Device"
+#define	BLUEZ_SERVICE_INTERFACE		BLUEZ_SERVICE ".Service"
 
 #define DBUS_TIMEOUT 15
 
@@ -30,6 +32,9 @@
 /* Profiles bitfield */
 #define HFP_AG 0x01
 
+/* Service bitfield */
+#define DUN_GW 0x01
+
 struct bluetooth_profile {
 	const char *name;
 	int (*create)(const char *device, const char *dev_addr,
@@ -38,10 +43,19 @@ struct bluetooth_profile {
 	void (*set_alias)(const char *device, const char *);
 };
 
+typedef void (*ConnectFunc)(GIOChannel *io, GError *err, gpointer user_data);
+
+struct server;
+
 int bluetooth_register_uuid(const char *uuid,
 				struct bluetooth_profile *profile);
 void bluetooth_unregister_uuid(const char *uuid);
 
+struct server *bluetooth_register_server(guint16 service, char *name,
+						guint8 channel, ConnectFunc cb,
+						gpointer user_data);
+void bluetooth_unregister_server(struct server *server);
+
 void bluetooth_create_path(const char *dev_addr, const char *adapter_addr,
 							char *buf, int size);
 
Signature · 1 line
1.7.0.4
#4 lore raw

PATCH 4/21 emulator: Add emulator atom in oFono

In reply to Zhenhua Zhang

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.

---
 Makefile.am        |    4 +-
 include/emulator.h |   54 +++++++++++++++
 src/emulator.c     |  189 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/modem.c        |    2 +
 src/ofono.h        |    5 ++
 5 files changed, 252 insertions(+), 2 deletions(-)
 create mode 100644 include/emulator.h
 create mode 100644 src/emulator.c
Makefile.am +2 -2
index d4542e8..c509050 100644
@@ -13,7 +13,7 @@ include_HEADERS = include/log.h include/plugin.h include/history.h \
 			include/cbs.h include/call-volume.h \
 			include/gprs.h include/gprs-context.h \
 			include/radio-settings.h include/stk.h \
-			include/nettime.h
+			include/nettime.h include/emulator.h
 
 nodist_include_HEADERS = include/version.h
 
@@ -298,7 +298,7 @@ src_ofonod_SOURCES = $(gdbus_sources) $(builtin_sources) src/ofono.ver \
 			src/gprs.c src/idmap.h src/idmap.c \
 			src/radio-settings.c src/stkutil.h src/stkutil.c \
 			src/nettime.c src/stkagent.c src/stkagent.h \
-			src/simfs.c src/simfs.h
+			src/simfs.c src/simfs.h src/emulator.c
 
 src_ofonod_LDADD = $(builtin_libadd) @GLIB_LIBS@ @DBUS_LIBS@ @CAPNG_LIBS@ \
 			@BLUEZ_LIBS@ -ldl
include/emulator.h new file +54 -0

54 lines not on this page · raw message

src/emulator.c new file +189 -0

189 lines not on this page · raw message

src/modem.c +2 -0
index 9128f0d..534d85b 100644
@@ -403,6 +403,8 @@ static void modem_change_state(struct ofono_modem *modem,
 	case MODEM_STATE_ONLINE:
 		if (driver->post_online)
 			driver->post_online(modem);
+
+		__ofono_emulator_probe_drivers(modem);
 		break;
 	}
 }
src/ofono.h +5 -0
index 41ce011..29cfe57 100644
@@ -122,6 +122,7 @@ enum ofono_atom_type {
 	OFONO_ATOM_TYPE_RADIO_SETTINGS = 18,
 	OFONO_ATOM_TYPE_STK = 19,
 	OFONO_ATOM_TYPE_NETTIME = 20,
+	OFONO_ATOM_TYPE_EMULATOR_DUN = 21,
 };
 
 enum ofono_atom_watch_condition {
@@ -334,3 +335,7 @@ void __ofono_nettime_probe_drivers(struct ofono_modem *modem);
 
 void __ofono_nettime_info_received(struct ofono_modem *modem,
 					struct ofono_network_time *info);
+
+#include <ofono/emulator.h>
+
+void __ofono_emulator_probe_drivers(struct ofono_modem *modem);
Signature · 1 line
1.7.0.4
#5 lore raw

PATCH 5/21 dun_gw: Add DUN server plugin for oFono

In reply to Zhenhua Zhang

DUN server is probed when modem state changes to online. It registers DUN record to Bluetooth adapter and wait for incoming DUN connection.

---
 Makefile.am      |    3 +
 plugins/dun_gw.c |  113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 116 insertions(+), 0 deletions(-)
 create mode 100644 plugins/dun_gw.c
Makefile.am +3 -0
index c509050..9ef6858 100644
@@ -264,6 +264,9 @@ builtin_sources += plugins/bluetooth.c plugins/bluetooth.h
 builtin_modules += hfp
 builtin_sources += plugins/hfp.c plugins/bluetooth.h
 
+builtin_modules += dun_gw
+builtin_sources += $(btio_sources) plugins/dun_gw.c plugins/bluetooth.h
+
 builtin_modules += palmpre
 builtin_sources += plugins/palmpre.c
 
plugins/dun_gw.c new file +113 -0

113 lines not on this page · raw message

Signature · 1 line
1.7.0.4
#6 lore raw

PATCH 6/21 gprs: Rename status_watch to netreg_status_watch

In reply to Zhenhua Zhang

To be more precise and avoid name conflict with dun_status_watch.

---
 src/gprs.c |   15 ++++++++-------
 1 files changed, 8 insertions(+), 7 deletions(-)
src/gprs.c +8 -7
index 3f085ed..6e71b50 100644
@@ -75,7 +75,7 @@ struct ofono_gprs {
 	int netreg_status;
 	struct ofono_netreg *netreg;
 	unsigned int netreg_watch;
-	unsigned int status_watch;
+	unsigned int netreg_status_watch;
 	GKeyFile *settings;
 	char *imsi;
 	DBusMessage *pending;
@@ -1741,10 +1741,10 @@ static void gprs_unregister(struct ofono_atom *atom)
 	}
 
 	if (gprs->netreg_watch) {
-		if (gprs->status_watch) {
+		if (gprs->netreg_status_watch) {
 			__ofono_netreg_remove_status_watch(gprs->netreg,
-							gprs->status_watch);
-			gprs->status_watch = 0;
+						gprs->netreg_status_watch);
+			gprs->netreg_status_watch = 0;
 		}
 
 		__ofono_modem_remove_atom_watch(modem, gprs->netreg_watch);
@@ -1831,15 +1831,16 @@ static void netreg_watch(struct ofono_atom *atom,
 	struct ofono_gprs *gprs = data;
 
 	if (cond == OFONO_ATOM_WATCH_CONDITION_UNREGISTERED) {
-		gprs->status_watch = 0;
+		gprs->netreg_status_watch = 0;
 		gprs->netreg = NULL;
 		return;
 	}
 
 	gprs->netreg = __ofono_atom_get_data(atom);
 	gprs->netreg_status = ofono_netreg_get_status(gprs->netreg);
-	gprs->status_watch = __ofono_netreg_add_status_watch(gprs->netreg,
-					netreg_status_changed, gprs, NULL);
+	gprs->netreg_status_watch = __ofono_netreg_add_status_watch(
+					gprs->netreg, netreg_status_changed,
+					gprs, NULL);
 
 	gprs_netreg_update(gprs);
 }
Signature · 1 line
1.7.0.4
#7 lore raw

PATCH 7/21 emulator: Add status watches for ofono emulator

In reply to Zhenhua Zhang

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(-)
include/emulator.h +6 -0
index 2691928..99ae218 100644
@@ -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;
src/emulator.c +54 -0

84 lines not on this page · raw message

src/ofono.h +11 -0
index 29cfe57..4cc03f9 100644
@@ -339,3 +339,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);
Signature · 1 line
1.7.0.4
#8 lore raw

PATCH 8/21 emulator: Add emulator status watches in gprs atom

In reply to Zhenhua Zhang

Add dun_watch to watch emulator status update in gprs atom. So that gprs atom could get notified when we get request to create new PPP connection from DUN client;

---
 src/gprs.c |   60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 60 insertions(+), 0 deletions(-)
src/gprs.c +60 -0
index 6e71b50..8d7fefc 100644
@@ -83,6 +83,9 @@ struct ofono_gprs {
 	const struct ofono_gprs_driver *driver;
 	void *driver_data;
 	struct ofono_atom *atom;
+	struct ofono_emulator *dun;
+	unsigned int dun_watch;
+	unsigned int dun_status_watch;
 };
 
 struct ofono_gprs_context {
@@ -1603,6 +1606,39 @@ void ofono_gprs_context_deactivated(struct ofono_gprs_context *gc,
 	}
 }
 
+static void dun_status_watch(struct ofono_emulator *e,
+					enum ofono_emulator_status status,
+					void *data, void *user_data)
+{
+	if (e == NULL)
+		return;
+
+	switch (status) {
+	default:
+		break;
+	}
+}
+
+static void dun_watch(struct ofono_atom *atom,
+					enum ofono_atom_watch_condition cond,
+					void *data)
+{
+	struct ofono_gprs *gprs = data;
+	struct ofono_emulator *e = __ofono_atom_get_data(atom);
+
+	if (cond == OFONO_ATOM_WATCH_CONDITION_UNREGISTERED) {
+		gprs->dun_status_watch = 0;
+		gprs->dun = NULL;
+		return;
+	}
+
+	DBG("");
+
+	gprs->dun = e;
+	gprs->dun_status_watch = __ofono_emulator_add_status_watch(e,
+						dun_status_watch, gprs, NULL);
+}
+
 int ofono_gprs_context_driver_register(const struct ofono_gprs_context_driver *d)
 {
 	DBG("driver: %p, name: %s", d, d->name);
@@ -1752,6 +1788,18 @@ static void gprs_unregister(struct ofono_atom *atom)
 		gprs->netreg = NULL;
 	}
 
+	if (gprs->dun_watch) {
+		if (gprs->dun_status_watch) {
+			__ofono_emulator_remove_status_watch(gprs->dun,
+							gprs->dun_status_watch);
+			gprs->dun_status_watch = 0;
+		}
+
+		__ofono_modem_remove_atom_watch(modem, gprs->dun_watch);
+		gprs->dun_watch = 0;
+		gprs->dun = NULL;
+	}
+
 	ofono_modem_remove_interface(modem,
 					OFONO_CONNECTION_MANAGER_INTERFACE);
 	g_dbus_unregister_interface(conn, path,
@@ -2028,6 +2076,7 @@ void ofono_gprs_register(struct ofono_gprs *gprs)
 	const char *path = __ofono_atom_get_path(gprs->atom);
 	struct ofono_atom *netreg_atom;
 	struct ofono_atom *sim_atom;
+	struct ofono_atom *dun_atom;
 
 	if (!g_dbus_register_interface(conn, path,
 					OFONO_CONNECTION_MANAGER_INTERFACE,
@@ -2064,6 +2113,17 @@ void ofono_gprs_register(struct ofono_gprs *gprs)
 		netreg_watch(netreg_atom,
 				OFONO_ATOM_WATCH_CONDITION_REGISTERED, gprs);
 
+	gprs->dun_watch = __ofono_modem_add_atom_watch(modem,
+						OFONO_ATOM_TYPE_EMULATOR_DUN,
+						dun_watch, gprs, NULL);
+
+	dun_atom = __ofono_modem_find_atom(modem,
+						OFONO_ATOM_TYPE_EMULATOR_DUN);
+
+	if (dun_atom && __ofono_atom_get_registered(dun_atom))
+		dun_watch(dun_atom,
+				OFONO_ATOM_WATCH_CONDITION_REGISTERED, gprs);
+
 	__ofono_atom_register(gprs->atom, gprs_unregister);
 }
 
Signature · 1 line
1.7.0.4
#9 lore raw

PATCH 9/21 emulator: Add APIs to send GAtServer result

In reply to Zhenhua Zhang

Defines APIs to send final/intermediate/unsolicited result to DUN client.
Please enter the commit message for your changes. Lines starting

---
 include/emulator.h |    2 ++
 src/emulator.c     |   28 ++++++++++++++++++++++++++++
 src/ofono.h        |   11 +++++++++++
 3 files changed, 41 insertions(+), 0 deletions(-)
include/emulator.h +2 -0
index 99ae218..0d5568c 100644
@@ -36,6 +36,8 @@ enum ofono_emulator_status {
 	OFONO_EMULATOR_STATUS_DESTROY,
 };
 
+enum _GAtServerResult;
+
 struct ofono_emulator_driver {
 	const char *name;
 	enum ofono_atom_type type;
src/emulator.c +28 -0
index c68dbf8..455af11 100644
@@ -82,6 +82,34 @@ static void ofono_emulator_release_id(int id)
 	ofono_emulator_ids &= ~(0x1 << id);
 }
 
+void ofono_emulator_send_final(struct ofono_emulator *e, GAtServerResult result)
+{
+	g_at_server_send_final(e->server, result);
+}
+
+void ofono_emulator_send_ext_final(struct ofono_emulator *e, const char *result)
+{
+	g_at_server_send_ext_final(e->server, result);
+}
+
+void ofono_emulator_send_intermediate(struct ofono_emulator *e,
+						const char *result)
+{
+	g_at_server_send_intermediate(e->server, result);
+}
+
+void ofono_emulator_send_unsolicited(struct ofono_emulator *e,
+						const char *result)
+{
+	g_at_server_send_unsolicited(e->server, result);
+}
+
+void ofono_emulator_send_info(struct ofono_emulator *e, const char *line,
+						gboolean last)
+{
+	g_at_server_send_info(e->server, line, last);
+}
+
 static void notify_status_watches(struct ofono_emulator *e, void *data)
 {
 	struct ofono_watchlist_item *item;
src/ofono.h +11 -0
index 4cc03f9..9ace0d1 100644
@@ -350,3 +350,14 @@ unsigned int __ofono_emulator_add_status_watch(struct ofono_emulator *e,
 
 gboolean __ofono_emulator_remove_status_watch(struct ofono_emulator *e,
 						unsigned int id);
+
+void ofono_emulator_send_final(struct ofono_emulator *e,
+					enum _GAtServerResult result);
+void ofono_emulator_send_ext_final(struct ofono_emulator *e,
+					const char *result);
+void ofono_emulator_send_intermediate(struct ofono_emulator *e,
+					const char *result);
+void ofono_emulator_send_unsolicited(struct ofono_emulator *e,
+					const char *result);
+void ofono_emulator_send_info(struct ofono_emulator *e, const char *line,
+					gboolean last);
Signature · 1 line
1.7.0.4
#10 lore raw

PATCH 10/21 emulator: Implement dialing up for DUN

In reply to Zhenhua Zhang

It handles client ATD*99# request and complete GPRS connection. Pass client IP address through IPCP packet to client side.

---
 include/emulator.h |   30 +++++++
 src/emulator.c     |  221 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 251 insertions(+), 0 deletions(-)
include/emulator.h +30 -0
index 0d5568c..e6eeb06 100644
@@ -34,6 +34,9 @@ enum ofono_emulator_status {
 	OFONO_EMULATOR_STATUS_IDLE = 0,
 	OFONO_EMULATOR_STATUS_CREATE,
 	OFONO_EMULATOR_STATUS_DESTROY,
+	OFONO_EMULATOR_STATUS_DUN_CONNECT,
+	OFONO_EMULATOR_STATUS_DUN_CONNECTED,
+	OFONO_EMULATOR_STATUS_DUN_DISCONNECTED,
 };
 
 enum _GAtServerResult;
@@ -46,6 +49,33 @@ struct ofono_emulator_driver {
 	void (*remove)();
 };
 
+typedef void (*ofono_emulator_cb_t)(enum _GAtServerResult res, void *data);
+
+struct ofono_emulator_req {
+	void *data;
+	ofono_emulator_cb_t cb;
+	void *cb_data;
+};
+
+typedef void (*ofono_emulator_gprs_connect_cb)(const struct ofono_error *error,
+						const char *interface,
+						const char *local,
+						const char *peer,
+						const char **dns, void *data);
+
+struct ofono_emulator_gprs_connect_req {
+	const char *dial_str;
+	ofono_emulator_gprs_connect_cb cb;
+	void *cb_data;
+};
+
+struct ofono_emulator_gprs_context_req {
+	const char *proto;
+	const char *apn;
+	ofono_emulator_cb_t cb;
+	void *cb_data;
+};
+
 int ofono_emulator_enable(struct ofono_emulator *emulator, GIOChannel *channel);
 void ofono_emulator_remove(gpointer user_data);
 struct ofono_emulator *ofono_emulator_create(struct ofono_modem *modem,
src/emulator.c +221 -0

251 lines not on this page · raw message

Signature · 1 line
1.7.0.4
#11 lore raw

PATCH 11/21 gprs: Make gprs_proto_to/from_string non-static

In reply to Zhenhua Zhang

So DUN server could share gprs_proto_to_string and gprs_proto_from_string.

---
 include/gprs-context.h |    3 +++
 src/gprs.c             |    5 ++---
 2 files changed, 5 insertions(+), 3 deletions(-)
include/gprs-context.h +3 -0
index f4c7321..de170b8 100644
@@ -84,6 +84,9 @@ void *ofono_gprs_context_get_data(struct ofono_gprs_context *gc);
 
 struct ofono_modem *ofono_gprs_context_get_modem(struct ofono_gprs_context *gc);
 
+gboolean gprs_proto_from_string(const char *str, enum ofono_gprs_proto *proto);
+const char *gprs_proto_to_string(enum ofono_gprs_proto proto);
+
 #ifdef __cplusplus
 }
 #endif
src/gprs.c +2 -3
index 8d7fefc..f3f86ec 100644
@@ -145,7 +145,7 @@ static enum gprs_context_type gprs_context_string_to_type(const char *str)
 	return GPRS_CONTEXT_TYPE_INVALID;
 }
 
-static const char *gprs_proto_to_string(enum ofono_gprs_proto proto)
+const char *gprs_proto_to_string(enum ofono_gprs_proto proto)
 {
 	switch (proto) {
 	case OFONO_GPRS_PROTO_IP:
@@ -157,8 +157,7 @@ static const char *gprs_proto_to_string(enum ofono_gprs_proto proto)
 	return NULL;
 }
 
-static gboolean gprs_proto_from_string(const char *str,
-					enum ofono_gprs_proto *proto)
+gboolean gprs_proto_from_string(const char *str, enum ofono_gprs_proto *proto)
 {
 	if (g_str_equal(str, "ip")) {
 		*proto = OFONO_GPRS_PROTO_IP;
Signature · 1 line
1.7.0.4
#12 lore raw

PATCH 12/21 gprs: ignore the case of proto str when comparing

In reply to Zhenhua Zhang

Use g_ascii_strcasecmp in gprs_proto_from_string when getting the GPRS protocol from string.

---
 src/gprs.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
src/gprs.c +2 -2
index f3f86ec..b39e361 100644
@@ -159,10 +159,10 @@ const char *gprs_proto_to_string(enum ofono_gprs_proto proto)
 
 gboolean gprs_proto_from_string(const char *str, enum ofono_gprs_proto *proto)
 {
-	if (g_str_equal(str, "ip")) {
+	if (g_ascii_strcasecmp(str, "ip")) {
 		*proto = OFONO_GPRS_PROTO_IP;
 		return TRUE;
-	} else if (g_str_equal(str, "ipv6")) {
+	} else if (g_ascii_strcasecmp(str, "ipv6")) {
 		*proto = OFONO_GPRS_PROTO_IPV6;
 		return TRUE;
 	}
Signature · 1 line
1.7.0.4
#13 lore raw

PATCH 13/21 emulator: Register mandatory AT command handlers

In reply to Zhenhua Zhang

Register mandatory AT command handlers for DUN server.

---
 include/emulator.h |    2 +
 src/emulator.c     |  380 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 382 insertions(+), 0 deletions(-)
include/emulator.h +2 -0
index e6eeb06..71ed78a 100644
@@ -37,6 +37,8 @@ enum ofono_emulator_status {
 	OFONO_EMULATOR_STATUS_DUN_CONNECT,
 	OFONO_EMULATOR_STATUS_DUN_CONNECTED,
 	OFONO_EMULATOR_STATUS_DUN_DISCONNECTED,
+	OFONO_EMULATOR_STATUS_SET_CGATT,
+	OFONO_EMULATOR_STATUS_SET_CGDCONT,
 };
 
 enum _GAtServerResult;
src/emulator.c +380 -0

404 lines not on this page · raw message

Signature · 1 line
1.7.0.4
#14 lore raw

PATCH 14/21 gprs: Refactor to share remove_context method

In reply to Zhenhua Zhang

Remove duplicated code in gprs_deactivate_for_remove and gprs_remove_context. So they could share the same remove_context method.

---
 src/gprs.c |   58 ++++++++++++++++++++++++++++------------------------------
 1 files changed, 28 insertions(+), 30 deletions(-)
src/gprs.c +28 -30
index b39e361..ccc6ca5 100644
@@ -1324,26 +1324,14 @@ static DBusMessage *gprs_add_context(DBusConnection *conn,
 	return NULL;
 }
 
-static void gprs_deactivate_for_remove(const struct ofono_error *error,
-						void *data)
+static void remove_context(struct pri_context *ctx)
 {
-	struct pri_context *ctx = data;
 	struct ofono_gprs *gprs = ctx->gprs;
 	DBusConnection *conn;
 	char *path;
 	const char *atompath;
 
-	if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
-		DBG("Removing context failed with error: %s",
-				telephony_error_to_str(error));
-
-		__ofono_dbus_pending_reply(&gprs->pending,
-					__ofono_error_failed(gprs->pending));
-		return;
-	}
-
-	gprs_cid_release(gprs, ctx->context.cid);
-	ctx->context.cid = 0;
+	DBG("%s", ctx->path);
 
 	if (gprs->settings) {
 		g_key_file_remove_group(gprs->settings, ctx->key, NULL);
@@ -1356,9 +1344,6 @@ static void gprs_deactivate_for_remove(const struct ofono_error *error,
 	context_dbus_unregister(ctx);
 	gprs->contexts = g_slist_remove(gprs->contexts, ctx);
 
-	__ofono_dbus_pending_reply(&gprs->pending,
-				dbus_message_new_method_return(gprs->pending));
-
 	atompath = __ofono_atom_get_path(gprs->atom);
 	conn = ofono_dbus_get_connection();
 	g_dbus_emit_signal(conn, atompath, OFONO_CONNECTION_MANAGER_INTERFACE,
@@ -1367,13 +1352,37 @@ static void gprs_deactivate_for_remove(const struct ofono_error *error,
 	g_free(path);
 }
 
+static void gprs_deactivate_for_remove(const struct ofono_error *error,
+						void *data)
+{
+	struct pri_context *ctx = data;
+	struct ofono_gprs *gprs = ctx->gprs;
+
+	if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
+		DBG("Removing context failed with error: %s",
+				telephony_error_to_str(error));
+
+		__ofono_dbus_pending_reply(&gprs->pending,
+					__ofono_error_failed(gprs->pending));
+		return;
+	}
+
+	gprs_cid_release(gprs, ctx->context.cid);
+	ctx->context.cid = 0;
+
+	remove_context(ctx);
+
+	if (gprs->pending)
+		__ofono_dbus_pending_reply(&gprs->pending,
+				dbus_message_new_method_return(gprs->pending));
+}
+
 static DBusMessage *gprs_remove_context(DBusConnection *conn,
 					DBusMessage *msg, void *data)
 {
 	struct ofono_gprs *gprs = data;
 	struct pri_context *ctx;
 	const char *path;
-	const char *atompath;
 
 	if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
 					DBUS_TYPE_INVALID))
@@ -1395,22 +1404,11 @@ static DBusMessage *gprs_remove_context(DBusConnection *conn,
 		return NULL;
 	}
 
-	if (gprs->settings) {
-		g_key_file_remove_group(gprs->settings, ctx->key, NULL);
-		storage_sync(gprs->imsi, SETTINGS_STORE, gprs->settings);
-	}
-
 	DBG("Unregistering context: %s\n", ctx->path);
-	context_dbus_unregister(ctx);
-	gprs->contexts = g_slist_remove(gprs->contexts, ctx);
+	remove_context(ctx);
 
 	g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
 
-	atompath = __ofono_atom_get_path(gprs->atom);
-	g_dbus_emit_signal(conn, atompath, OFONO_CONNECTION_MANAGER_INTERFACE,
-				"ContextRemoved", DBUS_TYPE_OBJECT_PATH, &path,
-				DBUS_TYPE_INVALID);
-
 	return NULL;
 }
 
Signature · 1 line
1.7.0.4
#15 lore raw

PATCH 15/21 gprs: Add DUN +CGATT support in gprs atom

In reply to Zhenhua Zhang

DUN client may request to attach/deattach GPRS network. Use gprs_netreg_update to set attach/deattach status.

---
 src/gprs.c |   19 +++++++++++++++++++
 1 files changed, 19 insertions(+), 0 deletions(-)
src/gprs.c +19 -0
index ccc6ca5..01babdc 100644
@@ -39,6 +39,8 @@
 #include "common.h"
 #include "storage.h"
 #include "idmap.h"
+#include "gatserver.h"
+
 
 #define GPRS_FLAG_ATTACHING 0x1
 #define GPRS_FLAG_RECHECK 0x2
@@ -1603,14 +1605,31 @@ void ofono_gprs_context_deactivated(struct ofono_gprs_context *gc,
 	}
 }
 
+static void ofono_gprs_set_cgatt(struct ofono_gprs *gprs,
+				struct ofono_emulator_req *req)
+{
+	int *mode = req->data;
+
+	gprs->powered = *mode;
+
+	gprs_netreg_update(gprs);
+
+	req->cb(G_AT_SERVER_RESULT_OK, req->cb_data);
+}
+
 static void dun_status_watch(struct ofono_emulator *e,
 					enum ofono_emulator_status status,
 					void *data, void *user_data)
 {
+	struct ofono_gprs *gprs = user_data;
+
 	if (e == NULL)
 		return;
 
 	switch (status) {
+	case OFONO_EMULATOR_STATUS_SET_CGATT:
+		ofono_gprs_set_cgatt(gprs, data);
+		break;
 	default:
 		break;
 	}
Signature · 1 line
1.7.0.4
#16 lore raw

PATCH 16/21 gprs: Add DUN +CGDCONT support in gprs atom

In reply to Zhenhua Zhang

DUN client may request to define the APN of context. Find the existing context and update the APN.

---
 src/gprs.c |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 46 insertions(+), 0 deletions(-)
src/gprs.c +46 -0
index 01babdc..617ed05 100644
@@ -61,6 +61,13 @@ enum gprs_context_type {
 	GPRS_CONTEXT_TYPE_INVALID,
 };
 
+struct gprs_dun_data {
+	ofono_emulator_gprs_connect_cb cb;
+	void *cb_data;
+	struct pri_context *context;
+	struct pri_context *dun;
+};
+
 struct ofono_gprs {
 	GSList *contexts;
 	ofono_bool_t attached;
@@ -88,6 +95,7 @@ struct ofono_gprs {
 	struct ofono_emulator *dun;
 	unsigned int dun_watch;
 	unsigned int dun_status_watch;
+	struct gprs_dun_data dun_data;
 };
 
 struct ofono_gprs_context {
@@ -1617,6 +1625,41 @@ static void ofono_gprs_set_cgatt(struct ofono_gprs *gprs,
 	req->cb(G_AT_SERVER_RESULT_OK, req->cb_data);
 }
 
+static void ofono_gprs_set_cgdcont(struct ofono_gprs *gprs,
+				struct ofono_emulator_gprs_context_req *req)
+{
+	struct gprs_dun_data *data = &gprs->dun_data;
+	struct pri_context *context = NULL;
+	enum ofono_gprs_proto proto;
+	GSList *l;
+
+	gprs_proto_from_string(req->proto, &proto);
+
+	for (l = gprs->contexts; l; l = l->next) {
+		struct pri_context *ctx = l->data;
+
+		if (ctx->type != GPRS_CONTEXT_TYPE_INTERNET)
+			continue;
+
+		if (proto != ctx->context.proto)
+			continue;
+
+		context = ctx;
+		break;
+	}
+
+	if (context == NULL)
+		goto error;
+
+	strcpy(context->context.apn, req->apn);
+	data->context = context;
+	req->cb(G_AT_SERVER_RESULT_OK, req->cb_data);
+
+	return;
+error:
+	req->cb(G_AT_SERVER_RESULT_ERROR, req->cb_data);
+}
+
 static void dun_status_watch(struct ofono_emulator *e,
 					enum ofono_emulator_status status,
 					void *data, void *user_data)
@@ -1630,6 +1673,9 @@ static void dun_status_watch(struct ofono_emulator *e,
 	case OFONO_EMULATOR_STATUS_SET_CGATT:
 		ofono_gprs_set_cgatt(gprs, data);
 		break;
+	case OFONO_EMULATOR_STATUS_SET_CGDCONT:
+		ofono_gprs_set_cgdcont(gprs, data);
+		break;
 	default:
 		break;
 	}
Signature · 1 line
1.7.0.4
#17 lore raw

PATCH 17/21 gprs: Add DUN server GPRS connect support

In reply to Zhenhua Zhang

Implement dial up networking in GPRS atom.

---
 src/gprs.c |  232 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 229 insertions(+), 3 deletions(-)
src/gprs.c +229 -3
index 617ed05..eabc825 100644
@@ -51,6 +51,10 @@
 #define MAX_CONTEXTS 256
 #define SUSPEND_TIMEOUT 8
 
+#define DUN_LOCAL "192.168.1.1"
+#define DUN_PEER "192.168.1.2"
+#define DUN_CONNECT_TIMEOUT 20
+
 static GSList *g_drivers = NULL;
 static GSList *g_context_drivers = NULL;
 
@@ -96,6 +100,9 @@ struct ofono_gprs {
 	unsigned int dun_watch;
 	unsigned int dun_status_watch;
 	struct gprs_dun_data dun_data;
+	int attach_source;
+	int context_source;
+	int timeout_source;
 };
 
 struct ofono_gprs_context {
@@ -128,6 +135,7 @@ struct pri_context {
 };
 
 static void gprs_netreg_update(struct ofono_gprs *gprs);
+static gboolean dun_connect_remove(gpointer user_data);
 
 static const char *gprs_context_type_to_string(int type)
 {
@@ -464,17 +472,24 @@ static void pri_activate_callback(const struct ofono_error *error,
 	if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
 		DBG("Activating context failed with error: %s",
 				telephony_error_to_str(error));
-		__ofono_dbus_pending_reply(&gc->pending,
+
+		if (gc->pending)
+			__ofono_dbus_pending_reply(&gc->pending,
 					__ofono_error_failed(gc->pending));
 
 		gprs_cid_release(ctx->gprs, ctx->context.cid);
 		ctx->context.cid = 0;
 
+		if (ctx->gprs->dun)
+			dun_connect_remove(ctx->gprs);
+
 		return;
 	}
 
 	ctx->active = TRUE;
-	__ofono_dbus_pending_reply(&gc->pending,
+
+	if (gc->pending)
+		__ofono_dbus_pending_reply(&gc->pending,
 				dbus_message_new_method_return(gc->pending));
 
 	/*
@@ -1564,12 +1579,35 @@ void ofono_gprs_set_cid_range(struct ofono_gprs *gprs,
 	gprs->cid_map = idmap_new_from_range(min, max);
 }
 
+static void ofono_gprs_remove_sources(struct ofono_gprs *gprs)
+{
+	if (gprs == NULL)
+		return;
+
+	if (gprs->timeout_source) {
+		g_source_remove(gprs->timeout_source);
+		gprs->timeout_source = 0;
+	}
+
+	if (gprs->attach_source) {
+		g_source_remove(gprs->attach_source);
+		gprs->attach_source = 0;
+	}
+
+	if (gprs->context_source) {
+		g_source_remove(gprs->context_source);
+		gprs->context_source = 0;
+	}
+}
+
 static void gprs_context_unregister(struct ofono_atom *atom)
 {
 	struct ofono_gprs_context *gc = __ofono_atom_get_data(atom);
 
-	if (gc->gprs)
+	if (gc->gprs) {
 		gc->gprs->context_driver = NULL;
+		ofono_gprs_remove_sources(gc->gprs);
+	}
 
 	gc->gprs = NULL;
 }
@@ -1613,6 +1651,183 @@ void ofono_gprs_context_deactivated(struct ofono_gprs_context *gc,
 	}
 }
 
+static ofono_bool_t set_primary_context_powered(struct pri_context *ctx,
+						ofono_bool_t value)
+{
+	struct ofono_gprs_context *gc = ctx->gprs->context_driver;
+
+	if (gc == NULL || gc->driver->activate_primary == NULL ||
+			gc->driver->deactivate_primary == NULL ||
+			ctx->gprs->cid_map == NULL)
+		return FALSE;
+
+	if (gc->pending)
+		return FALSE;
+
+	if (ctx->active == value)
+		return FALSE;
+
+	if (value && !ctx->gprs->attached)
+		return FALSE;
+
+	if (ctx->gprs->flags & GPRS_FLAG_ATTACHING)
+		return FALSE;
+
+	if (value) {
+		ctx->context.cid = gprs_cid_alloc(ctx->gprs);
+
+		if (ctx->context.cid == 0)
+			return FALSE;
+
+		if (ctx->context.cid !=
+				idmap_get_min(ctx->gprs->cid_map)) {
+			ofono_error("Multiple active contexts are"
+					" not yet supported");
+
+			gprs_cid_release(ctx->gprs, ctx->context.cid);
+			ctx->context.cid = 0;
+
+			return FALSE;
+		}
+	}
+
+	if (value)
+		gc->driver->activate_primary(gc, &ctx->context,
+					pri_activate_callback, ctx);
+	else
+		gc->driver->deactivate_primary(gc, ctx->context.cid,
+					pri_deactivate_callback, ctx);
+
+	return TRUE;
+}
+
+static void ofono_gprs_dun_disconnect(struct ofono_gprs *gprs,
+				struct ofono_emulator_req *req)
+{
+	struct pri_context *context = gprs->dun_data.context;
+
+	ofono_gprs_remove_sources(gprs);
+
+	if (context->active) {
+		struct ofono_gprs_context *gc = gprs->context_driver;
+
+		gc->driver->deactivate_primary(gc, context->context.cid,
+					gprs_deactivate_for_remove, context);
+	} else
+		remove_context(context);
+}
+
+static gboolean dun_connect_remove(gpointer user_data)
+{
+	struct ofono_gprs *gprs = user_data;
+	struct gprs_dun_data *data = &gprs->dun_data;
+	struct ofono_error error;
+
+	ofono_gprs_remove_sources(gprs);
+	remove_context(data->context);
+
+	error.type = OFONO_ERROR_TYPE_FAILURE;
+	data->cb(&error, NULL, NULL, NULL, NULL, data->cb_data);
+
+	data->cb = NULL;
+	data->cb_data = NULL;
+	data->context = NULL;
+	data->dun = NULL;
+
+	return FALSE;
+}
+
+static gboolean pri_context_update(gpointer user_data)
+{
+	struct ofono_gprs *gprs = user_data;
+	struct gprs_dun_data *data = &gprs->dun_data;
+	struct pri_context *context = data->context;
+	struct context_settings *settings = context->settings;
+	struct ofono_error error;
+
+	DBG("active %d\n", context->active);
+
+	if (context->active != TRUE)
+		return TRUE;
+
+	gprs->context_source = 0;
+	g_source_remove(gprs->timeout_source);
+
+	error.type = OFONO_ERROR_TYPE_NO_ERROR;
+
+	data->cb(&error, settings->interface, DUN_LOCAL, DUN_PEER,
+					(const char **)settings->dns,
+					data->cb_data);
+
+	return FALSE;
+}
+
+static gboolean gprs_attach_update(gpointer user_data)
+{
+	struct ofono_gprs *gprs = user_data;
+	struct gprs_dun_data *data = &gprs->dun_data;
+	struct pri_context *context = data->context;
+
+	DBG("attached %d driver attached %d\n", gprs->attached,
+						gprs->driver_attached);
+
+	if (gprs->attached != TRUE || gprs->driver_attached != TRUE)
+		return TRUE;
+
+	gprs->attach_source = 0;
+
+	if (context->active != TRUE) {
+		set_primary_context_powered(context, TRUE);
+
+		/* wait until the primary context is actived */
+		gprs->context_source = g_timeout_add_seconds(1,
+						pri_context_update, gprs);
+		return FALSE;
+	}
+
+	pri_context_update(gprs);
+
+	return FALSE;
+}
+
+static void ofono_gprs_dun_connect(struct ofono_gprs *gprs,
+				struct ofono_emulator_gprs_connect_req *req)
+{
+	const char *dial_str = req->dial_str;
+	struct gprs_dun_data *data = &gprs->dun_data;
+	struct ofono_error error;
+
+	DBG("%s", dial_str);
+
+	if (data->context == NULL)
+		goto error;
+
+	data->cb = req->cb;
+	data->cb_data = req->cb_data;
+
+	gprs->timeout_source = g_timeout_add_seconds(DUN_CONNECT_TIMEOUT,
+						dun_connect_remove, gprs);
+
+	/* check gprs attaching status */
+	if (!gprs->powered || !gprs->attached || !gprs->driver_attached) {
+		gprs->powered = TRUE;
+
+		gprs_netreg_update(gprs);
+
+		/* wait until GPRS is attached */
+		gprs->attach_source = g_timeout_add_seconds(1,
+						gprs_attach_update, gprs);
+		return;
+	}
+
+	gprs_attach_update(gprs);
+
+	return;
+error:
+	error.type = OFONO_ERROR_TYPE_FAILURE;
+	req->cb(&error, NULL, NULL, NULL, NULL, req->cb_data);
+}
+
 static void ofono_gprs_set_cgatt(struct ofono_gprs *gprs,
 				struct ofono_emulator_req *req)
 {
@@ -1670,12 +1885,21 @@ static void dun_status_watch(struct ofono_emulator *e,
 		return;
 
 	switch (status) {
+	case OFONO_EMULATOR_STATUS_DUN_CONNECT:
+		ofono_gprs_dun_connect(gprs, data);
+		break;
+	case OFONO_EMULATOR_STATUS_DUN_DISCONNECTED:
+		ofono_gprs_dun_disconnect(gprs, data);
+		break;
 	case OFONO_EMULATOR_STATUS_SET_CGATT:
 		ofono_gprs_set_cgatt(gprs, data);
 		break;
 	case OFONO_EMULATOR_STATUS_SET_CGDCONT:
 		ofono_gprs_set_cgdcont(gprs, data);
 		break;
+	case OFONO_EMULATOR_STATUS_DESTROY:
+		ofono_gprs_remove_sources(gprs);
+		break;
 	default:
 		break;
 	}
@@ -1862,6 +2086,8 @@ static void gprs_unregister(struct ofono_atom *atom)
 		gprs->dun = NULL;
 	}
 
+	ofono_gprs_remove_sources(gprs);
+
 	ofono_modem_remove_interface(modem,
 					OFONO_CONNECTION_MANAGER_INTERFACE);
 	g_dbus_unregister_interface(conn, path,
Signature · 1 line
1.7.0.4
#18 lore raw

PATCH 18/21 gprs: Add status watch functions

In reply to Zhenhua Zhang

So DUN server could watch GPRS status changes and notify client unsolicited results like +CGREG.

---
 include/gprs.h |    9 +++++++++
 src/gprs.c     |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+), 0 deletions(-)
include/gprs.h +9 -0
index ad7925c..7985115 100644
@@ -36,6 +36,8 @@ typedef void (*ofono_gprs_status_cb_t)(const struct ofono_error *error,
 
 typedef void (*ofono_gprs_cb_t)(const struct ofono_error *error, void *data);
 
+typedef void (*ofono_gprs_status_notify_cb_t)(const int status, void *data);
+
 struct ofono_gprs_driver {
 	const char *name;
 	int (*probe)(struct ofono_gprs *gprs, unsigned int vendor,
@@ -77,6 +79,13 @@ void ofono_gprs_set_cid_range(struct ofono_gprs *gprs,
 void ofono_gprs_add_context(struct ofono_gprs *gprs,
 				struct ofono_gprs_context *gc);
 
+unsigned int __ofono_gprs_add_status_watch(struct ofono_gprs *gprs,
+				ofono_gprs_status_notify_cb_t cb,
+				void *data, ofono_destroy_func destroy);
+
+gboolean __ofono_gprs_remove_status_watch(struct ofono_gprs *gprs,
+						unsigned int id);
+
 #ifdef __cplusplus
 }
 #endif
src/gprs.c +55 -0

91 lines not on this page · raw message

Signature · 1 line
1.7.0.4
#19 lore raw

PATCH 19/21 gprs: Add gprs_get_status

In reply to Zhenhua Zhang

DUN server want to get latest GPRS status.

---
 include/gprs.h |    2 ++
 src/gprs.c     |    8 ++++++++
 2 files changed, 10 insertions(+), 0 deletions(-)
include/gprs.h +2 -0
index 7985115..05cc086 100644
@@ -86,6 +86,8 @@ unsigned int __ofono_gprs_add_status_watch(struct ofono_gprs *gprs,
 gboolean __ofono_gprs_remove_status_watch(struct ofono_gprs *gprs,
 						unsigned int id);
 
+int ofono_gprs_get_status(struct ofono_gprs *gprs);
+
 #ifdef __cplusplus
 }
 #endif
src/gprs.c +8 -0
index 1285a4b..e7fc768 100644
@@ -2063,6 +2063,14 @@ struct ofono_modem *ofono_gprs_context_get_modem(struct ofono_gprs_context *gc)
 	return __ofono_atom_get_modem(gc->atom);
 }
 
+int ofono_gprs_get_status(struct ofono_gprs *gprs)
+{
+	if (gprs == NULL)
+		return -1;
+
+	return gprs->status;
+}
+
 int ofono_gprs_driver_register(const struct ofono_gprs_driver *d)
 {
 	DBG("driver: %p, name: %s", d, d->name);
Signature · 1 line
1.7.0.4
#20 lore raw

PATCH 20/21 emulator: Watch GPRS status changes

In reply to Zhenhua Zhang

Watch GPRS status update to notify DUN client, such as +CGREG unsolicited result.

---
 src/emulator.c |   94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 94 insertions(+), 0 deletions(-)
src/emulator.c +94 -0
index 2a089a1..7701510 100644
@@ -55,6 +55,7 @@ struct modem_settings {
 	int cgreg;
 	int cops;
 	int attached;
+	int gprs_status;
 };
 
 struct ofono_emulator {
@@ -68,6 +69,9 @@ struct ofono_emulator {
 	struct modem_settings settings;
 	enum ofono_emulator_status status;
 	struct ofono_watchlist *status_watches;
+	struct ofono_gprs *gprs;
+	unsigned int gprs_watch;
+	unsigned int gprs_status_watch;
 };
 
 static GSList *emulator_drivers = NULL;
@@ -305,12 +309,16 @@ static void cgreg_cb(GAtServerRequestType type, GAtResult *result,
 					gpointer user_data)
 {
 	struct ofono_emulator *e = user_data;
+	char buf[256];
 
 	switch (type) {
 	case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
 		ofono_emulator_send_final(e, G_AT_SERVER_RESULT_OK);
 		break;
 	case G_AT_SERVER_REQUEST_TYPE_QUERY:
+		snprintf(buf, sizeof(buf), "+CGREG: %d, %d", e->settings.cgreg,
+					e->settings.gprs_status);
+		ofono_emulator_send_info(e, buf, TRUE);
 		ofono_emulator_send_final(e, G_AT_SERVER_RESULT_OK);
 		break;
 	case G_AT_SERVER_REQUEST_TYPE_SET:
@@ -710,6 +718,78 @@ error:
 	g_at_server_send_final(server, res);
 }
 
+static void emulator_gprs_update(struct ofono_emulator *e)
+{
+	GAtServer *server = e->server;
+	char buf[256];
+
+	switch (e->settings.cgreg) {
+	case 0:
+		break;
+	case 1:
+		snprintf(buf, sizeof(buf), "+CGREG: %d",
+						e->settings.gprs_status);
+		g_at_server_send_unsolicited(server, buf);
+		break;
+	case 2:
+		snprintf(buf, sizeof(buf), "+CGREG: %d",
+						e->settings.gprs_status);
+		g_at_server_send_unsolicited(server, buf);
+		break;
+	default:
+		break;
+	}
+}
+
+static void gprs_status_changed(int status, void *data)
+{
+	struct ofono_emulator *e = data;
+
+	DBG("%d", status);
+
+	e->settings.gprs_status = status;
+
+	emulator_gprs_update(e);
+}
+
+static void gprs_watch(struct ofono_atom *atom,
+				enum ofono_atom_watch_condition cond,
+				void *data)
+{
+	struct ofono_emulator *e = data;
+	struct ofono_gprs *gprs;
+
+	if (cond == OFONO_ATOM_WATCH_CONDITION_UNREGISTERED) {
+		e->gprs_status_watch = 0;
+		e->gprs = NULL;
+		return;
+	}
+
+	gprs = __ofono_atom_get_data(atom);
+	e->gprs = gprs;
+	e->settings.gprs_status = ofono_gprs_get_status(gprs);
+	e->gprs_status_watch = __ofono_gprs_add_status_watch(e->gprs,
+					gprs_status_changed, e, NULL);
+
+	emulator_gprs_update(e);
+}
+
+static void add_gprs_watches(struct ofono_emulator *e)
+{
+	struct ofono_modem *modem = e->modem;
+	struct ofono_atom *gprs_atom;
+
+	e->gprs_watch = __ofono_modem_add_atom_watch(modem,
+					OFONO_ATOM_TYPE_GPRS,
+					gprs_watch, e, NULL);
+
+	gprs_atom = __ofono_modem_find_atom(modem, OFONO_ATOM_TYPE_GPRS);
+
+	if (gprs_atom && __ofono_atom_get_registered(gprs_atom))
+		gprs_watch(gprs_atom,
+				OFONO_ATOM_WATCH_CONDITION_REGISTERED, e);
+}
+
 int ofono_emulator_enable(struct ofono_emulator *e, GIOChannel *channel)
 {
 	if (channel == NULL)
@@ -734,6 +814,8 @@ int ofono_emulator_enable(struct ofono_emulator *e, GIOChannel *channel)
 	g_at_server_register(e->server, "+CGATT", cgatt_cb, e, NULL);
 	g_at_server_register(e->server, "+CGDCONT", cgdcont_cb, e, NULL);
 
+	add_gprs_watches(e);
+
 	return 0;
 }
 
@@ -761,6 +843,18 @@ static void emulator_remove(struct ofono_atom *atom)
 	if (e->server)
 		emulator_disable(e);
 
+	if (e->gprs_watch) {
+		if (e->gprs_status_watch) {
+			__ofono_gprs_remove_status_watch(e->gprs,
+						e->gprs_status_watch);
+			e->gprs_status_watch = 0;
+		}
+
+		__ofono_modem_remove_atom_watch(e->modem, e->gprs_watch);
+		e->gprs_watch = 0;
+		e->gprs = NULL;
+	}
+
 	e->status = OFONO_EMULATOR_STATUS_DESTROY;
 	notify_status_watches(e, NULL);
 
Signature · 1 line
1.7.0.4
#21 lore raw

PATCH 21/21 emulator: Watch netreg status changes

In reply to Zhenhua Zhang

Watch netreg status update to notify DUN client, such as +CREG unsolicited result.

---
 src/emulator.c |  106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 106 insertions(+), 0 deletions(-)
src/emulator.c +106 -0
index 7701510..3444461 100644
@@ -56,6 +56,9 @@ struct modem_settings {
 	int cops;
 	int attached;
 	int gprs_status;
+	int netreg_status;
+	int netreg_lac;
+	int netreg_ci;
 };
 
 struct ofono_emulator {
@@ -72,6 +75,9 @@ struct ofono_emulator {
 	struct ofono_gprs *gprs;
 	unsigned int gprs_watch;
 	unsigned int gprs_status_watch;
+	struct ofono_netreg *netreg;
+	unsigned int netreg_watch;
+	unsigned int netreg_status_watch;
 };
 
 static GSList *emulator_drivers = NULL;
@@ -265,12 +271,19 @@ static void creg_cb(GAtServerRequestType type, GAtResult *result,
 					gpointer user_data)
 {
 	struct ofono_emulator *e = user_data;
+	char buf[256];
 
 	switch (type) {
 	case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
 		ofono_emulator_send_final(e, G_AT_SERVER_RESULT_OK);
 		break;
 	case G_AT_SERVER_REQUEST_TYPE_QUERY:
+		snprintf(buf, sizeof(buf), "+CREG: %d, %x, %x, %x",
+						e->settings.creg,
+						e->settings.netreg_status,
+						e->settings.netreg_lac,
+						e->settings.netreg_ci);
+		ofono_emulator_send_info(e, buf, TRUE);
 		ofono_emulator_send_final(e, G_AT_SERVER_RESULT_OK);
 		break;
 	case G_AT_SERVER_REQUEST_TYPE_SET:
@@ -790,6 +803,86 @@ static void add_gprs_watches(struct ofono_emulator *e)
 				OFONO_ATOM_WATCH_CONDITION_REGISTERED, e);
 }
 
+static void emulator_netreg_update(struct ofono_emulator *e)
+{
+	GAtServer *server = e->server;
+	char buf[256];
+
+	switch (e->settings.creg) {
+	case 0:
+		break;
+	case 1:
+		snprintf(buf, sizeof(buf), "+CREG: %d",
+						e->settings.netreg_status);
+		g_at_server_send_unsolicited(server, buf);
+		break;
+	case 2:
+		snprintf(buf, sizeof(buf), "+CREG: %d, %x, %x",
+						e->settings.netreg_status,
+						e->settings.netreg_lac,
+						e->settings.netreg_ci);
+		g_at_server_send_unsolicited(server, buf);
+		break;
+	default:
+		break;
+	}
+}
+
+static void netreg_status_changed(int status, int lac, int ci, int tech,
+					const char *mcc, const char *mnc,
+					void *data)
+{
+	struct ofono_emulator *e = data;
+
+	DBG("%d %d %d", status, lac, ci);
+
+	e->settings.netreg_status = status;
+	e->settings.netreg_lac = lac;
+	e->settings.netreg_ci = ci;
+
+	emulator_netreg_update(e);
+}
+
+static void netreg_watch(struct ofono_atom *atom,
+				enum ofono_atom_watch_condition cond,
+				void *data)
+{
+	struct ofono_emulator *e = data;
+	struct ofono_netreg *netreg;
+
+	if (cond == OFONO_ATOM_WATCH_CONDITION_UNREGISTERED) {
+		e->netreg_status_watch = 0;
+		e->netreg = NULL;
+		return;
+	}
+
+	netreg = __ofono_atom_get_data(atom);
+	e->netreg = netreg;
+	e->settings.netreg_status = ofono_netreg_get_status(netreg);
+	e->settings.netreg_lac = ofono_netreg_get_location(netreg);
+	e->settings.netreg_ci = ofono_netreg_get_cellid(netreg);
+	e->netreg_status_watch = __ofono_netreg_add_status_watch(e->netreg,
+					netreg_status_changed, e, NULL);
+
+	emulator_netreg_update(e);
+}
+
+static void add_netreg_watches(struct ofono_emulator *e)
+{
+	struct ofono_modem *modem = e->modem;
+	struct ofono_atom *netreg_atom;
+
+	e->netreg_watch = __ofono_modem_add_atom_watch(modem,
+					OFONO_ATOM_TYPE_NETREG,
+					netreg_watch, e, NULL);
+
+	netreg_atom = __ofono_modem_find_atom(modem, OFONO_ATOM_TYPE_NETREG);
+
+	if (netreg_atom && __ofono_atom_get_registered(netreg_atom))
+		netreg_watch(netreg_atom,
+				OFONO_ATOM_WATCH_CONDITION_REGISTERED, e);
+}
+
 int ofono_emulator_enable(struct ofono_emulator *e, GIOChannel *channel)
 {
 	if (channel == NULL)
@@ -815,6 +908,7 @@ int ofono_emulator_enable(struct ofono_emulator *e, GIOChannel *channel)
 	g_at_server_register(e->server, "+CGDCONT", cgdcont_cb, e, NULL);
 
 	add_gprs_watches(e);
+	add_netreg_watches(e);
 
 	return 0;
 }
@@ -855,6 +949,18 @@ static void emulator_remove(struct ofono_atom *atom)
 		e->gprs = NULL;
 	}
 
+	if (e->netreg_watch) {
+		if (e->netreg_status_watch) {
+			__ofono_netreg_remove_status_watch(e->netreg,
+						e->netreg_status_watch);
+			e->netreg_status_watch = 0;
+		}
+
+		__ofono_modem_remove_atom_watch(e->modem, e->netreg_watch);
+		e->netreg_watch = 0;
+		e->netreg = NULL;
+	}
+
 	e->status = OFONO_EMULATOR_STATUS_DESTROY;
 	notify_status_watches(e, NULL);
 
Signature · 1 line
1.7.0.4