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.
Fork of mbed-os by
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 int open(NetworkStack *stack); 00050 00051 template <typename S> 00052 int 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 int 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 int 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 int 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 int 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 stack-specific socket options 00124 * 00125 * The setsockopt allow an application to pass stack-specific hints 00126 * to the underlying stack. For unsupported options, 00127 * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified. 00128 * 00129 * @param level Stack-specific protocol level 00130 * @param optname Stack-specific option identifier 00131 * @param optval Option value 00132 * @param optlen Length of the option value 00133 * @return 0 on success, negative error code on failure 00134 */ 00135 int setsockopt(int level, int optname, const void *optval, unsigned optlen); 00136 00137 /* Get stack-specific socket options 00138 * 00139 * The getstackopt allow an application to retrieve stack-specific hints 00140 * from the underlying stack. For unsupported options, 00141 * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified. 00142 * 00143 * @param level Stack-specific protocol level 00144 * @param optname Stack-specific option identifier 00145 * @param optval Destination for option value 00146 * @param optlen Length of the option value 00147 * @return 0 on success, negative error code on failure 00148 */ 00149 int getsockopt(int level, int optname, void *optval, unsigned *optlen); 00150 00151 /** Register a callback on state change of the socket 00152 * 00153 * The specified callback will be called on state changes such as when 00154 * the socket can recv/send/accept successfully and on when an error 00155 * occurs. The callback may also be called spuriously without reason. 00156 * 00157 * The callback may be called in an interrupt context and should not 00158 * perform expensive operations such as recv/send calls. 00159 * 00160 * @param func Function to call on state change 00161 */ 00162 void attach(mbed::Callback<void()> func); 00163 00164 /** Register a callback on state change of the socket 00165 * 00166 * The specified callback will be called on state changes such as when 00167 * the socket can recv/send/accept successfully and on when an error 00168 * occurs. The callback may also be called spuriously without reason. 00169 * 00170 * The callback may be called in an interrupt context and should not 00171 * perform expensive operations such as recv/send calls. 00172 * 00173 * @param obj Pointer to object to call method on 00174 * @param method Method to call on state change 00175 * 00176 * @deprecated 00177 * The attach function does not support cv-qualifiers. Replaced by 00178 * attach(callback(obj, method)). 00179 */ 00180 template <typename T, typename M> 00181 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00182 "The attach function does not support cv-qualifiers. Replaced by " 00183 "attach(callback(obj, method)).") 00184 void attach(T *obj, M method) { 00185 attach(mbed::callback(obj, method)); 00186 } 00187 00188 protected: 00189 Socket(); 00190 virtual nsapi_protocol_t get_proto() = 0; 00191 virtual void event() = 0; 00192 00193 NetworkStack *_stack; 00194 nsapi_socket_t _socket; 00195 uint32_t _timeout; 00196 mbed::Callback<void()> _event; 00197 mbed::Callback<void()> _callback; 00198 rtos::Mutex _lock; 00199 }; 00200 00201 00202 #endif 00203 00204 /** @}*/
Generated on Tue Jul 12 2022 13:16:07 by
