libraries / Bridge / src / BridgeUdp.hon commit Added link to project report (97a3ba0)
   1/*
   2  Copyright (c) 2015 Arduino LLC. All right reserved.
   3
   4  This library is free software; you can redistribute it and/or
   5  modify it under the terms of the GNU Lesser General Public
   6  License as published by the Free Software Foundation; either
   7  version 2.1 of the License, or (at your option) any later version.
   8
   9  This library is distributed in the hope that it will be useful,
  10  but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12  Lesser General Public License for more details.
  13
  14  You should have received a copy of the GNU Lesser General Public
  15  License along with this library; if not, write to the Free Software
  16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17*/
  18
  19#pragma once
  20
  21#include <Udp.h>
  22#include "Bridge.h"
  23
  24class BridgeUDP : public UDP {
  25
  26  public:
  27    BridgeUDP(BridgeClass &_b = Bridge);
  28    virtual uint8_t begin(uint16_t);
  29    virtual void stop();
  30
  31    virtual int beginPacket(IPAddress ip, uint16_t port);
  32    virtual int beginPacket(const char *host, uint16_t port);
  33    virtual int beginBroadcastPacket(uint16_t port);
  34    virtual int endPacket();
  35    virtual size_t write(uint8_t d) { return write(&d, 1); }
  36    virtual size_t write(const uint8_t *buffer, size_t size);
  37
  38    using Print::write;
  39
  40    virtual int parsePacket();
  41    /* return number of bytes available in the current packet,
  42       will return zero if parsePacket hasn't been called yet */
  43    virtual int available() { return avail; }
  44    virtual int read();
  45    virtual int read(unsigned char* buffer, size_t len);
  46    virtual int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); };
  47    virtual int peek();
  48    virtual void flush() { avail = 0; }
  49
  50    virtual IPAddress remoteIP();
  51    virtual uint16_t remotePort();
  52
  53  private:
  54    BridgeClass &bridge;
  55    uint8_t handle;
  56    boolean opened;
  57
  58  private:
  59    void doBuffer();
  60    uint16_t avail;
  61    uint8_t buffered;
  62    uint8_t readPos;
  63    static const int BUFFER_SIZE = 64;
  64    uint8_t buffer[BUFFER_SIZE];
  65};