Mbed OS Reference
Loading...
Searching...
No Matches
EMACInterface.h
1/* LWIP implementation of NetworkInterfaceAPI
2 * Copyright (c) 2015 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef EMAC_INTERFACE_H
19#define EMAC_INTERFACE_H
20
21#include "nsapi.h"
22#include "EMAC.h"
23#include "OnboardNetworkStack.h"
24
25/** \defgroup netinterface Network Interfaces
26 * \ingroup NetSocket */
27/** @{*/
28
29/** EMACInterface class
30 * Implementation of the NetworkInterface for an EMAC-based driver
31 *
32 * This class provides the necessary glue logic to create a NetworkInterface
33 * based on an EMAC and an OnboardNetworkStack. EthernetInterface and
34 * EMAC-based Wi-Fi drivers derive from it.
35 *
36 * Drivers derived from EMACInterface should be constructed so that their
37 * EMAC is functional without the need to call `connect()`. For example
38 * a Wi-Fi driver should permit `WiFi::get_emac().power_up()` as soon as
39 * the credentials have been set. This is necessary to support specialized
40 * applications such as 6LoWPAN mesh border routers.
41 */
42class EMACInterface : public virtual NetworkInterface {
43public:
44 /** Create an EMAC-based network interface.
45 *
46 * The default arguments obtain the default EMAC, which will be target-
47 * dependent (and the target may have some JSON option to choose which
48 * is the default, if there are multiple). The default stack is configured
49 * by JSON option nsapi.default-stack.
50 *
51 * Due to inability to return errors from the constructor, no real
52 * work is done until the first call to connect().
53 *
54 * @param emac Reference to EMAC to use
55 * @param stack Reference to onboard-network stack to use
56 */
59
60 /** Set a static IP address
61 *
62 * Configures this network interface to use a static IP address.
63 * Implicitly disables DHCP, which can be enabled in set_dhcp.
64 * Requires that the network is disconnected.
65 *
66 * @param ip_address SocketAddress representation of the local IP address
67 * @param netmask SocketAddress representation of the local network mask
68 * @param gateway SocketAddress representation of the local gateway
69 * @return 0 on success, negative error code on failure
70 */
71 nsapi_error_t set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway) override;
72
73 /** Enable or disable DHCP on the network
74 *
75 * Requires that the network is disconnected
76 *
77 * @param dhcp False to disable dhcp (defaults to enabled)
78 * @retval NSAPI_ERROR_OK on success.
79 * @retval NSAPI_ERROR_UNSUPPORTED if operation is not supported.
80 */
81 nsapi_error_t set_dhcp(bool dhcp) override;
82
83 /** @copydoc NetworkInterface::connect */
85
86 /** @copydoc NetworkInterface::disconnect */
88
89 /** @copydoc NetworkInterface::get_hostname */
90 const char *get_hostname() override;
91
92 /** @copydoc NetworkInterface::set_hostname */
93 nsapi_error_t set_hostname(const char *hostname) override;
94
95 /** @copydoc NetworkInterface::get_mac_address */
96 const char *get_mac_address() override;
97
98 /** @copydoc NetworkInterface::set_mac_address */
99 nsapi_error_t set_mac_address(uint8_t *mac_addr, nsapi_size_t addr_len) override;
100
101 /** @copydoc NetworkInterface::get_ip_address */
103
104 /** @copydoc NetworkInterface::get_ipv6_link_local_address */
106
107 /** @copydoc NetworkInterface::get_netmask */
109
110 /** @copydoc NetworkInterface::get_gateway */
112
113 /** @copydoc NetworkInterface::get_interface_name */
114 char *get_interface_name(char *interface_name) override;
115
116 /** @copydoc NetworkInterface::set_as_default */
117 void set_as_default() override;
118
119 /** @copydoc NetworkInterface::attach */
120 void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) override;
121
122 /** @copydoc NetworkInterface::get_connection_status */
123 nsapi_connection_status_t get_connection_status() const override;
124
125 /** @copydoc NetworkInterface::set_blocking */
126 nsapi_error_t set_blocking(bool blocking) override;
127
128 /** Provide access to the EMAC
129 *
130 * This should be used with care - normally the network stack would
131 * control the EMAC, so manipulating the EMAC while the stack
132 * is also using it (ie after connect) will likely cause problems.
133 *
134 * @return Reference to the EMAC in use
135 */
136 EMAC &get_emac() const
137 {
138 return _emac;
139 }
140
142 {
143 return this;
144 }
145
146protected:
147 /** Provide access to the underlying stack
148 *
149 * @return The underlying network stack
150 */
152
153 EMAC &_emac;
154 OnboardNetworkStack &_stack;
155 OnboardNetworkStack::Interface *_interface = nullptr;
156 bool _dhcp = true;
157 bool _blocking = true;
158 bool _hostname_set = false;
159 char _hostname[NSAPI_HOSTNAME_SIZE];
160 bool _hw_mac_addr_set = false;
161 char _mac_address[NSAPI_MAC_SIZE];
162 char _ip_address[NSAPI_IPv6_SIZE] {};
163 char _netmask[NSAPI_IPv4_SIZE] {};
164 char _gateway[NSAPI_IPv4_SIZE] {};
165 uint8_t _hw_mac_addr[NSAPI_MAC_BYTES] {};
166 mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
167};
168
169/** @}*/
170
171#endif
This interface should be used to abstract low level access to networking hardware All operations rece...
Definition: EMAC.h:33
static EMAC & get_default_instance()
Return the default on-board EMAC.
EMACInterface class Implementation of the NetworkInterface for an EMAC-based driver.
Definition: EMACInterface.h:42
EMACInterface(EMAC &emac=EMAC::get_default_instance(), OnboardNetworkStack &stack=OnboardNetworkStack::get_default_instance())
Create an EMAC-based network interface.
nsapi_error_t disconnect() override
Disconnect from the network.
char * get_interface_name(char *interface_name) override
Get the network interface name.
nsapi_error_t set_mac_address(uint8_t *mac_addr, nsapi_size_t addr_len) override
Set the MAC address to the interface.
EMACInterface * emacInterface() final
Return pointer to an EMACInterface.
const char * get_mac_address() override
Get the local MAC address.
nsapi_error_t get_ipv6_link_local_address(SocketAddress *address) override
Get the IPv6 link local address.
NetworkStack * get_stack() final
Provide access to the underlying stack.
nsapi_connection_status_t get_connection_status() const override
Get the connection status.
nsapi_error_t connect() override
Connect to a network.
void set_as_default() override
Set network interface as default one.
EMAC & get_emac() const
Provide access to the EMAC.
nsapi_error_t get_gateway(SocketAddress *address) override
Get the local gateway.
const char * get_hostname() override
Get hostname.
nsapi_error_t get_ip_address(SocketAddress *address) override
Get the local IP address.
nsapi_error_t set_dhcp(bool dhcp) override
Enable or disable DHCP on the network.
void attach(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb) override
Register callback for status reporting.
nsapi_error_t set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway) override
Set a static IP address.
nsapi_error_t set_blocking(bool blocking) override
Set asynchronous operation of connect() and disconnect() calls.
nsapi_error_t set_hostname(const char *hostname) override
Set hostname.
nsapi_error_t get_netmask(SocketAddress *address) override
Get the local network mask.
Common interface that is shared between network devices.
NetworkStack class.
Definition: NetworkStack.h:42
mbed OS API for onboard IP stack abstraction
static OnboardNetworkStack & get_default_instance()
Return the default on-board network stack.
SocketAddress class.
Definition: SocketAddress.h:37
Callback class based on template specialization.
Definition: Callback.h:53
#define NSAPI_MAC_BYTES
Maximum number of bytes for MAC address.
Definition: nsapi_types.h:215
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:142
#define NSAPI_MAC_SIZE
Maximum size of MAC address representation.
Definition: nsapi_types.h:211
#define NSAPI_IPv4_SIZE
Size of IPv4 representation.
Definition: nsapi_types.h:219
#define NSAPI_IPv6_SIZE
Size of IPv6 representation.
Definition: nsapi_types.h:227
#define NSAPI_HOSTNAME_SIZE
Maximum size of hostname.
Definition: nsapi_types.h:207
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:146