@@ -404,36 +404,61 @@ void UBLOX_AT_CellularStack::clear_socket(CellularSocket *socket)
404404 }
405405}
406406
407+ #ifndef UBX_MDM_SARA_R41XM
407408const 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
438463nsapi_error_t UBLOX_AT_CellularStack::gethostbyname (const char *host, SocketAddress *address, nsapi_version_t version, const char *interface_name)
439464{
0 commit comments