-
Notifications
You must be signed in to change notification settings - Fork 0
/
s7_tcp.c
251 lines (212 loc) · 7.91 KB
/
s7_tcp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
* NEURON IIoT System for Industry 4.0
* Copyright (C) 2020-2022 EMQ Technologies Co., Ltd All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include <stdlib.h>
#include <neuron.h>
#include "errcodes.h"
#include "s7_point.h"
#include "s7_req.h"
#include "s7_stack.h"
static neu_plugin_t *driver_open(void);
static int driver_close(neu_plugin_t *plugin);
static int driver_init(neu_plugin_t *plugin, bool load);
static int driver_uninit(neu_plugin_t *plugin);
static int driver_start(neu_plugin_t *plugin);
static int driver_stop(neu_plugin_t *plugin);
static int driver_config(neu_plugin_t *plugin, const char *config);
static int driver_request(neu_plugin_t *plugin, neu_reqresp_head_t *head,
void *data);
static int driver_tag_validator(const neu_datatag_t *tag);
static int driver_validate_tag(neu_plugin_t *plugin, neu_datatag_t *tag);
static int driver_group_timer(neu_plugin_t *plugin, neu_plugin_group_t *group);
static int driver_write(neu_plugin_t *plugin, void *req, neu_datatag_t *tag,
neu_value_u value);
static int driver_write_tags(neu_plugin_t *plugin, void *req, UT_array *tags);
static const neu_plugin_intf_funs_t plugin_intf_funs = {
.open = driver_open,
.close = driver_close,
.init = driver_init,
.uninit = driver_uninit,
.start = driver_start,
.stop = driver_stop,
.setting = driver_config,
.request = driver_request,
.driver.validate_tag = driver_validate_tag,
.driver.group_timer = driver_group_timer,
.driver.write_tag = driver_write,
.driver.tag_validator = driver_tag_validator,
.driver.write_tags = driver_write_tags,
.driver.add_tags = NULL,
.driver.load_tags = NULL,
.driver.del_tags = NULL,
};
const neu_plugin_module_t neu_plugin_module = {
.version = NEURON_PLUGIN_VER_1_0,
.schema = "s7-tcp",
.module_name = "Siemens S7",
.module_descr =
"This plugin is used to connect Siemens PLC using the s7 TCP protocol.",
.module_descr_zh =
"该插件用于使用S7协议连接西门子PLC",
.intf_funs = &plugin_intf_funs,
.kind = NEU_PLUGIN_KIND_SYSTEM,
.type = NEU_NA_TYPE_DRIVER,
.display = true,
.single = false,
};
static neu_plugin_t *driver_open(void)
{
neu_plugin_t *plugin = calloc(1, sizeof(neu_plugin_t));
neu_plugin_common_init(&plugin->common);
return plugin;
}
static int driver_close(neu_plugin_t *plugin)
{
free(plugin);
return 0;
}
static int driver_init(neu_plugin_t *plugin, bool load)
{
(void) load;
plugin->protocol = S7_PROTOCOL_TCP;
plugin->events = neu_event_new();
plugin->stack = s7_stack_create((void *) plugin, S7_PROTOCOL_TCP,
s7_send_msg, s7_value_handle,
s7_write_resp);
plog_notice(plugin, "%s init success", plugin->common.name);
return 0;
}
static int driver_uninit(neu_plugin_t *plugin)
{
plog_notice(plugin, "%s uninit start", plugin->common.name);
if (plugin->conn != NULL) {
neu_conn_destory(plugin->conn);
}
if (plugin->stack) {
s7_stack_destroy(plugin->stack);
}
neu_event_close(plugin->events);
plog_notice(plugin, "%s uninit success", plugin->common.name);
return 0;
}
static int driver_start(neu_plugin_t *plugin)
{
neu_conn_start(plugin->conn);
plog_notice(plugin, "%s start success", plugin->common.name);
return 0;
}
static int driver_stop(neu_plugin_t *plugin)
{
neu_conn_stop(plugin->conn);
plog_notice(plugin, "%s stop success", plugin->common.name);
return 0;
}
static int driver_config(neu_plugin_t *plugin, const char *config)
{
int ret = 0;
char * err_param = NULL;
neu_json_elem_t host = { .name = "host",
.t = NEU_JSON_STR,
.v.val_str = NULL };
neu_json_elem_t port = { .name = "port", .t = NEU_JSON_INT };
neu_json_elem_t pdu_size = { .name = "pdu_size", .t = NEU_JSON_INT };
neu_json_elem_t module = { .name = "module", .t = NEU_JSON_INT };
neu_json_elem_t rack = { .name = "rack", .t = NEU_JSON_INT };
neu_json_elem_t slot = { .name = "slot", .t = NEU_JSON_INT };
neu_json_elem_t interval = { .name = "interval", .t = NEU_JSON_INT };
neu_conn_param_t param = { 0 };
ret = neu_parse_param((char *) config, &err_param, 6,&host, &port, &pdu_size,
&module,&rack, &slot);
if (ret != 0) {
plog_error(plugin, "config: %s, decode error: %s", config, err_param);
free(err_param);
if (host.v.val_str != NULL) {
free(host.v.val_str);
}
return -1;
}
param.log = plugin->common.log;
plugin->interval = interval.v.val_int;
param.type = NEU_CONN_TCP_CLIENT;
param.params.tcp_client.ip = host.v.val_str;
param.params.tcp_client.port = port.v.val_int;
param.params.tcp_client.timeout = 3000;
plugin->is_server = false;
plog_notice(plugin,
"config: host: %s, port: %" PRId64 ", module: %" PRId64 "",
host.v.val_str, port.v.val_int, module.v.val_int);
if (plugin->conn != NULL) {
plugin->conn = neu_conn_reconfig(plugin->conn, ¶m);
} else {
plugin->common.link_state = NEU_NODE_LINK_STATE_DISCONNECTED;
plugin->conn =
neu_conn_new(¶m, (void *) plugin, s7_conn_connected,
s7_conn_disconnected);
}
free(host.v.val_str);
return 0;
}
static int driver_request(neu_plugin_t *plugin, neu_reqresp_head_t *head,
void *data)
{
(void) plugin;
(void) head;
(void) data;
return 0;
}
static int driver_tag_validator(const neu_datatag_t *tag)
{
s7_point_t point = { 0 };
return s7_tag_to_point(tag, &point);
}
static int driver_validate_tag(neu_plugin_t *plugin, neu_datatag_t *tag)
{
s7_point_t point = { 0 };
int ret = s7_tag_to_point(tag, &point);
if (ret == 0) {
plog_notice(
plugin,
"validate tag success, name: %s, address: %s, type: %d, slave id: "
"%d, start address: %d, n register: %d, area: %s",
tag->name, tag->address, tag->type, point.dbnumber,
point.start_address, point.n_register,
s7_area_to_str(point.area));
} else {
plog_error(
plugin,
"validate tag error, name: %s, address: %s, type: %d, slave id: "
"%d, start address: %d, n register: %d, area: %s",
tag->name, tag->address, tag->type, point.dbnumber,
point.start_address, point.n_register,
s7_area_to_str(point.area));
}
return ret;
}
static int driver_group_timer(neu_plugin_t *plugin, neu_plugin_group_t *group)
{
return s7_group_timer(plugin, group);
}
static int driver_write(neu_plugin_t *plugin, void *req, neu_datatag_t *tag,
neu_value_u value)
{
return s7_write_tag(plugin, req, tag, value);
}
static int driver_write_tags(neu_plugin_t *plugin, void *req, UT_array *tags)
{
return s7_write_tags(plugin, req, tags);
}