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  * 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 
26 /** EMACInterface class
27  * Implementation of the NetworkInterface for an EMAC-based driver
28  *
29  * This class provides the necessary glue logic to create a NetworkInterface
30  * based on an EMAC and an OnboardNetworkStack. EthernetInterface and
31  * EMAC-based Wi-Fi drivers derive from it.
32  *
33  * Drivers derived from EMACInterface should be constructed so that their
34  * EMAC is functional without the need to call `connect()`. For example
35  * a Wi-Fi driver should permit `WiFi::get_emac().power_up()` as soon as
36  * the credentials have been set. This is necessary to support specialized
37  * applications such as 6LoWPAN mesh border routers.
38  */
39 class EMACInterface : public virtual NetworkInterface {
40 public:
41  /** Create an EMAC-based network interface.
42  *
43  * The default arguments obtain the default EMAC, which will be target-
44  * dependent (and the target may have some JSON option to choose which
45  * is the default, if there are multiple). The default stack is configured
46  * by JSON option nsapi.default-stack.
47  *
48  * Due to inability to return errors from the constructor, no real
49  * work is done until the first call to connect().
50  *
51  * @param emac Reference to EMAC to use
52  * @param stack Reference to onboard-network stack to use
53  */
56 
57  /** Set a static IP address
58  *
59  * Configures this network interface to use a static IP address.
60  * Implicitly disables DHCP, which can be enabled in set_dhcp.
61  * Requires that the network is disconnected.
62  *
63  * @param ip_address SocketAddress representation of the local IP address
64  * @param netmask SocketAddress representation of the local network mask
65  * @param gateway SocketAddress representation of the local gateway
66  * @return 0 on success, negative error code on failure
67  */
68  nsapi_error_t set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway) override;
69 
70  /** Enable or disable DHCP on the network
71  *
72  * Requires that the network is disconnected
73  *
74  * @param dhcp False to disable dhcp (defaults to enabled)
75  * @retval NSAPI_ERROR_OK on success.
76  * @retval NSAPI_ERROR_UNSUPPORTED if operation is not supported.
77  */
78  nsapi_error_t set_dhcp(bool dhcp) override;
79 
80  /** @copydoc NetworkInterface::connect */
81  nsapi_error_t connect() override;
82 
83  /** @copydoc NetworkInterface::disconnect */
84  nsapi_error_t disconnect() override;
85 
86  /** @copydoc NetworkInterface::get_mac_address */
87  const char *get_mac_address() override;
88 
89  /** @copydoc NetworkInterface::set_mac_address */
90  nsapi_error_t set_mac_address(uint8_t *mac_addr, nsapi_size_t addr_len) override;
91 
92  /** @copydoc NetworkInterface::get_ip_address */
93  nsapi_error_t get_ip_address(SocketAddress *address) override;
94 
95  /** @copydoc NetworkInterface::get_ipv6_link_local_address */
97 
98  /** @copydoc NetworkInterface::get_netmask */
99  nsapi_error_t get_netmask(SocketAddress *address) override;
100 
101  /** @copydoc NetworkInterface::get_gateway */
102  nsapi_error_t get_gateway(SocketAddress *address) override;
103 
104  /** @copydoc NetworkInterface::get_interface_name */
105  char *get_interface_name(char *interface_name) override;
106 
107  /** @copydoc NetworkInterface::set_as_default */
108  void set_as_default() override;
109 
110  /** @copydoc NetworkInterface::attach */
111  void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) override;
112 
113  /** @copydoc NetworkInterface::get_connection_status */
114  nsapi_connection_status_t get_connection_status() const override;
115 
116  /** @copydoc NetworkInterface::set_blocking */
117  nsapi_error_t set_blocking(bool blocking) override;
118 
119  /** Provide access to the EMAC
120  *
121  * This should be used with care - normally the network stack would
122  * control the EMAC, so manipulating the EMAC while the stack
123  * is also using it (ie after connect) will likely cause problems.
124  *
125  * @return Reference to the EMAC in use
126  */
127  EMAC &get_emac() const
128  {
129  return _emac;
130  }
131 
133  {
134  return this;
135  }
136 
137 protected:
138  /** Provide access to the underlying stack
139  *
140  * @return The underlying network stack
141  */
142  NetworkStack *get_stack() final;
143 
144  EMAC &_emac;
145  OnboardNetworkStack &_stack;
146  OnboardNetworkStack::Interface *_interface = nullptr;
147  bool _dhcp = true;
148  bool _blocking = true;
149  bool _hw_mac_addr_set = false;
150  char _mac_address[NSAPI_MAC_SIZE];
151  char _ip_address[NSAPI_IPv6_SIZE] {};
152  char _netmask[NSAPI_IPv4_SIZE] {};
153  char _gateway[NSAPI_IPv4_SIZE] {};
154  uint8_t _hw_mac_addr[NSAPI_MAC_BYTES] {};
155  mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
156 };
157 
158 #endif
#define NSAPI_MAC_BYTES
Maximum number of bytes for MAC address.
Definition: nsapi_types.h:203
NetworkStack class.
Definition: NetworkStack.h:42
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:39
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:140
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.
nsapi_error_t set_mac_address(uint8_t *mac_addr, nsapi_size_t addr_len) override
Set the MAC address to the interface.
mbed OS API for onboard IP stack abstraction
#define NSAPI_MAC_SIZE
Maximum size of MAC address representation.
Definition: nsapi_types.h:199
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:207
static EMAC & get_default_instance()
Return the default on-board EMAC.
SocketAddress class.
Definition: SocketAddress.h:37
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:215
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:144
This interface should be used to abstract low level access to networking hardware All operations rece...
Definition: EMAC.h:33
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.