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.
 
 
 
 
 
 

163 lines
5.9 KiB

  1. # Copyright 2021 John-Mark Gurney.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions
  5. # are met:
  6. # 1. Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # 2. Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  13. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  16. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  17. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  18. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  19. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  20. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  22. # SUCH DAMAGE.
  23. #
  24. .if $(.OBJDIR) == $(.CURDIR)
  25. .error Need to set MAKEOBJDIR.
  26. .endif
  27. PLATFORM != uname -s
  28. .if $(PLATFORM) == "Darwin"
  29. SOEXT=dylib
  30. .else
  31. .error Unsupported platform: $(PLATFORM)
  32. .endif
  33. PROGEXT = .elf
  34. PROGS = lora.gw lora.irr
  35. SRCS.lora.gw = main.c
  36. SRCS.lora.gw+= $(SRCS.NODE151)
  37. SRCS.lora.irr = irr_main.c
  38. SRCS.lora.irr+= comms.c
  39. SRCS.lora.irr+= strobe_rng_init.c
  40. SRCS.lora.irr+= $(STROBE_SRCS)
  41. SRCS.lora.irr+= $(SRCS.NODE151)
  42. CFLAGS+= -I$(.CURDIR)
  43. CFLAGS+= -g
  44. #CFLAGS+= -DNDEBUG
  45. CFLAGS+= -I$(.OBJDIR) # for shared_key.h
  46. WITH_STROBE=yes
  47. WITH_SX1276=yes
  48. WITH_NODE151=yes
  49. WITH_USB_CDC=yes
  50. .include <mk/boards.mk>
  51. CFLAGS+= -Werror -Wall
  52. LIBSYOTE_TEST_SRCS= comms.c strobe.c x25519.c
  53. LIBSYOTE_TEST_OBJS= $(LIBSYOTE_TEST_SRCS:C/.c$/.no/)
  54. LIBSYOTE_TEST = libsyote_test.$(SOEXT)
  55. $(LIBSYOTE_TEST): $(LIBSYOTE_TEST_OBJS)
  56. $(CC) -shared -o $@ $(.ALLSRC)
  57. DEPENDS = .arm_deps .test_deps
  58. .PHONY: $(DEPENDS)
  59. depend: $(DEPENDS)
  60. .for i in $(DEPENDS)
  61. .sinclude "$i"
  62. .endfor
  63. # Temporarily disabled as OpenOCD can't program EEPROM on the STM32L1.
  64. # This could be used to add the data to flash.
  65. rng_save.c: Makefile
  66. ( echo '#include <strobe_rng_init.h>'; \
  67. echo 'const rng_word_t rng_save[roundup(32, sizeof(rng_word_t)) / sizeof(rng_word_t)] __attribute__ ((section (".eeprom"))) = {'; \
  68. dd if=/dev/random bs=32 count=1 | hexdump -e '4/4 " %3u," "\n"'; \
  69. echo '};' \
  70. ) > $@ || (rm $@ && false)
  71. .arm_deps:
  72. $(ARMCC) $(ARMTARGET) $(CFLAGS) $(.ALLSRC) -MM > $@ || (rm -f $@ && false)
  73. .test_deps: $(LIBSYOTE_TEST_SRCS)
  74. $(CC) $(CFLAGS) $(.ALLSRC) -MM | sed -e 's/\.o:/\.no:/' > $@ || (rm -f $@ && false)
  75. .PHONY: runbuild
  76. runbuild: $(SRCS) Makefile mk/*.mk
  77. for i in $(.MAKEFILE_LIST) $(.ALLSRC) $$(cat $(DEPENDS) | gsed ':x; /\\$$/ { N; s/\\\n//; tx }' | sed -e 's/^[^:]*://'); do if [ "$$i" != ".." ]; then echo $$i; fi; done | sort -u | entr -d sh -c 'echo starting...; cd $(.CURDIR) && $(MAKE) $(.MAKEFLAGS) depend && $(MAKE) $(.MAKEFLAGS) all'
  78. .PHONY: runtests
  79. runtests: Makefile syote_comms.py lora.py loraserv.py multicast.py $(LIBSYOTE_TEST) $(LIBSYOTE_TEST_SRCS)
  80. ls $(.ALLSRC) | entr sh -c '(cd $(.CURDIR) && $(MAKE) $(.MAKEFLAGS) $(LIBSYOTE_TEST)) && ((PYTHONPATH="$(.CURDIR)" python3 -m coverage run -m unittest lora multicast loraserv syote_comms && coverage report --omit=$(.CURDIR)/p/\* -m -i) 2>&1 | head -n 30)'
  81. .if empty(IRR_KEY) && exists($(.CURDIR)/.irr_key)
  82. IRR_KEY!= cat $(.CURDIR)/.irr_key
  83. .endif
  84. .PHONY: irrigation_key
  85. irrigation_key:
  86. @LANG=C tr -c -d a-zA-Z0-9 < /dev/urandom | dd bs=1 of=$(.CURDIR)/.irr_key count=32 2>/dev/null
  87. @echo 'Irrgation key created and put into .irr_key.'
  88. # make this a phony target so it's always run
  89. # dependancies will only be made when it's updated
  90. .PHONY: $(.OBJDIR)/shared_key.h
  91. $(.OBJDIR)/shared_key.h:
  92. @if [ "$(IRR_KEY)" = "" ]; then echo 'Must provide IRR_KEY make variable or have a non-empty file ".irr_key".'; false; fi
  93. @echo 'static uint8_t shared_key[] = {' $$(python3 -c 'import sys; print(", ".join(str(x) for x in sys.argv[1].encode("utf-8")))' $(IRR_KEY) ) "};" > shared_key.h.tmp
  94. @echo 'static struct pktbuf shared_key_buf = (struct pktbuf){ .pkt = shared_key, .pktlen = sizeof shared_key, };' >> shared_key.h.tmp
  95. (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
  96. STROBE_NAME = strobe
  97. STROBE_REPO = https://git.code.sf.net/p/strobe/code
  98. STROBE_BRANCH = master
  99. LORAMAC_NAME = loramac
  100. LORAMAC_REPO = https://github.com/Lora-net/LoRaMac-node.git
  101. LORAMAC_BRANCH = master
  102. .for module in STROBE LORAMAC
  103. .PHONY: init-$($(module)_NAME)
  104. init-$($(module)_NAME):
  105. git subtree add -P $($(module)_NAME) --squash $($(module)_REPO) $($(module)_BRANCH)
  106. .PHONY: update-$($(module)_NAME)
  107. update-$($(module)_NAME):
  108. git subtree pull -P $($(module)_NAME) --squash $($(module)_REPO) $($(module)_BRANCH)
  109. .endfor
  110. # Making diagrams, imported from fbsdembdev
  111. # =========================================
  112. # https://github.com/ironcamel/Graph-Easy
  113. .SUFFIXES: .getxt .diag .html
  114. .getxt.diag: box.sh
  115. graph-easy < $< | sh $(.CURDIR)/box.sh > $@
  116. .diag.html:
  117. (cat $<; echo; echo '<!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://casual-effects.com/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>') > $@
  118. DIAG?=diag
  119. test-diag:
  120. ls $(.CURDIR)/box.sh $(.CURDIR)/$(DIAG).getxt $(.CURDIR)/Makefile | entr sh -c 'cd $(.CURDIR) && $(MAKE) $(.MAKEFLAGS) $(DIAG).diag && cat $(.OBJDIR)/$(DIAG).diag'
  121. # hard coded dependancy for when "make depend" has not been run.
  122. irr_main.o: $(.OBJDIR)/shared_key.h
  123. .include <mk/mu.progs.mk>