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