- Main
- Tutorials
- Library
- Samples

- File List
- Globals

sample_node_operator.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2019 Moxa Inc. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Sample Node Operator
6 *
7 * Date Author Comment
8 * 2019-06-05 Rich Liao Created it.
9 ******************************************************************************/
10
25 #include <unistd.h>
26 #include <string.h>
27
29 #include <mx_node_sdk/mx_node_interface_basic.h>
30 #include <mx_node_sdk/mx_node_interface_data_access.h>
31
33 typedef enum _OPERATOR_STATE
34 {
35 OPERATOR_STATE_STOP,
36 OPERATOR_STATE_STOPPING,
37 OPERATOR_STATE_RUNNING,
38 } OPERATOR_STATE;
39
40 typedef struct _MY_DATA
41 {
42 MX_NODE_NODE_HANDLE node_handle;
43 int data;
44 struct timeval timestamp;
45 } MY_DATA;
46
48 struct timeval now()
49 {
50 struct timeval t;
51 gettimeofday(&t, NULL);
52 return t;
53 }
54
56 volatile OPERATOR_STATE g_state;
57 MY_DATA g_my_node;
58
61 const char* mx_node_operator_get_version()
62 {
63 return "1.0";
64 }
65
66 long long mx_node_operator_get_supported_interfaces()
67 {
68 return INTERFACE_MX_NODE_BASIC | INTERFACE_MX_NODE_DATA_ACCESS;
69 }
70
71 MX_NODE_RESULT mx_node_operator_initialize(MX_NODE_NODE_OPERATOR_HANDLE operator_handle)
72 {
73 MX_NODE_RESULT res;
74
75 // Initialize state and data.
76 g_state = OPERATOR_STATE_STOP;
77 g_my_node.data = 0;
78
79 // Create Nodes
80 // Node SampleFolder
81 MX_NODE_NODE folder;
82 MX_NODE_NODE_HANDLE folder_handle;
83 {
84 // Fill in SampleFolder's information.
85 strcpy(folder.node_name, "SampleFolder");
86 folder.node_type = MX_NODE_NODE_TYPE_FOLDER;
87 strcpy(folder.description, "SampleFolder's Description");
88
89 // Add SampleFolder to OPC UA server.
90 res = mx_node_add_node(operator_handle, &folder, &folder_handle);
91 if (res != MX_NODE_RESULT_GOOD)
92 return res;
93 }
94
95 // Node SampleInteger
96 MX_NODE_NODE var;
97 {
98 // Fill in SampleInteger's information.
99 strcpy(var.node_name, "SampleInteger");
100 var.node_type = MX_NODE_NODE_TYPE_VARIABLE;
101 strcpy(var.description, "SampleInteger's Description");
102 var.variable.access_right = MX_NODE_ACCESS_RIGHT_READWRITE;
103 var.variable.value.type = MX_NODE_VALUE_TYPE_INT32;
104 var.variable.value.value.i32 = g_my_node.data;
105
106 // Add SampleInteger to OPC UA server.
107 res = mx_node_add_node(operator_handle, &var, &g_my_node.node_handle);
108 if (res != MX_NODE_RESULT_GOOD)
109 return res;
110
111 // Set SampleInteger's parent to SampleFolder.
112 res = mx_node_set_parent_node(g_my_node.node_handle, folder_handle);
113 if (res != MX_NODE_RESULT_GOOD)
114 return res;
115 }
116
117 return MX_NODE_RESULT_GOOD;
118 }
119
120 void mx_node_operator_uninitialize()
121 {
122 }
123
124 void mx_node_operator_start()
125 {
126 g_state = OPERATOR_STATE_RUNNING;
127 while (g_state == OPERATOR_STATE_RUNNING)
128 {
129 g_my_node.data += 1;
130 g_my_node.timestamp = now();
131 MX_NODE_VARIANT var;
132 {
133 var.type = MX_NODE_VALUE_TYPE_INT32;
134 var.value.i32 = g_my_node.data;
135 }
136 mx_node_update_node(g_my_node.node_handle, &var, &g_my_node.timestamp);
137 sleep(3);
138 }
139 g_state = OPERATOR_STATE_STOP;
140 }
141
142 void mx_node_operator_stop()
143 {
144 if (g_state == OPERATOR_STATE_RUNNING)
145 {
146 g_state = OPERATOR_STATE_STOPPING;
147 while (g_state != OPERATOR_STATE_STOP);
148 }
149 }
151
153 MX_NODE_RESULT mx_node_operator_read_node(MX_NODE_NODE_HANDLE node_handle, MX_NODE_VARIANT* node_value, struct timeval* node_timestamp)
154 {
155 if (node_handle == g_my_node.node_handle)
156 {
157 node_value->type = MX_NODE_VALUE_TYPE_INT32;
158 node_value->value.i32 = g_my_node.data;
159 *node_timestamp = g_my_node.timestamp;
160 return MX_NODE_RESULT_GOOD;
161 }
162 return MX_NODE_RESULT_BAD;
163 }
164
165 MX_NODE_RESULT mx_node_operator_write_node(MX_NODE_NODE_HANDLE node_handle, const MX_NODE_VARIANT* node_value)
166 {
167 if (node_handle == g_my_node.node_handle && MX_NODE_VALUE_TYPE_INT32 == node_value->type)
168 {
169 g_my_node.data = node_value->value.i32;
170 g_my_node.timestamp = now();
171 return MX_NODE_RESULT_GOOD;
172 }
173 return MX_NODE_RESULT_BAD;
174 }
_MX_NODE_VARIABLE::value
MX_NODE_VARIANT value
Definition: mx_node_node_def.h:123
mx_node_add_node
MX_NODE_RESULT mx_node_add_node(MX_NODE_NODE_OPERATOR_HANDLE operator_handle, MX_NODE_NODE *node, MX_NODE_NODE_HANDLE *node_handle)
Add a node to OPC UA server.
mx_node_operator_write_node
MX_NODE_RESULT mx_node_operator_write_node(MX_NODE_NODE_HANDLE node_handle, const MX_NODE_VARIANT *node_value)
Write data by handle index.
_MX_NODE_VARIANT
A variant structure is used to store different type&#39;s data.
Definition: mx_node_node_def.h:97
mx_node_operator_start
void mx_node_operator_start()
A notify to inform node operator start with non-blocking.
MY_DATA
struct _MY_DATA MY_DATA
MX_NODE_NODE_HANDLE
size_t MX_NODE_NODE_HANDLE
Handle of a node.
Definition: mx_node_node.h:42
INTERFACE_MX_NODE_BASIC
#define INTERFACE_MX_NODE_BASIC
Interface basic&#39;s flag.
Definition: mx_node_interface_basic.h:42
MX_NODE_ACCESS_RIGHT_READWRITE
Definition: mx_node_node_def.h:90
MX_NODE_NODE_TYPE_VARIABLE
Definition: mx_node_node_def.h:44
_MX_NODE_NODE::variable
MX_NODE_VARIABLE variable
Definition: mx_node_node_def.h:148
MX_NODE_RESULT
enum _MX_NODE_RESULT MX_NODE_RESULT
A result enumeration represents success or not.
now
struct timeval now()
custom function
Definition: sample_node_operator.c:48
_MY_DATA::data
MX_NODE_VARIANT data
Definition: demo_node_operator.c:45
mx_node_operator_stop
void mx_node_operator_stop()
A notify to inform node operator stopping.
_MX_NODE_NODE
A node&#39;s sturcture contain name, description, node type and attribute.
Definition: mx_node_node_def.h:139
OPERATOR_STATE_STOP
Definition: sample_node_operator.c:35
mx_node_set_parent_node
MX_NODE_RESULT mx_node_set_parent_node(MX_NODE_NODE_HANDLE node_handle, MX_NODE_NODE_HANDLE parent_node_handle)
Set a node&#39;s parent.
INTERFACE_MX_NODE_DATA_ACCESS
#define INTERFACE_MX_NODE_DATA_ACCESS
Interface data access&#39;s flag.
Definition: mx_node_interface_basic.h:48
mx_node_operator_get_version
const char * mx_node_operator_get_version()
Get node operator&#39;s version.
mx_node_operator_read_node
MX_NODE_RESULT mx_node_operator_read_node(MX_NODE_NODE_HANDLE node_handle, MX_NODE_VARIANT *node_value, struct timeval *node_timestamp)
Read a node&#39;s data and timestamp.
_MX_NODE_VARIANT::i32
int32_t i32
Definition: mx_node_node_def.h:105
mx_node_operator_get_supported_interfaces
long long mx_node_operator_get_supported_interfaces()
Get supported interfaces.
g_my_node
MY_DATA g_my_node
Definition: sample_node_operator.c:57
_MX_NODE_VARIABLE::access_right
MX_NODE_ACCESS_RIGHT access_right
Definition: mx_node_node_def.h:124
OPERATOR_STATE_STOPPING
Definition: sample_node_operator.c:36
MX_NODE_RESULT_GOOD
Definition: mx_node_node_def.h:158
MX_NODE_VALUE_TYPE_INT32
Definition: mx_node_node_def.h:71
_MY_DATA::data
int data
Definition: sample_node_operator.c:43
OPERATOR_STATE
enum _OPERATOR_STATE OPERATOR_STATE
include interfaces
_MX_NODE_NODE::node_name
char node_name[MAX_NODE_NAME_LEN]
Definition: mx_node_node_def.h:141
_MX_NODE_NODE::description
char description[MAX_NODE_DESC_LEN]
Definition: mx_node_node_def.h:142
_MX_NODE_VARIANT::type
MX_NODE_VALUE_TYPE type
Definition: mx_node_node_def.h:99
mx_node_operator_uninitialize
void mx_node_operator_uninitialize()
Do node operator&#39;s uninitialize.
MX_NODE_NODE_TYPE_FOLDER
Definition: mx_node_node_def.h:42
_OPERATOR_STATE
_OPERATOR_STATE
include interfaces
Definition: sample_node_operator.c:33
_MY_DATA::node_handle
MX_NODE_NODE_HANDLE node_handle
Definition: demo_node_operator.c:44
g_state
volatile OPERATOR_STATE g_state
custom variables
Definition: sample_node_operator.c:56
MX_NODE_RESULT_BAD
Definition: mx_node_node_def.h:159
_MX_NODE_VARIANT::value
union _MX_NODE_VARIANT::@0 value
OPERATOR_STATE_RUNNING
Definition: sample_node_operator.c:37
_MY_DATA::timestamp
struct timeval timestamp
Definition: demo_node_operator.c:46
_MY_DATA
Definition: demo_node_operator.c:42
_MX_NODE_NODE::node_type
MX_NODE_NODE_TYPE node_type
Definition: mx_node_node_def.h:143
mx_node_operator_initialize
MX_NODE_RESULT mx_node_operator_initialize(MX_NODE_NODE_OPERATOR_HANDLE operator_handle)
Do node operator&#39;s initialize, like create nodes.
mx_node_update_node
MX_NODE_RESULT mx_node_update_node(MX_NODE_NODE_HANDLE node_handle, MX_NODE_VARIANT *value, struct timeval *timestamp)
Update a node&#39;s data and timestamp.
MX_NODE_NODE_OPERATOR_HANDLE
size_t MX_NODE_NODE_OPERATOR_HANDLE
Handle of a node operator.
Definition: mx_node_node.h:37
OPERATOR_STATE
OPERATOR_STATE
custom define
Definition: demo_node_operator.c:35