Annotation of libaitmqtt/src/cmds.c, revision 1.4.2.2

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.2 ! misho       6: * $Id: cmds.c,v 1.4.2.1 2022/09/15 15:04:44 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);
                    112:                if ((ret = recv(sock, msg->msg_base, msg->msg_len, 0)) == -1) {
1.2       misho     113:                        LOGERR;
                    114:                        break;
                    115:                }
1.4.2.2 ! misho     116:                if (!mqtt_readPINGRESP(msg)) {
1.2       misho     117:                        ret = 0;        /* Host is alive */
                    118:                        break;
                    119:                } else
                    120:                        ret = 2;        /* Session is broken ... must be disconnect! */
                    121:        }
                    122: end:
1.4.2.1   misho     123:        mqtt_msgFree(&msg, 0);
1.2       misho     124:        return ret;
                    125: }
1.3       misho     126: 
                    127: /*
                    128:  * mqtt_WillMessage() - Publish WILL message
                    129:  *
                    130:  * @sock = connected socket
                    131:  * @ka = keep alive timeout
                    132:  * @topic = will topic
                    133:  * @data = will message
                    134:  * return: -1 error, 1 timeout, 2 not ack or 0 ok
                    135:  */
                    136: int
                    137: mqtt_WillMessage(int sock, u_short ka, const char *topic, const char *data)
                    138: {
                    139:        int ret = 0;
1.4.2.2 ! misho     140:        mqtt_msg_t *msg = NULL;
1.3       misho     141: 
                    142:        if (!topic)
                    143:                return -1;      /* error */
                    144: 
                    145:        /* will message */
                    146:        if ((ret = mqtt_wait4data(sock, ka, POLLOUT)))
                    147:                return ret;
1.4.2.2 ! misho     148:        msg = mqtt_msgPUBLISH(topic, 0xDEAD, 0, 1, 0, data, data ? strlen(data) : 0);
        !           149:        if (!msg)
1.3       misho     150:                return -1;      /* error */
1.4.2.2 ! misho     151:        if ((ret = send(sock, msg->msg_base, msg->msg_len, MSG_NOSIGNAL)) == -1) {
1.3       misho     152:                LOGERR;
1.4.2.2 ! misho     153:                mqtt_msgFree(&msg, 0);
1.3       misho     154:                return -1;      /* error */
                    155:        } else
1.4.2.2 ! misho     156:                mqtt_msgFree(&msg, 0);
1.3       misho     157: 
                    158:        /* will ack */
1.4.2.2 ! misho     159:        if ((ret = mqtt_wait4data(sock, ka, POLLIN | POLLPRI)))
1.3       misho     160:                return ret;
                    161:        /* receive & decode packet */
1.4.2.2 ! misho     162:        msg = mqtt_msgAlloc(BUFSIZ);
        !           163:        if ((ret = recv(sock, msg->msg_base, msg->msg_len, 0)) == -1) {
1.3       misho     164:                LOGERR;
1.4.2.2 ! misho     165:                mqtt_msgFree(&msg, 0);
1.3       misho     166:                return -1;      /* error */
                    167:        }
1.4.2.2 ! misho     168:        if (mqtt_readPUBACK(msg))
1.3       misho     169:                ret = 0;        /* ok */
                    170:        else
                    171:                ret = 2;        /* semi-error */
                    172: 
1.4.2.2 ! misho     173:        mqtt_msgFree(&msg, 0);
1.3       misho     174:        return ret;
                    175: }

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