Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
osie
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
osie
Commits
e59409b5
Commit
e59409b5
authored
Oct 04, 2021
by
Ivan Tyagov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Expose variables over OPC-UA.
parent
57c14e10
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
193 additions
and
62 deletions
+193
-62
coupler/opc-ua-server/ic2_set_relays_example.c
coupler/opc-ua-server/ic2_set_relays_example.c
+0
-56
coupler/opc-ua-server/server.c
coupler/opc-ua-server/server.c
+193
-6
No files found.
coupler/opc-ua-server/ic2_set_relays_example.c
deleted
100644 → 0
View file @
57c14e10
/*
Example controling MOD-IO's relays' state
Based on https://www.kernel.org/doc/Documentation/i2c/dev-interface
To compile:
root@olimex-rt:~/osie/coupler/opc-ua-server# gcc ic2_set_relays_example.c -o ic2_set_relays_example
root@olimex-rt:~/osie/coupler/opc-ua-server# ./ic2_set_relays_example
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
int
main
()
{
int
file
;
int
adapter_nr
=
1
;
/* probably dynamically determined */
int
addr
=
0x58
;
/* The I2C slave's address */
char
filename
[
20
];
// step 1: open device
snprintf
(
filename
,
19
,
"/dev/i2c-%d"
,
adapter_nr
);
file
=
open
(
filename
,
O_RDWR
);
if
(
file
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf
(
"Error openind i2c device."
);
exit
(
1
);
}
// step 2: address the slave by its address
if
(
ioctl
(
file
,
I2C_SLAVE
,
addr
)
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf
(
"Error addressing i2c slave."
);
exit
(
1
);
}
// step 3: write command over I2c
__u8
reg
=
0x10
;
/* Device register to access */
__s32
res
;
char
buf
[
10
];
buf
[
0
]
=
reg
;
buf
[
1
]
=
0x07
;
// the most right bits represent 4 relays' state: for example 00001111 - means all ON
// send as a hexadecimal number
buf
[
2
]
=
0x65
;
// seems irrelevant the value
if
(
write
(
file
,
buf
,
3
)
!=
3
)
{
/* ERROR HANDLING: i2c transaction failed */
printf
(
"Error writing to i2c slave."
);
}
printf
(
"OK
\n
"
);
return
0
;
}
coupler/opc-ua-server/server.c
View file @
e59409b5
/*
* POC OPC-UA server based on https://github.com/open62541
/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
* See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
/**
* Adding Variables to a Server
* ----------------------------
*
* This tutorial shows how to work with data types and how to add variable nodes
* to a server. First, we add a new variable to the server. Take a look at the
* definition of the ``UA_VariableAttributes`` structure to see the list of all
* attributes defined for VariableNodes.
*
* Note that the default settings have the AccessLevel of the variable value as
* read only. See below for making the variable writable.
*/
// Example controling MOD-IO's relays' state
// Based on https://www.kernel.org/doc/Documentation/i2c/dev-interface
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <signal.h>
#include <open62541/plugin/log_stdout.h>
#include <open62541/server.h>
#include <open62541/server_config_default.h>
#include <signal.h>
#include <stdlib.h>
// relays
static
int
setRelayState
(
int
command
)
{
// Set relays' state over I2C
int
file
;
int
adapter_nr
=
1
;
/* probably dynamically determined */
char
filename
[
20
];
// step 1: open device
snprintf
(
filename
,
19
,
"/dev/i2c-%d"
,
adapter_nr
);
file
=
open
(
filename
,
O_RDWR
);
if
(
file
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf
(
"Error openind i2c device."
);
exit
(
1
);
}
// step 2: address the slave by its address
int
addr
=
0x58
;
/* The I2C address */
if
(
ioctl
(
file
,
I2C_SLAVE
,
addr
)
<
0
)
{
/* ERROR HANDLING; you can check errno to see what went wrong */
printf
(
"Error addressing i2c slave."
);
exit
(
1
);
}
// step 3: write command over I2c
__u8
reg
=
0x10
;
/* Device register to access */
__s32
res
;
char
buf
[
10
];
buf
[
0
]
=
reg
;
buf
[
1
]
=
command
;
//0x00 -all off, 0x0F - all 4 on
buf
[
2
]
=
0x65
;
// seems irrelevant the value
if
(
write
(
file
,
buf
,
3
)
!=
3
)
{
/* ERROR HANDLING: i2c transaction failed */
printf
(
"Error writing to i2c slave."
);
}
}
static
void
addVariable
(
UA_Server
*
server
)
{
/* Define the attribute of the myInteger variable node */
UA_VariableAttributes
attr
=
UA_VariableAttributes_default
;
UA_Int32
myInteger
=
42
;
UA_Variant_setScalar
(
&
attr
.
value
,
&
myInteger
,
&
UA_TYPES
[
UA_TYPES_INT32
]);
attr
.
description
=
UA_LOCALIZEDTEXT
(
"en-US"
,
"the answer"
);
attr
.
displayName
=
UA_LOCALIZEDTEXT
(
"en-US"
,
"the answer"
);
attr
.
dataType
=
UA_TYPES
[
UA_TYPES_INT32
].
typeId
;
attr
.
accessLevel
=
UA_ACCESSLEVELMASK_READ
|
UA_ACCESSLEVELMASK_WRITE
;
/* Add the variable node to the information model */
UA_NodeId
myIntegerNodeId
=
UA_NODEID_STRING
(
1
,
"the.answer"
);
UA_QualifiedName
myIntegerName
=
UA_QUALIFIEDNAME
(
1
,
"the answer"
);
UA_NodeId
parentNodeId
=
UA_NODEID_NUMERIC
(
0
,
UA_NS0ID_OBJECTSFOLDER
);
UA_NodeId
parentReferenceNodeId
=
UA_NODEID_NUMERIC
(
0
,
UA_NS0ID_ORGANIZES
);
UA_Server_addVariableNode
(
server
,
myIntegerNodeId
,
parentNodeId
,
parentReferenceNodeId
,
myIntegerName
,
UA_NODEID_NUMERIC
(
0
,
UA_NS0ID_BASEDATAVARIABLETYPE
),
attr
,
NULL
,
NULL
);
}
static
void
addMatrixVariable
(
UA_Server
*
server
)
{
UA_VariableAttributes
attr
=
UA_VariableAttributes_default
;
attr
.
displayName
=
UA_LOCALIZEDTEXT
(
"en-US"
,
"Double Matrix"
);
attr
.
accessLevel
=
UA_ACCESSLEVELMASK_READ
|
UA_ACCESSLEVELMASK_WRITE
;
/* Set the variable value constraints */
attr
.
dataType
=
UA_TYPES
[
UA_TYPES_DOUBLE
].
typeId
;
attr
.
valueRank
=
UA_VALUERANK_TWO_DIMENSIONS
;
UA_UInt32
arrayDims
[
2
]
=
{
2
,
2
};
attr
.
arrayDimensions
=
arrayDims
;
attr
.
arrayDimensionsSize
=
2
;
/* Set the value. The array dimensions need to be the same for the value. */
UA_Double
zero
[
4
]
=
{
0
.
0
,
0
.
0
,
0
.
0
,
0
.
0
};
UA_Variant_setArray
(
&
attr
.
value
,
zero
,
4
,
&
UA_TYPES
[
UA_TYPES_DOUBLE
]);
attr
.
value
.
arrayDimensions
=
arrayDims
;
attr
.
value
.
arrayDimensionsSize
=
2
;
UA_NodeId
myIntegerNodeId
=
UA_NODEID_STRING
(
1
,
"double.matrix"
);
UA_QualifiedName
myIntegerName
=
UA_QUALIFIEDNAME
(
1
,
"double matrix"
);
UA_NodeId
parentNodeId
=
UA_NODEID_NUMERIC
(
0
,
UA_NS0ID_OBJECTSFOLDER
);
UA_NodeId
parentReferenceNodeId
=
UA_NODEID_NUMERIC
(
0
,
UA_NS0ID_ORGANIZES
);
UA_Server_addVariableNode
(
server
,
myIntegerNodeId
,
parentNodeId
,
parentReferenceNodeId
,
myIntegerName
,
UA_NODEID_NUMERIC
(
0
,
UA_NS0ID_BASEDATAVARIABLETYPE
),
attr
,
NULL
,
NULL
);
}
/**
* Now we change the value with the write service. This uses the same service
* implementation that can also be reached over the network by an OPC UA client.
*/
static
void
writeVariable
(
UA_Server
*
server
)
{
UA_NodeId
myIntegerNodeId
=
UA_NODEID_STRING
(
1
,
"the.answer"
);
/* Write a different integer value */
UA_Int32
myInteger
=
43
;
UA_Variant
myVar
;
UA_Variant_init
(
&
myVar
);
UA_Variant_setScalar
(
&
myVar
,
&
myInteger
,
&
UA_TYPES
[
UA_TYPES_INT32
]);
UA_Server_writeValue
(
server
,
myIntegerNodeId
,
myVar
);
/* Set the status code of the value to an error code. The function
* UA_Server_write provides access to the raw service. The above
* UA_Server_writeValue is syntactic sugar for writing a specific node
* attribute with the write service. */
UA_WriteValue
wv
;
UA_WriteValue_init
(
&
wv
);
wv
.
nodeId
=
myIntegerNodeId
;
wv
.
attributeId
=
UA_ATTRIBUTEID_VALUE
;
wv
.
value
.
status
=
UA_STATUSCODE_BADNOTCONNECTED
;
wv
.
value
.
hasStatus
=
true
;
UA_Server_write
(
server
,
&
wv
);
/* Reset the variable to a good statuscode with a value */
wv
.
value
.
hasStatus
=
false
;
wv
.
value
.
value
=
myVar
;
wv
.
value
.
hasValue
=
true
;
UA_Server_write
(
server
,
&
wv
);
}
/**
* Note how we initially set the DataType attribute of the variable node to the
* NodeId of the Int32 data type. This forbids writing values that are not an
* Int32. The following code shows how this consistency check is performed for
* every write.
*/
static
void
writeWrongVariable
(
UA_Server
*
server
)
{
UA_NodeId
myIntegerNodeId
=
UA_NODEID_STRING
(
1
,
"the.answer"
);
/* Write a string */
UA_String
myString
=
UA_STRING
(
"test"
);
UA_Variant
myVar
;
UA_Variant_init
(
&
myVar
);
UA_Variant_setScalar
(
&
myVar
,
&
myString
,
&
UA_TYPES
[
UA_TYPES_STRING
]);
UA_StatusCode
retval
=
UA_Server_writeValue
(
server
,
myIntegerNodeId
,
myVar
);
printf
(
"Writing a string returned statuscode %s
\n
"
,
UA_StatusCode_name
(
retval
));
}
/** It follows the main server code, making use of the above definitions. */
static
volatile
UA_Boolean
running
=
true
;
static
void
stopHandler
(
int
sig
)
{
UA_LOG_INFO
(
UA_Log_Stdout
,
UA_LOGCATEGORY_
USERLAND
,
"received ctrl-c"
);
static
void
stopHandler
(
int
sig
n
)
{
UA_LOG_INFO
(
UA_Log_Stdout
,
UA_LOGCATEGORY_
SERVER
,
"received ctrl-c"
);
running
=
false
;
}
int
main
(
void
)
{
// switch on all relays
setRelayState
(
0x0F
);
printf
(
"Relays ON.
\n
"
);
// wait 1s
sleep
(
1
);
// switch off all relays
setRelayState
(
0x00
);
printf
(
"Relays OFF.
\n
"
);
signal
(
SIGINT
,
stopHandler
);
signal
(
SIGTERM
,
stopHandler
);
UA_Server
*
server
=
UA_Server_new
();
UA_ServerConfig_setDefault
(
UA_Server_getConfig
(
server
));
UA_ServerConfig
*
config
=
UA_Server_getConfig
(
server
);
config
->
verifyRequestTimestamp
=
UA_RULEHANDLING_ACCEPT
;
#ifdef UA_ENABLE_WEBSOCKET_SERVER
UA_ServerConfig_addNetworkLayerWS
(
UA_Server_getConfig
(
server
),
7681
,
0
,
0
,
NULL
,
NULL
);
#endif
addVariable
(
server
);
addMatrixVariable
(
server
);
writeVariable
(
server
);
writeWrongVariable
(
server
);
UA_StatusCode
retval
=
UA_Server_run
(
server
,
&
running
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment