Preliminary main mbed library for nexpaq development
features/net/network-socket/Socket.h@0:6c56fb4bc5f0, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:27:58 2016 +0000
- Revision:
- 0:6c56fb4bc5f0
Moving to library for sharing updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:6c56fb4bc5f0 | 1 | /* Socket |
nexpaq | 0:6c56fb4bc5f0 | 2 | * Copyright (c) 2015 ARM Limited |
nexpaq | 0:6c56fb4bc5f0 | 3 | * |
nexpaq | 0:6c56fb4bc5f0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
nexpaq | 0:6c56fb4bc5f0 | 5 | * you may not use this file except in compliance with the License. |
nexpaq | 0:6c56fb4bc5f0 | 6 | * You may obtain a copy of the License at |
nexpaq | 0:6c56fb4bc5f0 | 7 | * |
nexpaq | 0:6c56fb4bc5f0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
nexpaq | 0:6c56fb4bc5f0 | 9 | * |
nexpaq | 0:6c56fb4bc5f0 | 10 | * Unless required by applicable law or agreed to in writing, software |
nexpaq | 0:6c56fb4bc5f0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
nexpaq | 0:6c56fb4bc5f0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
nexpaq | 0:6c56fb4bc5f0 | 13 | * See the License for the specific language governing permissions and |
nexpaq | 0:6c56fb4bc5f0 | 14 | * limitations under the License. |
nexpaq | 0:6c56fb4bc5f0 | 15 | */ |
nexpaq | 0:6c56fb4bc5f0 | 16 | |
nexpaq | 0:6c56fb4bc5f0 | 17 | #ifndef SOCKET_H |
nexpaq | 0:6c56fb4bc5f0 | 18 | #define SOCKET_H |
nexpaq | 0:6c56fb4bc5f0 | 19 | |
nexpaq | 0:6c56fb4bc5f0 | 20 | #include "network-socket/SocketAddress.h" |
nexpaq | 0:6c56fb4bc5f0 | 21 | #include "network-socket/NetworkStack.h" |
nexpaq | 0:6c56fb4bc5f0 | 22 | #include "rtos/Mutex.h" |
nexpaq | 0:6c56fb4bc5f0 | 23 | #include "Callback.h" |
nexpaq | 0:6c56fb4bc5f0 | 24 | #include "toolchain.h" |
nexpaq | 0:6c56fb4bc5f0 | 25 | |
nexpaq | 0:6c56fb4bc5f0 | 26 | |
nexpaq | 0:6c56fb4bc5f0 | 27 | /** Abstract socket class |
nexpaq | 0:6c56fb4bc5f0 | 28 | */ |
nexpaq | 0:6c56fb4bc5f0 | 29 | class Socket { |
nexpaq | 0:6c56fb4bc5f0 | 30 | public: |
nexpaq | 0:6c56fb4bc5f0 | 31 | /** Destroy a socket |
nexpaq | 0:6c56fb4bc5f0 | 32 | * |
nexpaq | 0:6c56fb4bc5f0 | 33 | * Closes socket if the socket is still open |
nexpaq | 0:6c56fb4bc5f0 | 34 | */ |
nexpaq | 0:6c56fb4bc5f0 | 35 | virtual ~Socket() {} |
nexpaq | 0:6c56fb4bc5f0 | 36 | |
nexpaq | 0:6c56fb4bc5f0 | 37 | /** Opens a socket |
nexpaq | 0:6c56fb4bc5f0 | 38 | * |
nexpaq | 0:6c56fb4bc5f0 | 39 | * Creates a network socket on the network stack of the given |
nexpaq | 0:6c56fb4bc5f0 | 40 | * network interface. Not needed if stack is passed to the |
nexpaq | 0:6c56fb4bc5f0 | 41 | * socket's constructor. |
nexpaq | 0:6c56fb4bc5f0 | 42 | * |
nexpaq | 0:6c56fb4bc5f0 | 43 | * @param stack Network stack as target for socket |
nexpaq | 0:6c56fb4bc5f0 | 44 | * @return 0 on success, negative error code on failure |
nexpaq | 0:6c56fb4bc5f0 | 45 | */ |
nexpaq | 0:6c56fb4bc5f0 | 46 | int open(NetworkStack *stack); |
nexpaq | 0:6c56fb4bc5f0 | 47 | |
nexpaq | 0:6c56fb4bc5f0 | 48 | template <typename S> |
nexpaq | 0:6c56fb4bc5f0 | 49 | int open(S *stack) { |
nexpaq | 0:6c56fb4bc5f0 | 50 | return open(nsapi_create_stack(stack)); |
nexpaq | 0:6c56fb4bc5f0 | 51 | } |
nexpaq | 0:6c56fb4bc5f0 | 52 | |
nexpaq | 0:6c56fb4bc5f0 | 53 | /** Close the socket |
nexpaq | 0:6c56fb4bc5f0 | 54 | * |
nexpaq | 0:6c56fb4bc5f0 | 55 | * Closes any open connection and deallocates any memory associated |
nexpaq | 0:6c56fb4bc5f0 | 56 | * with the socket. Called from destructor if socket is not closed. |
nexpaq | 0:6c56fb4bc5f0 | 57 | * |
nexpaq | 0:6c56fb4bc5f0 | 58 | * @return 0 on success, negative error code on failure |
nexpaq | 0:6c56fb4bc5f0 | 59 | */ |
nexpaq | 0:6c56fb4bc5f0 | 60 | int close(); |
nexpaq | 0:6c56fb4bc5f0 | 61 | |
nexpaq | 0:6c56fb4bc5f0 | 62 | /** Bind a specific address to a socket |
nexpaq | 0:6c56fb4bc5f0 | 63 | * |
nexpaq | 0:6c56fb4bc5f0 | 64 | * Binding a socket specifies the address and port on which to recieve |
nexpaq | 0:6c56fb4bc5f0 | 65 | * data. |
nexpaq | 0:6c56fb4bc5f0 | 66 | * |
nexpaq | 0:6c56fb4bc5f0 | 67 | * @param port Local port to bind |
nexpaq | 0:6c56fb4bc5f0 | 68 | * @return 0 on success, negative error code on failure. |
nexpaq | 0:6c56fb4bc5f0 | 69 | */ |
nexpaq | 0:6c56fb4bc5f0 | 70 | int bind(uint16_t port); |
nexpaq | 0:6c56fb4bc5f0 | 71 | |
nexpaq | 0:6c56fb4bc5f0 | 72 | /** Bind a specific address to a socket |
nexpaq | 0:6c56fb4bc5f0 | 73 | * |
nexpaq | 0:6c56fb4bc5f0 | 74 | * Binding a socket specifies the address and port on which to recieve |
nexpaq | 0:6c56fb4bc5f0 | 75 | * data. If the IP address is zeroed, only the port is bound. |
nexpaq | 0:6c56fb4bc5f0 | 76 | * |
nexpaq | 0:6c56fb4bc5f0 | 77 | * @param address Null-terminated local address to bind |
nexpaq | 0:6c56fb4bc5f0 | 78 | * @param port Local port to bind |
nexpaq | 0:6c56fb4bc5f0 | 79 | * @return 0 on success, negative error code on failure. |
nexpaq | 0:6c56fb4bc5f0 | 80 | */ |
nexpaq | 0:6c56fb4bc5f0 | 81 | int bind(const char *address, uint16_t port); |
nexpaq | 0:6c56fb4bc5f0 | 82 | |
nexpaq | 0:6c56fb4bc5f0 | 83 | /** Bind a specific address to a socket |
nexpaq | 0:6c56fb4bc5f0 | 84 | * |
nexpaq | 0:6c56fb4bc5f0 | 85 | * Binding a socket specifies the address and port on which to recieve |
nexpaq | 0:6c56fb4bc5f0 | 86 | * data. If the IP address is zeroed, only the port is bound. |
nexpaq | 0:6c56fb4bc5f0 | 87 | * |
nexpaq | 0:6c56fb4bc5f0 | 88 | * @param address Local address to bind |
nexpaq | 0:6c56fb4bc5f0 | 89 | * @return 0 on success, negative error code on failure. |
nexpaq | 0:6c56fb4bc5f0 | 90 | */ |
nexpaq | 0:6c56fb4bc5f0 | 91 | int bind(const SocketAddress &address); |
nexpaq | 0:6c56fb4bc5f0 | 92 | |
nexpaq | 0:6c56fb4bc5f0 | 93 | /** Set blocking or non-blocking mode of the socket |
nexpaq | 0:6c56fb4bc5f0 | 94 | * |
nexpaq | 0:6c56fb4bc5f0 | 95 | * Initially all sockets are in blocking mode. In non-blocking mode |
nexpaq | 0:6c56fb4bc5f0 | 96 | * blocking operations such as send/recv/accept return |
nexpaq | 0:6c56fb4bc5f0 | 97 | * NSAPI_ERROR_WOULD_BLOCK if they can not continue. |
nexpaq | 0:6c56fb4bc5f0 | 98 | * |
nexpaq | 0:6c56fb4bc5f0 | 99 | * set_blocking(false) is equivalent to set_timeout(-1) |
nexpaq | 0:6c56fb4bc5f0 | 100 | * set_blocking(true) is equivalent to set_timeout(0) |
nexpaq | 0:6c56fb4bc5f0 | 101 | * |
nexpaq | 0:6c56fb4bc5f0 | 102 | * @param blocking true for blocking mode, false for non-blocking mode. |
nexpaq | 0:6c56fb4bc5f0 | 103 | */ |
nexpaq | 0:6c56fb4bc5f0 | 104 | void set_blocking(bool blocking); |
nexpaq | 0:6c56fb4bc5f0 | 105 | |
nexpaq | 0:6c56fb4bc5f0 | 106 | /** Set timeout on blocking socket operations |
nexpaq | 0:6c56fb4bc5f0 | 107 | * |
nexpaq | 0:6c56fb4bc5f0 | 108 | * Initially all sockets have unbounded timeouts. NSAPI_ERROR_WOULD_BLOCK |
nexpaq | 0:6c56fb4bc5f0 | 109 | * is returned if a blocking operation takes longer than the specified |
nexpaq | 0:6c56fb4bc5f0 | 110 | * timeout. A timeout of 0 removes the timeout from the socket. A negative |
nexpaq | 0:6c56fb4bc5f0 | 111 | * value give the socket an unbounded timeout. |
nexpaq | 0:6c56fb4bc5f0 | 112 | * |
nexpaq | 0:6c56fb4bc5f0 | 113 | * set_timeout(0) is equivalent to set_blocking(false) |
nexpaq | 0:6c56fb4bc5f0 | 114 | * set_timeout(-1) is equivalent to set_blocking(true) |
nexpaq | 0:6c56fb4bc5f0 | 115 | * |
nexpaq | 0:6c56fb4bc5f0 | 116 | * @param timeout Timeout in milliseconds |
nexpaq | 0:6c56fb4bc5f0 | 117 | */ |
nexpaq | 0:6c56fb4bc5f0 | 118 | void set_timeout(int timeout); |
nexpaq | 0:6c56fb4bc5f0 | 119 | |
nexpaq | 0:6c56fb4bc5f0 | 120 | /* Set stack-specific socket options |
nexpaq | 0:6c56fb4bc5f0 | 121 | * |
nexpaq | 0:6c56fb4bc5f0 | 122 | * The setsockopt allow an application to pass stack-specific hints |
nexpaq | 0:6c56fb4bc5f0 | 123 | * to the underlying stack. For unsupported options, |
nexpaq | 0:6c56fb4bc5f0 | 124 | * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified. |
nexpaq | 0:6c56fb4bc5f0 | 125 | * |
nexpaq | 0:6c56fb4bc5f0 | 126 | * @param level Stack-specific protocol level |
nexpaq | 0:6c56fb4bc5f0 | 127 | * @param optname Stack-specific option identifier |
nexpaq | 0:6c56fb4bc5f0 | 128 | * @param optval Option value |
nexpaq | 0:6c56fb4bc5f0 | 129 | * @param optlen Length of the option value |
nexpaq | 0:6c56fb4bc5f0 | 130 | * @return 0 on success, negative error code on failure |
nexpaq | 0:6c56fb4bc5f0 | 131 | */ |
nexpaq | 0:6c56fb4bc5f0 | 132 | int setsockopt(int level, int optname, const void *optval, unsigned optlen); |
nexpaq | 0:6c56fb4bc5f0 | 133 | |
nexpaq | 0:6c56fb4bc5f0 | 134 | /* Get stack-specific socket options |
nexpaq | 0:6c56fb4bc5f0 | 135 | * |
nexpaq | 0:6c56fb4bc5f0 | 136 | * The getstackopt allow an application to retrieve stack-specific hints |
nexpaq | 0:6c56fb4bc5f0 | 137 | * from the underlying stack. For unsupported options, |
nexpaq | 0:6c56fb4bc5f0 | 138 | * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified. |
nexpaq | 0:6c56fb4bc5f0 | 139 | * |
nexpaq | 0:6c56fb4bc5f0 | 140 | * @param level Stack-specific protocol level |
nexpaq | 0:6c56fb4bc5f0 | 141 | * @param optname Stack-specific option identifier |
nexpaq | 0:6c56fb4bc5f0 | 142 | * @param optval Destination for option value |
nexpaq | 0:6c56fb4bc5f0 | 143 | * @param optlen Length of the option value |
nexpaq | 0:6c56fb4bc5f0 | 144 | * @return 0 on success, negative error code on failure |
nexpaq | 0:6c56fb4bc5f0 | 145 | */ |
nexpaq | 0:6c56fb4bc5f0 | 146 | int getsockopt(int level, int optname, void *optval, unsigned *optlen); |
nexpaq | 0:6c56fb4bc5f0 | 147 | |
nexpaq | 0:6c56fb4bc5f0 | 148 | /** Register a callback on state change of the socket |
nexpaq | 0:6c56fb4bc5f0 | 149 | * |
nexpaq | 0:6c56fb4bc5f0 | 150 | * The specified callback will be called on state changes such as when |
nexpaq | 0:6c56fb4bc5f0 | 151 | * the socket can recv/send/accept successfully and on when an error |
nexpaq | 0:6c56fb4bc5f0 | 152 | * occurs. The callback may also be called spuriously without reason. |
nexpaq | 0:6c56fb4bc5f0 | 153 | * |
nexpaq | 0:6c56fb4bc5f0 | 154 | * The callback may be called in an interrupt context and should not |
nexpaq | 0:6c56fb4bc5f0 | 155 | * perform expensive operations such as recv/send calls. |
nexpaq | 0:6c56fb4bc5f0 | 156 | * |
nexpaq | 0:6c56fb4bc5f0 | 157 | * @param func Function to call on state change |
nexpaq | 0:6c56fb4bc5f0 | 158 | */ |
nexpaq | 0:6c56fb4bc5f0 | 159 | void attach(mbed::Callback<void()> func); |
nexpaq | 0:6c56fb4bc5f0 | 160 | |
nexpaq | 0:6c56fb4bc5f0 | 161 | /** Register a callback on state change of the socket |
nexpaq | 0:6c56fb4bc5f0 | 162 | * |
nexpaq | 0:6c56fb4bc5f0 | 163 | * The specified callback will be called on state changes such as when |
nexpaq | 0:6c56fb4bc5f0 | 164 | * the socket can recv/send/accept successfully and on when an error |
nexpaq | 0:6c56fb4bc5f0 | 165 | * occurs. The callback may also be called spuriously without reason. |
nexpaq | 0:6c56fb4bc5f0 | 166 | * |
nexpaq | 0:6c56fb4bc5f0 | 167 | * The callback may be called in an interrupt context and should not |
nexpaq | 0:6c56fb4bc5f0 | 168 | * perform expensive operations such as recv/send calls. |
nexpaq | 0:6c56fb4bc5f0 | 169 | * |
nexpaq | 0:6c56fb4bc5f0 | 170 | * @param obj Pointer to object to call method on |
nexpaq | 0:6c56fb4bc5f0 | 171 | * @param method Method to call on state change |
nexpaq | 0:6c56fb4bc5f0 | 172 | * |
nexpaq | 0:6c56fb4bc5f0 | 173 | * @deprecated |
nexpaq | 0:6c56fb4bc5f0 | 174 | * The attach function does not support cv-qualifiers. Replaced by |
nexpaq | 0:6c56fb4bc5f0 | 175 | * attach(callback(obj, method)). |
nexpaq | 0:6c56fb4bc5f0 | 176 | */ |
nexpaq | 0:6c56fb4bc5f0 | 177 | template <typename T, typename M> |
nexpaq | 0:6c56fb4bc5f0 | 178 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
nexpaq | 0:6c56fb4bc5f0 | 179 | "The attach function does not support cv-qualifiers. Replaced by " |
nexpaq | 0:6c56fb4bc5f0 | 180 | "attach(callback(obj, method)).") |
nexpaq | 0:6c56fb4bc5f0 | 181 | void attach(T *obj, M method) { |
nexpaq | 0:6c56fb4bc5f0 | 182 | attach(mbed::callback(obj, method)); |
nexpaq | 0:6c56fb4bc5f0 | 183 | } |
nexpaq | 0:6c56fb4bc5f0 | 184 | |
nexpaq | 0:6c56fb4bc5f0 | 185 | protected: |
nexpaq | 0:6c56fb4bc5f0 | 186 | Socket(); |
nexpaq | 0:6c56fb4bc5f0 | 187 | virtual nsapi_protocol_t get_proto() = 0; |
nexpaq | 0:6c56fb4bc5f0 | 188 | virtual void event() = 0; |
nexpaq | 0:6c56fb4bc5f0 | 189 | |
nexpaq | 0:6c56fb4bc5f0 | 190 | NetworkStack *_stack; |
nexpaq | 0:6c56fb4bc5f0 | 191 | nsapi_socket_t _socket; |
nexpaq | 0:6c56fb4bc5f0 | 192 | uint32_t _timeout; |
nexpaq | 0:6c56fb4bc5f0 | 193 | mbed::Callback<void()> _event; |
nexpaq | 0:6c56fb4bc5f0 | 194 | mbed::Callback<void()> _callback; |
nexpaq | 0:6c56fb4bc5f0 | 195 | rtos::Mutex _lock; |
nexpaq | 0:6c56fb4bc5f0 | 196 | }; |
nexpaq | 0:6c56fb4bc5f0 | 197 | |
nexpaq | 0:6c56fb4bc5f0 | 198 | |
nexpaq | 0:6c56fb4bc5f0 | 199 | #endif |