Skip to content

Commit

Permalink
create device factory and cmake commands for it
Browse files Browse the repository at this point in the history
  • Loading branch information
cochicde committed Jan 19, 2025
1 parent 1511891 commit 7569db5
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 34 deletions.
12 changes: 6 additions & 6 deletions buildsupport/forte.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,6 @@ FUNCTION(forte_add_architecture)
ENDFOREACH(ARG)
ENDFUNCTION(forte_add_architecture)

FUNCTION(forte_add_device)
FOREACH(ARG ${ARGV})
set_property(GLOBAL APPEND PROPERTY FORTE_DEVICES ${ARG})
ENDFOREACH(ARG)
ENDFUNCTION(forte_add_device)

MACRO(forte_add_network_layer NAME ONOFF CONFIGNAME CLASSNAME FILENAME DESCRIPTION)
# TODO: parse filename from filename
set(FORTE_COM_${NAME} ${ONOFF} CACHE BOOL "${DESCRIPTION}")
Expand All @@ -248,6 +242,12 @@ MACRO(forte_add_ecet CLASSNAME FILENAME ECET_NAME)
set_property(GLOBAL APPEND PROPERTY FORTE_ECET_NAME "${ECET_NAME}")
ENDMACRO(forte_add_ecet)

MACRO(forte_add_device CLASSNAME FILENAME DEVICE_NAME)
set_property(GLOBAL APPEND PROPERTY FORTE_DEVICE_CLASSES ${CLASSNAME})
set_property(GLOBAL APPEND PROPERTY FORTE_DEVICE_FILENAMES "${FILENAME}.h")
set_property(GLOBAL APPEND PROPERTY FORTE_DEVICE_NAMES "${DEVICE_NAME}")
ENDMACRO(forte_add_device)

MACRO(forte_add_startup_hook FUNCTION_NAME)
set_property(GLOBAL APPEND PROPERTY FORTE_STARTUP_HOOK_FUNCTIONS ${FUNCTION_NAME})
ENDMACRO(forte_add_startup_hook)
Expand Down
58 changes: 48 additions & 10 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,57 @@ file(REMOVE ${CMAKE_BINARY_DIR}/core/ecetFactory_new.cpp)
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/core/ecetFactory.h.in ${CMAKE_BINARY_DIR}/core/ecetFactory_new.h)
forte_replacefile_if_changed(${CMAKE_BINARY_DIR}/core/ecetFactory_new.h ${CMAKE_BINARY_DIR}/core/ecetFactory.h)
file(REMOVE ${CMAKE_BINARY_DIR}/core/ecetFactory_new.h)

######################################################################################
# Available Devices
######################################################################################
forte_add_sourcefile_with_path_cpp(${CMAKE_BINARY_DIR}/deviceFactory.cpp) # created file

GET_PROPERTY(DEVICE_CLASSES GLOBAL PROPERTY FORTE_DEVICE_CLASSES)
GET_PROPERTY(DEVICE_FILENAMES GLOBAL PROPERTY FORTE_DEVICE_FILENAMES)
GET_PROPERTY(DEVICE_NAMES GLOBAL PROPERTY FORTE_DEVICE_NAMES)

set(FORTE_DEVICE_DEFAULT "RMT_DEV" CACHE STRING "The default device to use")

if(NOT "${FORTE_DEVICE_DEFAULT}" IN_LIST DEVICE_NAMES)
message(FATAL_ERROR "${FORTE_DEVICE_DEFAULT} is not a valid device. Choose one of the following: ${DEVICE_NAMES}")
endif()

LIST(LENGTH DEVICE_CLASSES DEVICE_CLASSES_LEN)
math(EXPR DEVICE_CLASSES_LEN ${DEVICE_CLASSES_LEN}-1)

SET(DEVICE_INCLUDE "")
SET(DEVICE_CREATE "")
SET(DEVICE_ENUM "")

FOREACH(POS RANGE ${DEVICE_CLASSES_LEN})
LIST(GET DEVICE_CLASSES ${POS} CLASS)
LIST(GET DEVICE_FILENAMES ${POS} FILENAME)
LIST(GET DEVICE_NAMES ${POS} DEVICE_NAME)

SET(DEVICE_INCLUDE "${DEVICE_INCLUDE}#include \"${FILENAME}\"\n")

SET(DEVICE_ENUM "${DEVICE_ENUM}\n ${DEVICE_NAME},")

SET(DEVICE_STRING_TO_ENUM "${DEVICE_STRING_TO_ENUM} if(paDevice == \"${DEVICE_NAME}\") {\n deviceToCreate = AvailableDevices::${DEVICE_NAME};\n return true;\n }\n")


SET(DEVICE_CREATE "${DEVICE_CREATE} if(deviceToCreate == AvailableDevices::${DEVICE_NAME})\n return (paMGRID.length() != 0) ? std::make_unique<${CLASS}>(paMGRID) : std::make_unique<${CLASS}>();\n")
math(EXPR ECET_ID ${ECET_ID}+1)
ENDFOREACH(POS)

CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/deviceFactory.cpp.in ${CMAKE_BINARY_DIR}/deviceFactory_new.cpp)
forte_replacefile_if_changed(${CMAKE_BINARY_DIR}/deviceFactory_new.cpp ${CMAKE_BINARY_DIR}/deviceFactory.cpp)
file(REMOVE ${CMAKE_BINARY_DIR}/deviceFactory_new.cpp)

CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/deviceFactory.h.in ${CMAKE_BINARY_DIR}/deviceFactory_new.h)
forte_replacefile_if_changed(${CMAKE_BINARY_DIR}/deviceFactory_new.h ${CMAKE_BINARY_DIR}/deviceFactory.h)
file(REMOVE ${CMAKE_BINARY_DIR}/deviceFactory_new.h)

#############################################################################
# Configure startuphook
#############################################################################

GET_PROPERTY(STARTUP_HOOK_FUNCTIONS GLOBAL PROPERTY FORTE_STARTUP_HOOK_FUNCTIONS)
LIST(APPEND STARTUP_HOOK_FUNCTIONS_LIST ${STARTUP_HOOK_FUNCTIONS})

Expand Down Expand Up @@ -296,16 +344,6 @@ GET_PROPERTY(architectures GLOBAL PROPERTY FORTE_ARCHITECTURES)
list(SORT architectures)
set_property(CACHE FORTE_ARCHITECTURE PROPERTY STRINGS None ${architectures})

######################################################################################
# Device to be used in this 4diac FORTE instance
######################################################################################
set(FORTE_DEVICE "RMT_DEV" CACHE STRING "Device to be used in this 4diac FORTE instance")
GET_PROPERTY(devices GLOBAL PROPERTY FORTE_DEVICES)
list(SORT devices)
set_property(CACHE FORTE_DEVICE PROPERTY STRINGS None ${devices})

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/forte_device_config.h.in ${CMAKE_BINARY_DIR}/forte_device_config.h)

#######################################################################################
# Create Files
#######################################################################################
Expand Down
2 changes: 1 addition & 1 deletion src/arch/c_interface/forte_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ FORTE_STATUS forteStartInstanceGeneric(int argc, char *argv[], TForteInstance* p
}

const auto ipPort = parseCommandLineArguments(argc, argv);
if((0 == strlen(ipPort)) || (nullptr == strchr(ipPort, ':'))){
if((ipPort == nullptr) || (0 == strlen(ipPort)) || (nullptr == strchr(ipPort, ':'))){
listHelp();
return FORTE_WRONG_PARAMETERS;
}
Expand Down
2 changes: 1 addition & 1 deletion src/arch/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int main(int argc, char *arg[]){
hookSignals();

const char *ipPort = parseCommandLineArguments(argc, arg);
if((0 == strlen(ipPort)) || (nullptr == strchr(ipPort, ':'))) {
if((ipPort == nullptr) || (0 == strlen(ipPort)) || (nullptr == strchr(ipPort, ':'))) {
//! Lists the help for FORTE
listHelp();
return -1;
Expand Down
8 changes: 8 additions & 0 deletions src/arch/utils/mainparam_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "deviceFactory.h"

#ifdef FORTE_TRACE_CTF
#include <string>
Expand All @@ -28,6 +29,7 @@ void listHelp(){
printf("Options:\n");
printf("%-20s Display this information\n", " -h");
printf("%-20s Set the listening IP and port for the incoming connections\n", " -c <IP>:<port>");
printf("%-20s Set the device to be used\n", " -d DEVICE_NAME");
#ifdef FORTE_SUPPORT_BOOT_FILE
printf("%-20s Set the boot-file where to read from to load the applications\n", " -f <file>");
#endif
Expand Down Expand Up @@ -60,6 +62,12 @@ const char *parseCommandLineArguments(int argc, char *arg[]){
case 'c': //! sets the destination for the connection
pIpPort = arg[i + 1];
break;
case 'd': //! sets the destination for the connection
if(!DeviceFactory::setDeviceToCreate(arg[i + 1])){
printf("The selected device '%s' is not valid. Select one of the following: %s\n", arg[i + 1], DeviceFactory::getAvailableDevices().c_str());
return nullptr;
}
break;
#ifdef FORTE_SUPPORT_BOOT_FILE
case 'f': //! sets the boot-file to be used
gCommandLineBootFile = arg[i + 1];
Expand Down
4 changes: 2 additions & 2 deletions src/core/forteinstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
*******************************************************************************/

#include "forteinstance.h"
#include "forte_device_config.h"
#include "deviceFactory.h"


std::unique_ptr<CDevice> C4diacFORTEInstance::createDev(const std::string &paMGRID) {
std::unique_ptr<FORTE_DEVICE> dev = (paMGRID.length() != 0) ? std::make_unique<FORTE_DEVICE>(paMGRID) : std::make_unique<FORTE_DEVICE>();
auto dev = DeviceFactory::createDevice(paMGRID);
dev->initialize();
return dev;
}
Expand Down
39 changes: 39 additions & 0 deletions src/deviceFactory.cpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*******************************************************************************
* Copyright (c) 2025
* 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.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Jose Cabral - initial API and implementation and/or initial documentation
*******************************************************************************/

#include "deviceFactory.h"

${DEVICE_INCLUDE}

namespace DeviceFactory {

AvailableDevices deviceToCreate{AvailableDevices::${FORTE_DEVICE_DEFAULT}};

void setDeviceToCreate(AvailableDevices paDevice){
deviceToCreate = paDevice;
}

bool setDeviceToCreate(std::string paDevice){
${DEVICE_STRING_TO_ENUM}
return false;
}

std::string getAvailableDevices() {
return "${DEVICE_NAMES}";
}

std::unique_ptr<CDevice> createDevice(const std::string &paMGRID){
${DEVICE_CREATE}
return nullptr;
}

} // DeviceFactory
32 changes: 32 additions & 0 deletions src/deviceFactory.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*******************************************************************************
* Copyright (c) 2025 Jose Cabral
* 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.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Jose Cabral - initial API and implementation and/or initial documentation
*******************************************************************************/

#include <memory>
#include <string>

class CDevice;

namespace DeviceFactory {

enum class AvailableDevices {${DEVICE_ENUM}
};

std::unique_ptr<CDevice> createDevice(const std::string &paMGRID);

void setDeviceToCreate(AvailableDevices paDevice);

bool setDeviceToCreate(std::string paDevice);

std::string getAvailableDevices();

};

4 changes: 0 additions & 4 deletions src/forte_device_config.h.in

This file was deleted.

16 changes: 7 additions & 9 deletions src/stdfblib/ita/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@
SET(SOURCE_GROUP ${SOURCE_GROUP}\\ita)
forte_add_sourcefile_hcpp(DEV_MGR EMB_RES RMT_DEV RMT_RES)

forte_add_device(RMT_DEV)
forte_add_device(RMT_DEV "stdfblib/ita/RMT_DEV" "RMT_DEV")

if (FORTE_COM_OPC_UA)
forte_add_sourcefile_hcpp(Config_EMB_RES)

forte_add_device(OPCUA_DEV)
if("${FORTE_DEVICE}" STREQUAL "OPCUA_DEV")
set(FORTE_DISABLE_OPCUA_DEV FALSE CACHE BOOL "Do not compile the OPCUA Device")
if(NOT ${FORTE_DISABLE_OPCUA_DEV})
forte_add_device(OPCUA_DEV "stdfblib/ita/OPCUA_DEV" "OPCUA_DEV")
forte_add_sourcefile_hcpp(OPCUA_DEV OPCUA_MGR)
endif()
forte_add_sourcefile_hcpp(Config_EMB_RES)
endif ()

if(FORTE_FAKE_TIME)
forte_add_device(FakeTimeDev)
if("${FORTE_DEVICE}" STREQUAL "FakeTimeDev")
forte_add_sourcefile_hcpp(FakeTimeDev)
endif()
forte_add_device(FakeTimeDev "stdfblib/ita/FakeTimeDev" "FakeTimeDev")
forte_add_sourcefile_hcpp(FakeTimeDev)
endif()

forte_add_include_directories(${CMAKE_CURRENT_SOURCE_DIR})
Expand Down
2 changes: 1 addition & 1 deletion src/stdfblib/ita/OPCUA_DEV.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class OPCUA_DEV : public RMT_DEV {

OPCUA_MGR mOPCUAMgr;

OPCUA_DEV(const std::string &paMGRID);
OPCUA_DEV(const std::string &paMGRID = "opc.tcp://localhost:4840");
virtual ~OPCUA_DEV();

virtual int startDevice(void);
Expand Down

0 comments on commit 7569db5

Please sign in to comment.