Skip to content

Commit 1676454

Browse files
Override virtual get_ip_address funtion in ublox-api for targets UBLOX_C030_U201 and UBLOX_C027
1 parent b77f6b4 commit 1676454

File tree

2 files changed

+78
-17
lines changed

2 files changed

+78
-17
lines changed

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

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -404,36 +404,95 @@ 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
{
409410
_at.lock();
410-
_at.cmd_start_stop("+UPSND", "=", "%d%d", PROFILE, 0);
411411

412+
bool ipv4 = false, ipv6 = false;
413+
414+
_at.cmd_start_stop("+UPSND", "=", "%d%d", PROFILE, 0);
412415
_at.resp_start("+UPSND:");
416+
413417
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;
418+
_at.skip_param(2);
419+
420+
if (_at.read_string(_ip, PDP_IPV6_SIZE) != -1) {
421+
convert_ipv6(_ip);
422+
SocketAddress address;
423+
address.set_ip_address(_ip);
424+
425+
ipv4 = (address.get_ip_version() == NSAPI_IPv4);
426+
ipv6 = (address.get_ip_version() == NSAPI_IPv6);
427+
428+
// Try to look for second address ONLY if modem has support for dual stack(can handle both IPv4 and IPv6 simultaneously).
429+
// Otherwise assumption is that second address is not reliable, even if network provides one.
430+
if ((_device.get_property(AT_CellularDevice::PROPERTY_IPV4V6_PDP_TYPE) && (_at.read_string(_ip, PDP_IPV6_SIZE) != -1))) {
431+
convert_ipv6(_ip);
432+
address.set_ip_address(_ip);
433+
ipv6 = (address.get_ip_version() == NSAPI_IPv6);
434+
}
422435
}
436+
}
437+
_at.resp_stop();
438+
_at.unlock();
439+
440+
if (ipv4 && ipv6) {
441+
_stack_type = IPV4V6_STACK;
442+
} else if (ipv4) {
443+
_stack_type = IPV4_STACK;
444+
} else if (ipv6) {
445+
_stack_type = IPV6_STACK;
446+
}
423447

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);
448+
return (ipv4 || ipv6) ? _ip : NULL;
449+
}
450+
451+
nsapi_error_t UBLOX_AT_CellularStack::get_ip_address(SocketAddress *address)
452+
{
453+
if (!address) {
454+
return NSAPI_ERROR_PARAMETER;
455+
}
456+
_at.lock();
457+
458+
bool ipv4 = false, ipv6 = false;
459+
460+
_at.cmd_start_stop("+UPSND", "=", "%d%d", PROFILE, 0);
461+
_at.resp_start("+UPSND:");
462+
463+
if (_at.info_resp()) {
464+
_at.skip_param(2);
465+
466+
if (_at.read_string(_ip, PDP_IPV6_SIZE) != -1) {
467+
convert_ipv6(_ip);
468+
address->set_ip_address(_ip);
469+
470+
ipv4 = (address->get_ip_version() == NSAPI_IPv4);
471+
ipv6 = (address->get_ip_version() == NSAPI_IPv6);
472+
473+
// Try to look for second address ONLY if modem has support for dual stack(can handle both IPv4 and IPv6 simultaneously).
474+
// Otherwise assumption is that second address is not reliable, even if network provides one.
475+
if ((_device.get_property(AT_CellularDevice::PROPERTY_IPV4V6_PDP_TYPE) && (_at.read_string(_ip, PDP_IPV6_SIZE) != -1))) {
476+
convert_ipv6(_ip);
477+
address->set_ip_address(_ip);
478+
ipv6 = (address->get_ip_version() == NSAPI_IPv6);
479+
}
427480
}
428481
}
429482
_at.resp_stop();
430483
_at.unlock();
431484

432-
// we have at least IPV4 address
433-
convert_ipv6(_ip);
485+
if (ipv4 && ipv6) {
486+
_stack_type = IPV4V6_STACK;
487+
} else if (ipv4) {
488+
_stack_type = IPV4_STACK;
489+
} else if (ipv6) {
490+
_stack_type = IPV6_STACK;
491+
}
434492

435-
return _ip;
493+
return (ipv4 || ipv6) ? NSAPI_ERROR_OK : NSAPI_ERROR_NO_ADDRESS;
436494
}
495+
#endif
437496

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

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)