@@ -32,6 +32,7 @@ static iface_ethernet_ip_disconnected_cb_t disconn_cb;
32
32
static struct l_timeout * connect_to ;
33
33
struct knot_thing thing_ethernet_ip ;
34
34
static struct l_io * ethernet_op_io ;
35
+ static int status_ethernet_ip ;
35
36
36
37
union ethernet_ip_types {
37
38
float val_float ;
@@ -113,12 +114,6 @@ static int connect_ethernet_ip(struct knot_data_item *data_item)
113
114
{
114
115
int rc = 0 ;
115
116
116
- /* Check and destroy if an IO is already allocated */
117
- if (ethernet_op_io ) {
118
- l_io_destroy (ethernet_op_io );
119
- ethernet_op_io = NULL ;
120
- }
121
-
122
117
plc_tag_destroy (data_item -> tag );
123
118
data_item -> tag = plc_tag_create (data_item -> string_tag_path ,
124
119
DATA_TIMEOUT );
@@ -131,20 +126,6 @@ static int connect_ethernet_ip(struct knot_data_item *data_item)
131
126
return - EINVAL ;
132
127
}
133
128
134
- ethernet_op_io = l_io_new (plc_tag_status (data_item -> tag ));
135
- if (!ethernet_op_io ) {
136
- l_error ("Error setting up tag internal state. %s" ,
137
- plc_tag_decode_error (
138
- data_item -> tag ));
139
- return - EIO ;
140
- }
141
-
142
- if (!l_io_set_disconnect_handler (ethernet_op_io , on_disconnected , NULL ,
143
- NULL )) {
144
- l_error ("Couldn't set Ethernet/IP disconnect handler" );
145
- return - EINVAL ;
146
- }
147
-
148
129
return rc ;
149
130
}
150
131
@@ -188,21 +169,40 @@ static void attempt_connect(struct l_timeout *to, void *user_data)
188
169
189
170
l_debug ("Trying to connect to Ethernet/Ip" );
190
171
172
+ /* Check and destroy if an IO is already allocated */
173
+ if (ethernet_op_io ) {
174
+ l_io_destroy (ethernet_op_io );
175
+ ethernet_op_io = NULL ;
176
+ }
177
+
191
178
l_hashmap_foreach (thing_ethernet_ip .data_items ,
192
179
foreach_data_item_ethernet_ip , & rc );
193
180
if (rc ) {
194
181
l_error ("error connecting to Ethernet/Ip" );
195
182
goto retry ;
196
183
}
197
184
185
+ ethernet_op_io = l_io_new (status_ethernet_ip );
186
+ if (!ethernet_op_io ) {
187
+ l_error ("Error setting up tag internal state." );
188
+ goto io_destroy ;
189
+ }
190
+
191
+ if (!l_io_set_disconnect_handler (ethernet_op_io , on_disconnected , NULL ,
192
+ NULL )) {
193
+ l_error ("Couldn't set Ethernet/IP disconnect handler" );
194
+ goto io_destroy ;
195
+ }
196
+
198
197
if (conn_cb )
199
198
conn_cb (user_data );
200
199
201
200
return ;
202
201
203
- retry :
202
+ io_destroy :
204
203
l_io_destroy (ethernet_op_io );
205
204
ethernet_op_io = NULL ;
205
+ retry :
206
206
l_timeout_modify (to , RECONNECT_TIMEOUT );
207
207
}
208
208
0 commit comments