Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

380 рядки
7.5 KiB

  1. /*-
  2. * Copyright 2022 John-Mark Gurney.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  14. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  17. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. * SUCH DAMAGE.
  24. *
  25. */
  26. #include <si_usb.h>
  27. #include <rs485frame.h>
  28. #include <misc.h>
  29. #include <stdbool.h>
  30. #include <stdint.h>
  31. #include <string.h>
  32. #include <sysinit.h>
  33. #include <usb_device.h>
  34. SYSINIT_VF(usb_cdc, SI_SUB_USB, SI_ORDER_MIDDLE, MX_USB_DEVICE_Init);
  35. /* XXX - where's a better place? */
  36. extern PCD_HandleTypeDef hpcd_USB_FS;
  37. void
  38. USB_LP_IRQHandler(void)
  39. {
  40. HAL_PCD_IRQHandler(&hpcd_USB_FS);
  41. }
  42. static int dorx = 0;
  43. static uint8_t rxbuf[128];
  44. static int gotrxdone = 0;
  45. static int gottxdone = 0;
  46. static int goterr = 0;
  47. static int rxbufsize = 0;
  48. static int rxbuftrunc = 0;
  49. static void
  50. c13led(const void *none)
  51. {
  52. GPIO_InitTypeDef GPIO_InitStruct;
  53. __HAL_RCC_GPIOB_CLK_ENABLE();
  54. __HAL_RCC_GPIOC_CLK_ENABLE();
  55. GPIO_InitStruct = (GPIO_InitTypeDef){
  56. .Pin = GPIO_PIN_13,
  57. .Mode = GPIO_MODE_OUTPUT_PP,
  58. .Pull = GPIO_NOPULL,
  59. .Speed = GPIO_SPEED_FREQ_LOW,
  60. };
  61. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  62. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
  63. }
  64. SYSINIT(c13led, SI_SUB_HAL, SI_ORDER_SECOND, c13led, NULL);
  65. #if 0
  66. #undef usb_printf
  67. #define usb_printf debug_printf
  68. #endif
  69. char *
  70. findeol(char *pos, size_t len)
  71. {
  72. while (len) {
  73. if (*pos == '\r' || *pos == '\n')
  74. return pos;
  75. pos++;
  76. len--;
  77. }
  78. return NULL;
  79. }
  80. void
  81. hexdump(const uint8_t *ptr, size_t len)
  82. {
  83. int i;
  84. for (i = 0; i < len; i++)
  85. usb_printf("%02x", ptr[i]);
  86. }
  87. void
  88. txdone(void)
  89. {
  90. gottxdone = 1;
  91. }
  92. void
  93. errfunc(void)
  94. {
  95. goterr = 1;
  96. }
  97. void
  98. rxdone(const uint8_t *payload, size_t size)
  99. {
  100. if (size > sizeof rxbuf) {
  101. size = sizeof rxbuf;
  102. rxbuftrunc = 1;
  103. }
  104. memcpy(rxbuf, payload, size);
  105. rxbufsize = size;
  106. gotrxdone = 1;
  107. }
  108. static uint8_t
  109. hexchartonib(char s)
  110. {
  111. switch (s) {
  112. case '0'...'9':
  113. return s - '0';
  114. case 'a'...'f':
  115. return s - 'a' + 10;
  116. case 'A'...'F':
  117. return s - 'A' + 10;
  118. default:
  119. return -1;
  120. }
  121. }
  122. static bool
  123. hexdecode(char *buf, size_t len, uint8_t *out)
  124. {
  125. uint8_t topchr, botchr;
  126. if (len % 2)
  127. return false;
  128. /* NB: only needed to silence a bad gcc warning */
  129. topchr = -1;
  130. while (len) {
  131. if (len % 2) {
  132. /* bottom nibble */
  133. botchr = hexchartonib(*buf);
  134. if (topchr == -1 || botchr == -1)
  135. return false;
  136. *out = topchr << 4 | botchr;
  137. out++;
  138. } else {
  139. /* top nibble */
  140. topchr = hexchartonib(*buf);
  141. }
  142. len--;
  143. buf++;
  144. }
  145. return true;
  146. }
  147. static const char pktstart[] = "pkt:";
  148. static const size_t pktstartlen = sizeof pktstart - 1;
  149. static uint8_t pktbuf[128];
  150. static void
  151. process_line(char *start, char *end)
  152. {
  153. size_t len;
  154. /* trim off leading CR/NL */
  155. while (start < end && (*start == '\r' || *start == '\n'))
  156. start++;
  157. len = end - start;
  158. if (len >= pktstartlen && memcmp(start, pktstart, sizeof pktstart - 1) == 0) {
  159. start += pktstartlen;
  160. len -= pktstartlen;
  161. if (len % 2) {
  162. usb_printf("invalid pkt len\r\n");
  163. return;
  164. }
  165. if (!hexdecode(start, len, pktbuf)) {
  166. usb_printf("invalid pkt\r\n");
  167. return;
  168. }
  169. rs485_starttx(pktbuf, len / 2);
  170. return;
  171. }
  172. usb_printf("line: %.*s", end - start, start);
  173. fflush(vcp_usb);
  174. }
  175. static void
  176. WaitForIRQ()
  177. {
  178. __disable_irq();
  179. HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
  180. __enable_irq( );
  181. }
  182. int
  183. main(void)
  184. {
  185. debug_printf("starting...\n");
  186. #if 0
  187. int i;
  188. for (i = 0; i < 5; i++) {
  189. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
  190. HAL_Delay(250);
  191. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
  192. HAL_Delay(250);
  193. }
  194. #endif
  195. setlinebuf(vcp_usb);
  196. #if 1
  197. wait_for_vcp();
  198. /*
  199. * This is required to use w/ FreeBSD. This is an issue w/ the
  200. * STM32 Core USB library:
  201. * https://github.com/STMicroelectronics/STM32CubeL1/issues/10
  202. */
  203. HAL_Delay(50);
  204. usb_printf("starting...\r\n");
  205. #endif
  206. char inpbuf[1024];
  207. char *lastcheck;
  208. char *endchr;
  209. int inpbufpos = 0;
  210. int cpylen;
  211. rs485_register(txdone, rxdone, errfunc);
  212. rs485_startrx();
  213. unsigned lasttick = -1;
  214. uint32_t gt;
  215. loop:
  216. /*
  217. * This is disabled as when it's enabled, it causes a hang
  218. * when sending USB data. The USB CDC end point never becomes
  219. * unbusy to allow the next send to progress.
  220. */
  221. #if 0
  222. /*
  223. * A ms delay isn't a big deal, so no special locking is
  224. * used to make sure we don't got to sleep w/ data pending.
  225. */
  226. WaitForIRQ();
  227. #else
  228. (void)WaitForIRQ;
  229. #endif
  230. gt = HAL_GetTick();
  231. if (lasttick != gt / 1000) {
  232. lasttick = gt / 1000;
  233. //usb_printf("tick: %u\r\n", lasttick);
  234. }
  235. if (goterr) {
  236. //usb_printf("error\r\n");
  237. goterr = 0;
  238. dorx = 1;
  239. }
  240. if (gottxdone) {
  241. usb_printf("txdone\r\n");
  242. dorx = 1;
  243. gottxdone = 0;
  244. }
  245. if (gotrxdone) {
  246. if (rxbuftrunc) {
  247. rxbuftrunc = 0;
  248. //usb_printf("rxdone: buffer overflow\r\n");
  249. } else {
  250. usb_printf("rxdone: size: %u\r\ndata: ", rxbufsize);
  251. hexdump(rxbuf, rxbufsize);
  252. usb_printf("\r\n");
  253. }
  254. dorx = 1;
  255. gotrxdone = 0;
  256. }
  257. if (dorx) {
  258. //usb_printf("dorx\r\n");
  259. rs485_startrx();
  260. //usb_printf("startrx res: %s\r\n", rs485err);
  261. dorx = 0;
  262. }
  263. /* while we have data */
  264. while (CDC_RX_LEN) {
  265. /* store last position */
  266. lastcheck = &inpbuf[inpbufpos];
  267. /* calculate how much space left */
  268. cpylen = MIN(sizeof inpbuf - inpbufpos, CDC_RX_LEN);
  269. /* copy into buffer */
  270. memcpy(&inpbuf[inpbufpos], CDC_RX_BUFFER, cpylen);
  271. /* and point to end of buffer */
  272. inpbufpos += cpylen;
  273. do {
  274. /* find first end of line characters */
  275. endchr = findeol(lastcheck, cpylen);
  276. if (endchr != NULL) {
  277. /* if so, process it */
  278. process_line(inpbuf, endchr);
  279. /* skip end of line char */
  280. endchr++;
  281. /* move remaining buffer to the beginning */
  282. memmove(inpbuf, endchr, inpbufpos - (endchr - inpbuf));
  283. /* and store new length */
  284. inpbufpos = inpbufpos - (endchr - inpbuf);
  285. /* mark begining of stream as last checked */
  286. lastcheck = inpbuf;
  287. /* and try to process another line */
  288. continue;
  289. } else if (inpbufpos == sizeof inpbuf) {
  290. /* we overflowed the buffer */
  291. /* XXX - best way is to throw away this line */
  292. inpbufpos = 0;
  293. }
  294. } while (0);
  295. /* if we copied all the data */
  296. if (cpylen == CDC_RX_LEN) {
  297. /* declare that we are ready to receive more data */
  298. CDC_RX_LEN = 0;
  299. USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  300. } else {
  301. /* if not, move the remaining to the begining and try again */
  302. memmove(CDC_RX_BUFFER, &CDC_RX_BUFFER[cpylen], CDC_RX_LEN - cpylen);
  303. CDC_RX_LEN -= cpylen;
  304. }
  305. }
  306. goto loop;
  307. }