vars.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * Copyright (c) 2018 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef VARS_H__
  41. #define VARS_H__
  42. #include <stdint.h>
  43. #include <stdbool.h>
  44. #include <stddef.h>
  45. #include "nrf_cli.h"
  46. #define VAR_NAME_LENGTH_MAX 15 ///< Maximum length of CLI variable including '$' character and not including NULL character
  47. /** @brief Create dynamic command listing all existing CLI variables.
  48. * @param name Name of the dynamic command.
  49. * @param next Next subcommand after this command or NULL.
  50. */
  51. #define VAR_REGISTER_SUB_COMMANDS(name, next) \
  52. static void _##name##_get(size_t idx, nrf_cli_static_entry_t * p_static) \
  53. { \
  54. var_dynamic_cmd((next), idx, p_static); \
  55. } \
  56. NRF_CLI_CREATE_DYNAMIC_CMD(name, _##name##_get)
  57. /** @brief Contains header of the variable.
  58. *
  59. * Some blocks inside variable memory space contain only headers, e.g. dynamically allocated memory
  60. * by @ref var_malloc. Data size and name are skipped to save memory.
  61. */
  62. typedef struct var_header_s
  63. {
  64. size_t total_size; ///< Total size of the block that contains this variable including this header.
  65. struct var_type_s const * p_type; ///< Pointer to type definition of this variable.
  66. } var_header_t;
  67. /** @brief Structure holding variable.
  68. *
  69. * It has to by located at the variables memory space. Immediately after this structure content
  70. * data is located and it can be retried by @ref VAR_DATA macro.
  71. */
  72. typedef struct var_s
  73. {
  74. var_header_t header; ///< Variable header.
  75. size_t data_size; ///< Actual content size that can be smaller than allocated space.
  76. char name[VAR_NAME_LENGTH_MAX + 1]; ///< Name of the variable.
  77. } var_t;
  78. /** @brief Macro to get data of the CLI variable
  79. */
  80. #define VAR_DATA(p_var) ((uint8_t *)(&((var_t *)(p_var))[1]))
  81. /** @brief Callback function printing content of the variable of the specific type.
  82. * @param p_var Variable to print.
  83. * @param short_info true if printed text must be short (brief); no new lines. The text appears after the variable description.
  84. * false if printed text can be long (full content); multiple lines are allowed.
  85. */
  86. typedef void (*var_print_fn_t)(var_t * p_var, bool short_info);
  87. /** @brief Callback function for deallocating variables of the specific type.
  88. * @param p_var Variable to deallocate.
  89. * @return false to prevent variable to be removed, true otherwise.
  90. */
  91. typedef bool (*var_free_fn_t)(var_t * p_var);
  92. /** @brief Structure holding information regarding the type of variable.
  93. */
  94. typedef struct var_type_s
  95. {
  96. char const * p_description; ///< Short description of the type.
  97. var_free_fn_t free_fn; ///< Callback function that is to be called when variable is about to be deleted.
  98. var_print_fn_t print_fn; ///< Callback function that is to be called to print information about content.
  99. void * p_tag; ///< Transparent pointer that can be used by callback functions.
  100. } var_type_t;
  101. /** @brief Initialize CLI variable memory space.
  102. */
  103. void var_init(void);
  104. /** @brief Starts iterator over all CLI variables.
  105. * @return Pointer to the first variable, or NULL if there are no variables.
  106. */
  107. var_t * var_begin(void);
  108. /** @brief Go to next defined CLI variable.
  109. * @param current_var Pointer to the current CLI variable.
  110. * @return Pointer to the next variable, or NULL if there are no more variables.
  111. */
  112. var_t * var_next(var_t * current_var);
  113. /** @brief Finds a variable of the specified name in the CLI variable memory space.
  114. * @param p_name Name of the variable to search for (including '$' character).
  115. * @param verbose Print messages in case of problems if true.
  116. * @return Pointer to variable or NULL if not found.
  117. */
  118. var_t * var_get(const char * p_name, bool verbose);
  119. /** @brief Deletes a variable of the specified name from the CLI variable memory space.
  120. * @param p_name Name of the variable to search for (including '$' character).
  121. * @param verbose Print messages in case of problems if true.
  122. * @return true if variable was deleted successfully, false otherwise.
  123. */
  124. bool var_delete(const char * p_name, bool verbose);
  125. /** @brief Renames a variable of the specified name.
  126. * @param p_name Name of the variable to search for (including '$' character).
  127. * @param p_new_name New name of the variable (including '$' character).
  128. * @param verbose Print messages in case of problems if true.
  129. * @return true if variable was renamed successfully, false otherwise.
  130. */
  131. bool var_rename(const char * p_name, const char * p_new_name, bool verbose);
  132. /** @brief Creates new variable.
  133. * @param p_name Name of the new variable.
  134. * @param p_type Pointer to structure thet defines the type of the variable.
  135. * @param size Initial content data size. It is an initial value and it can be changed later to
  136. * a smaller one, but not bigger than the initial value.
  137. * @param verbose Print messages in case of problems if true.
  138. * @return Pointer to the newly created variable with zeroed content.
  139. */
  140. var_t * var_create(const char * p_name, var_type_t const * p_type, size_t size, bool verbose);
  141. /** @brief Creates a new temporal variable.
  142. *
  143. * Name of the variable is automatically generated and is indicated as temporary.
  144. * Normally, this kind of variables are deleted after exit from command by @ref var_all_temp_delete.
  145. * It is possible to rename variable to make it not temporary.
  146. *
  147. * @param p_type Pointer to structure that defines the type of the variable.
  148. * @param size Initial content data size. It is an initial value and it can be changed later to
  149. * a smaller one, but not bigger than the initial value.
  150. * @param verbose Print messages in case of problems if true.
  151. * @return Pointer to newly created variable with zeroed content.
  152. */
  153. var_t * var_temp_create(var_type_t const * p_type, size_t size, bool verbose);
  154. /** @brief Deletes all variables with the name indicated as temporary.
  155. */
  156. void var_all_temp_delete(void);
  157. /** @brief Checks if variable is temporary.
  158. * @param p_var Variable to check.
  159. * @return true if variable is temporary.
  160. */
  161. bool var_is_temp(var_t const * p_var);
  162. /** @brief Prints variable on terminal.
  163. * @param p_var Variable to print.
  164. */
  165. void var_show(var_t * p_var);
  166. /** @brief Allocates memory block inside the variable memory space.
  167. *
  168. * The variable memory space can be also used to handle dynamic memory allocation. In this case, the memory blocks
  169. * are reduced to save memory (@see var_header_t), and they only have total size and type.
  170. *
  171. * @param size Number of bytes to allocate.
  172. * @return Pointer to the newly allocated data, or NULL if there is no free block big enough to
  173. * fit requested size.
  174. */
  175. void * var_malloc(size_t size);
  176. /** @brief Chage size of the allocated block.
  177. * @param p_old Pointer to the old memory block.
  178. * @param new_size New size.
  179. * @return Pointer to the new memory block.
  180. */
  181. void * var_realloc(void * p_old, size_t new_size);
  182. /** @brief Memory allocation in calloc style. @see var_malloc.
  183. */
  184. void * var_calloc(size_t count, size_t size);
  185. /** @brief Dynamic memory free.
  186. * @param p_data Pointer to free.
  187. */
  188. void var_free(void* p_data);
  189. /** @brief Shortcut to create a temporary variable just to use it as a temporary buffer.
  190. *
  191. * Returned pointer does not have to be deallocated, because this will be done by @ref var_all_temp_delete.
  192. * @param size Number of bytes to allocate.
  193. */
  194. void * var_temp_alloc(size_t size);
  195. /** @brief Function that generates dynamic command that lists all variables used by the @ref VAR_REGISTER_SUB_COMMANDS macro.
  196. */
  197. void var_dynamic_cmd(nrf_cli_cmd_entry_t const * p_subcmd, size_t idx, nrf_cli_static_entry_t * p_static);
  198. #endif // VARS_H__