Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix MQTT MISRA violations (aws#993)
Browse files Browse the repository at this point in the history
* Fix MQTT MISRA violations

* Remove unnecessary const

* Remove unused function

* Fix string.h warning and add macro to disable logs

* Use ternary for MISRA 10.5

* Remove TODO from logging header
muneebahmed10 authored and leegeth committed Aug 27, 2020
1 parent 7e8b366 commit 318aa36
Showing 8 changed files with 188 additions and 197 deletions.
7 changes: 5 additions & 2 deletions demos/logging-stack/logging_stack.h
Original file line number Diff line number Diff line change
@@ -40,8 +40,11 @@
#define LOG_METADATA_ARGS __FILE__, __LINE__

/* Common macro for all logging interface macros. */
/* TODO - Replace printf with an implementation function. */
#define SdkLog( string ) printf string
#if !defined( DISABLE_LOGGING )
#define SdkLog( string ) printf string
#else
#define SdkLog( string )
#endif

/* Check that LIBRARY_LOG_LEVEL is defined and has a valid value. */
#if !defined( LIBRARY_LOG_LEVEL ) || \
1 change: 1 addition & 0 deletions demos/mqtt/mqtt_demo_lightweight/mqtt_demo_lightweight.c
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@

/* Standard includes. */
#include <stdlib.h>
#include <string.h>

/* POSIX socket includes. */
#include <netdb.h>
2 changes: 1 addition & 1 deletion demos/mqtt/mqtt_demo_plaintext/mqtt_demo_plaintext.c
Original file line number Diff line number Diff line change
@@ -588,7 +588,7 @@ static int establishMqttSession( MQTTContext_t * pContext,
int status = EXIT_SUCCESS;
MQTTStatus_t mqttStatus;
MQTTConnectInfo_t connectInfo;
char sessionPresent;
bool sessionPresent;
MQTTTransportInterface_t transport;
MQTTFixedBuffer_t networkBuffer;
MQTTApplicationCallbacks_t callbacks;
36 changes: 18 additions & 18 deletions libraries/standard/mqtt/include/mqtt.h
Original file line number Diff line number Diff line change
@@ -134,10 +134,10 @@ struct MQTTContext
* @return #MQTTBadParameter if invalid parameters are passed;
* #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_Init( MQTTContext_t * const pContext,
const MQTTTransportInterface_t * const pTransportInterface,
const MQTTApplicationCallbacks_t * const pCallbacks,
const MQTTFixedBuffer_t * const pNetworkBuffer );
MQTTStatus_t MQTT_Init( MQTTContext_t * pContext,
const MQTTTransportInterface_t * pTransportInterface,
const MQTTApplicationCallbacks_t * pCallbacks,
const MQTTFixedBuffer_t * pNetworkBuffer );

/**
* @brief Establish a MQTT session.
@@ -159,11 +159,11 @@ MQTTStatus_t MQTT_Init( MQTTContext_t * const pContext,
* the #timeoutMs for CONNACK;
* #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_Connect( MQTTContext_t * const pContext,
const MQTTConnectInfo_t * const pConnectInfo,
const MQTTPublishInfo_t * const pWillInfo,
MQTTStatus_t MQTT_Connect( MQTTContext_t * pContext,
const MQTTConnectInfo_t * pConnectInfo,
const MQTTPublishInfo_t * pWillInfo,
uint32_t timeoutMs,
bool * const pSessionPresent );
bool * pSessionPresent );

/**
* @brief Sends MQTT SUBSCRIBE for the given list of topic filters to
@@ -180,8 +180,8 @@ MQTTStatus_t MQTT_Connect( MQTTContext_t * const pContext,
* #MQTTSendFailed if transport write failed;
* #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_Subscribe( MQTTContext_t * const pContext,
const MQTTSubscribeInfo_t * const pSubscriptionList,
MQTTStatus_t MQTT_Subscribe( MQTTContext_t * pContext,
const MQTTSubscribeInfo_t * pSubscriptionList,
size_t subscriptionCount,
uint16_t packetId );

@@ -197,8 +197,8 @@ MQTTStatus_t MQTT_Subscribe( MQTTContext_t * const pContext,
* #MQTTSendFailed if transport write failed;
* #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_Publish( MQTTContext_t * const pContext,
const MQTTPublishInfo_t * const pPublishInfo,
MQTTStatus_t MQTT_Publish( MQTTContext_t * pContext,
const MQTTPublishInfo_t * pPublishInfo,
uint16_t packetId );

/**
@@ -211,7 +211,7 @@ MQTTStatus_t MQTT_Publish( MQTTContext_t * const pContext,
* #MQTTSendFailed if transport write failed;
* #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_Ping( MQTTContext_t * const pContext );
MQTTStatus_t MQTT_Ping( MQTTContext_t * pContext );

/**
* @brief Sends MQTT UNSUBSCRIBE for the given list of topic filters to
@@ -228,8 +228,8 @@ MQTTStatus_t MQTT_Ping( MQTTContext_t * const pContext );
* #MQTTSendFailed if transport write failed;
* #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_Unsubscribe( MQTTContext_t * const pContext,
const MQTTSubscribeInfo_t * const pSubscriptionList,
MQTTStatus_t MQTT_Unsubscribe( MQTTContext_t * pContext,
const MQTTSubscribeInfo_t * pSubscriptionList,
size_t subscriptionCount,
uint16_t packetId );

@@ -244,7 +244,7 @@ MQTTStatus_t MQTT_Unsubscribe( MQTTContext_t * const pContext,
* #MQTTSendFailed if transport send failed;
* #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_Disconnect( MQTTContext_t * const pContext );
MQTTStatus_t MQTT_Disconnect( MQTTContext_t * pContext );

/**
* @brief Loop to receive packets from the transport interface.
@@ -263,7 +263,7 @@ MQTTStatus_t MQTT_Disconnect( MQTTContext_t * const pContext );
* invalid transition for the internal state machine;
* #MQTTSuccess on success.
*/
MQTTStatus_t MQTT_ProcessLoop( MQTTContext_t * const pContext,
MQTTStatus_t MQTT_ProcessLoop( MQTTContext_t * pContext,
uint32_t timeoutMs );

/**
@@ -273,7 +273,7 @@ MQTTStatus_t MQTT_ProcessLoop( MQTTContext_t * const pContext,
*
* @return A non-zero number.
*/
uint16_t MQTT_GetPacketId( MQTTContext_t * const pContext );
uint16_t MQTT_GetPacketId( MQTTContext_t * pContext );

/**
* @brief Error code to string conversion for MQTT statuses.
78 changes: 37 additions & 41 deletions libraries/standard/mqtt/include/mqtt_lightweight.h
Original file line number Diff line number Diff line change
@@ -22,18 +22,18 @@
#ifndef MQTT_LIGHTWEIGHT_H
#define MQTT_LIGHTWEIGHT_H

#include <stddef.h>
#include <stdint.h>

/* bools are only defined in C99+ */
#if defined( __cplusplus ) || __STDC_VERSION__ >= 199901L
#if defined( __cplusplus ) || ( defined( __STDC_VERSION__ ) && ( __STDC_VERSION__ >= 199901L ) )
#include <stdbool.h>
#elif !defined( bool )
#define bool signed char
#define false 0
#define true 1
#define bool int8_t
#define false ( int8_t ) 0
#define true ( int8_t ) 1
#endif

#include <stddef.h>
#include <stdint.h>

/* Include config file before other headers. */
#include "mqtt_config.h"

@@ -271,10 +271,10 @@ struct MQTTPacketInfo
* @return #MQTTBadParameter if the packet would exceed the size allowed by the
* MQTT spec; #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_GetConnectPacketSize( const MQTTConnectInfo_t * const pConnectInfo,
const MQTTPublishInfo_t * const pWillInfo,
size_t * const pRemainingLength,
size_t * const pPacketSize );
MQTTStatus_t MQTT_GetConnectPacketSize( const MQTTConnectInfo_t * pConnectInfo,
const MQTTPublishInfo_t * pWillInfo,
size_t * pRemainingLength,
size_t * pPacketSize );

/**
* @brief Serialize an MQTT CONNECT packet in the given buffer.
@@ -288,10 +288,10 @@ MQTTStatus_t MQTT_GetConnectPacketSize( const MQTTConnectInfo_t * const pConnect
* #MQTTBadParameter if invalid parameters are passed;
* #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_SerializeConnect( const MQTTConnectInfo_t * const pConnectInfo,
const MQTTPublishInfo_t * const pWillInfo,
MQTTStatus_t MQTT_SerializeConnect( const MQTTConnectInfo_t * pConnectInfo,
const MQTTPublishInfo_t * pWillInfo,
size_t remainingLength,
const MQTTFixedBuffer_t * const pBuffer );
const MQTTFixedBuffer_t * pBuffer );

/**
* @brief Get packet size and Remaining Length of an MQTT SUBSCRIBE packet.
@@ -304,7 +304,7 @@ MQTTStatus_t MQTT_SerializeConnect( const MQTTConnectInfo_t * const pConnectInfo
* @return #MQTTBadParameter if the packet would exceed the size allowed by the
* MQTT spec; #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_GetSubscribePacketSize( const MQTTSubscribeInfo_t * const pSubscriptionList,
MQTTStatus_t MQTT_GetSubscribePacketSize( const MQTTSubscribeInfo_t * pSubscriptionList,
size_t subscriptionCount,
size_t * pRemainingLength,
size_t * pPacketSize );
@@ -322,11 +322,11 @@ MQTTStatus_t MQTT_GetSubscribePacketSize( const MQTTSubscribeInfo_t * const pSub
* #MQTTBadParameter if invalid parameters are passed;
* #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_SerializeSubscribe( const MQTTSubscribeInfo_t * const pSubscriptionList,
MQTTStatus_t MQTT_SerializeSubscribe( const MQTTSubscribeInfo_t * pSubscriptionList,
size_t subscriptionCount,
uint16_t packetId,
size_t remainingLength,
const MQTTFixedBuffer_t * const pBuffer );
const MQTTFixedBuffer_t * pBuffer );

/**
* @brief Get packet size and Remaining Length of an MQTT UNSUBSCRIBE packet.
@@ -339,7 +339,7 @@ MQTTStatus_t MQTT_SerializeSubscribe( const MQTTSubscribeInfo_t * const pSubscri
* @return #MQTTBadParameter if the packet would exceed the size allowed by the
* MQTT spec; #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_GetUnsubscribePacketSize( const MQTTSubscribeInfo_t * const pSubscriptionList,
MQTTStatus_t MQTT_GetUnsubscribePacketSize( const MQTTSubscribeInfo_t * pSubscriptionList,
size_t subscriptionCount,
size_t * pRemainingLength,
size_t * pPacketSize );
@@ -357,11 +357,11 @@ MQTTStatus_t MQTT_GetUnsubscribePacketSize( const MQTTSubscribeInfo_t * const pS
* #MQTTBadParameter if invalid parameters are passed;
* #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_SerializeUnsubscribe( const MQTTSubscribeInfo_t * const pSubscriptionList,
MQTTStatus_t MQTT_SerializeUnsubscribe( const MQTTSubscribeInfo_t * pSubscriptionList,
size_t subscriptionCount,
uint16_t packetId,
size_t remainingLength,
const MQTTFixedBuffer_t * const pBuffer );
const MQTTFixedBuffer_t * pBuffer );

/**
* @brief Get the packet size and remaining length of an MQTT PUBLISH packet.
@@ -373,9 +373,9 @@ MQTTStatus_t MQTT_SerializeUnsubscribe( const MQTTSubscribeInfo_t * const pSubsc
* @return #MQTTBadParameter if the packet would exceed the size allowed by the
* MQTT spec or if invalid parameters are passed; #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_GetPublishPacketSize( const MQTTPublishInfo_t * const pPublishInfo,
size_t * const pRemainingLength,
size_t * const pPacketSize );
MQTTStatus_t MQTT_GetPublishPacketSize( const MQTTPublishInfo_t * pPublishInfo,
size_t * pRemainingLength,
size_t * pPacketSize );

/**
* @brief Serialize an MQTT PUBLISH packet in the given buffer.
@@ -394,10 +394,10 @@ MQTTStatus_t MQTT_GetPublishPacketSize( const MQTTPublishInfo_t * const pPublish
* #MQTTBadParameter if invalid parameters are passed;
* #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_SerializePublish( const MQTTPublishInfo_t * const pPublishInfo,
MQTTStatus_t MQTT_SerializePublish( const MQTTPublishInfo_t * pPublishInfo,
uint16_t packetId,
size_t remainingLength,
const MQTTFixedBuffer_t * const pBuffer );
const MQTTFixedBuffer_t * pBuffer );

/**
* @brief Serialize an MQTT PUBLISH packet header in the given buffer.
@@ -418,11 +418,11 @@ MQTTStatus_t MQTT_SerializePublish( const MQTTPublishInfo_t * const pPublishInfo
* #MQTTBadParameter if invalid parameters are passed;
* #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_SerializePublishHeader( const MQTTPublishInfo_t * const pPublishInfo,
MQTTStatus_t MQTT_SerializePublishHeader( const MQTTPublishInfo_t * pPublishInfo,
uint16_t packetId,
size_t remainingLength,
const MQTTFixedBuffer_t * const pBuffer,
size_t * const pHeaderSize );
const MQTTFixedBuffer_t * pBuffer,
size_t * pHeaderSize );

/**
* @brief Serialize an MQTT PUBACK, PUBREC, PUBREL, or PUBCOMP into the given
@@ -435,7 +435,7 @@ MQTTStatus_t MQTT_SerializePublishHeader( const MQTTPublishInfo_t * const pPubli
*
* @return #MQTTBadParameter, #MQTTNoMemory, or #MQTTSuccess.
*/
MQTTStatus_t MQTT_SerializeAck( const MQTTFixedBuffer_t * const pBuffer,
MQTTStatus_t MQTT_SerializeAck( const MQTTFixedBuffer_t * pBuffer,
uint8_t packetType,
uint16_t packetId );

@@ -457,7 +457,7 @@ MQTTStatus_t MQTT_GetDisconnectPacketSize( size_t * pPacketSize );
* #MQTTBadParameter if invalid parameters are passed;
* #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_SerializeDisconnect( const MQTTFixedBuffer_t * const pBuffer );
MQTTStatus_t MQTT_SerializeDisconnect( const MQTTFixedBuffer_t * pBuffer );

/**
* @brief Get the size of an MQTT PINGREQ packet.
@@ -477,11 +477,7 @@ MQTTStatus_t MQTT_GetPingreqPacketSize( size_t * pPacketSize );
* #MQTTBadParameter if invalid parameters are passed;
* #MQTTSuccess otherwise.
*/
MQTTStatus_t MQTT_SerializePingreq( const MQTTFixedBuffer_t * const pBuffer );

MQTTStatus_t MQTT_GetIncomingPacket( MQTTTransportRecvFunc_t recvFunc,
MQTTNetworkContext_t networkContext,
MQTTPacketInfo_t * const pIncomingPacket );
MQTTStatus_t MQTT_SerializePingreq( const MQTTFixedBuffer_t * pBuffer );

/**
* @brief Deserialize an MQTT PUBLISH packet.
@@ -492,9 +488,9 @@ MQTTStatus_t MQTT_GetIncomingPacket( MQTTTransportRecvFunc_t recvFunc,
*
* @return #MQTTBadParameter, #MQTTBadResponse, or #MQTTSuccess.
*/
MQTTStatus_t MQTT_DeserializePublish( const MQTTPacketInfo_t * const pIncomingPacket,
uint16_t * const pPacketId,
MQTTPublishInfo_t * const pPublishInfo );
MQTTStatus_t MQTT_DeserializePublish( const MQTTPacketInfo_t * pIncomingPacket,
uint16_t * pPacketId,
MQTTPublishInfo_t * pPublishInfo );

/**
* @brief Deserialize an MQTT CONNACK, SUBACK, UNSUBACK, PUBACK, PUBREC, PUBREL,
@@ -507,9 +503,9 @@ MQTTStatus_t MQTT_DeserializePublish( const MQTTPacketInfo_t * const pIncomingPa
*
* @return #MQTTBadParameter, #MQTTBadResponse, or #MQTTSuccess.
*/
MQTTStatus_t MQTT_DeserializeAck( const MQTTPacketInfo_t * const pIncomingPacket,
uint16_t * const pPacketId,
bool * const pSessionPresent );
MQTTStatus_t MQTT_DeserializeAck( const MQTTPacketInfo_t * pIncomingPacket,
uint16_t * pPacketId,
bool * pSessionPresent );

/**
* @brief Extract MQTT packet type and length from incoming packet.
Loading

0 comments on commit 318aa36

Please sign in to comment.