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.
 
 
 
 
 
 

343 lines
7.3 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 <misc.h>
  28. #include <stdbool.h>
  29. #include <stdint.h>
  30. #include <string.h>
  31. #include <sysinit.h>
  32. SYSINIT(hal_init, SI_SUB_HAL, SI_ORDER_FIRST, (void (*)(const void *))HAL_Init, NULL);
  33. static void
  34. c13led(const void *none)
  35. {
  36. GPIO_InitTypeDef GPIO_InitStruct;
  37. __HAL_RCC_GPIOB_CLK_ENABLE();
  38. __HAL_RCC_GPIOC_CLK_ENABLE();
  39. GPIO_InitStruct = (GPIO_InitTypeDef){
  40. .Pin = GPIO_PIN_13,
  41. .Mode = GPIO_MODE_OUTPUT_PP,
  42. .Pull = GPIO_NOPULL,
  43. .Speed = GPIO_SPEED_FREQ_LOW,
  44. };
  45. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  46. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
  47. }
  48. SYSINIT(c13led, SI_SUB_HAL, SI_ORDER_SECOND, c13led, NULL);
  49. /*
  50. * Referenced from:
  51. * Projects/STM32F103RB-Nucleo/Applications/USB_Device/HID_Standalone/Src/main.c
  52. */
  53. static void
  54. oscconfig(const void *none)
  55. {
  56. RCC_ClkInitTypeDef clkinitstruct;
  57. RCC_OscInitTypeDef oscinitstruct;
  58. RCC_PeriphCLKInitTypeDef rccperiphclkinit;
  59. __HAL_RCC_PWR_CLK_ENABLE();
  60. oscinitstruct = (RCC_OscInitTypeDef){
  61. .OscillatorType = RCC_OSCILLATORTYPE_HSE,
  62. .HSEState = RCC_HSE_ON,
  63. .HSEPredivValue = RCC_HSE_PREDIV_DIV1,
  64. .PLL.PLLMUL = RCC_PLL_MUL9,
  65. .PLL.PLLState = RCC_PLL_ON,
  66. .PLL.PLLSource = RCC_PLLSOURCE_HSE,
  67. };
  68. HAL_RCC_OscConfig(&oscinitstruct);
  69. /* USB clock selection */
  70. rccperiphclkinit = (RCC_PeriphCLKInitTypeDef){
  71. .PeriphClockSelection = RCC_PERIPHCLK_USB,
  72. .UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5,
  73. };
  74. HAL_RCCEx_PeriphCLKConfig(&rccperiphclkinit);
  75. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  76. clocks dividers */
  77. clkinitstruct = (RCC_ClkInitTypeDef){
  78. .ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2),
  79. .SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK,
  80. .AHBCLKDivider = RCC_SYSCLK_DIV1,
  81. .APB1CLKDivider = RCC_HCLK_DIV2,
  82. .APB2CLKDivider = RCC_HCLK_DIV1,
  83. };
  84. HAL_RCC_ClockConfig(&clkinitstruct, FLASH_LATENCY_2);
  85. }
  86. SYSINIT(oscconfig, SI_SUB_HAL, SI_ORDER_THIRD, oscconfig, NULL);
  87. char *
  88. findeol(char *pos, size_t len)
  89. {
  90. while (len) {
  91. if (*pos == '\r' || *pos == '\n')
  92. return pos;
  93. pos++;
  94. len--;
  95. }
  96. return NULL;
  97. }
  98. void
  99. hexdump(const uint8_t *ptr, size_t len)
  100. {
  101. int i;
  102. for (i = 0; i < len; i++)
  103. usb_printf("%02x", ptr[i]);
  104. }
  105. void
  106. txdone(void)
  107. {
  108. usb_printf("txdone\r\n");
  109. //Radio.Rx(0);
  110. }
  111. void
  112. txtimeout(void)
  113. {
  114. usb_printf("txtimeout\r\n");
  115. }
  116. void
  117. rxdone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
  118. {
  119. usb_printf("rxdone: size: %hu, rssi: %hd, snr: %d\r\ndata: ", size, rssi, snr);
  120. hexdump(payload, size);
  121. usb_printf("\r\n");
  122. }
  123. void
  124. rxtimeout(void)
  125. {
  126. usb_printf("rxtimeout\r\n");
  127. }
  128. void
  129. rxerr(void)
  130. {
  131. usb_printf("rxerr\r\n");
  132. }
  133. static uint8_t
  134. hexchartonib(char s)
  135. {
  136. switch (s) {
  137. case '0'...'9':
  138. return s - '0';
  139. case 'a'...'f':
  140. return s - 'a' + 10;
  141. case 'A'...'F':
  142. return s - 'A' + 10;
  143. default:
  144. return -1;
  145. }
  146. }
  147. static bool
  148. hexdecode(char *buf, size_t len, uint8_t *out)
  149. {
  150. uint8_t topchr, botchr;
  151. if (len % 2)
  152. return false;
  153. /* NB: only needed to silence a bad gcc warning */
  154. topchr = -1;
  155. while (len) {
  156. if (len % 2) {
  157. /* bottom nibble */
  158. botchr = hexchartonib(*buf);
  159. if (topchr == -1 || botchr == -1)
  160. return false;
  161. *out = topchr << 4 | botchr;
  162. out++;
  163. } else {
  164. /* top nibble */
  165. topchr = hexchartonib(*buf);
  166. }
  167. len--;
  168. buf++;
  169. }
  170. return true;
  171. }
  172. static const char pktstart[] = "pkt:";
  173. static const size_t pktstartlen = sizeof pktstart - 1;
  174. static uint8_t pktbuf[128];
  175. static void
  176. process_line(char *start, char *end)
  177. {
  178. size_t len;
  179. /* trim off leading CR/NL */
  180. while (start < end && (*start == '\r' || *start == '\n'))
  181. start++;
  182. len = end - start;
  183. if (len >= pktstartlen && memcmp(start, pktstart, sizeof pktstart - 1) == 0) {
  184. start += pktstartlen;
  185. len -= pktstartlen;
  186. if (len % 2) {
  187. usb_printf("invalid pkt len\r\n");
  188. return;
  189. }
  190. if (!hexdecode(start, len, pktbuf)) {
  191. usb_printf("invalid pkt\r\n");
  192. return;
  193. }
  194. //Radio.Send(pktbuf, len / 2);
  195. return;
  196. }
  197. usb_printf("line: %.*s", end - start, start);
  198. fflush(vcp_usb);
  199. }
  200. int
  201. main(void)
  202. {
  203. sysinit_run();
  204. //debug_printf("starting...\n");
  205. #if 1
  206. int i;
  207. for (i = 0; i < 5; i++) {
  208. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
  209. HAL_Delay(250);
  210. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
  211. HAL_Delay(250);
  212. }
  213. #endif
  214. setlinebuf(vcp_usb);
  215. #if 1
  216. wait_for_vcp();
  217. /*
  218. * This is required to use w/ FreeBSD. This is an issue w/ the
  219. * STM32 Core USB library:
  220. * https://github.com/STMicroelectronics/STM32CubeL1/issues/10
  221. */
  222. HAL_Delay(50);
  223. usb_printf("starting...\r\n");
  224. #endif
  225. char inpbuf[1024];
  226. char *lastcheck;
  227. char *endchr;
  228. int inpbufpos = 0;
  229. int cpylen;
  230. loop:
  231. //BoardLowPowerHandler();
  232. /* while we have data */
  233. while (CDC_RX_LEN) {
  234. /* store last position */
  235. lastcheck = &inpbuf[inpbufpos];
  236. /* calculate how much space left */
  237. cpylen = MIN(sizeof inpbuf - inpbufpos, CDC_RX_LEN);
  238. /* copy into buffer */
  239. memcpy(&inpbuf[inpbufpos], CDC_RX_BUFFER, cpylen);
  240. /* and point to end of buffer */
  241. inpbufpos += cpylen;
  242. do {
  243. /* find first end of line characters */
  244. endchr = findeol(lastcheck, cpylen);
  245. if (endchr != NULL) {
  246. /* if so, process it */
  247. process_line(inpbuf, endchr);
  248. /* skip end of line char */
  249. endchr++;
  250. /* move remaining buffer to the beginning */
  251. memmove(inpbuf, endchr, inpbufpos - (endchr - inpbuf));
  252. /* and store new length */
  253. inpbufpos = inpbufpos - (endchr - inpbuf);
  254. /* mark begining of stream as last checked */
  255. lastcheck = inpbuf;
  256. /* and try to process another line */
  257. continue;
  258. } else if (inpbufpos == sizeof inpbuf) {
  259. /* we overflowed the buffer */
  260. /* XXX - best way is to throw away this line */
  261. inpbufpos = 0;
  262. }
  263. } while (0);
  264. /* if we copied all the data */
  265. if (cpylen == CDC_RX_LEN) {
  266. /* declare that we are ready to receive more data */
  267. CDC_RX_LEN = 0;
  268. USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  269. } else {
  270. /* if not, move the remaining to the begining and try again */
  271. memmove(CDC_RX_BUFFER, &CDC_RX_BUFFER[cpylen], CDC_RX_LEN - cpylen);
  272. CDC_RX_LEN -= cpylen;
  273. }
  274. }
  275. goto loop;
  276. }