Skip to content

Add DNS servers from cellular PDP to nsapi #11162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ set(unittest-test-sources
stubs/SerialBase_stub.cpp
stubs/CellularContext_stub.cpp
stubs/CellularUtil_stub.cpp
stubs/SocketAddress_stub.cpp
)
5 changes: 5 additions & 0 deletions UNITTESTS/stubs/AT_CellularContext_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,8 @@ void AT_CellularContext::do_connect_with_retry()
{

}

char *AT_CellularContext::get_interface_name(char *interface_name)
{
return NULL;
}
35 changes: 34 additions & 1 deletion features/cellular/framework/AT/AT_CellularContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,16 @@ const char *AT_CellularContext::get_ip_address()
#endif
}

char *AT_CellularContext::get_interface_name(char *interface_name)
{
if (_cid < 0) {
return NULL;
}
MBED_ASSERT(interface_name);
sprintf(interface_name, "ce%02d", _cid);
return interface_name;
}

void AT_CellularContext::attach(Callback<void(nsapi_event_t, intptr_t)> status_cb)
{
_status_cb = status_cb;
Expand Down Expand Up @@ -458,7 +468,30 @@ nsapi_error_t AT_CellularContext::do_activate_context()

nsapi_error_t AT_CellularContext::activate_ip_context()
{
return find_and_activate_context();
nsapi_error_t ret = find_and_activate_context();
#if !NSAPI_PPP_AVAILABLE
if (ret == NSAPI_ERROR_OK) {
pdpContextList_t params_list;
if (get_pdpcontext_params(params_list) == NSAPI_ERROR_OK) {
pdpcontext_params_t *pdp = params_list.get_head();
while (pdp) {
SocketAddress addr;
if (addr.set_ip_address(pdp->dns_secondary_addr)) {
tr_info("DNS secondary %s", pdp->dns_secondary_addr);
char ifn[5]; // "ce" + two digit _cid + zero
add_dns_server(addr, get_interface_name(ifn));
}
if (addr.set_ip_address(pdp->dns_primary_addr)) {
tr_info("DNS primary %s", pdp->dns_primary_addr);
char ifn[5]; // "ce" + two digit _cid + zero
add_dns_server(addr, get_interface_name(ifn));
}
pdp = pdp->next;
}
}
}
#endif
return ret;
}

nsapi_error_t AT_CellularContext::activate_non_ip_context()
Expand Down
1 change: 1 addition & 0 deletions features/cellular/framework/AT/AT_CellularContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class AT_CellularContext : public CellularContext, public AT_CellularBase {
virtual nsapi_error_t set_blocking(bool blocking);
virtual NetworkStack *get_stack();
virtual const char *get_ip_address();
virtual char *get_interface_name(char *interface_name);
virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
virtual nsapi_error_t connect();
virtual nsapi_error_t disconnect();
Expand Down
7 changes: 7 additions & 0 deletions features/netsocket/nsapi_dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ static bool dns_timer_running = false;
// DNS server configuration
extern "C" nsapi_error_t nsapi_dns_add_server(nsapi_addr_t addr, const char *interface_name)
{
// check if addr was already added
for (int i = 0; i < DNS_SERVERS_SIZE; i++) {
if (memcmp(&addr, &dns_servers[i], sizeof(nsapi_addr_t)) == 0) {
return NSAPI_ERROR_OK;
}
}

memmove(&dns_servers[1], &dns_servers[0],
(DNS_SERVERS_SIZE - 1)*sizeof(nsapi_addr_t));

Expand Down