Skip to content

Commit

Permalink
lib: pdn: add function to retrieve dynamic parameters
Browse files Browse the repository at this point in the history
To retrieve parameters returned by AT+CGCONTRDP=<cid>.

Signed-off-by: Tomi Fontanilles <[email protected]>
  • Loading branch information
tomi-font authored and rlubos committed Dec 12, 2023
1 parent e4ce36a commit 5db623d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions include/modem/pdn.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <zephyr/net/net_ip.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -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).
*
Expand Down
30 changes: 30 additions & 0 deletions lib/pdn/pdn.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <limits.h>
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <zephyr/net/socket.h>
#include <zephyr/sys/slist.h>
#include <zephyr/logging/log.h>
#include <nrf_modem_at.h>
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5db623d

Please sign in to comment.