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.
Socket.h
00001 00002 /** \addtogroup netsocket */ 00003 /** @{*/ 00004 /* Socket 00005 * Copyright (c) 2015 ARM Limited 00006 * 00007 * Licensed under the Apache License, Version 2.0 (the "License"); 00008 * you may not use this file except in compliance with the License. 00009 * You may obtain a copy of the License at 00010 * 00011 * http://www.apache.org/licenses/LICENSE-2.0 00012 * 00013 * Unless required by applicable law or agreed to in writing, software 00014 * distributed under the License is distributed on an "AS IS" BASIS, 00015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00016 * See the License for the specific language governing permissions and 00017 * limitations under the License. 00018 */ 00019 00020 #ifndef SOCKET_H 00021 #define SOCKET_H 00022 00023 #include "netsocket/SocketAddress.h" 00024 #include "netsocket/NetworkStack.h" 00025 #include "rtos/Mutex.h" 00026 #include "Callback.h" 00027 #include "mbed_toolchain.h" 00028 00029 00030 /** Abstract socket class 00031 */ 00032 class Socket { 00033 public: 00034 /** Destroy a socket 00035 * 00036 * Closes socket if the socket is still open 00037 */ 00038 virtual ~Socket() {} 00039 00040 /** Opens a socket 00041 * 00042 * Creates a network socket on the network stack of the given 00043 * network interface. Not needed if stack is passed to the 00044 * socket's constructor. 00045 * 00046 * @param stack Network stack as target for socket 00047 * @return 0 on success, negative error code on failure 00048 */ 00049 nsapi_error_t open(NetworkStack *stack); 00050 00051 template <typename S> 00052 nsapi_error_t open(S *stack) { 00053 return open(nsapi_create_stack(stack)); 00054 } 00055 00056 /** Close the socket 00057 * 00058 * Closes any open connection and deallocates any memory associated 00059 * with the socket. Called from destructor if socket is not closed. 00060 * 00061 * @return 0 on success, negative error code on failure 00062 */ 00063 nsapi_error_t close(); 00064 00065 /** Bind a specific address to a socket 00066 * 00067 * Binding a socket specifies the address and port on which to recieve 00068 * data. 00069 * 00070 * @param port Local port to bind 00071 * @return 0 on success, negative error code on failure. 00072 */ 00073 nsapi_error_t bind(uint16_t port); 00074 00075 /** Bind a specific address to a socket 00076 * 00077 * Binding a socket specifies the address and port on which to recieve 00078 * data. If the IP address is zeroed, only the port is bound. 00079 * 00080 * @param address Null-terminated local address to bind 00081 * @param port Local port to bind 00082 * @return 0 on success, negative error code on failure. 00083 */ 00084 nsapi_error_t bind(const char *address, uint16_t port); 00085 00086 /** Bind a specific address to a socket 00087 * 00088 * Binding a socket specifies the address and port on which to recieve 00089 * data. If the IP address is zeroed, only the port is bound. 00090 * 00091 * @param address Local address to bind 00092 * @return 0 on success, negative error code on failure. 00093 */ 00094 nsapi_error_t bind(const SocketAddress &address); 00095 00096 /** Set blocking or non-blocking mode of the socket 00097 * 00098 * Initially all sockets are in blocking mode. In non-blocking mode 00099 * blocking operations such as send/recv/accept return 00100 * NSAPI_ERROR_WOULD_BLOCK if they can not continue. 00101 * 00102 * set_blocking(false) is equivalent to set_timeout(-1) 00103 * set_blocking(true) is equivalent to set_timeout(0) 00104 * 00105 * @param blocking true for blocking mode, false for non-blocking mode. 00106 */ 00107 void set_blocking(bool blocking); 00108 00109 /** Set timeout on blocking socket operations 00110 * 00111 * Initially all sockets have unbounded timeouts. NSAPI_ERROR_WOULD_BLOCK 00112 * is returned if a blocking operation takes longer than the specified 00113 * timeout. A timeout of 0 removes the timeout from the socket. A negative 00114 * value give the socket an unbounded timeout. 00115 * 00116 * set_timeout(0) is equivalent to set_blocking(false) 00117 * set_timeout(-1) is equivalent to set_blocking(true) 00118 * 00119 * @param timeout Timeout in milliseconds 00120 */ 00121 void set_timeout(int timeout); 00122 00123 /* Set socket options 00124 * 00125 * setsockopt allows an application to pass stack-specific options 00126 * to the underlying stack using stack-specific level and option names, 00127 * or to request generic options using levels from nsapi_socket_level_t. 00128 * 00129 * For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned 00130 * and the socket is unmodified. 00131 * 00132 * @param level Stack-specific protocol level or nsapi_socket_level_t 00133 * @param optname Level-specific option name 00134 * @param optval Option value 00135 * @param optlen Length of the option value 00136 * @return 0 on success, negative error code on failure 00137 */ 00138 nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen); 00139 00140 /* Get socket options 00141 * 00142 * getsockopt allows an application to retrieve stack-specific options 00143 * from the underlying stack using stack-specific level and option names, 00144 * or to request generic options using levels from nsapi_socket_level_t. 00145 * 00146 * For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned 00147 * and the socket is unmodified. 00148 * 00149 * @param level Stack-specific protocol level or nsapi_socket_level_t 00150 * @param optname Level-specific option name 00151 * @param optval Destination for option value 00152 * @param optlen Length of the option value 00153 * @return 0 on success, negative error code on failure 00154 */ 00155 nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen); 00156 00157 /** Register a callback on state change of the socket 00158 * 00159 * The specified callback will be called on state changes such as when 00160 * the socket can recv/send/accept successfully and on when an error 00161 * occurs. The callback may also be called spuriously without reason. 00162 * 00163 * The callback may be called in an interrupt context and should not 00164 * perform expensive operations such as recv/send calls. 00165 * 00166 * Note! This is not intended as a replacement for a poll or attach-like 00167 * asynchronous api, but rather as a building block for constructing 00168 * such functionality. The exact timing of when the registered function 00169 * is called is not guaranteed and susceptible to change. 00170 * 00171 * @param func Function to call on state change 00172 */ 00173 void sigio(mbed::Callback<void()> func); 00174 00175 /** Register a callback on state change of the socket 00176 * 00177 * @see Socket::sigio 00178 * @deprecated 00179 * The behaviour of Socket::attach differs from other attach functions in 00180 * mbed OS and has been known to cause confusion. Replaced by Socket::sigio. 00181 */ 00182 MBED_DEPRECATED_SINCE("mbed-os-5.4", 00183 "The behaviour of Socket::attach differs from other attach functions in " 00184 "mbed OS and has been known to cause confusion. Replaced by Socket::sigio.") 00185 void attach(mbed::Callback<void()> func); 00186 00187 /** Register a callback on state change of the socket 00188 * 00189 * @see Socket::sigio 00190 * @deprecated 00191 * The attach function does not support cv-qualifiers. Replaced by 00192 * attach(callback(obj, method)). 00193 */ 00194 template <typename T, typename M> 00195 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00196 "The attach function does not support cv-qualifiers. Replaced by " 00197 "attach(callback(obj, method)).") 00198 void attach(T *obj, M method) { 00199 attach(mbed::callback(obj, method)); 00200 } 00201 00202 protected: 00203 Socket(); 00204 virtual nsapi_protocol_t get_proto() = 0; 00205 virtual void event() = 0; 00206 00207 NetworkStack *_stack; 00208 nsapi_socket_t _socket; 00209 uint32_t _timeout; 00210 mbed::Callback<void()> _event; 00211 mbed::Callback<void()> _callback; 00212 rtos::Mutex _lock; 00213 }; 00214 00215 00216 #endif 00217 00218 /** @}*/
Generated on Tue Jul 12 2022 20:03:23 by
