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

Add generic event accessors #243

Merged
merged 1 commit into from
Sep 26, 2024
Merged
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
64 changes: 63 additions & 1 deletion src/core/genfb.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*******************************************************************************
* Copyright (c) 2018 Johannes Kepler University
* Copyright (c) 2018, 2024 Johannes Kepler University
* Martin Erich Jobst
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -9,6 +10,7 @@
*
* Contributors:
* Alois Zoitl - initial implementation and rework communication infrastructure
* Martin Erich Jobst - add generic event accessor helpers
*******************************************************************************/
#ifndef _GENFB_H_
#define _GENFB_H_
Expand Down Expand Up @@ -65,6 +67,66 @@ class CGenFunctionBlock : public T {

bool initialize();

template<typename ...Args>
void writeArguments(Args &&...paArgs) {
TPortId index = 0;
(writeArgument(index++, std::forward<Args>(paArgs)), ...);
}

template<typename Arg>
void writeArgument(TPortId paIndex, const Arg &paArg) {
const SFBInterfaceSpec &interfaceSpec = T::getFBInterfaceSpec();
if (paIndex < interfaceSpec.mNumDIs) {
T::getDIFromPortId(paIndex)->setValue(paArg);
} else if (paIndex < interfaceSpec.mNumDIs + interfaceSpec.mNumDIOs) {
T::getDIOFromPortId(paIndex - interfaceSpec.mNumDIs)->setValue(paArg);
} else if (paIndex < interfaceSpec.mNumDIs + interfaceSpec.mNumDIOs + interfaceSpec.mNumDOs) {
T::getDOFromPortId(paIndex - interfaceSpec.mNumDIs - interfaceSpec.mNumDIOs)->setValue(paArg);
}
}

template<typename ...Args>
void writeInputArguments(Args &&...paArgs) {
TPortId index = 0;
(writeInputArgument(index++, std::forward<Args>(paArgs)), ...);
}

template<typename Arg>
void writeInputArgument(TPortId paIndex, const Arg &paArg) {
const SFBInterfaceSpec &interfaceSpec = T::getFBInterfaceSpec();
if (paIndex < interfaceSpec.mNumDIs) {
T::getDIFromPortId(paIndex)->setValue(paArg);
} else if (paIndex < interfaceSpec.mNumDIs + interfaceSpec.mNumDIOs) {
T::getDIOFromPortId(paIndex - interfaceSpec.mNumDIs)->setValue(paArg);
} // skip DO
}

template<typename ...Args>
void readOutputArguments(Args &&...paArgs) {
TPortId index = 0;
(readOutputArgument(index++, std::forward<Args>(paArgs)), ...);
}

template<typename Arg>
void readOutputArgument(TPortId paIndex, Arg &&paArg) {
const SFBInterfaceSpec &interfaceSpec = T::getFBInterfaceSpec();
if (paIndex < interfaceSpec.mNumDIs) {
// do nothing
} else if (paIndex < interfaceSpec.mNumDIs + interfaceSpec.mNumDIOs) {
if constexpr (std::is_const_v<std::remove_reference_t<Arg>>) {
DEVLOG_ERROR("[CGenFunctionBlock] Trying to pass const argument to in/out variable\n");
} else {
paArg.setValue(T::getDIOFromPortId(paIndex - interfaceSpec.mNumDIs)->unwrap());
}
} else if (paIndex < interfaceSpec.mNumDIs + interfaceSpec.mNumDIOs + interfaceSpec.mNumDOs) {
if constexpr (std::is_const_v<std::remove_reference_t<Arg>>) {
DEVLOG_ERROR("[CGenFunctionBlock] Trying to pass const argument to output variable\n");
} else {
paArg.setValue(T::getDOFromPortId(paIndex - interfaceSpec.mNumDIs - interfaceSpec.mNumDIOs)->unwrap());
}
}
}

static void generateGenericInterfacePointNameArray(const char * const paPrefix,
CStringDictionary::TStringId* paNamesArayStart,
size_t paNumGenericDataPoints);
Expand Down
31 changes: 27 additions & 4 deletions src/stdfblib/net/GEN_CLIENT_fbt.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/*******************************************************************************
* Copyright (c) 2006 - 2011 ACIN, Profactor GmbH
* Copyright (c) 2006, 2024 ACIN, Profactor GmbH
* Martin Erich Jobst
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand All @@ -9,6 +11,8 @@
* Contributors:
* Alois Zoitl, Rene Smodic, Gerhard Ebenhofer, Martin Melik Merkumians
* - initial API and implementation and/or initial documentation
* Martin Erich Jobst
* - add generic event accessors
*******************************************************************************/
#ifndef _GEN_CLIENT_H_
#define _GEN_CLIENT_H_
Expand All @@ -22,10 +26,29 @@
class GEN_CLIENT : public forte::com_infra::CCommFB {
DECLARE_GENERIC_FIRMWARE_FB(GEN_CLIENT)

public:
GEN_CLIENT(const CStringDictionary::TStringId paInstanceNameId, forte::core::CFBContainer &paContainer);
~GEN_CLIENT() override = default;
public:
GEN_CLIENT(const CStringDictionary::TStringId paInstanceNameId, forte::core::CFBContainer &paContainer);

~GEN_CLIENT() override = default;

template<typename ...Args>
void evt_INIT(Args &&...paArgs) {
writeInputArguments(std::forward<Args>(paArgs)...);
receiveInputEvent(scmEventINITID, nullptr);
readOutputArguments(std::forward<Args>(paArgs)...);
}

template<typename ...Args>
void evt_REQ(Args &&...paArgs) {
writeInputArguments(std::forward<Args>(paArgs)...);
receiveInputEvent(scmSendNotificationEventID, nullptr);
readOutputArguments(std::forward<Args>(paArgs)...);
}

template<typename ...Args>
void operator()(Args &&...paArgs) {
evt_INIT(std::forward<Args>(paArgs)...);
}
};

#endif //_GEN_CLIENT_H_
34 changes: 29 additions & 5 deletions src/stdfblib/net/GEN_PUBLISH_fbt.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/*******************************************************************************
* Copyright (c) 2006 - 2011 ACIN, Profactor GmbH
* Copyright (c) 2006, 2024 ACIN, Profactor GmbH
* Martin Erich Jobst
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand All @@ -9,6 +11,8 @@
* Contributors:
* Alois Zoitl, Rene Smodic, Gerhard Ebenhofer, Martin Melik Merkumians
* - initial API and implementation and/or initial documentation
* Martin Erich Jobst
* - add generic event accessors
*******************************************************************************/
#ifndef _GEN_PUBLISH_H_
#define _GEN_PUBLISH_H_
Expand All @@ -19,11 +23,31 @@
*/

class GEN_PUBLISH : public forte::com_infra::CCommFB {
DECLARE_GENERIC_FIRMWARE_FB(GEN_PUBLISH)
DECLARE_GENERIC_FIRMWARE_FB(GEN_PUBLISH)

public:
GEN_PUBLISH(const CStringDictionary::TStringId paInstanceNameId, forte::core::CFBContainer &paContainer);

~GEN_PUBLISH() override = default;

template<typename ...Args>
void evt_INIT(Args&& ...paArgs) {
writeInputArguments(std::forward<Args>(paArgs)...);
receiveInputEvent(scmEventINITID, nullptr);
readOutputArguments(std::forward<Args>(paArgs)...);
}

template<typename ...Args>
void evt_REQ(Args&& ...paArgs) {
writeInputArguments(std::forward<Args>(paArgs)...);
receiveInputEvent(scmSendNotificationEventID, nullptr);
readOutputArguments(std::forward<Args>(paArgs)...);
}

public:
GEN_PUBLISH(const CStringDictionary::TStringId paInstanceNameId, forte::core::CFBContainer &paContainer);
~GEN_PUBLISH() override = default;
template<typename ...Args>
void operator()(Args&& ...paArgs) {
evt_INIT(std::forward<Args>(paArgs)...);
}
};

#endif //_GEN_PUBLISH_H_
Expand Down
29 changes: 26 additions & 3 deletions src/stdfblib/net/GEN_SERVER_fbt.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/*******************************************************************************
* Copyright (c) 2006 - 2011 ACIN, Profactor GmbH
* Copyright (c) 2006, 2024 ACIN, Profactor GmbH
* Martin Erich Jobst
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand All @@ -9,6 +11,8 @@
* Contributors:
* Rene Smodic, Alois Zoitl, Gerhard Ebenhofer, Martin Melik Merkumians
* - initial API and implementation and/or initial documentation
* Martin Erich Jobst
* - add generic event accessors
*******************************************************************************/
#ifndef _GEN_SERVER_H_
#define _GEN_SERVER_H_
Expand All @@ -19,12 +23,31 @@
*
*/

class GEN_SERVER: public forte::com_infra::CCommFB {
DECLARE_GENERIC_FIRMWARE_FB(GEN_SERVER)
class GEN_SERVER : public forte::com_infra::CCommFB {
DECLARE_GENERIC_FIRMWARE_FB(GEN_SERVER)
public:
GEN_SERVER(const CStringDictionary::TStringId paInstanceNameId, forte::core::CFBContainer &paContainer);

~GEN_SERVER() override = default;

template<typename ...Args>
void evt_INIT(Args &&...paArgs) {
writeArguments(std::forward<Args>(paArgs)...); // write all arguments to get type information
receiveInputEvent(scmEventINITID, nullptr);
readOutputArguments(std::forward<Args>(paArgs)...);
}

template<typename ...Args>
void evt_RSP(Args &&...paArgs) {
writeInputArguments(std::forward<Args>(paArgs)...);
receiveInputEvent(scmSendNotificationEventID, nullptr);
readOutputArguments(std::forward<Args>(paArgs)...);
}

template<typename ...Args>
void operator()(Args &&...paArgs) {
evt_INIT(std::forward<Args>(paArgs)...);
}
};

#endif //_GEN_SERVER_H_
27 changes: 25 additions & 2 deletions src/stdfblib/net/GEN_SUBSCRIBE_fbt.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/*******************************************************************************
* Copyright (c) 2006 - 2011 ACIN, Profactor GmbH
* Copyright (c) 2006, 2024 ACIN, Profactor GmbH
* Martin Erich Jobst
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand All @@ -9,6 +11,8 @@
* Contributors:
* Rene Smodic, Alois Zoitl, Gerhard Ebenhofer, Martin Melik Merkumians
* - initial API and implementation and/or initial documentation
* Martin Erich Jobst
* - add generic event accessors
*******************************************************************************/
#ifndef _GEN_SUBSCRIBE_H_
#define _GEN_SUBSCRIBE_H_
Expand All @@ -20,12 +24,31 @@


class GEN_SUBSCRIBE : public forte::com_infra::CCommFB {
DECLARE_GENERIC_FIRMWARE_FB(GEN_SUBSCRIBE)
DECLARE_GENERIC_FIRMWARE_FB(GEN_SUBSCRIBE)

public:
GEN_SUBSCRIBE(const CStringDictionary::TStringId paInstanceNameId, forte::core::CFBContainer &paContainer);

~GEN_SUBSCRIBE() override = default;

template<typename ...Args>
void evt_INIT(Args &&...paArgs) {
writeArguments(std::forward<Args>(paArgs)...); // write all arguments to get type information
receiveInputEvent(scmEventINITID, nullptr);
readOutputArguments(std::forward<Args>(paArgs)...);
}

template<typename ...Args>
void evt_RSP(Args &&...paArgs) {
writeInputArguments(std::forward<Args>(paArgs)...);
receiveInputEvent(scmSendNotificationEventID, nullptr);
readOutputArguments(std::forward<Args>(paArgs)...);
}

template<typename ...Args>
void operator()(Args &&...paArgs) {
evt_INIT(std::forward<Args>(paArgs)...);
}
};

#endif //_GEN_SUBSCRIBE_H_
Expand Down