Skip to content

Commit

Permalink
[SYS] Externalize utils function keep cmpToMainTopic (1technophile#2048)
Browse files Browse the repository at this point in the history
And fill generateHash

Co-authored-by: Florian <[email protected]>
  • Loading branch information
2 people authored and odoral committed Oct 18, 2024
1 parent 65a9a62 commit 83618af
Show file tree
Hide file tree
Showing 23 changed files with 162 additions and 41 deletions.
24 changes: 16 additions & 8 deletions lib/TheengsUtils/TheengsUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,25 @@ std::string TheengsUtils::processCert(const char* cert) {
return certStr;
}

#ifdef ESP32
# include "mbedtls/sha256.h"

std::string TheengsUtils::generateHash(const std::string& input) {
// Implementation depends on your hash function
// This is a placeholder
return "hash_placeholder";
}
unsigned char hash[32];
mbedtls_sha256((unsigned char*)input.c_str(), input.length(), hash, 0);

char hashString[65]; // Room for null terminator
for (int i = 0; i < 32; ++i) {
sprintf(&hashString[i * 2], "%02x", hash[i]);
}

bool TheengsUtils::cmpToMainTopic(const char* topicOri, const char* toAdd) {
// Implementation depends on mqtt_topic and gateway_name
// You might need to pass these as parameters or make them class members
return false;
return std::string(hashString);
}
#else
std::string TheengsUtils::generateHash(const std::string& input) {
return "Not implemented for ESP8266";
}
#endif

unsigned long TheengsUtils::uptime() {
static unsigned long lastUptime = 0;
Expand Down
1 change: 0 additions & 1 deletion lib/TheengsUtils/TheengsUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class TheengsUtils {
static String toString(uint64_t input);
static std::string processCert(const char* cert);
static std::string generateHash(const std::string& input);
static bool cmpToMainTopic(const char* topicOri, const char* toAdd);
static unsigned long uptime();
static void syncNTP();
static int unixtimestamp();
Expand Down
8 changes: 4 additions & 4 deletions main/ZactuatorFASTLED.ino
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void MQTTtoFASTLED(char* topicOri, JsonObject& jsonData) {
//trc(topicOri);
//number = (long)strtol(&datacallback[1], NULL, 16);

if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoFASTLEDsetled)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoFASTLEDsetled)) {
Log.trace(F("MQTTtoFASTLED JSON analysis" CR));
int ledNr = jsonData["led"];
Log.notice(F("Led numero: %d" CR), ledNr);
Expand All @@ -126,19 +126,19 @@ void MQTTtoFASTLED(char* topicOri, char* datacallback) {
Log.trace(F("MQTTtoFASTLED: " CR));
currentLEDState = GENERAL;
long number = 0;
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoFASTLED)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoFASTLED)) {
number = (long)strtol(&datacallback[1], NULL, 16);
Log.notice(F("Number: %l" CR), number);
for (int i = 0; i < FASTLED_NUM_LEDS; i++) {
leds[i] = number;
}
FastLED.show();
} else if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoFASTLEDsetbrightness)) {
} else if (cmpToMainTopic(topicOri, subjectMQTTtoFASTLEDsetbrightness)) {
number = (long)strtol(&datacallback[1], NULL, 16);
Log.notice(F("Number: %l" CR), number);
FastLED.setBrightness(number);
FastLED.show();
} else if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoFASTLEDsetanimation)) {
} else if (cmpToMainTopic(topicOri, subjectMQTTtoFASTLEDsetanimation)) {
String payload = datacallback;
Log.notice(F("Datacallback: %s" CR), datacallback);
if (strstr(datacallback, "fire") != NULL) {
Expand Down
6 changes: 3 additions & 3 deletions main/ZactuatorONOFF.ino
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void setupONOFF() {

# if jsonReceiving
void MQTTtoONOFF(char* topicOri, JsonObject& ONOFFdata) {
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoONOFF)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoONOFF)) {
Log.trace(F("MQTTtoONOFF json data analysis" CR));
int boolSWITCHTYPE = ONOFFdata["cmd"] | 99;
int gpio = ONOFFdata["gpio"] | ACTUATOR_ONOFF_GPIO;
Expand Down Expand Up @@ -179,7 +179,7 @@ void MQTTtoONOFF(char* topicOri, JsonObject& ONOFFdata) {
}
}
}
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoONOFFset)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoONOFFset)) {
Log.trace(F("MQTTtoONOFF json set" CR));
/*
* Configuration modifications priorities:
Expand All @@ -204,7 +204,7 @@ void MQTTtoONOFF(char* topicOri, JsonObject& ONOFFdata) {

# if simpleReceiving
void MQTTtoONOFF(char* topicOri, char* datacallback) {
if ((TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoONOFF))) {
if ((cmpToMainTopic(topicOri, subjectMQTTtoONOFF))) {
Log.trace(F("MQTTtoONOFF" CR));
char* endptr = NULL;
long gpio = strtol(datacallback, &endptr, 10);
Expand Down
4 changes: 2 additions & 2 deletions main/ZactuatorPWM.ino
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ boolean PWMtoMQTT() {

# if jsonReceiving
void MQTTtoPWM(char* topicOri, JsonObject& jsonData) {
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoPWMset)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoPWMset)) {
Log.trace(F("MQTTtoPWM JSON analysis" CR));
// Parse the target value for each channel
int modifiedChannelBits = 0;
Expand Down Expand Up @@ -230,7 +230,7 @@ void MQTTtoPWM(char* topicOri, JsonObject& jsonData) {
}
}
fadeIsComplete = false; // The values will start to change during PWMLoop
} else if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoPWMcalibrate)) {
} else if (cmpToMainTopic(topicOri, subjectMQTTtoPWMcalibrate)) {
// Read the optional calibration data for each channel
for (int i = 0; i < kNumChannels; ++i) {
char key[64];
Expand Down
2 changes: 1 addition & 1 deletion main/ZactuatorSomfy.ino
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void setupSomfy() {

# if jsonReceiving
void MQTTtoSomfy(char* topicOri, JsonObject& jsonData) {
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoSomfy)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoSomfy)) {
Log.trace(F("MQTTtoSomfy json data analysis" CR));
float txFrequency = jsonData["frequency"] | RFConfig.frequency;
# ifdef ZradioCC1101 // set Receive off and Transmitt on
Expand Down
2 changes: 1 addition & 1 deletion main/ZboardM5.ino
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void loopM5() {
}

void MQTTtoM5(char* topicOri, JsonObject& M5data) { // json object decoding
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoM5set)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoM5set)) {
Log.trace(F("MQTTtoM5 json set" CR));
// Log display set between M5 lcd (true) and serial monitor (false)
if (M5data.containsKey("log-lcd")) {
Expand Down
2 changes: 1 addition & 1 deletion main/ZcommonRF.ino
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ void RFConfig_load() {
}

void MQTTtoRFset(char* topicOri, JsonObject& RFdata) {
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoRFset)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoRFset)) {
Log.trace(F("MQTTtoRF json set" CR));

/*
Expand Down
2 changes: 1 addition & 1 deletion main/ZdisplaySSD1306.ino
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Handler for mqtt commands sent to the module
*/
void MQTTtoSSD1306(char* topicOri, JsonObject& SSD1306data) { // json object decoding
bool success = false;
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoSSD1306set)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoSSD1306set)) {
Log.trace(F("MQTTtoSSD1306 json set" CR));
// properties
if (SSD1306data.containsKey("onstate")) {
Expand Down
4 changes: 2 additions & 2 deletions main/Zgateway2G.ino
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void MQTTto2G(char* topicOri, char* datacallback) {
String data = datacallback;
String topic = topicOri;

if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTto2G)) {
if (cmpToMainTopic(topicOri, subjectMQTTto2G)) {
Log.trace(F("MQTTto2G data analysis" CR));
// 2G DATA ANALYSIS
String phone_number = "";
Expand Down Expand Up @@ -128,7 +128,7 @@ void MQTTto2G(char* topicOri, char* datacallback) {

# if jsonReceiving
void MQTTto2G(char* topicOri, JsonObject& SMSdata) {
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTto2G)) {
if (cmpToMainTopic(topicOri, subjectMQTTto2G)) {
const char* sms = SMSdata["message"];
const char* phone = SMSdata["phone"];
Log.trace(F("MQTTto2G json data analysis" CR));
Expand Down
9 changes: 9 additions & 0 deletions main/ZgatewayBLEConnect.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,14 @@ class SBBT_connect : public zBLEConnect {
bool processActions(std::vector<BLEAction>& actions) override;
};

class SBCU_connect : public zBLEConnect {
uint8_t m_notifyVal;
void notifyCB(NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify);

public:
SBCU_connect(NimBLEAddress& addr) : zBLEConnect(addr) {}
bool processActions(std::vector<BLEAction>& actions) override;
};

#endif //ESP32
#endif //zBLEConnect_h
89 changes: 89 additions & 0 deletions main/ZgatewayBLEConnect.ino
Original file line number Diff line number Diff line change
Expand Up @@ -609,5 +609,94 @@ bool SBBT_connect::processActions(std::vector<BLEAction>& actions) {
return result;
}

/*-----------------------SBCU HANDLING-----------------------*/
void SBCU_connect::notifyCB(NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) {
if (m_taskHandle == nullptr) {
return; // unexpected notification
}
if (!BTProcessLock) {
Log.trace(F("Callback from %s characteristic" CR), pChar->getUUID().toString().c_str());

if (length) {
m_notifyVal = *pData;
} else {
Log.notice(F("Invalid notification data" CR));
return;
}
} else {
Log.trace(F("Callback process canceled by BTProcessLock" CR));
}

xTaskNotifyGive(m_taskHandle);
}

bool SBCU_connect::processActions(std::vector<BLEAction>& actions) {
NimBLEUUID serviceUUID("cba20d00-224d-11e6-9fb8-0002a5d5c51b");
NimBLEUUID charUUID("cba20002-224d-11e6-9fb8-0002a5d5c51b");
NimBLEUUID notifyCharUUID("cba20003-224d-11e6-9fb8-0002a5d5c51b");
static byte CLOSE[] = {0x57, 0x0f, 0x45, 0x01, 0x01, 0x01, 0x64};
static byte OPEN[] = {0x57, 0x0f, 0x45, 0x01, 0x01, 0x01, 0x00};
static byte MOVE[] = {0x57, 0x0f, 0x45, 0x01, 0x01, 0x01, 0x00};
static byte STOP[] = {0x57, 0x0f, 0x45, 0x01, 0x01, 0x00, 0x01};

bool result = false;
if (actions.size() > 0) {
for (auto& it : actions) {
if (NimBLEAddress(it.addr) == m_pClient->getPeerAddress()) {
NimBLERemoteCharacteristic* pChar = getCharacteristic(serviceUUID, charUUID);
NimBLERemoteCharacteristic* pNotifyChar = getCharacteristic(serviceUUID, notifyCharUUID);
int value = -1;
if (it.value_type == BLE_VAL_INT) {
value = std::stoi(it.value);
}
if (it.write && pChar && pNotifyChar) {
Log.trace(F("processing Switchbot %s" CR), it.value.c_str());
if (pNotifyChar->subscribe(true,
std::bind(&SBCU_connect::notifyCB,
this, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3, std::placeholders::_4),
true)) {
if (it.value == "open" && it.value_type == BLE_VAL_STRING) {
result = pChar->writeValue(OPEN, 7, false);
value = 100;
} else if (it.value == "close" && it.value_type == BLE_VAL_STRING) {
result = pChar->writeValue(CLOSE, 7, false);
value = 0;
} else if (it.value == "stop" && it.value_type == BLE_VAL_STRING) {
result = pChar->writeValue(STOP, 7, false);
} else if (it.value_type == BLE_VAL_INT) {
if (value >= 0 && value <= 100) {
byte posByte = (byte)value;
MOVE[6] = posByte;
result = pChar->writeValue(MOVE, 7, false);
}
}

if (result) {
m_taskHandle = xTaskGetCurrentTaskHandle();
if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(BLE_CNCT_TIMEOUT)) == pdFALSE) {
m_taskHandle = nullptr;
}
result = m_notifyVal == 0x01;
}
}
}

it.complete = result;
if (result || it.ttl <= 1) {
StaticJsonDocument<JSON_MSG_BUFFER> BLEdataBuffer;
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
BLEdata["id"] = it.addr;
BLEdata["position"] = value;
buildTopicFromId(BLEdata, subjectBTtoMQTT);
enqueueJsonObject(BLEdata, QueueSemaphoreTimeOutTask);
}
}
}
}

return result;
}

# endif //ZgatewayBT
#endif //ESP32
4 changes: 2 additions & 2 deletions main/ZgatewayBT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,7 @@ void MQTTtoBTAction(JsonObject& BTdata) {
}

void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoBTset)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoBTset)) {
Log.trace(F("MQTTtoBT json set" CR));

// Black list & white list set
Expand Down Expand Up @@ -1512,7 +1512,7 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding
// Load config from json if available
BTConfig_fromJson(BTdata);

} else if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoBT)) {
} else if (cmpToMainTopic(topicOri, subjectMQTTtoBT)) {
KnownBTActions(BTdata);
MQTTtoBTAction(BTdata);
}
Expand Down
2 changes: 1 addition & 1 deletion main/ZgatewayIR.ino
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ bool sendIdentifiedProtocol(const char* protocol_name, uint64_t data, const char

# if jsonReceiving
void MQTTtoIR(char* topicOri, JsonObject& IRdata) {
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoIR)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoIR)) {
Log.trace(F("MQTTtoIR json" CR));
uint64_t data = IRdata["value"];
const char* raw = IRdata["raw"];
Expand Down
6 changes: 3 additions & 3 deletions main/ZgatewayLORA.ino
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ void LORAtoMQTT() {

# if jsonReceiving
void MQTTtoLORA(char* topicOri, JsonObject& LORAdata) { // json object decoding
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoLORA)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoLORA)) {
Log.trace(F("MQTTtoLORA json" CR));
const char* message = LORAdata["message"];
const char* hex = LORAdata["hex"];
Expand Down Expand Up @@ -512,7 +512,7 @@ void MQTTtoLORA(char* topicOri, JsonObject& LORAdata) { // json object decoding
Log.error(F("MQTTtoLORA Fail json" CR));
}
}
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoLORAset)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoLORAset)) {
Log.trace(F("MQTTtoLORA json set" CR));
/*
* Configuration modifications priorities:
Expand All @@ -536,7 +536,7 @@ void MQTTtoLORA(char* topicOri, JsonObject& LORAdata) { // json object decoding
# endif
# if simpleReceiving
void MQTTtoLORA(char* topicOri, char* LORAarray) { // json object decoding
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoLORA)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoLORA)) {
LoRa.beginPacket();
LoRa.print(LORAarray);
LoRa.endPacket();
Expand Down
4 changes: 2 additions & 2 deletions main/ZgatewayPilight.ino
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void PilighttoMQTT() {
}

void MQTTtoPilight(char* topicOri, JsonObject& Pilightdata) {
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoPilightProtocol)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoPilightProtocol)) {
bool success = false;
if (Pilightdata.containsKey("reset")) {
rf.limitProtocols(rf.availableProtocols());
Expand Down Expand Up @@ -194,7 +194,7 @@ void MQTTtoPilight(char* topicOri, JsonObject& Pilightdata) {
pub(subjectGTWPilighttoMQTT, "{\"Status\": \"Error\"}"); // Fail feedback
Log.error(F("MQTTtoPilightProtocol Fail json" CR));
}
} else if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoPilight)) {
} else if (cmpToMainTopic(topicOri, subjectMQTTtoPilight)) {
const char* message = Pilightdata["message"];
Log.notice(F("MQTTtoPilight message: %s" CR), message);
const char* protocol = Pilightdata["protocol"];
Expand Down
4 changes: 2 additions & 2 deletions main/ZgatewayRF.ino
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ void MQTTtoRF(char* topicOri, char* datacallback) {
valueBITS = (topic.substring(pos3, pos3 + 2)).toInt();
}

if ((TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoRF)) && (valuePRT == 0) && (valuePLSL == 0) && (valueBITS == 0)) {
if ((cmpToMainTopic(topicOri, subjectMQTTtoRF)) && (valuePRT == 0) && (valuePLSL == 0) && (valueBITS == 0)) {
Log.trace(F("MQTTtoRF dflt" CR));
mySwitch.setProtocol(1, 350);
mySwitch.send(data, 24);
Expand Down Expand Up @@ -225,7 +225,7 @@ void MQTTtoRF(char* topicOri, char* datacallback) {

# if jsonReceiving
void MQTTtoRF(char* topicOri, JsonObject& RFdata) { // json object decoding
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoRF)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoRF)) {
Log.trace(F("MQTTtoRF json" CR));
uint64_t data = RFdata["value"];
if (data != 0) {
Expand Down
2 changes: 1 addition & 1 deletion main/ZgatewayRF2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ void MQTTtoRF2(char* topicOri, char* datacallback) {
# if jsonReceiving
void MQTTtoRF2(char* topicOri, JsonObject& RF2data) { // json object decoding

if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoRF2)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoRF2)) {
Log.trace(F("MQTTtoRF2 json" CR));
int boolSWITCHTYPE = RF2data["switchType"] | 99;
bool success = false;
Expand Down
4 changes: 2 additions & 2 deletions main/ZgatewayRFM69.ino
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ bool RFM69toMQTT(void) {

# if simpleReceiving
void MQTTtoRFM69(char* topicOri, char* datacallback) {
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoRFM69)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoRFM69)) {
Log.trace(F("MQTTtoRFM69 data analysis" CR));
char data[RF69_MAX_DATA_LEN + 1];
memcpy(data, (void*)datacallback, RF69_MAX_DATA_LEN);
Expand Down Expand Up @@ -201,7 +201,7 @@ void MQTTtoRFM69(char* topicOri, char* datacallback) {
# endif
# if jsonReceiving
void MQTTtoRFM69(char* topicOri, JsonObject& RFM69data) {
if (TheengsUtils::cmpToMainTopic(topicOri, subjectMQTTtoRFM69)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoRFM69)) {
const char* data = RFM69data["data"];
Log.trace(F("MQTTtoRFM69 json data analysis" CR));
if (data) {
Expand Down
Loading

0 comments on commit 83618af

Please sign in to comment.