Mistake on this page?
Report an issue in GitHub or email us
EMACInterface.h
1 /* LWIP implementation of NetworkInterfaceAPI
2  * Copyright (c) 2015 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef EMAC_INTERFACE_H
18 #define EMAC_INTERFACE_H
19 
20 #include "nsapi.h"
21 #include "EMAC.h"
22 #include "OnboardNetworkStack.h"
23 
24 
25 /** EMACInterface class
26  * Implementation of the NetworkInterface for an EMAC-based driver
27  *
28  * This class provides the necessary glue logic to create a NetworkInterface
29  * based on an EMAC and an OnboardNetworkStack. EthernetInterface and
30  * EMAC-based Wi-Fi drivers derive from it.
31  *
32  * Drivers derived from EMACInterface should be constructed so that their
33  * EMAC is functional without the need to call `connect()`. For example
34  * a Wi-Fi driver should permit `WiFi::get_emac().power_up()` as soon as
35  * the credentials have been set. This is necessary to support specialized
36  * applications such as 6LoWPAN mesh border routers.
37  */
38 class EMACInterface : public virtual NetworkInterface {
39 public:
40  /** Create an EMAC-based network interface.
41  *
42  * The default arguments obtain the default EMAC, which will be target-
43  * dependent (and the target may have some JSON option to choose which
44  * is the default, if there are multiple). The default stack is configured
45  * by JSON option nsapi.default-stack.
46  *
47  * Due to inability to return errors from the constructor, no real
48  * work is done until the first call to connect().
49  *
50  * @param emac Reference to EMAC to use
51  * @param stack Reference to onboard-network stack to use
52  */
55 
56  /** Set a static IP address
57  *
58  * Configures this network interface to use a static IP address.
59  * Implicitly disables DHCP, which can be enabled in set_dhcp.
60  * Requires that the network is disconnected.
61  *
62  * @param ip_address Null-terminated representation of the local IP address
63  * @param netmask Null-terminated representation of the local network mask
64  * @param gateway Null-terminated representation of the local gateway
65  * @return 0 on success, negative error code on failure
66  */
67  virtual nsapi_error_t set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway);
68 
69  MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
70  virtual nsapi_error_t set_network(const char *ip_address, const char *netmask, const char *gateway);
71 
72  /** Enable or disable DHCP on the network
73  *
74  * Requires that the network is disconnected
75  *
76  * @param dhcp False to disable dhcp (defaults to enabled)
77  * @return 0 on success, negative error code on failure
78  */
79  virtual nsapi_error_t set_dhcp(bool dhcp);
80 
81  /** Start the interface
82  * @return 0 on success, negative on failure
83  */
84  virtual nsapi_error_t connect();
85 
86  /** Stop the interface
87  * @return 0 on success, negative on failure
88  */
89  virtual nsapi_error_t disconnect();
90 
91  /** Get the local MAC address
92  *
93  * Provided MAC address is intended for info or debug purposes and
94  * may not be provided if the underlying network interface does not
95  * provide a MAC address
96  *
97  * @return Null-terminated representation of the local MAC address
98  * or null if no MAC address is available
99  */
100  virtual const char *get_mac_address();
101 
102  /** Get the local IP address
103  *
104  * @return Null-terminated representation of the local IP address
105  * or null if no IP address has been received
106  */
107  virtual nsapi_error_t get_ip_address(SocketAddress *address);
108 
109  MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
110  virtual const char *get_ip_address();
111 
112  /** Get the IPv6 link local address
113  *
114  * @address SocketAddress representation of the link local IPv6 address
115  * @return 0 on success, negative error code on failure
116  */
118 
119  /** Get the local network mask
120  *
121  * @return Null-terminated representation of the local network mask
122  * or null if no network mask has been received
123  */
124  virtual nsapi_error_t get_netmask(SocketAddress *address);
125 
126  MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
127  virtual const char *get_netmask();
128 
129  /** Get the local gateways
130  *
131  * @return Null-terminated representation of the local gateway
132  * or null if no network mask has been received
133  */
134  virtual nsapi_error_t get_gateway(SocketAddress *address);
135 
136  MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
137  virtual const char *get_gateway();
138 
139  /** Get the network interface name
140  *
141  * @return Null-terminated representation of the network interface name
142  * or null if interface not exists
143  */
144  virtual char *get_interface_name(char *interface_name);
145 
146  /** Set the network interface as default one
147  */
148  virtual void set_as_default();
149 
150  /** Register callback for status reporting
151  *
152  * @param status_cb The callback for status changes
153  */
154  virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
155 
156  /** Get the connection status
157  *
158  * @return The connection status according to nsapi_connection_status_t
159  */
160  virtual nsapi_connection_status_t get_connection_status() const;
161 
162  /** Set blocking status of connect() which by default should be blocking
163  *
164  * @param blocking true if connect is blocking
165  * @return 0 on success, negative error code on failure
166  */
167  virtual nsapi_error_t set_blocking(bool blocking);
168 
169  /** Provide access to the EMAC
170  *
171  * This should be used with care - normally the network stack would
172  * control the EMAC, so manipulating the EMAC while the stack
173  * is also using it (ie after connect) will likely cause problems.
174  *
175  * @return Reference to the EMAC in use
176  */
177  EMAC &get_emac() const
178  {
179  return _emac;
180  }
181 
183  {
184  return this;
185  }
186 
187 protected:
188  /** Provide access to the underlying stack
189  *
190  * @return The underlying network stack
191  */
192  virtual NetworkStack *get_stack();
193 
194  EMAC &_emac;
195  OnboardNetworkStack &_stack;
196  OnboardNetworkStack::Interface *_interface;
197  bool _dhcp;
198  bool _blocking;
199  char _mac_address[NSAPI_MAC_SIZE];
200  char _ip_address[NSAPI_IPv6_SIZE];
201  char _netmask[NSAPI_IPv4_SIZE];
202  char _gateway[NSAPI_IPv4_SIZE];
203  mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
204 };
205 
206 #endif
virtual void attach(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb)
Register callback for status reporting.
NetworkStack class.
Definition: NetworkStack.h:40
Representation of a stack&#39;s view of an interface.
virtual EMACInterface * emacInterface()
Return pointer to an EMACInterface.
EMACInterface class Implementation of the NetworkInterface for an EMAC-based driver.
Definition: EMACInterface.h:38
virtual nsapi_error_t get_ipv6_link_local_address(SocketAddress *address)
Get the IPv6 link local address.
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:95
EMAC & get_emac() const
Provide access to the EMAC.
mbed OS API for onboard IP stack abstraction
#define NSAPI_MAC_SIZE
Maximum size of MAC address representation.
Definition: nsapi_types.h:152
virtual void set_as_default()
Set the network interface as default one.
virtual nsapi_error_t connect()
Start the interface.
virtual const char * get_mac_address()
Get the local MAC address.
#define NSAPI_IPv4_SIZE
Size of IPv4 representation.
Definition: nsapi_types.h:160
static EMAC & get_default_instance()
Return the default on-board EMAC.
SocketAddress class.
Definition: SocketAddress.h:35
Common interface that is shared between network devices.
virtual char * get_interface_name(char *interface_name)
Get the network interface name.
#define NSAPI_IPv6_SIZE
Size of IPv6 representation.
Definition: nsapi_types.h:168
virtual nsapi_error_t get_gateway(SocketAddress *address)
Get the local gateways.
This interface should be used to abstract low level access to networking hardware All operations rece...
Definition: EMAC.h:32
virtual nsapi_error_t get_ip_address(SocketAddress *address)
Get the local IP address.
virtual NetworkStack * get_stack()
Provide access to the underlying stack.
virtual nsapi_error_t set_dhcp(bool dhcp)
Enable or disable DHCP on the network.
virtual nsapi_error_t set_blocking(bool blocking)
Set blocking status of connect() which by default should be blocking.
EMACInterface(EMAC &emac=EMAC::get_default_instance(), OnboardNetworkStack &stack=OnboardNetworkStack::get_default_instance())
Create an EMAC-based network interface.
virtual nsapi_error_t disconnect()
Stop the interface.
static OnboardNetworkStack & get_default_instance()
Return the default on-board network stack.
Callback class based on template specialization.
Definition: Callback.h:39
virtual nsapi_error_t set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway)
Set a static IP address.
virtual nsapi_connection_status_t get_connection_status() const
Get the connection status.
#define MBED_DEPRECATED_SINCE(D, M)
MBED_DEPRECATED("message string") Mark a function declaration as deprecated, if it used then a warnin...
virtual nsapi_error_t get_netmask(SocketAddress *address)
Get the local network mask.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.