|
| 1 | + |
| 2 | +/** \addtogroup netsocket */ |
| 3 | +/** @{*/ |
| 4 | +/* Socket |
| 5 | + * Copyright (c) 2015 ARM Limited |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#ifndef INTERNETSOCKET_H |
| 21 | +#define INTERNETSOCKET_H |
| 22 | + |
| 23 | +#include "netsocket/Socket.h" |
| 24 | +#include "netsocket/NetworkStack.h" |
| 25 | +#include "rtos/Mutex.h" |
| 26 | +#include "Callback.h" |
| 27 | +#include "mbed_toolchain.h" |
| 28 | + |
| 29 | +/** Socket implementation that uses IP network stack. |
| 30 | + * Not to be directly used by applications. Cannot be directly instantiated. |
| 31 | + */ |
| 32 | +class InternetSocket : public Socket { |
| 33 | +public: |
| 34 | + /** Destroy a socket |
| 35 | + * |
| 36 | + * Closes socket if the socket is still open |
| 37 | + */ |
| 38 | + virtual ~InternetSocket() {} |
| 39 | + |
| 40 | + /** Opens a socket |
| 41 | + * |
| 42 | + * Creates a network socket on the network stack of the given |
| 43 | + * network interface. Not needed if stack is passed to the |
| 44 | + * socket's constructor. |
| 45 | + * |
| 46 | + * @param stack Network stack as target for socket |
| 47 | + * @return 0 on success, negative error code on failure |
| 48 | + */ |
| 49 | + nsapi_error_t open(NetworkStack *stack); |
| 50 | + |
| 51 | + template <typename S> |
| 52 | + nsapi_error_t open(S *stack) { |
| 53 | + return open(nsapi_create_stack(stack)); |
| 54 | + } |
| 55 | + |
| 56 | + /** Close the socket |
| 57 | + * |
| 58 | + * Closes any open connection and deallocates any memory associated |
| 59 | + * with the socket. Called from destructor if socket is not closed. |
| 60 | + * |
| 61 | + * @return 0 on success, negative error code on failure |
| 62 | + */ |
| 63 | + virtual nsapi_error_t close(); |
| 64 | + |
| 65 | + /** Subscribes to an IP multicast group |
| 66 | + * |
| 67 | + * @param address Multicast group IP address |
| 68 | + * @return Negative error code on failure |
| 69 | + */ |
| 70 | + int join_multicast_group(const SocketAddress &address); |
| 71 | + |
| 72 | + /** Leave an IP multicast group |
| 73 | + * |
| 74 | + * @param address Multicast group IP address |
| 75 | + * @return Negative error code on failure |
| 76 | + */ |
| 77 | + int leave_multicast_group(const SocketAddress &address); |
| 78 | + |
| 79 | + |
| 80 | + /** Bind a specific address to a socket |
| 81 | + * |
| 82 | + * Binding a socket specifies the address and port on which to receive |
| 83 | + * data. |
| 84 | + * |
| 85 | + * @param port Local port to bind |
| 86 | + * @return 0 on success, negative error code on failure. |
| 87 | + */ |
| 88 | + nsapi_error_t bind(uint16_t port); |
| 89 | + |
| 90 | + /** Bind a specific address to a socket |
| 91 | + * |
| 92 | + * Binding a socket specifies the address and port on which to receive |
| 93 | + * data. If the IP address is zeroed, only the port is bound. |
| 94 | + * |
| 95 | + * @param address Null-terminated local address to bind |
| 96 | + * @param port Local port to bind |
| 97 | + * @return 0 on success, negative error code on failure. |
| 98 | + */ |
| 99 | + nsapi_error_t bind(const char *address, uint16_t port); |
| 100 | + |
| 101 | + /** Bind a specific address to a socket |
| 102 | + * |
| 103 | + * Binding a socket specifies the address and port on which to receive |
| 104 | + * data. If the IP address is zeroed, only the port is bound. |
| 105 | + * |
| 106 | + * @param address Local address to bind |
| 107 | + * @return 0 on success, negative error code on failure. |
| 108 | + */ |
| 109 | + virtual nsapi_error_t bind(const SocketAddress &address); |
| 110 | + |
| 111 | + /** Set blocking or non-blocking mode of the socket |
| 112 | + * |
| 113 | + * Initially all sockets are in blocking mode. In non-blocking mode |
| 114 | + * blocking operations such as send/recv/accept return |
| 115 | + * NSAPI_ERROR_WOULD_BLOCK if they can not continue. |
| 116 | + * |
| 117 | + * set_blocking(false) is equivalent to set_timeout(-1) |
| 118 | + * set_blocking(true) is equivalent to set_timeout(0) |
| 119 | + * |
| 120 | + * @param blocking true for blocking mode, false for non-blocking mode. |
| 121 | + */ |
| 122 | + virtual void set_blocking(bool blocking); |
| 123 | + |
| 124 | + /** Set timeout on blocking socket operations |
| 125 | + * |
| 126 | + * Initially all sockets have unbounded timeouts. NSAPI_ERROR_WOULD_BLOCK |
| 127 | + * is returned if a blocking operation takes longer than the specified |
| 128 | + * timeout. A timeout of 0 removes the timeout from the socket. A negative |
| 129 | + * value give the socket an unbounded timeout. |
| 130 | + * |
| 131 | + * set_timeout(0) is equivalent to set_blocking(false) |
| 132 | + * set_timeout(-1) is equivalent to set_blocking(true) |
| 133 | + * |
| 134 | + * @param timeout Timeout in milliseconds |
| 135 | + */ |
| 136 | + virtual void set_timeout(int timeout); |
| 137 | + |
| 138 | + /* Set socket options |
| 139 | + * |
| 140 | + * setsockopt allows an application to pass stack-specific options |
| 141 | + * to the underlying stack using stack-specific level and option names, |
| 142 | + * or to request generic options using levels from nsapi_socket_level_t. |
| 143 | + * |
| 144 | + * For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned |
| 145 | + * and the socket is unmodified. |
| 146 | + * |
| 147 | + * @param level Stack-specific protocol level or nsapi_socket_level_t |
| 148 | + * @param optname Level-specific option name |
| 149 | + * @param optval Option value |
| 150 | + * @param optlen Length of the option value |
| 151 | + * @return 0 on success, negative error code on failure |
| 152 | + */ |
| 153 | + virtual nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen); |
| 154 | + |
| 155 | + /* Get socket options |
| 156 | + * |
| 157 | + * getsockopt allows an application to retrieve stack-specific options |
| 158 | + * from the underlying stack using stack-specific level and option names, |
| 159 | + * or to request generic options using levels from nsapi_socket_level_t. |
| 160 | + * |
| 161 | + * For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned |
| 162 | + * and the socket is unmodified. |
| 163 | + * |
| 164 | + * @param level Stack-specific protocol level or nsapi_socket_level_t |
| 165 | + * @param optname Level-specific option name |
| 166 | + * @param optval Destination for option value |
| 167 | + * @param optlen Length of the option value |
| 168 | + * @return 0 on success, negative error code on failure |
| 169 | + */ |
| 170 | + virtual nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen); |
| 171 | + |
| 172 | + /** Register a callback on state change of the socket |
| 173 | + * |
| 174 | + * The specified callback will be called on state changes such as when |
| 175 | + * the socket can recv/send/accept successfully and on when an error |
| 176 | + * occurs. The callback may also be called spuriously without reason. |
| 177 | + * |
| 178 | + * The callback may be called in an interrupt context and should not |
| 179 | + * perform expensive operations such as recv/send calls. |
| 180 | + * |
| 181 | + * Note! This is not intended as a replacement for a poll or attach-like |
| 182 | + * asynchronous api, but rather as a building block for constructing |
| 183 | + * such functionality. The exact timing of when the registered function |
| 184 | + * is called is not guaranteed and susceptible to change. |
| 185 | + * |
| 186 | + * @param func Function to call on state change |
| 187 | + */ |
| 188 | + virtual void sigio(mbed::Callback<void()> func); |
| 189 | + |
| 190 | + /** Register a callback on state change of the socket |
| 191 | + * |
| 192 | + * @see Socket::sigio |
| 193 | + * @deprecated |
| 194 | + * The behaviour of Socket::attach differs from other attach functions in |
| 195 | + * mbed OS and has been known to cause confusion. Replaced by Socket::sigio. |
| 196 | + */ |
| 197 | + MBED_DEPRECATED_SINCE("mbed-os-5.4", |
| 198 | + "The behaviour of Socket::attach differs from other attach functions in " |
| 199 | + "mbed OS and has been known to cause confusion. Replaced by Socket::sigio.") |
| 200 | + void attach(mbed::Callback<void()> func); |
| 201 | + |
| 202 | + /** Register a callback on state change of the socket |
| 203 | + * |
| 204 | + * @see Socket::sigio |
| 205 | + * @deprecated |
| 206 | + * The attach function does not support cv-qualifiers. Replaced by |
| 207 | + * attach(callback(obj, method)). |
| 208 | + */ |
| 209 | + template <typename T, typename M> |
| 210 | + MBED_DEPRECATED_SINCE("mbed-os-5.1", |
| 211 | + "The attach function does not support cv-qualifiers. Replaced by " |
| 212 | + "attach(callback(obj, method)).") |
| 213 | + void attach(T *obj, M method) { |
| 214 | + attach(mbed::callback(obj, method)); |
| 215 | + } |
| 216 | + |
| 217 | +protected: |
| 218 | + InternetSocket(); |
| 219 | + virtual nsapi_protocol_t get_proto() = 0; |
| 220 | + virtual void event() = 0; |
| 221 | + int modify_multicast_group(const SocketAddress &address, nsapi_socket_option_t socketopt); |
| 222 | + |
| 223 | + NetworkStack *_stack; |
| 224 | + nsapi_socket_t _socket; |
| 225 | + uint32_t _timeout; |
| 226 | + mbed::Callback<void()> _event; |
| 227 | + mbed::Callback<void()> _callback; |
| 228 | + rtos::Mutex _lock; |
| 229 | + SocketAddress _remote_peer; |
| 230 | +}; |
| 231 | + |
| 232 | +#endif // INTERNETSOCKET_H |
0 commit comments