Skip to content

Add NetworkInterface::get_default_instance() #6108

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

Closed
wants to merge 1 commit into from

Conversation

kjbracey
Copy link
Contributor

@kjbracey kjbracey commented Feb 15, 2018

Provide an initial framework to make it easier to find a default network
interface.

This is the germ of a concept. Follows the same pattern as just established for EMAC::get_default_instance() and OnboardNetworkStack::get_default_instance().

Envisaged use:

NetworkInterface &net = NetworkInterface::get_default_instance();
net.connect();

// for advanced users:
WiFiInterface *wifi = net.wifiInterface();
if (wifi) {
    wifi->do_something_wifiy();
}

Provide an initial framework to make it easier to find a default network
interface.
@kjbracey
Copy link
Contributor Author

I've put this on top of feature-emac, but it could be done without the feature-emac stuff to master as-is. Only difference would be the condition for selecting EthernetInterface - it would be if FEATURE_LWIP is enabled and LWIP_ETHERNET is 1.

Copy link
Contributor

@sg- sg- left a comment

Choose a reason for hiding this comment

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

what does MESH, LOWPAN, ETHERNET, ... 0x2345 mean?
Why does Wi-Fi not get a default instance like MESH and THREAD?

@0xc0170
Copy link
Contributor

0xc0170 commented Feb 19, 2018

@kjbracey-arm Please fix travis and jenkins CI

@kjbracey
Copy link
Contributor Author

kjbracey commented Feb 20, 2018

The 0x2345 is just magic to let you do a "string comparison" in the C preprocessor.

#define CONF  FOO
#define FOO 0x2345
#if CONF == FOO   // evaluates as 0x2345 == 0x2345: true

#define CONF BAR
#define FOO 0x2345
#if CONF == FOO  // evaluates as 0 == 0x2345: false

Aim is to make this namespace open-ended - new code/systems can invent just new configuration names. Easy connect uses the same mechanism but closed, with:

#define ETHERNET 1
#define MESH 2
#define WIFI 3
#if CONF == WIFI

Wi-fi doesn't get a default instance because we don't (yet) have WiFiInterface:;get_default_instance(). If we define that, we get it. It would make sense to do so.

(We also don't have NanostackRfPhy::get_default_instance() - needs to be done).

I'm liking this pattern, but my main concern is how to indicate at compile time rather than link time that each of those definitions exist, to help probing for test etc.

#define MBED_CONF_DEFAULT_CELLULAR_PASSWORD NULL
#endif
cellular.set_credentials(MBED_CONF_DEFAULT_CELLULAR_APN, MBED_CONF_DEFAULT_CELLULAR_USERNAME, MBED_CONF_DEFAULT_CELLULAR_PASSWORD);
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

one endif missing here (see Travis failure)

@lauri-piikivi
Copy link

I like the basic approach. Is the advanced usage (wifyish in the original comment) still in plans?

Should we have a getInterfaceType() method, which would return if the interface is actually Wifi or Mesh etc? Thinking about the device that have e.g. Wifi and cellular, the app might want to know and act on the type of interface (cast the interface to correct class and use the methods?).

@kjbracey
Copy link
Contributor Author

kjbracey commented Mar 1, 2018

The "wifyish" isn't that advanced, but yes.

The "xxxInterface" methods function as the "get type" - it's equivalent to C++ dynamic_cast, returning NULL if it's not that sort of interface, so that example is checking if it's wifi.

If we had run-time type information enabled in the compilers that could have been this:

WiFiInterface *wifi = dynamic_cast<WiFiInterface *>(&net);
if (wifi) {
    wifi->do_something_wifiy();
} else {
    // it's not a wifi interface
}

Little bit clunky. But one day in C++17(?) you could do:

if (WiFiInterface *wifi = dynamic_cast<WiFiInterface *>(&net)) {
     wifi->do_something();
} else if (CellularNetwork *cell = dynamic_cast<CellularNetwork *>(&net)) {
     cell->cellular_device()->do_something_new_api();
} else if (CellularBase *cellular = dynamic_cast<CellularBase *>(&net)) {
     cell->do_something_old_api();
} ...

@cmonr
Copy link
Contributor

cmonr commented Mar 15, 2018

@kjbracey-arm Is this still in progress?

@kjbracey
Copy link
Contributor Author

Yes, still work in progress.

@0xc0170
Copy link
Contributor

0xc0170 commented Apr 9, 2018

@kjbracey-arm Please reopen with an update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants