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 SocketAddress representation of the local IP address
63  * @param netmask SocketAddress representation of the local network mask
64  * @param gateway SocketAddress representation of the local gateway
65  * @return 0 on success, negative error code on failure
66  */
67  nsapi_error_t set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway) override;
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  * @retval NSAPI_ERROR_OK on success.
75  * @retval NSAPI_ERROR_UNSUPPORTED if operation is not supported.
76  */
77  nsapi_error_t set_dhcp(bool dhcp) override;
78 
79  /** @copydoc NetworkInterface::connect */
80  nsapi_error_t connect() override;
81 
82  /** @copydoc NetworkInterface::disconnect */
83  nsapi_error_t disconnect() override;
84 
85  /** @copydoc NetworkInterface::get_mac_address */
86  const char *get_mac_address() override;
87 
88  /** @copydoc NetworkInterface::get_ip_address */
89  nsapi_error_t get_ip_address(SocketAddress *address) override;
90 
91  /** @copydoc NetworkInterface::get_ipv6_link_local_address */
93 
94  /** @copydoc NetworkInterface::get_netmask */
95  nsapi_error_t get_netmask(SocketAddress *address) override;
96 
97  /** @copydoc NetworkInterface::get_gateway */
98  nsapi_error_t get_gateway(SocketAddress *address) override;
99 
100  /** @copydoc NetworkInterface::get_interface_name */
101  char *get_interface_name(char *interface_name) override;
102 
103  /** @copydoc NetworkInterface::set_as_default */
104  void set_as_default() override;
105 
106  /** @copydoc NetworkInterface::attach */
107  void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) override;
108 
109  /** @copydoc NetworkInterface::get_connection_status */
110  nsapi_connection_status_t get_connection_status() const override;
111 
112  /** @copydoc NetworkInterface::set_blocking */
113  nsapi_error_t set_blocking(bool blocking) override;
114 
115  /** Provide access to the EMAC
116  *
117  * This should be used with care - normally the network stack would
118  * control the EMAC, so manipulating the EMAC while the stack
119  * is also using it (ie after connect) will likely cause problems.
120  *
121  * @return Reference to the EMAC in use
122  */
123  EMAC &get_emac() const
124  {
125  return _emac;
126  }
127 
129  {
130  return this;
131  }
132 
133 protected:
134  /** Provide access to the underlying stack
135  *
136  * @return The underlying network stack
137  */
138  NetworkStack *get_stack() final;
139 
140  EMAC &_emac;
141  OnboardNetworkStack &_stack;
142  OnboardNetworkStack::Interface *_interface = nullptr;
143  bool _dhcp = true;
144  bool _blocking = true;
145  char _mac_address[NSAPI_MAC_SIZE];
146  char _ip_address[NSAPI_IPv6_SIZE] {};
147  char _netmask[NSAPI_IPv4_SIZE] {};
148  char _gateway[NSAPI_IPv4_SIZE] {};
149  mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
150 };
151 
152 #endif
NetworkStack class.
Definition: NetworkStack.h:41
Representation of a stack&#39;s view of an interface.
nsapi_error_t disconnect() override
Disconnect from the network.
EMACInterface class Implementation of the NetworkInterface for an EMAC-based driver.
Definition: EMACInterface.h:38
nsapi_error_t get_ip_address(SocketAddress *address) override
Get the local IP address.
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:95
void set_as_default() override
Set network interface as default one.
const char * get_mac_address() override
Get the local MAC address.
nsapi_error_t get_gateway(SocketAddress *address) override
Get the local gateway.
nsapi_error_t connect() override
Connect to a network.
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:154
nsapi_error_t get_netmask(SocketAddress *address) override
Get the local network mask.
#define NSAPI_IPv4_SIZE
Size of IPv4 representation.
Definition: nsapi_types.h:162
static EMAC & get_default_instance()
Return the default on-board EMAC.
SocketAddress class.
Definition: SocketAddress.h:36
Common interface that is shared between network devices.
nsapi_connection_status_t get_connection_status() const override
Get the connection status.
char * get_interface_name(char *interface_name) override
Get the network interface name.
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_error_t set_dhcp(bool dhcp) override
Enable or disable DHCP on the network.
#define NSAPI_IPv6_SIZE
Size of IPv6 representation.
Definition: nsapi_types.h:170
This interface should be used to abstract low level access to networking hardware All operations rece...
Definition: EMAC.h:32
nsapi_error_t set_blocking(bool blocking) override
Set asynchronous operation of connect() and disconnect() calls.
void attach(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb) override
Register callback for status reporting.
EMACInterface(EMAC &emac=EMAC::get_default_instance(), OnboardNetworkStack &stack=OnboardNetworkStack::get_default_instance())
Create an EMAC-based network interface.
static OnboardNetworkStack & get_default_instance()
Return the default on-board network stack.
Callback class based on template specialization.
Definition: Callback.h:53
nsapi_error_t set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway) override
Set a static IP address.
EMACInterface * emacInterface() final
Return pointer to an EMACInterface.
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.