-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodbusacap.c
450 lines (391 loc) · 12.5 KB
/
modbusacap.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/**
* Copyright (C) 2023, Axis Communications AB, Lund, Sweden
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <axevent.h>
#include <axparameter.h>
#include <libgen.h>
#include <param.h>
#include "modbus_client.h"
#include "modbus_server.h"
#include "modbusacap_common.h"
enum Mode
{
SERVER = 0,
CLIENT = 1
};
static GMainLoop *main_loop = NULL;
static AXEventHandler *ehandler;
static AXParameter *axparameter = NULL;
static gboolean initialized = FALSE;
static guint16 address = 0;
static guint8 mode = 0;
static guint32 port = 0;
static guint subscription_base;
static guint subscription_threshold;
static pthread_mutex_t lock;
static void open_syslog(const char *app_name)
{
openlog(app_name, LOG_PID, LOG_LOCAL4);
}
static void close_syslog(void)
{
LOG_I("%s/%s: Exiting!", __FILE__, __FUNCTION__);
closelog();
}
static void event_callback(guint subscription, AXEvent *event, void *data)
{
const AXEventKeyValueSet *key_value_set;
gboolean active;
(void)subscription;
(void)data;
// Handle event
key_value_set = ax_event_get_key_value_set(event);
if (ax_event_key_value_set_get_boolean(key_value_set, "active", NULL, &active, NULL))
{
LOG_I(
"aoa-event %s active (%s)",
active ? "is" : "NOT",
CLIENT == mode ? "running in client mode, passing on via Modbus"
: "running in server mode, not forwarded anywhere");
// Send event over Modbus
if (CLIENT == mode)
{
if (!modbus_client_send_event(address, active))
{
LOG_E("%s/%s: Failed to send event data over Modbus", __FILE__, __FUNCTION__);
}
}
}
else
{
LOG_I(
"%s/%s: Received event without boolean value 'active' (probably not a stateful event)",
__FILE__,
__FUNCTION__);
}
// Free the received event, n.b. AXEventKeyValueSet should not be freed
// since it's owned by the event system until unsubscribing
ax_event_free(event);
}
static guint aoatrigger_subscription(const guint newscenario, const gchar *subtype)
{
assert(NULL != ehandler);
AXEventKeyValueSet *key_value_set;
guint subscription;
key_value_set = ax_event_key_value_set_new();
// Setup subscription string and key/value set for subscription
gchar *subscriptionstr = g_strdup_printf("Device1Scenario%u%s", newscenario, NULL == subtype ? "" : subtype);
LOG_I("%s/%s: Create subscription for '%s'", __FILE__, __FUNCTION__, subscriptionstr);
ax_event_key_value_set_add_key_values(
key_value_set,
NULL,
"topic0",
"tnsaxis",
"CameraApplicationPlatform",
AX_VALUE_TYPE_STRING,
"topic1",
"tnsaxis",
"ObjectAnalytics",
AX_VALUE_TYPE_STRING,
"topic2",
"tnsaxis",
subscriptionstr,
AX_VALUE_TYPE_STRING,
NULL);
// Setup subscription and connect to callback function
ax_event_handler_subscribe(
ehandler, // event handler
key_value_set, // key value set
&subscription, // subscription id
(AXSubscriptionCallback)event_callback, // callback function
NULL, // user data
NULL); // GError
// Cleanup
g_free(subscriptionstr);
ax_event_key_value_set_free(key_value_set);
// Return subscription id
LOG_I("%s/%s: AOA trigger subscription id: %d", __FILE__, __FUNCTION__, subscription);
return subscription;
}
static void teardown_event_subscriptions(void)
{
assert(NULL != ehandler);
(void)ax_event_handler_unsubscribe(ehandler, subscription_base, NULL);
(void)ax_event_handler_unsubscribe(ehandler, subscription_threshold, NULL);
}
static void setup_event_subscriptions(const guint newscenario)
{
assert(NULL != ehandler);
// Unsubscribe from eventual existing subscriptions
teardown_event_subscriptions();
// New subscriptions; we currently subscribe to and handle the stateful (active/inactive)
// events with topic2 set to (and X = 1, 2 ... N):
// - "Device1ScenarioX"
// - "Device1ScenarioXThreshold"
subscription_base = aoatrigger_subscription(newscenario, NULL);
subscription_threshold = aoatrigger_subscription(newscenario, "Threshold");
}
static gboolean setup_modbus(const guint8 mode, const guint32 port, const gchar *server)
{
switch (mode)
{
case SERVER:
return modbus_server_start(port);
case CLIENT:
assert(NULL != server);
return modbus_client_init(server, port);
default:
LOG_E("%s/%s: %u is not a known mode", __FILE__, __FUNCTION__, mode);
break;
}
return FALSE;
}
static gchar *get_param(AXParameter *axparameter, const gchar *name)
{
assert(NULL != axparameter);
GError *error = NULL;
gchar *value = NULL;
if (!ax_parameter_get(axparameter, name, &value, &error))
{
LOG_E("%s/%s: failed to get %s parameter", __FILE__, __FUNCTION__, name);
if (NULL != error)
{
LOG_E("%s/%s: %s", __FILE__, __FUNCTION__, error->message);
g_error_free(error);
}
return NULL;
}
LOG_I("Got %s value: %s", name, value);
return value;
}
static void close_current_modbus(const guint8 m)
{
switch (m)
{
case SERVER:
modbus_server_stop();
break;
case CLIENT:
modbus_client_cleanup();
break;
default:
LOG_E("%s/%s: %u is not a known mode", __FILE__, __FUNCTION__, mode);
break;
}
}
static void address_callback(const gchar *name, const gchar *value, void *data)
{
(void)data;
if (NULL == value)
{
LOG_E("%s/%s: Unexpected NULL value for %s", __FILE__, __FUNCTION__, name);
return;
}
const int newaddress = atoi(value);
assert(0 <= newaddress || G_MAXUINT16 >= newaddress);
address = newaddress;
LOG_I("%s/%s: Got new %s (%u)", __FILE__, __FUNCTION__, name, address);
}
static void mode_callback(const gchar *name, const gchar *value, void *data)
{
(void)data;
if (NULL == value)
{
LOG_E("%s/%s: Unexpected NULL value for %s", __FILE__, __FUNCTION__, name);
return;
}
pthread_mutex_lock(&lock);
close_current_modbus(mode);
mode = atoi(value);
assert(0 == mode || 1 == mode);
LOG_I("%s/%s: Got new %s (%s)", __FILE__, __FUNCTION__, name, mode == SERVER ? "server" : "client");
// Setup Modbus for this mode
if (initialized && !setup_modbus(mode, port, get_param(axparameter, "Server")))
{
LOG_I("%s/%s: Failed to setup Modbus", __FILE__, __FUNCTION__);
assert(FALSE);
}
pthread_mutex_unlock(&lock);
}
static void port_callback(const gchar *name, const gchar *value, void *data)
{
(void)data;
if (NULL == value)
{
LOG_E("%s/%s: Unexpected NULL value for %s", __FILE__, __FUNCTION__, name);
return;
}
pthread_mutex_lock(&lock);
close_current_modbus(mode);
port = atoi(value);
assert(1024 <= port || 65535 >= port);
LOG_I("%s/%s: Got new %s (%u)", __FILE__, __FUNCTION__, name, port);
// Setup Modbus for this port
if (initialized && !setup_modbus(mode, port, get_param(axparameter, "Server")))
{
LOG_I("%s/%s: Failed to setup Modbus", __FILE__, __FUNCTION__);
assert(FALSE);
}
pthread_mutex_unlock(&lock);
}
static void scenario_callback(const gchar *name, const gchar *value, void *data)
{
(void)data;
if (NULL == value)
{
LOG_E("%s/%s: Unexpected NULL value for %s", __FILE__, __FUNCTION__, name);
return;
}
guint scenario = atoi(value);
assert(0 < scenario);
assert(100 > scenario);
LOG_I("%s/%s: Got new %s (%u)", __FILE__, __FUNCTION__, name, scenario);
// Update subscription
setup_event_subscriptions(scenario);
}
static void server_callback(const gchar *name, const gchar *value, void *data)
{
(void)data;
if (NULL == value)
{
LOG_E("%s/%s: Unexpected NULL value for %s", __FILE__, __FUNCTION__, name);
return;
}
pthread_mutex_lock(&lock);
close_current_modbus(mode);
LOG_I("%s/%s: Got new %s (%s)", __FILE__, __FUNCTION__, name, value);
// Setup Modbus for this mode
if (initialized && !setup_modbus(mode, port, value))
{
LOG_I("%s/%s: Failed to setup Modbus", __FILE__, __FUNCTION__);
}
pthread_mutex_unlock(&lock);
}
static gboolean setup_param(const gchar *name, AXParameterCallback callbackfn)
{
GError *error = NULL;
gchar *value = NULL;
assert(NULL != name);
assert(NULL != axparameter);
assert(NULL != callbackfn);
if (!ax_parameter_register_callback(axparameter, name, callbackfn, NULL, &error))
{
LOG_E("%s/%s: failed to register %s callback", __FILE__, __FUNCTION__, name);
if (NULL != error)
{
LOG_E("%s/%s: %s", __FILE__, __FUNCTION__, error->message);
g_error_free(error);
}
return FALSE;
}
value = get_param(axparameter, name);
if (NULL == value)
{
return FALSE;
}
LOG_I("%s/%s: Got %s value: %s", __FILE__, __FUNCTION__, name, value);
callbackfn(name, value, NULL);
g_free(value);
return TRUE;
}
static void signal_handler(gint signal_num)
{
switch (signal_num)
{
case SIGTERM:
case SIGABRT:
case SIGINT:
LOG_I("%s/%s: Unsubscribe from events ...", __FILE__, __FUNCTION__);
teardown_event_subscriptions();
g_main_loop_quit(main_loop);
break;
default:
break;
}
}
static gboolean signal_handler_init(void)
{
struct sigaction sa = {0};
if (-1 == sigemptyset(&sa.sa_mask))
{
LOG_E("%s/%s: Failed to initialize signal handler: %s", __FILE__, __FUNCTION__, strerror(errno));
return FALSE;
}
sa.sa_handler = signal_handler;
if (0 > sigaction(SIGTERM, &sa, NULL) || 0 > sigaction(SIGABRT, &sa, NULL) || 0 > sigaction(SIGINT, &sa, NULL))
{
LOG_E("%s/%s: Failed to install signal handler: %s", __FILE__, __FUNCTION__, strerror(errno));
return FALSE;
}
return TRUE;
}
int main(int argc, char **argv)
{
GError *error = NULL;
char *app_name = basename(argv[0]);
open_syslog(app_name);
int ret = EXIT_SUCCESS;
if (!signal_handler_init())
{
ret = EXIT_FAILURE;
goto exit_syslog;
}
// Create event handler
ehandler = ax_event_handler_new();
// ACAP parameter setup
axparameter = ax_parameter_new(app_name, &error);
if (NULL != error)
{
LOG_E("%s/%s: ax_parameter_new failed (%s)", __FILE__, __FUNCTION__, error->message);
g_error_free(error);
ret = EXIT_FAILURE;
goto exit_ehandler;
}
// clang-format off
if (!setup_param("ModbusAddress", address_callback) ||
!setup_param("Mode", mode_callback) ||
!setup_param("Port", port_callback) ||
!setup_param("Scenario", scenario_callback) ||
!setup_param("Server", server_callback))
// clang-format on
{
ret = EXIT_FAILURE;
goto exit_param;
}
// We are initialized, trigger start of Modbus handling via parameter callback
initialized = TRUE;
mode_callback("Mode", get_param(axparameter, "Mode"), NULL);
// Main loop
LOG_I("%s/%s: Ready", __FILE__, __FUNCTION__);
main_loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(main_loop);
// Cleanup and controlled shutdown
LOG_I("%s/%s: Unreference main loop ...", __FILE__, __FUNCTION__);
g_main_loop_unref(main_loop);
exit_param:
LOG_I("%s/%s: Free parameter handler ...", __FILE__, __FUNCTION__);
ax_parameter_free(axparameter);
exit_ehandler:
LOG_I("%s/%s: Free event handler ...", __FILE__, __FUNCTION__);
ax_event_handler_free(ehandler);
// Cleanup Modbus
modbus_client_cleanup();
modbus_server_stop();
exit_syslog:
LOG_I("%s/%s: Closing syslog ...", __FILE__, __FUNCTION__);
close_syslog();
return ret;
}