diff --git a/lib/channel/GwChannel.cpp b/lib/channel/GwChannel.cpp index e51a1eb..5430f71 100644 --- a/lib/channel/GwChannel.cpp +++ b/lib/channel/GwChannel.cpp @@ -88,7 +88,7 @@ void GwChannel::begin( this->toN2k=toN2k; this->readActisense=readActisense; this->writeActisense=writeActisense; - if (readActisense|| writeActisense){ + if (impl && (readActisense|| writeActisense)){ channelStream=impl->getStream(false); if (! channelStream) { this->readActisense=false; diff --git a/lib/channel/GwChannelList.cpp b/lib/channel/GwChannelList.cpp index 45a2392..6a6b2ef 100644 --- a/lib/channel/GwChannelList.cpp +++ b/lib/channel/GwChannelList.cpp @@ -56,6 +56,7 @@ void GwChannelList::begin(bool fallbackSerial){ logger->setWriter(new GwSerialLog(usb)); logger->prefix="GWSERIAL:"; channel=new GwChannel(logger,"USB",USB_CHANNEL_ID); + channel->setImpl(usb); channel->begin(true, config->getBool(config->sendUsb), config->getBool(config->receiveUsb), @@ -140,9 +141,19 @@ void GwChannelList::begin(bool fallbackSerial){ } //tcp client + bool tclEnabled=config->getBool(config->tclEnabled); channel=new GwChannel(logger,"TCPClient",TCP_CLIENT_CHANNEL_ID); + if (tclEnabled){ + client=new GwTcpClient(logger); + client->begin(TCP_CLIENT_CHANNEL_ID, + config->getString(config->remoteAddress), + config->getInt(config->remotePort), + config->getBool(config->readTCL) + ); + channel->setImpl(client); + } channel->begin( - config->getBool(config->tclEnabled), + tclEnabled, config->getBool(config->sendTCL), config->getBool(config->readTCL), config->getString(config->tclReadFilter), @@ -152,15 +163,7 @@ void GwChannelList::begin(bool fallbackSerial){ false, false ); - if (channel->isEnabled()){ - client=new GwTcpClient(logger); - client->begin(TCP_CLIENT_CHANNEL_ID, - config->getString(config->remoteAddress), - config->getInt(config->remotePort), - channel->shouldRead() - ); - channel->setImpl(client); - } + theChannels.push_back(channel); LOG_DEBUG(GwLog::LOG,"%s",channel->toString().c_str()); logger->flush(); } diff --git a/lib/socketserver/GwTcpClient.cpp b/lib/socketserver/GwTcpClient.cpp index b5d819f..10de8a8 100644 --- a/lib/socketserver/GwTcpClient.cpp +++ b/lib/socketserver/GwTcpClient.cpp @@ -8,7 +8,7 @@ bool GwTcpClient::isConnected(){ } void GwTcpClient::stop() { - if (connection->hasClient()) + if (connection && connection->hasClient()) { LOG_DEBUG(GwLog::DEBUG, "stopping tcp client"); connection->stop(); @@ -17,7 +17,8 @@ void GwTcpClient::stop() } void GwTcpClient::startConnection() { - //TODO + LOG_DEBUG(GwLog::DEBUG,"TcpClient::startConnection to %s:%d", + remoteAddress.c_str(),port); state = C_INITIALIZED; error=""; connectStart=millis(); @@ -50,15 +51,19 @@ void GwTcpClient::startConnection() } state=C_CONNECTING; connection->setClient(sockfd); + LOG_DEBUG(GwLog::DEBUG,"TcpClient connecting..."); } else{ state=C_CONNECTED; connection->setClient(sockfd); + LOG_DEBUG(GwLog::DEBUG,"TcpClient connected"); } } void GwTcpClient::checkConnection() { unsigned long now=millis(); + LOG_DEBUG(GwLog::DEBUG+3,"TcpClient::checkConnection state=%d, start=%ul, now=%ul", + (int)state,connectStart,now); if (! connection->hasClient()){ state = hasConfig()?C_INITIALIZED:C_DISABLED; } @@ -118,6 +123,7 @@ void GwTcpClient::checkConnection() GwTcpClient::GwTcpClient(GwLog *logger) { this->logger = logger; + this->connection=NULL; } GwTcpClient::~GwTcpClient(){ if (connection) diff --git a/lib/socketserver/GwTcpClient.h b/lib/socketserver/GwTcpClient.h index 702baaf..d3488d1 100644 --- a/lib/socketserver/GwTcpClient.h +++ b/lib/socketserver/GwTcpClient.h @@ -3,7 +3,7 @@ #include "GwChannelInterface.h" class GwTcpClient : public GwChannelInterface { - static const unsigned long CON_TIMEOUT=10; + static const unsigned long CON_TIMEOUT=10000; GwSocketConnection *connection = NULL; String remoteAddress; uint16_t port = 0; diff --git a/src/main.cpp b/src/main.cpp index 7a35061..cccb273 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -573,9 +573,9 @@ void setup() { logger.prefix="FALLBACK:"; #endif userCodeHandler.startInitTasks(MIN_USER_TASK); - channels.begin(fallbackSerial); - MDNS.begin(config.getConfigItem(config.systemName)->asCString()); gwWifi.setup(); + MDNS.begin(config.getConfigItem(config.systemName)->asCString()); + channels.begin(fallbackSerial); logger.flush(); webserver.registerMainHandler("/api/reset", [](AsyncWebServerRequest *request)->GwRequestMessage *{ return new ResetRequest(request->arg("_hash"));