diff --git a/CommandStation-EX.ino b/CommandStation-EX.ino index e2e9922..73923c8 100644 --- a/CommandStation-EX.ino +++ b/CommandStation-EX.ino @@ -82,7 +82,7 @@ void setup() eth1.setup(ETHERNET, UDPR); // ETHERNET, UDP on Port 2560 // eth1.setup(ETHERNET, TCP); // ETHERNET, UDP on Port 2560 - eth2.setup(ETHERNET, TCP, 23); // ETHERNET, TCP on Port 23 for the CLI + // eth2.setup(ETHERNET, TCP, 23); // ETHERNET, TCP on Port 23 for the CLI // eth1.setup(ETHERNET, TCP, 8888); // ETHERNET, TCP on Port 8888 // wifi.setup(WIFI, TCP); // WIFI on Port 2560 // eth1.setHttpCallback(httpRequestHandler); // HTTP callback diff --git a/EthernetSetup.cpp b/EthernetSetup.cpp index f6ce2b1..bc64e8d 100644 --- a/EthernetSetup.cpp +++ b/EthernetSetup.cpp @@ -73,7 +73,7 @@ byte EthernetSetup::setup() } else { - ERR(F("\nUDP failed to start")); + ERR(F("UDP failed to start")); connected = false; } break; diff --git a/Transport.cpp b/Transport.cpp index fbdead6..818b5f0 100644 --- a/Transport.cpp +++ b/Transport.cpp @@ -27,12 +27,15 @@ extern uint8_t diagNetworkClient; template bool Transport::setup(NetworkInterface *nw) { + t = new TransportProcessor(); + if (protocol == TCP) { connectionPool(server); // server should have started here so create the connection pool only for TCP though + t->udp = 0; } else { connectionPool(udp); + t->udp = udp; } - t = new TransportProcessor(); t->nwi = nw; // The TransportProcessor needs to know which Interface he is connected to connected = true; // server & clients which will recieve/send data have all e setup and are available return true; @@ -49,7 +52,7 @@ void Transport::loop() { }; case TCP: { - DBG(F("Transport: %s\n"), this->transport == WIFI ? "WIFI" : "ETHERNET"); + DBG(F("Transport: %s"), this->transport == WIFI ? "WIFI" : "ETHERNET"); tcpSessionHandler(server); }; case MQTT: @@ -69,7 +72,7 @@ void Transport::connectionPool(S *server) connections[i].client = &clients[i]; memset(connections[i].overflow, 0, MAX_OVERFLOW); connections[i].id = i; - TRC(F("\nTCP Connection pool: [%d:%x]"), i, connections[i].client); + TRC(F("TCP Connection pool: [%d:%x]"), i, connections[i].client); } } template @@ -82,7 +85,7 @@ void Transport::connectionPool(U *udp) memset(connections[i].overflow, 0, MAX_OVERFLOW); connections[i].id = i; - TRC(F("\nUDP Connection pool: [%d:%x]"), i, connections[i].client); + TRC(F("UDP Connection pool: [%d:%x]"), i, udp); } } /** @@ -99,7 +102,7 @@ void Transport::udpHandler(U* udp) int packetSize = udp->parsePacket(); if (packetSize > 0) { - TRC(F("\nReceived packet of size:[%d]"), packetSize); + TRC(F("Received packet of size:[%d]"), packetSize); IPAddress remote = udp->remoteIP(); char portBuffer[6]; TRC(F("From: [%d.%d.%d.%d: %s]"), remote[0], remote[1], remote[2], remote[3], utoa(udp->remotePort(), portBuffer, 10)); // DIAG has issues with unsigend int's so go through utoa diff --git a/TransportProcessor.cpp b/TransportProcessor.cpp index c4c4768..8e42c49 100644 --- a/TransportProcessor.cpp +++ b/TransportProcessor.cpp @@ -59,23 +59,28 @@ RingStream streamer(512); // buffer into which to feed the commands for handling void sendWiThrottleToDCC(Connection *c, TransportProcessor *t, bool blocking) { - // streamer.printStream(); + byte *_buffer = streamer.getBuffer(); memset(_buffer, 0, 512); // clear out the _buffer WiThrottle *wt = WiThrottle::getThrottle(c->id); // get a throttle for the Connection; will be created if it doesn't exist - // STRINGIFY(__FILE__); - DBG(F("WiThrottle [%x:%x] parsing: [%e]"), wt, _buffer, t->command); + TRC(F("WiThrottle [%x:%x] parsing: [%e]"), wt, _buffer, t->command); + wt->parse(&streamer, (byte *)t->command); // get the response; not all commands will produce a reply if (streamer.count() != -1) { dumpRingStreamBuffer(_buffer, 512); - if (c->client->connected()) - { + TRC(F("UDP %x"), t->udp); + + if ( t->udp != 0) { + TRC(F("Sending UDP WiThrottle response ...")); + t->udp->beginPacket(t->udp->remoteIP(), t->udp->remotePort()); + t->udp->write(_buffer, strlen((char *)_buffer)); + t->udp->endPacket(); + } else if (c->client->connected()) { c->client->write(_buffer, strlen((char *)_buffer)); } } - // streamer.printStream(); streamer.resetStream(); } diff --git a/TransportProcessor.h b/TransportProcessor.h index fd3675f..2ff7c34 100644 --- a/TransportProcessor.h +++ b/TransportProcessor.h @@ -35,9 +35,10 @@ private: #ifdef DCCEX_ENABLED void sendToDCC(Connection *c, TransportProcessor* t, bool blocking); #endif + public: - + UDP *udp; // need to carry the single UDP server instance over to the processor for sending packest NetworkInterface *nwi; uint8_t buffer[MAX_ETH_BUFFER]; char command[MAX_JMRI_CMD];