|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry). |
| 3 | + * This product is licensed under an Apache license, see the LICENSE file in the top-level directory. |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * Ethernet remote capability plugin. This file is a plugin file and should not be directly edited, |
| 8 | + * it will be replaced each time the project is built. If you want to edit this file in place, |
| 9 | + * make sure to rename it first. |
| 10 | + */ |
| 11 | + |
| 12 | +#include "EthernetTransport.h" |
| 13 | +#include <TaskManager.h> |
| 14 | + |
| 15 | +using namespace tcremote; |
| 16 | + |
| 17 | +#if ETHERNET_BUFFER_SIZE > 0 // we need buffering when dealing with Ethernet2 |
| 18 | + |
| 19 | +bool EthernetTagValTransport::available() { |
| 20 | + return client && client.connected(); |
| 21 | +} |
| 22 | + |
| 23 | +bool EthernetTagValTransport::connected() { |
| 24 | + return client && client.connected(); |
| 25 | +} |
| 26 | + |
| 27 | +void EthernetTagValTransport::flush() { |
| 28 | + if(!client || writeBufferPos == 0) return; |
| 29 | + |
| 30 | + if((int)client.write(writeBuffer, writeBufferPos) == writeBufferPos) { |
| 31 | + serdebugF2("Buffer written ", writeBufferPos); |
| 32 | + writeBufferPos = 0; |
| 33 | + client.flush(); |
| 34 | + } |
| 35 | + else { |
| 36 | + writeBufferPos = 0; |
| 37 | + close(); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +int EthernetTagValTransport::fillReadBuffer(uint8_t* dataBuffer, int maxData) { |
| 42 | + if(client && client.connected() && client.available()) { |
| 43 | + auto amt = client.read(dataBuffer, maxData); |
| 44 | + if(amt <= 0) { |
| 45 | + close(); |
| 46 | + return 0; |
| 47 | + } |
| 48 | + serdebugF2("read to buffer ", amt); |
| 49 | + return amt; |
| 50 | + } |
| 51 | + return 0; |
| 52 | +} |
| 53 | + |
| 54 | +void EthernetTagValTransport::close() { |
| 55 | + serdebugF("socket close"); |
| 56 | + BaseBufferedRemoteTransport::close(); |
| 57 | + client.stop(); |
| 58 | +} |
| 59 | + |
| 60 | +#else // unbuffed client - requires library to support Nagle algorythm. |
| 61 | + |
| 62 | +bool EthernetTagValTransport::available() { |
| 63 | + return client && client.connected(); |
| 64 | +} |
| 65 | + |
| 66 | +bool EthernetTagValTransport::connected() { |
| 67 | + return client && client.connected(); |
| 68 | +} |
| 69 | + |
| 70 | +int EthernetTagValTransport::writeChar(char data) { |
| 71 | + // only uncomment below for worst case debugging.. |
| 72 | +// serdebug2("writing ", data); |
| 73 | + return client.write(data); |
| 74 | +} |
| 75 | + |
| 76 | +int EthernetTagValTransport::writeStr(const char* data) { |
| 77 | + // only uncomment below for worst case debugging.. |
| 78 | +// serdebug2("writing ", data); |
| 79 | + return client.write(data); |
| 80 | +} |
| 81 | + |
| 82 | +void EthernetTagValTransport::flush() { |
| 83 | + if(client) client.flush(); |
| 84 | +} |
| 85 | + |
| 86 | +uint8_t EthernetTagValTransport::readByte() { |
| 87 | + return client.read(); |
| 88 | +} |
| 89 | + |
| 90 | +bool EthernetTagValTransport::readAvailable() { |
| 91 | + return client && client.connected() && client.available(); |
| 92 | +} |
| 93 | + |
| 94 | +void EthernetTagValTransport::close() { |
| 95 | + serdebugF("socket close"); |
| 96 | + client.stop(); |
| 97 | + currentField.msgType = UNKNOWN_MSG_TYPE; |
| 98 | + currentField.fieldType = FVAL_PROCESSING_AWAITINGMSG; |
| 99 | +} |
| 100 | + |
| 101 | +#endif |
| 102 | + |
| 103 | + |
| 104 | +int tcremote::fromWiFiRSSITo4StateIndicator(int strength) { |
| 105 | + int qualityIcon = 0; |
| 106 | + if(strength > -50) qualityIcon = 4; |
| 107 | + else if(strength > -60) qualityIcon = 3; |
| 108 | + else if(strength > -75) qualityIcon = 2; |
| 109 | + else if(strength > -90) qualityIcon = 1; |
| 110 | + return qualityIcon; |
| 111 | +} |
| 112 | + |
| 113 | +bool EthernetInitialisation::attemptInitialisation() { |
| 114 | +#ifdef ARDUINO_ARCH_STM32 |
| 115 | + // we'll keep checking if the link is up before trying to initialise further |
| 116 | + if(Ethernet.linkStatus() == LinkOFF) return false; |
| 117 | +#endif |
| 118 | + serdebugF("Initialising server "); |
| 119 | + this->server->begin(); |
| 120 | + initialised = true; |
| 121 | + return initialised; |
| 122 | +} |
| 123 | + |
| 124 | +bool EthernetInitialisation::attemptNewConnection(BaseRemoteServerConnection *remoteServerConnection) { |
| 125 | + auto client = server->available(); |
| 126 | + if(client) { |
| 127 | + serdebugF("Client found"); |
| 128 | + auto* tvCon = reinterpret_cast<TagValueRemoteServerConnection*>(remoteServerConnection); |
| 129 | + reinterpret_cast<EthernetTagValTransport*>(tvCon->transport())->setClient(client); |
| 130 | + return true; |
| 131 | + } |
| 132 | + return false; |
| 133 | +} |
0 commit comments