Improve result names
This commit is contained in:
parent
00d877c13a
commit
ac40656c1c
@ -36,31 +36,31 @@ enum class MessageResult: uint8_t {
|
|||||||
MessageAccepted = 0,
|
MessageAccepted = 0,
|
||||||
|
|
||||||
/// @brief The web socket received text while waiting for binary data.
|
/// @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.
|
/// @brief An unexpected socket event occured while performing the exchange.
|
||||||
UnexpectedSocketEvent = 2,
|
UnexpectedSocketEvent = 2,
|
||||||
|
|
||||||
/// @brief The received message size is invalid.
|
/// @brief The received message size is invalid.
|
||||||
InvalidMessageSize = 3,
|
InvalidMessageSizeFromRemote = 3,
|
||||||
|
|
||||||
/// @brief The message signature was incorrect.
|
/// @brief The message signature was incorrect.
|
||||||
MessageAuthenticationFailed = 4,
|
InvalidSignatureFromRemote = 4,
|
||||||
|
|
||||||
/// @brief The server challenge of the message did not match previous messages
|
/// @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
|
/// @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
|
/// @brief An unexpected or unsupported message type was received
|
||||||
InvalidMessageType = 7,
|
InvalidMessageTypeFromRemote = 7,
|
||||||
|
|
||||||
/// @brief A message is already being processed
|
/// @brief A message is already being processed
|
||||||
TooManyRequests = 8,
|
TooManyRequests = 8,
|
||||||
|
|
||||||
/// @brief The received message result was invalid
|
/// @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
|
/// @brief An invalid Url parameter was set sending a message to the device over a local connection
|
||||||
InvalidUrlParameter = 10,
|
InvalidUrlParameter = 10,
|
||||||
|
@ -78,7 +78,7 @@ void SesameController::handleLocalMessage(AsyncWebServerRequest *request) {
|
|||||||
String encoded = request->getParam(messageUrlParameter)->value();
|
String encoded = request->getParam(messageUrlParameter)->value();
|
||||||
if (!convertHexMessageToBinary(encoded.c_str())) {
|
if (!convertHexMessageToBinary(encoded.c_str())) {
|
||||||
Serial.println("Invalid hex encoding");
|
Serial.println("Invalid hex encoding");
|
||||||
prepareResponseBuffer(MessageResult::InvalidMessageSize);
|
prepareResponseBuffer(MessageResult::InvalidMessageSizeFromRemote);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
processMessage(&receivedLocalMessage);
|
processMessage(&receivedLocalMessage);
|
||||||
@ -86,7 +86,7 @@ void SesameController::handleLocalMessage(AsyncWebServerRequest *request) {
|
|||||||
|
|
||||||
void SesameController::sendPreparedLocalResponse(AsyncWebServerRequest *request) {
|
void SesameController::sendPreparedLocalResponse(AsyncWebServerRequest *request) {
|
||||||
request->send_P(200, "application/octet-stream", (uint8_t*) &outgoingMessage, SIGNED_MESSAGE_SIZE);
|
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
|
// MARK: Server
|
||||||
@ -99,7 +99,7 @@ void SesameController::sendServerError(MessageResult result) {
|
|||||||
void SesameController::handleServerMessage(uint8_t* payload, size_t length) {
|
void SesameController::handleServerMessage(uint8_t* payload, size_t length) {
|
||||||
if (length != SIGNED_MESSAGE_SIZE) {
|
if (length != SIGNED_MESSAGE_SIZE) {
|
||||||
// No message saved to discard, don't accidentally delete for other operation
|
// No message saved to discard, don't accidentally delete for other operation
|
||||||
sendServerError(MessageResult::InvalidMessageSize);
|
sendServerError(MessageResult::InvalidMessageSizeFromRemote);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
processMessage((SignedMessage*) payload);
|
processMessage((SignedMessage*) payload);
|
||||||
@ -108,7 +108,7 @@ void SesameController::handleServerMessage(uint8_t* payload, size_t length) {
|
|||||||
|
|
||||||
void SesameController::sendPreparedResponseToServer() {
|
void SesameController::sendPreparedResponseToServer() {
|
||||||
server.sendResponse((uint8_t*) &outgoingMessage, SIGNED_MESSAGE_SIZE);
|
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
|
// MARK: Message handling
|
||||||
@ -116,11 +116,11 @@ void SesameController::sendPreparedResponseToServer() {
|
|||||||
void SesameController::processMessage(SignedMessage* message) {
|
void SesameController::processMessage(SignedMessage* message) {
|
||||||
// Result must be empty
|
// Result must be empty
|
||||||
if (message->message.result != MessageResult::MessageAccepted) {
|
if (message->message.result != MessageResult::MessageAccepted) {
|
||||||
prepareResponseBuffer(MessageResult::InvalidMessageResult);
|
prepareResponseBuffer(MessageResult::InvalidMessageResultFromRemote);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!isAuthenticMessage(message, keyConfig.remoteKey)) {
|
if (!isAuthenticMessage(message, keyConfig.remoteKey)) {
|
||||||
prepareResponseBuffer(MessageResult::MessageAuthenticationFailed);
|
prepareResponseBuffer(MessageResult::InvalidSignatureFromRemote);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switch (message->message.messageType) {
|
switch (message->message.messageType) {
|
||||||
@ -131,7 +131,7 @@ void SesameController::processMessage(SignedMessage* message) {
|
|||||||
completeUnlockRequest(&message->message);
|
completeUnlockRequest(&message->message);
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
prepareResponseBuffer(MessageResult::InvalidMessageType);
|
prepareResponseBuffer(MessageResult::InvalidMessageTypeFromRemote);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -139,7 +139,7 @@ void SesameController::processMessage(SignedMessage* message) {
|
|||||||
void SesameController::checkAndPrepareChallenge(Message* message) {
|
void SesameController::checkAndPrepareChallenge(Message* message) {
|
||||||
// Server challenge must be empty
|
// Server challenge must be empty
|
||||||
if (message->serverChallenge != 0) {
|
if (message->serverChallenge != 0) {
|
||||||
prepareResponseBuffer(MessageResult::ClientChallengeInvalid);
|
prepareResponseBuffer(MessageResult::InvalidClientChallengeFromRemote);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
prepareChallenge(message);
|
prepareChallenge(message);
|
||||||
@ -153,6 +153,7 @@ void SesameController::prepareChallenge(Message* message) {
|
|||||||
// Set challenge and respond
|
// Set challenge and respond
|
||||||
currentClientChallenge = message->clientChallenge;
|
currentClientChallenge = message->clientChallenge;
|
||||||
currentServerChallenge = randomChallenge();
|
currentServerChallenge = randomChallenge();
|
||||||
|
message->serverChallenge = currentServerChallenge;
|
||||||
currentChallengeExpiry = currentTime + keyConfig.challengeExpiryMs;
|
currentChallengeExpiry = currentTime + keyConfig.challengeExpiryMs;
|
||||||
|
|
||||||
prepareResponseBuffer(MessageResult::MessageAccepted, message);
|
prepareResponseBuffer(MessageResult::MessageAccepted, message);
|
||||||
@ -161,11 +162,11 @@ void SesameController::prepareChallenge(Message* message) {
|
|||||||
void SesameController::completeUnlockRequest(Message* message) {
|
void SesameController::completeUnlockRequest(Message* message) {
|
||||||
// Client and server challenge must match
|
// Client and server challenge must match
|
||||||
if (message->clientChallenge != currentClientChallenge) {
|
if (message->clientChallenge != currentClientChallenge) {
|
||||||
prepareResponseBuffer(MessageResult::ClientChallengeInvalid, message);
|
prepareResponseBuffer(MessageResult::InvalidClientChallengeFromRemote, message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (message->serverChallenge != currentServerChallenge) {
|
if (message->serverChallenge != currentServerChallenge) {
|
||||||
prepareResponseBuffer(MessageResult::ServerChallengeMismatch, message);
|
prepareResponseBuffer(MessageResult::InvalidServerChallengeFromRemote, message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!hasCurrentChallenge()) {
|
if (!hasCurrentChallenge()) {
|
||||||
|
@ -76,7 +76,7 @@ switch(type) {
|
|||||||
didConnect();
|
didConnect();
|
||||||
break;
|
break;
|
||||||
case WStype_TEXT:
|
case WStype_TEXT:
|
||||||
controller->sendServerError(MessageResult::TextReceived);
|
controller->sendServerError(MessageResult::TextReceivedOverSocket);
|
||||||
break;
|
break;
|
||||||
case WStype_BIN:
|
case WStype_BIN:
|
||||||
controller->handleServerMessage(payload, length);
|
controller->handleServerMessage(payload, length);
|
||||||
@ -99,5 +99,7 @@ switch(type) {
|
|||||||
void ServerConnection::sendResponse(uint8_t* buffer, uint16_t length) {
|
void ServerConnection::sendResponse(uint8_t* buffer, uint16_t length) {
|
||||||
if (socketIsConnected()) {
|
if (socketIsConnected()) {
|
||||||
webSocket.sendBIN(buffer, length);
|
webSocket.sendBIN(buffer, length);
|
||||||
|
} else {
|
||||||
|
Serial.println("Failed to send response, socket not connected.");
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user