From ffc425603dcbedf0da1d6aa1759f8f14f3a5e841 Mon Sep 17 00:00:00 2001 From: fredizzimo Date: Sat, 13 Feb 2016 23:26:39 +0200 Subject: [PATCH 01/64] Initial commit --- LICENSE | 21 +++++++++++++++++++++ README.md | 1 + 2 files changed, 22 insertions(+) create mode 100644 LICENSE create mode 100644 README.md diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000000..d66406efdf7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 fredizzimo + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 00000000000..94af9125cef --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# tmk_serial_link \ No newline at end of file From e70cefc12fb897f7221f7b1bc6fe0ce595eb2de1 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 00:05:58 +0200 Subject: [PATCH 02/64] Add cgreen unit test library and makefile --- .gitmodules | 3 +++ cgreen/Makefile | 38 ++++++++++++++++++++++++++++++++++++++ cgreen/Makefile.build | 33 +++++++++++++++++++++++++++++++++ cgreen/cgreen | 1 + serial_link.mk | 11 +++++++++++ 5 files changed, 86 insertions(+) create mode 100644 .gitmodules create mode 100644 cgreen/Makefile create mode 100644 cgreen/Makefile.build create mode 160000 cgreen/cgreen create mode 100644 serial_link.mk diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000000..991cfe96d76 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "cgreen/cgreen"] + path = cgreen/cgreen + url = http://github.com/cgreen-devs/cgreen diff --git a/cgreen/Makefile b/cgreen/Makefile new file mode 100644 index 00000000000..6b31a3f9232 --- /dev/null +++ b/cgreen/Makefile @@ -0,0 +1,38 @@ +# This Makefile ensures that the build is made out of source in a subdirectory called 'build' +# If it doesn't exist, it is created and a Makefile created there (from Makefile.build) +# +# This Makefile also contains delegation of the most common make commands +# +# If you have cmake installed you should be able to do: +# +# make +# make test +# make install +# make package +# +# That should build cgreen for C and C++, run some tests, install it locally and +# generate two distributable packages. + +all: build + cd $(CGREEN_BUILD_DIR); make all + +test: build + cd $(CGREEN_BUILD_DIR); make test + +clean: build + cd $(CGREEN_BUILD_DIR); make clean + +package: build + cd $(CGREEN_BUILD_DIR); make package + +install: + cd $(CGREEN_BUILD_DIR); make install + +############# Internal + +build: + mkdir -p $(CGREEN_BUILD_DIR) + cp Makefile.build $(CGREEN_BUILD_DIR)/Makefile + + +.SILENT: diff --git a/cgreen/Makefile.build b/cgreen/Makefile.build new file mode 100644 index 00000000000..f76165244ca --- /dev/null +++ b/cgreen/Makefile.build @@ -0,0 +1,33 @@ +# This Makefile is copied from the cgreen top directory (where it is +# named Makefile.build) and put in a subdirectory called 'build' where +# builds are made This Makefile then automatically creates +# subdirectories for C and C++ builds configuring them using the cmake +# command. Once created you can always tweak the cmake setup as with +# any cmake build directory + +all: build-c build-c++ + for d in build-* ; do cd $$d; make ; cd .. ; done + +clean: + for d in build-* ; do cd $$d; make clean ; cd .. ; done + +check test: + for d in build-* ; do cd $$d; make check ; cd .. ; done + +package: + for d in build-* ; do cd $$d; make package ; cd .. ; done + +install: + for d in build-* ; do cd $$d; make install ; cd .. ; done + +############ Internal + +build-c: + mkdir build-c + cd build-c; cmake -G "Unix Makefiles" $(CGREEN_DIR) + +build-c++: + mkdir build-c++ + cd build-c++; cmake -G "Unix Makefiles" -DWITH_CXX:bool=ON $(CGREEN_DIR) + +.SILENT: diff --git a/cgreen/cgreen b/cgreen/cgreen new file mode 160000 index 00000000000..d4d438dda1b --- /dev/null +++ b/cgreen/cgreen @@ -0,0 +1 @@ +Subproject commit d4d438dda1b7131f0bd0530b2c258e9dea6a2a9f diff --git a/serial_link.mk b/serial_link.mk new file mode 100644 index 00000000000..51d33fcb41c --- /dev/null +++ b/serial_link.mk @@ -0,0 +1,11 @@ +CGREEN_LIB = $(BUILDDIR)/cgreen/build-c/src/libcgreen.a + +.phony testserial: +testserial: $(CGREEN_LIB) + +CGREEN_DIR = "$(CURDIR)/$(SERIAL_DIR)/cgreen/cgreen" +CGREEN_BUILD_DIR = "$(CURDIR)/$(BUILDDIR)/cgreen" +export CGREEN_DIR +export CGREEN_BUILD_DIR +$(CGREEN_LIB): + make -C $(SERIAL_DIR)/cgreen \ No newline at end of file From e9a1474bb6a92024c8d4e3095d6b871833db1cf4 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 00:20:14 +0200 Subject: [PATCH 03/64] Add makefile for compiling and running unit tests --- serial_link.mk | 1 + tests/Makefile | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 tests/Makefile diff --git a/serial_link.mk b/serial_link.mk index 51d33fcb41c..8e8de807f5e 100644 --- a/serial_link.mk +++ b/serial_link.mk @@ -2,6 +2,7 @@ CGREEN_LIB = $(BUILDDIR)/cgreen/build-c/src/libcgreen.a .phony testserial: testserial: $(CGREEN_LIB) + make -C $(SERIAL_DIR)/tests BUILDDIR=../../$(BUILDDIR) CGREEN_DIR = "$(CURDIR)/$(SERIAL_DIR)/cgreen/cgreen" CGREEN_BUILD_DIR = "$(CURDIR)/$(BUILDDIR)/cgreen" diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 00000000000..239e9f60934 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,49 @@ +CC = gcc +CFLAGS = +INCLUDES = -I. +LDFLAGS = -L$(BUILDDIR)/cgreen/build-c/src +LDLIBS = -lcgreen +UNITOBJ = $(BUILDDIR)/testserial/unitobj +DEPDIR = $(BUILDDIR)/testserial/unit.d +UNITEXE = $(BUILDDIR)/testserial/unitexe +DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td +EXT = +UNAME := $(shell uname) +ifneq (, $(findstring mingw, $(UNAME))) + EXT += exe +endif +ifneq (, $(findstring cygwin, $(UNAME))) + EXT += exe +endif + +SRC = $(wildcard *.c) +EXE = $(patsubst %.c, $(UNITEXE)/%$(EXT), $(SRC)) +$(shell mkdir -p $(DEPDIR) >/dev/null) + +test: $(EXE) + @for f in $^; do \ + echo "++++++++++++++++++"; \ + echo "Running unit tests"; \ + echo $$(basename $$f); \ + echo "++++++++++++++++++"; \ + echo ""; \ + $$f || exit 1; \ + echo ""; \ + echo "******************"; \ + echo ""; \ + done + +$(UNITEXE)/%$(EXT): $(UNITOBJ)/%.o + mkdir -p $(UNITEXE) + $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) + +$(UNITOBJ)/%.o : %.c +$(UNITOBJ)/%.o: %.c $(DEPDIR)/%.d + mkdir -p $(UNITOBJ) + $(CC) $(CFLAGS) $(DEPFLAGS) -c $< -o $@ + mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d + +$(DEPDIR)/%.d: ; +.PRECIOUS: $(DEPDIR)/%.d + +-include $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRC).c)) \ No newline at end of file From 3c6b93a491ab6ea280c30be26980e2ca91ba5c4c Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 01:31:01 +0200 Subject: [PATCH 04/64] Add separate makefile for tests --- serial_link.mk | 12 ------------ serial_link_tests.mk | 13 +++++++++++++ 2 files changed, 13 insertions(+), 12 deletions(-) create mode 100644 serial_link_tests.mk diff --git a/serial_link.mk b/serial_link.mk index 8e8de807f5e..e69de29bb2d 100644 --- a/serial_link.mk +++ b/serial_link.mk @@ -1,12 +0,0 @@ -CGREEN_LIB = $(BUILDDIR)/cgreen/build-c/src/libcgreen.a - -.phony testserial: -testserial: $(CGREEN_LIB) - make -C $(SERIAL_DIR)/tests BUILDDIR=../../$(BUILDDIR) - -CGREEN_DIR = "$(CURDIR)/$(SERIAL_DIR)/cgreen/cgreen" -CGREEN_BUILD_DIR = "$(CURDIR)/$(BUILDDIR)/cgreen" -export CGREEN_DIR -export CGREEN_BUILD_DIR -$(CGREEN_LIB): - make -C $(SERIAL_DIR)/cgreen \ No newline at end of file diff --git a/serial_link_tests.mk b/serial_link_tests.mk new file mode 100644 index 00000000000..36d7c09b3f0 --- /dev/null +++ b/serial_link_tests.mk @@ -0,0 +1,13 @@ +CGREEN_LIB = $(BUILDDIR)/cgreen/build-c/src/libcgreen.a + +CGREEN_DIR = "$(CURDIR)/$(SERIAL_DIR)/cgreen/cgreen" +CGREEN_BUILD_DIR = "$(CURDIR)/$(BUILDDIR)/cgreen" +export CGREEN_DIR +export CGREEN_BUILD_DIR +$(CGREEN_LIB): + make -C $(SERIAL_DIR)/cgreen + +.PHONY serialtest: +serialtest : $(CGREEN_LIB) + @echo $(CGREEN_LIB) + make -C $(SERIAL_DIR)/tests BUILDDIR=../../$(BUILDDIR) \ No newline at end of file From 3eaddd65141f880ddc7d23fc05cafb01ca5477ac Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 01:58:26 +0200 Subject: [PATCH 05/64] Use cgreen test runner instead of executables --- tests/Makefile | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index 239e9f60934..3f89cba471f 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,40 +1,30 @@ CC = gcc CFLAGS = INCLUDES = -I. -LDFLAGS = -L$(BUILDDIR)/cgreen/build-c/src +LDFLAGS = -L$(BUILDDIR)/cgreen/build-c/src -shared LDLIBS = -lcgreen -UNITOBJ = $(BUILDDIR)/testserial/unitobj -DEPDIR = $(BUILDDIR)/testserial/unit.d -UNITEXE = $(BUILDDIR)/testserial/unitexe +UNITOBJ = $(BUILDDIR)/serialtest/unitobj +DEPDIR = $(BUILDDIR)/serialtest/unit.d +UNITTESTS = $(BUILDDIR)/serialtest/unittests DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td -EXT = +EXT = .so UNAME := $(shell uname) -ifneq (, $(findstring mingw, $(UNAME))) - EXT += exe +ifneq (, $(findstring MINGW, $(UNAME))) + EXT = .dll endif -ifneq (, $(findstring cygwin, $(UNAME))) - EXT += exe +ifneq (, $(findstring CYGWIN, $(UNAME))) + EXT = .dll endif SRC = $(wildcard *.c) -EXE = $(patsubst %.c, $(UNITEXE)/%$(EXT), $(SRC)) +TESTFILES = $(patsubst %.c, $(UNITTESTS)/%$(EXT), $(SRC)) $(shell mkdir -p $(DEPDIR) >/dev/null) -test: $(EXE) - @for f in $^; do \ - echo "++++++++++++++++++"; \ - echo "Running unit tests"; \ - echo $$(basename $$f); \ - echo "++++++++++++++++++"; \ - echo ""; \ - $$f || exit 1; \ - echo ""; \ - echo "******************"; \ - echo ""; \ - done +test: $(TESTFILES) + @$(BUILDDIR)/cgreen/build-c/tools/cgreen-runner --color $(TESTFILES) -$(UNITEXE)/%$(EXT): $(UNITOBJ)/%.o - mkdir -p $(UNITEXE) +$(UNITTESTS)/%$(EXT): $(UNITOBJ)/%.o + mkdir -p $(UNITTESTS) $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) $(UNITOBJ)/%.o : %.c From 1f1bc183dd1080e14a2d85010d5c7f6455f023f6 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 12:45:12 +0200 Subject: [PATCH 06/64] Start implementing data_link --- LICENSE | 2 +- serial_link.mk | 23 +++++++++++ serial_link/protocol/data_link.c | 29 ++++++++++++++ serial_link/protocol/data_link.h | 25 ++++++++++++ serial_link/protocol/routing.h | 25 ++++++++++++ serial_link/tests/Makefile | 61 +++++++++++++++++++++++++++++ serial_link/tests/data_link_tests.c | 52 ++++++++++++++++++++++++ serial_link_tests.mk | 27 +++++++++++-- tests/Makefile | 39 ------------------ 9 files changed, 240 insertions(+), 43 deletions(-) create mode 100644 serial_link/protocol/data_link.c create mode 100644 serial_link/protocol/data_link.h create mode 100644 serial_link/protocol/routing.h create mode 100644 serial_link/tests/Makefile create mode 100644 serial_link/tests/data_link_tests.c delete mode 100644 tests/Makefile diff --git a/LICENSE b/LICENSE index d66406efdf7..d7cc3198cb3 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2016 fredizzimo +Copyright (c) 2016 Fred Sundvik Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/serial_link.mk b/serial_link.mk index e69de29bb2d..de2364108a3 100644 --- a/serial_link.mk +++ b/serial_link.mk @@ -0,0 +1,23 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016 Fred Sundvik +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +INC += $(SERIAL_DIR) \ No newline at end of file diff --git a/serial_link/protocol/data_link.c b/serial_link/protocol/data_link.c new file mode 100644 index 00000000000..71d538470cf --- /dev/null +++ b/serial_link/protocol/data_link.c @@ -0,0 +1,29 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "protocol/data_link.h" +#include "protocol/routing.h" + +void recv_byte(uint8_t data) { +} diff --git a/serial_link/protocol/data_link.h b/serial_link/protocol/data_link.h new file mode 100644 index 00000000000..3b9f9ea5d48 --- /dev/null +++ b/serial_link/protocol/data_link.h @@ -0,0 +1,25 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +void recv_byte(uint8_t data); diff --git a/serial_link/protocol/routing.h b/serial_link/protocol/routing.h new file mode 100644 index 00000000000..ee1f9a78aea --- /dev/null +++ b/serial_link/protocol/routing.h @@ -0,0 +1,25 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +void recv_frame(uint8_t* data, uint16_t size); diff --git a/serial_link/tests/Makefile b/serial_link/tests/Makefile new file mode 100644 index 00000000000..0d8ba4b7b65 --- /dev/null +++ b/serial_link/tests/Makefile @@ -0,0 +1,61 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016 Fred Sundvik +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +CC = gcc +CFLAGS = +INCLUDES = -I. -I../ +LDFLAGS = -L$(BUILDDIR)/cgreen/build-c/src -shared +LDLIBS = -lcgreen +UNITOBJ = $(BUILDDIR)/serialtest/unitobj +DEPDIR = $(BUILDDIR)/serialtest/unit.d +UNITTESTS = $(BUILDDIR)/serialtest/unittests +DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td +EXT = .so +UNAME := $(shell uname) +ifneq (, $(findstring MINGW, $(UNAME))) + EXT = .dll +endif +ifneq (, $(findstring CYGWIN, $(UNAME))) + EXT = .dll +endif + +SRC = $(wildcard *.c) +TESTFILES = $(patsubst %.c, $(UNITTESTS)/%$(EXT), $(SRC)) +$(shell mkdir -p $(DEPDIR) >/dev/null) + +test: $(TESTFILES) + @$(BUILDDIR)/cgreen/build-c/tools/cgreen-runner --color $(TESTFILES) + +$(UNITTESTS)/%$(EXT): $(UNITOBJ)/%.o + @mkdir -p $(UNITTESTS) + $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) + +$(UNITOBJ)/%.o : %.c +$(UNITOBJ)/%.o: %.c $(DEPDIR)/%.d + @mkdir -p $(UNITOBJ) + $(CC) $(CFLAGS) $(DEPFLAGS) $(INCLUDES) -c $< -o $@ + @mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d + +$(DEPDIR)/%.d: ; +.PRECIOUS: $(DEPDIR)/%.d + +-include $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRC))) \ No newline at end of file diff --git a/serial_link/tests/data_link_tests.c b/serial_link/tests/data_link_tests.c new file mode 100644 index 00000000000..1b7107e708f --- /dev/null +++ b/serial_link/tests/data_link_tests.c @@ -0,0 +1,52 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include +#include +#include "protocol/data_link.h" +#include "protocol/data_link.c" +#include "protocol/routing.h" + +Describe(DataLink); +BeforeEach(DataLink) {} +AfterEach(DataLink) {} + +void recv_frame(uint8_t* data, uint16_t size) { + mock(data, size); +} + +Ensure(DataLink, receives_no_frame_for_a_single_zero_byte) { + never_expect(recv_frame); + recv_byte(0); +} + +Ensure(DataLink, receives_no_frame_for_a_single_FF_byte) { + never_expect(recv_frame); + recv_byte(0xFF); +} + +Ensure(DataLink, receives_no_frame_for_a_single_random_byte) { + never_expect(recv_frame); + recv_byte(0x4A); +} diff --git a/serial_link_tests.mk b/serial_link_tests.mk index 36d7c09b3f0..e292f582a98 100644 --- a/serial_link_tests.mk +++ b/serial_link_tests.mk @@ -1,3 +1,25 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016 Fred Sundvik +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + CGREEN_LIB = $(BUILDDIR)/cgreen/build-c/src/libcgreen.a CGREEN_DIR = "$(CURDIR)/$(SERIAL_DIR)/cgreen/cgreen" @@ -5,9 +27,8 @@ CGREEN_BUILD_DIR = "$(CURDIR)/$(BUILDDIR)/cgreen" export CGREEN_DIR export CGREEN_BUILD_DIR $(CGREEN_LIB): - make -C $(SERIAL_DIR)/cgreen + @make -C $(SERIAL_DIR)/cgreen .PHONY serialtest: serialtest : $(CGREEN_LIB) - @echo $(CGREEN_LIB) - make -C $(SERIAL_DIR)/tests BUILDDIR=../../$(BUILDDIR) \ No newline at end of file + @make -C $(SERIAL_DIR)/serial_link/tests BUILDDIR=../../../$(BUILDDIR) \ No newline at end of file diff --git a/tests/Makefile b/tests/Makefile deleted file mode 100644 index 3f89cba471f..00000000000 --- a/tests/Makefile +++ /dev/null @@ -1,39 +0,0 @@ -CC = gcc -CFLAGS = -INCLUDES = -I. -LDFLAGS = -L$(BUILDDIR)/cgreen/build-c/src -shared -LDLIBS = -lcgreen -UNITOBJ = $(BUILDDIR)/serialtest/unitobj -DEPDIR = $(BUILDDIR)/serialtest/unit.d -UNITTESTS = $(BUILDDIR)/serialtest/unittests -DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td -EXT = .so -UNAME := $(shell uname) -ifneq (, $(findstring MINGW, $(UNAME))) - EXT = .dll -endif -ifneq (, $(findstring CYGWIN, $(UNAME))) - EXT = .dll -endif - -SRC = $(wildcard *.c) -TESTFILES = $(patsubst %.c, $(UNITTESTS)/%$(EXT), $(SRC)) -$(shell mkdir -p $(DEPDIR) >/dev/null) - -test: $(TESTFILES) - @$(BUILDDIR)/cgreen/build-c/tools/cgreen-runner --color $(TESTFILES) - -$(UNITTESTS)/%$(EXT): $(UNITOBJ)/%.o - mkdir -p $(UNITTESTS) - $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) - -$(UNITOBJ)/%.o : %.c -$(UNITOBJ)/%.o: %.c $(DEPDIR)/%.d - mkdir -p $(UNITOBJ) - $(CC) $(CFLAGS) $(DEPFLAGS) -c $< -o $@ - mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d - -$(DEPDIR)/%.d: ; -.PRECIOUS: $(DEPDIR)/%.d - --include $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRC).c)) \ No newline at end of file From 8a991a266e7f8f9aff48049dffb17eccfa353ca1 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 13:05:50 +0200 Subject: [PATCH 07/64] Rename files to match what they actually do Also add validator before routing. --- .../protocol/{data_link.c => byte_stuffer.c} | 4 ++-- .../protocol/{data_link.h => byte_stuffer.h} | 0 .../protocol/{routing.h => frame_validator.h} | 0 ...{data_link_tests.c => byte_stuffer_tests.c} | 18 +++++++++--------- 4 files changed, 11 insertions(+), 11 deletions(-) rename serial_link/protocol/{data_link.c => byte_stuffer.c} (93%) rename serial_link/protocol/{data_link.h => byte_stuffer.h} (100%) rename serial_link/protocol/{routing.h => frame_validator.h} (100%) rename serial_link/tests/{data_link_tests.c => byte_stuffer_tests.c} (78%) diff --git a/serial_link/protocol/data_link.c b/serial_link/protocol/byte_stuffer.c similarity index 93% rename from serial_link/protocol/data_link.c rename to serial_link/protocol/byte_stuffer.c index 71d538470cf..95ce86524f1 100644 --- a/serial_link/protocol/data_link.c +++ b/serial_link/protocol/byte_stuffer.c @@ -22,8 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "protocol/data_link.h" -#include "protocol/routing.h" +#include "protocol/byte_stuffer.h" +#include "protocol/frame_validator.h" void recv_byte(uint8_t data) { } diff --git a/serial_link/protocol/data_link.h b/serial_link/protocol/byte_stuffer.h similarity index 100% rename from serial_link/protocol/data_link.h rename to serial_link/protocol/byte_stuffer.h diff --git a/serial_link/protocol/routing.h b/serial_link/protocol/frame_validator.h similarity index 100% rename from serial_link/protocol/routing.h rename to serial_link/protocol/frame_validator.h diff --git a/serial_link/tests/data_link_tests.c b/serial_link/tests/byte_stuffer_tests.c similarity index 78% rename from serial_link/tests/data_link_tests.c rename to serial_link/tests/byte_stuffer_tests.c index 1b7107e708f..418d48f6a4e 100644 --- a/serial_link/tests/data_link_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -24,29 +24,29 @@ SOFTWARE. #include #include -#include "protocol/data_link.h" -#include "protocol/data_link.c" -#include "protocol/routing.h" +#include "protocol/byte_stuffer.h" +#include "protocol/byte_stuffer.c" +#include "protocol/frame_validator.h" -Describe(DataLink); -BeforeEach(DataLink) {} -AfterEach(DataLink) {} +Describe(ByteStuffer); +BeforeEach(ByteStuffer) {} +AfterEach(ByteStuffer) {} void recv_frame(uint8_t* data, uint16_t size) { mock(data, size); } -Ensure(DataLink, receives_no_frame_for_a_single_zero_byte) { +Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { never_expect(recv_frame); recv_byte(0); } -Ensure(DataLink, receives_no_frame_for_a_single_FF_byte) { +Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) { never_expect(recv_frame); recv_byte(0xFF); } -Ensure(DataLink, receives_no_frame_for_a_single_random_byte) { +Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { never_expect(recv_frame); recv_byte(0x4A); } From ce3a21cbeaa58cfa6b117a8be13be47a69f086ae Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 14:04:51 +0200 Subject: [PATCH 08/64] Implement receive of valid small byte stuffed frames --- serial_link/protocol/byte_stuffer.c | 36 ++++++++++++++- serial_link/protocol/byte_stuffer.h | 4 +- serial_link/tests/byte_stuffer_tests.c | 61 ++++++++++++++++++++++++-- 3 files changed, 95 insertions(+), 6 deletions(-) diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index 95ce86524f1..7ce01a96ab1 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -25,5 +25,39 @@ SOFTWARE. #include "protocol/byte_stuffer.h" #include "protocol/frame_validator.h" -void recv_byte(uint8_t data) { +// This implements the "Consistent overhead byte stuffing protocol" +// https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing +// http://www.stuartcheshire.org/papers/COBSforToN.pdf + +typedef struct byte_stuffer_state { + uint16_t next_zero; + uint16_t data_pos; + uint8_t data[256]; +}byte_stuffer_state_t; + +void init_byte_stuffer_state(byte_stuffer_state_t* state) { + state->next_zero = 0; + state->data_pos = 0; +} + +void recv_byte(byte_stuffer_state_t* state, uint8_t data) { + if (state->next_zero == 0) { + state->next_zero = data; + state->data_pos = 0; + return; + } + + state->next_zero--; + if (data == 0) { + recv_frame(state->data, state->data_pos); + } + else { + if (state->next_zero == 0) { + state->next_zero = data; + state->data[state->data_pos++] = 0; + } + else { + state->data[state->data_pos++] = data; + } + } } diff --git a/serial_link/protocol/byte_stuffer.h b/serial_link/protocol/byte_stuffer.h index 3b9f9ea5d48..9a5551fab58 100644 --- a/serial_link/protocol/byte_stuffer.h +++ b/serial_link/protocol/byte_stuffer.h @@ -22,4 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -void recv_byte(uint8_t data); +typedef struct byte_stuffer_state byte_stuffer_state_t; +void init_byte_stuffer_state(byte_stuffer_state_t* state); +void recv_byte(byte_stuffer_state_t* state, uint8_t data); diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 418d48f6a4e..2fc7a0b26c8 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -28,8 +28,12 @@ SOFTWARE. #include "protocol/byte_stuffer.c" #include "protocol/frame_validator.h" +byte_stuffer_state_t state; + Describe(ByteStuffer); -BeforeEach(ByteStuffer) {} +BeforeEach(ByteStuffer) { + init_byte_stuffer_state(&state); +} AfterEach(ByteStuffer) {} void recv_frame(uint8_t* data, uint16_t size) { @@ -38,15 +42,64 @@ void recv_frame(uint8_t* data, uint16_t size) { Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { never_expect(recv_frame); - recv_byte(0); + recv_byte(&state, 0); } Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) { never_expect(recv_frame); - recv_byte(0xFF); + recv_byte(&state, 0xFF); } Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { never_expect(recv_frame); - recv_byte(0x4A); + recv_byte(&state, 0x4A); +} + +Ensure(ByteStuffer, receives_single_byte_valid_frame) { + uint8_t expected[] = {0x37}; + expect(recv_frame, + when(size, is_equal_to(1)), + when(data, is_equal_to_contents_of(expected, 1)) + ); + recv_byte(&state, 2); + recv_byte(&state, 0x37); + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, receives_three_bytes_valid_frame) { + uint8_t expected[] = {0x37, 0x99, 0xFF}; + expect(recv_frame, + when(size, is_equal_to(3)), + when(data, is_equal_to_contents_of(expected, 3)) + ); + recv_byte(&state, 5); + recv_byte(&state, 0x37); + recv_byte(&state, 0x99); + recv_byte(&state, 0xFF); + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, receives_single_zero_valid_frame) { + uint8_t expected[] = {0}; + expect(recv_frame, + when(size, is_equal_to(1)), + when(data, is_equal_to_contents_of(expected, 1)) + ); + recv_byte(&state, 1); + recv_byte(&state, 1); + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { + uint8_t expected[] = {5, 0, 3, 0}; + expect(recv_frame, + when(size, is_equal_to(4)), + when(data, is_equal_to_contents_of(expected, 4)) + ); + recv_byte(&state, 2); + recv_byte(&state, 5); + recv_byte(&state, 2); + recv_byte(&state, 3); + recv_byte(&state, 1); + recv_byte(&state, 0); } From eefb5b5634e341396fb535f4eeac1323bf716ed0 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 14:25:54 +0200 Subject: [PATCH 09/64] Mutiple frame handling for byte stuffer Also handles unexpected data. --- serial_link/protocol/byte_stuffer.c | 12 +++++- serial_link/tests/byte_stuffer_tests.c | 53 +++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index 7ce01a96ab1..e578f88dc1a 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -41,6 +41,7 @@ void init_byte_stuffer_state(byte_stuffer_state_t* state) { } void recv_byte(byte_stuffer_state_t* state, uint8_t data) { + // Start of a new frame if (state->next_zero == 0) { state->next_zero = data; state->data_pos = 0; @@ -49,10 +50,19 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) { state->next_zero--; if (data == 0) { - recv_frame(state->data, state->data_pos); + if (state->next_zero == 0) { + // The frame is completed + recv_frame(state->data, state->data_pos); + } + else { + // The frame is invalid, so reset + state->next_zero = 0; + state->data_pos = 0; + } } else { if (state->next_zero == 0) { + // Special case for zeroes state->next_zero = data; state->data[state->data_pos++] = 0; } diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 2fc7a0b26c8..74a349b1f34 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -72,7 +72,7 @@ Ensure(ByteStuffer, receives_three_bytes_valid_frame) { when(size, is_equal_to(3)), when(data, is_equal_to_contents_of(expected, 3)) ); - recv_byte(&state, 5); + recv_byte(&state, 4); recv_byte(&state, 0x37); recv_byte(&state, 0x99); recv_byte(&state, 0xFF); @@ -103,3 +103,54 @@ Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { recv_byte(&state, 1); recv_byte(&state, 0); } + +Ensure(ByteStuffer, receives_two_valid_frames) { + uint8_t expected1[] = {5, 0}; + uint8_t expected2[] = {3}; + expect(recv_frame, + when(size, is_equal_to(2)), + when(data, is_equal_to_contents_of(expected1, 2)) + ); + expect(recv_frame, + when(size, is_equal_to(1)), + when(data, is_equal_to_contents_of(expected2, 1)) + ); + recv_byte(&state, 2); + recv_byte(&state, 5); + recv_byte(&state, 1); + recv_byte(&state, 0); + recv_byte(&state, 2); + recv_byte(&state, 3); + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { + uint8_t expected[] = {5, 7}; + expect(recv_frame, + when(size, is_equal_to(2)), + when(data, is_equal_to_contents_of(expected, 2)) + ); + recv_byte(&state, 3); + recv_byte(&state, 1); + recv_byte(&state, 0); + recv_byte(&state, 3); + recv_byte(&state, 5); + recv_byte(&state, 7); + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { + uint8_t expected[] = {5, 7}; + expect(recv_frame, + when(size, is_equal_to(2)), + when(data, is_equal_to_contents_of(expected, 2)) + ); + recv_byte(&state, 2); + recv_byte(&state, 9); + recv_byte(&state, 4); // This should have been zero + recv_byte(&state, 0); + recv_byte(&state, 3); + recv_byte(&state, 5); + recv_byte(&state, 7); + recv_byte(&state, 0); +} From e8cb6d8023cf2912a08dc1a0a1b108b6dbc429cc Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 15:28:57 +0200 Subject: [PATCH 10/64] Bytestuffer recv handling of long frames --- serial_link/protocol/byte_stuffer.c | 26 ++++++-- serial_link/tests/byte_stuffer_tests.c | 92 ++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 6 deletions(-) diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index e578f88dc1a..cc7afe97ae9 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -24,26 +24,35 @@ SOFTWARE. #include "protocol/byte_stuffer.h" #include "protocol/frame_validator.h" +#include // This implements the "Consistent overhead byte stuffing protocol" // https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing // http://www.stuartcheshire.org/papers/COBSforToN.pdf +#define MAX_FRAME_SIZE 1024 + typedef struct byte_stuffer_state { uint16_t next_zero; uint16_t data_pos; - uint8_t data[256]; + bool long_frame; + uint8_t data[MAX_FRAME_SIZE]; }byte_stuffer_state_t; void init_byte_stuffer_state(byte_stuffer_state_t* state) { state->next_zero = 0; state->data_pos = 0; + state->long_frame = false; +} + +static void start_frame(byte_stuffer_state_t* state, uint8_t data) { } void recv_byte(byte_stuffer_state_t* state, uint8_t data) { // Start of a new frame if (state->next_zero == 0) { state->next_zero = data; + state->long_frame = data == 0xFF; state->data_pos = 0; return; } @@ -56,15 +65,20 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) { } else { // The frame is invalid, so reset - state->next_zero = 0; - state->data_pos = 0; + init_byte_stuffer_state(state); } } else { if (state->next_zero == 0) { - // Special case for zeroes - state->next_zero = data; - state->data[state->data_pos++] = 0; + if (state->long_frame) { + state->next_zero = data; + state->long_frame = data == 0xFF; + } + else { + // Special case for zeroes + state->next_zero = data; + state->data[state->data_pos++] = 0; + } } else { state->data[state->data_pos++] = data; diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 74a349b1f34..a28c3619382 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -154,3 +154,95 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { recv_byte(&state, 7); recv_byte(&state, 0); } + +Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_of_frame) { + uint8_t expected[254]; + int i; + for (i=0;i<254;i++) { + expected[i] = i + 1; + } + expect(recv_frame, + when(size, is_equal_to(254)), + when(data, is_equal_to_contents_of(expected, 254)) + ); + recv_byte(&state, 0xFF); + for (i=0;i<254;i++) { + recv_byte(&state, i+1); + } + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_non_zero) { + uint8_t expected[255]; + int i; + for (i=0;i<254;i++) { + expected[i] = i + 1; + } + expected[254] = 7; + expect(recv_frame, + when(size, is_equal_to(255)), + when(data, is_equal_to_contents_of(expected, 255)) + ); + recv_byte(&state, 0xFF); + for (i=0;i<254;i++) { + recv_byte(&state, i+1); + } + recv_byte(&state, 2); + recv_byte(&state, 7); + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_zero) { + uint8_t expected[255]; + int i; + for (i=0;i<254;i++) { + expected[i] = i + 1; + } + expected[254] = 0; + expect(recv_frame, + when(size, is_equal_to(255)), + when(data, is_equal_to_contents_of(expected, 255)) + ); + recv_byte(&state, 0xFF); + for (i=0;i<254;i++) { + recv_byte(&state, i+1); + } + recv_byte(&state, 1); + recv_byte(&state, 1); + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { + uint8_t expected[515]; + int i; + int j; + for (j=0;j<2;j++) { + for (i=0;i<254;i++) { + expected[i+254*j] = i + 1; + } + } + for (i=0;i<7;i++) { + expected[254*2+i] = i + 1; + } + expect(recv_frame, + when(size, is_equal_to(515)), + when(data, is_equal_to_contents_of(expected, 510)) + ); + recv_byte(&state, 0xFF); + for (i=0;i<254;i++) { + recv_byte(&state, i+1); + } + recv_byte(&state, 0xFF); + for (i=0;i<254;i++) { + recv_byte(&state, i+1); + } + recv_byte(&state, 8); + recv_byte(&state, 1); + recv_byte(&state, 2); + recv_byte(&state, 3); + recv_byte(&state, 4); + recv_byte(&state, 5); + recv_byte(&state, 6); + recv_byte(&state, 7); + recv_byte(&state, 0); +} From 26537474ae1d65cdf40276299d7e04648474357b Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 15:57:44 +0200 Subject: [PATCH 11/64] Add byte stuffer recv handling of too long frames --- .gitignore | 1 + serial_link/protocol/byte_stuffer.c | 17 ++++++--- serial_link/tests/byte_stuffer_tests.c | 48 ++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..2d68e206e81 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.stackdump diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index cc7afe97ae9..f0071b1f777 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -45,9 +45,6 @@ void init_byte_stuffer_state(byte_stuffer_state_t* state) { state->long_frame = false; } -static void start_frame(byte_stuffer_state_t* state, uint8_t data) { -} - void recv_byte(byte_stuffer_state_t* state, uint8_t data) { // Start of a new frame if (state->next_zero == 0) { @@ -61,7 +58,9 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) { if (data == 0) { if (state->next_zero == 0) { // The frame is completed - recv_frame(state->data, state->data_pos); + if (state->data_pos > 0) { + recv_frame(state->data, state->data_pos); + } } else { // The frame is invalid, so reset @@ -69,8 +68,16 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) { } } else { - if (state->next_zero == 0) { + if (state->data_pos == MAX_FRAME_SIZE) { + // We exceeded our maximum frame size + // therefore there's nothing else to do than reset to a new frame + state->next_zero = data; + state->long_frame = data == 0xFF; + state->data_pos = 0; + } + else if (state->next_zero == 0) { if (state->long_frame) { + // This is part of a long frame, so continue state->next_zero = data; state->long_frame = data == 0xFF; } diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index a28c3619382..bfa0193861b 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -55,6 +55,12 @@ Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { recv_byte(&state, 0x4A); } +Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) { + never_expect(recv_frame); + recv_byte(&state, 1); + recv_byte(&state, 0); +} + Ensure(ByteStuffer, receives_single_byte_valid_frame) { uint8_t expected[] = {0x37}; expect(recv_frame, @@ -246,3 +252,45 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { recv_byte(&state, 7); recv_byte(&state, 0); } + +Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { + uint8_t expected[MAX_FRAME_SIZE] = {}; + expect(recv_frame, + when(size, is_equal_to(MAX_FRAME_SIZE)), + when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE)) + ); + int i; + recv_byte(&state, 1); + for(i=0;i Date: Sun, 14 Feb 2016 17:45:25 +0200 Subject: [PATCH 12/64] Add sending of small frames with no zeroes --- serial_link/protocol/byte_stuffer.c | 12 +++++- serial_link/protocol/byte_stuffer.h | 1 + serial_link/protocol/physical.h | 25 +++++++++++ serial_link/tests/byte_stuffer_tests.c | 60 +++++++++++++++++++------- 4 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 serial_link/protocol/physical.h diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index f0071b1f777..dfd5942ebc2 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -24,7 +24,7 @@ SOFTWARE. #include "protocol/byte_stuffer.h" #include "protocol/frame_validator.h" -#include +#include "protocol/physical.h" // This implements the "Consistent overhead byte stuffing protocol" // https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing @@ -92,3 +92,13 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) { } } } + +void send_frame(uint8_t* data, uint16_t size) { + if (size > 0) { + uint8_t numZeroes = size + 1; + const uint8_t zero = 0; + send_data(&numZeroes, 1); + send_data(data, size); + send_data(&zero, 1); + } +} diff --git a/serial_link/protocol/byte_stuffer.h b/serial_link/protocol/byte_stuffer.h index 9a5551fab58..ea6b8451d4e 100644 --- a/serial_link/protocol/byte_stuffer.h +++ b/serial_link/protocol/byte_stuffer.h @@ -25,3 +25,4 @@ SOFTWARE. typedef struct byte_stuffer_state byte_stuffer_state_t; void init_byte_stuffer_state(byte_stuffer_state_t* state); void recv_byte(byte_stuffer_state_t* state, uint8_t data); +void send_frame(uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/physical.h b/serial_link/protocol/physical.h new file mode 100644 index 00000000000..73a7855dfba --- /dev/null +++ b/serial_link/protocol/physical.h @@ -0,0 +1,25 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +void send_data(const uint8_t* data, uint16_t size); diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index bfa0193861b..6e44c9170c5 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -27,12 +27,16 @@ SOFTWARE. #include "protocol/byte_stuffer.h" #include "protocol/byte_stuffer.c" #include "protocol/frame_validator.h" +#include "protocol/physical.h" -byte_stuffer_state_t state; +static byte_stuffer_state_t state; +static uint8_t sent_data[MAX_FRAME_SIZE*2]; +static uint16_t sent_data_size; Describe(ByteStuffer); BeforeEach(ByteStuffer) { init_byte_stuffer_state(&state); + sent_data_size = 0; } AfterEach(ByteStuffer) {} @@ -40,6 +44,11 @@ void recv_frame(uint8_t* data, uint16_t size) { mock(data, size); } +void send_data(const uint8_t* data, uint16_t size) { + memcpy(sent_data + sent_data_size, data, size); + sent_data_size += size; +} + Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { never_expect(recv_frame); recv_byte(&state, 0); @@ -66,7 +75,7 @@ Ensure(ByteStuffer, receives_single_byte_valid_frame) { expect(recv_frame, when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) - ); + ); recv_byte(&state, 2); recv_byte(&state, 0x37); recv_byte(&state, 0); @@ -77,7 +86,7 @@ Ensure(ByteStuffer, receives_three_bytes_valid_frame) { expect(recv_frame, when(size, is_equal_to(3)), when(data, is_equal_to_contents_of(expected, 3)) - ); + ); recv_byte(&state, 4); recv_byte(&state, 0x37); recv_byte(&state, 0x99); @@ -90,7 +99,7 @@ Ensure(ByteStuffer, receives_single_zero_valid_frame) { expect(recv_frame, when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) - ); + ); recv_byte(&state, 1); recv_byte(&state, 1); recv_byte(&state, 0); @@ -101,7 +110,7 @@ Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { expect(recv_frame, when(size, is_equal_to(4)), when(data, is_equal_to_contents_of(expected, 4)) - ); + ); recv_byte(&state, 2); recv_byte(&state, 5); recv_byte(&state, 2); @@ -116,11 +125,11 @@ Ensure(ByteStuffer, receives_two_valid_frames) { expect(recv_frame, when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected1, 2)) - ); + ); expect(recv_frame, when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected2, 1)) - ); + ); recv_byte(&state, 2); recv_byte(&state, 5); recv_byte(&state, 1); @@ -135,7 +144,7 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { expect(recv_frame, when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) - ); + ); recv_byte(&state, 3); recv_byte(&state, 1); recv_byte(&state, 0); @@ -150,7 +159,7 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { expect(recv_frame, when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) - ); + ); recv_byte(&state, 2); recv_byte(&state, 9); recv_byte(&state, 4); // This should have been zero @@ -170,7 +179,7 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_ expect(recv_frame, when(size, is_equal_to(254)), when(data, is_equal_to_contents_of(expected, 254)) - ); + ); recv_byte(&state, 0xFF); for (i=0;i<254;i++) { recv_byte(&state, i+1); @@ -188,7 +197,7 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ expect(recv_frame, when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) - ); + ); recv_byte(&state, 0xFF); for (i=0;i<254;i++) { recv_byte(&state, i+1); @@ -208,7 +217,7 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ expect(recv_frame, when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) - ); + ); recv_byte(&state, 0xFF); for (i=0;i<254;i++) { recv_byte(&state, i+1); @@ -233,7 +242,7 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { expect(recv_frame, when(size, is_equal_to(515)), when(data, is_equal_to_contents_of(expected, 510)) - ); + ); recv_byte(&state, 0xFF); for (i=0;i<254;i++) { recv_byte(&state, i+1); @@ -258,7 +267,7 @@ Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { expect(recv_frame, when(size, is_equal_to(MAX_FRAME_SIZE)), when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE)) - ); + ); int i; recv_byte(&state, 1); for(i=0;i Date: Sun, 14 Feb 2016 18:59:01 +0200 Subject: [PATCH 13/64] Hanlde sending of zero bytes for small packets --- serial_link/protocol/byte_stuffer.c | 27 +++++++++++-- serial_link/tests/byte_stuffer_tests.c | 56 ++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index dfd5942ebc2..6118557c1d1 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -93,12 +93,31 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) { } } +static void send_block(uint8_t* start, uint8_t* end, uint8_t num_non_zero) { + send_data(&num_non_zero, 1); + if (end > start) { + send_data(start, end-start); + } +} + void send_frame(uint8_t* data, uint16_t size) { + const uint8_t zero = 0; if (size > 0) { - uint8_t numZeroes = size + 1; - const uint8_t zero = 0; - send_data(&numZeroes, 1); - send_data(data, size); + uint8_t num_non_zero = 1; + uint8_t* end = data + size; + uint8_t* start = data; + while (data < end) { + if (*data == 0) { + send_block(start, data, num_non_zero); + start = data + 1; + num_non_zero = 1; + } + else { + num_non_zero++; + } + ++data; + } + send_block(start, data, num_non_zero); send_data(&zero, 1); } } diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 6e44c9170c5..0c324e9c486 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -312,15 +312,63 @@ Ensure(ByteStuffer, send_zero_size_frame_does_nothing) { Ensure(ByteStuffer, send_one_byte_frame) { uint8_t data[] = {5}; send_frame(data, 1); - assert_that(sent_data_size, is_equal_to(3)); uint8_t expected[] = {2, 5, 0}; - assert_that(sent_data, is_equal_to_contents_of(expected, 3)); + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } Ensure(ByteStuffer, send_two_byte_frame) { uint8_t data[] = {5, 0x77}; send_frame(data, 2); - assert_that(sent_data_size, is_equal_to(4)); uint8_t expected[] = {3, 5, 0x77, 0}; - assert_that(sent_data, is_equal_to_contents_of(expected, 4)); + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} + +Ensure(ByteStuffer, send_one_byte_frame_with_zero) { + uint8_t data[] = {0}; + send_frame(data, 1); + uint8_t expected[] = {1, 1, 0}; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} + +Ensure(ByteStuffer, send_two_byte_frame_starting_with_zero) { + uint8_t data[] = {0, 9}; + send_frame(data, 2); + uint8_t expected[] = {1, 2, 9, 0}; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} + +Ensure(ByteStuffer, send_two_byte_frame_starting_with_non_zero) { + uint8_t data[] = {9, 0}; + send_frame(data, 2); + uint8_t expected[] = {2, 9, 1, 0}; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} + +Ensure(ByteStuffer, send_three_byte_frame_zero_in_the_middle) { + uint8_t data[] = {9, 0, 0x68}; + send_frame(data, 3); + uint8_t expected[] = {2, 9, 2, 0x68, 0}; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} + +Ensure(ByteStuffer, send_three_byte_frame_data_in_the_middle) { + uint8_t data[] = {0, 0x55, 0}; + send_frame(data, 3); + uint8_t expected[] = {1, 2, 0x55, 1, 0}; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} + +Ensure(ByteStuffer, send_three_byte_frame_all_zeroes) { + uint8_t data[] = {0, 0, 0}; + send_frame(data, 3); + uint8_t expected[] = {1, 1, 1, 1, 0}; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } From 2f3ea76428276b871b454effd1164648f5e49f23 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 19:03:34 +0200 Subject: [PATCH 14/64] Fix name of some unit tests --- serial_link/tests/byte_stuffer_tests.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 0c324e9c486..1982421e6ef 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -304,7 +304,7 @@ Ensure(ByteStuffer, received_frame_is_aborted_when_its_too_long) { recv_byte(&state, 0); } -Ensure(ByteStuffer, send_zero_size_frame_does_nothing) { +Ensure(ByteStuffer, does_nothing_when_sending_zero_size_frame) { assert_that(sent_data_size, is_equal_to(0)); send_frame(NULL, 0); } @@ -317,7 +317,7 @@ Ensure(ByteStuffer, send_one_byte_frame) { assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } -Ensure(ByteStuffer, send_two_byte_frame) { +Ensure(ByteStuffer, sends_two_byte_frame) { uint8_t data[] = {5, 0x77}; send_frame(data, 2); uint8_t expected[] = {3, 5, 0x77, 0}; @@ -325,7 +325,7 @@ Ensure(ByteStuffer, send_two_byte_frame) { assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } -Ensure(ByteStuffer, send_one_byte_frame_with_zero) { +Ensure(ByteStuffer, sends_one_byte_frame_with_zero) { uint8_t data[] = {0}; send_frame(data, 1); uint8_t expected[] = {1, 1, 0}; @@ -333,7 +333,7 @@ Ensure(ByteStuffer, send_one_byte_frame_with_zero) { assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } -Ensure(ByteStuffer, send_two_byte_frame_starting_with_zero) { +Ensure(ByteStuffer, sends_two_byte_frame_starting_with_zero) { uint8_t data[] = {0, 9}; send_frame(data, 2); uint8_t expected[] = {1, 2, 9, 0}; @@ -341,7 +341,7 @@ Ensure(ByteStuffer, send_two_byte_frame_starting_with_zero) { assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } -Ensure(ByteStuffer, send_two_byte_frame_starting_with_non_zero) { +Ensure(ByteStuffer, sends_two_byte_frame_starting_with_non_zero) { uint8_t data[] = {9, 0}; send_frame(data, 2); uint8_t expected[] = {2, 9, 1, 0}; @@ -349,7 +349,7 @@ Ensure(ByteStuffer, send_two_byte_frame_starting_with_non_zero) { assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } -Ensure(ByteStuffer, send_three_byte_frame_zero_in_the_middle) { +Ensure(ByteStuffer, sends_three_byte_frame_zero_in_the_middle) { uint8_t data[] = {9, 0, 0x68}; send_frame(data, 3); uint8_t expected[] = {2, 9, 2, 0x68, 0}; @@ -357,7 +357,7 @@ Ensure(ByteStuffer, send_three_byte_frame_zero_in_the_middle) { assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } -Ensure(ByteStuffer, send_three_byte_frame_data_in_the_middle) { +Ensure(ByteStuffer, sends_three_byte_frame_data_in_the_middle) { uint8_t data[] = {0, 0x55, 0}; send_frame(data, 3); uint8_t expected[] = {1, 2, 0x55, 1, 0}; @@ -365,7 +365,7 @@ Ensure(ByteStuffer, send_three_byte_frame_data_in_the_middle) { assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } -Ensure(ByteStuffer, send_three_byte_frame_all_zeroes) { +Ensure(ByteStuffer, sends_three_byte_frame_with_all_zeroes) { uint8_t data[] = {0, 0, 0}; send_frame(data, 3); uint8_t expected[] = {1, 1, 1, 1, 0}; From 6956c177da8737f999eef72a21f0fc3caea2ac3e Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 19:56:22 +0200 Subject: [PATCH 15/64] Add byte stuffing send support for large frames --- serial_link/protocol/byte_stuffer.c | 21 +++++++--- serial_link/tests/byte_stuffer_tests.c | 56 ++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index 6118557c1d1..69cfca3594f 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -25,6 +25,7 @@ SOFTWARE. #include "protocol/byte_stuffer.h" #include "protocol/frame_validator.h" #include "protocol/physical.h" +#include // This implements the "Consistent overhead byte stuffing protocol" // https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing @@ -103,19 +104,29 @@ static void send_block(uint8_t* start, uint8_t* end, uint8_t num_non_zero) { void send_frame(uint8_t* data, uint16_t size) { const uint8_t zero = 0; if (size > 0) { - uint8_t num_non_zero = 1; + uint16_t num_non_zero = 1; uint8_t* end = data + size; uint8_t* start = data; while (data < end) { - if (*data == 0) { + if (num_non_zero == 0xFF) { + // There's more data after big non-zero block + // So send it, and start a new block send_block(start, data, num_non_zero); - start = data + 1; + start = data; num_non_zero = 1; } else { - num_non_zero++; + if (*data == 0) { + // A zero encountered, so send the block + send_block(start, data, num_non_zero); + start = data + 1; + num_non_zero = 1; + } + else { + num_non_zero++; + } + ++data; } - ++data; } send_block(start, data, num_non_zero); send_data(&zero, 1); diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 1982421e6ef..8598cddd30b 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -372,3 +372,59 @@ Ensure(ByteStuffer, sends_three_byte_frame_with_all_zeroes) { assert_that(sent_data_size, is_equal_to(sizeof(expected))); assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } + +Ensure(ByteStuffer, sends_frame_with_254_non_zeroes) { + uint8_t data[254]; + int i; + for(i=0;i<254;i++) { + data[i] = i + 1; + } + send_frame(data, 254); + uint8_t expected[256]; + expected[0] = 0xFF; + for(i=1;i<255;i++) { + expected[i] = i; + } + expected[255] = 0; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} + +Ensure(ByteStuffer, sends_frame_with_255_non_zeroes) { + uint8_t data[255]; + int i; + for(i=0;i<255;i++) { + data[i] = i + 1; + } + send_frame(data, 255); + uint8_t expected[258]; + expected[0] = 0xFF; + for(i=1;i<255;i++) { + expected[i] = i; + } + expected[255] = 2; + expected[256] = 255; + expected[257] = 0; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} + +Ensure(ByteStuffer, sends_frame_with_254_non_zeroes_followed_by_zero) { + uint8_t data[255]; + int i; + for(i=0;i<254;i++) { + data[i] = i + 1; + } + data[255] = 0; + send_frame(data, 255); + uint8_t expected[258]; + expected[0] = 0xFF; + for(i=1;i<255;i++) { + expected[i] = i; + } + expected[255] = 1; + expected[256] = 1; + expected[257] = 0; + assert_that(sent_data_size, is_equal_to(sizeof(expected))); + assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); +} From 713465fb416785c3a544e8626f85aca7e57dafa1 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 20:07:51 +0200 Subject: [PATCH 16/64] Add some full round trip tests Mostly to ensure that the receive and send framing works the same way. Especially for the special case with 254 non-zeros. --- serial_link/tests/byte_stuffer_tests.c | 77 ++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 8598cddd30b..fbdf39e1b26 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -428,3 +428,80 @@ Ensure(ByteStuffer, sends_frame_with_254_non_zeroes_followed_by_zero) { assert_that(sent_data_size, is_equal_to(sizeof(expected))); assert_that(sent_data, is_equal_to_contents_of(expected, sizeof(expected))); } + +Ensure(ByteStuffer, sends_and_receives_full_roundtrip_small_packet) { + uint8_t original_data[] = { 1, 2, 3}; + send_frame(original_data, sizeof(original_data)); + expect(recv_frame, + when(size, is_equal_to(sizeof(original_data))), + when(data, is_equal_to_contents_of(original_data, sizeof(original_data))) + ); + int i; + for(i=0;i Date: Sun, 14 Feb 2016 20:45:59 +0200 Subject: [PATCH 17/64] Start implement frame validator --- serial_link/protocol/frame_router.h | 25 ++++++ serial_link/protocol/frame_validator.c | 105 ++++++++++++++++++++++ serial_link/tests/frame_validator_tests.c | 44 +++++++++ 3 files changed, 174 insertions(+) create mode 100644 serial_link/protocol/frame_router.h create mode 100644 serial_link/protocol/frame_validator.c create mode 100644 serial_link/tests/frame_validator_tests.c diff --git a/serial_link/protocol/frame_router.h b/serial_link/protocol/frame_router.h new file mode 100644 index 00000000000..b5beba13c3f --- /dev/null +++ b/serial_link/protocol/frame_router.h @@ -0,0 +1,25 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +void route_frame(uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c new file mode 100644 index 00000000000..1ffd3aad855 --- /dev/null +++ b/serial_link/protocol/frame_validator.c @@ -0,0 +1,105 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "protocol/frame_validator.h" + +const uint32_t poly8_lookup[256] = +{ + 0, 0x77073096, 0xEE0E612C, 0x990951BA, + 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, + 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, + 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, + 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, + 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, + 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, + 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, + 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, + 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, + 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, + 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, + 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, + 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, + 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, + 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, + 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, + 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, + 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, + 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, + 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, + 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, + 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, + 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, + 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, + 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, + 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, + 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, + 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, + 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, + 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, + 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, + 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, + 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, + 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, + 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, + 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, + 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, + 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, + 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, + 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, + 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, + 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, + 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, + 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, + 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, + 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, + 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, + 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, + 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, + 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, + 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, + 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, + 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, + 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, + 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, + 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, + 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, + 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, + 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, + 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, + 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, + 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, + 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D +}; + +static uint32_t crc32_byte(uint8_t *p, uint32_t bytelength) +{ + uint32_t crc = 0xffffffff; + while (bytelength-- !=0) crc = poly8_lookup[((uint8_t) crc ^ *(p++))] ^ (crc >> 8); + // return (~crc); also works + return (crc ^ 0xffffffff); +} + +void recv_frame(uint8_t* data, uint16_t size) { + +} diff --git a/serial_link/tests/frame_validator_tests.c b/serial_link/tests/frame_validator_tests.c new file mode 100644 index 00000000000..f6a4fcd8428 --- /dev/null +++ b/serial_link/tests/frame_validator_tests.c @@ -0,0 +1,44 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include +#include +#include "protocol/frame_validator.c" + +void route_frame(uint8_t* data, uint16_t size) { + mock(data, size); +} + +Describe(FrameValidator); +BeforeEach(FrameValidator) {} +AfterEach(FrameValidator) {} + +Ensure(FrameValidator, doesnt_validate_frames_under_5_bytes) { + never_expect(route_frame); + uint8_t data[] = {1, 2}; + recv_frame(0, 1); + recv_frame(data, 2); + recv_frame(data, 3); + recv_frame(data, 4); +} From 62058329ff9000589ddba6454ff8ef8a551b7243 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 21:13:16 +0200 Subject: [PATCH 18/64] Add crc32 validation of received frames --- serial_link/protocol/frame_validator.c | 9 +++++- serial_link/tests/frame_validator_tests.c | 35 +++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index 1ffd3aad855..d3337f6e328 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -23,6 +23,7 @@ SOFTWARE. */ #include "protocol/frame_validator.h" +#include "protocol/frame_router.h" const uint32_t poly8_lookup[256] = { @@ -101,5 +102,11 @@ static uint32_t crc32_byte(uint8_t *p, uint32_t bytelength) } void recv_frame(uint8_t* data, uint16_t size) { - + if (size > 4) { + uint32_t frame_crc = *(uint32_t*)(data + size - 4); + uint32_t expected_crc = crc32_byte(data, size - 4); + if (frame_crc == expected_crc) { + route_frame(data, size-4); + } + } } diff --git a/serial_link/tests/frame_validator_tests.c b/serial_link/tests/frame_validator_tests.c index f6a4fcd8428..1aca9f95fce 100644 --- a/serial_link/tests/frame_validator_tests.c +++ b/serial_link/tests/frame_validator_tests.c @@ -42,3 +42,38 @@ Ensure(FrameValidator, doesnt_validate_frames_under_5_bytes) { recv_frame(data, 3); recv_frame(data, 4); } + +Ensure(FrameValidator, validates_one_byte_frame_with_correct_crc) { + uint8_t data[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3}; + expect(route_frame, + when(size, is_equal_to(1)), + when(data, is_equal_to_contents_of(data, 1)) + ); + recv_frame(data, 5); +} + +Ensure(FrameValidator, does_not_validate_one_byte_frame_with_incorrect_crc) { + uint8_t data[] = {0x44, 0, 0, 0, 0}; + never_expect(route_frame); + recv_frame(data, 5); +} + +Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) { + uint8_t data[] = {0x44, 0x10, 0xFF, 0x00, 0x74, 0x4E, 0x30, 0xBA}; + expect(route_frame, + when(size, is_equal_to(4)), + when(data, is_equal_to_contents_of(data, 4)) + ); + recv_frame(data, 8); +} + +Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) { + //0xBA304E74 + //0x470B99F4 + uint8_t data[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47}; + expect(route_frame, + when(size, is_equal_to(5)), + when(data, is_equal_to_contents_of(data, 5)) + ); + recv_frame(data, 9); +} From 2a6696bd3d0837c2e655a99000bcbcc759b40075 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 21:31:01 +0200 Subject: [PATCH 19/64] Add validator send frame --- serial_link/protocol/frame_validator.c | 7 ++++++ serial_link/tests/frame_validator_tests.c | 26 +++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index d3337f6e328..fdb3ef51c3f 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -24,6 +24,7 @@ SOFTWARE. #include "protocol/frame_validator.h" #include "protocol/frame_router.h" +#include "protocol/byte_stuffer.h" const uint32_t poly8_lookup[256] = { @@ -110,3 +111,9 @@ void recv_frame(uint8_t* data, uint16_t size) { } } } + +void validator_send_frame(uint8_t* data, uint16_t size) { + uint32_t* crc = (uint32_t*)(data + size); + *crc = crc32_byte(data, size); + send_frame(data, size + 4); +} diff --git a/serial_link/tests/frame_validator_tests.c b/serial_link/tests/frame_validator_tests.c index 1aca9f95fce..816256f6b7a 100644 --- a/serial_link/tests/frame_validator_tests.c +++ b/serial_link/tests/frame_validator_tests.c @@ -30,6 +30,10 @@ void route_frame(uint8_t* data, uint16_t size) { mock(data, size); } +void send_frame(uint8_t* data, uint16_t size) { + mock(data, size); +} + Describe(FrameValidator); BeforeEach(FrameValidator) {} AfterEach(FrameValidator) {} @@ -68,8 +72,6 @@ Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) { } Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) { - //0xBA304E74 - //0x470B99F4 uint8_t data[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47}; expect(route_frame, when(size, is_equal_to(5)), @@ -77,3 +79,23 @@ Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) { ); recv_frame(data, 9); } + +Ensure(FrameValidator, sends_one_byte_with_correct_crc) { + uint8_t original[] = {0x44, 0, 0, 0, 0}; + uint8_t expected[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3}; + expect(send_frame, + when(size, is_equal_to(sizeof(expected))), + when(data, is_equal_to_contents_of(expected, sizeof(expected))) + ); + validator_send_frame(original, 1); +} + +Ensure(FrameValidator, sends_five_bytes_with_correct_crc) { + uint8_t original[] = {1, 2, 3, 4, 5, 0, 0, 0, 0}; + uint8_t expected[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47}; + expect(send_frame, + when(size, is_equal_to(sizeof(expected))), + when(data, is_equal_to_contents_of(expected, sizeof(expected))) + ); + validator_send_frame(original, 5); +} From 0eb62c4ce61e19cf7629bb74cf01d812c042ac57 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 21:33:14 +0200 Subject: [PATCH 20/64] Rename recv_frame to validator_recv_frame --- serial_link/protocol/byte_stuffer.c | 2 +- serial_link/protocol/frame_validator.c | 2 +- serial_link/protocol/frame_validator.h | 4 +- serial_link/tests/byte_stuffer_tests.c | 50 +++++++++++------------ serial_link/tests/frame_validator_tests.c | 16 ++++---- 5 files changed, 38 insertions(+), 36 deletions(-) diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index 69cfca3594f..f9aa02a3f50 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -60,7 +60,7 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) { if (state->next_zero == 0) { // The frame is completed if (state->data_pos > 0) { - recv_frame(state->data, state->data_pos); + validator_recv_frame(state->data, state->data_pos); } } else { diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index fdb3ef51c3f..0ffcda0473a 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -102,7 +102,7 @@ static uint32_t crc32_byte(uint8_t *p, uint32_t bytelength) return (crc ^ 0xffffffff); } -void recv_frame(uint8_t* data, uint16_t size) { +void validator_recv_frame(uint8_t* data, uint16_t size) { if (size > 4) { uint32_t frame_crc = *(uint32_t*)(data + size - 4); uint32_t expected_crc = crc32_byte(data, size - 4); diff --git a/serial_link/protocol/frame_validator.h b/serial_link/protocol/frame_validator.h index ee1f9a78aea..2e518894fbb 100644 --- a/serial_link/protocol/frame_validator.h +++ b/serial_link/protocol/frame_validator.h @@ -22,4 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -void recv_frame(uint8_t* data, uint16_t size); +void validator_recv_frame(uint8_t* data, uint16_t size); +// The buffer pointed to by the data needs 4 additional bytes +void validator_send_frame(uint8_t* data, uint16_t size); diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index fbdf39e1b26..78bcbb4734c 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -40,7 +40,7 @@ BeforeEach(ByteStuffer) { } AfterEach(ByteStuffer) {} -void recv_frame(uint8_t* data, uint16_t size) { +void validator_recv_frame(uint8_t* data, uint16_t size) { mock(data, size); } @@ -50,29 +50,29 @@ void send_data(const uint8_t* data, uint16_t size) { } Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { - never_expect(recv_frame); + never_expect(validator_recv_frame); recv_byte(&state, 0); } Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) { - never_expect(recv_frame); + never_expect(validator_recv_frame); recv_byte(&state, 0xFF); } Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { - never_expect(recv_frame); + never_expect(validator_recv_frame); recv_byte(&state, 0x4A); } Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) { - never_expect(recv_frame); + never_expect(validator_recv_frame); recv_byte(&state, 1); recv_byte(&state, 0); } Ensure(ByteStuffer, receives_single_byte_valid_frame) { uint8_t expected[] = {0x37}; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); @@ -83,7 +83,7 @@ Ensure(ByteStuffer, receives_single_byte_valid_frame) { Ensure(ByteStuffer, receives_three_bytes_valid_frame) { uint8_t expected[] = {0x37, 0x99, 0xFF}; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(3)), when(data, is_equal_to_contents_of(expected, 3)) ); @@ -96,7 +96,7 @@ Ensure(ByteStuffer, receives_three_bytes_valid_frame) { Ensure(ByteStuffer, receives_single_zero_valid_frame) { uint8_t expected[] = {0}; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); @@ -107,7 +107,7 @@ Ensure(ByteStuffer, receives_single_zero_valid_frame) { Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { uint8_t expected[] = {5, 0, 3, 0}; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(4)), when(data, is_equal_to_contents_of(expected, 4)) ); @@ -122,11 +122,11 @@ Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { Ensure(ByteStuffer, receives_two_valid_frames) { uint8_t expected1[] = {5, 0}; uint8_t expected2[] = {3}; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected1, 2)) ); - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected2, 1)) ); @@ -141,7 +141,7 @@ Ensure(ByteStuffer, receives_two_valid_frames) { Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { uint8_t expected[] = {5, 7}; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); @@ -156,7 +156,7 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { uint8_t expected[] = {5, 7}; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); @@ -176,7 +176,7 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_ for (i=0;i<254;i++) { expected[i] = i + 1; } - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(254)), when(data, is_equal_to_contents_of(expected, 254)) ); @@ -194,7 +194,7 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ expected[i] = i + 1; } expected[254] = 7; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); @@ -214,7 +214,7 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ expected[i] = i + 1; } expected[254] = 0; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); @@ -239,7 +239,7 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { for (i=0;i<7;i++) { expected[254*2+i] = i + 1; } - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(515)), when(data, is_equal_to_contents_of(expected, 510)) ); @@ -264,7 +264,7 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { uint8_t expected[MAX_FRAME_SIZE] = {}; - expect(recv_frame, + expect(validator_recv_frame, when(size, is_equal_to(MAX_FRAME_SIZE)), when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE)) ); @@ -278,7 +278,7 @@ Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { Ensure(ByteStuffer, doesnt_recv_a_frame_thats_too_long_all_zeroes) { uint8_t expected[1] = {0}; - never_expect(recv_frame); + never_expect(validator_recv_frame); int i; recv_byte(&state, 1); for(i=0;i Date: Sun, 14 Feb 2016 21:34:40 +0200 Subject: [PATCH 21/64] Rename route_frame to route_incoming_frame --- serial_link/protocol/frame_router.h | 2 +- serial_link/protocol/frame_validator.c | 2 +- serial_link/tests/frame_validator_tests.c | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/serial_link/protocol/frame_router.h b/serial_link/protocol/frame_router.h index b5beba13c3f..dd1bfb12302 100644 --- a/serial_link/protocol/frame_router.h +++ b/serial_link/protocol/frame_router.h @@ -22,4 +22,4 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -void route_frame(uint8_t* data, uint16_t size); +void route_incoming_frame(uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index 0ffcda0473a..0a5f05068eb 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -107,7 +107,7 @@ void validator_recv_frame(uint8_t* data, uint16_t size) { uint32_t frame_crc = *(uint32_t*)(data + size - 4); uint32_t expected_crc = crc32_byte(data, size - 4); if (frame_crc == expected_crc) { - route_frame(data, size-4); + route_incoming_frame(data, size-4); } } } diff --git a/serial_link/tests/frame_validator_tests.c b/serial_link/tests/frame_validator_tests.c index 3938f3eacac..6f9b746f697 100644 --- a/serial_link/tests/frame_validator_tests.c +++ b/serial_link/tests/frame_validator_tests.c @@ -26,7 +26,7 @@ SOFTWARE. #include #include "protocol/frame_validator.c" -void route_frame(uint8_t* data, uint16_t size) { +void route_incoming_frame(uint8_t* data, uint16_t size) { mock(data, size); } @@ -39,7 +39,7 @@ BeforeEach(FrameValidator) {} AfterEach(FrameValidator) {} Ensure(FrameValidator, doesnt_validate_frames_under_5_bytes) { - never_expect(route_frame); + never_expect(route_incoming_frame); uint8_t data[] = {1, 2}; validator_recv_frame(0, 1); validator_recv_frame(data, 2); @@ -49,7 +49,7 @@ Ensure(FrameValidator, doesnt_validate_frames_under_5_bytes) { Ensure(FrameValidator, validates_one_byte_frame_with_correct_crc) { uint8_t data[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3}; - expect(route_frame, + expect(route_incoming_frame, when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(data, 1)) ); @@ -58,13 +58,13 @@ Ensure(FrameValidator, validates_one_byte_frame_with_correct_crc) { Ensure(FrameValidator, does_not_validate_one_byte_frame_with_incorrect_crc) { uint8_t data[] = {0x44, 0, 0, 0, 0}; - never_expect(route_frame); + never_expect(route_incoming_frame); validator_recv_frame(data, 5); } Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) { uint8_t data[] = {0x44, 0x10, 0xFF, 0x00, 0x74, 0x4E, 0x30, 0xBA}; - expect(route_frame, + expect(route_incoming_frame, when(size, is_equal_to(4)), when(data, is_equal_to_contents_of(data, 4)) ); @@ -73,7 +73,7 @@ Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) { Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) { uint8_t data[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47}; - expect(route_frame, + expect(route_incoming_frame, when(size, is_equal_to(5)), when(data, is_equal_to_contents_of(data, 5)) ); From 8d50880686dfc2b06958fc3bd6212945ee9029c4 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 21:49:31 +0200 Subject: [PATCH 22/64] Use memcpy instead of cast to avoid alignment issues --- serial_link/protocol/frame_validator.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index 0a5f05068eb..f326097ffad 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -104,7 +104,8 @@ static uint32_t crc32_byte(uint8_t *p, uint32_t bytelength) void validator_recv_frame(uint8_t* data, uint16_t size) { if (size > 4) { - uint32_t frame_crc = *(uint32_t*)(data + size - 4); + uint32_t frame_crc; + memcpy(&frame_crc, data + size -4, 4); uint32_t expected_crc = crc32_byte(data, size - 4); if (frame_crc == expected_crc) { route_incoming_frame(data, size-4); @@ -113,7 +114,7 @@ void validator_recv_frame(uint8_t* data, uint16_t size) { } void validator_send_frame(uint8_t* data, uint16_t size) { - uint32_t* crc = (uint32_t*)(data + size); - *crc = crc32_byte(data, size); + uint32_t crc = crc32_byte(data, size); + memcpy(data + size, &crc, 4); send_frame(data, size + 4); } From 419908e5ef15481bd22dcd4173a25562f6153a5a Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 17 Feb 2016 09:07:38 +0200 Subject: [PATCH 23/64] Hide the byte stuffer state inside it --- serial_link/protocol/byte_stuffer.c | 52 ++++---- serial_link/protocol/byte_stuffer.h | 5 +- serial_link/tests/byte_stuffer_tests.c | 177 ++++++++++++------------- 3 files changed, 117 insertions(+), 117 deletions(-) diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index f9aa02a3f50..e5a26c2c2e4 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -40,56 +40,58 @@ typedef struct byte_stuffer_state { uint8_t data[MAX_FRAME_SIZE]; }byte_stuffer_state_t; -void init_byte_stuffer_state(byte_stuffer_state_t* state) { - state->next_zero = 0; - state->data_pos = 0; - state->long_frame = false; +static byte_stuffer_state_t state; + +void init_byte_stuffer(void) { + state.next_zero = 0; + state.data_pos = 0; + state.long_frame = false; } -void recv_byte(byte_stuffer_state_t* state, uint8_t data) { +void recv_byte(uint8_t data) { // Start of a new frame - if (state->next_zero == 0) { - state->next_zero = data; - state->long_frame = data == 0xFF; - state->data_pos = 0; + if (state.next_zero == 0) { + state.next_zero = data; + state.long_frame = data == 0xFF; + state.data_pos = 0; return; } - state->next_zero--; + state.next_zero--; if (data == 0) { - if (state->next_zero == 0) { + if (state.next_zero == 0) { // The frame is completed - if (state->data_pos > 0) { - validator_recv_frame(state->data, state->data_pos); + if (state.data_pos > 0) { + validator_recv_frame(state.data, state.data_pos); } } else { // The frame is invalid, so reset - init_byte_stuffer_state(state); + init_byte_stuffer(); } } else { - if (state->data_pos == MAX_FRAME_SIZE) { + if (state.data_pos == MAX_FRAME_SIZE) { // We exceeded our maximum frame size // therefore there's nothing else to do than reset to a new frame - state->next_zero = data; - state->long_frame = data == 0xFF; - state->data_pos = 0; + state.next_zero = data; + state.long_frame = data == 0xFF; + state.data_pos = 0; } - else if (state->next_zero == 0) { - if (state->long_frame) { + else if (state.next_zero == 0) { + if (state.long_frame) { // This is part of a long frame, so continue - state->next_zero = data; - state->long_frame = data == 0xFF; + state.next_zero = data; + state.long_frame = data == 0xFF; } else { // Special case for zeroes - state->next_zero = data; - state->data[state->data_pos++] = 0; + state.next_zero = data; + state.data[state.data_pos++] = 0; } } else { - state->data[state->data_pos++] = data; + state.data[state.data_pos++] = data; } } } diff --git a/serial_link/protocol/byte_stuffer.h b/serial_link/protocol/byte_stuffer.h index ea6b8451d4e..4dcdd24c112 100644 --- a/serial_link/protocol/byte_stuffer.h +++ b/serial_link/protocol/byte_stuffer.h @@ -22,7 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -typedef struct byte_stuffer_state byte_stuffer_state_t; -void init_byte_stuffer_state(byte_stuffer_state_t* state); -void recv_byte(byte_stuffer_state_t* state, uint8_t data); +void init_byte_stuffer(void); +void recv_byte(uint8_t data); void send_frame(uint8_t* data, uint16_t size); diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 78bcbb4734c..f29cc129936 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -29,13 +29,12 @@ SOFTWARE. #include "protocol/frame_validator.h" #include "protocol/physical.h" -static byte_stuffer_state_t state; static uint8_t sent_data[MAX_FRAME_SIZE*2]; static uint16_t sent_data_size; Describe(ByteStuffer); BeforeEach(ByteStuffer) { - init_byte_stuffer_state(&state); + init_byte_stuffer(); sent_data_size = 0; } AfterEach(ByteStuffer) {} @@ -51,23 +50,23 @@ void send_data(const uint8_t* data, uint16_t size) { Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { never_expect(validator_recv_frame); - recv_byte(&state, 0); + recv_byte(0); } Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) { never_expect(validator_recv_frame); - recv_byte(&state, 0xFF); + recv_byte(0xFF); } Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { never_expect(validator_recv_frame); - recv_byte(&state, 0x4A); + recv_byte(0x4A); } Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) { never_expect(validator_recv_frame); - recv_byte(&state, 1); - recv_byte(&state, 0); + recv_byte(1); + recv_byte(0); } Ensure(ByteStuffer, receives_single_byte_valid_frame) { @@ -76,9 +75,9 @@ Ensure(ByteStuffer, receives_single_byte_valid_frame) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); - recv_byte(&state, 2); - recv_byte(&state, 0x37); - recv_byte(&state, 0); + recv_byte(2); + recv_byte(0x37); + recv_byte(0); } Ensure(ByteStuffer, receives_three_bytes_valid_frame) { @@ -87,11 +86,11 @@ Ensure(ByteStuffer, receives_three_bytes_valid_frame) { when(size, is_equal_to(3)), when(data, is_equal_to_contents_of(expected, 3)) ); - recv_byte(&state, 4); - recv_byte(&state, 0x37); - recv_byte(&state, 0x99); - recv_byte(&state, 0xFF); - recv_byte(&state, 0); + recv_byte(4); + recv_byte(0x37); + recv_byte(0x99); + recv_byte(0xFF); + recv_byte(0); } Ensure(ByteStuffer, receives_single_zero_valid_frame) { @@ -100,9 +99,9 @@ Ensure(ByteStuffer, receives_single_zero_valid_frame) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); - recv_byte(&state, 1); - recv_byte(&state, 1); - recv_byte(&state, 0); + recv_byte(1); + recv_byte(1); + recv_byte(0); } Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { @@ -111,12 +110,12 @@ Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { when(size, is_equal_to(4)), when(data, is_equal_to_contents_of(expected, 4)) ); - recv_byte(&state, 2); - recv_byte(&state, 5); - recv_byte(&state, 2); - recv_byte(&state, 3); - recv_byte(&state, 1); - recv_byte(&state, 0); + recv_byte(2); + recv_byte(5); + recv_byte(2); + recv_byte(3); + recv_byte(1); + recv_byte(0); } Ensure(ByteStuffer, receives_two_valid_frames) { @@ -130,13 +129,13 @@ Ensure(ByteStuffer, receives_two_valid_frames) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected2, 1)) ); - recv_byte(&state, 2); - recv_byte(&state, 5); - recv_byte(&state, 1); - recv_byte(&state, 0); - recv_byte(&state, 2); - recv_byte(&state, 3); - recv_byte(&state, 0); + recv_byte(2); + recv_byte(5); + recv_byte(1); + recv_byte(0); + recv_byte(2); + recv_byte(3); + recv_byte(0); } Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { @@ -145,13 +144,13 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); - recv_byte(&state, 3); - recv_byte(&state, 1); - recv_byte(&state, 0); - recv_byte(&state, 3); - recv_byte(&state, 5); - recv_byte(&state, 7); - recv_byte(&state, 0); + recv_byte(3); + recv_byte(1); + recv_byte(0); + recv_byte(3); + recv_byte(5); + recv_byte(7); + recv_byte(0); } Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { @@ -160,14 +159,14 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); - recv_byte(&state, 2); - recv_byte(&state, 9); - recv_byte(&state, 4); // This should have been zero - recv_byte(&state, 0); - recv_byte(&state, 3); - recv_byte(&state, 5); - recv_byte(&state, 7); - recv_byte(&state, 0); + recv_byte(2); + recv_byte(9); + recv_byte(4); // This should have been zero + recv_byte(0); + recv_byte(3); + recv_byte(5); + recv_byte(7); + recv_byte(0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_of_frame) { @@ -180,11 +179,11 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_ when(size, is_equal_to(254)), when(data, is_equal_to_contents_of(expected, 254)) ); - recv_byte(&state, 0xFF); + recv_byte(0xFF); for (i=0;i<254;i++) { - recv_byte(&state, i+1); + recv_byte(i+1); } - recv_byte(&state, 0); + recv_byte(0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_non_zero) { @@ -198,13 +197,13 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); - recv_byte(&state, 0xFF); + recv_byte(0xFF); for (i=0;i<254;i++) { - recv_byte(&state, i+1); + recv_byte(i+1); } - recv_byte(&state, 2); - recv_byte(&state, 7); - recv_byte(&state, 0); + recv_byte(2); + recv_byte(7); + recv_byte(0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_zero) { @@ -218,13 +217,13 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); - recv_byte(&state, 0xFF); + recv_byte(0xFF); for (i=0;i<254;i++) { - recv_byte(&state, i+1); + recv_byte(i+1); } - recv_byte(&state, 1); - recv_byte(&state, 1); - recv_byte(&state, 0); + recv_byte(1); + recv_byte(1); + recv_byte(0); } Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { @@ -243,23 +242,23 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { when(size, is_equal_to(515)), when(data, is_equal_to_contents_of(expected, 510)) ); - recv_byte(&state, 0xFF); + recv_byte(0xFF); for (i=0;i<254;i++) { - recv_byte(&state, i+1); + recv_byte(i+1); } - recv_byte(&state, 0xFF); + recv_byte(0xFF); for (i=0;i<254;i++) { - recv_byte(&state, i+1); + recv_byte(i+1); } - recv_byte(&state, 8); - recv_byte(&state, 1); - recv_byte(&state, 2); - recv_byte(&state, 3); - recv_byte(&state, 4); - recv_byte(&state, 5); - recv_byte(&state, 6); - recv_byte(&state, 7); - recv_byte(&state, 0); + recv_byte(8); + recv_byte(1); + recv_byte(2); + recv_byte(3); + recv_byte(4); + recv_byte(5); + recv_byte(6); + recv_byte(7); + recv_byte(0); } Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { @@ -269,23 +268,23 @@ Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE)) ); int i; - recv_byte(&state, 1); + recv_byte(1); for(i=0;i Date: Sat, 20 Feb 2016 12:06:23 +0200 Subject: [PATCH 24/64] Add link parameter --- serial_link/protocol/byte_stuffer.c | 77 ++++---- serial_link/protocol/byte_stuffer.h | 4 +- serial_link/protocol/frame_router.h | 2 +- serial_link/protocol/frame_validator.c | 8 +- serial_link/protocol/frame_validator.h | 4 +- serial_link/protocol/physical.h | 2 +- serial_link/tests/byte_stuffer_tests.c | 212 +++++++++++----------- serial_link/tests/frame_validator_tests.c | 24 +-- 8 files changed, 171 insertions(+), 162 deletions(-) diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index e5a26c2c2e4..05b7aa6f80e 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -32,6 +32,7 @@ SOFTWARE. // http://www.stuartcheshire.org/papers/COBSforToN.pdf #define MAX_FRAME_SIZE 1024 +#define NUM_LINKS 2 typedef struct byte_stuffer_state { uint16_t next_zero; @@ -40,70 +41,78 @@ typedef struct byte_stuffer_state { uint8_t data[MAX_FRAME_SIZE]; }byte_stuffer_state_t; -static byte_stuffer_state_t state; +static byte_stuffer_state_t states[NUM_LINKS]; -void init_byte_stuffer(void) { - state.next_zero = 0; - state.data_pos = 0; - state.long_frame = false; +void init_byte_stuffer_state(byte_stuffer_state_t* state) { + state->next_zero = 0; + state->data_pos = 0; + state->long_frame = false; } -void recv_byte(uint8_t data) { +void init_byte_stuffer(void) { + int i; + for (i=0;inext_zero == 0) { + state->next_zero = data; + state->long_frame = data == 0xFF; + state->data_pos = 0; return; } - state.next_zero--; + state->next_zero--; if (data == 0) { - if (state.next_zero == 0) { + if (state->next_zero == 0) { // The frame is completed - if (state.data_pos > 0) { - validator_recv_frame(state.data, state.data_pos); + if (state->data_pos > 0) { + validator_recv_frame(link, state->data, state->data_pos); } } else { // The frame is invalid, so reset - init_byte_stuffer(); + init_byte_stuffer_state(state); } } else { - if (state.data_pos == MAX_FRAME_SIZE) { + if (state->data_pos == MAX_FRAME_SIZE) { // We exceeded our maximum frame size // therefore there's nothing else to do than reset to a new frame - state.next_zero = data; - state.long_frame = data == 0xFF; - state.data_pos = 0; + state->next_zero = data; + state->long_frame = data == 0xFF; + state->data_pos = 0; } - else if (state.next_zero == 0) { - if (state.long_frame) { + else if (state->next_zero == 0) { + if (state->long_frame) { // This is part of a long frame, so continue - state.next_zero = data; - state.long_frame = data == 0xFF; + state->next_zero = data; + state->long_frame = data == 0xFF; } else { // Special case for zeroes - state.next_zero = data; - state.data[state.data_pos++] = 0; + state->next_zero = data; + state->data[state->data_pos++] = 0; } } else { - state.data[state.data_pos++] = data; + state->data[state->data_pos++] = data; } } } -static void send_block(uint8_t* start, uint8_t* end, uint8_t num_non_zero) { - send_data(&num_non_zero, 1); +static void send_block(uint8_t link, uint8_t* start, uint8_t* end, uint8_t num_non_zero) { + send_data(link, &num_non_zero, 1); if (end > start) { - send_data(start, end-start); + send_data(link, start, end-start); } } -void send_frame(uint8_t* data, uint16_t size) { +void send_frame(uint8_t link, uint8_t* data, uint16_t size) { const uint8_t zero = 0; if (size > 0) { uint16_t num_non_zero = 1; @@ -113,14 +122,14 @@ void send_frame(uint8_t* data, uint16_t size) { if (num_non_zero == 0xFF) { // There's more data after big non-zero block // So send it, and start a new block - send_block(start, data, num_non_zero); + send_block(link, start, data, num_non_zero); start = data; num_non_zero = 1; } else { if (*data == 0) { // A zero encountered, so send the block - send_block(start, data, num_non_zero); + send_block(link, start, data, num_non_zero); start = data + 1; num_non_zero = 1; } @@ -130,7 +139,7 @@ void send_frame(uint8_t* data, uint16_t size) { ++data; } } - send_block(start, data, num_non_zero); - send_data(&zero, 1); + send_block(link, start, data, num_non_zero); + send_data(link, &zero, 1); } } diff --git a/serial_link/protocol/byte_stuffer.h b/serial_link/protocol/byte_stuffer.h index 4dcdd24c112..cd94b458284 100644 --- a/serial_link/protocol/byte_stuffer.h +++ b/serial_link/protocol/byte_stuffer.h @@ -23,5 +23,5 @@ SOFTWARE. */ void init_byte_stuffer(void); -void recv_byte(uint8_t data); -void send_frame(uint8_t* data, uint16_t size); +void recv_byte(uint8_t link, uint8_t data); +void send_frame(uint8_t link, uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/frame_router.h b/serial_link/protocol/frame_router.h index dd1bfb12302..3906b06c712 100644 --- a/serial_link/protocol/frame_router.h +++ b/serial_link/protocol/frame_router.h @@ -22,4 +22,4 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -void route_incoming_frame(uint8_t* data, uint16_t size); +void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index f326097ffad..80e662b2e8c 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -102,19 +102,19 @@ static uint32_t crc32_byte(uint8_t *p, uint32_t bytelength) return (crc ^ 0xffffffff); } -void validator_recv_frame(uint8_t* data, uint16_t size) { +void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size) { if (size > 4) { uint32_t frame_crc; memcpy(&frame_crc, data + size -4, 4); uint32_t expected_crc = crc32_byte(data, size - 4); if (frame_crc == expected_crc) { - route_incoming_frame(data, size-4); + route_incoming_frame(link, data, size-4); } } } -void validator_send_frame(uint8_t* data, uint16_t size) { +void validator_send_frame(uint8_t link, uint8_t* data, uint16_t size) { uint32_t crc = crc32_byte(data, size); memcpy(data + size, &crc, 4); - send_frame(data, size + 4); + send_frame(link, data, size + 4); } diff --git a/serial_link/protocol/frame_validator.h b/serial_link/protocol/frame_validator.h index 2e518894fbb..c35fc27260b 100644 --- a/serial_link/protocol/frame_validator.h +++ b/serial_link/protocol/frame_validator.h @@ -22,6 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -void validator_recv_frame(uint8_t* data, uint16_t size); +void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size); // The buffer pointed to by the data needs 4 additional bytes -void validator_send_frame(uint8_t* data, uint16_t size); +void validator_send_frame(uint8_t link, uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/physical.h b/serial_link/protocol/physical.h index 73a7855dfba..ee5883d36bf 100644 --- a/serial_link/protocol/physical.h +++ b/serial_link/protocol/physical.h @@ -22,4 +22,4 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -void send_data(const uint8_t* data, uint16_t size); +void send_data(uint8_t link, const uint8_t* data, uint16_t size); diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index f29cc129936..0f58479cb9e 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -39,34 +39,34 @@ BeforeEach(ByteStuffer) { } AfterEach(ByteStuffer) {} -void validator_recv_frame(uint8_t* data, uint16_t size) { +void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size) { mock(data, size); } -void send_data(const uint8_t* data, uint16_t size) { +void send_data(uint8_t link, const uint8_t* data, uint16_t size) { memcpy(sent_data + sent_data_size, data, size); sent_data_size += size; } Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { never_expect(validator_recv_frame); - recv_byte(0); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) { never_expect(validator_recv_frame); - recv_byte(0xFF); + recv_byte(0, 0xFF); } Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { never_expect(validator_recv_frame); - recv_byte(0x4A); + recv_byte(0, 0x4A); } Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) { never_expect(validator_recv_frame); - recv_byte(1); - recv_byte(0); + recv_byte(0, 1); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_single_byte_valid_frame) { @@ -75,9 +75,9 @@ Ensure(ByteStuffer, receives_single_byte_valid_frame) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); - recv_byte(2); - recv_byte(0x37); - recv_byte(0); + recv_byte(0, 2); + recv_byte(0, 0x37); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_three_bytes_valid_frame) { @@ -86,11 +86,11 @@ Ensure(ByteStuffer, receives_three_bytes_valid_frame) { when(size, is_equal_to(3)), when(data, is_equal_to_contents_of(expected, 3)) ); - recv_byte(4); - recv_byte(0x37); - recv_byte(0x99); - recv_byte(0xFF); - recv_byte(0); + recv_byte(0, 4); + recv_byte(0, 0x37); + recv_byte(0, 0x99); + recv_byte(0, 0xFF); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_single_zero_valid_frame) { @@ -99,9 +99,9 @@ Ensure(ByteStuffer, receives_single_zero_valid_frame) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); - recv_byte(1); - recv_byte(1); - recv_byte(0); + recv_byte(0, 1); + recv_byte(0, 1); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { @@ -110,12 +110,12 @@ Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { when(size, is_equal_to(4)), when(data, is_equal_to_contents_of(expected, 4)) ); - recv_byte(2); - recv_byte(5); - recv_byte(2); - recv_byte(3); - recv_byte(1); - recv_byte(0); + recv_byte(0, 2); + recv_byte(0, 5); + recv_byte(0, 2); + recv_byte(0, 3); + recv_byte(0, 1); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_two_valid_frames) { @@ -129,13 +129,13 @@ Ensure(ByteStuffer, receives_two_valid_frames) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected2, 1)) ); - recv_byte(2); - recv_byte(5); - recv_byte(1); - recv_byte(0); - recv_byte(2); - recv_byte(3); - recv_byte(0); + recv_byte(1, 2); + recv_byte(1, 5); + recv_byte(1, 1); + recv_byte(1, 0); + recv_byte(1, 2); + recv_byte(1, 3); + recv_byte(1, 0); } Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { @@ -144,13 +144,13 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); - recv_byte(3); - recv_byte(1); - recv_byte(0); - recv_byte(3); - recv_byte(5); - recv_byte(7); - recv_byte(0); + recv_byte(1, 3); + recv_byte(1, 1); + recv_byte(1, 0); + recv_byte(1, 3); + recv_byte(1, 5); + recv_byte(1, 7); + recv_byte(1, 0); } Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { @@ -159,14 +159,14 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); - recv_byte(2); - recv_byte(9); - recv_byte(4); // This should have been zero - recv_byte(0); - recv_byte(3); - recv_byte(5); - recv_byte(7); - recv_byte(0); + recv_byte(0, 2); + recv_byte(0, 9); + recv_byte(0, 4); // This should have been zero + recv_byte(0, 0); + recv_byte(0, 3); + recv_byte(0, 5); + recv_byte(0, 7); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_of_frame) { @@ -179,11 +179,11 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_ when(size, is_equal_to(254)), when(data, is_equal_to_contents_of(expected, 254)) ); - recv_byte(0xFF); + recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(i+1); + recv_byte(0, i+1); } - recv_byte(0); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_non_zero) { @@ -197,13 +197,13 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); - recv_byte(0xFF); + recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(i+1); + recv_byte(0, i+1); } - recv_byte(2); - recv_byte(7); - recv_byte(0); + recv_byte(0, 2); + recv_byte(0, 7); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_zero) { @@ -217,13 +217,13 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); - recv_byte(0xFF); + recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(i+1); + recv_byte(0, i+1); } - recv_byte(1); - recv_byte(1); - recv_byte(0); + recv_byte(0, 1); + recv_byte(0, 1); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { @@ -242,23 +242,23 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { when(size, is_equal_to(515)), when(data, is_equal_to_contents_of(expected, 510)) ); - recv_byte(0xFF); + recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(i+1); + recv_byte(0, i+1); } - recv_byte(0xFF); + recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(i+1); + recv_byte(0, i+1); } - recv_byte(8); - recv_byte(1); - recv_byte(2); - recv_byte(3); - recv_byte(4); - recv_byte(5); - recv_byte(6); - recv_byte(7); - recv_byte(0); + recv_byte(0, 8); + recv_byte(0, 1); + recv_byte(0, 2); + recv_byte(0, 3); + recv_byte(0, 4); + recv_byte(0, 5); + recv_byte(0, 6); + recv_byte(0, 7); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { @@ -268,23 +268,23 @@ Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE)) ); int i; - recv_byte(1); + recv_byte(0, 1); for(i=0;i #include "protocol/frame_validator.c" -void route_incoming_frame(uint8_t* data, uint16_t size) { +void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size) { mock(data, size); } -void send_frame(uint8_t* data, uint16_t size) { +void send_frame(uint8_t link, uint8_t* data, uint16_t size) { mock(data, size); } @@ -41,10 +41,10 @@ AfterEach(FrameValidator) {} Ensure(FrameValidator, doesnt_validate_frames_under_5_bytes) { never_expect(route_incoming_frame); uint8_t data[] = {1, 2}; - validator_recv_frame(0, 1); - validator_recv_frame(data, 2); - validator_recv_frame(data, 3); - validator_recv_frame(data, 4); + validator_recv_frame(0, 0, 1); + validator_recv_frame(0, data, 2); + validator_recv_frame(0, data, 3); + validator_recv_frame(0, data, 4); } Ensure(FrameValidator, validates_one_byte_frame_with_correct_crc) { @@ -53,13 +53,13 @@ Ensure(FrameValidator, validates_one_byte_frame_with_correct_crc) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(data, 1)) ); - validator_recv_frame(data, 5); + validator_recv_frame(0, data, 5); } Ensure(FrameValidator, does_not_validate_one_byte_frame_with_incorrect_crc) { uint8_t data[] = {0x44, 0, 0, 0, 0}; never_expect(route_incoming_frame); - validator_recv_frame(data, 5); + validator_recv_frame(1, data, 5); } Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) { @@ -68,7 +68,7 @@ Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) { when(size, is_equal_to(4)), when(data, is_equal_to_contents_of(data, 4)) ); - validator_recv_frame(data, 8); + validator_recv_frame(1, data, 8); } Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) { @@ -77,7 +77,7 @@ Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) { when(size, is_equal_to(5)), when(data, is_equal_to_contents_of(data, 5)) ); - validator_recv_frame(data, 9); + validator_recv_frame(0, data, 9); } Ensure(FrameValidator, sends_one_byte_with_correct_crc) { @@ -87,7 +87,7 @@ Ensure(FrameValidator, sends_one_byte_with_correct_crc) { when(size, is_equal_to(sizeof(expected))), when(data, is_equal_to_contents_of(expected, sizeof(expected))) ); - validator_send_frame(original, 1); + validator_send_frame(0, original, 1); } Ensure(FrameValidator, sends_five_bytes_with_correct_crc) { @@ -97,5 +97,5 @@ Ensure(FrameValidator, sends_five_bytes_with_correct_crc) { when(size, is_equal_to(sizeof(expected))), when(data, is_equal_to_contents_of(expected, sizeof(expected))) ); - validator_send_frame(original, 5); + validator_send_frame(0, original, 5); } From 4b9fccc75633b4ea297de373d5d7f01288d2fb11 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 13:59:48 +0200 Subject: [PATCH 25/64] Start implement router --- serial_link/protocol/byte_stuffer.c | 4 +- serial_link/protocol/byte_stuffer.h | 4 +- serial_link/protocol/frame_router.c | 33 ++++ serial_link/protocol/frame_router.h | 4 + serial_link/protocol/frame_validator.c | 2 +- serial_link/tests/byte_stuffer_tests.c | 208 +++++++++++----------- serial_link/tests/frame_router_tests.c | 89 +++++++++ serial_link/tests/frame_validator_tests.c | 6 +- 8 files changed, 238 insertions(+), 112 deletions(-) create mode 100644 serial_link/protocol/frame_router.c create mode 100644 serial_link/tests/frame_router_tests.c diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index 05b7aa6f80e..8b529667fd1 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -56,7 +56,7 @@ void init_byte_stuffer(void) { } } -void recv_byte(uint8_t link, uint8_t data) { +void byte_stuffer_recv_byte(uint8_t link, uint8_t data) { byte_stuffer_state_t* state = &states[link]; // Start of a new frame if (state->next_zero == 0) { @@ -112,7 +112,7 @@ static void send_block(uint8_t link, uint8_t* start, uint8_t* end, uint8_t num_n } } -void send_frame(uint8_t link, uint8_t* data, uint16_t size) { +void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size) { const uint8_t zero = 0; if (size > 0) { uint16_t num_non_zero = 1; diff --git a/serial_link/protocol/byte_stuffer.h b/serial_link/protocol/byte_stuffer.h index cd94b458284..839a876fe81 100644 --- a/serial_link/protocol/byte_stuffer.h +++ b/serial_link/protocol/byte_stuffer.h @@ -23,5 +23,5 @@ SOFTWARE. */ void init_byte_stuffer(void); -void recv_byte(uint8_t link, uint8_t data); -void send_frame(uint8_t link, uint8_t* data, uint16_t size); +void byte_stuffer_recv_byte(uint8_t link, uint8_t data); +void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/frame_router.c b/serial_link/protocol/frame_router.c new file mode 100644 index 00000000000..db6223a8d7b --- /dev/null +++ b/serial_link/protocol/frame_router.c @@ -0,0 +1,33 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "protocol/frame_router.h" + +void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){ + validator_send_frame(DOWN_LINK, data, size); +} + +void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { + validator_send_frame(DOWN_LINK, data, size); +} diff --git a/serial_link/protocol/frame_router.h b/serial_link/protocol/frame_router.h index 3906b06c712..a0238079e84 100644 --- a/serial_link/protocol/frame_router.h +++ b/serial_link/protocol/frame_router.h @@ -22,4 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#define UP_LINK 0 +#define DOWN_LINK 1 + void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size); +void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index 80e662b2e8c..a3face6506e 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -116,5 +116,5 @@ void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size) { void validator_send_frame(uint8_t link, uint8_t* data, uint16_t size) { uint32_t crc = crc32_byte(data, size); memcpy(data + size, &crc, 4); - send_frame(link, data, size + 4); + byte_stuffer_send_frame(link, data, size + 4); } diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 0f58479cb9e..912c4d3211c 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -50,23 +50,23 @@ void send_data(uint8_t link, const uint8_t* data, uint16_t size) { Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { never_expect(validator_recv_frame); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) { never_expect(validator_recv_frame); - recv_byte(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); } Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { never_expect(validator_recv_frame); - recv_byte(0, 0x4A); + byte_stuffer_recv_byte(0, 0x4A); } Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) { never_expect(validator_recv_frame); - recv_byte(0, 1); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_single_byte_valid_frame) { @@ -75,9 +75,9 @@ Ensure(ByteStuffer, receives_single_byte_valid_frame) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); - recv_byte(0, 2); - recv_byte(0, 0x37); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 0x37); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_three_bytes_valid_frame) { @@ -86,11 +86,11 @@ Ensure(ByteStuffer, receives_three_bytes_valid_frame) { when(size, is_equal_to(3)), when(data, is_equal_to_contents_of(expected, 3)) ); - recv_byte(0, 4); - recv_byte(0, 0x37); - recv_byte(0, 0x99); - recv_byte(0, 0xFF); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 4); + byte_stuffer_recv_byte(0, 0x37); + byte_stuffer_recv_byte(0, 0x99); + byte_stuffer_recv_byte(0, 0xFF); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_single_zero_valid_frame) { @@ -99,9 +99,9 @@ Ensure(ByteStuffer, receives_single_zero_valid_frame) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); - recv_byte(0, 1); - recv_byte(0, 1); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { @@ -110,12 +110,12 @@ Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { when(size, is_equal_to(4)), when(data, is_equal_to_contents_of(expected, 4)) ); - recv_byte(0, 2); - recv_byte(0, 5); - recv_byte(0, 2); - recv_byte(0, 3); - recv_byte(0, 1); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 5); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 3); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_two_valid_frames) { @@ -129,13 +129,13 @@ Ensure(ByteStuffer, receives_two_valid_frames) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected2, 1)) ); - recv_byte(1, 2); - recv_byte(1, 5); - recv_byte(1, 1); - recv_byte(1, 0); - recv_byte(1, 2); - recv_byte(1, 3); - recv_byte(1, 0); + byte_stuffer_recv_byte(1, 2); + byte_stuffer_recv_byte(1, 5); + byte_stuffer_recv_byte(1, 1); + byte_stuffer_recv_byte(1, 0); + byte_stuffer_recv_byte(1, 2); + byte_stuffer_recv_byte(1, 3); + byte_stuffer_recv_byte(1, 0); } Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { @@ -144,13 +144,13 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); - recv_byte(1, 3); - recv_byte(1, 1); - recv_byte(1, 0); - recv_byte(1, 3); - recv_byte(1, 5); - recv_byte(1, 7); - recv_byte(1, 0); + byte_stuffer_recv_byte(1, 3); + byte_stuffer_recv_byte(1, 1); + byte_stuffer_recv_byte(1, 0); + byte_stuffer_recv_byte(1, 3); + byte_stuffer_recv_byte(1, 5); + byte_stuffer_recv_byte(1, 7); + byte_stuffer_recv_byte(1, 0); } Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { @@ -159,14 +159,14 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); - recv_byte(0, 2); - recv_byte(0, 9); - recv_byte(0, 4); // This should have been zero - recv_byte(0, 0); - recv_byte(0, 3); - recv_byte(0, 5); - recv_byte(0, 7); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 9); + byte_stuffer_recv_byte(0, 4); // This should have been zero + byte_stuffer_recv_byte(0, 0); + byte_stuffer_recv_byte(0, 3); + byte_stuffer_recv_byte(0, 5); + byte_stuffer_recv_byte(0, 7); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_of_frame) { @@ -179,11 +179,11 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_ when(size, is_equal_to(254)), when(data, is_equal_to_contents_of(expected, 254)) ); - recv_byte(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(0, i+1); + byte_stuffer_recv_byte(0, i+1); } - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_non_zero) { @@ -197,13 +197,13 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); - recv_byte(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(0, i+1); + byte_stuffer_recv_byte(0, i+1); } - recv_byte(0, 2); - recv_byte(0, 7); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 7); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_zero) { @@ -217,13 +217,13 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); - recv_byte(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(0, i+1); + byte_stuffer_recv_byte(0, i+1); } - recv_byte(0, 1); - recv_byte(0, 1); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { @@ -242,23 +242,23 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { when(size, is_equal_to(515)), when(data, is_equal_to_contents_of(expected, 510)) ); - recv_byte(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(0, i+1); + byte_stuffer_recv_byte(0, i+1); } - recv_byte(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(0, i+1); + byte_stuffer_recv_byte(0, i+1); } - recv_byte(0, 8); - recv_byte(0, 1); - recv_byte(0, 2); - recv_byte(0, 3); - recv_byte(0, 4); - recv_byte(0, 5); - recv_byte(0, 6); - recv_byte(0, 7); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 8); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 3); + byte_stuffer_recv_byte(0, 4); + byte_stuffer_recv_byte(0, 5); + byte_stuffer_recv_byte(0, 6); + byte_stuffer_recv_byte(0, 7); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { @@ -268,23 +268,23 @@ Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE)) ); int i; - recv_byte(0, 1); + byte_stuffer_recv_byte(0, 1); for(i=0;i +#include +#include "protocol/byte_stuffer.c" +#include "protocol/frame_validator.c" +#include "protocol/frame_router.c" + +static uint8_t received_data[256]; +static uint16_t received_data_size; + +typedef struct { + uint8_t received_data[256]; + uint16_t sent_data_size; +} receive_buffer_t; + +typedef struct { + receive_buffer_t send_buffers[2]; +} router_buffer_t; + +router_buffer_t router_buffers[8]; + +router_buffer_t* current_router_buffer; + + +Describe(ByteStuffer); +BeforeEach(ByteStuffer) { + init_byte_stuffer(); + memset(router_buffers, 0, sizeof(router_buffers)); + current_router_buffer = 0; +} +AfterEach(ByteStuffer) {} + +typedef struct { + uint32_t data; + uint8_t extra[16]; +} frame_buffer_t; + + +void send_data(uint8_t link, const uint8_t* data, uint16_t size) { + receive_buffer_t* buffer = ¤t_router_buffer->send_buffers[link]; + memcpy(buffer->received_data + buffer->sent_data_size, data, size); + buffer->sent_data_size += size; +} + +static void receive_data(uint8_t link, uint8_t* data, uint16_t size) { + int i; + for(i=0;i Date: Sat, 20 Feb 2016 14:32:26 +0200 Subject: [PATCH 26/64] Add complete master broadcast test --- serial_link/protocol/frame_router.c | 8 +++++ serial_link/protocol/frame_router.h | 1 + serial_link/protocol/transport.h | 25 ++++++++++++++ serial_link/tests/frame_router_tests.c | 45 ++++++++++++++++++++++---- 4 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 serial_link/protocol/transport.h diff --git a/serial_link/protocol/frame_router.c b/serial_link/protocol/frame_router.c index db6223a8d7b..0675cea284c 100644 --- a/serial_link/protocol/frame_router.c +++ b/serial_link/protocol/frame_router.c @@ -23,8 +23,16 @@ SOFTWARE. */ #include "protocol/frame_router.h" +#include "protocol/transport.h" + +static bool is_master; + +void router_set_master(bool master) { + is_master = master; +} void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){ + transport_recv_frame(0, data, size); validator_send_frame(DOWN_LINK, data, size); } diff --git a/serial_link/protocol/frame_router.h b/serial_link/protocol/frame_router.h index a0238079e84..67db3122fa4 100644 --- a/serial_link/protocol/frame_router.h +++ b/serial_link/protocol/frame_router.h @@ -25,5 +25,6 @@ SOFTWARE. #define UP_LINK 0 #define DOWN_LINK 1 +void router_set_master(bool master); void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size); void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h new file mode 100644 index 00000000000..a40d64d4144 --- /dev/null +++ b/serial_link/protocol/transport.h @@ -0,0 +1,25 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size); diff --git a/serial_link/tests/frame_router_tests.c b/serial_link/tests/frame_router_tests.c index 5ce6c9c0f5b..eb0c710ea14 100644 --- a/serial_link/tests/frame_router_tests.c +++ b/serial_link/tests/frame_router_tests.c @@ -27,12 +27,13 @@ SOFTWARE. #include "protocol/byte_stuffer.c" #include "protocol/frame_validator.c" #include "protocol/frame_router.c" +#include "protocol/transport.h" static uint8_t received_data[256]; static uint16_t received_data_size; typedef struct { - uint8_t received_data[256]; + uint8_t sent_data[256]; uint16_t sent_data_size; } receive_buffer_t; @@ -61,7 +62,7 @@ typedef struct { void send_data(uint8_t link, const uint8_t* data, uint16_t size) { receive_buffer_t* buffer = ¤t_router_buffer->send_buffers[link]; - memcpy(buffer->received_data + buffer->sent_data_size, data, size); + memcpy(buffer->sent_data + buffer->sent_data_size, data, size); buffer->sent_data_size += size; } @@ -72,18 +73,50 @@ static void receive_data(uint8_t link, uint8_t* data, uint16_t size) { } } +static void activate_router(uint8_t num) { + current_router_buffer = router_buffers + num; + router_set_master(num==0); +} + +static void simulate_transport(uint8_t from, uint8_t to) { + activate_router(to); + if (from > to) { + } + else if(to > from) { + receive_data(UP_LINK, + router_buffers[from].send_buffers[DOWN_LINK].sent_data, + router_buffers[from].send_buffers[DOWN_LINK].sent_data_size); + } +} + +void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { + mock(from, data, size); +} + Ensure(ByteStuffer, master_broadcast_is_received_by_everyone) { frame_buffer_t data; data.data = 0xAB7055BB; - current_router_buffer = router_buffers + 0; + activate_router(0); router_send_frame(0xFF, (uint8_t*)&data, 4); assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); - current_router_buffer = router_buffers + 1; - receive_data(UP_LINK, router_buffers[0].send_buffers[DOWN_LINK].received_data, - router_buffers[0].send_buffers[DOWN_LINK].sent_data_size); + expect(transport_recv_frame, + when(from, is_equal_to(0)), + when(size, is_equal_to(4)), + when(data, is_equal_to_contents_of(&data.data, 4)) + ); + simulate_transport(0, 1); assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); + + expect(transport_recv_frame, + when(from, is_equal_to(0)), + when(size, is_equal_to(4)), + when(data, is_equal_to_contents_of(&data.data, 4)) + ); + simulate_transport(1, 2); + assert_that(router_buffers[2].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[2].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); } From 82b5037b9db628c3f34d8fb38848753ff3e41e2f Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 14:52:17 +0200 Subject: [PATCH 27/64] Add master send to specific destinations --- serial_link/protocol/frame_router.c | 8 +++++-- serial_link/tests/frame_router_tests.c | 32 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/serial_link/protocol/frame_router.c b/serial_link/protocol/frame_router.c index 0675cea284c..861c0d7d296 100644 --- a/serial_link/protocol/frame_router.c +++ b/serial_link/protocol/frame_router.c @@ -32,10 +32,14 @@ void router_set_master(bool master) { } void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){ - transport_recv_frame(0, data, size); + if (data[size-1] & 1) { + transport_recv_frame(0, data, size - 1); + } + data[size-1] >>= 1; validator_send_frame(DOWN_LINK, data, size); } void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { - validator_send_frame(DOWN_LINK, data, size); + data[size] = destination; + validator_send_frame(DOWN_LINK, data, size + 1); } diff --git a/serial_link/tests/frame_router_tests.c b/serial_link/tests/frame_router_tests.c index eb0c710ea14..7a4ccb0c408 100644 --- a/serial_link/tests/frame_router_tests.c +++ b/serial_link/tests/frame_router_tests.c @@ -120,3 +120,35 @@ Ensure(ByteStuffer, master_broadcast_is_received_by_everyone) { assert_that(router_buffers[2].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); assert_that(router_buffers[2].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); } + +Ensure(ByteStuffer, master_send_is_received_by_targets) { + printf("Here\n"); + frame_buffer_t data; + data.data = 0xAB7055BB; + activate_router(0); + router_send_frame((1 << 1) | (1 << 2), (uint8_t*)&data, 4); + assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); + + simulate_transport(0, 1); + assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); + + expect(transport_recv_frame, + when(from, is_equal_to(0)), + when(size, is_equal_to(4)), + when(data, is_equal_to_contents_of(&data.data, 4)) + ); + simulate_transport(1, 2); + assert_that(router_buffers[2].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[2].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); + + expect(transport_recv_frame, + when(from, is_equal_to(0)), + when(size, is_equal_to(4)), + when(data, is_equal_to_contents_of(&data.data, 4)) + ); + simulate_transport(2, 3); + assert_that(router_buffers[3].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[3].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); +} From c28e19c5467e92f532f64db020ad4817b531dbec Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 15:02:53 +0200 Subject: [PATCH 28/64] First link sending to master --- serial_link/protocol/frame_router.c | 22 +++++++++++++----- serial_link/tests/frame_router_tests.c | 32 +++++++++++++++++++++----- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/serial_link/protocol/frame_router.c b/serial_link/protocol/frame_router.c index 861c0d7d296..480673f0c0c 100644 --- a/serial_link/protocol/frame_router.c +++ b/serial_link/protocol/frame_router.c @@ -32,14 +32,24 @@ void router_set_master(bool master) { } void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){ - if (data[size-1] & 1) { - transport_recv_frame(0, data, size - 1); + if (is_master) { + transport_recv_frame(1, data, size); + } + else { + if (data[size-1] & 1) { + transport_recv_frame(0, data, size - 1); + } + data[size-1] >>= 1; + validator_send_frame(DOWN_LINK, data, size); } - data[size-1] >>= 1; - validator_send_frame(DOWN_LINK, data, size); } void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { - data[size] = destination; - validator_send_frame(DOWN_LINK, data, size + 1); + if (destination == 0) { + validator_send_frame(UP_LINK, data, size); + } + else { + data[size] = destination; + validator_send_frame(DOWN_LINK, data, size + 1); + } } diff --git a/serial_link/tests/frame_router_tests.c b/serial_link/tests/frame_router_tests.c index 7a4ccb0c408..02e6367d21b 100644 --- a/serial_link/tests/frame_router_tests.c +++ b/serial_link/tests/frame_router_tests.c @@ -46,13 +46,13 @@ router_buffer_t router_buffers[8]; router_buffer_t* current_router_buffer; -Describe(ByteStuffer); -BeforeEach(ByteStuffer) { +Describe(FrameRouter); +BeforeEach(FrameRouter) { init_byte_stuffer(); memset(router_buffers, 0, sizeof(router_buffers)); current_router_buffer = 0; } -AfterEach(ByteStuffer) {} +AfterEach(FrameRouter) {} typedef struct { uint32_t data; @@ -81,6 +81,9 @@ static void activate_router(uint8_t num) { static void simulate_transport(uint8_t from, uint8_t to) { activate_router(to); if (from > to) { + receive_data(DOWN_LINK, + router_buffers[from].send_buffers[UP_LINK].sent_data, + router_buffers[from].send_buffers[UP_LINK].sent_data_size); } else if(to > from) { receive_data(UP_LINK, @@ -94,7 +97,7 @@ void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { } -Ensure(ByteStuffer, master_broadcast_is_received_by_everyone) { +Ensure(FrameRouter, master_broadcast_is_received_by_everyone) { frame_buffer_t data; data.data = 0xAB7055BB; activate_router(0); @@ -121,8 +124,7 @@ Ensure(ByteStuffer, master_broadcast_is_received_by_everyone) { assert_that(router_buffers[2].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); } -Ensure(ByteStuffer, master_send_is_received_by_targets) { - printf("Here\n"); +Ensure(FrameRouter, master_send_is_received_by_targets) { frame_buffer_t data; data.data = 0xAB7055BB; activate_router(0); @@ -152,3 +154,21 @@ Ensure(ByteStuffer, master_send_is_received_by_targets) { assert_that(router_buffers[3].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0)); assert_that(router_buffers[3].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); } + +Ensure(FrameRouter, first_sends_to_master) { + frame_buffer_t data; + data.data = 0xAB7055BB; + activate_router(1); + router_send_frame(0, (uint8_t*)&data, 4); + assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); + + expect(transport_recv_frame, + when(from, is_equal_to(1)), + when(size, is_equal_to(4)), + when(data, is_equal_to_contents_of(&data.data, 4)) + ); + simulate_transport(1, 0); + assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); + assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); +} From 532f98eef6a29ae83d7ed61fbc4e25e23ad972d5 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 15:14:40 +0200 Subject: [PATCH 29/64] Support for other links sending to master --- serial_link/protocol/frame_router.c | 19 +++++++++++++------ serial_link/tests/frame_router_tests.c | 25 ++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/serial_link/protocol/frame_router.c b/serial_link/protocol/frame_router.c index 480673f0c0c..c61c50ebd68 100644 --- a/serial_link/protocol/frame_router.c +++ b/serial_link/protocol/frame_router.c @@ -33,20 +33,27 @@ void router_set_master(bool master) { void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){ if (is_master) { - transport_recv_frame(1, data, size); + transport_recv_frame(data[size-1], data, size - 1); } else { - if (data[size-1] & 1) { - transport_recv_frame(0, data, size - 1); + if (link == UP_LINK) { + if (data[size-1] & 1) { + transport_recv_frame(0, data, size - 1); + } + data[size-1] >>= 1; + validator_send_frame(DOWN_LINK, data, size); + } + else { + data[size-1]++; + validator_send_frame(UP_LINK, data, size); } - data[size-1] >>= 1; - validator_send_frame(DOWN_LINK, data, size); } } void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { if (destination == 0) { - validator_send_frame(UP_LINK, data, size); + data[size] = 1; + validator_send_frame(UP_LINK, data, size + 1); } else { data[size] = destination; diff --git a/serial_link/tests/frame_router_tests.c b/serial_link/tests/frame_router_tests.c index 02e6367d21b..6b00aca6777 100644 --- a/serial_link/tests/frame_router_tests.c +++ b/serial_link/tests/frame_router_tests.c @@ -155,7 +155,7 @@ Ensure(FrameRouter, master_send_is_received_by_targets) { assert_that(router_buffers[3].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); } -Ensure(FrameRouter, first_sends_to_master) { +Ensure(FrameRouter, first_link_sends_to_master) { frame_buffer_t data; data.data = 0xAB7055BB; activate_router(1); @@ -172,3 +172,26 @@ Ensure(FrameRouter, first_sends_to_master) { assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); } + +Ensure(FrameRouter, second_link_sends_to_master) { + printf("Second to master start\n"); + frame_buffer_t data; + data.data = 0xAB7055BB; + activate_router(2); + router_send_frame(0, (uint8_t*)&data, 4); + assert_that(router_buffers[2].send_buffers[UP_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[2].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); + + simulate_transport(2, 1); + assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); + + expect(transport_recv_frame, + when(from, is_equal_to(2)), + when(size, is_equal_to(4)), + when(data, is_equal_to_contents_of(&data.data, 4)) + ); + simulate_transport(1, 0); + assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); + assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); +} From ea346125e8ef0c14e1566070380ec777d4a1a56a Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 15:28:42 +0200 Subject: [PATCH 30/64] Add some validation for invalid router destinations --- serial_link/protocol/frame_router.c | 16 ++++++++---- serial_link/tests/frame_router_tests.c | 36 +++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/serial_link/protocol/frame_router.c b/serial_link/protocol/frame_router.c index c61c50ebd68..890ebbe9ea3 100644 --- a/serial_link/protocol/frame_router.c +++ b/serial_link/protocol/frame_router.c @@ -33,7 +33,9 @@ void router_set_master(bool master) { void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){ if (is_master) { - transport_recv_frame(data[size-1], data, size - 1); + if (link == DOWN_LINK) { + transport_recv_frame(data[size-1], data, size - 1); + } } else { if (link == UP_LINK) { @@ -52,11 +54,15 @@ void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){ void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { if (destination == 0) { - data[size] = 1; - validator_send_frame(UP_LINK, data, size + 1); + if (!is_master) { + data[size] = 1; + validator_send_frame(UP_LINK, data, size + 1); + } } else { - data[size] = destination; - validator_send_frame(DOWN_LINK, data, size + 1); + if (is_master) { + data[size] = destination; + validator_send_frame(DOWN_LINK, data, size + 1); + } } } diff --git a/serial_link/tests/frame_router_tests.c b/serial_link/tests/frame_router_tests.c index 6b00aca6777..0b0ea6e7f48 100644 --- a/serial_link/tests/frame_router_tests.c +++ b/serial_link/tests/frame_router_tests.c @@ -174,7 +174,6 @@ Ensure(FrameRouter, first_link_sends_to_master) { } Ensure(FrameRouter, second_link_sends_to_master) { - printf("Second to master start\n"); frame_buffer_t data; data.data = 0xAB7055BB; activate_router(2); @@ -195,3 +194,38 @@ Ensure(FrameRouter, second_link_sends_to_master) { assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); } + +Ensure(FrameRouter, master_sends_to_master_does_nothing) { + frame_buffer_t data; + data.data = 0xAB7055BB; + activate_router(0); + router_send_frame(0, (uint8_t*)&data, 4); + assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); + assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); +} + +Ensure(FrameRouter, link_sends_to_other_link_does_nothing) { + frame_buffer_t data; + data.data = 0xAB7055BB; + activate_router(1); + router_send_frame(2, (uint8_t*)&data, 4); + assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); + assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); +} + +Ensure(FrameRouter, master_receives_on_uplink_does_nothing) { + frame_buffer_t data; + data.data = 0xAB7055BB; + activate_router(1); + router_send_frame(0, (uint8_t*)&data, 4); + assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_greater_than(0)); + assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); + + never_expect(transport_recv_frame); + activate_router(0); + receive_data(UP_LINK, + router_buffers[1].send_buffers[UP_LINK].sent_data, + router_buffers[1].send_buffers[UP_LINK].sent_data_size); + assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0)); + assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_equal_to(0)); +} From c78eefba498ffafc3cb577a72492256849384de6 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 15:49:31 +0200 Subject: [PATCH 31/64] Add first transport tests --- serial_link/protocol/transport.c | 39 +++++++++++++++++++++++++++++ serial_link/protocol/transport.h | 2 ++ serial_link/tests/transport_tests.c | 39 +++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 serial_link/protocol/transport.c create mode 100644 serial_link/tests/transport_tests.c diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c new file mode 100644 index 00000000000..399894ff980 --- /dev/null +++ b/serial_link/protocol/transport.c @@ -0,0 +1,39 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "protocol/transport.h" + +static uint32_t current_send_frame; + +void init_transport(void) { + current_send_frame = 0; +} + +void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { + +} + +uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size) { + return ++current_send_frame; +} diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index a40d64d4144..01119857d36 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -22,4 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +void init_transport(void); void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size); +uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size); diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c new file mode 100644 index 00000000000..a64e7446d81 --- /dev/null +++ b/serial_link/tests/transport_tests.c @@ -0,0 +1,39 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include +#include "protocol/transport.c" + +Describe(Transport); +BeforeEach(Transport) { + init_transport(); +} +AfterEach(Transport) {} + +Ensure(Transport, packet_number_is_sequential) { + assert_that(transport_send_frame(0, NULL, 0), is_equal_to(1)); + assert_that(transport_send_frame(0, NULL, 0), is_equal_to(2)); + // It doesn't matter if the destination changes + assert_that(transport_send_frame(1, NULL, 0), is_equal_to(3)); +} From 50edb3d90c0076eb088f79564e38382452a21d03 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 22:22:42 +0200 Subject: [PATCH 32/64] Add first version of triple buffered object --- serial_link/protocol/triple_buffered_object.c | 33 ++++++++++++++ serial_link/protocol/triple_buffered_object.h | 37 ++++++++++++++++ .../tests/triple_buffered_object_tests.c | 44 +++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 serial_link/protocol/triple_buffered_object.c create mode 100644 serial_link/protocol/triple_buffered_object.h create mode 100644 serial_link/tests/triple_buffered_object_tests.c diff --git a/serial_link/protocol/triple_buffered_object.c b/serial_link/protocol/triple_buffered_object.c new file mode 100644 index 00000000000..f5600364f44 --- /dev/null +++ b/serial_link/protocol/triple_buffered_object.c @@ -0,0 +1,33 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "protocol/triple_buffered_object.h" + +void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src) { + memcpy(object->buffer, src, object_size); +} + +void triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { + memcpy(dst, object->buffer, object_size); +} diff --git a/serial_link/protocol/triple_buffered_object.h b/serial_link/protocol/triple_buffered_object.h new file mode 100644 index 00000000000..ebdbcae7921 --- /dev/null +++ b/serial_link/protocol/triple_buffered_object.h @@ -0,0 +1,37 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#ifndef SERIAL_LINK_TRIPLE_BUFFERED_OBJECT_H +#define SERIAL_LINK_TRIPLE_BUFFERED_OBJECT_H + +typedef struct { + uint8_t state; + uint8_t buffer[]; +}triple_buffer_object_t; + +void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src); +void triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst); + + +#endif diff --git a/serial_link/tests/triple_buffered_object_tests.c b/serial_link/tests/triple_buffered_object_tests.c new file mode 100644 index 00000000000..66dcefa7f3c --- /dev/null +++ b/serial_link/tests/triple_buffered_object_tests.c @@ -0,0 +1,44 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include +#include "protocol/triple_buffered_object.c" + +Describe(TripleBufferedObject); +BeforeEach(TripleBufferedObject) {} +AfterEach(TripleBufferedObject) {} + +typedef struct { + uint8_t state; + uint32_t buffer[3]; +}test_object_t; + +Ensure(TripleBufferedObject, writes_and_and_reads_object) { + test_object_t test_object; + uint32_t src = 0x3456ABCC; + uint32_t dst; + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); + assert_that(dst, is_equal_to(src)); +} From 4bb5733cdca62350c900ce094e1cbbaae59f2246 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 23:40:52 +0200 Subject: [PATCH 33/64] Updates to triple buffer --- serial_link/protocol/triple_buffered_object.c | 54 ++++++++++++++++++- .../tests/triple_buffered_object_tests.c | 48 ++++++++++++++--- 2 files changed, 94 insertions(+), 8 deletions(-) diff --git a/serial_link/protocol/triple_buffered_object.c b/serial_link/protocol/triple_buffered_object.c index f5600364f44..a82cef5a638 100644 --- a/serial_link/protocol/triple_buffered_object.c +++ b/serial_link/protocol/triple_buffered_object.c @@ -24,10 +24,60 @@ SOFTWARE. #include "protocol/triple_buffered_object.h" +#define GET_READ_INDEX() object->state & 3 +#define GET_WRITE1_INDEX() (object->state >> 2) & 3 +#define GET_WRITE2_INDEX() (object->state >> 4) & 3 +#define GET_FREE_INDEX() (object->state >> 6) & 3 + +#define SET_READ_INDEX(i) object->state = ((object->state & ~3) | i) +#define SET_WRITE1_INDEX(i) object->state = ((object->state & ~(3 << 2)) | (i << 2)) +#define SET_WRITE2_INDEX(i) object->state = ((object->state & ~(3 << 4)) | (i << 4)) +#define SET_FREE_INDEX(i) object->state = ((object->state & ~(3 << 6)) | (i << 6)) + +void triple_buffer_init(triple_buffer_object_t* object) { + object->state = 0; + SET_WRITE1_INDEX(0); + SET_WRITE2_INDEX(0); + SET_READ_INDEX(1); + SET_FREE_INDEX(2); +} + +static void triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_t* object) { + uint8_t newest = GET_WRITE2_INDEX(); + uint8_t free_index = GET_READ_INDEX(); + SET_READ_INDEX(newest); + SET_FREE_INDEX(free_index); +} + +static void triple_buffer_actual_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { + uint8_t read_index = GET_READ_INDEX(); + memcpy(dst, object->buffer + object_size*read_index, object_size); +} + +static void triple_buffer_end_read(uint16_t object_size, triple_buffer_object_t* object) { +} + void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src) { - memcpy(object->buffer, src, object_size); + uint8_t write1_index = GET_WRITE1_INDEX(); + uint8_t write2_index = GET_WRITE2_INDEX(); + uint8_t read_index = GET_READ_INDEX(); + uint8_t free_index = GET_FREE_INDEX(); + + if (write2_index == read_index) { + // We are reading from the other write index + SET_WRITE1_INDEX(free_index); + memcpy(object->buffer + object_size * free_index, src, object_size); + SET_WRITE2_INDEX(free_index); + } + else { + SET_WRITE1_INDEX(write2_index); + memcpy(object->buffer + object_size * write2_index, src, object_size); + SET_WRITE2_INDEX(write2_index); + } } void triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { - memcpy(dst, object->buffer, object_size); + triple_buffer_begin_read(object_size, object); + triple_buffer_actual_read(object_size, object, dst); + triple_buffer_end_read(object_size, object); } diff --git a/serial_link/tests/triple_buffered_object_tests.c b/serial_link/tests/triple_buffered_object_tests.c index 66dcefa7f3c..bf0e3ca67a3 100644 --- a/serial_link/tests/triple_buffered_object_tests.c +++ b/serial_link/tests/triple_buffered_object_tests.c @@ -25,20 +25,56 @@ SOFTWARE. #include #include "protocol/triple_buffered_object.c" -Describe(TripleBufferedObject); -BeforeEach(TripleBufferedObject) {} -AfterEach(TripleBufferedObject) {} - typedef struct { uint8_t state; uint32_t buffer[3]; }test_object_t; -Ensure(TripleBufferedObject, writes_and_and_reads_object) { - test_object_t test_object; +test_object_t test_object; + +Describe(TripleBufferedObject); +BeforeEach(TripleBufferedObject) { + triple_buffer_init((triple_buffer_object_t*)&test_object); +} +AfterEach(TripleBufferedObject) {} + + +Ensure(TripleBufferedObject, writes_and_reads_object) { uint32_t src = 0x3456ABCC; uint32_t dst; triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); assert_that(dst, is_equal_to(src)); } + +Ensure(TripleBufferedObject, writes_and_reads_object_decomposed) { + uint32_t src = 0x3456ABCC; + uint32_t dst; + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + triple_buffer_begin_read(4, (triple_buffer_object_t*)&test_object); + triple_buffer_actual_read(4, (triple_buffer_object_t*)&test_object, &dst); + triple_buffer_end_read(4, (triple_buffer_object_t*)&test_object); + assert_that(dst, is_equal_to(src)); +} + +Ensure(TripleBufferedObject, writes_twice_and_reads_object) { + uint32_t src = 0x3456ABCC; + uint32_t dst; + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + src = 0x44778899; + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); + assert_that(dst, is_equal_to(src)); +} + +Ensure(TripleBufferedObject, performs_another_write_in_the_middle_of_read) { + uint32_t src = 1; + uint32_t dst; + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + src = 2; + triple_buffer_begin_read(4, (triple_buffer_object_t*)&test_object); + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + triple_buffer_actual_read(4, (triple_buffer_object_t*)&test_object, &dst); + triple_buffer_end_read(4, (triple_buffer_object_t*)&test_object); + assert_that(dst, is_equal_to(1)); +} From 8e2d70d11fd852cf6e33b48a027a5fba61c08779 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 00:44:25 +0200 Subject: [PATCH 34/64] Simplify the triple buffer implementation --- serial_link/protocol/triple_buffered_object.c | 46 ++++++++----------- .../tests/triple_buffered_object_tests.c | 18 ++++++++ 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/serial_link/protocol/triple_buffered_object.c b/serial_link/protocol/triple_buffered_object.c index a82cef5a638..77e45ec412c 100644 --- a/serial_link/protocol/triple_buffered_object.c +++ b/serial_link/protocol/triple_buffered_object.c @@ -25,28 +25,28 @@ SOFTWARE. #include "protocol/triple_buffered_object.h" #define GET_READ_INDEX() object->state & 3 -#define GET_WRITE1_INDEX() (object->state >> 2) & 3 -#define GET_WRITE2_INDEX() (object->state >> 4) & 3 -#define GET_FREE_INDEX() (object->state >> 6) & 3 +#define GET_WRITE_INDEX() (object->state >> 2) & 3 +#define GET_SHARED_INDEX() (object->state >> 4) & 3 +#define GET_DATA_AVAILABLE() (object->state >> 6) & 1 #define SET_READ_INDEX(i) object->state = ((object->state & ~3) | i) -#define SET_WRITE1_INDEX(i) object->state = ((object->state & ~(3 << 2)) | (i << 2)) -#define SET_WRITE2_INDEX(i) object->state = ((object->state & ~(3 << 4)) | (i << 4)) -#define SET_FREE_INDEX(i) object->state = ((object->state & ~(3 << 6)) | (i << 6)) +#define SET_WRITE_INDEX(i) object->state = ((object->state & ~(3 << 2)) | (i << 2)) +#define SET_SHARED_INDEX(i) object->state = ((object->state & ~(3 << 4)) | (i << 4)) +#define SET_DATA_AVAILABLE(i) object->state = ((object->state & ~(1 << 6)) | (i << 6)) void triple_buffer_init(triple_buffer_object_t* object) { object->state = 0; - SET_WRITE1_INDEX(0); - SET_WRITE2_INDEX(0); + SET_WRITE_INDEX(0); SET_READ_INDEX(1); - SET_FREE_INDEX(2); + SET_SHARED_INDEX(2); + SET_DATA_AVAILABLE(0); } static void triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_t* object) { - uint8_t newest = GET_WRITE2_INDEX(); - uint8_t free_index = GET_READ_INDEX(); - SET_READ_INDEX(newest); - SET_FREE_INDEX(free_index); + uint8_t shared_index = GET_SHARED_INDEX(); + uint8_t read_index = GET_READ_INDEX(); + SET_READ_INDEX(shared_index); + SET_SHARED_INDEX(read_index); } static void triple_buffer_actual_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { @@ -58,22 +58,12 @@ static void triple_buffer_end_read(uint16_t object_size, triple_buffer_object_t* } void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src) { - uint8_t write1_index = GET_WRITE1_INDEX(); - uint8_t write2_index = GET_WRITE2_INDEX(); - uint8_t read_index = GET_READ_INDEX(); - uint8_t free_index = GET_FREE_INDEX(); - if (write2_index == read_index) { - // We are reading from the other write index - SET_WRITE1_INDEX(free_index); - memcpy(object->buffer + object_size * free_index, src, object_size); - SET_WRITE2_INDEX(free_index); - } - else { - SET_WRITE1_INDEX(write2_index); - memcpy(object->buffer + object_size * write2_index, src, object_size); - SET_WRITE2_INDEX(write2_index); - } + uint8_t write_index = GET_WRITE_INDEX(); + memcpy(object->buffer + object_size * write_index, src, object_size); + uint8_t shared_index = GET_SHARED_INDEX(); + SET_SHARED_INDEX(write_index); + SET_WRITE_INDEX(shared_index); } void triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { diff --git a/serial_link/tests/triple_buffered_object_tests.c b/serial_link/tests/triple_buffered_object_tests.c index bf0e3ca67a3..5fa1b8b62fc 100644 --- a/serial_link/tests/triple_buffered_object_tests.c +++ b/serial_link/tests/triple_buffered_object_tests.c @@ -77,4 +77,22 @@ Ensure(TripleBufferedObject, performs_another_write_in_the_middle_of_read) { triple_buffer_actual_read(4, (triple_buffer_object_t*)&test_object, &dst); triple_buffer_end_read(4, (triple_buffer_object_t*)&test_object); assert_that(dst, is_equal_to(1)); + triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); + assert_that(dst, is_equal_to(2)); +} + +Ensure(TripleBufferedObject, performs_two_writes_in_the_middle_of_read) { + uint32_t src = 1; + uint32_t dst; + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + triple_buffer_begin_read(4, (triple_buffer_object_t*)&test_object); + src = 2; + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + src = 3; + triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); + triple_buffer_actual_read(4, (triple_buffer_object_t*)&test_object, &dst); + triple_buffer_end_read(4, (triple_buffer_object_t*)&test_object); + assert_that(dst, is_equal_to(1)); + triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); + assert_that(dst, is_equal_to(3)); } From b7059d35f90a7cc8b07944d78984afa832190bdc Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 01:07:44 +0200 Subject: [PATCH 35/64] Working triple buffering --- serial_link/protocol/triple_buffered_object.c | 37 ++++++++++++++----- serial_link/protocol/triple_buffered_object.h | 3 +- serial_link/system/system.h | 36 ++++++++++++++++++ .../tests/triple_buffered_object_tests.c | 8 ++++ 4 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 serial_link/system/system.h diff --git a/serial_link/protocol/triple_buffered_object.c b/serial_link/protocol/triple_buffered_object.c index 77e45ec412c..3fabb7d84ba 100644 --- a/serial_link/protocol/triple_buffered_object.c +++ b/serial_link/protocol/triple_buffered_object.c @@ -23,6 +23,7 @@ SOFTWARE. */ #include "protocol/triple_buffered_object.h" +#include "system/system.h" #define GET_READ_INDEX() object->state & 3 #define GET_WRITE_INDEX() (object->state >> 2) & 3 @@ -42,11 +43,21 @@ void triple_buffer_init(triple_buffer_object_t* object) { SET_DATA_AVAILABLE(0); } -static void triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_t* object) { - uint8_t shared_index = GET_SHARED_INDEX(); - uint8_t read_index = GET_READ_INDEX(); - SET_READ_INDEX(shared_index); - SET_SHARED_INDEX(read_index); +static bool triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_t* object) { + serial_link_lock(); + if (GET_DATA_AVAILABLE()) { + uint8_t shared_index = GET_SHARED_INDEX(); + uint8_t read_index = GET_READ_INDEX(); + SET_READ_INDEX(shared_index); + SET_SHARED_INDEX(read_index); + SET_DATA_AVAILABLE(false); + serial_link_unlock(); + return true; + } + else { + serial_link_unlock(); + return false; + } } static void triple_buffer_actual_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { @@ -61,13 +72,21 @@ void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, v uint8_t write_index = GET_WRITE_INDEX(); memcpy(object->buffer + object_size * write_index, src, object_size); + serial_link_lock(); uint8_t shared_index = GET_SHARED_INDEX(); SET_SHARED_INDEX(write_index); SET_WRITE_INDEX(shared_index); + SET_DATA_AVAILABLE(true); + serial_link_unlock(); } -void triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { - triple_buffer_begin_read(object_size, object); - triple_buffer_actual_read(object_size, object, dst); - triple_buffer_end_read(object_size, object); +bool triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { + if (triple_buffer_begin_read(object_size, object)) { + triple_buffer_actual_read(object_size, object, dst); + triple_buffer_end_read(object_size, object); + return true; + } + else { + return false; + } } diff --git a/serial_link/protocol/triple_buffered_object.h b/serial_link/protocol/triple_buffered_object.h index ebdbcae7921..705f0c49f9a 100644 --- a/serial_link/protocol/triple_buffered_object.h +++ b/serial_link/protocol/triple_buffered_object.h @@ -30,8 +30,9 @@ typedef struct { uint8_t buffer[]; }triple_buffer_object_t; +void triple_buffer_init(triple_buffer_object_t* object); void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src); -void triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst); +bool triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst); #endif diff --git a/serial_link/system/system.h b/serial_link/system/system.h new file mode 100644 index 00000000000..c798e647746 --- /dev/null +++ b/serial_link/system/system.h @@ -0,0 +1,36 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#ifndef SERIAL_LINK_SYSTEM_H +#define SERIAL_LINK_SYSTEM_H + +void serial_link_lock() { + +} + +void serial_link_unlock() { + +} + +#endif diff --git a/serial_link/tests/triple_buffered_object_tests.c b/serial_link/tests/triple_buffered_object_tests.c index 5fa1b8b62fc..cd3ecb6a228 100644 --- a/serial_link/tests/triple_buffered_object_tests.c +++ b/serial_link/tests/triple_buffered_object_tests.c @@ -47,6 +47,12 @@ Ensure(TripleBufferedObject, writes_and_reads_object) { assert_that(dst, is_equal_to(src)); } +Ensure(TripleBufferedObject, does_not_read_empty) { + uint32_t dst; + bool res = triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); + assert_that(res, is_equal_to(false)); +} + Ensure(TripleBufferedObject, writes_and_reads_object_decomposed) { uint32_t src = 0x3456ABCC; uint32_t dst; @@ -79,6 +85,7 @@ Ensure(TripleBufferedObject, performs_another_write_in_the_middle_of_read) { assert_that(dst, is_equal_to(1)); triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); assert_that(dst, is_equal_to(2)); + assert_that(triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst), is_equal_to(false)); } Ensure(TripleBufferedObject, performs_two_writes_in_the_middle_of_read) { @@ -95,4 +102,5 @@ Ensure(TripleBufferedObject, performs_two_writes_in_the_middle_of_read) { assert_that(dst, is_equal_to(1)); triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); assert_that(dst, is_equal_to(3)); + assert_that(triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst), is_equal_to(false)); } From 4ee6eadf9e88b89f017c3c06e2d376cf953f6a42 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 12:53:51 +0200 Subject: [PATCH 36/64] Local and remote objects WIP --- serial_link/protocol/transport.h | 67 +++++++++++++++++++ serial_link/protocol/triple_buffered_object.h | 2 +- serial_link/tests/transport_tests.c | 26 +++++++ 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index 01119857d36..6f2cf277fb2 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -22,6 +22,73 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#ifndef SERIAL_LINK_TRANSPORT_H +#define SERIAL_LINK_TRANSPORT_H + +#include "protocol/triple_buffered_object.h" + +#define NUM_SLAVES 8 + +typedef struct { + uint16_t element_size; + uint16_t buffer_size; + uint8_t is_master; + uint8_t buffer[] __attribute__((aligned(4))); +} remote_object_t; + +typedef struct { + uint16_t element_size; + uint8_t destination; + uint8_t buffer[] __attribute__((aligned(4))); +} local_object_t; + +#define REMOTE_OBJECT_BUFFER(id, type) \ +typedef struct { \ + triple_buffer_object_t object; \ + type buffer[3]; \ +} remote_object_buffer_##id##_t; + +#define MASTER_REMOTE_OBJECT(id, type) \ +REMOTE_OBJECT_BUFFER(id, type) \ +typedef struct { \ + remote_object_t object; \ + remote_object_buffer_##id##_t buffer; \ +} master_remote_object_##id##_t; \ +master_remote_object_##id##_t remote_object_##id = { \ + .object = { \ + .element_size = sizeof(type), \ + .buffer_size = sizeof(remote_object_buffer_##id##_t), \ + .is_master = true \ + }}; + +#define SLAVE_REMOTE_OBJECT(id, type) \ +REMOTE_OBJECT_BUFFER(id, type) \ +typedef struct { \ + remote_object_t object; \ + remote_object_buffer_##id##_t buffer[NUM_SLAVES];\ +} slave_remote_object_##id##_t; \ +slave_remote_object_##id##_t remote_object_##id = { \ + .object = { \ + .element_size = sizeof(type), \ + .buffer_size = sizeof(remote_object_buffer_##id##_t), \ + .is_master = true \ + }}; + +#define LOCAL_OBJECT(id, type) \ +typedef struct { \ + uint32_t element_size; \ + uint8_t buffer[NUM_SLAVES][sizeof(type) + 16][3]; \ +} remote_object_##id##_t; \ +remote_object_##id##_t remote_object_##id = {.element_size = sizeof(type) + 16}; + +#define REMOTE_OBJECT(id) (remote_object_t*)&remote_object_##id + + void init_transport(void); void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size); uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size); + +void transport_register_master_remote_object(uint8_t id, void* ptr, uint16_t size); +void transport_register_slave_remote_object(uint8_t id, void* ptr, uint16_t size); + +#endif diff --git a/serial_link/protocol/triple_buffered_object.h b/serial_link/protocol/triple_buffered_object.h index 705f0c49f9a..d224f36a2e2 100644 --- a/serial_link/protocol/triple_buffered_object.h +++ b/serial_link/protocol/triple_buffered_object.h @@ -27,7 +27,7 @@ SOFTWARE. typedef struct { uint8_t state; - uint8_t buffer[]; + uint8_t buffer[] __attribute__((aligned(4))); }triple_buffer_object_t; void triple_buffer_init(triple_buffer_object_t* object); diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index a64e7446d81..f9f5b477370 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -25,6 +25,32 @@ SOFTWARE. #include #include "protocol/transport.c" +typedef struct { + uint32_t test; +} test_object1_t; + +typedef struct { + uint32_t test1; + uint32_t test2; +} test_object2_t; + +MASTER_REMOTE_OBJECT(0, test_object1_t); +SLAVE_REMOTE_OBJECT(1, test_object1_t); +MASTER_REMOTE_OBJECT(2, test_object2_t); +SLAVE_REMOTE_OBJECT(3, test_object2_t); + +// We want +// master -> slave = 1 local(target all), 1 remote object +// slave -> master = 1 local(target 0), multiple remote objects +// master -> single slave (multiple local, target id), 1 remote object + +remote_object_t* remote_objects[] = { + REMOTE_OBJECT(0), + REMOTE_OBJECT(1), + REMOTE_OBJECT(2), + REMOTE_OBJECT(3), +}; + Describe(Transport); BeforeEach(Transport) { init_transport(); From 679bfe7c5452f69a790e648c7661bbbdb12ce4ae Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 17:33:31 +0200 Subject: [PATCH 37/64] More transport WIP --- serial_link/protocol/transport.c | 9 +-- serial_link/protocol/transport.h | 91 +++++++++++++++-------------- serial_link/tests/transport_tests.c | 29 +++------ 3 files changed, 61 insertions(+), 68 deletions(-) diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index 399894ff980..7b2c1495041 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -24,10 +24,12 @@ SOFTWARE. #include "protocol/transport.h" -static uint32_t current_send_frame; +static remote_object_t* remote_objects; +static uint32_t num_remote_objects; -void init_transport(void) { - current_send_frame = 0; +void init_transport(remote_object_t* _remote_objects, uint32_t _num_remote_objects) { + remote_objects = _remote_objects; + num_remote_objects = _num_remote_objects; } void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { @@ -35,5 +37,4 @@ void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { } uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size) { - return ++current_send_frame; } diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index 6f2cf277fb2..86086b8b8eb 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -28,63 +28,66 @@ SOFTWARE. #include "protocol/triple_buffered_object.h" #define NUM_SLAVES 8 +#define LOCAL_OBJECT_EXTRA 16 + +// master -> slave = 1 local(target all), 1 remote object +// slave -> master = 1 local(target 0), multiple remote objects +// master -> single slave (multiple local, target id), 1 remote object +typedef enum { + MASTER_TO_ALL_SLAVES, + MASTER_TO_SINGLE_SLAVE, + SLAVE_TO_MASTER, +} remote_object_type; typedef struct { - uint16_t element_size; - uint16_t buffer_size; - uint8_t is_master; + remote_object_type object_type; + uint16_t object_size; uint8_t buffer[] __attribute__((aligned(4))); } remote_object_t; -typedef struct { - uint16_t element_size; - uint8_t destination; - uint8_t buffer[] __attribute__((aligned(4))); -} local_object_t; +#define REMOTE_OBJECT_SIZE(objectsize) \ + (sizeof(triple_buffer_object_t) + objectsize * 3) +#define LOCAL_OBJECT_SIZE(objectsize) \ + (sizeof(triple_buffer_object_t) + (objectsize + LOCAL_OBJECT_EXTRA) * 3) -#define REMOTE_OBJECT_BUFFER(id, type) \ -typedef struct { \ - triple_buffer_object_t object; \ - type buffer[3]; \ -} remote_object_buffer_##id##_t; - -#define MASTER_REMOTE_OBJECT(id, type) \ -REMOTE_OBJECT_BUFFER(id, type) \ +#define REMOTE_OBJECT_HELPER(name, type, num_local, num_remote) \ typedef struct { \ remote_object_t object; \ - remote_object_buffer_##id##_t buffer; \ -} master_remote_object_##id##_t; \ -master_remote_object_##id##_t remote_object_##id = { \ - .object = { \ - .element_size = sizeof(type), \ - .buffer_size = sizeof(remote_object_buffer_##id##_t), \ - .is_master = true \ - }}; + uint8_t buffer[ \ + num_remote * REMOTE_OBJECT_SIZE(sizeof(type)) + \ + num_local * LOCAL_OBJECT_SIZE(sizeof(type))]; \ +} remote_object_##name##_t; -#define SLAVE_REMOTE_OBJECT(id, type) \ -REMOTE_OBJECT_BUFFER(id, type) \ -typedef struct { \ - remote_object_t object; \ - remote_object_buffer_##id##_t buffer[NUM_SLAVES];\ -} slave_remote_object_##id##_t; \ -slave_remote_object_##id##_t remote_object_##id = { \ - .object = { \ - .element_size = sizeof(type), \ - .buffer_size = sizeof(remote_object_buffer_##id##_t), \ - .is_master = true \ - }}; +#define MASTER_TO_ALL_SLAVES_OBJECT(name, type) \ + REMOTE_OBJECT_HELPER(name, type, 1, 1) \ + remote_object_##name##_t remote_object_##name = { \ + .object = { \ + .object_type = MASTER_TO_ALL_SLAVES, \ + .object_size = sizeof(type), \ + } \ + }; -#define LOCAL_OBJECT(id, type) \ -typedef struct { \ - uint32_t element_size; \ - uint8_t buffer[NUM_SLAVES][sizeof(type) + 16][3]; \ -} remote_object_##id##_t; \ -remote_object_##id##_t remote_object_##id = {.element_size = sizeof(type) + 16}; +#define MASTER_TO_SINGLE_SLAVE_OBJECT(name, type) \ + REMOTE_OBJECT_HELPER(name, type, NUM_SLAVES, 1) \ + remote_object_##name##_t remote_object_##name = { \ + .object = { \ + .object_type = MASTER_TO_SINGLE_SLAVE, \ + .object_size = sizeof(type), \ + } \ + }; -#define REMOTE_OBJECT(id) (remote_object_t*)&remote_object_##id +#define SLAVE_TO_MASTER_OBJECT(name, type) \ + REMOTE_OBJECT_HELPER(name, type, 1, NUM_SLAVES) \ + remote_object_##name##_t remote_object_##name = { \ + .object = { \ + .object_type = SLAVE_TO_MASTER, \ + .object_size = sizeof(type), \ + } \ + }; +#define REMOTE_OBJECT(name) (remote_object_t*)&remote_object_##name -void init_transport(void); +void init_transport(remote_object_t* remote_objects, uint32_t num_remote_objects); void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size); uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size); diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index f9f5b477370..31e7b2dc7e3 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -34,32 +34,21 @@ typedef struct { uint32_t test2; } test_object2_t; -MASTER_REMOTE_OBJECT(0, test_object1_t); -SLAVE_REMOTE_OBJECT(1, test_object1_t); -MASTER_REMOTE_OBJECT(2, test_object2_t); -SLAVE_REMOTE_OBJECT(3, test_object2_t); +MASTER_TO_ALL_SLAVES_OBJECT(master_to_slave, test_object1_t); +MASTER_TO_SINGLE_SLAVE_OBJECT(master_to_single_slave, test_object1_t); +SLAVE_TO_MASTER_OBJECT(slave_to_master, test_object1_t); -// We want -// master -> slave = 1 local(target all), 1 remote object -// slave -> master = 1 local(target 0), multiple remote objects -// master -> single slave (multiple local, target id), 1 remote object - -remote_object_t* remote_objects[] = { - REMOTE_OBJECT(0), - REMOTE_OBJECT(1), - REMOTE_OBJECT(2), - REMOTE_OBJECT(3), +remote_object_t* test_remote_objects[] = { + REMOTE_OBJECT(master_to_slave), + REMOTE_OBJECT(master_to_single_slave), + REMOTE_OBJECT(slave_to_master), }; Describe(Transport); BeforeEach(Transport) { - init_transport(); + init_transport(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t)); } AfterEach(Transport) {} -Ensure(Transport, packet_number_is_sequential) { - assert_that(transport_send_frame(0, NULL, 0), is_equal_to(1)); - assert_that(transport_send_frame(0, NULL, 0), is_equal_to(2)); - // It doesn't matter if the destination changes - assert_that(transport_send_frame(1, NULL, 0), is_equal_to(3)); +Ensure(Transport, write_to_local_signals_an_event) { } From 2710361cd818aae6dd349fa433ebff365908de06 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 18:30:04 +0200 Subject: [PATCH 38/64] Change the triple buffer object interface --- serial_link/protocol/triple_buffered_object.c | 34 +++----- serial_link/protocol/triple_buffered_object.h | 15 +++- .../tests/triple_buffered_object_tests.c | 78 +++++++------------ 3 files changed, 49 insertions(+), 78 deletions(-) diff --git a/serial_link/protocol/triple_buffered_object.c b/serial_link/protocol/triple_buffered_object.c index 3fabb7d84ba..6b3cf75adf5 100644 --- a/serial_link/protocol/triple_buffered_object.c +++ b/serial_link/protocol/triple_buffered_object.c @@ -43,7 +43,7 @@ void triple_buffer_init(triple_buffer_object_t* object) { SET_DATA_AVAILABLE(0); } -static bool triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_t* object) { +void* triple_buffer_read_internal(uint16_t object_size, triple_buffer_object_t* object) { serial_link_lock(); if (GET_DATA_AVAILABLE()) { uint8_t shared_index = GET_SHARED_INDEX(); @@ -52,41 +52,25 @@ static bool triple_buffer_begin_read(uint16_t object_size, triple_buffer_object_ SET_SHARED_INDEX(read_index); SET_DATA_AVAILABLE(false); serial_link_unlock(); - return true; + return object->buffer + object_size * shared_index; } else { serial_link_unlock(); - return false; + return NULL; } } -static void triple_buffer_actual_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { - uint8_t read_index = GET_READ_INDEX(); - memcpy(dst, object->buffer + object_size*read_index, object_size); -} - -static void triple_buffer_end_read(uint16_t object_size, triple_buffer_object_t* object) { -} - -void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src) { - +void* triple_buffer_begin_write_internal(uint16_t object_size, triple_buffer_object_t* object) { uint8_t write_index = GET_WRITE_INDEX(); - memcpy(object->buffer + object_size * write_index, src, object_size); + return object->buffer + object_size * write_index; +} + +void triple_buffer_end_write_internal(triple_buffer_object_t* object) { serial_link_lock(); uint8_t shared_index = GET_SHARED_INDEX(); + uint8_t write_index = GET_WRITE_INDEX(); SET_SHARED_INDEX(write_index); SET_WRITE_INDEX(shared_index); SET_DATA_AVAILABLE(true); serial_link_unlock(); } - -bool triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst) { - if (triple_buffer_begin_read(object_size, object)) { - triple_buffer_actual_read(object_size, object, dst); - triple_buffer_end_read(object_size, object); - return true; - } - else { - return false; - } -} diff --git a/serial_link/protocol/triple_buffered_object.h b/serial_link/protocol/triple_buffered_object.h index d224f36a2e2..03209709cbc 100644 --- a/serial_link/protocol/triple_buffered_object.h +++ b/serial_link/protocol/triple_buffered_object.h @@ -31,8 +31,19 @@ typedef struct { }triple_buffer_object_t; void triple_buffer_init(triple_buffer_object_t* object); -void triple_buffer_write(uint16_t object_size, triple_buffer_object_t* object, void* src); -bool triple_buffer_read(uint16_t object_size, triple_buffer_object_t* object, void* dst); + +#define triple_buffer_begin_write(object) \ + (typeof(*object.buffer[0])*)triple_buffer_begin_write_internal(sizeof(*object.buffer[0]), (triple_buffer_object_t*)object) + +#define triple_buffer_end_write(object) \ + triple_buffer_end_write_internal((triple_buffer_object_t*)object) + +#define triple_buffer_read(object) \ + (typeof(*object.buffer[0])*)triple_buffer_read_internal(sizeof(*object.buffer[0]), (triple_buffer_object_t*)object) + +void* triple_buffer_begin_write_internal(uint16_t object_size, triple_buffer_object_t* object); +void triple_buffer_end_write_internal(triple_buffer_object_t* object); +void* triple_buffer_read_internal(uint16_t object_size, triple_buffer_object_t* object); #endif diff --git a/serial_link/tests/triple_buffered_object_tests.c b/serial_link/tests/triple_buffered_object_tests.c index cd3ecb6a228..1017df8f5ef 100644 --- a/serial_link/tests/triple_buffered_object_tests.c +++ b/serial_link/tests/triple_buffered_object_tests.c @@ -40,67 +40,43 @@ AfterEach(TripleBufferedObject) {} Ensure(TripleBufferedObject, writes_and_reads_object) { - uint32_t src = 0x3456ABCC; - uint32_t dst; - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); - assert_that(dst, is_equal_to(src)); + *triple_buffer_begin_write(&test_object) = 0x3456ABCC; + triple_buffer_end_write(&test_object); + assert_that(*triple_buffer_read(&test_object), is_equal_to(0x3456ABCC)); } Ensure(TripleBufferedObject, does_not_read_empty) { - uint32_t dst; - bool res = triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); - assert_that(res, is_equal_to(false)); -} - -Ensure(TripleBufferedObject, writes_and_reads_object_decomposed) { - uint32_t src = 0x3456ABCC; - uint32_t dst; - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - triple_buffer_begin_read(4, (triple_buffer_object_t*)&test_object); - triple_buffer_actual_read(4, (triple_buffer_object_t*)&test_object, &dst); - triple_buffer_end_read(4, (triple_buffer_object_t*)&test_object); - assert_that(dst, is_equal_to(src)); + assert_that(triple_buffer_read(&test_object), is_equal_to(NULL)); } Ensure(TripleBufferedObject, writes_twice_and_reads_object) { - uint32_t src = 0x3456ABCC; - uint32_t dst; - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - src = 0x44778899; - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); - assert_that(dst, is_equal_to(src)); + *triple_buffer_begin_write(&test_object) = 0x3456ABCC; + triple_buffer_end_write(&test_object); + *triple_buffer_begin_write(&test_object) = 0x44778899; + triple_buffer_end_write(&test_object); + assert_that(*triple_buffer_read(&test_object), is_equal_to(0x44778899)); } Ensure(TripleBufferedObject, performs_another_write_in_the_middle_of_read) { - uint32_t src = 1; - uint32_t dst; - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - src = 2; - triple_buffer_begin_read(4, (triple_buffer_object_t*)&test_object); - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - triple_buffer_actual_read(4, (triple_buffer_object_t*)&test_object, &dst); - triple_buffer_end_read(4, (triple_buffer_object_t*)&test_object); - assert_that(dst, is_equal_to(1)); - triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); - assert_that(dst, is_equal_to(2)); - assert_that(triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst), is_equal_to(false)); + *triple_buffer_begin_write(&test_object) = 1; + triple_buffer_end_write(&test_object); + uint32_t* read = triple_buffer_read(&test_object); + *triple_buffer_begin_write(&test_object) = 2; + triple_buffer_end_write(&test_object); + assert_that(*read, is_equal_to(1)); + assert_that(*triple_buffer_read(&test_object), is_equal_to(2)); + assert_that(triple_buffer_read(&test_object), is_equal_to(NULL)); } Ensure(TripleBufferedObject, performs_two_writes_in_the_middle_of_read) { - uint32_t src = 1; - uint32_t dst; - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - triple_buffer_begin_read(4, (triple_buffer_object_t*)&test_object); - src = 2; - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - src = 3; - triple_buffer_write(4, (triple_buffer_object_t*)&test_object, &src); - triple_buffer_actual_read(4, (triple_buffer_object_t*)&test_object, &dst); - triple_buffer_end_read(4, (triple_buffer_object_t*)&test_object); - assert_that(dst, is_equal_to(1)); - triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst); - assert_that(dst, is_equal_to(3)); - assert_that(triple_buffer_read(4, (triple_buffer_object_t*)&test_object, &dst), is_equal_to(false)); + *triple_buffer_begin_write(&test_object) = 1; + triple_buffer_end_write(&test_object); + uint32_t* read = triple_buffer_read(&test_object); + *triple_buffer_begin_write(&test_object) = 2; + triple_buffer_end_write(&test_object); + *triple_buffer_begin_write(&test_object) = 3; + triple_buffer_end_write(&test_object); + assert_that(*read, is_equal_to(1)); + assert_that(*triple_buffer_read(&test_object), is_equal_to(3)); + assert_that(triple_buffer_read(&test_object), is_equal_to(NULL)); } From fe1db50fc32c13bb1030676b1381125398bce5de Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 18:51:10 +0200 Subject: [PATCH 39/64] Writing signals events --- serial_link/protocol/transport.h | 22 +++++++++++++++++++--- serial_link/system/system.h | 4 ++-- serial_link/tests/transport_tests.c | 14 ++++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index 86086b8b8eb..c4c52947b27 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -26,6 +26,7 @@ SOFTWARE. #define SERIAL_LINK_TRANSPORT_H #include "protocol/triple_buffered_object.h" +#include "system/system.h" #define NUM_SLAVES 8 #define LOCAL_OBJECT_EXTRA 16 @@ -65,7 +66,12 @@ typedef struct { \ .object_type = MASTER_TO_ALL_SLAVES, \ .object_size = sizeof(type), \ } \ - }; + }; \ + type* begin_write_##name(void) { \ + }\ + void end_write_##name(void) { \ + signal_data_written(); \ + } #define MASTER_TO_SINGLE_SLAVE_OBJECT(name, type) \ REMOTE_OBJECT_HELPER(name, type, NUM_SLAVES, 1) \ @@ -74,7 +80,12 @@ typedef struct { \ .object_type = MASTER_TO_SINGLE_SLAVE, \ .object_size = sizeof(type), \ } \ - }; + }; \ + type* begin_write_##name(uint8_t slave) { \ + }\ + void end_write_##name(uint8_t slave) { \ + signal_data_written(); \ + } #define SLAVE_TO_MASTER_OBJECT(name, type) \ REMOTE_OBJECT_HELPER(name, type, 1, NUM_SLAVES) \ @@ -83,7 +94,12 @@ typedef struct { \ .object_type = SLAVE_TO_MASTER, \ .object_size = sizeof(type), \ } \ - }; + }; \ + type* begin_write_##name(void) { \ + }\ + void end_write_##name(void) { \ + signal_data_written(); \ + } #define REMOTE_OBJECT(name) (remote_object_t*)&remote_object_##name diff --git a/serial_link/system/system.h b/serial_link/system/system.h index c798e647746..163349953df 100644 --- a/serial_link/system/system.h +++ b/serial_link/system/system.h @@ -26,11 +26,11 @@ SOFTWARE. #define SERIAL_LINK_SYSTEM_H void serial_link_lock() { - } void serial_link_unlock() { - } +void signal_data_written(void); + #endif diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index 31e7b2dc7e3..61b9a650802 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -23,8 +23,13 @@ SOFTWARE. */ #include +#include #include "protocol/transport.c" +void signal_data_written(void) { + mock(); +} + typedef struct { uint32_t test; } test_object1_t; @@ -51,4 +56,13 @@ BeforeEach(Transport) { AfterEach(Transport) {} Ensure(Transport, write_to_local_signals_an_event) { + begin_write_master_to_slave(); + expect(signal_data_written); + end_write_master_to_slave(); + begin_write_slave_to_master(); + expect(signal_data_written); + end_write_slave_to_master(); + begin_write_master_to_single_slave(1); + expect(signal_data_written); + end_write_master_to_single_slave(1); } From 415ab91c9d2ed034bbbabf3cbb8502f525b91abb Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 21:04:42 +0200 Subject: [PATCH 40/64] Add master to slave transport --- serial_link/protocol/transport.c | 31 ++++++++++++++++++++++++++--- serial_link/protocol/transport.h | 18 +++++++++++++---- serial_link/tests/transport_tests.c | 28 +++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index 7b2c1495041..55dae9fa220 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -23,18 +23,43 @@ SOFTWARE. */ #include "protocol/transport.h" +#include "protocol/frame_router.h" +#include "protocol/triple_buffered_object.h" -static remote_object_t* remote_objects; +static remote_object_t** remote_objects; static uint32_t num_remote_objects; -void init_transport(remote_object_t* _remote_objects, uint32_t _num_remote_objects) { +void init_transport(remote_object_t** _remote_objects, uint32_t _num_remote_objects) { remote_objects = _remote_objects; num_remote_objects = _num_remote_objects; } void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { - + uint8_t id = data[size-1]; + remote_object_t* obj = remote_objects[id]; + if (obj->object_type == MASTER_TO_ALL_SLAVES) { + uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; + void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); + memcpy(ptr, data, size -1); + triple_buffer_end_write_internal(tb); + } } uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size) { } + +void update_transport(void) { + int i; + for(i=0;iobject_type == MASTER_TO_ALL_SLAVES) { + triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; + uint8_t* ptr = (uint8_t*)triple_buffer_read_internal(obj->object_size + LOCAL_OBJECT_EXTRA, tb); + if (ptr) { + ptr[obj->object_size] = i; + router_send_frame(0xFF, ptr, obj->object_size + 1); + } + } + } +} diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index c4c52947b27..fa2e00d2d33 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -68,9 +68,21 @@ typedef struct { \ } \ }; \ type* begin_write_##name(void) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \ + return (type*)triple_buffer_begin_write_internal(sizeof(type) + LOCAL_OBJECT_EXTRA, tb); \ }\ void end_write_##name(void) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \ + triple_buffer_end_write_internal(tb); \ signal_data_written(); \ + }\ + type* read_##name(void) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);\ + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \ + return triple_buffer_read_internal(obj->object_size, tb); \ } #define MASTER_TO_SINGLE_SLAVE_OBJECT(name, type) \ @@ -103,11 +115,9 @@ typedef struct { \ #define REMOTE_OBJECT(name) (remote_object_t*)&remote_object_##name -void init_transport(remote_object_t* remote_objects, uint32_t num_remote_objects); +void init_transport(remote_object_t** remote_objects, uint32_t num_remote_objects); void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size); uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size); - -void transport_register_master_remote_object(uint8_t id, void* ptr, uint16_t size); -void transport_register_slave_remote_object(uint8_t id, void* ptr, uint16_t size); +void update_transport(void); #endif diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index 61b9a650802..34d451de4d2 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -25,11 +25,21 @@ SOFTWARE. #include #include #include "protocol/transport.c" +#include "protocol/triple_buffered_object.c" void signal_data_written(void) { mock(); } +static uint8_t sent_data[2048]; +static uint16_t sent_data_size; + +void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { + mock(destination); + memcpy(sent_data + sent_data_size, data, size); + sent_data_size += size; +} + typedef struct { uint32_t test; } test_object1_t; @@ -51,7 +61,8 @@ remote_object_t* test_remote_objects[] = { Describe(Transport); BeforeEach(Transport) { - init_transport(remote_objects, sizeof(remote_objects) / sizeof(remote_object_t)); + init_transport(test_remote_objects, sizeof(test_remote_objects) / sizeof(remote_object_t*)); + sent_data_size = 0; } AfterEach(Transport) {} @@ -66,3 +77,18 @@ Ensure(Transport, write_to_local_signals_an_event) { expect(signal_data_written); end_write_master_to_single_slave(1); } + +Ensure(Transport, writes_from_master_to_all_slaves) { + update_transport(); + test_object1_t* obj = begin_write_master_to_slave(); + obj->test = 5; + expect(signal_data_written); + end_write_master_to_slave(); + expect(router_send_frame, + when(destination, is_equal_to(0xFF))); + update_transport(); + transport_recv_frame(0, sent_data, sent_data_size); + test_object1_t* obj2 = read_master_to_slave(); + assert_that(obj2, is_not_equal_to(NULL)); + assert_that(obj2->test, is_equal_to(5)); +} From 64b7efeb72ff67d088641bda798226e619601a43 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 21:15:42 +0200 Subject: [PATCH 41/64] Add proper initialization of transport buffers --- serial_link/protocol/transport.c | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index 55dae9fa220..c4019d7fed8 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -32,6 +32,40 @@ static uint32_t num_remote_objects; void init_transport(remote_object_t** _remote_objects, uint32_t _num_remote_objects) { remote_objects = _remote_objects; num_remote_objects = _num_remote_objects; + int i; + for(i=0;iobject_type == MASTER_TO_ALL_SLAVES) { + triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; + triple_buffer_init(tb); + uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + tb = (triple_buffer_object_t*)start; + triple_buffer_init(tb); + } + else if(obj->object_type == MASTER_TO_SINGLE_SLAVE) { + uint8_t* start = obj->buffer; + int j; + for (j=0;jobject_size); + } + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; + triple_buffer_init(tb); + } + else { + uint8_t* start = obj->buffer; + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; + triple_buffer_init(tb); + start += LOCAL_OBJECT_SIZE(obj->object_size); + int j; + for (j=0;jobject_size); + } + } + } } void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { From 7b88b8b7e1fd50b1fc2a219c734a3069576bcbe2 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 21:32:01 +0200 Subject: [PATCH 42/64] Add slave to master transport --- serial_link/protocol/transport.c | 20 +++++++++++++------- serial_link/protocol/transport.h | 13 +++++++++++++ serial_link/tests/transport_tests.c | 16 ++++++++++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index c4019d7fed8..e49e9d69d9b 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -71,13 +71,18 @@ void init_transport(remote_object_t** _remote_objects, uint32_t _num_remote_obje void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { uint8_t id = data[size-1]; remote_object_t* obj = remote_objects[id]; + uint8_t* start; if (obj->object_type == MASTER_TO_ALL_SLAVES) { - uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); - triple_buffer_object_t* tb = (triple_buffer_object_t*)start; - void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); - memcpy(ptr, data, size -1); - triple_buffer_end_write_internal(tb); + start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); } + else if(obj->object_type == SLAVE_TO_MASTER) { + start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size); + } + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; + void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); + memcpy(ptr, data, size -1); + triple_buffer_end_write_internal(tb); } uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size) { @@ -87,12 +92,13 @@ void update_transport(void) { int i; for(i=0;iobject_type == MASTER_TO_ALL_SLAVES) { + if (obj->object_type == MASTER_TO_ALL_SLAVES || obj->object_type == SLAVE_TO_MASTER) { triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; uint8_t* ptr = (uint8_t*)triple_buffer_read_internal(obj->object_size + LOCAL_OBJECT_EXTRA, tb); if (ptr) { ptr[obj->object_size] = i; - router_send_frame(0xFF, ptr, obj->object_size + 1); + uint8_t dest = obj->object_type == MASTER_TO_ALL_SLAVES ? 0xFF : 0; + router_send_frame(dest, ptr, obj->object_size + 1); } } } diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index fa2e00d2d33..94ff85312ab 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -108,9 +108,22 @@ typedef struct { \ } \ }; \ type* begin_write_##name(void) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \ + return (type*)triple_buffer_begin_write_internal(sizeof(type) + LOCAL_OBJECT_EXTRA, tb); \ }\ void end_write_##name(void) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; \ + triple_buffer_end_write_internal(tb); \ signal_data_written(); \ + }\ + type* read_##name(uint8_t slave) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + uint8_t* start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size);\ + start+=slave * REMOTE_OBJECT_SIZE(obj->object_size); \ + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \ + return triple_buffer_read_internal(obj->object_size, tb); \ } #define REMOTE_OBJECT(name) (remote_object_t*)&remote_object_##name diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index 34d451de4d2..334827507a0 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -92,3 +92,19 @@ Ensure(Transport, writes_from_master_to_all_slaves) { assert_that(obj2, is_not_equal_to(NULL)); assert_that(obj2->test, is_equal_to(5)); } + +Ensure(Transport, writes_from_slave_to_master) { + update_transport(); + test_object1_t* obj = begin_write_slave_to_master(); + obj->test = 7; + expect(signal_data_written); + end_write_slave_to_master(); + expect(router_send_frame, + when(destination, is_equal_to(0))); + update_transport(); + transport_recv_frame(3, sent_data, sent_data_size); + test_object1_t* obj2 = read_slave_to_master(2); + assert_that(read_slave_to_master(0), is_equal_to(NULL)); + assert_that(obj2, is_not_equal_to(NULL)); + assert_that(obj2->test, is_equal_to(7)); +} From 8cbfe79dd9ce2e8113a0f99e15d6ae4d3176b545 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 21:47:40 +0200 Subject: [PATCH 43/64] Add master to single slave transport --- serial_link/protocol/transport.c | 17 +++++++++++++++++ serial_link/protocol/transport.h | 16 ++++++++++++++++ serial_link/tests/transport_tests.c | 15 +++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index e49e9d69d9b..03f83a8068c 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -79,6 +79,9 @@ void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size); } + else { + start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size); + } triple_buffer_object_t* tb = (triple_buffer_object_t*)start; void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); memcpy(ptr, data, size -1); @@ -101,5 +104,19 @@ void update_transport(void) { router_send_frame(dest, ptr, obj->object_size + 1); } } + else { + uint8_t* start = obj->buffer; + int j; + for (j=0;jobject_size + LOCAL_OBJECT_EXTRA, tb); + if (ptr) { + ptr[obj->object_size] = i; + uint8_t dest = j + 1; + router_send_frame(dest, ptr, obj->object_size + 1); + } + start += LOCAL_OBJECT_SIZE(obj->object_size); + } + } } } diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index 94ff85312ab..a1a83b8f76c 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -94,9 +94,25 @@ typedef struct { \ } \ }; \ type* begin_write_##name(uint8_t slave) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + uint8_t* start = obj->buffer;\ + start += slave * LOCAL_OBJECT_SIZE(obj->object_size); \ + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \ + return (type*)triple_buffer_begin_write_internal(sizeof(type) + LOCAL_OBJECT_EXTRA, tb); \ }\ void end_write_##name(uint8_t slave) { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + uint8_t* start = obj->buffer;\ + start += slave * LOCAL_OBJECT_SIZE(obj->object_size); \ + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \ + triple_buffer_end_write_internal(tb); \ signal_data_written(); \ + }\ + type* read_##name() { \ + remote_object_t* obj = (remote_object_t*)&remote_object_##name; \ + uint8_t* start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size);\ + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; \ + return triple_buffer_read_internal(obj->object_size, tb); \ } #define SLAVE_TO_MASTER_OBJECT(name, type) \ diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index 334827507a0..3fa8eab4ac8 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -108,3 +108,18 @@ Ensure(Transport, writes_from_slave_to_master) { assert_that(obj2, is_not_equal_to(NULL)); assert_that(obj2->test, is_equal_to(7)); } + +Ensure(Transport, writes_from_master_to_single_slave) { + update_transport(); + test_object1_t* obj = begin_write_master_to_single_slave(3); + obj->test = 7; + expect(signal_data_written); + end_write_master_to_single_slave(3); + expect(router_send_frame, + when(destination, is_equal_to(4))); + update_transport(); + transport_recv_frame(0, sent_data, sent_data_size); + test_object1_t* obj2 = read_master_to_single_slave(); + assert_that(obj2, is_not_equal_to(NULL)); + assert_that(obj2->test, is_equal_to(7)); +} From 6873b17117a41715c9a8ca63060974b64b4cdcf4 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 23:17:59 +0200 Subject: [PATCH 44/64] Support for Chibios compilation Remove some warnings, change the include paths. --- serial_link.mk | 3 ++- serial_link/protocol/byte_stuffer.c | 8 ++++---- serial_link/protocol/byte_stuffer.h | 7 +++++++ serial_link/protocol/frame_router.c | 5 +++-- serial_link/protocol/frame_router.h | 8 ++++++++ serial_link/protocol/frame_validator.c | 7 ++++--- serial_link/protocol/frame_validator.h | 7 +++++++ serial_link/protocol/physical.h | 5 +++++ serial_link/protocol/transport.c | 20 +++++++++---------- serial_link/protocol/transport.h | 5 ++--- serial_link/protocol/triple_buffered_object.c | 6 ++++-- serial_link/protocol/triple_buffered_object.h | 2 ++ serial_link/system/system.h | 12 ++++++++--- serial_link/tests/Makefile | 2 +- serial_link/tests/byte_stuffer_tests.c | 8 ++++---- serial_link/tests/frame_router_tests.c | 8 ++++---- serial_link/tests/frame_validator_tests.c | 2 +- serial_link/tests/transport_tests.c | 4 ++-- .../tests/triple_buffered_object_tests.c | 2 +- 19 files changed, 79 insertions(+), 42 deletions(-) diff --git a/serial_link.mk b/serial_link.mk index de2364108a3..e8915a33f96 100644 --- a/serial_link.mk +++ b/serial_link.mk @@ -20,4 +20,5 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -INC += $(SERIAL_DIR) \ No newline at end of file +INC += $(SERIAL_DIR) +SRC += $(wildcard $(SERIAL_DIR)/serial_link/protocol/*.c) \ No newline at end of file diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index 8b529667fd1..fb4c45a8dc0 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -22,10 +22,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "protocol/byte_stuffer.h" -#include "protocol/frame_validator.h" -#include "protocol/physical.h" -#include +#include "serial_link/protocol/byte_stuffer.h" +#include "serial_link/protocol/frame_validator.h" +#include "serial_link/protocol/physical.h" +#include // This implements the "Consistent overhead byte stuffing protocol" // https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing diff --git a/serial_link/protocol/byte_stuffer.h b/serial_link/protocol/byte_stuffer.h index 839a876fe81..2cc88beb42a 100644 --- a/serial_link/protocol/byte_stuffer.h +++ b/serial_link/protocol/byte_stuffer.h @@ -22,6 +22,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#ifndef SERIAL_LINK_BYTE_STUFFER_H +#define SERIAL_LINK_BYTE_STUFFER_H + +#include + void init_byte_stuffer(void); void byte_stuffer_recv_byte(uint8_t link, uint8_t data); void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size); + +#endif diff --git a/serial_link/protocol/frame_router.c b/serial_link/protocol/frame_router.c index 890ebbe9ea3..04b8c2e75ca 100644 --- a/serial_link/protocol/frame_router.c +++ b/serial_link/protocol/frame_router.c @@ -22,8 +22,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "protocol/frame_router.h" -#include "protocol/transport.h" +#include "serial_link/protocol/frame_router.h" +#include "serial_link/protocol/transport.h" +#include "serial_link/protocol/frame_validator.h" static bool is_master; diff --git a/serial_link/protocol/frame_router.h b/serial_link/protocol/frame_router.h index 67db3122fa4..712250ff359 100644 --- a/serial_link/protocol/frame_router.h +++ b/serial_link/protocol/frame_router.h @@ -22,9 +22,17 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#ifndef SERIAL_LINK_FRAME_ROUTER_H +#define SERIAL_LINK_FRAME_ROUTER_H + +#include +#include + #define UP_LINK 0 #define DOWN_LINK 1 void router_set_master(bool master); void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size); void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size); + +#endif diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index a3face6506e..474f80ee8ec 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -22,9 +22,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "protocol/frame_validator.h" -#include "protocol/frame_router.h" -#include "protocol/byte_stuffer.h" +#include "serial_link/protocol/frame_validator.h" +#include "serial_link/protocol/frame_router.h" +#include "serial_link/protocol/byte_stuffer.h" +#include const uint32_t poly8_lookup[256] = { diff --git a/serial_link/protocol/frame_validator.h b/serial_link/protocol/frame_validator.h index c35fc27260b..4a910d510b9 100644 --- a/serial_link/protocol/frame_validator.h +++ b/serial_link/protocol/frame_validator.h @@ -22,6 +22,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#ifndef SERIAL_LINK_FRAME_VALIDATOR_H +#define SERIAL_LINK_FRAME_VALIDATOR_H + +#include + void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size); // The buffer pointed to by the data needs 4 additional bytes void validator_send_frame(uint8_t link, uint8_t* data, uint16_t size); + +#endif diff --git a/serial_link/protocol/physical.h b/serial_link/protocol/physical.h index ee5883d36bf..425e06cdd29 100644 --- a/serial_link/protocol/physical.h +++ b/serial_link/protocol/physical.h @@ -22,4 +22,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#ifndef SERIAL_LINK_PHYSICAL_H +#define SERIAL_LINK_PHYSICAL_H + void send_data(uint8_t link, const uint8_t* data, uint16_t size); + +#endif diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index 03f83a8068c..4542a7a050e 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -22,9 +22,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "protocol/transport.h" -#include "protocol/frame_router.h" -#include "protocol/triple_buffered_object.h" +#include "serial_link/protocol/transport.h" +#include "serial_link/protocol/frame_router.h" +#include "serial_link/protocol/triple_buffered_object.h" +#include static remote_object_t** remote_objects; static uint32_t num_remote_objects; @@ -32,7 +33,7 @@ static uint32_t num_remote_objects; void init_transport(remote_object_t** _remote_objects, uint32_t _num_remote_objects) { remote_objects = _remote_objects; num_remote_objects = _num_remote_objects; - int i; + unsigned int i; for(i=0;iobject_type == MASTER_TO_ALL_SLAVES) { @@ -44,7 +45,7 @@ void init_transport(remote_object_t** _remote_objects, uint32_t _num_remote_obje } else if(obj->object_type == MASTER_TO_SINGLE_SLAVE) { uint8_t* start = obj->buffer; - int j; + unsigned int j; for (j=0;jobject_size); - int j; + unsigned int j; for (j=0;jobject_type == MASTER_TO_ALL_SLAVES || obj->object_type == SLAVE_TO_MASTER) { @@ -106,7 +104,7 @@ void update_transport(void) { } else { uint8_t* start = obj->buffer; - int j; + unsigned int j; for (j=0;jobject_size + LOCAL_OBJECT_EXTRA, tb); diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index a1a83b8f76c..9e9e22462c7 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -25,8 +25,8 @@ SOFTWARE. #ifndef SERIAL_LINK_TRANSPORT_H #define SERIAL_LINK_TRANSPORT_H -#include "protocol/triple_buffered_object.h" -#include "system/system.h" +#include "serial_link/protocol/triple_buffered_object.h" +#include "serial_link/system/system.h" #define NUM_SLAVES 8 #define LOCAL_OBJECT_EXTRA 16 @@ -146,7 +146,6 @@ typedef struct { \ void init_transport(remote_object_t** remote_objects, uint32_t num_remote_objects); void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size); -uint32_t transport_send_frame(uint8_t to, uint8_t* data, uint16_t size); void update_transport(void); #endif diff --git a/serial_link/protocol/triple_buffered_object.c b/serial_link/protocol/triple_buffered_object.c index 6b3cf75adf5..c6bf28af0aa 100644 --- a/serial_link/protocol/triple_buffered_object.c +++ b/serial_link/protocol/triple_buffered_object.c @@ -22,8 +22,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "protocol/triple_buffered_object.h" -#include "system/system.h" +#include "serial_link/protocol/triple_buffered_object.h" +#include "serial_link/system/system.h" +#include +#include #define GET_READ_INDEX() object->state & 3 #define GET_WRITE_INDEX() (object->state >> 2) & 3 diff --git a/serial_link/protocol/triple_buffered_object.h b/serial_link/protocol/triple_buffered_object.h index 03209709cbc..2e57db3f50f 100644 --- a/serial_link/protocol/triple_buffered_object.h +++ b/serial_link/protocol/triple_buffered_object.h @@ -25,6 +25,8 @@ SOFTWARE. #ifndef SERIAL_LINK_TRIPLE_BUFFERED_OBJECT_H #define SERIAL_LINK_TRIPLE_BUFFERED_OBJECT_H +#include + typedef struct { uint8_t state; uint8_t buffer[] __attribute__((aligned(4))); diff --git a/serial_link/system/system.h b/serial_link/system/system.h index 163349953df..1e4c610b1d6 100644 --- a/serial_link/system/system.h +++ b/serial_link/system/system.h @@ -25,12 +25,18 @@ SOFTWARE. #ifndef SERIAL_LINK_SYSTEM_H #define SERIAL_LINK_SYSTEM_H -void serial_link_lock() { +inline void serial_link_lock(void) { } -void serial_link_unlock() { +inline void serial_link_unlock(void) { } -void signal_data_written(void); +void singal_data_written(void); + +#if defined(PROTOCOL_CHIBIOS) + +inline void signal_data_written(void) { +} +#endif #endif diff --git a/serial_link/tests/Makefile b/serial_link/tests/Makefile index 0d8ba4b7b65..1b072c6f1d2 100644 --- a/serial_link/tests/Makefile +++ b/serial_link/tests/Makefile @@ -22,7 +22,7 @@ CC = gcc CFLAGS = -INCLUDES = -I. -I../ +INCLUDES = -I. -I../../ LDFLAGS = -L$(BUILDDIR)/cgreen/build-c/src -shared LDLIBS = -lcgreen UNITOBJ = $(BUILDDIR)/serialtest/unitobj diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 912c4d3211c..64b170e8c1d 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -24,10 +24,10 @@ SOFTWARE. #include #include -#include "protocol/byte_stuffer.h" -#include "protocol/byte_stuffer.c" -#include "protocol/frame_validator.h" -#include "protocol/physical.h" +#include "serial_link/protocol/byte_stuffer.h" +#include "serial_link/protocol/byte_stuffer.c" +#include "serial_link/protocol/frame_validator.h" +#include "serial_link/protocol/physical.h" static uint8_t sent_data[MAX_FRAME_SIZE*2]; static uint16_t sent_data_size; diff --git a/serial_link/tests/frame_router_tests.c b/serial_link/tests/frame_router_tests.c index 0b0ea6e7f48..6c806fa939f 100644 --- a/serial_link/tests/frame_router_tests.c +++ b/serial_link/tests/frame_router_tests.c @@ -24,10 +24,10 @@ SOFTWARE. #include #include -#include "protocol/byte_stuffer.c" -#include "protocol/frame_validator.c" -#include "protocol/frame_router.c" -#include "protocol/transport.h" +#include "serial_link/protocol/byte_stuffer.c" +#include "serial_link/protocol/frame_validator.c" +#include "serial_link/protocol/frame_router.c" +#include "serial_link/protocol/transport.h" static uint8_t received_data[256]; static uint16_t received_data_size; diff --git a/serial_link/tests/frame_validator_tests.c b/serial_link/tests/frame_validator_tests.c index f4abd14d1be..d20947e2c9c 100644 --- a/serial_link/tests/frame_validator_tests.c +++ b/serial_link/tests/frame_validator_tests.c @@ -24,7 +24,7 @@ SOFTWARE. #include #include -#include "protocol/frame_validator.c" +#include "serial_link/protocol/frame_validator.c" void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size) { mock(data, size); diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index 3fa8eab4ac8..3e9bffdfa71 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -24,8 +24,8 @@ SOFTWARE. #include #include -#include "protocol/transport.c" -#include "protocol/triple_buffered_object.c" +#include "serial_link/protocol/transport.c" +#include "serial_link/protocol/triple_buffered_object.c" void signal_data_written(void) { mock(); diff --git a/serial_link/tests/triple_buffered_object_tests.c b/serial_link/tests/triple_buffered_object_tests.c index 1017df8f5ef..6f7c82b468f 100644 --- a/serial_link/tests/triple_buffered_object_tests.c +++ b/serial_link/tests/triple_buffered_object_tests.c @@ -23,7 +23,7 @@ SOFTWARE. */ #include -#include "protocol/triple_buffered_object.c" +#include "serial_link/protocol/triple_buffered_object.c" typedef struct { uint8_t state; From 36e4869531b19b19eb4301af2016875b9f4f6e82 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 21 Feb 2016 23:52:00 +0200 Subject: [PATCH 45/64] Create a thread for the serial link --- serial_link.mk | 3 ++- serial_link/system/system.c | 40 +++++++++++++++++++++++++++++++++++++ serial_link/system/system.h | 24 +++++++++++++++++----- 3 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 serial_link/system/system.c diff --git a/serial_link.mk b/serial_link.mk index e8915a33f96..434966a22ae 100644 --- a/serial_link.mk +++ b/serial_link.mk @@ -21,4 +21,5 @@ # SOFTWARE. INC += $(SERIAL_DIR) -SRC += $(wildcard $(SERIAL_DIR)/serial_link/protocol/*.c) \ No newline at end of file +SRC += $(wildcard $(SERIAL_DIR)/serial_link/protocol/*.c) +SRC += $(wildcard $(SERIAL_DIR)/serial_link/system/*.c) \ No newline at end of file diff --git a/serial_link/system/system.c b/serial_link/system/system.c new file mode 100644 index 00000000000..2001f4d5cc9 --- /dev/null +++ b/serial_link/system/system.c @@ -0,0 +1,40 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +#include "serial_link/system/system.h" + +// TODO: Optimize the stack size, this is probably way too big +static THD_WORKING_AREA(serialThreadStack, 1024); +static THD_FUNCTION(serialThread, arg) { + (void)arg; +} + + +void init_serial_link(void) { + (void)chThdCreateStatic(serialThreadStack, sizeof(serialThreadStack), + LOWPRIO, serialThread, NULL); +} + +void signal_data_written(void) { + +} diff --git a/serial_link/system/system.h b/serial_link/system/system.h index 1e4c610b1d6..e8c1caec089 100644 --- a/serial_link/system/system.h +++ b/serial_link/system/system.h @@ -25,18 +25,32 @@ SOFTWARE. #ifndef SERIAL_LINK_SYSTEM_H #define SERIAL_LINK_SYSTEM_H + +void init_serial_link(void); + +#if defined(PROTOCOL_CHIBIOS) +#include "ch.h" + +static inline void serial_link_lock(void) { + chSysLock(); +} + +static inline void serial_link_unlock(void) { + chSysUnlock(); +} + +void signal_data_written(void); + +#else + inline void serial_link_lock(void) { } inline void serial_link_unlock(void) { } -void singal_data_written(void); +void signal_data_written(void); -#if defined(PROTOCOL_CHIBIOS) - -inline void signal_data_written(void) { -} #endif #endif From 17a1102a2a73e619d1a2573764fdfbb0dec9c9a9 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 24 Feb 2016 22:31:01 +0200 Subject: [PATCH 46/64] Fix include dir to use uincir instead of just inc --- serial_link.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serial_link.mk b/serial_link.mk index 434966a22ae..f0cd60b0460 100644 --- a/serial_link.mk +++ b/serial_link.mk @@ -20,6 +20,6 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -INC += $(SERIAL_DIR) +UINCDIR += $(SERIAL_DIR) SRC += $(wildcard $(SERIAL_DIR)/serial_link/protocol/*.c) SRC += $(wildcard $(SERIAL_DIR)/serial_link/system/*.c) \ No newline at end of file From 9cb139adc4e08ff393b1cb8bf16203d670996e71 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 27 Feb 2016 22:28:35 +0200 Subject: [PATCH 47/64] Add simple loop for updating the serial link --- serial_link/system/system.c | 66 ++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index 2001f4d5cc9..2dbd9a4c151 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -22,19 +22,83 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "serial_link/system/system.h" +#include "hal.h" +#include "serial_link/protocol/byte_stuffer.h" +#include "serial_link/protocol/transport.h" +#include "serial_link/protocol/frame_router.h" +#include + +static event_source_t new_data_event; + + +// Slow speed for testing +static SerialConfig config = { + .sc_speed = 38400 +}; + +static uint32_t read_from_serial(SerialDriver* driver) { + const uint32_t buffer_size = 16; + uint8_t buffer[buffer_size]; + uint32_t bytes_read = sdAsynchronousRead(driver, buffer, buffer_size); + uint8_t* current = buffer; + uint8_t* end = current + bytes_read; + while(current < end) { + byte_stuffer_recv_byte(0, *current); + current++; + } + return bytes_read; +} // TODO: Optimize the stack size, this is probably way too big static THD_WORKING_AREA(serialThreadStack, 1024); static THD_FUNCTION(serialThread, arg) { (void)arg; + event_listener_t new_data_listener; + event_listener_t sd1_listener; + event_listener_t sd2_listener; + chEvtRegister(&new_data_event, &new_data_listener, 0); + chEvtRegisterMaskWithFlags(chnGetEventSource(&SD1), + &sd1_listener, + EVENT_MASK(1), + CHN_INPUT_AVAILABLE); + chEvtRegisterMaskWithFlags(chnGetEventSource(&SD2), + &sd2_listener, + EVENT_MASK(2), + CHN_INPUT_AVAILABLE); + bool need_wait = false; + while(true) { + if (need_wait) { + chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(1000)); + } + bool is_master = usbGetDriverStateI(&USBD1) == USB_ACTIVE; + router_set_master(is_master); + + need_wait = true; + need_wait &= read_from_serial(&SD1) == 0; + need_wait &= read_from_serial(&SD2) == 0; + update_transport(); + } +} + +void send_data(uint8_t link, const uint8_t* data, uint16_t size) { + if (link == 0) { + sdWrite(&SD1, data, size); + } + else { + sdWrite(&SD2, data, size); + } } void init_serial_link(void) { + init_byte_stuffer(); + sdStart(&SD1, &config); + sdStart(&SD2, &config); + chEvtObjectInit(&new_data_event); (void)chThdCreateStatic(serialThreadStack, sizeof(serialThreadStack), LOWPRIO, serialThread, NULL); } void signal_data_written(void) { - + chEvtBroadcast(&new_data_event); } From 46482460fa5ba7e1606656f651117cc30f643952 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 28 Feb 2016 15:52:03 +0200 Subject: [PATCH 48/64] Some small fixes for the serial update loop --- serial_link/system/system.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index 2dbd9a4c151..e40a18cec70 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -36,14 +36,14 @@ static SerialConfig config = { .sc_speed = 38400 }; -static uint32_t read_from_serial(SerialDriver* driver) { +static uint32_t read_from_serial(SerialDriver* driver, uint8_t link) { const uint32_t buffer_size = 16; uint8_t buffer[buffer_size]; uint32_t bytes_read = sdAsynchronousRead(driver, buffer, buffer_size); uint8_t* current = buffer; uint8_t* end = current + bytes_read; while(current < end) { - byte_stuffer_recv_byte(0, *current); + byte_stuffer_recv_byte(link, *current); current++; } return bytes_read; @@ -74,14 +74,14 @@ static THD_FUNCTION(serialThread, arg) { router_set_master(is_master); need_wait = true; - need_wait &= read_from_serial(&SD1) == 0; - need_wait &= read_from_serial(&SD2) == 0; + need_wait &= read_from_serial(&SD2, UP_LINK) == 0; + need_wait &= read_from_serial(&SD1, DOWN_LINK) == 0; update_transport(); } } void send_data(uint8_t link, const uint8_t* data, uint16_t size) { - if (link == 0) { + if (link == DOWN_LINK) { sdWrite(&SD1, data, size); } else { From d8d4622802c1a50779830eade0f79a0680def34a Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 28 Feb 2016 21:46:29 +0200 Subject: [PATCH 49/64] Improve serial link initialization, and add driver --- serial_link.mk | 4 +- serial_link/system/driver.h | 36 +++++++++++++++ serial_link/system/system.c | 90 +++++++++++++++++++++++++++++++++++++ serial_link/system/system.h | 1 - 4 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 serial_link/system/driver.h diff --git a/serial_link.mk b/serial_link.mk index f0cd60b0460..e164cc5ff36 100644 --- a/serial_link.mk +++ b/serial_link.mk @@ -22,4 +22,6 @@ UINCDIR += $(SERIAL_DIR) SRC += $(wildcard $(SERIAL_DIR)/serial_link/protocol/*.c) -SRC += $(wildcard $(SERIAL_DIR)/serial_link/system/*.c) \ No newline at end of file +SRC += $(wildcard $(SERIAL_DIR)/serial_link/system/*.c) +SRC += serial_link_hal.c +OPT_DEFS += -DUSE_SERIAL_LINK diff --git a/serial_link/system/driver.h b/serial_link/system/driver.h new file mode 100644 index 00000000000..76e2d682c80 --- /dev/null +++ b/serial_link/system/driver.h @@ -0,0 +1,36 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#ifndef SERIAL_LINK_DRIVER_H +#define SERIAL_LINK_DRIVER_H + +#include "host_driver.h" + +void init_serial_link(void); +void init_serial_link_hal(void); +bool is_serial_link_connected(void); +host_driver_t* get_serial_link_driver(void); +void serial_link_update(void); + +#endif diff --git a/serial_link/system/system.c b/serial_link/system/system.c index e40a18cec70..c38bcd87d0d 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -21,14 +21,33 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include "report.h" +#include "host_driver.h" #include "serial_link/system/system.h" +#include "serial_link/system/driver.h" #include "hal.h" #include "serial_link/protocol/byte_stuffer.h" #include "serial_link/protocol/transport.h" #include "serial_link/protocol/frame_router.h" #include +#include "print.h" static event_source_t new_data_event; +static bool serial_link_connected; + +static uint8_t keyboard_leds(void); +static void send_keyboard(report_keyboard_t *report); +static void send_mouse(report_mouse_t *report); +static void send_system(uint16_t data); +static void send_consumer(uint16_t data); + +host_driver_t serial_driver = { + keyboard_leds, + send_keyboard, + send_mouse, + send_system, + send_consumer +}; // Slow speed for testing @@ -89,8 +108,25 @@ void send_data(uint8_t link, const uint8_t* data, uint16_t size) { } } +static systime_t last_update = 0; + +typedef struct { + uint32_t test; +} test_object1_t; + + +SLAVE_TO_MASTER_OBJECT(slave_to_master, test_object1_t); +MASTER_TO_ALL_SLAVES_OBJECT(serial_link_connected, bool); + +remote_object_t* test_remote_objects[] = { + REMOTE_OBJECT(serial_link_connected), + REMOTE_OBJECT(slave_to_master), +}; void init_serial_link(void) { + serial_link_connected = false; + init_serial_link_hal(); + init_transport(test_remote_objects, sizeof(test_remote_objects)/sizeof(remote_object_t*)); init_byte_stuffer(); sdStart(&SD1, &config); sdStart(&SD2, &config); @@ -99,6 +135,60 @@ void init_serial_link(void) { LOWPRIO, serialThread, NULL); } +void serial_link_update(void) { + systime_t current_time = chVTGetSystemTimeX(); + if (current_time - last_update > 1000) { + *begin_write_serial_link_connected() = true; + end_write_serial_link_connected(); + test_object1_t* obj = begin_write_slave_to_master(); + obj->test = current_time; + end_write_slave_to_master(); + xprintf("writing %d\n", current_time); + last_update = current_time; + } + test_object1_t* obj = read_slave_to_master(0); + if (obj) { + xprintf("%d\n", obj->test); + } + obj = read_slave_to_master(1); + if (obj) { + xprintf("%d\n", obj->test); + } + + if (read_serial_link_connected()) { + serial_link_connected = true; + } +} + void signal_data_written(void) { chEvtBroadcast(&new_data_event); } + +bool is_serial_link_connected(void) { + return serial_link_connected; +} + +host_driver_t* get_serial_link_driver(void) { + return &serial_driver; +} + +uint8_t keyboard_leds(void) { + return 0; +} + +void send_keyboard(report_keyboard_t *report) { + (void)report; +} + +void send_mouse(report_mouse_t *report) { + (void)report; +} + +void send_system(uint16_t data) { + (void)data; +} + +void send_consumer(uint16_t data) { + (void)data; +} + diff --git a/serial_link/system/system.h b/serial_link/system/system.h index e8c1caec089..fcc27425e18 100644 --- a/serial_link/system/system.h +++ b/serial_link/system/system.h @@ -26,7 +26,6 @@ SOFTWARE. #define SERIAL_LINK_SYSTEM_H -void init_serial_link(void); #if defined(PROTOCOL_CHIBIOS) #include "ch.h" From 6fe6d111bf563962e0d3cc2e4812b6d7959836a3 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 28 Feb 2016 22:52:30 +0200 Subject: [PATCH 50/64] Send the keyboard matrix over the serial link --- serial_link/system/system.c | 53 ++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index c38bcd87d0d..68ccbdb45e9 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -29,6 +29,7 @@ SOFTWARE. #include "serial_link/protocol/byte_stuffer.h" #include "serial_link/protocol/transport.h" #include "serial_link/protocol/frame_router.h" +#include "matrix.h" #include #include "print.h" @@ -111,16 +112,17 @@ void send_data(uint8_t link, const uint8_t* data, uint16_t size) { static systime_t last_update = 0; typedef struct { - uint32_t test; -} test_object1_t; + matrix_row_t rows[MATRIX_ROWS]; +} matrix_object_t; +static matrix_object_t last_matrix = {}; -SLAVE_TO_MASTER_OBJECT(slave_to_master, test_object1_t); +SLAVE_TO_MASTER_OBJECT(keyboard_matrix, matrix_object_t); MASTER_TO_ALL_SLAVES_OBJECT(serial_link_connected, bool); remote_object_t* test_remote_objects[] = { REMOTE_OBJECT(serial_link_connected), - REMOTE_OBJECT(slave_to_master), + REMOTE_OBJECT(keyboard_matrix), }; void init_serial_link(void) { @@ -140,24 +142,42 @@ void serial_link_update(void) { if (current_time - last_update > 1000) { *begin_write_serial_link_connected() = true; end_write_serial_link_connected(); - test_object1_t* obj = begin_write_slave_to_master(); - obj->test = current_time; - end_write_slave_to_master(); - xprintf("writing %d\n", current_time); last_update = current_time; } - test_object1_t* obj = read_slave_to_master(0); - if (obj) { - xprintf("%d\n", obj->test); - } - obj = read_slave_to_master(1); - if (obj) { - xprintf("%d\n", obj->test); - } if (read_serial_link_connected()) { serial_link_connected = true; } + + matrix_object_t matrix; + bool changed = false; + for(uint8_t i=0;irows[i] = matrix.rows[i]; + } + end_write_keyboard_matrix(); + } + + matrix_object_t* m = read_keyboard_matrix(0); + if (m) { + xprintf("\nr/c 01234567\n"); + for (uint8_t row = 0; row < MATRIX_ROWS; row++) { + xprintf("%X0: ", row); + for (int col = 0; col < MATRIX_COLS; col++) { + if (m->rows[row] & (1< Date: Sun, 28 Feb 2016 23:58:47 +0200 Subject: [PATCH 51/64] Call remote matrix update, when the matrix changes --- serial_link/system/system.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index 68ccbdb45e9..9cf695a308b 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -137,6 +137,8 @@ void init_serial_link(void) { LOWPRIO, serialThread, NULL); } +void matrix_set_remote(matrix_row_t* rows, uint8_t index); + void serial_link_update(void) { systime_t current_time = chVTGetSystemTimeX(); if (current_time - last_update > 1000) { @@ -166,17 +168,7 @@ void serial_link_update(void) { matrix_object_t* m = read_keyboard_matrix(0); if (m) { - xprintf("\nr/c 01234567\n"); - for (uint8_t row = 0; row < MATRIX_ROWS; row++) { - xprintf("%X0: ", row); - for (int col = 0; col < MATRIX_COLS; col++) { - if (m->rows[row] & (1<rows, 0); } } From 11bd4ba0dd39654318bd0a3ae495656c2b2187bb Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 12 Mar 2016 19:35:09 +0200 Subject: [PATCH 52/64] Remote object callable from many places Change init_transport to add_remote_objects, so that it can be called many times from different places. --- serial_link/protocol/transport.c | 14 +++++++------- serial_link/protocol/transport.h | 2 +- serial_link/system/system.c | 4 ++-- serial_link/tests/transport_tests.c | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index 4542a7a050e..fbcb040bf1c 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -27,15 +27,15 @@ SOFTWARE. #include "serial_link/protocol/triple_buffered_object.h" #include -static remote_object_t** remote_objects; -static uint32_t num_remote_objects; +#define MAX_REMOTE_OBJECTS 16 +static remote_object_t* remote_objects[MAX_REMOTE_OBJECTS]; +static uint32_t num_remote_objects = 0; -void init_transport(remote_object_t** _remote_objects, uint32_t _num_remote_objects) { - remote_objects = _remote_objects; - num_remote_objects = _num_remote_objects; +void add_remote_objects(remote_object_t** _remote_objects, uint32_t _num_remote_objects) { unsigned int i; - for(i=0;iobject_type == MASTER_TO_ALL_SLAVES) { triple_buffer_object_t* tb = (triple_buffer_object_t*)obj->buffer; triple_buffer_init(tb); diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index 9e9e22462c7..e518aaa6eb7 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -144,7 +144,7 @@ typedef struct { \ #define REMOTE_OBJECT(name) (remote_object_t*)&remote_object_##name -void init_transport(remote_object_t** remote_objects, uint32_t num_remote_objects); +void add_remote_objects(remote_object_t** remote_objects, uint32_t num_remote_objects); void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size); void update_transport(void); diff --git a/serial_link/system/system.c b/serial_link/system/system.c index 9cf695a308b..9e4ee5d95e1 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -120,7 +120,7 @@ static matrix_object_t last_matrix = {}; SLAVE_TO_MASTER_OBJECT(keyboard_matrix, matrix_object_t); MASTER_TO_ALL_SLAVES_OBJECT(serial_link_connected, bool); -remote_object_t* test_remote_objects[] = { +static remote_object_t* remote_objects[] = { REMOTE_OBJECT(serial_link_connected), REMOTE_OBJECT(keyboard_matrix), }; @@ -128,7 +128,7 @@ remote_object_t* test_remote_objects[] = { void init_serial_link(void) { serial_link_connected = false; init_serial_link_hal(); - init_transport(test_remote_objects, sizeof(test_remote_objects)/sizeof(remote_object_t*)); + add_remote_objects(remote_objects, sizeof(remote_objects)/sizeof(remote_object_t*)); init_byte_stuffer(); sdStart(&SD1, &config); sdStart(&SD2, &config); diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index 3e9bffdfa71..02a7a10425b 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -53,7 +53,7 @@ MASTER_TO_ALL_SLAVES_OBJECT(master_to_slave, test_object1_t); MASTER_TO_SINGLE_SLAVE_OBJECT(master_to_single_slave, test_object1_t); SLAVE_TO_MASTER_OBJECT(slave_to_master, test_object1_t); -remote_object_t* test_remote_objects[] = { +static remote_object_t* test_remote_objects[] = { REMOTE_OBJECT(master_to_slave), REMOTE_OBJECT(master_to_single_slave), REMOTE_OBJECT(slave_to_master), @@ -61,7 +61,7 @@ remote_object_t* test_remote_objects[] = { Describe(Transport); BeforeEach(Transport) { - init_transport(test_remote_objects, sizeof(test_remote_objects) / sizeof(remote_object_t*)); + add_remote_objects(test_remote_objects, sizeof(test_remote_objects) / sizeof(remote_object_t*)); sent_data_size = 0; } AfterEach(Transport) {} From 32f0171d393211cc29f57a0ed29327b45e9d3747 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 12 Mar 2016 19:41:34 +0200 Subject: [PATCH 53/64] Fix crash when receiving unregistered remotes --- serial_link/protocol/transport.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index fbcb040bf1c..efc00e79e0d 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -71,22 +71,24 @@ void add_remote_objects(remote_object_t** _remote_objects, uint32_t _num_remote_ void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { uint8_t id = data[size-1]; - remote_object_t* obj = remote_objects[id]; - uint8_t* start; - if (obj->object_type == MASTER_TO_ALL_SLAVES) { - start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + if (id < num_remote_objects) { + remote_object_t* obj = remote_objects[id]; + uint8_t* start; + if (obj->object_type == MASTER_TO_ALL_SLAVES) { + start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + } + else if(obj->object_type == SLAVE_TO_MASTER) { + start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size); + } + else { + start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size); + } + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; + void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); + memcpy(ptr, data, size -1); + triple_buffer_end_write_internal(tb); } - else if(obj->object_type == SLAVE_TO_MASTER) { - start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); - start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size); - } - else { - start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size); - } - triple_buffer_object_t* tb = (triple_buffer_object_t*)start; - void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); - memcpy(ptr, data, size -1); - triple_buffer_end_write_internal(tb); } void update_transport(void) { From d835ad91a35afd4aa01a07a6907b846d5a32323d Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 12 Mar 2016 20:08:08 +0200 Subject: [PATCH 54/64] Resend the keyboard state every ms In case there's some errors on the link, and the packet gets lost --- serial_link/system/system.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index 9e4ee5d95e1..efd0991e9c6 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -140,13 +140,6 @@ void init_serial_link(void) { void matrix_set_remote(matrix_row_t* rows, uint8_t index); void serial_link_update(void) { - systime_t current_time = chVTGetSystemTimeX(); - if (current_time - last_update > 1000) { - *begin_write_serial_link_connected() = true; - end_write_serial_link_connected(); - last_update = current_time; - } - if (read_serial_link_connected()) { serial_link_connected = true; } @@ -157,13 +150,19 @@ void serial_link_update(void) { matrix.rows[i] = matrix_get_row(i); changed |= matrix.rows[i] != last_matrix.rows[i]; } - if (changed) { + + systime_t current_time = chVTGetSystemTimeX(); + systime_t delta = current_time - last_update; + if (changed || delta > US2ST(1000)) { + last_update = current_time; last_matrix = matrix; matrix_object_t* m = begin_write_keyboard_matrix(); for(uint8_t i=0;irows[i] = matrix.rows[i]; } end_write_keyboard_matrix(); + *begin_write_serial_link_connected() = true; + end_write_serial_link_connected(); } matrix_object_t* m = read_keyboard_matrix(0); From f7275f0edea61aa0b52160f5dbdeeddb1f0e7176 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 12 Mar 2016 22:37:04 +0200 Subject: [PATCH 55/64] Configurable baud setting --- serial_link/system/system.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index efd0991e9c6..5406db3f486 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -50,10 +50,12 @@ host_driver_t serial_driver = { send_consumer }; +#ifndef SERIAL_LINK_BAUD +#error "Serial link baud is not set" +#endif -// Slow speed for testing static SerialConfig config = { - .sc_speed = 38400 + .sc_speed = SERIAL_LINK_BAUD }; static uint32_t read_from_serial(SerialDriver* driver, uint8_t link) { From 59657355d22cd6f661fda307675c6f712b5542ec Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 13 Mar 2016 21:38:51 +0200 Subject: [PATCH 56/64] Master mode remains active It remains active even if the USB link is suspended or otherwise temporarilly goes down. --- serial_link/system/system.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index 5406db3f486..f74d8c9e9eb 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -88,11 +88,13 @@ static THD_FUNCTION(serialThread, arg) { EVENT_MASK(2), CHN_INPUT_AVAILABLE); bool need_wait = false; + bool is_master = false; while(true) { if (need_wait) { chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(1000)); } - bool is_master = usbGetDriverStateI(&USBD1) == USB_ACTIVE; + // Always stay as master, even if the USB goes into sleep mode + is_master |= usbGetDriverStateI(&USBD1) == USB_ACTIVE; router_set_master(is_master); need_wait = true; From 3b422d2ac4340ecea6b2fc2f3a855581c737faf8 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 15 May 2016 11:36:39 +0300 Subject: [PATCH 57/64] Add debug print for serial link errors This is disabled by default --- serial_link/system/system.c | 52 ++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index f74d8c9e9eb..5a47d8b83c0 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -58,6 +58,8 @@ static SerialConfig config = { .sc_speed = SERIAL_LINK_BAUD }; +//#define DEBUG_LINK_ERRORS + static uint32_t read_from_serial(SerialDriver* driver, uint8_t link) { const uint32_t buffer_size = 16; uint8_t buffer[buffer_size]; @@ -71,6 +73,37 @@ static uint32_t read_from_serial(SerialDriver* driver, uint8_t link) { return bytes_read; } +static void print_error(char* str, eventflags_t flags, SerialDriver* driver) { +#ifdef DEBUG_LINK_ERRORS + if (flags & SD_PARITY_ERROR) { + print(str); + print(" Parity error\n"); + } + if (flags & SD_FRAMING_ERROR) { + print(str); + print(" Framing error\n"); + } + if (flags & SD_OVERRUN_ERROR) { + print(str); + uint32_t size = qSpaceI(&(driver->iqueue)); + xprintf(" Overrun error, queue size %d\n", size); + + } + if (flags & SD_NOISE_ERROR) { + print(str); + print(" Noise error\n"); + } + if (flags & SD_BREAK_DETECTED) { + print(str); + print(" Break detected\n"); + } +#else + (void)str; + (void)flags; + (void)driver; +#endif +} + // TODO: Optimize the stack size, this is probably way too big static THD_WORKING_AREA(serialThreadStack, 1024); static THD_FUNCTION(serialThread, arg) { @@ -79,20 +112,33 @@ static THD_FUNCTION(serialThread, arg) { event_listener_t sd1_listener; event_listener_t sd2_listener; chEvtRegister(&new_data_event, &new_data_listener, 0); + eventflags_t events = CHN_INPUT_AVAILABLE + | SD_PARITY_ERROR | SD_FRAMING_ERROR | SD_OVERRUN_ERROR | SD_NOISE_ERROR | SD_BREAK_DETECTED; chEvtRegisterMaskWithFlags(chnGetEventSource(&SD1), &sd1_listener, EVENT_MASK(1), - CHN_INPUT_AVAILABLE); + events); chEvtRegisterMaskWithFlags(chnGetEventSource(&SD2), &sd2_listener, EVENT_MASK(2), - CHN_INPUT_AVAILABLE); + events); bool need_wait = false; bool is_master = false; while(true) { + eventflags_t flags1 = 0; + eventflags_t flags2 = 0; if (need_wait) { - chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(1000)); + eventmask_t mask = chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(1000)); + if (mask & EVENT_MASK(1)) { + flags1 = chEvtGetAndClearFlags(&sd1_listener); + print_error("DOWNLINK", flags1, &SD1); + } + if (mask & EVENT_MASK(2)) { + flags2 = chEvtGetAndClearFlags(&sd2_listener); + print_error("UPLINK", flags2, &SD2); + } } + // Always stay as master, even if the USB goes into sleep mode is_master |= usbGetDriverStateI(&USBD1) == USB_ACTIVE; router_set_master(is_master); From a08bcea9983cc97fb2f567c303622495f19a5a1e Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 15 May 2016 11:58:20 +0300 Subject: [PATCH 58/64] Don't accept remote objects with the wrong size Fixes memory corruption when the crc happens to match, but the size doesn't. --- serial_link/protocol/transport.c | 30 ++++++++++---------- serial_link/tests/transport_tests.c | 43 +++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/serial_link/protocol/transport.c b/serial_link/protocol/transport.c index efc00e79e0d..f418d11ceb6 100644 --- a/serial_link/protocol/transport.c +++ b/serial_link/protocol/transport.c @@ -73,21 +73,23 @@ void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) { uint8_t id = data[size-1]; if (id < num_remote_objects) { remote_object_t* obj = remote_objects[id]; - uint8_t* start; - if (obj->object_type == MASTER_TO_ALL_SLAVES) { - start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + if (obj->object_size == size - 1) { + uint8_t* start; + if (obj->object_type == MASTER_TO_ALL_SLAVES) { + start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + } + else if(obj->object_type == SLAVE_TO_MASTER) { + start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); + start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size); + } + else { + start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size); + } + triple_buffer_object_t* tb = (triple_buffer_object_t*)start; + void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); + memcpy(ptr, data, size - 1); + triple_buffer_end_write_internal(tb); } - else if(obj->object_type == SLAVE_TO_MASTER) { - start = obj->buffer + LOCAL_OBJECT_SIZE(obj->object_size); - start += (from - 1) * REMOTE_OBJECT_SIZE(obj->object_size); - } - else { - start = obj->buffer + NUM_SLAVES * LOCAL_OBJECT_SIZE(obj->object_size); - } - triple_buffer_object_t* tb = (triple_buffer_object_t*)start; - void* ptr = triple_buffer_begin_write_internal(obj->object_size, tb); - memcpy(ptr, data, size -1); - triple_buffer_end_write_internal(tb); } } diff --git a/serial_link/tests/transport_tests.c b/serial_link/tests/transport_tests.c index 02a7a10425b..358e1b9fd45 100644 --- a/serial_link/tests/transport_tests.c +++ b/serial_link/tests/transport_tests.c @@ -123,3 +123,46 @@ Ensure(Transport, writes_from_master_to_single_slave) { assert_that(obj2, is_not_equal_to(NULL)); assert_that(obj2->test, is_equal_to(7)); } + +Ensure(Transport, ignores_object_with_invalid_id) { + update_transport(); + test_object1_t* obj = begin_write_master_to_single_slave(3); + obj->test = 7; + expect(signal_data_written); + end_write_master_to_single_slave(3); + expect(router_send_frame, + when(destination, is_equal_to(4))); + update_transport(); + sent_data[sent_data_size - 1] = 44; + transport_recv_frame(0, sent_data, sent_data_size); + test_object1_t* obj2 = read_master_to_single_slave(); + assert_that(obj2, is_equal_to(NULL)); +} + +Ensure(Transport, ignores_object_with_size_too_small) { + update_transport(); + test_object1_t* obj = begin_write_master_to_slave(); + obj->test = 7; + expect(signal_data_written); + end_write_master_to_slave(); + expect(router_send_frame); + update_transport(); + sent_data[sent_data_size - 2] = 0; + transport_recv_frame(0, sent_data, sent_data_size - 1); + test_object1_t* obj2 = read_master_to_slave(); + assert_that(obj2, is_equal_to(NULL)); +} + +Ensure(Transport, ignores_object_with_size_too_big) { + update_transport(); + test_object1_t* obj = begin_write_master_to_slave(); + obj->test = 7; + expect(signal_data_written); + end_write_master_to_slave(); + expect(router_send_frame); + update_transport(); + sent_data[sent_data_size + 21] = 0; + transport_recv_frame(0, sent_data, sent_data_size + 22); + test_object1_t* obj2 = read_master_to_slave(); + assert_that(obj2, is_equal_to(NULL)); +} From b4ab61171e83eeca82fbc1930fcaab175b45c656 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 15 May 2016 12:59:50 +0300 Subject: [PATCH 59/64] Configurable serializer thread priority --- serial_link/system/system.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/serial_link/system/system.c b/serial_link/system/system.c index 5a47d8b83c0..f5af9baa74f 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/system.c @@ -32,6 +32,7 @@ SOFTWARE. #include "matrix.h" #include #include "print.h" +#include "config.h" static event_source_t new_data_event; static bool serial_link_connected; @@ -50,10 +51,15 @@ host_driver_t serial_driver = { send_consumer }; +// Define these in your Config.h file #ifndef SERIAL_LINK_BAUD #error "Serial link baud is not set" #endif +#ifndef SERIAL_LINK_THREAD_PRIORITY +#error "Serial link thread priority not set" +#endif + static SerialConfig config = { .sc_speed = SERIAL_LINK_BAUD }; @@ -184,7 +190,7 @@ void init_serial_link(void) { sdStart(&SD2, &config); chEvtObjectInit(&new_data_event); (void)chThdCreateStatic(serialThreadStack, sizeof(serialThreadStack), - LOWPRIO, serialThread, NULL); + SERIAL_LINK_THREAD_PRIORITY, serialThread, NULL); } void matrix_set_remote(matrix_row_t* rows, uint8_t index); From a0297a892ee1fc571cd9bdc447d2c2726a210629 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 15 May 2016 13:38:59 +0300 Subject: [PATCH 60/64] Rename system to serial_link Also combined driver.h with serial_link.h --- serial_link/protocol/transport.h | 2 +- serial_link/protocol/triple_buffered_object.c | 2 +- serial_link/system/driver.h | 36 ------------------- .../system/{system.c => serial_link.c} | 3 +- .../system/{system.h => serial_link.h} | 11 ++++-- 5 files changed, 12 insertions(+), 42 deletions(-) delete mode 100644 serial_link/system/driver.h rename serial_link/system/{system.c => serial_link.c} (98%) rename serial_link/system/{system.h => serial_link.h} (84%) diff --git a/serial_link/protocol/transport.h b/serial_link/protocol/transport.h index e518aaa6eb7..9a052d88095 100644 --- a/serial_link/protocol/transport.h +++ b/serial_link/protocol/transport.h @@ -26,7 +26,7 @@ SOFTWARE. #define SERIAL_LINK_TRANSPORT_H #include "serial_link/protocol/triple_buffered_object.h" -#include "serial_link/system/system.h" +#include "serial_link/system/serial_link.h" #define NUM_SLAVES 8 #define LOCAL_OBJECT_EXTRA 16 diff --git a/serial_link/protocol/triple_buffered_object.c b/serial_link/protocol/triple_buffered_object.c index c6bf28af0aa..e3e8989d30b 100644 --- a/serial_link/protocol/triple_buffered_object.c +++ b/serial_link/protocol/triple_buffered_object.c @@ -23,7 +23,7 @@ SOFTWARE. */ #include "serial_link/protocol/triple_buffered_object.h" -#include "serial_link/system/system.h" +#include "serial_link/system/serial_link.h" #include #include diff --git a/serial_link/system/driver.h b/serial_link/system/driver.h deleted file mode 100644 index 76e2d682c80..00000000000 --- a/serial_link/system/driver.h +++ /dev/null @@ -1,36 +0,0 @@ -/* -The MIT License (MIT) - -Copyright (c) 2016 Fred Sundvik - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -#ifndef SERIAL_LINK_DRIVER_H -#define SERIAL_LINK_DRIVER_H - -#include "host_driver.h" - -void init_serial_link(void); -void init_serial_link_hal(void); -bool is_serial_link_connected(void); -host_driver_t* get_serial_link_driver(void); -void serial_link_update(void); - -#endif diff --git a/serial_link/system/system.c b/serial_link/system/serial_link.c similarity index 98% rename from serial_link/system/system.c rename to serial_link/system/serial_link.c index f5af9baa74f..27e61a9e976 100644 --- a/serial_link/system/system.c +++ b/serial_link/system/serial_link.c @@ -23,8 +23,7 @@ SOFTWARE. */ #include "report.h" #include "host_driver.h" -#include "serial_link/system/system.h" -#include "serial_link/system/driver.h" +#include "serial_link/system/serial_link.h" #include "hal.h" #include "serial_link/protocol/byte_stuffer.h" #include "serial_link/protocol/transport.h" diff --git a/serial_link/system/system.h b/serial_link/system/serial_link.h similarity index 84% rename from serial_link/system/system.h rename to serial_link/system/serial_link.h index fcc27425e18..132cb657ffd 100644 --- a/serial_link/system/system.h +++ b/serial_link/system/serial_link.h @@ -22,10 +22,17 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifndef SERIAL_LINK_SYSTEM_H -#define SERIAL_LINK_SYSTEM_H +#ifndef SERIAL_LINK_H +#define SERIAL_LINK_H +#include "host_driver.h" +#include +void init_serial_link(void); +void init_serial_link_hal(void); +bool is_serial_link_connected(void); +host_driver_t* get_serial_link_driver(void); +void serial_link_update(void); #if defined(PROTOCOL_CHIBIOS) #include "ch.h" From a20d513e3cdacbf6e0e70a80402497ad10166434 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 15 May 2016 19:28:15 +0300 Subject: [PATCH 61/64] Add function to check if the keyboard is a master --- serial_link/system/serial_link.c | 6 +++++- serial_link/system/serial_link.h | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/serial_link/system/serial_link.c b/serial_link/system/serial_link.c index 27e61a9e976..75c7e77a765 100644 --- a/serial_link/system/serial_link.c +++ b/serial_link/system/serial_link.c @@ -35,6 +35,7 @@ SOFTWARE. static event_source_t new_data_event; static bool serial_link_connected; +static bool is_master = false; static uint8_t keyboard_leds(void); static void send_keyboard(report_keyboard_t *report); @@ -109,6 +110,10 @@ static void print_error(char* str, eventflags_t flags, SerialDriver* driver) { #endif } +bool is_serial_link_master(void) { + return is_master; +} + // TODO: Optimize the stack size, this is probably way too big static THD_WORKING_AREA(serialThreadStack, 1024); static THD_FUNCTION(serialThread, arg) { @@ -128,7 +133,6 @@ static THD_FUNCTION(serialThread, arg) { EVENT_MASK(2), events); bool need_wait = false; - bool is_master = false; while(true) { eventflags_t flags1 = 0; eventflags_t flags2 = 0; diff --git a/serial_link/system/serial_link.h b/serial_link/system/serial_link.h index 132cb657ffd..351e03877b0 100644 --- a/serial_link/system/serial_link.h +++ b/serial_link/system/serial_link.h @@ -31,6 +31,7 @@ SOFTWARE. void init_serial_link(void); void init_serial_link_hal(void); bool is_serial_link_connected(void); +bool is_serial_link_master(void); host_driver_t* get_serial_link_driver(void); void serial_link_update(void); From 3ac52b2e7623152f177670549b780492a0447c12 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 6 Jul 2016 13:09:51 +0300 Subject: [PATCH 62/64] Remove serial_link .gitignore and .gitmodules --- quantum/serial_link/.gitignore | 1 - quantum/serial_link/.gitmodules | 3 --- 2 files changed, 4 deletions(-) delete mode 100644 quantum/serial_link/.gitignore delete mode 100644 quantum/serial_link/.gitmodules diff --git a/quantum/serial_link/.gitignore b/quantum/serial_link/.gitignore deleted file mode 100644 index 2d68e206e81..00000000000 --- a/quantum/serial_link/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.stackdump diff --git a/quantum/serial_link/.gitmodules b/quantum/serial_link/.gitmodules deleted file mode 100644 index 991cfe96d76..00000000000 --- a/quantum/serial_link/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "cgreen/cgreen"] - path = cgreen/cgreen - url = http://github.com/cgreen-devs/cgreen From c5bf090d16cf1fc4d8186f1e8fbbd62457e72da6 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 6 Jul 2016 13:10:30 +0300 Subject: [PATCH 63/64] Remove CGreen make files --- quantum/serial_link/cgreen/Makefile | 38 ----------------------- quantum/serial_link/cgreen/Makefile.build | 33 -------------------- quantum/serial_link/cgreen/cgreen | 1 - 3 files changed, 72 deletions(-) delete mode 100644 quantum/serial_link/cgreen/Makefile delete mode 100644 quantum/serial_link/cgreen/Makefile.build delete mode 160000 quantum/serial_link/cgreen/cgreen diff --git a/quantum/serial_link/cgreen/Makefile b/quantum/serial_link/cgreen/Makefile deleted file mode 100644 index 6b31a3f9232..00000000000 --- a/quantum/serial_link/cgreen/Makefile +++ /dev/null @@ -1,38 +0,0 @@ -# This Makefile ensures that the build is made out of source in a subdirectory called 'build' -# If it doesn't exist, it is created and a Makefile created there (from Makefile.build) -# -# This Makefile also contains delegation of the most common make commands -# -# If you have cmake installed you should be able to do: -# -# make -# make test -# make install -# make package -# -# That should build cgreen for C and C++, run some tests, install it locally and -# generate two distributable packages. - -all: build - cd $(CGREEN_BUILD_DIR); make all - -test: build - cd $(CGREEN_BUILD_DIR); make test - -clean: build - cd $(CGREEN_BUILD_DIR); make clean - -package: build - cd $(CGREEN_BUILD_DIR); make package - -install: - cd $(CGREEN_BUILD_DIR); make install - -############# Internal - -build: - mkdir -p $(CGREEN_BUILD_DIR) - cp Makefile.build $(CGREEN_BUILD_DIR)/Makefile - - -.SILENT: diff --git a/quantum/serial_link/cgreen/Makefile.build b/quantum/serial_link/cgreen/Makefile.build deleted file mode 100644 index f76165244ca..00000000000 --- a/quantum/serial_link/cgreen/Makefile.build +++ /dev/null @@ -1,33 +0,0 @@ -# This Makefile is copied from the cgreen top directory (where it is -# named Makefile.build) and put in a subdirectory called 'build' where -# builds are made This Makefile then automatically creates -# subdirectories for C and C++ builds configuring them using the cmake -# command. Once created you can always tweak the cmake setup as with -# any cmake build directory - -all: build-c build-c++ - for d in build-* ; do cd $$d; make ; cd .. ; done - -clean: - for d in build-* ; do cd $$d; make clean ; cd .. ; done - -check test: - for d in build-* ; do cd $$d; make check ; cd .. ; done - -package: - for d in build-* ; do cd $$d; make package ; cd .. ; done - -install: - for d in build-* ; do cd $$d; make install ; cd .. ; done - -############ Internal - -build-c: - mkdir build-c - cd build-c; cmake -G "Unix Makefiles" $(CGREEN_DIR) - -build-c++: - mkdir build-c++ - cd build-c++; cmake -G "Unix Makefiles" -DWITH_CXX:bool=ON $(CGREEN_DIR) - -.SILENT: diff --git a/quantum/serial_link/cgreen/cgreen b/quantum/serial_link/cgreen/cgreen deleted file mode 160000 index d4d438dda1b..00000000000 --- a/quantum/serial_link/cgreen/cgreen +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d4d438dda1b7131f0bd0530b2c258e9dea6a2a9f From d5e7603d551a31836bf0c59db259ddc3593a1aa7 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Wed, 6 Jul 2016 13:26:20 +0300 Subject: [PATCH 64/64] Remove extra serial_link subdirectory --- quantum/serial_link/README.md | 2 +- .../{serial_link => }/protocol/byte_stuffer.c | 0 .../{serial_link => }/protocol/byte_stuffer.h | 0 .../{serial_link => }/protocol/frame_router.c | 0 .../{serial_link => }/protocol/frame_router.h | 0 .../protocol/frame_validator.c | 0 .../protocol/frame_validator.h | 0 .../{serial_link => }/protocol/physical.h | 0 .../{serial_link => }/protocol/transport.c | 0 .../{serial_link => }/protocol/transport.h | 0 .../protocol/triple_buffered_object.c | 0 .../protocol/triple_buffered_object.h | 0 quantum/serial_link/serial_link.mk | 27 --------------- quantum/serial_link/serial_link_tests.mk | 34 ------------------- .../{serial_link => }/system/serial_link.c | 0 .../{serial_link => }/system/serial_link.h | 0 .../{serial_link => }/tests/Makefile | 0 .../tests/byte_stuffer_tests.c | 0 .../tests/frame_router_tests.c | 0 .../tests/frame_validator_tests.c | 0 .../{serial_link => }/tests/transport_tests.c | 0 .../tests/triple_buffered_object_tests.c | 0 22 files changed, 1 insertion(+), 62 deletions(-) rename quantum/serial_link/{serial_link => }/protocol/byte_stuffer.c (100%) rename quantum/serial_link/{serial_link => }/protocol/byte_stuffer.h (100%) rename quantum/serial_link/{serial_link => }/protocol/frame_router.c (100%) rename quantum/serial_link/{serial_link => }/protocol/frame_router.h (100%) rename quantum/serial_link/{serial_link => }/protocol/frame_validator.c (100%) rename quantum/serial_link/{serial_link => }/protocol/frame_validator.h (100%) rename quantum/serial_link/{serial_link => }/protocol/physical.h (100%) rename quantum/serial_link/{serial_link => }/protocol/transport.c (100%) rename quantum/serial_link/{serial_link => }/protocol/transport.h (100%) rename quantum/serial_link/{serial_link => }/protocol/triple_buffered_object.c (100%) rename quantum/serial_link/{serial_link => }/protocol/triple_buffered_object.h (100%) delete mode 100644 quantum/serial_link/serial_link.mk delete mode 100644 quantum/serial_link/serial_link_tests.mk rename quantum/serial_link/{serial_link => }/system/serial_link.c (100%) rename quantum/serial_link/{serial_link => }/system/serial_link.h (100%) rename quantum/serial_link/{serial_link => }/tests/Makefile (100%) rename quantum/serial_link/{serial_link => }/tests/byte_stuffer_tests.c (100%) rename quantum/serial_link/{serial_link => }/tests/frame_router_tests.c (100%) rename quantum/serial_link/{serial_link => }/tests/frame_validator_tests.c (100%) rename quantum/serial_link/{serial_link => }/tests/transport_tests.c (100%) rename quantum/serial_link/{serial_link => }/tests/triple_buffered_object_tests.c (100%) diff --git a/quantum/serial_link/README.md b/quantum/serial_link/README.md index 94af9125cef..e8490e290dd 100644 --- a/quantum/serial_link/README.md +++ b/quantum/serial_link/README.md @@ -1 +1 @@ -# tmk_serial_link \ No newline at end of file +# qmk_serial_link \ No newline at end of file diff --git a/quantum/serial_link/serial_link/protocol/byte_stuffer.c b/quantum/serial_link/protocol/byte_stuffer.c similarity index 100% rename from quantum/serial_link/serial_link/protocol/byte_stuffer.c rename to quantum/serial_link/protocol/byte_stuffer.c diff --git a/quantum/serial_link/serial_link/protocol/byte_stuffer.h b/quantum/serial_link/protocol/byte_stuffer.h similarity index 100% rename from quantum/serial_link/serial_link/protocol/byte_stuffer.h rename to quantum/serial_link/protocol/byte_stuffer.h diff --git a/quantum/serial_link/serial_link/protocol/frame_router.c b/quantum/serial_link/protocol/frame_router.c similarity index 100% rename from quantum/serial_link/serial_link/protocol/frame_router.c rename to quantum/serial_link/protocol/frame_router.c diff --git a/quantum/serial_link/serial_link/protocol/frame_router.h b/quantum/serial_link/protocol/frame_router.h similarity index 100% rename from quantum/serial_link/serial_link/protocol/frame_router.h rename to quantum/serial_link/protocol/frame_router.h diff --git a/quantum/serial_link/serial_link/protocol/frame_validator.c b/quantum/serial_link/protocol/frame_validator.c similarity index 100% rename from quantum/serial_link/serial_link/protocol/frame_validator.c rename to quantum/serial_link/protocol/frame_validator.c diff --git a/quantum/serial_link/serial_link/protocol/frame_validator.h b/quantum/serial_link/protocol/frame_validator.h similarity index 100% rename from quantum/serial_link/serial_link/protocol/frame_validator.h rename to quantum/serial_link/protocol/frame_validator.h diff --git a/quantum/serial_link/serial_link/protocol/physical.h b/quantum/serial_link/protocol/physical.h similarity index 100% rename from quantum/serial_link/serial_link/protocol/physical.h rename to quantum/serial_link/protocol/physical.h diff --git a/quantum/serial_link/serial_link/protocol/transport.c b/quantum/serial_link/protocol/transport.c similarity index 100% rename from quantum/serial_link/serial_link/protocol/transport.c rename to quantum/serial_link/protocol/transport.c diff --git a/quantum/serial_link/serial_link/protocol/transport.h b/quantum/serial_link/protocol/transport.h similarity index 100% rename from quantum/serial_link/serial_link/protocol/transport.h rename to quantum/serial_link/protocol/transport.h diff --git a/quantum/serial_link/serial_link/protocol/triple_buffered_object.c b/quantum/serial_link/protocol/triple_buffered_object.c similarity index 100% rename from quantum/serial_link/serial_link/protocol/triple_buffered_object.c rename to quantum/serial_link/protocol/triple_buffered_object.c diff --git a/quantum/serial_link/serial_link/protocol/triple_buffered_object.h b/quantum/serial_link/protocol/triple_buffered_object.h similarity index 100% rename from quantum/serial_link/serial_link/protocol/triple_buffered_object.h rename to quantum/serial_link/protocol/triple_buffered_object.h diff --git a/quantum/serial_link/serial_link.mk b/quantum/serial_link/serial_link.mk deleted file mode 100644 index e164cc5ff36..00000000000 --- a/quantum/serial_link/serial_link.mk +++ /dev/null @@ -1,27 +0,0 @@ -# The MIT License (MIT) -# -# Copyright (c) 2016 Fred Sundvik -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -UINCDIR += $(SERIAL_DIR) -SRC += $(wildcard $(SERIAL_DIR)/serial_link/protocol/*.c) -SRC += $(wildcard $(SERIAL_DIR)/serial_link/system/*.c) -SRC += serial_link_hal.c -OPT_DEFS += -DUSE_SERIAL_LINK diff --git a/quantum/serial_link/serial_link_tests.mk b/quantum/serial_link/serial_link_tests.mk deleted file mode 100644 index e292f582a98..00000000000 --- a/quantum/serial_link/serial_link_tests.mk +++ /dev/null @@ -1,34 +0,0 @@ -# The MIT License (MIT) -# -# Copyright (c) 2016 Fred Sundvik -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -CGREEN_LIB = $(BUILDDIR)/cgreen/build-c/src/libcgreen.a - -CGREEN_DIR = "$(CURDIR)/$(SERIAL_DIR)/cgreen/cgreen" -CGREEN_BUILD_DIR = "$(CURDIR)/$(BUILDDIR)/cgreen" -export CGREEN_DIR -export CGREEN_BUILD_DIR -$(CGREEN_LIB): - @make -C $(SERIAL_DIR)/cgreen - -.PHONY serialtest: -serialtest : $(CGREEN_LIB) - @make -C $(SERIAL_DIR)/serial_link/tests BUILDDIR=../../../$(BUILDDIR) \ No newline at end of file diff --git a/quantum/serial_link/serial_link/system/serial_link.c b/quantum/serial_link/system/serial_link.c similarity index 100% rename from quantum/serial_link/serial_link/system/serial_link.c rename to quantum/serial_link/system/serial_link.c diff --git a/quantum/serial_link/serial_link/system/serial_link.h b/quantum/serial_link/system/serial_link.h similarity index 100% rename from quantum/serial_link/serial_link/system/serial_link.h rename to quantum/serial_link/system/serial_link.h diff --git a/quantum/serial_link/serial_link/tests/Makefile b/quantum/serial_link/tests/Makefile similarity index 100% rename from quantum/serial_link/serial_link/tests/Makefile rename to quantum/serial_link/tests/Makefile diff --git a/quantum/serial_link/serial_link/tests/byte_stuffer_tests.c b/quantum/serial_link/tests/byte_stuffer_tests.c similarity index 100% rename from quantum/serial_link/serial_link/tests/byte_stuffer_tests.c rename to quantum/serial_link/tests/byte_stuffer_tests.c diff --git a/quantum/serial_link/serial_link/tests/frame_router_tests.c b/quantum/serial_link/tests/frame_router_tests.c similarity index 100% rename from quantum/serial_link/serial_link/tests/frame_router_tests.c rename to quantum/serial_link/tests/frame_router_tests.c diff --git a/quantum/serial_link/serial_link/tests/frame_validator_tests.c b/quantum/serial_link/tests/frame_validator_tests.c similarity index 100% rename from quantum/serial_link/serial_link/tests/frame_validator_tests.c rename to quantum/serial_link/tests/frame_validator_tests.c diff --git a/quantum/serial_link/serial_link/tests/transport_tests.c b/quantum/serial_link/tests/transport_tests.c similarity index 100% rename from quantum/serial_link/serial_link/tests/transport_tests.c rename to quantum/serial_link/tests/transport_tests.c diff --git a/quantum/serial_link/serial_link/tests/triple_buffered_object_tests.c b/quantum/serial_link/tests/triple_buffered_object_tests.c similarity index 100% rename from quantum/serial_link/serial_link/tests/triple_buffered_object_tests.c rename to quantum/serial_link/tests/triple_buffered_object_tests.c