Skip to content

Commit

Permalink
Clearer assignment of return codes for bus->Send
Browse files Browse the repository at this point in the history
  • Loading branch information
fredlcore committed Mar 15, 2024
1 parent 49071ae commit f7570ba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
32 changes: 18 additions & 14 deletions BSB_LAN/BSB_LAN.ino
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,10 @@

#include <Arduino.h>

#define BUS_OK 1
#define BUS_NOTFREE -1
#define BUS_NOMATCH -2

#define LOGTELEGRAM_OFF 0
#define LOGTELEGRAM_ON 1
#define LOGTELEGRAM_UNKNOWN_ONLY 2
Expand Down Expand Up @@ -3550,7 +3554,7 @@ int set(float line // the ProgNr of the heater parameter
{
uint8_t t=atoi(val);
int8_t return_value = bus->Send(TYPE_QINF, c, msg, tx_msg);
if (return_value < 0) {
if (return_value != BUS_OK) {
printlnToWebClient("No response to initial query, cannot get required data, aborting.");
printFmtToDebug("Error: %d", return_value);
}
Expand Down Expand Up @@ -3610,7 +3614,7 @@ int set(float line // the ProgNr of the heater parameter
, tx_msg // xmit buffer
, param // payload
, param_len // payload length
, setcmd) < 0) // wait_for_reply
, setcmd) != BUS_OK) // wait_for_reply
{
printFmtToDebug(PSTR("set failed\r\n"));
return 0;
Expand Down Expand Up @@ -3665,7 +3669,7 @@ int queryDefaultValue(float line, byte *msg, byte *tx_msg) {
decodedTelegram.error = 258; //not found
return 0;
} else {
if (bus->Send(TYPE_QRV, c, msg, tx_msg) < 0) {
if (bus->Send(TYPE_QRV, c, msg, tx_msg) != BUS_OK) {
decodedTelegram.error = 261; //query failed
return 0;
} else {
Expand Down Expand Up @@ -4136,7 +4140,7 @@ void query(float line) { // line (ProgNr)
if (flags & FL_QINF_ONLY) {
query_type = TYPE_QINF;
}
if (bus->Send(query_type, c, msg, tx_msg) == 1) {
if (bus->Send(query_type, c, msg, tx_msg) == BUS_OK) {
// Decode the xmit telegram and send it to the PC serial interface
if (verbose) {
printTelegram(tx_msg, line);
Expand Down Expand Up @@ -4350,7 +4354,7 @@ void SetDateTime() {
if (bus->getBusType() != BUS_PPS) {
findLine(0,0,&c);
if (c!=CMD_UNKNOWN) { // send only valid command codes
if (bus->Send(TYPE_QUR, c, rx_msg, tx_msg) == 1) {
if (bus->Send(TYPE_QUR, c, rx_msg, tx_msg) == BUS_OK) {
if (bus->getBusType() == BUS_LPB) {
setTime(rx_msg[18], rx_msg[19], rx_msg[20], rx_msg[16], rx_msg[15], rx_msg[14]+1900);
} else {
Expand Down Expand Up @@ -5300,7 +5304,7 @@ void loop() {
flushToWebClient();

uint8_t found_ids[10] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
if (bus->Send(TYPE_QINF, 0x053D0002, msg, tx_msg, NULL, 0, false) == 1) {
if (bus->Send(TYPE_QINF, 0x053D0002, msg, tx_msg, NULL, 0, false) == BUS_OK) {
printTelegram(tx_msg, -1);
unsigned long startquery = millis();
while (millis() - startquery < 10000) {
Expand Down Expand Up @@ -5397,7 +5401,7 @@ void loop() {
#endif
if (((dev_fam != temp_dev_fam && dev_fam != DEV_FAM(DEV_ALL)) || (dev_var != temp_dev_var && dev_var != DEV_VAR(DEV_ALL))) && c!=CMD_UNKNOWN) {
printFmtToDebug(PSTR("%02X\r\n"), c);
if (bus->Send(TYPE_QUR, c, msg, tx_msg) < 0) {
if (bus->Send(TYPE_QUR, c, msg, tx_msg) != BUS_OK) {
print_bus_send_failed();
} else {
if (msg[4+(bus->getBusType()*4)]!=TYPE_ERR) {
Expand Down Expand Up @@ -5441,7 +5445,7 @@ void loop() {
c = 0;
int outBufLen = strlen(outBuf);
unsigned long timeout = millis() + 3000;
while (bus->Send(TYPE_QUR, 0x053D0001, msg, tx_msg) < 0 && (millis() < timeout)) {
while (bus->Send(TYPE_QUR, 0x053D0001, msg, tx_msg) != BUS_OK && (millis() < timeout)) {
printTelegram(tx_msg, -1);
printTelegram(msg, -1);
delay(500);
Expand All @@ -5452,7 +5456,7 @@ void loop() {
printToWebClient(outBuf + outBufLen);
printToWebClient(PSTR("\r\n"));
timeout = millis() + 3000;
while (bus->Send(TYPE_QUR, 0x053D0064, msg, tx_msg) < 0 && (millis() < timeout)) {
while (bus->Send(TYPE_QUR, 0x053D0064, msg, tx_msg) != BUS_OK 0 && (millis() < timeout)) {
printTelegram(tx_msg, -1);
printTelegram(msg, -1);
delay(500);
Expand All @@ -5464,7 +5468,7 @@ void loop() {
printToWebClient(PSTR("\r\n"));
flushToWebClient();
timeout = millis() + 3000;
while (bus->Send(TYPE_IQ1, c, msg, tx_msg) < 0 && (millis() < timeout)) {
while (bus->Send(TYPE_IQ1, c, msg, tx_msg) != BUS_OK && (millis() < timeout)) {
printTelegram(tx_msg, -1);
printTelegram(msg, -1);
printToWebClient(PSTR("Didn't receive matching telegram, resending...\r\n"));
Expand All @@ -5475,7 +5479,7 @@ void loop() {
int IA1_max = (msg[7+bus->getBusType()*4] << 8) + msg[8+bus->getBusType()*4];
if (msg[4+bus->getBusType()*4] == 0x13 && IA1_max > 0) {
timeout = millis() + 3000;
while (bus->Send(TYPE_IQ2, c, msg, tx_msg) < 0 && (millis() < timeout)) {
while (bus->Send(TYPE_IQ2, c, msg, tx_msg) != BUS_OK && (millis() < timeout)) {
printToWebClient(PSTR("Didn't receive matching telegram, resending...\r\n"));
delay(500);
}
Expand All @@ -5487,7 +5491,7 @@ void loop() {
esp_task_wdt_reset();
#endif
timeout = millis() + 3000;
while (bus->Send(TYPE_IQ1, IA1_counter, msg, tx_msg) < 0 && (millis() < timeout)) {
while (bus->Send(TYPE_IQ1, IA1_counter, msg, tx_msg) != BUS_OK && (millis() < timeout)) {
printToWebClient(PSTR("Didn't receive matching telegram, resending...\r\n"));
delay(500);
}
Expand All @@ -5501,7 +5505,7 @@ void loop() {
esp_task_wdt_reset();
#endif
timeout = millis() + 3000;
while (bus->Send(TYPE_IQ2, IA2_counter, msg, tx_msg) < 0 && (millis() < timeout)) {
while (bus->Send(TYPE_IQ2, IA2_counter, msg, tx_msg) != BUS_OK && (millis() < timeout)) {
printToWebClient(PSTR("Didn't receive matching telegram, resending...\r\n"));
delay(500);
}
Expand Down Expand Up @@ -5538,7 +5542,7 @@ void loop() {
}
}
int8_t return_value = bus->Send(type, c, msg, tx_msg, param, param_len, true);
if (return_value < 0) {
if (return_value != BUS_OK) {
print_bus_send_failed();
} else {
// Decode the xmit telegram and send it to the PC serial interface
Expand Down
14 changes: 7 additions & 7 deletions BSB_LAN/src/BSB/bsb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ inline int8_t BSB::_send(byte* msg) {
waitfree = random(1,20) + 3 + 59; // range 63 .. 82 ms, BSB mimimum delay between telegrams is 59 ms (25 for LPB -> miwi), plus duration of one full (32 bytes) telegram (3 ms), plus random amount of 1-20 ms.
{ // block begins
if(millis()-start_timer > timeoutabort){ // one second has elapsed
return -1;
return BUS_NOTFREE;
}
if (bus_type != 2) {
unsigned long timeout = millis();
Expand Down Expand Up @@ -503,7 +503,7 @@ Ob damit weiterhin eine Bus-Kollisionserkennung möglich ist, muss noch geprüft
char c = readByte();
c = c;
}
return 1; // In case we emulate a DC225, we are regularly sending single byte (0x17) messages, so abort loop after first byte.
return BUS_OK; // In case we emulate a DC225, we are regularly sending single byte (0x17) messages, so abort loop after first byte.
}
/*
// if ((HwSerial == true && rx_pin_read() == false) || (HwSerial == false && rx_pin_read())) { // Test RX pin (logical 1 is 0 with HardwareSerial and 1 with SoftwareSerial inverted)
Expand Down Expand Up @@ -555,7 +555,7 @@ Ob damit weiterhin eine Bus-Kollisionserkennung möglich ist, muss noch geprüft
sei();
#endif
}
return 1;
return BUS_OK;
}

int8_t BSB::Send(uint8_t type, uint32_t cmd, byte* rx_msg, byte* tx_msg, byte* param, byte param_len, bool wait_for_reply) {
Expand Down Expand Up @@ -635,7 +635,7 @@ int8_t BSB::Send(uint8_t type, uint32_t cmd, byte* rx_msg, byte* tx_msg, byte* p
i--;
byte msg_type = rx_msg[4+(bus_type*4)];
if (rx_msg[2] == myAddr && ((type == 0x12 && msg_type == 0x13) || (type=0x14 && msg_type == 0x15))) {
return 1;
return BUS_OK;
}
if (bus_type == 1) {
/* Activate for LPB systems with truncated error messages (no commandID in return telegram)
Expand All @@ -644,7 +644,7 @@ int8_t BSB::Send(uint8_t type, uint32_t cmd, byte* rx_msg, byte* tx_msg, byte* p
}
*/
if (rx_msg[2] == myAddr && rx_msg[9] == A2 && rx_msg[10] == A1 && rx_msg[11] == A3 && rx_msg[12] == A4) {
return 1;
return BUS_OK;
} else {
#if DEBUG_LL
Serial.println(F("Message received, but not for us:"));
Expand All @@ -653,7 +653,7 @@ int8_t BSB::Send(uint8_t type, uint32_t cmd, byte* rx_msg, byte* tx_msg, byte* p
}
} else {
if ((rx_msg[2] == myAddr) && (rx_msg[5] == A2) && (rx_msg[6] == A1) && (rx_msg[7] == A3) && (rx_msg[8] == A4)) {
return 1;
return BUS_OK;
} else {
#if DEBUG_LL
Serial.println(F("Message received, but not for us:"));
Expand All @@ -670,7 +670,7 @@ int8_t BSB::Send(uint8_t type, uint32_t cmd, byte* rx_msg, byte* tx_msg, byte* p
#endif
print(tx_msg);

return -2;
return BUS_NOMATCH;
}

bool BSB::rx_pin_read() { // not tested if this will work on ESP32
Expand Down

0 comments on commit f7570ba

Please sign in to comment.