From fc68b304f6da8db8ec5fc2ab3cdc2e558afbc381 Mon Sep 17 00:00:00 2001 From: Rodrigo Alfonso Date: Sun, 18 Aug 2024 01:40:54 -0300 Subject: [PATCH] Adding isConfigurationValid() --- README.md | 1 + examples/LinkMobile_demo/src/main.cpp | 7 ++++--- lib/LinkMobile.hpp | 19 +++++++++++++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0a29db2..ab756e5 100644 --- a/README.md +++ b/README.md @@ -449,6 +449,7 @@ Name | Return type | Description `readConfiguration(configurationData)` | **bool** | Retrieves the adapter configuration, and puts it in the `configurationData` struct. If the adapter has an active session, the data is already loaded, so it's instantaneous. `getState()` | **LinkMobile::State** | Returns the current state (one of `LinkMobile::State::NEEDS_RESET`, `LinkMobile::State::PINGING`, `LinkMobile::State::WAITING_TO_START`, `LinkMobile::State::STARTING_SESSION`, `LinkMobile::State::ACTIVATING_SIO32`, `LinkMobile::State::WAITING_32BIT_SWITCH`, `LinkMobile::State::READING_CONFIGURATION`, `LinkMobile::State::SESSION_ACTIVE`, `LinkMobile::State::CALL_REQUESTED`, `LinkMobile::State::CALLING`, `LinkMobile::State::CALL_ESTABLISHED`, `LinkMobile::State::SESSION_ACTIVE_ISP`, `LinkMobile::State::SHUTDOWN_REQUESTED`, `LinkMobile::State::ENDING_SESSION`, `LinkMobile::State::WAITING_8BIT_SWITCH`, or `LinkMobile::State::SHUTDOWN`). `getRole()` | **LinkMobile::Role** | Returns the current role in the P2P connection (one of `LinkMobile::Role::NO_P2P_CONNECTION`, `LinkMobile::Role::CALLER`, or `LinkMobile::Role::RECEIVER`). +`isConfigurationValid()` | **int** | Returns whether the adapter has been configured or not. Returns `1` = yes, `0` = no, `-1` = unknown (no session active). `isConnected()` | **bool** | Returns `true` if a P2P call is established (the state is `CALL_ESTABLISHED`). `isSessionActive()` | **bool** | Returns `true` if the session is active. `canShutdown()` | **bool** | Returns `true` if there's an active session and there's no previous shutdown requests. diff --git a/examples/LinkMobile_demo/src/main.cpp b/examples/LinkMobile_demo/src/main.cpp index 0a66392..92d546b 100644 --- a/examples/LinkMobile_demo/src/main.cpp +++ b/examples/LinkMobile_demo/src/main.cpp @@ -217,8 +217,8 @@ std::string readConfiguration() { if (!linkMobile->readConfiguration(data)) return "Read failed :("; - return ("Magic:\n " + toStr(data.magic, 2) + "\nIsRegistering:\n " + - (data.isRegistering ? "Yes" : "No") + "\nPrimary DNS:\n " + + return ("Magic:\n " + toStr(data.magic, 2) + "\nRegistered:\n $" + + toHex(data.registrationState) + "\nPrimary DNS:\n " + std::to_string(data.primaryDNS[0]) + "." + std::to_string(data.primaryDNS[1]) + "." + std::to_string(data.primaryDNS[2]) + "." + @@ -229,7 +229,8 @@ std::string readConfiguration() { std::to_string(data.secondaryDNS[3]) + "\nLoginID:\n " + toStr(data.loginID, 10) + "\nEmail:\n " + toStr(data.email, 24) + "\nSMTP Server:\n " + toStr(data.smtpServer, 20) + - "\nPOP Server:\n " + toStr(data.popServer, 19) + "\n\nMode: " + + "\nPOP Server:\n " + toStr(data.popServer, 19) + "\nIs Valid:\n " + + std::to_string(linkMobile->isConfigurationValid()) + "\n\nMode: " + (linkMobile->getDataSize() == LinkSPI::DataSize::SIZE_32BIT ? "SIO32" : "SIO8")); diff --git a/lib/LinkMobile.hpp b/lib/LinkMobile.hpp index 002525b..e5ee945 100644 --- a/lib/LinkMobile.hpp +++ b/lib/LinkMobile.hpp @@ -179,7 +179,7 @@ class LinkMobile { struct ConfigurationData { char magic[2]; - bool isRegistering; + u8 registrationState; u8 _unused1_; u8 primaryDNS[4]; u8 secondaryDNS[4]; @@ -356,6 +356,17 @@ class LinkMobile { */ [[nodiscard]] Role getRole() { return role; } + /** + * @brief Returns whether the adapter has been configured or not. + * @return 1 = yes, 0 = no, -1 = unknown (no session active). + */ + [[nodiscard]] int isConfigurationValid() { + if (!isSessionActive()) + return -1; + + return (int)adapterConfiguration.isValid(); + } + /** * @brief Returns `true` if a P2P call is established (the state is * `CALL_ESTABLISHED`). @@ -497,7 +508,11 @@ class LinkMobile { ConfigurationData fields; char bytes[CONFIGURATION_DATA_SIZE]; - bool isValid() { return calculatedChecksum() == reportedChecksum(); } + bool isValid() { + return fields.magic[0] == 'M' && fields.magic[1] == 'A' && + (fields.registrationState & 1) == 1 && + calculatedChecksum() == reportedChecksum(); + } u16 calculatedChecksum() { u16 result = 0;