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
7.3 KiB

  1. /*-
  2. * Copyright 2021 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 <usbd_cdc_if.h>
  27. #include <strobe_rng_init.h>
  28. #include <string.h>
  29. #include <board.h>
  30. #include <misc.h>
  31. #include <radio.h>
  32. #include <delay.h>
  33. char *
  34. findeol(char *pos, size_t len)
  35. {
  36. while (len) {
  37. if (*pos == '\r' || *pos == '\n')
  38. return pos;
  39. pos++;
  40. len--;
  41. }
  42. return NULL;
  43. }
  44. void
  45. hexdump(const uint8_t *ptr, size_t len)
  46. {
  47. int i;
  48. for (i = 0; i < len; i++)
  49. usb_printf("%02x", ptr[i]);
  50. }
  51. void
  52. txdone(void)
  53. {
  54. usb_printf("txdone\r\n");
  55. }
  56. void
  57. txtimeout(void)
  58. {
  59. usb_printf("txtimeout\r\n");
  60. }
  61. void
  62. rxdone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
  63. {
  64. usb_printf("rxdone: size: %hu, rssi: %hd, snr: %d\r\ndata: ", size, rssi, snr);
  65. hexdump(payload, size);
  66. usb_printf("\r\n");
  67. }
  68. void
  69. rxtimeout(void)
  70. {
  71. usb_printf("rxtimeout\r\n");
  72. }
  73. void
  74. rxerr(void)
  75. {
  76. usb_printf("rxerr\r\n");
  77. }
  78. RadioEvents_t revents = {
  79. .TxDone = txdone,
  80. .TxTimeout = txtimeout,
  81. .RxDone = rxdone,
  82. .RxTimeout = rxtimeout,
  83. .RxError = rxerr,
  84. };
  85. static uint8_t
  86. hexchartonib(char s)
  87. {
  88. switch (s) {
  89. case '0'...'9':
  90. return s - '0';
  91. case 'a'...'f':
  92. return s - 'a' + 10;
  93. case 'A'...'F':
  94. return s - 'A' + 10;
  95. default:
  96. return -1;
  97. }
  98. }
  99. static bool
  100. hexdecode(char *buf, size_t len, uint8_t *out)
  101. {
  102. uint8_t topchr, botchr;
  103. if (len % 2)
  104. return false;
  105. /* NB: only needed to silence a bad gcc warning */
  106. topchr = -1;
  107. while (len) {
  108. if (len % 2) {
  109. /* bottom nibble */
  110. botchr = hexchartonib(*buf);
  111. if (topchr == -1 || botchr == -1)
  112. return false;
  113. *out = topchr << 4 | botchr;
  114. out++;
  115. } else {
  116. /* top nibble */
  117. topchr = hexchartonib(*buf);
  118. }
  119. len--;
  120. buf++;
  121. }
  122. return true;
  123. }
  124. static const char pktstart[] = "pkt:";
  125. static const size_t pktstartlen = sizeof pktstart - 1;
  126. static uint8_t pktbuf[128];
  127. static void
  128. process_line(char *start, char *end)
  129. {
  130. size_t len;
  131. /* trim off leading CR/NL */
  132. while (start < end && (*start == '\r' || *start == '\n'))
  133. start++;
  134. len = end - start;
  135. if (len >= pktstartlen && memcmp(start, pktstart, sizeof pktstart - 1) == 0) {
  136. start += pktstartlen;
  137. len -= pktstartlen;
  138. if (len % 2) {
  139. usb_printf("invalid pkt len\r\n");
  140. return;
  141. }
  142. if (!hexdecode(start, len, pktbuf)) {
  143. usb_printf("invalid pkt\r\n");
  144. return;
  145. }
  146. Radio.Send(pktbuf, len / 2);
  147. return;
  148. }
  149. usb_printf("line: %.*s", end - start, start);
  150. }
  151. /*
  152. * Seed the randomness from the radio. This is not a great
  153. * seed, and is hard to gauge how much randomness is really
  154. * there. Assuming about 1 bit per 8 bits looks pretty safe,
  155. * so add 256 * 8 / 32 words.
  156. */
  157. static void
  158. radio_seed_rng(void)
  159. {
  160. #if 1
  161. uint32_t v;
  162. int i;
  163. for (i = 0; i < 256 * 8 / 32; i++) {
  164. v = Radio.Random();
  165. strobe_seed_prng((uint8_t *)&v, sizeof v);
  166. }
  167. #endif
  168. }
  169. int
  170. main(void)
  171. {
  172. uint8_t bytes[8];
  173. strobe_rng_init();
  174. BoardInitMcu();
  175. Radio.Init(&revents);
  176. radio_seed_rng();
  177. strobe_rng_save();
  178. /* turn on LED */
  179. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
  180. #if 1
  181. wait_for_vcp();
  182. usb_printf("starting...\r\n");
  183. bare_strobe_randomize(bytes, sizeof bytes);
  184. hexdump(bytes, sizeof bytes);
  185. usb_printf("\r\n");
  186. #endif
  187. uint32_t v;
  188. usb_printf("gs: %#x\r\n", Radio.GetStatus());
  189. usb_printf("set modem\r\n");
  190. Radio.SetModem(MODEM_LORA);
  191. usb_printf("check rffreq: %d\r\n", (int)Radio.CheckRfFrequency(914350 * 1000));
  192. usb_printf("set channel\r\n");
  193. Radio.SetChannel(914350 * 1000);
  194. v = Radio.Random();
  195. usb_printf("rr: %#x\r\n", v);
  196. usb_printf("rssi: %#hx\r\n", Radio.Rssi(MODEM_LORA));
  197. /* RX/TX parameters */
  198. const uint8_t modem = MODEM_LORA;
  199. const uint8_t bandwidth = 0 /* 128 kHz */;
  200. const uint8_t datarate = 7 /* 128 chips */;
  201. const uint8_t coderate = 1 /* 4/5 */;
  202. const uint8_t preambleLen = 4 /* symbols */;
  203. const uint8_t fixLen = 0 /* variable */;
  204. const uint8_t crcOn = 1 /* on */;
  205. const uint8_t freqHopOn = 0 /* off */;
  206. const bool iqInverted = false /* not inverted */;
  207. Radio.SetRxConfig(modem, bandwidth, datarate, coderate, 0/*afc*/,
  208. preambleLen, 5/*symTimeout*/, fixLen, 0/*payloadlen*/, crcOn,
  209. freqHopOn, 0/*hopPeriod*/, iqInverted, true/*rxcont*/);
  210. Radio.SetTxConfig(modem, 11/*power*/, 0/*fdev*/, bandwidth, datarate,
  211. coderate, preambleLen, fixLen, crcOn, freqHopOn, 0/*hopPeriod*/,
  212. iqInverted, 1000/*timeout*/);
  213. uint8_t sendmsg[] = "testing lora123";
  214. usb_printf("sending...\r\n");
  215. Radio.Send(sendmsg, sizeof sendmsg);
  216. DelayMs(200);
  217. Radio.Send(sendmsg, sizeof sendmsg);
  218. DelayMs(200);
  219. Radio.Send(sendmsg, sizeof sendmsg);
  220. DelayMs(200);
  221. usb_printf("rx(0)...\r\n");
  222. Radio.Rx(0);
  223. char inpbuf[1024];
  224. char *lastcheck;
  225. char *endchr;
  226. int inpbufpos = 0;
  227. int cpylen;
  228. loop:
  229. BoardLowPowerHandler();
  230. if (Radio.IrqProcess != NULL)
  231. Radio.IrqProcess();
  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. }