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.
 
 
 
 
 
 

337 lines
8.9 KiB

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : usbd_cdc_if.c
  5. * @version : v2.0_Cube
  6. * @brief : Usb device for Virtual Com Port.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  11. * All rights reserved.</center></h2>
  12. *
  13. * This software component is licensed by ST under Ultimate Liberty license
  14. * SLA0044, the "License"; You may not use this file except in compliance with
  15. * the License. You may obtain a copy of the License at:
  16. * www.st.com/SLA0044
  17. *
  18. ******************************************************************************
  19. */
  20. /* USER CODE END Header */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "usbd_cdc_if.h"
  23. /* USER CODE BEGIN INCLUDE */
  24. uint8_t* CDC_RX_BUFFER = NULL ;
  25. /* USER CODE END INCLUDE */
  26. /* Private typedef -----------------------------------------------------------*/
  27. /* Private define ------------------------------------------------------------*/
  28. /* Private macro -------------------------------------------------------------*/
  29. /* USER CODE BEGIN PV */
  30. /* Private variables ---------------------------------------------------------*/
  31. /* USER CODE END PV */
  32. /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
  33. * @brief Usb device library.
  34. * @{
  35. */
  36. /** @addtogroup USBD_CDC_IF
  37. * @{
  38. */
  39. /** @defgroup USBD_CDC_IF_Private_TypesDefinitions USBD_CDC_IF_Private_TypesDefinitions
  40. * @brief Private types.
  41. * @{
  42. */
  43. /* USER CODE BEGIN PRIVATE_TYPES */
  44. /* USER CODE END PRIVATE_TYPES */
  45. /**
  46. * @}
  47. */
  48. /** @defgroup USBD_CDC_IF_Private_Defines USBD_CDC_IF_Private_Defines
  49. * @brief Private defines.
  50. * @{
  51. */
  52. /* USER CODE BEGIN PRIVATE_DEFINES */
  53. /* Define size for the receive and transmit buffer over CDC */
  54. /* It's up to user to redefine and/or remove those define */
  55. #define APP_RX_DATA_SIZE 1000
  56. #define APP_TX_DATA_SIZE 1000
  57. /* USER CODE END PRIVATE_DEFINES */
  58. /**
  59. * @}
  60. */
  61. /** @defgroup USBD_CDC_IF_Private_Macros USBD_CDC_IF_Private_Macros
  62. * @brief Private macros.
  63. * @{
  64. */
  65. /* USER CODE BEGIN PRIVATE_MACRO */
  66. /* USER CODE END PRIVATE_MACRO */
  67. /**
  68. * @}
  69. */
  70. /** @defgroup USBD_CDC_IF_Private_Variables USBD_CDC_IF_Private_Variables
  71. * @brief Private variables.
  72. * @{
  73. */
  74. /* Create buffer for reception and transmission */
  75. /* It's up to user to redefine and/or remove those define */
  76. /** Received data over USB are stored in this buffer */
  77. uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
  78. /** Data to send over USB CDC are stored in this buffer */
  79. uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
  80. /* USER CODE BEGIN PRIVATE_VARIABLES */
  81. /* USER CODE END PRIVATE_VARIABLES */
  82. /**
  83. * @}
  84. */
  85. /** @defgroup USBD_CDC_IF_Exported_Variables USBD_CDC_IF_Exported_Variables
  86. * @brief Public variables.
  87. * @{
  88. */
  89. extern USBD_HandleTypeDef hUsbDeviceFS;
  90. /* USER CODE BEGIN EXPORTED_VARIABLES */
  91. /* USER CODE END EXPORTED_VARIABLES */
  92. /**
  93. * @}
  94. */
  95. /** @defgroup USBD_CDC_IF_Private_FunctionPrototypes USBD_CDC_IF_Private_FunctionPrototypes
  96. * @brief Private functions declaration.
  97. * @{
  98. */
  99. static int8_t CDC_Init_FS(void);
  100. static int8_t CDC_DeInit_FS(void);
  101. static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length);
  102. static int8_t CDC_Receive_FS(uint8_t* pbuf, uint32_t *Len);
  103. /* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
  104. /* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */
  105. /**
  106. * @}
  107. */
  108. USBD_CDC_ItfTypeDef USBD_Interface_fops_FS =
  109. {
  110. CDC_Init_FS,
  111. CDC_DeInit_FS,
  112. CDC_Control_FS,
  113. CDC_Receive_FS
  114. };
  115. /* Private functions ---------------------------------------------------------*/
  116. /**
  117. * @brief Initializes the CDC media low layer over the FS USB IP
  118. * @retval USBD_OK if all operations are OK else USBD_FAIL
  119. */
  120. static int8_t CDC_Init_FS(void)
  121. {
  122. /* USER CODE BEGIN 3 */
  123. /* Set Application Buffers */
  124. USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0);
  125. USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);
  126. return (USBD_OK);
  127. /* USER CODE END 3 */
  128. }
  129. /**
  130. * @brief DeInitializes the CDC media low layer
  131. * @retval USBD_OK if all operations are OK else USBD_FAIL
  132. */
  133. static int8_t CDC_DeInit_FS(void)
  134. {
  135. /* USER CODE BEGIN 4 */
  136. return (USBD_OK);
  137. /* USER CODE END 4 */
  138. }
  139. /**
  140. * @brief Manage the CDC class requests
  141. * @param cmd: Command code
  142. * @param pbuf: Buffer containing command data (request parameters)
  143. * @param length: Number of data to be sent (in bytes)
  144. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  145. */
  146. static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length)
  147. {
  148. /* USER CODE BEGIN 5 */
  149. switch(cmd)
  150. {
  151. case CDC_SEND_ENCAPSULATED_COMMAND:
  152. break;
  153. case CDC_GET_ENCAPSULATED_RESPONSE:
  154. break;
  155. case CDC_SET_COMM_FEATURE:
  156. break;
  157. case CDC_GET_COMM_FEATURE:
  158. break;
  159. case CDC_CLEAR_COMM_FEATURE:
  160. break;
  161. /*******************************************************************************/
  162. /* Line Coding Structure */
  163. /*-----------------------------------------------------------------------------*/
  164. /* Offset | Field | Size | Value | Description */
  165. /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
  166. /* 4 | bCharFormat | 1 | Number | Stop bits */
  167. /* 0 - 1 Stop bit */
  168. /* 1 - 1.5 Stop bits */
  169. /* 2 - 2 Stop bits */
  170. /* 5 | bParityType | 1 | Number | Parity */
  171. /* 0 - None */
  172. /* 1 - Odd */
  173. /* 2 - Even */
  174. /* 3 - Mark */
  175. /* 4 - Space */
  176. /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
  177. /*******************************************************************************/
  178. case CDC_SET_LINE_CODING:
  179. break;
  180. case CDC_GET_LINE_CODING:
  181. break;
  182. case CDC_SET_CONTROL_LINE_STATE:
  183. break;
  184. case CDC_SEND_BREAK:
  185. break;
  186. default:
  187. break;
  188. }
  189. return (USBD_OK);
  190. /* USER CODE END 5 */
  191. }
  192. /**
  193. * @brief Data received over USB OUT endpoint are sent over CDC interface
  194. * through this function.
  195. *
  196. * @note
  197. * This function will block any OUT packet reception on USB endpoint
  198. * untill exiting this function. If you exit this function before transfer
  199. * is complete on CDC interface (ie. using DMA controller) it will result
  200. * in receiving more data while previous ones are still not sent.
  201. *
  202. * @param Buf: Buffer of data to be received
  203. * @param Len: Number of data received (in bytes)
  204. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  205. */
  206. static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
  207. {
  208. /* USER CODE BEGIN 6 */
  209. USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
  210. USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  211. CDC_RX_BUFFER = Buf;
  212. return (USBD_OK);
  213. /* USER CODE END 6 */
  214. }
  215. /**
  216. * @brief CDC_Transmit_FS
  217. * Data to send over USB IN endpoint are sent over CDC interface
  218. * through this function.
  219. * @note
  220. *
  221. *
  222. * @param Buf: Buffer of data to be sent
  223. * @param Len: Number of data to be sent (in bytes)
  224. * @retval USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
  225. */
  226. uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
  227. {
  228. uint8_t result = USBD_OK;
  229. /* USER CODE BEGIN 7 */
  230. USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
  231. if (hcdc->TxState != 0){
  232. return USBD_BUSY;
  233. }
  234. USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
  235. result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
  236. /* USER CODE END 7 */
  237. return result;
  238. }
  239. /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
  240. #include <stdarg.h>
  241. uint16_t vcp_status( USBD_SetupReqTypedef *req){
  242. if(req->bRequest ==CDC_SET_CONTROL_LINE_STATE){
  243. if(req->wValue){
  244. return 1;
  245. }
  246. }
  247. return 0;
  248. }
  249. void usb_printf(const char *format, ...)
  250. {
  251. va_list args;
  252. uint32_t length;
  253. va_start(args, format);
  254. length = vsnprintf((char *)UserTxBufferFS, APP_TX_DATA_SIZE, (char *)format, args);
  255. va_end(args);
  256. for (;;) {
  257. if (CDC_Transmit_FS(UserTxBufferFS, length) == USBD_BUSY) {
  258. HAL_Delay(1);
  259. continue;
  260. }
  261. break;
  262. }
  263. }
  264. /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
  265. /**
  266. * @}
  267. */
  268. /**
  269. * @}
  270. */
  271. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/