Skip to contentSkip to conversation

Add ofono_modem_reset()

PATCH In review Gustavo F. Padovan 2 participants 9 messages Nov 25, 2010 – Dec 2, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 Add ofono_modem_reset()

Some modems can screw up everything and then we will need to do a silent reset of the modem. This patch take the modem back to the OFFLINE state.

---
 include/modem.h |    2 ++
 src/modem.c     |   15 +++++++++++++++
 2 files changed, 17 insertions(+), 0 deletions(-)
include/modem.h +2 -0
index 7b13ee0..a92eb88 100644
@@ -46,6 +46,8 @@ int ofono_modem_register(struct ofono_modem *modem);
 ofono_bool_t ofono_modem_is_registered(struct ofono_modem *modem);
 void ofono_modem_remove(struct ofono_modem *modem);
 
+void ofono_modem_reset(struct ofono_modem *modem);
+
 void ofono_modem_set_powered(struct ofono_modem *modem, ofono_bool_t powered);
 ofono_bool_t ofono_modem_get_powered(struct ofono_modem *modem);
 
src/modem.c +15 -0
index f032d93..6233944 100644
@@ -1615,6 +1615,21 @@ void ofono_modem_remove(struct ofono_modem *modem)
 	g_free(modem);
 }
 
+void ofono_modem_reset(struct ofono_modem *modem)
+{
+	int err;
+
+	DBG("%p", modem);
+
+	ofono_modem_set_powered(modem, FALSE);
+
+	err = set_powered(modem, TRUE);
+	if (err == -EINPROGRESS)
+		return;
+
+	modem_change_state(modem, MODEM_STATE_PRE_SIM);
+}
+
 int ofono_modem_driver_register(const struct ofono_modem_driver *d)
 {
 	DBG("driver: %p, name: %s", d, d->name);
Signature · 1 line
1.7.3.2
#1 lore raw

PATCH 2/5 phonesim: Add modem reset trigger

---
 plugins/phonesim.c |   27 +++++++++++++++++++++++++++
 1 files changed, 27 insertions(+), 0 deletions(-)
plugins/phonesim.c +27 -0
index d2faf42..14dae47 100644
@@ -237,6 +237,30 @@ static void cfun_set_on_cb(gboolean ok, GAtResult *result, gpointer user_data)
 	ofono_modem_set_powered(modem, ok);
 }
 
+static gboolean phonesim_reset(void *user_data)
+{
+	struct ofono_modem *modem = user_data;
+	struct phonesim_data *data = ofono_modem_get_data(modem);
+
+	g_at_chat_unref(data->chat);
+	data->chat = NULL;
+
+	if (data->mux) {
+		g_at_mux_shutdown(data->mux);
+		g_at_mux_unref(data->mux);
+		data->mux = NULL;
+	}
+
+	ofono_modem_reset(modem);
+
+	return FALSE;
+}
+
+static void crst_notify(GAtResult *result, gpointer user_data)
+{
+	g_idle_add(phonesim_reset, user_data);
+}
+
 static void phonesim_disconnected(gpointer user_data)
 {
 	struct ofono_modem *modem = user_data;
@@ -389,6 +413,9 @@ static int phonesim_enable(struct ofono_modem *modem)
 	g_at_chat_send(data->chat, "AT+CSCS=\"GSM\"", none_prefix,
 			NULL, NULL, NULL);
 
+	g_at_chat_register(data->chat, "+CRST:",
+				crst_notify, FALSE, modem, NULL);
+
 	return 0;
 }
 
Signature · 1 line
1.7.3.2
#2 lore raw

PATCH 3/5 modem: add support to restore state when resetting the modem

---
 src/modem.c |   70 +++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 40 insertions(+), 30 deletions(-)
src/modem.c +40 -30
index 6233944..1828a3b 100644
@@ -70,6 +70,7 @@ struct ofono_modem {
 	guint			interface_update;
 	ofono_bool_t		powered;
 	ofono_bool_t		powered_pending;
+	ofono_bool_t		get_online;
 	guint			timeout;
 	ofono_bool_t		online;
 	struct ofono_watchlist	*online_watches;
@@ -434,30 +435,6 @@ static void modem_change_state(struct ofono_modem *modem,
 	}
 }
 
-static void sim_state_watch(enum ofono_sim_state new_state, void *user)
-{
-	struct ofono_modem *modem = user;
-
-	switch (new_state) {
-	case OFONO_SIM_STATE_NOT_PRESENT:
-		modem_change_state(modem, MODEM_STATE_PRE_SIM);
-		break;
-	case OFONO_SIM_STATE_INSERTED:
-		break;
-	case OFONO_SIM_STATE_READY:
-		modem_change_state(modem, MODEM_STATE_OFFLINE);
-
-		/*
-		 * If we don't have the set_online method, also proceed
-		 * straight to the online state
-		 */
-		if (modem->driver->set_online == NULL)
-			modem_change_state(modem, MODEM_STATE_ONLINE);
-
-		break;
-	}
-}
-
 unsigned int __ofono_modem_add_online_watch(struct ofono_modem *modem,
 					ofono_modem_online_notify_func notify,
 					void *data, ofono_destroy_func destroy)
@@ -488,16 +465,18 @@ static void online_cb(const struct ofono_error *error, void *data)
 	DBusMessage *reply;
 
 	if (error->type == OFONO_ERROR_TYPE_NO_ERROR &&
-			modem->modem_state == MODEM_STATE_OFFLINE)
+			modem->modem_state == MODEM_STATE_OFFLINE) {
+		modem_change_state(modem, MODEM_STATE_ONLINE);
+
+		if (!modem->pending)
+			return;
+
 		reply = dbus_message_new_method_return(modem->pending);
-	else
+	} else {
 		reply = __ofono_error_failed(modem->pending);
+	}
 
 	__ofono_dbus_pending_reply(&modem->pending, reply);
-
-	if (error->type == OFONO_ERROR_TYPE_NO_ERROR &&
-			modem->modem_state == MODEM_STATE_OFFLINE)
-		modem_change_state(modem, MODEM_STATE_ONLINE);
 }
 
 static void offline_cb(const struct ofono_error *error, void *data)
@@ -517,6 +496,34 @@ static void offline_cb(const struct ofono_error *error, void *data)
 		modem_change_state(modem, MODEM_STATE_OFFLINE);
 }
 
+static void sim_state_watch(enum ofono_sim_state new_state, void *user)
+{
+	struct ofono_modem *modem = user;
+
+	switch (new_state) {
+	case OFONO_SIM_STATE_NOT_PRESENT:
+		modem_change_state(modem, MODEM_STATE_PRE_SIM);
+		break;
+	case OFONO_SIM_STATE_INSERTED:
+		break;
+	case OFONO_SIM_STATE_READY:
+		modem_change_state(modem, MODEM_STATE_OFFLINE);
+
+		/*
+		 * If we don't have the set_online method, also proceed
+		 * straight to the online state
+		 */
+		if (modem->driver->set_online == NULL)
+			modem_change_state(modem, MODEM_STATE_ONLINE);
+		else if (modem->get_online)
+			modem->driver->set_online(modem, 1, online_cb, modem);
+
+		modem->get_online = FALSE;
+
+		break;
+	}
+}
+
 static DBusMessage *set_property_online(struct ofono_modem *modem,
 					DBusMessage *msg,
 					DBusMessageIter *var)
@@ -1621,6 +1628,9 @@ void ofono_modem_reset(struct ofono_modem *modem)
 
 	DBG("%p", modem);
 
+	if (modem->modem_state == MODEM_STATE_ONLINE)
+		modem->get_online = TRUE;
+
 	ofono_modem_set_powered(modem, FALSE);
 
 	err = set_powered(modem, TRUE);
Signature · 1 line
1.7.3.2
#3 lore raw

PATCH 4/5 Add Lockdown property to Modem interface

Setting Lockdown to TRUE means power down the modem and hold a lock that only permits the lock's owner power up the modem back. When released it restores the last state of the modem before holding the lock.

---
 doc/modem-api.txt |   10 +++++
 src/modem.c       |  105 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 115 insertions(+), 0 deletions(-)
doc/modem-api.txt +10 -0
index b92e53c..7189245 100644
@@ -37,6 +37,16 @@ Properties	boolean Powered [readwrite]
 			Boolean representing the rf state of the modem.
 			Online is false in flight mode.
 
+		boolean Lockdown [readwrite]
+
+			Boolean representing the lock state of the modem.
+			Setting it to true, makes the calling application hold
+			the modem lock and power it down. Setting to false
+			makes the it restore the modem state before the
+			lockdown and release the modem lock. Only the
+			application that holds the lock can power up the modem.
+			If the the application exits Lockdown is set to false.
+
 		boolean Emergency [readonly, optional, experimental]
 
 			Boolean representing the emergency mode of the
src/modem.c +105 -0

141 lines not on this page · raw message

Signature · 1 line
1.7.3.2
#4 lore raw

PATCH 5/5 Add test script for the Lockdown property

---
 test/lockdown-modem |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)
 create mode 100755 test/lockdown-modem
test/lockdown-modem new file +25 -0
new file mode 100755
index 0000000..5d98154
@@ -0,0 +1,25 @@
+#!/usr/bin/python
+
+import dbus
+import sys
+
+bus = dbus.SystemBus()
+
+if len(sys.argv) == 2:
+	path = sys.argv[1]
+else:
+	manager = dbus.Interface(bus.get_object('org.ofono', '/'),
+			'org.ofono.Manager')
+	modems = manager.GetModems()
+	path = modems[0][0]
+
+print "Locking and disconnecting modem %s..." % path
+modem = dbus.Interface(bus.get_object('org.ofono', path),
+						'org.ofono.Modem')
+
+modem.SetProperty("Lockdown", dbus.Boolean(1))
+
+print "press ENTER to unlock the modem %s" % path
+sys.stdin.readline()
+
+modem.SetProperty("Lockdown", dbus.Boolean(0))
Signature · 1 line
1.7.3.2
#8 lore raw
In reply to Gustavo F. Padovan

Hi Gustavo,

Quoted 6 lines · Gustavo F. Padovan

Some modems can screw up everything and then we will need to do a silent reset of the modem. This patch take the modem back to the OFFLINE state.

---
 include/modem.h |    2 ++
 src/modem.c     |   15 +++++++++++++++
 2 files changed, 17 insertions(+), 0 deletions(-)

Two patches please. One for the include change, one for the implementation.

Otherwise looks good.

Regards,
-Denis

#7 lore raw

Re: [PATCH 3/5] modem: add support to restore state when resetting the modem

In reply to Gustavo F. Padovan

Hi Gustavo,

src/modem.c static void online_cb(const struct ofono_error *error, void *data) · line 473

Quoted 11 lines
src/modem.c +6 -1
@@ -488,16 +465,18 @@ static void online_cb(const struct ofono_error *error, void *data)
 	DBusMessage *reply;
 
 	if (error->type == OFONO_ERROR_TYPE_NO_ERROR &&
-			modem->modem_state == MODEM_STATE_OFFLINE)
+			modem->modem_state == MODEM_STATE_OFFLINE) {
+		modem_change_state(modem, MODEM_STATE_ONLINE);
+
+		if (!modem->pending)
+			return;
+

Fair enough...

src/modem.c static void online_cb(const struct ofono_error *error, void *data) · line 476

Quoted 4 lines
src/modem.c +1 -1
 		reply = dbus_message_new_method_return(modem->pending);
-	else
+	} else {
 		reply = __ofono_error_failed(modem->pending);

But what about the error case?

src/modem.c static void online_cb(const struct ofono_error *error, void *data) · line 482

Quoted 10 lines
src/modem.c +1 -4
+	}
 
 	__ofono_dbus_pending_reply(&modem->pending, reply);
-
-	if (error->type == OFONO_ERROR_TYPE_NO_ERROR &&
-			modem->modem_state == MODEM_STATE_OFFLINE)
-		modem_change_state(modem, MODEM_STATE_ONLINE);
 }
 
 static void offline_cb(const struct ofono_error *error, void *data)

Regards,
-Denis

#5 lore raw

Re: [PATCH 5/5] Add test script for the Lockdown property

In reply to Gustavo F. Padovan

Hi Gustavo,

Quoted 5 lines · Gustavo F. Padovan
---
 test/lockdown-modem |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)
 create mode 100755 test/lockdown-modem

Please make sure to update Makefile.am as well.

Regards,
-Denis

#6 lore raw

Re: [PATCH 4/5] Add Lockdown property to Modem interface

In reply to Gustavo F. Padovan
Quoted 13 lines · Gustavo F. Padovan

Setting Lockdown to TRUE means power down the modem and hold a lock that only permits the lock's owner power up the modem back. When released it restores the last state of the modem before holding the lock.

---
 doc/modem-api.txt |   10 +++++
 src/modem.c       |  105 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 115 insertions(+), 0 deletions(-)
doc/modem-api.txt +0 -0
index b92e53c..7189245 100644

doc/modem-api.txt Properties boolean Powered [readwrite] · line 46

Quoted 10 lines
doc/modem-api.txt +7 -0
@@ -37,6 +37,16 @@ Properties	boolean Powered [readwrite]
 			Boolean representing the rf state of the modem.
 			Online is false in flight mode.
 
+		boolean Lockdown [readwrite]
+
+			Boolean representing the lock state of the modem.
+			Setting it to true, makes the calling application hold
+			the modem lock and power it down. Setting to false
+			makes the it restore the modem state before the
+			lockdown and release the modem lock. Only the

Can you update the docs to mention that oFono does not re-power up the modem.

Quoted 64 lines
doc/modem-api.txt +3 -0
+			application that holds the lock can power up the modem.
+			If the the application exits Lockdown is set to false.
+
 		boolean Emergency [readonly, optional, experimental]
 
 			Boolean representing the emergency mode of the
src/modem.c +25 -0
index 1828a3b..0ad488d 100644
@@ -71,6 +71,9 @@ struct ofono_modem {
 	ofono_bool_t		powered;
 	ofono_bool_t		powered_pending;
 	ofono_bool_t		get_online;
+	ofono_bool_t		lockdown;
+	char			*lock_owner;
+	guint			lock_watch;
 	guint			timeout;
 	ofono_bool_t		online;
 	struct ofono_watchlist	*online_watches;
@@ -579,6 +582,9 @@ void __ofono_modem_append_properties(struct ofono_modem *modem,
 	ofono_dbus_dict_append(dict, "Powered", DBUS_TYPE_BOOLEAN,
 				&modem->powered);
 
+	ofono_dbus_dict_append(dict, "Lockdown", DBUS_TYPE_BOOLEAN,
+				&modem->lockdown);
+
 	devinfo_atom = __ofono_modem_find_atom(modem, OFONO_ATOM_TYPE_DEVINFO);
 
 	/* We cheat a little here and don't check the registered status */
@@ -713,6 +719,22 @@ static gboolean set_powered_timeout(gpointer user)
 	return FALSE;
 }
 
+static void lockdown_disconnect(DBusConnection *conn, void *user_data)
+{
+	struct ofono_modem *modem = user_data;
+
+	DBG("");
+
+	g_free(modem->lock_owner);
+
+	modem->lockdown = FALSE;
+
+	ofono_dbus_signal_property_changed(conn, modem->path,
+					OFONO_MODEM_INTERFACE,
+					"Lockdown", DBUS_TYPE_BOOLEAN,
+					&modem->lockdown);
+}
+
 static DBusMessage *modem_set_property(DBusConnection *conn,
 					DBusMessage *msg, void *data)
 {
@@ -755,6 +777,9 @@ static DBusMessage *modem_set_property(DBusConnection *conn,
 		if (modem->powered == powered)
 			return dbus_message_new_method_return(msg);
 
+		if (modem->lockdown)
+			return __ofono_error_access_denied(msg);
+
 		err = set_powered(modem, powered);
 		if (err < 0) {
 			if (err != -EINPROGRESS)

src/modem.c static DBusMessage *modem_set_property(DBusConnection *conn, · line 814

Quoted 4 lines
src/modem.c +1 -0
@@ -786,6 +811,81 @@ static DBusMessage *modem_set_property(DBusConnection *conn,
 		return NULL;
 	}
 
+	if (g_str_equal(name, "Lockdown")) {

Can you break this out into a separate function, similar to how we handle Online? Otherwise this function becomes way too long.

Quoted 64 lines
src/modem.c +64 -0
+		ofono_bool_t lockdown, powered;
+		const char *caller;
+		int err;
+
+		if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_BOOLEAN)
+			return __ofono_error_invalid_args(msg);
+
+		dbus_message_iter_get_basic(&var, &lockdown);
+
+		if (modem->pending != NULL)
+			return __ofono_error_busy(msg);
+
+		if (modem->lockdown == lockdown)
+			return dbus_message_new_method_return(msg);
+
+		if (modem->powered != modem->powered_pending)
+			return __ofono_error_not_available(msg);
+
+		caller = dbus_message_get_sender(msg);
+
+		if (lockdown) {
+			modem->lock_owner = g_strdup(caller);
+
+			modem->lock_watch = g_dbus_add_service_watch(conn,
+					modem->lock_owner, NULL,
+					lockdown_disconnect, modem, NULL);
+
+			if (modem->lock_watch == 0) {
+				g_free(modem->lock_owner);
+				return __ofono_error_failed(msg);
+			}
+
+			powered = FALSE;
+
+		} else {
+			if (g_strcmp0(caller, modem->lock_owner))
+				return __ofono_error_access_denied(msg);
+
+			g_free(modem->lock_owner);
+			g_dbus_remove_watch(conn, modem->lock_watch);
+			modem->lock_watch = 0;
+		}
+
+		modem->lockdown = lockdown;
+
+		g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
+
+		ofono_dbus_signal_property_changed(conn, modem->path,
+						OFONO_MODEM_INTERFACE,
+						"Lockdown", DBUS_TYPE_BOOLEAN,
+						&lockdown);
+
+		if (!modem->lockdown)
+			return NULL;
+
+		err = set_powered(modem, powered);
+		if (err < 0) {
+			if (err != -EINPROGRESS)
+				return __ofono_error_failed(msg);
+
+			modem->pending = dbus_message_ref(msg);
+			modem->timeout = g_timeout_add_seconds(20,
+						set_powered_timeout, modem);
+			return NULL;

src/modem.c static DBusMessage *modem_set_property(DBusConnection *conn, · line 888

Quoted 10 lines
src/modem.c +10 -0
+		}
+
+		ofono_dbus_signal_property_changed(conn, modem->path,
+						OFONO_MODEM_INTERFACE,
+						"Powered", DBUS_TYPE_BOOLEAN,
+						&powered);
+
+		return NULL;
+	}
+

One thing that might make this a little bit safer is to set pending and return from setting lockdown only when the modem has been powered off. Otherwise you might do nastiness like this:
Lockdown = True,
Lockdown = False
Powered = True

Since we're in the middle of the old powered down operation as a result of Lockdown, we might get truly confused.

Quoted 5 lines
src/modem.c +0 -0
 	return __ofono_error_invalid_args(msg);
 }
 
@@ -1588,6 +1688,11 @@ static void modem_unregister(struct ofono_modem *modem)
 		modem->interface_update = 0;

src/modem.c static void modem_unregister(struct ofono_modem *modem) · line 1698

Quoted 10 lines
src/modem.c +5 -0
 	}
 
+	if (modem->lock_watch) {
+		g_dbus_remove_watch(conn, modem->lock_watch);
+		modem->lock_watch = 0;
+	}
+
 	g_dbus_unregister_interface(conn, modem->path, OFONO_MODEM_INTERFACE);
 
 	if (modem->driver && modem->driver->remove)

Regards,
-Denis