-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example plugin for adding timestamp properties to MQTT v5 messages.
For #1225.
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
all : mosquitto_timestamp.so | ||
|
||
mosquitto_timestamp.so : mosquitto_timestamp.c | ||
$(CC) -I../../include -fPIC -shared $< -o $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
Copyright (c) 2020 Roger Light <[email protected]> | ||
All rights reserved. This program and the accompanying materials | ||
are made available under the terms of the Eclipse Public License v1.0 | ||
and Eclipse Distribution License v1.0 which accompany this distribution. | ||
The Eclipse Public License is available at | ||
http:https://www.eclipse.org/legal/epl-v10.html | ||
and the Eclipse Distribution License is available at | ||
http:https://www.eclipse.org/org/documents/edl-v10.php. | ||
Contributors: | ||
Roger Light - initial implementation and documentation. | ||
*/ | ||
|
||
/* | ||
* Add an MQTT v5 user-property with key "timestamp" and value of timestamp in ISO-8601 format to all messages. | ||
* | ||
* Compile with: | ||
* gcc -I<path to mosquitto-repo/include> -fPIC -shared mosquitto_timestamp.c -o mosquitto_timestamp.so | ||
* | ||
* Use in config with: | ||
* | ||
* plugin /path/to/mosquitto_timestamp.so | ||
* | ||
* Note that this only works on Mosquitto 2.0 or later. | ||
*/ | ||
|
||
|
||
#include <stdio.h> | ||
#include <time.h> | ||
|
||
#include "mosquitto_broker.h" | ||
#include "mosquitto_plugin.h" | ||
#include "mosquitto.h" | ||
#include "mqtt_protocol.h" | ||
|
||
static mosquitto_plugin_id_t *mosq_pid = NULL; | ||
|
||
static int callback_message(int event, void *event_data, void *userdata) | ||
{ | ||
struct mosquitto_evt_message *ed = event_data; | ||
struct timespec ts; | ||
struct tm *ti; | ||
char time_buf[25]; | ||
|
||
clock_gettime(CLOCK_REALTIME, &ts); | ||
ti = gmtime(&ts.tv_sec); | ||
strftime(time_buf, sizeof(time_buf), "%Y-%m-%dT%H:%M:%SZ", ti); | ||
|
||
return mosquitto_property_add_string_pair(&ed->properties, MQTT_PROP_USER_PROPERTY, "timestamp", time_buf); | ||
} | ||
|
||
int mosquitto_plugin_version(void) | ||
{ | ||
return 5; | ||
} | ||
|
||
int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, struct mosquitto_opt *opts, int opt_count) | ||
{ | ||
mosq_pid = identifier; | ||
return mosquitto_callback_register(mosq_pid, MOSQ_EVT_MESSAGE, callback_message, NULL, NULL); | ||
} | ||
|
||
int mosquitto_plugin_cleanup(void *user_data, struct mosquitto_opt *opts, int opt_count) | ||
{ | ||
return mosquitto_callback_unregister(mosq_pid, MOSQ_EVT_MESSAGE, callback_message, NULL); | ||
} |