Open62541のチュートリアルを動かしてみる

# Open62541 Tutorial
## 4.3 Valiablesの追加

### main処理とツリーの表示

```c

105 int main( void )
106 {
107   signal( SIGINT, stopHandler );
108   signal( SIGTERM, stopHandler );
109
110   UA_Server *server = UA_Server_new();
111   UA_ServerConfig_setDefault( UA_Server_getConfig( server ) );
112   UA_ServerConfig* config = UA_Server_getConfig( server );
113   config->verifyRequestTimestamp = UA_RULEHANDLING_ACCEPT;
114
115   addVariable( server );
116   addMatrixVariable( server );
117
118 //  writeVariable( server );
119 //  writeWrongVariable( server );
120
121   UA_StatusCode retval = UA_Server_run( server, &running );
122
123
124   UA_Server_delete( server );
125
126   return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
127 }
```

![t431png.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/28184/ac685d06-ec10-651c-5439-b889a25082b7.png)

### AddVariable関数

```c

  8 static void addVariable( UA_Server *server )
  9 {
 10   /* Define the attribute of the myInteger variable node */
 11   UA_VariableAttributes attr = UA_VariableAttributes_default;
 12   UA_Int32 myInteger = 42;
 13   UA_Variant_setScalar( &attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32] );
 14   attr.description = UA_LOCALIZEDTEXT( "en-US", "the answer" );
 15   attr.displayName = UA_LOCALIZEDTEXT( "en-US", "the answer" );
 16   attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
 17   attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
 18
 19   /* Add the variable node to the information model */
 20   UA_NodeId myIntegerNodeId = UA_NODEID_STRING( 1, "the.answer" );
 21   UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME( 1, "the answer" );
 22   UA_NodeId parentNodeId = UA_NODEID_NUMERIC( 0, UA_NS0ID_OBJECTSFOLDER );
 23   UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC( 0, UA_NS0ID_ORGANIZES );
 24   UA_Server_addVariableNode( server, myIntegerNodeId, parentNodeId, parentReferenceNodeId, myIntegerName, UA_NODEI    D_NUMERIC( 0, UA_NS0ID_BASEDATAVARIABLETYPE ), attr, NULL, NULL );
 25 }
```

![t433.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/28184/cf9dee72-280c-b662-27ec-7a8bc33243b0.png)

### addMatrixVariable

```c

 27 static void addMatrixVariable( UA_Server *server )
 28 {
 29   UA_VariableAttributes attr = UA_VariableAttributes_default;
 30   attr.displayName = UA_LOCALIZEDTEXT( "en-US", "Double Matrix" );
 31   attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
 32
 33   /* Set the variable value constraints */
 34   attr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
 35   attr.valueRank = UA_VALUERANK_TWO_DIMENSIONS;
 36   UA_UInt32 arrayDims[2] = { 2, 2 };
 37   attr.arrayDimensions = arrayDims;
 38   attr.arrayDimensionsSize = 2;
 39
 40   /* Set the value. The array dimensions need to be the same for the value. */
 41   UA_Double zero[4] = { 0.0, 0.0, 0.0, 0.0 };
 42   UA_Variant_setArray( &attr.value, zero, 4, &UA_TYPES[UA_TYPES_DOUBLE] );
 43   attr.value.arrayDimensions = arrayDims;
 44   attr.value.arrayDimensionsSize = 2;
 45
 46   UA_NodeId myIntegerNodeId = UA_NODEID_STRING( 1, "double.matrix" );
 47   UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME( 1, "doouble matrix" );
 48   UA_NodeId parentNodeId = UA_NODEID_NUMERIC( 0, UA_NS0ID_OBJECTSFOLDER );
 49   UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC( 0, UA_NS0ID_ORGANIZES );
 50   UA_Server_addVariableNode( server, myIntegerNodeId, parentNodeId, parentReferenceNodeId, myIntegerName, UA_NODEI    D_NUMERIC( 0, UA_NS0ID_BASEDATAVARIABLETYPE ), attr, NULL, NULL );
 51 }
```

![t432.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/28184/8ca5d075-fcdb-576e-f59c-42fa00041a3e.png)