xref: /CoreMQTT-Agent-v1.2.0/source/include/core_mqtt_agent_message_interface.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_message_interface.h
25  * @brief Functions to interact with queues.
26  */
27 #ifndef CORE_MQTT_AGENT_MESSAGE_INTERFACE_H
28 #define CORE_MQTT_AGENT_MESSAGE_INTERFACE_H
29 
30 #include <stddef.h>
31 #include <stdint.h>
32 #include <stdbool.h>
33 
34 /* *INDENT-OFF* */
35 #ifdef __cplusplus
36     extern "C" {
37 #endif
38 /* *INDENT-ON* */
39 
40 /* Declare here so interface functions can use. */
41 struct MQTTAgentCommand;
42 struct MQTTAgentMessageContext;
43 
44 /**
45  * @brief The commands sent from the APIs to the MQTT agent task.
46  */
47 typedef struct MQTTAgentCommand          MQTTAgentCommand_t;
48 
49 /**
50  * @ingroup mqtt_agent_struct_types
51  * @brief Context with which tasks may deliver messages to the agent.
52  */
53 /* @[define_messagectx] */
54 typedef struct MQTTAgentMessageContext   MQTTAgentMessageContext_t;
55 /* @[define_messagectx] */
56 
57 /**
58  * @brief Send a message to the specified context.
59  * Must be thread safe.
60  *
61  * @param[in] pMsgCtx An #MQTTAgentMessageContext_t.
62  * @param[in] pCommandToSend Pointer to address to send to queue.
63  * @param[in] blockTimeMs Block time to wait for a send.
64  *
65  * @return `true` if send was successful, else `false`.
66  */
67 /* @[define_messagesend] */
68 typedef bool ( * MQTTAgentMessageSend_t )( MQTTAgentMessageContext_t * pMsgCtx,
69                                            MQTTAgentCommand_t * const * pCommandToSend,
70                                            uint32_t blockTimeMs );
71 /* @[define_messagesend] */
72 
73 /**
74  * @brief Receive a message from the specified context.
75  * Must be thread safe.
76  *
77  * @param[in] pMsgCtx An #MQTTAgentMessageContext_t.
78  * @param[out] pReceivedCommand Pointer to write address of received command.
79  * @param[in] blockTimeMs Block time to wait for a receive.
80  *
81  * @return `true` if receive was successful, else `false`.
82  */
83 /* @[define_messagerecv] */
84 typedef bool ( * MQTTAgentMessageRecv_t )( MQTTAgentMessageContext_t * pMsgCtx,
85                                            MQTTAgentCommand_t ** pReceivedCommand,
86                                            uint32_t blockTimeMs );
87 /* @[define_messagerecv] */
88 
89 /**
90  * @brief Obtain a MQTTAgentCommand_t structure.
91  *
92  * @note MQTTAgentCommand_t structures hold everything the MQTT agent needs to process a
93  * command that originates from application.  Examples of commands are PUBLISH and
94  * SUBSCRIBE. The MQTTAgentCommand_t structure must persist for the duration of the command's
95  * operation.
96  *
97  * @param[in] blockTimeMs The length of time the calling task should remain in the
98  * Blocked state (so not consuming any CPU time) to wait for a MQTTAgentCommand_t structure to
99  * become available should one not be immediately at the time of the call.
100  *
101  * @return A pointer to a MQTTAgentCommand_t structure if one becomes available before
102  * blockTimeMs time expired, otherwise NULL.
103  */
104 /* @[define_messageget] */
105 typedef MQTTAgentCommand_t * ( * MQTTAgentCommandGet_t )( uint32_t blockTimeMs );
106 /* @[define_messageget] */
107 
108 /**
109  * @brief Give a MQTTAgentCommand_t structure back to the application.
110  *
111  * @note MQTTAgentCommand_t structures hold everything the MQTT agent needs to process a
112  * command that originates from application.  Examples of commands are PUBLISH and
113  * SUBSCRIBE. The MQTTAgentCommand_t structure must persist for the duration of the command's
114  * operation.
115  *
116  * @param[in] pCommandToRelease A pointer to the MQTTAgentCommand_t structure to return to
117  * the application. The structure must first have been obtained by calling
118  * an #MQTTAgentCommandGet_t, otherwise it will have no effect.
119  *
120  * @return true if the MQTTAgentCommand_t structure was returned to the application, otherwise false.
121  */
122 /* @[define_messagerelease] */
123 typedef bool ( * MQTTAgentCommandRelease_t )( MQTTAgentCommand_t * pCommandToRelease );
124 /* @[define_messagerelease] */
125 
126 /**
127  * @ingroup mqtt_agent_struct_types
128  * @brief Function pointers and contexts used for sending and receiving commands,
129  * and allocating memory for them.
130  */
131 /* @[define_messageinterface] */
132 typedef struct MQTTAgentMessageInterface
133 {
134     MQTTAgentMessageContext_t * pMsgCtx;      /**< Context with which tasks may deliver messages to the agent. */
135     MQTTAgentMessageSend_t send;              /**< Function to send a command to the agent. */
136     MQTTAgentMessageRecv_t recv;              /**< Function for the agent to receive a command. */
137     MQTTAgentCommandGet_t getCommand;         /**< Function to obtain a pointer to an allocated command. */
138     MQTTAgentCommandRelease_t releaseCommand; /**< Function to release an allocated command. */
139 } MQTTAgentMessageInterface_t;
140 /* @[define_messageinterface] */
141 
142 /* *INDENT-OFF* */
143 #ifdef __cplusplus
144     }
145 #endif
146 /* *INDENT-ON* */
147 
148 #endif /* CORE_MQTT_AGENT_MESSAGE_INTERFACE_H */
149