From 3a6fa59e4f182b0e1df40e39ab1a88d8744f2d07 Mon Sep 17 00:00:00 2001 From: Pierre Ferran Date: Wed, 1 May 2024 00:23:03 -0400 Subject: [PATCH] Clang tidy --- backend/.clang-tidy | 29 +++++----- backend/src/Helpers.hpp | 2 +- backend/src/RemoteData.hpp | 24 +++++--- backend/src/Shared.hpp | 2 +- backend/src/main.cpp | 85 +++++++++++++++++------------ backend/src/sdk.cpp | 12 ++-- backend/src/sdk.hpp | 24 ++++---- backend/src/sdkWebsocketMessage.hpp | 28 ++++++---- 8 files changed, 122 insertions(+), 84 deletions(-) diff --git a/backend/.clang-tidy b/backend/.clang-tidy index 8ea2208..105dfc1 100644 --- a/backend/.clang-tidy +++ b/backend/.clang-tidy @@ -1,18 +1,21 @@ --- Checks: "*, - -abseil-*, - -altera-*, - -android-*, - -fuchsia-*, - -google-*, - -llvm*, - -modernize-use-trailing-return-type, - -zircon-*, - -readability-else-after-return, - -readability-static-accessed-through-instance, - -readability-avoid-const-params-in-decls, - -cppcoreguidelines-non-private-member-variables-in-classes, - -misc-non-private-member-variables-in-classes" + -altera-*, + -android-*, + -fuchsia-*, + -google-*, + -llvm*, + -modernize-use-trailing-return-type, + -zircon-*, + -readability-else-after-return, + -readability-static-accessed-through-instance, + -readability-avoid-const-params-in-decls, + -cppcoreguidelines-non-private-member-variables-in-classes, + -misc-non-private-member-variables-in-classes, + -cppcoreguidelines-avoid-magic-numbers, + -readability-magic-numbers, + -unused-includes" + WarningsAsErrors: '' HeaderFilterRegex: '' FormatStyle: none diff --git a/backend/src/Helpers.hpp b/backend/src/Helpers.hpp index d1f8381..68e993d 100644 --- a/backend/src/Helpers.hpp +++ b/backend/src/Helpers.hpp @@ -18,7 +18,7 @@ class Helpers { return RadioSimulation::round8_33kHzChannel(frequency); } - static void CallbackWithError(std::string message) + static void CallbackWithError(const std::string& message) { std::lock_guard lock(errorCallbackMutex); if (!callbackAvailable) { diff --git a/backend/src/RemoteData.hpp b/backend/src/RemoteData.hpp index 89bb778..62590f8 100644 --- a/backend/src/RemoteData.hpp +++ b/backend/src/RemoteData.hpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -13,23 +14,30 @@ class RemoteData { public: RemoteData() - : timer(3 * 1000, TIMER_CALLBACK_INTERVAL_SEC * 1000) + : timer(static_cast(3 * 1000), static_cast(TIMER_CALLBACK_INTERVAL_SEC * 1000)) , slurperCli(SLURPER_BASE_URL) { timer.start(Poco::TimerCallback(*this, &RemoteData::onTimer)); } + RemoteData(const RemoteData&) = delete; + RemoteData(RemoteData&&) = delete; + RemoteData& operator=(const RemoteData&) = delete; + RemoteData& operator=(RemoteData&&) = delete; ~RemoteData() { timer.stop(); } protected: - void onTimer(Poco::Timer& timer) + void onTimer(Poco::Timer& /*timer*/) { auto slurperData = getSlurperData(); auto previousCallsign = UserSession::callsign; - - auto isConnected = parseSlurper(slurperData); - updateSessionStatus(previousCallsign, isConnected); + try { + auto isConnected = parseSlurper(slurperData); + updateSessionStatus(previousCallsign, isConnected); + } catch (const std::exception& ex) { + LOG_ERROR(logger, "Error while parsing slurper data: {}", ex.what()); + } } std::string getSlurperData() @@ -40,7 +48,7 @@ class RemoteData { auto res = slurperCli.Get(SLURPER_DATA_ENDPOINT + std::string("?cid=") + UserSession::cid); - if (!res || res->status != 200) { + if (!res || res->status != httplib::StatusCode::OK_200) { // Notify the client the slurper is offline RemoteDataStatus::isSlurperAvailable = false; LOG_ERROR(logger, "Slurper is offline {}", res->status); @@ -55,6 +63,7 @@ class RemoteData { return res->body; } + // NOLINTNEXTLINE bool parseSlurper(const std::string& sluper_data) { if (sluper_data.empty()) { @@ -111,6 +120,7 @@ class RemoteData { } res3.erase(std::remove(res3.begin(), res3.end(), '.'), res3.end()); + // NOLINTNEXTLINE Handled above in the try catch block int u334 = std::atoi(res3.c_str()) * 1000; int k422 = std::stoi(res2, nullptr, 16) == 10 && pYx ? 1 : 0; @@ -131,7 +141,7 @@ class RemoteData { return true; } - void updateSessionStatus(std::string previousCallsign, bool isConnected) + static void updateSessionStatus(std::string previousCallsign, bool isConnected) { if (UserSession::isConnectedToTheNetwork && UserSession::callsign != previousCallsign && !previousCallsign.empty() && isConnected && mClient->IsVoiceConnected()) { LOG_INFO(logger, diff --git a/backend/src/Shared.hpp b/backend/src/Shared.hpp index 930b783..5293fea 100644 --- a/backend/src/Shared.hpp +++ b/backend/src/Shared.hpp @@ -10,7 +10,7 @@ constexpr semver::version VERSION = semver::version { 1, 0, 2, semver::prerelease::beta, 2 }; -static const std::string CLIENT_NAME = std::string("TrackAudio-") + VERSION.to_string(); +const std::string CLIENT_NAME = std::string("TrackAudio-") + VERSION.to_string(); static Napi::ThreadSafeFunction callbackRef; static bool callbackAvailable = false; diff --git a/backend/src/main.cpp b/backend/src/main.cpp index dc91529..2266fc5 100644 --- a/backend/src/main.cpp +++ b/backend/src/main.cpp @@ -20,6 +20,7 @@ #include "Shared.hpp" #include "sdk.hpp" +namespace mainStaticData { static std::unique_ptr mRemoteDataHandler = nullptr; static std::unique_ptr mApiServer = nullptr; @@ -27,6 +28,7 @@ static bool ShouldRun = true; static std::unique_ptr vuMeterThread = nullptr; static std::atomic_bool runVuMeterCallback = false; +} Napi::Array GetAudioApis(const Napi::CallbackInfo& info) { @@ -49,7 +51,7 @@ Napi::Array GetAudioInputDevices(const Napi::CallbackInfo& info) int apiId = info[0].As().Int32Value(); Napi::Array arr = Napi::Array::New(env); - for (const auto [deviceId, deviceName, isDefault] : + for (const auto& [deviceId, deviceName, isDefault] : mClient->GetAudioInputDevices(apiId)) { Napi::Object obj = Napi::Object::New(env); obj.Set("id", deviceId); @@ -81,7 +83,7 @@ Napi::Array GetAudioOutputDevices(const Napi::CallbackInfo& info) Napi::Boolean Connect(const Napi::CallbackInfo& info) { - if (!ShouldRun) { + if (!mainStaticData::ShouldRun) { return Napi::Boolean::New(info.Env(), false); } @@ -103,13 +105,13 @@ Napi::Boolean Connect(const Napi::CallbackInfo& info) return Napi::Boolean::New(env, mClient->Connect()); } -void Disconnect(const Napi::CallbackInfo& info) +void Disconnect(const Napi::CallbackInfo& /*info*/) { if (!mClient->IsVoiceConnected()) { return; } mClient->Disconnect(); - mApiServer->handleAFVEventForWebsocket( + mainStaticData::mApiServer->handleAFVEventForWebsocket( sdk::types::Event::kDisconnectFrequencyStateUpdate, {}, {}); } @@ -148,7 +150,7 @@ Napi::Boolean AddFrequency(const Napi::CallbackInfo& info) mClient->SetRx(frequency, false); mClient->SetRadioGainAll(UserSession::currentRadioGain); - mApiServer->handleAFVEventForWebsocket( + mainStaticData::mApiServer->handleAFVEventForWebsocket( sdk::types::Event::kFrequencyStateUpdate, {}, {}); return Napi::Boolean::New(info.Env(), true); @@ -158,11 +160,11 @@ void RemoveFrequency(const Napi::CallbackInfo& info) { int frequency = info[0].As().Int32Value(); mClient->RemoveFrequency(frequency); - mApiServer->handleAFVEventForWebsocket( + mainStaticData::mApiServer->handleAFVEventForWebsocket( sdk::types::Event::kFrequencyStateUpdate, {}, {}); } -void Reset(const Napi::CallbackInfo& info) { mClient->reset(); } +void Reset(const Napi::CallbackInfo& /*info*/) { mClient->reset(); } Napi::Boolean SetFrequencyState(const Napi::CallbackInfo& info) { @@ -203,7 +205,7 @@ Napi::Boolean SetFrequencyState(const Napi::CallbackInfo& info) mClient->SetOnHeadset(frequency, !onSpeaker); - mApiServer->handleAFVEventForWebsocket( + mainStaticData::mApiServer->handleAFVEventForWebsocket( sdk::types::Event::kFrequencyStateUpdate, {}, {}); return Napi::Boolean::New(info.Env(), true); @@ -316,9 +318,9 @@ Napi::Boolean IsConnected(const Napi::CallbackInfo& info) return Napi::Boolean::New(info.Env(), mClient->IsVoiceConnected()); } -void StartMicTest(const Napi::CallbackInfo& info) +void StartMicTest(const Napi::CallbackInfo& /*info*/) { - if (!mClient || !callbackAvailable || vuMeterThread != nullptr) { + if (!mClient || !callbackAvailable || mainStaticData::vuMeterThread != nullptr) { LOG_WARNING(logger, "Attempted to start mic test without callback or " "uninitiated client, or already running mic test, this " "will be useless"); @@ -326,9 +328,9 @@ void StartMicTest(const Napi::CallbackInfo& info) } mClient->StartAudio(); - runVuMeterCallback = true; + mainStaticData::runVuMeterCallback = true; - vuMeterThread = std::make_unique([] { + mainStaticData::vuMeterThread = std::make_unique([] { for (int i = 0; i < 2400; i++) { // Max of 2 minutes, don't allow infinite test #ifndef WIN32 @@ -340,7 +342,7 @@ void StartMicTest(const Napi::CallbackInfo& info) break; } - if (!runVuMeterCallback) { + if (!mainStaticData::runVuMeterCallback) { break; } @@ -358,22 +360,22 @@ void StartMicTest(const Napi::CallbackInfo& info) }); } -void StopMicTest(const Napi::CallbackInfo& info) +void StopMicTest(const Napi::CallbackInfo& /*info*/) { if (!mClient) { return; } - runVuMeterCallback = false; - if (vuMeterThread && vuMeterThread->joinable()) { - vuMeterThread->join(); + mainStaticData::runVuMeterCallback = false; + if (mainStaticData::vuMeterThread && mainStaticData::vuMeterThread->joinable()) { + mainStaticData::vuMeterThread->join(); } - vuMeterThread.reset(nullptr); + mainStaticData::vuMeterThread.reset(nullptr); mClient->StopAudio(); } -void StartAudio(const Napi::CallbackInfo& info) +void StartAudio(const Napi::CallbackInfo& /*info*/) { if (!mClient || mClient->IsAudioRunning()) { LOG_WARNING(logger, "Attempted to start audio when audio already running"); @@ -383,7 +385,7 @@ void StartAudio(const Napi::CallbackInfo& info) mClient->StartAudio(); } -void StopAudio(const Napi::CallbackInfo& info) +void StopAudio(const Napi::CallbackInfo& /*info*/) { if (!mClient || !mClient->IsAudioRunning()) { LOG_WARNING(logger, "Attempted to stop audio when audio not running"); @@ -419,6 +421,7 @@ static void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, return; } + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) std::string station = *reinterpret_cast(data); auto transceiverCount = mClient->GetTransceiverCountForStation(station); callbackRef.NonBlockingCall( @@ -435,11 +438,13 @@ static void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, return; } + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) bool found = *reinterpret_cast(data); if (!found) { Helpers::CallbackWithError("Station not found"); return; } + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) auto stationData = *reinterpret_cast*>(data2); std::string callsign = stationData.first; unsigned int frequency = stationData.second; @@ -460,6 +465,7 @@ static void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, if (data == nullptr || data2 == nullptr) { return; } + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) std::map stations = *reinterpret_cast*>(data2); for (const auto& station : stations) { @@ -485,6 +491,7 @@ static void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, return; } + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) int frequency = *reinterpret_cast(data); callbackRef.NonBlockingCall( [frequency](Napi::Env env, Napi::Function jsCallback) { @@ -501,6 +508,7 @@ static void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, return; } + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) int frequency = *reinterpret_cast(data); callbackRef.NonBlockingCall( [frequency](Napi::Env env, Napi::Function jsCallback) { @@ -516,7 +524,9 @@ static void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, return; } + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) int frequency = *reinterpret_cast(data); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) std::string callsign = *reinterpret_cast(data2); callbackRef.NonBlockingCall( [frequency, callsign](Napi::Env env, Napi::Function jsCallback) { @@ -526,7 +536,7 @@ static void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, Napi::String::New(env, callsign) }); }); - mApiServer->handleAFVEventForWebsocket(sdk::types::Event::kRxBegin, + mainStaticData::mApiServer->handleAFVEventForWebsocket(sdk::types::Event::kRxBegin, callsign, frequency); return; @@ -537,10 +547,12 @@ static void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, return; } + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) int frequency = *reinterpret_cast(data); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) std::string callsign = *reinterpret_cast(data2); - mApiServer->handleAFVEventForWebsocket(sdk::types::Event::kRxEnd, callsign, + mainStaticData::mApiServer->handleAFVEventForWebsocket(sdk::types::Event::kRxEnd, callsign, frequency); } @@ -571,6 +583,7 @@ static void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, return; } + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) auto err = *reinterpret_cast(data); if (err == afv_native::afv::APISessionError::BadPassword || err == afv_native::afv::APISessionError::RejectedCredentials) { @@ -656,26 +669,26 @@ void CreateLoggers() }()); // Create a file logger - auto _ = quill::create_logger("afv_logger", std::move(afv_logger)); + auto* _ = quill::create_logger("afv_logger", std::move(afv_logger)); afv_native::api::setLogger([](std::string subsystem, std::string file, int line, std::string lineOut) { - auto logger = quill::get_logger("afv_logger"); - auto strippedFiledName = file.substr(file.find_last_of("/") + 1); + auto* logger = quill::get_logger("afv_logger"); + auto strippedFiledName = file.substr(file.find_last_of('/') + 1); LOG_INFO(logger, "[{}] [{}@{}] {}", subsystem, strippedFiledName, line, lineOut); }); } -bool CheckVersionSync(const Napi::CallbackInfo& info) +bool CheckVersionSync(const Napi::CallbackInfo& /*info*/) { // We force do a mandatory version check, if an update is needed, the // programme won't run httplib::Client client(VERSION_CHECK_BASE_URL); auto res = client.Get(VERSION_CHECK_ENDPOINT); - if (!res || res->status != 200) { - ShouldRun = false; + if (!res || res->status != httplib::StatusCode::OK_200) { + mainStaticData::ShouldRun = false; LOG_CRITICAL(logger, "Error fetching version: {}", res->status); return false; } @@ -683,15 +696,15 @@ bool CheckVersionSync(const Napi::CallbackInfo& info) try { std::string cleanBody = res->body; absl::StripAsciiWhitespace(&cleanBody); - semver::version mandatoryVersion = semver::version(cleanBody); + auto mandatoryVersion = semver::version(cleanBody); if (VERSION < mandatoryVersion) { - ShouldRun = false; + mainStaticData::ShouldRun = false; LOG_ERROR(logger, "Mandatory update required: {} -> {}", VERSION.to_string(), mandatoryVersion.to_string()); return false; } } catch (const std::exception& e) { - ShouldRun = false; + mainStaticData::ShouldRun = false; LOG_CRITICAL(logger, "Error parsing version: {}", e.what()); return false; } @@ -710,7 +723,7 @@ Napi::Boolean Bootstrap(const Napi::CallbackInfo& info) std::string resourcePath = info[0].As().Utf8Value(); mClient = std::make_unique(CLIENT_NAME, resourcePath); - mRemoteDataHandler = std::make_unique(); + mainStaticData::mRemoteDataHandler = std::make_unique(); // Setup afv mClient->RaiseClientEvent( @@ -718,19 +731,19 @@ Napi::Boolean Bootstrap(const Napi::CallbackInfo& info) HandleAfvEvents(eventType, data1, data2); }); - mApiServer = std::make_unique(); + mainStaticData::mApiServer = std::make_unique(); return Napi::Boolean::New(info.Env(), true); } -void Exit(const Napi::CallbackInfo& info) +void Exit(const Napi::CallbackInfo& /*info*/) { LOG_INFO(logger, "Exiting TrackAudio"); if (mClient->IsVoiceConnected()) { mClient->Disconnect(); } - mApiServer.reset(); - mRemoteDataHandler.reset(); + mainStaticData::mApiServer.reset(); + mainStaticData::mRemoteDataHandler.reset(); mClient.reset(); } diff --git a/backend/src/sdk.cpp b/backend/src/sdk.cpp index d4d453b..347a5aa 100644 --- a/backend/src/sdk.cpp +++ b/backend/src/sdk.cpp @@ -1,4 +1,6 @@ #include "sdk.hpp" +#include "Helpers.hpp" +#include "Shared.hpp" SDK::SDK() { this->buildServer(); } @@ -81,6 +83,7 @@ void SDK::handleAFVEventForWebsocket(sdk::types::Event event, std::vector xcBar; auto allRadios = mClient->getRadioState(); for (const auto& [frequency, state] : allRadios) { + // NOLINTNEXTLINE ns::Station stationObject = ns::Station::build(state.stationName, frequency); if (state.rx) { rxBar.push_back(stationObject); @@ -106,22 +109,23 @@ void SDK::handleAFVEventForWebsocket(sdk::types::Event event, void SDK::buildRouter() { try { + auto routeMap = getSDKCallUrlMap(); this->pRouter = std::make_unique>(); - this->pRouter->http_get(mSDKCallUrl[sdkCall::kTransmitting], + this->pRouter->http_get(routeMap[sdkCall::kTransmitting], [&](auto req, auto /*params*/) { return SDK::handleTransmittingSDKCall(req); }); this->pRouter->http_get( - mSDKCallUrl[sdkCall::kRx], + routeMap[sdkCall::kRx], [&](auto req, auto /*params*/) { return this->handleRxSDKCall(req); }); this->pRouter->http_get( - mSDKCallUrl[sdkCall::kTx], + routeMap[sdkCall::kTx], [&](auto req, auto /*params*/) { return this->handleTxSDKCall(req); }); this->pRouter->http_get( - mSDKCallUrl[sdkCall::kWebSocket], + routeMap[sdkCall::kWebSocket], [&](auto req, auto /*params*/) { return handleWebSocketSDKCall(req); }); this->pRouter->non_matched_request_handler([](auto req) { diff --git a/backend/src/sdk.hpp b/backend/src/sdk.hpp index ee22de0..3b7bfee 100644 --- a/backend/src/sdk.hpp +++ b/backend/src/sdk.hpp @@ -1,6 +1,5 @@ #pragma once -#include "afv-native/afv_native.h" #include "sdkWebsocketMessage.hpp" #include #include @@ -15,9 +14,6 @@ #include #include -#include "Helpers.hpp" -#include "Shared.hpp" -#include "sdkWebsocketMessage.hpp" #include #include #include @@ -41,6 +37,10 @@ class SDK { public: explicit SDK(); + SDK(const SDK&) = delete; + SDK(SDK&&) = delete; + SDK& operator=(const SDK&) = delete; + SDK& operator=(SDK&&) = delete; ~SDK(); /** * Handles an AFV event for the websocket. @@ -75,12 +75,16 @@ class SDK { static inline std::mutex BroadcastMutex; - static inline std::map mSDKCallUrl = { - { kTransmitting, "/transmitting" }, - { kRx, "/rx" }, - { kTx, "/tx" }, - { kWebSocket, "/ws" } - }; + inline static std::map& getSDKCallUrlMap() + { + static std::map mSDKCallUrl = { + { kTransmitting, "/transmitting" }, + { kRx, "/rx" }, + { kTx, "/tx" }, + { kWebSocket, "/ws" } + }; + return mSDKCallUrl; + } /** * @brief Broadcasts data on the websocket. diff --git a/backend/src/sdkWebsocketMessage.hpp b/backend/src/sdkWebsocketMessage.hpp index da96e17..87f2a6e 100644 --- a/backend/src/sdkWebsocketMessage.hpp +++ b/backend/src/sdkWebsocketMessage.hpp @@ -10,11 +10,15 @@ enum class WebsocketMessageType { kRxBegin, kRxEnd, kFrequencyStateUpdate }; -const std::map kWebsocketMessageTypeMap { - { WebsocketMessageType::kRxBegin, "kRxBegin" }, - { WebsocketMessageType::kRxEnd, "kRxEnd" }, - { WebsocketMessageType::kFrequencyStateUpdate, "kFrequenciesUpdate" } -}; +inline const std::map& getWebsocketMessageTypeMap() +{ + static const std::map kWebsocketMessageTypeMap { + { WebsocketMessageType::kRxBegin, "kRxBegin" }, + { WebsocketMessageType::kRxEnd, "kRxEnd" }, + { WebsocketMessageType::kFrequencyStateUpdate, "kFrequenciesUpdate" } + }; + return kWebsocketMessageTypeMap; +} class WebsocketMessage { public: @@ -29,7 +33,7 @@ class WebsocketMessage { static WebsocketMessage buildMessage(WebsocketMessageType messageType) { - const std::string& typeString = kWebsocketMessageTypeMap.at(messageType); + const std::string& typeString = getWebsocketMessageTypeMap().at(messageType); return WebsocketMessage(typeString); } @@ -63,20 +67,20 @@ class Station { inline static Station build(const std::string& callsign, int freqHz) { - Station s; - s.pCallsign = std::move(callsign); - s.pFrequencyHz = freqHz; + Station station; + station.pCallsign = callsign; + station.pFrequencyHz = freqHz; std::string temp = std::to_string(freqHz / 1000); - s.pHumanFreq = temp.substr(0, 3) + "." + temp.substr(3, 7); + station.pHumanFreq = temp.substr(0, 3) + "." + temp.substr(3, 7); - return s; + return station; } NLOHMANN_DEFINE_TYPE_INTRUSIVE(Station, pCallsign, pFrequencyHz); protected: - int pFrequencyHz; + int pFrequencyHz = 0; std::string pCallsign; std::string pHumanFreq;