From 9fc72aa4f520ba0f87ebb0aacd096783f0f55fd5 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Mon, 10 May 2021 17:13:15 -0700 Subject: [PATCH] add some usage to README, also doc and improve shared key use/gen... --- Makefile | 26 +++++++++++++++++++++++++- README.md | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- irr_main.c | 6 +----- 3 files changed, 78 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 9516044..3837fd9 100644 --- a/Makefile +++ b/Makefile @@ -58,6 +58,7 @@ SRCS+= rng_save.c CFLAGS+= -I$(.CURDIR) CFLAGS+= -g #CFLAGS+= -DNDEBUG +CFLAGS+= -I$(.OBJDIR) # for shared_key.h # Strobe .PATH: $(.CURDIR)/strobe @@ -185,6 +186,26 @@ runbuild: $(SRCS) runtests: Makefile lora_comms.py lora.py loraserv.py multicast.py $(LIBLORA_TEST) $(LIBLORA_TEST_SRCS) ls $(.ALLSRC) | entr sh -c '(cd $(.CURDIR) && $(MAKE) $(.MAKEFLAGS) $(LIBLORA_TEST)) && ((PYTHONPATH="$(.CURDIR)" python -m coverage run -m unittest lora multicast loraserv && coverage report --omit=$(.CURDIR)/p/\* -m -i) 2>&1 | head -n 30)' +.if empty(IRR_KEY) && exists($(.CURDIR)/.irr_key) +IRR_KEY!= cat $(.CURDIR)/.irr_key +.endif + +.PHONY: irrigation_key +irrigation_key: + @LANG=C tr -c -d a-zA-Z0-9 < /dev/urandom | dd bs=1 of=$(.CURDIR)/.irr_key count=32 2>/dev/null + @echo 'Irrgation key created and put into .irr_key.' + +# make this a phony target so it's always run +# dependancies will only be made when it's updated +.PHONY: $(.OBJDIR)/shared_key.h +$(.OBJDIR)/shared_key.h: + @if [ "$(IRR_KEY)" = "" ]; then echo 'Must provide IRR_KEY make variable or have a non-empty file ".irr_key".'; false; fi + + @echo 'static uint8_t shared_key[] = {' $$(python -c 'import sys; print(", ".join(str(x) for x in sys.argv[1].encode("utf-8")))' $(IRR_KEY) ) "};" > shared_key.h.tmp + @echo 'static struct pktbuf shared_key_buf = (struct pktbuf){ .pkt = shared_key, .pktlen = sizeof shared_key, };' >> shared_key.h.tmp + + (cmp shared_key.h.tmp shared_key.h >/dev/null 2>&1 && rm shared_key.h.tmp) || mv shared_key.h.tmp shared_key.h + # native objects .SUFFIXES: .no .c.no: @@ -225,4 +246,7 @@ update-$($(module)_NAME): DIAG?=diag test-diag: - ls $(.CURDIR)/box.sh $(.CURDIR)/$(DIAG).getxt $(.CURDIR)/Makefile | entr sh -c 'cd $(.CURDIR) && $(MAKE) $(.MAKEFLAGS) $(DIAG).diag && cat $(MAKEOBJDIR)/$(DIAG).diag' + ls $(.CURDIR)/box.sh $(.CURDIR)/$(DIAG).getxt $(.CURDIR)/Makefile | entr sh -c 'cd $(.CURDIR) && $(MAKE) $(.MAKEFLAGS) $(DIAG).diag && cat $(.OBJDIR)/$(DIAG).diag' + +# hard coded dependancy for when "make depend" has not been run. +irr_main.o: $(.OBJDIR)/shared_key.h diff --git a/README.md b/README.md index 3457b95..648fc66 100644 --- a/README.md +++ b/README.md @@ -74,14 +74,28 @@ which uses gcc as the compiler. It would be good to get it cross-compile with clang as well, but that requires finding a libc like the nano libc that is provided by the toolchain. +One of the required parameters of the build is the shared key used for +authentication. A random key can be made using the command: +`make irrigation_key`, or it can be provided via the make command by +setting the variable IRR_KEY. + +Note: Both IRR_KEY and the argument to `lora.py` will encode the +provided key to UTF-8. + Once ARM's toolchain is in your path, the following should work: ``` export MAKEOBJDIR=build mkdir $MAKEOBJDIR -bsdmake all +bsdmake all IRR_KEY= ``` -And in the directory `build`, a file `lora.irr.elf` should be present. +And in the directory `build`, two files, `lora.irr.elf` and +`lora.gw.elf` should be present. The file `lora.irr.elf` should be +flashed on the Node151 device that is used for interfacing to the +irrigation system as described in [Deploying](#deploying). The file +`lora.gw.elf` should be used on another Node151 that will be attached +to a computer used as the gateway which runs the `loraserv.py` software +as described in [Using](#using). Flashing -------- @@ -165,4 +179,40 @@ GND and JD-VCC should be connected to the 5V power supply, while VCC is connected to VDD on the Node151, and INx pins to the respective GPIO pins. +Using +----- + +The `lora.py` requires at least Python 3.8. It also using the +strobe library that in distributed with this program. In general a +[virtualenv](https://virtualenv.pypa.io/en/latest/) is recommended for +all installed Python software to prevent version conflicts, but is not +always necessary. The `requirements.txt` file contains the necessary +modules to be installed, but simply addeding the directory +`strobe/python` to PYTHONPATH is also sufficient. + +The program `loraserv.py` takes a single argument, which is the device +file for the VCP that runs on the gateway. In my case, the device +name is `/dev/cu.usbmodem1451` as I am on my MacBook Pro, so the comand +to launch the gateway is simply: +``` +python loraserv.py /dev/cu.usbmodem1451 +``` + +Once that is running, then the `lora.py` program's multicast packets +will be forwarded out via the LoRa radio. + +To test it, a simple `ping` command can be used, or turning on or off +the on board LED via channel 4 using the `setunset` command. The ping +command: +``` +python lora.py -s ping +``` + +To turn off the LED (which defaults to on): +``` +python lora.py -s setunset 4 0 +``` + +Either of these commands should exit w/o message or error. + diff --git a/irr_main.c b/irr_main.c index 26df54c..9d4787b 100644 --- a/irr_main.c +++ b/irr_main.c @@ -58,11 +58,7 @@ static uint8_t rxpkt[128]; static struct pktbuf rxpktbuf; static volatile bool rxpktavail; -static uint8_t shared_key[] = "foobar"; -static struct pktbuf shared_key_buf = (struct pktbuf){ - .pkt = shared_key, - .pktlen = sizeof shared_key - 1, -}; +#include static struct comms_state cs;