open62541 icon indicating copy to clipboard operation
open62541 copied to clipboard

Feature Request: Provide invalid node callback for server

Open smitha-cgi opened this issue 8 months ago • 0 comments

It would be useful for the server to receive a callback when a client attempts to read a node that does not exist (to log an error message or take some other sort of action). I believe this could be done by changing 4 files:

server.h - callback definition

typedef void(*UA_InvalidNodeCallback)(const UA_NodeId *sessionId, const UA_NodeId *nodeId);
void UA_Server_set_invalid_node_callback(UA_Server *server, UA_InvalidNodeCallback cb);

ua_services_attribute.c - invoke the callback when bad node read occurs:


void
Operation_Read(UA_Server *server, UA_Session *session, UA_TimestampsToReturn *ttr,
               const UA_ReadValueId *rvi, UA_DataValue *dv) {
    /* Get the node (with only the selected attribute if the NodeStore supports that) */
    const UA_Node *node =
        UA_NODESTORE_GET_SELECTIVE(server, &rvi->nodeId,
                                   attributeId2AttributeMask((UA_AttributeId)rvi->attributeId),
                                   UA_REFERENCETYPESET_NONE,
                                   UA_BROWSEDIRECTION_INVALID);
    if(!node) {
        dv->hasStatus = true;
        dv->status = UA_STATUSCODE_BADNODEIDUNKNOWN;
		
        if (server->invalidNodeCallback)
        {
	    UA_UNLOCK(server->serviceMutex);
	    server->invalidNodeCallback(session ? &session->sessionId : NULL, &rvi->nodeId);
	    UA_LOCK(server->serviceMutex);
        }
		
        return;
    }

    /* Perform the read operation */
    ReadWithNode(node, server, session, *ttr, rvi, dv);
    UA_NODESTORE_RELEASE(server, node);
}

ua_server_internal.h - callback property

UA_InvalidNodeCallback invalidNodeCallback;

ua_server.c - provide function to set the callback

void UA_Server_set_invalid_node_callback(UA_Server *server, UA_InvalidNodeCallback cb)
{
    server->invalidNodeCallback = cb;
}

Checklist

Please provide the following information:

  • [x] open62541 Version (release number or git tag): 1.4.1
  • [ ] Other OPC UA SDKs used (client or server):
  • [x] Operating system: Windows 11
  • [ ] Logs (with UA_LOGLEVEL set as low as necessary) attached
  • [ ] Wireshark network dump attached
  • [ ] Self-contained code example attached
  • [ ] Critical issue

smitha-cgi avatar Jun 16 '24 23:06 smitha-cgi