Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
NetworkInterface.h
00001 /* 00002 * Copyright (c) 2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 /** @file NetworkInterface.h Network Interface base class */ 00018 /** @addtogroup netinterface 00019 * Network Interface classes 00020 * @{ */ 00021 00022 00023 #ifndef NETWORK_INTERFACE_H 00024 #define NETWORK_INTERFACE_H 00025 00026 #include "netsocket/nsapi_types.h" 00027 #include "netsocket/SocketAddress.h" 00028 #include "Callback.h" 00029 #include "DNS.h" 00030 00031 00032 // Predeclared classes 00033 class NetworkStack; 00034 class EthInterface; 00035 class WiFiInterface; 00036 class MeshInterface; 00037 class CellularInterface; 00038 class EMACInterface; 00039 class PPPInterface; 00040 00041 /** Common interface that is shared between network devices. 00042 * 00043 */ 00044 class NetworkInterface: public DNS { 00045 public: 00046 00047 virtual ~NetworkInterface(); 00048 00049 /** Return the default network interface. 00050 * 00051 * Returns the default network interface, as determined by JSON option 00052 * target.network-default-interface-type or other overrides. 00053 * 00054 * The type of the interface returned can be tested by calling ethInterface(), 00055 * wifiInterface(), meshInterface(), cellularInterface(), emacInterface() and checking 00056 * for NULL pointers. 00057 * 00058 * The default behavior is to return the default interface for the 00059 * interface type specified by target.network-default-interface-type. Targets 00060 * should set this in their targets.json to guide default selection, 00061 * and applications may override. 00062 * 00063 * The interface returned should be already configured for use such that its 00064 * connect() method works with no parameters. For connection types needing 00065 * configuration, settings should normally be obtained from JSON - the 00066 * settings for the core types are under the "nsapi" JSON config tree. 00067 * 00068 * The list of possible settings for default interface type is open-ended, 00069 * as is the number of possible providers. Core providers are: 00070 * 00071 * * ETHERNET: EthernetInterface, using default EMAC and OnboardNetworkStack 00072 * * MESH: ThreadInterface or LoWPANNDInterface, using default NanostackRfPhy 00073 * * CELLULAR: OnboardModemInterface 00074 * * WIFI: None - always provided by a specific class 00075 * 00076 * Specific drivers may be activated by other settings of the 00077 * default-network-interface-type configuration. This will depend on the 00078 * target and the driver. For example a board may have its default setting 00079 * as "AUTO" which causes it to autodetect an Ethernet cable. This should 00080 * be described in the target's documentation. 00081 * 00082 * An application can override all target settings by implementing 00083 * NetworkInterface::get_default_instance() themselves - the default 00084 * definition is weak, and calls get_target_default_instance(). 00085 */ 00086 static NetworkInterface *get_default_instance(); 00087 00088 /** Set network interface as default one. 00089 */ 00090 virtual void set_as_default(); 00091 00092 /** Get the local MAC address. 00093 * 00094 * Provided MAC address is intended for info or debug purposes and 00095 * may be not provided if the underlying network interface does not 00096 * provide a MAC address. 00097 * 00098 * @return Null-terminated representation of the local MAC address 00099 * or null if no MAC address is available. 00100 */ 00101 virtual const char *get_mac_address(); 00102 00103 /** Get the local IP address 00104 * 00105 * @param address SocketAddress representation of the local IP address 00106 * @retval NSAPI_ERROR_OK on success 00107 * @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported 00108 * @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid 00109 * @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack 00110 */ 00111 virtual nsapi_error_t get_ip_address(SocketAddress *address); 00112 00113 MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated") 00114 virtual const char *get_ip_address(); 00115 00116 /** Get the IPv6 link local address 00117 * 00118 * @param address SocketAddress representation of the link local IPv6 address 00119 * @retval NSAPI_ERROR_OK on success 00120 * @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported 00121 * @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid 00122 */ 00123 virtual nsapi_error_t get_ipv6_link_local_address(SocketAddress *address); 00124 00125 /** Get the local network mask. 00126 * 00127 * @param address SocketAddress representation of netmask 00128 * @retval NSAPI_ERROR_OK on success 00129 * @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported 00130 * @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid 00131 * @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack 00132 */ 00133 virtual nsapi_error_t get_netmask(SocketAddress *address); 00134 00135 MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated") 00136 virtual const char *get_netmask(); 00137 00138 /** Get the local gateway. 00139 * 00140 * @param address SocketAddress representation of gateway address 00141 * @retval NSAPI_ERROR_OK on success 00142 * @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported 00143 * @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid 00144 * @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack 00145 */ 00146 virtual nsapi_error_t get_gateway(SocketAddress *address); 00147 00148 MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated") 00149 virtual const char *get_gateway(); 00150 00151 /** Get the network interface name 00152 * 00153 * @return Null-terminated representation of the network interface name 00154 * or null if interface not exists 00155 */ 00156 virtual char *get_interface_name(char *interface_name); 00157 00158 /** Configure this network interface to use a static IP address. 00159 * Implicitly disables DHCP, which can be enabled in set_dhcp. 00160 * Requires that the network is disconnected. 00161 * 00162 * @param ip_address SocketAddress object containing the local IP address 00163 * @param netmask SocketAddress object containing the local network mask 00164 * @param gateway SocketAddress object containing the local gateway 00165 * @retval NSAPI_ERROR_OK on success 00166 * @retval NSAPI_ERROR_UNSUPPORTED if this function is unsupported 00167 */ 00168 virtual nsapi_error_t set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway); 00169 00170 MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated") 00171 virtual nsapi_error_t set_network(const char *ip_address, const char *netmask, const char *gateway); 00172 00173 /** Enable or disable DHCP on connecting the network. 00174 * 00175 * Enabled by default unless a static IP address has been assigned. Requires 00176 * that the network is disconnected. 00177 * 00178 * @param dhcp True to enable DHCP. 00179 * @retval NSAPI_ERROR_OK on success. 00180 * @retval NSAPI_ERROR_UNSUPPORTED if operation is not supported. 00181 */ 00182 virtual nsapi_error_t set_dhcp(bool dhcp); 00183 00184 /** Connect to a network. 00185 * 00186 * This blocks until connection is established, but asynchronous operation can be enabled 00187 * by calling NetworkInterface::set_blocking(false). 00188 * 00189 * In asynchronous mode this starts the connection sequence and returns immediately. 00190 * Status of the connection can then checked from NetworkInterface::get_connection_status() 00191 * or from status callbacks. 00192 * 00193 * NetworkInterface internally handles reconnections until disconnect() is called. 00194 * 00195 * @return NSAPI_ERROR_OK if connection established in blocking mode. 00196 * @return NSAPI_ERROR_OK if asynchronous operation started. 00197 * @return NSAPI_ERROR_BUSY if asynchronous operation cannot be started. 00198 Implementation guarantees event generation, which can be used as an 00199 trigger to reissue the rejected request. 00200 * @return NSAPI_ERROR_IS_CONNECTED if already connected. 00201 * @return negative error code on failure. 00202 */ 00203 virtual nsapi_error_t connect() = 0; 00204 00205 /** Disconnect from the network 00206 * 00207 * This blocks until interface is disconnected, unless interface is set to 00208 * asynchronous (non-blocking) mode by calling NetworkInterface::set_blocking(false). 00209 * 00210 * @return NSAPI_ERROR_OK on successfully disconnected in blocking mode. 00211 * @return NSAPI_ERROR_OK if asynchronous operation started. 00212 * @return NSAPI_ERROR_BUSY if asynchronous operation cannot be started. 00213 Implementation guarantees event generation, which can be used as an 00214 trigger to reissue the rejected request. 00215 * @return NSAPI_ERROR_NO_CONNECTION if already disconnected. 00216 * @return negative error code on failure. 00217 */ 00218 virtual nsapi_error_t disconnect() = 0; 00219 00220 /** Translate a hostname to an IP address with specific version using network interface name. 00221 * 00222 * The hostname may be either a domain name or an IP address. If the 00223 * hostname is an IP address, no network transactions will be performed. 00224 * 00225 * If no stack-specific DNS resolution is provided, the hostname 00226 * will be resolve using a UDP socket on the stack. 00227 * 00228 * @param host Hostname to resolve. 00229 * @param address Pointer to a SocketAddress to store the result. 00230 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates 00231 * version is chosen by the stack (defaults to NSAPI_UNSPEC). 00232 * @param interface_name Network interface name 00233 * @retval NSAPI_ERROR_OK on success 00234 * @retval int Negative error code on failure. 00235 * See @ref NetworkStack::gethostbyname 00236 */ 00237 virtual nsapi_error_t gethostbyname(const char *host, 00238 SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC , const char *interface_name = NULL); 00239 00240 /** Hostname translation callback (for use with gethostbyname_async()). 00241 * 00242 * Callback will be called after DNS resolution completes or a failure occurs. 00243 * 00244 * @note Callback should not take more than 10ms to execute, otherwise it might 00245 * prevent underlying thread processing. A portable user of the callback 00246 * should not make calls to network operations due to stack size limitations. 00247 * The callback should not perform expensive operations such as socket recv/send 00248 * calls or blocking operations. 00249 * 00250 * @param result NSAPI_ERROR_OK on success, negative error code on failure. 00251 * @param address On success, destination for the host SocketAddress. 00252 */ 00253 typedef mbed::Callback<void (nsapi_error_t result, SocketAddress *address)> hostbyname_cb_t; 00254 00255 /** Translate a hostname to an IP address (asynchronous) using network interface name. 00256 * 00257 * The hostname may be either a domain name or a dotted IP address. If the 00258 * hostname is an IP address, no network transactions will be performed. 00259 * 00260 * If no stack-specific DNS resolution is provided, the hostname 00261 * will be resolve using a UDP socket on the stack. 00262 * 00263 * Call is non-blocking. Result of the DNS operation is returned by the callback. 00264 * If this function returns failure, callback will not be called. In case result 00265 * is success (IP address was found from DNS cache), callback will be called 00266 * before function returns. 00267 * 00268 * @param host Hostname to resolve. 00269 * @param callback Callback that is called for result. 00270 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates 00271 * version is chosen by the stack (defaults to NSAPI_UNSPEC). 00272 * @param interface_name Network interface name 00273 * @return 0 on immediate success, 00274 * negative error code on immediate failure or 00275 * a positive unique id that represents the hostname translation operation 00276 * and can be passed to cancel. 00277 */ 00278 virtual nsapi_value_or_error_t gethostbyname_async(const char *host, hostbyname_cb_t callback, nsapi_version_t version = NSAPI_UNSPEC , 00279 const char *interface_name = NULL); 00280 00281 /** Cancel asynchronous hostname translation. 00282 * 00283 * When translation is cancelled, callback will not be called. 00284 * 00285 * @param id Unique id of the hostname translation operation (returned 00286 * by gethostbyname_async) 00287 * @return NSAPI_ERROR_OK on success, negative error code on failure. 00288 */ 00289 virtual nsapi_error_t gethostbyname_async_cancel(int id); 00290 00291 /** Add a domain name server to list of servers to query 00292 * 00293 * @param address Address for the dns host. 00294 * @return NSAPI_ERROR_OK on success, negative error code on failure. 00295 */ 00296 virtual nsapi_error_t add_dns_server(const SocketAddress &address, const char *interface_name); 00297 00298 /** Register callback for status reporting. 00299 * 00300 * The specified status callback function will be called on status changes 00301 * on the network. The parameters on the callback are the event type and 00302 * event-type dependent reason parameter. Only one callback can be registered at a time. 00303 * 00304 * To unregister a callback call with status_cb parameter as a zero. 00305 * 00306 * *NOTE:* Any callbacks registered with this function will be overwritten if 00307 * add_event_listener() API is used. 00308 * 00309 * @param status_cb The callback for status changes. 00310 */ 00311 virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb); 00312 00313 /** Add event listener for interface. 00314 * 00315 * This API allows multiple callback to be registered for a single interface. 00316 * When first called, internal list of event handlers are created and registered to 00317 * interface through attach() API. 00318 * 00319 * Application may only use attach() or add_event_listener() interface. Mixing usage 00320 * of both leads to undefined behavior. 00321 * 00322 * @param status_cb The callback for status changes. 00323 */ 00324 void add_event_listener(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb); 00325 00326 /** Remove event listener from interface. 00327 * 00328 * Remove previously added callback from the handler list. 00329 * 00330 * @param status_cb The callback to unregister. 00331 */ 00332 void remove_event_listener(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb); 00333 00334 /** Get the connection status. 00335 * 00336 * @return The connection status (@see nsapi_types.h). 00337 */ 00338 virtual nsapi_connection_status_t get_connection_status() const; 00339 00340 /** Set asynchronous operation of connect() and disconnect() calls. 00341 * 00342 * By default, interfaces are in synchronous mode which means that 00343 * connect() or disconnect() blocks until it reach the target state or requested operation fails. 00344 * 00345 * @param blocking Use false to set NetworkInterface in asynchronous mode. 00346 * @return NSAPI_ERROR_OK on success 00347 * @return NSAPI_ERROR_UNSUPPORTED if driver does not support asynchronous mode. 00348 * @return negative error code on failure. 00349 */ 00350 virtual nsapi_error_t set_blocking(bool blocking); 00351 00352 /** Return pointer to an EthInterface. 00353 * @return Pointer to requested interface type or NULL if this class doesn't implement the interface. 00354 */ 00355 virtual EthInterface *ethInterface() 00356 { 00357 return 0; 00358 } 00359 00360 /** Return pointer to a WiFiInterface. 00361 * @return Pointer to requested interface type or NULL if this class doesn't implement the interface. 00362 */ 00363 virtual WiFiInterface *wifiInterface() 00364 { 00365 return 0; 00366 } 00367 00368 /** Return pointer to a MeshInterface. 00369 * @return Pointer to requested interface type or NULL if this class doesn't implement the interface. 00370 */ 00371 virtual MeshInterface *meshInterface() 00372 { 00373 return 0; 00374 } 00375 00376 /** Return pointer to an EMACInterface. 00377 * @return Pointer to requested interface type or NULL if this class doesn't implement the interface. 00378 */ 00379 virtual EMACInterface *emacInterface() 00380 { 00381 return 0; 00382 } 00383 00384 /** Return pointer to a CellularInterface. 00385 * @return Pointer to requested interface type or NULL if this class doesn't implement the interface. 00386 */ 00387 virtual CellularInterface *cellularInterface() 00388 { 00389 return 0; 00390 } 00391 00392 #if !defined(DOXYGEN_ONLY) 00393 00394 protected: 00395 friend class InternetSocket; 00396 friend class UDPSocket; 00397 friend class TCPSocket; 00398 friend class TCPServer; 00399 friend class SocketAddress; 00400 template <typename IF> 00401 friend NetworkStack *nsapi_create_stack(IF *iface); 00402 00403 /** Provide access to the NetworkStack object 00404 * 00405 * @return The underlying NetworkStack object 00406 */ 00407 virtual NetworkStack *get_stack() = 0; 00408 00409 /** Get the target's default network instance. 00410 * 00411 * This method can be overridden by the target. Default implementations 00412 * are provided weakly by various subsystems as described in 00413 * NetworkInterface::get_default_instance(), so targets should not 00414 * need to override in simple cases. 00415 * 00416 * If a target has more elaborate interface selection, it can completely 00417 * override this behavior by implementing 00418 * NetworkInterface::get_target_default_instance() themselves, either 00419 * unconditionally, or for a specific network-default-interface-type setting 00420 * 00421 * For example, a device with both Ethernet and Wi-fi could be set up its 00422 * target so that: 00423 * * DEVICE_EMAC is set, and it provides EMAC::get_default_instance(), 00424 * which means EthernetInterface provides EthInterface::get_target_instance() 00425 * based on that EMAC. 00426 * * It provides WifiInterface::get_target_default_instance(). 00427 * * The core will route NetworkInterface::get_default_instance() to 00428 * either of those if network-default-interface-type is set to 00429 * ETHERNET or WIFI. 00430 * * The board overrides NetworkInterface::get_target_default_instance() 00431 * if network-default-interface-type is set to AUTO. This returns 00432 * either EthInterface::get_default_instance() or WiFIInterface::get_default_instance() 00433 * depending on a cable detection. 00434 * 00435 * 00436 * performs the search described by get_default_instance. 00437 */ 00438 static NetworkInterface *get_target_default_instance(); 00439 #endif //!defined(DOXYGEN_ONLY) 00440 00441 public: 00442 /** Set default parameters on an interface. 00443 * 00444 * A network interface instantiated directly or using calls such as 00445 * WiFiInterface::get_default_instance() is initially unconfigured. 00446 * This call can be used to set the default parameters that would 00447 * have been set if the interface had been requested using 00448 * NetworkInterface::get_default_instance() (see nsapi JSON 00449 * configuration). 00450 */ 00451 virtual void set_default_parameters(); 00452 }; 00453 00454 #endif 00455 00456 /** @}*/
Generated on Tue Jul 12 2022 13:54:37 by
