2014-05-07 22:27:00 +00:00
|
|
|
/*
|
2016-07-08 08:42:24 +00:00
|
|
|
Copyright (c) 2009-2016 Roger Light <roger@atchoo.org>
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
All rights reserved. This program and the accompanying materials
|
|
|
|
are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
and Eclipse Distribution License v1.0 which accompany this distribution.
|
|
|
|
|
|
|
|
The Eclipse Public License is available at
|
|
|
|
http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
and the Eclipse Distribution License is available at
|
|
|
|
http://www.eclipse.org/org/documents/edl-v10.php.
|
|
|
|
|
|
|
|
Contributors:
|
|
|
|
Roger Light - initial implementation and documentation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-29 20:37:47 +00:00
|
|
|
#include "mosquitto.h"
|
|
|
|
#include "mosquitto_internal.h"
|
|
|
|
#include "logging_mosq.h"
|
|
|
|
#include "mqtt3_protocol.h"
|
|
|
|
#include "memory_mosq.h"
|
|
|
|
#include "net_mosq.h"
|
|
|
|
#include "packet_mosq.h"
|
|
|
|
#include "send_mosq.h"
|
|
|
|
#include "time_mosq.h"
|
|
|
|
#include "util_mosq.h"
|
2014-05-07 22:27:00 +00:00
|
|
|
|
|
|
|
#ifdef WITH_BROKER
|
2016-07-08 09:10:04 +00:00
|
|
|
# include "mosquitto_broker_internal.h"
|
2015-05-16 11:25:35 +00:00
|
|
|
# include "sys_tree.h"
|
|
|
|
#else
|
|
|
|
# define G_PUB_BYTES_SENT_INC(A)
|
2014-05-07 22:27:00 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
int send__pingreq(struct mosquitto *mosq)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
assert(mosq);
|
|
|
|
#ifdef WITH_BROKER
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PINGREQ to %s", mosq->id);
|
2014-05-07 22:27:00 +00:00
|
|
|
#else
|
2015-05-18 07:53:21 +00:00
|
|
|
log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PINGREQ", mosq->id);
|
2014-05-07 22:27:00 +00:00
|
|
|
#endif
|
2015-05-16 14:24:24 +00:00
|
|
|
rc = send__simple_command(mosq, PINGREQ);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(rc == MOSQ_ERR_SUCCESS){
|
|
|
|
mosq->ping_t = mosquitto_time();
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
int send__pingresp(struct mosquitto *mosq)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
#ifdef WITH_BROKER
|
2015-05-18 07:53:21 +00:00
|
|
|
if(mosq) log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PINGRESP to %s", mosq->id);
|
2014-05-07 22:27:00 +00:00
|
|
|
#else
|
2015-05-18 07:53:21 +00:00
|
|
|
if(mosq) log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PINGRESP", mosq->id);
|
2014-05-07 22:27:00 +00:00
|
|
|
#endif
|
2015-05-16 14:24:24 +00:00
|
|
|
return send__simple_command(mosq, PINGRESP);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
int send__puback(struct mosquitto *mosq, uint16_t mid)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
#ifdef WITH_BROKER
|
2015-05-18 07:53:21 +00:00
|
|
|
if(mosq) log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBACK to %s (Mid: %d)", mosq->id, mid);
|
2014-05-07 22:27:00 +00:00
|
|
|
#else
|
2015-05-18 07:53:21 +00:00
|
|
|
if(mosq) log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBACK (Mid: %d)", mosq->id, mid);
|
2014-05-07 22:27:00 +00:00
|
|
|
#endif
|
2015-05-16 14:24:24 +00:00
|
|
|
return send__command_with_mid(mosq, PUBACK, mid, false);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
int send__pubcomp(struct mosquitto *mosq, uint16_t mid)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
#ifdef WITH_BROKER
|
2015-05-18 07:53:21 +00:00
|
|
|
if(mosq) log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBCOMP to %s (Mid: %d)", mosq->id, mid);
|
2014-05-07 22:27:00 +00:00
|
|
|
#else
|
2015-05-18 07:53:21 +00:00
|
|
|
if(mosq) log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBCOMP (Mid: %d)", mosq->id, mid);
|
2014-05-07 22:27:00 +00:00
|
|
|
#endif
|
2015-05-16 14:24:24 +00:00
|
|
|
return send__command_with_mid(mosq, PUBCOMP, mid, false);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
int send__pubrec(struct mosquitto *mosq, uint16_t mid)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
#ifdef WITH_BROKER
|
2015-05-18 07:53:21 +00:00
|
|
|
if(mosq) log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBREC to %s (Mid: %d)", mosq->id, mid);
|
2014-05-07 22:27:00 +00:00
|
|
|
#else
|
2015-05-18 07:53:21 +00:00
|
|
|
if(mosq) log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBREC (Mid: %d)", mosq->id, mid);
|
2014-05-07 22:27:00 +00:00
|
|
|
#endif
|
2015-05-16 14:24:24 +00:00
|
|
|
return send__command_with_mid(mosq, PUBREC, mid, false);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 14:24:24 +00:00
|
|
|
int send__pubrel(struct mosquitto *mosq, uint16_t mid)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
|
|
|
#ifdef WITH_BROKER
|
2015-05-18 07:53:21 +00:00
|
|
|
if(mosq) log__printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBREL to %s (Mid: %d)", mosq->id, mid);
|
2014-05-07 22:27:00 +00:00
|
|
|
#else
|
2015-05-18 07:53:21 +00:00
|
|
|
if(mosq) log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBREL (Mid: %d)", mosq->id, mid);
|
2014-05-07 22:27:00 +00:00
|
|
|
#endif
|
2015-05-16 14:24:24 +00:00
|
|
|
return send__command_with_mid(mosq, PUBREL|2, mid, false);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* For PUBACK, PUBCOMP, PUBREC, and PUBREL */
|
2015-05-16 14:24:24 +00:00
|
|
|
int send__command_with_mid(struct mosquitto *mosq, uint8_t command, uint16_t mid, bool dup)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2015-04-19 21:10:59 +00:00
|
|
|
struct mosquitto__packet *packet = NULL;
|
2014-05-07 22:27:00 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert(mosq);
|
2015-04-19 21:10:59 +00:00
|
|
|
packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet));
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!packet) return MOSQ_ERR_NOMEM;
|
|
|
|
|
|
|
|
packet->command = command;
|
|
|
|
if(dup){
|
|
|
|
packet->command |= 8;
|
|
|
|
}
|
|
|
|
packet->remaining_length = 2;
|
2015-05-16 13:16:40 +00:00
|
|
|
rc = packet__alloc(packet);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(rc){
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(packet);
|
2014-05-07 22:27:00 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
packet->payload[packet->pos+0] = MOSQ_MSB(mid);
|
|
|
|
packet->payload[packet->pos+1] = MOSQ_LSB(mid);
|
|
|
|
|
2015-05-16 13:16:40 +00:00
|
|
|
return packet__queue(mosq, packet);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* For DISCONNECT, PINGREQ and PINGRESP */
|
2015-05-16 14:24:24 +00:00
|
|
|
int send__simple_command(struct mosquitto *mosq, uint8_t command)
|
2014-05-07 22:27:00 +00:00
|
|
|
{
|
2015-04-19 21:10:59 +00:00
|
|
|
struct mosquitto__packet *packet = NULL;
|
2014-05-07 22:27:00 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert(mosq);
|
2015-04-19 21:10:59 +00:00
|
|
|
packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet));
|
2014-05-07 22:27:00 +00:00
|
|
|
if(!packet) return MOSQ_ERR_NOMEM;
|
|
|
|
|
|
|
|
packet->command = command;
|
|
|
|
packet->remaining_length = 0;
|
|
|
|
|
2015-05-16 13:16:40 +00:00
|
|
|
rc = packet__alloc(packet);
|
2014-05-07 22:27:00 +00:00
|
|
|
if(rc){
|
2015-04-19 21:10:59 +00:00
|
|
|
mosquitto__free(packet);
|
2014-05-07 22:27:00 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-05-16 13:16:40 +00:00
|
|
|
return packet__queue(mosq, packet);
|
2014-05-07 22:27:00 +00:00
|
|
|
}
|
|
|
|
|