xref: /CoreMQTT-Agent-v1.2.0/source/include/core_mqtt_agent_command_functions.h (revision 86c51db1a7d060744a06fa3619bd3ba86a938a94)
1 /*
2  * coreMQTT Agent v1.2.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 
23 /**
24  * @file core_mqtt_agent_command_functions.h
25  * @brief Functions for processing an MQTT agent command.
26  */
27 #ifndef CORE_MQTT_AGENT_COMMAND_FUNCTIONS_H
28 #define CORE_MQTT_AGENT_COMMAND_FUNCTIONS_H
29 
30 /* *INDENT-OFF* */
31 #ifdef __cplusplus
32     extern "C" {
33 #endif
34 /* *INDENT-ON* */
35 
36 /* MQTT Agent include. */
37 #include "core_mqtt_agent.h"
38 
39 /**
40  * @brief An array of function pointers mapping command types to a function to
41  * execute. Configurable to allow a linker to remove unneeded functions.
42  *
43  * @note This array controls the behavior of each command. Altering the array
44  * would allow a linker to discard unused MQTT functions if desired. The size
45  * of this array MUST equal #NUM_COMMANDS and the order MUST correspond to
46  * #MQTTAgentCommandType_t commands if not using C99 designated initializers. If any
47  * function is desired not to be linked, it may be set to #MQTTAgentCommand_ProcessLoop
48  * or a custom function matching an #MQTTAgentCommandFunc_t prototype.
49  *
50  * <b>Default value:</b> @code{c}
51  * {
52  *     [ NONE ]        = MQTTAgentCommand_ProcessLoop,
53  *     [ PROCESSLOOP ] = MQTTAgentCommand_ProcessLoop,
54  *     [ PUBLISH ]     = MQTTAgentCommand_Publish,
55  *     [ SUBSCRIBE ]   = MQTTAgentCommand_Subscribe,
56  *     [ UNSUBSCRIBE ] = MQTTAgentCommand_Unsubscribe,
57  *     [ PING ]        = MQTTAgentCommand_Ping,
58  *     [ CONNECT ]     = MQTTAgentCommand_Connect,
59  *     [ DISCONNECT ]  = MQTTAgentCommand_Disconnect,
60  *     [ TERMINATE ]   = MQTTAgentCommand_Terminate
61  * }
62  * @endcode
63  */
64 #ifndef MQTT_AGENT_FUNCTION_TABLE
65     /* Designated initializers are only in C99+. */
66     #if defined( __STDC_VERSION__ ) && ( __STDC_VERSION__ >= 199901L )
67         #define MQTT_AGENT_FUNCTION_TABLE               \
68     {                                                   \
69         [ NONE ] = MQTTAgentCommand_ProcessLoop,        \
70         [ PROCESSLOOP ] = MQTTAgentCommand_ProcessLoop, \
71         [ PUBLISH ] = MQTTAgentCommand_Publish,         \
72         [ SUBSCRIBE ] = MQTTAgentCommand_Subscribe,     \
73         [ UNSUBSCRIBE ] = MQTTAgentCommand_Unsubscribe, \
74         [ PING ] = MQTTAgentCommand_Ping,               \
75         [ CONNECT ] = MQTTAgentCommand_Connect,         \
76         [ DISCONNECT ] = MQTTAgentCommand_Disconnect,   \
77         [ TERMINATE ] = MQTTAgentCommand_Terminate      \
78     }
79     #else /* if defined( __STDC_VERSION__ ) && ( __STDC_VERSION__ >= 199901L ) */
80 
81 /* If not using designated initializers, this must correspond
82  * to the order of MQTTAgentCommandType_t commands. */
83         #define MQTT_AGENT_FUNCTION_TABLE \
84     {                                     \
85         MQTTAgentCommand_ProcessLoop,     \
86         MQTTAgentCommand_ProcessLoop,     \
87         MQTTAgentCommand_Publish,         \
88         MQTTAgentCommand_Subscribe,       \
89         MQTTAgentCommand_Unsubscribe,     \
90         MQTTAgentCommand_Ping,            \
91         MQTTAgentCommand_Connect,         \
92         MQTTAgentCommand_Disconnect,      \
93         MQTTAgentCommand_Terminate        \
94     }
95     #endif /* if defined( __STDC_VERSION__ ) && ( __STDC_VERSION__ >= 199901L ) */
96 #endif /* ifndef MQTT_AGENT_FUNCTION_TABLE */
97 
98 /*-----------------------------------------------------------*/
99 
100 /**
101  * @ingroup mqtt_agent_struct_types
102  * @brief A structure of values and flags expected to be returned
103  * by command functions.
104  */
105 typedef struct MQTTAgentCommandFuncReturns
106 {
107     uint16_t packetId;      /**< @brief Packet ID of packet sent by command. */
108     bool endLoop;           /**< @brief Flag to indicate command loop should terminate. */
109     bool addAcknowledgment; /**< @brief Flag to indicate an acknowledgment should be tracked. */
110     bool runProcessLoop;    /**< @brief Flag to indicate MQTT_ProcessLoop() should be called after this command. */
111 } MQTTAgentCommandFuncReturns_t;
112 
113 /**
114  * @brief Function prototype for a command.
115  *
116  * @note These functions should only be called from within
117  * #MQTTAgent_CommandLoop.
118  *
119  * @param[in] pMqttAgentContext MQTT Agent context.
120  * @param[in] pArgs Arguments for the command.
121  * @param[out] pFlags Return flags set by the function.
122  *
123  * @return Return code of MQTT call.
124  */
125 typedef MQTTStatus_t (* MQTTAgentCommandFunc_t ) ( MQTTAgentContext_t * pMqttAgentContext,
126                                                    void * pArgs,
127                                                    MQTTAgentCommandFuncReturns_t * pFlags );
128 
129 /*-----------------------------------------------------------*/
130 
131 /**
132  * @brief Function to execute for a NONE command. This function does not call
133  * #MQTT_ProcessLoop itself, but instead sets a flag to indicate it should be called.
134  *
135  * This sets the following flags to `true`:
136  * - MQTTAgentCommandFuncReturns_t.runProcessLoop
137  *
138  * @param[in] pMqttAgentContext MQTT Agent context information.
139  * @param[in] pUnusedArg Unused NULL argument.
140  * @param[out] pReturnFlags Flags set to indicate actions the MQTT agent should take.
141  *
142  * @return #MQTTSuccess.
143  */
144 MQTTStatus_t MQTTAgentCommand_ProcessLoop( MQTTAgentContext_t * pMqttAgentContext,
145                                            void * pUnusedArg,
146                                            MQTTAgentCommandFuncReturns_t * pReturnFlags );
147 
148 /**
149  * @brief Function to execute for a PUBLISH command.
150  *
151  * This sets the following flags to `true`:
152  * - MQTTAgentCommandFuncReturns_t.runProcessLoop
153  * - MQTTAgentCommandFuncReturns_t.addAcknowledgment (for QoS > 0)
154  *
155  * @param[in] pMqttAgentContext MQTT Agent context information.
156  * @param[in] pPublishArg Publish information for MQTT_Publish().
157  * @param[out] pReturnFlags Flags set to indicate actions the MQTT agent should take.
158  *
159  * @return Status code of MQTT_Publish().
160  */
161 MQTTStatus_t MQTTAgentCommand_Publish( MQTTAgentContext_t * pMqttAgentContext,
162                                        void * pPublishArg,
163                                        MQTTAgentCommandFuncReturns_t * pReturnFlags );
164 
165 /**
166  * @brief Function to execute for a SUBSCRIBE command.
167  *
168  * This sets the following flags to `true`:
169  * - MQTTAgentCommandFuncReturns_t.runProcessLoop
170  * - MQTTAgentCommandFuncReturns_t.addAcknowledgment
171  *
172  * @param[in] pMqttAgentContext MQTT Agent context information.
173  * @param[in] pVoidSubscribeArgs Arguments for MQTT_Subscribe().
174  * @param[out] pReturnFlags Flags set to indicate actions the MQTT agent should take.
175  *
176  * @return Status code of MQTT_Subscribe().
177  */
178 MQTTStatus_t MQTTAgentCommand_Subscribe( MQTTAgentContext_t * pMqttAgentContext,
179                                          void * pVoidSubscribeArgs,
180                                          MQTTAgentCommandFuncReturns_t * pReturnFlags );
181 
182 /**
183  * @brief Function to execute for an UNSUBSCRIBE command.
184  *
185  * This sets the following flags to `true`:
186  * - MQTTAgentCommandFuncReturns_t.runProcessLoop
187  * - MQTTAgentCommandFuncReturns_t.addAcknowledgment
188  *
189  * @param[in] pMqttAgentContext MQTT Agent context information.
190  * @param[in] pVoidSubscribeArgs Arguments for MQTT_Unsubscribe().
191  * @param[out] pReturnFlags Flags set to indicate actions the MQTT agent should take.
192  *
193  * @return Status code of MQTT_Unsubscribe().
194  */
195 MQTTStatus_t MQTTAgentCommand_Unsubscribe( MQTTAgentContext_t * pMqttAgentContext,
196                                            void * pVoidSubscribeArgs,
197                                            MQTTAgentCommandFuncReturns_t * pReturnFlags );
198 
199 /**
200  * @brief Function to execute for a CONNECT command.
201  *
202  * This sets all return flags to `false`.
203  *
204  * @param[in] pMqttAgentContext MQTT Agent context information.
205  * @param[in] pVoidConnectArgs Arguments for MQTT_Connect().
206  * @param[out] pReturnFlags Flags set to indicate actions the MQTT agent should take.
207  *
208  * @return Status code of MQTT_Connect().
209  */
210 MQTTStatus_t MQTTAgentCommand_Connect( MQTTAgentContext_t * pMqttAgentContext,
211                                        void * pVoidConnectArgs,
212                                        MQTTAgentCommandFuncReturns_t * pReturnFlags );
213 
214 /**
215  * @brief Function to execute for a DISCONNECT command.
216  *
217  * This sets the following flags to `true`:
218  * - MQTTAgentCommandFuncReturns_t.endLoop
219  *
220  * @param[in] pMqttAgentContext MQTT Agent context information.
221  * @param[in] pUnusedArg Unused NULL argument.
222  * @param[out] pReturnFlags Flags set to indicate actions the MQTT agent should take.
223  *
224  * @return Status code of MQTT_Disconnect().
225  */
226 MQTTStatus_t MQTTAgentCommand_Disconnect( MQTTAgentContext_t * pMqttAgentContext,
227                                           void * pUnusedArg,
228                                           MQTTAgentCommandFuncReturns_t * pReturnFlags );
229 
230 /**
231  * @brief Function to execute for a PING command.
232  *
233  * This sets the following flags to `true`:
234  * - MQTTAgentCommandFuncReturns_t.runProcessLoop
235  *
236  * @param[in] pMqttAgentContext MQTT Agent context information.
237  * @param[in] pUnusedArg Unused NULL argument.
238  * @param[out] pReturnFlags Flags set to indicate actions the MQTT agent should take.
239  *
240  * @return Status code of MQTT_Ping().
241  */
242 MQTTStatus_t MQTTAgentCommand_Ping( MQTTAgentContext_t * pMqttAgentContext,
243                                     void * pUnusedArg,
244                                     MQTTAgentCommandFuncReturns_t * pReturnFlags );
245 
246 /**
247  * @brief Function to execute for a TERMINATE command. Calls #MQTTAgent_CancelAll
248  * to terminate all unfinished commands with #MQTTRecvFailed.
249  *
250  * This sets the following flags to `true`:
251  * - MQTTAgentCommandFuncReturns_t.endLoop
252  *
253  * @param[in] pMqttAgentContext MQTT Agent context information.
254  * @param[in] pUnusedArg Unused NULL argument.
255  * @param[out] pReturnFlags Flags set to indicate actions the MQTT agent should take.
256  *
257  * @return #MQTTSuccess.
258  */
259 MQTTStatus_t MQTTAgentCommand_Terminate( MQTTAgentContext_t * pMqttAgentContext,
260                                          void * pUnusedArg,
261                                          MQTTAgentCommandFuncReturns_t * pReturnFlags );
262 
263 /* *INDENT-OFF* */
264 #ifdef __cplusplus
265     }
266 #endif
267 /* *INDENT-ON* */
268 
269 #endif /* CORE_MQTT_AGENT_COMMAND_FUNCTIONS_H */
270