Files
marte-debug/Headers/DebugBrokerWrapper.h
2026-02-25 21:07:27 +01:00

249 lines
12 KiB
C++

#ifndef DEBUGBROKERWRAPPER_H
#define DEBUGBROKERWRAPPER_H
#include "DebugService.h"
#include "BrokerI.h"
#include "MemoryMapBroker.h"
#include "ObjectRegistryDatabase.h"
#include "ObjectBuilder.h"
#include "Vector.h"
#include "FastPollingMutexSem.h"
#include "HighResolutionTimer.h"
#include "Atomic.h"
// Original broker headers
#include "MemoryMapInputBroker.h"
#include "MemoryMapOutputBroker.h"
#include "MemoryMapSynchronisedInputBroker.h"
#include "MemoryMapSynchronisedOutputBroker.h"
#include "MemoryMapInterpolatedInputBroker.h"
#include "MemoryMapMultiBufferInputBroker.h"
#include "MemoryMapMultiBufferOutputBroker.h"
#include "MemoryMapSynchronisedMultiBufferInputBroker.h"
#include "MemoryMapSynchronisedMultiBufferOutputBroker.h"
#include "MemoryMapAsyncOutputBroker.h"
#include "MemoryMapAsyncTriggerOutputBroker.h"
namespace MARTe {
/**
* @brief Helper for optimized signal processing within brokers.
*/
class DebugBrokerHelper {
public:
static void Process(DebugService* service, BrokerInfo& info) {
if (service == NULL_PTR(DebugService*)) return;
while (service->IsPaused()) {
Sleep::MSec(10);
}
if (*info.anyActiveFlag) {
uint32 idx = info.currentSetIdx;
BrokerActiveSet& set = info.sets[idx];
uint64 ts = (uint64)((float64)HighResolutionTimer::Counter() * HighResolutionTimer::Period() * 1000000.0);
for (uint32 i = 0; i < set.numForced; i++) {
SignalExecuteInfo& s = set.forcedSignals[i];
DebugService::CopySignal(s.memoryAddress, s.forcedValue, s.size);
}
for (uint32 i = 0; i < set.numTraced; i++) {
SignalExecuteInfo& s = set.tracedSignals[i];
(void)service->traceBuffer.Push(s.internalID, ts, s.memoryAddress, s.size);
}
}
}
static void InitSignals(BrokerI* broker, DataSourceI &dataSourceIn, DebugService* &service, DebugSignalInfo** &signalInfoPointers, uint32 numCopies, MemoryMapBrokerCopyTableEntry* copyTable, const char8* functionName, SignalDirection direction, volatile bool* anyActiveFlag) {
if (numCopies > 0) {
signalInfoPointers = new DebugSignalInfo*[numCopies];
for (uint32 i=0; i<numCopies; i++) signalInfoPointers[i] = NULL_PTR(DebugSignalInfo*);
}
if (service == NULL_PTR(DebugService*)) service = DebugService::Instance();
if (service && (copyTable != NULL_PTR(MemoryMapBrokerCopyTableEntry*))) {
StreamString dsPath;
DebugService::GetFullObjectName(dataSourceIn, dsPath);
MemoryMapBroker* mmb = dynamic_cast<MemoryMapBroker*>(broker);
for (uint32 i = 0; i < numCopies; i++) {
void *addr = copyTable[i].dataSourcePointer;
TypeDescriptor type = copyTable[i].type;
uint32 dsIdx = i;
if (mmb != NULL_PTR(MemoryMapBroker*)) dsIdx = mmb->GetDSCopySignalIndex(i);
StreamString signalName;
if (!dataSourceIn.GetSignalName(dsIdx, signalName)) {
signalName.Printf("Signal_%u", dsIdx);
}
StreamString dsFullName = dsPath;
if (dsFullName.Size() > 0) dsFullName += ".";
dsFullName += signalName;
service->RegisterSignal(addr, type, dsFullName.Buffer());
if (functionName != NULL_PTR(const char8*)) {
StreamString gamFullName;
const char8* dirStr = (direction == InputSignals) ? "In" : "Out";
Reference gamRef = ObjectRegistryDatabase::Instance()->Find(functionName);
if (gamRef.IsValid()) {
StreamString absGamPath;
DebugService::GetFullObjectName(*(gamRef.operator->()), absGamPath);
gamFullName = absGamPath;
} else {
gamFullName = functionName;
}
gamFullName += ".";
gamFullName += dirStr;
gamFullName += ".";
gamFullName += signalName;
signalInfoPointers[i] = service->RegisterSignal(addr, type, gamFullName.Buffer());
} else {
signalInfoPointers[i] = service->RegisterSignal(addr, type, dsFullName.Buffer());
}
}
service->RegisterBroker(signalInfoPointers, numCopies, mmb, anyActiveFlag);
}
}
};
class DebugMemoryMapInputBroker : public MemoryMapInputBroker, public DebugBrokerI {
public:
DebugMemoryMapInputBroker() : MemoryMapInputBroker(), service(NULL), infoPtr(NULL), anyActive(false) {
(void)ObjectRegistryDatabase::Instance()->Insert(Reference(this));
}
virtual void SetService(DebugService* s) { service = s; }
virtual bool IsLinked() const { return infoPtr != NULL; }
virtual bool Execute() {
bool ret = MemoryMapInputBroker::Execute();
if (ret && infoPtr) DebugBrokerHelper::Process(service, *infoPtr);
return ret;
}
virtual bool Init(SignalDirection direction, DataSourceI &ds, const char8 *const name, void *gamMem) {
bool ret = MemoryMapInputBroker::Init(direction, ds, name, gamMem);
if (ret) {
DebugSignalInfo** sigPtrs = NULL;
DebugBrokerHelper::InitSignals(this, ds, service, sigPtrs, GetNumberOfCopies(), this->copyTable, name, direction, &anyActive);
if (service) infoPtr = service->GetBrokerInfo(service->numberOfBrokers - 1);
}
return ret;
}
DebugService* service;
BrokerInfo* infoPtr;
volatile bool anyActive;
};
template <typename T>
class DebugGenericBroker : public T, public DebugBrokerI {
public:
DebugGenericBroker() : T(), service(NULL), infoPtr(NULL), anyActive(false) {
(void)ObjectRegistryDatabase::Instance()->Insert(Reference(this));
}
virtual void SetService(DebugService* s) { service = s; }
virtual bool IsLinked() const { return infoPtr != NULL; }
virtual bool Execute() {
bool ret = T::Execute();
if (ret && infoPtr) DebugBrokerHelper::Process(service, *infoPtr);
return ret;
}
virtual bool Init(SignalDirection direction, DataSourceI &ds, const char8 *const name, void *gamMem) {
bool ret = T::Init(direction, ds, name, gamMem);
if (ret) {
DebugSignalInfo** sigPtrs = NULL;
DebugBrokerHelper::InitSignals(this, ds, service, sigPtrs, this->GetNumberOfCopies(), this->copyTable, name, direction, &anyActive);
if (service) infoPtr = service->GetBrokerInfo(service->numberOfBrokers - 1);
}
return ret;
}
DebugService* service;
BrokerInfo* infoPtr;
volatile bool anyActive;
};
class DebugMemoryMapAsyncOutputBroker : public MemoryMapAsyncOutputBroker, public DebugBrokerI {
public:
DebugMemoryMapAsyncOutputBroker() : MemoryMapAsyncOutputBroker(), service(NULL), infoPtr(NULL), anyActive(false) {
(void)ObjectRegistryDatabase::Instance()->Insert(Reference(this));
}
virtual void SetService(DebugService* s) { service = s; }
virtual bool IsLinked() const { return infoPtr != NULL; }
virtual bool Execute() {
bool ret = MemoryMapAsyncOutputBroker::Execute();
if (ret && infoPtr) DebugBrokerHelper::Process(service, *infoPtr);
return ret;
}
virtual bool InitWithBufferParameters(const SignalDirection d, DataSourceI &ds, const char8* n, void* m, const uint32 nb, const ProcessorType& c, const uint32 s) {
bool ret = MemoryMapAsyncOutputBroker::InitWithBufferParameters(d, ds, n, m, nb, c, s);
if (ret) {
DebugSignalInfo** sigPtrs = NULL;
DebugBrokerHelper::InitSignals(this, ds, service, sigPtrs, this->GetNumberOfCopies(), this->copyTable, n, d, &anyActive);
if (service) infoPtr = service->GetBrokerInfo(service->numberOfBrokers - 1);
}
return ret;
}
DebugService* service;
BrokerInfo* infoPtr;
volatile bool anyActive;
};
class DebugMemoryMapAsyncTriggerOutputBroker : public MemoryMapAsyncTriggerOutputBroker, public DebugBrokerI {
public:
DebugMemoryMapAsyncTriggerOutputBroker() : MemoryMapAsyncTriggerOutputBroker(), service(NULL), infoPtr(NULL), anyActive(false) {
(void)ObjectRegistryDatabase::Instance()->Insert(Reference(this));
}
virtual void SetService(DebugService* s) { service = s; }
virtual bool IsLinked() const { return infoPtr != NULL; }
virtual bool Execute() {
bool ret = MemoryMapAsyncTriggerOutputBroker::Execute();
if (ret && infoPtr) DebugBrokerHelper::Process(service, *infoPtr);
return ret;
}
virtual bool InitWithTriggerParameters(const SignalDirection d, DataSourceI &ds, const char8* n, void* m, const uint32 nb, const uint32 pre, const uint32 post, const ProcessorType& c, const uint32 s) {
bool ret = MemoryMapAsyncTriggerOutputBroker::InitWithTriggerParameters(d, ds, n, m, nb, pre, post, c, s);
if (ret) {
DebugSignalInfo** sigPtrs = NULL;
DebugBrokerHelper::InitSignals(this, ds, service, sigPtrs, this->GetNumberOfCopies(), this->copyTable, n, d, &anyActive);
if (service) infoPtr = service->GetBrokerInfo(service->numberOfBrokers - 1);
}
return ret;
}
DebugService* service;
BrokerInfo* infoPtr;
volatile bool anyActive;
};
typedef DebugGenericBroker<MemoryMapOutputBroker> DebugMemoryMapOutputBroker;
typedef DebugGenericBroker<MemoryMapSynchronisedInputBroker> DebugMemoryMapSynchronisedInputBroker;
typedef DebugGenericBroker<MemoryMapSynchronisedOutputBroker> DebugMemoryMapSynchronisedOutputBroker;
typedef DebugGenericBroker<MemoryMapInterpolatedInputBroker> DebugMemoryMapInterpolatedInputBroker;
typedef DebugGenericBroker<MemoryMapMultiBufferInputBroker> DebugMemoryMapMultiBufferInputBroker;
typedef DebugGenericBroker<MemoryMapMultiBufferOutputBroker> DebugMemoryMapMultiBufferOutputBroker;
typedef DebugGenericBroker<MemoryMapSynchronisedMultiBufferInputBroker> DebugMemoryMapSynchronisedMultiBufferInputBroker;
typedef DebugGenericBroker<MemoryMapSynchronisedMultiBufferOutputBroker> DebugMemoryMapSynchronisedMultiBufferOutputBroker;
template <typename T>
class DebugBrokerBuilder : public ObjectBuilder {
public:
virtual Object *Build(HeapI* const heap) const {
return new (heap) T();
}
};
typedef DebugBrokerBuilder<DebugMemoryMapInputBroker> DebugMemoryMapInputBrokerBuilder;
typedef DebugBrokerBuilder<DebugMemoryMapOutputBroker> DebugMemoryMapOutputBrokerBuilder;
typedef DebugBrokerBuilder<DebugMemoryMapSynchronisedInputBroker> DebugMemoryMapSynchronisedInputBrokerBuilder;
typedef DebugBrokerBuilder<DebugMemoryMapSynchronisedOutputBroker> DebugMemoryMapSynchronisedOutputBrokerBuilder;
typedef DebugBrokerBuilder<DebugMemoryMapInterpolatedInputBroker> DebugMemoryMapInterpolatedInputBrokerBuilder;
typedef DebugBrokerBuilder<DebugMemoryMapMultiBufferInputBroker> DebugMemoryMapMultiBufferInputBrokerBuilder;
typedef DebugBrokerBuilder<DebugMemoryMapMultiBufferOutputBroker> DebugMemoryMapMultiBufferOutputBrokerBuilder;
typedef DebugBrokerBuilder<DebugMemoryMapSynchronisedMultiBufferInputBroker> DebugMemoryMapSynchronisedMultiBufferInputBrokerBuilder;
typedef DebugBrokerBuilder<DebugMemoryMapSynchronisedMultiBufferOutputBroker> DebugMemoryMapSynchronisedMultiBufferOutputBrokerBuilder;
typedef DebugBrokerBuilder<DebugMemoryMapAsyncOutputBroker> DebugMemoryMapAsyncOutputBrokerBuilder;
typedef DebugBrokerBuilder<DebugMemoryMapAsyncTriggerOutputBroker> DebugMemoryMapAsyncTriggerOutputBrokerBuilder;
}
#endif