Skip to content

Commit 43d51a4

Browse files
committed
#215 ability to set heartbeat frequency at runtime
1 parent aac974b commit 43d51a4

File tree

7 files changed

+47
-14
lines changed

7 files changed

+47
-14
lines changed

examples/esp/esp32Amplifier/esp32Amplifier.ino

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,16 @@ void setup() {
6262
menuVolume.setCurrentValue(20, true);
6363
menuDirect.setBoolean(true, true);
6464
});
65+
6566
prepareWifiForUse();
6667

68+
// option 1 globally set HB timeouts.
69+
remoteServer.setHeartbeatIntervalAll(30000);
70+
71+
// option 2 selectively set HB timeouts.
72+
//auto tvRemote = remoteServer.getRemoteConnector(0);
73+
//if(tvRemote != nullptr) tvRemote->setHeartbeatTimeout(30000);
74+
6775
controller.initialise();
6876

6977
#if MENU_USING_CALIBRATION_MGR == true
@@ -101,19 +109,19 @@ void setup() {
101109
// use a 4BPP (16 color) icon bitmap by providing both the data and the palette.
102110
themeBuilder.menuItemOverride(menuSettings)
103111
.withImage4bpp(Coord(31, 40), statusBitmap_palette0, statusBitmap0)
104-
.onRow(3).multiCol(1, 3)
112+
.onRowCol(3, 1, 3)
105113
.apply();
106114

107115
// Again we take a menu item override for the status submenu item, and this time it will render a single color bitmap
108116
themeBuilder.menuItemOverride(menuStatus)
109117
.withImageXbmp(iconSize, statusIcon40Bits)
110-
.onRow(3).multiCol(2, 3)
118+
.onRowCol(3, 2, 3)
111119
.apply();
112120

113121
// Again we take a menu item override for the mute boolean menu, and this time it will render a single color bitmap
114122
themeBuilder.menuItemOverride(menuMute)
115123
.withImageXbmp(iconSize, muteOffIcon40Bits, muteOnIcon40Bits)
116-
.onRow(3).multiCol(3, 3)
124+
.onRowCol(3, 3, 3)
117125
.apply();
118126

119127
/**
@@ -158,13 +166,13 @@ void prepareWifiForUse() {
158166
if(strlen(menuConnectivitySSID.getTextValue())==0) {
159167
// no SSID come up as an access point
160168
WiFi.mode(WIFI_AP);
161-
WiFi.softAP("tcmenu", "secret");
169+
WiFi.softAP("tcmenu", "secret1234");
162170
serdebugF("Started up in AP mode, connect with 'tcmenu' and 'secret'");
163171
}
164172
else {
165-
WiFi.begin(menuConnectivitySSID.getTextValue(), menuConnectivityPasscode.getTextValue());
166173
WiFi.mode(WIFI_STA);
167-
serdebugF("Connecting to Wifi using settings from connectivity menu");
174+
WiFi.begin(menuConnectivitySSID.getTextValue(), menuConnectivityPasscode.getTextValue());
175+
serdebugF2("Connecting to Wifi using settings for ", menuConnectivitySSID.getTextValue());
168176
}
169177

170178
// now monitor the wifi level every few seconds and report it in a widget.

examples/esp/esp32Amplifier/esp32Amplifier_menu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ iotouch::ResistiveTouchInterrogator touchInterrogator(2, 33, 32, 0);
2424
iotouch::TouchOrientationSettings touchOrientation(true, false, true);
2525
MenuTouchScreenManager touchScreen(&touchInterrogator, &renderer, touchOrientation);
2626
tcextras::IoaTouchScreenCalibrator touchCalibrator(&touchScreen, &renderer, 400);
27-
ClientEthernetInitialisation ethernetInitialisation("192.168.0.99", 3333);
27+
ClientEthernetInitialisation ethernetInitialisation("192.168.0.37", 3333);
2828
ClientEthernetTagValTransport ethernetTransport;
2929
TagValueRemoteServerConnection ethernetConnection(ethernetTransport, ethernetInitialisation);
3030

examples/esp/esp32Amplifier/esp32Amplifier_menu.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ extern tcextras::IoaTouchScreenCalibrator touchCalibrator;
3838
extern WiFiServer server;
3939
extern ClientEthernetInitialisation ethernetInitialisation;
4040

41+
42+
extern EepromAuthenticatorManager authManager;
43+
44+
45+
4146
// Any externals needed by IO expanders, EEPROMs etc
4247

4348

src/RemoteConnector.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ TagValueRemoteConnector::TagValueRemoteConnector(uint8_t remoteNo) :
5050
this->flags = 0;
5151
this->commsCallback = nullptr;
5252
this->authManager = nullptr;
53+
this->hbTimeoutTicks = HEARTBEAT_INTERVAL;
5354
}
5455

5556
void TagValueRemoteConnector::initialise(TagValueTransport* transport_, CombinedMessageProcessor* processor_,
@@ -223,8 +224,8 @@ void TagValueRemoteConnector::dealWithHeartbeating() {
223224
++ticksLastRead;
224225
++ticksLastSend;
225226

226-
// pairing will not send heartbeats, so we wait about 10 seconds before closing out
227-
unsigned int maximumWaitTime = isPairing() ? (PAIRING_TIMEOUT_TICKS) : (HEARTBEAT_INTERVAL_TICKS * 3);
227+
// when pairing the timeout is hardwired to 15 seconds, otherwise we wait a multiplier of the heartbeat frequency
228+
unsigned int maximumWaitTime = isPairing() ? (PAIRING_TIMEOUT_TICKS) : (hbTimeoutTicks > 10000) ? hbTimeoutTicks * 2U : hbTimeoutTicks * 3U;
228229

229230
if(ticksLastRead > maximumWaitTime && isConnected()) {
230231
serlogF3(SER_NETWORK_INFO, "Remote disconnected (rNo, ticks): ", remoteNo, ticksLastSend);
@@ -238,7 +239,7 @@ void TagValueRemoteConnector::dealWithHeartbeating() {
238239
setConnected(true);
239240
}
240241

241-
if(ticksLastSend > HEARTBEAT_INTERVAL_TICKS) {
242+
if(ticksLastSend > hbTimeoutTicks) {
242243
if(isConnectionFullyEstablished() && transport->available()) {
243244
serlogF3(SER_NETWORK_INFO, "Sending HB (rNo, ticks) : ", remoteNo, ticksLastSend);
244245
encodeHeartbeat(HBMODE_NORMAL);
@@ -383,7 +384,7 @@ void TagValueRemoteConnector::encodeBootstrap(bool isComplete) {
383384

384385
void TagValueRemoteConnector::encodeHeartbeat(HeartbeatMode hbMode) {
385386
if(!prepareWriteMsg(MSG_HEARTBEAT)) return;
386-
transport->writeFieldInt(FIELD_HB_INTERVAL, HEARTBEAT_INTERVAL);
387+
transport->writeFieldInt(FIELD_HB_INTERVAL, hbTimeoutTicks);
387388
transport->writeFieldLong(FIELD_HB_MILLISEC, millis());
388389
transport->writeFieldInt(FIELD_HB_MODE, hbMode);
389390
transport->endMsg();

src/RemoteConnector.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@
2525
#define START_OF_MESSAGE 0x01
2626
#define TICK_INTERVAL 1
2727

28-
// when debugging to reduce disconnects set the following (give 1 minute timeout): -DHEARTBEAT_INTERVAL=20000
28+
// This sets the DEFAULT heartbeat interval when no other interval has been set on a connection. IE if you create a
29+
// connection and do not call setHeartbeatInterval(millis), this is what you'll get.
2930
#ifndef HEARTBEAT_INTERVAL
3031
# define HEARTBEAT_INTERVAL 1500
3132
#endif
3233

33-
#define HEARTBEAT_INTERVAL_TICKS (HEARTBEAT_INTERVAL / TICK_INTERVAL)
34+
// Pairing is different to a normal connection, in that we just need to provide the user with reasonable time to
35+
// accept the pairing request on a device, usually around 15 seconds is enough. You can redefine if needed.
36+
#ifndef PAIRING_TIMEOUT_TICKS
3437
#define PAIRING_TIMEOUT_TICKS (15000 / TICK_INTERVAL)
38+
#endif
3539

3640
/**
3741
* @file RemoteConnector.h
@@ -185,7 +189,8 @@ class TagValueRemoteConnector {
185189
const ConnectorLocalInfo* localInfoPgm;
186190
uint16_t ticksLastSend;
187191
uint16_t ticksLastRead;
188-
CombinedMessageProcessor* processor;
192+
uint16_t hbTimeoutTicks;
193+
CombinedMessageProcessor* processor;
189194
TagValueTransport* transport;
190195
CommsCallbackFn commsCallback;
191196
AuthenticationManager* authManager;
@@ -480,6 +485,8 @@ class TagValueRemoteConnector {
480485
/** indicates if the connection is yet authenicated */
481486
bool isAuthenticated() { return bitRead(flags, FLAG_AUTHENTICATED); }
482487
AuthenticationManager* getAuthManager() { return authManager; }
488+
489+
void setHeartbeatTimeout(uint16_t milli) { hbTimeoutTicks = milli / TICK_INTERVAL; }
483490
private:
484491
void encodeBaseMenuFields(int parentId, MenuItem* item);
485492
bool prepareWriteMsg(uint16_t msgType);

src/remote/BaseRemoteComponents.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ void TcMenuRemoteServer::exec() {
8484
}
8585
}
8686

87+
void TcMenuRemoteServer::setHeartbeatIntervalAll(uint16_t milli) {
88+
serlogF2(SER_NETWORK_INFO, "Heartbeats set globally: ", milli);
89+
for (int i = 0; i < remotesAdded; i++) {
90+
if(connections[i] && connections[i]->getRemoteServerType() == TAG_VAL_REMOTE_SERVER) {
91+
auto tvCon = reinterpret_cast<TagValueRemoteServerConnection*>(connections[i]);
92+
tvCon->connector()->setHeartbeatTimeout(milli);
93+
}
94+
}
95+
}
96+
8797
int tcremote::fromWiFiRSSITo4StateIndicator(int strength) {
8898
int qualityIcon = 0;
8999
if(strength > -50) qualityIcon = 4;

src/remote/BaseRemoteComponents.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ namespace tcremote {
180180
if(num >= remotesAdded) return nullptr;
181181
return connections[num];
182182
}
183+
184+
void setHeartbeatIntervalAll(uint16_t milli);
183185
};
184186

185187
/**

0 commit comments

Comments
 (0)