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