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.
 
 
 
 
 
 

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