mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-10 04:22:05 +03:00
committed by
david gauchard
parent
98125f8860
commit
eea9999dc5
@ -1,23 +1,23 @@
|
||||
/*
|
||||
WiFiUdp.cpp - UDP client/server for esp8266, mostly compatible
|
||||
WiFiUdp.cpp - UDP client/server for esp8266, mostly compatible
|
||||
with Arduino WiFi shield library
|
||||
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#define LWIP_INTERNAL
|
||||
@ -25,9 +25,9 @@
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "include/wl_definitions.h"
|
||||
#include "osapi.h"
|
||||
#include "ets_sys.h"
|
||||
#include "include/wl_definitions.h"
|
||||
#include "osapi.h"
|
||||
#include "ets_sys.h"
|
||||
}
|
||||
|
||||
#include "debug.h"
|
||||
@ -53,9 +53,7 @@ WiFiUDP::WiFiUDP(const WiFiUDP& other)
|
||||
{
|
||||
_ctx = other._ctx;
|
||||
if (_ctx)
|
||||
{
|
||||
_ctx->ref();
|
||||
}
|
||||
WiFiUDP::_add(this);
|
||||
}
|
||||
|
||||
@ -63,9 +61,7 @@ WiFiUDP& WiFiUDP::operator=(const WiFiUDP& rhs)
|
||||
{
|
||||
_ctx = rhs._ctx;
|
||||
if (_ctx)
|
||||
{
|
||||
_ctx->ref();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -73,16 +69,13 @@ WiFiUDP::~WiFiUDP()
|
||||
{
|
||||
WiFiUDP::_remove(this);
|
||||
if (_ctx)
|
||||
{
|
||||
_ctx->unref();
|
||||
}
|
||||
}
|
||||
|
||||
/* Start WiFiUDP socket, listening at local port */
|
||||
uint8_t WiFiUDP::begin(uint16_t port)
|
||||
{
|
||||
if (_ctx)
|
||||
{
|
||||
if (_ctx) {
|
||||
_ctx->unref();
|
||||
_ctx = 0;
|
||||
}
|
||||
@ -94,41 +87,35 @@ uint8_t WiFiUDP::begin(uint16_t port)
|
||||
|
||||
uint8_t WiFiUDP::beginMulticast(IPAddress interfaceAddr, IPAddress multicast, uint16_t port)
|
||||
{
|
||||
if (_ctx)
|
||||
{
|
||||
if (_ctx) {
|
||||
_ctx->unref();
|
||||
_ctx = 0;
|
||||
}
|
||||
|
||||
if (igmp_joingroup(interfaceAddr, multicast) != ERR_OK)
|
||||
{
|
||||
if (igmp_joingroup(interfaceAddr, multicast)!= ERR_OK) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
_ctx = new UdpContext;
|
||||
_ctx->ref();
|
||||
ip_addr_t addr = IPADDR4_INIT(INADDR_ANY);
|
||||
if (!_ctx->listen(&addr, port))
|
||||
{
|
||||
if (!_ctx->listen(&addr, port)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* return number of bytes available in the current packet,
|
||||
will return zero if parsePacket hasn't been called yet */
|
||||
int WiFiUDP::available()
|
||||
{
|
||||
/* return number of bytes available in the current packet,
|
||||
will return zero if parsePacket hasn't been called yet */
|
||||
int WiFiUDP::available() {
|
||||
int result = 0;
|
||||
|
||||
if (_ctx)
|
||||
{
|
||||
if (_ctx) {
|
||||
result = static_cast<int>(_ctx->getSize());
|
||||
}
|
||||
|
||||
if (!result)
|
||||
{
|
||||
if (!result) {
|
||||
// yielding here will not make more data "available",
|
||||
// but it will prevent the system from going into WDT reset
|
||||
optimistic_yield(1000);
|
||||
@ -140,8 +127,7 @@ int WiFiUDP::available()
|
||||
/* Release any resources being used by this WiFiUDP instance */
|
||||
void WiFiUDP::stop()
|
||||
{
|
||||
if (_ctx)
|
||||
{
|
||||
if (_ctx) {
|
||||
_ctx->disconnect();
|
||||
_ctx->unref();
|
||||
}
|
||||
@ -160,8 +146,7 @@ int WiFiUDP::beginPacket(const char *host, uint16_t port)
|
||||
|
||||
int WiFiUDP::beginPacket(IPAddress ip, uint16_t port)
|
||||
{
|
||||
if (!_ctx)
|
||||
{
|
||||
if (!_ctx) {
|
||||
_ctx = new UdpContext;
|
||||
_ctx->ref();
|
||||
}
|
||||
@ -169,15 +154,13 @@ int WiFiUDP::beginPacket(IPAddress ip, uint16_t port)
|
||||
}
|
||||
|
||||
int WiFiUDP::beginPacketMulticast(IPAddress multicastAddress, uint16_t port,
|
||||
IPAddress interfaceAddress, int ttl)
|
||||
IPAddress interfaceAddress, int ttl)
|
||||
{
|
||||
if (!_ctx)
|
||||
{
|
||||
if (!_ctx) {
|
||||
_ctx = new UdpContext;
|
||||
_ctx->ref();
|
||||
}
|
||||
if (!_ctx->connect(multicastAddress, port))
|
||||
{
|
||||
if (!_ctx->connect(multicastAddress, port)) {
|
||||
return 0;
|
||||
}
|
||||
_ctx->setMulticastInterface(interfaceAddress);
|
||||
@ -188,9 +171,7 @@ int WiFiUDP::beginPacketMulticast(IPAddress multicastAddress, uint16_t port,
|
||||
int WiFiUDP::endPacket()
|
||||
{
|
||||
if (!_ctx)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (_ctx->send()) ? 1 : 0;
|
||||
}
|
||||
@ -203,9 +184,7 @@ size_t WiFiUDP::write(uint8_t byte)
|
||||
size_t WiFiUDP::write(const uint8_t *buffer, size_t size)
|
||||
{
|
||||
if (!_ctx)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _ctx->append(reinterpret_cast<const char*>(buffer), size);
|
||||
}
|
||||
@ -213,12 +192,9 @@ size_t WiFiUDP::write(const uint8_t *buffer, size_t size)
|
||||
int WiFiUDP::parsePacket()
|
||||
{
|
||||
if (!_ctx)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!_ctx->next())
|
||||
{
|
||||
if (!_ctx->next()) {
|
||||
optimistic_yield(100);
|
||||
return 0;
|
||||
}
|
||||
@ -229,9 +205,7 @@ int WiFiUDP::parsePacket()
|
||||
int WiFiUDP::read()
|
||||
{
|
||||
if (!_ctx)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return _ctx->read();
|
||||
}
|
||||
@ -239,9 +213,7 @@ int WiFiUDP::read()
|
||||
int WiFiUDP::read(unsigned char* buffer, size_t len)
|
||||
{
|
||||
if (!_ctx)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _ctx->read(reinterpret_cast<char*>(buffer), len);
|
||||
}
|
||||
@ -249,9 +221,7 @@ int WiFiUDP::read(unsigned char* buffer, size_t len)
|
||||
int WiFiUDP::peek()
|
||||
{
|
||||
if (!_ctx)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return _ctx->peek();
|
||||
}
|
||||
@ -264,9 +234,7 @@ void WiFiUDP::flush()
|
||||
IPAddress WiFiUDP::remoteIP()
|
||||
{
|
||||
if (!_ctx)
|
||||
{
|
||||
return INADDR_ANY;
|
||||
}
|
||||
|
||||
return _ctx->getRemoteAddress();
|
||||
}
|
||||
@ -274,9 +242,7 @@ IPAddress WiFiUDP::remoteIP()
|
||||
uint16_t WiFiUDP::remotePort()
|
||||
{
|
||||
if (!_ctx)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _ctx->getRemotePort();
|
||||
}
|
||||
@ -284,9 +250,7 @@ uint16_t WiFiUDP::remotePort()
|
||||
IPAddress WiFiUDP::destinationIP() const
|
||||
{
|
||||
if (!_ctx)
|
||||
{
|
||||
return INADDR_ANY;
|
||||
}
|
||||
|
||||
return _ctx->getDestAddress();
|
||||
}
|
||||
@ -294,28 +258,22 @@ IPAddress WiFiUDP::destinationIP() const
|
||||
uint16_t WiFiUDP::localPort() const
|
||||
{
|
||||
if (!_ctx)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _ctx->getLocalPort();
|
||||
}
|
||||
|
||||
void WiFiUDP::stopAll()
|
||||
{
|
||||
for (WiFiUDP* it = _s_first; it; it = it->_next)
|
||||
{
|
||||
for (WiFiUDP* it = _s_first; it; it = it->_next) {
|
||||
DEBUGV("%s %p %p\n", __func__, it, _s_first);
|
||||
it->stop();
|
||||
}
|
||||
}
|
||||
|
||||
void WiFiUDP::stopAllExcept(WiFiUDP * exC)
|
||||
{
|
||||
for (WiFiUDP* it = _s_first; it; it = it->_next)
|
||||
{
|
||||
if (it->_ctx != exC->_ctx)
|
||||
{
|
||||
void WiFiUDP::stopAllExcept(WiFiUDP * exC) {
|
||||
for (WiFiUDP* it = _s_first; it; it = it->_next) {
|
||||
if (it->_ctx != exC->_ctx) {
|
||||
DEBUGV("%s %p %p\n", __func__, it, _s_first);
|
||||
it->stop();
|
||||
}
|
||||
|
Reference in New Issue
Block a user