From fa06f0cdacaa58ca20f6c99d351d209e6d731a04 Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Wed, 6 Mar 2019 01:31:26 +0100 Subject: [PATCH] MQTT protocol support going on, adding connack packet check. Compiles but still untested. --- include/lib/protocols/mqtt.h | 17 +++++++++++++---- lib/protocols/mqtt.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/include/lib/protocols/mqtt.h b/include/lib/protocols/mqtt.h index c2c458f..3ea54d2 100644 --- a/include/lib/protocols/mqtt.h +++ b/include/lib/protocols/mqtt.h @@ -37,6 +37,8 @@ * This code only handles the packet encoding and decoding according to the MQTT specification and * provides usage information in order to respect the protocol flow, but does not provide the communication * parts of the protocol, which should be application specific. + * This lets the application designer choose between any of the possible MQTT flow available, from single + * "in-flight" message window to a single server to multiple servers and complex message queues. * */ @@ -116,8 +118,8 @@ struct mqtt_connect_pkt { }; /* Create MQTT connect packet - * This function must be called in order to connectte the connect MQTT packet used to - * connect to the server. + * This function must be called in order to create the connect MQTT packet used to connect to the + * server. * The client must send a connect packet whenever the server does not acknoledge a publish, * subscribe, unsubscribe, or ping packet, or when the server explicitely closes the connection. * @@ -140,6 +142,7 @@ enum MQTT_connack_return_codes { MQTT_CONNACK_REFUSED_SERVER_UNAVAILABLE = 3, MQTT_CONNACK_REFUSED_BAD_USER_NAME_OR_PASSWORD = 4, MQTT_CONNACK_REFUSED_NOT_AUTHORIZED = 5, + MQTT_CONNACK_MAX, }; /* MQTT connack packet */ @@ -154,8 +157,14 @@ struct mqtt_connack_response_pkt { * the server accepts the connection and has a stored session for this client id */ #define MQTT_CONNACK_SESSION_PRESENT (0x01 << 0) -/* Check and Decode MQTT connack packet */ -int mqtt_unpackconnack_response_pkt(uint8_t* buf, uint8_t buf_size); +/* Check MQTT connack packet + * This function may get called to check a supposed connect acknowledge packet. + * The function checks for conformance to the MQTT protocol and returns 0 if the packet is valid and + * the server accepted the connection. + * The function returns -EPROTO in case of invalid connack packet or -EREMOTEIO in case of connection + * refused by the server (ret_code is then valid in the packet). + */ +int mqtt_check_connack_response_pkt(struct mqtt_connack_response_pkt* pkt); /***************************************************************************** */ /* publish and puback packets */ diff --git a/lib/protocols/mqtt.c b/lib/protocols/mqtt.c index 861fecd..690ba9e 100644 --- a/lib/protocols/mqtt.c +++ b/lib/protocols/mqtt.c @@ -195,3 +195,34 @@ int mqtt_pack_connect_packet(struct mqtt_connect_pkt* pkt, uint8_t* buf, uint32_ } +/***************************************************************************** */ +/* Check MQTT connack packet + * This function may get called to check a supposed connect acknowledge packet. + * The function checks for conformance to the MQTT protocol and returns 0 if the packet is valid, + * regardless of the retrun code value. + * The function returns -EPROTO in case of protocol error. + */ +int mqtt_check_connack_response_pkt(struct mqtt_connack_response_pkt* pkt) +{ + if (pkt->control != (MQTT_CONTROL_CONNACK << 4)) { + return -EPROTO; + } + if (pkt->rem_len != 2) { + return -EPROTO; + } + if (pkt->flags & 0xFE) { + return -EPROTO; + } + if (pkt->ret_code >= MQTT_CONNACK_MAX) { + return -EPROTO; + } + if (pkt->ret_code != 0) { + return -EREMOTEIO; + } + return 0; +} + + + +/***************************************************************************** */ + -- 2.43.0