From 3b8c9a8ed4e6a1d7cbed59436176c88bdde7155e Mon Sep 17 00:00:00 2001 From: wlyu Date: Wed, 23 Mar 2022 19:21:44 +0800 Subject: [PATCH] format OPUCA codes and optimize demo cases --- .../control_app/opcua_demo/opcua_demo.c | 97 +++++---- .../control_app/plc_demo/plc_control_demo.c | 197 ++++++++++++------ .../plc/interoperability/opcua/open62541.c | 82 +++----- .../plc/interoperability/opcua/ua_api.c | 23 +- .../plc/interoperability/opcua/ua_api.h | 37 ++-- .../plc/interoperability/opcua/ua_client.c | 138 ++++++++---- .../plc/interoperability/opcua/ua_server.c | 28 +-- .../plc/interoperability/opcua/ua_test.c | 72 +++---- .../control/plc/shared/plc_channel.c | 2 +- .../control/plc/shared/plc_channel.h | 28 +-- .../Framework/control/plc/shared/plc_device.c | 8 +- .../resources/ethernet/LwIP/arch/sys_arch.c | 1 + 12 files changed, 413 insertions(+), 300 deletions(-) diff --git a/APP_Framework/Applications/control_app/opcua_demo/opcua_demo.c b/APP_Framework/Applications/control_app/opcua_demo/opcua_demo.c index e1edcf29a..eb4962eef 100755 --- a/APP_Framework/Applications/control_app/opcua_demo/opcua_demo.c +++ b/APP_Framework/Applications/control_app/opcua_demo/opcua_demo.c @@ -40,6 +40,9 @@ /******************************************************************************* * Variables ******************************************************************************/ +// to count test +static int test_cnt = 0; +static int fail_cnt = 0; // count failure times char test_ua_ip[] = {192, 168, 250, 2}; @@ -50,37 +53,56 @@ char test_ua_ip[] = {192, 168, 250, 2}; static void UaConnectTestTask(void* arg) { struct netif net; - UA_StatusCode retval; - char ua_uri[UA_URL_SIZE]; - memset(ua_uri, 0, sizeof(ua_uri)); + UA_StatusCode ret; + char url[UA_URL_SIZE]; + memset(url, 0, sizeof(url)); + UA_Client* client = UA_Client_new(); if(client == NULL) { - ua_print("ua: [%s] tcp client null\n", __func__); + ua_error("ua: [%s] tcp client null\n", __func__); return; } UA_ClientConfig* config = UA_Client_getConfig(client); - UA_ClientConfig_setDefault(config); - snprintf(ua_uri, sizeof(ua_uri), "opc.tcp://%d.%d.%d.%d:4840", - test_ua_ip[0], test_ua_ip[1], test_ua_ip[2], test_ua_ip[3]); - ua_notice("ua uri: %d %s\n", strlen(ua_uri), ua_uri); - retval = UA_Client_connect(client,ua_uri); - if(retval != UA_STATUSCODE_GOOD) + UA_ClientConfig_setDefault(config); + + snprintf(url, sizeof(url), "opc.tcp://%d.%d.%d.%d:4840", + test_ua_ip[0], test_ua_ip[1], test_ua_ip[2], test_ua_ip[3]); + + ua_notice("ua connect cnt %d fail %d\n", test_cnt++, fail_cnt ++); + ua_notice("ua connect uri: %.*s\n", strlen(url), url); + + ret = UA_Client_connect(client, url); + + if(ret != UA_STATUSCODE_GOOD) { - ua_notice("ua: [%s] connected failed %x\n", __func__, retval); + ua_error("ua: [%s] connected failed %x\n", __func__, ret); UA_Client_delete(client); + fail_cnt++; return; } - ua_notice("ua: [%s] connected ok!\n", __func__); + ua_notice("ua connected ok!\n"); UA_Client_delete(client); } -void UaConnectTest(void* arg) +static void UaConnectTest(int argc, char *argv[]) { + if(argc == 2) + { + if(isdigit(argv[1][0])) + { + if(sscanf(argv[1], "%d.%d.%d.%d", &test_ua_ip[0], &test_ua_ip[1], &test_ua_ip[2], &test_ua_ip[3]) == EOF) + { + lw_notice("input wrong ip\n"); + return; + } + } + } + lwip_config_tcp(lwip_ipaddr, lwip_netmask, test_ua_ip); sys_thread_new("ua test", UaConnectTestTask, NULL, UA_STACK_SIZE, UA_TASK_PRIO); } @@ -90,10 +112,10 @@ SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | void UaBrowserObjectsTestTask(void* param) { - static int test_cnt = 0; - UA_Client* client = UA_Client_new(); ua_notice("ua: [%s] start %d ...\n", __func__, test_cnt++); + UA_Client* client = UA_Client_new(); + if(client == NULL) { ua_error("ua: [%s] tcp client NULL\n", __func__); @@ -102,25 +124,26 @@ void UaBrowserObjectsTestTask(void* param) UA_ClientConfig* config = UA_Client_getConfig(client); UA_ClientConfig_setDefault(config); - UA_StatusCode retval = UA_Client_connect(client, opc_server_url); + UA_StatusCode ret = UA_Client_connect(client, opc_server_url); - if(retval != UA_STATUSCODE_GOOD) + if(ret != UA_STATUSCODE_GOOD) { - ua_error("ua: [%s] connect failed %#x\n", __func__, retval); + ua_error("ua: [%s] connect failed %#x\n", __func__, ret); UA_Client_delete(client); return; } ua_notice("--- start read time ---\n", __func__); - ua_read_time(client); + UaGetServerTime(client); + ua_notice("--- get server info ---\n", __func__); - ua_test_browser_objects(client); + UaTestBrowserObjects(client); /* Clean up */ UA_Client_delete(client); /* Disconnects the client internally */ } -void* UaBrowserObjectsTest(int argc, char* argv[]) +static void* UaBrowserObjectsTest(int argc, char* argv[]) { if(argc == 2) { @@ -140,35 +163,33 @@ void* UaBrowserObjectsTest(int argc, char* argv[]) } SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3), - UaObj, UaBrowserObjectsTest, UaObj [IP]); + UaObject, UaBrowserObjectsTest, UaObject [IP]); void UaGetInfoTestTask(void* param) { UA_Client* client = UA_Client_new(); - ua_notice("ua: [%s] start ...\n", __func__); + ua_notice("--- Get OPUCA objects ---\n", __func__); if(client == NULL) { - ua_print("ua: [%s] tcp client null\n", __func__); + ua_error("ua: [%s] tcp client null\n", __func__); return; } UA_ClientConfig* config = UA_Client_getConfig(client); UA_ClientConfig_setDefault(config); - UA_StatusCode retval = UA_Client_connect(client, opc_server_url); + UA_StatusCode ret = UA_Client_connect(client, opc_server_url); - if(retval != UA_STATUSCODE_GOOD) + if(ret != UA_STATUSCODE_GOOD) { - ua_print("ua: [%s] connect failed %#x\n", __func__, retval); + ua_error("ua: [%s] connect failed %#x\n", __func__, ret); UA_Client_delete(client); return; } - ua_print("ua: [%s] connect ok!\n", __func__); ua_notice("--- interactive server ---\n", __func__); - ua_test_interact_server(client); - /* Clean up */ - UA_Client_disconnect(client); + UaTestInteractServer(client); + UA_Client_delete(client); /* Disconnects the client internally */ } @@ -201,26 +222,24 @@ void UaAddNodesTask(void* param) if(client == NULL) { - ua_print("ua: [%s] tcp client null\n", __func__); + ua_error("ua: [%s] client null\n", __func__); return; } UA_ClientConfig* config = UA_Client_getConfig(client); UA_ClientConfig_setDefault(config); - UA_StatusCode retval = UA_Client_connect(client, opc_server_url); + UA_StatusCode ret = UA_Client_connect(client, opc_server_url); - if(retval != UA_STATUSCODE_GOOD) + if(ret != UA_STATUSCODE_GOOD) { - ua_print("ua: [%s] connect failed %#x\n", __func__, retval); + ua_print("ua: [%s] connect failed %#x\n", __func__, ret); UA_Client_delete(client); return; } - ua_print("ua: [%s] connect ok!\n", __func__); ua_notice("--- add nodes ---\n", __func__); - ua_add_nodes(client); - /* Clean up */ - UA_Client_disconnect(client); + UaAddNodes(client); + UA_Client_delete(client); /* Disconnects the client internally */ } diff --git a/APP_Framework/Applications/control_app/plc_demo/plc_control_demo.c b/APP_Framework/Applications/control_app/plc_demo/plc_control_demo.c index 64cb7d913..0df64375f 100755 --- a/APP_Framework/Applications/control_app/plc_demo/plc_control_demo.c +++ b/APP_Framework/Applications/control_app/plc_demo/plc_control_demo.c @@ -36,6 +36,32 @@ UA_NodeId test_nodeid = {4, UA_NODEIDTYPE_NUMERIC, 5}; /******************************************************************************/ +// get NodeId from str +void PlcGetTestNodeId(char *str, UA_NodeId *id) +{ + static char node_str[UA_NODE_LEN]; + memset(node_str, 0, sizeof(node_str)); + + plc_print("plc: arg %s\n", str); + + if(sscanf(str, PLC_NS_FORMAT, &id->namespaceIndex, node_str) != EOF) + { + if(isdigit(node_str[0])) + { + id->identifierType = UA_NODEIDTYPE_NUMERIC; + id->identifier.numeric = atoi(node_str); + plc_print("ns %d num %d\n", id->namespaceIndex, id->identifier.numeric); + } + else + { + id->identifierType = UA_NODEIDTYPE_STRING; + id->identifier.string.length = strlen(node_str); + id->identifier.string.data = node_str; + plc_print("ns %d str %s\n", id->namespaceIndex, id->identifier.string.data); + } + } +} + void PlcDemoChannelDrvInit(void) { static uint8_t init_flag = 0; @@ -52,33 +78,42 @@ void PlcDemoChannelDrvInit(void) memset(&plc_demo_dev, 0, sizeof(plc_demo_dev)); } +static void PlcGetDemoDev(PlcDeviceType *dev, UA_NodeId *id) +{ + // register plc device + dev->state = CHDEV_INIT; + strcpy(dev->name, "UA Demo"); + dev->info.product = "CPU 1215C"; + dev->info.vendor = "SIEMENS"; + dev->info.model = "S7-1200"; + dev->info.id = 123; + dev->net = PLC_IND_ENET_OPCUA; + + // register UA parameter + if(!dev->priv_data) + { + dev->priv_data = (UaParamType*)malloc(sizeof(UaParamType)); + } + UaParamType* ua_ptr = dev->priv_data; + memset(ua_ptr, 0, sizeof(UaParamType)); + strcpy(ua_ptr->ua_remote_ip, opc_server_url); + ua_ptr->act = UA_ACT_ATTR; + memcpy(&ua_ptr->ua_id, id, sizeof(*id)); +} + static void PlcCtrlDemoInit(void) { static uint8_t init_flag = 0; PlcDemoChannelDrvInit(); - // register plc device - plc_demo_dev.state = CHDEV_INIT; - strcpy(plc_demo_dev.name, "UA Demo"); - plc_demo_dev.info.product = "CPU 1215C"; - plc_demo_dev.info.vendor = "SIEMENS"; - plc_demo_dev.info.model = "S7-1200"; - plc_demo_dev.info.id = 123; - plc_demo_dev.net = PLC_IND_ENET_OPCUA; - // register UA parameter - if(!plc_demo_dev.priv_data) - { - plc_demo_dev.priv_data = (UaParamType*)malloc(sizeof(UaParamType)); - } - UaParamType* ua_ptr = plc_demo_dev.priv_data; - memset(ua_ptr, 0, sizeof(UaParamType)); - strcpy(ua_ptr->ua_remote_ip, opc_server_url); - ua_ptr->act = UA_ACT_ATTR; - memcpy(&ua_ptr->ua_id, &test_nodeid, sizeof(test_nodeid)); + // register plc device + PlcGetDemoDev(&plc_demo_dev, &test_nodeid); if(init_flag) + { return; + } init_flag = 1; if(PlcDevRegister(&plc_demo_dev, NULL, plc_demo_dev.name) != EOK) @@ -121,24 +156,7 @@ void PlcReadTest(int argc, char* argv[]) if(argc > 1) { - plc_print("plc: arg %s\n", argv[1]); - - if(sscanf(argv[1], PLC_NS_FORMAT, &test_nodeid.namespaceIndex, node_str) != EOF) - { - if(isdigit(node_str[0])) - { - test_nodeid.identifierType = UA_NODEIDTYPE_NUMERIC; - test_nodeid.identifier.numeric = atoi(node_str); - plc_print("ns %d num %d\n", test_nodeid.namespaceIndex, test_nodeid.identifier.numeric); - } - else - { - test_nodeid.identifierType = UA_NODEIDTYPE_STRING; - test_nodeid.identifier.string.length = strlen(node_str); - test_nodeid.identifier.string.data = node_str; - plc_print("ns %d str %s\n", test_nodeid.namespaceIndex, test_nodeid.identifier.string.data); - } - } + PlcGetTestNodeId(argv[1], &test_nodeid); } sys_thread_new("plc read", PlcReadUATask, NULL, PLC_STACK_SIZE, PLC_TASK_PRIO); @@ -168,7 +186,7 @@ void PlcWriteUATask(void* arg) if(EOK != ret) { - plc_print("plc: [%s] read failed\n", __func__); + plc_print("plc: [%s] write failed\n", __func__); } ops->close(&plc_demo_dev); @@ -183,30 +201,13 @@ void PlcWriteTest(int argc, char* argv[]) if(argc > 1) { - plc_print("plc: arg %s\n", argv[1]); + PlcGetTestNodeId(argv[1], &test_nodeid); + } - if(sscanf(argv[1], PLC_NS_FORMAT, &test_nodeid.namespaceIndex, node_str) != EOF) - { - if(isdigit(node_str[0])) - { - test_nodeid.identifierType = UA_NODEIDTYPE_NUMERIC; - test_nodeid.identifier.numeric = atoi(node_str); - plc_print("ns %d num %d\n", test_nodeid.namespaceIndex, test_nodeid.identifier.numeric); - } - else - { - test_nodeid.identifierType = UA_NODEIDTYPE_STRING; - test_nodeid.identifier.string.length = strlen(node_str); - test_nodeid.identifier.string.data = node_str; - plc_print("ns %d str %s\n", test_nodeid.namespaceIndex, test_nodeid.identifier.string.data); - } - } - - if(argc > 2) - { - strcpy(val_param, argv[2]); - plc_print("write value %s\n", val_param); - } + if(argc > 2) + { + strcpy(val_param, argv[2]); + plc_print("write value %s\n", val_param); } sys_thread_new("plc write", PlcWriteUATask, val_param, PLC_STACK_SIZE, PLC_TASK_PRIO); @@ -215,3 +216,79 @@ void PlcWriteTest(int argc, char* argv[]) SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3), PlcWrite, PlcWriteTest, Read PLC); +// test motor +// 清零 +//PlcWrite n4,2 0b +//PlcWrite n4,3 0b +//PlcWrite n4,4 0b +//PlcWrite n4,5 0b +// +// 使能 +//PlcWrite n4,2 1b +// +// 设转速 +//PlcWrite n4,7 50 +// +// 正转 +//PlcWrite n4,4 1b +// +// 反转 +//PlcWrite n4,5 1b + + +void PlcMotorTestTask(void* arg) +{ + char *test_target[] = {"n4,2", "n4,3", "n4,4", "n4,5"}; + char *test_cmd[] = {"1b", "50", "1b", "1b"}; + + int ret = 0; + struct PlcOps* ops = NULL; + char buf[PLC_BUF_SIZE]; + memset(buf, 0, sizeof(buf)); + + PlcCtrlDemoInit(); + ops = plc_demo_dev.ops; + ret = ops->open(&plc_demo_dev); + + if(EOK != ret) + { + plc_print("plc: [%s] open failed %#x\n", __func__, ret); + return; + } + + UaParamType* ua_ptr = plc_demo_dev.priv_data; + + for(int i = 0; i < 4; i++) + { + UA_NodeId id; + PlcGetTestNodeId(test_target[i], &id); + memcpy(&ua_ptr->ua_id, &id, sizeof(id)); + ret = ops->write(&plc_demo_dev, "0b", PLC_BUF_SIZE); + if(EOK != ret) + { + plc_print("plc: [%s] %d write failed\n", __func__, __LINE__); + } + } + + for(int i = 0; i < 4; i++) + { + UA_NodeId id; + PlcGetTestNodeId(test_target[i], &id); + memcpy(&ua_ptr->ua_id, &id, sizeof(id)); + ret = ops->write(&plc_demo_dev, test_cmd[i], PLC_BUF_SIZE); + if(EOK != ret) + { + plc_print("plc: [%s] %d write failed\n", __func__, __LINE__); + } + } + ops->close(&plc_demo_dev); +} + +void PlcMotorTest(int argc, char* argv[]) +{ + sys_thread_new("plc motor", PlcMotorTestTask, NULL, PLC_STACK_SIZE, PLC_TASK_PRIO); +} + +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3), + PlcMotorTest, PlcMotorTest, Run motor); + diff --git a/APP_Framework/Framework/control/plc/interoperability/opcua/open62541.c b/APP_Framework/Framework/control/plc/interoperability/opcua/open62541.c index fc525aa8d..eb88c92f5 100755 --- a/APP_Framework/Framework/control/plc/interoperability/opcua/open62541.c +++ b/APP_Framework/Framework/control/plc/interoperability/opcua/open62541.c @@ -36,6 +36,8 @@ #include "open62541.h" #include "ua_api.h" +int ua_run_flag = 0; + #if LWIP_DNS #else @@ -7482,8 +7484,7 @@ Array_encodeBinary(const void *src, size_t length, const UA_DataType *type, Ctx } UA_assert(ret != UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED); - //tst by wly - ua_debug("ua: [%s] src %p len %d %d type %p <%d> <%d> %p ret %d\n", __func__, + ua_debug1("ua: [%s] src %p len %d %d type %p <%d> <%d> %p ret %d\n", __func__, src, length, signed_length, *type, type->typeKind, type->overlayable, ctx, ret); return ret; @@ -8334,7 +8335,7 @@ encodeBinaryStruct(const void *src, const UA_DataType *type, Ctx *ctx) { return ret; } - ua_debug("ua: [%s] > %d < %d mt %p %d %d dep %d msg %p %p:<%x> <%d> isArry %d ret %d\n", __func__, + ua_debug1("ua: [%s] > %d < %d mt %p %d %d dep %d msg %p %p:<%x> <%d> isArry %d ret %d\n", __func__, i, type->membersSize, mt, @@ -8362,7 +8363,7 @@ encodeBinaryStruct(const void *src, const UA_DataType *type, Ctx *ctx) { ret = encodeWithExchangeBuffer((const void*)ptr, mt, ctx); UA_assert(ret != UA_STATUSCODE_BADENCODINGLIMITSEXCEEDED); - ua_debug("ua: [%s] >> %d < %d mt %p %d %d dep %d msg %p %p:<%x> <%d> isArry %d ret %d\n", __func__, + ua_debug1("ua: [%s] >> %d < %d mt %p %d %d dep %d msg %p %p:<%x> <%d> isArry %d ret %d\n", __func__, i, type->membersSize, mt, @@ -8614,7 +8615,7 @@ decodeBinaryStructure(void *dst, const UA_DataType *type, Ctx *ctx) { if(mt->typeKind >= UA_DATATYPEKINDS) { - ua_debug("ua: [%s] fail %d < %d mt %p %d %d dep %d msg %p %p:<%x> <%d>\n", __func__, + ua_debug1("ua: [%s] fail %d < %d mt %p %d %d dep %d msg %p %p:<%x> <%d>\n", __func__, i, membersSize, mt, @@ -8629,7 +8630,7 @@ decodeBinaryStructure(void *dst, const UA_DataType *type, Ctx *ctx) { return ret; } - ua_debug("ua: [%s] > %d < %d mt %p %d %d dep %d msg %p %p:<%x> <%d> isArry %d ret %d\n", __func__, + ua_debug1("ua: [%s] > %d < %d mt %p %d %d dep %d msg %p %p:<%x> <%d> isArry %d ret %d\n", __func__, i, membersSize, mt, @@ -8649,7 +8650,7 @@ decodeBinaryStructure(void *dst, const UA_DataType *type, Ctx *ctx) { ptr += sizeof(size_t); ret = Array_decodeBinary((void *UA_RESTRICT *UA_RESTRICT)ptr, length, mt , ctx); ptr += sizeof(void*); - ua_debug("ua: [%s] %d ret %d ptr %p len %d\n", __func__, i, ret, ptr, length); + ua_debug1("ua: [%s] %d ret %d ptr %p len %d\n", __func__, i, ret, ptr, length); continue; } @@ -8657,7 +8658,7 @@ decodeBinaryStructure(void *dst, const UA_DataType *type, Ctx *ctx) { ret = decodeBinaryJumpTable[mt->typeKind]((void *UA_RESTRICT)ptr, mt, ctx); ptr += mt->memSize; - ua_debug("ua: [%s] >> %d < %d dep %d msg %p %p:<%x> <%d> ret %d\n", __func__, + ua_debug1("ua: [%s] >> %d < %d dep %d msg %p %p:<%x> <%d> ret %d\n", __func__, i, membersSize, ctx->depth, @@ -8668,7 +8669,7 @@ decodeBinaryStructure(void *dst, const UA_DataType *type, Ctx *ctx) { ret); } - ua_debug("ua: [%s] >>> dep %d msg %p %p:<%x> <%d> ret %d\n", __func__, + ua_debug1("ua: [%s] >>> dep %d msg %p %p:<%x> <%d> ret %d\n", __func__, ctx->depth, ptr, dst, @@ -8818,14 +8819,14 @@ UA_decodeBinaryInternal(const UA_ByteString *src, size_t *offset, /* Decode */ memset(dst, 0, type->memSize); /* Initialize the value */ - ua_debug("ua: [%s] t %d mem %d len %d off %d pos %d end %d dst %p type %x size %x\n", __func__, + ua_debug1("ua: [%s] t %d mem %d len %d off %d pos %d end %d dst %p type %x size %x\n", __func__, type->typeKind, type->memSize, src->length, *offset, *ctx.pos, *ctx.end, dst, ((UA_TcpMessageHeader *)dst)->messageTypeAndChunkType, ((UA_TcpMessageHeader *)dst)->messageSize); status ret = decodeBinaryJumpTable[type->typeKind](dst, type, &ctx); - ua_debug("ua: [%s] -> t %d dst %p type %x size %x ret %d\n", __func__, + ua_debug1("ua: [%s] -> t %d dst %p type %x size %x ret %d\n", __func__, type->typeKind, dst, ((UA_TcpMessageHeader *)dst)->messageTypeAndChunkType, ((UA_TcpMessageHeader *)dst)->messageSize, ret); @@ -8837,12 +8838,12 @@ UA_decodeBinaryInternal(const UA_ByteString *src, size_t *offset, UA_clear(dst, type); memset(dst, 0, type->memSize); - ua_debug("ua: [%s] => t %d dst %p type %x size %x\n", __func__, + ua_debug1("ua: [%s] => t %d dst %p type %x size %x\n", __func__, type->typeKind, dst, ((UA_TcpMessageHeader *)dst)->messageTypeAndChunkType, ((UA_TcpMessageHeader *)dst)->messageSize); } - ua_debug("ua: [%s] #> off %d %p %p t %d dst %p type %x size %x\n", __func__, *offset, + ua_debug1("ua: [%s] #> off %d %p %p t %d dst %p type %x size %x\n", __func__, *offset, ctx.pos, src->data, type->typeKind, dst, ((UA_TcpMessageHeader *)dst)->messageTypeAndChunkType, ((UA_TcpMessageHeader *)dst)->messageSize); @@ -18656,7 +18657,7 @@ extractCompleteChunk(UA_SecureChannel *channel, const UA_ByteString *buffer, UA_decodeBinaryInternal(buffer, &initial_offset, &hdr, &UA_TRANSPORT[UA_TRANSPORT_TCPMESSAGEHEADER], NULL); - ua_debug("ua: [%s] res %d buf %p offset %d hdr %d size %d\n", __func__, res, buffer, *offset, + ua_debug1("ua: [%s] res %d buf %p offset %d hdr %d size %d\n", __func__, res, buffer, *offset, hdr.messageTypeAndChunkType, hdr.messageSize); UA_assert(res == UA_STATUSCODE_GOOD); @@ -18671,7 +18672,7 @@ extractCompleteChunk(UA_SecureChannel *channel, const UA_ByteString *buffer, return UA_STATUSCODE_BADTCPMESSAGETYPEINVALID; if(hdr.messageSize > channel->config.recvBufferSize) { - ua_debug("lw: [%s] msg size %d rec %d\n", __func__, hdr.messageSize, channel->config.recvBufferSize); + ua_debug1("lw: [%s] msg size %d rec %d\n", __func__, hdr.messageSize, channel->config.recvBufferSize); return UA_STATUSCODE_BADTCPMESSAGETOOLARGE; } @@ -43657,6 +43658,7 @@ UA_Client_delete(UA_Client* client) { UA_Client_clear(client); UA_ClientConfig_clear(&client->config); UA_free(client); + ua_run_flag = 0; } void @@ -45187,9 +45189,6 @@ connectIterate(UA_Client *client, UA_UInt32 timeout) { return UA_STATUSCODE_BADCONNECTIONCLOSED; } - ua_debug("ua: [%s] conn %d state %d handle %p\n", __func__, client->connectStatus, - client->connection.state, client->connection.handle); - /* The connection is closed. Reset the SecureChannel and open a new TCP * connection */ if(client->connection.state == UA_CONNECTIONSTATE_CLOSED) @@ -45201,7 +45200,7 @@ connectIterate(UA_Client *client, UA_UInt32 timeout) { client->config.pollConnectionFunc(&client->connection, timeout, &client->config.logger); - ua_debug("ua: [%s] exit conn %x %d time %d handle %p\n", __func__, + ua_debug1("ua: [%s] exit conn %x %d time %d handle %p\n", __func__, client->connectStatus, client->connection.state, timeout, client->connection.handle); @@ -45363,6 +45362,8 @@ initConnect(UA_Client *client) { (int)client->endpointUrl.length, client->endpointUrl.data); client->connectStatus = UA_STATUSCODE_BADCONNECTIONCLOSED; closeSecureChannel(client); + ua_debug("ua: [%s] connect %d failed timeout %d\n", __func__, client->connection.state, + client->config.timeout); } return client->connectStatus; @@ -45399,7 +45400,7 @@ connectSync(UA_Client *client) { UA_DateTime now = UA_DateTime_nowMonotonic(); UA_DateTime maxDate = now + ((UA_DateTime)client->config.timeout * UA_DATETIME_MSEC); - ua_print("ua; [%s] time %d\n", __func__, (UA_DateTime)client->config.timeout); + ua_debug("ua: [%s] time %d\n", __func__, (UA_DateTime)client->config.timeout); UA_StatusCode retval = initConnect(client); if(retval != UA_STATUSCODE_GOOD) @@ -70300,16 +70301,21 @@ UA_ServerConfig_setDefaultWithSecurityPolicies(UA_ServerConfig *conf, UA_Client * UA_Client_new() { UA_ClientConfig config; + + if(ua_run_flag) + return NULL; + memset(&config, 0, sizeof(UA_ClientConfig)); config.logger.log = UA_Log_Stdout_log; config.logger.context = NULL; config.logger.clear = UA_Log_Stdout_clear; + ua_run_flag = 1; return UA_Client_newWithConfig(&config); } UA_StatusCode UA_ClientConfig_setDefault(UA_ClientConfig *config) { - config->timeout = 20000; + config->timeout = 5000; config->secureChannelLifeTime = 10 * 60 * 1000; /* 10 minutes */ if(!config->logger.log) { @@ -71651,15 +71657,11 @@ UA_ClientConnectionTCP_poll(UA_Connection *connection, UA_UInt32 timeout, * and getsockopt using SO_ERROR on win32 and posix. */ if(connection->sockfd == UA_INVALID_SOCKET) { - ua_print("ua: [%s] start socket %p ai %d sa %d\n", __func__, - tcpConnection->server, - tcpConnection->server->ai_family, - tcpConnection->server->ai_addr->sa_family); + connection->sockfd = UA_socket(tcpConnection->server->ai_family, tcpConnection->server->ai_socktype, tcpConnection->server->ai_protocol); if(connection->sockfd == UA_INVALID_SOCKET) { - ua_print("ua: [%s] %s\n", __func__, strerror(UA_ERRNO)); UA_LOG_WARNING(logger, UA_LOGCATEGORY_NETWORK, "Could not create client socket: %s", strerror(UA_ERRNO)); ClientNetworkLayerTCP_close(connection); @@ -71680,11 +71682,6 @@ UA_ClientConnectionTCP_poll(UA_Connection *connection, UA_UInt32 timeout, return UA_STATUSCODE_BADDISCONNECT; } - ua_print("ua: [%s] check fd %d %p fam %d sa %d\n", __func__, - connection->sockfd, - tcpConnection->server, - tcpConnection->server->ai_family, - tcpConnection->server->ai_addr->sa_family); /* Don't have the socket create interrupt signals */ #ifdef SO_NOSIGPIPE int val = 1; @@ -71693,12 +71690,6 @@ UA_ClientConnectionTCP_poll(UA_Connection *connection, UA_UInt32 timeout, if(sso_result < 0) UA_LOG_WARNING(logger, UA_LOGCATEGORY_NETWORK, "Couldn't set SO_NOSIGPIPE"); #endif - ua_print("ua: [%s] connect ai %d sa fam %d len %d %x addr len %d\n", __func__, - tcpConnection->server->ai_family, - tcpConnection->server->ai_addr->sa_family, - tcpConnection->server->ai_addr->sa_len, - tcpConnection->server->ai_addr->sa_data[0], - tcpConnection->server->ai_addrlen); int error = UA_connect(connection->sockfd, tcpConnection->server->ai_addr, tcpConnection->server->ai_addrlen); @@ -71772,6 +71763,7 @@ UA_ClientConnectionTCP_poll(UA_Connection *connection, UA_UInt32 timeout, (int)tcpConnection->endpointUrl.length, tcpConnection->endpointUrl.data, strerror(UA_ERRNO)); ClientNetworkLayerTCP_close(connection); + ua_error("ua: [%s] line %d failed\n", __func__, __LINE__); return UA_STATUSCODE_BADDISCONNECT; } @@ -71802,6 +71794,7 @@ UA_ClientConnectionTCP_poll(UA_Connection *connection, UA_UInt32 timeout, LocalFree(errno_str); #endif ClientNetworkLayerTCP_close(connection); + ua_error("ua: [%s] line %d failed\n", __func__, __LINE__); return UA_STATUSCODE_BADDISCONNECT; } @@ -71831,14 +71824,11 @@ UA_ClientConnectionTCP_init(UA_ConnectionConfig config, const UA_String endpoint connection.releaseSendBuffer = connection_releasesendbuffer; connection.releaseRecvBuffer = connection_releaserecvbuffer; - ua_print("ua: [%s] endpoint url (%d)%.28s %d\n", __func__, endpointUrl.length, endpointUrl.data, - sizeof(TCPClientConnection)); - TCPClientConnection *tcpClientConnection = (TCPClientConnection*) UA_malloc(sizeof(TCPClientConnection)); if(!tcpClientConnection) { connection.state = UA_CONNECTIONSTATE_CLOSED; - ua_print("ua: [%s] malloc %d failed\n", __func__, sizeof(TCPClientConnection)); + ua_error("ua: [%s] malloc %d failed\n", __func__, sizeof(TCPClientConnection)); return connection; } memset(tcpClientConnection, 0, sizeof(TCPClientConnection)); @@ -71850,8 +71840,6 @@ UA_ClientConnectionTCP_init(UA_ConnectionConfig config, const UA_String endpoint char hostname[512]; tcpClientConnection->connStart = UA_DateTime_nowMonotonic(); - ua_print("ua: [%s] line %d!\n", __func__, __LINE__); - UA_String_copy(&endpointUrl, &tcpClientConnection->endpointUrl); UA_StatusCode parse_retval = @@ -71864,8 +71852,6 @@ UA_ClientConnectionTCP_init(UA_ConnectionConfig config, const UA_String endpoint return connection; } - ua_print("ua: [%s] line %d!\n", __func__, __LINE__); - memcpy(hostname, hostnameString.data, hostnameString.length); hostname[hostnameString.length] = 0; @@ -71875,22 +71861,16 @@ UA_ClientConnectionTCP_init(UA_ConnectionConfig config, const UA_String endpoint "No port defined, using default port %" PRIu16, port); } - ua_print("ua: [%s] line %d!\n", __func__, __LINE__); - memset(&tcpClientConnection->hints, 0, sizeof(tcpClientConnection->hints)); tcpClientConnection->hints.ai_family = AF_UNSPEC; tcpClientConnection->hints.ai_socktype = SOCK_STREAM; char portStr[6]; UA_snprintf(portStr, 6, "%d", port); -// ua_print("ua: [%s] host %s port %s fam %d\n", __func__, hostname, portStr, -// tcpClientConnection->server->ai_addr->sa_family); - #if LWIP_DNS int error = UA_getaddrinfo(hostname, portStr, &tcpClientConnection->hints, &tcpClientConnection->server); if(error != 0 || !tcpClientConnection->server) { - ua_print("ua: [%s] host %s error %d\n", __func__, hostname, error); UA_LOG_SOCKET_ERRNO_GAI_WRAP(UA_LOG_WARNING(logger, UA_LOGCATEGORY_NETWORK, "DNS lookup of %s failed with error %d - %s", hostname, error, errno_str)); diff --git a/APP_Framework/Framework/control/plc/interoperability/opcua/ua_api.c b/APP_Framework/Framework/control/plc/interoperability/opcua/ua_api.c index 4512368be..7115ca1c3 100755 --- a/APP_Framework/Framework/control/plc/interoperability/opcua/ua_api.c +++ b/APP_Framework/Framework/control/plc/interoperability/opcua/ua_api.c @@ -22,7 +22,7 @@ #include #include "ua_api.h" -int ua_open(void *dev) +int UaDevOpen(void *dev) { UaParamType *param = (UaParamType *)dev; @@ -32,7 +32,7 @@ int ua_open(void *dev) if (param->client == NULL) { - ua_print("ua: [%s] tcp client null\n", __func__); + ua_error("ua: [%s] tcp client null\n", __func__); return EEMPTY; } @@ -43,28 +43,29 @@ int ua_open(void *dev) UA_StatusCode retval = UA_Client_connect(param->client, param->ua_remote_ip); if(retval != UA_STATUSCODE_GOOD) { - ua_notice("ua: [%s] deleted ret %x!\n", __func__, retval); + ua_error("ua: [%s] deleted ret %x!\n", __func__, retval); return (int)retval; } return EOK; } -void ua_close(void *dev) +void UaDevClose(void *dev) { UaParamType *param = (UaParamType *)dev; + ua_notice("ua: [%s] close %s!\n", __func__, param->ua_remote_ip); UA_Client_delete(param->client); /* Disconnects the client internally */ } -int ua_read(void *dev, void *buf, size_t len) +int UaDevRead(void *dev, void *buf, size_t len) { UaParamType *param = (UaParamType *)dev; switch(param->act) { case UA_ACT_ATTR: - ua_read_nodeid_value(param->client, param->ua_id, buf); + UaReadNodeValue(param->client, param->ua_id, buf); break; case UA_ACT_OBJ: - ua_test_browser_objects(param->client); + UaTestBrowserObjects(param->client); break; default: break; @@ -72,17 +73,17 @@ int ua_read(void *dev, void *buf, size_t len) return EOK; } -int ua_write(void *dev, const void *buf, size_t len) +int UaDevWrite(void *dev, const void *buf, size_t len) { UaParamType *param = (UaParamType *)dev; switch(param->act) { case UA_ACT_ATTR: - ua_write_nodeid_value(param->client, param->ua_id, (char *)buf); + UaWriteNodeValue(param->client, param->ua_id, (char *)buf); break; case UA_ACT_OBJ: - ua_test_browser_objects(param->client); + UaTestBrowserObjects(param->client); break; default: break; @@ -90,7 +91,7 @@ int ua_write(void *dev, const void *buf, size_t len) return EOK; } -int ua_ioctl(void *dev, int cmd, void *arg) +int UaDevIoctl(void *dev, int cmd, void *arg) { return EOK; } diff --git a/APP_Framework/Framework/control/plc/interoperability/opcua/ua_api.h b/APP_Framework/Framework/control/plc/interoperability/opcua/ua_api.h index 7da7a4faa..4c006c197 100755 --- a/APP_Framework/Framework/control/plc/interoperability/opcua/ua_api.h +++ b/APP_Framework/Framework/control/plc/interoperability/opcua/ua_api.h @@ -42,32 +42,33 @@ typedef struct UaParam }UaParamType; #define ua_print //KPrintf -#define ua_trace() //KPrintf("ua: [%s] line %d checked!\n", __func__, __LINE__) #define ua_notice KPrintf #define ua_debug //KPrintf #define ua_error KPrintf +#define ua_debug1 extern const char *opc_server_url; extern char test_ua_ip[]; -int ua_server_connect(void); -void ua_browser_nodes(UA_Client *client); -void ua_browser_id(UA_Client *client, UA_NodeId id); -void ua_read_time(UA_Client *client); -void ua_add_nodes(UA_Client *client); +int UaServerRun(void); +void UaBrowserNodes(UA_Client *client); +void UaBrowserNodeId(UA_Client *client, UA_NodeId id); +void UaGetServerTime(UA_Client *client); +void UaAddNodes(UA_Client *client); -int ua_open(void *dev); // open and connect PLC device -void ua_close(void* dev); // close and disconnect PLC device -int ua_read(void* dev, void *buf, size_t len); // read data from PLC -int ua_write(void* dev, const void *buf, size_t len); // write data from PLC -int ua_ioctl(void* dev, int cmd, void *arg); // send control command to PLC +int UaDevOpen(void *dev); // open and connect PLC device +void UaDevClose(void* dev); // close and disconnect PLC device +int UaDevRead(void* dev, void *buf, size_t len); // read data from PLC +int UaDevWrite(void* dev, const void *buf, size_t len); // write data from PLC +int UaDevIoctl(void* dev, int cmd, void *arg); // send control command to PLC -char *ua_get_nodeid_str(UA_NodeId *node_id); -void ua_read_nodeid_value(UA_Client *client, UA_NodeId id, UA_Int32 *value); -void ua_write_nodeid_value(UA_Client *client, UA_NodeId id, char* value); -void ua_test_attr(UA_Client *client); -UA_StatusCode ua_read_array_value(UA_Client *client, int array_size, UA_ReadValueId *array); -void ua_test_browser_objects(UA_Client *client); -int ua_test_interact_server(UA_Client *client); +char *UaGetNodeIdString(UA_NodeId *node_id); +void UaReadNodeValue(UA_Client *client, UA_NodeId id, UA_Int32 *value); +void UaWriteNodeValue(UA_Client *client, UA_NodeId id, char* value); +UA_StatusCode UaReadArrayValue(UA_Client *client, int array_size, UA_ReadValueId *array); +int UaGetNodeIdArray(UA_Client* client, UA_NodeId id, int array_size, int *id_array); + +void UaTestBrowserObjects(UA_Client *client); +int UaTestInteractServer(UA_Client *client); #endif diff --git a/APP_Framework/Framework/control/plc/interoperability/opcua/ua_client.c b/APP_Framework/Framework/control/plc/interoperability/opcua/ua_client.c index 749025f75..cd4ec281a 100755 --- a/APP_Framework/Framework/control/plc/interoperability/opcua/ua_client.c +++ b/APP_Framework/Framework/control/plc/interoperability/opcua/ua_client.c @@ -27,14 +27,14 @@ const char *opc_server_url = {"opc.tcp://192.168.250.2:4840"}; #ifdef UA_ENABLE_SUBSCRIPTIONS -static void handler_TheAnswerChanged(UA_Client* client, UA_UInt32 subId, void* subContext, +static void UaAnswerChangedHandler(UA_Client* client, UA_UInt32 subId, void* subContext, UA_UInt32 monId, void* monContext, UA_DataValue* value) { - ua_print("The Answer has changed!\n"); + ua_notice("Answer changed!\n"); } #endif -static UA_StatusCode nodeIter(UA_NodeId childId, UA_Boolean isInverse, UA_NodeId referenceTypeId, void* handle) +static UA_StatusCode UaShowNodeIdIterate(UA_NodeId childId, UA_Boolean isInverse, UA_NodeId referenceTypeId, void* handle) { if(isInverse) { @@ -49,7 +49,7 @@ static UA_StatusCode nodeIter(UA_NodeId childId, UA_Boolean isInverse, UA_NodeId return UA_STATUSCODE_GOOD; } -int ua_get_points(UA_Client* client) +int UaGetEndPoints(UA_Client* client) { /* Listing endpoints */ UA_EndpointDescription* endpointArray = NULL; @@ -76,7 +76,7 @@ int ua_get_points(UA_Client* client) return EXIT_SUCCESS; } -void ua_print_value(UA_Variant* val) +static void UaShowNodeValue(UA_Variant* val) { if(val->type == &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]) { @@ -103,6 +103,16 @@ void ua_print_value(UA_Variant* val) UA_Int16* ptr = (UA_Int16*)val->data; ua_notice("%d (Int16)\n", *ptr); } + else if(val->type == &UA_TYPES[UA_TYPES_FLOAT]) + { + UA_Float* ptr = (UA_Float*)val->data; + ua_notice("%d (Float)\n", *ptr); + } + else if(val->type == &UA_TYPES[UA_TYPES_DOUBLE]) + { + UA_Double* ptr = (UA_Double*)val->data; + ua_notice("%d (Double)\n", *ptr); + } else if(val->type == &UA_TYPES[UA_TYPES_STRING]) { UA_String* ptr = (UA_String*)val->data; @@ -117,7 +127,7 @@ void ua_print_value(UA_Variant* val) } } -char *ua_get_nodeid_str(UA_NodeId *node_id) +char *UaGetNodeIdString(UA_NodeId *node_id) { static char nodeid_str[UA_NODE_LEN] = {0}; @@ -139,7 +149,7 @@ char *ua_get_nodeid_str(UA_NodeId *node_id) return nodeid_str; } -void ua_print_nodeid(UA_NodeId *node_id) +static void UaShowNodeId(UA_NodeId *node_id) { switch(node_id->identifierType) { @@ -158,7 +168,7 @@ void ua_print_nodeid(UA_NodeId *node_id) } } -void ua_print_object(UA_BrowseResponse* res) +static void UaShowObject(UA_BrowseResponse* res) { ua_notice("%-9s %-16s %-16s %-16s\n", "NAMESPACE", "NODEID", "BROWSE NAME", "DISPLAY NAME"); @@ -191,7 +201,7 @@ void ua_print_object(UA_BrowseResponse* res) ua_notice("\n"); } -UA_StatusCode ua_read_array_value(UA_Client* client, int array_size, UA_ReadValueId* array) +UA_StatusCode UaReadArrayValue(UA_Client* client, int array_size, UA_ReadValueId* array) { UA_ReadRequest request; UA_ReadRequest_init(&request); @@ -209,14 +219,19 @@ UA_StatusCode ua_read_array_value(UA_Client* client, int array_size, UA_ReadValu } UA_StatusCode* arr_ret = malloc(array_size * sizeof(UA_StatusCode)); + if(arr_ret == NULL) + { + ua_error("ua: [%s] malloc %d failed!\n", __func__, array_size * sizeof(UA_StatusCode)); + return UA_STATUSCODE_BADOUTOFMEMORY; + } for(int i = 0; i < array_size; ++i) { if((response.results[i].status == UA_STATUSCODE_GOOD) && (response.results[i].hasValue)) { - ua_notice("node %s: ", ua_get_nodeid_str(&array[i].nodeId)); - ua_print_value(&response.results[i].value); + ua_notice("node %s: ", UaGetNodeIdString(&array[i].nodeId)); + UaShowNodeValue(&response.results[i].value); } } ua_notice("\n"); @@ -226,32 +241,82 @@ UA_StatusCode ua_read_array_value(UA_Client* client, int array_size, UA_ReadValu return UA_STATUSCODE_GOOD; } -void ua_browser_id(UA_Client* client, UA_NodeId id) +void UaBrowserNodeId(UA_Client* client, UA_NodeId id) { + UA_BrowseRequest ua_req; + UA_BrowseResponse ua_resp; + /* Browse some objects */ ua_notice("Browsing nodes in objects folder:\n"); - UA_BrowseRequest bReq; - UA_BrowseRequest_init(&bReq); - bReq.requestedMaxReferencesPerNode = 0; - bReq.nodesToBrowse = UA_BrowseDescription_new(); - bReq.nodesToBrowseSize = 1; - bReq.nodesToBrowse[0].nodeId = id; /* browse objects folder */ - bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; /* return everything */ - UA_BrowseResponse res = UA_Client_Service_browse(client, bReq); - ua_print_object(&res); - UA_BrowseResponse_clear(&res); -// UA_BrowseRequest_clear(&bReq); + + UA_BrowseRequest_init(&ua_req); + + ua_req.requestedMaxReferencesPerNode = 0; + ua_req.nodesToBrowse = UA_BrowseDescription_new(); + ua_req.nodesToBrowseSize = 1; + ua_req.nodesToBrowse[0].nodeId = id; /* browse objects folder */ + ua_req.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; /* return everything */ + + ua_resp = UA_Client_Service_browse(client, ua_req); + + UaShowObject(&ua_resp); + + UA_BrowseResponse_clear(&ua_resp); } -void ua_browser_nodes(UA_Client* client) + +int UaGetNodeIdArray(UA_Client* client, UA_NodeId id, int array_size, int *id_array) +{ + int i, j; + int array_cnt = 0;// return array number + UA_BrowseRequest ua_req; + UA_BrowseResponse ua_resp; + + /* Browse some objects */ + ua_notice("Browsing nodes in objects folder:\n"); + + UA_BrowseRequest_init(&ua_req); + + ua_req.requestedMaxReferencesPerNode = 0; + ua_req.nodesToBrowse = UA_BrowseDescription_new(); + ua_req.nodesToBrowseSize = 1; + ua_req.nodesToBrowse[0].nodeId = id; /* browse objects folder */ + ua_req.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; /* return everything */ + + ua_resp = UA_Client_Service_browse(client, ua_req); + + for(i = 0; i < ua_resp.resultsSize; ++i) + { + for(j = 0; j < ua_resp.results[i].referencesSize; ++j) + { + UA_ReferenceDescription* ref = &(ua_resp.results[i].references[j]); + + if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) + { + *(id_array + array_cnt) = ref->nodeId.nodeId.identifier.numeric; + array_cnt ++; + if(array_cnt >= array_size) + { + UA_BrowseResponse_clear(&ua_resp); + return array_cnt; + } + } + } + } + + UA_BrowseResponse_clear(&ua_resp); + return array_cnt; +} + +void UaBrowserNodes(UA_Client* client) { UA_NodeId* parent = UA_NodeId_new(); *parent = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); - UA_Client_forEachChildNodeCall(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), nodeIter, (void*) parent); + UA_Client_forEachChildNodeCall(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UaShowNodeIdIterate, (void*) parent); UA_NodeId_delete(parent); } -UA_UInt32 ua_start_sub(UA_Client* client, UA_NodeId node_id) +UA_UInt32 UaStartSubscription(UA_Client* client, UA_NodeId node_id) { /* Create a subscription */ UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default(); @@ -274,7 +339,7 @@ UA_UInt32 ua_start_sub(UA_Client* client, UA_NodeId node_id) UA_MonitoredItemCreateResult monResponse = UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId, UA_TIMESTAMPSTORETURN_BOTH, - monRequest, NULL, handler_TheAnswerChanged, NULL); + monRequest, NULL, UaAnswerChangedHandler, NULL); if(monResponse.statusCode == UA_STATUSCODE_GOOD) { @@ -290,7 +355,7 @@ UA_UInt32 ua_start_sub(UA_Client* client, UA_NodeId node_id) return subId; } -void ua_write_nodeid_value(UA_Client* client, UA_NodeId id, char* value) +void UaWriteNodeValue(UA_Client* client, UA_NodeId id, char* value) { UA_Boolean bool_val; uint32_t integer_val; @@ -332,24 +397,17 @@ void ua_write_nodeid_value(UA_Client* client, UA_NodeId id, char* value) UA_WriteRequest_clear(&wReq); UA_WriteResponse_clear(&wResp); - -// /* Write node attribute (using the highlevel API) */ -// value++; -// UA_Variant *myVariant = UA_Variant_new(); -// UA_Variant_setScalarCopy(myVariant, &value, &UA_TYPES[UA_TYPES_INT32]); -// UA_Client_writeValueAttribute(client, UA_NODEID_STRING(1, UA_NODE_STR), myVariant); -// UA_Variant_delete(myVariant); } /* Read attribute */ -void ua_read_nodeid_value(UA_Client* client, UA_NodeId id, UA_Int32 *value) +void UaReadNodeValue(UA_Client* client, UA_NodeId id, UA_Int32 *value) { UA_Variant* val = UA_Variant_new(); UA_StatusCode ret = UA_Client_readValueAttribute(client, id, val); if(ret == UA_STATUSCODE_GOOD) { - ua_print_value(val); + UaShowNodeValue(val); if(UA_Variant_isScalar(val)) { if(val->type == &UA_TYPES[UA_TYPES_BOOLEAN]) @@ -370,7 +428,7 @@ void ua_read_nodeid_value(UA_Client* client, UA_NodeId id, UA_Int32 *value) UA_Variant_delete(val); } -void ua_call_remote(UA_Client* client) +void UaCallRemote(UA_Client* client) { /* Call a remote method */ UA_Variant input; @@ -397,7 +455,7 @@ void ua_call_remote(UA_Client* client) } -void ua_add_nodes(UA_Client* client) +void UaAddNodes(UA_Client* client) { /* Add new nodes*/ /* New ReferenceType */ @@ -477,7 +535,7 @@ void ua_add_nodes(UA_Client* client) } } -void ua_read_time(UA_Client* client) +void UaGetServerTime(UA_Client* client) { UA_Variant value; UA_Variant_init(&value); diff --git a/APP_Framework/Framework/control/plc/interoperability/opcua/ua_server.c b/APP_Framework/Framework/control/plc/interoperability/opcua/ua_server.c index b3d24eba7..548a357ce 100755 --- a/APP_Framework/Framework/control/plc/interoperability/opcua/ua_server.c +++ b/APP_Framework/Framework/control/plc/interoperability/opcua/ua_server.c @@ -10,29 +10,29 @@ * See the Mulan PSL v2 for more details. */ +/** + * @file ua_server.c + * @brief Server for OpcUa function + * @version 1.0 + * @author AIIT XUOS Lab + * @date 2021.11.11 + */ #include "open62541.h" -#include -#include +UA_Boolean ua_server_flag = true; -UA_Boolean running = true; - -static void stopHandler(int sign) { - UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c"); - running = false; -} - -int ua_server_connect(void) +int UaRunServer(void) { - signal(SIGINT, stopHandler); - signal(SIGTERM, stopHandler); + UA_StatusCode ret; UA_Server *server = UA_Server_new(); + UA_ServerConfig_setDefault(UA_Server_getConfig(server)); - UA_StatusCode retval = UA_Server_run(server, &running); + + ret = UA_Server_run(server, &ua_server_flag); UA_Server_delete(server); - return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE; + return ret; } diff --git a/APP_Framework/Framework/control/plc/interoperability/opcua/ua_test.c b/APP_Framework/Framework/control/plc/interoperability/opcua/ua_test.c index d11b86896..1b9ed8427 100755 --- a/APP_Framework/Framework/control/plc/interoperability/opcua/ua_test.c +++ b/APP_Framework/Framework/control/plc/interoperability/opcua/ua_test.c @@ -27,72 +27,60 @@ #define UA_TEST_BROWSER_NODEID1 UA_NODEID_NUMERIC(4, 1) #define UA_TEST_WRITE_NODEID UA_NODEID_NUMERIC(4, 5) +#define UA_TEST_NODE_ARRAY_NUM 10 -static UA_StatusCode ua_test_read_array(UA_Client *client) +static UA_StatusCode UaTestReadArrayValue(UA_Client *client, UA_NodeId id) { - const int item_size = 4; - UA_ReadValueId test_item[item_size]; + int i; + int array_size = 0; + int test_id[UA_TEST_NODE_ARRAY_NUM]; + UA_ReadValueId test_array[UA_TEST_NODE_ARRAY_NUM]; - for (int i = 0; i < item_size; i++) + for(i = 0; i < UA_TEST_NODE_ARRAY_NUM; i++) { - UA_ReadValueId_init(&test_item[i]); - test_item[i].attributeId = UA_ATTRIBUTEID_VALUE; + UA_ReadValueId_init(&test_array[i]); + test_array[i].attributeId = UA_ATTRIBUTEID_VALUE; } - test_item[0].nodeId = UA_NODEID_NUMERIC(4, 2); - test_item[1].nodeId = UA_NODEID_NUMERIC(4, 3); - test_item[2].nodeId = UA_NODEID_NUMERIC(4, 4); - test_item[3].nodeId = UA_NODEID_NUMERIC(4, 5); + array_size = UaGetNodeIdArray(client, id, UA_TEST_NODE_ARRAY_NUM, test_id); - return ua_read_array_value(client, item_size, test_item); + for(i = 0; i < array_size; i++) + { + test_array[i].nodeId = UA_NODEID_NUMERIC(id.namespaceIndex, test_id[i]); + } + + return UaReadArrayValue(client, array_size, test_array); } -void ua_test_browser_objects(UA_Client *client) +void UaTestBrowserObjects(UA_Client *client) { UA_NodeId test_id; - ua_browser_id(client, UA_TEST_BROWSER_NODEID); - ua_browser_id(client, UA_TEST_BROWSER_NODEID1); + UaBrowserNodeId(client, UA_TEST_BROWSER_NODEID); + UaBrowserNodeId(client, UA_TEST_BROWSER_NODEID1); test_id = UA_TEST_BROWSER_NODEID1; - ua_notice("Show values in %s:\n", ua_get_nodeid_str(&test_id)); - ua_test_read_array(client); + ua_notice("Show values in %s:\n", UaGetNodeIdString(&test_id)); + UaTestReadArrayValue(client, test_id); return; } -void ua_test_write_attr(UA_Client *client) +static void UaTestWriteNodeValue(UA_Client *client) { UA_Int32 value; char val_str[UA_NODE_LEN]; UA_NodeId id = UA_TEST_WRITE_NODEID; - ua_notice("--- Test write %s ---\n", ua_get_nodeid_str(&id)); - ua_read_nodeid_value(client, id, &value); - ua_write_nodeid_value(client, id, itoa(value + 1, val_str, 10)); - ua_read_nodeid_value(client, id, &value); + ua_notice("--- Test write %s ---\n", UaGetNodeIdString(&id)); + UaReadNodeValue(client, id, &value); + UaWriteNodeValue(client, id, itoa(value + 1, val_str, 10)); + UaReadNodeValue(client, id, &value); ua_notice("\n"); } -int ua_test_interact_server(UA_Client *client) +int UaTestInteractServer(UA_Client *client) { - ua_read_time(client); - ua_test_browser_objects(client); - ua_test_write_attr(client); - return EXIT_SUCCESS; -} - -int16 ua_test(void) -{ - UA_Client *client = UA_Client_new(); - UA_StatusCode retval = UA_Client_connect(client, opc_server_url); - if(retval != UA_STATUSCODE_GOOD) { - UA_Client_delete(client); - return (int)retval; - } - - ua_read_time(client); - - /* Clean up */ - UA_Client_disconnect(client); - UA_Client_delete(client); /* Disconnects the client internally */ + UaGetServerTime(client); + UaTestBrowserObjects(client); + UaTestWriteNodeValue(client); return EXIT_SUCCESS; } diff --git a/APP_Framework/Framework/control/plc/shared/plc_channel.c b/APP_Framework/Framework/control/plc/shared/plc_channel.c index 3fe084514..7959a42e1 100755 --- a/APP_Framework/Framework/control/plc/shared/plc_channel.c +++ b/APP_Framework/Framework/control/plc/shared/plc_channel.c @@ -322,7 +322,7 @@ ChDevType ChannelFindDevice(struct Channel *ch, const char *device_name) * @param dev_recv_callback - callback function * @return successful:EOK,failed:ERROR */ -uint32 ChannelDevRecvCallback(struct ChDev *dev, int (*dev_recv_callback) (void *dev, x_size_t length)) +uint32 ChannelDevRecvCallback(struct ChDev *dev, int (*dev_recv_callback) (void *dev, size_t length)) { CHECK_CH_PARAM(dev ); diff --git a/APP_Framework/Framework/control/plc/shared/plc_channel.h b/APP_Framework/Framework/control/plc/shared/plc_channel.h index a00262bf1..9d0075687 100755 --- a/APP_Framework/Framework/control/plc/shared/plc_channel.h +++ b/APP_Framework/Framework/control/plc/shared/plc_channel.h @@ -12,7 +12,7 @@ /** * @file plc_channel.h -* @brief define ch driver framework function and common API +* @brief define channel driver framework function and common API * @version 1.0 * @author AIIT XUOS Lab * @date 2022-03-01 @@ -23,9 +23,6 @@ #include "list.h" -#define x_OffPos uint32 -#define x_size_t size_t - #ifdef __cplusplus extern "C" { #endif @@ -97,27 +94,19 @@ struct ChConfigInfo struct ChReadParam { - x_OffPos pos; + uint32 pos; void* buffer; - x_size_t size; - x_size_t read_length; + size_t size; + size_t read_length; }; struct ChWriteParam { - x_OffPos pos; + uint32 pos; const void* buffer; - x_size_t size; + size_t size; }; -//struct ChHalDevBlockParam -//{ -// uint32 cmd; -////tst by wly -//// struct DeviceBlockArrange dev_block; -//// struct DeviceBlockAddr *dev_addr; -//}; - struct ChHalDevDone { uint32 (*open) (void *dev); @@ -134,8 +123,7 @@ struct ChDev const struct ChHalDevDone *dev_done; - int (*dev_recv_callback) (void *dev, x_size_t length); -// int (*dev_block_control) (struct ChDev *dev, struct ChHalDevBlockParam *block_param); + int (*dev_recv_callback) (void *dev, size_t length); struct Channel *owner_ch; void *private_data; @@ -220,7 +208,7 @@ ChDrvType ChannelFindDriver(struct Channel *ch, const char *driver_name); ChDevType ChannelFindDevice(struct Channel *ch, const char *device_name); /*Dev receive data callback function*/ -uint32 ChannelDevRecvCallback(struct ChDev *dev, int (*dev_recv_callback) (void *dev, x_size_t length)); +uint32 ChannelDevRecvCallback(struct ChDev *dev, int (*dev_recv_callback) (void *dev, size_t length)); /*Open the device of the channel*/ uint32 ChannelDevOpen(struct ChDev *dev); diff --git a/APP_Framework/Framework/control/plc/shared/plc_device.c b/APP_Framework/Framework/control/plc/shared/plc_device.c index 670452e2a..8860aea37 100755 --- a/APP_Framework/Framework/control/plc/shared/plc_device.c +++ b/APP_Framework/Framework/control/plc/shared/plc_device.c @@ -40,7 +40,7 @@ static int PlcDeviceOpen(void *dev) if(plc_dev->net == PLC_IND_ENET_OPCUA) { - return ua_open(plc_dev->priv_data); + return UaDevOpen(plc_dev->priv_data); } return EOK; @@ -54,7 +54,7 @@ static void PlcDeviceClose(void *dev) if(plc_dev->net == PLC_IND_ENET_OPCUA) { - ua_close(plc_dev->priv_data); + UaDevClose(plc_dev->priv_data); } } @@ -67,7 +67,7 @@ static int PlcDeviceWrite(void *dev, const void *buf, size_t len) if(plc_dev->net == PLC_IND_ENET_OPCUA) { - ret = ua_write(plc_dev->priv_data, buf, len); + ret = UaDevWrite(plc_dev->priv_data, buf, len); } return ret; @@ -83,7 +83,7 @@ static int PlcDeviceRead(void *dev, void *buf, size_t len) if(plc_dev->net == PLC_IND_ENET_OPCUA) { - ret = ua_read(plc_dev->priv_data, buf, len); + ret = UaDevRead(plc_dev->priv_data, buf, len); } return ret; diff --git a/Ubiquitous/XiZi/resources/ethernet/LwIP/arch/sys_arch.c b/Ubiquitous/XiZi/resources/ethernet/LwIP/arch/sys_arch.c index 5088c7f37..d9fe5b640 100644 --- a/Ubiquitous/XiZi/resources/ethernet/LwIP/arch/sys_arch.c +++ b/Ubiquitous/XiZi/resources/ethernet/LwIP/arch/sys_arch.c @@ -446,6 +446,7 @@ void lwip_input_thread(void *param) /* Poll the driver, get any outstanding frames */ ethernetif_input(net); sys_check_timeouts(); /* Handle all system timeouts for all core protocols */ + KPrintf(""); } } }