Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:380207fcb5c1 1 /* NetworkStack
borlanic 0:380207fcb5c1 2 * Copyright (c) 2015 ARM Limited
borlanic 0:380207fcb5c1 3 *
borlanic 0:380207fcb5c1 4 * Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:380207fcb5c1 5 * you may not use this file except in compliance with the License.
borlanic 0:380207fcb5c1 6 * You may obtain a copy of the License at
borlanic 0:380207fcb5c1 7 *
borlanic 0:380207fcb5c1 8 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:380207fcb5c1 9 *
borlanic 0:380207fcb5c1 10 * Unless required by applicable law or agreed to in writing, software
borlanic 0:380207fcb5c1 11 * distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:380207fcb5c1 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:380207fcb5c1 13 * See the License for the specific language governing permissions and
borlanic 0:380207fcb5c1 14 * limitations under the License.
borlanic 0:380207fcb5c1 15 */
borlanic 0:380207fcb5c1 16
borlanic 0:380207fcb5c1 17 #ifndef NETWORK_INTERFACE_H
borlanic 0:380207fcb5c1 18 #define NETWORK_INTERFACE_H
borlanic 0:380207fcb5c1 19
borlanic 0:380207fcb5c1 20 #include "netsocket/nsapi_types.h"
borlanic 0:380207fcb5c1 21 #include "netsocket/SocketAddress.h"
borlanic 0:380207fcb5c1 22 #include "Callback.h"
borlanic 0:380207fcb5c1 23
borlanic 0:380207fcb5c1 24 // Predeclared class
borlanic 0:380207fcb5c1 25 class NetworkStack;
borlanic 0:380207fcb5c1 26
borlanic 0:380207fcb5c1 27
borlanic 0:380207fcb5c1 28 /** NetworkInterface class
borlanic 0:380207fcb5c1 29 *
borlanic 0:380207fcb5c1 30 * Common interface that is shared between network devices
borlanic 0:380207fcb5c1 31 * @addtogroup netsocket
borlanic 0:380207fcb5c1 32 */
borlanic 0:380207fcb5c1 33 class NetworkInterface {
borlanic 0:380207fcb5c1 34 public:
borlanic 0:380207fcb5c1 35
borlanic 0:380207fcb5c1 36
borlanic 0:380207fcb5c1 37
borlanic 0:380207fcb5c1 38 virtual ~NetworkInterface() {};
borlanic 0:380207fcb5c1 39
borlanic 0:380207fcb5c1 40 /** Get the local MAC address
borlanic 0:380207fcb5c1 41 *
borlanic 0:380207fcb5c1 42 * Provided MAC address is intended for info or debug purposes and
borlanic 0:380207fcb5c1 43 * may not be provided if the underlying network interface does not
borlanic 0:380207fcb5c1 44 * provide a MAC address
borlanic 0:380207fcb5c1 45 *
borlanic 0:380207fcb5c1 46 * @return Null-terminated representation of the local MAC address
borlanic 0:380207fcb5c1 47 * or null if no MAC address is available
borlanic 0:380207fcb5c1 48 */
borlanic 0:380207fcb5c1 49 virtual const char *get_mac_address();
borlanic 0:380207fcb5c1 50
borlanic 0:380207fcb5c1 51 /** Get the local IP address
borlanic 0:380207fcb5c1 52 *
borlanic 0:380207fcb5c1 53 * @return Null-terminated representation of the local IP address
borlanic 0:380207fcb5c1 54 * or null if no IP address has been received
borlanic 0:380207fcb5c1 55 */
borlanic 0:380207fcb5c1 56 virtual const char *get_ip_address();
borlanic 0:380207fcb5c1 57
borlanic 0:380207fcb5c1 58 /** Get the local network mask
borlanic 0:380207fcb5c1 59 *
borlanic 0:380207fcb5c1 60 * @return Null-terminated representation of the local network mask
borlanic 0:380207fcb5c1 61 * or null if no network mask has been received
borlanic 0:380207fcb5c1 62 */
borlanic 0:380207fcb5c1 63 virtual const char *get_netmask();
borlanic 0:380207fcb5c1 64
borlanic 0:380207fcb5c1 65 /** Get the local gateway
borlanic 0:380207fcb5c1 66 *
borlanic 0:380207fcb5c1 67 * @return Null-terminated representation of the local gateway
borlanic 0:380207fcb5c1 68 * or null if no network mask has been received
borlanic 0:380207fcb5c1 69 */
borlanic 0:380207fcb5c1 70 virtual const char *get_gateway();
borlanic 0:380207fcb5c1 71
borlanic 0:380207fcb5c1 72 /** Set a static IP address
borlanic 0:380207fcb5c1 73 *
borlanic 0:380207fcb5c1 74 * Configures this network interface to use a static IP address.
borlanic 0:380207fcb5c1 75 * Implicitly disables DHCP, which can be enabled in set_dhcp.
borlanic 0:380207fcb5c1 76 * Requires that the network is disconnected.
borlanic 0:380207fcb5c1 77 *
borlanic 0:380207fcb5c1 78 * @param ip_address Null-terminated representation of the local IP address
borlanic 0:380207fcb5c1 79 * @param netmask Null-terminated representation of the local network mask
borlanic 0:380207fcb5c1 80 * @param gateway Null-terminated representation of the local gateway
borlanic 0:380207fcb5c1 81 * @return 0 on success, negative error code on failure
borlanic 0:380207fcb5c1 82 */
borlanic 0:380207fcb5c1 83 virtual nsapi_error_t set_network(
borlanic 0:380207fcb5c1 84 const char *ip_address, const char *netmask, const char *gateway);
borlanic 0:380207fcb5c1 85
borlanic 0:380207fcb5c1 86 /** Enable or disable DHCP on the network
borlanic 0:380207fcb5c1 87 *
borlanic 0:380207fcb5c1 88 * Enables DHCP on connecting the network. Defaults to enabled unless
borlanic 0:380207fcb5c1 89 * a static IP address has been assigned. Requires that the network is
borlanic 0:380207fcb5c1 90 * disconnected.
borlanic 0:380207fcb5c1 91 *
borlanic 0:380207fcb5c1 92 * @param dhcp True to enable DHCP
borlanic 0:380207fcb5c1 93 * @return 0 on success, negative error code on failure
borlanic 0:380207fcb5c1 94 */
borlanic 0:380207fcb5c1 95 virtual nsapi_error_t set_dhcp(bool dhcp);
borlanic 0:380207fcb5c1 96
borlanic 0:380207fcb5c1 97 /** Start the interface
borlanic 0:380207fcb5c1 98 *
borlanic 0:380207fcb5c1 99 * @return 0 on success, negative error code on failure
borlanic 0:380207fcb5c1 100 */
borlanic 0:380207fcb5c1 101 virtual nsapi_error_t connect() = 0;
borlanic 0:380207fcb5c1 102
borlanic 0:380207fcb5c1 103 /** Stop the interface
borlanic 0:380207fcb5c1 104 *
borlanic 0:380207fcb5c1 105 * @return 0 on success, negative error code on failure
borlanic 0:380207fcb5c1 106 */
borlanic 0:380207fcb5c1 107 virtual nsapi_error_t disconnect() = 0;
borlanic 0:380207fcb5c1 108
borlanic 0:380207fcb5c1 109 /** Translates a hostname to an IP address with specific version
borlanic 0:380207fcb5c1 110 *
borlanic 0:380207fcb5c1 111 * The hostname may be either a domain name or an IP address. If the
borlanic 0:380207fcb5c1 112 * hostname is an IP address, no network transactions will be performed.
borlanic 0:380207fcb5c1 113 *
borlanic 0:380207fcb5c1 114 * If no stack-specific DNS resolution is provided, the hostname
borlanic 0:380207fcb5c1 115 * will be resolve using a UDP socket on the stack.
borlanic 0:380207fcb5c1 116 *
borlanic 0:380207fcb5c1 117 * @param address Destination for the host SocketAddress
borlanic 0:380207fcb5c1 118 * @param host Hostname to resolve
borlanic 0:380207fcb5c1 119 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
borlanic 0:380207fcb5c1 120 * version is chosen by the stack (defaults to NSAPI_UNSPEC)
borlanic 0:380207fcb5c1 121 * @return 0 on success, negative error code on failure
borlanic 0:380207fcb5c1 122 */
borlanic 0:380207fcb5c1 123 virtual nsapi_error_t gethostbyname(const char *host,
borlanic 0:380207fcb5c1 124 SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC);
borlanic 0:380207fcb5c1 125
borlanic 0:380207fcb5c1 126 /** Add a domain name server to list of servers to query
borlanic 0:380207fcb5c1 127 *
borlanic 0:380207fcb5c1 128 * @param address Destination for the host address
borlanic 0:380207fcb5c1 129 * @return 0 on success, negative error code on failure
borlanic 0:380207fcb5c1 130 */
borlanic 0:380207fcb5c1 131 virtual nsapi_error_t add_dns_server(const SocketAddress &address);
borlanic 0:380207fcb5c1 132
borlanic 0:380207fcb5c1 133 /** Register callback for status reporting
borlanic 0:380207fcb5c1 134 *
borlanic 0:380207fcb5c1 135 * The specified status callback function will be called on status changes
borlanic 0:380207fcb5c1 136 * on the network. The parameters on the callback are the event type and
borlanic 0:380207fcb5c1 137 * event-type dependent reason parameter.
borlanic 0:380207fcb5c1 138 *
borlanic 0:380207fcb5c1 139 * @param status_cb The callback for status changes
borlanic 0:380207fcb5c1 140 */
borlanic 0:380207fcb5c1 141 virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
borlanic 0:380207fcb5c1 142
borlanic 0:380207fcb5c1 143 /** Get the connection status
borlanic 0:380207fcb5c1 144 *
borlanic 0:380207fcb5c1 145 * @return The connection status according to ConnectionStatusType
borlanic 0:380207fcb5c1 146 */
borlanic 0:380207fcb5c1 147 virtual nsapi_connection_status_t get_connection_status() const;
borlanic 0:380207fcb5c1 148
borlanic 0:380207fcb5c1 149 /** Set blocking status of connect() which by default should be blocking
borlanic 0:380207fcb5c1 150 *
borlanic 0:380207fcb5c1 151 * @param blocking true if connect is blocking
borlanic 0:380207fcb5c1 152 * @return 0 on success, negative error code on failure
borlanic 0:380207fcb5c1 153 */
borlanic 0:380207fcb5c1 154 virtual nsapi_error_t set_blocking(bool blocking);
borlanic 0:380207fcb5c1 155
borlanic 0:380207fcb5c1 156
borlanic 0:380207fcb5c1 157 protected:
borlanic 0:380207fcb5c1 158 friend class Socket;
borlanic 0:380207fcb5c1 159 friend class UDPSocket;
borlanic 0:380207fcb5c1 160 friend class TCPSocket;
borlanic 0:380207fcb5c1 161 friend class TCPServer;
borlanic 0:380207fcb5c1 162 friend class SocketAddress;
borlanic 0:380207fcb5c1 163 template <typename IF>
borlanic 0:380207fcb5c1 164 friend NetworkStack *nsapi_create_stack(IF *iface);
borlanic 0:380207fcb5c1 165
borlanic 0:380207fcb5c1 166 /** Provide access to the NetworkStack object
borlanic 0:380207fcb5c1 167 *
borlanic 0:380207fcb5c1 168 * @return The underlying NetworkStack object
borlanic 0:380207fcb5c1 169 */
borlanic 0:380207fcb5c1 170 virtual NetworkStack *get_stack() = 0;
borlanic 0:380207fcb5c1 171 };
borlanic 0:380207fcb5c1 172
borlanic 0:380207fcb5c1 173
borlanic 0:380207fcb5c1 174 #endif