I think it was an attempt to make the library behave the same as a remembered past version, for compatibility with existing past code.
A post with some history:
https://forum.pjrc.com/index.php?threads/compiler-error.73841/post-333932
An additional note:
The original Arduino Ethernet library was essentially a model of how to do things with the WIZnet chips over SPI. Its design reflected that, and I don't think it's an appropriate model for how to do things with other sorts of chips and peripherals. However, there's an existing code base and API, so there's still that to consider. I've made efforts to keep
QNEthernet compatible with this API, while slowly steering away from it. My strategy is to have lots of additional functionality, such as callbacks (eg. for link state) and lots of other capabilities, while still being backwards compatible. I'm thinking about eventually removing the parts of the Arduino-style API I'm not fond of from the main library, and separating that into a separate layer, so there's both a better API
and backwards compatibility, but without forcing users who want something newer to keep that part.
It sounds like you want to only do certain things after the link is up. There's a few ways to do this:
1. Use the
onLinkState()
callback to notify on a change of state. Note that
no network tasks should be performed inside the listeners. Instead, set a flag and then process that flag somewhere from the main loop.
2. You could use the simple
Ethernet.waitForLink(timeout)
approach; it returns a Boolean indicating success within the given timeout.
3. Poll the link via
linkState()
(or the non-Boolean-returning
linkStatus()
) somewhere from your main loop.
That's how to detect a link.
Some more notes:
1. You can do things like start servers before the link is up.
2. I noticed in your code that you call
Ethernet.begin()
after the link is detected. This won't work (at least with
QNEthernet; I'm not sure about other libraries) because nothing will happen unless
Ethernet.begin(...)
is called at program startup. In other words, call
begin(...)
first. You
can, however, set up callbacks before calling
begin(...)
so no events are missed. (The README says: "...callbacks should be registered before any other Ethernet functions are called. This ensures that all events are captured. This includes
Ethernet.begin(...)
.")
3. You don't need to specify a MAC address. To use DHCP:
Ethernet.begin()
. To use a static IP:
Ethernet.begin(ip, mask, gateway)
. They both return a Boolean indicating a successful start. With DHCP, the IP will come up later (See
Ethernet.onAddressChanged()
or
Ethernet.waitForIP(timeout)
, or even
localIP()
.)
4. Check out the examples that come with the library for a variety of ways of doing things. Think of them as a supermarket; the more complex examples have concepts you can cherry-pick.
Some relevant README links:
1.
https://github.com/ssilverman/QNEthernet/blob/master/README.md#how-to-use-listeners (no need to worry about the interface-up listener).
2.
https://github.com/ssilverman/QNEth...ME.md#connections-and-linkinterface-detection