private fork
Fork of GSwifiInterface by
GSwifi/GSwifi_sock.cpp@11:71d67fea5ace, 2013-11-15 (annotated)
- Committer:
- gsfan
- Date:
- Fri Nov 15 04:20:14 2013 +0000
- Revision:
- 11:71d67fea5ace
- Parent:
- 8:64184a968e3b
- Child:
- 12:057089026a20
fix api function, miss spell
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 | 11:71d67fea5ace | 25 | if (!isConnected() && _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 | 8:64184a968e3b | 47 | int GSwifi::open (Protocol proto, const char *ip, int port, int src) { |
gsfan | 11:71d67fea5ace | 48 | int cid; |
gsfan | 11:71d67fea5ace | 49 | |
gsfan | 11:71d67fea5ace | 50 | if (!isConnected() && _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 | 11:71d67fea5ace | 62 | return cid; |
gsfan | 8:64184a968e3b | 63 | } |
gsfan | 8:64184a968e3b | 64 | |
gsfan | 8:64184a968e3b | 65 | int GSwifi::listen (Protocol proto, int port) { |
gsfan | 11:71d67fea5ace | 66 | int cid; |
gsfan | 11:71d67fea5ace | 67 | |
gsfan | 11:71d67fea5ace | 68 | if (!isConnected() && _state.status == STAT_READY) return -1; |
gsfan | 11:71d67fea5ace | 69 | |
gsfan | 8:64184a968e3b | 70 | _state.cid = -1; |
gsfan | 8:64184a968e3b | 71 | if (proto == PROTO_TCP) { |
gsfan | 8:64184a968e3b | 72 | if (cmdNSTCP(port)) return -1; |
gsfan | 8:64184a968e3b | 73 | } else { |
gsfan | 8:64184a968e3b | 74 | if (cmdNSUDP(port)) return -1; |
gsfan | 8:64184a968e3b | 75 | } |
gsfan | 8:64184a968e3b | 76 | if (_state.cid < 0) return -1; |
gsfan | 11:71d67fea5ace | 77 | cid = _state.cid; |
gsfan | 11:71d67fea5ace | 78 | _con[cid].protocol = proto; |
gsfan | 11:71d67fea5ace | 79 | _con[cid].type = TYPE_SERVER; |
gsfan | 11:71d67fea5ace | 80 | return cid; |
gsfan | 8:64184a968e3b | 81 | } |
gsfan | 8:64184a968e3b | 82 | |
gsfan | 8:64184a968e3b | 83 | int GSwifi::close (int cid) { |
gsfan | 11:71d67fea5ace | 84 | |
gsfan | 11:71d67fea5ace | 85 | if (!isConnected(cid)) return -1; |
gsfan | 11:71d67fea5ace | 86 | |
gsfan | 8:64184a968e3b | 87 | _con[cid].connected = false; |
gsfan | 8:64184a968e3b | 88 | return cmdNCLOSE(cid); |
gsfan | 8:64184a968e3b | 89 | } |
gsfan | 8:64184a968e3b | 90 | |
gsfan | 8:64184a968e3b | 91 | int GSwifi::send (int cid, const char *buf, int len) { |
gsfan | 8:64184a968e3b | 92 | char cmd[CFG_CMD_SIZE]; |
gsfan | 8:64184a968e3b | 93 | |
gsfan | 11:71d67fea5ace | 94 | if (!isConnected(cid)) return -1; |
gsfan | 8:64184a968e3b | 95 | |
gsfan | 8:64184a968e3b | 96 | if ((_con[cid].protocol == PROTO_TCP) || |
gsfan | 8:64184a968e3b | 97 | (_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_CLIENT) || |
gsfan | 8:64184a968e3b | 98 | (_con[cid].protocol == PROTO_HTTPD)) { |
gsfan | 8:64184a968e3b | 99 | if (len > CFG_DATA_SIZE) len = CFG_DATA_SIZE; |
gsfan | 8:64184a968e3b | 100 | sprintf(cmd, "\x1bZ%X%04d", cid, len); |
gsfan | 11:71d67fea5ace | 101 | return sendData(buf, len, CFG_TIMEOUT, cmd); |
gsfan | 8:64184a968e3b | 102 | } else { |
gsfan | 8:64184a968e3b | 103 | return -1; |
gsfan | 8:64184a968e3b | 104 | } |
gsfan | 8:64184a968e3b | 105 | } |
gsfan | 8:64184a968e3b | 106 | |
gsfan | 8:64184a968e3b | 107 | int GSwifi::sendto (int cid, const char *buf, int len, const char *ip, int port) { |
gsfan | 8:64184a968e3b | 108 | char cmd[CFG_CMD_SIZE]; |
gsfan | 8:64184a968e3b | 109 | |
gsfan | 11:71d67fea5ace | 110 | if (!isConnected(cid)) return -1; |
gsfan | 8:64184a968e3b | 111 | |
gsfan | 8:64184a968e3b | 112 | if ((_con[cid].protocol == PROTO_UDP && _con[cid].type == TYPE_SERVER)) { |
gsfan | 8:64184a968e3b | 113 | if (len > CFG_DATA_SIZE) len = CFG_DATA_SIZE; |
gsfan | 8:64184a968e3b | 114 | sprintf(cmd, "\x1bY%X%s:%d:%04d", cid, ip, port, len); |
gsfan | 11:71d67fea5ace | 115 | return sendData(buf, len, CFG_TIMEOUT, cmd); |
gsfan | 8:64184a968e3b | 116 | } else { |
gsfan | 8:64184a968e3b | 117 | return -1; |
gsfan | 8:64184a968e3b | 118 | } |
gsfan | 8:64184a968e3b | 119 | } |
gsfan | 8:64184a968e3b | 120 | |
gsfan | 8:64184a968e3b | 121 | int GSwifi::recv (int cid, char *buf, int len) { |
gsfan | 8:64184a968e3b | 122 | int i; |
gsfan | 8:64184a968e3b | 123 | |
gsfan | 11:71d67fea5ace | 124 | if (!isConnected(cid)) return -1; |
gsfan | 8:64184a968e3b | 125 | |
gsfan | 8:64184a968e3b | 126 | if (_con[cid].buf == NULL) return 0; |
gsfan | 8:64184a968e3b | 127 | while (!_con[cid].received && _state.mode != MODE_COMMAND); |
gsfan | 8:64184a968e3b | 128 | _con[cid].received = false; |
gsfan | 8:64184a968e3b | 129 | for (i = 0; i < len; i ++) { |
gsfan | 8:64184a968e3b | 130 | if (_con[cid].buf->dequeue(&buf[i]) == false) break; |
gsfan | 8:64184a968e3b | 131 | } |
gsfan | 8:64184a968e3b | 132 | setRts(true); |
gsfan | 8:64184a968e3b | 133 | return i; |
gsfan | 8:64184a968e3b | 134 | } |
gsfan | 8:64184a968e3b | 135 | |
gsfan | 8:64184a968e3b | 136 | int GSwifi::recvfrom (int cid, char *buf, int len, char *ip, int *port) { |
gsfan | 8:64184a968e3b | 137 | int i; |
gsfan | 8:64184a968e3b | 138 | |
gsfan | 11:71d67fea5ace | 139 | if (!isConnected(cid)) return -1; |
gsfan | 8:64184a968e3b | 140 | |
gsfan | 8:64184a968e3b | 141 | if (_con[cid].buf == NULL) return 0; |
gsfan | 8:64184a968e3b | 142 | while (!_con[cid].received && _state.mode != MODE_COMMAND); |
gsfan | 8:64184a968e3b | 143 | _con[cid].received = false; |
gsfan | 8:64184a968e3b | 144 | for (i = 0; i < len; i ++) { |
gsfan | 8:64184a968e3b | 145 | if (_con[cid].buf->dequeue(&buf[i]) == false) break; |
gsfan | 8:64184a968e3b | 146 | } |
gsfan | 8:64184a968e3b | 147 | strncpy(ip, _con[cid].ip, 16); |
gsfan | 8:64184a968e3b | 148 | *port = _con[cid].port; |
gsfan | 8:64184a968e3b | 149 | setRts(true); |
gsfan | 8:64184a968e3b | 150 | return i; |
gsfan | 8:64184a968e3b | 151 | } |
gsfan | 8:64184a968e3b | 152 | |
gsfan | 8:64184a968e3b | 153 | int GSwifi::readable (int cid) { |
gsfan | 11:71d67fea5ace | 154 | if (!isConnected(cid)) return -1; |
gsfan | 11:71d67fea5ace | 155 | |
gsfan | 8:64184a968e3b | 156 | if (_con[cid].buf == NULL) return -1; |
gsfan | 8:64184a968e3b | 157 | return _con[cid].buf->available(); |
gsfan | 8:64184a968e3b | 158 | } |
gsfan | 8:64184a968e3b | 159 | |
gsfan | 8:64184a968e3b | 160 | bool GSwifi::isConnected (int cid) { |
gsfan | 8:64184a968e3b | 161 | if (cid < 0 || cid >= 16) return false; |
gsfan | 11:71d67fea5ace | 162 | |
gsfan | 8:64184a968e3b | 163 | return _con[cid].connected; |
gsfan | 8:64184a968e3b | 164 | } |
gsfan | 8:64184a968e3b | 165 | |
gsfan | 8:64184a968e3b | 166 | int GSwifi::accept (int cid) { |
gsfan | 8:64184a968e3b | 167 | int i; |
gsfan | 11:71d67fea5ace | 168 | |
gsfan | 11:71d67fea5ace | 169 | if (!isConnected(cid)) return -1; |
gsfan | 11:71d67fea5ace | 170 | |
gsfan | 8:64184a968e3b | 171 | for (i = 0; i < 16; i ++) { |
gsfan | 8:64184a968e3b | 172 | if (_con[i].connected && _con[i].accept && _con[i].parent == cid) { |
gsfan | 8:64184a968e3b | 173 | _con[i].accept = false; |
gsfan | 8:64184a968e3b | 174 | return i; |
gsfan | 8:64184a968e3b | 175 | } |
gsfan | 8:64184a968e3b | 176 | } |
gsfan | 8:64184a968e3b | 177 | return -1; |
gsfan | 8:64184a968e3b | 178 | } |
gsfan | 8:64184a968e3b | 179 | |
gsfan | 8:64184a968e3b | 180 | int GSwifi::getRemote(int cid, char **ip, int *port) { |
gsfan | 11:71d67fea5ace | 181 | |
gsfan | 11:71d67fea5ace | 182 | if (!isConnected(cid)) return -1; |
gsfan | 11:71d67fea5ace | 183 | |
gsfan | 8:64184a968e3b | 184 | *ip = _con[cid].ip; |
gsfan | 8:64184a968e3b | 185 | *port = _con[cid].port; |
gsfan | 8:64184a968e3b | 186 | return 0; |
gsfan | 8:64184a968e3b | 187 | } |