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