From 5180c51b45c313cf663a6185ed28fa25db37dec9 Mon Sep 17 00:00:00 2001 From: Pierre Ferran Date: Tue, 19 Nov 2024 15:11:31 +0100 Subject: [PATCH] Implement modern afv callback --- .github/workflows/ci.yml | 2 +- .github/workflows/release.yml | 2 +- backend/extern/afv-native | 2 +- backend/src/main.cpp | 63 +++++++++++++++-------------------- package-lock.json | 3 +- 5 files changed, 32 insertions(+), 40 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c572523..ebc26d9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,7 +37,7 @@ jobs: git submodule update --init --remote backend/extern/afv-native git submodule update --init --remote backend/extern/libuiohook cd backend/extern/afv-native - git checkout unicom2 + git checkout modern-chained-callback - name: Setup Node.js uses: actions/setup-node@v4 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e371260..35fc461 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -29,7 +29,7 @@ jobs: git submodule update --init --remote backend/vcpkg git submodule update --init --remote backend/extern/afv-native cd backend/extern/afv-native - git checkout unicom2 + git checkout modern-chained-callback - name: Setup Node.js uses: actions/setup-node@v4 diff --git a/backend/extern/afv-native b/backend/extern/afv-native index b4424ad..def3739 160000 --- a/backend/extern/afv-native +++ b/backend/extern/afv-native @@ -1 +1 @@ -Subproject commit b4424ada406ca5b8541d4ce07d58029a6a7d6ce8 +Subproject commit def3739684b8b0e66f5023cc582ab35ebdbef51c diff --git a/backend/src/main.cpp b/backend/src/main.cpp index c0ba1bc..394a6cf 100644 --- a/backend/src/main.cpp +++ b/backend/src/main.cpp @@ -491,7 +491,9 @@ void RequestPttKeyName(const Napi::CallbackInfo& info) } // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast,readability-function-cognitive-complexity) -void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, void* data2) +void HandleAfvEvents(afv_native::ClientEventType eventType, std::optional string1, + std::optional int1, std::optional> stationData, + std::optional> vccsData) { if (!NapiHelpers::callbackAvailable) { return; @@ -508,12 +510,11 @@ void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, void* da } if (eventType == afv_native::ClientEventType::StationTransceiversUpdated) { - if (data == nullptr) { + if (!string1) { return; } - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - std::string station = static_cast(data); + std::string station = string1.value(); auto transceiverCount = mClient->GetTransceiverCountForStation(station); auto states = mClient->getRadioState(); for (const auto& state : states) { @@ -528,20 +529,18 @@ void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, void* da } if (eventType == afv_native::ClientEventType::StationDataReceived) { - if (data == nullptr || data2 == nullptr) { + if (!stationData || !int1) { return; } - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - bool found = static_cast(data); + bool found = static_cast(int1); if (!found) { NapiHelpers::sendErrorToElectron("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; + + std::string callsign = stationData->first; + unsigned int frequency = stationData->second; if (mClient->IsFrequencyActive(frequency)) { PLOGW << "StationDataReceived: Frequency " << frequency << " already active, skipping"; @@ -553,13 +552,11 @@ void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, void* da } if (eventType == afv_native::ClientEventType::VccsReceived) { - if (data == nullptr || data2 == nullptr) { + if (!vccsData) { return; } - std::map stations - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - = *reinterpret_cast*>(data2); + std::map stations = vccsData.value(); for (const auto& station : stations) { const std::string& callsign = station.first; @@ -570,19 +567,19 @@ void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, void* da continue; } - NapiHelpers::callElectron("StationDataReceived", callsign, std::to_string(frequency)); - MainThreadShared::mApiServer->publishStationAdded( + NapiHelpers::callElectron("StationDataReceived", callsign, + std::to_string(frequency)); MainThreadShared::mApiServer->publishStationAdded( callsign, static_cast(frequency)); } } if (eventType == afv_native::ClientEventType::FrequencyRxBegin) { - if (data == nullptr) { + if (!int1) { return; } // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - int frequency = *reinterpret_cast(data); + int frequency = int1.value(); if (!mClient->IsFrequencyActive(frequency)) { PLOGW << "FrequencyRxBegin: Frequency " << frequency << " not active, skipping"; return; @@ -594,12 +591,11 @@ void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, void* da } if (eventType == afv_native::ClientEventType::FrequencyRxEnd) { - if (data == nullptr) { + if (!int1) { return; } - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - int frequency = *reinterpret_cast(data); + int frequency = int1.value(); if (!mClient->IsFrequencyActive(frequency)) { PLOGW << "FrequencyRxEnd: Frequency " << frequency << " not active, skipping"; return; @@ -610,14 +606,14 @@ void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, void* da } if (eventType == afv_native::ClientEventType::StationRxBegin) { - if (data == nullptr || data2 == nullptr) { + if (!int1 || !string1) { return; } // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - int frequency = *reinterpret_cast(data); + int frequency = int1.value(); // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - std::string callsign = static_cast(data2); + std::string callsign = string1.value(); if (!mClient->IsFrequencyActive(frequency)) { PLOGW << "StationRxBegin: Frequency " << frequency << " not active, skipping"; return; @@ -632,14 +628,12 @@ void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, void* da } if (eventType == afv_native::ClientEventType::StationRxEnd) { - if (data == nullptr || data2 == nullptr) { + if (!int1 || !string1) { return; } - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - int frequency = *reinterpret_cast(data); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - std::string callsign = static_cast(data2);; + int frequency = int1.value(); + std::string callsign = string1.value(); if (!mClient->IsFrequencyActive(frequency)) { PLOGW << "StationRxEnd: Frequency " << frequency << " not active, skipping"; @@ -667,12 +661,11 @@ void HandleAfvEvents(afv_native::ClientEventType eventType, void* data, void* da } if (eventType == afv_native::ClientEventType::APIServerError) { - if (data == nullptr) { + if (!int1) { return; } - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - auto err = *reinterpret_cast(data); + auto err = static_cast(int1.value()); if (err == afv_native::afv::APISessionError::BadPassword || err == afv_native::afv::APISessionError::RejectedCredentials) { @@ -787,9 +780,7 @@ Napi::Object Bootstrap(const Napi::CallbackInfo& info) MainThreadShared::mRemoteDataHandler = std::make_unique(); // Setup afv - mClient->RaiseClientEvent([](afv_native::ClientEventType eventType, void* data1, void* data2) { - HandleAfvEvents(eventType, data1, data2); - }); + mClient->RaiseModernClientEvent(std::function(&HandleAfvEvents)); MainThreadShared::mApiServer = std::make_shared(); diff --git a/package-lock.json b/package-lock.json index ed566b8..379c107 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8307,7 +8307,8 @@ "node_modules/trackaudio-afv": { "version": "1.0.0", "resolved": "file:backend/trackaudio-afv-1.0.0.tgz", - "integrity": "sha512-VbpikVdOWozbW6q2lg4vhq0O2u+ZdUaWrPtA3GiUu2PqhZM0gmd1OEQaFODVvWgQV/1eBfxy08c+axn/o7j9kw==", + "integrity": "sha512-TW2luNoredfY0b4vAQu6sgMC+BtGcw83vMoVj+mAt7gRviDoQa9apMkHFg+rzVJ40gdsVJ/j2DvI/hkGQgwNcw==", + "license": "GPL-3.0-only", "dependencies": { "bindings": "^1.5.0", "node-addon-api": "^1.1.0"