From 5db623d98680099b25ddc37bf47ce08508071a7f Mon Sep 17 00:00:00 2001 From: Tomi Fontanilles Date: Fri, 8 Dec 2023 11:39:33 +0200 Subject: [PATCH] lib: pdn: add function to retrieve dynamic parameters To retrieve parameters returned by AT+CGCONTRDP=. Signed-off-by: Tomi Fontanilles --- include/modem/pdn.h | 14 ++++++++++++++ lib/pdn/pdn.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/include/modem/pdn.h b/include/modem/pdn.h index 44830c851ddb..18923d45c516 100644 --- a/include/modem/pdn.h +++ b/include/modem/pdn.h @@ -10,6 +10,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -167,6 +168,19 @@ int pdn_deactivate(uint8_t cid); */ int pdn_id_get(uint8_t cid); +/** + * @brief Retrieves dynamic parameters of a given PDP context. + * + * @param cid The PDP context ID. + * @param[out] dns4_pri The address of the primary IPv4 DNS server. + * @param[out] dns4_sec The address of the secondary IPv4 DNS server. + * @param[out] ipv4_mtu The IPv4 MTU. + * + * @return Zero on success or an error code on failure. + */ +int pdn_dynamic_params_get(uint8_t cid, struct in_addr *dns4_pri, + struct in_addr *dns4_sec, unsigned int *ipv4_mtu); + /** * @brief Retrieve the default Access Point Name (APN). * diff --git a/lib/pdn/pdn.c b/lib/pdn/pdn.c index f59d7a5e7215..fbfd4cc87a18 100644 --- a/lib/pdn/pdn.c +++ b/lib/pdn/pdn.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -598,6 +599,35 @@ int pdn_id_get(uint8_t cid) return strtoul(p + 1, NULL, 10); } +int pdn_dynamic_params_get(uint8_t cid, struct in_addr *dns4_pri, + struct in_addr *dns4_sec, unsigned int *ipv4_mtu) +{ + int ret; + char resp[256]; + const char *fmt; + char dns4_pri_str[INET_ADDRSTRLEN]; + char dns4_sec_str[INET_ADDRSTRLEN]; + + ret = nrf_modem_at_cmd(resp, sizeof(resp), "AT+CGCONTRDP=%u", cid); + if (ret) { + LOG_ERR("Failed to read dynamic params for CID %u, err %d", cid, ret); + return ret; + } + /* "+CGCONTRDP: 0,,"example.com","","","198.276.154.230","12.34.56.78",,,,,1464" */ + fmt = "+CGCONTRDP: %*u,,\"%*[^\"]\",\"\",\"\",\"%15[0-9.]\",\"%15[0-9.]\",,,,,%u"; + + /* If IPv4 is enabled, it will be the first response line. */ + if (sscanf(resp, fmt, &dns4_pri_str, &dns4_sec_str, ipv4_mtu) != 3) { + return -EBADMSG; + } + + if (zsock_inet_pton(AF_INET, dns4_pri_str, dns4_pri) != 1 + || zsock_inet_pton(AF_INET, dns4_sec_str, dns4_sec) != 1) { + return -EFAULT; + } + return 0; +} + int pdn_default_apn_get(char *buf, size_t len) { int err;