Annotation of libaitmqtt/src/cmds.c, revision 1.4.2.3
1.4.2.1 misho 1: /*************************************************************************
2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@elwix.org>
4: *
5: * $Author: misho $
1.4.2.3 ! misho 6: * $Id: cmds.c,v 1.4.2.2 2022/09/15 15:13:31 misho Exp $
1.4.2.1 misho 7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
15: Copyright 2004 - 2022
16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
1.2 misho 46: #include "global.h"
47:
48:
49: #pragma GCC visibility push(hidden)
50:
1.4 misho 51: int
1.2 misho 52: mqtt_wait4data(int sock, u_short ka, short events)
53: {
54: int ret = 0;
55: struct pollfd pfd;
56:
57: if (sock < 3)
58: return -1; /* error */
59:
60: pfd.fd = sock;
61: pfd.events = POLLOUT;
62: if ((ret = poll(&pfd, 1, ka * 1000)) == -1 ||
63: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
64: LOGERR;
65: return -1; /* error */
66: } else if (!ret)
67: return 1; /* timeout */
68:
69: return 0; /* ready */
70: }
71:
72: #pragma GCC visibility pop
73:
74:
75: /*
76: * mqtt_KeepAlive() - Keep Alive check routine
77: *
78: * @sock = connected socket
79: * @ka = keep alive timeout
80: * @tries = tries for receive correct ping response, usually ==1
81: * return: -1 error, 0 host is alive, 1 timeout session or 2 broken session
82: */
83: int
84: mqtt_KeepAlive(int sock, u_short ka, u_char tries)
85: {
86: int ret = 0;
1.4.2.1 misho 87: mqtt_msg_t *msg = NULL;
1.2 misho 88:
89: if (sock < 3)
90: return -1; /* error */
91:
1.4.2.2 misho 92: if ((ret = mqtt_wait4data(sock, ka, POLLOUT)))
1.2 misho 93: return ret;
94: /* ping request */
1.4.2.1 misho 95: if (!(msg = mqtt_msgPINGREQ()))
1.2 misho 96: return -1; /* error */
1.4.2.1 misho 97: if ((ret = send(sock, msg->msg_base, msg->msg_len, MSG_NOSIGNAL)) == -1) {
1.2 misho 98: LOGERR;
99: goto end;
1.4.2.1 misho 100: } else
101: mqtt_msgFree(&msg, 0);
1.2 misho 102:
103: while (tries--) {
104: if ((ret = mqtt_wait4data(sock, ka, POLLIN | POLLPRI))) {
105: if (ret == -1)
106: break;
107: else
108: continue;
109: }
110: /* receive & decode packet */
1.4.2.1 misho 111: msg = mqtt_msgAlloc(BUFSIZ);
1.4.2.3 ! misho 112: if (!msg) {
! 113: ret = -1;
! 114: break;
! 115: }
1.4.2.1 misho 116: if ((ret = recv(sock, msg->msg_base, msg->msg_len, 0)) == -1) {
1.2 misho 117: LOGERR;
118: break;
119: }
1.4.2.2 misho 120: if (!mqtt_readPINGRESP(msg)) {
1.2 misho 121: ret = 0; /* Host is alive */
122: break;
123: } else
124: ret = 2; /* Session is broken ... must be disconnect! */
1.4.2.3 ! misho 125: mqtt_msgFree(&msg, 0);
1.2 misho 126: }
127: end:
1.4.2.1 misho 128: mqtt_msgFree(&msg, 0);
1.2 misho 129: return ret;
130: }
1.3 misho 131:
132: /*
133: * mqtt_WillMessage() - Publish WILL message
134: *
135: * @sock = connected socket
136: * @ka = keep alive timeout
137: * @topic = will topic
138: * @data = will message
139: * return: -1 error, 1 timeout, 2 not ack or 0 ok
140: */
141: int
142: mqtt_WillMessage(int sock, u_short ka, const char *topic, const char *data)
143: {
144: int ret = 0;
1.4.2.2 misho 145: mqtt_msg_t *msg = NULL;
1.3 misho 146:
147: if (!topic)
148: return -1; /* error */
149:
150: /* will message */
151: if ((ret = mqtt_wait4data(sock, ka, POLLOUT)))
152: return ret;
1.4.2.2 misho 153: msg = mqtt_msgPUBLISH(topic, 0xDEAD, 0, 1, 0, data, data ? strlen(data) : 0);
154: if (!msg)
1.3 misho 155: return -1; /* error */
1.4.2.2 misho 156: if ((ret = send(sock, msg->msg_base, msg->msg_len, MSG_NOSIGNAL)) == -1) {
1.3 misho 157: LOGERR;
1.4.2.2 misho 158: mqtt_msgFree(&msg, 0);
1.3 misho 159: return -1; /* error */
160: } else
1.4.2.2 misho 161: mqtt_msgFree(&msg, 0);
1.3 misho 162:
163: /* will ack */
1.4.2.2 misho 164: if ((ret = mqtt_wait4data(sock, ka, POLLIN | POLLPRI)))
1.3 misho 165: return ret;
166: /* receive & decode packet */
1.4.2.2 misho 167: msg = mqtt_msgAlloc(BUFSIZ);
1.4.2.3 ! misho 168: if (!msg)
! 169: return -1;
1.4.2.2 misho 170: if ((ret = recv(sock, msg->msg_base, msg->msg_len, 0)) == -1) {
1.3 misho 171: LOGERR;
1.4.2.2 misho 172: mqtt_msgFree(&msg, 0);
1.3 misho 173: return -1; /* error */
174: }
1.4.2.2 misho 175: if (mqtt_readPUBACK(msg))
1.3 misho 176: ret = 0; /* ok */
177: else
178: ret = 2; /* semi-error */
1.4.2.2 misho 179: mqtt_msgFree(&msg, 0);
1.4.2.3 ! misho 180:
1.3 misho 181: return ret;
182: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>