Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move failure orchestrator to commbus/eventsync #9657

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"${workspaceFolder}/fbw-a32nx/src/wasm/extra-backend-a32nx/src/**",
"${workspaceFolder}/fbw-a380x/src/wasm/extra-backend-a380x/src/**",
"${workspaceFolder}/fbw-common/src/wasm/fadec_common/src",
"${workspaceFolder}/fbw-common/src/wasm/**"
"${workspaceFolder}/fbw-common/src/wasm/**",
"${workspaceFolder}/fbw-common/src/wasm/fbw_common/src"
],
"defines": [
"_MSFS_WASM=1",
Expand Down
12 changes: 11 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,17 @@
"__functional_03": "cpp",
"memory_resource": "cpp",
"numbers": "cpp",
"xmemory0": "cpp"
"xmemory0": "cpp",
"any": "cpp",
"random": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"coroutine": "cpp",
"numeric": "cpp",
"span": "cpp",
"valarray": "cpp",
"ranges": "cpp",
"source_location": "cpp"
},
"typescript.preferences.quoteStyle": "single",
"typescript.preferences.importModuleSpecifier": "non-relative"
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion fbw-a32nx/src/systems/instruments/src/PFD/PFD.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class PFDComponent extends DisplayComponent<PFDProps> {

constructor(props: PFDProps) {
super(props);
this.failuresConsumer = new FailuresConsumer('A32NX');
this.failuresConsumer = new FailuresConsumer();
}

public onAfterRender(node: VNode): void {
Expand Down
3 changes: 3 additions & 0 deletions fbw-a32nx/src/wasm/fbw_a320/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ clang \
-flto \
-D_MSFS_WASM=1 \
-D__wasi__ \
-D_LARGEFILE64_SOURCE \
-D_LIBCPP_HAS_NO_THREADS \
-D_WINDLL \
-D_MBCS \
Expand Down Expand Up @@ -78,10 +79,12 @@ clang++ \
-target wasm32-unknown-wasi \
-D_MSFS_WASM=1 \
-D__wasi__ \
-D_LARGEFILE64_SOURCE \
-D_LIBCPP_HAS_NO_THREADS \
-D_WINDLL \
-D_MBCS \
-DNOMINMAX \
-DJSON_HAS_FILESYSTEM=0 \
-mthread-model single \
-fno-exceptions \
-fms-extensions \
Expand Down
3 changes: 0 additions & 3 deletions fbw-a32nx/src/wasm/fbw_a320/src/FlyByWireInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ void FlyByWireInterface::disconnect() {
bool FlyByWireInterface::update(double sampleTime) {
bool result = true;

// update failures handler
failuresConsumer.update();

// get data & inputs
result &= readDataAndLocalVariables(sampleTime);

Expand Down
56 changes: 27 additions & 29 deletions fbw-a32nx/src/wasm/fbw_a320/src/failures/FailuresConsumer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
#include "FailuresConsumer.h"
#include <MSFS/MSFS_CommBus.h>
#include <algorithm>
#include <nlohmann/json.hpp>
#include <utility>

using json = nlohmann::json;

static void FailuresConsumerCommBusHandler(const char* buf, unsigned int bufSize, void* ctx) {
auto* failuresConsumer = static_cast<FailuresConsumer*>(ctx);
if (failuresConsumer == nullptr || buf == nullptr || bufSize < 1 || buf[bufSize - 1] != '\0') {
return;
}

json data = json::parse(buf);
if (!data.is_array()) {
return;
}

std::set<int> failures;
data.get_to(failures);

failuresConsumer->acceptFailures(failures);
}

FailuresConsumer::FailuresConsumer() {
activeFailures.emplace(std::make_pair<Failures, bool>(Failures::Elac1, false));
activeFailures.emplace(std::make_pair<Failures, bool>(Failures::Elac2, false));
Expand All @@ -19,43 +40,20 @@ FailuresConsumer::FailuresConsumer() {
}

void FailuresConsumer::initialize() {
activateLvar = std::make_unique<LocalVariable>("A32NX_FAILURE_ACTIVATE");
deactivateLvar = std::make_unique<LocalVariable>("A32NX_FAILURE_DEACTIVATE");
fsCommBusRegister("FBW_FAILURE_UPDATE", FailuresConsumerCommBusHandler, this);
fsCommBusCall("FBW_FAILURE_REQUEST", nullptr, 0, FsCommBusBroadcast_JS);
}

void FailuresConsumer::update() {
updateActivate();
updateDeactivate();
void FailuresConsumer::acceptFailures(const std::set<int>& failures) {
for (auto& activeFailure : activeFailures) {
activeFailure.second = failures.contains(static_cast<int>(activeFailure.first));
}
}

bool FailuresConsumer::isActive(Failures failure) {
return (*activeFailures.find(failure)).second;
}

void FailuresConsumer::updateActivate() {
double index = activateLvar->get();
if (setIfFound(index, true)) {
activateLvar->set(0);
}
}

void FailuresConsumer::updateDeactivate() {
double index = deactivateLvar->get();
if (setIfFound(index, false)) {
deactivateLvar->set(0);
}
}

bool FailuresConsumer::setIfFound(double identifier, bool value) {
auto pair = activeFailures.find(static_cast<Failures>(identifier));
if (pair == activeFailures.end()) {
return false;
} else {
(*pair).second = value;
return true;
}
}

bool FailuresConsumer::isAnyActive() {
if (std::any_of(activeFailures.begin(), activeFailures.end(), [](auto pair) { return pair.second; })) {
return true;
Expand Down
15 changes: 3 additions & 12 deletions fbw-a32nx/src/wasm/fbw_a320/src/failures/FailuresConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,22 @@

#include <map>
#include <memory>
#include <set>
#include "../LocalVariable.h"
#include "FailureList.h"

class FailuresConsumer {
public:
FailuresConsumer();

void update();

bool isActive(Failures failure);

bool isAnyActive();

void initialize();

void acceptFailures(const std::set<int>& failures);

private:
std::map<Failures, bool> activeFailures;

void updateActivate();

void updateDeactivate();

bool setIfFound(double identifier, bool value);

std::unique_ptr<LocalVariable> activateLvar;

std::unique_ptr<LocalVariable> deactivateLvar;
};
2 changes: 2 additions & 0 deletions fbw-a32nx/src/wasm/fbw_a320/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <MSFS/MSFS.h>
#include <MSFS/MSFS_CommBus.h>

#include "FlyByWireInterface.h"
#include "main.h"
Expand All @@ -21,6 +22,7 @@ __attribute__((export_name("fbw_gauge_callback"))) extern "C" bool fbw_gauge_cal
case PANEL_SERVICE_PRE_KILL: {
// disconnect sim connect
flyByWireInterface.disconnect();
fsCommBusUnregisterAll();
} break;
}

Expand Down
2 changes: 1 addition & 1 deletion fbw-a32nx/src/wasm/systems/a320_systems_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async fn systems(mut gauge: msfs::Gauge) -> Result<(), Box<dyn Error>> {
.with_auxiliary_power_unit(Variable::named("OVHD_APU_START_PB_IS_AVAILABLE"), 8, 7)?
.with_engine_anti_ice(2)?
.with_wing_anti_ice()?
.with_failures(vec![
.with_failures([
(
21_000,
FailureType::Acsc(AcscId::Acsc1(Channel::ChannelOne)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AtcMailboxInstrument implements FsInstrument {

private readonly hEventPublisher = new HEventPublisher(this.bus);

private readonly failuresConsumer = new FailuresConsumer('A32NX');
private readonly failuresConsumer = new FailuresConsumer();

constructor(public readonly instrument: BaseInstrument) {
this.hEventPublisher = new HEventPublisher(this.bus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class FcuFsInstrument implements FsInstrument {
private readonly clock = new Clock(this.bus);
private readonly hEventPublisher = new HEventPublisher(this.bus);

private readonly failuresConsumer = new FailuresConsumer('A32NX');
private readonly failuresConsumer = new FailuresConsumer();

//private readonly isFailedKey = A380Failure.Fcu;
private readonly isFailed = Subject.create(false);
Expand Down
2 changes: 1 addition & 1 deletion fbw-a380x/src/systems/instruments/src/MFD/instrument.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class MfdInstrument implements FsInstrument {
private readonly fmcBFailed = Subject.create(false);
private readonly fmcCFailed = Subject.create(false);

private readonly failuresConsumer = new FailuresConsumer('A32NX');
private readonly failuresConsumer = new FailuresConsumer();

constructor(public readonly instrument: BaseInstrument) {
this.backplane.addPublisher('mfd', this.simVarPublisher);
Expand Down
2 changes: 1 addition & 1 deletion fbw-a380x/src/systems/instruments/src/PFD/PFD.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class PFDComponent extends DisplayComponent<PFDProps> {

constructor(props: PFDProps) {
super(props);
this.failuresConsumer = new FailuresConsumer('A32NX');
this.failuresConsumer = new FailuresConsumer();
}

public onAfterRender(node: VNode): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class A380xRmpStateController implements Instrument {

private readonly standbyLedVar = `L:A380X_RMP_${this.rmpIndex}_GREEN_LED`;

private readonly failuresConsumer = new FailuresConsumer('A32NX');
private readonly failuresConsumer = new FailuresConsumer();

private readonly rmp3BusA = Subject.create(false);

Expand Down
2 changes: 1 addition & 1 deletion fbw-a380x/src/systems/systems-host/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class SystemsHost extends BaseInstrument {

private readonly hEventPublisher: HEventPublisher;

private readonly failuresConsumer = new FailuresConsumer('A32NX');
private readonly failuresConsumer = new FailuresConsumer();

// TODO: Migrate PowerSupplyBusses, if needed
private gpws: LegacyGpws;
Expand Down
3 changes: 3 additions & 0 deletions fbw-a380x/src/wasm/fbw_a380/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ clang \
-flto \
-D_MSFS_WASM=1 \
-D__wasi__ \
-D_LARGEFILE64_SOURCE \
-D_LIBCPP_HAS_NO_THREADS \
-D_WINDLL \
-D_MBCS \
Expand Down Expand Up @@ -74,10 +75,12 @@ clang++ \
-target wasm32-unknown-wasi \
-D_MSFS_WASM=1 \
-D__wasi__ \
-D_LARGEFILE64_SOURCE \
-D_LIBCPP_HAS_NO_THREADS \
-D_WINDLL \
-D_MBCS \
-DNOMINMAX \
-DJSON_HAS_FILESYSTEM=0 \
-mthread-model single \
-fno-exceptions \
-fms-extensions \
Expand Down
3 changes: 0 additions & 3 deletions fbw-a380x/src/wasm/fbw_a380/src/FlyByWireInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ void FlyByWireInterface::disconnect() {
bool FlyByWireInterface::update(double sampleTime) {
bool result = true;

// update failures handler
failuresConsumer.update();

// get data & inputs
result &= readDataAndLocalVariables(sampleTime);

Expand Down
57 changes: 28 additions & 29 deletions fbw-a380x/src/wasm/fbw_a380/src/failures/FailuresConsumer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
#include "FailuresConsumer.h"
#include <MSFS/MSFS_CommBus.h>
#include <algorithm>
#include <iostream>
#include <nlohmann/json.hpp>
#include <utility>

using json = nlohmann::json;

static void FailuresConsumerCommBusHandler(const char* buf, unsigned int bufSize, void* ctx) {
auto* failuresConsumer = static_cast<FailuresConsumer*>(ctx);
if (failuresConsumer == nullptr || buf == nullptr || bufSize < 1 || buf[bufSize - 1] != '\0') {
return;
}

json data = json::parse(buf);
if (!data.is_array()) {
return;
}

std::set<int> failures;
data.get_to(failures);

failuresConsumer->acceptFailures(failures);
}

FailuresConsumer::FailuresConsumer() {
activeFailures.emplace(std::make_pair<Failures, bool>(Failures::Prim1, false));
activeFailures.emplace(std::make_pair<Failures, bool>(Failures::Prim2, false));
Expand All @@ -14,43 +36,20 @@ FailuresConsumer::FailuresConsumer() {
}

void FailuresConsumer::initialize() {
activateLvar = std::make_unique<LocalVariable>("A32NX_FAILURE_ACTIVATE");
deactivateLvar = std::make_unique<LocalVariable>("A32NX_FAILURE_DEACTIVATE");
fsCommBusRegister("FBW_FAILURE_UPDATE", FailuresConsumerCommBusHandler, this);
fsCommBusCall("FBW_FAILURE_REQUEST", nullptr, 0, FsCommBusBroadcast_JS);
}

void FailuresConsumer::update() {
updateActivate();
updateDeactivate();
void FailuresConsumer::acceptFailures(const std::set<int>& failures) {
for (auto& activeFailure : activeFailures) {
activeFailure.second = failures.contains(static_cast<int>(activeFailure.first));
}
}

bool FailuresConsumer::isActive(Failures failure) {
return (*activeFailures.find(failure)).second;
}

void FailuresConsumer::updateActivate() {
double index = activateLvar->get();
if (setIfFound(index, true)) {
activateLvar->set(0);
}
}

void FailuresConsumer::updateDeactivate() {
double index = deactivateLvar->get();
if (setIfFound(index, false)) {
deactivateLvar->set(0);
}
}

bool FailuresConsumer::setIfFound(double identifier, bool value) {
auto pair = activeFailures.find(static_cast<Failures>(identifier));
if (pair == activeFailures.end()) {
return false;
} else {
(*pair).second = value;
return true;
}
}

bool FailuresConsumer::isAnyActive() {
if (std::any_of(activeFailures.begin(), activeFailures.end(), [](auto pair) { return pair.second; })) {
return true;
Expand Down
Loading
Loading