Skip to content

Commit 0a810f8

Browse files
committed
Fix: Stop if upstream tcp server closes connection
+ unable to connect again after connection stopped.
1 parent 531c719 commit 0a810f8

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/ArduinoSerialToTCPBridgeClient.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,7 @@ ISR(TIMER1_COMPA_vect) {
2727
}
2828

2929
ArduinoSerialToTCPBridgeClient::ArduinoSerialToTCPBridgeClient() {
30-
ackOutstanding = false;
31-
expectedRxSeqFlag = false;
32-
pubSequence = false;
33-
tx_retries = 0;
34-
rxBufpH = 0;
35-
rxBufpT = 0;
36-
rxBufisFull = false;
37-
state = STATE_DISCONNECTED;
30+
reset();
3831
ser0 = this;
3932
NeoSerial.attachInterrupt(rxISR0);
4033
NeoSerial.begin(115200);
@@ -193,10 +186,9 @@ void ArduinoSerialToTCPBridgeClient::flush() {
193186
}
194187

195188
void ArduinoSerialToTCPBridgeClient::stop() {
196-
stopAckTimer();
197189
writePacket(PROTOCOL_DISCONNECT, NULL, 0);
198190
flush();
199-
state = STATE_DISCONNECTED;
191+
reset();
200192
//NeoSerial.end();
201193
}
202194

@@ -208,6 +200,18 @@ ArduinoSerialToTCPBridgeClient::operator bool() {
208200
return 1;
209201
}
210202

203+
void ArduinoSerialToTCPBridgeClient::reset() {
204+
stopAckTimer();
205+
state = STATE_DISCONNECTED;
206+
ackOutstanding = false;
207+
expectedRxSeqFlag = false;
208+
pubSequence = false;
209+
tx_retries = 0;
210+
rxBufpH = 0;
211+
rxBufpT = 0;
212+
rxBufisFull = false;
213+
}
214+
211215
boolean ArduinoSerialToTCPBridgeClient::writePacket(uint8_t command, uint8_t* payload, uint8_t pLength) {
212216
if (pLength > 250)
213217
return false;
@@ -312,6 +316,11 @@ void ArduinoSerialToTCPBridgeClient::rxCallback(uint8_t c) {
312316
if (p_length == 5)
313317
state = STATE_CONNECTED;
314318
break;
319+
// Upstream tcp connection closed.
320+
case PROTOCOL_DISCONNECT:
321+
if (p_length == 5 && (state == STATE_CONNECTED))
322+
reset();
323+
break;
315324
// Incoming data.
316325
case PROTOCOL_PUBLISH:
317326
writePacket(PROTOCOL_ACK | (p_cmd & 0x80), NULL, 0);

src/ArduinoSerialToTCPBridgeClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class ArduinoSerialToTCPBridgeClient : public Client {
5454
uint8_t rxBufpH, rxBufpT;
5555
boolean rxBufisFull;
5656

57+
void reset();
5758
boolean writePacket(uint8_t command, uint8_t* payload, uint8_t pLength);
5859
void rxCallback(uint8_t c);
5960
void setupAckTimer();

0 commit comments

Comments
 (0)