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

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 $
        !             6: * $Id: aitmqtt.c,v 1.3.4.4 2022/09/14 14:32:48 misho Exp $
        !             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.1 ! misho      92:        if (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:                }
                    116:                if (!mqtt_readPINGRESP(&msg)) {
                    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: 
1.4.2.1 ! misho     127: #if 0
1.3       misho     128: /*
                    129:  * mqtt_WillMessage() - Publish WILL message
                    130:  *
                    131:  * @sock = connected socket
                    132:  * @ka = keep alive timeout
                    133:  * @topic = will topic
                    134:  * @data = will message
                    135:  * return: -1 error, 1 timeout, 2 not ack or 0 ok
                    136:  */
                    137: int
                    138: mqtt_WillMessage(int sock, u_short ka, const char *topic, const char *data)
                    139: {
                    140:        int ret = 0;
                    141:        mqtt_msg_t msg = { NULL, 0 };
                    142: 
                    143:        if (!topic)
                    144:                return -1;      /* error */
                    145: 
                    146:        /* will message */
                    147:        if ((ret = mqtt_wait4data(sock, ka, POLLOUT)))
                    148:                return ret;
                    149:        ret = mqtt_msgPUBLISH(&msg, topic, 0xDEAD, 0, 1, 0, data, data ? strlen(data) : 0);
                    150:        if (ret == -1)
                    151:                return -1;      /* error */
                    152:        if ((ret = send(sock, msg.msg_base, ret, MSG_NOSIGNAL)) == -1) {
                    153:                LOGERR;
                    154:                free(msg.msg_base);
                    155:                return -1;      /* error */
                    156:        } else
                    157:                memset(msg.msg_base, 0, msg.msg_len);
                    158: 
                    159:        /* will ack */
                    160:        if ((ret = mqtt_wait4data(sock, ka, POLLIN | POLLPRI))) {
                    161:                free(msg.msg_base);
                    162:                return ret;
                    163:        }
                    164:        /* receive & decode packet */
                    165:        if ((ret = recv(sock, msg.msg_base, msg.msg_len, 0)) == -1) {
                    166:                LOGERR;
                    167:                free(msg.msg_base);
                    168:                return -1;      /* error */
                    169:        }
                    170:        if (mqtt_readPUBACK(&msg))
                    171:                ret = 0;        /* ok */
                    172:        else
                    173:                ret = 2;        /* semi-error */
                    174: 
                    175:        free(msg.msg_base);
                    176:        return ret;
                    177: }
1.4.2.1 ! misho     178: #endif

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