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.
 
 
 
 
 
 

258 lines
7.7 KiB

  1. /*!
  2. * \file CayenneLpp.c
  3. *
  4. * \brief Implements the Cayenne Low Power Protocol
  5. *
  6. * \copyright Revised BSD License, see section \ref LICENSE.
  7. *
  8. * \code
  9. * ______ _
  10. * / _____) _ | |
  11. * ( (____ _____ ____ _| |_ _____ ____| |__
  12. * \____ \| ___ | (_ _) ___ |/ ___) _ \
  13. * _____) ) ____| | | || |_| ____( (___| | | |
  14. * (______/|_____)_|_|_| \__)_____)\____)_| |_|
  15. * (C)2013-2018 Semtech
  16. *
  17. * \endcode
  18. *
  19. * \author Miguel Luis ( Semtech )
  20. */
  21. #include <stdint.h>
  22. #include "utilities.h"
  23. #include "CayenneLpp.h"
  24. #define CAYENNE_LPP_MAXBUFFER_SIZE 242
  25. static uint8_t CayenneLppBuffer[CAYENNE_LPP_MAXBUFFER_SIZE];
  26. static uint8_t CayenneLppCursor = 0;
  27. void CayenneLppInit( void )
  28. {
  29. CayenneLppCursor = 0;
  30. }
  31. void CayenneLppReset( void )
  32. {
  33. CayenneLppCursor = 0;
  34. }
  35. uint8_t CayenneLppGetSize( void )
  36. {
  37. return CayenneLppCursor;
  38. }
  39. uint8_t* CayenneLppGetBuffer( void )
  40. {
  41. return CayenneLppBuffer;
  42. }
  43. uint8_t CayenneLppCopy( uint8_t* dst )
  44. {
  45. memcpy1( dst, CayenneLppBuffer, CayenneLppCursor );
  46. return CayenneLppCursor;
  47. }
  48. uint8_t CayenneLppAddDigitalInput( uint8_t channel, uint8_t value )
  49. {
  50. if( ( CayenneLppCursor + LPP_DIGITAL_INPUT_SIZE ) > CAYENNE_LPP_MAXBUFFER_SIZE )
  51. {
  52. return 0;
  53. }
  54. CayenneLppBuffer[CayenneLppCursor++] = channel;
  55. CayenneLppBuffer[CayenneLppCursor++] = LPP_DIGITAL_INPUT;
  56. CayenneLppBuffer[CayenneLppCursor++] = value;
  57. return CayenneLppCursor;
  58. }
  59. uint8_t CayenneLppAddDigitalOutput( uint8_t channel, uint8_t value )
  60. {
  61. if( ( CayenneLppCursor + LPP_DIGITAL_OUTPUT_SIZE ) > CAYENNE_LPP_MAXBUFFER_SIZE )
  62. {
  63. return 0;
  64. }
  65. CayenneLppBuffer[CayenneLppCursor++] = channel;
  66. CayenneLppBuffer[CayenneLppCursor++] = LPP_DIGITAL_OUTPUT;
  67. CayenneLppBuffer[CayenneLppCursor++] = value;
  68. return CayenneLppCursor;
  69. }
  70. uint8_t CayenneLppAddAnalogInput( uint8_t channel, float value )
  71. {
  72. if( ( CayenneLppCursor + LPP_ANALOG_INPUT_SIZE ) > CAYENNE_LPP_MAXBUFFER_SIZE )
  73. {
  74. return 0;
  75. }
  76. int16_t val = ( int16_t ) ( value * 100 );
  77. CayenneLppBuffer[CayenneLppCursor++] = channel;
  78. CayenneLppBuffer[CayenneLppCursor++] = LPP_ANALOG_INPUT;
  79. CayenneLppBuffer[CayenneLppCursor++] = val >> 8;
  80. CayenneLppBuffer[CayenneLppCursor++] = val;
  81. return CayenneLppCursor;
  82. }
  83. uint8_t CayenneLppAddAnalogOutput( uint8_t channel, float value )
  84. {
  85. if( ( CayenneLppCursor + LPP_ANALOG_OUTPUT_SIZE ) > CAYENNE_LPP_MAXBUFFER_SIZE )
  86. {
  87. return 0;
  88. }
  89. int16_t val = ( int16_t ) ( value * 100 );
  90. CayenneLppBuffer[CayenneLppCursor++] = channel;
  91. CayenneLppBuffer[CayenneLppCursor++] = LPP_ANALOG_OUTPUT;
  92. CayenneLppBuffer[CayenneLppCursor++] = val >> 8;
  93. CayenneLppBuffer[CayenneLppCursor++] = val;
  94. return CayenneLppCursor;
  95. }
  96. uint8_t CayenneLppAddLuminosity( uint8_t channel, uint16_t lux )
  97. {
  98. if( ( CayenneLppCursor + LPP_LUMINOSITY_SIZE ) > CAYENNE_LPP_MAXBUFFER_SIZE )
  99. {
  100. return 0;
  101. }
  102. CayenneLppBuffer[CayenneLppCursor++] = channel;
  103. CayenneLppBuffer[CayenneLppCursor++] = LPP_LUMINOSITY;
  104. CayenneLppBuffer[CayenneLppCursor++] = lux >> 8;
  105. CayenneLppBuffer[CayenneLppCursor++] = lux;
  106. return CayenneLppCursor;
  107. }
  108. uint8_t CayenneLppAddPresence( uint8_t channel, uint8_t value )
  109. {
  110. if( ( CayenneLppCursor + LPP_PRESENCE_SIZE ) > CAYENNE_LPP_MAXBUFFER_SIZE )
  111. {
  112. return 0;
  113. }
  114. CayenneLppBuffer[CayenneLppCursor++] = channel;
  115. CayenneLppBuffer[CayenneLppCursor++] = LPP_PRESENCE;
  116. CayenneLppBuffer[CayenneLppCursor++] = value;
  117. return CayenneLppCursor;
  118. }
  119. uint8_t CayenneLppAddTemperature( uint8_t channel, float celsius )
  120. {
  121. if( ( CayenneLppCursor + LPP_TEMPERATURE_SIZE ) > CAYENNE_LPP_MAXBUFFER_SIZE )
  122. {
  123. return 0;
  124. }
  125. int16_t val = ( int16_t) ( celsius * 10 );
  126. CayenneLppBuffer[CayenneLppCursor++] = channel;
  127. CayenneLppBuffer[CayenneLppCursor++] = LPP_TEMPERATURE;
  128. CayenneLppBuffer[CayenneLppCursor++] = val >> 8;
  129. CayenneLppBuffer[CayenneLppCursor++] = val;
  130. return CayenneLppCursor;
  131. }
  132. uint8_t CayenneLppAddRelativeHumidity( uint8_t channel, float rh )
  133. {
  134. if( ( CayenneLppCursor + LPP_RELATIVE_HUMIDITY_SIZE ) > CAYENNE_LPP_MAXBUFFER_SIZE )
  135. {
  136. return 0;
  137. }
  138. CayenneLppBuffer[CayenneLppCursor++] = channel;
  139. CayenneLppBuffer[CayenneLppCursor++] = LPP_RELATIVE_HUMIDITY;
  140. CayenneLppBuffer[CayenneLppCursor++] = (uint8_t ) ( rh * 2 );
  141. return CayenneLppCursor;
  142. }
  143. uint8_t CayenneLppAddAccelerometer( uint8_t channel, float x, float y, float z )
  144. {
  145. if( ( CayenneLppCursor + LPP_ACCELEROMETER_SIZE ) > CAYENNE_LPP_MAXBUFFER_SIZE )
  146. {
  147. return 0;
  148. }
  149. int16_t vx = ( int16_t ) ( x * 1000 );
  150. int16_t vy = ( int16_t ) ( y * 1000 );
  151. int16_t vz = ( int16_t ) ( z * 1000 );
  152. CayenneLppBuffer[CayenneLppCursor++] = channel;
  153. CayenneLppBuffer[CayenneLppCursor++] = LPP_ACCELEROMETER;
  154. CayenneLppBuffer[CayenneLppCursor++] = vx >> 8;
  155. CayenneLppBuffer[CayenneLppCursor++] = vx;
  156. CayenneLppBuffer[CayenneLppCursor++] = vy >> 8;
  157. CayenneLppBuffer[CayenneLppCursor++] = vy;
  158. CayenneLppBuffer[CayenneLppCursor++] = vz >> 8;
  159. CayenneLppBuffer[CayenneLppCursor++] = vz;
  160. return CayenneLppCursor;
  161. }
  162. uint8_t CayenneLppAddBarometricPressure( uint8_t channel, float hpa )
  163. {
  164. if( ( CayenneLppCursor + LPP_BAROMETRIC_PRESSURE_SIZE ) > CAYENNE_LPP_MAXBUFFER_SIZE )
  165. {
  166. return 0;
  167. }
  168. int16_t val = ( int16_t ) ( hpa * 10 );
  169. CayenneLppBuffer[CayenneLppCursor++] = channel;
  170. CayenneLppBuffer[CayenneLppCursor++] = LPP_BAROMETRIC_PRESSURE;
  171. CayenneLppBuffer[CayenneLppCursor++] = val >> 8;
  172. CayenneLppBuffer[CayenneLppCursor++] = val;
  173. return CayenneLppCursor;
  174. }
  175. uint8_t CayenneLppAddGyrometer( uint8_t channel, float x, float y, float z )
  176. {
  177. if( ( CayenneLppCursor + LPP_GYROMETER_SIZE ) > CAYENNE_LPP_MAXBUFFER_SIZE )
  178. {
  179. return 0;
  180. }
  181. int16_t vx = ( int16_t ) ( x * 100 );
  182. int16_t vy = ( int16_t ) ( y * 100 );
  183. int16_t vz = ( int16_t ) ( z * 100 );
  184. CayenneLppBuffer[CayenneLppCursor++] = channel;
  185. CayenneLppBuffer[CayenneLppCursor++] = LPP_GYROMETER;
  186. CayenneLppBuffer[CayenneLppCursor++] = vx >> 8;
  187. CayenneLppBuffer[CayenneLppCursor++] = vx;
  188. CayenneLppBuffer[CayenneLppCursor++] = vy >> 8;
  189. CayenneLppBuffer[CayenneLppCursor++] = vy;
  190. CayenneLppBuffer[CayenneLppCursor++] = vz >> 8;
  191. CayenneLppBuffer[CayenneLppCursor++] = vz;
  192. return CayenneLppCursor;
  193. }
  194. uint8_t CayenneLppAddGps( uint8_t channel, float latitude, float longitude, float meters )
  195. {
  196. if( ( CayenneLppCursor + LPP_GPS_SIZE ) > CAYENNE_LPP_MAXBUFFER_SIZE )
  197. {
  198. return 0;
  199. }
  200. int32_t lat = ( int32_t ) ( latitude * 10000 );
  201. int32_t lon = ( int32_t ) ( longitude * 10000 );
  202. int32_t alt = ( int32_t ) ( meters * 100 );
  203. CayenneLppBuffer[CayenneLppCursor++] = channel;
  204. CayenneLppBuffer[CayenneLppCursor++] = LPP_GPS;
  205. CayenneLppBuffer[CayenneLppCursor++] = lat >> 16;
  206. CayenneLppBuffer[CayenneLppCursor++] = lat >> 8;
  207. CayenneLppBuffer[CayenneLppCursor++] = lat;
  208. CayenneLppBuffer[CayenneLppCursor++] = lon >> 16;
  209. CayenneLppBuffer[CayenneLppCursor++] = lon >> 8;
  210. CayenneLppBuffer[CayenneLppCursor++] = lon;
  211. CayenneLppBuffer[CayenneLppCursor++] = alt >> 16;
  212. CayenneLppBuffer[CayenneLppCursor++] = alt >> 8;
  213. CayenneLppBuffer[CayenneLppCursor++] = alt;
  214. return CayenneLppCursor;
  215. }