coreMQTT Agent v1.0.0
Thread safe MQTT 3.1.1 Client
core_mqtt_agent.h
Go to the documentation of this file.
1/*
2 * coreMQTT Agent v1.0.0
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
27#ifndef CORE_MQTT_AGENT_H
28#define CORE_MQTT_AGENT_H
29
30/* MQTT library includes. */
31#include "core_mqtt.h"
32#include "core_mqtt_state.h"
33
34/* Command messaging interface include. */
36
49#ifndef MQTT_AGENT_MAX_OUTSTANDING_ACKS
50 #define MQTT_AGENT_MAX_OUTSTANDING_ACKS ( 20U )
51#endif
52
65#ifndef MQTT_AGENT_MAX_EVENT_QUEUE_WAIT_TIME
66 #define MQTT_AGENT_MAX_EVENT_QUEUE_WAIT_TIME ( 1000U )
67#endif
68
69/*-----------------------------------------------------------*/
70
75typedef enum MQTTCommandType
76{
77 NONE = 0,
88
89struct MQTTAgentContext;
90struct MQTTAgentCommandContext;
91
96typedef struct MQTTAgentReturnInfo
97{
99 uint8_t * pSubackCodes;
101
109typedef struct MQTTAgentCommandContext MQTTAgentCommandContext_t;
110
127typedef void (* MQTTAgentCommandCallback_t )( MQTTAgentCommandContext_t * pCmdCallbackContext,
128 MQTTAgentReturnInfo_t * pReturnInfo );
129
137{
139 void * pArgs;
142};
143
148typedef struct MQTTAckInfo
149{
150 uint16_t packetId;
151 MQTTAgentCommand_t * pOriginalCommand;
153
168typedef void (* MQTTAgentIncomingPublishCallback_t )( struct MQTTAgentContext * pMqttAgentContext,
169 uint16_t packetId,
170 MQTTPublishInfo_t * pPublishInfo );
171
178typedef struct MQTTAgentContext
179{
187
192typedef struct MQTTAgentSubscribeArgs
193{
197
202typedef struct MQTTAgentConnectArgs
203{
206 uint32_t timeoutMs;
209
214typedef struct MQTTAgentCommandInfo
215{
218 uint32_t blockTimeMs;
220
221/*-----------------------------------------------------------*/
222
312/* @[declare_mqtt_agent_init] */
314 const MQTTAgentMessageInterface_t * pMsgInterface,
315 const MQTTFixedBuffer_t * pNetworkBuffer,
316 const TransportInterface_t * pTransportInterface,
317 MQTTGetCurrentTimeFunc_t getCurrentTimeMs,
318 MQTTAgentIncomingPublishCallback_t incomingCallback,
319 void * pIncomingPacketContext );
320/* @[declare_mqtt_agent_init] */
321
362/* @[declare_mqtt_agent_commandloop] */
364/* @[declare_mqtt_agent_commandloop] */
365
403/* @[declare_mqtt_agent_resumesession] */
405 bool sessionPresent );
406/* @[declare_mqtt_agent_resumesession] */
407
442/* @[declare_mqtt_agent_cancelall] */
444/* @[declare_mqtt_agent_cancelall] */
445
501/* @[declare_mqtt_agent_subscribe] */
502MQTTStatus_t MQTTAgent_Subscribe( const MQTTAgentContext_t * pMqttAgentContext,
503 MQTTAgentSubscribeArgs_t * pSubscriptionArgs,
504 const MQTTAgentCommandInfo_t * pCommandInfo );
505/* @[declare_mqtt_agent_subscribe] */
506
561/* @[declare_mqtt_agent_unsubscribe] */
562MQTTStatus_t MQTTAgent_Unsubscribe( const MQTTAgentContext_t * pMqttAgentContext,
563 MQTTAgentSubscribeArgs_t * pSubscriptionArgs,
564 const MQTTAgentCommandInfo_t * pCommandInfo );
565/* @[declare_mqtt_agent_unsubscribe] */
566
621/* @[declare_mqtt_agent_publish] */
622MQTTStatus_t MQTTAgent_Publish( const MQTTAgentContext_t * pMqttAgentContext,
623 MQTTPublishInfo_t * pPublishInfo,
624 const MQTTAgentCommandInfo_t * pCommandInfo );
625/* @[declare_mqtt_agent_publish] */
626
672/* @[declare_mqtt_agent_processloop] */
673MQTTStatus_t MQTTAgent_ProcessLoop( const MQTTAgentContext_t * pMqttAgentContext,
674 const MQTTAgentCommandInfo_t * pCommandInfo );
675/* @[declare_mqtt_agent_processloop] */
676
728/* @[declare_mqtt_agent_ping] */
729MQTTStatus_t MQTTAgent_Ping( const MQTTAgentContext_t * pMqttAgentContext,
730 const MQTTAgentCommandInfo_t * pCommandInfo );
731/* @[declare_mqtt_agent_ping] */
732
821/* @[declare_mqtt_agent_connect] */
822MQTTStatus_t MQTTAgent_Connect( const MQTTAgentContext_t * pMqttAgentContext,
823 MQTTAgentConnectArgs_t * pConnectArgs,
824 const MQTTAgentCommandInfo_t * pCommandInfo );
825/* @[declare_mqtt_agent_connect] */
826
884/* @[declare_mqtt_agent_disconnect] */
885MQTTStatus_t MQTTAgent_Disconnect( const MQTTAgentContext_t * pMqttAgentContext,
886 const MQTTAgentCommandInfo_t * pCommandInfo );
887/* @[declare_mqtt_agent_disconnect] */
888
947/* @[declare_mqtt_agent_terminate] */
948MQTTStatus_t MQTTAgent_Terminate( const MQTTAgentContext_t * pMqttAgentContext,
949 const MQTTAgentCommandInfo_t * pCommandInfo );
950/* @[declare_mqtt_agent_terminate] */
951
952#endif /* CORE_MQTT_AGENT_H */
MQTTStatus_t MQTTAgent_CancelAll(MQTTAgentContext_t *pMqttAgentContext)
Cancel all enqueued commands and those awaiting acknowledgment while the command loop is not running.
Definition: core_mqtt_agent.c:1081
MQTTStatus_t MQTTAgent_Disconnect(const MQTTAgentContext_t *pMqttAgentContext, const MQTTAgentCommandInfo_t *pCommandInfo)
Add a command to disconnect an MQTT connection.
Definition: core_mqtt_agent.c:1253
MQTTStatus_t MQTTAgent_Init(MQTTAgentContext_t *pMqttAgentContext, const MQTTAgentMessageInterface_t *pMsgInterface, const MQTTFixedBuffer_t *pNetworkBuffer, const TransportInterface_t *pTransportInterface, MQTTGetCurrentTimeFunc_t getCurrentTimeMs, MQTTAgentIncomingPublishCallback_t incomingCallback, void *pIncomingPacketContext)
Perform any initialization the MQTT agent requires before it can be used. Must be called before any o...
Definition: core_mqtt_agent.c:946
MQTTStatus_t MQTTAgent_Subscribe(const MQTTAgentContext_t *pMqttAgentContext, MQTTAgentSubscribeArgs_t *pSubscriptionArgs, const MQTTAgentCommandInfo_t *pCommandInfo)
Add a command to call MQTT_Subscribe() for an MQTT connection.
Definition: core_mqtt_agent.c:1130
MQTTStatus_t MQTTAgent_ResumeSession(MQTTAgentContext_t *pMqttAgentContext, bool sessionPresent)
Resume a session by resending publishes if a session is present in the broker, or clear state informa...
Definition: core_mqtt_agent.c:1038
MQTTStatus_t MQTTAgent_Connect(const MQTTAgentContext_t *pMqttAgentContext, MQTTAgentConnectArgs_t *pConnectArgs, const MQTTAgentCommandInfo_t *pCommandInfo)
Add a command to call MQTT_Connect() for an MQTT connection. If a session is resumed with the broker,...
Definition: core_mqtt_agent.c:1228
MQTTStatus_t MQTTAgent_CommandLoop(MQTTAgentContext_t *pMqttAgentContext)
Process commands from the command queue in a loop.
Definition: core_mqtt_agent.c:996
MQTTStatus_t MQTTAgent_ProcessLoop(const MQTTAgentContext_t *pMqttAgentContext, const MQTTAgentCommandInfo_t *pCommandInfo)
Send a message to the MQTT agent purely to trigger an iteration of its loop, which will result in a c...
Definition: core_mqtt_agent.c:1205
MQTTStatus_t MQTTAgent_Unsubscribe(const MQTTAgentContext_t *pMqttAgentContext, MQTTAgentSubscribeArgs_t *pSubscriptionArgs, const MQTTAgentCommandInfo_t *pCommandInfo)
Add a command to call MQTT_Unsubscribe() for an MQTT connection.
Definition: core_mqtt_agent.c:1155
MQTTStatus_t MQTTAgent_Terminate(const MQTTAgentContext_t *pMqttAgentContext, const MQTTAgentCommandInfo_t *pCommandInfo)
Add a termination command to the command queue.
Definition: core_mqtt_agent.c:1299
MQTTStatus_t MQTTAgent_Ping(const MQTTAgentContext_t *pMqttAgentContext, const MQTTAgentCommandInfo_t *pCommandInfo)
Add a command to call MQTT_Ping() for an MQTT connection.
Definition: core_mqtt_agent.c:1276
#define MQTT_AGENT_MAX_OUTSTANDING_ACKS
The maximum number of pending acknowledgments to track for a single connection.
Definition: core_mqtt_agent.h:50
MQTTStatus_t MQTTAgent_Publish(const MQTTAgentContext_t *pMqttAgentContext, MQTTPublishInfo_t *pPublishInfo, const MQTTAgentCommandInfo_t *pCommandInfo)
Add a command to call MQTT_Publish() for an MQTT connection.
Definition: core_mqtt_agent.c:1180
Functions to interact with queues.
void(* MQTTAgentCommandCallback_t)(MQTTAgentCommandContext_t *pCmdCallbackContext, MQTTAgentReturnInfo_t *pReturnInfo)
Callback function called when a command completes.
Definition: core_mqtt_agent.h:127
void(* MQTTAgentIncomingPublishCallback_t)(struct MQTTAgentContext *pMqttAgentContext, uint16_t packetId, MQTTPublishInfo_t *pPublishInfo)
Callback function called when receiving a publish.
Definition: core_mqtt_agent.h:168
MQTTAgentCommandType_t
A type of command for interacting with the MQTT API.
Definition: core_mqtt_agent.h:76
@ NUM_COMMANDS
The number of command types handled by the agent.
Definition: core_mqtt_agent.h:86
@ CONNECT
Call MQTT_Connect().
Definition: core_mqtt_agent.h:83
@ DISCONNECT
Call MQTT_Disconnect().
Definition: core_mqtt_agent.h:84
@ PING
Call MQTT_Ping().
Definition: core_mqtt_agent.h:82
@ UNSUBSCRIBE
Call MQTT_Unsubscribe().
Definition: core_mqtt_agent.h:81
@ PROCESSLOOP
Call MQTT_ProcessLoop().
Definition: core_mqtt_agent.h:78
@ TERMINATE
Exit the command loop and stop processing commands.
Definition: core_mqtt_agent.h:85
@ SUBSCRIBE
Call MQTT_Subscribe().
Definition: core_mqtt_agent.h:80
@ PUBLISH
Call MQTT_Publish().
Definition: core_mqtt_agent.h:79
@ NONE
No command received. Must be zero (its memset() value).
Definition: core_mqtt_agent.h:77
struct MQTTAgentCommandContext MQTTAgentCommandContext_t
Struct containing context for a specific command.
Definition: core_mqtt_agent.h:109
uint32_t(* MQTTGetCurrentTimeFunc_t)(void)
MQTTStatus_t
Information for a pending MQTT ack packet expected by the agent.
Definition: core_mqtt_agent.h:149
MQTTAgentCommand_t * pOriginalCommand
Definition: core_mqtt_agent.h:151
uint16_t packetId
Definition: core_mqtt_agent.h:150
The commands sent from the APIs to the MQTT agent task.
Definition: core_mqtt_agent.h:137
void * pArgs
Arguments of command.
Definition: core_mqtt_agent.h:139
MQTTAgentCommandCallback_t pCommandCompleteCallback
Callback to invoke upon completion.
Definition: core_mqtt_agent.h:140
MQTTAgentCommandContext_t * pCmdContext
Context for completion callback.
Definition: core_mqtt_agent.h:141
MQTTAgentCommandType_t commandType
Type of command.
Definition: core_mqtt_agent.h:138
Struct holding arguments that are common to every command.
Definition: core_mqtt_agent.h:215
MQTTAgentCommandContext_t * pCmdCompleteCallbackContext
Context for completion callback.
Definition: core_mqtt_agent.h:217
MQTTAgentCommandCallback_t cmdCompleteCallback
Callback to invoke upon completion.
Definition: core_mqtt_agent.h:216
uint32_t blockTimeMs
Maximum block time for enqueueing the command.
Definition: core_mqtt_agent.h:218
Struct holding arguments for a CONNECT call.
Definition: core_mqtt_agent.h:203
bool sessionPresent
Output flag set if a previous session was present.
Definition: core_mqtt_agent.h:207
MQTTPublishInfo_t * pWillInfo
Optional Last Will and Testament.
Definition: core_mqtt_agent.h:205
uint32_t timeoutMs
Maximum timeout for a CONNACK packet.
Definition: core_mqtt_agent.h:206
MQTTConnectInfo_t * pConnectInfo
MQTT CONNECT packet information.
Definition: core_mqtt_agent.h:204
Information used by each MQTT agent. A context will be initialized by MQTTAgent_Init(),...
Definition: core_mqtt_agent.h:179
void * pIncomingCallbackContext
Definition: core_mqtt_agent.h:184
MQTTAgentIncomingPublishCallback_t pIncomingCallback
Definition: core_mqtt_agent.h:183
MQTTAgentMessageInterface_t agentInterface
Definition: core_mqtt_agent.h:181
MQTTContext_t mqttContext
Definition: core_mqtt_agent.h:180
bool packetReceivedInLoop
Definition: core_mqtt_agent.h:185
Function pointers and contexts used for sending and receiving commands, and allocating memory for the...
Definition: core_mqtt_agent_message_interface.h:127
Struct holding return codes and outputs from a command.
Definition: core_mqtt_agent.h:97
MQTTStatus_t returnCode
Definition: core_mqtt_agent.h:98
uint8_t * pSubackCodes
Definition: core_mqtt_agent.h:99
Struct holding arguments for a SUBSCRIBE or UNSUBSCRIBE call.
Definition: core_mqtt_agent.h:193
MQTTSubscribeInfo_t * pSubscribeInfo
List of MQTT subscriptions.
Definition: core_mqtt_agent.h:194
size_t numSubscriptions
Number of elements in pSubscribeInfo.
Definition: core_mqtt_agent.h:195