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: mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510
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 "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 * @param func Function to call on state change 00167 */ 00168 void attach(mbed::Callback<void()> func); 00169 00170 /** Register a callback on state change of the socket 00171 * 00172 * The specified callback will be called on state changes such as when 00173 * the socket can recv/send/accept successfully and on when an error 00174 * occurs. The callback may also be called spuriously without reason. 00175 * 00176 * The callback may be called in an interrupt context and should not 00177 * perform expensive operations such as recv/send calls. 00178 * 00179 * @param obj Pointer to object to call method on 00180 * @param method Method to call on state change 00181 * 00182 * @deprecated 00183 * The attach function does not support cv-qualifiers. Replaced by 00184 * attach(callback(obj, method)). 00185 */ 00186 template <typename T, typename M> 00187 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00188 "The attach function does not support cv-qualifiers. Replaced by " 00189 "attach(callback(obj, method)).") 00190 void attach(T *obj, M method) { 00191 attach(mbed::callback(obj, method)); 00192 } 00193 00194 protected: 00195 Socket(); 00196 virtual nsapi_protocol_t get_proto() = 0; 00197 virtual void event() = 0; 00198 00199 NetworkStack *_stack; 00200 nsapi_socket_t _socket; 00201 uint32_t _timeout; 00202 mbed::Callback<void()> _event; 00203 mbed::Callback<void()> _callback; 00204 rtos::Mutex _lock; 00205 }; 00206 00207 00208 #endif 00209 00210 /** @}*/
Generated on Tue Jul 12 2022 11:02:52 by
