You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mosquitto/plugins/examples/limit-subscription-qos/mosquitto_limit_subscriptio...

72 lines
1.9 KiB
C

/*
Copyright (c) 2021 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Abilio Marques - initial implementation and documentation.
*/
/*
* This is an *example* plugin which limits all the subscriptions' QoS to 1.
*
* Compile with:
* gcc -I<path to mosquitto-repo/include> -fPIC -shared mosquitto_limit_subscription.c -o mosquitto_limit_subscription_qos.so
*
* Use in config with:
*
* plugin /path/to/mosquitto_limit_subscription_qos.so
*
* Note that this only works on Mosquitto 2.0 or later.
*/
#include <stdio.h>
#include <string.h>
#include "mosquitto_broker.h"
#include "mosquitto_plugin.h"
#include "mosquitto.h"
#include "mqtt_protocol.h"
#define PLUGIN_NAME "limit-subscription-qos"
#define PLUGIN_VERSION "1.0"
#define UNUSED(A) (void)(A)
MOSQUITTO_PLUGIN_DECLARE_VERSION(5);
static mosquitto_plugin_id_t *mosq_pid = NULL;
static int callback_subscribe(int event, void *event_data, void *userdata)
{
struct mosquitto_evt_subscribe *ed = event_data;
UNUSED(event);
UNUSED(userdata);
if(ed->qos > 1){
ed->qos = 1;
}
return MOSQ_ERR_SUCCESS;
}
int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, struct mosquitto_opt *opts, int opt_count)
{
UNUSED(user_data);
UNUSED(opts);
UNUSED(opt_count);
mosq_pid = identifier;
mosquitto_plugin_set_info(identifier, PLUGIN_NAME, PLUGIN_VERSION);
return mosquitto_callback_register(mosq_pid, MOSQ_EVT_SUBSCRIBE, callback_subscribe, NULL, NULL);
}