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  MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
114  virtual const char *get_ip_address();
115 
116  /** Get the IPv6 link local address
117  *
118  * @param address SocketAddress representation of the link local IPv6 address
119  * @retval NSAPI_ERROR_OK on success
120  * @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
121  * @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
122  */
124 
125  /** Get the local network mask.
126  *
127  * @param address SocketAddress representation of netmask
128  * @retval NSAPI_ERROR_OK on success
129  * @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
130  * @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
131  * @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack
132  */
133  virtual nsapi_error_t get_netmask(SocketAddress *address);
134 
135  MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
136  virtual const char *get_netmask();
137 
138  /** Get the local gateway.
139  *
140  * @param address SocketAddress representation of gateway address
141  * @retval NSAPI_ERROR_OK on success
142  * @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
143  * @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
144  * @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack
145  */
146  virtual nsapi_error_t get_gateway(SocketAddress *address);
147 
148  MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
149  virtual const char *get_gateway();
150 
151  /** Get the network interface name
152  *
153  * @return Null-terminated representation of the network interface name
154  * or null if interface not exists
155  */
156  virtual char *get_interface_name(char *interface_name);
157 
158  /** Configure this network interface to use a static IP address.
159  * Implicitly disables DHCP, which can be enabled in set_dhcp.
160  * Requires that the network is disconnected.
161  *
162  * @param ip_address SocketAddress object containing the local IP address
163  * @param netmask SocketAddress object containing the local network mask
164  * @param gateway SocketAddress object containing the local gateway
165  * @return NSAPI_ERROR_OK on success, negative error code on failure
166  */
167  virtual nsapi_error_t set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway);
168 
169  MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
170  virtual nsapi_error_t set_network(const char *ip_address, const char *netmask, const char *gateway);
171 
172  /** Enable or disable DHCP on connecting the network.
173  *
174  * Enabled by default unless a static IP address has been assigned. Requires
175  * that the network is disconnected.
176  *
177  * @param dhcp True to enable DHCP.
178  * @return NSAPI_ERROR_OK on success, negative error code on failure.
179  */
180  virtual nsapi_error_t set_dhcp(bool dhcp);
181 
182  /** Connect to a network.
183  *
184  * This blocks until connection is established, but asynchronous operation can be enabled
185  * by calling NetworkInterface::set_blocking(false).
186  *
187  * In asynchronous mode this starts the connection sequence and returns immediately.
188  * Status of the connection can then checked from NetworkInterface::get_connection_status()
189  * or from status callbacks.
190  *
191  * NetworkInterface internally handles reconnections until disconnect() is called.
192  *
193  * @return NSAPI_ERROR_OK if connection established in blocking mode.
194  * @return NSAPI_ERROR_OK if asynchronous operation started.
195  * @return NSAPI_ERROR_BUSY if asynchronous operation cannot be started.
196  Implementation guarantees event generation, which can be used as an
197  trigger to reissue the rejected request.
198  * @return NSAPI_ERROR_IS_CONNECTED if already connected.
199  * @return negative error code on failure.
200  */
201  virtual nsapi_error_t connect() = 0;
202 
203  /** Disconnect from the network
204  *
205  * This blocks until interface is disconnected, unless interface is set to
206  * asynchronous (non-blocking) mode by calling NetworkInterface::set_blocking(false).
207  *
208  * @return NSAPI_ERROR_OK on successfully disconnected in blocking mode.
209  * @return NSAPI_ERROR_OK if asynchronous operation started.
210  * @return NSAPI_ERROR_BUSY if asynchronous operation cannot be started.
211  Implementation guarantees event generation, which can be used as an
212  trigger to reissue the rejected request.
213  * @return NSAPI_ERROR_NO_CONNECTION if already disconnected.
214  * @return negative error code on failure.
215  */
216  virtual nsapi_error_t disconnect() = 0;
217 
218  /** Translate a hostname to an IP address with specific version using network interface name.
219  *
220  * The hostname may be either a domain name or an IP address. If the
221  * hostname is an IP address, no network transactions will be performed.
222  *
223  * If no stack-specific DNS resolution is provided, the hostname
224  * will be resolve using a UDP socket on the stack.
225  *
226  * @param host Hostname to resolve.
227  * @param address Pointer to a SocketAddress to store the result.
228  * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
229  * version is chosen by the stack (defaults to NSAPI_UNSPEC).
230  * @param interface_name Network interface name
231  * @return NSAPI_ERROR_OK on success, negative error code on failure.
232  */
233  virtual nsapi_error_t gethostbyname(const char *host,
234  SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC, const char *interface_name = NULL);
235 
236  /** Hostname translation callback (for use with gethostbyname_async()).
237  *
238  * Callback will be called after DNS resolution completes or a failure occurs.
239  *
240  * @note Callback should not take more than 10ms to execute, otherwise it might
241  * prevent underlying thread processing. A portable user of the callback
242  * should not make calls to network operations due to stack size limitations.
243  * The callback should not perform expensive operations such as socket recv/send
244  * calls or blocking operations.
245  *
246  * @param result NSAPI_ERROR_OK on success, negative error code on failure.
247  * @param address On success, destination for the host SocketAddress.
248  */
250 
251  /** Translate a hostname to an IP address (asynchronous) using network interface name.
252  *
253  * The hostname may be either a domain name or a dotted IP address. If the
254  * hostname is an IP address, no network transactions will be performed.
255  *
256  * If no stack-specific DNS resolution is provided, the hostname
257  * will be resolve using a UDP socket on the stack.
258  *
259  * Call is non-blocking. Result of the DNS operation is returned by the callback.
260  * If this function returns failure, callback will not be called. In case result
261  * is success (IP address was found from DNS cache), callback will be called
262  * before function returns.
263  *
264  * @param host Hostname to resolve.
265  * @param callback Callback that is called for result.
266  * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
267  * version is chosen by the stack (defaults to NSAPI_UNSPEC).
268  * @param interface_name Network interface name
269  * @return 0 on immediate success,
270  * negative error code on immediate failure or
271  * a positive unique id that represents the hostname translation operation
272  * and can be passed to cancel.
273  */
274  virtual nsapi_value_or_error_t gethostbyname_async(const char *host, hostbyname_cb_t callback, nsapi_version_t version = NSAPI_UNSPEC,
275  const char *interface_name = NULL);
276 
277  /** Cancel asynchronous hostname translation.
278  *
279  * When translation is cancelled, callback will not be called.
280  *
281  * @param id Unique id of the hostname translation operation (returned
282  * by gethostbyname_async)
283  * @return NSAPI_ERROR_OK on success, negative error code on failure.
284  */
286 
287  /** Add a domain name server to list of servers to query
288  *
289  * @param address Address for the dns host.
290  * @return NSAPI_ERROR_OK on success, negative error code on failure.
291  */
292  virtual nsapi_error_t add_dns_server(const SocketAddress &address, const char *interface_name);
293 
294  /** Register callback for status reporting.
295  *
296  * The specified status callback function will be called on status changes
297  * on the network. The parameters on the callback are the event type and
298  * event-type dependent reason parameter. Only one callback can be registered at a time.
299  *
300  * To unregister a callback call with status_cb parameter as a zero.
301  *
302  * *NOTE:* Any callbacks registered with this function will be overwritten if
303  * add_event_listener() API is used.
304  *
305  * @param status_cb The callback for status changes.
306  */
307  virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
308 
309  /** Add event listener for interface.
310  *
311  * This API allows multiple callback to be registered for a single interface.
312  * When first called, internal list of event handlers are created and registered to
313  * interface through attach() API.
314  *
315  * Application may only use attach() or add_event_listener() interface. Mixing usage
316  * of both leads to undefined behavior.
317  *
318  * @param status_cb The callback for status changes.
319  */
320  void add_event_listener(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
321 
322  /** Remove event listener from interface.
323  *
324  * Remove previously added callback from the handler list.
325  *
326  * @param status_cb The callback to unregister.
327  */
328  void remove_event_listener(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
329 
330  /** Get the connection status.
331  *
332  * @return The connection status (@see nsapi_types.h).
333  */
334  virtual nsapi_connection_status_t get_connection_status() const;
335 
336  /** Set asynchronous operation of connect() and disconnect() calls.
337  *
338  * By default, interfaces are in synchronous mode which means that
339  * connect() or disconnect() blocks until it reach the target state or requested operation fails.
340  *
341  * @param blocking Use false to set NetworkInterface in asynchronous mode.
342  * @return NSAPI_ERROR_OK on success
343  * @return NSAPI_ERROR_UNSUPPORTED if driver does not support asynchronous mode.
344  * @return negative error code on failure.
345  */
346  virtual nsapi_error_t set_blocking(bool blocking);
347 
348  /** Return pointer to an EthInterface.
349  * @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
350  */
352  {
353  return 0;
354  }
355 
356  /** Return pointer to a WiFiInterface.
357  * @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
358  */
360  {
361  return 0;
362  }
363 
364  /** Return pointer to a MeshInterface.
365  * @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
366  */
368  {
369  return 0;
370  }
371 
372  /** Return pointer to a CellularInterface.
373  * @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
374  * @deprecated CellularBase migrated to CellularInterface - use cellularInterface()
375  */
376  MBED_DEPRECATED_SINCE("mbed-os-5.12", "CellularBase migrated to CellularInterface - use cellularInterface()")
377  virtual CellularInterface *cellularBase() // virtual retained for binary compatibility
378  {
379  return 0;
380  }
381 
382  /** Return pointer to an EMACInterface.
383  * @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
384  */
386  {
387  return 0;
388  }
389 
390 #if !defined(DOXYGEN_ONLY)
391 
392 protected:
393  friend class InternetSocket;
394  friend class UDPSocket;
395  friend class TCPSocket;
396  friend class TCPServer;
397  friend class SocketAddress;
398  template <typename IF>
399  friend NetworkStack *nsapi_create_stack(IF *iface);
400 
401  /** Provide access to the NetworkStack object
402  *
403  * @return The underlying NetworkStack object
404  */
405  virtual NetworkStack *get_stack() = 0;
406 
407  /** Get the target's default network instance.
408  *
409  * This method can be overridden by the target. Default implementations
410  * are provided weakly by various subsystems as described in
411  * NetworkInterface::get_default_instance(), so targets should not
412  * need to override in simple cases.
413  *
414  * If a target has more elaborate interface selection, it can completely
415  * override this behavior by implementing
416  * NetworkInterface::get_target_default_instance() themselves, either
417  * unconditionally, or for a specific network-default-interface-type setting
418  *
419  * For example, a device with both Ethernet and Wi-fi could be set up its
420  * target so that:
421  * * DEVICE_EMAC is set, and it provides EMAC::get_default_instance(),
422  * which means EthernetInterface provides EthInterface::get_target_instance()
423  * based on that EMAC.
424  * * It provides WifiInterface::get_target_default_instance().
425  * * The core will route NetworkInterface::get_default_instance() to
426  * either of those if network-default-interface-type is set to
427  * ETHERNET or WIFI.
428  * * The board overrides NetworkInterface::get_target_default_instance()
429  * if network-default-interface-type is set to AUTO. This returns
430  * either EthInterface::get_default_instance() or WiFIInterface::get_default_instance()
431  * depending on a cable detection.
432  *
433  *
434  * performs the search described by get_default_instance.
435  */
436  static NetworkInterface *get_target_default_instance();
437 #endif //!defined(DOXYGEN_ONLY)
438 
439 public:
440  /** Set default parameters on an interface.
441  *
442  * A network interface instantiated directly or using calls such as
443  * WiFiInterface::get_default_instance() is initially unconfigured.
444  * This call can be used to set the default parameters that would
445  * have been set if the interface had been requested using
446  * NetworkInterface::get_default_instance() (see nsapi JSON
447  * configuration).
448  */
449  virtual void set_default_parameters();
450 
451  /** Return pointer to a CellularInterface.
452  * @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
453  */
455  {
456  return 0;
457  }
458 };
459 
460 #endif
461 
462 /** @}*/
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.
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:40
virtual nsapi_error_t disconnect()=0
Disconnect from the network.
UDP socket implementation.
Definition: UDPSocket.h:33
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.
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.
Callback< R(ArgTs...)> callback(R(*func)(ArgTs...)=0)
Create a callback class with type inferred from the arguments.
Definition: Callback.h:709
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.
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_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.
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.
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.
#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.
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.