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

QUESTION - How to add args to callback function #6413

Closed
EusebioAlsoliman opened this issue Apr 15, 2024 · 4 comments
Closed

QUESTION - How to add args to callback function #6413

EusebioAlsoliman opened this issue Apr 15, 2024 · 4 comments

Comments

@EusebioAlsoliman
Copy link

I'm trying to use a callback similar to the one in https://www.open62541.org/doc/v1.4.0/tutorial_server_datasource.html#variable-value-callback

In the example, the function afterWriteTime() is static and also doesn't need additional arguments or data. However, in my case, I need to pass extra arguments and make calls to functions. How can I do that?

Thanks in advance.

@ccvca
Copy link
Contributor

ccvca commented Apr 16, 2024

You may either use the nodeContext or the sessionContext for this. Own arguments are not possible in C.

@EusebioAlsoliman
Copy link
Author

You may either use the nodeContext or the sessionContext for this. Own arguments are not possible in C.

From the example I have seen that the callback functions should have this structure:

afterWriteTime(UA_Server *server,
               const UA_NodeId *sessionId, void *sessionContext,
               const UA_NodeId *nodeId, void *nodeContext,
               const UA_NumericRange *range, const UA_DataValue *data){
               // Code to be executed
}

But I don't know exactly how I can get the data from that nodeContext pointer. Also, how UA_Server_setNodeContext() really works?

Thanks in advance.

@ccvca
Copy link
Contributor

ccvca commented Apr 17, 2024

Use UA_Server_setNodeContext to set a pointer, which then can be used in the callback again:

// your Initialization

pMyVariable = (mytype*) malloc(sizeof(mytype));
UA_Server_setNodeContext(pServer, nodeId, pMyVariable);


afterWriteTime(UA_Server *server,
               const UA_NodeId *sessionId, void *sessionContext,
               const UA_NodeId *nodeId, void *nodeContext,
               const UA_NumericRange *range, const UA_DataValue *data){
               /// Get your variable again
               pVariableFromContext = (mytype*) nodeContext;

               // Code to be executed
}

PS: All NULL-Checks are missing!

@EusebioAlsoliman
Copy link
Author

Use UA_Server_setNodeContext to set a pointer, which then can be used in the callback again:

// your Initialization

pMyVariable = (mytype*) malloc(sizeof(mytype));
UA_Server_setNodeContext(pServer, nodeId, pMyVariable);


afterWriteTime(UA_Server *server,
               const UA_NodeId *sessionId, void *sessionContext,
               const UA_NodeId *nodeId, void *nodeContext,
               const UA_NumericRange *range, const UA_DataValue *data){
               /// Get your variable again
               pVariableFromContext = (mytype*) nodeContext;

               // Code to be executed
}

PS: All NULL-Checks are missing!

Ok, now I've understood how it works, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants