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 local network mask
107  *
108  * @return Null-terminated representation of the local network mask
109  * or null if no network mask has been received
110  */
111  virtual const char *get_netmask();
112 
113  /** Get the local gateways
114  *
115  * @return Null-terminated representation of the local gateway
116  * or null if no network mask has been received
117  */
118  virtual const char *get_gateway();
119 
120  /** Get the network interface name
121  *
122  * @return Null-terminated representation of the network interface name
123  * or null if interface not exists
124  */
125  virtual char *get_interface_name(char *interface_name);
126 
127  /** Set the network interface as default one
128  */
129  virtual void set_as_default();
130 
131  /** Register callback for status reporting
132  *
133  * @param status_cb The callback for status changes
134  */
135  virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
136 
137  /** Get the connection status
138  *
139  * @return The connection status according to nsapi_connection_status_t
140  */
141  virtual nsapi_connection_status_t get_connection_status() const;
142 
143  /** Set blocking status of connect() which by default should be blocking
144  *
145  * @param blocking true if connect is blocking
146  * @return 0 on success, negative error code on failure
147  */
148  virtual nsapi_error_t set_blocking(bool blocking);
149 
150  /** Provide access to the EMAC
151  *
152  * This should be used with care - normally the network stack would
153  * control the EMAC, so manipulating the EMAC while the stack
154  * is also using it (ie after connect) will likely cause problems.
155  *
156  * @return Reference to the EMAC in use
157  */
158  EMAC &get_emac() const
159  {
160  return _emac;
161  }
162 
164  {
165  return this;
166  }
167 
168 protected:
169  /** Provide access to the underlying stack
170  *
171  * @return The underlying network stack
172  */
173  virtual NetworkStack *get_stack();
174 
175  EMAC &_emac;
176  OnboardNetworkStack &_stack;
177  OnboardNetworkStack::Interface *_interface;
178  bool _dhcp;
179  bool _blocking;
180  char _mac_address[NSAPI_MAC_SIZE];
181  char _ip_address[NSAPI_IPv6_SIZE];
182  char _netmask[NSAPI_IPv4_SIZE];
183  char _gateway[NSAPI_IPv4_SIZE];
184  mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
185 };
186 
187 #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
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:151
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:159
static EMAC & get_default_instance()
Return the default on-board EMAC.
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:167
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.