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