Skip to content

Commit 5c09ff1

Browse files
authored
Merge pull request #11220 from jarvte/fix_cellular_dns_test
Fix cellular dns test with IAR compiled binary
2 parents 6812869 + 65bf17e commit 5c09ff1

File tree

13 files changed

+57
-31
lines changed

13 files changed

+57
-31
lines changed

TESTS/netsocket/dns/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ static void net_bringup()
160160
net = NetworkInterface::get_default_instance();
161161
nsapi_error_t err = net->connect();
162162
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
163-
printf("MBED: IP address is '%s'\n", net->get_ip_address());
163+
printf("MBED: IP address is '%s'\n", net->get_ip_address() ? net->get_ip_address() : "null");
164164
}
165165

166166
static void net_bringdown()

TESTS/netsocket/tcp/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static void _ifup()
7272
NetworkInterface *net = NetworkInterface::get_default_instance();
7373
nsapi_error_t err = net->connect();
7474
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
75-
printf("MBED: TCPClient IP address is '%s'\n", net->get_ip_address());
75+
printf("MBED: TCPClient IP address is '%s'\n", net->get_ip_address() ? net->get_ip_address() : "null");
7676
}
7777

7878
static void _ifdown()

TESTS/netsocket/tls/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static void _ifup()
9494
NetworkInterface *net = NetworkInterface::get_default_instance();
9595
nsapi_error_t err = net->connect();
9696
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
97-
printf("MBED: TLSClient IP address is '%s'\n", net->get_ip_address());
97+
printf("MBED: TLSClient IP address is '%s'\n", net->get_ip_address() ? net->get_ip_address() : "null");
9898
}
9999

100100
static void _ifdown()

TESTS/netsocket/udp/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static void _ifup()
5959
NetworkInterface *net = NetworkInterface::get_default_instance();
6060
nsapi_error_t err = net->connect();
6161
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
62-
printf("MBED: UDPClient IP address is '%s'\n", net->get_ip_address());
62+
printf("MBED: UDPClient IP address is '%s'\n", net->get_ip_address() ? net->get_ip_address() : "null");
6363
}
6464

6565
static void _ifdown()

UNITTESTS/features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ TEST_F(TestAT_CellularContext, connect_disconnect_sync)
537537
ATHandler_stub::read_string_index = 2;
538538
ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_OK);
539539

540-
ASSERT_EQ(network_cb_count, 5);
540+
ASSERT_EQ(network_cb_count, 4);
541541

542542
ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_OK);
543543
ATHandler_stub::resp_info_true_counter = 1;
@@ -704,7 +704,7 @@ TEST_F(TestAT_CellularContext, connect_disconnect_async)
704704
data.status_data = CellularNetwork::Attached;
705705
ctx1.cellular_callback((nsapi_event_t)CellularAttachNetwork, (intptr_t)&data);
706706

707-
ASSERT_EQ(network_cb_count, 5);
707+
ASSERT_EQ(network_cb_count, 4);
708708
ASSERT_EQ(ctx1.connect(), NSAPI_ERROR_IS_CONNECTED);
709709
EXPECT_TRUE(ctx1.is_connected() == true);
710710
ASSERT_EQ(ctx1.disconnect(), NSAPI_ERROR_NO_MEMORY);

UNITTESTS/features/cellular/framework/AT/at_cellularstack/at_cellularstacktest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ TEST_F(TestAT_CellularStack, test_AT_CellularStack_get_ip_address)
175175
ATHandler at(&fh1, que, 0, ",");
176176

177177
MyStack st(at, 0, IPV6_STACK);
178-
EXPECT_EQ(strlen(st.get_ip_address()), 0);
178+
EXPECT_TRUE(st.get_ip_address() == NULL);
179179

180180
char table[] = "1.2.3.4.5.65.7.8.9.10.11\0";
181181
ATHandler_stub::ssize_value = -1;

UNITTESTS/features/cellular/framework/AT/at_cellularstack/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ set(unittest-test-sources
2828
stubs/NetworkStack_stub.cpp
2929
stubs/SocketAddress_stub.cpp
3030
stubs/mbed_assert_stub.c
31+
stubs/ThisThread_stub.cpp
3132
)

UNITTESTS/stubs/AT_CellularContext_stub.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ using namespace mbed;
2121

2222
AT_CellularContext::AT_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req) :
2323
AT_CellularBase(at), _is_connected(false),
24-
_current_op(OP_INVALID), _fh(0), _cp_req(cp_req), _nonip_req(nonip_req), _cp_in_use(false)
24+
_current_op(OP_INVALID), _fh(0), _cp_req(cp_req)
2525
{
2626
_stack = NULL;
2727
_pdp_type = DEFAULT_PDP_TYPE;
@@ -43,6 +43,8 @@ AT_CellularContext::AT_CellularContext(ATHandler &at, CellularDevice *device, co
4343
_is_blocking = true;
4444
_device = device;
4545
_nw = NULL;
46+
_nonip_req = nonip_req;
47+
_cp_in_use = false;
4648
}
4749

4850
AT_CellularContext::~AT_CellularContext()

features/cellular/framework/API/CellularContext.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ class CellularContext : public CellularInterface {
337337
*/
338338
virtual void do_connect();
339339

340+
/** After we have connected successfully we must check that we have a valid IP address.
341+
* Some modems/networks don't give IP address right after connect so we must poll it for a while.
342+
*/
343+
void validate_ip_address();
344+
340345
// member variables needed in target override methods
341346
NetworkStack *_stack; // must be pointer because of PPP
342347
pdp_type_t _pdp_type;
@@ -361,6 +366,10 @@ class CellularContext : public CellularInterface {
361366
CellularDevice *_device;
362367
CellularNetwork *_nw;
363368
bool _is_blocking;
369+
// flag indicating if Non-IP context was requested to be setup
370+
bool _nonip_req;
371+
// tells if CCIOTOPTI received green from network for CP optimization use
372+
bool _cp_in_use;
364373
};
365374

366375
/**

features/cellular/framework/AT/AT_CellularContext.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ using namespace mbed;
4747
using namespace rtos;
4848

4949
AT_CellularContext::AT_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req) :
50-
AT_CellularBase(at), _is_connected(false), _current_op(OP_INVALID), _fh(0), _cp_req(cp_req),
51-
_nonip_req(nonip_req), _cp_in_use(false)
50+
AT_CellularBase(at), _is_connected(false), _current_op(OP_INVALID), _fh(0), _cp_req(cp_req)
5251
{
5352
tr_info("New CellularContext %s (%p)", apn ? apn : "", this);
53+
_nonip_req = nonip_req;
5454
_apn = apn;
5555
_device = device;
5656
}
@@ -570,7 +570,6 @@ void AT_CellularContext::do_connect()
570570
}
571571
#else
572572
_is_connected = true;
573-
call_network_cb(NSAPI_STATUS_GLOBAL_UP);
574573
#endif
575574
}
576575

features/cellular/framework/AT/AT_CellularContext.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ class AT_CellularContext : public CellularContext, public AT_CellularBase {
130130
char _found_apn[MAX_APN_LENGTH];
131131
// flag indicating if CP was requested to be setup
132132
bool _cp_req;
133-
// flag indicating if Non-IP context was requested to be setup
134-
bool _nonip_req;
135-
// tells if CCIOTOPTI received green from network for CP optimization use
136-
bool _cp_in_use;
137133
};
138134

139135
} // namespace mbed

features/cellular/framework/AT/AT_CellularStack.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "AT_CellularStack.h"
1919
#include "CellularUtil.h"
2020
#include "CellularLog.h"
21+
#include "ThisThread.h"
2122

2223
using namespace mbed_cellular_util;
2324
using namespace mbed;
@@ -54,41 +55,31 @@ int AT_CellularStack::find_socket_index(nsapi_socket_t handle)
5455

5556
/** NetworkStack
5657
*/
57-
5858
const char *AT_CellularStack::get_ip_address()
5959
{
6060
_at.lock();
6161

6262
_at.cmd_start_stop("+CGPADDR", "=", "%d", _cid);
63-
6463
_at.resp_start("+CGPADDR:");
6564

65+
int len = -1;
6666
if (_at.info_resp()) {
67-
6867
_at.skip_param();
6968

70-
int len = _at.read_string(_ip, NSAPI_IPv4_SIZE);
71-
if (len == -1) {
72-
_ip[0] = '\0';
73-
_at.resp_stop();
74-
_at.unlock();
75-
// no IPV4 address, return
76-
return NULL;
77-
}
69+
len = _at.read_string(_ip, PDP_IPV6_SIZE);
7870

79-
// in case stack type is not IPV4 only, try to look also for IPV6 address
80-
if (_stack_type != IPV4_STACK) {
71+
if (len != -1 && _stack_type != IPV4_STACK) {
72+
// in case stack type is not IPV4 only, try to look also for IPV6 address
8173
(void)_at.read_string(_ip, PDP_IPV6_SIZE);
8274
}
8375
}
84-
8576
_at.resp_stop();
8677
_at.unlock();
8778

8879
// we have at least IPV4 address
8980
convert_ipv6(_ip);
9081

91-
return _ip;
82+
return len != -1 ? _ip : NULL;
9283
}
9384

9485
nsapi_error_t AT_CellularStack::socket_stack_init()

features/cellular/framework/device/CellularContext.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ CellularContext::CellularContext() : _next(0), _stack(0), _pdp_type(DEFAULT_PDP_
6262
_authentication_type(CellularContext::CHAP), _connect_status(NSAPI_STATUS_DISCONNECTED), _status_cb(0),
6363
_cid(-1), _new_context_set(false), _is_context_active(false), _is_context_activated(false),
6464
_apn(0), _uname(0), _pwd(0), _dcd_pin(NC), _active_high(false), _cp_netif(0), _retry_array_length(0),
65-
_retry_count(0), _device(0), _nw(0), _is_blocking(true)
65+
_retry_count(0), _device(0), _nw(0), _is_blocking(true), _nonip_req(false), _cp_in_use(false)
6666
{
6767
memset(_retry_timeout_array, 0, CELLULAR_RETRY_ARRAY_SIZE);
6868
}
@@ -87,6 +87,28 @@ void CellularContext::set_authentication_type(AuthenticationType type)
8787
_authentication_type = type;
8888
}
8989

90+
void CellularContext::validate_ip_address()
91+
{
92+
const int IP_MAX_TRIES = 10; // maximum of 2 seconds as we wait 200ms between tries
93+
const int IP_WAIT_INTERVAL = 200; // 200 ms between retries
94+
const char *ip = NULL;
95+
int i = 0;
96+
97+
while (1) {
98+
ip = get_ip_address();
99+
if (ip || i >= IP_MAX_TRIES) {
100+
if (ip == NULL) {
101+
tr_warning("Connected but no local ip address");
102+
} else {
103+
tr_info("Cellular local IP: %s", ip);
104+
}
105+
break;
106+
}
107+
rtos::ThisThread::sleep_for(IP_WAIT_INTERVAL);
108+
i++;
109+
}
110+
}
111+
90112
void CellularContext::do_connect_with_retry()
91113
{
92114
if (_cb_data.final_try) {
@@ -97,6 +119,12 @@ void CellularContext::do_connect_with_retry()
97119
}
98120
do_connect();
99121
if (_cb_data.error == NSAPI_ERROR_OK) {
122+
// Some modems don't get the ip address right after connect so we must
123+
// validate it but even if we don't get ip we still send NSAPI_STATUS_GLOBAL_UP
124+
if (!_nonip_req && !_cp_in_use) { // don't validate if non-ip case
125+
validate_ip_address();
126+
}
127+
call_network_cb(NSAPI_STATUS_GLOBAL_UP);
100128
return;
101129
}
102130

0 commit comments

Comments
 (0)