static int readPacket(MQTTClient* c, Timer* timer)
{
	int rc = FAILURE;
	MQTTHeader header = {0};
	int len = 0;
	int rem_len = 0;

#ifdef CONFIG_AXTLS
	if(!(len = c->ipstack->mqttread(c->ipstack, c->readbuf, c->readbuf_size, TimerLeftMS(timer))))
		goto exit;
#else
	/* 1. read the header byte.  This has the packet type in it */
	if (c->ipstack->mqttread(c->ipstack, c->readbuf, 1, TimerLeftMS(timer)) != 1)
		goto exit;

	len = 1;
	/* 2. read the remaining length.  This is variable in itself */
	decodePacket(c, &rem_len, TimerLeftMS(timer));

	len += MQTTPacket_encode(c->readbuf + 1, rem_len); /* put the original remaining length back into the buffer */

	/* 3. read the rest of the buffer using a callback to supply the rest of the data */
	if (rem_len > 0 && (c->ipstack->mqttread(c->ipstack, c->readbuf + len, rem_len, TimerLeftMS(timer)) != rem_len))
		goto exit;
#endif

	header.byte = c->readbuf[0];
	rc = header.bits.type;
exit:
	return rc;
}
示例#2
0
static int readPacket(MQTTClient *c, Timer *timer)
{
    MQTTHeader header = {0};
    int len = 0;
    int rem_len = 0;

    /* 1. read the header byte.  This has the packet type in it */
    int rc = c->ipstack->mqttread(c->ipstack, c->readbuf, 1, TimerLeftMS(timer));

    if (rc != 1) {
        goto exit;
    }

    len = 1;
    /* 2. read the remaining length.  This is variable in itself */
    decodePacket(c, &rem_len, TimerLeftMS(timer));
    len += MQTTPacket_encode(c->readbuf + 1, rem_len); /* put the original remaining length back into the buffer */

    if (rem_len > (c->readbuf_size - len)) {
        rc = BUFFER_OVERFLOW;
        goto exit;
    }

    /* 3. read the rest of the buffer using a callback to supply the rest of the data */
    if (rem_len > 0 && (rc = c->ipstack->mqttread(c->ipstack, c->readbuf + len, rem_len, TimerLeftMS(timer)) != rem_len)) {
        rc = 0;
        goto exit;
    }

    header.byte = c->readbuf[0];
    rc = header.bits.type;

    if (c->keepAliveInterval > 0) {
        TimerCountdown(&c->last_received, c->keepAliveInterval);    // record the fact that we have successfully received a packet
    }

exit:
    return rc;
}
static int sendPacket(MQTTClient* c, int length, Timer* timer)
{
	int rc = FAILURE, sent = 0;

	while ((sent < length) && !TimerIsExpired(timer)) {
		rc = c->ipstack->mqttwrite(c->ipstack, &c->buf[sent], length, TimerLeftMS(timer));
		if (rc < 0)
			break;
		sent += rc;
	}
	/*
		The send progress only send the packet to the socket buffer
		we dont know if it successeds, default it is SUCCESS
	*/

	if (sent == length) {		
		rc = SUCCESS;
	} else {
		rc = FAILURE;
	}

	return rc;
}
示例#4
0
static int sendPacket(MQTTClient *c, int length, Timer *timer)
{
    int rc = FAILURE,
        sent = 0;

    while (sent < length && !TimerIsExpired(timer)) {
        rc = c->ipstack->mqttwrite(c->ipstack, &c->buf[sent], length, TimerLeftMS(timer));

        if (rc < 0) { // there was an error writing the data
            break;
        }

        sent += rc;
    }

    if (sent == length) {
        TimerCountdown(&c->last_sent, c->keepAliveInterval); // record the fact that we have successfully sent the packet
        rc = SUCCESS;
    } else {
        rc = FAILURE;
    }

    return rc;
}