Simple IoT Board用のライブラリです。 ESP8266ライブラリの軽量化 送信のみのソフトシリアルライブラリを含んでいます。

Dependents:   SITB_HttpGetSample SITB_IFTTTSample SITB_INA226PRC AmbientExampleSITB ... more

Committer:
jksoft
Date:
Sun Nov 15 13:36:44 2015 +0000
Revision:
0:890c12951e96
??

Who changed what in which revision?

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