diff --git a/README.md b/README.md index c7f35c9..ad374dc 100644 --- a/README.md +++ b/README.md @@ -37,12 +37,18 @@ In order to connect to your MQTT Broker, first create a file called `mqtt.h` in #define MQTT_PASSWORD "password" #define MQTT_AUTH_TOPIC "topic/auth" #define MQTT_SET_STATE_TOPIC "topic/set_state" +#define MQTT_SET_TARGET_STATE_TOPIC "topic/set_target_state" +#define MQTT_SET_CURRENT_STATE_TOPIC "topic/set_current_state" #define MQTT_STATE_TOPIC "topic/state" ``` `MQTT_AUTH_TOPIC` is where authentication details will be published on success - `MQTT_SET_STATE_TOPIC` is the control topic from which the state of the lock can be changed + `MQTT_SET_STATE_TOPIC` is the control topic that sets the current and target state of the lock + + `MQTT_SET_TARGET_STATE_TOPIC` controls the target state of the lock + + `MQTT_SET_CURRENT_STATE_TOPIC` controls the current state of the lock `MQTT_STATE_TOPIC` is where changes to the lock state will be published. diff --git a/src/auth/authContext.cpp b/src/auth/authContext.cpp index 80b9bc7..82c6d14 100644 --- a/src/auth/authContext.cpp +++ b/src/auth/authContext.cpp @@ -1,5 +1,4 @@ #include -#include "authContext.h" /** * The function `get_public_points` takes a public key as input, reads the X and Y coordinates of the diff --git a/src/main.cpp b/src/main.cpp index 0c3eccf..6c6433e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -108,13 +108,30 @@ struct LockMechanism : Service::LockMechanism new Characteristic::Name("NFC Lock"); lockCurrentState = new Characteristic::LockCurrentState(0, true); lockTargetState = new Characteristic::LockTargetState(); - mqtt.subscribe(MQTT_SET_STATE_TOPIC, [this](const char *payload) - { + mqtt.subscribe( + MQTT_SET_STATE_TOPIC, [this](const char *payload) + { Serial.printf("\nReceived message in topic set_state: %s\n", payload); int state = atoi(payload); - lockTargetState->setVal(state == 0 || state == 1 ? state : lockTargetState->getVal()); + lockTargetState->setVal(state == 0 || state == 1 ? state : lockTargetState->getVal(), false); lockCurrentState->setVal(state == 0 || state == 1 ? state : lockCurrentState->getVal()); - }); + }, + false); + mqtt.subscribe( + MQTT_SET_TARGET_STATE_TOPIC, [this](const char *payload) + { + Serial.printf("\nReceived message in topic set_target_state: %s\n", payload); + int state = atoi(payload); + lockTargetState->setVal(state == 0 || state == 1 ? state : lockTargetState->getVal(), false); + }, + false); + mqtt.subscribe( + MQTT_SET_CURRENT_STATE_TOPIC, [this](const char *payload) + { + Serial.printf("\nReceived message in topic set_target_state: %s\n", payload); + int state = atoi(payload); + lockCurrentState->setVal(state == 0 || state == 1 ? state : lockCurrentState->getVal()); }, + false); } // end constructor boolean update(std::vector *callback, int *callbackLen) @@ -163,7 +180,9 @@ struct LockMechanism : Service::LockMechanism unsigned long stopTime = millis(); ESP_LOGI(TAG, "Transaction took %lu ms", stopTime - startTime); ESP_LOGI(TAG, "Device has been authenticated, toggling lock state"); - mqtt.publish(MQTT_STATE_TOPIC, std::to_string(lockTargetState->getNewVal()).c_str()); + int newTargetState = lockTargetState->getNewVal(); + int targetState = lockTargetState->getVal(); + mqtt.publish(MQTT_STATE_TOPIC, std::to_string(newTargetState == targetState ? !lockCurrentState->getVal() : newTargetState).c_str()); // lockTargetState->setVal(lockTargetState->getNewVal()); // lockCurrentState->setVal(lockTargetState->getVal()); json payload; @@ -178,7 +197,8 @@ struct LockMechanism : Service::LockMechanism } } } - if(foundIssuer != nullptr){ + if (foundIssuer != nullptr) + { payload["issuerId"] = utils::bufToHexString(foundIssuer->issuerId, 8); payload["endpointId"] = utils::bufToHexString(std::get<0>(auth)->endpointId, 6); payload["homekey"] = true; @@ -210,7 +230,8 @@ struct LockMechanism : Service::LockMechanism } } } - if(foundIssuer != nullptr){ + if (foundIssuer != nullptr) + { payload["issuerId"] = utils::bufToHexString(foundIssuer->issuerId, 8); payload["endpointId"] = utils::bufToHexString(std::get<0>(auth)->endpointId, 6); payload["homekey"] = true; @@ -227,16 +248,18 @@ struct LockMechanism : Service::LockMechanism } else { - ESP_LOGW(TAG,"Authentication Failed, lock state not changed"); + ESP_LOGW(TAG, "Authentication Failed, lock state not changed"); } - } else { - json payload; - payload["atqa"] = utils::bufToHexString(atqa, 1); - payload["sak"] = utils::bufToHexString(sak, 1); - payload["uid"] = utils::bufToHexString(uid, uidLen); - payload["homekey"] = false; - mqtt.publish(MQTT_AUTH_TOPIC, payload.dump().c_str()); - } + } + else + { + json payload; + payload["atqa"] = utils::bufToHexString(atqa, 1); + payload["sak"] = utils::bufToHexString(sak, 1); + payload["uid"] = utils::bufToHexString(uid, uidLen); + payload["homekey"] = false; + mqtt.publish(MQTT_AUTH_TOPIC, payload.dump().c_str()); + } } } else @@ -286,13 +309,13 @@ struct NFCAccess : Service::NFCAccess mbedtls_ecp_group_init(&grp); mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP256R1); int ret = mbedtls_ecp_point_read_binary(&grp, &point, pubKey.data(), pubKey.size()); - ESP_LOGV(TAG,"mbedtls_ecp_point_read_binary status: %d", ret); + ESP_LOGV(TAG, "mbedtls_ecp_point_read_binary status: %d", ret); size_t buffer_size_x = mbedtls_mpi_size(&point.X); std::vector X; X.resize(buffer_size_x); X.reserve(buffer_size_x); mbedtls_mpi_write_binary(&point.X, X.data(), buffer_size_x); - ESP_LOGV(TAG,"PublicKey: %s, X Coordinate: %s", utils::bufToHexString(pubKey.data(), pubKey.size()).c_str(), utils::bufToHexString(X.data(), X.size()).c_str()); + ESP_LOGV(TAG, "PublicKey: %s, X Coordinate: %s", utils::bufToHexString(pubKey.data(), pubKey.size()).c_str(), utils::bufToHexString(X.data(), X.size()).c_str()); mbedtls_ecp_group_free(&grp); mbedtls_ecp_point_free(&point); return X; @@ -333,7 +356,7 @@ struct NFCAccess : Service::NFCAccess tlv8.create(kDevice_Req_Key_State, 1, "KEY.STATE"); tlv8.create(kDevice_Req_Key_Identifier, 8, "KEY.IDENTIFIER"); - ESP_LOGV(TAG,"DCR TLV DECODE STATE: %d", tlv8.unpack(buf, len)); + ESP_LOGV(TAG, "DCR TLV DECODE STATE: %d", tlv8.unpack(buf, len)); tlv8.print(1); Issuers::homeKeyIssuers_t *foundIssuer = nullptr; for (auto &issuer : readerData.issuers) @@ -434,7 +457,7 @@ struct NFCAccess : Service::NFCAccess // tlv8.create(kRequest_Reader_Key_Request, 64, "READER.REQ"); // tlv8.create(kReader_Req_Key_Identifier, 64, "KEY.IDENTIFIER"); - ESP_LOGV(TAG,"RKR TLV DECODE STATE: %d", tlv8.unpack(buf, len)); + ESP_LOGV(TAG, "RKR TLV DECODE STATE: %d", tlv8.unpack(buf, len)); tlv8.print(1); uint8_t *readerKey = tlv8.buf(kReader_Req_Reader_Private_Key); uint8_t *uniqueIdentifier = tlv8.buf(kReader_Req_Identifier); @@ -758,7 +781,8 @@ void print_issuers(const char *buf) } } -void wifiCallback(){ +void wifiCallback() +{ mqtt.host = MQTT_HOST; mqtt.port = MQTT_PORT; mqtt.client_id = MQTT_CLIENTID;