Skip to content
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

Cellular.resolve() #1227

Merged
merged 1 commit into from Jan 13, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/reference/firmware.md
Expand Up @@ -2166,6 +2166,36 @@ else {
}
```

### resolve()
_Since 0.6.1_

`Cellular.resolve()` finds the IP address for a domain name.

```cpp
// SYNTAX
IPAddress ip = Cellular.resolve(name);
```

Parameters:

- `name`: the domain name to resolve (string)

It returns the IP address if the domain name is found, otherwise a blank IP address.

```cpp
// EXAMPLE USAGE

IPAddress ip;
void setup() {
ip = Cellular.resolve("www.google.com");
if(ip) {
// IP address was resolved
} else {
// name resolution failed
}
}
```

### localIP()
_Since 0.5.0_

Expand Down
6 changes: 6 additions & 0 deletions user/tests/wiring/api/cellular.cpp
Expand Up @@ -132,4 +132,10 @@ test(api_cellular_band_select) {
(void)result; // avoid unused warning
}

test(api_cellular_resolve) {
IPAddress ip;
API_COMPILE(ip = Cellular.resolve("www.google.com"));
(void)ip;
}

#endif
5 changes: 5 additions & 0 deletions user/tests/wiring/no_fixture/cellular.cpp
Expand Up @@ -108,6 +108,11 @@ test(CELLULAR_04_local_ip_cellular_config)
checkIPAddress("local", Cellular.localIP());
}

test(CELLULAR_05_resolve) {
connect_to_cloud(6*60*1000);
checkIPAddress("www.google.com", Cellular.resolve("www.google.com"));
}

int how_many_band_options_are_available(void)
{
CellularBand band_avail;
Expand Down
8 changes: 8 additions & 0 deletions wiring/inc/spark_wiring_cellular.h
Expand Up @@ -27,6 +27,7 @@
#if Wiring_Cellular

#include "cellular_hal.h"
#include "inet_hal.h"
#include "spark_wiring_cellular_printable.h"

namespace spark {
Expand Down Expand Up @@ -124,6 +125,13 @@ class CellularClass : public NetworkClass
{
return cellular_command((_CALLBACKPTR_MDM)cb, (void*)param, timeout_ms, format, Fargs...);
}

IPAddress resolve(const char* name)
{
HAL_IPAddress ip;
return (inet_gethostbyname(name, strlen(name), &ip, *this, NULL)<0) ?
IPAddress(uint32_t(0)) : IPAddress(ip);
}
};


Expand Down