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.
 
 
 
 
 
 

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