1 /*!
2  * \file      LoRaMacCommands.h
3  *
4  * \brief     LoRa MAC commands
5  *
6  * \copyright Revised BSD License, see section \ref LICENSE.
7  *
8  * \code
9  *                ______                              _
10  *               / _____)             _              | |
11  *              ( (____  _____ ____ _| |_ _____  ____| |__
12  *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
13  *               _____) ) ____| | | || |_| ____( (___| | | |
14  *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
15  *              (C)2013 Semtech
16  *
17  *               ___ _____ _   ___ _  _____ ___  ___  ___ ___
18  *              / __|_   _/_\ / __| |/ / __/ _ \| _ \/ __| __|
19  *              \__ \ | |/ _ \ (__| ' <| _| (_) |   / (__| _|
20  *              |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
21  *              embedded.connectivity.solutions===============
22  *
23  * \endcode
24  *
25  * \author    Miguel Luis ( Semtech )
26  *
27  * \author    Daniel Jaeckle ( STACKFORCE )
28  *
29  * \author    Johannes Bruder ( STACKFORCE )
30  *
31  * addtogroup LORAMAC
32  * \{
33  *
34  */
35 #ifndef __LORAMAC_COMMANDS_H__
36 #define __LORAMAC_COMMANDS_H__
37 
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #endif
42 
43 #include <stdint.h>
44 #include <stddef.h>
45 #include "LoRaMacTypes.h"
46 
47 
48 /*
49  * Number of MAC Command slots
50  */
51 #define LORAMAC_COMMADS_MAX_NUM_OF_PARAMS   2
52 
53 /*!
54  * LoRaWAN MAC Command element
55  */
56 typedef struct sMacCommand MacCommand_t;
57 
58 struct sMacCommand
59 {
60     /*!
61      *  The pointer to the next MAC Command element in the list
62      */
63     MacCommand_t* Next;
64     /*!
65      * MAC command identifier
66      */
67     uint8_t CID;
68     /*!
69      * MAC command payload
70      */
71     uint8_t Payload[LORAMAC_COMMADS_MAX_NUM_OF_PARAMS];
72     /*!
73      * Size of MAC command payload
74      */
75     size_t PayloadSize;
76     /*!
77      * Indicates if it's a sticky MAC command
78      */
79     bool IsSticky;
80 };
81 
82 /*!
83  * LoRaMac Commands Status
84  */
85 typedef enum eLoRaMacCommandsStatus
86 {
87     /*!
88      * No error occurred
89      */
90     LORAMAC_COMMANDS_SUCCESS = 0,
91     /*!
92      * Null pointer exception
93      */
94     LORAMAC_COMMANDS_ERROR_NPE,
95     /*!
96      * There is no memory left to add a further MAC command
97      */
98     LORAMAC_COMMANDS_ERROR_MEMORY,
99     /*!
100      * MAC command not found.
101      */
102     LORAMAC_COMMANDS_ERROR_CMD_NOT_FOUND,
103     /*!
104      * Unknown or corrupted command error occurred.
105      */
106     LORAMAC_COMMANDS_ERROR_UNKNOWN_CMD,
107     /*!
108      * Undefined Error occurred
109      */
110     LORAMAC_COMMANDS_ERROR,
111 }LoRaMacCommandStatus_t;
112 
113 /*!
114  * Signature of callback function to be called by this module when the
115  * non-volatile needs to be saved.
116  */
117 typedef void ( *LoRaMacCommandsNvmEvent )( void );
118 
119 /*!
120  * \brief Initialization of LoRaMac MAC commands module
121  *
122  * \retval                            - Status of the operation
123  */
124 LoRaMacCommandStatus_t LoRaMacCommandsInit( void );
125 
126 /*!
127  * \brief Adds a new MAC command to be sent.
128  *
129  * \param[IN]   cid                - MAC command identifier
130  * \param[IN]   payload            - MAC command payload containing parameters
131  * \param[IN]   payloadSize        - Size of MAC command payload
132  *
133  * \retval                     - Status of the operation
134  */
135 LoRaMacCommandStatus_t LoRaMacCommandsAddCmd( uint8_t cid, uint8_t* payload, size_t payloadSize );
136 
137 /*!
138  * \brief Remove a MAC command.
139  *
140  * \param[OUT]  cmd                - MAC command
141  *
142  * \retval                     - Status of the operation
143  */
144 LoRaMacCommandStatus_t LoRaMacCommandsRemoveCmd( MacCommand_t* macCmd );
145 
146 /*!
147  * \brief Get the MAC command with corresponding CID.
148  *
149  * \param[IN]   cid                - MAC command identifier
150  * \param[OUT]  cmd                - MAC command
151  *
152  * \retval                     - Status of the operation
153  */
154 LoRaMacCommandStatus_t LoRaMacCommandsGetCmd( uint8_t cid, MacCommand_t** macCmd );
155 
156 /*!
157  * \brief Remove all none sticky MAC commands.
158  *
159  * \retval                     - Status of the operation
160  */
161 LoRaMacCommandStatus_t LoRaMacCommandsRemoveNoneStickyCmds( void );
162 
163 /*!
164  * \brief Remove all sticky answer MAC commands.
165  *
166  * \retval                     - Status of the operation
167  */
168 LoRaMacCommandStatus_t LoRaMacCommandsRemoveStickyAnsCmds( void );
169 
170 /*!
171  * \brief Get size of all MAC commands serialized as buffer
172  *
173  * \param[out]   size               - Available size of memory for MAC commands
174  *
175  * \retval                     - Status of the operation
176  */
177 LoRaMacCommandStatus_t LoRaMacCommandsGetSizeSerializedCmds( size_t* size );
178 
179 /*!
180  * \brief Get as many as possible MAC commands serialized
181  *
182  * \param[IN]   availableSize      - Available size of memory for MAC commands
183  * \param[out]  effectiveSize      - Size of memory which was effectively used for serializing.
184  * \param[out]  buffer             - Destination data buffer
185  *
186  * \retval                     - Status of the operation
187  */
188 LoRaMacCommandStatus_t LoRaMacCommandsSerializeCmds( size_t availableSize, size_t* effectiveSize,  uint8_t* buffer );
189 
190 /*!
191  * \brief Determines if there are sticky MAC commands pending.
192  *
193  * \param[IN]   cmdsPending        - Indicates if there are sticky MAC commands in the queue.
194  *
195  * \retval                     - Status of the operation
196  */
197 LoRaMacCommandStatus_t LoRaMacCommandsStickyCmdsPending( bool* cmdsPending );
198 
199 /*!
200  * \brief Get the MAC command size with corresponding CID.
201  *
202  * \param[IN]   cid                - MAC command identifier
203  *
204  * \retval Size of the command.
205  */
206 uint8_t LoRaMacCommandsGetCmdSize( uint8_t cid );
207 
208 /*! \} addtogroup LORAMAC */
209 
210 #ifdef __cplusplus
211 }
212 #endif
213 
214 #endif // __LORAMAC_COMMANDS_H__
215 
216