Skip to contentSkip to conversation

ublox: netreg: Also subscribe to UREG URC's

PATCH Superseded [email protected] 2 participants 8 messages Sep 29, 2019 – Oct 1, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ublox: netreg: Also subscribe to UREG URC's

From: Richard Röjfors <[email protected]>

It turns out that both L2xx and L4xx modems are a bit buggy when it comes to send CREG URC's when the tech changes. Try to overcome this by subscribing to both UREG and CREG, and poll the other when any of the URC's are received. Protect from doing simultanious polls though.

---
 drivers/ubloxmodem/network-registration.c | 198 ++++++++++++++++------
 1 file changed, 148 insertions(+), 50 deletions(-)
drivers/ubloxmodem/network-registration.c +148 -50
index 69af4644..ad905fb3 100644
@@ -47,11 +47,15 @@
 static const char *none_prefix[] = { NULL };
 static const char *cmer_prefix[] = { "+CMER:", NULL };
 static const char *ureg_prefix[] = { "+UREG:", NULL };
+static const char *creg_prefix[] = { "+CREG:", NULL };
+
+#define UBLOX_UPDATING_STATUS 0x01
 
 struct netreg_data {
 	struct at_netreg_data at_data;
 
 	const struct ublox_model *model;
+	int flags;
 };
 
 struct tech_query {
@@ -213,13 +217,72 @@ static void ctze_notify(GAtResult *result, gpointer user_data)
 	ofono_netreg_time_notify(netreg, &nd->time);
 }
 
-static void ublox_query_tech_cb(gboolean ok, GAtResult *result,
+static int ublox_ureg_state_to_tech(int state)
+{
+	switch (state) {
+	case 1:
+		return ACCESS_TECHNOLOGY_GSM;
+	case 2:
+		return ACCESS_TECHNOLOGY_GSM_EGPRS;
+	case 3:
+		return ACCESS_TECHNOLOGY_UTRAN;
+	case 4:
+		return ACCESS_TECHNOLOGY_UTRAN_HSDPA;
+	case 5:
+		return ACCESS_TECHNOLOGY_UTRAN_HSUPA;
+	case 6:
+		return ACCESS_TECHNOLOGY_UTRAN_HSDPA_HSUPA;
+	case 7:
+		return ACCESS_TECHNOLOGY_EUTRAN;
+	case 8:
+		return ACCESS_TECHNOLOGY_GSM;
+	case 9:
+		return ACCESS_TECHNOLOGY_GSM_EGPRS;
+	default:
+		/* Not registered for PS (0) or something unknown (>9)... */
+		return -1;
+	}
+}
+
+static gboolean is_registered(int status)
+{
+	return status == NETWORK_REGISTRATION_STATUS_REGISTERED ||
+		status == NETWORK_REGISTRATION_STATUS_ROAMING;
+}
+
+static void ublox_creg_cb(gboolean ok, GAtResult *result,
+				gpointer user_data)
+{
+	struct tech_query *tq = user_data;
+	struct netreg_data *nd = ofono_netreg_get_data(tq->netreg);
+	int status, lac, ci, tech;
+
+	nd->flags &= ~UBLOX_UPDATING_STATUS;
+
+	if (!ok)
+		return;
+
+	if (at_util_parse_reg(result, "+CREG:", NULL, &status,
+				&lac, &ci, &tech, OFONO_VENDOR_GENERIC) == FALSE)
+		return;
+
+	/* The query provided a tech, use that */
+	if (is_registered(status) && tq->tech != -1)
+		tech = tq->tech;
+
+	ofono_netreg_status_notify(tq->netreg, status, lac, ci, tech);
+}
+
+static void ublox_ureg_cb(gboolean ok, GAtResult *result,
 							gpointer user_data)
 {
 	struct tech_query *tq = user_data;
+	struct netreg_data *nd = ofono_netreg_get_data(tq->netreg);
 	GAtResultIter iter;
 	gint enabled, state;
-	int tech = -1;
+	int tech = tq->tech;
+
+	nd->flags &= ~UBLOX_UPDATING_STATUS;
 
 	if (!ok)
 		goto error;
@@ -235,48 +298,49 @@ static void ublox_query_tech_cb(gboolean ok, GAtResult *result,
 	if (!g_at_result_iter_next_number(&iter, &state))
 		return;
 
-	switch (state) {
-	case 0:
-		/* Not registered for PS, then we have to trust CREG... */
-		tech = tq->tech;
-		break;
-	case 1:
-		tech = ACCESS_TECHNOLOGY_GSM;
-		break;
-	case 2:
-		tech = ACCESS_TECHNOLOGY_GSM_EGPRS;
-		break;
-	case 3:
-		tech = ACCESS_TECHNOLOGY_UTRAN;
-		break;
-	case 4:
-		tech = ACCESS_TECHNOLOGY_UTRAN_HSDPA;
-		break;
-	case 5:
-		tech = ACCESS_TECHNOLOGY_UTRAN_HSUPA;
-		break;
-	case 6:
-		tech = ACCESS_TECHNOLOGY_UTRAN_HSDPA_HSUPA;
-		break;
-	case 7:
-		tech = ACCESS_TECHNOLOGY_EUTRAN;
-		break;
-	case 8:
-		tech = ACCESS_TECHNOLOGY_GSM;
-		break;
-	case 9:
-		tech = ACCESS_TECHNOLOGY_GSM_EGPRS;
-		break;
-	default:
-		/* Not registered for PS or something unknown, trust CREG... */
+	tech = ublox_ureg_state_to_tech(state);
+	if (tech < 0)
+		/* No valid UREG status, we have to trust CREG... */
 		tech = tq->tech;
-	}
 
 error:
 	ofono_netreg_status_notify(tq->netreg,
 			tq->status, tq->lac, tq->ci, tech);
 }
 
+static void ureg_notify(GAtResult *result, gpointer user_data)
+{
+	struct ofono_netreg *netreg = user_data;
+	int state;
+	struct netreg_data *nd = ofono_netreg_get_data(netreg);
+	struct tech_query *tq;
+	GAtResultIter iter;
+
+	if (nd->flags & UBLOX_UPDATING_STATUS)
+		return;
+
+	g_at_result_iter_init(&iter, result);
+
+	if (!g_at_result_iter_next(&iter, "+UREG:"))
+		return;
+
+	if (!g_at_result_iter_next_number(&iter, &state))
+		return;
+
+	tq = g_new0(struct tech_query, 1);
+
+	tq->tech = ublox_ureg_state_to_tech(state);
+	tq->netreg = netreg;
+
+	if (g_at_chat_send(nd->at_data.chat, "AT+CREG?", creg_prefix,
+			ublox_creg_cb, tq, g_free) > 0) {
+		nd->flags |= UBLOX_UPDATING_STATUS;
+		return;
+	}
+
+	g_free(tq);
+}
+
 static void creg_notify(GAtResult *result, gpointer user_data)
 {
 	struct ofono_netreg *netreg = user_data;
@@ -284,11 +348,14 @@ static void creg_notify(GAtResult *result, gpointer user_data)
 	struct netreg_data *nd = ofono_netreg_get_data(netreg);
 	struct tech_query *tq;
 
+	if (nd->flags & UBLOX_UPDATING_STATUS)
+		return;
+
 	if (at_util_parse_reg_unsolicited(result, "+CREG:", &status,
 			&lac, &ci, &tech, OFONO_VENDOR_GENERIC) == FALSE)
 		return;
 
-	if (status != 1 && status != 5)
+	if (!is_registered(status))
 		goto notify;
 
 	if (ublox_is_toby_l4(nd->model) || ublox_is_toby_l2(nd->model)) {
@@ -301,13 +368,15 @@ static void creg_notify(GAtResult *result, gpointer user_data)
 		tq->netreg = netreg;
 
 		if (g_at_chat_send(nd->at_data.chat, "AT+UREG?", ureg_prefix,
-				ublox_query_tech_cb, tq, g_free) > 0)
+				ublox_ureg_cb, tq, g_free) > 0) {
+			nd->flags |= UBLOX_UPDATING_STATUS;
 			return;
+		}
 
 		g_free(tq);
 	}
 
-	if ((status == 1 || status == 5) && tech == -1)
+	if (tech == -1)
 		tech = nd->at_data.tech;
 
 notify:
@@ -322,24 +391,56 @@ static void at_cmer_not_supported(struct ofono_netreg *netreg)
 	ofono_netreg_remove(netreg);
 }
 
+static void ublox_finish_registration(struct ofono_netreg *netreg)
+{
+	struct netreg_data *nd = ofono_netreg_get_data(netreg);
+
+	if (ublox_is_toby_l4(nd->model) || ublox_is_toby_l2(nd->model))
+		g_at_chat_register(nd->at_data.chat, "+UREG:",
+					ureg_notify, FALSE, netreg, NULL);
+
+	g_at_chat_register(nd->at_data.chat, "+CIEV:",
+			ciev_notify, FALSE, netreg, NULL);
+
+	g_at_chat_register(nd->at_data.chat, "+CREG:",
+				creg_notify, FALSE, netreg, NULL);
+
+	ofono_netreg_register(netreg);
+}
+
+static void ublox_ureg_set_cb(gboolean ok,
+				GAtResult *result, gpointer user_data)
+{
+	struct ofono_netreg *netreg = user_data;
+
+	if (!ok) {
+		ofono_error("Unable to initialize Network Registration");
+		ofono_netreg_remove(netreg);
+		return;
+	}
+
+	ublox_finish_registration(netreg);
+}
+
 static void ublox_cmer_set_cb(gboolean ok,
 				GAtResult *result, gpointer user_data)
 {
 	struct ofono_netreg *netreg = user_data;
-	struct at_netreg_data *nd = ofono_netreg_get_data(netreg);
+	struct netreg_data *nd = ofono_netreg_get_data(netreg);
 
 	if (!ok) {
 		at_cmer_not_supported(netreg);
 		return;
 	}
 
-	g_at_chat_register(nd->chat, "+CIEV:",
-			ciev_notify, FALSE, netreg, NULL);
+	if (ublox_is_toby_l4(nd->model) || ublox_is_toby_l2(nd->model)) {
+		g_at_chat_send(nd->at_data.chat, "AT+UREG=1", none_prefix,
+			ublox_ureg_set_cb, netreg, NULL);
 
-	g_at_chat_register(nd->chat, "+CREG:",
-				creg_notify, FALSE, netreg, NULL);
+		return;
+	}
 
-	ofono_netreg_register(netreg);
+	ublox_finish_registration(netreg);
 }
 
 static void ublox_creg_set_cb(gboolean ok,
@@ -354,12 +455,9 @@ static void ublox_creg_set_cb(gboolean ok,
 		return;
 	}
 
-	if (ublox_is_toby_l4(nd->model)) {
+	if (ublox_is_toby_l4(nd->model))
 		/* FIXME */
 		ofono_error("TOBY L4 requires polling of ECSQ");
-		ofono_error("TOBY L4 wants UREG notifications for"
-				" tech updates");
-	}
 
 	/* Register for network time update reports */
 	if (ublox_is_toby_l2(nd->model)) {
Signature · 1 line
2.20.1
#1 lore raw

Hi,

Here is a log showing why this is needed, no CREG for more than 30 minutes when changing tech from UTRAN to E-UTRAN.

This happens on a L210, but as I understood Jonas, and comments in the code, the L4xx suffers from similar issues, so I added this for both L4
and L2.

Sep 23 21:45:37 info : Aux: < \r\n+CIEV: 2,3\r\n
Sep 23 21:59:29 info : Aux: < \r\n+CGREG: 4\r\n
Sep 23 21:59:29 debug : ../git/src/gprs.c:ofono_gprs_status_notify()
/ublox_0 status unknown (4)
Sep 23 21:59:29 debug : ../git/src/gprs.c:ofono_gprs_detached_notify()
/ublox_0
Sep 23 21:59:29 info : Aux: < \r\n+UREG: 0\r\n
Sep 23 21:59:29 info : Aux: < \r\n+CIEV: 9,1\r\n

We get an UREG URC indicating E-UTRAN,.

Sep 23 21:59:29 info : Aux: < \r\n+UREG: 7\r\n
Sep 23 21:59:29 info : Aux: < \r\n+CIEV: 9,2\r\n
Sep 23 21:59:29 info : Aux: < \r\n+CGEV: NW MODIFY 1,0,0\r\n
Sep 23 21:59:31 info : Aux: < \r\n+CIEV: 2,2\r\n
Sep 23 21:59:39 info : Aux: < \r\n+CIEV: 2,3\r\n
Sep 23 21:59:44 info : Aux: < \r\n+CIEV: 2,2\r\n
Sep 23 22:01:38 info : Aux: < \r\n+CIEV: 2,3\r\n
Sep 23 22:01:43 info : Aux: < \r\n+CIEV: 2,2\r\n

CREG After 30+ minutes!! Indicating UTRAN (which is incorrect, but worked around in another commit)

Sep 23 22:29:39 info : Aux: < \r\n+CREG: 5,"0000","00000000",6\r\n

BR
Richard

Den sön 29 sep. 2019 kl 21:40 skrev <[email protected]>:

Quoted 316 lines

From: Richard Röjfors <[email protected]>

It turns out that both L2xx and L4xx modems are a bit buggy when it comes to send CREG URC's when the tech changes. Try to overcome this by subscribing to both UREG and CREG, and poll the other when any of the URC's are received. Protect from doing simultanious polls though.

---
 drivers/ubloxmodem/network-registration.c | 198 ++++++++++++++++------
 1 file changed, 148 insertions(+), 50 deletions(-)
drivers/ubloxmodem/network-registration.c +65 -2
index 69af4644..ad905fb3 100644
@@ -47,11 +47,15 @@
 static const char *none_prefix[] = { NULL };
 static const char *cmer_prefix[] = { "+CMER:", NULL };
 static const char *ureg_prefix[] = { "+UREG:", NULL };
+static const char *creg_prefix[] = { "+CREG:", NULL };
+
+#define UBLOX_UPDATING_STATUS 0x01
 
 struct netreg_data {
        struct at_netreg_data at_data;
 
        const struct ublox_model *model;
+       int flags;
 };
 
 struct tech_query {
@@ -213,13 +217,72 @@ static void ctze_notify(GAtResult *result, gpointer
 user_data)
        ofono_netreg_time_notify(netreg, &nd->time);
 }
 
-static void ublox_query_tech_cb(gboolean ok, GAtResult *result,
+static int ublox_ureg_state_to_tech(int state)
+{
+       switch (state) {
+       case 1:
+               return ACCESS_TECHNOLOGY_GSM;
+       case 2:
+               return ACCESS_TECHNOLOGY_GSM_EGPRS;
+       case 3:
+               return ACCESS_TECHNOLOGY_UTRAN;
+       case 4:
+               return ACCESS_TECHNOLOGY_UTRAN_HSDPA;
+       case 5:
+               return ACCESS_TECHNOLOGY_UTRAN_HSUPA;
+       case 6:
+               return ACCESS_TECHNOLOGY_UTRAN_HSDPA_HSUPA;
+       case 7:
+               return ACCESS_TECHNOLOGY_EUTRAN;
+       case 8:
+               return ACCESS_TECHNOLOGY_GSM;
+       case 9:
+               return ACCESS_TECHNOLOGY_GSM_EGPRS;
+       default:
+               /* Not registered for PS (0) or something unknown (>9)...
 */
+               return -1;
+       }
+}
+
+static gboolean is_registered(int status)
+{
+       return status == NETWORK_REGISTRATION_STATUS_REGISTERED ||
+               status == NETWORK_REGISTRATION_STATUS_ROAMING;
+}
+
+static void ublox_creg_cb(gboolean ok, GAtResult *result,
+                               gpointer user_data)
+{
+       struct tech_query *tq = user_data;
+       struct netreg_data *nd = ofono_netreg_get_data(tq->netreg);
+       int status, lac, ci, tech;
+
+       nd->flags &= ~UBLOX_UPDATING_STATUS;
+
+       if (!ok)
+               return;
+
+       if (at_util_parse_reg(result, "+CREG:", NULL, &status,
+                               &lac, &ci, &tech, OFONO_VENDOR_GENERIC) ==
 FALSE)
+               return;
+
+       /* The query provided a tech, use that */
+       if (is_registered(status) && tq->tech != -1)
+               tech = tq->tech;
+
+       ofono_netreg_status_notify(tq->netreg, status, lac, ci, tech);
+}
+
+static void ublox_ureg_cb(gboolean ok, GAtResult *result,
                                                        gpointer user_data)
 {
        struct tech_query *tq = user_data;
+       struct netreg_data *nd = ofono_netreg_get_data(tq->netreg);
        GAtResultIter iter;
        gint enabled, state;
-       int tech = -1;
+       int tech = tq->tech;
+
+       nd->flags &= ~UBLOX_UPDATING_STATUS;
        if (!ok)
                goto error;
drivers/ubloxmodem/network-registration.c +0 -34
@@ -235,48 +298,49 @@ static void ublox_query_tech_cb(gboolean ok,
 GAtResult *result,
        if (!g_at_result_iter_next_number(&iter, &state))
                return;
 
-       switch (state) {
-       case 0:
-               /* Not registered for PS, then we have to trust CREG... */
-               tech = tq->tech;
-               break;
-       case 1:
-               tech = ACCESS_TECHNOLOGY_GSM;
-               break;
-       case 2:
-               tech = ACCESS_TECHNOLOGY_GSM_EGPRS;
-               break;
-       case 3:
-               tech = ACCESS_TECHNOLOGY_UTRAN;
-               break;
-       case 4:
-               tech = ACCESS_TECHNOLOGY_UTRAN_HSDPA;
-               break;
-       case 5:
-               tech = ACCESS_TECHNOLOGY_UTRAN_HSUPA;
-               break;
-       case 6:
-               tech = ACCESS_TECHNOLOGY_UTRAN_HSDPA_HSUPA;
-               break;
-       case 7:
-               tech = ACCESS_TECHNOLOGY_EUTRAN;
-               break;
-       case 8:
-               tech = ACCESS_TECHNOLOGY_GSM;
-               break;
-       case 9:
-               tech = ACCESS_TECHNOLOGY_GSM_EGPRS;
-               break;
-       default:
-               /* Not registered for PS or something unknown, trust

CREG... */

drivers/ubloxmodem/network-registration.c +40 -2
+       tech = ublox_ureg_state_to_tech(state);
+       if (tech < 0)
+               /* No valid UREG status, we have to trust CREG... */
                tech = tq->tech;
-       }
 
 error:
        ofono_netreg_status_notify(tq->netreg,
                        tq->status, tq->lac, tq->ci, tech);
 }
 
+static void ureg_notify(GAtResult *result, gpointer user_data)
+{
+       struct ofono_netreg *netreg = user_data;
+       int state;
+       struct netreg_data *nd = ofono_netreg_get_data(netreg);
+       struct tech_query *tq;
+       GAtResultIter iter;
+
+       if (nd->flags & UBLOX_UPDATING_STATUS)
+               return;
+
+       g_at_result_iter_init(&iter, result);
+
+       if (!g_at_result_iter_next(&iter, "+UREG:"))
+               return;
+
+       if (!g_at_result_iter_next_number(&iter, &state))
+               return;
+
+       tq = g_new0(struct tech_query, 1);
+
+       tq->tech = ublox_ureg_state_to_tech(state);
+       tq->netreg = netreg;
+
+       if (g_at_chat_send(nd->at_data.chat, "AT+CREG?", creg_prefix,
+                       ublox_creg_cb, tq, g_free) > 0) {
+               nd->flags |= UBLOX_UPDATING_STATUS;
+               return;
+       }
+
+       g_free(tq);
+}
+
 static void creg_notify(GAtResult *result, gpointer user_data)
 {
        struct ofono_netreg *netreg = user_data;
@@ -284,11 +348,14 @@ static void creg_notify(GAtResult *result, gpointer
 user_data)
        struct netreg_data *nd = ofono_netreg_get_data(netreg);
        struct tech_query *tq;
 
+       if (nd->flags & UBLOX_UPDATING_STATUS)
+               return;
+
        if (at_util_parse_reg_unsolicited(result, "+CREG:", &status,
                        &lac, &ci, &tech, OFONO_VENDOR_GENERIC) == FALSE)
                return;
 
-       if (status != 1 && status != 5)
+       if (!is_registered(status))
                goto notify;
 
        if (ublox_is_toby_l4(nd->model) || ublox_is_toby_l2(nd->model)) {
drivers/ubloxmodem/network-registration.c +4 -2
@@ -301,13 +368,15 @@ static void creg_notify(GAtResult *result, gpointer
 user_data)
                tq->netreg = netreg;
 
                if (g_at_chat_send(nd->at_data.chat, "AT+UREG?",
 ureg_prefix,
-                               ublox_query_tech_cb, tq, g_free) > 0)
+                               ublox_ureg_cb, tq, g_free) > 0) {
+                       nd->flags |= UBLOX_UPDATING_STATUS;
                        return;
+               }
 
                g_free(tq);
        }
 
-       if ((status == 1 || status == 5) && tech == -1)
+       if (tech == -1)
                tech = nd->at_data.tech;

notify:

drivers/ubloxmodem/network-registration.c +38 -6
@@ -322,24 +391,56 @@ static void at_cmer_not_supported(struct
 ofono_netreg *netreg)
        ofono_netreg_remove(netreg);
 }
 
+static void ublox_finish_registration(struct ofono_netreg *netreg)
+{
+       struct netreg_data *nd = ofono_netreg_get_data(netreg);
+
+       if (ublox_is_toby_l4(nd->model) || ublox_is_toby_l2(nd->model))
+               g_at_chat_register(nd->at_data.chat, "+UREG:",
+                                       ureg_notify, FALSE, netreg, NULL);
+
+       g_at_chat_register(nd->at_data.chat, "+CIEV:",
+                       ciev_notify, FALSE, netreg, NULL);
+
+       g_at_chat_register(nd->at_data.chat, "+CREG:",
+                               creg_notify, FALSE, netreg, NULL);
+
+       ofono_netreg_register(netreg);
+}
+
+static void ublox_ureg_set_cb(gboolean ok,
+                               GAtResult *result, gpointer user_data)
+{
+       struct ofono_netreg *netreg = user_data;
+
+       if (!ok) {
+               ofono_error("Unable to initialize Network Registration");
+               ofono_netreg_remove(netreg);
+               return;
+       }
+
+       ublox_finish_registration(netreg);
+}
+
 static void ublox_cmer_set_cb(gboolean ok,
                                GAtResult *result, gpointer user_data)
 {
        struct ofono_netreg *netreg = user_data;
-       struct at_netreg_data *nd = ofono_netreg_get_data(netreg);
+       struct netreg_data *nd = ofono_netreg_get_data(netreg);
 
        if (!ok) {
                at_cmer_not_supported(netreg);
                return;
        }
 
-       g_at_chat_register(nd->chat, "+CIEV:",
-                       ciev_notify, FALSE, netreg, NULL);
+       if (ublox_is_toby_l4(nd->model) || ublox_is_toby_l2(nd->model)) {
+               g_at_chat_send(nd->at_data.chat, "AT+UREG=1", none_prefix,
+                       ublox_ureg_set_cb, netreg, NULL);
 
-       g_at_chat_register(nd->chat, "+CREG:",
-                               creg_notify, FALSE, netreg, NULL);
+               return;
+       }
 
-       ofono_netreg_register(netreg);
+       ublox_finish_registration(netreg);
 }
 

static void ublox_creg_set_cb(gboolean ok,

drivers/ubloxmodem/network-registration.c +1 -5
@@ -354,12 +455,9 @@ static void ublox_creg_set_cb(gboolean ok,
                return;
        }
 
-       if (ublox_is_toby_l4(nd->model)) {
+       if (ublox_is_toby_l4(nd->model))
                /* FIXME */
                ofono_error("TOBY L4 requires polling of ECSQ");
-               ofono_error("TOBY L4 wants UREG notifications for"
-                               " tech updates");
-       }
 
        /* Register for network time update reports */
        if (ublox_is_toby_l2(nd->model)) {
--

2.20.1

#2 lore raw
In reply to [email protected]

Hi Richard,

Quoted 7 lines · richard.rojfors(a)gmail.com

From: Richard Röjfors <[email protected]>

It turns out that both L2xx and L4xx modems are a bit buggy when it comes to send CREG URC's when the tech changes. Try to overcome this by subscribing to both UREG and CREG, and poll the other when any of the URC's are received. Protect from doing simultanious polls though.

nit: typo, simultaneous

Also, this patch doesn't apply?

denkenz(a)localhost ~/ofono-master $ git am --3way ~/merge/\[PATCH\]\
ublox\:\ netreg\:\ Also\ subscribe\ to\ UREG\ URC\'s.eml
Applying: ublox: netreg: Also subscribe to UREG URC's
Using index info to reconstruct a base tree...
error: patch failed: drivers/ubloxmodem/network-registration.c:47
error: drivers/ubloxmodem/network-registration.c: patch does not apply
error: Did you hand edit your patch?
It does not apply to blobs recorded in its index.
Patch failed at 0001 ublox: netreg: Also subscribe to UREG URC's
hint: Use 'git am --show-current-patch' to see the failed patch When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort".

Quoted 11 lines
---
  drivers/ubloxmodem/network-registration.c | 198 ++++++++++++++++------
  1 file changed, 148 insertions(+), 50 deletions(-)
drivers/ubloxmodem/network-registration.c +0 -0
index 69af4644..ad905fb3 100644
@@ -47,11 +47,15 @@
  static const char *none_prefix[] = { NULL };
  static const char *cmer_prefix[] = { "+CMER:", NULL };

drivers/ubloxmodem/network-registration.c line 58

Quoted 10 lines
drivers/ubloxmodem/network-registration.c +4 -0
  static const char *ureg_prefix[] = { "+UREG:", NULL };
+static const char *creg_prefix[] = { "+CREG:", NULL };
+
+#define UBLOX_UPDATING_STATUS 0x01
 
  struct netreg_data {
  	struct at_netreg_data at_data;
 
  	const struct ublox_model *model;
+	int flags;

Nowadays we prefer to use bool foo : 1 notation to manipulate bit fields.

So maybe something like:

bool updating_status : 1;

Quoted 37 lines
drivers/ubloxmodem/network-registration.c +29 -1
  };
 
  struct tech_query {
@@ -213,13 +217,72 @@ static void ctze_notify(GAtResult *result, gpointer user_data)
  	ofono_netreg_time_notify(netreg, &nd->time);
  }
 
-static void ublox_query_tech_cb(gboolean ok, GAtResult *result,
+static int ublox_ureg_state_to_tech(int state)
+{
+	switch (state) {
+	case 1:
+		return ACCESS_TECHNOLOGY_GSM;
+	case 2:
+		return ACCESS_TECHNOLOGY_GSM_EGPRS;
+	case 3:
+		return ACCESS_TECHNOLOGY_UTRAN;
+	case 4:
+		return ACCESS_TECHNOLOGY_UTRAN_HSDPA;
+	case 5:
+		return ACCESS_TECHNOLOGY_UTRAN_HSUPA;
+	case 6:
+		return ACCESS_TECHNOLOGY_UTRAN_HSDPA_HSUPA;
+	case 7:
+		return ACCESS_TECHNOLOGY_EUTRAN;
+	case 8:
+		return ACCESS_TECHNOLOGY_GSM;
+	case 9:
+		return ACCESS_TECHNOLOGY_GSM_EGPRS;
+	default:
+		/* Not registered for PS (0) or something unknown (>9)... */
+		return -1;
+	}
+}
+
+static gboolean is_registered(int status)
+{

drivers/ubloxmodem/network-registration.c static void ctze_notify(GAtResult *result, gpointer user_data) · line 258

Quoted 10 lines
drivers/ubloxmodem/network-registration.c +10 -0
+	return status == NETWORK_REGISTRATION_STATUS_REGISTERED ||
+		status == NETWORK_REGISTRATION_STATUS_ROAMING;
+}
+
+static void ublox_creg_cb(gboolean ok, GAtResult *result,
+				gpointer user_data)
+{
+	struct tech_query *tq = user_data;
+	struct netreg_data *nd = ofono_netreg_get_data(tq->netreg);
+	int status, lac, ci, tech;

One variable declaration per line please.

Quoted 31 lines
drivers/ubloxmodem/network-registration.c +22 -1
+
+	nd->flags &= ~UBLOX_UPDATING_STATUS;
+
+	if (!ok)
+		return;
+
+	if (at_util_parse_reg(result, "+CREG:", NULL, &status,
+				&lac, &ci, &tech, OFONO_VENDOR_GENERIC) == FALSE)
+		return;
+
+	/* The query provided a tech, use that */
+	if (is_registered(status) && tq->tech != -1)
+		tech = tq->tech;
+
+	ofono_netreg_status_notify(tq->netreg, status, lac, ci, tech);
+}
+
+static void ublox_ureg_cb(gboolean ok, GAtResult *result,
  							gpointer user_data)
  {
  	struct tech_query *tq = user_data;
+	struct netreg_data *nd = ofono_netreg_get_data(tq->netreg);
  	GAtResultIter iter;
  	gint enabled, state;
-	int tech = -1;
+	int tech = tq->tech;
+
+	nd->flags &= ~UBLOX_UPDATING_STATUS;
 
  	if (!ok)
  		goto error;

<snip>

Quoted 23 lines
drivers/ubloxmodem/network-registration.c +23 -0
+static void ureg_notify(GAtResult *result, gpointer user_data)
+{
+	struct ofono_netreg *netreg = user_data;
+	int state;
+	struct netreg_data *nd = ofono_netreg_get_data(netreg);
+	struct tech_query *tq;
+	GAtResultIter iter;
+
+	if (nd->flags & UBLOX_UPDATING_STATUS)
+		return;
+
+	g_at_result_iter_init(&iter, result);
+
+	if (!g_at_result_iter_next(&iter, "+UREG:"))
+		return;
+
+	if (!g_at_result_iter_next_number(&iter, &state))
+		return;
+
+	tq = g_new0(struct tech_query, 1);
+
+	tq->tech = ublox_ureg_state_to_tech(state);
+	tq->netreg = netreg;

drivers/ubloxmodem/network-registration.c static void ublox_query_tech_cb(gboolean ok, GAtResult *result, · line 343

Quoted 10 lines
drivers/ubloxmodem/network-registration.c +10 -0
+
+	if (g_at_chat_send(nd->at_data.chat, "AT+CREG?", creg_prefix,
+			ublox_creg_cb, tq, g_free) > 0) {
+		nd->flags |= UBLOX_UPDATING_STATUS;
+		return;
+	}
+
+	g_free(tq);
+}
+

So it seems to me you're firing off a CREG? when UREG unsolicited comes in but also firing off UREG? when CREG unsolicited comes in.

Briefly looking at some random ublox docs via google, I understand that UREG was meant to tell you what Packet Switching technology was active. One can still be connected to Voice Circuit Switched service with no PS active. So I'm not entirely sure whether the two can be assumed to be related the way you do it here...?

I really see nothing that can go wrong in being paranoid and re-querying CREG/UREG, so I'm ok taking this up. Just wondering whether you'll end up fixing this again later.

Regards,
-Denis

#3 lore raw

Den mån 30 sep. 2019 kl 19:00 skrev Denis Kenzior <[email protected]>:

Quoted 5 lines

Hi Richard,

Quoted 2 lines · richard.rojfors(a)gmail.com

From: Richard Röjfors <[email protected]>

Quoted 8 lines
Quoted 5 lines

It turns out that both L2xx and L4xx modems are a bit buggy when it comes to send CREG URC's when the tech changes. Try to overcome this by subscribing to both UREG and CREG, and poll the other when any of the URC's are received. Protect from doing simultanious polls though.

nit: typo, simultaneous

Thanks :)

Quoted 3 lines

Also, this patch doesn't apply?

This is strange, I did a git send-email.

Anyway, I have changes to do according to comments below, so I will make sure to rebase on master.

Quoted 35 lines

denkenz(a)localhost ~/ofono-master $ git am --3way ~/merge/\[PATCH\]\
ublox\:\ netreg\:\ Also\ subscribe\ to\ UREG\ URC\'s.eml
Applying: ublox: netreg: Also subscribe to UREG URC's
Using index info to reconstruct a base tree...
error: patch failed: drivers/ubloxmodem/network-registration.c:47
error: drivers/ubloxmodem/network-registration.c: patch does not apply
error: Did you hand edit your patch?
It does not apply to blobs recorded in its index.
Patch failed at 0001 ublox: netreg: Also subscribe to UREG URC's
hint: Use 'git am --show-current-patch' to see the failed patch When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort".

Review

Quoted 5 lines
---
  drivers/ubloxmodem/network-registration.c | 198 ++++++++++++++++------
  1 file changed, 148 insertions(+), 50 deletions(-)
(quoted diff) +0 -0

b/drivers/ubloxmodem/network-registration.c

Quoted 14 lines

index 69af4644..ad905fb3 100644

drivers/ubloxmodem/network-registration.c +3 -0
@@ -47,11 +47,15 @@
  static const char *none_prefix[] = { NULL };
  static const char *cmer_prefix[] = { "+CMER:", NULL };
  static const char *ureg_prefix[] = { "+UREG:", NULL };
+static const char *creg_prefix[] = { "+CREG:", NULL };
+
+#define UBLOX_UPDATING_STATUS 0x01
 
  struct netreg_data {
      struct at_netreg_data at_data;
 
Quoted 9 lines

drivers/ubloxmodem/network-registration.c line 58

Quoted 2 lines
drivers/ubloxmodem/network-registration.c +1 -0
      const struct ublox_model *model;
+     int flags;

Nowadays we prefer to use bool foo : 1 notation to manipulate bit fields.

So maybe something like:

bool updating_status : 1;

Will filx.

Quoted 44 lines

drivers/ubloxmodem/network-registration.c static void ctze_notify(GAtResult *result,

Quoted 5 lines
drivers/ubloxmodem/network-registration.c +0 -0
  };
 
  struct tech_query {
@@ -213,13 +217,72 @@ static void ctze_notify(GAtResult *result,

gpointer user_data)

Quoted 17 lines
drivers/ubloxmodem/network-registration.c +13 -1
      ofono_netreg_time_notify(netreg, &nd->time);
  }
 
-static void ublox_query_tech_cb(gboolean ok, GAtResult *result,
+static int ublox_ureg_state_to_tech(int state)
+{
+     switch (state) {
+     case 1:
+             return ACCESS_TECHNOLOGY_GSM;
+     case 2:
+             return ACCESS_TECHNOLOGY_GSM_EGPRS;
+     case 3:
+             return ACCESS_TECHNOLOGY_UTRAN;
+     case 4:
+             return ACCESS_TECHNOLOGY_UTRAN_HSDPA;
+     case 5:
+             return ACCESS_TECHNOLOGY_UTRAN_HSUPA;

drivers/ubloxmodem/network-registration.c static void ctze_notify(GAtResult *result, gpointer user_data)

Quoted 10 lines
drivers/ubloxmodem/network-registration.c +10 -0
+     case 6:
+             return ACCESS_TECHNOLOGY_UTRAN_HSDPA_HSUPA;
+     case 7:
+             return ACCESS_TECHNOLOGY_EUTRAN;
+     case 8:
+             return ACCESS_TECHNOLOGY_GSM;
+     case 9:
+             return ACCESS_TECHNOLOGY_GSM_EGPRS;
+     default:
+             /* Not registered for PS (0) or something unknown (>9)...

*/

Quoted 10 lines
drivers/ubloxmodem/network-registration.c +10 -0
+             return -1;
+     }
+}
+
+static gboolean is_registered(int status)
+{
+     return status == NETWORK_REGISTRATION_STATUS_REGISTERED ||
+             status == NETWORK_REGISTRATION_STATUS_ROAMING;
+}
+
Quoted 9 lines

drivers/ubloxmodem/network-registration.c static void ctze_notify(GAtResult *result, gpointer user_data) · line 258

Quoted 6 lines
drivers/ubloxmodem/network-registration.c +6 -0
+static void ublox_creg_cb(gboolean ok, GAtResult *result,
+                             gpointer user_data)
+{
+     struct tech_query *tq = user_data;
+     struct netreg_data *nd = ofono_netreg_get_data(tq->netreg);
+     int status, lac, ci, tech;

One variable declaration per line please.

Will fix

Quoted 64 lines

drivers/ubloxmodem/network-registration.c static void ctze_notify(GAtResult *result, gpointer user_data)

Quoted 9 lines
drivers/ubloxmodem/network-registration.c +8 -0
+
+     nd->flags &= ~UBLOX_UPDATING_STATUS;
+
+     if (!ok)
+             return;
+
+     if (at_util_parse_reg(result, "+CREG:", NULL, &status,
+                             &lac, &ci, &tech, OFONO_VENDOR_GENERIC) ==

FALSE)

Quoted 23 lines
drivers/ubloxmodem/network-registration.c +14 -1
+             return;
+
+     /* The query provided a tech, use that */
+     if (is_registered(status) && tq->tech != -1)
+             tech = tq->tech;
+
+     ofono_netreg_status_notify(tq->netreg, status, lac, ci, tech);
+}
+
+static void ublox_ureg_cb(gboolean ok, GAtResult *result,
                                                      gpointer user_data)
  {
      struct tech_query *tq = user_data;
+     struct netreg_data *nd = ofono_netreg_get_data(tq->netreg);
      GAtResultIter iter;
      gint enabled, state;
-     int tech = -1;
+     int tech = tq->tech;
+
+     nd->flags &= ~UBLOX_UPDATING_STATUS;
 
      if (!ok)
              goto error;

<snip>

Quoted 28 lines
drivers/ubloxmodem/network-registration.c +28 -0
+static void ureg_notify(GAtResult *result, gpointer user_data)
+{
+     struct ofono_netreg *netreg = user_data;
+     int state;
+     struct netreg_data *nd = ofono_netreg_get_data(netreg);
+     struct tech_query *tq;
+     GAtResultIter iter;
+
+     if (nd->flags & UBLOX_UPDATING_STATUS)
+             return;
+
+     g_at_result_iter_init(&iter, result);
+
+     if (!g_at_result_iter_next(&iter, "+UREG:"))
+             return;
+
+     if (!g_at_result_iter_next_number(&iter, &state))
+             return;
+
+     tq = g_new0(struct tech_query, 1);
+
+     tq->tech = ublox_ureg_state_to_tech(state);
+     tq->netreg = netreg;
+
+     if (g_at_chat_send(nd->at_data.chat, "AT+CREG?", creg_prefix,
+                     ublox_creg_cb, tq, g_free) > 0) {
+             nd->flags |= UBLOX_UPDATING_STATUS;
+             return;
Quoted 9 lines

drivers/ubloxmodem/network-registration.c static void ublox_query_tech_cb(gboolean ok, GAtResult *result, · line 343

Quoted 5 lines
drivers/ubloxmodem/network-registration.c +5 -0
+     }
+
+     g_free(tq);
+}
+

So it seems to me you're firing off a CREG? when UREG unsolicited comes in but also firing off UREG? when CREG unsolicited comes in.

Yes that's the idea, but in case we are already firing off something, at least we
avoid acting it on the second URC.

The reason for fire off when we get CREG is that the tech is sometimes incorrect in the CREG.
The reason for fire off when we get UREG is that the CREG is sometimes missing, so we need to poll CREG to get status, lac, ci.

Quoted 6 lines

Briefly looking at some random ublox docs via google, I understand that UREG was meant to tell you what Packet Switching technology was active. One can still be connected to Voice Circuit Switched service with no PS active. So I'm not entirely sure whether the two can be assumed to be related the way you do it here...?

This is completely true. And in that case it will signal UREG status 0, and we then fall back to use the status from CREG.

I'm afraid this is the best we can do, since I have seen missing CREG's and CREG's with incorrect status. In those occasions it has fired UREG URC's at least..

Quoted 5 lines

I really see nothing that can go wrong in being paranoid and re-querying CREG/UREG, so I'm ok taking this up. Just wondering whether you'll end up fixing this again later.

I see your point. All this is to try doing the best to work around bugs in the
modem.
I'm afraid we have to live with it. Even if it gets fixed in later FW there will be modems out there that works like this. What I read from Jonas and comments in the code is that the L4xx seems even worse. For the L210 it seems like the URC's are missing only sometimes. But those situation could be annoying. We could switch from E-UTRAN to UTRAN and should attach and activate context. But ofono will still think we are on E-UTRAN and just waiting for the context to show up...(if the URCs are missing)

--Richard

#4 lore raw

PATCH v2 1/1 ublox: netreg: Also subscribe to UREG URC's

In reply to [email protected]

From: Richard Röjfors <[email protected]>

It turns out that both L2xx and L4xx modems are a bit buggy when it comes to send CREG URC's when the tech changes. Try to overcome this by subscribing to both UREG and CREG, and poll the other when any of the URC's are received. Protect from doing simultaneous polls though.

---
 drivers/ubloxmodem/network-registration.c | 205 ++++++++++++++++------
 1 file changed, 154 insertions(+), 51 deletions(-)
drivers/ubloxmodem/network-registration.c +154 -51
index 69af4644..25f239a6 100644
@@ -28,6 +28,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <stdbool.h>
 
 #include <glib.h>
 
@@ -47,11 +48,13 @@
 static const char *none_prefix[] = { NULL };
 static const char *cmer_prefix[] = { "+CMER:", NULL };
 static const char *ureg_prefix[] = { "+UREG:", NULL };
+static const char *creg_prefix[] = { "+CREG:", NULL };
 
 struct netreg_data {
 	struct at_netreg_data at_data;
 
 	const struct ublox_model *model;
+	bool updating_status : 1;
 };
 
 struct tech_query {
@@ -213,13 +216,75 @@ static void ctze_notify(GAtResult *result, gpointer user_data)
 	ofono_netreg_time_notify(netreg, &nd->time);
 }
 
-static void ublox_query_tech_cb(gboolean ok, GAtResult *result,
+static int ublox_ureg_state_to_tech(int state)
+{
+	switch (state) {
+	case 1:
+		return ACCESS_TECHNOLOGY_GSM;
+	case 2:
+		return ACCESS_TECHNOLOGY_GSM_EGPRS;
+	case 3:
+		return ACCESS_TECHNOLOGY_UTRAN;
+	case 4:
+		return ACCESS_TECHNOLOGY_UTRAN_HSDPA;
+	case 5:
+		return ACCESS_TECHNOLOGY_UTRAN_HSUPA;
+	case 6:
+		return ACCESS_TECHNOLOGY_UTRAN_HSDPA_HSUPA;
+	case 7:
+		return ACCESS_TECHNOLOGY_EUTRAN;
+	case 8:
+		return ACCESS_TECHNOLOGY_GSM;
+	case 9:
+		return ACCESS_TECHNOLOGY_GSM_EGPRS;
+	default:
+		/* Not registered for PS (0) or something unknown (>9)... */
+		return -1;
+	}
+}
+
+static gboolean is_registered(int status)
+{
+	return status == NETWORK_REGISTRATION_STATUS_REGISTERED ||
+		status == NETWORK_REGISTRATION_STATUS_ROAMING;
+}
+
+static void ublox_creg_cb(gboolean ok, GAtResult *result,
+				gpointer user_data)
+{
+	struct tech_query *tq = user_data;
+	struct netreg_data *nd = ofono_netreg_get_data(tq->netreg);
+	int status;
+	int lac;
+	int ci;
+	int tech;
+
+	nd->updating_status = false;
+
+	if (!ok)
+		return;
+
+	if (at_util_parse_reg(result, "+CREG:", NULL, &status,
+				&lac, &ci, &tech, OFONO_VENDOR_GENERIC) == FALSE)
+		return;
+
+	/* The query provided a tech, use that */
+	if (is_registered(status) && tq->tech != -1)
+		tech = tq->tech;
+
+	ofono_netreg_status_notify(tq->netreg, status, lac, ci, tech);
+}
+
+static void ublox_ureg_cb(gboolean ok, GAtResult *result,
 							gpointer user_data)
 {
 	struct tech_query *tq = user_data;
+	struct netreg_data *nd = ofono_netreg_get_data(tq->netreg);
 	GAtResultIter iter;
 	gint enabled, state;
-	int tech = -1;
+	int tech = tq->tech;
+
+	nd->updating_status = false;
 
 	if (!ok)
 		goto error;
@@ -235,60 +300,67 @@ static void ublox_query_tech_cb(gboolean ok, GAtResult *result,
 	if (!g_at_result_iter_next_number(&iter, &state))
 		return;
 
-	switch (state) {
-	case 0:
-		/* Not registered for PS, then we have to trust CREG... */
+	tech = ublox_ureg_state_to_tech(state);
+	if (tech < 0)
+		/* No valid UREG status, we have to trust CREG... */
 		tech = tq->tech;
-		break;
-	case 1:
-		tech = ACCESS_TECHNOLOGY_GSM;
-		break;
-	case 2:
-		tech = ACCESS_TECHNOLOGY_GSM_EGPRS;
-		break;
-	case 3:
-		tech = ACCESS_TECHNOLOGY_UTRAN;
-		break;
-	case 4:
-		tech = ACCESS_TECHNOLOGY_UTRAN_HSDPA;
-		break;
-	case 5:
-		tech = ACCESS_TECHNOLOGY_UTRAN_HSUPA;
-		break;
-	case 6:
-		tech = ACCESS_TECHNOLOGY_UTRAN_HSDPA_HSUPA;
-		break;
-	case 7:
-		tech = ACCESS_TECHNOLOGY_EUTRAN;
-		break;
-	case 8:
-		tech = ACCESS_TECHNOLOGY_GSM;
-		break;
-	case 9:
-		tech = ACCESS_TECHNOLOGY_GSM_EGPRS;
-		break;
-	default:
-		/* Not registered for PS or something unknown, trust CREG... */
-		tech = tq->tech;
-	}
 
 error:
 	ofono_netreg_status_notify(tq->netreg,
 			tq->status, tq->lac, tq->ci, tech);
 }
 
+static void ureg_notify(GAtResult *result, gpointer user_data)
+{
+	struct ofono_netreg *netreg = user_data;
+	struct netreg_data *nd = ofono_netreg_get_data(netreg);
+	struct tech_query *tq;
+	GAtResultIter iter;
+	int state;
+
+	if (nd->updating_status)
+		return;
+
+	g_at_result_iter_init(&iter, result);
+
+	if (!g_at_result_iter_next(&iter, "+UREG:"))
+		return;
+
+	if (!g_at_result_iter_next_number(&iter, &state))
+		return;
+
+	tq = g_new0(struct tech_query, 1);
+
+	tq->tech = ublox_ureg_state_to_tech(state);
+	tq->netreg = netreg;
+
+	if (g_at_chat_send(nd->at_data.chat, "AT+CREG?", creg_prefix,
+			ublox_creg_cb, tq, g_free) > 0) {
+		nd->updating_status = true;
+		return;
+	}
+
+	g_free(tq);
+}
+
 static void creg_notify(GAtResult *result, gpointer user_data)
 {
 	struct ofono_netreg *netreg = user_data;
-	int status, lac, ci, tech;
 	struct netreg_data *nd = ofono_netreg_get_data(netreg);
 	struct tech_query *tq;
+	int status;
+	int lac;
+	int ci;
+	int tech;
+
+	if (nd->updating_status)
+		return;
 
 	if (at_util_parse_reg_unsolicited(result, "+CREG:", &status,
 			&lac, &ci, &tech, OFONO_VENDOR_GENERIC) == FALSE)
 		return;
 
-	if (status != 1 && status != 5)
+	if (!is_registered(status))
 		goto notify;
 
 	if (ublox_is_toby_l4(nd->model) || ublox_is_toby_l2(nd->model)) {
@@ -301,13 +373,15 @@ static void creg_notify(GAtResult *result, gpointer user_data)
 		tq->netreg = netreg;
 
 		if (g_at_chat_send(nd->at_data.chat, "AT+UREG?", ureg_prefix,
-				ublox_query_tech_cb, tq, g_free) > 0)
+				ublox_ureg_cb, tq, g_free) > 0) {
+			nd->updating_status = true;
 			return;
+		}
 
 		g_free(tq);
 	}
 
-	if ((status == 1 || status == 5) && tech == -1)
+	if (tech == -1)
 		tech = nd->at_data.tech;
 
 notify:
@@ -322,24 +396,56 @@ static void at_cmer_not_supported(struct ofono_netreg *netreg)
 	ofono_netreg_remove(netreg);
 }
 
+static void ublox_finish_registration(struct ofono_netreg *netreg)
+{
+	struct netreg_data *nd = ofono_netreg_get_data(netreg);
+
+	if (ublox_is_toby_l4(nd->model) || ublox_is_toby_l2(nd->model))
+		g_at_chat_register(nd->at_data.chat, "+UREG:",
+					ureg_notify, FALSE, netreg, NULL);
+
+	g_at_chat_register(nd->at_data.chat, "+CIEV:",
+			ciev_notify, FALSE, netreg, NULL);
+
+	g_at_chat_register(nd->at_data.chat, "+CREG:",
+				creg_notify, FALSE, netreg, NULL);
+
+	ofono_netreg_register(netreg);
+}
+
+static void ublox_ureg_set_cb(gboolean ok,
+				GAtResult *result, gpointer user_data)
+{
+	struct ofono_netreg *netreg = user_data;
+
+	if (!ok) {
+		ofono_error("Unable to initialize Network Registration");
+		ofono_netreg_remove(netreg);
+		return;
+	}
+
+	ublox_finish_registration(netreg);
+}
+
 static void ublox_cmer_set_cb(gboolean ok,
 				GAtResult *result, gpointer user_data)
 {
 	struct ofono_netreg *netreg = user_data;
-	struct at_netreg_data *nd = ofono_netreg_get_data(netreg);
+	struct netreg_data *nd = ofono_netreg_get_data(netreg);
 
 	if (!ok) {
 		at_cmer_not_supported(netreg);
 		return;
 	}
 
-	g_at_chat_register(nd->chat, "+CIEV:",
-			ciev_notify, FALSE, netreg, NULL);
+	if (ublox_is_toby_l4(nd->model) || ublox_is_toby_l2(nd->model)) {
+		g_at_chat_send(nd->at_data.chat, "AT+UREG=1", none_prefix,
+			ublox_ureg_set_cb, netreg, NULL);
 
-	g_at_chat_register(nd->chat, "+CREG:",
-				creg_notify, FALSE, netreg, NULL);
+		return;
+	}
 
-	ofono_netreg_register(netreg);
+	ublox_finish_registration(netreg);
 }
 
 static void ublox_creg_set_cb(gboolean ok,
@@ -354,12 +460,9 @@ static void ublox_creg_set_cb(gboolean ok,
 		return;
 	}
 
-	if (ublox_is_toby_l4(nd->model)) {
+	if (ublox_is_toby_l4(nd->model))
 		/* FIXME */
 		ofono_error("TOBY L4 requires polling of ECSQ");
-		ofono_error("TOBY L4 wants UREG notifications for"
-				" tech updates");
-	}
 
 	/* Register for network time update reports */
 	if (ublox_is_toby_l2(nd->model)) {
Signature · 1 line
2.20.1
#5 lore raw

Hi Richard,

Quoted 11 lines · richard.rojfors(a)gmail.com

From: Richard Röjfors <[email protected]>

It turns out that both L2xx and L4xx modems are a bit buggy when it comes to send CREG URC's when the tech changes. Try to overcome this by subscribing to both UREG and CREG, and poll the other when any of the URC's are received. Protect from doing simultaneous polls though.

---
  drivers/ubloxmodem/network-registration.c | 205 ++++++++++++++++------
  1 file changed, 154 insertions(+), 51 deletions(-)

Still no go:

denkenz(a)localhost ~/ofono-master $ git am --3way ~/merge/\[PATCH\ v2\]\
ublox\:\ netreg\:\ Also\ subscribe\ to\ UREG\ URC\'s.eml
Applying: ublox: netreg: Also subscribe to UREG URC's
Using index info to reconstruct a base tree...
error: patch failed: drivers/ubloxmodem/network-registration.c:28
error: drivers/ubloxmodem/network-registration.c: patch does not apply
error: Did you hand edit your patch?
It does not apply to blobs recorded in its index.
Patch failed at 0001 ublox: netreg: Also subscribe to UREG URC's
hint: Use 'git am --show-current-patch' to see the failed patch When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort".

#6 lore raw

Hi Denis,

Den mån 30 sep. 2019 kl 23:10 skrev Denis Kenzior <[email protected]>:

Quoted 17 lines

Hi Richard,

Quoted 11 lines · richard.rojfors(a)gmail.com

From: Richard Röjfors <[email protected]>

It turns out that both L2xx and L4xx modems are a bit buggy when it comes to send CREG URC's when the tech changes. Try to overcome this by subscribing to both UREG and CREG, and poll the other when any of the URC's are received. Protect from doing simultaneous polls though.

---
  drivers/ubloxmodem/network-registration.c | 205 ++++++++++++++++------
  1 file changed, 154 insertions(+), 51 deletions(-)

Still no go:

Quoted 14 lines

denkenz(a)localhost ~/ofono-master $ git am --3way ~/merge/\[PATCH\ v2\]\
ublox\:\ netreg\:\ Also\ subscribe\ to\ UREG\ URC\'s.eml
Applying: ublox: netreg: Also subscribe to UREG URC's
Using index info to reconstruct a base tree...
error: patch failed: drivers/ubloxmodem/network-registration.c:28
error: drivers/ubloxmodem/network-registration.c: patch does not apply
error: Did you hand edit your patch?
It does not apply to blobs recorded in its index.
Patch failed at 0001 ublox: netreg: Also subscribe to UREG URC's
hint: Use 'git am --show-current-patch' to see the failed patch When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort".

This is strange.
I use gmail smtps, but to check if they tamper with the emails, I cloned a fresh ofono and ran git am and pasted my email, it did work(?).

Line 28 is the first part which looks correct to me.

Not really sure why this does not work, I attached it, hope its okay.

--Richard

From 4668aaf3272750ea38e87b227bff64470c4a1d06 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Richard=20R=C3=B6jfors?= <[email protected]>
Date: Fri, 27 Sep 2019 07:22:47 +0200
Subject: [PATCH] ublox: netreg: Also subscribe to UREG URC's

It turns out that both L2xx and L4xx modems are a bit buggy when it comes to send CREG URC's when the tech changes. Try to overcome this by subscribing to both UREG and CREG, and poll the other when any of the URC's are received. Protect from doing simultaneous polls though.

---
 drivers/ubloxmodem/network-registration.c | 205 ++++++++++++++++------
 1 file changed, 154 insertions(+), 51 deletions(-)
drivers/ubloxmodem/network-registration.c +154 -51
index 69af4644..25f239a6 100644
@@ -28,6 +28,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <stdbool.h>
 
 #include <glib.h>
 
@@ -47,11 +48,13 @@
 static const char *none_prefix[] = { NULL };
 static const char *cmer_prefix[] = { "+CMER:", NULL };
 static const char *ureg_prefix[] = { "+UREG:", NULL };
+static const char *creg_prefix[] = { "+CREG:", NULL };
 
 struct netreg_data {
 	struct at_netreg_data at_data;
 
 	const struct ublox_model *model;
+	bool updating_status : 1;
 };
 
 struct tech_query {
@@ -213,13 +216,75 @@ static void ctze_notify(GAtResult *result, gpointer user_data)
 	ofono_netreg_time_notify(netreg, &nd->time);
 }
 
-static void ublox_query_tech_cb(gboolean ok, GAtResult *result,
+static int ublox_ureg_state_to_tech(int state)
+{
+	switch (state) {
+	case 1:
+		return ACCESS_TECHNOLOGY_GSM;
+	case 2:
+		return ACCESS_TECHNOLOGY_GSM_EGPRS;
+	case 3:
+		return ACCESS_TECHNOLOGY_UTRAN;
+	case 4:
+		return ACCESS_TECHNOLOGY_UTRAN_HSDPA;
+	case 5:
+		return ACCESS_TECHNOLOGY_UTRAN_HSUPA;
+	case 6:
+		return ACCESS_TECHNOLOGY_UTRAN_HSDPA_HSUPA;
+	case 7:
+		return ACCESS_TECHNOLOGY_EUTRAN;
+	case 8:
+		return ACCESS_TECHNOLOGY_GSM;
+	case 9:
+		return ACCESS_TECHNOLOGY_GSM_EGPRS;
+	default:
+		/* Not registered for PS (0) or something unknown (>9)... */
+		return -1;
+	}
+}
+
+static gboolean is_registered(int status)
+{
+	return status == NETWORK_REGISTRATION_STATUS_REGISTERED ||
+		status == NETWORK_REGISTRATION_STATUS_ROAMING;
+}
+
+static void ublox_creg_cb(gboolean ok, GAtResult *result,
+				gpointer user_data)
+{
+	struct tech_query *tq = user_data;
+	struct netreg_data *nd = ofono_netreg_get_data(tq->netreg);
+	int status;
+	int lac;
+	int ci;
+	int tech;
+
+	nd->updating_status = false;
+
+	if (!ok)
+		return;
+
+	if (at_util_parse_reg(result, "+CREG:", NULL, &status,
+				&lac, &ci, &tech, OFONO_VENDOR_GENERIC) == FALSE)
+		return;
+
+	/* The query provided a tech, use that */
+	if (is_registered(status) && tq->tech != -1)
+		tech = tq->tech;
+
+	ofono_netreg_status_notify(tq->netreg, status, lac, ci, tech);
+}
+
+static void ublox_ureg_cb(gboolean ok, GAtResult *result,
 							gpointer user_data)
 {
 	struct tech_query *tq = user_data;
+	struct netreg_data *nd = ofono_netreg_get_data(tq->netreg);
 	GAtResultIter iter;
 	gint enabled, state;
-	int tech = -1;
+	int tech = tq->tech;
+
+	nd->updating_status = false;
 
 	if (!ok)
 		goto error;
@@ -235,60 +300,67 @@ static void ublox_query_tech_cb(gboolean ok, GAtResult *result,
 	if (!g_at_result_iter_next_number(&iter, &state))
 		return;
 
-	switch (state) {
-	case 0:
-		/* Not registered for PS, then we have to trust CREG... */
+	tech = ublox_ureg_state_to_tech(state);
+	if (tech < 0)
+		/* No valid UREG status, we have to trust CREG... */
 		tech = tq->tech;
-		break;
-	case 1:
-		tech = ACCESS_TECHNOLOGY_GSM;
-		break;
-	case 2:
-		tech = ACCESS_TECHNOLOGY_GSM_EGPRS;
-		break;
-	case 3:
-		tech = ACCESS_TECHNOLOGY_UTRAN;
-		break;
-	case 4:
-		tech = ACCESS_TECHNOLOGY_UTRAN_HSDPA;
-		break;
-	case 5:
-		tech = ACCESS_TECHNOLOGY_UTRAN_HSUPA;
-		break;
-	case 6:
-		tech = ACCESS_TECHNOLOGY_UTRAN_HSDPA_HSUPA;
-		break;
-	case 7:
-		tech = ACCESS_TECHNOLOGY_EUTRAN;
-		break;
-	case 8:
-		tech = ACCESS_TECHNOLOGY_GSM;
-		break;
-	case 9:
-		tech = ACCESS_TECHNOLOGY_GSM_EGPRS;
-		break;
-	default:
-		/* Not registered for PS or something unknown, trust CREG... */
-		tech = tq->tech;
-	}
 
 error:
 	ofono_netreg_status_notify(tq->netreg,
 			tq->status, tq->lac, tq->ci, tech);
 }
 
+static void ureg_notify(GAtResult *result, gpointer user_data)
+{
+	struct ofono_netreg *netreg = user_data;
+	struct netreg_data *nd = ofono_netreg_get_data(netreg);
+	struct tech_query *tq;
+	GAtResultIter iter;
+	int state;
+
+	if (nd->updating_status)
+		return;
+
+	g_at_result_iter_init(&iter, result);
+
+	if (!g_at_result_iter_next(&iter, "+UREG:"))
+		return;
+
+	if (!g_at_result_iter_next_number(&iter, &state))
+		return;
+
+	tq = g_new0(struct tech_query, 1);
+
+	tq->tech = ublox_ureg_state_to_tech(state);
+	tq->netreg = netreg;
+
+	if (g_at_chat_send(nd->at_data.chat, "AT+CREG?", creg_prefix,
+			ublox_creg_cb, tq, g_free) > 0) {
+		nd->updating_status = true;
+		return;
+	}
+
+	g_free(tq);
+}
+
 static void creg_notify(GAtResult *result, gpointer user_data)
 {
 	struct ofono_netreg *netreg = user_data;
-	int status, lac, ci, tech;
 	struct netreg_data *nd = ofono_netreg_get_data(netreg);
 	struct tech_query *tq;
+	int status;
+	int lac;
+	int ci;
+	int tech;
+
+	if (nd->updating_status)
+		return;
 
 	if (at_util_parse_reg_unsolicited(result, "+CREG:", &status,
 			&lac, &ci, &tech, OFONO_VENDOR_GENERIC) == FALSE)
 		return;
 
-	if (status != 1 && status != 5)
+	if (!is_registered(status))
 		goto notify;
 
 	if (ublox_is_toby_l4(nd->model) || ublox_is_toby_l2(nd->model)) {
@@ -301,13 +373,15 @@ static void creg_notify(GAtResult *result, gpointer user_data)
 		tq->netreg = netreg;
 
 		if (g_at_chat_send(nd->at_data.chat, "AT+UREG?", ureg_prefix,
-				ublox_query_tech_cb, tq, g_free) > 0)
+				ublox_ureg_cb, tq, g_free) > 0) {
+			nd->updating_status = true;
 			return;
+		}
 
 		g_free(tq);
 	}
 
-	if ((status == 1 || status == 5) && tech == -1)
+	if (tech == -1)
 		tech = nd->at_data.tech;
 
 notify:
@@ -322,24 +396,56 @@ static void at_cmer_not_supported(struct ofono_netreg *netreg)
 	ofono_netreg_remove(netreg);
 }
 
+static void ublox_finish_registration(struct ofono_netreg *netreg)
+{
+	struct netreg_data *nd = ofono_netreg_get_data(netreg);
+
+	if (ublox_is_toby_l4(nd->model) || ublox_is_toby_l2(nd->model))
+		g_at_chat_register(nd->at_data.chat, "+UREG:",
+					ureg_notify, FALSE, netreg, NULL);
+
+	g_at_chat_register(nd->at_data.chat, "+CIEV:",
+			ciev_notify, FALSE, netreg, NULL);
+
+	g_at_chat_register(nd->at_data.chat, "+CREG:",
+				creg_notify, FALSE, netreg, NULL);
+
+	ofono_netreg_register(netreg);
+}
+
+static void ublox_ureg_set_cb(gboolean ok,
+				GAtResult *result, gpointer user_data)
+{
+	struct ofono_netreg *netreg = user_data;
+
+	if (!ok) {
+		ofono_error("Unable to initialize Network Registration");
+		ofono_netreg_remove(netreg);
+		return;
+	}
+
+	ublox_finish_registration(netreg);
+}
+
 static void ublox_cmer_set_cb(gboolean ok,
 				GAtResult *result, gpointer user_data)
 {
 	struct ofono_netreg *netreg = user_data;
-	struct at_netreg_data *nd = ofono_netreg_get_data(netreg);
+	struct netreg_data *nd = ofono_netreg_get_data(netreg);
 
 	if (!ok) {
 		at_cmer_not_supported(netreg);
 		return;
 	}
 
-	g_at_chat_register(nd->chat, "+CIEV:",
-			ciev_notify, FALSE, netreg, NULL);
+	if (ublox_is_toby_l4(nd->model) || ublox_is_toby_l2(nd->model)) {
+		g_at_chat_send(nd->at_data.chat, "AT+UREG=1", none_prefix,
+			ublox_ureg_set_cb, netreg, NULL);
 
-	g_at_chat_register(nd->chat, "+CREG:",
-				creg_notify, FALSE, netreg, NULL);
+		return;
+	}
 
-	ofono_netreg_register(netreg);
+	ublox_finish_registration(netreg);
 }
 
 static void ublox_creg_set_cb(gboolean ok,
@@ -354,12 +460,9 @@ static void ublox_creg_set_cb(gboolean ok,
 		return;
 	}
 
-	if (ublox_is_toby_l4(nd->model)) {
+	if (ublox_is_toby_l4(nd->model))
 		/* FIXME */
 		ofono_error("TOBY L4 requires polling of ECSQ");
-		ofono_error("TOBY L4 wants UREG notifications for"
-				" tech updates");
-	}
 
 	/* Register for network time update reports */
 	if (ublox_is_toby_l2(nd->model)) {
Signature · 1 line
2.20.1
#7 lore raw

Hi Richard,

Quoted 5 lines

This is strange.
I use gmail smtps, but to check if they tamper with the emails, I cloned a fresh ofono and ran git am and  pasted my email, it did work(?).

Not sure. I've been using the same process for >10 years now and other patches obtained the same day worked fine. What was strange was that even 'patch -p1' complained of garbage data when I tried to apply the file manually.

Quoted 4 lines

Line 28 is the first part which looks correct to me.

Not really sure why this does not work, I attached it, hope its okay.

Anyway, the attachment worked, so I've applied this now.

Regards,
-Denis