|
| 1 | +/* |
| 2 | + EthernetServerStream.h |
| 3 | +
|
| 4 | + Copyright (C) 2017 Marc Josef Pees. All rights reserved. |
| 5 | +
|
| 6 | + This library is free software; you can redistribute it and/or |
| 7 | + modify it under the terms of the GNU Lesser General Public |
| 8 | + License as published by the Free Software Foundation; either |
| 9 | + version 2.1 of the License, or (at your option) any later version. |
| 10 | +
|
| 11 | + See file LICENSE.txt for further informations on licensing terms. |
| 12 | +
|
| 13 | + Last updated July 10th, 2017 |
| 14 | + */ |
| 15 | + |
| 16 | +#ifndef ETHERNETSERVERSTREAM_H |
| 17 | +#define ETHERNETSERVERSTREAM_H |
| 18 | + |
| 19 | +#include <inttypes.h> |
| 20 | +#include <Stream.h> |
| 21 | +#include <Ethernet.h> |
| 22 | + |
| 23 | +//#define SERIAL_DEBUG |
| 24 | +#include "firmataDebug.h" |
| 25 | + |
| 26 | +class EthernetServerStream : public Stream |
| 27 | +{ |
| 28 | + public: |
| 29 | + EthernetServerStream(IPAddress localip, uint16_t port); |
| 30 | + int available(); |
| 31 | + int read(); |
| 32 | + int peek(); |
| 33 | + void flush(); |
| 34 | + size_t write(uint8_t); |
| 35 | + void maintain(IPAddress localip); |
| 36 | + |
| 37 | + private: |
| 38 | + EthernetClient client; |
| 39 | + IPAddress localip; |
| 40 | + uint16_t port; |
| 41 | + bool connected; |
| 42 | + bool maintain(); |
| 43 | + void stop(); |
| 44 | + |
| 45 | + protected: |
| 46 | + EthernetServer server = EthernetServer(3030); |
| 47 | + bool listening = false; |
| 48 | + bool connect_client(); |
| 49 | +}; |
| 50 | + |
| 51 | + |
| 52 | +/* |
| 53 | + * EthernetServerStream.cpp |
| 54 | + * Copied here as a hack to linker issues with 3rd party board packages that don't properly |
| 55 | + * implement the Arduino network APIs. |
| 56 | + */ |
| 57 | +EthernetServerStream::EthernetServerStream(IPAddress localip, uint16_t port) |
| 58 | + : localip(localip), |
| 59 | + port(port), |
| 60 | + connected(false) |
| 61 | +{ |
| 62 | +} |
| 63 | + |
| 64 | +bool EthernetServerStream::connect_client() |
| 65 | + { |
| 66 | + if ( connected ) |
| 67 | + { |
| 68 | + if ( client && client.connected() ) return true; |
| 69 | + stop(); |
| 70 | + } |
| 71 | + |
| 72 | + EthernetClient newClient = server.available(); |
| 73 | + if ( !newClient ) return false; |
| 74 | + client = newClient; |
| 75 | + connected = true; |
| 76 | + DEBUG_PRINTLN("Connected"); |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | +int |
| 81 | +EthernetServerStream::available() |
| 82 | +{ |
| 83 | + return maintain() ? client.available() : 0; |
| 84 | +} |
| 85 | + |
| 86 | +int |
| 87 | +EthernetServerStream::read() |
| 88 | +{ |
| 89 | + return maintain() ? client.read() : -1; |
| 90 | +} |
| 91 | + |
| 92 | +int |
| 93 | +EthernetServerStream::peek() |
| 94 | +{ |
| 95 | + return maintain() ? client.peek() : -1; |
| 96 | +} |
| 97 | + |
| 98 | +void EthernetServerStream::flush() |
| 99 | +{ |
| 100 | + if (maintain()) |
| 101 | + client.flush(); |
| 102 | +} |
| 103 | + |
| 104 | +size_t |
| 105 | +EthernetServerStream::write(uint8_t c) |
| 106 | +{ |
| 107 | + return maintain() ? client.write(c) : 0; |
| 108 | +} |
| 109 | + |
| 110 | +void |
| 111 | +EthernetServerStream::maintain(IPAddress localip) |
| 112 | +{ |
| 113 | + // ensure the local IP is updated in the case that it is changed by the DHCP server |
| 114 | + if (this->localip != localip) { |
| 115 | + this->localip = localip; |
| 116 | + if (connected) |
| 117 | + stop(); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +void |
| 122 | +EthernetServerStream::stop() |
| 123 | +{ |
| 124 | + if(client) |
| 125 | + { |
| 126 | + client.stop(); |
| 127 | + } |
| 128 | + connected = false; |
| 129 | +} |
| 130 | + |
| 131 | +bool |
| 132 | +EthernetServerStream::maintain() |
| 133 | +{ |
| 134 | + if (connect_client()) return true; |
| 135 | + |
| 136 | + stop(); |
| 137 | + |
| 138 | + if(!listening) |
| 139 | + { |
| 140 | + server = EthernetServer(port); |
| 141 | + server.begin(); |
| 142 | + listening = true; |
| 143 | + } |
| 144 | + return false; |
| 145 | +} |
| 146 | + |
| 147 | +#endif /* ETHERNETSERVERSTREAM_H */ |
0 commit comments