Skip to content

Commit ec3fc67

Browse files
authored
Merge pull request ARMmbed#12215 from u-blox/ubx_get_ip_addr
Cellular: Implementation of virtual get_ip_address funtion in ublox-api
2 parents 1fb9dd7 + 075a6fd commit ec3fc67

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.cpp

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -404,36 +404,61 @@ void UBLOX_AT_CellularStack::clear_socket(CellularSocket *socket)
404404
}
405405
}
406406

407+
#ifndef UBX_MDM_SARA_R41XM
407408
const char *UBLOX_AT_CellularStack::get_ip_address()
408409
{
410+
SocketAddress address;
411+
412+
get_ip_address(&address);
413+
414+
return (address.get_ip_version()) ? (address.get_ip_address()) : NULL;
415+
}
416+
417+
nsapi_error_t UBLOX_AT_CellularStack::get_ip_address(SocketAddress *address)
418+
{
419+
if (!address) {
420+
return NSAPI_ERROR_PARAMETER;
421+
}
409422
_at.lock();
410-
_at.cmd_start_stop("+UPSND", "=", "%d%d", PROFILE, 0);
411423

424+
bool ipv4 = false, ipv6 = false;
425+
426+
_at.cmd_start_stop("+UPSND", "=", "%d%d", PROFILE, 0);
412427
_at.resp_start("+UPSND:");
428+
413429
if (_at.info_resp()) {
414-
_at.skip_param();
415-
_at.skip_param();
416-
int len = _at.read_string(_ip, NSAPI_IPv4_SIZE);
417-
if (len == -1) {
418-
_ip[0] = '\0';
419-
_at.unlock();
420-
// no IPV4 address, return
421-
return NULL;
422-
}
430+
_at.skip_param(2);
431+
432+
if (_at.read_string(_ip, PDP_IPV6_SIZE) != -1) {
433+
convert_ipv6(_ip);
434+
address->set_ip_address(_ip);
423435

424-
// in case stack type is not IPV4 only, try to look also for IPV6 address
425-
if (_stack_type != IPV4_STACK) {
426-
len = _at.read_string(_ip, PDP_IPV6_SIZE);
436+
ipv4 = (address->get_ip_version() == NSAPI_IPv4);
437+
ipv6 = (address->get_ip_version() == NSAPI_IPv6);
438+
439+
// Try to look for second address ONLY if modem has support for dual stack(can handle both IPv4 and IPv6 simultaneously).
440+
// Otherwise assumption is that second address is not reliable, even if network provides one.
441+
if ((_device.get_property(AT_CellularDevice::PROPERTY_IPV4V6_PDP_TYPE) && (_at.read_string(_ip, PDP_IPV6_SIZE) != -1))) {
442+
convert_ipv6(_ip);
443+
address->set_ip_address(_ip);
444+
ipv6 = (address->get_ip_version() == NSAPI_IPv6);
445+
}
427446
}
428447
}
429448
_at.resp_stop();
430449
_at.unlock();
431450

432-
// we have at least IPV4 address
433-
convert_ipv6(_ip);
451+
if (ipv4 && ipv6) {
452+
_stack_type = IPV4V6_STACK;
453+
} else if (ipv4) {
454+
_stack_type = IPV4_STACK;
455+
} else if (ipv6) {
456+
_stack_type = IPV6_STACK;
457+
}
434458

435-
return _ip;
459+
return (ipv4 || ipv6) ? NSAPI_ERROR_OK : NSAPI_ERROR_NO_ADDRESS;
436460
}
461+
#endif
437462

438463
nsapi_error_t UBLOX_AT_CellularStack::gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version, const char *interface_name)
439464
{

features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ class UBLOX_AT_CellularStack : public AT_CellularStack {
3030
UBLOX_AT_CellularStack(ATHandler &atHandler, int cid, nsapi_ip_stack_t stack_type, AT_CellularDevice &device);
3131
virtual ~UBLOX_AT_CellularStack();
3232

33+
#ifndef UBX_MDM_SARA_R41XM
3334
virtual const char *get_ip_address();
3435

36+
virtual nsapi_error_t get_ip_address(SocketAddress *address);
37+
#endif
38+
3539
virtual nsapi_error_t gethostbyname(const char *host,
3640
SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC, const char *interface_name = NULL);
3741

@@ -41,8 +45,6 @@ class UBLOX_AT_CellularStack : public AT_CellularStack {
4145
virtual nsapi_error_t socket_accept(nsapi_socket_t server,
4246
nsapi_socket_t *handle, SocketAddress *address = 0);
4347

44-
protected: // AT_CellularStack
45-
4648
/** The profile to use (on board the modem).
4749
*/
4850
#define PROFILE 0

0 commit comments

Comments
 (0)