Skip to content

Commit ea1b2b8

Browse files
author
Mirela Chirica
committed
Cellular: Stack type based on assigned IP addresses versions
1 parent 0c18a77 commit ea1b2b8

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

UNITTESTS/stubs/CellularUtil_stub.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ uint16_t char_str_to_hex(const char *str, uint16_t len, char *buf, bool omit_lea
5151
return CellularUtil_stub::uint16_value;
5252
}
5353

54-
void convert_ipv6(char *ip)
54+
nsapi_version_t convert_ipv6(char *ip)
5555
{
56-
56+
return NSAPI_UNSPEC;
5757
}
5858

5959
char *find_dot_number(char *str, int dot_number)

features/cellular/framework/AT/AT_CellularStack.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,43 @@ const char *AT_CellularStack::get_ip_address()
5959
{
6060
_at.lock();
6161

62+
bool ipv4 = false, ipv6 = false;
63+
6264
_at.cmd_start_stop("+CGPADDR", "=", "%d", _cid);
6365
_at.resp_start("+CGPADDR:");
6466

65-
int len = -1;
6667
if (_at.info_resp()) {
6768
_at.skip_param();
6869

69-
len = _at.read_string(_ip, PDP_IPV6_SIZE);
70-
71-
if (len != -1 && _stack_type != IPV4_STACK) {
72-
// in case stack type is not IPV4 only, try to look also for IPV6 address
73-
(void)_at.read_string(_ip, PDP_IPV6_SIZE);
70+
if (_at.read_string(_ip, PDP_IPV6_SIZE) != -1) {
71+
convert_ipv6(_ip);
72+
SocketAddress address;
73+
address.set_ip_address(_ip);
74+
75+
ipv4 = (address.get_ip_version() == NSAPI_IPv4);
76+
ipv6 = (address.get_ip_version() == NSAPI_IPv6);
77+
78+
// Try to look for second address ONLY if modem has support for dual stack(can handle both IPv4 and IPv6 simultaneously).
79+
// Otherwise assumption is that second address is not reliable, even if network provides one.
80+
if ((get_property(PROPERTY_IPV4V6_PDP_TYPE) && (_at.read_string(_ip, PDP_IPV6_SIZE) != -1))) {
81+
convert_ipv6(_ip);
82+
address.set_ip_address(_ip);
83+
ipv6 = (address.get_ip_version() == NSAPI_IPv6);
84+
}
7485
}
7586
}
7687
_at.resp_stop();
7788
_at.unlock();
7889

79-
// we have at least IPV4 address
80-
convert_ipv6(_ip);
90+
if (ipv4 && ipv6) {
91+
_stack_type = IPV4V6_STACK;
92+
} else if (ipv4) {
93+
_stack_type = IPV4_STACK;
94+
} else if (ipv6) {
95+
_stack_type = IPV6_STACK;
96+
}
8197

82-
return len != -1 ? _ip : NULL;
98+
return (ipv4 || ipv6) ? _ip : NULL;
8399
}
84100

85101
nsapi_error_t AT_CellularStack::socket_stack_init()

features/cellular/framework/AT/AT_CellularStack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class AT_CellularStack : public NetworkStack, public AT_CellularBase {
203203
// PDP context id
204204
int _cid;
205205

206-
// stack type from PDP context
206+
// stack type - initialised as PDP type and set accordingly after CGPADDR checked
207207
nsapi_ip_stack_t _stack_type;
208208

209209
private:

features/cellular/framework/common/CellularUtil.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
using namespace mbed;
2929
namespace mbed_cellular_util {
3030

31-
void convert_ipv6(char *ip)
31+
nsapi_version_t convert_ipv6(char *ip)
3232
{
3333
if (!ip) {
34-
return;
34+
return NSAPI_UNSPEC;
3535
}
3636

3737
int len = strlen(ip);
@@ -49,7 +49,11 @@ void convert_ipv6(char *ip)
4949

5050
// 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
5151
// we need to convert it to hexadecimal format separated with colons
52-
if (pos > 3) {
52+
if (pos == 3) {
53+
54+
return NSAPI_IPv4;
55+
56+
} else if (pos > 3) {
5357
pos = 0;
5458
int ip_pos = 0;
5559
char b;
@@ -74,7 +78,11 @@ void convert_ipv6(char *ip)
7478
ip[pos] = '\0';
7579
}
7680
}
81+
82+
return NSAPI_IPv6;
7783
}
84+
85+
return NSAPI_UNSPEC;
7886
}
7987

8088
// For example "32.1.13.184.0.0.205.48.0.0.0.0.0.0.0.0"

features/cellular/framework/common/CellularUtil.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ static const char hex_values[] = "0123456789ABCDEF";
4848
* where ax are in decimal format. In this case, function converts decimals to hex with separated with colons.
4949
*
5050
* @param ip IP address that can be IPv4 or IPv6 in different formats from AT command +CGPADDR. Converted result uses same buffer.
51+
* @return IP version of the address or NSAPI_UNSPEC if param ip empty or if IPv4 or IPv6 version could not be concluded.
5152
*/
52-
void convert_ipv6(char *ip);
53+
nsapi_version_t convert_ipv6(char *ip);
5354

5455
/** Separates IP addresses from the given 'orig' string. 'orig' may contain zero, one or two IP addresses in various formats.
5556
* See AT command +CGPIAF from 3GPP TS 27.007 for details. Does also needed conversions for IPv6 addresses.

0 commit comments

Comments
 (0)