FESA3 C++ Code Snippets
Working with devices in the server-part
// Since server actions are only executed for a specific device,
// we directly can use the argument "pDev", which is passed to the execute-method
pDev->myField.set(someValue,pEvt->getMultiplexingContext());
// In Server-Actions the global-device can be accessed by using the service-locator
MyClassServiceLocator_->getGlobalDevice()->myField.set(someValue,pEvt->getMultiplexingContext());
For the complete API of the class "device" and it's relatives, check the
doxygen documentation.
Working with devices in the RT-part
// In a RT-Action we can iterate over the whole device-collection like that:
std::vector<Device*>::iterator device;
for(device=deviceCol_.begin();device!=deviceCol_.end();++device)
{
// perform some action with the device
(*device)->myField.set(someValue,pEvt->getMultiplexingContext());
}
// In RT-Action global-fields can be accessed by using the service-locator
MyClassServiceLocator_->getGlobalDevice()->myField.set(someValue,pEvt->getMultiplexingContext());
For the complete API of the class "device" and it's relatives, check the
doxygen documentation.
Working with devices in the specificInit methods
#include <fesa-core/Synchronization/NoneContext.h>
#include <MyClass/GeneratedCode/DeviceFactory.h>//put in the name of your class
//Since an empty context is needed to access any field, you can make use of the fesa-internal-type "NoneContext"
fesa::NoneContext context;
// In the method "specificInit" of the RT/Server-DeviceClass a member "devCol_" is not provided to access devices.
// For now, we have to use the DeviceFactory to obtain device-references outside of the Server/RTAction
// Later this job will be done by the Service-Locator
const std::vector<Device*> devCol = DeviceFactory::getInstance()->getDeviceCollection();
GlobalDevice* globalDev = DeviceFactory::getInstance()->getGlobalDevice();
std::vector<Device*>::const_iterator device;
for(device= devCol.begin();device!=devCol.end();++device)
{
std::cout << "DeviceName: " << (*device)->getName() << std::endl;
(*device)->myField.set(someValue,&context);
}
Working with FESA scalar-fields
int32_t myData = (*device)->myField.get(pEvt->getMultiplexingContext());
myData ++;
(*device)->myField.set(myData,pEvt->getMultiplexingContext());
For the complete API of scalar-fields and it's relatives, check the
doxygen documentation.
Working with FESA array-fields
// Set data to a array-field:
uint32_t sizeOfmyData = 4;
int32_t myData[sizeOfmyData];
myData[1] = 15;
(*device)->myArrayField.set(myData,sizeOfmyData,pEvt->getMultiplexingContext());
// Get data from a array-field:
uint32_t dim;
const int32_t* myData = (*device)->myArrayField.get(dim,pEvt->getMultiplexingContext());
For the complete API of array-fields and it's relatives, check the
doxygen documentation.
Working with FESA 2D-array-fields
uint32_t index1 = 1;
uint32_t index2 = 1;
uint32_t dim1 = 3;
uint32_t dim2 = 2;
//Get a cell-value
const int32_t myData = (*device)->my2DArray.getCell(index1,index2,pEvt->getMultiplexingContext());
//Set a cell-value
int32_t myNewData
(*device)->my2DArray.setCell(myNewData,index1,index2,pEvt->getMultiplexingContext());
//Get a row
const int32_t* myData = (*device)->my2DArray.getRow(index1,dim1,pEvt->getMultiplexingContext());
//Set a row/column
uint32_t index = 1;
int32_t myDataColumn[dim2];
int32_t myDataRow[dim1];
(*device)->my2DArray.setColumn(myDataColumn,index,dim2,pEvt->getMultiplexingContext());
(*device)->my2DArray.setRow(myDataRow,index,dim1,pEvt->getMultiplexingContext());
//Get the complete array2D
// Currently this method does not work for 2D-arrays because of some internal bug ... please use getCell(..) for now !
// uint32_t dim1;
// uint32_t dim2;
// const int32_t** myDataAll = (*device)->my2DArray.get(dim1,dim2,pEvt->getMultiplexingContext());
For the complete API of 2D-array-fields and it's relatives, check the
doxygen documentation.
Working with FESA char-array-fields (strings)
std::string myString;
(*device)->myCharArray.set(myString.c_str(),pEvt->getMultiplexingContext());
const char* myCharArrayPointer = (*device)->myCharArray.get(pEvt->getMultiplexingContext());
For the complete API of string-fields and it's relatives, check the
doxygen documentation.
Working with FESA 2D-char-array-fields (string-arrays)
uint32_t index = 0;
std::string myString = "MyString";
(*device)-->myCharArray2D.setString(myString.c_str(), index,pEvt->getMultiplexingContext());
const char* myCharArrayPointer = (*device)->myCharArray2D.getString(index,pEvt->getMultiplexingContext());
uint32_t size;
const char** my2DCharArrayPointer = (*device)->myCharArray2D.get(size,pEvt->getMultiplexingContext());
std::cout << "Number of strings: " << size << std::endl;
std::cout << "First String: " << my2DCharArrayPointer[0] << std::endl;
For the complete API of string-array-fields and it's relatives, check the
doxygen documentation.
Working with bit-enum-fields
// generated code in TypeDefinition.h (just an example):
namespace MyBitEnum
{
enum MyBitEnum
{
isActive=1,
isValid=2,
hasPower=4
}; // 3 bits
}
// Access the field in Server/RT-Actions:
int32_t value = MyBitEnum::isActive | MyBitEnum::hasPower;
(*device)->myBitEnumField.set( value, pCtxt );
int32_t newValue = (*device)->myBitEnumField.get(pEvt->getMultiplexingContext());
Fill the GSI-Multiplexing-context-field
// easy method (cyclestamp, cylcename and acquisitionstamp are obtained from MultiplexingContext, when available)
// This method will only work when using the TimingEventSource, since this is the only source which provides iterrupt-stamps which can be used as acquisition-stamp
(*device)->acquisitionContext.insert(pCtxt);
// advanced method ( usage of self-defined acquisition-stamp )
//stamp should be in Nanoseconds - UTC
int64_t stamp = 12345678;
(*device)->acquisitionContext.insert(pCtxt,acquisitionStamp);
Report an error by using the GSI-error_collection-field
std::string errorString= "Put in your error-text here";
int32_t error_code= 4711;
(*device)->error_collection.addError(error_code,errorString,pEvt->getMultiplexingContext(),*device);
For the complete API of the class "GSIErrorCollectionField" and it's relatives, check the
doxygen documentation.
Usage of class-specific custom-types
// Take a look into "generated/cpp/MyClassName/GeneratedCode/TypeDefinition.h" to see all custom-types, defined by your class
(*device)->control.set(DEVICE_CONTROL::LOCAL,pEvt->getMultiplexingContext());
Usage of client-data in server-actions
//In a Set-Server-Acttion you may want to retrieve the data and the filter, which was send by the client:
bool myData = data.myValueItemName.get();
bool myData = filter.myFilterItemName.get();
//In a Get-Server-Action you may want to fill the data-container which will be send to the client.
int32_t myData = 1234;
data.myValueItemName.set(myData );
Usage of external headers and libraries
# Modify the file "Makefile.specific" of your class / deploy-unit, according to your needs
SOME_LIBRARY_HOME= /path/to/the/library
COMPILER_FLAGS += -I$(SOME_LIBRARY_HOME)/include
LINKER_FLAGS += -L$(SOME_LIBRARY_HOME)/lib/$(CPU) -lsome-library
Usage of the FESA-Logger
std::ostringstream message;
message << "Put your logg-message here " ;
LOG_ERROR_IF(logger, message.str());
// Depending on how important your logg-message is, you should use one of the following macros:
// LOG_TRACE_IF(logger, message.str());
// LOG_DEBUG_IF(logger, message.str());
// LOG_INFO_IF(logger, message.str());
// LOG_WARNING_IF(logger, message.str());
// LOG_ERROR_IF(logger, message.str());
// Diagnostic-Information can be logged like this (see link below for more information)
// In FESA v1.0.0 Diagnostic-Logging is not fully supported yet
LOG_DIAG_IF("MyTopic",message.str());
- Use the application-arguments -v and -vv to control the verbosity of your FESA-Software
- If needed you as well can re-configure the logging-configuration by providing a foreign config-file (Application-Argument -cfglog )
- Information on where the log-files are stored can be found here.
- Here you can find some Information about diagnostic-logging in the next fesa-release
- As well check doxygen documentation of the complete logger API.
How to trigger an on-demand-event manually
// First you have to choose "@automatic = false" on your action/triggered-event-source in the class-design
// In the server/rt-action, you can fire the event with the following command:
AbstractAction::triggerOnDemandEventSource("MyOnDemandEventSource",pEvt->getMultiplexingContext(),0);
If required, you as well can add some payload-information to the event. Check the
payload-section for more infrmation!
For the complete API of the class "AbstractAction" and it's relatives, check the
doxygen documentation.
How to trigger a property-notification manually
// The same code can be used in Server- and RT-actions
// Make sure that the names of the property/device you want to notify are correct
// For inherited or composed classes, dont use the className::propertyName format! It is sufficient to only put the property-name.
std::string notifiedProperty = "MyProperty";
std::string notifiedDevice = "MyDevice";
// You can add any number of property/device-combinations by using the following method:
registerManualNotification(notifiedProperty,notifiedDevice);
//If you are finished with adding properties/devices, send the notification by using the method below
sendManualNotification(pEvt->getMultiplexingContext());
Throwing Exceptions to clients
Prefably the type "FesaException" should be used here. ( However as well any type of RDAException will do. )
Either an object of this type can be created directly, or the class developer can inherit from the class, in order to define a class-specific exception-type.
std::string errorMessage = "Something went wrong here!";
//Currently at GSI there are no standards regarding the error-codes
std::string errorCode = "4711";
// errorCategory should be "FESACLASS_" as prefix an the classname.
std::string errorCategory = "FESACLASS_MyFesaClass";
throw fesa::FesaException(errorMessage,__FILE__,__LINE__,errorCode,errorCategory);
Enable/Disable logical events and event-sources
// By the use of the service-locator, events and sources can be enabled/disabled everywhere in the user-code
// "this->" is not needed here, it just helps to trigger the eclipse-auto-completion
this->serviceLocator_->enableEventSource("MySource");
this->serviceLocator_->disableEventSource("MySource");
// The second argument is deprecated and will be removed in comming fesa-versions
this->serviceLocator_->enableRTEvent("MyLogicalEvent","");
this->serviceLocator_->disableRTEvent("MyLogicalEvent","")
Working with Event-Payload
OnDemand Payload
// In the action which triggers the OnDemandEvent, you can add a payload. The data-format is an char-array
std::string payload = "myPayload";
AbstractAction::triggerOnDemandEventSource("MyOnDemandEventSource",pEvt->getMultiplexingContext(),payload.c_str(),payload.size(),0);
// In the RT-Action you than can read-out the defined payload like that:
#include <fesa-core/RealTime/RTEventPayload.h>
....
std::string payload = pEvt->getPayload()->getValue<char>();
Timing Payload
Comming soon
OnSubscription Payload
// In the RTAction, add this line to the includes
#include <fesa-core/RealTime/OnSubscriptionRTEventPayload.h>
...
// Use the following code in the execute-method
// This cast will not be the final solution ... however for now it works
const OnSubscriptionRTEventPayload* payload = dynamic_cast<const OnSubscriptionRTEventPayload*> (pEvt->getPayload().get());
rdaData data = payload->getRDAData();
// now you can perform different operations on the data-container
// E.g. print() will produce a printout of all items inside
// Check the cmw-rda header of Data.h for more options ( Or make use of the auto-completion! )
data.print();