1
0
mirror of https://github.com/FreeRTOS/coreMQTT synced 2025-10-21 06:03:12 +08:00
Files
coreMQTT/source/core_mqtt_utils.c
Aditi-Chaudhary a4fad60fce MQTTv5 Implementation (#316)
MQTTv5 Library preview

Description
-----------
- This change adds the client implementation of MQTT version 5 CONNECT,
CONNACK, Outgoing PUBLISH, PUBLISH ACKS, Incoming PUBLISH, SUBSCRIBE,
SUBACK, UNSUBSCRIBE, UNSUBACK, DISCONNECT.
- Existing data structures and functions are modified, and some new
functions are added to serialize and deserialize the packets.
- Plaintext Demo for reference (
https://github.com/adituc/FreeRTOS/blob/pooja-main-branch/FreeRTOS-Plus/Demo/coreMQTT_Windows_Simulator/MQTTV5_Plain_Text/DemoTasks/PlaintextMQTTExampleV5.c
)

Test Steps
-----------
- Unit Tests are added for all new and modified features.
- Unit Tests for MQTTv5 functions are added in a separate folder in
test.
2025-08-20 13:44:51 -07:00

67 lines
2.3 KiB
C

/*
* coreMQTT <DEVELOPMENT BRANCH>
* Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* @file core_mqtt_utils.c
* @brief Implements the utility functions in core_mqtt_utils.h.
*/
#include <string.h>
#include <assert.h>
#include <stdint.h>
/* Include config defaults header to get default values of configs. */
#include "core_mqtt_config_defaults.h"
#include "core_mqtt_utils.h"
uint8_t * encodeVariableLength( uint8_t * pDestination,
size_t length )
{
uint8_t lengthByte;
uint8_t * pLengthEnd = NULL;
size_t remainingLength = length;
assert( pDestination != NULL );
pLengthEnd = pDestination;
/* This algorithm is copied from the MQTT 5.0 spec. */
do
{
lengthByte = ( uint8_t ) ( remainingLength % 128U );
remainingLength = remainingLength / 128U;
/* Set the high bit of this byte, indicating that there's more data. */
if( remainingLength > 0U )
{
UINT8_SET_BIT( lengthByte, 7 );
}
/* Output a single encoded byte. */
*pLengthEnd = lengthByte;
pLengthEnd++;
} while( remainingLength > 0U );
return pLengthEnd;
}