Junichi Katsu
/
MilkcocoaSampleESP8266_LED
ワークショップ用のサンプル
Fork of MilkcocoaSampleESP8266 by
ESP8266InterfaceTiny/Socket/UDPSocket.cpp@2:d2e4e7ce7a65, 2016-02-27 (annotated)
- Committer:
- jksoft
- Date:
- Sat Feb 27 00:12:32 2016 +0000
- Revision:
- 2:d2e4e7ce7a65
- Parent:
- 0:82d5445a9461
??
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:82d5445a9461 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
jksoft | 0:82d5445a9461 | 2 | * |
jksoft | 0:82d5445a9461 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
jksoft | 0:82d5445a9461 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
jksoft | 0:82d5445a9461 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
jksoft | 0:82d5445a9461 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
jksoft | 0:82d5445a9461 | 7 | * furnished to do so, subject to the following conditions: |
jksoft | 0:82d5445a9461 | 8 | * |
jksoft | 0:82d5445a9461 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
jksoft | 0:82d5445a9461 | 10 | * substantial portions of the Software. |
jksoft | 0:82d5445a9461 | 11 | * |
jksoft | 0:82d5445a9461 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
jksoft | 0:82d5445a9461 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
jksoft | 0:82d5445a9461 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
jksoft | 0:82d5445a9461 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
jksoft | 0:82d5445a9461 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
jksoft | 0:82d5445a9461 | 17 | */ |
jksoft | 0:82d5445a9461 | 18 | |
jksoft | 0:82d5445a9461 | 19 | #include "UDPSocket.h" |
jksoft | 0:82d5445a9461 | 20 | |
jksoft | 0:82d5445a9461 | 21 | #include <string> |
jksoft | 0:82d5445a9461 | 22 | #include <algorithm> |
jksoft | 0:82d5445a9461 | 23 | |
jksoft | 0:82d5445a9461 | 24 | UDPSocket::UDPSocket() |
jksoft | 0:82d5445a9461 | 25 | { |
jksoft | 0:82d5445a9461 | 26 | endpoint_configured = false; |
jksoft | 0:82d5445a9461 | 27 | endpoint_read = false; |
jksoft | 0:82d5445a9461 | 28 | Endpoint currentEndpoint; |
jksoft | 0:82d5445a9461 | 29 | } |
jksoft | 0:82d5445a9461 | 30 | |
jksoft | 0:82d5445a9461 | 31 | int UDPSocket::init(void) |
jksoft | 0:82d5445a9461 | 32 | { |
jksoft | 0:82d5445a9461 | 33 | return 0; |
jksoft | 0:82d5445a9461 | 34 | } |
jksoft | 0:82d5445a9461 | 35 | |
jksoft | 0:82d5445a9461 | 36 | // Server initialization |
jksoft | 0:82d5445a9461 | 37 | int UDPSocket::bind(int port) |
jksoft | 0:82d5445a9461 | 38 | { |
jksoft | 0:82d5445a9461 | 39 | return 0; |
jksoft | 0:82d5445a9461 | 40 | } |
jksoft | 0:82d5445a9461 | 41 | |
jksoft | 0:82d5445a9461 | 42 | // -1 if unsuccessful, else number of bytes written |
jksoft | 0:82d5445a9461 | 43 | int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) |
jksoft | 0:82d5445a9461 | 44 | { |
jksoft | 0:82d5445a9461 | 45 | Timer tmr; |
jksoft | 0:82d5445a9461 | 46 | int idx = 0; |
jksoft | 0:82d5445a9461 | 47 | |
jksoft | 0:82d5445a9461 | 48 | |
jksoft | 0:82d5445a9461 | 49 | confEndpoint(remote); |
jksoft | 0:82d5445a9461 | 50 | |
jksoft | 0:82d5445a9461 | 51 | // initialize transparent mode if not already done |
jksoft | 0:82d5445a9461 | 52 | if(!endpoint_configured) { |
jksoft | 0:82d5445a9461 | 53 | // initialize UDP (default id of -1 means transparent mode) |
jksoft | 0:82d5445a9461 | 54 | //!wifi->start(ESP_UDP_TYPE, remote._ipAddress, remote._port, remote._id |
jksoft | 0:82d5445a9461 | 55 | if(!wifi->startUDP(remote._ipAddress, remote._port, 0,length)) { |
jksoft | 0:82d5445a9461 | 56 | return(-1); |
jksoft | 0:82d5445a9461 | 57 | } |
jksoft | 0:82d5445a9461 | 58 | endpoint_configured = true; |
jksoft | 0:82d5445a9461 | 59 | } |
jksoft | 0:82d5445a9461 | 60 | |
jksoft | 0:82d5445a9461 | 61 | tmr.start(); |
jksoft | 0:82d5445a9461 | 62 | |
jksoft | 0:82d5445a9461 | 63 | while ((tmr.read_ms() < _timeout) || _blocking) { |
jksoft | 0:82d5445a9461 | 64 | |
jksoft | 0:82d5445a9461 | 65 | idx += wifi->send(packet, length); |
jksoft | 0:82d5445a9461 | 66 | |
jksoft | 0:82d5445a9461 | 67 | if (idx == length) |
jksoft | 0:82d5445a9461 | 68 | return idx; |
jksoft | 0:82d5445a9461 | 69 | } |
jksoft | 0:82d5445a9461 | 70 | return (idx == 0) ? -1 : idx; |
jksoft | 0:82d5445a9461 | 71 | } |
jksoft | 0:82d5445a9461 | 72 | |
jksoft | 0:82d5445a9461 | 73 | // -1 if unsuccessful, else number of bytes received |
jksoft | 0:82d5445a9461 | 74 | int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) |
jksoft | 0:82d5445a9461 | 75 | { |
jksoft | 0:82d5445a9461 | 76 | Timer tmr; |
jksoft | 0:82d5445a9461 | 77 | int idx = 0; |
jksoft | 0:82d5445a9461 | 78 | int nb_available = 0; |
jksoft | 0:82d5445a9461 | 79 | int time = -1; |
jksoft | 0:82d5445a9461 | 80 | |
jksoft | 0:82d5445a9461 | 81 | //make this the non-blocking case and return if <= 0 |
jksoft | 0:82d5445a9461 | 82 | // remember to change the config to blocking |
jksoft | 0:82d5445a9461 | 83 | // if ( ! _blocking) { |
jksoft | 0:82d5445a9461 | 84 | // if ( wifi.readable <= 0 ) { |
jksoft | 0:82d5445a9461 | 85 | // return (wifi.readable); |
jksoft | 0:82d5445a9461 | 86 | // } |
jksoft | 0:82d5445a9461 | 87 | // } |
jksoft | 0:82d5445a9461 | 88 | //--- |
jksoft | 0:82d5445a9461 | 89 | tmr.start(); |
jksoft | 0:82d5445a9461 | 90 | if (_blocking) { |
jksoft | 0:82d5445a9461 | 91 | while (1) { |
jksoft | 0:82d5445a9461 | 92 | nb_available = wifi->readable(); |
jksoft | 0:82d5445a9461 | 93 | if (nb_available != 0) { |
jksoft | 0:82d5445a9461 | 94 | break; |
jksoft | 0:82d5445a9461 | 95 | } |
jksoft | 0:82d5445a9461 | 96 | } |
jksoft | 0:82d5445a9461 | 97 | } |
jksoft | 0:82d5445a9461 | 98 | //--- |
jksoft | 0:82d5445a9461 | 99 | // blocking case |
jksoft | 0:82d5445a9461 | 100 | else { |
jksoft | 0:82d5445a9461 | 101 | tmr.reset(); |
jksoft | 0:82d5445a9461 | 102 | |
jksoft | 0:82d5445a9461 | 103 | while (time < _timeout) { |
jksoft | 0:82d5445a9461 | 104 | nb_available = wifi->readable(); |
jksoft | 0:82d5445a9461 | 105 | if (nb_available < 0) return nb_available; |
jksoft | 0:82d5445a9461 | 106 | if (nb_available > 0) break ; |
jksoft | 0:82d5445a9461 | 107 | time = tmr.read_ms(); |
jksoft | 0:82d5445a9461 | 108 | } |
jksoft | 0:82d5445a9461 | 109 | |
jksoft | 0:82d5445a9461 | 110 | if (nb_available == 0) return nb_available; |
jksoft | 0:82d5445a9461 | 111 | } |
jksoft | 0:82d5445a9461 | 112 | |
jksoft | 0:82d5445a9461 | 113 | // change this to < 20 mS timeout per byte to detect end of packet gap |
jksoft | 0:82d5445a9461 | 114 | // this may not work due to buffering at the UART interface |
jksoft | 0:82d5445a9461 | 115 | tmr.reset(); |
jksoft | 0:82d5445a9461 | 116 | // while ( tmr.read_ms() < 20 ) { |
jksoft | 0:82d5445a9461 | 117 | // if ( wifi.readable() && (idx < length) ) { |
jksoft | 0:82d5445a9461 | 118 | // buffer[idx++] = wifi->getc(); |
jksoft | 0:82d5445a9461 | 119 | // tmr.reset(); |
jksoft | 0:82d5445a9461 | 120 | // } |
jksoft | 0:82d5445a9461 | 121 | // if ( idx == length ) { |
jksoft | 0:82d5445a9461 | 122 | // break; |
jksoft | 0:82d5445a9461 | 123 | // } |
jksoft | 0:82d5445a9461 | 124 | // } |
jksoft | 0:82d5445a9461 | 125 | //--- |
jksoft | 0:82d5445a9461 | 126 | while (time < _timeout) { |
jksoft | 0:82d5445a9461 | 127 | |
jksoft | 0:82d5445a9461 | 128 | nb_available = wifi->readable(); |
jksoft | 0:82d5445a9461 | 129 | //for (int i = 0; i < min(nb_available, length); i++) { |
jksoft | 0:82d5445a9461 | 130 | for (int i = 0; i < min(nb_available, (length-idx)); i++) { |
jksoft | 0:82d5445a9461 | 131 | buffer[idx] = wifi->getc(); |
jksoft | 0:82d5445a9461 | 132 | idx++; |
jksoft | 0:82d5445a9461 | 133 | } |
jksoft | 0:82d5445a9461 | 134 | if (idx == length) { |
jksoft | 0:82d5445a9461 | 135 | break; |
jksoft | 0:82d5445a9461 | 136 | } |
jksoft | 0:82d5445a9461 | 137 | time = tmr.read_ms(); |
jksoft | 0:82d5445a9461 | 138 | } |
jksoft | 0:82d5445a9461 | 139 | //--- |
jksoft | 0:82d5445a9461 | 140 | readEndpoint(remote); |
jksoft | 0:82d5445a9461 | 141 | return (idx == 0) ? -1 : idx; |
jksoft | 0:82d5445a9461 | 142 | } |
jksoft | 0:82d5445a9461 | 143 | |
jksoft | 0:82d5445a9461 | 144 | bool UDPSocket::confEndpoint(Endpoint & ep) |
jksoft | 0:82d5445a9461 | 145 | { |
jksoft | 0:82d5445a9461 | 146 | currentEndpoint = ep; |
jksoft | 0:82d5445a9461 | 147 | return true; |
jksoft | 0:82d5445a9461 | 148 | } |
jksoft | 0:82d5445a9461 | 149 | |
jksoft | 0:82d5445a9461 | 150 | bool UDPSocket::readEndpoint(Endpoint & ep) |
jksoft | 0:82d5445a9461 | 151 | { |
jksoft | 0:82d5445a9461 | 152 | ep = currentEndpoint; |
jksoft | 0:82d5445a9461 | 153 | return true; |
jksoft | 0:82d5445a9461 | 154 | } |