Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

215 lines
5.1 KiB

  1. /*!
  2. * \file utilities.h
  3. *
  4. * \brief Helper functions implementation
  5. *
  6. * \copyright Revised BSD License, see section \ref LICENSE.
  7. *
  8. * \code
  9. * ______ _
  10. * / _____) _ | |
  11. * ( (____ _____ ____ _| |_ _____ ____| |__
  12. * \____ \| ___ | (_ _) ___ |/ ___) _ \
  13. * _____) ) ____| | | || |_| ____( (___| | | |
  14. * (______/|_____)_|_|_| \__)_____)\____)_| |_|
  15. * (C)2013-2017 Semtech
  16. *
  17. * \endcode
  18. *
  19. * \author Miguel Luis ( Semtech )
  20. *
  21. * \author Gregory Cristian ( Semtech )
  22. */
  23. #ifndef __UTILITIES_H__
  24. #define __UTILITIES_H__
  25. #ifdef __cplusplus
  26. extern "C"
  27. {
  28. #endif
  29. #include <stdint.h>
  30. /*!
  31. * LMN (LoRaMac-node) status
  32. */
  33. typedef enum LmnStatus_e
  34. {
  35. LMN_STATUS_ERROR = 0,
  36. LMN_STATUS_OK = !LMN_STATUS_ERROR
  37. } LmnStatus_t;
  38. /*!
  39. * \brief Returns the minimum value between a and b
  40. *
  41. * \param [IN] a 1st value
  42. * \param [IN] b 2nd value
  43. * \retval minValue Minimum value
  44. */
  45. #ifndef MIN
  46. #define MIN( a, b ) ( ( ( a ) < ( b ) ) ? ( a ) : ( b ) )
  47. #endif
  48. /*!
  49. * \brief Returns the maximum value between a and b
  50. *
  51. * \param [IN] a 1st value
  52. * \param [IN] b 2nd value
  53. * \retval maxValue Maximum value
  54. */
  55. #ifndef MAX
  56. #define MAX( a, b ) ( ( ( a ) > ( b ) ) ? ( a ) : ( b ) )
  57. #endif
  58. /*!
  59. * \brief Returns 2 raised to the power of n
  60. *
  61. * \param [IN] n power value
  62. * \retval result of raising 2 to the power n
  63. */
  64. #define POW2( n ) ( 1 << n )
  65. /*!
  66. * Version
  67. */
  68. typedef union Version_u
  69. {
  70. struct Version_s
  71. {
  72. uint8_t Revision;
  73. uint8_t Patch;
  74. uint8_t Minor;
  75. uint8_t Major;
  76. }Fields;
  77. uint32_t Value;
  78. }Version_t;
  79. /*!
  80. * \brief Initializes the pseudo random generator initial value
  81. *
  82. * \param [IN] seed Pseudo random generator initial value
  83. */
  84. void srand1( uint32_t seed );
  85. /*!
  86. * \brief Computes a random number between min and max
  87. *
  88. * \param [IN] min range minimum value
  89. * \param [IN] max range maximum value
  90. * \retval random random value in range min..max
  91. */
  92. int32_t randr( int32_t min, int32_t max );
  93. /*!
  94. * \brief Copies size elements of src array to dst array
  95. *
  96. * \remark STM32 Standard memcpy function only works on pointers that are aligned
  97. *
  98. * \param [OUT] dst Destination array
  99. * \param [IN] src Source array
  100. * \param [IN] size Number of bytes to be copied
  101. */
  102. void memcpy1( uint8_t *dst, const uint8_t *src, uint16_t size );
  103. /*!
  104. * \brief Copies size elements of src array to dst array reversing the byte order
  105. *
  106. * \param [OUT] dst Destination array
  107. * \param [IN] src Source array
  108. * \param [IN] size Number of bytes to be copied
  109. */
  110. void memcpyr( uint8_t *dst, const uint8_t *src, uint16_t size );
  111. /*!
  112. * \brief Set size elements of dst array with value
  113. *
  114. * \remark STM32 Standard memset function only works on pointers that are aligned
  115. *
  116. * \param [OUT] dst Destination array
  117. * \param [IN] value Default value
  118. * \param [IN] size Number of bytes to be copied
  119. */
  120. void memset1( uint8_t *dst, uint8_t value, uint16_t size );
  121. /*!
  122. * \brief Converts a nibble to an hexadecimal character
  123. *
  124. * \param [IN] a Nibble to be converted
  125. * \retval hexChar Converted hexadecimal character
  126. */
  127. int8_t Nibble2HexChar( uint8_t a );
  128. /*!
  129. * \brief Computes a CCITT 32 bits CRC
  130. *
  131. * \param [IN] buffer Data buffer used to compute the CRC
  132. * \param [IN] length Data buffer length
  133. *
  134. * \retval crc The computed buffer of length CRC
  135. */
  136. uint32_t Crc32( uint8_t *buffer, uint16_t length );
  137. /*!
  138. * \brief Computes the initial value of the CCITT 32 bits CRC. This function
  139. * can be used with functions \ref Crc32Update and \ref Crc32Finalize.
  140. *
  141. * \retval crc Initial crc value.
  142. */
  143. uint32_t Crc32Init( void );
  144. /*!
  145. * \brief Updates the value of the crc value.
  146. *
  147. * \param [IN] crcInit Previous or initial crc value.
  148. * \param [IN] buffer Data pointer.
  149. * \param [IN] length Length of the data.
  150. *
  151. * \retval crc Updated crc value.
  152. */
  153. uint32_t Crc32Update( uint32_t crcInit, uint8_t *buffer, uint16_t length );
  154. /*!
  155. * \brief Finalizes the crc value after the calls to \ref Crc32Update.
  156. *
  157. * \param [IN] crc Recent crc value.
  158. *
  159. * \retval crc Updated crc value.
  160. */
  161. uint32_t Crc32Finalize( uint32_t crc );
  162. /*!
  163. * Begins critical section
  164. */
  165. #define CRITICAL_SECTION_BEGIN( ) uint32_t mask; BoardCriticalSectionBegin( &mask )
  166. /*!
  167. * Ends critical section
  168. */
  169. #define CRITICAL_SECTION_END( ) BoardCriticalSectionEnd( &mask )
  170. /*
  171. * ============================================================================
  172. * Following functions must be implemented inside the specific platform
  173. * board.c file.
  174. * ============================================================================
  175. */
  176. /*!
  177. * Disable interrupts, begins critical section
  178. *
  179. * \param [IN] mask Pointer to a variable where to store the CPU IRQ mask
  180. */
  181. void BoardCriticalSectionBegin( uint32_t *mask );
  182. /*!
  183. * Ends critical section
  184. *
  185. * \param [IN] mask Pointer to a variable where the CPU IRQ mask was stored
  186. */
  187. void BoardCriticalSectionEnd( uint32_t *mask );
  188. #ifdef __cplusplus
  189. }
  190. #endif
  191. #endif // __UTILITIES_H__