Skip to contentSkip to conversation

gprs: Add list_active_contexts

PATCH Superseded [email protected] 2 participants 5 messages Aug 12, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 gprs: Add list_active_contexts

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

This is useful for instance during startup where early activated contexts can be listed at a suitable point in time.

---
 include/gprs.h | 2 ++
 1 file changed, 2 insertions(+)
include/gprs.h +2 -0
index 988d6102..c276517f 100644
@@ -45,6 +45,8 @@ struct ofono_gprs_driver {
 				ofono_gprs_cb_t cb, void *data);
 	void (*attached_status)(struct ofono_gprs *gprs,
 					ofono_gprs_status_cb_t cb, void *data);
+	void (*list_active_contexts)(struct ofono_gprs *gprs,
+					ofono_gprs_status_cb_t cb, void *data);
 };
 
 enum gprs_suspend_cause {
Signature · 1 line
2.20.1
#1 lore raw

PATCH 2/3 gprs: Ignore activated contexts during init, list them later

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

There is an issue if an context gets auto activated early, then provisioning might not have run yet for instance, so a "new" context is created, which might be duplicated by a provisioning context later.
So ignore the activated contexts until gprs is ready, then it calls the driver to list active contexts.

---
 src/gprs.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
src/gprs.c +20 -0
index 9cb69d14..07b475c0 100644
@@ -1959,6 +1959,11 @@ void ofono_gprs_cid_activated(struct ofono_gprs  *gprs, unsigned int cid,
 
 	DBG("");
 
+	if (!__ofono_atom_get_registered(gprs->atom)) {
+		DBG("cid %u activated before atom registered", cid);
+		return;
+	}
+
 	if (l_uintset_contains(gprs->used_cids, cid)) {
 		DBG("cid %u already activated", cid);
 		return;
@@ -3338,11 +3343,20 @@ remove:
 		storage_sync(imsi, SETTINGS_STORE, gprs->settings);
 }
 
+static void gprs_list_active_contexts_callback(const struct ofono_error *error,
+						int num, void *data)
+{
+	(void)data;
+
+	DBG("error = %d, num = %d", error->type, num);
+}
+
 static void ofono_gprs_finish_register(struct ofono_gprs *gprs)
 {
 	DBusConnection *conn = ofono_dbus_get_connection();
 	struct ofono_modem *modem = __ofono_atom_get_modem(gprs->atom);
 	const char *path = __ofono_atom_get_path(gprs->atom);
+	const struct ofono_gprs_driver *driver = gprs->driver;
 
 	if (gprs->contexts == NULL) /* Automatic provisioning failed */
 		add_context(gprs, NULL, OFONO_GPRS_CONTEXT_TYPE_INTERNET);
@@ -3366,6 +3380,12 @@ static void ofono_gprs_finish_register(struct ofono_gprs *gprs)
 					netreg_watch, gprs, NULL);
 
 	__ofono_atom_register(gprs->atom, gprs_unregister);
+
+	/* Find any context activated during init */
+	if (driver->list_active_contexts)
+		driver->list_active_contexts(gprs,
+					     gprs_list_active_contexts_callback,
+					     gprs);
 }
 
 static void spn_read_cb(const char *spn, const char *dc, void *data)
Signature · 1 line
2.20.1
#2 lore raw

PATCH 3/3 atmodem: Implement the list_activated_contexts callback

In reply to [email protected]

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

The callback calls cgact and cgdcont to get information regarding any activate context.

---
 drivers/atmodem/gprs.c | 149 ++++++++++++++++++++++++++++++++---------
 1 file changed, 116 insertions(+), 33 deletions(-)
drivers/atmodem/gprs.c +116 -33
index 10108281..ab8f20f1 100644
@@ -29,6 +29,7 @@
 #include <stdio.h>
 #include <errno.h>
 
+#include <ell/ell.h>
 #include <glib.h>
 
 #include <ofono/log.h>
@@ -41,6 +42,8 @@
 #include "atmodem.h"
 #include "vendor.h"
 
+#define MAX_CONTEXTS 255
+
 static const char *cgreg_prefix[] = { "+CGREG:", NULL };
 static const char *cgdcont_prefix[] = { "+CGDCONT:", NULL };
 static const char *cgact_prefix[] = { "+CGACT:", NULL };
@@ -54,6 +57,14 @@ struct gprs_data {
 	int attached;
 };
 
+struct list_contexts_data
+{
+	struct ofono_gprs *gprs;
+	void *cb;
+	void *data;
+	struct l_uintset *active_cids;
+};
+
 static void at_cgatt_cb(gboolean ok, GAtResult *result, gpointer user_data)
 {
 	struct cb_data *cbd = user_data;
@@ -146,14 +157,48 @@ static void at_gprs_registration_status(struct ofono_gprs *gprs,
 	CALLBACK_WITH_FAILURE(cb, -1, data);
 }
 
+static int at_cgdcont_parse(struct ofono_gprs *gprs, GAtResult *result,
+				struct l_uintset *cids)
+{
+	GAtResultIter iter;
+	int count = 0;
+
+	g_at_result_iter_init(&iter, result);
+
+	while (g_at_result_iter_next(&iter, "+CGDCONT:")) {
+		int read_cid;
+		const char *apn = NULL;
+
+		if (!g_at_result_iter_next_number(&iter, &read_cid))
+			break;
+
+		if (!l_uintset_contains(cids, read_cid))
+			continue;
+
+		/* ignore protocol */
+		g_at_result_iter_skip_next(&iter);
+
+		g_at_result_iter_next_string(&iter, &apn);
+
+		if (apn) {
+			count++;
+			ofono_gprs_cid_activated(gprs, read_cid, apn);
+		} else {
+			ofono_warn("cid %d: Activated but no apn present",
+					read_cid);
+		}
+	}
+
+	return count;
+}
+
 static void at_cgdcont_read_cb(gboolean ok, GAtResult *result,
 				gpointer user_data)
 {
 	struct ofono_gprs *gprs = user_data;
 	struct gprs_data *gd = ofono_gprs_get_data(gprs);
 	int activated_cid = gd->last_auto_context_id;
-	const char *apn = NULL;
-	GAtResultIter iter;
+	struct l_uintset *cids;
 
 	DBG("ok %d", ok);
 
@@ -162,47 +207,57 @@ static void at_cgdcont_read_cb(gboolean ok, GAtResult *result,
 		return;
 	}
 
-	if (gd->last_auto_context_id == -1) {
+	if (activated_cid == -1) {
 		DBG("Context got deactivated while calling CGDCONT");
 		return;
 	}
 
-	g_at_result_iter_init(&iter, result);
+	cids = l_uintset_new(activated_cid);
 
-	while (g_at_result_iter_next(&iter, "+CGDCONT:")) {
-		int read_cid;
+	l_uintset_put(cids, activated_cid);
 
-		if (!g_at_result_iter_next_number(&iter, &read_cid))
-			break;
+	at_cgdcont_parse(gprs, result, cids);
 
-		if (read_cid != activated_cid)
-			continue;
-
-		/* ignore protocol */
-		g_at_result_iter_skip_next(&iter);
+	l_uintset_free(cids);
+}
 
-		g_at_result_iter_next_string(&iter, &apn);
+static void at_cgdcont_act_read_cb(gboolean ok, GAtResult *result,
+					gpointer user_data)
+{
+	struct list_contexts_data *ld = user_data;
+	ofono_gprs_status_cb_t cb = ld->cb;
+	struct ofono_error error;
+	int count = 0;
 
-		break;
-	}
+	decode_at_error(&error, g_at_result_final_response(result));
 
-	if (apn)
-		ofono_gprs_cid_activated(gprs, activated_cid, apn);
+	if (!ok)
+		ofono_warn("Can't read CGDCONT context.");
 	else
-		ofono_warn("cid %u: Received activated but no apn present",
-				activated_cid);
+		count = at_cgdcont_parse(ld->gprs, result, ld->active_cids);
+
+	cb(&error, count, ld->data);
+
+	l_uintset_free(ld->active_cids);
 }
 
 static void at_cgact_cb(gboolean ok, GAtResult *result, gpointer user_data)
 {
-	struct ofono_gprs *gprs = user_data;
-	struct gprs_data *gd = ofono_gprs_get_data(gprs);
+	struct list_contexts_data *ld = user_data;
+	struct gprs_data *gd = ofono_gprs_get_data(ld->gprs);
+	ofono_gprs_status_cb_t cb = ld->cb;
+	struct ofono_error error;
 	GAtResultIter iter;
 
-	DBG("ok %d", ok);
+	decode_at_error(&error, g_at_result_final_response(result));
 
 	if (!ok) {
 		ofono_warn("Can't read CGACT contexts.");
+
+		cb(&error, 0, ld->data);
+
+		g_free(ld);
+
 		return;
 	}
 
@@ -222,17 +277,48 @@ static void at_cgact_cb(gboolean ok, GAtResult *result, gpointer user_data)
 			continue;
 
 		/* Flag this as auto context as it was obviously active */
-		if (gd->last_auto_context_id == 0)
+		if (gd->last_auto_context_id == -1)
 			gd->last_auto_context_id = read_cid;
 
-		if (read_cid != gd->last_auto_context_id)
-			continue;
+		if (!ld->active_cids)
+			ld->active_cids = l_uintset_new(MAX_CONTEXTS);
 
-		g_at_chat_send(gd->chat, "AT+CGDCONT?", cgdcont_prefix,
-				at_cgdcont_read_cb, gprs, NULL);
+		l_uintset_put(ld->active_cids, read_cid);
+	}
 
-		break;
+	if (ld->active_cids != NULL) {
+		if (g_at_chat_send(gd->chat, "AT+CGDCONT?", cgdcont_prefix,
+					at_cgdcont_act_read_cb, ld, g_free))
+			return;
+
+		CALLBACK_WITH_FAILURE(cb, 0, ld->data);
+	} else {
+		/* No active contexts found */
+		cb(&error, 0, ld->data);
 	}
+
+	l_uintset_free(ld->active_cids);
+	g_free(ld);
+}
+
+static void at_gprs_list_active_contexts(struct ofono_gprs *gprs,
+						ofono_gprs_status_cb_t cb,
+						void *data)
+{
+	struct gprs_data *gd = ofono_gprs_get_data(gprs);
+	struct list_contexts_data *ld = g_new0(struct list_contexts_data, 1);
+
+	ld->gprs = gprs;
+	ld->data = data;
+	ld->cb = cb;
+
+	if (g_at_chat_send(gd->chat, "AT+CGACT?", cgact_prefix,
+				at_cgact_cb, ld, NULL))
+		return;
+
+	g_free(ld);
+
+	CALLBACK_WITH_FAILURE(cb, 0, data);
 }
 
 static void cgreg_notify(GAtResult *result, gpointer user_data)
@@ -538,10 +624,6 @@ static void gprs_initialized(gboolean ok, GAtResult *result, gpointer user_data)
 		break;
 	}
 
-	/* Check if there is any already activated contexts at init */
-	g_at_chat_send(gd->chat, "AT+CGACT?", cgact_prefix,
-			at_cgact_cb, gprs, NULL);
-
 	ofono_gprs_register(gprs);
 }
 
@@ -705,6 +787,7 @@ static const struct ofono_gprs_driver driver = {
 	.remove			= at_gprs_remove,
 	.set_attached		= at_gprs_set_attached,
 	.attached_status	= at_gprs_registration_status,
+	.list_active_contexts	= at_gprs_list_active_contexts,
 };
 
 void at_gprs_init(void)
Signature · 1 line
2.20.1
#4 lore raw
In reply to [email protected]

Hi Richard,

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

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

This is useful for instance during startup where early activated contexts can be listed at a suitable point in time.

---
  include/gprs.h | 2 ++
  1 file changed, 2 insertions(+)
include/gprs.h +0 -0
index 988d6102..c276517f 100644

include/gprs.h struct ofono_gprs_driver { · line 49

Quoted 5 lines
include/gprs.h +2 -0
@@ -45,6 +45,8 @@ struct ofono_gprs_driver {
  				ofono_gprs_cb_t cb, void *data);
  	void (*attached_status)(struct ofono_gprs *gprs,
  					ofono_gprs_status_cb_t cb, void *data);
+	void (*list_active_contexts)(struct ofono_gprs *gprs,
+					ofono_gprs_status_cb_t cb, void *data);

Using ofono_gprs_status_cb_t is a bit confusing. Do you really need the number of contexts? The core can easily compute this anyway if it cares.

Otherwise just use ofono_gprs_cb_t.

include/gprs.h struct ofono_gprs_driver { · line 52

Quoted 4 lines
include/gprs.h +0 -0
  };
 
  enum gprs_suspend_cause {

Regards,
-Denis

#3 lore raw

Re: [PATCH 3/3] atmodem: Implement the list_activated_contexts callback

In reply to [email protected]

Hi Richard,

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

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

The callback calls cgact and cgdcont to get information regarding any activate context.

---
  drivers/atmodem/gprs.c | 149 ++++++++++++++++++++++++++++++++---------
  1 file changed, 116 insertions(+), 33 deletions(-)
drivers/atmodem/gprs.c +3 -0
index 10108281..ab8f20f1 100644
@@ -29,6 +29,7 @@
  #include <stdio.h>
  #include <errno.h>
 
+#include <ell/ell.h>
  #include <glib.h>
 
  #include <ofono/log.h>
@@ -41,6 +42,8 @@
  #include "atmodem.h"
  #include "vendor.h"
 
+#define MAX_CONTEXTS 255
+
  static const char *cgreg_prefix[] = { "+CGREG:", NULL };
  static const char *cgdcont_prefix[] = { "+CGDCONT:", NULL };
  static const char *cgact_prefix[] = { "+CGACT:", NULL };

drivers/atmodem/gprs.c struct gprs_data { · line 65

Quoted 9 lines
drivers/atmodem/gprs.c +6 -0
@@ -54,6 +57,14 @@ struct gprs_data {
  	int attached;
  };
 
+struct list_contexts_data
+{
+	struct ofono_gprs *gprs;
+	void *cb;
+	void *data;
+	struct l_uintset *active_cids;

You may want to use a reference count here, see below

Quoted 5 lines
drivers/atmodem/gprs.c +2 -0
+};
+
  static void at_cgatt_cb(gboolean ok, GAtResult *result, gpointer user_data)
  {
  	struct cb_data *cbd = user_data;

<snip>

Quoted 93 lines
drivers/atmodem/gprs.c +34 -25
@@ -162,47 +207,57 @@ static void at_cgdcont_read_cb(gboolean ok, GAtResult *result,
  		return;
  	}
 
-	if (gd->last_auto_context_id == -1) {
+	if (activated_cid == -1) {
  		DBG("Context got deactivated while calling CGDCONT");
  		return;
  	}
 
-	g_at_result_iter_init(&iter, result);
+	cids = l_uintset_new(activated_cid);
 
-	while (g_at_result_iter_next(&iter, "+CGDCONT:")) {
-		int read_cid;
+	l_uintset_put(cids, activated_cid);
 
-		if (!g_at_result_iter_next_number(&iter, &read_cid))
-			break;
+	at_cgdcont_parse(gprs, result, cids);
 
-		if (read_cid != activated_cid)
-			continue;
-
-		/* ignore protocol */
-		g_at_result_iter_skip_next(&iter);
+	l_uintset_free(cids);
+}
 
-		g_at_result_iter_next_string(&iter, &apn);
+static void at_cgdcont_act_read_cb(gboolean ok, GAtResult *result,
+					gpointer user_data)
+{
+	struct list_contexts_data *ld = user_data;
+	ofono_gprs_status_cb_t cb = ld->cb;
+	struct ofono_error error;
+	int count = 0;
 
-		break;
-	}
+	decode_at_error(&error, g_at_result_final_response(result));
 
-	if (apn)
-		ofono_gprs_cid_activated(gprs, activated_cid, apn);
+	if (!ok)
+		ofono_warn("Can't read CGDCONT context.");
  	else
-		ofono_warn("cid %u: Received activated but no apn present",
-				activated_cid);
+		count = at_cgdcont_parse(ld->gprs, result, ld->active_cids);
+
+	cb(&error, count, ld->data);
+
+	l_uintset_free(ld->active_cids);
  }
 
  static void at_cgact_cb(gboolean ok, GAtResult *result, gpointer user_data)
  {
-	struct ofono_gprs *gprs = user_data;
-	struct gprs_data *gd = ofono_gprs_get_data(gprs);
+	struct list_contexts_data *ld = user_data;
+	struct gprs_data *gd = ofono_gprs_get_data(ld->gprs);
+	ofono_gprs_status_cb_t cb = ld->cb;
+	struct ofono_error error;
  	GAtResultIter iter;
 
-	DBG("ok %d", ok);
+	decode_at_error(&error, g_at_result_final_response(result));
 
  	if (!ok) {
  		ofono_warn("Can't read CGACT contexts.");
+
+		cb(&error, 0, ld->data);
+
+		g_free(ld);
+
  		return;
  	}
 
@@ -222,17 +277,48 @@ static void at_cgact_cb(gboolean ok, GAtResult *result, gpointer user_data)
  			continue;
 
  		/* Flag this as auto context as it was obviously active */
-		if (gd->last_auto_context_id == 0)
+		if (gd->last_auto_context_id == -1)
  			gd->last_auto_context_id = read_cid;
 
-		if (read_cid != gd->last_auto_context_id)
-			continue;
+		if (!ld->active_cids)
+			ld->active_cids = l_uintset_new(MAX_CONTEXTS);
 
-		g_at_chat_send(gd->chat, "AT+CGDCONT?", cgdcont_prefix,

drivers/atmodem/gprs.c static void at_cgact_cb(gboolean ok, GAtResult *result, gpointer user_data) · line 293

Quoted 10 lines
drivers/atmodem/gprs.c +7 -2
-				at_cgdcont_read_cb, gprs, NULL);
+		l_uintset_put(ld->active_cids, read_cid);
+	}
 
-		break;
+	if (ld->active_cids != NULL) {
+		if (g_at_chat_send(gd->chat, "AT+CGDCONT?", cgdcont_prefix,
+					at_cgdcont_act_read_cb, ld, g_free))
+			return;
+

You still need to free ld here. Note that it is much easier to use reference counting when you're performing a transaction, and simply take a reference when you've successfully queued the next command in the transaction. See cb_data_ref for an example.

Quoted 13 lines
drivers/atmodem/gprs.c +12 -0
+		CALLBACK_WITH_FAILURE(cb, 0, ld->data);
+	} else {
+		/* No active contexts found */
+		cb(&error, 0, ld->data);
  	}
+
+	l_uintset_free(ld->active_cids);
+	g_free(ld);
+}
+
+static void at_gprs_list_active_contexts(struct ofono_gprs *gprs,
+						ofono_gprs_status_cb_t cb,
+						void *data)

drivers/atmodem/gprs.c static void at_cgact_cb(gboolean ok, GAtResult *result, gpointer user_data) · line 316

Quoted 10 lines
drivers/atmodem/gprs.c +10 -0
+{
+	struct gprs_data *gd = ofono_gprs_get_data(gprs);
+	struct list_contexts_data *ld = g_new0(struct list_contexts_data, 1);
+
+	ld->gprs = gprs;
+	ld->data = data;
+	ld->cb = cb;
+
+	if (g_at_chat_send(gd->chat, "AT+CGACT?", cgact_prefix,
+				at_cgact_cb, ld, NULL))

This would actually cause a memory leak if the modem was forcefully removed for example. Always provide a destroy callback to g_at_chat_send if you're sending allocated memory as user data.

drivers/atmodem/gprs.c static void at_cgact_cb(gboolean ok, GAtResult *result, gpointer user_data) · line 324

Quoted 8 lines
drivers/atmodem/gprs.c +5 -0
+		return;
+
+	g_free(ld);
+
+	CALLBACK_WITH_FAILURE(cb, 0, data);
  }
 
  static void cgreg_notify(GAtResult *result, gpointer user_data)

Regards,
-Denis