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 char *ip_address, const char *netmask, const char *gateway);
68 
69  /** Enable or disable DHCP on the network
70  *
71  * Requires that the network is disconnected
72  *
73  * @param dhcp False to disable dhcp (defaults to enabled)
74  * @return 0 on success, negative error code on failure
75  */
76  virtual nsapi_error_t set_dhcp(bool dhcp);
77 
78  /** Start the interface
79  * @return 0 on success, negative on failure
80  */
81  virtual nsapi_error_t connect();
82 
83  /** Stop the interface
84  * @return 0 on success, negative on failure
85  */
86  virtual nsapi_error_t disconnect();
87 
88  /** Get the local MAC address
89  *
90  * Provided MAC address is intended for info or debug purposes and
91  * may not be provided if the underlying network interface does not
92  * provide a MAC address
93  *
94  * @return Null-terminated representation of the local MAC address
95  * or null if no MAC address is available
96  */
97  virtual const char *get_mac_address();
98 
99  /** Get the local IP address
100  *
101  * @return Null-terminated representation of the local IP address
102  * or null if no IP address has been received
103  */
104  virtual const char *get_ip_address();
105 
106  /** Get the IPv6 link local address
107  *
108  * @address SocketAddress representation of the link local IPv6 address
109  * @return 0 on success, negative error code on failure
110  */
112 
113  /** Get the local network mask
114  *
115  * @return Null-terminated representation of the local network mask
116  * or null if no network mask has been received
117  */
118  virtual const char *get_netmask();
119 
120  /** Get the local gateways
121  *
122  * @return Null-terminated representation of the local gateway
123  * or null if no network mask has been received
124  */
125  virtual const char *get_gateway();
126 
127  /** Get the network interface name
128  *
129  * @return Null-terminated representation of the network interface name
130  * or null if interface not exists
131  */
132  virtual char *get_interface_name(char *interface_name);
133 
134  /** Set the network interface as default one
135  */
136  virtual void set_as_default();
137 
138  /** Register callback for status reporting
139  *
140  * @param status_cb The callback for status changes
141  */
142  virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
143 
144  /** Get the connection status
145  *
146  * @return The connection status according to nsapi_connection_status_t
147  */
148  virtual nsapi_connection_status_t get_connection_status() const;
149 
150  /** Set blocking status of connect() which by default should be blocking
151  *
152  * @param blocking true if connect is blocking
153  * @return 0 on success, negative error code on failure
154  */
155  virtual nsapi_error_t set_blocking(bool blocking);
156 
157  /** Provide access to the EMAC
158  *
159  * This should be used with care - normally the network stack would
160  * control the EMAC, so manipulating the EMAC while the stack
161  * is also using it (ie after connect) will likely cause problems.
162  *
163  * @return Reference to the EMAC in use
164  */
165  EMAC &get_emac() const
166  {
167  return _emac;
168  }
169 
171  {
172  return this;
173  }
174 
175 protected:
176  /** Provide access to the underlying stack
177  *
178  * @return The underlying network stack
179  */
180  virtual NetworkStack *get_stack();
181 
182  EMAC &_emac;
183  OnboardNetworkStack &_stack;
184  OnboardNetworkStack::Interface *_interface;
185  bool _dhcp;
186  bool _blocking;
187  char _mac_address[NSAPI_MAC_SIZE];
188  char _ip_address[NSAPI_IPv6_SIZE];
189  char _netmask[NSAPI_IPv4_SIZE];
190  char _gateway[NSAPI_IPv4_SIZE];
191  mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
192 };
193 
194 #endif
virtual void attach(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb)
Register callback for status reporting.
virtual const char * get_netmask()
Get the local network mask.
NetworkStack class.
Definition: NetworkStack.h:40
virtual const char * get_ip_address()
Get the local IP address.
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 nsapi_error_t set_network(const char *ip_address, const char *netmask, const char *gateway)
Set a static IP address.
virtual char * get_interface_name(char *interface_name)
Get the network interface name.
virtual const char * get_gateway()
Get the local gateways.
#define NSAPI_IPv6_SIZE
Size of IPv6 representation.
Definition: nsapi_types.h:168
This interface should be used to abstract low level access to networking hardware All operations rece...
Definition: EMAC.h:32
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_connection_status_t get_connection_status() const
Get the connection status.
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.