一、官方源码示例
源码参考
服务端官方示例:
/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.* See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */#include <open62541/plugin/accesscontrol_default.h>
#include <open62541/plugin/log_stdout.h>
#include <open62541/server.h>
#include <open62541/server_config_default.h>#include <signal.h>
#include <stdlib.h>static UA_Boolean
allowAddNode(UA_Server *server, UA_AccessControl *ac,const UA_NodeId *sessionId, void *sessionContext,const UA_AddNodesItem *item) {printf("Called allowAddNode\n");return UA_TRUE;
}static UA_Boolean
allowAddReference(UA_Server *server, UA_AccessControl *ac,const UA_NodeId *sessionId, void *sessionContext,const UA_AddReferencesItem *item) {printf("Called allowAddReference\n");return UA_TRUE;
}static UA_Boolean
allowDeleteNode(UA_Server *server, UA_AccessControl *ac,const UA_NodeId *sessionId, void *sessionContext,const UA_DeleteNodesItem *item) {printf("Called allowDeleteNode\n");return UA_FALSE; // Do not allow deletion from client
}static UA_Boolean
allowDeleteReference(UA_Server *server, UA_AccessControl *ac,const UA_NodeId *sessionId, void *sessionContext,const UA_DeleteReferencesItem *item) {printf("Called allowDeleteReference\n");return UA_TRUE;
}UA_Boolean running = true;
static void stopHandler(int sign) {UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");running = false;
}static UA_UsernamePasswordLogin logins[2] = {{UA_STRING_STATIC("peter"), UA_STRING_STATIC("peter123")},{UA_STRING_STATIC("paula"), UA_STRING_STATIC("paula123")}
};int main(void) {signal(SIGINT, stopHandler);signal(SIGTERM, stopHandler);UA_Server *server = UA_Server_new();UA_ServerConfig *config = UA_Server_getConfig(server);UA_ServerConfig_setDefault(config);/* Disable anonymous logins, enable two user/password logins */config->accessControl.clear(&config->accessControl);UA_StatusCode retval = UA_AccessControl_default(config, false, NULL,&config->securityPolicies[config->securityPoliciesSize-1].policyUri, 2, logins);if(retval != UA_STATUSCODE_GOOD)goto cleanup;/* Set accessControl functions for nodeManagement */config->accessControl.allowAddNode = allowAddNode;config->accessControl.allowAddReference = allowAddReference;config->accessControl.allowDeleteNode = allowDeleteNode;config->accessControl.allowDeleteReference = allowDeleteReference;retval = UA_Server_run(server, &running);cleanup:UA_Server_delete(server);return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
}
二、程序测试
将官方的示例集成到工程种,使用源码的方式开发,需要修改头文件,修改后如下:
/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.* See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */#include "open62541.h"#include <signal.h>
#include <stdlib.h>//arm-linux-gcc open62541.c server3.c -o server3static UA_Boolean
allowAddNode(UA_Server *server, UA_AccessControl *ac,const UA_NodeId *sessionId, void *sessionContext,const UA_AddNodesItem *item) {printf("Called allowAddNode\n");return UA_TRUE;
}static UA_Boolean
allowAddReference(UA_Server *server, UA_AccessControl *ac,const UA_NodeId *sessionId, void *sessionContext,const UA_AddReferencesItem *item) {printf("Called allowAddReference\n");return UA_TRUE;
}static UA_Boolean
allowDeleteNode(UA_Server *server, UA_AccessControl *ac,const UA_NodeId *sessionId, void *sessionContext,const UA_DeleteNodesItem *item) {printf("Called allowDeleteNode\n");return UA_FALSE; // Do not allow deletion from client
}static UA_Boolean
allowDeleteReference(UA_Server *server, UA_AccessControl *ac,const UA_NodeId *sessionId, void *sessionContext,const UA_DeleteReferencesItem *item) {printf("Called allowDeleteReference\n");return UA_TRUE;
}UA_Boolean running = true;
static void stopHandler(int sign) {UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");running = false;
}static UA_UsernamePasswordLogin logins[2] = {{UA_STRING_STATIC("peter"), UA_STRING_STATIC("peter123")},{UA_STRING_STATIC("paula"), UA_STRING_STATIC("paula123")}
};int main(void) {signal(SIGINT, stopHandler);signal(SIGTERM, stopHandler);UA_Server *server = UA_Server_new();UA_ServerConfig *config = UA_Server_getConfig(server);UA_ServerConfig_setDefault(config);/* Disable anonymous logins, enable two user/password logins */config->accessControl.clear(&config->accessControl);UA_StatusCode retval = UA_AccessControl_default(config, false, NULL,&config->securityPolicies[config->securityPoliciesSize-1].policyUri, 2, logins);if(retval != UA_STATUSCODE_GOOD)goto cleanup;/* Set accessControl functions for nodeManagement */config->accessControl.allowAddNode = allowAddNode;config->accessControl.allowAddReference = allowAddReference;config->accessControl.allowDeleteNode = allowDeleteNode;config->accessControl.allowDeleteReference = allowDeleteReference;retval = UA_Server_run(server, &running);cleanup:UA_Server_delete(server);return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
}
文件结构如下:
交叉编译指令:
arm-linux-gcc open62541.c server3.c -o server3
将编译后的可执行程序下载到 Arm Linux 开发板,打开客户端进行测试。
无账号密码直接连接,提示失败,符合预期
改为账号密码登录,正常连接,获取数据,符合预期,如下: