aboutsummaryrefslogtreecommitdiff
path: root/libraries/WiFi/WiFiUdp.cpp
diff options
context:
space:
mode:
authorCristian Maglie <c.maglie@bug.st>2013-04-03 13:51:04 +0200
committerCristian Maglie <c.maglie@bug.st>2013-04-03 13:51:04 +0200
commitee90e68e86dd61d86f5d17b69080338328765b22 (patch)
treee620c0edc2690ab789b665e567910640597aa6fe /libraries/WiFi/WiFiUdp.cpp
parent0ecdc5ebc96ad4c7c548c438a03d9ce00679db8b (diff)
parentf50c307be280dc6ece9e70c43b301c1db36291a0 (diff)
Merged 1.0.5
Merge remote-tracking branch 'arduino/master' into ide-1.5.x Conflicts: app/src/processing/app/Base.java build/shared/revisions.txt hardware/arduino/avr/cores/arduino/malloc.c hardware/arduino/cores/arduino/avr-libc/malloc.c hardware/arduino/cores/arduino/malloc.c todo.txt
Diffstat (limited to 'libraries/WiFi/WiFiUdp.cpp')
-rw-r--r--libraries/WiFi/WiFiUdp.cpp163
1 files changed, 163 insertions, 0 deletions
diff --git a/libraries/WiFi/WiFiUdp.cpp b/libraries/WiFi/WiFiUdp.cpp
new file mode 100644
index 0000000..7020df8
--- /dev/null
+++ b/libraries/WiFi/WiFiUdp.cpp
@@ -0,0 +1,163 @@
+
+extern "C" {
+ #include "utility/debug.h"
+ #include "utility/wifi_spi.h"
+}
+#include <string.h>
+#include "server_drv.h"
+#include "wifi_drv.h"
+
+#include "WiFi.h"
+#include "WiFiUdp.h"
+#include "WiFiClient.h"
+#include "WiFiServer.h"
+
+
+/* Constructor */
+WiFiUDP::WiFiUDP() : _sock(NO_SOCKET_AVAIL) {}
+
+/* Start WiFiUDP socket, listening at local port PORT */
+uint8_t WiFiUDP::begin(uint16_t port) {
+
+ uint8_t sock = WiFiClass::getSocket();
+ if (sock != NO_SOCKET_AVAIL)
+ {
+ ServerDrv::startServer(port, sock, UDP_MODE);
+ WiFiClass::_server_port[sock] = port;
+ _sock = sock;
+ _port = port;
+ return 1;
+ }
+ return 0;
+
+}
+
+/* return number of bytes available in the current packet,
+ will return zero if parsePacket hasn't been called yet */
+int WiFiUDP::available() {
+ if (_sock != NO_SOCKET_AVAIL)
+ {
+ return ServerDrv::availData(_sock);
+ }
+ return 0;
+}
+
+/* Release any resources being used by this WiFiUDP instance */
+void WiFiUDP::stop()
+{
+ if (_sock == NO_SOCKET_AVAIL)
+ return;
+
+ ServerDrv::stopClient(_sock);
+
+ _sock = NO_SOCKET_AVAIL;
+}
+
+int WiFiUDP::beginPacket(const char *host, uint16_t port)
+{
+ // Look up the host first
+ int ret = 0;
+ IPAddress remote_addr;
+ if (WiFi.hostByName(host, remote_addr))
+ {
+ return beginPacket(remote_addr, port);
+ }
+ return ret;
+}
+
+int WiFiUDP::beginPacket(IPAddress ip, uint16_t port)
+{
+ if (_sock == NO_SOCKET_AVAIL)
+ _sock = WiFiClass::getSocket();
+ if (_sock != NO_SOCKET_AVAIL)
+ {
+ ServerDrv::startClient(uint32_t(ip), port, _sock, UDP_MODE);
+ WiFiClass::_state[_sock] = _sock;
+ return 1;
+ }
+ return 0;
+}
+
+int WiFiUDP::endPacket()
+{
+ return ServerDrv::sendUdpData(_sock);
+}
+
+size_t WiFiUDP::write(uint8_t byte)
+{
+ return write(&byte, 1);
+}
+
+size_t WiFiUDP::write(const uint8_t *buffer, size_t size)
+{
+ ServerDrv::insertDataBuf(_sock, buffer, size);
+ return size;
+}
+
+int WiFiUDP::parsePacket()
+{
+ return available();
+}
+
+int WiFiUDP::read()
+{
+ uint8_t b;
+ if (available())
+ {
+ ServerDrv::getData(_sock, &b);
+ return b;
+ }else{
+ return -1;
+ }
+}
+
+int WiFiUDP::read(unsigned char* buffer, size_t len)
+{
+ if (available())
+ {
+ size_t size = 0;
+ if (!ServerDrv::getDataBuf(_sock, buffer, &size))
+ return -1;
+ // TODO check if the buffer is too smal respect to buffer size
+ return size;
+ }else{
+ return -1;
+ }
+}
+
+int WiFiUDP::peek()
+{
+ uint8_t b;
+ if (!available())
+ return -1;
+
+ ServerDrv::getData(_sock, &b, 1);
+ return b;
+}
+
+void WiFiUDP::flush()
+{
+ while (available())
+ read();
+}
+
+IPAddress WiFiUDP::remoteIP()
+{
+ uint8_t _remoteIp[4] = {0};
+ uint8_t _remotePort[2] = {0};
+
+ WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);
+ IPAddress ip(_remoteIp);
+ return ip;
+}
+
+uint16_t WiFiUDP::remotePort()
+{
+ uint8_t _remoteIp[4] = {0};
+ uint8_t _remotePort[2] = {0};
+
+ WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);
+ uint16_t port = (_remotePort[0]<<8)+_remotePort[1];
+ return port;
+}
+