Cellular: Stack type based on assigned IP addresses versions

pull/11424/head
Mirela Chirica 2019-09-12 12:55:33 +03:00
parent 0c18a7721e
commit ea1b2b8045
5 changed files with 40 additions and 15 deletions

View File

@ -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)

View File

@ -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()

View File

@ -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:

View File

@ -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"

View File

@ -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.