Skip to content

Commit

Permalink
Server Max Packet size and Keep Alive
Browse files Browse the repository at this point in the history
  • Loading branch information
embhorn committed Aug 3, 2018
1 parent 2c520a1 commit d37c130
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ AM_CONDITIONAL([BUILD_STDINCAP], [test "x$ENABLED_STDINCAP" = "xyes"])

# MQTT v5.0
AC_ARG_ENABLE([mqtt5],
[ --enable-mqtt5 Enable MQTT v5.0 support (default: disabled)],
[AS_HELP_STRING([--enable-mqtt5],[Enable MQTT v5.0 support (default: disabled)])],
[ ENABLED_MQTTV50=$enableval ],
[ ENABLED_MQTTV50=no ]
)
Expand Down
20 changes: 18 additions & 2 deletions examples/mqttclient/mqttclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,34 @@ static int mqtt_property_cb(MqttClient *client, MqttProp *head, void *ctx)
(((MQTTCtx*)client->ctx)->topic_alias_max < prop->data_short) ?
((MQTTCtx*)client->ctx)->topic_alias_max : prop->data_short;
break;
case MQTT_PROP_MAX_PACKET_SZ:
if ((prop->data_int > 0) &&
(prop->data_int <= MQTT_PACKET_SZ_MAX))
{
client->packet_sz_max =
(client->packet_sz_max < prop->data_int) ?
client->packet_sz_max : prop->data_int;
}
else {
/* Protocol error */
rc = MQTT_CODE_ERROR_PROPERTY;
}
break;
case MQTT_PROP_SERVER_KEEP_ALIVE:
((MQTTCtx*)client->ctx)->keep_alive_sec = prop->data_short;
break;
case MQTT_PROP_PLAYLOAD_FORMAT_IND:
case MQTT_PROP_MSG_EXPIRY_INTERVAL:
case MQTT_PROP_CONTENT_TYPE:
case MQTT_PROP_RESP_TOPIC:
case MQTT_PROP_CORRELATION_DATA:
case MQTT_PROP_SUBSCRIPTION_ID:
case MQTT_PROP_SESSION_EXPIRY_INTERVAL:
case MQTT_PROP_SERVER_KEEP_ALIVE:
case MQTT_PROP_TOPIC_ALIAS:
case MQTT_PROP_TYPE_MAX:
case MQTT_PROP_RECEIVE_MAX:
case MQTT_PROP_MAX_QOS:
case MQTT_PROP_RETAIN_AVAIL:
case MQTT_PROP_MAX_PACKET_SZ:
case MQTT_PROP_REASON_STR:
case MQTT_PROP_USER_PROP:
case MQTT_PROP_WILDCARD_SUB_AVAIL:
Expand Down Expand Up @@ -284,6 +298,8 @@ int mqttclient_test(MQTTCtx *mqttCtx)
mqttCtx->connect.username = mqttCtx->username;
mqttCtx->connect.password = mqttCtx->password;
#ifdef WOLFMQTT_V5
mqttCtx->client.packet_sz_max = mqttCtx->max_packet_size;

if (mqttCtx->enable_eauth == 1)
{
/* Enhanced authentication */
Expand Down
2 changes: 1 addition & 1 deletion src/mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ int MqttClient_SetPropertyCallback(MqttClient *client, MqttPropertyCb cb,

int MqttClient_Connect(MqttClient *client, MqttConnect *connect)
{
int rc, len;
int rc, len = 0;

/* Validate required arguments */
if (client == NULL || connect == NULL) {
Expand Down
12 changes: 11 additions & 1 deletion src/mqtt_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,17 @@ static int MqttPacket_HandleNetError(MqttClient *client, int rc)
int MqttPacket_Write(MqttClient *client, byte* tx_buf, int tx_buf_len)
{
int rc;
rc = MqttSocket_Write(client, tx_buf, tx_buf_len, client->cmd_timeout_ms);
#ifdef WOLFMQTT_V5
if ((client->packet_sz_max > 0) && (tx_buf_len >
(int)client->packet_sz_max))
{
rc = MQTT_CODE_ERROR_PAK_SIZE;
}
else
#endif
{
rc = MqttSocket_Write(client, tx_buf, tx_buf_len, client->cmd_timeout_ms);
}

return MqttPacket_HandleNetError(client, rc);
}
Expand Down
4 changes: 4 additions & 0 deletions wolfmqtt/mqtt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ typedef struct _MqttClient {

void* ctx; /* user supplied context for publish callbacks */

#ifdef WOLFMQTT_V5
word32 packet_sz_max; /* Server property */
#endif

#ifdef WOLFMQTT_DISCONNECT_CB
MqttDisconnectCb disconnect_cb;
void *disconnect_ctx;
Expand Down
4 changes: 3 additions & 1 deletion wolfmqtt/mqtt_packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
#define MQTT_DATA_LEN_SIZE 2
#define MQTT_DATA_INT_SIZE 4


#ifdef WOLFMQTT_V5

#define MQTT_PACKET_SZ_MAX 0xA0000005

/* DATA TYPES */
typedef enum MqttDataType {
MQTT_DATA_TYPE_BYTE,
Expand Down
1 change: 1 addition & 0 deletions wolfmqtt/mqtt_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ enum MqttPacketResponseCodes {
MQTT_CODE_ERROR_MEMORY = -9,
MQTT_CODE_ERROR_STAT = -10,
MQTT_CODE_ERROR_PROPERTY = -11,
MQTT_CODE_ERROR_PAK_SIZE = -12,

MQTT_CODE_CONTINUE = -101,
MQTT_CODE_STDIN_WAKE = -102,
Expand Down

0 comments on commit d37c130

Please sign in to comment.