Host library for controlling a WiConnect enabled Wi-Fi module.
Dependents: wiconnect-ota_example wiconnect-web_setup_example wiconnect-test-console wiconnect-tcp_server_example ... more
SocketInterface.cpp@40:4b4306f3d829, 2015-02-23 (annotated)
- Committer:
- dan_ackme
- Date:
- Mon Feb 23 22:21:50 2015 -0800
- Revision:
- 40:4b4306f3d829
- Parent:
- 29:b6af04b77a56
minor bug fixes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dan_ackme | 29:b6af04b77a56 | 1 | /** |
dan_ackme | 29:b6af04b77a56 | 2 | * ACKme WiConnect Host Library is licensed under the BSD licence: |
dan_ackme | 29:b6af04b77a56 | 3 | * |
dan_ackme | 29:b6af04b77a56 | 4 | * Copyright (c)2014 ACKme Networks. |
dan_ackme | 29:b6af04b77a56 | 5 | * All rights reserved. |
dan_ackme | 29:b6af04b77a56 | 6 | * |
dan_ackme | 29:b6af04b77a56 | 7 | * Redistribution and use in source and binary forms, with or without modification, |
dan_ackme | 29:b6af04b77a56 | 8 | * are permitted provided that the following conditions are met: |
dan_ackme | 29:b6af04b77a56 | 9 | * |
dan_ackme | 29:b6af04b77a56 | 10 | * 1. Redistributions of source code must retain the above copyright notice, |
dan_ackme | 29:b6af04b77a56 | 11 | * this list of conditions and the following disclaimer. |
dan_ackme | 29:b6af04b77a56 | 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
dan_ackme | 29:b6af04b77a56 | 13 | * this list of conditions and the following disclaimer in the documentation |
dan_ackme | 29:b6af04b77a56 | 14 | * and/or other materials provided with the distribution. |
dan_ackme | 29:b6af04b77a56 | 15 | * 3. The name of the author may not be used to endorse or promote products |
dan_ackme | 29:b6af04b77a56 | 16 | * derived from this software without specific prior written permission. |
dan_ackme | 29:b6af04b77a56 | 17 | * |
dan_ackme | 29:b6af04b77a56 | 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED |
dan_ackme | 29:b6af04b77a56 | 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
dan_ackme | 29:b6af04b77a56 | 20 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
dan_ackme | 29:b6af04b77a56 | 21 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
dan_ackme | 29:b6af04b77a56 | 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
dan_ackme | 29:b6af04b77a56 | 23 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
dan_ackme | 29:b6af04b77a56 | 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
dan_ackme | 29:b6af04b77a56 | 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
dan_ackme | 29:b6af04b77a56 | 26 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
dan_ackme | 29:b6af04b77a56 | 27 | * OF SUCH DAMAGE. |
dan_ackme | 29:b6af04b77a56 | 28 | */ |
dan_ackme | 29:b6af04b77a56 | 29 | |
dan_ackme | 29:b6af04b77a56 | 30 | #include <string.h> |
dan_ackme | 29:b6af04b77a56 | 31 | #include "Wiconnect.h" |
dan_ackme | 29:b6af04b77a56 | 32 | #include "internal/common.h" |
dan_ackme | 29:b6af04b77a56 | 33 | #include "api/StringUtil.h" |
dan_ackme | 29:b6af04b77a56 | 34 | |
dan_ackme | 29:b6af04b77a56 | 35 | #ifdef WICONNECT_GPIO_IRQ_ENABLED |
dan_ackme | 29:b6af04b77a56 | 36 | #include "api/types/SocketIrqHandlerMap.h" |
dan_ackme | 29:b6af04b77a56 | 37 | #endif |
dan_ackme | 29:b6af04b77a56 | 38 | |
dan_ackme | 29:b6af04b77a56 | 39 | |
dan_ackme | 29:b6af04b77a56 | 40 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 41 | SocketInterface::SocketInterface(Wiconnect *wiconnect_) |
dan_ackme | 29:b6af04b77a56 | 42 | { |
dan_ackme | 29:b6af04b77a56 | 43 | wiconnect = wiconnect_; |
dan_ackme | 29:b6af04b77a56 | 44 | serverConnectedClientList = 0; |
dan_ackme | 29:b6af04b77a56 | 45 | } |
dan_ackme | 29:b6af04b77a56 | 46 | |
dan_ackme | 29:b6af04b77a56 | 47 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 48 | SocketInterface::~SocketInterface() |
dan_ackme | 29:b6af04b77a56 | 49 | { |
dan_ackme | 29:b6af04b77a56 | 50 | } |
dan_ackme | 29:b6af04b77a56 | 51 | |
dan_ackme | 29:b6af04b77a56 | 52 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 53 | WiconnectResult SocketInterface::connect(WiconnectSocket &socket, SocketType type, const char *host, uint16_t remortPort, uint16_t localPort, const void *args GPIO_IRQ_ARG) |
dan_ackme | 29:b6af04b77a56 | 54 | { |
dan_ackme | 29:b6af04b77a56 | 55 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 56 | int32_t handle; |
dan_ackme | 29:b6af04b77a56 | 57 | char *cmdBuffer = wiconnect->internalBuffer; |
dan_ackme | 29:b6af04b77a56 | 58 | |
dan_ackme | 29:b6af04b77a56 | 59 | if(WICONNECT_IS_IDLE()) |
dan_ackme | 29:b6af04b77a56 | 60 | { |
dan_ackme | 29:b6af04b77a56 | 61 | char gpioOption[8] = ""; |
dan_ackme | 29:b6af04b77a56 | 62 | |
dan_ackme | 29:b6af04b77a56 | 63 | #ifdef WICONNECT_GPIO_IRQ_ENABLED |
dan_ackme | 29:b6af04b77a56 | 64 | if(irqPin != PIN_NC) |
dan_ackme | 29:b6af04b77a56 | 65 | { |
dan_ackme | 29:b6af04b77a56 | 66 | PinToGpioMapper mapper = wiconnect->pinToGpioMapper; |
dan_ackme | 29:b6af04b77a56 | 67 | if(mapper == NULL) |
dan_ackme | 29:b6af04b77a56 | 68 | { |
dan_ackme | 29:b6af04b77a56 | 69 | return WICONNECT_PINNAME_TO_GPIO_MAPPER_NULL; |
dan_ackme | 29:b6af04b77a56 | 70 | } |
dan_ackme | 29:b6af04b77a56 | 71 | int8_t gpio = mapper(irqPin); |
dan_ackme | 29:b6af04b77a56 | 72 | if(gpio == -1) |
dan_ackme | 29:b6af04b77a56 | 73 | { |
dan_ackme | 29:b6af04b77a56 | 74 | return WICONNECT_PINNAME_TO_GPIO_NO_MAPPING; |
dan_ackme | 29:b6af04b77a56 | 75 | } |
dan_ackme | 29:b6af04b77a56 | 76 | else if(!irqHandlers.pinIsRegistered(irqPin)) |
dan_ackme | 29:b6af04b77a56 | 77 | { |
dan_ackme | 29:b6af04b77a56 | 78 | return WICONNECT_NOT_FOUND; |
dan_ackme | 29:b6af04b77a56 | 79 | } |
dan_ackme | 29:b6af04b77a56 | 80 | |
dan_ackme | 29:b6af04b77a56 | 81 | sprintf(gpioOption, "-g %d ", gpio); |
dan_ackme | 29:b6af04b77a56 | 82 | } |
dan_ackme | 29:b6af04b77a56 | 83 | #endif |
dan_ackme | 29:b6af04b77a56 | 84 | |
dan_ackme | 29:b6af04b77a56 | 85 | switch(type) |
dan_ackme | 29:b6af04b77a56 | 86 | { |
dan_ackme | 29:b6af04b77a56 | 87 | case SOCKET_TYPE_TCP: |
dan_ackme | 29:b6af04b77a56 | 88 | sprintf(cmdBuffer, "tcpc %s%s %d", gpioOption, host, remortPort); |
dan_ackme | 29:b6af04b77a56 | 89 | break; |
dan_ackme | 29:b6af04b77a56 | 90 | |
dan_ackme | 29:b6af04b77a56 | 91 | case SOCKET_TYPE_UDP: { |
dan_ackme | 29:b6af04b77a56 | 92 | char tmp[16]; |
dan_ackme | 29:b6af04b77a56 | 93 | sprintf(cmdBuffer, "udpc %s%s %d %s", gpioOption, host, remortPort, |
dan_ackme | 29:b6af04b77a56 | 94 | (localPort != SOCKET_ANY_PORT) ? StringUtil::uint32ToStr(tmp, localPort) : ""); |
dan_ackme | 29:b6af04b77a56 | 95 | } break; |
dan_ackme | 29:b6af04b77a56 | 96 | |
dan_ackme | 29:b6af04b77a56 | 97 | case SOCKET_TYPE_TLS: |
dan_ackme | 29:b6af04b77a56 | 98 | sprintf(cmdBuffer, "tlsc %s%s %d %s", gpioOption, host, remortPort, |
dan_ackme | 29:b6af04b77a56 | 99 | (args != NULL) ? (char*)args : ""); |
dan_ackme | 29:b6af04b77a56 | 100 | break; |
dan_ackme | 29:b6af04b77a56 | 101 | |
dan_ackme | 29:b6af04b77a56 | 102 | case SOCKET_TYPE_HTTP: { |
dan_ackme | 29:b6af04b77a56 | 103 | const HttpSocketArgs *httpArgs = (const HttpSocketArgs*)args; |
dan_ackme | 29:b6af04b77a56 | 104 | switch(httpArgs->type) |
dan_ackme | 29:b6af04b77a56 | 105 | { |
dan_ackme | 29:b6af04b77a56 | 106 | case SOCKET_HTTP_GET: |
dan_ackme | 29:b6af04b77a56 | 107 | sprintf(cmdBuffer, "http_get %s%s %s", httpArgs->openOnly ? "-o " : "", |
dan_ackme | 29:b6af04b77a56 | 108 | host, |
dan_ackme | 29:b6af04b77a56 | 109 | (httpArgs->certName != NULL) ? httpArgs->certName : ""); |
dan_ackme | 29:b6af04b77a56 | 110 | break; |
dan_ackme | 29:b6af04b77a56 | 111 | |
dan_ackme | 29:b6af04b77a56 | 112 | case SOCKET_HTTP_HEAD: |
dan_ackme | 29:b6af04b77a56 | 113 | sprintf(cmdBuffer, "http_head %s%s %s", httpArgs->openOnly ? "-o " : "", |
dan_ackme | 29:b6af04b77a56 | 114 | host, |
dan_ackme | 29:b6af04b77a56 | 115 | (httpArgs->certName != NULL) ? httpArgs->certName : ""); |
dan_ackme | 29:b6af04b77a56 | 116 | break; |
dan_ackme | 29:b6af04b77a56 | 117 | |
dan_ackme | 29:b6af04b77a56 | 118 | case SOCKET_HTTP_POST: |
dan_ackme | 29:b6af04b77a56 | 119 | sprintf(cmdBuffer, "http_post %s%s %s %s", httpArgs->openOnly ? "-o " : "", |
dan_ackme | 29:b6af04b77a56 | 120 | host, |
dan_ackme | 29:b6af04b77a56 | 121 | httpArgs->contextType, |
dan_ackme | 29:b6af04b77a56 | 122 | (httpArgs->certName != NULL) ? httpArgs->certName : ""); |
dan_ackme | 29:b6af04b77a56 | 123 | break; |
dan_ackme | 29:b6af04b77a56 | 124 | |
dan_ackme | 29:b6af04b77a56 | 125 | default: |
dan_ackme | 29:b6af04b77a56 | 126 | return WICONNECT_BAD_ARG; |
dan_ackme | 29:b6af04b77a56 | 127 | } |
dan_ackme | 29:b6af04b77a56 | 128 | |
dan_ackme | 29:b6af04b77a56 | 129 | } break; |
dan_ackme | 29:b6af04b77a56 | 130 | default: |
dan_ackme | 29:b6af04b77a56 | 131 | return WICONNECT_BAD_ARG; |
dan_ackme | 29:b6af04b77a56 | 132 | } |
dan_ackme | 29:b6af04b77a56 | 133 | } |
dan_ackme | 29:b6af04b77a56 | 134 | |
dan_ackme | 29:b6af04b77a56 | 135 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 136 | |
dan_ackme | 29:b6af04b77a56 | 137 | if(WICONNECT_SUCCEEDED(result, wiconnect->sendCommand(cmdBuffer))) |
dan_ackme | 29:b6af04b77a56 | 138 | { |
dan_ackme | 29:b6af04b77a56 | 139 | if(!WICONNECT_FAILED(result, wiconnect->responseToInt32(&handle))) |
dan_ackme | 29:b6af04b77a56 | 140 | { |
dan_ackme | 29:b6af04b77a56 | 141 | socket.init(handle, type, host, remortPort, localPort); |
dan_ackme | 29:b6af04b77a56 | 142 | } |
dan_ackme | 29:b6af04b77a56 | 143 | } |
dan_ackme | 29:b6af04b77a56 | 144 | |
dan_ackme | 29:b6af04b77a56 | 145 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 146 | |
dan_ackme | 29:b6af04b77a56 | 147 | return result; |
dan_ackme | 29:b6af04b77a56 | 148 | } |
dan_ackme | 29:b6af04b77a56 | 149 | |
dan_ackme | 29:b6af04b77a56 | 150 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 151 | WiconnectResult SocketInterface::tcpConnect(WiconnectSocket &socket, const char *host, uint16_t remortPort GPIO_IRQ_ARG) |
dan_ackme | 29:b6af04b77a56 | 152 | { |
dan_ackme | 29:b6af04b77a56 | 153 | return connect(socket, SOCKET_TYPE_TCP, host, remortPort, SOCKET_ANY_PORT, NULL GPIO_IRQ_PARAM); |
dan_ackme | 29:b6af04b77a56 | 154 | } |
dan_ackme | 29:b6af04b77a56 | 155 | |
dan_ackme | 29:b6af04b77a56 | 156 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 157 | WiconnectResult SocketInterface::tlsConnect(WiconnectSocket &socket, const char *host, uint16_t remortPort, const char *certFilename GPIO_IRQ_ARG) |
dan_ackme | 29:b6af04b77a56 | 158 | { |
dan_ackme | 29:b6af04b77a56 | 159 | return connect(socket, SOCKET_TYPE_TLS, host, remortPort, SOCKET_ANY_PORT, certFilename GPIO_IRQ_PARAM); |
dan_ackme | 29:b6af04b77a56 | 160 | } |
dan_ackme | 29:b6af04b77a56 | 161 | |
dan_ackme | 29:b6af04b77a56 | 162 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 163 | WiconnectResult SocketInterface::udpConnect(WiconnectSocket &socket, const char *host, uint16_t remortPort, uint16_t localPort GPIO_IRQ_ARG) |
dan_ackme | 29:b6af04b77a56 | 164 | { |
dan_ackme | 29:b6af04b77a56 | 165 | return connect(socket, SOCKET_TYPE_UDP, host, remortPort, localPort, NULL GPIO_IRQ_PARAM); |
dan_ackme | 29:b6af04b77a56 | 166 | } |
dan_ackme | 29:b6af04b77a56 | 167 | |
dan_ackme | 29:b6af04b77a56 | 168 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 169 | WiconnectResult SocketInterface::httpConnect(WiconnectSocket &socket, const char *url, const HttpSocketArgs *args) |
dan_ackme | 29:b6af04b77a56 | 170 | { |
dan_ackme | 29:b6af04b77a56 | 171 | #ifdef WICONNECT_GPIO_IRQ_ENABLED |
dan_ackme | 29:b6af04b77a56 | 172 | #define IRQ_NC ,NC |
dan_ackme | 29:b6af04b77a56 | 173 | #else |
dan_ackme | 29:b6af04b77a56 | 174 | #define IRQ_NC |
dan_ackme | 29:b6af04b77a56 | 175 | #endif |
dan_ackme | 29:b6af04b77a56 | 176 | return connect(socket, SOCKET_TYPE_HTTP, url, SOCKET_ANY_PORT, SOCKET_ANY_PORT, args IRQ_NC); |
dan_ackme | 29:b6af04b77a56 | 177 | } |
dan_ackme | 29:b6af04b77a56 | 178 | |
dan_ackme | 29:b6af04b77a56 | 179 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 180 | WiconnectResult SocketInterface::httpGet(WiconnectSocket &socket, const char *url, bool openOnly, const char *certFilename) |
dan_ackme | 29:b6af04b77a56 | 181 | { |
dan_ackme | 29:b6af04b77a56 | 182 | const HttpSocketArgs args = |
dan_ackme | 29:b6af04b77a56 | 183 | { |
dan_ackme | 29:b6af04b77a56 | 184 | NULL, |
dan_ackme | 29:b6af04b77a56 | 185 | certFilename, |
dan_ackme | 29:b6af04b77a56 | 186 | openOnly, |
dan_ackme | 29:b6af04b77a56 | 187 | SOCKET_HTTP_GET |
dan_ackme | 29:b6af04b77a56 | 188 | }; |
dan_ackme | 29:b6af04b77a56 | 189 | return httpConnect(socket, url, &args); |
dan_ackme | 29:b6af04b77a56 | 190 | } |
dan_ackme | 29:b6af04b77a56 | 191 | |
dan_ackme | 29:b6af04b77a56 | 192 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 193 | WiconnectResult SocketInterface::httpPost(WiconnectSocket &socket, const char *url, const char *contextType, bool openOnly, const char *certFilename) |
dan_ackme | 29:b6af04b77a56 | 194 | { |
dan_ackme | 29:b6af04b77a56 | 195 | const HttpSocketArgs args = |
dan_ackme | 29:b6af04b77a56 | 196 | { |
dan_ackme | 29:b6af04b77a56 | 197 | contextType, |
dan_ackme | 29:b6af04b77a56 | 198 | certFilename, |
dan_ackme | 29:b6af04b77a56 | 199 | openOnly, |
dan_ackme | 29:b6af04b77a56 | 200 | SOCKET_HTTP_POST |
dan_ackme | 29:b6af04b77a56 | 201 | }; |
dan_ackme | 29:b6af04b77a56 | 202 | return httpConnect(socket, url, &args); |
dan_ackme | 29:b6af04b77a56 | 203 | } |
dan_ackme | 29:b6af04b77a56 | 204 | |
dan_ackme | 29:b6af04b77a56 | 205 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 206 | WiconnectResult SocketInterface::httpHead(WiconnectSocket &socket, const char *url, const char *certFilename) |
dan_ackme | 29:b6af04b77a56 | 207 | { |
dan_ackme | 29:b6af04b77a56 | 208 | const HttpSocketArgs args = |
dan_ackme | 29:b6af04b77a56 | 209 | { |
dan_ackme | 29:b6af04b77a56 | 210 | NULL, |
dan_ackme | 29:b6af04b77a56 | 211 | certFilename, |
dan_ackme | 29:b6af04b77a56 | 212 | false, |
dan_ackme | 29:b6af04b77a56 | 213 | SOCKET_HTTP_HEAD |
dan_ackme | 29:b6af04b77a56 | 214 | }; |
dan_ackme | 29:b6af04b77a56 | 215 | return httpConnect(socket, url, &args); |
dan_ackme | 29:b6af04b77a56 | 216 | } |
dan_ackme | 29:b6af04b77a56 | 217 | |
dan_ackme | 29:b6af04b77a56 | 218 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 219 | WiconnectResult SocketInterface::httpAddHeader(WiconnectSocket &socket, const char *key, const char *value) |
dan_ackme | 29:b6af04b77a56 | 220 | { |
dan_ackme | 29:b6af04b77a56 | 221 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 222 | char *cmdBuffer = wiconnect->internalBuffer; |
dan_ackme | 29:b6af04b77a56 | 223 | |
dan_ackme | 29:b6af04b77a56 | 224 | if(WICONNECT_IS_IDLE()) |
dan_ackme | 29:b6af04b77a56 | 225 | { |
dan_ackme | 29:b6af04b77a56 | 226 | sprintf(cmdBuffer, "http_add_header %d %s %s", socket.getHandle(), key, value); |
dan_ackme | 29:b6af04b77a56 | 227 | } |
dan_ackme | 29:b6af04b77a56 | 228 | |
dan_ackme | 29:b6af04b77a56 | 229 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 230 | |
dan_ackme | 29:b6af04b77a56 | 231 | result = wiconnect->sendCommand(cmdBuffer); |
dan_ackme | 29:b6af04b77a56 | 232 | |
dan_ackme | 29:b6af04b77a56 | 233 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 234 | |
dan_ackme | 29:b6af04b77a56 | 235 | return result; |
dan_ackme | 29:b6af04b77a56 | 236 | } |
dan_ackme | 29:b6af04b77a56 | 237 | |
dan_ackme | 29:b6af04b77a56 | 238 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 239 | WiconnectResult SocketInterface::httpGetStatus(WiconnectSocket &socket, uint32_t *statusCodePtr) |
dan_ackme | 29:b6af04b77a56 | 240 | { |
dan_ackme | 29:b6af04b77a56 | 241 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 242 | |
dan_ackme | 29:b6af04b77a56 | 243 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 244 | |
dan_ackme | 29:b6af04b77a56 | 245 | result = wiconnect->sendCommand("http_read_status %d", socket.getHandle()); |
dan_ackme | 29:b6af04b77a56 | 246 | |
dan_ackme | 29:b6af04b77a56 | 247 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 248 | |
dan_ackme | 29:b6af04b77a56 | 249 | return result; |
dan_ackme | 29:b6af04b77a56 | 250 | } |
dan_ackme | 40:4b4306f3d829 | 251 | |
dan_ackme | 40:4b4306f3d829 | 252 | /*************************************************************************************************/ |
dan_ackme | 40:4b4306f3d829 | 253 | WiconnectResult SocketInterface::httpAcceptWebSocket(WiconnectSocket &socket, uint32_t timeoutMs) |
dan_ackme | 40:4b4306f3d829 | 254 | { |
dan_ackme | 40:4b4306f3d829 | 255 | TimeoutTimer timer; |
dan_ackme | 40:4b4306f3d829 | 256 | |
dan_ackme | 40:4b4306f3d829 | 257 | do |
dan_ackme | 40:4b4306f3d829 | 258 | { |
dan_ackme | 40:4b4306f3d829 | 259 | uint8_t handle; |
dan_ackme | 40:4b4306f3d829 | 260 | uint16_t local, remote; |
dan_ackme | 40:4b4306f3d829 | 261 | uint32_t ipAddress; |
dan_ackme | 40:4b4306f3d829 | 262 | WiconnectResult result; |
dan_ackme | 40:4b4306f3d829 | 263 | |
dan_ackme | 40:4b4306f3d829 | 264 | if(WICONNECT_SUCCEEDED(result, pollForClient(SOCKET_TYPE_WS, &handle, &local, &remote, &ipAddress))) |
dan_ackme | 40:4b4306f3d829 | 265 | { |
dan_ackme | 40:4b4306f3d829 | 266 | char ipBuffer[17]; |
dan_ackme | 40:4b4306f3d829 | 267 | if(WICONNECT_FAILED(result, socket.init(handle, SOCKET_TYPE_WS, Wiconnect::ipToStr(ipAddress, ipBuffer), remote, local))) |
dan_ackme | 40:4b4306f3d829 | 268 | { |
dan_ackme | 40:4b4306f3d829 | 269 | return result; |
dan_ackme | 40:4b4306f3d829 | 270 | } |
dan_ackme | 40:4b4306f3d829 | 271 | websocketConnectedList |= (1 << handle); |
dan_ackme | 40:4b4306f3d829 | 272 | return WICONNECT_SUCCESS; |
dan_ackme | 40:4b4306f3d829 | 273 | } |
dan_ackme | 40:4b4306f3d829 | 274 | else if(!(result == WICONNECT_PROCESSING || result == WICONNECT_NOT_FOUND)) |
dan_ackme | 40:4b4306f3d829 | 275 | { |
dan_ackme | 40:4b4306f3d829 | 276 | return result; |
dan_ackme | 40:4b4306f3d829 | 277 | } |
dan_ackme | 40:4b4306f3d829 | 278 | |
dan_ackme | 40:4b4306f3d829 | 279 | } while(timeoutMs == WICONNECT_WAIT_FOREVER || !timer.timedOut(timeoutMs)); |
dan_ackme | 40:4b4306f3d829 | 280 | |
dan_ackme | 40:4b4306f3d829 | 281 | return WICONNECT_TIMEOUT; |
dan_ackme | 40:4b4306f3d829 | 282 | } |
dan_ackme | 29:b6af04b77a56 | 283 | |
dan_ackme | 29:b6af04b77a56 | 284 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 285 | WiconnectResult SocketInterface::closeAllSockets() |
dan_ackme | 29:b6af04b77a56 | 286 | { |
dan_ackme | 29:b6af04b77a56 | 287 | WiconnectResult result; |
dan_ackme | 29:b6af04b77a56 | 288 | |
dan_ackme | 29:b6af04b77a56 | 289 | CHECK_OTHER_COMMAND_EXECUTING(); |
dan_ackme | 29:b6af04b77a56 | 290 | |
dan_ackme | 29:b6af04b77a56 | 291 | result = wiconnect->sendCommand("close all"); |
dan_ackme | 29:b6af04b77a56 | 292 | |
dan_ackme | 29:b6af04b77a56 | 293 | CHECK_CLEANUP_COMMAND(); |
dan_ackme | 29:b6af04b77a56 | 294 | |
dan_ackme | 29:b6af04b77a56 | 295 | return result; |
dan_ackme | 29:b6af04b77a56 | 296 | } |
dan_ackme | 29:b6af04b77a56 | 297 | |
dan_ackme | 29:b6af04b77a56 | 298 | |
dan_ackme | 29:b6af04b77a56 | 299 | #ifdef WICONNECT_GPIO_IRQ_ENABLED |
dan_ackme | 29:b6af04b77a56 | 300 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 301 | WiconnectResult SocketInterface::registerSocketIrqHandler(Pin irqPin, const Callback &handler) |
dan_ackme | 29:b6af04b77a56 | 302 | { |
dan_ackme | 29:b6af04b77a56 | 303 | PinToGpioMapper mapper = wiconnect->pinToGpioMapper; |
dan_ackme | 29:b6af04b77a56 | 304 | if(irqHandlers.pinIsRegistered(irqPin)) |
dan_ackme | 29:b6af04b77a56 | 305 | { |
dan_ackme | 29:b6af04b77a56 | 306 | return WICONNECT_DUPLICATE; |
dan_ackme | 29:b6af04b77a56 | 307 | } |
dan_ackme | 29:b6af04b77a56 | 308 | else if(mapper == NULL) |
dan_ackme | 29:b6af04b77a56 | 309 | { |
dan_ackme | 29:b6af04b77a56 | 310 | return WICONNECT_PINNAME_TO_GPIO_MAPPER_NULL; |
dan_ackme | 29:b6af04b77a56 | 311 | } |
dan_ackme | 29:b6af04b77a56 | 312 | int8_t gpio = mapper(irqPin); |
dan_ackme | 29:b6af04b77a56 | 313 | if(gpio == -1) |
dan_ackme | 29:b6af04b77a56 | 314 | { |
dan_ackme | 29:b6af04b77a56 | 315 | return WICONNECT_PINNAME_TO_GPIO_NO_MAPPING; |
dan_ackme | 29:b6af04b77a56 | 316 | } |
dan_ackme | 29:b6af04b77a56 | 317 | |
dan_ackme | 29:b6af04b77a56 | 318 | return irqHandlers.registerHandler(irqPin, handler); |
dan_ackme | 29:b6af04b77a56 | 319 | } |
dan_ackme | 29:b6af04b77a56 | 320 | |
dan_ackme | 29:b6af04b77a56 | 321 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 322 | WiconnectResult SocketInterface::unregisterSocketIrqHandler(Pin irqPin) |
dan_ackme | 29:b6af04b77a56 | 323 | { |
dan_ackme | 29:b6af04b77a56 | 324 | return irqHandlers.unregisterHandler(irqPin); |
dan_ackme | 29:b6af04b77a56 | 325 | } |
dan_ackme | 29:b6af04b77a56 | 326 | #endif |
dan_ackme | 29:b6af04b77a56 | 327 | |
dan_ackme | 29:b6af04b77a56 | 328 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 329 | void SocketInterface::socketClosedCallback(const WiconnectSocket *socket) |
dan_ackme | 29:b6af04b77a56 | 330 | { |
dan_ackme | 29:b6af04b77a56 | 331 | const uint32_t handleMask = (1 << socket->handle); |
dan_ackme | 29:b6af04b77a56 | 332 | if(serverConnectedClientList & handleMask) |
dan_ackme | 29:b6af04b77a56 | 333 | { |
dan_ackme | 29:b6af04b77a56 | 334 | serverConnectedClientList &= ~handleMask; |
dan_ackme | 29:b6af04b77a56 | 335 | } |
dan_ackme | 29:b6af04b77a56 | 336 | } |