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.
 
 
 
 
 
 

420 lines
8.6 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 "stm32l1xx.h"
  27. #include "stm32l1xx_hal.h"
  28. /* LoRaMac headers */
  29. #include <board.h>
  30. #include <adc.h>
  31. #include <radio.h>
  32. #include <delay.h>
  33. /* lora-irr headers */
  34. #include <misc.h>
  35. #include <strobe_rng_init.h>
  36. #include <comms.h>
  37. enum {
  38. CMD_TERMINATE = 1,
  39. CMD_WAITFOR = 2,
  40. CMD_RUNFOR = 3,
  41. CMD_PING = 4,
  42. CMD_SETUNSET = 5,
  43. };
  44. /*
  45. * rxpktavail is initialized to true meaning that the data in rxpkt
  46. * can be over written. When a packet is received, the data is copied
  47. * to rxpkt, and then rxpktavail is set to false. Once the packet has
  48. * been processed, it is set back to true.
  49. */
  50. static uint8_t rxpkt[128];
  51. static struct pktbuf rxpktbuf;
  52. static volatile bool rxpktavail;
  53. static uint8_t shared_key[] = "foobar";
  54. static struct pktbuf shared_key_buf = (struct pktbuf){
  55. .pkt = shared_key,
  56. .pktlen = sizeof shared_key - 1,
  57. };
  58. static struct comms_state cs;
  59. void
  60. txdone(void)
  61. {
  62. /* restart Rx when Tx done */
  63. Radio.Rx(0);
  64. }
  65. void
  66. txtimeout(void)
  67. {
  68. /* restart Rx when Tx done */
  69. Radio.Rx(0);
  70. }
  71. void
  72. rxdone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
  73. {
  74. if (rxpktavail) {
  75. memcpy(rxpkt, payload, MIN(sizeof rxpkt, size));
  76. rxpktbuf = (struct pktbuf){
  77. .pkt = rxpkt,
  78. .pktlen = size,
  79. };
  80. rxpktavail = false;
  81. }
  82. }
  83. void
  84. rxtimeout(void)
  85. {
  86. }
  87. void
  88. rxerr(void)
  89. {
  90. }
  91. RadioEvents_t revents = {
  92. .TxDone = txdone,
  93. .TxTimeout = txtimeout,
  94. .RxDone = rxdone,
  95. .RxTimeout = rxtimeout,
  96. .RxError = rxerr,
  97. };
  98. /*
  99. * Seed the randomness from the radio. This is not a great
  100. * seed, and is hard to gauge how much randomness is really
  101. * there. Assuming about 1 bit per 8 bits looks pretty safe,
  102. * so add 256 * 8 / 32 words.
  103. */
  104. static void
  105. radio_seed_rng(void)
  106. {
  107. #if 0
  108. uint32_t v;
  109. int i;
  110. for (i = 0; i < 256 * 8 / 32; i++) {
  111. v = Radio.Random();
  112. strobe_seed_prng((uint8_t *)&v, sizeof v);
  113. }
  114. #endif
  115. }
  116. static void
  117. analog_seed_rng(void)
  118. {
  119. #if 1
  120. uint16_t v;
  121. int i;
  122. for (i = 0; i < 256 / 2; i++) {
  123. /*
  124. * Capture some ADC data. If pin is floating, 0xfff
  125. * happens frequently, if pin is grounded, 0 happens
  126. * frequently, filter these values out.
  127. */
  128. do {
  129. v = AdcReadChannel(&Adc, ADC_CHANNEL_21);
  130. } while (v == 0 || v == 0xfff);
  131. strobe_seed_prng((uint8_t *)&v, sizeof v);
  132. }
  133. #endif
  134. }
  135. static inline uint32_t
  136. letoh_32(uint8_t *v)
  137. {
  138. return v[0] | (v[1] << 8) | (v[2] << 16) | (v[3] << 24);
  139. }
  140. struct chaninfo {
  141. GPIO_TypeDef *bank;
  142. uint16_t pinnum;
  143. bool init;
  144. bool invert;
  145. } chans[] = {
  146. [0] = { .bank = GPIOB, .pinnum = GPIO_PIN_5, .init = true, .invert = true, },
  147. [1] = { .bank = GPIOB, .pinnum = GPIO_PIN_6, .init = true, .invert = true, },
  148. [2] = { .bank = GPIOB, .pinnum = GPIO_PIN_7, .init = true, .invert = true, },
  149. [3] = { .bank = GPIOB, .pinnum = GPIO_PIN_9, .init = true, .invert = true, },
  150. [4] = { .bank = GPIOB, .pinnum = GPIO_PIN_8, .init = true, },
  151. };
  152. #define nitems(x) (sizeof(x) / sizeof *(x))
  153. static void
  154. set_chan(uint32_t chan, bool val)
  155. {
  156. struct chaninfo ci;
  157. if (chan < nitems(chans)) {
  158. ci = chans[chan];
  159. HAL_GPIO_WritePin(ci.bank, ci.pinnum, val ^ ci.invert ?
  160. GPIO_PIN_SET : GPIO_PIN_RESET);
  161. }
  162. }
  163. static void
  164. setup_gpio()
  165. {
  166. GPIO_InitTypeDef GPIO_InitStruct;
  167. int i;
  168. for (i = 0; i < nitems(chans); i++) {
  169. GPIO_InitStruct = (GPIO_InitTypeDef){
  170. .Pin = chans[i].pinnum,
  171. .Mode = GPIO_MODE_OUTPUT_PP,
  172. .Pull = GPIO_NOPULL,
  173. .Speed = GPIO_SPEED_FREQ_LOW,
  174. };
  175. HAL_GPIO_Init(chans[i].bank, &GPIO_InitStruct);
  176. set_chan(i, chans[i].init);
  177. }
  178. }
  179. static struct sched {
  180. uint32_t cmd;
  181. uint32_t end_wait_tick; /* end if running, otherwise how long to wait */
  182. uint32_t chan;
  183. } schedule[20];
  184. static int schedpos; /* position in schedule, % nitems(schedule)*/
  185. static int schedcnt; /* total items waiting */
  186. #define SCHED_HEAD (schedule[(schedpos) % nitems(schedule)])
  187. #define SCHED_TAIL (schedule[(schedpos + schedcnt) % nitems(schedule)])
  188. static void
  189. start_sched(struct sched *sched)
  190. {
  191. sched->end_wait_tick += uwTick;
  192. if (sched->cmd == CMD_RUNFOR)
  193. set_chan(sched->chan, 1);
  194. }
  195. static void
  196. process_sched()
  197. {
  198. /* nothing to do? */
  199. if (schedcnt == 0)
  200. return;
  201. /* not yet expired */
  202. if (uwTick < SCHED_HEAD.end_wait_tick)
  203. return;
  204. if (SCHED_HEAD.cmd == CMD_RUNFOR)
  205. set_chan(SCHED_HEAD.chan, 0);
  206. /* we are done, advance */
  207. schedpos++;
  208. schedcnt--;
  209. if (schedcnt)
  210. start_sched(&SCHED_HEAD);
  211. }
  212. static void
  213. enqueue_sched(uint32_t cmd, uint32_t ticks, uint32_t chan)
  214. {
  215. if (schedcnt >= nitems(schedule))
  216. return;
  217. SCHED_TAIL = (struct sched){
  218. .cmd = cmd,
  219. .end_wait_tick = ticks,
  220. .chan = chan,
  221. };
  222. if (schedcnt == 0)
  223. start_sched(&SCHED_HEAD);
  224. schedcnt++;
  225. }
  226. static void
  227. procmsg(struct pktbuf inbuf, struct pktbuf *outbuf)
  228. {
  229. uint32_t args[5];
  230. int i, apos;
  231. i = 1;
  232. apos = 0;
  233. while (i < inbuf.pktlen) {
  234. if (i + 4 <= inbuf.pktlen) {
  235. args[apos++] = letoh_32(&inbuf.pkt[i]);
  236. i += 4;
  237. }
  238. }
  239. outbuf->pkt[0] = inbuf.pkt[0];
  240. switch (inbuf.pkt[0]) {
  241. case CMD_WAITFOR:
  242. if (apos == 1)
  243. enqueue_sched(CMD_WAITFOR, args[0], -1);
  244. break;
  245. case CMD_RUNFOR:
  246. if (apos == 2)
  247. enqueue_sched(CMD_RUNFOR, args[0], args[1]);
  248. break;
  249. case CMD_PING:
  250. break;
  251. case CMD_SETUNSET:
  252. if (apos == 2)
  253. set_chan(args[0], args[1]);
  254. break;
  255. default:
  256. outbuf->pkt[0] = 0;
  257. break;
  258. }
  259. outbuf->pktlen = 1;
  260. }
  261. int
  262. main()
  263. {
  264. strobe_rng_init();
  265. BoardInitMcu();
  266. Radio.Init(&revents);
  267. analog_seed_rng();
  268. radio_seed_rng();
  269. strobe_rng_save();
  270. setup_gpio();
  271. /* turn on LED */
  272. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
  273. Radio.SetModem(MODEM_LORA);
  274. Radio.SetChannel(914350 * 1000);
  275. /* RX/TX parameters */
  276. const uint8_t modem = MODEM_LORA;
  277. const uint8_t bandwidth = 0 /* 128 kHz */;
  278. const uint8_t datarate = 7 /* 128 chips */;
  279. const uint8_t coderate = 1 /* 4/5 */;
  280. const uint8_t preambleLen = 4 /* symbols */;
  281. const uint8_t fixLen = 0 /* variable */;
  282. const uint8_t crcOn = 1 /* on */;
  283. const uint8_t freqHopOn = 0 /* off */;
  284. const bool iqInverted = false /* not inverted */;
  285. Radio.SetRxConfig(modem, bandwidth, datarate, coderate, 0/*afc*/,
  286. preambleLen, 5/*symTimeout*/, fixLen, 0/*payloadlen*/, crcOn,
  287. freqHopOn, 0/*hopPeriod*/, iqInverted, true/*rxcont*/);
  288. Radio.SetTxConfig(modem, 11/*power*/, 0/*fdev*/, bandwidth, datarate,
  289. coderate, preambleLen, fixLen, crcOn, freqHopOn, 0/*hopPeriod*/,
  290. iqInverted, 1000/*timeout*/);
  291. /* blink led */
  292. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
  293. DelayMs(300);
  294. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
  295. Radio.Rx(0);
  296. comms_init(&cs, procmsg, &shared_key_buf);
  297. uint8_t txbuf[128] = "i'mhere";
  298. struct pktbuf txpktbuf;
  299. txpktbuf = (struct pktbuf){
  300. .pkt = txbuf,
  301. .pktlen = 8,
  302. };
  303. Radio.Send(txpktbuf.pkt, txpktbuf.pktlen);
  304. rxpktavail = true;
  305. //Radio.Rx(0);
  306. loop:
  307. process_sched();
  308. BoardLowPowerHandler();
  309. if (Radio.IrqProcess != NULL)
  310. Radio.IrqProcess();
  311. if (!rxpktavail) {
  312. txpktbuf = (struct pktbuf){
  313. .pkt = txbuf,
  314. .pktlen = sizeof txbuf,
  315. };
  316. /* process available packet */
  317. comms_process(&cs, rxpktbuf, &txpktbuf);
  318. rxpktavail = true;
  319. if (txpktbuf.pktlen) {
  320. int i;
  321. for (i = 0; i < 1; i++) {
  322. DelayMs(20);
  323. Radio.Send(txpktbuf.pkt, txpktbuf.pktlen);
  324. }
  325. #if 0
  326. /* blink led */
  327. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
  328. DelayMs(300);
  329. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
  330. DelayMs(300);
  331. #endif
  332. }
  333. #if 0
  334. /* blink led */
  335. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
  336. DelayMs(300);
  337. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
  338. #endif
  339. }
  340. goto loop;
  341. }