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