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.
 
 
 
 
 
 

196 lines
6.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. ARMOBJDUMP?= arm-none-eabi-objdump
  25. ARMCC?= arm-none-eabi-gcc
  26. ARMTARGET?= -mcpu=cortex-m3 -mthumb -DSTROBE_SINGLE_THREAD=1
  27. # Clang doesn't work due to no-nano libc
  28. #ARMCC?=clang-mp-9.0
  29. #ARMTARGET?= -nostdlib -ffreestanding -target arm-none-eabi -mcpu=cortex-m3 -mfloat-abi=soft -mthumb
  30. .if $(.OBJDIR) == $(.CURDIR)
  31. .error Need to set MAKEOBJDIR.
  32. .endif
  33. PLATFORM != uname -s
  34. .if $(PLATFORM) == "Darwin"
  35. SOEXT=dylib
  36. .else
  37. .error Unsupported platform: $(PLATFORM)
  38. .endif
  39. PROGEXT = .elf
  40. PROGS = lora.gw lora.irr
  41. SRCS.lora.gw = main.c
  42. SRCS.lora.gw+= $(SRCS.NODE151)
  43. SRCS.lora.irr = irr_main.c
  44. SRCS.lora.irr+= comms.c
  45. SRCS.lora.irr+= strobe_rng_init.c
  46. SRCS.lora.irr+= $(STROBE_SRCS)
  47. SRCS.lora.irr+= $(SRCS.NODE151)
  48. CFLAGS+= -I$(.CURDIR)
  49. CFLAGS+= -g
  50. #CFLAGS+= -DNDEBUG
  51. CFLAGS+= -I$(.OBJDIR) # for shared_key.h
  52. WITH_STROBE=yes
  53. WITH_SX1276=yes
  54. WITH_NODE151=yes
  55. WITH_USB_CDC=yes
  56. .include <mk/boards.mk>
  57. CFLAGS+= -Werror -Wall
  58. LIBLORA_TEST_SRCS= comms.c strobe.c x25519.c
  59. LIBLORA_TEST_OBJS= $(LIBLORA_TEST_SRCS:C/.c$/.no/)
  60. LIBLORA_TEST = liblora_test.$(SOEXT)
  61. $(LIBLORA_TEST): $(LIBLORA_TEST_OBJS)
  62. $(CC) -shared -o $@ $(.ALLSRC)
  63. .MAIN: all
  64. .PHONY: all
  65. DEPENDS = .arm_deps .test_deps
  66. .PHONY: $(DEPENDS)
  67. depend: $(DEPENDS)
  68. .for i in $(DEPENDS)
  69. .sinclude "$i"
  70. .endfor
  71. # Temporarily disabled as OpenOCD can't program EEPROM on the STM32L1.
  72. # This could be used to add the data to flash.
  73. rng_save.c: Makefile
  74. ( echo '#include <strobe_rng_init.h>'; \
  75. echo 'const rng_word_t rng_save[roundup(32, sizeof(rng_word_t)) / sizeof(rng_word_t)] __attribute__ ((section (".eeprom"))) = {'; \
  76. dd if=/dev/random bs=32 count=1 | hexdump -e '4/4 " %3u," "\n"'; \
  77. echo '};' \
  78. ) > $@ || (rm $@ && false)
  79. .arm_deps:
  80. $(ARMCC) $(ARMTARGET) $(CFLAGS) $(.ALLSRC) -MM > $@ || (rm -f $@ && false)
  81. .test_deps: $(LIBLORA_TEST_SRCS)
  82. $(CC) $(CFLAGS) $(.ALLSRC) -MM | sed -e 's/\.o:/\.no:/' > $@ || (rm -f $@ && false)
  83. .for i in $(PROGS)
  84. ALLTGTS+= $(i)$(PROGEXT) $(i).list
  85. ASRCS.$(i) = $(SRCS) $(SRCS.$(i))
  86. OBJS.$(i) = $(ASRCS.$(i):C/.c$/.o/)
  87. .arm_deps: $(ASRCS.$(i))
  88. $(i)$(PROGEXT) $(i).map: $(OBJS.$(i))
  89. $(ARMCC) $(ARMTARGET) -o $(i)$(PROGEXT) $(.ALLSRC) -T$(LINKER_SCRIPT) --specs=nosys.specs -Wl,-Map="$(i).map" -Wl,--gc-sections -static --specs=nano.specs -Wl,--start-group -lc -lm -Wl,--end-group
  90. $(i).list: $(i)$(PROGEXT)
  91. $(ARMOBJDUMP) -h -S $(.ALLSRC) > $@ || (rm -f $@ && false)
  92. .endfor
  93. all: $(ALLTGTS)
  94. .PHONY: runbuild
  95. runbuild: $(SRCS) Makefile mk/*.mk
  96. 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'
  97. .PHONY: runtests
  98. runtests: Makefile lora_comms.py lora.py loraserv.py multicast.py $(LIBLORA_TEST) $(LIBLORA_TEST_SRCS)
  99. ls $(.ALLSRC) | entr sh -c '(cd $(.CURDIR) && $(MAKE) $(.MAKEFLAGS) $(LIBLORA_TEST)) && ((PYTHONPATH="$(.CURDIR)" python3 -m coverage run -m unittest lora multicast loraserv lora_comms && coverage report --omit=$(.CURDIR)/p/\* -m -i) 2>&1 | head -n 30)'
  100. .if empty(IRR_KEY) && exists($(.CURDIR)/.irr_key)
  101. IRR_KEY!= cat $(.CURDIR)/.irr_key
  102. .endif
  103. .PHONY: irrigation_key
  104. irrigation_key:
  105. @LANG=C tr -c -d a-zA-Z0-9 < /dev/urandom | dd bs=1 of=$(.CURDIR)/.irr_key count=32 2>/dev/null
  106. @echo 'Irrgation key created and put into .irr_key.'
  107. # make this a phony target so it's always run
  108. # dependancies will only be made when it's updated
  109. .PHONY: $(.OBJDIR)/shared_key.h
  110. $(.OBJDIR)/shared_key.h:
  111. @if [ "$(IRR_KEY)" = "" ]; then echo 'Must provide IRR_KEY make variable or have a non-empty file ".irr_key".'; false; fi
  112. @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
  113. @echo 'static struct pktbuf shared_key_buf = (struct pktbuf){ .pkt = shared_key, .pktlen = sizeof shared_key, };' >> shared_key.h.tmp
  114. (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
  115. # native objects
  116. .SUFFIXES: .no
  117. .c.no:
  118. $(CC) $(CFLAGS) -c $< -o $@
  119. .c.o:
  120. $(ARMCC) $(ARMTARGET) $(CFLAGS) -c $< -o $@
  121. STROBE_NAME = strobe
  122. STROBE_REPO = https://git.code.sf.net/p/strobe/code
  123. STROBE_BRANCH = master
  124. LORAMAC_NAME = loramac
  125. LORAMAC_REPO = https://github.com/Lora-net/LoRaMac-node.git
  126. LORAMAC_BRANCH = master
  127. .for module in STROBE LORAMAC
  128. .PHONY: init-$($(module)_NAME)
  129. init-$($(module)_NAME):
  130. git subtree add -P $($(module)_NAME) --squash $($(module)_REPO) $($(module)_BRANCH)
  131. .PHONY: update-$($(module)_NAME)
  132. update-$($(module)_NAME):
  133. git subtree pull -P $($(module)_NAME) --squash $($(module)_REPO) $($(module)_BRANCH)
  134. .endfor
  135. # Making diagrams, imported from fbsdembdev
  136. # =========================================
  137. # https://github.com/ironcamel/Graph-Easy
  138. .SUFFIXES: .getxt .diag .html
  139. .getxt.diag: box.sh
  140. graph-easy < $< | sh $(.CURDIR)/box.sh > $@
  141. .diag.html:
  142. (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>') > $@
  143. DIAG?=diag
  144. test-diag:
  145. ls $(.CURDIR)/box.sh $(.CURDIR)/$(DIAG).getxt $(.CURDIR)/Makefile | entr sh -c 'cd $(.CURDIR) && $(MAKE) $(.MAKEFLAGS) $(DIAG).diag && cat $(.OBJDIR)/$(DIAG).diag'
  146. # hard coded dependancy for when "make depend" has not been run.
  147. irr_main.o: $(.OBJDIR)/shared_key.h