Browse Source

Backported handle attachement to method callbacks from application.

ichrispa 9 years ago
parent
commit
198597c520

+ 6 - 3
examples/server.c

@@ -157,8 +157,9 @@ static UA_StatusCode writeLedStatus(void *handle, const UA_Variant *data, const
 }
 
 #ifdef ENABLE_METHODCALLS
-static UA_StatusCode getMonitoredItems(const UA_NodeId objectId, const UA_Variant *input, UA_Variant *output) {
+static UA_StatusCode getMonitoredItems(const UA_NodeId objectId, const UA_Variant *input, UA_Variant *output, void *handle) {
     UA_String tmp = UA_STRING("Hello World");
+    //UA_Server *theServer = (UA_Server *) handle; // Commented, would result in "unused variable" error
     UA_Variant_setScalarCopy(output, &tmp, &UA_TYPES[UA_TYPES_STRING]);
     printf("getMonitoredItems was called\n");
     return UA_STATUSCODE_GOOD;
@@ -348,8 +349,10 @@ int main(int argc, char** argv) {
   UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1,62541), UA_QUALIFIEDNAME(1,"ping"), UA_LOCALIZEDTEXT("en_US", "ping"),
                           UA_LOCALIZEDTEXT("en_US", "Return a single argument as passed by the caller"),
                           UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
-                          0,0,
-                          &getMonitoredItems, 1, &inputArguments, 1, &outputArguments, &methodId);
+                          0, 0,
+                          &getMonitoredItems, // Call this method
+                          (void *) server,    // Pass our server pointer as a handle to the method
+                          1, &inputArguments, 1, &outputArguments, &methodId);
 #endif
    
   // Example for iterating over all nodes referenced by "Objects":

+ 2 - 2
examples/server_method.c

@@ -18,7 +18,7 @@
 UA_Boolean running = UA_TRUE;
 UA_Logger logger;
 
-static UA_StatusCode helloWorldMethod(const UA_NodeId objectId, const UA_Variant *input, UA_Variant *output) {
+static UA_StatusCode helloWorldMethod(const UA_NodeId objectId, const UA_Variant *input, UA_Variant *output, void *handle) {
     UA_String *inputStr = (UA_String*)input->data;
     UA_String tmp = UA_STRING_ALLOC("Hello ");
     if(inputStr->length > 0) {
@@ -68,7 +68,7 @@ int main(int argc, char** argv) {
     UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1,62541), UA_QUALIFIEDNAME(1, "hello world"), 
                             UA_LOCALIZEDTEXT("en_US","Hello World"), UA_LOCALIZEDTEXT("en_US","Say `Hello World`"),
                             UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
-                            0, 0, &helloWorldMethod, 1, &inputArguments, 1, &outputArguments, NULL);
+                            0, 0, &helloWorldMethod, NULL, 1, &inputArguments, 1, &outputArguments, NULL);
 
     /* start server */
     UA_StatusCode retval = UA_Server_run(server, 1, &running); //blocks until running=false

+ 2 - 2
include/ua_server.h

@@ -252,7 +252,7 @@ UA_Server_AddMonodirectionalReference(UA_Server *server, UA_NodeId sourceNodeId,
 
 #ifdef ENABLE_METHODCALLS
 typedef UA_StatusCode (*UA_MethodCallback)(const UA_NodeId objectId, const UA_Variant *input,
-                                           UA_Variant *output);
+                                           UA_Variant *output, void *handle);
 /** Creates a serverside method including input- and output variable descriptions
  * 
  * @param server The server object.
@@ -282,7 +282,7 @@ UA_StatusCode UA_EXPORT
 UA_Server_addMethodNode(UA_Server *server, UA_NodeId nodeId, const UA_QualifiedName browseName,
                         UA_LocalizedText displayName, UA_LocalizedText description, const UA_NodeId parentNodeId, 
                         const UA_NodeId referenceTypeId, UA_UInt32 userWriteMask, UA_UInt32 writeMask, 
-                        UA_MethodCallback method, UA_Int32 inputArgumentsSize, const UA_Argument *inputArguments, 
+                        UA_MethodCallback method, void *handle, UA_Int32 inputArgumentsSize, const UA_Argument *inputArguments, 
                         UA_Int32 outputArgumentsSize, const UA_Argument *outputArguments,
                         UA_NodeId *createdNodeId);
 #endif

+ 3 - 0
src/server/ua_nodes.c

@@ -239,6 +239,7 @@ void UA_MethodNode_init(UA_MethodNode *p) {
     p->executable = UA_FALSE;
     p->userExecutable = UA_FALSE;
 #ifdef ENABLE_METHODCALLS
+    p->methodHandle        = UA_NULL;
     p->attachedMethod      = UA_NULL;
 #endif
 }
@@ -260,6 +261,7 @@ void UA_MethodNode_deleteMembers(UA_MethodNode *p) {
 void UA_MethodNode_delete(UA_MethodNode *p) {
     UA_MethodNode_deleteMembers(p);
 #ifdef ENABLE_METHODCALLS
+    p->methodHandle   = UA_NULL;
     p->attachedMethod = UA_NULL;
 #endif
     UA_free(p);
@@ -272,6 +274,7 @@ UA_StatusCode UA_MethodNode_copy(const UA_MethodNode *src, UA_MethodNode *dst) {
     dst->executable = src->executable;
     dst->userExecutable = src->userExecutable;
 #ifdef ENABLE_METHODCALLS
+    dst->methodHandle  = src->methodHandle;
     dst->attachedMethod = src->attachedMethod;
 #endif
     return retval;

+ 1 - 0
src/server/ua_nodes.h

@@ -109,6 +109,7 @@ typedef struct {
     UA_Boolean executable;
     UA_Boolean userExecutable;
 #ifdef ENABLE_METHODCALLS
+    void *methodHandle;
     UA_MethodCallback attachedMethod;
 #endif
 } UA_MethodNode;

+ 2 - 1
src/server/ua_server_addressspace.c

@@ -765,7 +765,7 @@ UA_StatusCode
 UA_Server_addMethodNode(UA_Server* server, UA_NodeId nodeId, const UA_QualifiedName browseName, 
                         UA_LocalizedText displayName, UA_LocalizedText description, const UA_NodeId parentNodeId, 
                         const UA_NodeId referenceTypeId, UA_UInt32 userWriteMask, UA_UInt32 writeMask, 
-                        UA_MethodCallback method, UA_Int32 inputArgumentsSize, const UA_Argument* inputArguments, 
+                        UA_MethodCallback method, void *handle, UA_Int32 inputArgumentsSize, const UA_Argument* inputArguments, 
                         UA_Int32 outputArgumentsSize, const UA_Argument* outputArguments, 
                         UA_NodeId* createdNodeId) {
     UA_StatusCode retval = UA_STATUSCODE_GOOD;
@@ -777,6 +777,7 @@ UA_Server_addMethodNode(UA_Server* server, UA_NodeId nodeId, const UA_QualifiedN
     newMethod->writeMask = writeMask;
     newMethod->userWriteMask = userWriteMask;
     newMethod->attachedMethod = method;
+    newMethod->methodHandle   = handle;
     newMethod->executable = UA_TRUE;
     newMethod->userExecutable = UA_TRUE;
     

+ 1 - 1
src/server/ua_services_call.c

@@ -173,7 +173,7 @@ static void callMethod(UA_Server *server, UA_Session *session, UA_CallMethodRequ
                                                outputArguments->value.variant.arrayLength);
         result->outputArgumentsSize = outputArguments->value.variant.arrayLength;
         result->statusCode = methodCalled->attachedMethod(withObject->nodeId, request->inputArguments,
-                                                          result->outputArguments);
+                                                          result->outputArguments, methodCalled->methodHandle);
     }
     else
         result->statusCode = UA_STATUSCODE_BADNOTWRITABLE; // There is no NOTEXECUTABLE?

+ 1 - 0
tools/pyUANamespace/ua_node_types.py

@@ -1137,6 +1137,7 @@ class opcua_node_method_t(opcua_node_t):
       code.append("       // Note: in/outputArguments are added by attaching the variable nodes,")
       code.append("       //       not by including the in the addMethodNode() call.")
       code.append("       UA_NULL,")
+      code.append("       UA_NULL,")
       code.append("       0, UA_NULL,")
       code.append("       0, UA_NULL,")
       code.append("       // FIXME: Missing executable")