Open Source Telephony
 help / color / mirror / Atom feed
From: Denis Kenzior <denkenz@gmail.com>
To: ofono@lists.linux.dev
Cc: Denis Kenzior <denkenz@gmail.com>
Subject: [PATCH v2 16/18] lte: Add provisioning support
Date: Fri,  2 Feb 2024 16:53:48 -0600	[thread overview]
Message-ID: <20240202225405.993792-16-denkenz@gmail.com> (raw)
In-Reply-To: <20240202225405.993792-1-denkenz@gmail.com>

If settings do not exist on disk, try to obtain them from the
provisioning database.  This requires for lte atom to wait until the
EFspn has been read from the SIM.  This is accomplished by creating a
sim spn watch and providing a callback.

When the callback is invoked, try to provision settings with the
MCC/MNC/SPN.  If provisioning succeeds, set the settings into the
currently active default_attach_info.  In all cases, forward the
settings to the driver and register the interface with D-Bus.
---
 src/lte.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 82 insertions(+), 4 deletions(-)

diff --git a/src/lte.c b/src/lte.c
index 158c8e9c2b17..706d596b59b2 100644
--- a/src/lte.c
+++ b/src/lte.c
@@ -36,6 +36,7 @@
 
 #include "ofono.h"
 
+#include "provisiondb.h"
 #include "common.h"
 #include "storage.h"
 
@@ -55,8 +56,54 @@ struct ofono_lte {
 	DBusMessage *pending;
 	struct ofono_lte_default_attach_info pending_info;
 	struct ofono_lte_default_attach_info info;
+	unsigned int spn_watch;
 };
 
+static bool provision_default_attach_info(struct ofono_lte *lte,
+						const char *mcc, const char *mnc,
+						const char *spn)
+{
+	struct provision_db_entry *settings;
+	_auto_(l_free) const struct provision_db_entry *ap = NULL;
+	size_t count;
+	size_t i;
+
+	DBG("Provisioning default bearer info with mcc:'%s', mnc:'%s', spn:'%s'",
+			mcc, mnc, spn);
+
+	if (!__ofono_provision_get_settings(mcc, mnc, spn, &settings, &count))
+		return false;
+
+	DBG("Obtained %zu candidates", count);
+
+	for (i = 0; i < count; i++) {
+		if (settings[i].type & OFONO_GPRS_CONTEXT_TYPE_IA) {
+			ap = &settings[i];
+			break;
+		}
+	}
+
+	if (!is_valid_apn(ap->apn))
+		return false;
+
+	if (ap->username && strlen(ap->username) >
+			OFONO_GPRS_MAX_USERNAME_LENGTH)
+		return false;
+
+	if (ap->password && strlen(ap->password) >
+			OFONO_GPRS_MAX_PASSWORD_LENGTH)
+		return false;
+
+	l_strlcpy(lte->info.apn, ap->apn, sizeof(lte->info.apn));
+	l_strlcpy(lte->info.username, ap->username, sizeof(lte->info.username));
+	l_strlcpy(lte->info.password, ap->password, sizeof(lte->info.password));
+	lte->info.proto = ap->proto;
+	lte->info.auth_method = ap->auth_method;
+
+	DBG("Provisioned successfully");
+	return true;
+}
+
 static int lte_load_settings(struct ofono_lte *lte)
 {
 	struct ofono_modem *modem = __ofono_atom_get_modem(lte->atom);
@@ -360,6 +407,11 @@ static void lte_atom_unregister(struct ofono_atom *atom)
 	DBusConnection *conn = ofono_dbus_get_connection();
 	struct ofono_modem *modem = __ofono_atom_get_modem(atom);
 	const char *path = __ofono_atom_get_path(atom);
+	struct ofono_lte *lte = __ofono_atom_get_data(atom);
+	struct ofono_sim *sim = __ofono_atom_find(OFONO_ATOM_TYPE_SIM, modem);
+
+	if (lte->spn_watch)
+		ofono_sim_remove_spn_watch(sim, &lte->spn_watch);
 
 	ofono_modem_remove_interface(modem, OFONO_LTE_INTERFACE);
 	g_dbus_unregister_interface(conn, path, OFONO_LTE_INTERFACE);
@@ -393,11 +445,38 @@ static void lte_init_default_attach_info_cb(const struct ofono_error *error,
 	ofono_lte_finish_register(lte);
 }
 
+static void spn_read_cb(const char *spn, const char *dc, void *data)
+{
+	struct ofono_lte *lte = data;
+	struct ofono_modem *modem = __ofono_atom_get_modem(lte->atom);
+	struct ofono_sim *sim = __ofono_atom_find(OFONO_ATOM_TYPE_SIM, modem);
+
+	ofono_sim_remove_spn_watch(sim, &lte->spn_watch);
+
+	provision_default_attach_info(lte, ofono_sim_get_mcc(sim),
+					ofono_sim_get_mnc(sim), spn);
+
+	if (lte->driver->set_default_attach_info) {
+		lte->driver->set_default_attach_info(lte, &lte->info,
+					lte_init_default_attach_info_cb, lte);
+		return;
+	}
+
+	ofono_lte_finish_register(lte);
+}
+
 void ofono_lte_register(struct ofono_lte *lte)
 {
-	/* No settings, go straight to registering the interface on D-Bus */
-	if (lte_load_settings(lte) < 0)
-		goto finish_register;
+	/* Wait for SPN to be read in order to try provisioning */
+	if (lte_load_settings(lte) < 0) {
+		struct ofono_modem *modem = __ofono_atom_get_modem(lte->atom);
+		struct ofono_sim *sim = __ofono_atom_find(OFONO_ATOM_TYPE_SIM,
+								modem);
+
+		ofono_sim_add_spn_watch(sim, &lte->spn_watch,
+						spn_read_cb, lte, NULL);
+		return;
+	}
 
 	if (lte->driver->set_default_attach_info) {
 		lte->driver->set_default_attach_info(lte, &lte->info,
@@ -405,7 +484,6 @@ void ofono_lte_register(struct ofono_lte *lte)
 		return;
 	}
 
-finish_register:
 	ofono_lte_finish_register(lte);
 }
 
-- 
2.43.0


  parent reply	other threads:[~2024-02-02 22:55 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-02 22:53 [PATCH v2 01/18] umlrunner: Also mount /var/lib as tmpfs Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 02/18] build: Only enable backtrace(3) in maintainer mode Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 03/18] build: Bring in more ell classes Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 04/18] build: Enable _auto_ syntax Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 05/18] provisiondb: Remove some duplicate MCCMNC entries Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 06/18] storage: Introduce storage_get_file_path() Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 07/18] storage: Convert g_strdup_* use to l_strdup_* Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 08/18] common: Drop GLib use from gprs_auth_proto_to_string Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 09/18] common: Drop GLib use from gprs_proto_to_string Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 10/18] storage: Remove mode parameter Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 11/18] storage: Use l_malloc Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 12/18] storage: Remove mode argument Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 13/18] storage: Use void * instead of unsigned char * Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 14/18] storage: use l_file_set_contents Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 15/18] lte: Refactor lte settings management Denis Kenzior
2024-02-02 22:53 ` Denis Kenzior [this message]
2024-02-02 22:53 ` [PATCH v2 17/18] phonesim: Add lte atom Denis Kenzior
2024-02-02 22:53 ` [PATCH v2 18/18] lte: Write provisioned info to disk Denis Kenzior
2024-02-02 23:30 ` [PATCH v2 01/18] umlrunner: Also mount /var/lib as tmpfs patchwork-bot+ofono

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240202225405.993792-16-denkenz@gmail.com \
    --to=denkenz@gmail.com \
    --cc=ofono@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox