Browse Source

Also in terms of the symmetric API, the UA_Client_writeAttribute<X> function set appears to have vanished. UA_Client_writeAttributeValue was restored in accordance with @jpfr's new implementation.

ichrispa 9 years ago
parent
commit
d354cf723f
3 changed files with 52 additions and 4 deletions
  1. 8 1
      examples/client.c
  2. 10 2
      include/ua_client_highlevel.h
  3. 34 1
      src/client/ua_client_highlevel.c

+ 8 - 1
examples/client.c

@@ -96,7 +96,7 @@ int main(int argc, char *argv[]) {
     UA_BrowseRequest_deleteMembers(&bReq);
     UA_BrowseResponse_deleteMembers(&bResp);
     
-    // Same thing, less code:
+    // Same thing, this time using the node iterator...
     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);
@@ -166,6 +166,13 @@ int main(int argc, char *argv[]) {
     UA_WriteRequest_deleteMembers(&wReq);
     UA_WriteResponse_deleteMembers(&wResp);
 
+    // Alternate Form, this time using the hl 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, "the.answer"), myVariant);
+    UA_Variant_delete(myVariant);
+    
 #ifdef UA_ENABLE_SUBSCRIPTIONS
     // Take another look at the.answer... this should call the handler.
     UA_Client_Subscriptions_manuallySendPublishRequest(client);

+ 10 - 2
include/ua_client_highlevel.h

@@ -137,7 +137,11 @@ UA_Client_addMethodNode(UA_Client *client, const UA_NodeId requestedNewNodeId,
 UA_StatusCode UA_EXPORT
 __UA_Client_readAttribute(UA_Client *client, UA_NodeId nodeId, UA_AttributeId attributeId,
                           void *out, const UA_DataType *outDataType);
-  
+
+UA_StatusCode UA_EXPORT
+__UA_Client_writeAttribute(UA_Client *client, UA_NodeId nodeId, UA_AttributeId attributeId,
+                           void *in, const UA_DataType *inDataType);
+
 static UA_INLINE UA_StatusCode
 UA_Client_readNodeIdAttribute(UA_Client *client, UA_NodeId nodeId, UA_NodeId *outNodeId) {
     return __UA_Client_readAttribute(client, nodeId, UA_ATTRIBUTEID_NODEID,
@@ -202,7 +206,11 @@ static UA_INLINE UA_StatusCode
 UA_Client_readValueAttribute(UA_Client *client, UA_NodeId nodeId, UA_Variant *outValue) {
     return __UA_Client_readAttribute(client, nodeId, UA_ATTRIBUTEID_VALUE,
                                      outValue, &UA_TYPES[UA_TYPES_VARIANT]); }
-
+static UA_INLINE UA_StatusCode
+UA_Client_writeValueAttribute(UA_Client *client, UA_NodeId nodeId, UA_Variant *inValue) {
+    return __UA_Client_writeAttribute(client, nodeId, UA_ATTRIBUTEID_VALUE,
+                                     inValue, &UA_TYPES[UA_TYPES_VARIANT]); }
+                                     
 static UA_INLINE UA_StatusCode
 UA_Client_readDataTypeAttribute(UA_Client *client, UA_NodeId nodeId, UA_NodeId *outDataType) {
     return __UA_Client_readAttribute(client, nodeId, UA_ATTRIBUTEID_DATATYPE,

+ 34 - 1
src/client/ua_client_highlevel.c

@@ -3,6 +3,7 @@
 #include "ua_client_highlevel.h"
 #include "ua_types_encoding_binary.h"
 #include "ua_util.h"
+#include "ua_types.h"
 
 UA_StatusCode
 UA_Client_NamespaceGetIndex(UA_Client *client, UA_String *namespaceUri, UA_UInt16 *namespaceIndex) {
@@ -248,6 +249,38 @@ UA_Client_call(UA_Client *client, const UA_NodeId objectId, const UA_NodeId meth
 /* Attributes */
 /**************/
 
+UA_StatusCode 
+__UA_Client_writeAttribute(UA_Client *client, UA_NodeId nodeId, UA_AttributeId attributeId,
+                           void *in, const UA_DataType *inDataType) {
+    if(in == NULL)
+      return UA_STATUSCODE_BADTYPEMISMATCH;
+    
+    UA_Variant *tmp = (UA_Variant *) in;
+    if (tmp == NULL) return 1;
+    
+    UA_WriteRequest *wReq = UA_WriteRequest_new();
+    wReq->nodesToWrite = UA_WriteValue_new();
+    wReq->nodesToWriteSize = 1;
+    UA_NodeId_copy(&nodeId, &wReq->nodesToWrite[0].nodeId);
+    wReq->nodesToWrite[0].attributeId = attributeId;
+    if (attributeId == UA_ATTRIBUTEID_VALUE) {
+      UA_Variant_copy((UA_Variant *) in, &wReq->nodesToWrite[0].value.value);
+      wReq->nodesToWrite[0].value.hasValue = true;
+    }
+    else {
+      if( ! UA_Variant_setScalarCopy(&wReq->nodesToWrite[0].value.value, in, inDataType) )
+        wReq->nodesToWrite[0].value.hasValue = true;
+    }
+    
+    UA_WriteResponse wResp = UA_Client_Service_write(client, *wReq);
+    UA_StatusCode retval = wResp.responseHeader.serviceResult;
+    
+    UA_WriteRequest_delete(wReq);
+    UA_WriteResponse_deleteMembers(&wResp);
+    
+    return retval;
+}
+
 UA_StatusCode 
 __UA_Client_readAttribute(UA_Client *client, UA_NodeId nodeId, UA_AttributeId attributeId,
                           void *out, const UA_DataType *outDataType) {
@@ -291,4 +324,4 @@ __UA_Client_readAttribute(UA_Client *client, UA_NodeId nodeId, UA_AttributeId at
 
     UA_ReadResponse_deleteMembers(&response);
     return retval;
-}
+}