-
Notifications
You must be signed in to change notification settings - Fork 66
/
mt.patch
368 lines (338 loc) · 9.11 KB
/
mt.patch
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
diff -cr -x .git ./libmodbus.orig/src/modbus.c ./libmodbus/src/modbus.c
*** ./libmodbus.orig/src/modbus.c 2014-02-07 22:46:00.000000000 +0100
--- ./libmodbus/src/modbus.c 2014-02-07 22:46:48.000000000 +0100
***************
*** 1457,1468 ****
--- 1457,1477 ----
ctx->backend->close(ctx);
}
+ void modbus_close_mt(modbus_t *ctx)
+ {
+ if (ctx == NULL)
+ return;
+
+ ctx->backend->close_mt(ctx);
+ }
+
void modbus_free(modbus_t *ctx)
{
if (ctx == NULL)
return;
free(ctx->backend_data);
+ free(ctx->mt_data);
free(ctx);
}
diff -cr -x .git ./libmodbus.orig/src/modbus.h ./libmodbus/src/modbus.h
*** ./libmodbus.orig/src/modbus.h 2014-02-07 22:46:00.000000000 +0100
--- ./libmodbus/src/modbus.h 2014-02-07 22:46:48.000000000 +0100
***************
*** 169,174 ****
--- 169,175 ----
EXPORT int modbus_connect(modbus_t *ctx);
EXPORT void modbus_close(modbus_t *ctx);
+ EXPORT void modbus_close_mt(modbus_t *ctx);
EXPORT void modbus_free(modbus_t *ctx);
diff -cr -x .git ./libmodbus.orig/src/modbus-private.h ./libmodbus/src/modbus-private.h
*** ./libmodbus.orig/src/modbus-private.h 2014-02-07 22:46:00.000000000 +0100
--- ./libmodbus/src/modbus-private.h 2014-02-07 22:46:48.000000000 +0100
***************
*** 109,114 ****
--- 109,115 ----
const uint8_t *rsp, int rsp_length);
int (*connect) (modbus_t *ctx);
void (*close) (modbus_t *ctx);
+ void (*close_mt) (modbus_t *ctx);
int (*flush) (modbus_t *ctx);
int (*select) (modbus_t *ctx, fd_set *rset, struct timeval *tv, int msg_length);
} modbus_backend_t;
***************
*** 124,129 ****
--- 125,131 ----
struct timeval byte_timeout;
const modbus_backend_t *backend;
void *backend_data;
+ void *mt_data;
};
void _modbus_init_common(modbus_t *ctx);
diff -cr -x .git ./libmodbus.orig/src/modbus-rtu.c ./libmodbus/src/modbus-rtu.c
*** ./libmodbus.orig/src/modbus-rtu.c 2014-02-07 22:46:00.000000000 +0100
--- ./libmodbus/src/modbus-rtu.c 2014-02-07 22:58:00.000000000 +0100
***************
*** 937,942 ****
--- 937,954 ----
#endif
}
+ void _modbus_rtu_close_mt(modbus_t *ctx)
+ {
+ ssize_t size;
+ _modbus_rtu_close(ctx);
+
+ modbus_rtu_mt_t *ctx_mt = ctx->mt_data;
+ size = write(ctx_mt->mtp_w, "q", 1);
+ if (size == -1)
+ fprintf(stderr, "ERROR returned by write() (%s)\n",
+ strerror(errno));
+ }
+
int _modbus_rtu_flush(modbus_t *ctx)
{
#if defined(_WIN32)
***************
*** 964,970 ****
return -1;
}
#else
! while ((s_rc = select(ctx->s+1, rset, NULL, NULL, tv)) == -1) {
if (errno == EINTR) {
if (ctx->debug) {
fprintf(stderr, "A non blocked signal was caught\n");
--- 976,988 ----
return -1;
}
#else
! /* Add pipe descriptor to select */
! modbus_rtu_mt_t *ctx_mt = ctx->mt_data;
! FD_SET(ctx_mt->mtp_r, rset);
!
! int max_n = (ctx->s > ctx_mt->mtp_r) ? ctx->s : ctx_mt->mtp_r;
!
! while ((s_rc = select(max_n+1, rset, NULL, NULL, tv)) == -1) {
if (errno == EINTR) {
if (ctx->debug) {
fprintf(stderr, "A non blocked signal was caught\n");
***************
*** 972,977 ****
--- 990,996 ----
/* Necessary after an error */
FD_ZERO(rset);
FD_SET(ctx->s, rset);
+ FD_SET(ctx_mt->mtp_r, rset);
} else {
return -1;
}
***************
*** 983,989 ****
return -1;
}
#endif
!
return s_rc;
}
--- 1002,1013 ----
return -1;
}
#endif
! if (FD_ISSET(ctx_mt->mtp_r, rset)) {
! /* Connection reset. */
! errno = ECONNRESET;
! return -1;
! }
!
return s_rc;
}
***************
*** 1004,1009 ****
--- 1028,1034 ----
_modbus_rtu_pre_check_confirmation,
_modbus_rtu_connect,
_modbus_rtu_close,
+ _modbus_rtu_close_mt,
_modbus_rtu_flush,
_modbus_rtu_select
};
***************
*** 1014,1019 ****
--- 1039,1045 ----
{
modbus_t *ctx;
modbus_rtu_t *ctx_rtu;
+ modbus_rtu_mt_t *ctx_mt;
size_t dest_size;
size_t ret_size;
***************
*** 1062,1067 ****
--- 1088,1107 ----
#endif
ctx_rtu->confirmation_to_ignore = FALSE;
+
+ /* Create pipe chanel */
+ ctx->mt_data = (modbus_rtu_mt_t *) malloc(sizeof(modbus_rtu_mt_t));
+ ctx_mt = (modbus_rtu_mt_t *)ctx->mt_data;
+
+ int mtp[2];
+ if (pipe(mtp) == -1) {
+ fprintf(stderr, "ERROR Can't create pipe (%s)\n",
+ strerror(errno));
+ modbus_free(ctx);
+ return NULL;
+ }
+ ctx_mt->mtp_r = mtp[0];
+ ctx_mt->mtp_w = mtp[1];
return ctx;
}
diff -cr -x .git ./libmodbus.orig/src/modbus-rtu-private.h ./libmodbus/src/modbus-rtu-private.h
*** ./libmodbus.orig/src/modbus-rtu-private.h 2014-02-07 22:46:00.000000000 +0100
--- ./libmodbus/src/modbus-rtu-private.h 2014-02-07 22:46:48.000000000 +0100
***************
*** 94,97 ****
--- 94,103 ----
int confirmation_to_ignore;
} modbus_rtu_t;
+ typedef struct _modbus_rtu_mt {
+ /* Pipes for control in multithread */
+ int mtp_r;
+ int mtp_w;
+ } modbus_rtu_mt_t;
+
#endif /* _MODBUS_RTU_PRIVATE_H_ */
diff -cr -x .git ./libmodbus.orig/src/modbus-tcp.c ./libmodbus/src/modbus-tcp.c
*** ./libmodbus.orig/src/modbus-tcp.c 2014-02-07 22:46:00.000000000 +0100
--- ./libmodbus/src/modbus-tcp.c 2014-02-07 22:46:48.000000000 +0100
***************
*** 404,409 ****
--- 404,420 ----
close(ctx->s);
}
+ void _modbus_tcp_close_mt(modbus_t *ctx)
+ {
+ _modbus_tcp_close(ctx);
+
+ modbus_tcp_mt_t *ctx_mt = ctx->mt_data;
+ if (ctx_mt->lst_socket != -1) {
+ shutdown(ctx_mt->lst_socket, SHUT_RDWR);
+ close(ctx_mt->lst_socket);
+ }
+ }
+
int _modbus_tcp_flush(modbus_t *ctx)
{
int rc;
***************
*** 448,453 ****
--- 459,465 ----
int yes;
struct sockaddr_in addr;
modbus_tcp_t *ctx_tcp = ctx->backend_data;
+ modbus_tcp_mt_t *ctx_mt = ctx->mt_data;
#ifdef OS_WIN32
if (_modbus_tcp_init_win32() == -1) {
***************
*** 481,486 ****
--- 493,500 ----
close(new_socket);
return -1;
}
+
+ ctx_mt->lst_socket = new_socket;
return new_socket;
}
***************
*** 495,500 ****
--- 509,515 ----
const char *service;
int new_socket;
modbus_tcp_pi_t *ctx_tcp_pi = ctx->backend_data;
+ modbus_tcp_mt_t *ctx_mt = ctx->mt_data;
if (ctx_tcp_pi->node[0] == 0)
node = NULL; /* == any */
***************
*** 577,582 ****
--- 592,599 ----
if (new_socket < 0) {
return -1;
}
+
+ ctx_mt->lst_socket = new_socket;
return new_socket;
}
***************
*** 671,676 ****
--- 688,694 ----
_modbus_tcp_pre_check_confirmation,
_modbus_tcp_connect,
_modbus_tcp_close,
+ _modbus_tcp_close_mt,
_modbus_tcp_flush,
_modbus_tcp_select
};
***************
*** 693,698 ****
--- 711,717 ----
_modbus_tcp_pre_check_confirmation,
_modbus_tcp_pi_connect,
_modbus_tcp_close,
+ _modbus_tcp_close_mt,
_modbus_tcp_flush,
_modbus_tcp_select
};
***************
*** 701,706 ****
--- 720,726 ----
{
modbus_t *ctx;
modbus_tcp_t *ctx_tcp;
+ modbus_tcp_mt_t *ctx_mt;
size_t dest_size;
size_t ret_size;
***************
*** 727,732 ****
--- 747,755 ----
ctx->backend_data = (modbus_tcp_t *) malloc(sizeof(modbus_tcp_t));
ctx_tcp = (modbus_tcp_t *)ctx->backend_data;
+
+ ctx->mt_data = (modbus_tcp_mt_t *) malloc(sizeof(modbus_tcp_mt_t));
+ ctx_mt = (modbus_tcp_mt_t *)ctx->mt_data;
dest_size = sizeof(char) * 16;
ret_size = strlcpy(ctx_tcp->ip, ip, dest_size);
***************
*** 746,751 ****
--- 769,776 ----
ctx_tcp->port = port;
ctx_tcp->t_id = 0;
+
+ ctx_mt->lst_socket = -1;
return ctx;
}
***************
*** 755,760 ****
--- 780,786 ----
{
modbus_t *ctx;
modbus_tcp_pi_t *ctx_tcp_pi;
+ modbus_tcp_mt_t *ctx_mt;
size_t dest_size;
size_t ret_size;
***************
*** 768,773 ****
--- 794,802 ----
ctx->backend_data = (modbus_tcp_pi_t *) malloc(sizeof(modbus_tcp_pi_t));
ctx_tcp_pi = (modbus_tcp_pi_t *)ctx->backend_data;
+
+ ctx->mt_data = (modbus_tcp_mt_t *) malloc(sizeof(modbus_tcp_mt_t));
+ ctx_mt = (modbus_tcp_mt_t *)ctx->mt_data;
dest_size = sizeof(char) * _MODBUS_TCP_PI_NODE_LENGTH;
ret_size = strlcpy(ctx_tcp_pi->node, node, dest_size);
***************
*** 802,807 ****
--- 831,838 ----
}
ctx_tcp_pi->t_id = 0;
+
+ ctx_mt->lst_socket = -1;
return ctx;
}
diff -cr -x .git ./libmodbus.orig/src/modbus-tcp-private.h ./libmodbus/src/modbus-tcp-private.h
*** ./libmodbus.orig/src/modbus-tcp-private.h 2014-02-07 22:46:00.000000000 +0100
--- ./libmodbus/src/modbus-tcp-private.h 2014-02-07 22:46:48.000000000 +0100
***************
*** 53,56 ****
--- 53,62 ----
char service[_MODBUS_TCP_PI_SERVICE_LENGTH];
} modbus_tcp_pi_t;
+
+ typedef struct _modbus_tcp_mt {
+ /* Main listen socket */
+ int lst_socket;
+ } modbus_tcp_mt_t;
+
#endif /* _MODBUS_TCP_PRIVATE_H_ */