private fork
Fork of GSwifiInterface by
GSwifi/GSwifi_sock.cpp@16:105b56570533, 2014-03-04 (annotated)
- Committer:
- gsfan
- Date:
- Tue Mar 04 15:01:38 2014 +0000
- Revision:
- 16:105b56570533
- Parent:
- 12:057089026a20
bug fix (cause of hard fault)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gsfan | 8:64184a968e3b | 1 | /* Copyright (C) 2013 gsfan, MIT License |
gsfan | 8:64184a968e3b | 2 | * |
gsfan | 8:64184a968e3b | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
gsfan | 8:64184a968e3b | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
gsfan | 8:64184a968e3b | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
gsfan | 8:64184a968e3b | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
gsfan | 8:64184a968e3b | 7 | * furnished to do so, subject to the following conditions: |
gsfan | 8:64184a968e3b | 8 | * |
gsfan | 8:64184a968e3b | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
gsfan | 8:64184a968e3b | 10 | * substantial portions of the Software. |
gsfan | 8:64184a968e3b | 11 | * |
gsfan | 8:64184a968e3b | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
gsfan | 8:64184a968e3b | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
gsfan | 8:64184a968e3b | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
gsfan | 8:64184a968e3b | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
gsfan | 8:64184a968e3b | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
gsfan | 8:64184a968e3b | 17 | */ |
gsfan | 8:64184a968e3b | 18 | |
gsfan | 8:64184a968e3b | 19 | #include "GSwifi.h" |
gsfan | 8:64184a968e3b | 20 | |
gsfan | 8:64184a968e3b | 21 | int GSwifi::getHostByName(const char * host, char * ip) |
gsfan | 8:64184a968e3b | 22 | { |
gsfan | 8:64184a968e3b | 23 | int i, flg = 0; |
gsfan | 8:64184a968e3b | 24 | |
gsfan | 12:057089026a20 | 25 | if (!isAssociated() || _state.status != STAT_READY) return -1; |
gsfan | 11:71d67fea5ace | 26 | |
gsfan | 8:64184a968e3b | 27 | for (i = 0; i < strlen(host); i ++) { |
gsfan | 8:64184a968e3b | 28 | if ((host[i] < '0' || host[i] > '9') && host[i] != '.') { |
gsfan | 8:64184a968e3b | 29 | flg = 1; |
gsfan | 8:64184a968e3b | 30 | break; |
gsfan | 8:64184a968e3b | 31 | } |
gsfan | 8:64184a968e3b | 32 | } |
gsfan | 8:64184a968e3b | 33 | if (!flg) { |
gsfan | 8:64184a968e3b | 34 | strncpy(ip, host, 16); |
gsfan | 8:64184a968e3b | 35 | return 0; |
gsfan | 8:64184a968e3b | 36 | } |
gsfan | 8:64184a968e3b | 37 | |
gsfan | 8:64184a968e3b | 38 | if (cmdDNSLOOKUP(host)) { |
gsfan | 8:64184a968e3b | 39 | // retry |
gsfan | 8:64184a968e3b | 40 | wait_ms(1000); |
gsfan | 8:64184a968e3b | 41 | if (cmdDNSLOOKUP(host)) return -1; |
gsfan | 8:64184a968e3b | 42 | } |
gsfan | 8:64184a968e3b | 43 | strncpy(ip, _state.resolv, 16); |
gsfan | 8:64184a968e3b | 44 | return 0; |
gsfan | 8:64184a968e3b | 45 | } |
gsfan | 8:64184a968e3b | 46 | |
gsfan | 12:057089026a20 | 47 | int GSwifi::open (Protocol proto, const char *ip, int port, int src, void(*func)(int)) { |
gsfan | 11:71d67fea5ace | 48 | int cid; |
gsfan | 11:71d67fea5ace | 49 | |
gsfan | 12:057089026a20 | 50 | if (!isAssociated() || _state.status != STAT_READY) return -1; |
gsfan | 11:71d67fea5ace | 51 | |
gsfan | 8:64184a968e3b | 52 | _state.cid = -1; |
gsfan | 8:64184a968e3b | 53 | if (proto == PROTO_TCP) { |
gsfan | 8:64184a968e3b | 54 | if (cmdNCTCP(ip, port)) return -1; |
gsfan | 8:64184a968e3b | 55 | } else { |
gsfan | 8:64184a968e3b | 56 | if (cmdNCUDP(ip, port, src)) return -1; |
gsfan | 8:64184a968e3b | 57 | } |
gsfan | 8:64184a968e3b | 58 | if (_state.cid < 0) return -1; |
gsfan | 11:71d67fea5ace | 59 | cid = _state.cid; |
gsfan | 11:71d67fea5ace | 60 | _con[cid].protocol = proto; |
gsfan | 11:71d67fea5ace | 61 | _con[cid].type = TYPE_CLIENT; |
gsfan | 12:057089026a20 | 62 | _con[cid].func = func; |
gsfan | 11:71d67fea5ace | 63 | return cid; |
gsfan | 8:64184a968e3b | 64 | } |
gsfan | 8:64184a968e3b | 65 | |
gsfan | 12:057089026a20 | 66 | int GSwifi::listen (Protocol proto, int port, void(*func)(int)) { |
gsfan | 11:71d67fea5ace | 67 | int cid; |
gsfan | 11:71d67fea5ace | 68 | |
gsfan | 12:057089026a20 | 69 | if (!isAssociated() || _state.status != STAT_READY) return -1; |
gsfan | 11:71d67fea5ace | 70 | |
gsfan | 8:64184a968e3b | 71 | _state.cid = -1; |
gsfan | 8:64184a968e3b | 72 | if (proto == PROTO_TCP) { |
gsfan | 8:64184a968e3b | 73 | if (cmdNSTCP(port)) return -1; |
gsfan | 8:64184a968e3b | 74 | } else { |
gsfan | 8:64184a968e3b | 75 | if (cmdNSUDP(port)) return -1; |
gsfan | 8:64184a968e3b | 76 | } |
gsfan | 8:64184a968e3b | 77 | if (_state.cid < 0) return -1; |
gsfan | 11:71d67fea5ace | 78 | cid = _state.cid; |
gsfan | 11:71d67fea5ace | 79 | _con[cid].protocol = proto; |
gsfan | 11:71d67fea5ace | 80 | _con[cid].type = TYPE_SERVER; |
gsfan | 12:057089026a20 | 81 | _con[cid].func = func; |
gsfan | 11:71d67fea5ace | 82 | return cid; |
gsfan | 8:64184a968e3b | 83 | } |
gsfan | 8:64184a968e3b | 84 | |
gsfan | 8:64184a968e3b | 85 | int GSwifi::close (int cid) { |
gsfan | 11:71d67fea5ace | 86 | |
gsfan | 11:71d67fea5ace | 87 | if (!isConnected(cid)) return -1; |
gsfan | 11:71d67fea5ace | 88 | |
gsfan | 8:64184a968e3b | 89 | _con[cid].connected = false; |
gsfan | 8:64184a968e3b | 90 | return cmdNCLOSE(cid); |
gsfan | 8:64184a968e3b | 91 | } |
gsfan | 8:64184a968e3b | 92 | |
gsfan | 8:64184a968e3b | 93 | int GSwifi::send (int cid, const char *buf, int len) { |
gsfan | 8:64184a968e3b | 94 | char cmd[CFG_CMD_SIZE]; |
gsfan | 8:64184a968e3b | 95 | |
gsfan | 11:71d67fea5ace | 96 | if (!isConnected(cid)) return -1; |
gsfan | 8:64184a968e3b | 97 | |
gsfan | 8:64184a968e3b | 98 | if ((_con[cid].protocol == PROTO_TCP) || |
gsfan | 8:64184a968e3b | 99 | (_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_CLIENT) || |
gsfan | 8:64184a968e3b | 100 | (_con[cid].protocol == PROTO_HTTPD)) { |
gsfan | 8:64184a968e3b | 101 | if (len > CFG_DATA_SIZE) len = CFG_DATA_SIZE; |
gsfan | 8:64184a968e3b | 102 | sprintf(cmd, "\x1bZ%X%04d", cid, len); |
gsfan | 11:71d67fea5ace | 103 | return sendData(buf, len, CFG_TIMEOUT, cmd); |
gsfan | 8:64184a968e3b | 104 | } else { |
gsfan | 8:64184a968e3b | 105 | return -1; |
gsfan | 8:64184a968e3b | 106 | } |
gsfan | 8:64184a968e3b | 107 | } |
gsfan | 8:64184a968e3b | 108 | |
gsfan | 8:64184a968e3b | 109 | int GSwifi::sendto (int cid, const char *buf, int len, const char *ip, int port) { |
gsfan | 8:64184a968e3b | 110 | char cmd[CFG_CMD_SIZE]; |
gsfan | 8:64184a968e3b | 111 | |
gsfan | 11:71d67fea5ace | 112 | if (!isConnected(cid)) return -1; |
gsfan | 8:64184a968e3b | 113 | |
gsfan | 8:64184a968e3b | 114 | if ((_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_SERVER)) { |
gsfan | 8:64184a968e3b | 115 | if (len > CFG_DATA_SIZE) len = CFG_DATA_SIZE; |
gsfan | 8:64184a968e3b | 116 | sprintf(cmd, "\x1bY%X%s:%d:%04d", cid, ip, port, len); |
gsfan | 11:71d67fea5ace | 117 | return sendData(buf, len, CFG_TIMEOUT, cmd); |
gsfan | 8:64184a968e3b | 118 | } else { |
gsfan | 8:64184a968e3b | 119 | return -1; |
gsfan | 8:64184a968e3b | 120 | } |
gsfan | 8:64184a968e3b | 121 | } |
gsfan | 8:64184a968e3b | 122 | |
gsfan | 8:64184a968e3b | 123 | int GSwifi::recv (int cid, char *buf, int len) { |
gsfan | 8:64184a968e3b | 124 | int i; |
gsfan | 8:64184a968e3b | 125 | |
gsfan | 11:71d67fea5ace | 126 | if (!isConnected(cid)) return -1; |
gsfan | 8:64184a968e3b | 127 | |
gsfan | 8:64184a968e3b | 128 | if (_con[cid].buf == NULL) return 0; |
gsfan | 8:64184a968e3b | 129 | while (!_con[cid].received && _state.mode != MODE_COMMAND); |
gsfan | 8:64184a968e3b | 130 | _con[cid].received = false; |
gsfan | 8:64184a968e3b | 131 | for (i = 0; i < len; i ++) { |
gsfan | 8:64184a968e3b | 132 | if (_con[cid].buf->dequeue(&buf[i]) == false) break; |
gsfan | 8:64184a968e3b | 133 | } |
gsfan | 8:64184a968e3b | 134 | setRts(true); |
gsfan | 8:64184a968e3b | 135 | return i; |
gsfan | 8:64184a968e3b | 136 | } |
gsfan | 8:64184a968e3b | 137 | |
gsfan | 8:64184a968e3b | 138 | int GSwifi::recvfrom (int cid, char *buf, int len, char *ip, int *port) { |
gsfan | 8:64184a968e3b | 139 | int i; |
gsfan | 8:64184a968e3b | 140 | |
gsfan | 11:71d67fea5ace | 141 | if (!isConnected(cid)) return -1; |
gsfan | 8:64184a968e3b | 142 | |
gsfan | 8:64184a968e3b | 143 | if (_con[cid].buf == NULL) return 0; |
gsfan | 8:64184a968e3b | 144 | while (!_con[cid].received && _state.mode != MODE_COMMAND); |
gsfan | 8:64184a968e3b | 145 | _con[cid].received = false; |
gsfan | 8:64184a968e3b | 146 | for (i = 0; i < len; i ++) { |
gsfan | 8:64184a968e3b | 147 | if (_con[cid].buf->dequeue(&buf[i]) == false) break; |
gsfan | 8:64184a968e3b | 148 | } |
gsfan | 8:64184a968e3b | 149 | strncpy(ip, _con[cid].ip, 16); |
gsfan | 8:64184a968e3b | 150 | *port = _con[cid].port; |
gsfan | 8:64184a968e3b | 151 | setRts(true); |
gsfan | 8:64184a968e3b | 152 | return i; |
gsfan | 8:64184a968e3b | 153 | } |
gsfan | 8:64184a968e3b | 154 | |
gsfan | 8:64184a968e3b | 155 | int GSwifi::readable (int cid) { |
gsfan | 11:71d67fea5ace | 156 | if (!isConnected(cid)) return -1; |
gsfan | 11:71d67fea5ace | 157 | |
gsfan | 8:64184a968e3b | 158 | if (_con[cid].buf == NULL) return -1; |
gsfan | 8:64184a968e3b | 159 | return _con[cid].buf->available(); |
gsfan | 8:64184a968e3b | 160 | } |
gsfan | 8:64184a968e3b | 161 | |
gsfan | 8:64184a968e3b | 162 | bool GSwifi::isConnected (int cid) { |
gsfan | 8:64184a968e3b | 163 | if (cid < 0 || cid >= 16) return false; |
gsfan | 11:71d67fea5ace | 164 | |
gsfan | 8:64184a968e3b | 165 | return _con[cid].connected; |
gsfan | 8:64184a968e3b | 166 | } |
gsfan | 8:64184a968e3b | 167 | |
gsfan | 8:64184a968e3b | 168 | int GSwifi::accept (int cid) { |
gsfan | 8:64184a968e3b | 169 | int i; |
gsfan | 11:71d67fea5ace | 170 | |
gsfan | 11:71d67fea5ace | 171 | if (!isConnected(cid)) return -1; |
gsfan | 11:71d67fea5ace | 172 | |
gsfan | 8:64184a968e3b | 173 | for (i = 0; i < 16; i ++) { |
gsfan | 8:64184a968e3b | 174 | if (_con[i].connected && _con[i].accept && _con[i].parent == cid) { |
gsfan | 8:64184a968e3b | 175 | _con[i].accept = false; |
gsfan | 8:64184a968e3b | 176 | return i; |
gsfan | 8:64184a968e3b | 177 | } |
gsfan | 8:64184a968e3b | 178 | } |
gsfan | 8:64184a968e3b | 179 | return -1; |
gsfan | 8:64184a968e3b | 180 | } |
gsfan | 8:64184a968e3b | 181 | |
gsfan | 8:64184a968e3b | 182 | int GSwifi::getRemote(int cid, char **ip, int *port) { |
gsfan | 11:71d67fea5ace | 183 | |
gsfan | 11:71d67fea5ace | 184 | if (!isConnected(cid)) return -1; |
gsfan | 11:71d67fea5ace | 185 | |
gsfan | 8:64184a968e3b | 186 | *ip = _con[cid].ip; |
gsfan | 8:64184a968e3b | 187 | *port = _con[cid].port; |
gsfan | 8:64184a968e3b | 188 | return 0; |
gsfan | 8:64184a968e3b | 189 | } |
gsfan | 12:057089026a20 | 190 | |
gsfan | 12:057089026a20 | 191 | void GSwifi::initCon (int cid, bool connected) { |
gsfan | 12:057089026a20 | 192 | _con[cid].parent = -1; |
gsfan | 12:057089026a20 | 193 | _con[cid].func = NULL; |
gsfan | 12:057089026a20 | 194 | _con[cid].accept = false; |
gsfan | 16:105b56570533 | 195 | #ifndef CFG_ENABLE_RTOS |
gsfan | 12:057089026a20 | 196 | if (_con[cid].buf == NULL) { |
gsfan | 12:057089026a20 | 197 | _con[cid].buf = new CircBuffer<char>(CFG_DATA_SIZE); |
gsfan | 12:057089026a20 | 198 | if (_con[cid].buf == NULL) error("Can't allocate memory"); |
gsfan | 12:057089026a20 | 199 | } |
gsfan | 16:105b56570533 | 200 | #endif |
gsfan | 16:105b56570533 | 201 | if (_con[cid].buf != NULL) { |
gsfan | 16:105b56570533 | 202 | _con[cid].buf->flush(); |
gsfan | 16:105b56570533 | 203 | } |
gsfan | 12:057089026a20 | 204 | _con[cid].connected = connected; |
gsfan | 12:057089026a20 | 205 | } |