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 "rtos.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 specialised
37  * applications such as 6LoWPAN mesh border routers.
38  */
39 class EMACInterface : public virtual NetworkInterface
40 {
41 public:
42  /** Create an EMAC-based network interface.
43  *
44  * The default arguments obtain the default EMAC, which will be target-
45  * dependent (and the target may have some JSON option to choose which
46  * is the default, if there are multiple). The default stack is configured
47  * by JSON option nsapi.default-stack.
48  *
49  * Due to inability to return errors from the constructor, no real
50  * work is done until the first call to connect().
51  *
52  * @param emac Reference to EMAC to use
53  * @param stack Reference to onboard-network stack to use
54  */
58 
59  /** Set a static IP address
60  *
61  * Configures this network interface to use a static IP address.
62  * Implicitly disables DHCP, which can be enabled in set_dhcp.
63  * Requires that the network is disconnected.
64  *
65  * @param ip_address Null-terminated representation of the local IP address
66  * @param netmask Null-terminated representation of the local network mask
67  * @param gateway Null-terminated representation of the local gateway
68  * @return 0 on success, negative error code on failure
69  */
70  virtual nsapi_error_t set_network(
71  const char *ip_address, const char *netmask, const char *gateway);
72 
73  /** Enable or disable DHCP on the network
74  *
75  * Requires that the network is disconnected
76  *
77  * @param dhcp False to disable dhcp (defaults to enabled)
78  * @return 0 on success, negative error code on failure
79  */
80  virtual nsapi_error_t set_dhcp(bool dhcp);
81 
82  /** Start the interface
83  * @return 0 on success, negative on failure
84  */
85  virtual nsapi_error_t connect();
86 
87  /** Stop the interface
88  * @return 0 on success, negative on failure
89  */
90  virtual nsapi_error_t disconnect();
91 
92  /** Get the local MAC address
93  *
94  * Provided MAC address is intended for info or debug purposes and
95  * may not be provided if the underlying network interface does not
96  * provide a MAC address
97  *
98  * @return Null-terminated representation of the local MAC address
99  * or null if no MAC address is available
100  */
101  virtual const char *get_mac_address();
102 
103  /** Get the local IP address
104  *
105  * @return Null-terminated representation of the local IP address
106  * or null if no IP address has been recieved
107  */
108  virtual const char *get_ip_address();
109 
110  /** Get the local network mask
111  *
112  * @return Null-terminated representation of the local network mask
113  * or null if no network mask has been recieved
114  */
115  virtual const char *get_netmask();
116 
117  /** Get the local gateways
118  *
119  * @return Null-terminated representation of the local gateway
120  * or null if no network mask has been recieved
121  */
122  virtual const char *get_gateway();
123 
124  /** Register callback for status reporting
125  *
126  * @param status_cb The callback for status changes
127  */
128  virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
129 
130  /** Get the connection status
131  *
132  * @return The connection status according to nsapi_connection_status_t
133  */
134  virtual nsapi_connection_status_t get_connection_status() const;
135 
136  /** Set blocking status of connect() which by default should be blocking
137  *
138  * @param blocking true if connect is blocking
139  * @return 0 on success, negative error code on failure
140  */
141  virtual nsapi_error_t set_blocking(bool blocking);
142 
143  /** Provide access to the EMAC
144  *
145  * This should be used with care - normally the network stack would
146  * control the EMAC, so manipulating the EMAC while the stack
147  * is also using it (ie after connect) will likely cause problems.
148  *
149  * @return Reference to the EMAC in use
150  */
151  EMAC &get_emac() const { return _emac; }
152 
154  return this;
155  }
156 
157 protected:
158  /** Provide access to the underlying stack
159  *
160  * @return The underlying network stack
161  */
162  virtual NetworkStack *get_stack();
163 
164  EMAC &_emac;
165  OnboardNetworkStack &_stack;
166  OnboardNetworkStack::Interface *_interface;
167  bool _dhcp;
168  bool _blocking;
169  char _mac_address[NSAPI_MAC_SIZE];
170  char _ip_address[NSAPI_IPv6_SIZE];
171  char _netmask[NSAPI_IPv4_SIZE];
172  char _gateway[NSAPI_IPv4_SIZE];
173  Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
174 };
175 
176 #endif
virtual nsapi_error_t connect()
Definition: EMACInterface.cpp:52
Definition: NetworkStack.h:37
Definition: OnboardNetworkStack.h:47
virtual const char * get_mac_address()
Definition: EMACInterface.cpp:79
Definition: EMACInterface.h:39
signed int nsapi_error_t
Definition: nsapi_types.h:94
EMAC & get_emac() const
Definition: EMACInterface.h:151
virtual NetworkStack * get_stack()
Definition: EMACInterface.cpp:114
virtual nsapi_error_t set_blocking(bool blocking)
Definition: EMACInterface.cpp:137
static OnboardNetworkStack & get_default_instance()
Definition: OnboardNetworkStack.h:33
#define NSAPI_MAC_SIZE
Definition: nsapi_types.h:140
#define NSAPI_IPv4_SIZE
Definition: nsapi_types.h:148
virtual nsapi_error_t set_network(const char *ip_address, const char *netmask, const char *gateway)
Definition: EMACInterface.cpp:32
Definition: NetworkInterface.h:39
virtual nsapi_connection_status_t get_connection_status() const
Definition: EMACInterface.cpp:128
static EMAC & get_default_instance()
Definition: kinetis_emac.cpp:598
virtual const char * get_netmask()
Definition: EMACInterface.cpp:96
virtual const char * get_ip_address()
Definition: EMACInterface.cpp:87
EMACInterface(EMAC &emac=EMAC::get_default_instance(), OnboardNetworkStack &stack=OnboardNetworkStack::get_default_instance())
Definition: EMACInterface.cpp:20
#define NSAPI_IPv6_SIZE
Definition: nsapi_types.h:156
virtual const char * get_gateway()
Definition: EMACInterface.cpp:105
Definition: EMAC.h:32
virtual EMACInterface * emacInterface()
Definition: EMACInterface.h:153
virtual nsapi_error_t set_dhcp(bool dhcp)
Definition: EMACInterface.cpp:46
Definition: Callback.h:38
virtual nsapi_error_t disconnect()
Definition: EMACInterface.cpp:71
virtual void attach(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb)
Definition: EMACInterface.cpp:119
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.