mosquitto/lib/will_mosq.c
Roger Light e2324ff9bb Merge remote-tracking branch 'remotes/origin/master' into develop
Conflicts:
	CMakeLists.txt
	ChangeLog.txt
	THANKS.txt
	appveyor.yml
	config.mk
	installer/mosquitto-cygwin.nsi
	installer/mosquitto.nsi
	lib/messages_mosq.c
	lib/messages_mosq.h
	lib/mosquitto.c
	lib/mosquitto.h
	lib/net_mosq.c
	lib/net_mosq.h
	lib/send_client_mosq.c
	lib/send_mosq.c
	lib/socks_mosq.c
	lib/will_mosq.c
	src/bridge.c
	src/conf.c
	src/context.c
	src/database.c
	src/loop.c
	src/mosquitto.c
	src/mosquitto_broker.h
	src/net.c
	src/read_handle.c
	src/read_handle_server.c
	src/subs.c
2015-09-09 22:02:46 +01:00

109 lines
2.6 KiB
C

/*
Copyright (c) 2010-2015 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 v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
http://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
Contributors:
Roger Light - initial implementation and documentation.
*/
#include <stdio.h>
#include <string.h>
#include "mosquitto.h"
#include "mosquitto_internal.h"
#include "logging_mosq.h"
#include "messages_mosq.h"
#include "memory_mosq.h"
#include "mqtt3_protocol.h"
#include "net_mosq.h"
#include "read_handle.h"
#include "send_mosq.h"
#include "util_mosq.h"
int will__set(struct mosquitto *mosq, const char *topic, int payloadlen, const void *payload, int qos, bool retain)
{
int rc = MOSQ_ERR_SUCCESS;
if(!mosq || !topic) return MOSQ_ERR_INVAL;
if(payloadlen < 0 || payloadlen > MQTT_MAX_PAYLOAD) return MOSQ_ERR_PAYLOAD_SIZE;
if(payloadlen > 0 && !payload) return MOSQ_ERR_INVAL;
if(mosquitto_pub_topic_check(topic)) return MOSQ_ERR_INVAL;
if(mosq->will){
if(mosq->will->topic){
mosquitto__free(mosq->will->topic);
mosq->will->topic = NULL;
}
if(mosq->will->payload){
mosquitto__free(mosq->will->payload);
mosq->will->payload = NULL;
}
mosquitto__free(mosq->will);
mosq->will = NULL;
}
mosq->will = mosquitto__calloc(1, sizeof(struct mosquitto_message));
if(!mosq->will) return MOSQ_ERR_NOMEM;
mosq->will->topic = mosquitto__strdup(topic);
if(!mosq->will->topic){
rc = MOSQ_ERR_NOMEM;
goto cleanup;
}
mosq->will->payloadlen = payloadlen;
if(mosq->will->payloadlen > 0){
if(!payload){
rc = MOSQ_ERR_INVAL;
goto cleanup;
}
mosq->will->payload = mosquitto__malloc(sizeof(char)*mosq->will->payloadlen);
if(!mosq->will->payload){
rc = MOSQ_ERR_NOMEM;
goto cleanup;
}
memcpy(mosq->will->payload, payload, payloadlen);
}
mosq->will->qos = qos;
mosq->will->retain = retain;
return MOSQ_ERR_SUCCESS;
cleanup:
if(mosq->will){
mosquitto__free(mosq->will->topic);
mosquitto__free(mosq->will->payload);
}
mosquitto__free(mosq->will);
mosq->will = NULL;
return rc;
}
int will__clear(struct mosquitto *mosq)
{
if(!mosq->will) return MOSQ_ERR_SUCCESS;
if(mosq->will->topic){
mosquitto__free(mosq->will->topic);
mosq->will->topic = NULL;
}
if(mosq->will->payload){
mosquitto__free(mosq->will->payload);
mosq->will->payload = NULL;
}
mosquitto__free(mosq->will);
mosq->will = NULL;
return MOSQ_ERR_SUCCESS;
}