File:  [ELWIX - Embedded LightWeight unIX -] / libaitmqtt / src / sub.c
Revision 1.3.12.4: download - view: text, annotated - select for diffs - revision graph
Fri Sep 16 04:14:03 2022 UTC (21 months, 2 weeks ago) by misho
Branches: mqtt1_8
Diff to: branchpoint 1.3: preferred, unified
fix issue with subscribers

/*************************************************************************
* (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
*  by Michael Pounov <misho@openbsd-bg.org>
*
* $Author: misho $
* $Id: sub.c,v 1.3.12.4 2022/09/16 04:14:03 misho Exp $
*
**************************************************************************
The ELWIX and AITNET software is distributed under the following
terms:

All of the documentation and software included in the ELWIX and AITNET
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>

Copyright 2004 - 2022
	by Michael Pounov <misho@elwix.org>.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
This product includes software developed by Michael Pounov <misho@elwix.org>
ELWIX - Embedded LightWeight unIX and its contributors.
4. Neither the name of AITNET nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "global.h"


/*
 * mqtt_msgSUBSCRIBE() Create SUBSCRIBE message
 *
 * @Topics = MQTT subscription topics
 * @msgID = MessageID
 * return: NULL error or allocated SUBSCRIBE message
 */
mqtt_msg_t *
mqtt_msgSUBSCRIBE(mqtt_subscr_t * __restrict Topics, u_short msgID)
{
	int len, siz;
	u_int n, *l;
	struct mqtthdr *hdr;
	mqtthdr_var_t *topic;
	mqtt_len_t *mid;
	mqtt_subscr_t *t;
	void *data;
	mqtt_msg_t *msg = NULL;

	if (!Topics)
		return NULL;
	if (!msgID) {
		mqtt_SetErr(EINVAL, "Invalid MessageID parameter must be >0");
		return NULL;
	}

	/* calculate message size */
	len = sizeof(mqtt_len_t);				/* msgid */
	for (t = Topics; t && t->sub_topic.msg_base; t++)	/* subscribes & qos */
		len += sizeof(mqtt_len_t) + t->sub_topic.msg_len + 1;

	/* calculate header size */
	siz = sizeof(struct mqtthdr);				/* mqtt fixed header */
	n = mqtt_encodeLen(len);				/* message size */
	siz += mqtt_sizeLen(n) - 1;				/* length size */

	if (!(msg = mqtt_msgAlloc(siz + len)))
		return NULL;
	else {
		data = msg->msg_base;
		hdr = (struct mqtthdr *) data;
	}

	/* fixed header */
	hdr->mqtt_msg.type = MQTT_TYPE_SUBSCRIBE;
	hdr->mqtt_msg.qos = MQTT_QOS_ACK;
	l = (u_int*) hdr->mqtt_len;
	*l = n;
	data += siz;

	/* variable header */
	mid = (mqtt_len_t*) data;
	mid->val = htons(msgID);
	data += sizeof(mqtt_len_t);

	/* payload with subscriptions */
	for (t = Topics; t && t->sub_topic.msg_base; t++) {
		topic = (mqtthdr_var_t*) data;
		topic->var_sb.val = htons(t->sub_topic.msg_len);
		memcpy(topic->var_data, t->sub_topic.msg_base, ntohs(topic->var_sb.val));
		data += MQTTHDR_VAR_SIZEOF(topic);
		*((char*) data) = t->sub_qos;
		data++;
	}

	return msg;
}

/*
 * mqtt_msgSUBACK() Create SUBACK message
 *
 * @Topics = MQTT subscription topics
 * @msgID = MessageID
 * return: NULL error or allocated SUBACK message
 */
mqtt_msg_t *
mqtt_msgSUBACK(mqtt_subscr_t * __restrict Topics, u_short msgID)
{
	int siz = 0;
	struct mqtthdr *hdr;
	mqtt_len_t *v;
	mqtt_subscr_t *t;
	u_char *qos;
	mqtt_msg_t *msg = NULL;

	if (!Topics)
		return NULL;

	if (!(msg = mqtt_msgAlloc(MQTTMSG_MAX)))
		return NULL;
	else {
		hdr = (struct mqtthdr *) msg->msg_base;
		siz = sizeof(struct mqtthdr);
		v = (mqtt_len_t*) (msg->msg_base + siz);
		siz += sizeof(mqtt_len_t);
	}

	/* MessageID */
	v->val = htons(msgID);

	/* QoS payload from subscriptions */
	for (t = Topics; t && t->sub_topic.msg_base; t++, siz++) {
		qos = (msg->msg_base + siz);
		*qos = t->sub_qos;
	}

	/* fixed header */
	hdr->mqtt_msg.type = MQTT_TYPE_SUBACK;
	*hdr->mqtt_len = mqtt_encodeLen(siz - sizeof(struct mqtthdr));

	return msg;
}

/*
 * mqtt_msgUNSUBSCRIBE() Create UNSUBSCRIBE message
 *
 * @Topics = MQTT subscription topics
 * @msgID = MessageID
 * @Dup = Duplicate message
 * @QOS = QoS
 * return: NULL error or allocated UNSUBSCRIBE message
 */
mqtt_msg_t *
mqtt_msgUNSUBSCRIBE(mqtt_subscr_t * __restrict Topics, u_short msgID, 
		u_char Dup, u_char QOS)
{
	int len, siz = 0;
	u_int n, *l;
	struct mqtthdr *hdr;
	mqtthdr_var_t *topic;
	mqtt_len_t *mid;
	mqtt_subscr_t *t;
	void *data;
	mqtt_msg_t *msg = NULL;

	if (!Topics)
		return NULL;
	if (QOS > MQTT_QOS_EXACTLY) {
		mqtt_SetErr(EINVAL, "Invalid QoS parameter");
		return NULL;
	}
	if (!msgID && QOS != MQTT_QOS_ONCE) {
		mqtt_SetErr(EINVAL, "Invalid MessageID parameter must be >0");
		return NULL;
	}

	/* calculate message size */
	len = sizeof(mqtt_len_t);				/* msgid */
	for (t = Topics; t && t->sub_topic.msg_base; t++)	/* subscribes */
		len += sizeof(mqtt_len_t) + t->sub_topic.msg_len;

	/* calculate header size */
	siz = sizeof(struct mqtthdr);				/* mqtt fixed header */
	n = mqtt_encodeLen(len);				/* message size */
	siz += mqtt_sizeLen(n) - 1;				/* length size */

	if (!(msg = mqtt_msgAlloc(siz + len)))
		return NULL;
	else {
		data = msg->msg_base;
		hdr = (struct mqtthdr *) data;
	}

	/* fixed header */
	hdr->mqtt_msg.type = MQTT_TYPE_UNSUBSCRIBE;
	hdr->mqtt_msg.qos = QOS;
	hdr->mqtt_msg.dup = Dup ? 1 : 0;
	hdr->mqtt_msg.retain = 0;
	l = (u_int*) hdr->mqtt_len;
	*l = n;
	data += siz;

	/* variable header */
	mid = (mqtt_len_t*) (msg->msg_base + siz);
	mid->val = htons(msgID);
	data += sizeof(mqtt_len_t);

	/* payload with subscriptions */
	for (t = Topics; t && t->sub_topic.msg_base; t++) {
		topic = (mqtthdr_var_t*) data;
		topic->var_sb.val = htons(t->sub_topic.msg_len);
		memcpy(topic->var_data, t->sub_topic.msg_base, ntohs(topic->var_sb.val));
		data += MQTTHDR_VAR_SIZEOF(topic);
	}

	return msg;
}

/*
 * mqtt_msgUNSUBACK() Create UNSUBACK message
 *
 * @msgID = MessageID
 * return: NULL error or allocated UNSUBACK message
 */
mqtt_msg_t *
mqtt_msgUNSUBACK(u_short msgID)
{
	struct mqtthdr *hdr;
	mqtt_len_t *v;
	mqtt_msg_t *msg = NULL;

	if (!(msg = mqtt_msgAlloc(sizeof(struct mqtthdr) + sizeof(mqtt_len_t))))
		return NULL;
	else {
		hdr = (struct mqtthdr *) msg->msg_base;
		v = (mqtt_len_t*) (msg->msg_base + sizeof(struct mqtthdr));
	}

	/* fixed header */
	hdr->mqtt_msg.type = MQTT_TYPE_UNSUBACK;
	*hdr->mqtt_len = sizeof(mqtt_len_t);

	/* MessageID */
	v->val = htons(msgID);

	return msg;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>