This is an automated email from the ASF dual-hosted git repository.

lupyuen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new af371bbaa5c arch/nrf91: add more ioctl calls for modem socket
af371bbaa5c is described below

commit af371bbaa5c3b670ae3fc211bfaab1c4b45b5b4a
Author: raiden00pl <[email protected]>
AuthorDate: Fri Jun 5 11:59:59 2026 +0200

    arch/nrf91: add more ioctl calls for modem socket
    
    add support for these ioctl:
    
    - LTE_CMDID_RADIOON
    - LTE_CMDID_RADIOOFF
    - LTE_CMDID_ACTPDN
    
    Signed-off-by: raiden00pl <[email protected]>
---
 arch/arm/src/nrf91/nrf91_modem_sock.c | 159 ++++++++++++++++++++++++++++++++++
 1 file changed, 159 insertions(+)

diff --git a/arch/arm/src/nrf91/nrf91_modem_sock.c 
b/arch/arm/src/nrf91/nrf91_modem_sock.c
index e6d260e6642..c85557e32d2 100644
--- a/arch/arm/src/nrf91/nrf91_modem_sock.c
+++ b/arch/arm/src/nrf91/nrf91_modem_sock.c
@@ -474,6 +474,138 @@ static int nrf91_modem_getver(lte_version_t *version)
   return ret;
 }
 
+/****************************************************************************
+ * Name: nrf91_modem_actpdn
+ *
+ * Description:
+ *   Activate the default packet data network: optionally (re)configure the
+ *   APN, attach to the network (AT+CFUN=1), wait for registration and read
+ *   the assigned IP address.
+ *
+ ****************************************************************************/
+
+static int nrf91_modem_actpdn(FAR lte_apn_setting_t *apn,
+                              FAR lte_pdn_t *pdn)
+{
+  FAR const char *pdptype = "IP";
+  char resp[64];
+  FAR char *p;
+  FAR char *q;
+  size_t n;
+  int stat = 0;
+  int tmp = 0;
+  int elapsed = 0;
+  int ret;
+
+  switch (apn->ip_type)
+    {
+      case LTE_IPTYPE_V6:
+        pdptype = "IPV6";
+        break;
+      case LTE_IPTYPE_V4V6:
+        pdptype = "IPV4V6";
+        break;
+      default:
+        pdptype = "IP";
+        break;
+    }
+
+  /* (Re)configure the default PDN context when an APN is provided.  Toggle
+   * the radio off first so the new context is used on (re)attach.
+   */
+
+  if (apn->apn != NULL && apn->apn[0] != '\0')
+    {
+      nrf_modem_at_printf("AT+CFUN=4");
+
+      ret = nrf_modem_at_printf("AT+CGDCONT=0,\"%s\",\"%s\"",
+                                pdptype, apn->apn);
+      if (ret < 0)
+        {
+          nerr("AT+CGDCONT failed %d\n", ret);
+          return ret;
+        }
+
+      if (apn->auth_type != LTE_APN_AUTHTYPE_NONE &&
+          apn->user_name != NULL && apn->password != NULL)
+        {
+          ret = nrf_modem_at_printf("AT+CGAUTH=0,%u,\"%s\",\"%s\"",
+                                    (unsigned)apn->auth_type,
+                                    apn->user_name, apn->password);
+          if (ret < 0)
+            {
+              nerr("AT+CGAUTH failed %d\n", ret);
+              return ret;
+            }
+        }
+    }
+
+  /* Attach to the network */
+
+  ret = nrf_modem_at_printf("AT+CFUN=1");
+  if (ret < 0)
+    {
+      nerr("AT+CFUN=1 failed %d\n", ret);
+      return ret;
+    }
+
+  /* Wait for network registration (1 = home, 5 = roaming) */
+
+  while (elapsed < 120)
+    {
+      ret = nrf_modem_at_scanf("AT+CEREG?", "+CEREG: %d,%d", &tmp, &stat);
+      if (ret >= 2 && (stat == 1 || stat == 5))
+        {
+          break;
+        }
+
+      nxsig_usleep(1000 * 1000);
+      elapsed++;
+    }
+
+  if (stat != 1 && stat != 5)
+    {
+      nerr("LTE registration timed out, CEREG stat=%d\n", stat);
+      return -ETIMEDOUT;
+    }
+
+  /* Fill the PDN info and read the assigned IP address from
+   * "+CGPADDR: 0,\"a.b.c.d\"".
+   */
+
+  memset(pdn, 0, sizeof(*pdn));
+  pdn->session_id = 0;
+  pdn->active     = LTE_PDN_ACTIVE;
+  pdn->apn_type   = apn->apn_type;
+  pdn->ipaddr_num = 0;
+
+  ret = nrf_modem_at_cmd(resp, sizeof(resp), "AT+CGPADDR=0");
+  if (ret == 0)
+    {
+      p = strchr(resp, '"');
+      if (p != NULL)
+        {
+          p++;
+          q = strchr(p, '"');
+          if (q != NULL)
+            {
+              n = (size_t)(q - p);
+              if (n >= LTE_IPADDR_MAX_LEN)
+                {
+                  n = LTE_IPADDR_MAX_LEN - 1;
+                }
+
+              pdn->address[0].ip_type = apn->ip_type;
+              memcpy(pdn->address[0].address, p, n);
+              pdn->address[0].address[n] = '\0';
+              pdn->ipaddr_num = 1;
+            }
+        }
+    }
+
+  return OK;
+}
+
 /****************************************************************************
  * Name: nrf91_ioctl_ltecmd
  ****************************************************************************/
@@ -500,6 +632,32 @@ static int nrf91_ioctl_ltecmd(int fd, int cmd, unsigned 
long arg)
           break;
         }
 
+      case LTE_CMDID_RADIOON:
+        {
+          /* The radio is enabled by AT+CFUN=1 at power-on / PDN
+           * activation, so there is nothing extra to do here.
+           */
+
+          ret = OK;
+          break;
+        }
+
+      case LTE_CMDID_RADIOOFF:
+        {
+          ret = nrf_modem_at_printf("AT+CFUN=4");
+          break;
+        }
+
+      case LTE_CMDID_ACTPDN:
+        {
+          lte_apn_setting_t **apn =
+            (lte_apn_setting_t **)(ltecmd->inparam);
+          lte_pdn_t **pdn =
+            (lte_pdn_t **)(ltecmd->outparam + 1);
+          ret = nrf91_modem_actpdn(*apn, *pdn);
+          break;
+        }
+
       case LTE_CMDID_GETVER:
         {
           lte_version_t **version =
@@ -558,6 +716,7 @@ static int nrf91_ioctl_ltecmd(int fd, int cmd, unsigned 
long arg)
           }
 
         (*quality)->valid = true;
+        break;
       }
 
       /* TODO: commands from include/nuttx/wireless/lte/lte.h */

Reply via email to