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.
 
 
 
 
 
 

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