-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.c
73 lines (65 loc) · 2.42 KB
/
plugin.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
//
// Created by bluse on 2022/7/31.
//
#include <string.h>
#include <mosquitto.h>
#include <mosquitto_broker.h>
#include <mosquitto_plugin.h>
#include <stdlib.h>
#include "handler.h"
int max_delay = 60;
static mosquitto_plugin_id_t *mos_pid = NULL;
int mosquitto_plugin_version(int supported_version_count, const int *supported_versions) {
int i;
for (i = 0; i < supported_version_count; i++) {
if (supported_versions[i] == 5) {
return 5;
}
}
return -1;
}
int
mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, struct mosquitto_opt *opts, int opt_count) {
UNUSED(user_data);
for (int i = 0; i < opt_count; ++i) {
mosquitto_log_printf(MOSQ_LOG_DEBUG, "%s -> %s %d", opts->key, opts->value, strcmp(opts->key, "max_delay"));
if (strcmp(opts->key, "max_delay") == 0) {
max_delay = atoi(opts->value);
if (max_delay > 600 || max_delay < 6) {
return MOSQ_ERR_UNKNOWN;
}
mosquitto_log_printf(MOSQ_LOG_DEBUG, "maxDelay -> %d", max_delay);
}
opts++;
}
mos_pid = identifier;
int rs = mosquitto_callback_register(mos_pid, MOSQ_EVT_TICK, handle_delay_message_tick, NULL, NULL);
if (rs != MOSQ_ERR_SUCCESS) {
mosquitto_log_printf(MOSQ_LOG_ERR, "mosquitto_callback_register MOSQ_EVT_TICK err:%d", rs);
return rs;
}
rs = mosquitto_callback_register(mos_pid, MOSQ_EVT_MESSAGE, handle_delay_message, NULL, NULL);
if (rs != MOSQ_ERR_SUCCESS) {
mosquitto_log_printf(MOSQ_LOG_ERR, "mosquitto_callback_register MOSQ_EVT_MESSAGE err:%d", rs);
return rs;
}
return MOSQ_ERR_SUCCESS;
}
int mosquitto_plugin_cleanup(void *user_data, struct mosquitto_opt *opts, int opt_count) {
UNUSED(user_data);
UNUSED(opts);
UNUSED(opt_count);
if (mos_pid) {
int rs = mosquitto_callback_unregister(mos_pid, MOSQ_EVT_TICK, handle_delay_message_tick, NULL);
if (rs != MOSQ_ERR_SUCCESS) {
mosquitto_log_printf(MOSQ_LOG_ERR, "mosquitto_callback_unregister MOSQ_EVT_TICK err:%d", rs);
return rs;
}
rs = mosquitto_callback_unregister(mos_pid, MOSQ_EVT_MESSAGE, handle_delay_message, NULL);
if (rs != MOSQ_ERR_SUCCESS) {
mosquitto_log_printf(MOSQ_LOG_ERR, "mosquitto_callback_unregister MOSQ_EVT_MESSAGE err:%d", rs);
return rs;
}
}
return MOSQ_ERR_SUCCESS;
}