Improve result names

This commit is contained in:
Christoph Hagen 2023-12-11 00:05:18 +01:00
parent 00d877c13a
commit ac40656c1c
3 changed files with 21 additions and 18 deletions

View File

@ -36,31 +36,31 @@ enum class MessageResult: uint8_t {
MessageAccepted = 0,
/// @brief The web socket received text while waiting for binary data.
TextReceived = 1,
TextReceivedOverSocket = 1,
/// @brief An unexpected socket event occured while performing the exchange.
UnexpectedSocketEvent = 2,
/// @brief The received message size is invalid.
InvalidMessageSize = 3,
InvalidMessageSizeFromRemote = 3,
/// @brief The message signature was incorrect.
MessageAuthenticationFailed = 4,
InvalidSignatureFromRemote = 4,
/// @brief The server challenge of the message did not match previous messages
ServerChallengeMismatch = 5,
InvalidServerChallengeFromRemote = 5,
/// @brief The client challenge of the message did not match previous messages
ClientChallengeInvalid = 6,
InvalidClientChallengeFromRemote = 6,
/// @brief An unexpected or unsupported message type was received
InvalidMessageType = 7,
InvalidMessageTypeFromRemote = 7,
/// @brief A message is already being processed
TooManyRequests = 8,
/// @brief The received message result was invalid
InvalidMessageResult = 9,
InvalidMessageResultFromRemote = 9,
/// @brief An invalid Url parameter was set sending a message to the device over a local connection
InvalidUrlParameter = 10,

View File

@ -78,7 +78,7 @@ void SesameController::handleLocalMessage(AsyncWebServerRequest *request) {
String encoded = request->getParam(messageUrlParameter)->value();
if (!convertHexMessageToBinary(encoded.c_str())) {
Serial.println("Invalid hex encoding");
prepareResponseBuffer(MessageResult::InvalidMessageSize);
prepareResponseBuffer(MessageResult::InvalidMessageSizeFromRemote);
return;
}
processMessage(&receivedLocalMessage);
@ -86,7 +86,7 @@ void SesameController::handleLocalMessage(AsyncWebServerRequest *request) {
void SesameController::sendPreparedLocalResponse(AsyncWebServerRequest *request) {
request->send_P(200, "application/octet-stream", (uint8_t*) &outgoingMessage, SIGNED_MESSAGE_SIZE);
Serial.printf("[INFO] Local response %u\n", outgoingMessage.message.messageType);
Serial.printf("[INFO] Local response %u,%u\n", outgoingMessage.message.messageType, outgoingMessage.message.result);
}
// MARK: Server
@ -99,7 +99,7 @@ void SesameController::sendServerError(MessageResult result) {
void SesameController::handleServerMessage(uint8_t* payload, size_t length) {
if (length != SIGNED_MESSAGE_SIZE) {
// No message saved to discard, don't accidentally delete for other operation
sendServerError(MessageResult::InvalidMessageSize);
sendServerError(MessageResult::InvalidMessageSizeFromRemote);
return;
}
processMessage((SignedMessage*) payload);
@ -108,7 +108,7 @@ void SesameController::handleServerMessage(uint8_t* payload, size_t length) {
void SesameController::sendPreparedResponseToServer() {
server.sendResponse((uint8_t*) &outgoingMessage, SIGNED_MESSAGE_SIZE);
Serial.printf("[INFO] Server response %u\n", outgoingMessage.message.messageType);
Serial.printf("[INFO] Server response %u,%u\n", outgoingMessage.message.messageType, outgoingMessage.message.result);
}
// MARK: Message handling
@ -116,11 +116,11 @@ void SesameController::sendPreparedResponseToServer() {
void SesameController::processMessage(SignedMessage* message) {
// Result must be empty
if (message->message.result != MessageResult::MessageAccepted) {
prepareResponseBuffer(MessageResult::InvalidMessageResult);
prepareResponseBuffer(MessageResult::InvalidMessageResultFromRemote);
return;
}
if (!isAuthenticMessage(message, keyConfig.remoteKey)) {
prepareResponseBuffer(MessageResult::MessageAuthenticationFailed);
prepareResponseBuffer(MessageResult::InvalidSignatureFromRemote);
return;
}
switch (message->message.messageType) {
@ -131,7 +131,7 @@ void SesameController::processMessage(SignedMessage* message) {
completeUnlockRequest(&message->message);
return;
default:
prepareResponseBuffer(MessageResult::InvalidMessageType);
prepareResponseBuffer(MessageResult::InvalidMessageTypeFromRemote);
return;
}
}
@ -139,7 +139,7 @@ void SesameController::processMessage(SignedMessage* message) {
void SesameController::checkAndPrepareChallenge(Message* message) {
// Server challenge must be empty
if (message->serverChallenge != 0) {
prepareResponseBuffer(MessageResult::ClientChallengeInvalid);
prepareResponseBuffer(MessageResult::InvalidClientChallengeFromRemote);
return;
}
prepareChallenge(message);
@ -153,6 +153,7 @@ void SesameController::prepareChallenge(Message* message) {
// Set challenge and respond
currentClientChallenge = message->clientChallenge;
currentServerChallenge = randomChallenge();
message->serverChallenge = currentServerChallenge;
currentChallengeExpiry = currentTime + keyConfig.challengeExpiryMs;
prepareResponseBuffer(MessageResult::MessageAccepted, message);
@ -161,11 +162,11 @@ void SesameController::prepareChallenge(Message* message) {
void SesameController::completeUnlockRequest(Message* message) {
// Client and server challenge must match
if (message->clientChallenge != currentClientChallenge) {
prepareResponseBuffer(MessageResult::ClientChallengeInvalid, message);
prepareResponseBuffer(MessageResult::InvalidClientChallengeFromRemote, message);
return;
}
if (message->serverChallenge != currentServerChallenge) {
prepareResponseBuffer(MessageResult::ServerChallengeMismatch, message);
prepareResponseBuffer(MessageResult::InvalidServerChallengeFromRemote, message);
return;
}
if (!hasCurrentChallenge()) {

View File

@ -76,7 +76,7 @@ switch(type) {
didConnect();
break;
case WStype_TEXT:
controller->sendServerError(MessageResult::TextReceived);
controller->sendServerError(MessageResult::TextReceivedOverSocket);
break;
case WStype_BIN:
controller->handleServerMessage(payload, length);
@ -99,5 +99,7 @@ switch(type) {
void ServerConnection::sendResponse(uint8_t* buffer, uint16_t length) {
if (socketIsConnected()) {
webSocket.sendBIN(buffer, length);
} else {
Serial.println("Failed to send response, socket not connected.");
}
}