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