This lesson explains how to implement own methods.
Files used in this lesson:
Preliminary Note
The server from the previous lesson is extended by a node called “Object1” containing a method named “MultiplyMethod”.
Step 1: Implement and Set Custom Call Handler
A custom call handler is necessary to process own methods.
Implement Custom Call Handler
{
int i;
uint16_t ns_index;
unsigned int method_index;
fctMethodCall fctmethod;
continue;
}
continue;
}
#ifdef UA_AUTHORIZATION_SUPPORT
#endif
fctmethod = g_methods[method_index];
if (fctmethod) {
} else {
}
} else {
}
}
}
Set up Custom Call Handler
The call handler has to be registered as service handler for the call handler at provider initialization in custom_provider.c.
#ifdef ENABLE_SERVICE_CALL
ctx->call = custom_provider_method_call;
#endif
Step 2: Create Nodes Necessary for multiply Method
We need additional nodes for this lesson, so we’re extending the node creation from the previous lesson.
Create Method Node
Create the actual node of the method as child of “Object1”.
&nodeid,
UA_NODECLASS_METHOD,
nodeid.nsindex,
method_name,
NULL,
parent,
UA_NODE_HASCOMPONENT);
Create Input Argument Nodes
&nodeid,
UA_NODECLASS_VARIABLE,
0,
"InputArguments",
NULL,
UA_NODE_PROPERTYTYPE,
method_node,
UA_NODE_HASPROPERTY);
if (ret != 0) return ret;
Create Output Argument Nodes
&nodeid,
UA_NODECLASS_VARIABLE,
0,
"OutputArguments",
NULL,
UA_NODE_PROPERTYTYPE,
method_node,
UA_NODE_HASPROPERTY);
if (ret != 0) return ret;
Step 3: Initialize Method Arguments
In this step, we set the value of the input and output argument properties. The value includes the name, desciption, data type and value rank of the arguments and is persisted in the memorystore.
Initialize Input Arguments
if (ret != 0)
return -1;
if (ret != 0) {
return -1;
}
Initialize Output Arguments
if (ret != 0)
return -1;
if (ret != 0) {
return -1;
}
Step 4: Add multiply Method
The actual function to be executed when the method is called must still be implemented and added to our small array of method functions during provider initialization.
Implement Function
{
float a, b, product;
product = a * b;
return 0;
}
Add Function to Method Array
int custom_provider_method_init(void)
{
int ret;
if (ret != 0) return -1;
g_methods[0] = custom_provider_method_multiply;
return 0;
}