Skip to contentSkip to conversation

atmodem: fix crash during context deactivation

PATCH Superseded Kalle Valo 2 participants 4 messages May 25, 2010

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kalle Valo

author
#0 lore raw

PATCH 1/2 atmodem: fix crash during context deactivation

Ofono either crashed or busy looped with my Huawei E1552 3G modem when I tried to deactivate GPRS context. The reason was that gcd->chat was unreferenced already in setup_ppp() but the chat was still accessed later in at_gprs_deactivate_primary().

To fix the problem, change the logic instead to suspend chat session for PPP and resume when PPP has disconnected. Now it doesn't crash anymore.

Deactivation still doesn't work properly with Huawei E1552, and most probably with other Huawei modems, because the modem hangs up the chat line after PPP deactivation. This needs to be fixed separately.
---

 drivers/atmodem/gprs-context.c |   64 ++++++++++++++++++++++++++--------------
 1 files changed, 42 insertions(+), 22 deletions(-)
drivers/atmodem/gprs-context.c +42 -22
index ba5f0c0..4412a07 100644
@@ -103,14 +103,44 @@ static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data)
 {
 	struct ofono_gprs_context *gc = user_data;
 	struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
+	struct cb_data *cbd;
+	char buf[64];
+	guint ret;
+
+	DBG("");
+
+	g_at_chat_resume(gcd->chat);
 
 	if (gcd->state == STATE_ENABLING) {
 		CALLBACK_WITH_FAILURE(gcd->up_cb, NULL, FALSE, NULL,
 					NULL, NULL, NULL, gcd->cb_data);
-		return;
+	} else if (gcd->state == STATE_DISABLING) {
+		cbd = cb_data_new(gcd->down_cb, gcd->cb_data);
+
+		if (cbd == NULL) {
+			CALLBACK_WITH_FAILURE(gcd->down_cb, gcd->cb_data);
+			goto out;
+		}
+
+		cbd->user = gc;
+
+		snprintf(buf, sizeof(buf), "AT+CGACT=0,%u",
+				gcd->active_context);
+
+		ret = g_at_chat_send(gcd->chat, buf, none_prefix,
+					at_cgact_down_cb, cbd, g_free);
+
+		if (ret == 0) {
+			ofono_info("Unable to send AT+CGACT=0");
+			CALLBACK_WITH_FAILURE(gcd->down_cb, gcd->cb_data);
+			g_free(cbd);
+			goto out;
+		}
+	} else {
+		ofono_gprs_context_deactivated(gc, gcd->active_context);
 	}
 
-	ofono_gprs_context_deactivated(gc, gcd->active_context);
+out:
 	gcd->active_context = 0;
 	gcd->state = STATE_IDLE;
 }
@@ -118,13 +148,14 @@ static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data)
 static gboolean setup_ppp(struct ofono_gprs_context *gc)
 {
 	struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
-	GIOChannel *channel;
+	GAtIO *io;
+
+	io = g_at_chat_get_io(gcd->chat);
 
-	channel = g_at_chat_get_channel(gcd->chat);
-	g_at_chat_unref(gcd->chat);
+	g_at_chat_suspend(gcd->chat);
 
 	/* open ppp */
-	gcd->ppp = g_at_ppp_new(channel);
+	gcd->ppp = g_at_ppp_new_from_io(io);
 
 	if (gcd->ppp == NULL)
 		return FALSE;
@@ -227,25 +258,14 @@ static void at_gprs_deactivate_primary(struct ofono_gprs_context *gc,
 					ofono_gprs_context_cb_t cb, void *data)
 {
 	struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
-	struct cb_data *cbd = cb_data_new(cb, data);
-	char buf[64];
 
-	if (!cbd)
-		goto error;
+	DBG("");
 
-	cbd->user = gc;
-
-	snprintf(buf, sizeof(buf), "AT+CGACT=0,%u", id);
-
-	if (g_at_chat_send(gcd->chat, buf, none_prefix,
-				at_cgact_down_cb, cbd, g_free) > 0)
-		return;
-
-error:
-	if (cbd)
-		g_free(cbd);
+	gcd->state = STATE_DISABLING;
+	gcd->down_cb = cb;
+	gcd->cb_data = data;
 
-	CALLBACK_WITH_FAILURE(cb, data);
+	g_at_ppp_shutdown(gcd->ppp);
 }
 
 static int at_gprs_context_probe(struct ofono_gprs_context *gc,
author
#1 lore raw

PATCH 2/2 atmodem: timeout for gprs context deactivation

Huawei modems have an annoying feature to hangup the chat line after ppp shutdown. Basically what happens is this:

14:15:08 ofonod[11768]: > AT+CGACT=0,1\r
14:15:09 ofonod[11768]: EventChannel: < \r\n^BOOT:38645652,0,0,0,87\r\n 14:15:10 ofonod[11768]: EventChannel: < \r\n^RSSI:22\r\n

ofono sends AT+CGACT to the modem but does not get any reply and will wait forever.

Workaround the issue by adding a five second timeout for the CGACT command. This makes it possible to properly use huawei modems with connman. Without this patch you can connect only once from connman and then you have to restart connman.
---

 drivers/atmodem/gprs-context.c |   35 +++++++++++++++++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)
drivers/atmodem/gprs-context.c +35 -0
index 4412a07..82e923e 100644
@@ -63,8 +63,31 @@ struct gprs_context_data {
 		ofono_gprs_context_up_cb_t up_cb;       /* Up callback */
 	};
 	void *cb_data;                                  /* Callback data */
+	gint cgact_timeout;
 };
 
+static gboolean cgact_timeout(gpointer user_data)
+{
+	struct cb_data *cbd = user_data;
+	ofono_gprs_context_cb_t cb = cbd->cb;
+	struct ofono_gprs_context *gc = cbd->user;
+	struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
+	struct ofono_error error;
+
+	DBG("");
+
+	gcd->cgact_timeout = 0;
+
+	error.type = OFONO_ERROR_TYPE_NO_ERROR;
+	error.error = 0;
+
+	cb(&error, cbd->data);
+
+	g_free(cbd);
+
+	return FALSE;
+}
+
 static void at_cgact_down_cb(gboolean ok, GAtResult *result, gpointer user_data)
 {
 	struct cb_data *cbd = user_data;
@@ -73,6 +96,13 @@ static void at_cgact_down_cb(gboolean ok, GAtResult *result, gpointer user_data)
 	struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
 	struct ofono_error error;
 
+	if (gcd->cgact_timeout == 0)
+		/* already timed out */
+		return;
+
+	g_source_remove(gcd->cgact_timeout);
+	gcd->cgact_timeout = 0;
+
 	if (ok)
 		gcd->active_context = 0;
 
@@ -136,6 +166,11 @@ static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data)
 			g_free(cbd);
 			goto out;
 		}
+
+		/* workaround for huawei hangup problem */
+		gcd->cgact_timeout = g_timeout_add_seconds(5, cgact_timeout,
+							cbd);
+
 	} else {
 		ofono_gprs_context_deactivated(gc, gcd->active_context);
 	}
#2 lore raw

Re: [PATCH 2/2] atmodem: timeout for gprs context deactivation

Hi Kalle,

Quoted 6 lines

Huawei modems have an annoying feature to hangup the chat line after ppp shutdown. Basically what happens is this:

14:15:08 ofonod[11768]: > AT+CGACT=0,1\r
14:15:09 ofonod[11768]: EventChannel: < \r\n^BOOT:38645652,0,0,0,87\r\n 14:15:10 ofonod[11768]: EventChannel: < \r\n^RSSI:22\r\n

we might wanna@least decode the ^BOOT messages as extra debug information.

Quoted 7 lines

ofono sends AT+CGACT to the modem but does not get any reply and will wait forever.

Workaround the issue by adding a five second timeout for the CGACT command. This makes it possible to properly use huawei modems with connman. Without this patch you can connect only once from connman and then you have to restart connman.

We don't need to re-open the TTY or anything alike. Just waiting for the timeout is enough? After that the TTY starts responding to commands again?

drivers/atmodem/gprs-context.c struct gprs_context_data { · line 67

Quoted 5 lines
drivers/atmodem/gprs-context.c +1 -0
 		ofono_gprs_context_up_cb_t up_cb;       /* Up callback */
 	};
 	void *cb_data;                                  /* Callback data */
+	gint cgact_timeout;
 };

The timeouts are GSources and so normally guint actually.

drivers/atmodem/gprs-context.c static void at_cgact_down_cb(gboolean ok, GAtResult *result, gpointer user_data) · line 102

Quoted 11 lines

static void at_cgact_down_cb(gboolean ok, GAtResult *result, gpointer user_data) {

drivers/atmodem/gprs-context.c +4 -0
 	struct cb_data *cbd = user_data;
@@ -73,6 +96,13 @@ static void at_cgact_down_cb(gboolean ok, GAtResult *result, gpointer user_data)
 	struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
 	struct ofono_error error;
 
+	if (gcd->cgact_timeout == 0)
+		/* already timed out */
+		return;
+

In theory you are correct with no { } since it is a single statement, but if you put a comment on top of it, then please use the { } to make it easier for the reader.

Quoted 7 lines
drivers/atmodem/gprs-context.c +3 -0
+	g_source_remove(gcd->cgact_timeout);
+	gcd->cgact_timeout = 0;
+
 	if (ok)
 		gcd->active_context = 0;
 

drivers/atmodem/gprs-context.c static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data) · line 173

Quoted 8 lines
drivers/atmodem/gprs-context.c +5 -0
@@ -136,6 +166,11 @@ static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data)
 			g_free(cbd);
 			goto out;
 		}
+
+		/* workaround for huawei hangup problem */
+		gcd->cgact_timeout = g_timeout_add_seconds(5, cgact_timeout,
+							cbd);
+

I think we should include this into a Huawei specific quirk.

Regards

Marcel

#3 lore raw
In reply to Kalle Valo

Hi Kalle,

Quoted 5 lines

Ofono either crashed or busy looped with my Huawei E1552 3G modem when I tried to deactivate GPRS context. The reason was that gcd->chat was unreferenced already in setup_ppp() but the chat was still accessed later in at_gprs_deactivate_primary().

Quoted 7 lines

To fix the problem, change the logic instead to suspend chat session for PPP and resume when PPP has disconnected. Now it doesn't crash anymore.

Deactivation still doesn't work properly with Huawei E1552, and most probably with other Huawei modems, because the modem hangs up the chat line after PPP deactivation. This needs to be fixed separately.

didn't we fix this issue already. Or was it just in gsmdial?

Quoted 5 lines
drivers/atmodem/gprs-context.c +3 -0
 	struct ofono_gprs_context *gc = user_data;
 	struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
+	struct cb_data *cbd;
+	char buf[64];
+	guint ret;

drivers/atmodem/gprs-context.c static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data) · line 117

Quoted 10 lines
drivers/atmodem/gprs-context.c +5 -1
+
+	DBG("");
+
+	g_at_chat_resume(gcd->chat);
 
 	if (gcd->state == STATE_ENABLING) {
 		CALLBACK_WITH_FAILURE(gcd->up_cb, NULL, FALSE, NULL,
 					NULL, NULL, NULL, gcd->cb_data);
-		return;
+	} else if (gcd->state == STATE_DISABLING) {

This else if is rather pointless since the previous one already returned from the function.

	if (aaa...)
		return;

	if (bbb...) {
		...
	} else {
	}

That is way easier to follow than multiple else if. Also in this case a switch statement might be a lot better.

Quoted 11 lines
drivers/atmodem/gprs-context.c +11 -0
+		cbd = cb_data_new(gcd->down_cb, gcd->cb_data);
+
+		if (cbd == NULL) {
+			CALLBACK_WITH_FAILURE(gcd->down_cb, gcd->cb_data);
+			goto out;
+		}
+
+		cbd->user = gc;
+
+		snprintf(buf, sizeof(buf), "AT+CGACT=0,%u",
+				gcd->active_context);

drivers/atmodem/gprs-context.c static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data) · line 138

Quoted 10 lines
drivers/atmodem/gprs-context.c +10 -0
+
+		ret = g_at_chat_send(gcd->chat, buf, none_prefix,
+					at_cgact_down_cb, cbd, g_free);
+
+		if (ret == 0) {
+			ofono_info("Unable to send AT+CGACT=0");
+			CALLBACK_WITH_FAILURE(gcd->down_cb, gcd->cb_data);
+			g_free(cbd);
+			goto out;
+		}

What is this goto out; really buying you?

drivers/atmodem/gprs-context.c static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data) · line 146

Quoted 9 lines
drivers/atmodem/gprs-context.c +3 -1
+	} else {
+		ofono_gprs_context_deactivated(gc, gcd->active_context);
 	}
 
-	ofono_gprs_context_deactivated(gc, gcd->active_context);
+out:
 	gcd->active_context = 0;
 	gcd->state = STATE_IDLE;
 }

Just from a simple look at it. Using a switch statement for states and utilizing break; correctly makes this a lot more easier to read.

Quoted 38 lines
drivers/atmodem/gprs-context.c +6 -16
@@ -118,13 +148,14 @@ static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data)
 static gboolean setup_ppp(struct ofono_gprs_context *gc)
 {
 	struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
-	GIOChannel *channel;
+	GAtIO *io;
+
+	io = g_at_chat_get_io(gcd->chat);
 
-	channel = g_at_chat_get_channel(gcd->chat);
-	g_at_chat_unref(gcd->chat);
+	g_at_chat_suspend(gcd->chat);
 
 	/* open ppp */
-	gcd->ppp = g_at_ppp_new(channel);
+	gcd->ppp = g_at_ppp_new_from_io(io);
 
 	if (gcd->ppp == NULL)
 		return FALSE;
@@ -227,25 +258,14 @@ static void at_gprs_deactivate_primary(struct ofono_gprs_context *gc,
 					ofono_gprs_context_cb_t cb, void *data)
 {
 	struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
-	struct cb_data *cbd = cb_data_new(cb, data);
-	char buf[64];
 
-	if (!cbd)
-		goto error;
+	DBG("");
 
-	cbd->user = gc;
-
-	snprintf(buf, sizeof(buf), "AT+CGACT=0,%u", id);
-
-	if (g_at_chat_send(gcd->chat, buf, none_prefix,
-				at_cgact_down_cb, cbd, g_free) > 0)
-		return;
-

drivers/atmodem/gprs-context.c static void at_gprs_deactivate_primary(struct ofono_gprs_context *gc, · line 269

Quoted 10 lines
drivers/atmodem/gprs-context.c +4 -4
-error:
-	if (cbd)
-		g_free(cbd);
+	gcd->state = STATE_DISABLING;
+	gcd->down_cb = cb;
+	gcd->cb_data = data;
 
-	CALLBACK_WITH_FAILURE(cb, data);
+	g_at_ppp_shutdown(gcd->ppp);
 }

Do we handle g_at_chat_resume correctly and not resume and already active GAtChat?

Regards

Marcel