Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / protocol-plugin / plugins / mqtt-light / lib / will_mosq.c
1 /*
2 Copyright (c) 2010-2013 Roger Light <roger@atchoo.org>
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9    this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. Neither the name of mosquitto nor the names of its
14    contributors may be used to endorse or promote products derived from
15    this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <assert.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <string.h>
34 #ifndef WIN32
35 #include <sys/select.h>
36 #include <unistd.h>
37 #else
38 #include <winsock2.h>
39 typedef int ssize_t;
40 #endif
41
42 #include "mosquitto.h"
43 #include "mosquitto_internal.h"
44 #include "logging_mosq.h"
45 #include "messages_mosq.h"
46 #include "memory_mosq.h"
47 #include "mqtt3_protocol.h"
48 #include "net_mosq.h"
49 #include "read_handle.h"
50 #include "send_mosq.h"
51 #include "util_mosq.h"
52
53 int _mosquitto_will_set(struct mosquitto *mosq, const char *topic, int payloadlen, const void *payload, int qos, bool retain)
54 {
55         int rc = MOSQ_ERR_SUCCESS;
56
57         if(!mosq || !topic) return MOSQ_ERR_INVAL;
58         if(payloadlen < 0 || payloadlen > MQTT_MAX_PAYLOAD) return MOSQ_ERR_PAYLOAD_SIZE;
59         if(payloadlen > 0 && !payload) return MOSQ_ERR_INVAL;
60
61         if(_mosquitto_topic_wildcard_pos_check(topic)) return MOSQ_ERR_INVAL;
62
63         if(mosq->will){
64                 if(mosq->will->topic){
65                         _mosquitto_free(mosq->will->topic);
66                         mosq->will->topic = NULL;
67                 }
68                 if(mosq->will->payload){
69                         _mosquitto_free(mosq->will->payload);
70                         mosq->will->payload = NULL;
71                 }
72                 _mosquitto_free(mosq->will);
73                 mosq->will = NULL;
74         }
75
76         mosq->will = _mosquitto_calloc(1, sizeof(struct mosquitto_message));
77         if(!mosq->will) return MOSQ_ERR_NOMEM;
78         mosq->will->topic = _mosquitto_strdup(topic);
79         if(!mosq->will->topic){
80                 rc = MOSQ_ERR_NOMEM;
81                 goto cleanup;
82         }
83         mosq->will->payloadlen = payloadlen;
84         if(mosq->will->payloadlen > 0){
85                 if(!payload){
86                         rc = MOSQ_ERR_INVAL;
87                         goto cleanup;
88                 }
89                 mosq->will->payload = _mosquitto_malloc(sizeof(char)*mosq->will->payloadlen);
90                 if(!mosq->will->payload){
91                         rc = MOSQ_ERR_NOMEM;
92                         goto cleanup;
93                 }
94
95                 memcpy(mosq->will->payload, payload, payloadlen);
96         }
97         mosq->will->qos = qos;
98         mosq->will->retain = retain;
99
100         return MOSQ_ERR_SUCCESS;
101
102 cleanup:
103         if(mosq->will){
104                 if(mosq->will->topic) _mosquitto_free(mosq->will->topic);
105                 if(mosq->will->payload) _mosquitto_free(mosq->will->payload);
106         }
107         _mosquitto_free(mosq->will);
108         mosq->will = NULL;
109
110         return rc;
111 }
112
113 int _mosquitto_will_clear(struct mosquitto *mosq)
114 {
115         if(!mosq->will) return MOSQ_ERR_SUCCESS;
116
117         if(mosq->will->topic){
118                 _mosquitto_free(mosq->will->topic);
119                 mosq->will->topic = NULL;
120         }
121         if(mosq->will->payload){
122                 _mosquitto_free(mosq->will->payload);
123                 mosq->will->payload = NULL;
124         }
125         _mosquitto_free(mosq->will);
126         mosq->will = NULL;
127
128         return MOSQ_ERR_SUCCESS;
129 }
130