Skip to content

Network interface: Add status callback register #5457

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 16 commits into from Jan 3, 2018
39 changes: 37 additions & 2 deletions features/FEATURE_LWIP/lwip-interface/EthernetInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@


/* Interface implementation */
EthernetInterface::EthernetInterface()
: _dhcp(true), _ip_address(), _netmask(), _gateway()
EthernetInterface::EthernetInterface() :
_dhcp(true),
_ip_address(),
_netmask(),
_gateway(),
_connection_status_cb(NULL),
_connect_status(NSAPI_STATUS_DISCONNECTED)
{
}


nsapi_error_t EthernetInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
{
_dhcp = false;
Expand Down Expand Up @@ -94,3 +100,32 @@ NetworkStack *EthernetInterface::get_stack()
{
return nsapi_create_stack(&lwip_stack);
}

void EthernetInterface::attach(
Callback<void(nsapi_event_t, intptr_t)> status_cb)
{
_connection_status_cb = status_cb;
mbed_lwip_attach(netif_status_cb, this);
}

nsapi_connection_status_t EthernetInterface::get_connection_status() const
{
return _connect_status;
}

void EthernetInterface::netif_status_cb(void *ethernet_if_ptr,
nsapi_event_t reason, intptr_t parameter)
{
EthernetInterface *eth_ptr = static_cast<EthernetInterface*>(ethernet_if_ptr);
eth_ptr->_connect_status = (nsapi_connection_status_t)parameter;
if (eth_ptr->_connection_status_cb)
{
eth_ptr->_connection_status_cb(reason, parameter);
}
}

nsapi_error_t EthernetInterface::set_blocking(bool blocking)
{
mbed_lwip_set_blocking(blocking);
return NSAPI_ERROR_OK;
}
25 changes: 25 additions & 0 deletions features/FEATURE_LWIP/lwip-interface/EthernetInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,26 @@ class EthernetInterface : public EthInterface
*/
virtual const char *get_gateway();

/** Register callback for status reporting
*
* @param status_cb The callback for status changes
*/
virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);

/** Get the connection status
*
* @return The connection status according to nsapi_connection_status_t
*/
virtual nsapi_connection_status_t get_connection_status() const;

/** Set blocking status of connect() which by default should be blocking
*
* @param blocking true if connect is blocking
* @return 0 on success, negative error code on failure
*/
virtual nsapi_error_t set_blocking(bool blocking);


protected:
/** Provide access to the underlying stack
*
Expand All @@ -111,6 +131,11 @@ class EthernetInterface : public EthInterface
char _ip_address[IPADDR_STRLEN_MAX];
char _netmask[NSAPI_IPv4_SIZE];
char _gateway[NSAPI_IPv4_SIZE];


Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
nsapi_connection_status_t _connect_status;
static void netif_status_cb(void *, nsapi_event_t, intptr_t);
};


Expand Down
Loading