diff --git a/UNITTESTS/stubs/CellularUtil_stub.cpp b/UNITTESTS/stubs/CellularUtil_stub.cpp index 1ed70b7cfd..757fd2bbb8 100644 --- a/UNITTESTS/stubs/CellularUtil_stub.cpp +++ b/UNITTESTS/stubs/CellularUtil_stub.cpp @@ -51,9 +51,9 @@ uint16_t char_str_to_hex(const char *str, uint16_t len, char *buf, bool omit_lea return CellularUtil_stub::uint16_value; } -void convert_ipv6(char *ip) +nsapi_version_t convert_ipv6(char *ip) { - + return NSAPI_UNSPEC; } char *find_dot_number(char *str, int dot_number) diff --git a/features/cellular/framework/AT/AT_CellularStack.cpp b/features/cellular/framework/AT/AT_CellularStack.cpp index a8aba5b578..dc0f5732c2 100644 --- a/features/cellular/framework/AT/AT_CellularStack.cpp +++ b/features/cellular/framework/AT/AT_CellularStack.cpp @@ -59,27 +59,43 @@ const char *AT_CellularStack::get_ip_address() { _at.lock(); + bool ipv4 = false, ipv6 = false; + _at.cmd_start_stop("+CGPADDR", "=", "%d", _cid); _at.resp_start("+CGPADDR:"); - int len = -1; if (_at.info_resp()) { _at.skip_param(); - len = _at.read_string(_ip, PDP_IPV6_SIZE); + if (_at.read_string(_ip, PDP_IPV6_SIZE) != -1) { + convert_ipv6(_ip); + SocketAddress address; + address.set_ip_address(_ip); - if (len != -1 && _stack_type != IPV4_STACK) { - // in case stack type is not IPV4 only, try to look also for IPV6 address - (void)_at.read_string(_ip, PDP_IPV6_SIZE); + ipv4 = (address.get_ip_version() == NSAPI_IPv4); + ipv6 = (address.get_ip_version() == NSAPI_IPv6); + + // Try to look for second address ONLY if modem has support for dual stack(can handle both IPv4 and IPv6 simultaneously). + // Otherwise assumption is that second address is not reliable, even if network provides one. + if ((get_property(PROPERTY_IPV4V6_PDP_TYPE) && (_at.read_string(_ip, PDP_IPV6_SIZE) != -1))) { + convert_ipv6(_ip); + address.set_ip_address(_ip); + ipv6 = (address.get_ip_version() == NSAPI_IPv6); + } } } _at.resp_stop(); _at.unlock(); - // we have at least IPV4 address - convert_ipv6(_ip); + if (ipv4 && ipv6) { + _stack_type = IPV4V6_STACK; + } else if (ipv4) { + _stack_type = IPV4_STACK; + } else if (ipv6) { + _stack_type = IPV6_STACK; + } - return len != -1 ? _ip : NULL; + return (ipv4 || ipv6) ? _ip : NULL; } nsapi_error_t AT_CellularStack::socket_stack_init() diff --git a/features/cellular/framework/AT/AT_CellularStack.h b/features/cellular/framework/AT/AT_CellularStack.h index fd9adf28ac..a61515672f 100644 --- a/features/cellular/framework/AT/AT_CellularStack.h +++ b/features/cellular/framework/AT/AT_CellularStack.h @@ -203,7 +203,7 @@ protected: // PDP context id int _cid; - // stack type from PDP context + // stack type - initialised as PDP type and set accordingly after CGPADDR checked nsapi_ip_stack_t _stack_type; private: diff --git a/features/cellular/framework/common/CellularUtil.cpp b/features/cellular/framework/common/CellularUtil.cpp index 8c7bf7a9d3..33c5956899 100644 --- a/features/cellular/framework/common/CellularUtil.cpp +++ b/features/cellular/framework/common/CellularUtil.cpp @@ -28,10 +28,10 @@ using namespace mbed; namespace mbed_cellular_util { -void convert_ipv6(char *ip) +nsapi_version_t convert_ipv6(char *ip) { if (!ip) { - return; + return NSAPI_UNSPEC; } int len = strlen(ip); @@ -49,7 +49,11 @@ void convert_ipv6(char *ip) // more that 3 periods mean that it was ipv6 but in format of a1.a2.a3.a4.a5.a6.a7.a8.a9.a10.a11.a12.a13.a14.a15.a16 // we need to convert it to hexadecimal format separated with colons - if (pos > 3) { + if (pos == 3) { + + return NSAPI_IPv4; + + } else if (pos > 3) { pos = 0; int ip_pos = 0; char b; @@ -74,7 +78,11 @@ void convert_ipv6(char *ip) ip[pos] = '\0'; } } + + return NSAPI_IPv6; } + + return NSAPI_UNSPEC; } // For example "32.1.13.184.0.0.205.48.0.0.0.0.0.0.0.0" diff --git a/features/cellular/framework/common/CellularUtil.h b/features/cellular/framework/common/CellularUtil.h index cfd7b50e0d..cfe0c86cba 100644 --- a/features/cellular/framework/common/CellularUtil.h +++ b/features/cellular/framework/common/CellularUtil.h @@ -48,8 +48,9 @@ static const char hex_values[] = "0123456789ABCDEF"; * where ax are in decimal format. In this case, function converts decimals to hex with separated with colons. * * @param ip IP address that can be IPv4 or IPv6 in different formats from AT command +CGPADDR. Converted result uses same buffer. + * @return IP version of the address or NSAPI_UNSPEC if param ip empty or if IPv4 or IPv6 version could not be concluded. */ -void convert_ipv6(char *ip); +nsapi_version_t convert_ipv6(char *ip); /** Separates IP addresses from the given 'orig' string. 'orig' may contain zero, one or two IP addresses in various formats. * See AT command +CGPIAF from 3GPP TS 27.007 for details. Does also needed conversions for IPv6 addresses.