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.
 
 
 
 
 
 

162 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. PROGS = lora.gw lora.irr
  34. SRCS.lora.gw = main.c
  35. SRCS.lora.gw+= $(SRCS.NODE151)
  36. SRCS.lora.gw+= $(SRCS.USB_CDC)
  37. SRCS.lora.gw+= $(SRCS.USB)
  38. SRCS.lora.irr = irr_main.c
  39. SRCS.lora.irr+= comms.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. # XXX - bsd.mkopt.mk only allows an option to have one upstream, I want
  48. # more.
  49. WITH_HAL_INIT=yes
  50. WITH_SX1276=yes
  51. WITH_NODE151=yes
  52. WITH_USB_CDC=yes
  53. .include <mk/boards.mk>
  54. CFLAGS+= -Werror -Wall
  55. LIBSYOTE_TEST_SRCS= comms.c strobe.c x25519.c
  56. LIBSYOTE_TEST_OBJS= $(LIBSYOTE_TEST_SRCS:C/.c$/.no/)
  57. LIBSYOTE_TEST = libsyote_test.$(SOEXT)
  58. $(LIBSYOTE_TEST): $(LIBSYOTE_TEST_OBJS)
  59. $(CC) -shared -o $@ $(.ALLSRC)
  60. DEPENDS = .test_deps
  61. # Temporarily disabled as OpenOCD can't program EEPROM on the STM32L1.
  62. # This could be used to add the data to flash.
  63. rng_save.c: Makefile
  64. ( echo '#include <strobe_rng_init.h>'; \
  65. echo 'const rng_word_t rng_save[roundup(32, sizeof(rng_word_t)) / sizeof(rng_word_t)] __attribute__ ((section (".eeprom"))) = {'; \
  66. dd if=/dev/random bs=32 count=1 | hexdump -e '4/4 " %3u," "\n"'; \
  67. echo '};' \
  68. ) > $@ || (rm $@ && false)
  69. .test_deps: $(LIBSYOTE_TEST_SRCS)
  70. $(CC) $(CFLAGS) $(.ALLSRC) -MM | sed -e 's/\.o:/\.no:/' > $@ || (rm -f $@ && false)
  71. .PHONY: runtests
  72. runtests: Makefile syote_comms.py lora.py loraserv.py multicast.py $(LIBSYOTE_TEST) $(LIBSYOTE_TEST_SRCS)
  73. 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)'
  74. .if empty(IRR_KEY) && exists($(.CURDIR)/.irr_key)
  75. IRR_KEY!= cat $(.CURDIR)/.irr_key
  76. .endif
  77. .PHONY: irrigation_key
  78. irrigation_key:
  79. @LANG=C tr -c -d a-zA-Z0-9 < /dev/urandom | dd bs=1 of=$(.CURDIR)/.irr_key count=32 2>/dev/null
  80. @echo 'Irrgation key created and put into .irr_key.'
  81. # make this a phony target so it's always run
  82. # dependancies will only be made when it's updated
  83. .PHONY: $(.OBJDIR)/shared_key.h
  84. $(.OBJDIR)/shared_key.h:
  85. @if [ "$(IRR_KEY)" = "" ]; then echo 'Must provide IRR_KEY make variable or have a non-empty file ".irr_key".'; false; fi
  86. @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
  87. @echo 'static struct pktbuf shared_key_buf = (struct pktbuf){ .pkt = shared_key, .pktlen = sizeof shared_key, };' >> shared_key.h.tmp
  88. (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
  89. # hard coded dependancy for when "make depend" has not been run.
  90. irr_main.o: $(.OBJDIR)/shared_key.h
  91. .include <mk/mu.progs.mk>
  92. ########################################################
  93. # External Source Code #
  94. ########################################################
  95. STROBE_NAME = strobe
  96. STROBE_REPO = https://git.code.sf.net/p/strobe/code
  97. STROBE_BRANCH = master
  98. LORAMAC_NAME = loramac
  99. LORAMAC_REPO = https://github.com/Lora-net/LoRaMac-node.git
  100. LORAMAC_BRANCH = master
  101. .for module in STROBE LORAMAC
  102. .PHONY: init-$($(module)_NAME)
  103. init-$($(module)_NAME):
  104. git subtree add -P $($(module)_NAME) --squash $($(module)_REPO) $($(module)_BRANCH)
  105. .PHONY: update-$($(module)_NAME)
  106. update-$($(module)_NAME):
  107. git subtree pull -P $($(module)_NAME) --squash $($(module)_REPO) $($(module)_BRANCH)
  108. .endfor
  109. ########################################################
  110. # Diagrams #
  111. ########################################################
  112. # imported from fbsdembdev
  113. #
  114. # Uses Graph Easy to convert to ASCII art:
  115. # https://github.com/ironcamel/Graph-Easy
  116. .SUFFIXES: .getxt .diag .html
  117. .getxt.diag: box.sh
  118. graph-easy < $< | sh $(.CURDIR)/box.sh > $@
  119. .diag.html:
  120. (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>') > $@
  121. DIAG?=diag
  122. test-diag:
  123. ls $(.CURDIR)/box.sh $(.CURDIR)/$(DIAG).getxt $(.CURDIR)/Makefile | entr sh -c 'cd $(.CURDIR) && $(MAKE) $(.MAKEFLAGS) $(DIAG).diag && cat $(.OBJDIR)/$(DIAG).diag'