diff mbox series

[04/10] sim7100: implement set_online()

Message ID 20240430115234.180185-4-martin@geanix.com (mailing list archive)
State Accepted
Commit 960aefa86ca128605de6aef0ad34958222106ea6
Headers show
Series [01/10] sim7100: simplify serial device opening | expand

Commit Message

Martin Hundebøll April 30, 2024, 11:52 a.m. UTC
Enter pre-sim state using AT+CFUN=4, and move the AT+CFUN=1 call into
the added set_online() callback. The modem (at least the A7672E variant)
generously issues unsolicited events between the CFUN command and the OK
response, so match on the +CFUN: prefix only in set_online().
---
 plugins/sim7100.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/plugins/sim7100.c b/plugins/sim7100.c
index 1c80f4ba..f4979ffe 100644
--- a/plugins/sim7100.c
+++ b/plugins/sim7100.c
@@ -59,6 +59,8 @@ 
 #include <drivers/atmodem/vendor.h>
 #include <drivers/atmodem/atutil.h>
 
+static const char *cfun_prefix[] = { "+CFUN:", NULL };
+
 enum sim7x00_model {
 	SIMCOM_UNKNOWN = 0,
 	SIMCOM_A76XX,
@@ -150,7 +152,7 @@  static void cgmm_cb(gboolean ok, GAtResult *result, gpointer user_data)
 	}
 
 	/* power up modem */
-	g_at_chat_send(data->at, "AT+CFUN=1", NULL, cfun_set_on_cb, modem,
+	g_at_chat_send(data->at, "AT+CFUN=4", NULL, cfun_set_on_cb, modem,
 									NULL);
 }
 
@@ -265,11 +267,42 @@  static void sim7100_post_sim(struct ofono_modem *modem)
 		ofono_message_waiting_register(mw);
 }
 
+static void set_online_cb(gboolean ok, GAtResult *result, gpointer user_data)
+{
+	struct cb_data *cbd = user_data;
+	ofono_modem_online_cb_t cb = cbd->cb;
+	struct ofono_error error;
+
+	DBG("ok: %i", ok);
+
+	decode_at_error(&error, g_at_result_final_response(result));
+	cb(&error, cbd->data);
+}
+
+static void sim7100_set_online(struct ofono_modem *modem, ofono_bool_t online,
+				ofono_modem_online_cb_t cb, void *user_data)
+{
+	struct sim7100_data *data = ofono_modem_get_data(modem);
+	struct cb_data *cbd = cb_data_new(cb, user_data);
+	char const *command = online ? "AT+CFUN=1" : "AT+CFUN=4";
+
+	DBG("%s", online ? "online" : "offline");
+
+	if (g_at_chat_send(data->at, command, cfun_prefix, set_online_cb, cbd,
+				g_free) > 0)
+		return;
+
+	CALLBACK_WITH_FAILURE(cb, cbd->data);
+
+	g_free(cbd);
+}
+
 static struct ofono_modem_driver sim7100_driver = {
 	.probe		= sim7100_probe,
 	.remove		= sim7100_remove,
 	.enable		= sim7100_enable,
 	.disable	= sim7100_disable,
+	.set_online	= sim7100_set_online,
 	.pre_sim	= sim7100_pre_sim,
 	.post_sim	= sim7100_post_sim,
 };