Skip to content

Cellular: Implementation of virtual get_ip_address funtion in ublox-api #12215

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 1 commit into from
Jan 9, 2020
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 @@ -404,36 +404,61 @@ void UBLOX_AT_CellularStack::clear_socket(CellularSocket *socket)
}
}

#ifndef UBX_MDM_SARA_R41XM
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this one is being skipped? Can you add this detail to the commit message (similar is stated in the description - this is implemented for 2 targets as I understood) ?

Copy link
Contributor Author

@mudassar-ublox mudassar-ublox Jan 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, these changes are for UBLOX_C030_U201 and UBLOX_C027. UBLOX_C030_R41XM is skipped as it does not support UPSND command for getting IP address so instead of using these functions it will use parent class functions, get_ip_address.

const char *UBLOX_AT_CellularStack::get_ip_address()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to call get_ip_address(SocketAddress *) function here instead of duplicating the code?
Would be much easier to remove later on.

Copy link

@AnttiKauppila AnttiKauppila Jan 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like
`SocketAddress addr;

get_ip_address(&addr);

return _ip; //Set already in above function`

{
SocketAddress address;

get_ip_address(&address);

return (address.get_ip_version()) ? (address.get_ip_address()) : NULL;
}

nsapi_error_t UBLOX_AT_CellularStack::get_ip_address(SocketAddress *address)
{
if (!address) {
return NSAPI_ERROR_PARAMETER;
}
_at.lock();
_at.cmd_start_stop("+UPSND", "=", "%d%d", PROFILE, 0);

bool ipv4 = false, ipv6 = false;

_at.cmd_start_stop("+UPSND", "=", "%d%d", PROFILE, 0);
_at.resp_start("+UPSND:");

if (_at.info_resp()) {
_at.skip_param();
_at.skip_param();
int len = _at.read_string(_ip, NSAPI_IPv4_SIZE);
if (len == -1) {
_ip[0] = '\0';
_at.unlock();
// no IPV4 address, return
return NULL;
}
_at.skip_param(2);

if (_at.read_string(_ip, PDP_IPV6_SIZE) != -1) {
convert_ipv6(_ip);
address->set_ip_address(_ip);

// in case stack type is not IPV4 only, try to look also for IPV6 address
if (_stack_type != IPV4_STACK) {
len = _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 ((_device.get_property(AT_CellularDevice::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 _ip;
return (ipv4 || ipv6) ? NSAPI_ERROR_OK : NSAPI_ERROR_NO_ADDRESS;
}
#endif

nsapi_error_t UBLOX_AT_CellularStack::gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version, const char *interface_name)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ class UBLOX_AT_CellularStack : public AT_CellularStack {
UBLOX_AT_CellularStack(ATHandler &atHandler, int cid, nsapi_ip_stack_t stack_type, AT_CellularDevice &device);
virtual ~UBLOX_AT_CellularStack();

#ifndef UBX_MDM_SARA_R41XM
virtual const char *get_ip_address();

virtual nsapi_error_t get_ip_address(SocketAddress *address);
#endif

virtual nsapi_error_t gethostbyname(const char *host,
SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC, const char *interface_name = NULL);

Expand All @@ -41,8 +45,6 @@ class UBLOX_AT_CellularStack : public AT_CellularStack {
virtual nsapi_error_t socket_accept(nsapi_socket_t server,
nsapi_socket_t *handle, SocketAddress *address = 0);

protected: // AT_CellularStack

/** The profile to use (on board the modem).
*/
#define PROFILE 0
Expand Down