2014-05-07 22:27:00 +00:00
|
|
|
/*
|
2015-04-19 21:10:59 +00:00
|
|
|
Copyright (c) 2010-2015 Roger Light <roger@atchoo.org>
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
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>
|
|
|
|
|
2015-04-29 20:37:47 +00:00
|
|
|
#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"
|
2014-05-07 22:27:00 +00:00
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
int will__set(struct mosquitto *mosq, const char *topic, int payloadlen, const void *payload, int qos, bool retain)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2014-09-10 14:57:20 +00:00
|
|
|
if(mosquitto_pub_topic_check(topic)) return MOSQ_ERR_INVAL;
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
if(mosq->will){
|
|
|
|
if(mosq->will->topic){
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(mosq->will->topic);
|
2014-05-07 22:27:00 +00:00
|
|
|
mosq->will->topic = NULL;
|
|
|
|
}
|
|
|
|
if(mosq->will->payload){
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(mosq->will->payload);
|
2014-05-07 22:27:00 +00:00
|
|
|
mosq->will->payload = NULL;
|
|
|
|
}
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(mosq->will);
|
2014-05-07 22:27:00 +00:00
|
|
|
mosq->will = NULL;
|
|
|
|
}
|
|
|
|
|
2015-04-19 21:10:59 +00:00
|
|
|
mosq->will = mosquitto__calloc(1, sizeof(struct mosquitto_message));
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!mosq->will) return MOSQ_ERR_NOMEM;
|
2015-04-19 21:10:59 +00:00
|
|
|
mosq->will->topic = mosquitto__strdup(topic);
|
2014-05-07 22:27:00 +00:00
|
|
|
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;
|
|
|
|
}
|
2015-04-19 21:10:59 +00:00
|
|
|
mosq->will->payload = mosquitto__malloc(sizeof(char)*mosq->will->payloadlen);
|
2014-05-07 22:27:00 +00:00
|
|
|
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){
|
2015-08-18 13:53:22 +00:00
|
|
|
mosquitto__free(mosq->will->topic);
|
|
|
|
mosquitto__free(mosq->will->payload);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(mosq->will);
|
2014-05-07 22:27:00 +00:00
|
|
|
mosq->will = NULL;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
int will__clear(struct mosquitto *mosq)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
if(!mosq->will) return MOSQ_ERR_SUCCESS;
|
|
|
|
|
|
|
|
if(mosq->will->topic){
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(mosq->will->topic);
|
2014-05-07 22:27:00 +00:00
|
|
|
mosq->will->topic = NULL;
|
|
|
|
}
|
|
|
|
if(mosq->will->payload){
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(mosq->will->payload);
|
2014-05-07 22:27:00 +00:00
|
|
|
mosq->will->payload = NULL;
|
|
|
|
}
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(mosq->will);
|
2014-05-07 22:27:00 +00:00
|
|
|
mosq->will = NULL;
|
|
|
|
|
|
|
|
return MOSQ_ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|