Mistake on this page?
Report an issue in GitHub or email us
NetworkInterface.h
Go to the documentation of this file.
1 /*
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 /** @file NetworkInterface.h Network Interface base class */
18 /** @addtogroup netinterface
19  * Network Interface classes
20  * @{ */
21 
22 
23 #ifndef NETWORK_INTERFACE_H
24 #define NETWORK_INTERFACE_H
25 
26 #include "netsocket/nsapi_types.h"
28 #include "Callback.h"
29 #include "DNS.h"
30 
31 
32 // Predeclared classes
33 class NetworkStack;
34 class EthInterface;
35 class WiFiInterface;
36 class MeshInterface;
37 class CellularInterface;
38 class EMACInterface;
39 
40 /** Common interface that is shared between network devices.
41  *
42  */
43 class NetworkInterface: public DNS {
44 public:
45 
46  virtual ~NetworkInterface();
47 
48  /** Return the default network interface.
49  *
50  * Returns the default network interface, as determined by JSON option
51  * target.network-default-interface-type or other overrides.
52  *
53  * The type of the interface returned can be tested by calling ethInterface(),
54  * wifiInterface(), meshInterface(), cellularInterface(), emacInterface() and checking
55  * for NULL pointers.
56  *
57  * The default behavior is to return the default interface for the
58  * interface type specified by target.network-default-interface-type. Targets
59  * should set this in their targets.json to guide default selection,
60  * and applications may override.
61  *
62  * The interface returned should be already configured for use such that its
63  * connect() method works with no parameters. For connection types needing
64  * configuration, settings should normally be obtained from JSON - the
65  * settings for the core types are under the "nsapi" JSON config tree.
66  *
67  * The list of possible settings for default interface type is open-ended,
68  * as is the number of possible providers. Core providers are:
69  *
70  * * ETHERNET: EthernetInterface, using default EMAC and OnboardNetworkStack
71  * * MESH: ThreadInterface or LoWPANNDInterface, using default NanostackRfPhy
72  * * CELLULAR: OnboardModemInterface
73  * * WIFI: None - always provided by a specific class
74  *
75  * Specific drivers may be activated by other settings of the
76  * default-network-interface-type configuration. This will depend on the
77  * target and the driver. For example a board may have its default setting
78  * as "AUTO" which causes it to autodetect an Ethernet cable. This should
79  * be described in the target's documentation.
80  *
81  * An application can override all target settings by implementing
82  * NetworkInterface::get_default_instance() themselves - the default
83  * definition is weak, and calls get_target_default_instance().
84  */
86 
87  /** Set network interface as default one.
88  */
89  virtual void set_as_default();
90 
91  /** Get the local MAC address.
92  *
93  * Provided MAC address is intended for info or debug purposes and
94  * may be not provided if the underlying network interface does not
95  * provide a MAC address.
96  *
97  * @return Null-terminated representation of the local MAC address
98  * or null if no MAC address is available.
99  */
100  virtual const char *get_mac_address();
101 
102  /** Get the local IP address
103  *
104  * @return Null-terminated representation of the local IP address
105  * or null if not yet connected
106  */
107  virtual const char *get_ip_address();
108 
109  /** Get the local network mask.
110  *
111  * @return Null-terminated representation of the local network mask
112  * or null if no network mask has been received.
113  */
114  virtual const char *get_netmask();
115 
116  /** Get the local gateway.
117  *
118  * @return Null-terminated representation of the local gateway
119  * or null if no network mask has been received.
120  */
121  virtual const char *get_gateway();
122 
123  /** Get the network interface name
124  *
125  * @return Null-terminated representation of the network interface name
126  * or null if interface not exists
127  */
128  virtual char *get_interface_name(char *interface_name);
129 
130  /** Configure this network interface to use a static IP address.
131  * Implicitly disables DHCP, which can be enabled in set_dhcp.
132  * Requires that the network is disconnected.
133  *
134  * @param ip_address Null-terminated representation of the local IP address
135  * @param netmask Null-terminated representation of the local network mask
136  * @param gateway Null-terminated representation of the local gateway
137  * @return NSAPI_ERROR_OK on success, negative error code on failure
138  */
139  virtual nsapi_error_t set_network(const char *ip_address, const char *netmask, const char *gateway);
140 
141  /** Enable or disable DHCP on connecting the network.
142  *
143  * Enabled by default unless a static IP address has been assigned. Requires
144  * that the network is disconnected.
145  *
146  * @param dhcp True to enable DHCP.
147  * @return NSAPI_ERROR_OK on success, negative error code on failure.
148  */
149  virtual nsapi_error_t set_dhcp(bool dhcp);
150 
151  /** Connect to a network.
152  *
153  * This blocks until connection is established, but asynchronous operation can be enabled
154  * by calling NetworkInterface::set_blocking(false).
155  *
156  * In asynchronous mode this starts the connection sequence and returns immediately.
157  * Status of the connection can then checked from NetworkInterface::get_connection_status()
158  * or from status callbacks.
159  *
160  * NetworkInterface internally handles reconnections until disconnect() is called.
161  *
162  * @return NSAPI_ERROR_OK if connection established in blocking mode.
163  * @return NSAPI_ERROR_OK if asynchronous operation started.
164  * @return NSAPI_ERROR_BUSY if asynchronous operation cannot be started.
165  Implementation guarantees event generation, which can be used as an
166  trigger to reissue the rejected request.
167  * @return NSAPI_ERROR_IS_CONNECTED if already connected.
168  * @return negative error code on failure.
169  */
170  virtual nsapi_error_t connect() = 0;
171 
172  /** Disconnect from the network
173  *
174  * This blocks until interface is disconnected, unless interface is set to
175  * asynchronous (non-blocking) mode by calling NetworkInterface::set_blocking(false).
176  *
177  * @return NSAPI_ERROR_OK on successfully disconnected in blocking mode.
178  * @return NSAPI_ERROR_OK if asynchronous operation started.
179  * @return NSAPI_ERROR_BUSY if asynchronous operation cannot be started.
180  Implementation guarantees event generation, which can be used as an
181  trigger to reissue the rejected request.
182  * @return NSAPI_ERROR_NO_CONNECTION if already disconnected.
183  * @return negative error code on failure.
184  */
185  virtual nsapi_error_t disconnect() = 0;
186 
187  /** Translate a hostname to an IP address with specific version using network interface name.
188  *
189  * The hostname may be either a domain name or an IP address. If the
190  * hostname is an IP address, no network transactions will be performed.
191  *
192  * If no stack-specific DNS resolution is provided, the hostname
193  * will be resolve using a UDP socket on the stack.
194  *
195  * @param host Hostname to resolve.
196  * @param address Pointer to a SocketAddress to store the result.
197  * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
198  * version is chosen by the stack (defaults to NSAPI_UNSPEC).
199  * @param interface_name Network interface name
200  * @return NSAPI_ERROR_OK on success, negative error code on failure.
201  */
202  virtual nsapi_error_t gethostbyname(const char *host,
203  SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC, const char *interface_name = NULL);
204 
205  /** Hostname translation callback (for use with gethostbyname_async()).
206  *
207  * Callback will be called after DNS resolution completes or a failure occurs.
208  *
209  * @note Callback should not take more than 10ms to execute, otherwise it might
210  * prevent underlying thread processing. A portable user of the callback
211  * should not make calls to network operations due to stack size limitations.
212  * The callback should not perform expensive operations such as socket recv/send
213  * calls or blocking operations.
214  *
215  * @param result NSAPI_ERROR_OK on success, negative error code on failure.
216  * @param address On success, destination for the host SocketAddress.
217  */
219 
220  /** Translate a hostname to an IP address (asynchronous) using network interface name.
221  *
222  * The hostname may be either a domain name or a dotted IP address. If the
223  * hostname is an IP address, no network transactions will be performed.
224  *
225  * If no stack-specific DNS resolution is provided, the hostname
226  * will be resolve using a UDP socket on the stack.
227  *
228  * Call is non-blocking. Result of the DNS operation is returned by the callback.
229  * If this function returns failure, callback will not be called. In case result
230  * is success (IP address was found from DNS cache), callback will be called
231  * before function returns.
232  *
233  * @param host Hostname to resolve.
234  * @param callback Callback that is called for result.
235  * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
236  * version is chosen by the stack (defaults to NSAPI_UNSPEC).
237  * @param interface_name Network interface name
238  * @return 0 on immediate success,
239  * negative error code on immediate failure or
240  * a positive unique id that represents the hostname translation operation
241  * and can be passed to cancel.
242  */
243  virtual nsapi_value_or_error_t gethostbyname_async(const char *host, hostbyname_cb_t callback, nsapi_version_t version = NSAPI_UNSPEC,
244  const char *interface_name = NULL);
245 
246  /** Cancel asynchronous hostname translation.
247  *
248  * When translation is cancelled, callback will not be called.
249  *
250  * @param id Unique id of the hostname translation operation (returned
251  * by gethostbyname_async)
252  * @return NSAPI_ERROR_OK on success, negative error code on failure.
253  */
255 
256  /** Add a domain name server to list of servers to query
257  *
258  * @param address Address for the dns host.
259  * @return NSAPI_ERROR_OK on success, negative error code on failure.
260  */
261  virtual nsapi_error_t add_dns_server(const SocketAddress &address, const char *interface_name);
262 
263  /** Register callback for status reporting.
264  *
265  * The specified status callback function will be called on status changes
266  * on the network. The parameters on the callback are the event type and
267  * event-type dependent reason parameter. Only one callback can be registered at a time.
268  *
269  * To unregister a callback call with status_cb parameter as a zero.
270  *
271  * *NOTE:* Any callbacks registered with this function will be overwritten if
272  * add_event_listener() API is used.
273  *
274  * @param status_cb The callback for status changes.
275  */
276  virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
277 
278  /** Add event listener for interface.
279  *
280  * This API allows multiple callback to be registered for a single interface.
281  * When first called, internal list of event handlers are created and registered to
282  * interface through attach() API.
283  *
284  * Application may only use attach() or add_event_listener() interface. Mixing usage
285  * of both leads to undefined behavior.
286  *
287  * @param status_cb The callback for status changes.
288  */
289  void add_event_listener(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
290 
291  /** Remove event listener from interface.
292  *
293  * Remove previously added callback from the handler list.
294  *
295  * @param status_cb The callback to unregister.
296  */
297  void remove_event_listener(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
298 
299  /** Get the connection status.
300  *
301  * @return The connection status (@see nsapi_types.h).
302  */
303  virtual nsapi_connection_status_t get_connection_status() const;
304 
305  /** Set asynchronous operation of connect() and disconnect() calls.
306  *
307  * By default, interfaces are in synchronous mode which means that
308  * connect() or disconnect() blocks until it reach the target state or requested operation fails.
309  *
310  * @param blocking Use true to set NetworkInterface in asynchronous mode.
311  * @return NSAPI_ERROR_OK on success
312  * @return NSAPI_ERROR_UNSUPPORTED if driver does not support asynchronous mode.
313  * @return negative error code on failure.
314  */
315  virtual nsapi_error_t set_blocking(bool blocking);
316 
317  /** Return pointer to an EthInterface.
318  * @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
319  */
321  {
322  return 0;
323  }
324 
325  /** Return pointer to a WiFiInterface.
326  * @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
327  */
329  {
330  return 0;
331  }
332 
333  /** Return pointer to a MeshInterface.
334  * @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
335  */
337  {
338  return 0;
339  }
340 
341  /** Return pointer to a CellularInterface.
342  * @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
343  * @deprecated CellularBase migrated to CellularInterface - use cellularInterface()
344  */
345  MBED_DEPRECATED_SINCE("mbed-os-5.12", "CellularBase migrated to CellularInterface - use cellularInterface()")
346  virtual CellularInterface *cellularBase() // virtual retained for binary compatibility
347  {
348  return 0;
349  }
350 
351  /** Return pointer to an EMACInterface.
352  * @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
353  */
355  {
356  return 0;
357  }
358 
359 #if !defined(DOXYGEN_ONLY)
360 
361 protected:
362  friend class InternetSocket;
363  friend class UDPSocket;
364  friend class TCPSocket;
365  friend class TCPServer;
366  friend class SocketAddress;
367  template <typename IF>
368  friend NetworkStack *nsapi_create_stack(IF *iface);
369 
370  /** Provide access to the NetworkStack object
371  *
372  * @return The underlying NetworkStack object
373  */
374  virtual NetworkStack *get_stack() = 0;
375 
376  /** Get the target's default network instance.
377  *
378  * This method can be overridden by the target. Default implementations
379  * are provided weakly by various subsystems as described in
380  * NetworkInterface::get_default_instance(), so targets should not
381  * need to override in simple cases.
382  *
383  * If a target has more elaborate interface selection, it can completely
384  * override this behavior by implementing
385  * NetworkInterface::get_target_default_instance() themselves, either
386  * unconditionally, or for a specific network-default-interface-type setting
387  *
388  * For example, a device with both Ethernet and Wi-fi could be set up its
389  * target so that:
390  * * DEVICE_EMAC is set, and it provides EMAC::get_default_instance(),
391  * which means EthernetInterface provides EthInterface::get_target_instance()
392  * based on that EMAC.
393  * * It provides WifiInterface::get_target_default_instance().
394  * * The core will route NetworkInterface::get_default_instance() to
395  * either of those if network-default-interface-type is set to
396  * ETHERNET or WIFI.
397  * * The board overrides NetworkInterface::get_target_default_instance()
398  * if network-default-interface-type is set to AUTO. This returns
399  * either EthInterface::get_default_instance() or WiFIInterface::get_default_instance()
400  * depending on a cable detection.
401  *
402  *
403  * performs the search described by get_default_instance.
404  */
405  static NetworkInterface *get_target_default_instance();
406 #endif //!defined(DOXYGEN_ONLY)
407 
408 public:
409  /** Set default parameters on an interface.
410  *
411  * A network interface instantiated directly or using calls such as
412  * WiFiInterface::get_default_instance() is initially unconfigured.
413  * This call can be used to set the default parameters that would
414  * have been set if the interface had been requested using
415  * NetworkInterface::get_default_instance() (see nsapi JSON
416  * configuration).
417  */
418  virtual void set_default_parameters();
419 
420  /** Return pointer to a CellularInterface.
421  * @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
422  */
424  {
425  return 0;
426  }
427 };
428 
429 #endif
430 
431 /** @}*/
Socket implementation that uses IP network stack.
Common interface between Wi-Fi devices.
Definition: WiFiInterface.h:31
virtual void attach(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb)
Register callback for status reporting.
virtual MeshInterface * meshInterface()
Return pointer to a MeshInterface.
Base class for DNS provider.
Definition: DNS.h:25
SocketAddress class.
NetworkStack * nsapi_create_stack(nsapi_stack_t *stack)
Convert a raw nsapi_stack_t object into a C++ NetworkStack object.
virtual EMACInterface * emacInterface()
Return pointer to an EMACInterface.
virtual char * get_interface_name(char *interface_name)
Get the network interface name.
NetworkStack class.
Definition: NetworkStack.h:40
virtual const char * get_gateway()
Get the local gateway.
virtual nsapi_error_t disconnect()=0
Disconnect from the network.
UDP socket implementation.
Definition: UDPSocket.h:31
virtual nsapi_error_t gethostbyname_async_cancel(int id)
Cancel asynchronous hostname translation.
EMACInterface class Implementation of the NetworkInterface for an EMAC-based driver.
Definition: EMACInterface.h:38
TCP socket server.
Definition: TCPServer.h:32
virtual void set_as_default()
Set network interface as default one.
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:95
virtual void set_default_parameters()
defined(DOXYGEN_ONLY)
Common interface that is shared between mesh hardware.
Definition: MeshInterface.h:29
virtual const char * get_mac_address()
Get the local MAC address.
virtual nsapi_error_t set_network(const char *ip_address, const char *netmask, const char *gateway)
Configure this network interface to use a static IP address.
virtual const char * get_ip_address()
Get the local IP address.
Callback< R()> callback(R(*func)()=0)
Create a callback class with type inferred from the arguments.
Definition: Callback.h:3848
signed int nsapi_value_or_error_t
Type used to represent either a value or error.
Definition: nsapi_types.h:113
virtual nsapi_connection_status_t get_connection_status() const
Get the connection status.
virtual WiFiInterface * wifiInterface()
Return pointer to a WiFiInterface.
SocketAddress class.
Definition: SocketAddress.h:35
TCP socket connection.
Definition: TCPSocket.h:32
Common interface that is shared between network devices.
virtual nsapi_error_t set_dhcp(bool dhcp)
Enable or disable DHCP on connecting the network.
void add_event_listener(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb)
Add event listener for interface.
virtual nsapi_error_t connect()=0
Connect to a network.
virtual nsapi_error_t add_dns_server(const SocketAddress &address, const char *interface_name)
Add a domain name server to list of servers to query.
void remove_event_listener(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb)
Remove event listener from interface.
virtual nsapi_error_t gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version=NSAPI_UNSPEC, const char *interface_name=NULL)
Translate a hostname to an IP address with specific version using network interface name...
virtual nsapi_value_or_error_t gethostbyname_async(const char *host, hostbyname_cb_t callback, nsapi_version_t version=NSAPI_UNSPEC, const char *interface_name=NULL)
Translate a hostname to an IP address (asynchronous) using network interface name.
Common interface that is shared between cellular interfaces.
virtual nsapi_error_t set_blocking(bool blocking)
Set asynchronous operation of connect() and disconnect() calls.
virtual CellularInterface * cellularInterface()
Return pointer to a CellularInterface.
virtual const char * get_netmask()
Get the local network mask.
mbed::Callback< void(nsapi_error_t result, SocketAddress *address)> hostbyname_cb_t
Hostname translation callback (for use with gethostbyname_async()).
Callback class based on template specialization.
Definition: Callback.h:39
Common interface between Ethernet hardware.
Definition: EthInterface.h:29
virtual CellularInterface * cellularBase()
Return pointer to a CellularInterface.
static NetworkInterface * get_default_instance()
Return the default network interface.
#define MBED_DEPRECATED_SINCE(D, M)
MBED_DEPRECATED("message string") Mark a function declaration as deprecated, if it used then a warnin...
virtual EthInterface * ethInterface()
Return pointer to an EthInterface.
Domain Name Service.
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.