Compare commits
36
Commits
94b5fd47a4
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d8716c232e | ||
|
|
a3e330ed77 | ||
|
|
672267b991 | ||
|
|
8398442544 | ||
|
|
b29512d922 | ||
|
|
6e4525df38 | ||
|
|
60a304a93d | ||
|
|
f504553ab6 | ||
|
|
bbfe61a9ed | ||
|
|
73bc078465 | ||
|
|
8d4f1da028 | ||
|
|
1d36a757c0 | ||
|
|
648e201f5e | ||
|
|
400d308f4a | ||
|
|
1c9120a197 | ||
|
|
3b560799af | ||
|
|
3abdd8816c | ||
|
|
cf42e86322 | ||
|
|
59dbd7b035 | ||
|
|
d3e44125a2 | ||
|
|
ebb739a3a0 | ||
|
|
441347fc95 | ||
|
|
a9779cbade | ||
|
|
704d1c9c0b | ||
|
|
95bfcaa4d2 | ||
|
|
01d0be7004 | ||
|
|
a8c7c42471 | ||
|
|
0934105952 | ||
|
|
55228fff8d | ||
|
|
2bc6686d90 | ||
|
|
73d3e24786 | ||
|
|
fad6a0aee2 | ||
|
|
50ee8009fc | ||
|
|
beef75f31c | ||
|
|
c564fedf65 | ||
|
|
b4d9f24f0e |
@@ -13,6 +13,15 @@ get_code_gen:
|
|||||||
gen_prot:
|
gen_prot:
|
||||||
./alox.protogen -i prot.json -o main/uart
|
./alox.protogen -i prot.json -o main/uart
|
||||||
|
|
||||||
|
switch_to_s3:
|
||||||
|
idf.py set-target esp32s3
|
||||||
|
cp sdkconfig.s3 sdkconfig
|
||||||
|
idf.py build
|
||||||
|
|
||||||
|
switch_to_c3:
|
||||||
|
idf.py set-target esp32c3
|
||||||
|
cp sdkconfig.c3 sdkconfig
|
||||||
|
idf.py build
|
||||||
|
|
||||||
buildIdf:
|
buildIdf:
|
||||||
idf.py build
|
idf.py build
|
||||||
@@ -20,6 +29,23 @@ buildIdf:
|
|||||||
flashMini:
|
flashMini:
|
||||||
idf.py flash -p /dev/ttyACM0
|
idf.py flash -p /dev/ttyACM0
|
||||||
|
|
||||||
|
flashMini2:
|
||||||
|
idf.py flash -p /dev/ttyACM1
|
||||||
|
|
||||||
|
flashMini3:
|
||||||
|
idf.py flash -p /dev/ttyACM2
|
||||||
|
|
||||||
|
flashCluster:
|
||||||
|
idf.py flash -p /dev/ttyACM1
|
||||||
|
idf.py flash -p /dev/ttyACM2
|
||||||
|
idf.py flash -p /dev/ttyACM3
|
||||||
|
idf.py flash -p /dev/ttyACM4
|
||||||
|
idf.py flash -p /dev/ttyACM5
|
||||||
|
idf.py flash -p /dev/ttyACM6
|
||||||
|
idf.py flash -p /dev/ttyACM7
|
||||||
|
idf.py flash -p /dev/ttyACM8
|
||||||
|
|
||||||
|
|
||||||
monitorMini:
|
monitorMini:
|
||||||
idf.py monitor -p /dev/ttyACM0
|
idf.py monitor -p /dev/ttyACM0
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
module alox.tool
|
||||||
|
|
||||||
|
go 1.24.5
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/pterm/pterm v0.12.81
|
||||||
|
go.bug.st/serial v1.6.4
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
atomicgo.dev/cursor v0.2.0 // indirect
|
||||||
|
atomicgo.dev/keyboard v0.2.9 // indirect
|
||||||
|
atomicgo.dev/schedule v0.1.0 // indirect
|
||||||
|
github.com/containerd/console v1.0.5 // indirect
|
||||||
|
github.com/creack/goselect v0.1.2 // indirect
|
||||||
|
github.com/gookit/color v1.5.4 // indirect
|
||||||
|
github.com/lithammer/fuzzysearch v1.1.8 // indirect
|
||||||
|
github.com/mattn/go-runewidth v0.0.16 // indirect
|
||||||
|
github.com/rivo/uniseg v0.4.7 // indirect
|
||||||
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||||
|
golang.org/x/sys v0.33.0 // indirect
|
||||||
|
golang.org/x/term v0.32.0 // indirect
|
||||||
|
golang.org/x/text v0.26.0 // indirect
|
||||||
|
)
|
||||||
+124
@@ -0,0 +1,124 @@
|
|||||||
|
atomicgo.dev/assert v0.0.2 h1:FiKeMiZSgRrZsPo9qn/7vmr7mCsh5SZyXY4YGYiYwrg=
|
||||||
|
atomicgo.dev/assert v0.0.2/go.mod h1:ut4NcI3QDdJtlmAxQULOmA13Gz6e2DWbSAS8RUOmNYQ=
|
||||||
|
atomicgo.dev/cursor v0.2.0 h1:H6XN5alUJ52FZZUkI7AlJbUc1aW38GWZalpYRPpoPOw=
|
||||||
|
atomicgo.dev/cursor v0.2.0/go.mod h1:Lr4ZJB3U7DfPPOkbH7/6TOtJ4vFGHlgj1nc+n900IpU=
|
||||||
|
atomicgo.dev/keyboard v0.2.9 h1:tOsIid3nlPLZ3lwgG8KZMp/SFmr7P0ssEN5JUsm78K8=
|
||||||
|
atomicgo.dev/keyboard v0.2.9/go.mod h1:BC4w9g00XkxH/f1HXhW2sXmJFOCWbKn9xrOunSFtExQ=
|
||||||
|
atomicgo.dev/schedule v0.1.0 h1:nTthAbhZS5YZmgYbb2+DH8uQIZcTlIrd4eYr3UQxEjs=
|
||||||
|
atomicgo.dev/schedule v0.1.0/go.mod h1:xeUa3oAkiuHYh8bKiQBRojqAMq3PXXbJujjb0hw8pEU=
|
||||||
|
github.com/MarvinJWendt/testza v0.1.0/go.mod h1:7AxNvlfeHP7Z/hDQ5JtE3OKYT3XFUeLCDE2DQninSqs=
|
||||||
|
github.com/MarvinJWendt/testza v0.2.1/go.mod h1:God7bhG8n6uQxwdScay+gjm9/LnO4D3kkcZX4hv9Rp8=
|
||||||
|
github.com/MarvinJWendt/testza v0.2.8/go.mod h1:nwIcjmr0Zz+Rcwfh3/4UhBp7ePKVhuBExvZqnKYWlII=
|
||||||
|
github.com/MarvinJWendt/testza v0.2.10/go.mod h1:pd+VWsoGUiFtq+hRKSU1Bktnn+DMCSrDrXDpX2bG66k=
|
||||||
|
github.com/MarvinJWendt/testza v0.2.12/go.mod h1:JOIegYyV7rX+7VZ9r77L/eH6CfJHHzXjB69adAhzZkI=
|
||||||
|
github.com/MarvinJWendt/testza v0.3.0/go.mod h1:eFcL4I0idjtIx8P9C6KkAuLgATNKpX4/2oUqKc6bF2c=
|
||||||
|
github.com/MarvinJWendt/testza v0.4.2/go.mod h1:mSdhXiKH8sg/gQehJ63bINcCKp7RtYewEjXsvsVUPbE=
|
||||||
|
github.com/MarvinJWendt/testza v0.5.2 h1:53KDo64C1z/h/d/stCYCPY69bt/OSwjq5KpFNwi+zB4=
|
||||||
|
github.com/MarvinJWendt/testza v0.5.2/go.mod h1:xu53QFE5sCdjtMCKk8YMQ2MnymimEctc4n3EjyIYvEY=
|
||||||
|
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
|
||||||
|
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
|
||||||
|
github.com/containerd/console v1.0.5 h1:R0ymNeydRqH2DmakFNdmjR2k0t7UPuiOV/N/27/qqsc=
|
||||||
|
github.com/containerd/console v1.0.5/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
|
||||||
|
github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0=
|
||||||
|
github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQHCoQ=
|
||||||
|
github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo=
|
||||||
|
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
|
||||||
|
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.0.10/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.3 h1:sxCkb+qR91z4vsqw4vGGZlDgPz3G7gjaLyK3V8y70BU=
|
||||||
|
github.com/klauspost/cpuid/v2 v2.2.3/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
|
||||||
|
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||||
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
|
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
|
||||||
|
github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4=
|
||||||
|
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||||
|
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
|
||||||
|
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/pterm/pterm v0.12.27/go.mod h1:PhQ89w4i95rhgE+xedAoqous6K9X+r6aSOI2eFF7DZI=
|
||||||
|
github.com/pterm/pterm v0.12.29/go.mod h1:WI3qxgvoQFFGKGjGnJR849gU0TsEOvKn5Q8LlY1U7lg=
|
||||||
|
github.com/pterm/pterm v0.12.30/go.mod h1:MOqLIyMOgmTDz9yorcYbcw+HsgoZo3BQfg2wtl3HEFE=
|
||||||
|
github.com/pterm/pterm v0.12.31/go.mod h1:32ZAWZVXD7ZfG0s8qqHXePte42kdz8ECtRyEejaWgXU=
|
||||||
|
github.com/pterm/pterm v0.12.33/go.mod h1:x+h2uL+n7CP/rel9+bImHD5lF3nM9vJj80k9ybiiTTE=
|
||||||
|
github.com/pterm/pterm v0.12.36/go.mod h1:NjiL09hFhT/vWjQHSj1athJpx6H8cjpHXNAK5bUw8T8=
|
||||||
|
github.com/pterm/pterm v0.12.40/go.mod h1:ffwPLwlbXxP+rxT0GsgDTzS3y3rmpAO1NMjUkGTYf8s=
|
||||||
|
github.com/pterm/pterm v0.12.81 h1:ju+j5I2++FO1jBKMmscgh5h5DPFDFMB7epEjSoKehKA=
|
||||||
|
github.com/pterm/pterm v0.12.81/go.mod h1:TyuyrPjnxfwP+ccJdBTeWHtd/e0ybQHkOS/TakajZCw=
|
||||||
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
|
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||||
|
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||||
|
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
|
||||||
|
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
|
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||||
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
|
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
|
||||||
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
||||||
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||||
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
|
go.bug.st/serial v1.6.4 h1:7FmqNPgVp3pu2Jz5PoPtbZ9jJO5gnEnZIvnI1lzve8A=
|
||||||
|
go.bug.st/serial v1.6.4/go.mod h1:nofMJxTeNVny/m6+KaafC6vJGj3miwQZ6vW4BZUGJPI=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
|
||||||
|
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
|
||||||
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
|
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
|
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||||
|
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
||||||
|
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||||
|
golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
|
||||||
|
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||||
|
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||||
|
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
|
||||||
|
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||||
|
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
+508
@@ -0,0 +1,508 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/binary"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pterm/pterm"
|
||||||
|
"go.bug.st/serial"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ParserState int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// MISC
|
||||||
|
UART_ECHO = 0x01
|
||||||
|
UART_VERSION = 0x02
|
||||||
|
UART_CLIENT_INFO = 0x03
|
||||||
|
|
||||||
|
// OTA
|
||||||
|
UART_OTA_START = 0x10
|
||||||
|
UART_OTA_PAYLOAD = 0x11
|
||||||
|
UART_OTA_END = 0x12
|
||||||
|
UART_OTA_STATUS = 0x13
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
WAITING_FOR_START_BYTE ParserState = iota
|
||||||
|
ESCAPED_MESSAGE_ID
|
||||||
|
GET_MESSAGE_ID
|
||||||
|
IN_PAYLOD
|
||||||
|
ESCAPED_PAYLOAD_BYTE
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
START_BYTE = 0xAA
|
||||||
|
ESCAPE_BYTE = 0xBB
|
||||||
|
END_BYTE = 0xCC
|
||||||
|
)
|
||||||
|
|
||||||
|
type ParseError int
|
||||||
|
|
||||||
|
const (
|
||||||
|
WRONG_CHECKSUM ParseError = iota
|
||||||
|
UNEXPECETD_BYTE
|
||||||
|
)
|
||||||
|
|
||||||
|
type MessageReceive struct {
|
||||||
|
raw_message []byte
|
||||||
|
parsed_message []byte
|
||||||
|
checksum byte
|
||||||
|
error ParseError
|
||||||
|
state ParserState
|
||||||
|
write_index int
|
||||||
|
raw_write_index int
|
||||||
|
}
|
||||||
|
|
||||||
|
type OTASyncManager struct {
|
||||||
|
OTA_MessageCounter int
|
||||||
|
OTA_PayloadMessageSequence int
|
||||||
|
NewOTAMessage chan MessageReceive
|
||||||
|
TimeoutMessage time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ot *OTASyncManager) WaitForNextMessageTimeout() (*MessageReceive, error) {
|
||||||
|
select {
|
||||||
|
case msg := <-ot.NewOTAMessage:
|
||||||
|
return &msg, nil
|
||||||
|
case <-time.After(ot.TimeoutMessage):
|
||||||
|
return nil, fmt.Errorf("Message Timeout")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func initMessageReceive(mr *MessageReceive) {
|
||||||
|
mr.raw_message = make([]byte, 1024*4)
|
||||||
|
mr.parsed_message = make([]byte, 1024*4)
|
||||||
|
mr.checksum = 0
|
||||||
|
mr.error = 0
|
||||||
|
mr.write_index = 0
|
||||||
|
mr.raw_write_index = 0
|
||||||
|
mr.state = WAITING_FOR_START_BYTE
|
||||||
|
}
|
||||||
|
|
||||||
|
func addByteToRawBuffer(mr *MessageReceive, pbyte byte) {
|
||||||
|
mr.raw_message[mr.raw_write_index] = pbyte
|
||||||
|
mr.raw_write_index += 1
|
||||||
|
}
|
||||||
|
func addByteToParsedBuffer(mr *MessageReceive, pbyte byte) {
|
||||||
|
mr.parsed_message[mr.write_index] = pbyte
|
||||||
|
mr.write_index += 1
|
||||||
|
mr.checksum ^= pbyte
|
||||||
|
}
|
||||||
|
|
||||||
|
func parse_uart_ota_payload_payload(payloadBuffer []byte, payload_len int) {
|
||||||
|
//fmt.Printf("RAW BUFFER: % 02X", payloadBuffer[:payload_len])
|
||||||
|
if payload_len != 4 {
|
||||||
|
fmt.Printf("Payload should be 4 is %v", payload_len)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Sequence %v, WriteIndex %v", binary.LittleEndian.Uint16(payloadBuffer[0:1]), binary.LittleEndian.Uint16(payloadBuffer[2:3]))
|
||||||
|
}
|
||||||
|
|
||||||
|
func parse_uart_version_payload(payloadBuffer []byte, payload_len int) {
|
||||||
|
type payload_data struct {
|
||||||
|
Version uint16
|
||||||
|
BuildHash [7]uint8
|
||||||
|
}
|
||||||
|
|
||||||
|
tableHeaders := pterm.TableData{
|
||||||
|
{"Version", "Buildhash"},
|
||||||
|
}
|
||||||
|
|
||||||
|
tableData := tableHeaders
|
||||||
|
|
||||||
|
tableData = append(tableData, []string{
|
||||||
|
fmt.Sprintf("%d", binary.LittleEndian.Uint16(payloadBuffer[1:3])),
|
||||||
|
fmt.Sprintf("%s", payloadBuffer[3:10]),
|
||||||
|
})
|
||||||
|
|
||||||
|
err := pterm.DefaultTable.WithHasHeader().WithBoxed().WithData(tableData).Render()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Fehler beim Rendern der Tabelle: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func parse_uart_client_info_payload(payloadBuffer []byte, payload_len int) {
|
||||||
|
|
||||||
|
type payload_data struct {
|
||||||
|
ClientID uint8
|
||||||
|
IsAvailable uint8
|
||||||
|
SlotIsUsed uint8
|
||||||
|
MACAddr [6]uint8
|
||||||
|
LastPing uint32
|
||||||
|
LastSuccessfulPing uint32
|
||||||
|
Version uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
tableHeaders := pterm.TableData{
|
||||||
|
{"Client ID", "Verfügbar", "Genutzt", "MAC-Adresse", "Last Ping", "Letzter Erfolg Ping", "Version"},
|
||||||
|
}
|
||||||
|
|
||||||
|
tableData := tableHeaders
|
||||||
|
|
||||||
|
currentOffset := 2
|
||||||
|
|
||||||
|
const (
|
||||||
|
ENTRY_LEN = 19
|
||||||
|
OFFSET_MAC_ADDR = 3
|
||||||
|
OFFSET_LAST_PING = 9
|
||||||
|
OFFSET_LAST_SUCCESS_PING = 13
|
||||||
|
OFFSET_VERSION = 17
|
||||||
|
)
|
||||||
|
|
||||||
|
for i := 0; i < int(payloadBuffer[1]); i++ {
|
||||||
|
if currentOffset+ENTRY_LEN > payload_len {
|
||||||
|
fmt.Printf("Fehler: Payload zu kurz für Client-Eintrag %d\n", i)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
entryBytes := payloadBuffer[currentOffset : currentOffset+ENTRY_LEN]
|
||||||
|
|
||||||
|
var clientData payload_data
|
||||||
|
|
||||||
|
clientData.ClientID = entryBytes[0]
|
||||||
|
clientData.IsAvailable = entryBytes[1]
|
||||||
|
clientData.SlotIsUsed = entryBytes[2]
|
||||||
|
|
||||||
|
copy(clientData.MACAddr[:], entryBytes[OFFSET_MAC_ADDR:OFFSET_MAC_ADDR+6])
|
||||||
|
|
||||||
|
clientData.LastPing = binary.LittleEndian.Uint32(entryBytes[OFFSET_LAST_PING : OFFSET_LAST_PING+4])
|
||||||
|
clientData.LastSuccessfulPing = binary.LittleEndian.Uint32(entryBytes[OFFSET_LAST_SUCCESS_PING : OFFSET_LAST_SUCCESS_PING+4])
|
||||||
|
clientData.Version = binary.LittleEndian.Uint16(entryBytes[OFFSET_VERSION : OFFSET_VERSION+2])
|
||||||
|
|
||||||
|
// Füge die geparsten Daten als String-Slice zur Tabelle hinzu
|
||||||
|
tableData = append(tableData, []string{
|
||||||
|
fmt.Sprintf("%d", clientData.ClientID),
|
||||||
|
fmt.Sprintf("%d", clientData.IsAvailable),
|
||||||
|
fmt.Sprintf("%d", clientData.SlotIsUsed),
|
||||||
|
fmt.Sprintf("%X:%X:%X:%X:%X:%X",
|
||||||
|
clientData.MACAddr[0], clientData.MACAddr[1], clientData.MACAddr[2],
|
||||||
|
clientData.MACAddr[3], clientData.MACAddr[4], clientData.MACAddr[5]),
|
||||||
|
fmt.Sprintf("%d", clientData.LastPing),
|
||||||
|
fmt.Sprintf("%d", clientData.LastSuccessfulPing),
|
||||||
|
fmt.Sprintf("%d", clientData.Version),
|
||||||
|
})
|
||||||
|
|
||||||
|
currentOffset += ENTRY_LEN
|
||||||
|
}
|
||||||
|
err := pterm.DefaultTable.WithHasHeader().WithBoxed().WithData(tableData).Render()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Fehler beim Rendern der Tabelle: %s\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func message_receive_callback(mr MessageReceive) {
|
||||||
|
log.Printf("Message Received: % 02X\n", mr.raw_message[:mr.raw_write_index])
|
||||||
|
switch mr.parsed_message[0] {
|
||||||
|
case byte(UART_ECHO):
|
||||||
|
break
|
||||||
|
case UART_VERSION:
|
||||||
|
parse_uart_version_payload(mr.parsed_message, mr.write_index)
|
||||||
|
break
|
||||||
|
case UART_CLIENT_INFO:
|
||||||
|
parse_uart_client_info_payload(mr.parsed_message, mr.write_index)
|
||||||
|
break
|
||||||
|
case UART_OTA_START:
|
||||||
|
OTA_UpdateHandler.NewOTAMessage <- mr
|
||||||
|
break
|
||||||
|
case UART_OTA_PAYLOAD:
|
||||||
|
parse_uart_ota_payload_payload(mr.parsed_message, mr.write_index)
|
||||||
|
OTA_UpdateHandler.NewOTAMessage <- mr
|
||||||
|
break
|
||||||
|
case UART_OTA_END:
|
||||||
|
OTA_UpdateHandler.NewOTAMessage <- mr
|
||||||
|
break
|
||||||
|
case UART_OTA_STATUS:
|
||||||
|
OTA_UpdateHandler.NewOTAMessage <- mr
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func message_receive_failed_callback(mr MessageReceive) {
|
||||||
|
log.Printf("Error Message Received: % 02X\n", mr.raw_message[:mr.raw_write_index])
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseByte(mr *MessageReceive, pbyte byte) {
|
||||||
|
addByteToRawBuffer(mr, pbyte)
|
||||||
|
switch mr.state {
|
||||||
|
case WAITING_FOR_START_BYTE:
|
||||||
|
if pbyte == START_BYTE {
|
||||||
|
initMessageReceive(mr)
|
||||||
|
mr.state = GET_MESSAGE_ID
|
||||||
|
addByteToRawBuffer(mr, pbyte)
|
||||||
|
}
|
||||||
|
// ignore every other byte
|
||||||
|
break
|
||||||
|
case GET_MESSAGE_ID:
|
||||||
|
if pbyte == ESCAPE_BYTE {
|
||||||
|
mr.state = ESCAPED_MESSAGE_ID
|
||||||
|
} else {
|
||||||
|
addByteToParsedBuffer(mr, pbyte)
|
||||||
|
mr.state = IN_PAYLOD
|
||||||
|
}
|
||||||
|
break
|
||||||
|
case ESCAPED_MESSAGE_ID:
|
||||||
|
addByteToParsedBuffer(mr, pbyte)
|
||||||
|
mr.state = IN_PAYLOD
|
||||||
|
break
|
||||||
|
case IN_PAYLOD:
|
||||||
|
if pbyte == ESCAPE_BYTE {
|
||||||
|
mr.state = ESCAPED_PAYLOAD_BYTE
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if pbyte == START_BYTE {
|
||||||
|
mr.error = UNEXPECETD_BYTE
|
||||||
|
go message_receive_failed_callback(*mr)
|
||||||
|
initMessageReceive(mr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if pbyte == END_BYTE {
|
||||||
|
if mr.checksum != 0 { // checksum wrong
|
||||||
|
mr.error = WRONG_CHECKSUM
|
||||||
|
go message_receive_failed_callback(*mr)
|
||||||
|
initMessageReceive(mr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go message_receive_callback(*mr)
|
||||||
|
initMessageReceive(mr)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// normal case
|
||||||
|
addByteToParsedBuffer(mr, pbyte)
|
||||||
|
break
|
||||||
|
case ESCAPED_PAYLOAD_BYTE:
|
||||||
|
addByteToParsedBuffer(mr, pbyte)
|
||||||
|
mr.state = IN_PAYLOD
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
panic(fmt.Sprintf("unexpected main.ParserState: %#v", mr.state))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildMessage(payloadBuffer []byte, payload_len int, sendBuffer []byte) int {
|
||||||
|
var writeIndex int
|
||||||
|
checksum := byte(0x00)
|
||||||
|
writeIndex = 0
|
||||||
|
sendBuffer[writeIndex] = START_BYTE
|
||||||
|
writeIndex++
|
||||||
|
for i := range payload_len {
|
||||||
|
b := payloadBuffer[i]
|
||||||
|
if b == START_BYTE || b == ESCAPE_BYTE || b == END_BYTE {
|
||||||
|
sendBuffer[writeIndex] = ESCAPE_BYTE
|
||||||
|
writeIndex++
|
||||||
|
}
|
||||||
|
sendBuffer[writeIndex] = b
|
||||||
|
writeIndex++
|
||||||
|
checksum ^= b
|
||||||
|
}
|
||||||
|
if checksum == START_BYTE || checksum == ESCAPE_BYTE || checksum == END_BYTE {
|
||||||
|
sendBuffer[writeIndex] = ESCAPE_BYTE
|
||||||
|
writeIndex++
|
||||||
|
}
|
||||||
|
sendBuffer[writeIndex] = checksum
|
||||||
|
writeIndex++
|
||||||
|
sendBuffer[writeIndex] = END_BYTE
|
||||||
|
writeIndex++
|
||||||
|
return writeIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
func sendMessage(port serial.Port, sendBuffer []byte) {
|
||||||
|
n, err := port.Write(sendBuffer)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Could not Send %v to Serial Port", sendBuffer)
|
||||||
|
}
|
||||||
|
if n < len(sendBuffer) {
|
||||||
|
log.Printf("Did not send all data %v, only send %v", len(sendBuffer), n)
|
||||||
|
}
|
||||||
|
fmt.Printf("Send Message % 02X\n", sendBuffer[:n])
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
updatePath string
|
||||||
|
OTA_UpdateHandler OTASyncManager
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.StringVar(&updatePath, "update", "", "Path to Updatefile")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
OTA_UpdateHandler = OTASyncManager{
|
||||||
|
OTA_MessageCounter: 0,
|
||||||
|
OTA_PayloadMessageSequence: 0,
|
||||||
|
NewOTAMessage: make(chan MessageReceive),
|
||||||
|
TimeoutMessage: time.Millisecond * 30000,
|
||||||
|
}
|
||||||
|
|
||||||
|
mode := &serial.Mode{
|
||||||
|
//BaudRate: 115200,
|
||||||
|
BaudRate: 921600,
|
||||||
|
}
|
||||||
|
port, err := serial.Open("/dev/ttyUSB0", mode)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx, cancle := context.WithCancel(context.Background())
|
||||||
|
defer cancle()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
buff := make([]byte, 1024)
|
||||||
|
mr := MessageReceive{}
|
||||||
|
initMessageReceive(&mr)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
n, err := port.Read(buff)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
fmt.Println("\nEOF")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
for _, b := range buff[:n] {
|
||||||
|
parseByte(&mr, b)
|
||||||
|
}
|
||||||
|
//fmt.Printf("Empfangen: % 02X\n", string(buff[:n]))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if updatePath != "" {
|
||||||
|
// start update
|
||||||
|
update, err := os.ReadFile(updatePath)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Could not read Update file %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("Update Buffer read, update size %v", len(update))
|
||||||
|
log.Printf("Gonna break it down in 200 Bytes packages will send %v packages", len(update)/200)
|
||||||
|
|
||||||
|
// start
|
||||||
|
payload_buffer := make([]byte, 1024)
|
||||||
|
send_buffer := make([]byte, 1024)
|
||||||
|
payload_buffer[0] = UART_OTA_START
|
||||||
|
n := buildMessage(payload_buffer, 1, send_buffer)
|
||||||
|
sendMessage(port, send_buffer[:n])
|
||||||
|
msg, err := OTA_UpdateHandler.WaitForNextMessageTimeout()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error Message not acked %v", err)
|
||||||
|
} else {
|
||||||
|
if msg.parsed_message[2] != 0x00 {
|
||||||
|
log.Printf("Update Start failed %v", msg.parsed_message[2])
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
log.Printf("Update Start confirmed Updating Partition %v", msg.parsed_message[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
update_write_index := 0
|
||||||
|
// write update parts
|
||||||
|
for update_write_index < len(update) {
|
||||||
|
payload_buffer = make([]byte, 1024)
|
||||||
|
send_buffer = make([]byte, 1024)
|
||||||
|
payload_buffer[0] = UART_OTA_PAYLOAD
|
||||||
|
|
||||||
|
write_len := min(200, len(update)-update_write_index)
|
||||||
|
|
||||||
|
//end_payload_len := min(update_write_index+200, len(update))
|
||||||
|
|
||||||
|
copy(payload_buffer[1:write_len+1], update[update_write_index:update_write_index+write_len])
|
||||||
|
n = buildMessage(payload_buffer, write_len+1, send_buffer)
|
||||||
|
sendMessage(port, send_buffer[:n])
|
||||||
|
msg, err := OTA_UpdateHandler.WaitForNextMessageTimeout()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error Message not acked %v", err)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
seqCounter := binary.LittleEndian.Uint16(msg.parsed_message[1:3])
|
||||||
|
buff_write_index := binary.LittleEndian.Uint16(msg.parsed_message[3:5])
|
||||||
|
log.Printf("Sequenzce Counter: %d, Update buffer Write Index: %d", seqCounter, buff_write_index)
|
||||||
|
}
|
||||||
|
update_write_index += 200
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Update übertragen beende hier!!!")
|
||||||
|
// end
|
||||||
|
payload_buffer = make([]byte, 1024)
|
||||||
|
send_buffer = make([]byte, 1024)
|
||||||
|
payload_buffer[0] = UART_OTA_END
|
||||||
|
n = buildMessage(payload_buffer, 1, send_buffer)
|
||||||
|
sendMessage(port, send_buffer[:n])
|
||||||
|
|
||||||
|
_, err = OTA_UpdateHandler.WaitForNextMessageTimeout()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error Message not acked %v", err)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
log.Printf("Message Waiting hat funktionioert")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
var input string
|
||||||
|
_, err := fmt.Scanln(&input)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Could not read from stdin")
|
||||||
|
}
|
||||||
|
fmt.Printf("Input %v", input)
|
||||||
|
|
||||||
|
switch input {
|
||||||
|
case "q":
|
||||||
|
return
|
||||||
|
case "1":
|
||||||
|
payload_buffer := make([]byte, 1024)
|
||||||
|
send_buffer := make([]byte, 1024)
|
||||||
|
payload_buffer[0] = UART_ECHO
|
||||||
|
n := buildMessage(payload_buffer, 1, send_buffer)
|
||||||
|
sendMessage(port, send_buffer[:n])
|
||||||
|
break
|
||||||
|
case "2":
|
||||||
|
payload_buffer := make([]byte, 1024)
|
||||||
|
send_buffer := make([]byte, 1024)
|
||||||
|
payload_buffer[0] = UART_VERSION
|
||||||
|
n := buildMessage(payload_buffer, 1, send_buffer)
|
||||||
|
sendMessage(port, send_buffer[:n])
|
||||||
|
break
|
||||||
|
case "3":
|
||||||
|
payload_buffer := make([]byte, 1024)
|
||||||
|
send_buffer := make([]byte, 1024)
|
||||||
|
payload_buffer[0] = UART_CLIENT_INFO
|
||||||
|
n := buildMessage(payload_buffer, 1, send_buffer)
|
||||||
|
sendMessage(port, send_buffer[:n])
|
||||||
|
break
|
||||||
|
case "4": // start update
|
||||||
|
payload_buffer := make([]byte, 1024)
|
||||||
|
send_buffer := make([]byte, 1024)
|
||||||
|
payload_buffer[0] = UART_OTA_START
|
||||||
|
n := buildMessage(payload_buffer, 1, send_buffer)
|
||||||
|
sendMessage(port, send_buffer[:n])
|
||||||
|
break
|
||||||
|
case "5": // send payload
|
||||||
|
payload_buffer := make([]byte, 1024)
|
||||||
|
send_buffer := make([]byte, 1024)
|
||||||
|
payload_buffer[0] = UART_OTA_PAYLOAD
|
||||||
|
for i := range 200 {
|
||||||
|
payload_buffer[i+1] = byte(i)
|
||||||
|
}
|
||||||
|
n := buildMessage(payload_buffer, 201, send_buffer)
|
||||||
|
sendMessage(port, send_buffer[:n])
|
||||||
|
break
|
||||||
|
case "6": // end update
|
||||||
|
default:
|
||||||
|
fmt.Printf("Not a valid input")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+22
-1
@@ -1,3 +1,24 @@
|
|||||||
idf_component_register(SRCS "main.c" "uart_handler.c" "communication_handler.c" "uart_prot.c"
|
idf_component_register(SRCS "main.c" "uart_handler.c" "communication_handler.c" "client_handler.c" "message_parser.c" "message_builder.c" "message_handler.c" "ota_update.c"
|
||||||
INCLUDE_DIRS ".")
|
INCLUDE_DIRS ".")
|
||||||
|
|
||||||
|
# Get the short Git commit hash of the current HEAD.
|
||||||
|
# If not in a Git repository or git command fails, it will default to "N/A".
|
||||||
|
execute_process(
|
||||||
|
COMMAND git rev-parse --short HEAD
|
||||||
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
||||||
|
OUTPUT_VARIABLE GIT_COMMIT_HASH_SHORT
|
||||||
|
OUTPUT_STRIP_TRAILING_WHITESPACE
|
||||||
|
RESULT_VARIABLE GIT_HASH_RESULT
|
||||||
|
)
|
||||||
|
# Fallback if git is not available or not in a git repo
|
||||||
|
if(GIT_HASH_RESULT_CODE)
|
||||||
|
set(GIT_COMMIT_HASH_SHORT "N/A")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Add the Git hash as a preprocessor definition to your component.
|
||||||
|
# This makes BUILD_GIT_HASH available in your C/C++ source files.
|
||||||
|
target_compile_definitions(${COMPONENT_LIB} PRIVATE
|
||||||
|
BUILD_GIT_HASH="${GIT_COMMIT_HASH_SHORT}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
#include "client_handler.h"
|
||||||
|
#include "communication_handler.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int get_client_id(ClientList *list, const uint8_t *client_mac) {
|
||||||
|
for (int i = 0; i < MAX_CLIENTS; i++) {
|
||||||
|
if (memcmp(client_mac, list->Clients[i].macAddr, MAC_LENGTH) == 0) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return CLIENT_DOES_NOT_EXISTS;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Sanity check when list full then list->count should be MAX_CLIENTS
|
||||||
|
int get_next_free_slot(ClientList *list) {
|
||||||
|
for (int i = 0; i < MAX_CLIENTS; i++) {
|
||||||
|
// if slot is not used return index
|
||||||
|
if (!list->Clients[i].slotIsUsed) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// list is full
|
||||||
|
return CLIENT_LIST_FULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int add_client(ClientList *list, const uint8_t *client_mac) {
|
||||||
|
if (get_client_id(list, client_mac) >= 0) {
|
||||||
|
// Client already exists dont add to list
|
||||||
|
return CLIENT_EXISTS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int slot = get_next_free_slot(list);
|
||||||
|
if (slot < 0) {
|
||||||
|
// Client list full
|
||||||
|
return CLIENT_LIST_FULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
list->Clients[slot].slotIsUsed = true;
|
||||||
|
list->Clients[slot].isAvailable = true;
|
||||||
|
memcpy(list->Clients[slot].macAddr, client_mac, MAC_LENGTH);
|
||||||
|
list->ClientCount++;
|
||||||
|
return CLIENT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
int remove_client(ClientList *list, const uint8_t client_id) {
|
||||||
|
if (client_id >= MAX_CLIENTS)
|
||||||
|
return CLIENT_INVALID_ID; // invalid index
|
||||||
|
list->Clients[client_id].slotIsUsed = false;
|
||||||
|
list->ClientCount--;
|
||||||
|
return CLIENT_OK;
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
#ifndef CLIENT_HANDLER_H
|
||||||
|
#define CLIENT_HANDLER_H
|
||||||
|
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/_intsup.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#define MAX_CLIENTS 16
|
||||||
|
#define MAC_LENGTH 6
|
||||||
|
|
||||||
|
enum ClientErrors {
|
||||||
|
CLIENT_OK = 0,
|
||||||
|
CLIENT_EXISTS = -1,
|
||||||
|
CLIENT_DOES_NOT_EXISTS = -2,
|
||||||
|
CLIENT_LIST_FULL = -3,
|
||||||
|
CLIENT_INVALID_ID = -4,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
bool slotIsUsed;
|
||||||
|
bool isAvailable;
|
||||||
|
uint8_t clientID;
|
||||||
|
uint8_t macAddr[MAC_LENGTH];
|
||||||
|
TickType_t lastSuccessfullPing;
|
||||||
|
TickType_t lastPing;
|
||||||
|
uint16_t clientVersion;
|
||||||
|
} ClientInfo;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
ClientInfo Clients[MAX_CLIENTS];
|
||||||
|
uint8_t ClientCount;
|
||||||
|
} ClientList;
|
||||||
|
|
||||||
|
int get_client_id(ClientList *list, const uint8_t *client_mac);
|
||||||
|
int add_client(ClientList *list, const uint8_t *client_mac);
|
||||||
|
int remove_client(ClientList *list, const uint8_t clientid);
|
||||||
|
int get_next_free_slot(ClientList *list);
|
||||||
|
|
||||||
|
#endif
|
||||||
+357
-200
@@ -1,106 +1,171 @@
|
|||||||
|
#include "esp_err.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "esp_now.h"
|
||||||
#include "esp_timer.h"
|
#include "esp_timer.h"
|
||||||
#include "freertos/idf_additions.h"
|
#include "freertos/idf_additions.h"
|
||||||
|
|
||||||
|
#include "client_handler.h"
|
||||||
#include "communication_handler.h"
|
#include "communication_handler.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
static const char *TAG = "ALOX - COM";
|
static const char *TAG = "ALOX - COM";
|
||||||
|
|
||||||
QueueHandle_t messageQueue = NULL; // Warteschlange für empfangene Nachrichten
|
static struct ESP_MessageBroker mr;
|
||||||
ClientInfo clients[MAX_CLIENTS] = {
|
static QueueHandle_t ESP_recieved_message_queue;
|
||||||
0}; // Clients statisch initialisieren, alle auf 0 gesetzt
|
|
||||||
size_t numClients = 0;
|
|
||||||
size_t activeClients = 0;
|
|
||||||
bool hasMaster = false;
|
|
||||||
|
|
||||||
void init_com() {
|
void free_ESPNOW_MessageInfo(ESPNOW_MessageInfo *msg) {
|
||||||
// Initialisiere die Kommunikations-Warteschlange
|
if (msg->esp_now_info.src_addr) {
|
||||||
messageQueue = xQueueCreate(MESSAGE_QUEUE_SIZE, sizeof(BaseMessage));
|
free(msg->esp_now_info.src_addr);
|
||||||
if (messageQueue == NULL) {
|
msg->esp_now_info.src_addr = NULL;
|
||||||
ESP_LOGE(TAG, "Message queue creation failed");
|
}
|
||||||
|
if (msg->esp_now_info.des_addr) {
|
||||||
|
free(msg->esp_now_info.des_addr);
|
||||||
|
msg->esp_now_info.des_addr = NULL;
|
||||||
|
}
|
||||||
|
if (msg->esp_now_info.rx_ctrl) {
|
||||||
|
free(msg->esp_now_info.rx_ctrl);
|
||||||
|
msg->esp_now_info.rx_ctrl = NULL;
|
||||||
|
}
|
||||||
|
if (msg->data) {
|
||||||
|
free(msg->data);
|
||||||
|
msg->data = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Weitere Initialisierungen, falls nötig
|
|
||||||
numClients = 0;
|
|
||||||
activeClients = 0;
|
|
||||||
hasMaster = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// return any inactive client field for new usage
|
void ESP_InitMessageBroker(QueueHandle_t msg_queue_handle) {
|
||||||
int getNextFreeClientId() {
|
mr.num_direct_callbacks = 0;
|
||||||
for (int i = 0; i < MAX_CLIENTS; i++) {
|
mr.num_task_callbacks = 0;
|
||||||
if (!clients[i].isAvailable) {
|
ESP_recieved_message_queue = msg_queue_handle;
|
||||||
return i;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ESP_RegisterFunction(CommandPages command,
|
||||||
|
ESP_RegisterFunctionCallback callback) {
|
||||||
|
mr.FunctionList[mr.num_direct_callbacks].MSGID = command;
|
||||||
|
mr.FunctionList[mr.num_direct_callbacks].callback = callback;
|
||||||
|
mr.num_direct_callbacks++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ESP_RegisterTask(CommandPages command, ESP_RegisterTaskCallback callback) {
|
||||||
|
mr.TaskList[mr.num_task_callbacks].MSGID = command;
|
||||||
|
mr.TaskList[mr.num_task_callbacks].task = callback;
|
||||||
|
mr.num_task_callbacks++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ESP_MessageBrokerTask(void *param) {
|
||||||
|
ESPNOW_MessageInfo received_msg;
|
||||||
|
ESP_MessageBrokerTaskParams_t *task_params =
|
||||||
|
(ESP_MessageBrokerTaskParams_t *)param;
|
||||||
|
|
||||||
|
// Extrahiere die einzelnen Parameter
|
||||||
|
QueueHandle_t msg_queue = task_params->message_queue;
|
||||||
|
|
||||||
|
if (msg_queue == NULL) {
|
||||||
|
ESP_LOGE(TAG, "Message queue not initialized. Terminating task.");
|
||||||
|
vTaskDelete(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Message broker task started.");
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
if (xQueueReceive(msg_queue, &received_msg, portMAX_DELAY)) {
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Broker got message trying to relay it now");
|
||||||
|
const BaseMessage *message = (const BaseMessage *)received_msg.data;
|
||||||
|
ESP_LOGI(TAG, "Broker searching for command page %d",
|
||||||
|
message->commandPage);
|
||||||
|
for (int i = 0; i < mr.num_direct_callbacks;
|
||||||
|
i++) { // TODO: there should not be a loop needed here
|
||||||
|
if (mr.FunctionList[i].MSGID == message->commandPage) {
|
||||||
|
mr.FunctionList[i].callback(&received_msg.esp_now_info,
|
||||||
|
received_msg.data, received_msg.data_len);
|
||||||
|
ESP_LOGI(TAG, "Broker found matching msgid %d",
|
||||||
|
mr.FunctionList[i].MSGID);
|
||||||
|
free_ESPNOW_MessageInfo(&received_msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < mr.num_direct_callbacks; i++) {
|
||||||
|
// if (mr.FunctionList[i].MSGID == received_msg.msgid) {
|
||||||
|
// TODO: Not yet implemented
|
||||||
|
// Only send data to task, task should be created beforhead and wait
|
||||||
|
// for new data in the queue.
|
||||||
|
//}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_peer(uint8_t *macAddr) {
|
QueueHandle_t messageQueue = NULL; // Warteschlange für empfangene Nachrichten
|
||||||
|
static bool hasMaster = false;
|
||||||
|
static ClientList *esp_client_list;
|
||||||
|
static uint8_t channelNumber = 0;
|
||||||
|
|
||||||
|
#define MAC_STRING_BUFFER_SIZE 18
|
||||||
|
|
||||||
|
int init_com(ClientList *clients, uint8_t wifi_channel) {
|
||||||
|
// Initialisiere die Kommunikations-Warteschlange
|
||||||
|
messageQueue = xQueueCreate(MESSAGE_QUEUE_SIZE, sizeof(ESPNOW_MessageInfo));
|
||||||
|
if (messageQueue == NULL) {
|
||||||
|
ESP_LOGE(TAG, "Message queue creation failed");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_client_list = clients;
|
||||||
|
hasMaster = false;
|
||||||
|
channelNumber = wifi_channel;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int add_peer(uint8_t *macAddr) {
|
||||||
esp_now_peer_info_t peerInfo = {
|
esp_now_peer_info_t peerInfo = {
|
||||||
.channel =
|
.channel = channelNumber,
|
||||||
0, // Standardkanal, sollte uit den anderen Geräten übereinstimmen
|
|
||||||
.ifidx = ESP_IF_WIFI_STA,
|
.ifidx = ESP_IF_WIFI_STA,
|
||||||
.encrypt = false, // Keine Verschlüsselung (kann geändert werden)
|
.encrypt = false, // Keine Verschlüsselung // TODO: should be changed
|
||||||
};
|
};
|
||||||
memcpy(peerInfo.peer_addr, macAddr, ESP_NOW_ETH_ALEN);
|
memcpy(peerInfo.peer_addr, macAddr, ESP_NOW_ETH_ALEN);
|
||||||
|
|
||||||
esp_err_t result = esp_now_add_peer(&peerInfo);
|
esp_err_t result = esp_now_add_peer(&peerInfo);
|
||||||
if (result == ESP_OK) {
|
if (result == ESP_OK) {
|
||||||
ESP_LOGI(TAG, "Peer added: %02X:%02X:%02X:%02X:%02X:%02X",
|
ESP_LOGI(TAG, "Peer added: " MACSTR, MAC2STR(peerInfo.peer_addr));
|
||||||
peerInfo.peer_addr[0], peerInfo.peer_addr[1],
|
|
||||||
peerInfo.peer_addr[2], peerInfo.peer_addr[3],
|
|
||||||
peerInfo.peer_addr[4], peerInfo.peer_addr[5]);
|
|
||||||
|
|
||||||
if (!IS_BROADCAST_ADDR(macAddr)) {
|
if (!IS_BROADCAST_ADDR(macAddr)) {
|
||||||
|
int ret = add_client(esp_client_list, peerInfo.peer_addr);
|
||||||
if (numClients >= MAX_CLIENTS) {
|
if (ret < 0) {
|
||||||
ESP_LOGW(TAG, "Cannot add more clients, maximum reached.");
|
ESP_LOGE(TAG, "Client could not be added to client handler, removing "
|
||||||
return;
|
"it from esp now client list!");
|
||||||
|
esp_now_del_peer(peerInfo.peer_addr);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientInfo newClient = {};
|
|
||||||
memcpy(newClient.macAddr, macAddr, ESP_NOW_ETH_ALEN);
|
|
||||||
newClient.isAvailable = true;
|
|
||||||
newClient.lastSuccessfullPing = xTaskGetTickCount();
|
|
||||||
newClient.lastPing = 0;
|
|
||||||
clients[getNextFreeClientId()] = newClient;
|
|
||||||
ESP_LOGI(TAG, "New client added.");
|
ESP_LOGI(TAG, "New client added.");
|
||||||
}
|
}
|
||||||
} else if (result == ESP_ERR_ESPNOW_EXIST) {
|
} else if (result == ESP_ERR_ESPNOW_EXIST) {
|
||||||
ESP_LOGW(TAG, "Peer already exists.");
|
ESP_LOGW(TAG, "Peer already exists.");
|
||||||
// Überprüfen, ob der Client bereits existiert
|
int id = get_client_id(esp_client_list, peerInfo.peer_addr);
|
||||||
for (int i = 0; i < numClients; i++) {
|
if (id >= 0) {
|
||||||
if (memcmp(clients[i].macAddr, macAddr, ESP_NOW_ETH_ALEN) == 0) {
|
ESP_LOGI(TAG, "Client found again, welcome back!");
|
||||||
ESP_LOGI(TAG, "Client found again, welcome back!");
|
esp_client_list->Clients[id].isAvailable = true;
|
||||||
clients[i].isAvailable = true; // Reaktiviere den Client
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGE(TAG, "Failed to add peer: %s", esp_err_to_name(result));
|
ESP_LOGE(TAG, "Failed to add peer: %s", esp_err_to_name(result));
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
return 0;
|
||||||
|
|
||||||
// UNSAFE ACCROSS THREADS BUT EZ TO USE
|
|
||||||
const char *MACtoString(uint8_t *macAddr) {
|
|
||||||
static char output[18]; // 17 Zeichen + 1 für Nullterminierung
|
|
||||||
sprintf(output, "%02X:%02X:%02X:%02X:%02X:%02X", macAddr[0], macAddr[1],
|
|
||||||
macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
|
|
||||||
return output;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BaseMessage MessageBuilder(CommandPages commandPage, PayloadUnion payload,
|
BaseMessage MessageBuilder(CommandPages commandPage, PayloadUnion payload,
|
||||||
size_t payload_size) {
|
size_t payload_size) {
|
||||||
BaseMessage message;
|
BaseMessage message;
|
||||||
|
|
||||||
// Initialisierung der BaseMessage
|
|
||||||
message.commandPage = commandPage;
|
message.commandPage = commandPage;
|
||||||
message.version = 1;
|
message.version = 1;
|
||||||
message.length = (uint16_t)payload_size;
|
message.length = (uint16_t)payload_size;
|
||||||
|
|
||||||
// Kopieren des Payloads in die Union
|
memset(&message.payload, 0, sizeof(message.payload));
|
||||||
memset(&message.payload, 0, sizeof(message.payload)); // Sicherheitsmaßnahme
|
|
||||||
memcpy(&message.payload, &payload, payload_size);
|
memcpy(&message.payload, &payload, payload_size);
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
@@ -115,21 +180,21 @@ void master_broadcast_task(void *param) {
|
|||||||
|
|
||||||
ESP_ERROR_CHECK(esp_now_send(broadcast_address, (uint8_t *)&message,
|
ESP_ERROR_CHECK(esp_now_send(broadcast_address, (uint8_t *)&message,
|
||||||
sizeof(BaseMessage)));
|
sizeof(BaseMessage)));
|
||||||
ESP_LOGI(TAG, "Broadcast Message sent");
|
|
||||||
|
// ESP_LOGI(TAG, "Broadcast Message sent");
|
||||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void master_broadcast_ping(void *param) {
|
void master_broadcast_ping(void *param) {
|
||||||
while (1) {
|
while (1) {
|
||||||
// BroadCastPayload payload = {};
|
|
||||||
PingPayload payload = {};
|
PingPayload payload = {};
|
||||||
payload.timestamp = esp_timer_get_time();
|
payload.timestamp = esp_timer_get_time();
|
||||||
BaseMessage message =
|
BaseMessage message =
|
||||||
MessageBuilder(PingPage, *(PayloadUnion *)&payload, sizeof(payload));
|
MessageBuilder(PingPage, *(PayloadUnion *)&payload, sizeof(payload));
|
||||||
ESP_ERROR_CHECK(esp_now_send(broadcast_address, (uint8_t *)&message,
|
ESP_ERROR_CHECK(esp_now_send(broadcast_address, (uint8_t *)&message,
|
||||||
sizeof(BaseMessage)));
|
sizeof(BaseMessage)));
|
||||||
ESP_LOGI(TAG, "Broadcast PING Message sent");
|
// ESP_LOGI(TAG, "Broadcast PING Message sent");
|
||||||
vTaskDelay(pdMS_TO_TICKS(2500));
|
vTaskDelay(pdMS_TO_TICKS(2500));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -137,14 +202,14 @@ void master_broadcast_ping(void *param) {
|
|||||||
void master_ping_task(void *param) {
|
void master_ping_task(void *param) {
|
||||||
while (1) {
|
while (1) {
|
||||||
for (size_t i = 0; i < MAX_CLIENTS; i++) {
|
for (size_t i = 0; i < MAX_CLIENTS; i++) {
|
||||||
if (clients[i].isAvailable) {
|
if (esp_client_list->Clients[i].isAvailable) {
|
||||||
ESP_LOGI(TAG, "SEND PING TO %zu: %s", i,
|
ESP_LOGI(TAG, "SEND PING TO %zu: " MACSTR, i,
|
||||||
MACtoString(clients[i].macAddr));
|
MAC2STR(esp_client_list->Clients[i].macAddr));
|
||||||
PingPayload payload = {};
|
PingPayload payload = {};
|
||||||
payload.timestamp = esp_timer_get_time();
|
payload.timestamp = esp_timer_get_time();
|
||||||
BaseMessage message = MessageBuilder(
|
BaseMessage message = MessageBuilder(
|
||||||
PingPage, *(PayloadUnion *)&payload, sizeof(payload));
|
PingPage, *(PayloadUnion *)&payload, sizeof(payload));
|
||||||
esp_now_send(clients[i].macAddr, (uint8_t *)&message,
|
esp_now_send(esp_client_list->Clients[i].macAddr, (uint8_t *)&message,
|
||||||
sizeof(BaseMessage));
|
sizeof(BaseMessage));
|
||||||
ESP_LOGI(TAG, "SENDING PING!!!!");
|
ESP_LOGI(TAG, "SENDING PING!!!!");
|
||||||
}
|
}
|
||||||
@@ -153,172 +218,264 @@ void master_ping_task(void *param) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void master_StatusCallback(const esp_now_recv_info_t *esp_now_info,
|
||||||
|
const uint8_t *data, int data_len) {
|
||||||
|
const BaseMessage *message = (const BaseMessage *)data;
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "SRC " MACSTR, MAC2STR(esp_now_info->src_addr));
|
||||||
|
ESP_LOGI(TAG,
|
||||||
|
"Status Message Received: status: %d, runningPartition: %d, uptime: "
|
||||||
|
"%d, version: %d",
|
||||||
|
message->payload.status_payload.status,
|
||||||
|
message->payload.status_payload.runningPartition,
|
||||||
|
message->payload.status_payload.uptime,
|
||||||
|
message->payload.status_payload.version);
|
||||||
|
}
|
||||||
|
|
||||||
|
void master_RegisterCallback(const esp_now_recv_info_t *esp_now_info,
|
||||||
|
const uint8_t *data, int data_len) {
|
||||||
|
BaseMessage replyMessage = {};
|
||||||
|
const BaseMessage *message = (const BaseMessage *)data;
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "WILL REGISTER DEVICE");
|
||||||
|
esp_now_peer_info_t checkPeerInfo;
|
||||||
|
esp_err_t checkPeer =
|
||||||
|
esp_now_get_peer(esp_now_info->src_addr, &checkPeerInfo);
|
||||||
|
switch (checkPeer) {
|
||||||
|
case (ESP_OK):
|
||||||
|
ESP_LOGI(TAG, "CLIENT BEKANNT");
|
||||||
|
int id = get_client_id(esp_client_list, esp_now_info->src_addr);
|
||||||
|
esp_client_list->Clients[id].isAvailable = true;
|
||||||
|
esp_client_list->Clients[id].lastSuccessfullPing = xTaskGetTickCount();
|
||||||
|
ESP_LOGI(TAG, "Updated client %d last ping time to %lu", id,
|
||||||
|
esp_client_list->Clients[id].lastSuccessfullPing);
|
||||||
|
break;
|
||||||
|
case (ESP_ERR_ESPNOW_NOT_INIT):
|
||||||
|
ESP_LOGI(TAG, "Not initalised");
|
||||||
|
break;
|
||||||
|
case (ESP_ERR_ESPNOW_ARG):
|
||||||
|
ESP_LOGI(TAG, "ESP ERR ESPNOW_ARG");
|
||||||
|
break;
|
||||||
|
case (ESP_ERR_ESPNOW_NOT_FOUND):
|
||||||
|
ESP_LOGI(TAG, "CLIENT WIRD IN DIE LISTE AUFGENOMMEN " MACSTR,
|
||||||
|
MAC2STR(esp_now_info->src_addr));
|
||||||
|
int peer_err = add_peer(esp_now_info->src_addr);
|
||||||
|
if (peer_err < 0) {
|
||||||
|
ESP_LOGE(TAG, "Could not add ESP TO ClientList %d", peer_err);
|
||||||
|
}
|
||||||
|
ESP_LOGI(TAG, "FRAGE CLIENT STATUS AN");
|
||||||
|
|
||||||
|
GetStatusPayload payload = {};
|
||||||
|
replyMessage = MessageBuilder(GetStatusPage, *(PayloadUnion *)&payload,
|
||||||
|
sizeof(payload));
|
||||||
|
esp_err_t err = esp_now_send(esp_now_info->src_addr,
|
||||||
|
(uint8_t *)&replyMessage, sizeof(BaseMessage));
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
ESP_LOGE(TAG, "Could not send Message Error %s", esp_err_to_name(err));
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ESP_LOGI(TAG, "Unknown Message %i", checkPeer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void master_pingCallback(const esp_now_recv_info_t *esp_now_info,
|
||||||
|
const uint8_t *data, int data_len) {
|
||||||
|
BaseMessage replyMessage = {};
|
||||||
|
const BaseMessage *message = (const BaseMessage *)data;
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "GOT PING MESSAGE");
|
||||||
|
uint32_t currentTime = esp_timer_get_time();
|
||||||
|
uint32_t diff = currentTime - message->payload.ping_payload.timestamp;
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Start: %lu, End: %lu, Diff: %lu, Ping: %lu",
|
||||||
|
message->payload.ping_payload.timestamp, currentTime, diff,
|
||||||
|
diff / 1000); // ping in ms
|
||||||
|
|
||||||
|
int id = get_client_id(esp_client_list, esp_now_info->src_addr);
|
||||||
|
if (id >= 0) {
|
||||||
|
esp_client_list->Clients[id].lastSuccessfullPing = xTaskGetTickCount();
|
||||||
|
esp_client_list->Clients[id].lastPing = (diff / 1000);
|
||||||
|
ESP_LOGI(TAG, "Updated client %d: " MACSTR " last ping time to %lu", id,
|
||||||
|
MAC2STR(esp_now_info->src_addr),
|
||||||
|
esp_client_list->Clients[id].lastSuccessfullPing);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void master_broadcastCallback(const esp_now_recv_info_t *esp_now_info,
|
||||||
|
const uint8_t *data, int data_len) {
|
||||||
|
ESP_LOGI(TAG,
|
||||||
|
"Master should not recieve Broadcast is there another master "
|
||||||
|
"Calling got message from " MACSTR,
|
||||||
|
MAC2STR(esp_now_info->src_addr));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ESPNOW_RegisterMasterCallbacks() {
|
||||||
|
ESP_RegisterFunction(StatusPage, master_StatusCallback);
|
||||||
|
ESP_RegisterFunction(RegisterPage, master_RegisterCallback);
|
||||||
|
ESP_RegisterFunction(PingPage, master_pingCallback);
|
||||||
|
ESP_RegisterFunction(BroadCastPage, master_broadcastCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
void slave_broadcastCallback(const esp_now_recv_info_t *esp_now_info,
|
||||||
|
const uint8_t *data, int data_len) {
|
||||||
|
BaseMessage replyMessage = {};
|
||||||
|
const BaseMessage *message = (const BaseMessage *)data;
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "GOT BROADCAST MESSAGE");
|
||||||
|
if (!hasMaster) {
|
||||||
|
if (IS_BROADCAST_ADDR(esp_now_info->des_addr)) {
|
||||||
|
ESP_LOGI(TAG, "GOT BROADCAST MESSAGE ATTEMPTING TO REGISTER TO MASTER!");
|
||||||
|
add_peer(esp_now_info->src_addr);
|
||||||
|
replyMessage =
|
||||||
|
MessageBuilder(RegisterPage, *(PayloadUnion *)&message->payload,
|
||||||
|
sizeof(message->payload));
|
||||||
|
ESP_ERROR_CHECK(esp_now_send(esp_now_info->src_addr,
|
||||||
|
(uint8_t *)&replyMessage,
|
||||||
|
sizeof(BaseMessage)));
|
||||||
|
hasMaster = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ESP_LOGI(TAG, "Already have master wont register by the new one");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void slave_getstatusCallback(const esp_now_recv_info_t *esp_now_info,
|
||||||
|
const uint8_t *data, int data_len) {
|
||||||
|
BaseMessage replyMessage = {};
|
||||||
|
const BaseMessage *message = (const BaseMessage *)data;
|
||||||
|
|
||||||
|
StatusPayload payload = {
|
||||||
|
.status = 1,
|
||||||
|
.runningPartition = 1,
|
||||||
|
.uptime = 100,
|
||||||
|
.version = 0x0002,
|
||||||
|
|
||||||
|
};
|
||||||
|
replyMessage =
|
||||||
|
MessageBuilder(StatusPage, *(PayloadUnion *)&payload, sizeof(payload));
|
||||||
|
ESP_ERROR_CHECK(esp_now_send(esp_now_info->src_addr, (uint8_t *)&replyMessage,
|
||||||
|
sizeof(BaseMessage)));
|
||||||
|
}
|
||||||
|
void slave_pingCallback(const esp_now_recv_info_t *esp_now_info,
|
||||||
|
const uint8_t *data, int data_len) {
|
||||||
|
if (!hasMaster)
|
||||||
|
return;
|
||||||
|
|
||||||
|
BaseMessage replyMessage = {};
|
||||||
|
const BaseMessage *message = (const BaseMessage *)data;
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "GOT PING MESSAGE");
|
||||||
|
replyMessage = MessageBuilder(PingPage, *(PayloadUnion *)&message->payload,
|
||||||
|
sizeof(message->payload));
|
||||||
|
ESP_ERROR_CHECK(esp_now_send(esp_now_info->src_addr, (uint8_t *)&replyMessage,
|
||||||
|
sizeof(BaseMessage)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ESPNOW_RegisterSlaveCallbacks() {
|
||||||
|
ESP_RegisterFunction(BroadCastPage, slave_broadcastCallback);
|
||||||
|
ESP_RegisterFunction(GetStatusPage, slave_getstatusCallback);
|
||||||
|
ESP_RegisterFunction(PingPage, slave_pingCallback);
|
||||||
|
}
|
||||||
|
|
||||||
void master_receive_callback(const esp_now_recv_info_t *esp_now_info,
|
void master_receive_callback(const esp_now_recv_info_t *esp_now_info,
|
||||||
const uint8_t *data, int data_len) {
|
const uint8_t *data, int data_len) {
|
||||||
ESP_LOGI(TAG, "MASTER GOT MESSAGE");
|
ESP_LOGI(TAG, "MASTER GOT MESSAGE");
|
||||||
|
|
||||||
const BaseMessage *message = (const BaseMessage *)data;
|
// Allokiere Speicher für die Daten und kopiere sie
|
||||||
switch (message->commandPage) {
|
uint8_t *copied_data = (uint8_t *)malloc(data_len);
|
||||||
case StatusPage:
|
if (copied_data == NULL) {
|
||||||
ESP_LOGI(TAG, "GOT STATUS MESSAGE");
|
ESP_LOGE(TAG, "Failed to allocate memory for message data.");
|
||||||
break;
|
return;
|
||||||
case PingPage:
|
|
||||||
ESP_LOGI(TAG, "GOT PING MESSAGE");
|
|
||||||
uint32_t currentTime = esp_timer_get_time();
|
|
||||||
uint32_t diff = currentTime - message->payload.ping_payload.timestamp;
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Start: %lu, End: %lu, Diff: %lu, Ping: %lu",
|
|
||||||
message->payload.ping_payload.timestamp, currentTime, diff,
|
|
||||||
diff / 1000); // ping in ms
|
|
||||||
|
|
||||||
for (int i = 0; i < MAX_CLIENTS; i++) {
|
|
||||||
// Überprüfen, ob der Client existiert und die MAC-Adresse übereinstimmt
|
|
||||||
if (clients[i].isAvailable &&
|
|
||||||
memcmp(clients[i].macAddr, esp_now_info->src_addr,
|
|
||||||
ESP_NOW_ETH_ALEN) == 0) {
|
|
||||||
clients[i].lastSuccessfullPing = xTaskGetTickCount();
|
|
||||||
clients[i].lastPing = (diff/1000);
|
|
||||||
ESP_LOGI(TAG, "Updated client %d: %s last ping time to %lu", i,
|
|
||||||
MACtoString(clients[i].macAddr),
|
|
||||||
clients[i].lastSuccessfullPing);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case BroadCastPage:
|
|
||||||
ESP_LOGI(TAG, "MASTER SHOULD NOT GET BROADCAST MESSAGE, is there another "
|
|
||||||
"master calling?");
|
|
||||||
break;
|
|
||||||
case RegisterPage:
|
|
||||||
ESP_LOGI(TAG, "WILL REGISTER DEVICE");
|
|
||||||
esp_now_peer_info_t checkPeerInfo;
|
|
||||||
esp_err_t checkPeer =
|
|
||||||
esp_now_get_peer(esp_now_info->src_addr, &checkPeerInfo);
|
|
||||||
switch (checkPeer) {
|
|
||||||
case (ESP_OK):
|
|
||||||
ESP_LOGI(TAG, "CLIENT BEKANNT");
|
|
||||||
for (int i = 0; i < MAX_CLIENTS; i++) {
|
|
||||||
// client in liste wiederfinden
|
|
||||||
if (!clients[i].isAvailable &&
|
|
||||||
memcmp(clients[i].macAddr, esp_now_info->src_addr,
|
|
||||||
ESP_NOW_ETH_ALEN) == 0) {
|
|
||||||
clients[i].isAvailable = true;
|
|
||||||
clients[i].lastSuccessfullPing = xTaskGetTickCount();
|
|
||||||
ESP_LOGI(TAG, "Updated client %d last ping time to %lu", i,
|
|
||||||
clients[i].lastSuccessfullPing);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case (ESP_ERR_ESPNOW_NOT_INIT):
|
|
||||||
ESP_LOGI(TAG, "Not initalised");
|
|
||||||
break;
|
|
||||||
case (ESP_ERR_ESPNOW_ARG):
|
|
||||||
ESP_LOGI(TAG, "ESP ERR ESPNOW_ARG");
|
|
||||||
break;
|
|
||||||
case (ESP_ERR_ESPNOW_NOT_FOUND):
|
|
||||||
ESP_LOGI(TAG, "CLIENT WIRD IN DIE LISTE AUFGENOMMEN");
|
|
||||||
add_peer(esp_now_info->src_addr);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
ESP_LOGI(TAG, "Unknown Message %i", checkPeer);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
ESP_LOGI(TAG, "Unknown CommandPage %i", message->commandPage);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
memcpy(copied_data, data, data_len);
|
||||||
|
|
||||||
|
// Fülle die neue Struktur mit kopierten Daten
|
||||||
|
ESPNOW_MessageInfo msg_info;
|
||||||
|
msg_info.esp_now_info.src_addr = malloc(6);
|
||||||
|
if (msg_info.esp_now_info.src_addr) {
|
||||||
|
memcpy(msg_info.esp_now_info.src_addr, esp_now_info->src_addr, 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Speicher für des_addr kopieren
|
||||||
|
msg_info.esp_now_info.des_addr = malloc(6);
|
||||||
|
if (msg_info.esp_now_info.des_addr) {
|
||||||
|
memcpy(msg_info.esp_now_info.des_addr, esp_now_info->des_addr, 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
// rx_ctrl Struktur kopieren
|
||||||
|
msg_info.esp_now_info.rx_ctrl = malloc(sizeof(wifi_pkt_rx_ctrl_t));
|
||||||
|
if (msg_info.esp_now_info.rx_ctrl) {
|
||||||
|
memcpy(msg_info.esp_now_info.rx_ctrl, esp_now_info->rx_ctrl,
|
||||||
|
sizeof(wifi_pkt_rx_ctrl_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
msg_info.data = copied_data;
|
||||||
|
msg_info.data_len = data_len;
|
||||||
|
|
||||||
|
if (xQueueSend(ESP_recieved_message_queue, &msg_info, portMAX_DELAY) !=
|
||||||
|
pdPASS) {
|
||||||
|
// Fehlerbehandlung: Queue voll oder Senden fehlgeschlagen
|
||||||
|
ESP_LOGE(TAG, "Failed to send parsed message to queue.");
|
||||||
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void client_receive_callback(const esp_now_recv_info_t *esp_now_info,
|
void client_receive_callback(const esp_now_recv_info_t *esp_now_info,
|
||||||
const uint8_t *data, int data_len) {
|
const uint8_t *data, int data_len) {
|
||||||
ESP_LOGI(TAG, "SLAVE GOT MESSAGE");
|
ESP_LOGI(TAG, "SLAVE GOT MESSAGE");
|
||||||
ESP_LOGI(TAG, "Received message from: %02X:%02X:%02X:%02X:%02X:%02X",
|
ESP_LOGI(TAG, "Received message from: " MACSTR,
|
||||||
esp_now_info->src_addr[0], esp_now_info->src_addr[1],
|
MAC2STR(esp_now_info->src_addr));
|
||||||
esp_now_info->src_addr[2], esp_now_info->src_addr[3],
|
|
||||||
esp_now_info->src_addr[4], esp_now_info->src_addr[5]);
|
|
||||||
ESP_LOGI(TAG, "Message: %.*s", data_len, data);
|
|
||||||
|
|
||||||
BaseMessage replyMessage = {};
|
uint8_t *copied_data = (uint8_t *)malloc(data_len);
|
||||||
|
if (copied_data == NULL) {
|
||||||
const BaseMessage *message = (const BaseMessage *)data;
|
ESP_LOGE(TAG, "Failed to allocate memory for message data.");
|
||||||
switch (message->commandPage) {
|
return;
|
||||||
case StatusPage:
|
|
||||||
ESP_LOGI(TAG, "GOT STATUS MESSAGE");
|
|
||||||
break;
|
|
||||||
case PingPage:
|
|
||||||
ESP_LOGI(TAG, "GOT PING MESSAGE");
|
|
||||||
replyMessage = MessageBuilder(PingPage, *(PayloadUnion *)&message->payload,
|
|
||||||
sizeof(message->payload));
|
|
||||||
ESP_ERROR_CHECK(esp_now_send(
|
|
||||||
esp_now_info->src_addr, (uint8_t *)&replyMessage, sizeof(BaseMessage)));
|
|
||||||
break;
|
|
||||||
case BroadCastPage:
|
|
||||||
ESP_LOGI(TAG, "GOT BROADCAST MESSAGE");
|
|
||||||
if (!hasMaster) {
|
|
||||||
if (IS_BROADCAST_ADDR(esp_now_info->des_addr)) {
|
|
||||||
ESP_LOGI(TAG,
|
|
||||||
"GOT BROADCAST MESSAGE ATTEMPTING TO REGISTER TO MASTER!");
|
|
||||||
add_peer(esp_now_info->src_addr);
|
|
||||||
replyMessage =
|
|
||||||
MessageBuilder(RegisterPage, *(PayloadUnion *)&message->payload,
|
|
||||||
sizeof(message->payload));
|
|
||||||
ESP_ERROR_CHECK(esp_now_send(esp_now_info->src_addr,
|
|
||||||
(uint8_t *)&replyMessage,
|
|
||||||
sizeof(BaseMessage)));
|
|
||||||
hasMaster = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case RegisterPage:
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
ESP_LOGI(TAG, "GOT UNKONW MESSAGE");
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
memcpy(copied_data, data, data_len);
|
||||||
|
|
||||||
|
// Fülle die neue Struktur mit kopierten Daten
|
||||||
|
ESPNOW_MessageInfo msg_info;
|
||||||
|
memcpy(&msg_info.esp_now_info, esp_now_info, sizeof(esp_now_recv_info_t));
|
||||||
|
msg_info.data = copied_data;
|
||||||
|
msg_info.data_len = data_len;
|
||||||
|
|
||||||
|
if (xQueueSend(ESP_recieved_message_queue, &msg_info, portMAX_DELAY) !=
|
||||||
|
pdPASS) {
|
||||||
|
// Fehlerbehandlung: Queue voll oder Senden fehlgeschlagen
|
||||||
|
ESP_LOGE(TAG, "Failed to send parsed message to queue.");
|
||||||
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void client_data_sending_task(void *param) {
|
void client_data_sending_task(void *param) {
|
||||||
while (1) {
|
while (1) {
|
||||||
const char *dataToSend = "DATA:42";
|
const char *dataToSend = "DATA:42";
|
||||||
ESP_LOGI(TAG, "SEND DATA");
|
ESP_LOGI(TAG, "SEND DATA");
|
||||||
esp_now_send(NULL, (uint8_t *)dataToSend,
|
esp_now_send(NULL, (uint8_t *)dataToSend, strlen(dataToSend));
|
||||||
strlen(dataToSend)); // Sende Daten an Master
|
|
||||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void client_monitor_task(void *pvParameters) {
|
void client_monitor_task(void *pvParameters) {
|
||||||
TickType_t timeout_ticks =
|
TickType_t timeout_ticks = pdMS_TO_TICKS(CLIENT_TIMEOUT_MS);
|
||||||
pdMS_TO_TICKS(CLIENT_TIMEOUT_MS); // Timeout in Ticks
|
TickType_t interval_ticks = pdMS_TO_TICKS(CHECK_INTERVAL_MS);
|
||||||
TickType_t interval_ticks =
|
|
||||||
pdMS_TO_TICKS(CHECK_INTERVAL_MS); // Prüfintervall in Ticks
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
TickType_t now = xTaskGetTickCount(); // Aktuelle Zeit in Ticks
|
TickType_t now = xTaskGetTickCount();
|
||||||
|
|
||||||
for (int i = 0; i < MAX_CLIENTS; i++) {
|
for (int i = 0; i < MAX_CLIENTS; i++) {
|
||||||
if (clients[i].isAvailable) {
|
if (esp_client_list->Clients[i].isAvailable) {
|
||||||
TickType_t time_diff = now - clients[i].lastSuccessfullPing;
|
TickType_t time_diff =
|
||||||
|
now - esp_client_list->Clients[i].lastSuccessfullPing;
|
||||||
|
|
||||||
// Prüfen, ob der Client als "nicht verfügbar" markiert werden soll
|
|
||||||
if (time_diff > timeout_ticks) {
|
if (time_diff > timeout_ticks) {
|
||||||
clients[i].isAvailable = false;
|
esp_client_list->Clients[i].isAvailable = false;
|
||||||
ESP_LOGW(
|
ESP_LOGW(TAG, "Client %d (MAC: " MACSTR ") is unavailable",
|
||||||
TAG,
|
MAC2STR(esp_client_list->Clients[i].macAddr));
|
||||||
"Client %d (MAC: %02X:%02X:%02X:%02X:%02X:%02X) is unavailable",
|
|
||||||
i, clients[i].macAddr[0], clients[i].macAddr[1],
|
|
||||||
clients[i].macAddr[2], clients[i].macAddr[3],
|
|
||||||
clients[i].macAddr[4], clients[i].macAddr[5]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Task für das Prüfintervall anhalten
|
|
||||||
vTaskDelay(interval_ticks);
|
vTaskDelay(interval_ticks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientInfo *get_client_info(int entry) {
|
|
||||||
return &clients[entry];
|
|
||||||
}
|
|
||||||
|
|||||||
+107
-23
@@ -1,6 +1,7 @@
|
|||||||
#ifndef COMMUNICATION_HANDLER_H
|
#ifndef COMMUNICATION_HANDLER_H
|
||||||
#define COMMUNICATION_HANDLER_H
|
#define COMMUNICATION_HANDLER_H
|
||||||
|
|
||||||
|
#include "client_handler.h"
|
||||||
#include <esp_now.h>
|
#include <esp_now.h>
|
||||||
#include <esp_wifi.h>
|
#include <esp_wifi.h>
|
||||||
#include <freertos/FreeRTOS.h>
|
#include <freertos/FreeRTOS.h>
|
||||||
@@ -10,51 +11,103 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
#define BROADCAST_INTERVAL_MS 500
|
#define BROADCAST_INTERVAL_MS 500
|
||||||
|
|
||||||
#define CLIENT_TIMEOUT_MS 5000 // 5 Sekunden Timeout
|
#define CLIENT_TIMEOUT_MS 5000 // 5 Sekunden Timeout
|
||||||
#define CHECK_INTERVAL_MS 1000 // Jede Sekunde überprüfen
|
#define CHECK_INTERVAL_MS 1000 // Jede Sekunde überprüfen
|
||||||
static uint8_t broadcast_address[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF,
|
static uint8_t broadcast_address[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF,
|
||||||
0xFF, 0xFF, 0xFF};
|
0xFF, 0xFF, 0xFF};
|
||||||
#define IS_BROADCAST_ADDR(addr) \
|
#define IS_BROADCAST_ADDR(addr) \
|
||||||
(memcmp(addr, broadcast_address, ESP_NOW_ETH_ALEN) == 0)
|
(memcmp(addr, broadcast_address, ESP_NOW_ETH_ALEN) == 0)
|
||||||
|
|
||||||
#define MAX_CLIENTS 19
|
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
|
||||||
|
#define MACSTR "%02X:%02X:%02X:%02X:%02X:%02X"
|
||||||
|
|
||||||
#define MESSAGE_QUEUE_SIZE 10
|
#define MESSAGE_QUEUE_SIZE 10
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
BroadCastPage,
|
OTA_PREP_UPGRADE,
|
||||||
|
OTA_SEND_PAYLOAD,
|
||||||
|
OTA_WRITE_UPDATE_BUFFER,
|
||||||
|
OTA_SEND_MISSING,
|
||||||
|
OTA_UPDATE_INFO,
|
||||||
|
OTA_END_UPGRADE,
|
||||||
StatusPage,
|
StatusPage,
|
||||||
|
GetStatusPage,
|
||||||
|
ConfigPage,
|
||||||
PingPage,
|
PingPage,
|
||||||
|
BroadCastPage,
|
||||||
RegisterPage,
|
RegisterPage,
|
||||||
} CommandPages;
|
} CommandPages;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct __attribute__((packed)) {
|
||||||
uint32_t uptime;
|
|
||||||
|
} OTA_PREP_UPGRADE_Payload;
|
||||||
|
|
||||||
|
typedef struct __attribute__((packed)) {
|
||||||
|
} OTA_SEND_PAYLOAD_Payload;
|
||||||
|
typedef struct __attribute__((packed)) {
|
||||||
|
} OTA_WRITE_UPDATE_BUFFER_Payload;
|
||||||
|
typedef struct __attribute__((packed)) {
|
||||||
|
} OTA_SEND_MISSING_Payload;
|
||||||
|
typedef struct __attribute__((packed)) {
|
||||||
|
} OTA_UPDATE_INFO_Payload;
|
||||||
|
typedef struct __attribute__((packed)) {
|
||||||
|
} OTA_END_UPGRADE_Payload;
|
||||||
|
|
||||||
|
typedef struct __attribute__((packed)) {
|
||||||
|
uint16_t version; // software version
|
||||||
|
uint8_t runningPartition;
|
||||||
uint8_t status;
|
uint8_t status;
|
||||||
|
uint32_t uptime;
|
||||||
} StatusPayload;
|
} StatusPayload;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct __attribute__((packed)) {
|
||||||
|
} GetStatusPayload;
|
||||||
|
|
||||||
|
typedef struct __attribute__((packed)) {
|
||||||
|
uint8_t timeslot;
|
||||||
|
} ConfigPayload;
|
||||||
|
|
||||||
|
typedef struct __attribute__((packed)) {
|
||||||
uint32_t timestamp;
|
uint32_t timestamp;
|
||||||
} PingPayload;
|
} PingPayload;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct __attribute__((packed)) {
|
||||||
} BroadCastPayload;
|
} BroadCastPayload;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct __attribute__((packed)) {
|
||||||
bool familierClient;
|
bool familierClient;
|
||||||
} RegisterPayload;
|
} RegisterPayload;
|
||||||
|
|
||||||
typedef union {
|
// TODO: Check checksum fields
|
||||||
|
typedef struct __attribute__((packed)) {
|
||||||
|
uint16_t length; // length of complete firmware
|
||||||
|
uint8_t checksum; // checksum of firmware
|
||||||
|
} FirmwarePrepPayload;
|
||||||
|
|
||||||
|
// TODO: Check checksum fields
|
||||||
|
typedef struct __attribute__((packed)) {
|
||||||
|
uint8_t length;
|
||||||
|
uint8_t checksum;
|
||||||
|
uint32_t address;
|
||||||
|
uint8_t payload[240]; // TODO: need a way to figure out a safe value for this
|
||||||
|
} FirmwarePayload;
|
||||||
|
|
||||||
|
typedef union __attribute__((packed)) {
|
||||||
StatusPayload status_payload;
|
StatusPayload status_payload;
|
||||||
|
ConfigPayload config_payload;
|
||||||
PingPayload ping_payload;
|
PingPayload ping_payload;
|
||||||
BroadCastPayload broadcast_payload;
|
BroadCastPayload broadcast_payload;
|
||||||
RegisterPayload register_payload;
|
RegisterPayload register_payload;
|
||||||
|
FirmwarePrepPayload firmware_prep_payload;
|
||||||
|
FirmwarePayload firmware_payload;
|
||||||
} PayloadUnion;
|
} PayloadUnion;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct __attribute__((packed)) {
|
||||||
uint16_t version;
|
uint16_t version; // protcol version
|
||||||
CommandPages commandPage;
|
CommandPages commandPage;
|
||||||
uint16_t length;
|
uint16_t length;
|
||||||
PayloadUnion payload;
|
PayloadUnion payload;
|
||||||
@@ -63,18 +116,50 @@ typedef struct {
|
|||||||
static_assert(sizeof(BaseMessage) <= 255,
|
static_assert(sizeof(BaseMessage) <= 255,
|
||||||
"BaseMessage darf nicht größer als 255 sein");
|
"BaseMessage darf nicht größer als 255 sein");
|
||||||
|
|
||||||
typedef struct {
|
typedef void (*ESP_RegisterFunctionCallback)(
|
||||||
uint8_t macAddr[ESP_NOW_ETH_ALEN];
|
const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len);
|
||||||
int rssi;
|
typedef void (*ESP_RegisterTaskCallback)(
|
||||||
bool isAvailable;
|
const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len);
|
||||||
TickType_t lastSuccessfullPing;
|
|
||||||
TickType_t lastPing;
|
|
||||||
} ClientInfo;
|
|
||||||
|
|
||||||
void init_com();
|
struct ESP_RegisterdFunction {
|
||||||
|
CommandPages MSGID;
|
||||||
|
ESP_RegisterFunctionCallback callback;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ESP_RegisterdTask {
|
||||||
|
CommandPages MSGID;
|
||||||
|
ESP_RegisterTaskCallback task;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ESP_MessageBroker {
|
||||||
|
struct ESP_RegisterdFunction FunctionList[64];
|
||||||
|
uint8_t num_direct_callbacks;
|
||||||
|
struct ESP_RegisterdTask TaskList[64];
|
||||||
|
uint8_t num_task_callbacks;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
QueueHandle_t message_queue;
|
||||||
|
} ESP_MessageBrokerTaskParams_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
esp_now_recv_info_t esp_now_info;
|
||||||
|
uint8_t *data;
|
||||||
|
int data_len;
|
||||||
|
} ESPNOW_MessageInfo;
|
||||||
|
|
||||||
|
void ESP_InitMessageBroker(QueueHandle_t msg_queue_handle);
|
||||||
|
void ESP_RegisterFunction(CommandPages command,
|
||||||
|
ESP_RegisterFunctionCallback callback);
|
||||||
|
void ESP_RegisterTask(CommandPages command, ESP_RegisterTaskCallback callback);
|
||||||
|
void ESP_MessageBrokerTask(void *param);
|
||||||
|
|
||||||
|
void ESPNOW_RegisterMasterCallbacks();
|
||||||
|
void ESPNOW_RegisterSlaveCallbacks();
|
||||||
|
|
||||||
|
int init_com(ClientList *clients, uint8_t wifi_channel);
|
||||||
int getNextFreeClientId();
|
int getNextFreeClientId();
|
||||||
void add_peer(uint8_t *macAddr);
|
int add_peer(uint8_t *macAddr);
|
||||||
const char *MACtoString(uint8_t *macAddr);
|
|
||||||
BaseMessage MessageBuilder(CommandPages commandPage, PayloadUnion payload,
|
BaseMessage MessageBuilder(CommandPages commandPage, PayloadUnion payload,
|
||||||
size_t payload_size);
|
size_t payload_size);
|
||||||
|
|
||||||
@@ -87,8 +172,7 @@ void master_receive_callback(const esp_now_recv_info_t *esp_now_info,
|
|||||||
void client_receive_callback(const esp_now_recv_info_t *esp_now_info,
|
void client_receive_callback(const esp_now_recv_info_t *esp_now_info,
|
||||||
const uint8_t *data, int data_len);
|
const uint8_t *data, int data_len);
|
||||||
void client_data_sending_task(void *param);
|
void client_data_sending_task(void *param);
|
||||||
|
void client_send_random_data_task(void *param);
|
||||||
void client_monitor_task(void *pvParameters);
|
void client_monitor_task(void *pvParameters);
|
||||||
|
|
||||||
ClientInfo *get_client_info(int entry);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+246
-20
@@ -1,34 +1,163 @@
|
|||||||
|
#include "client_handler.h"
|
||||||
#include "driver/gpio.h"
|
#include "driver/gpio.h"
|
||||||
#include "driver/uart.h"
|
#include "driver/uart.h"
|
||||||
|
#include "esp_err.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "esp_log_buffer.h"
|
||||||
|
#include "esp_ota_ops.h"
|
||||||
|
#include "esp_partition.h"
|
||||||
#include "esp_phy_init.h"
|
#include "esp_phy_init.h"
|
||||||
#include "esp_rom_gpio.h"
|
#include "esp_rom_gpio.h"
|
||||||
#include "esp_timer.h"
|
#include "esp_timer.h"
|
||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
#include "freertos/idf_additions.h"
|
#include "freertos/idf_additions.h"
|
||||||
#include "hal/uart_types.h"
|
#include "hal/uart_types.h"
|
||||||
|
#include "message_handler.h"
|
||||||
|
#include "message_parser.h"
|
||||||
|
#include "nvs.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
|
|
||||||
#include "communication_handler.h"
|
#include "communication_handler.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "portmacro.h"
|
#include "ota_update.h"
|
||||||
#include "uart_handler.h"
|
#include "uart_handler.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include "message_builder.h"
|
||||||
|
#include "uart_msg_ids.h"
|
||||||
|
|
||||||
static const char *TAG = "ALOX - MAIN";
|
static const char *TAG = "ALOX - MAIN";
|
||||||
|
static const uint16_t version = 0x0001;
|
||||||
|
static uint8_t send_message_buffer[1024];
|
||||||
|
static uint8_t send_message_payload_buffer[512];
|
||||||
|
|
||||||
void SendClientInfoTask() {
|
static MessageBrokerTaskParams_t broker_task_params;
|
||||||
int clientId = 0;
|
static ESP_MessageBrokerTaskParams_t esp_broker_task_params;
|
||||||
while (1) {
|
|
||||||
ClientInfo info = *get_client_info(clientId);
|
ClientList clientList = {.Clients = {{0}}, .ClientCount = 0};
|
||||||
send_client_info(clientId, info.isAvailable, info.lastPing);
|
|
||||||
clientId += 1;
|
void echoCallback(uint8_t msgid, const uint8_t *payload, size_t payload_len,
|
||||||
clientId = clientId%20;
|
uint8_t *send_payload_buffer, size_t send_payload_buffer_size,
|
||||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
uint8_t *send_buffer, size_t send_buffer_size) {
|
||||||
|
ESP_LOGI(TAG, "Echo command 0x01...");
|
||||||
|
int len = build_message(UART_ECHO, payload, payload_len, send_buffer,
|
||||||
|
send_buffer_size);
|
||||||
|
if (len < 0) {
|
||||||
|
ESP_LOGE(TAG,
|
||||||
|
"Error Building UART Message: payload_len, %d, sendbuffer_size: "
|
||||||
|
"%d, mes_len(error): %d",
|
||||||
|
payload_len, send_buffer_size, len);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uart_write_bytes(MASTER_UART, send_buffer, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void versionCallback(uint8_t msgid, const uint8_t *payload, size_t payload_len,
|
||||||
|
uint8_t *send_payload_buffer,
|
||||||
|
size_t send_payload_buffer_size, uint8_t *send_buffer,
|
||||||
|
size_t send_buffer_size) {
|
||||||
|
ESP_LOGI(TAG, "Version command 0x02...");
|
||||||
|
size_t git_build_hash_len = strlen(BUILD_GIT_HASH);
|
||||||
|
size_t needed_buffer_size = 2 + git_build_hash_len;
|
||||||
|
|
||||||
|
if (send_payload_buffer_size < needed_buffer_size) {
|
||||||
|
ESP_LOGE(TAG, "send_payload_buffer to small size %d need %d",
|
||||||
|
send_payload_buffer_size, needed_buffer_size);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
send_payload_buffer[0] = (uint8_t)(version & 0xFF);
|
||||||
|
send_payload_buffer[1] = (uint8_t)((version >> 8) & 0xFF);
|
||||||
|
memcpy(&send_payload_buffer[2], &BUILD_GIT_HASH, git_build_hash_len);
|
||||||
|
|
||||||
|
int len = build_message(UART_VERSION, send_payload_buffer, needed_buffer_size,
|
||||||
|
send_buffer, send_buffer_size);
|
||||||
|
if (len < 0) {
|
||||||
|
ESP_LOGE(TAG,
|
||||||
|
"Error Building UART Message: payload_len, %d, sendbuffer_size: "
|
||||||
|
"%d, mes_len(error): %d",
|
||||||
|
payload_len, send_buffer_size, len);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
uart_write_bytes(MASTER_UART, send_buffer, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void clientInfoCallback(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len, uint8_t *send_payload_buffer,
|
||||||
|
size_t send_payload_buffer_size, uint8_t *send_buffer,
|
||||||
|
size_t send_buffer_size) {
|
||||||
|
ESP_LOGI(TAG, "Client Info Command 0x03...");
|
||||||
|
static uint8_t entryLength = 19;
|
||||||
|
uint8_t needed_buffer_size = 1 + entryLength * clientList.ClientCount;
|
||||||
|
|
||||||
|
if (send_payload_buffer_size < needed_buffer_size) {
|
||||||
|
ESP_LOGE(TAG, "send_payload_buffer to small size %d need %d",
|
||||||
|
send_payload_buffer_size, needed_buffer_size);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
send_payload_buffer[0] = clientList.ClientCount;
|
||||||
|
uint8_t offsetMult = 0;
|
||||||
|
|
||||||
|
uint8_t used_slots = 0;
|
||||||
|
for (int i = 0; i < MAX_CLIENTS; i++) {
|
||||||
|
if (clientList.Clients[i].slotIsUsed) {
|
||||||
|
used_slots++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t loop_sanity_counter = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_CLIENTS; i++) {
|
||||||
|
if (clientList.Clients[i].slotIsUsed) {
|
||||||
|
loop_sanity_counter++;
|
||||||
|
if (loop_sanity_counter > clientList.ClientCount) {
|
||||||
|
ESP_LOGE("SPECIAL",
|
||||||
|
"ERROR SANITY CHECK FAILED: loop_sanity_count: %d, "
|
||||||
|
"client_count: %d",
|
||||||
|
loop_sanity_counter, clientList.ClientCount);
|
||||||
|
}
|
||||||
|
size_t offset = 1 + (entryLength * offsetMult++);
|
||||||
|
|
||||||
|
ESP_LOGE("SPECIAL", "OFFSET %d", offset);
|
||||||
|
send_payload_buffer[offset] = i;
|
||||||
|
send_payload_buffer[offset + 1] = clientList.Clients[i].isAvailable;
|
||||||
|
send_payload_buffer[offset + 2] = clientList.Clients[i].slotIsUsed;
|
||||||
|
memcpy(&send_payload_buffer[offset + 3], clientList.Clients[i].macAddr,
|
||||||
|
MAC_LENGTH);
|
||||||
|
memcpy(&send_payload_buffer[offset + 9], &clientList.Clients[i].lastPing,
|
||||||
|
4);
|
||||||
|
memcpy(&send_payload_buffer[offset + 13],
|
||||||
|
&clientList.Clients[i].lastSuccessfullPing, 4);
|
||||||
|
memcpy(&send_payload_buffer[offset + 17],
|
||||||
|
&clientList.Clients[i].clientVersion, 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int len = build_message(UART_CLIENT_INFO, send_payload_buffer,
|
||||||
|
needed_buffer_size, send_buffer, send_buffer_size);
|
||||||
|
|
||||||
|
// ESP_LOG_BUFFER_HEX("SEND BUFFER: ", send_buffer, send_buffer_size);
|
||||||
|
|
||||||
|
if (len < 0) {
|
||||||
|
ESP_LOGE(TAG,
|
||||||
|
"Error Building UART Message: payload_len, %d, sendbuffer_size: "
|
||||||
|
"%d, mes_len(error): %d",
|
||||||
|
needed_buffer_size, send_buffer_size, len);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
uart_write_bytes(MASTER_UART, send_buffer, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
void app_main(void) {
|
void app_main(void) {
|
||||||
|
ESP_LOGI(TAG, "Starting Alox Powerpod Version %d Build: %s", version,
|
||||||
|
BUILD_GIT_HASH);
|
||||||
|
|
||||||
esp_err_t ret = nvs_flash_init();
|
esp_err_t ret = nvs_flash_init();
|
||||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
|
||||||
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||||
@@ -50,12 +179,11 @@ void app_main(void) {
|
|||||||
wifi_config_t wifi_config = {
|
wifi_config_t wifi_config = {
|
||||||
.sta =
|
.sta =
|
||||||
{
|
{
|
||||||
.channel = 1, // Kanal 1, stelle sicher, dass alle Geräte
|
.channel = 1,
|
||||||
// denselben Kanal verwenden
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||||
ESP_ERROR_CHECK(esp_wifi_start());
|
ESP_ERROR_CHECK(esp_wifi_start());
|
||||||
ESP_ERROR_CHECK(esp_now_init());
|
ESP_ERROR_CHECK(esp_now_init());
|
||||||
|
|
||||||
@@ -65,23 +193,121 @@ void app_main(void) {
|
|||||||
ESP_ERROR_CHECK(esp_now_register_recv_cb(client_receive_callback));
|
ESP_ERROR_CHECK(esp_now_register_recv_cb(client_receive_callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
init_com();
|
ret = init_com(&clientList, 1);
|
||||||
|
if (ret < 0) {
|
||||||
|
ESP_LOGE(TAG, "Could not Init ESP NOW Communication!");
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_partition_iterator_t partition_iter = esp_partition_find(
|
||||||
|
ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL);
|
||||||
|
|
||||||
|
while (partition_iter != NULL) {
|
||||||
|
const esp_partition_t *part1 = esp_partition_get(partition_iter);
|
||||||
|
ESP_LOGI(TAG, "Partition: %s, Address: %d, Size %d", part1->label,
|
||||||
|
part1->address, part1->size);
|
||||||
|
partition_iter = esp_partition_next(partition_iter);
|
||||||
|
}
|
||||||
|
|
||||||
|
const esp_partition_t *running = esp_ota_get_running_partition();
|
||||||
|
ESP_LOGI(TAG, "OTA: Running Partition: %s", running->label);
|
||||||
|
uint8_t ota_part_count = esp_ota_get_app_partition_count();
|
||||||
|
ESP_LOGI(TAG, "OTA: Got %d OTA Partitions", ota_part_count);
|
||||||
|
|
||||||
|
esp_ota_img_states_t ota_state;
|
||||||
|
if (esp_ota_get_state_partition(running, &ota_state) == ESP_OK) {
|
||||||
|
ESP_LOGI(TAG, "OTA: Partition State : %d", ota_state);
|
||||||
|
if (ota_state == ESP_OTA_IMG_PENDING_VERIFY) {
|
||||||
|
// run diagnostic function ...
|
||||||
|
bool diagnostic_is_ok = true; // TODO: a real function that checks if
|
||||||
|
// everything is running properly
|
||||||
|
if (diagnostic_is_ok) {
|
||||||
|
ESP_LOGI(
|
||||||
|
TAG,
|
||||||
|
"Diagnostics completed successfully! Continuing execution ...");
|
||||||
|
// esp_ota_mark_app_valid_cancel_rollback();
|
||||||
|
} else {
|
||||||
|
ESP_LOGE(
|
||||||
|
TAG,
|
||||||
|
"Diagnostics failed! Start rollback to the previous version ...");
|
||||||
|
// esp_ota_mark_app_invalid_rollback_and_reboot();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const char nvs_part_name[] = "nvs_data";
|
||||||
|
const char nvs_namespace_name[] = "saved_clients";
|
||||||
|
ret = nvs_flash_init_partition(nvs_part_name);
|
||||||
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
|
||||||
|
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||||
|
ESP_ERROR_CHECK(nvs_flash_erase_partition(nvs_part_name));
|
||||||
|
ret = nvs_flash_init_partition(nvs_part_name);
|
||||||
|
}
|
||||||
|
ESP_ERROR_CHECK(ret);
|
||||||
|
nvs_handle_t nt;
|
||||||
|
ESP_ERROR_CHECK(nvs_open_from_partition(nvs_part_name, nvs_namespace_name,
|
||||||
|
NVS_READWRITE, &nt));
|
||||||
|
|
||||||
|
int32_t outval;
|
||||||
|
ret = nvs_get_i32(nt, "test_entry", &outval);
|
||||||
|
if (ret == ESP_ERR_NVS_NOT_FOUND) {
|
||||||
|
ESP_ERROR_CHECK(nvs_set_i32(nt, "test_entry", 6969));
|
||||||
|
ESP_ERROR_CHECK(nvs_commit(nt));
|
||||||
|
ESP_LOGE(TAG, "Nichts im Flash gefunden hab was dahin geschrieben");
|
||||||
|
} else if (ret == ESP_OK) {
|
||||||
|
ESP_LOGE(TAG, "DAS WAR IM FLASH %d", outval);
|
||||||
|
}
|
||||||
|
nvs_close(nt);
|
||||||
|
|
||||||
|
QueueHandle_t espnow_message_queue =
|
||||||
|
xQueueCreate(10, sizeof(ESPNOW_MessageInfo));
|
||||||
|
ESP_InitMessageBroker(espnow_message_queue);
|
||||||
|
|
||||||
|
esp_broker_task_params.message_queue = espnow_message_queue;
|
||||||
|
xTaskCreate(ESP_MessageBrokerTask, "espnow_message_broker_task", 4096,
|
||||||
|
(void *)&esp_broker_task_params, 4, NULL);
|
||||||
|
|
||||||
// Tasks starten basierend auf Master/Client
|
// Tasks starten basierend auf Master/Client
|
||||||
if (isMaster) {
|
if (isMaster) {
|
||||||
ESP_LOGI(TAG, "Started in Mastermode");
|
ESP_LOGI(TAG, "Started in Mastermode");
|
||||||
|
ESPNOW_RegisterMasterCallbacks();
|
||||||
|
|
||||||
add_peer(broadcast_address);
|
add_peer(broadcast_address);
|
||||||
xTaskCreate(master_broadcast_task, "MasterBroadcast", 4096, NULL, 1, NULL);
|
xTaskCreate(master_broadcast_task, "MasterBroadcast", 4096, NULL, 1, NULL);
|
||||||
// xTaskCreate(master_ping_task, "MasterPing", 4096, NULL, 1, NULL);
|
// xTaskCreate(master_ping_task, "MasterPing", 4096, NULL, 1, NULL);
|
||||||
xTaskCreate(master_broadcast_ping, "MasterBroadcastPing", 4096, NULL, 1,
|
xTaskCreate(master_broadcast_ping, "MasterBroadcastPing", 4096, NULL, 1,
|
||||||
NULL);
|
NULL);
|
||||||
xTaskCreate(client_monitor_task, "MonitorClientTask", 4096, NULL, 1, NULL);
|
// xTaskCreate(client_monitor_task, "MonitorClientTask", 4096, NULL, 1,
|
||||||
init_uart();
|
// NULL);
|
||||||
//xTaskCreate(uart_status_task, "MasterUartStatusTask", 4096, NULL, 1, NULL);
|
|
||||||
xTaskCreate(SendClientInfoTask, "SendCientInfo", 4096, NULL, 1, NULL);
|
QueueHandle_t parsed_message_queue =
|
||||||
|
xQueueCreate(10, sizeof(ParsedMessage_t));
|
||||||
|
init_uart(parsed_message_queue);
|
||||||
|
InitMessageBroker();
|
||||||
|
|
||||||
|
// Initialisiere die Parameterstruktur
|
||||||
|
broker_task_params.message_queue = parsed_message_queue;
|
||||||
|
broker_task_params.send_buffer = send_message_buffer;
|
||||||
|
broker_task_params.send_buffer_size = sizeof(send_message_buffer);
|
||||||
|
broker_task_params.payload_buffer = send_message_payload_buffer;
|
||||||
|
broker_task_params.payload_buffer_size =
|
||||||
|
sizeof(send_message_payload_buffer);
|
||||||
|
|
||||||
|
xTaskCreate(MessageBrokerTask, "message_handler_task", 4096,
|
||||||
|
(void *)&broker_task_params, 5, NULL);
|
||||||
|
|
||||||
|
RegisterCallback(0x01, echoCallback);
|
||||||
|
RegisterCallback(0x02, versionCallback);
|
||||||
|
RegisterCallback(0x03, clientInfoCallback);
|
||||||
|
|
||||||
|
init_ota();
|
||||||
|
|
||||||
|
// xTaskCreate(uart_status_task, "MasterUartStatusTask", 4096, NULL, 1,
|
||||||
|
// NULL); xTaskCreate(SendClientInfoTask, "SendCientInfo", 4096, NULL, 1,
|
||||||
|
// NULL);
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGI(TAG, "Started in Slavemode");
|
ESP_LOGI(TAG, "Started in Slavemode");
|
||||||
xTaskCreate(client_data_sending_task, "ClientDataSending", 4096, NULL, 1,
|
ESPNOW_RegisterSlaveCallbacks();
|
||||||
NULL);
|
// xTaskCreate(client_data_sending_task, "ClientDataSending", 4096, NULL, 1,
|
||||||
|
// NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,10 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifdef CONFIG_IDF_TARGET_ESP32S3
|
||||||
|
#define MASTER_MODE_PIN GPIO_NUM_1 // Jumper-Erkennungspin
|
||||||
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
||||||
#define MASTER_MODE_PIN GPIO_NUM_0 // Jumper-Erkennungspin
|
#define MASTER_MODE_PIN GPIO_NUM_0 // Jumper-Erkennungspin
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
#include "message_builder.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "message_parser.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
bool needs_stuffing_byte(uint8_t byte) {
|
||||||
|
return (byte == StartByte || byte == EscapeByte || byte == EndByte);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool add_byte_with_length_check(uint8_t byte, size_t write_index, uint8_t *data,
|
||||||
|
size_t max_length) {
|
||||||
|
if (write_index >= max_length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
data[write_index] = byte;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int build_message(uint8_t msgid, const uint8_t *payload, size_t payload_len,
|
||||||
|
uint8_t *msg_buffer, size_t msg_buffer_size) {
|
||||||
|
|
||||||
|
//ESP_LOGE("BM", "payload_len %d, msg_buffer_size %d", payload_len + 4,
|
||||||
|
// msg_buffer_size);
|
||||||
|
if (payload_len + 4 > msg_buffer_size) {
|
||||||
|
return PayloadBiggerThenBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t checksum = 0;
|
||||||
|
size_t write_index = 0;
|
||||||
|
msg_buffer[write_index++] = StartByte;
|
||||||
|
|
||||||
|
if (needs_stuffing_byte(msgid)) {
|
||||||
|
if (!add_byte_with_length_check(EscapeByte, write_index, msg_buffer,
|
||||||
|
msg_buffer_size)) {
|
||||||
|
return BufferOverFlow;
|
||||||
|
}
|
||||||
|
write_index++;
|
||||||
|
}
|
||||||
|
if (!add_byte_with_length_check(msgid, write_index, msg_buffer,
|
||||||
|
msg_buffer_size)) {
|
||||||
|
return BufferOverFlow;
|
||||||
|
}
|
||||||
|
write_index++;
|
||||||
|
checksum ^= msgid;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < payload_len; i++) {
|
||||||
|
if (needs_stuffing_byte(payload[i])) {
|
||||||
|
if (!add_byte_with_length_check(EscapeByte, write_index, msg_buffer,
|
||||||
|
msg_buffer_size)) {
|
||||||
|
return BufferOverFlow;
|
||||||
|
}
|
||||||
|
write_index++;
|
||||||
|
}
|
||||||
|
if (!add_byte_with_length_check(payload[i], write_index, msg_buffer,
|
||||||
|
msg_buffer_size)) {
|
||||||
|
return BufferOverFlow;
|
||||||
|
}
|
||||||
|
|
||||||
|
write_index++;
|
||||||
|
checksum ^= payload[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (needs_stuffing_byte(checksum)) {
|
||||||
|
if (!add_byte_with_length_check(EscapeByte, write_index, msg_buffer,
|
||||||
|
msg_buffer_size)) {
|
||||||
|
return BufferOverFlow;
|
||||||
|
}
|
||||||
|
write_index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (write_index + 2 > msg_buffer_size) {
|
||||||
|
return BufferOverFlow;
|
||||||
|
}
|
||||||
|
msg_buffer[write_index++] = checksum;
|
||||||
|
msg_buffer[write_index++] = EndByte;
|
||||||
|
|
||||||
|
return write_index;
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef _MESSAGE_BUILDER_HEADER
|
||||||
|
#define _MESSAGE_BUILDER_HEADER
|
||||||
|
|
||||||
|
#include "message_parser.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
enum BuildMessageErrors {
|
||||||
|
NoBuildError = 0,
|
||||||
|
PayloadBiggerThenBuffer = -1,
|
||||||
|
BufferOverFlow = -2,
|
||||||
|
};
|
||||||
|
|
||||||
|
// returns the length of msg_buffer
|
||||||
|
int build_message(uint8_t msgid, const uint8_t *payload, size_t payload_len,
|
||||||
|
uint8_t *msg_buffer, size_t msg_buffer_length);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
#include "message_handler.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "freertos/idf_additions.h"
|
||||||
|
#include "uart_handler.h"
|
||||||
|
|
||||||
|
static struct MessageBroker mr;
|
||||||
|
static char *TAG = "ALOX - Message Handler";
|
||||||
|
|
||||||
|
void InitMessageBroker() {
|
||||||
|
mr.num_direct_callbacks = 0;
|
||||||
|
mr.num_task_callbacks = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RegisterCallback(uint8_t msgid, RegisterFunctionCallback callback) {
|
||||||
|
mr.FunctionList[mr.num_direct_callbacks].MSGID = msgid;
|
||||||
|
mr.FunctionList[mr.num_direct_callbacks].callback = callback;
|
||||||
|
mr.num_direct_callbacks++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RegisterTask(uint8_t msgid, RegisterTaskCallback callback) {
|
||||||
|
mr.TaskList[mr.num_task_callbacks].MSGID = msgid;
|
||||||
|
mr.TaskList[mr.num_task_callbacks].task = callback;
|
||||||
|
mr.num_task_callbacks++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MessageBrokerTask(void *param) {
|
||||||
|
ParsedMessage_t received_msg;
|
||||||
|
MessageBrokerTaskParams_t *task_params = (MessageBrokerTaskParams_t *)param;
|
||||||
|
|
||||||
|
// Extrahiere die einzelnen Parameter
|
||||||
|
QueueHandle_t msg_queue = task_params->message_queue;
|
||||||
|
uint8_t *send_message_buffer = task_params->send_buffer;
|
||||||
|
size_t send_message_buffer_size = task_params->send_buffer_size;
|
||||||
|
uint8_t *send_payload_buffer = task_params->payload_buffer;
|
||||||
|
size_t send_payload_buffer_size = task_params->payload_buffer_size;
|
||||||
|
|
||||||
|
if (msg_queue == NULL) {
|
||||||
|
ESP_LOGE(TAG, "Message queue not initialized. Terminating task.");
|
||||||
|
vTaskDelete(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Message broker task started.");
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
if (xQueueReceive(msg_queue, &received_msg, portMAX_DELAY)) {
|
||||||
|
//ESP_LOGI(TAG, "Received message from queue: MSGID=0x%02X, Length=%u",
|
||||||
|
// received_msg.msgid, received_msg.payload_len);
|
||||||
|
|
||||||
|
for (int i = 0; i < mr.num_direct_callbacks; i++) {
|
||||||
|
if (mr.FunctionList[i].MSGID == received_msg.msgid) {
|
||||||
|
mr.FunctionList[i].callback(
|
||||||
|
received_msg.msgid, received_msg.data, received_msg.payload_len,
|
||||||
|
send_payload_buffer, send_payload_buffer_size,
|
||||||
|
send_message_buffer, send_message_buffer_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < mr.num_direct_callbacks; i++) {
|
||||||
|
if (mr.FunctionList[i].MSGID == received_msg.msgid) {
|
||||||
|
// TODO: Not yet implemented
|
||||||
|
// Only send data to task, task should be created beforhead and wait
|
||||||
|
// for new data in the queue.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SendMessage(const uint8_t *buffer, size_t length);
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
#ifndef _MESSAGE_HANDLER_HEADER
|
||||||
|
#define _MESSAGE_HANDLER_HEADER
|
||||||
|
|
||||||
|
#include "freertos/idf_additions.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
QueueHandle_t message_queue;
|
||||||
|
uint8_t *send_buffer;
|
||||||
|
size_t send_buffer_size;
|
||||||
|
uint8_t *payload_buffer;
|
||||||
|
size_t payload_buffer_size;
|
||||||
|
} MessageBrokerTaskParams_t;
|
||||||
|
|
||||||
|
typedef void (*RegisterFunctionCallback)(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len,
|
||||||
|
uint8_t *send_payload_buffer,
|
||||||
|
size_t send_payload_buffer_size,
|
||||||
|
uint8_t *send_buffer,
|
||||||
|
size_t send_buffer_size);
|
||||||
|
|
||||||
|
typedef void (*RegisterTaskCallback)(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len,
|
||||||
|
uint8_t *send_payload_buffer,
|
||||||
|
size_t send_payload_buffer_size,
|
||||||
|
uint8_t *send_buffer,
|
||||||
|
size_t send_buffer_size);
|
||||||
|
|
||||||
|
struct RegisterdFunction {
|
||||||
|
uint8_t MSGID;
|
||||||
|
RegisterFunctionCallback callback;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RegisterdTask {
|
||||||
|
uint8_t MSGID;
|
||||||
|
RegisterTaskCallback task;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct MessageBroker {
|
||||||
|
struct RegisterdFunction FunctionList[64];
|
||||||
|
uint8_t num_direct_callbacks;
|
||||||
|
struct RegisterdTask TaskList[64];
|
||||||
|
uint8_t num_task_callbacks;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef void (*SendMessageHookCallback)(const uint8_t *buffer, size_t length);
|
||||||
|
|
||||||
|
void InitMessageBroker();
|
||||||
|
void RegisterCallback(uint8_t msgid, RegisterFunctionCallback callback);
|
||||||
|
void RegisterTask(uint8_t msgid, RegisterTaskCallback callback);
|
||||||
|
void SendMessage(const uint8_t *buffer, size_t length);
|
||||||
|
void MessageBrokerTask(void *param);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
#include "message_parser.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
MessageReceivedCallback on_message_received = NULL;
|
||||||
|
MessageFailCallback on_message_fail = NULL;
|
||||||
|
|
||||||
|
struct MessageReceive InitMessageReceive() {
|
||||||
|
struct MessageReceive mr = {
|
||||||
|
.state = WaitingForStartByte, // Startzustand des Parsers
|
||||||
|
.error = NoError, // Kein Fehler zu Beginn
|
||||||
|
.messageid = 0, // MSGID auf Standardwert setzen
|
||||||
|
// .message Array muss nicht explizit initialisiert werden, da es bei
|
||||||
|
// jedem Start geleert wird
|
||||||
|
.index = 0, // Index für das Nachrichten-Array initialisieren
|
||||||
|
.checksum = 0 // Checksumme initialisieren
|
||||||
|
};
|
||||||
|
return mr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Registrierungsfunktionen für die Callbacks
|
||||||
|
void register_message_callback(MessageReceivedCallback callback) {
|
||||||
|
on_message_received = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
void register_message_fail_callback(MessageFailCallback callback) {
|
||||||
|
on_message_fail = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
void parse_byte(struct MessageReceive *mr, uint8_t pbyte) {
|
||||||
|
switch (mr->state) {
|
||||||
|
case WaitingForStartByte:
|
||||||
|
if (pbyte == StartByte) {
|
||||||
|
mr->index = 0;
|
||||||
|
mr->checksum = 0;
|
||||||
|
mr->state = GetMessageType;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case EscapedMessageType:
|
||||||
|
mr->messageid = pbyte;
|
||||||
|
mr->checksum ^= pbyte;
|
||||||
|
mr->state = InPayload;
|
||||||
|
break;
|
||||||
|
case GetMessageType:
|
||||||
|
if (pbyte == EscapeByte) {
|
||||||
|
mr->state = EscapedMessageType;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (pbyte == StartByte || pbyte == EndByte) {
|
||||||
|
mr->state = WaitingForStartByte;
|
||||||
|
mr->error = UnexpectedCommandByte;
|
||||||
|
if (on_message_received) {
|
||||||
|
on_message_fail(mr->messageid, mr->message, mr->index, mr->error);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mr->messageid = pbyte;
|
||||||
|
mr->checksum ^= pbyte;
|
||||||
|
mr->state = InPayload;
|
||||||
|
break;
|
||||||
|
case EscapePayloadByte:
|
||||||
|
mr->message[mr->index++] = pbyte;
|
||||||
|
mr->checksum ^= pbyte;
|
||||||
|
mr->state = InPayload;
|
||||||
|
break;
|
||||||
|
case InPayload:
|
||||||
|
if (pbyte == EscapeByte) {
|
||||||
|
mr->state = EscapePayloadByte;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (pbyte == StartByte) {
|
||||||
|
mr->state = WaitingForStartByte;
|
||||||
|
mr->error = UnexpectedCommandByte;
|
||||||
|
if (on_message_received) {
|
||||||
|
on_message_fail(mr->messageid, mr->message, mr->index, mr->error);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (pbyte == EndByte) {
|
||||||
|
if (mr->checksum != 0x00) {
|
||||||
|
// Checksum failure
|
||||||
|
// The Checksum gets treated like a normal byte until the end byte
|
||||||
|
// accours. Therefore the last byte xor'ed to the checksum ist the
|
||||||
|
// checksum so the checksum must be Zero.
|
||||||
|
mr->state = WaitingForStartByte;
|
||||||
|
mr->error = WrongCheckSum;
|
||||||
|
if (on_message_received) {
|
||||||
|
on_message_fail(mr->messageid, mr->message, mr->index, mr->error);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (on_message_received) {
|
||||||
|
on_message_received(mr->messageid, mr->message,
|
||||||
|
mr->index - 1); // remove checksum byte by just
|
||||||
|
// setting the length of the message
|
||||||
|
}
|
||||||
|
mr->state = WaitingForStartByte;
|
||||||
|
}
|
||||||
|
if (mr->index < MAX_TOTAL_CONTENT_LENGTH) {
|
||||||
|
mr->message[mr->index++] = pbyte;
|
||||||
|
mr->checksum ^= pbyte;
|
||||||
|
} else {
|
||||||
|
mr->state = WaitingForStartByte;
|
||||||
|
mr->error = MessageToLong;
|
||||||
|
if (on_message_received) {
|
||||||
|
on_message_fail(mr->messageid, mr->message, mr->index, mr->error);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
#ifndef _MESSAGE_PARSER_HEADER
|
||||||
|
#define _MESSAGE_PARSER_HEADER
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define MAX_MESSAGE_PAYLOAD_LENGTH 512
|
||||||
|
#define MAX_TOTAL_CONTENT_LENGTH (MAX_MESSAGE_PAYLOAD_LENGTH + 1)
|
||||||
|
|
||||||
|
enum ParserState {
|
||||||
|
WaitingForStartByte,
|
||||||
|
GetMessageType,
|
||||||
|
EscapedMessageType,
|
||||||
|
EscapePayloadByte,
|
||||||
|
InPayload,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ParserError {
|
||||||
|
NoError,
|
||||||
|
WrongCheckSum,
|
||||||
|
MessageToLong,
|
||||||
|
UnexpectedCommandByte,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
StartByte = 0xAA,
|
||||||
|
EscapeByte = 0xBB,
|
||||||
|
EndByte = 0xCC,
|
||||||
|
} MessageBytes;
|
||||||
|
|
||||||
|
struct MessageReceive {
|
||||||
|
enum ParserState state;
|
||||||
|
enum ParserError error;
|
||||||
|
uint8_t messageid;
|
||||||
|
uint8_t message[MAX_MESSAGE_PAYLOAD_LENGTH];
|
||||||
|
uint16_t index;
|
||||||
|
uint8_t checksum;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef void (*MessageReceivedCallback)(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len);
|
||||||
|
typedef void (*MessageFailCallback)(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len, enum ParserError error);
|
||||||
|
|
||||||
|
struct MessageReceive InitMessageReceive();
|
||||||
|
|
||||||
|
void register_message_callback(MessageReceivedCallback callback);
|
||||||
|
void register_message_fail_callback(MessageFailCallback callback);
|
||||||
|
|
||||||
|
void parse_byte(struct MessageReceive *mr, uint8_t pbyte);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,215 @@
|
|||||||
|
#include "ota_update.h"
|
||||||
|
#include "driver/uart.h"
|
||||||
|
#include "esp_err.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "esp_ota_ops.h"
|
||||||
|
#include "esp_partition.h"
|
||||||
|
#include "esp_system.h"
|
||||||
|
#include "message_builder.h"
|
||||||
|
#include "message_handler.h"
|
||||||
|
#include "uart_handler.h"
|
||||||
|
#include "uart_msg_ids.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
||||||
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||||
|
|
||||||
|
static uint8_t update_buffer[UPDATE_BUFFER_SIZE];
|
||||||
|
static uint16_t update_buffer_write_index;
|
||||||
|
static uint32_t update_size;
|
||||||
|
static uint16_t sequenz_counter; // how often the update buffer gets written
|
||||||
|
static const char *TAG = "ALOX - OTA";
|
||||||
|
static esp_ota_handle_t update_handle;
|
||||||
|
|
||||||
|
int prepare_ota_update() {
|
||||||
|
const esp_partition_t *running = esp_ota_get_running_partition();
|
||||||
|
ESP_LOGI(TAG, "OTA: Running Partition: %s", running->label);
|
||||||
|
int part = 0;
|
||||||
|
|
||||||
|
char partition_to_update[] = "ota_0";
|
||||||
|
if (strcmp(running->label, "ota_0") == 0) {
|
||||||
|
strcpy(partition_to_update, "ota_1");
|
||||||
|
part = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const esp_partition_t *update_partition = esp_partition_find_first(
|
||||||
|
ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_ANY, partition_to_update);
|
||||||
|
|
||||||
|
// Check if the partition was found
|
||||||
|
if (update_partition == NULL) {
|
||||||
|
ESP_LOGE(TAG, "Failed to find OTA partition: %s", partition_to_update);
|
||||||
|
return -1; // Or handle the error appropriately
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Gonna write OTA Update in Partition: %s",
|
||||||
|
update_partition->label);
|
||||||
|
|
||||||
|
esp_err_t err =
|
||||||
|
esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
|
||||||
|
esp_ota_abort(update_handle);
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "OTA update started successfully.");
|
||||||
|
return part;
|
||||||
|
}
|
||||||
|
|
||||||
|
void start_uart_update(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len, uint8_t *send_payload_buffer,
|
||||||
|
size_t send_payload_buffer_size, uint8_t *send_buffer,
|
||||||
|
size_t send_buffer_size) {
|
||||||
|
ESP_LOGI(TAG, "OTA Update Start Uart Command");
|
||||||
|
|
||||||
|
vTaskPrioritySet(NULL, 2);
|
||||||
|
|
||||||
|
update_size = 0;
|
||||||
|
|
||||||
|
int part = prepare_ota_update();
|
||||||
|
|
||||||
|
// Message:
|
||||||
|
// byte partition
|
||||||
|
// byte error
|
||||||
|
|
||||||
|
if (part < 0) {
|
||||||
|
send_payload_buffer[1] = (part * -1) & 0xff;
|
||||||
|
} else {
|
||||||
|
send_payload_buffer[0] = part & 0xff;
|
||||||
|
}
|
||||||
|
|
||||||
|
int send_payload_len = 2;
|
||||||
|
int len = build_message(UART_OTA_START, send_payload_buffer, send_payload_len,
|
||||||
|
send_buffer, send_buffer_size);
|
||||||
|
if (len < 0) {
|
||||||
|
ESP_LOGE(TAG,
|
||||||
|
"Error Building UART Message: payload_len, %d, sendbuffer_size: "
|
||||||
|
"%d, mes_len(error): %d",
|
||||||
|
payload_len, send_buffer_size, len);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uart_write_bytes(MASTER_UART, send_buffer, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t write_ota_update(uint32_t write_len, const uint8_t *payload) {
|
||||||
|
if (update_buffer_write_index > UPDATE_BUFFER_SIZE - write_len) {
|
||||||
|
// ESP_LOGI(TAG, "Writing Data to Update BUffer Sequence %d, writing Data
|
||||||
|
// %d",
|
||||||
|
// sequenz_counter, write_len);
|
||||||
|
// write to ota
|
||||||
|
esp_err_t err =
|
||||||
|
esp_ota_write(update_handle, update_buffer, update_buffer_write_index);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
update_buffer_write_index = 0;
|
||||||
|
sequenz_counter++;
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(&update_buffer[update_buffer_write_index], payload, write_len);
|
||||||
|
update_buffer_write_index += write_len;
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
void payload_uart_update(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len, uint8_t *send_payload_buffer,
|
||||||
|
size_t send_payload_buffer_size, uint8_t *send_buffer,
|
||||||
|
size_t send_buffer_size) {
|
||||||
|
// ESP_LOGI(TAG, "OTA Update Payload Uart Command");
|
||||||
|
|
||||||
|
uint32_t write_len = MIN(UPDATE_PAYLOAD_SIZE, payload_len);
|
||||||
|
update_size += write_len;
|
||||||
|
|
||||||
|
esp_err_t err = write_ota_update(write_len, payload);
|
||||||
|
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
ESP_LOGE(TAG, "GOT ESP ERROR WRITE OTA %d", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t send_payload_len = 4;
|
||||||
|
memcpy(send_payload_buffer, &sequenz_counter, 2);
|
||||||
|
memcpy(&send_payload_buffer[2], &update_buffer_write_index, 2);
|
||||||
|
send_payload_buffer[4] = 0x00; // error
|
||||||
|
|
||||||
|
int len = build_message(UART_OTA_PAYLOAD, send_payload_buffer,
|
||||||
|
send_payload_len, send_buffer, send_buffer_size);
|
||||||
|
if (len < 0) {
|
||||||
|
ESP_LOGE(TAG,
|
||||||
|
"Error Building UART Message: payload_len, %d, sendbuffer_size: "
|
||||||
|
"%d, mes_len(error): %d",
|
||||||
|
payload_len, send_buffer_size, len);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uart_write_bytes(MASTER_UART, send_buffer, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t end_ota_update() {
|
||||||
|
esp_err_t err =
|
||||||
|
esp_ota_write(update_handle, update_buffer, update_buffer_write_index);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
ESP_LOGE(TAG, "GOT ESP ERROR WRITE OTA %d", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
err = esp_ota_end(update_handle);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
ESP_LOGE(TAG, "GOT ESP ERROR WRITE OTA %d", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGE(TAG, "UPDATE ENDE UPDATGE SIZE SIND %d BYTES", update_size);
|
||||||
|
|
||||||
|
// Hol dir die zuletzt geschriebene Partition
|
||||||
|
const esp_partition_t *partition = esp_ota_get_next_update_partition(NULL);
|
||||||
|
if (partition == NULL) {
|
||||||
|
ESP_LOGE(TAG, "Failed to get updated partition");
|
||||||
|
err = ESP_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setze sie als Boot-Partition
|
||||||
|
ESP_LOGE(TAG, "Setzte nächste Partition auf %s", partition->label);
|
||||||
|
err = esp_ota_set_boot_partition(partition);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed: %s",
|
||||||
|
esp_err_to_name(err));
|
||||||
|
}
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
void end_uart_update(uint8_t msgid, const uint8_t *payload, size_t payload_len,
|
||||||
|
uint8_t *send_payload_buffer,
|
||||||
|
size_t send_payload_buffer_size, uint8_t *send_buffer,
|
||||||
|
size_t send_buffer_size) {
|
||||||
|
ESP_LOGI(TAG, "OTA Update End Uart Command");
|
||||||
|
|
||||||
|
esp_err_t err = end_ota_update();
|
||||||
|
|
||||||
|
// message ret esp_err_t
|
||||||
|
int send_payload_len = 1;
|
||||||
|
send_payload_buffer[0] = err & 0xff;
|
||||||
|
int len = build_message(UART_OTA_END, send_payload_buffer, send_payload_len,
|
||||||
|
send_buffer, send_buffer_size);
|
||||||
|
if (len < 0) {
|
||||||
|
ESP_LOGE(TAG,
|
||||||
|
"Error Building UART Message: payload_len, %d, sendbuffer_size: "
|
||||||
|
"%d, mes_len(error): %d",
|
||||||
|
payload_len, send_buffer_size, len);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uart_write_bytes(MASTER_UART, send_buffer, len);
|
||||||
|
|
||||||
|
vTaskPrioritySet(NULL, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_ota_update_from_uart_task(void *param) {}
|
||||||
|
|
||||||
|
void init_ota() {
|
||||||
|
RegisterCallback(UART_OTA_START, start_uart_update);
|
||||||
|
RegisterCallback(UART_OTA_PAYLOAD, payload_uart_update);
|
||||||
|
RegisterCallback(UART_OTA_END, end_uart_update);
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef OTA_UPDATE_H
|
||||||
|
#define OTA_UPDATE_H
|
||||||
|
|
||||||
|
#include "esp_err.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#define UPDATE_BUFFER_SIZE 4000
|
||||||
|
#define UPDATE_PAYLOAD_SIZE 200
|
||||||
|
#define UPDATE_MAX_SEQUENZES (UPDATE_BUFFER_SIZE / UPDATE_PAYLOAD_SIZE)
|
||||||
|
|
||||||
|
void init_ota();
|
||||||
|
|
||||||
|
enum OTA_UPDATE_STATES {
|
||||||
|
IDEL,
|
||||||
|
START_REQUESTED,
|
||||||
|
WAITING_FOR_PAYLOAD,
|
||||||
|
WRITING_OTA_TO_PARTITION,
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
int prepare_ota_update();
|
||||||
|
esp_err_t write_ota_update(uint32_t write_len, const uint8_t *payload);
|
||||||
|
esp_err_t end_ota_update();
|
||||||
|
|
||||||
|
#endif
|
||||||
+44
-24
@@ -1,20 +1,24 @@
|
|||||||
#include "driver/gpio.h"
|
#include "driver/gpio.h"
|
||||||
#include "driver/uart.h"
|
#include "driver/uart.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "esp_log_buffer.h"
|
||||||
#include "freertos/idf_additions.h"
|
#include "freertos/idf_additions.h"
|
||||||
#include "hal/uart_types.h"
|
#include "hal/uart_types.h"
|
||||||
|
#include "message_handler.h"
|
||||||
|
#include "message_parser.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
#include "portmacro.h"
|
#include "portmacro.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "message_parser.h"
|
||||||
#include "uart_handler.h"
|
#include "uart_handler.h"
|
||||||
#include "uart_prot.h"
|
|
||||||
|
|
||||||
static const char *TAG = "ALOX - UART";
|
static const char *TAG = "ALOX - UART";
|
||||||
|
static QueueHandle_t parsed_message_queue;
|
||||||
|
|
||||||
void init_uart() {
|
void init_uart(QueueHandle_t msg_queue_handle) {
|
||||||
uart_config_t uart_config = {.baud_rate = 115200,
|
uart_config_t uart_config = {.baud_rate = 921600, // 921600, 115200
|
||||||
.data_bits = UART_DATA_8_BITS,
|
.data_bits = UART_DATA_8_BITS,
|
||||||
.parity = UART_PARITY_DISABLE,
|
.parity = UART_PARITY_DISABLE,
|
||||||
.stop_bits = UART_STOP_BITS_1,
|
.stop_bits = UART_STOP_BITS_1,
|
||||||
@@ -25,12 +29,16 @@ void init_uart() {
|
|||||||
uart_set_pin(MASTER_UART, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE,
|
uart_set_pin(MASTER_UART, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE,
|
||||||
UART_PIN_NO_CHANGE);
|
UART_PIN_NO_CHANGE);
|
||||||
|
|
||||||
message_handler_init();
|
parsed_message_queue = msg_queue_handle;
|
||||||
|
register_message_callback(HandleMessageReceivedCallback);
|
||||||
|
register_message_fail_callback(HandleMessageFailCallback);
|
||||||
|
|
||||||
xTaskCreate(uart_read_task, "Read Uart", 4096, NULL, 1, NULL);
|
xTaskCreate(uart_read_task, "Read Uart", 4096, NULL, 1, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void uart_read_task(void *param) {
|
void uart_read_task(void *param) {
|
||||||
QueueHandle_t inputQueue = message_handler_get_input_queue();
|
// Send all Input from Uart to the Message Handler for Parsing
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
uint8_t *data = (uint8_t *)malloc(BUF_SIZE);
|
uint8_t *data = (uint8_t *)malloc(BUF_SIZE);
|
||||||
int len = 0;
|
int len = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
@@ -39,32 +47,44 @@ void uart_read_task(void *param) {
|
|||||||
uart_read_bytes(MASTER_UART, data, BUF_SIZE, (20 / portTICK_PERIOD_MS));
|
uart_read_bytes(MASTER_UART, data, BUF_SIZE, (20 / portTICK_PERIOD_MS));
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
for (int i = 0; i < len; ++i) {
|
for (int i = 0; i < len; ++i) {
|
||||||
BaseType_t res = xQueueSend(inputQueue, &data[i], 0);
|
parse_byte(&mr, data[i]);
|
||||||
if (res == errQUEUE_FULL) {
|
|
||||||
ESP_LOGW(TAG, "inputQueue full");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void uart_status_task(void *param) {
|
// TODO: Remove this? or handle message sending in any other way reduce
|
||||||
while (1) {
|
// abstraction hell
|
||||||
uart_write_bytes(MASTER_UART, "c1,status,0\n\r", sizeof("c1,status,0\n\r"));
|
void send_message_hook(const uint8_t *buffer, size_t length) {
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
uart_write_bytes(MASTER_UART, buffer, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
void HandleMessageReceivedCallback(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len) {
|
||||||
|
/*ESP_LOGI(TAG, "GOT UART MESSAGE MSGID: %02X, Len: %u bytes \nMSG: ", msgid,
|
||||||
|
payload_len, payload);
|
||||||
|
ESP_LOG_BUFFER_HEX(TAG, payload, payload_len);*/
|
||||||
|
|
||||||
|
ParsedMessage_t msg_to_send;
|
||||||
|
msg_to_send.msgid = msgid;
|
||||||
|
msg_to_send.payload_len = payload_len;
|
||||||
|
memcpy(msg_to_send.data, payload, payload_len);
|
||||||
|
|
||||||
|
if (xQueueSend(parsed_message_queue, &msg_to_send, portMAX_DELAY) != pdPASS) {
|
||||||
|
// Fehlerbehandlung: Queue voll oder Senden fehlgeschlagen
|
||||||
|
ESP_LOGE(TAG, "Failed to send parsed message to queue.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_client_info(int clientid, bool isAvailable, TickType_t lastPing) {
|
void HandleMessageFailCallback(uint8_t msgid, const uint8_t *payload,
|
||||||
char buf[128];
|
size_t payload_len, enum ParserError error) {
|
||||||
snprintf(buf, sizeof(buf), "c%d,status,2,%d,%u\r\n", clientid,
|
ESP_LOGE(
|
||||||
isAvailable ? 1 : 0, (unsigned int)lastPing);
|
TAG,
|
||||||
uart_write_bytes(MASTER_UART, buf, strlen(buf));
|
"UART MESSAGE Parsing Failed MSGID: %02X, Len: %u, ERROR: %X, \nMSG: ",
|
||||||
}
|
msgid, payload_len, error);
|
||||||
|
ESP_LOG_BUFFER_HEX(TAG, payload, payload_len);
|
||||||
|
|
||||||
void esp_send_message_hook(ESPTOPCBaseMessage *msg) {
|
return;
|
||||||
// serialize + send via UART
|
|
||||||
uint8_t buffer[128];
|
|
||||||
uart_write_bytes(UART_NUM_1, (const char *)buffer,
|
|
||||||
sizeof(ESPTOPCBaseMessage));
|
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-4
@@ -1,16 +1,36 @@
|
|||||||
#ifndef UART_HANDLER_H
|
#ifndef UART_HANDLER_H
|
||||||
#define UART_HANDLER_H
|
#define UART_HANDLER_H
|
||||||
|
|
||||||
|
#include "freertos/idf_additions.h"
|
||||||
|
#include "message_parser.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifdef CONFIG_IDF_TARGET_ESP32S3
|
||||||
|
#define MASTER_UART UART_NUM_1
|
||||||
|
#define TXD_PIN (GPIO_NUM_2)
|
||||||
|
#define RXD_PIN (GPIO_NUM_3)
|
||||||
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
||||||
#define MASTER_UART UART_NUM_1
|
#define MASTER_UART UART_NUM_1
|
||||||
#define TXD_PIN (GPIO_NUM_1)
|
#define TXD_PIN (GPIO_NUM_1)
|
||||||
#define RXD_PIN (GPIO_NUM_2)
|
#define RXD_PIN (GPIO_NUM_2)
|
||||||
|
#endif
|
||||||
|
|
||||||
#define BUF_SIZE (1024)
|
#define BUF_SIZE (256)
|
||||||
|
|
||||||
void init_uart();
|
typedef struct {
|
||||||
|
uint8_t msgid;
|
||||||
|
size_t payload_len;
|
||||||
|
uint8_t data[MAX_MESSAGE_PAYLOAD_LENGTH];
|
||||||
|
} ParsedMessage_t;
|
||||||
|
|
||||||
|
void init_uart(QueueHandle_t msg_queue_handle);
|
||||||
void uart_read_task(void *param);
|
void uart_read_task(void *param);
|
||||||
void uart_status_task(void *param);
|
void uart_send_task(void *param);
|
||||||
|
|
||||||
void send_client_info(int clientid, bool isAvailable, TickType_t lastPing);
|
void HandleMessageReceivedCallback(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len);
|
||||||
|
void HandleMessageFailCallback(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len, enum ParserError error);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef UART_MSG_IDS_H
|
||||||
|
#define UART_MSG_IDS_H
|
||||||
|
|
||||||
|
enum UART_MSG_IDS {
|
||||||
|
// MISC
|
||||||
|
UART_ECHO = 0x01,
|
||||||
|
UART_VERSION = 0x02,
|
||||||
|
UART_CLIENT_INFO = 0x03,
|
||||||
|
|
||||||
|
// OTA
|
||||||
|
UART_OTA_START = 0x10,
|
||||||
|
UART_OTA_PAYLOAD = 0x11,
|
||||||
|
UART_OTA_END = 0x12,
|
||||||
|
UART_OTA_STATUS = 0x13,
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
#include "uart_prot.h"
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define MSG_QUEUE_LEN 64
|
|
||||||
static QueueHandle_t input_queue;
|
|
||||||
static QueueHandle_t output_queue;
|
|
||||||
|
|
||||||
QueueHandle_t message_handler_get_input_queue(void) {
|
|
||||||
return input_queue;
|
|
||||||
}
|
|
||||||
|
|
||||||
QueueHandle_t message_handler_get_output_queue(void) {
|
|
||||||
return output_queue;
|
|
||||||
}
|
|
||||||
|
|
||||||
void message_handler_init(void) {
|
|
||||||
input_queue = xQueueCreate(MSG_QUEUE_LEN, sizeof(uint8_t));
|
|
||||||
output_queue = xQueueCreate(MSG_QUEUE_LEN, sizeof(uint8_t));
|
|
||||||
}
|
|
||||||
|
|
||||||
void message_handler_task(void *param) {
|
|
||||||
uint8_t byte;
|
|
||||||
while (1) {
|
|
||||||
if (xQueueReceive(input_queue, &byte, portMAX_DELAY)) {
|
|
||||||
// handle byte, check message recieve with start and stop byte length and crc
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Message Dispatcher
|
|
||||||
void dispatch_message(uint8_t msg_id, void *payload) {
|
|
||||||
switch (msg_id) {
|
|
||||||
case RequestPing:
|
|
||||||
if (on_request_ping)
|
|
||||||
on_request_ping((RequestPingPayload *)payload);
|
|
||||||
break;
|
|
||||||
case RequestStatus:
|
|
||||||
if (on_request_status)
|
|
||||||
on_request_status((RequestStatusPayload *)payload);
|
|
||||||
break;
|
|
||||||
case PrepareFirmwareUpdate:
|
|
||||||
if (on_prepare_firmware_update)
|
|
||||||
on_prepare_firmware_update((PrepareFirmwareUpdatePayload *)payload);
|
|
||||||
break;
|
|
||||||
case FirmwareUpdateLine:
|
|
||||||
if (on_firmware_update_line)
|
|
||||||
on_firmware_update_line((FirmwareUpdateLinePayload *)payload);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
// Unknown message
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generic Send Function
|
|
||||||
void send_message(ESP_TO_PC_MESSAGE_IDS msgid, PayloadUnion *payload) {
|
|
||||||
ESPTOPCBaseMessage mes;
|
|
||||||
mes.Version = 1;
|
|
||||||
mes.MessageID = msgid;
|
|
||||||
mes.Payload = *payload;
|
|
||||||
|
|
||||||
esp_send_message_hook(&mes);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sepzific Send Functions
|
|
||||||
void send_clients(uint8_t clientCount, uint32_t clientAvaiableBitMask) {
|
|
||||||
ClientsPayload payload;
|
|
||||||
|
|
||||||
// Payload-Daten zuweisen
|
|
||||||
payload.clientCount = clientCount;
|
|
||||||
payload.clientAvaiableBitMask = clientAvaiableBitMask;
|
|
||||||
|
|
||||||
// Nachricht senden
|
|
||||||
send_message(Clients, (PayloadUnion *)&payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
void send_status(uint8_t clientId, uint8_t *mac) {
|
|
||||||
StatusPayload payload;
|
|
||||||
|
|
||||||
// Payload-Daten zuweisen
|
|
||||||
payload.clientId = clientId;
|
|
||||||
memcpy(payload.mac, mac, 6);
|
|
||||||
|
|
||||||
// Nachricht senden
|
|
||||||
send_message(Status, (PayloadUnion *)&payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
void send_pong(uint8_t clientId, uint32_t ping) {
|
|
||||||
PongPayload payload;
|
|
||||||
|
|
||||||
// Payload-Daten zuweisen
|
|
||||||
payload.clientId = clientId;
|
|
||||||
payload.ping = ping;
|
|
||||||
|
|
||||||
// Nachricht senden
|
|
||||||
send_message(Pong, (PayloadUnion *)&payload);
|
|
||||||
}
|
|
||||||
@@ -1,100 +0,0 @@
|
|||||||
#ifndef _PROTO_HEADER
|
|
||||||
#define _PROTO_HEADER
|
|
||||||
|
|
||||||
#include "freertos/idf_additions.h"
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
void message_handler_init(void);
|
|
||||||
QueueHandle_t message_handler_get_input_queue(void);
|
|
||||||
QueueHandle_t message_handler_get_output_queue(void);
|
|
||||||
|
|
||||||
// MessageIDs
|
|
||||||
typedef enum {
|
|
||||||
RequestPing = 0xE1,
|
|
||||||
RequestStatus = 0xE2,
|
|
||||||
PrepareFirmwareUpdate = 0xF1,
|
|
||||||
FirmwareUpdateLine = 0xF2,
|
|
||||||
} PC_TO_ESP_MESSAGE_IDS;
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
Clients = 0xE1,
|
|
||||||
Status = 0xE2,
|
|
||||||
Pong = 0xD1,
|
|
||||||
} ESP_TO_PC_MESSAGE_IDS;
|
|
||||||
|
|
||||||
// Payloads for single Messages
|
|
||||||
typedef struct {
|
|
||||||
uint8_t clientId;
|
|
||||||
} RequestPingPayload;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t clientId;
|
|
||||||
} RequestStatusPayload;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
// empty payload
|
|
||||||
} PrepareFirmwareUpdatePayload;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t data[240];
|
|
||||||
} FirmwareUpdateLinePayload;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t clientCount;
|
|
||||||
uint32_t clientAvaiableBitMask;
|
|
||||||
} ClientsPayload;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t clientId;
|
|
||||||
uint8_t mac[6];
|
|
||||||
} StatusPayload;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t clientId;
|
|
||||||
uint32_t ping;
|
|
||||||
} PongPayload;
|
|
||||||
|
|
||||||
// Union for all the Payloads
|
|
||||||
typedef union {
|
|
||||||
RequestPingPayload request_ping;
|
|
||||||
RequestStatusPayload request_status;
|
|
||||||
PrepareFirmwareUpdatePayload prepare_firmware_update;
|
|
||||||
FirmwareUpdateLinePayload firmware_update_line;
|
|
||||||
ClientsPayload clients;
|
|
||||||
StatusPayload status;
|
|
||||||
PongPayload pong;
|
|
||||||
} PayloadUnion;
|
|
||||||
|
|
||||||
// Base Message that can hold all Payloads
|
|
||||||
typedef struct {
|
|
||||||
uint8_t Version;
|
|
||||||
PC_TO_ESP_MESSAGE_IDS MessageID;
|
|
||||||
uint8_t Length;
|
|
||||||
PayloadUnion Payload;
|
|
||||||
} PCTOESPBaseMessage;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t Version;
|
|
||||||
ESP_TO_PC_MESSAGE_IDS MessageID;
|
|
||||||
uint8_t Length;
|
|
||||||
PayloadUnion Payload;
|
|
||||||
} ESPTOPCBaseMessage;
|
|
||||||
|
|
||||||
// deklarierte Hook-Signatur
|
|
||||||
void esp_send_message_hook(ESPTOPCBaseMessage *msg);
|
|
||||||
|
|
||||||
// Generic Send Function Prototype
|
|
||||||
void send_message(ESP_TO_PC_MESSAGE_IDS msgid, PayloadUnion *payload);
|
|
||||||
|
|
||||||
// Spezific Send Functions Prototype
|
|
||||||
void send_clients(uint8_t clientCount, uint32_t clientAvaiableBitMask);
|
|
||||||
void send_status(uint8_t clientId, uint8_t *mac);
|
|
||||||
void send_pong(uint8_t clientId, uint32_t ping);
|
|
||||||
|
|
||||||
// Prototypes for Message Recieve Handler to be set in user code
|
|
||||||
void (*on_request_ping)(RequestPingPayload *);
|
|
||||||
void (*on_request_status)(RequestStatusPayload *);
|
|
||||||
void (*on_prepare_firmware_update)(PrepareFirmwareUpdatePayload *);
|
|
||||||
void (*on_firmware_update_line)(FirmwareUpdateLinePayload *);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -2,54 +2,123 @@
|
|||||||
|
|
||||||
## Struktur einer Nachricht
|
## Struktur einer Nachricht
|
||||||
|
|
||||||
0xAA = Startbyte
|
- Control Bytes:
|
||||||
checksum = XOR über alle Bytes (ohne Startbyte und Checksum-Byte)
|
- 0xAA = Startbyte
|
||||||
|
- 0xBB = EscapeByte
|
||||||
|
- 0xCC = EndByte
|
||||||
|
|
||||||
|
checksum = XOR über alle Bytes (ohne Control Bytes und Checksum-Byte)
|
||||||
|
|
||||||
## Nachrichtenaufbau (Message Frame)
|
Command, Payload und Checksum werden Escaped sollten sie einem Control Byte ensteprechend
|
||||||
|
| Startbyte | Command | Payload (variable) | Checksum | Endbyte |
|
||||||
[ Startbyte ] [ Length ] [ CommandPage ] [ Payload (variabel) ] [ Checksum ]
|
|-----------|---------|--------------------|----------|---------|
|
||||||
|
|
||||||
### Felder im Detail:
|
### Felder im Detail:
|
||||||
|
|
||||||
- **Length** (`uint8_t`):
|
- **Command** (`uint8_t`):
|
||||||
Gibt die Gesamtlänge der Nachricht **ab `CommandPage` bis einschließlich `Payload`** an.
|
Gibt an, welcher Nachrichtentyp gesendet wird.
|
||||||
|
|
||||||
- **CommandPage** (`uint8_t`):
|
|
||||||
Gibt an, welcher Nachrichtentyp oder Befehl gesendet wird.
|
|
||||||
|
|
||||||
- **Payload** (`variabel`):
|
- **Payload** (`variabel`):
|
||||||
Datenfeld mit variabler Länge, abhängig vom `CommandPage`.
|
Datenfeld mit variabler Länge, abhängig vom `Command`.
|
||||||
|
|
||||||
- **Checksum** (`uint8_t`):
|
- **Checksum** (`uint8_t`):
|
||||||
XOR über alle Bytes ab `Length` bis einschließlich `Payload`.
|
XOR über aller Bytes von `Command` und `Payload`.
|
||||||
|
|
||||||
### Nachrichten von PC zu ESP:
|
|
||||||
|
|
||||||
clientid: 0x00 für master, 0xFF für broadcast, ansonsten 0xA0-0xB3 // 19 Clients
|
|
||||||
|
|
||||||
### RequestPing 0xE1
|
|
||||||
Payload: byte: clientid
|
|
||||||
### RequestInfo 0xE2
|
|
||||||
Payload: byte: clientid
|
|
||||||
### RequestRestart 0xE3
|
|
||||||
Payload: byte: clientid
|
|
||||||
### PrepareFirmwareUpdate 0xF1
|
|
||||||
Payload: none
|
|
||||||
### FirmwareUpdateLine 0xF2
|
|
||||||
Payload: firmware line 240Bytes MAX
|
|
||||||
### ExecuteFirmwareUpdate 0xF3
|
|
||||||
Payload: none
|
|
||||||
|
|
||||||
### Nachrichten von ESP zu PC:
|
|
||||||
|
|
||||||
|
|
||||||
|
### Messages
|
||||||
|
|
||||||
---
|
Command:
|
||||||
|
- UART_ECHO = 0x01
|
||||||
|
- UART_VERSION = 0x02
|
||||||
|
- UART_CLIENT_INFO = 0x03
|
||||||
|
|
||||||
# Roadmap
|
Grundlegend sind alle Zahlenwerte im LittleEndian format!
|
||||||
- [ ] SEND STATUS OF DEVICE OVER UART
|
|
||||||
- [ ] CONFIGURE PEERS OVER MASTER
|
#### UART_ECHO:
|
||||||
- [ ] SAVE PIN CONFIG ON PEERS
|
|
||||||
|
- Send Message: AA 01 01 CC
|
||||||
|
- Message Received: AA 01 01 CC
|
||||||
|
|
||||||
|
Sendet zurück was geschickt wird.
|
||||||
|
|
||||||
|
#### UART_VERSION:
|
||||||
|
|
||||||
|
| Offset | Länge (Bytes) | Bezeichnung | Beschreibung |
|
||||||
|
|--------|---------------|-------------|------------------|
|
||||||
|
| 0 | 2 | Version | Software Version |
|
||||||
|
| 2 | 7 | BuildHash | Git Hash |
|
||||||
|
|
||||||
|
|
||||||
|
- Send Message: AA 02 02 CC
|
||||||
|
- Message Received: AA 02 01 00 33 62 35 36 30 37 39 6F CC
|
||||||
|
|
||||||
|
| Version | Buildhash |
|
||||||
|
|---------|-----------|
|
||||||
|
| 1 | 3b56078 |
|
||||||
|
|
||||||
|
|
||||||
|
Sendet die Version und den Buildhash vom Master zurück.
|
||||||
|
|
||||||
|
#### UART_CLIENT_INFO:
|
||||||
|
|
||||||
|
Das erste Datenbyte nach dem Commando gibt an wie viele Client Infos in dieser Nachricht vorhanden sind.
|
||||||
|
|
||||||
|
Danach teilt sich ein Eintrag wie Folgt auf:
|
||||||
|
| Offset | Länge (Bytes) | Bezeichnung | Beschreibung |
|
||||||
|
|--------|---------------|----------------------------|---------------------------------------------------------------|
|
||||||
|
| 0 | 1 | Client ID | Eindeutige ID des Clients. |
|
||||||
|
| 1 | 1 | Ist verfügbar | Boolean-Wert (0 = nein, 1 = ja), ob der Client verfügbar ist. |
|
||||||
|
| 2 | 1 | Slot genutzt | Boolean-Wert (0 = nein, 1 = ja), ob der Slot belegt ist. |
|
||||||
|
| 3 | 6 | MAC-Adresse | Die Hardware-Adresse des Clients. |
|
||||||
|
| 9 | 4 | Letzter Ping | Zeit in Millisekunden seit dem letzten Ping. |
|
||||||
|
| 13 | 4 | Letzter erfolgreicher Ping | Zeit in Millisekunden seit dem letzten erfolgreichen Ping. |
|
||||||
|
| 17 | 2 | Version | Versionsnummer des Clients. |
|
||||||
|
|
||||||
|
##### Ein Client
|
||||||
|
|
||||||
|
- Send Message: AA 03 03 CC
|
||||||
|
- Message Received: AA 03 01 00 01 01 50 78 7D 18 89 F8 34 00 00 00 61 1F 00 00 02 00 76 CC
|
||||||
|
|
||||||
|
| Client ID | Verfügbar | Genutzt | MAC-Adresse | Last Ping | Last Successful Ping | Version |
|
||||||
|
|-----------|-----------|---------|-------------------|-----------|----------------------|---------|
|
||||||
|
| 0 | 1 | 1 | 50:78:7D:18:89:F8 | 52 | 8033 | 2 |
|
||||||
|
|
||||||
|
##### Zwei Clients
|
||||||
|
|
||||||
|
- Send Message: AA 03 03 CC
|
||||||
|
- Message Received: AA 03 02 00 01 01 50 78 7D 18 89 F8 22 00 00 00 F4 2A 01 00 02 00 01 01 01 50 78 7D 18 0C B4 10 00 00 00 F1 2A 01 00 02 00 FE CC
|
||||||
|
|
||||||
|
| Client ID | Verfügbar | Genutzt | MAC-Adresse | Last Ping | Last Successful Ping | Version |
|
||||||
|
|-----------|-----------|---------|-------------------|-----------|----------------------|---------|
|
||||||
|
| 0 | 1 | 1 | 50:78:7D:18:89:F8 | 34 | 76532 | 2 |
|
||||||
|
| 1 | 1 | 1 | 50:78:7D:18:C:B4 | 16 | 76529 | 2 |
|
||||||
|
|
||||||
|
#### UART_CLIENT_INPUT:
|
||||||
|
|
||||||
|
Die Identifizierung wird hier anhand der vorher gesendeten ClientID gemacht also muss einmal vorher `UART_CLIENT_INFO` aufgerufen werden.
|
||||||
|
|
||||||
|
Das erste Datenbyte nach dem Commando gibt an wie viele Client Infos in dieser Nachricht vorhanden sind.
|
||||||
|
|
||||||
|
Danach teilt sich ein Eintrag wie Folgt auf:
|
||||||
|
| Offset | Länge (Bytes) | Bezeichnung | Beschreibung |
|
||||||
|
|--------|---------------|-------------|----------------------------------------------------------------------------------|
|
||||||
|
| 0 | 1 | Client ID | Eindeutige ID des Clients. |
|
||||||
|
| 1 | 4 | LageX | Float Wert von der X Lage. |
|
||||||
|
| 5 | 4 | LageY | Float Wert von der Y Lage. |
|
||||||
|
| 9 | 4 | InputMaske | Int32 Wert der als Bitmaske genutzt wird um bis zu 32 Boolische Werte anzugeben. |
|
||||||
|
|
||||||
|
Inputmaske:
|
||||||
|
Taster1, Taster2, IOError1, IOErro2, AkkuStand1, AkkuStand2 (2 Bit kodiert für 25%,50%,75%,100%), rest unbelegt, default 0
|
||||||
|
|
||||||
|
| Bit1 | Bit2 | Akkustand |
|
||||||
|
|------|------|-----------|
|
||||||
|
| 0 | 0 | 25% |
|
||||||
|
| 0 | 1 | 50% |
|
||||||
|
| 1 | 0 | 75% |
|
||||||
|
| 1 | 1 | 100% |
|
||||||
|
|
||||||
|
|
||||||
|
<div style="page-break-after: always;"></div>
|
||||||
|
|
||||||
# Machbarkeits-Studie
|
# Machbarkeits-Studie
|
||||||
|
|
||||||
@@ -177,3 +246,55 @@ techn. Anforderungen hinreichend gut umsetzen lassen.
|
|||||||
und ob diese den ursprünglichen Anforderungen entsprechen.
|
und ob diese den ursprünglichen Anforderungen entsprechen.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## OTA-Update Technische Umsetzung:
|
||||||
|
|
||||||
|
### Vorrausetzung:
|
||||||
|
- Update File steht bereit und ist unter 2MB groß.
|
||||||
|
- UART Verbindung steht
|
||||||
|
- ESP Funktioniert einwandfrei
|
||||||
|
- ESP Läuft auf Partition A, Partition B soll geupdated werden
|
||||||
|
|
||||||
|
### Erste Schritt:
|
||||||
|
- Update in 200Byte stücke zerhacken und stück für stück per UART an den Master schicken
|
||||||
|
- Uart Protokol hat schon eine fehlercheck für die Übertragung drinnen
|
||||||
|
- Firmware wird in Partition B geschrieben
|
||||||
|
- OTA API Validiert Firmware am ende
|
||||||
|
|
||||||
|
#### Hier könnte man schon einen Neustart machen und Validieren ob die Firmware für den Master läuft!
|
||||||
|
#### Denn angeblich kann man beim ESP die aktuell laufende Partition auslesen
|
||||||
|
|
||||||
|
### Zweiter Schritt:
|
||||||
|
- Master liest in 200Byte stücken die Firmware aus seiner Partition B aus
|
||||||
|
- Und schickt per Broadcast die ersten 20 Packete an die Clients
|
||||||
|
- Die clients haben 4KB Buffer vorgesehen wo sie die 20 Packete unterbringen können
|
||||||
|
- Master Forder Ack Bitmaske an zur Validierung das alle 20 Packete da sind
|
||||||
|
- Sollten in der Bitmaske zeilen fehlen gibt der Master per unicast die fehlenden Zeilen an die Entsprechend Clients erneut
|
||||||
|
- ESP NOW kümmert sich hier um die Datenintigrität
|
||||||
|
- Wenn alle ihre ersten 20 packete haben gibt der master das go und alle schreiben die ersten 20 packete weg.
|
||||||
|
- Der master aktuallisiert den fortschritt für alle clients -> dann kann man das auch abfragen per uart und hat eine Fortschrittsanzeige
|
||||||
|
- Alle melden sich zurück wenn sie fertig sind mit dem schreiben per ota und der buffer leer ist.
|
||||||
|
- Repeat bis alle Daten da sind
|
||||||
|
|
||||||
|
Hier hab ich mal grob gerechnet:
|
||||||
|
2MB in 200Byte Schritten -> 10.000 Packete
|
||||||
|
10.000 Packete in 20er Schritten -> 500 Sequencen
|
||||||
|
Retries und Acks mal aussen vor hab ich leider keinen richtigen anhaltspunkt wie lang das dauern kann.
|
||||||
|
Aber 500* ca 300ms => ist schonmal 150sekunden nur für das acken das die Packete da sind. Annahme hier das die maximal latenz beim Ping mit 16 Clients ca 300ms sind.
|
||||||
|
Entsprechend mit Daten und retries... ja kp, Gemini schätzt max 10min. Wird sich zeigen. Da addiert sich zu viel auf.
|
||||||
|
|
||||||
|
- Alle Clients validieren ihren firmware
|
||||||
|
- Sollte das bei einem nicht klappen muss man hier nochmal gucken ob man den ganzen process nochmal von vorne anstößt nur mit dem fehlenden client...
|
||||||
|
|
||||||
|
### Dritter Schritt:
|
||||||
|
- Alle Clients rebooten
|
||||||
|
- Clients geben rückmeldung ob das Update funktioniert
|
||||||
|
|
||||||
|
#### Hier müssten war noch entscheiden was passiert wenn das Update bei nur ein paar funktionert hat?
|
||||||
|
#### Was passiert wenn das Update garnicht funktioniert hat, behält der master dann auch seinen stand?
|
||||||
|
#### Entsprechend hätte man ihn vorher auch nicht neustarten dürfen
|
||||||
|
|
||||||
|
- Sollten alle Clients ihr go geben startet der Master auch neu
|
||||||
|
#### Wenn das Master Update jetzt fehlschlägt sagt er den clients bescheid und die booten auch wieder um?
|
||||||
|
|
||||||
|
Gibt halt noch ein zwar sachen die man sich überlegen muss aber ich denke den rest hab ich soweit ausgearbeitet
|
||||||
|
|||||||
+2098
File diff suppressed because it is too large
Load Diff
+2152
File diff suppressed because it is too large
Load Diff
+2357
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,3 @@
|
|||||||
|
Unity/*
|
||||||
|
test_runner
|
||||||
|
test_builder
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
CC=gcc
|
||||||
|
CFLAGS=-Wall -Wextra -I../main
|
||||||
|
SRC = Unity/src/unity.c test_parser.c \
|
||||||
|
../main/message_parser.c
|
||||||
|
TARGET = test_runner
|
||||||
|
|
||||||
|
.PHONY: test_builder
|
||||||
|
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
$(TARGET): $(SRC)
|
||||||
|
$(CC) $(CFLAGS) -o $@ $^
|
||||||
|
./$(TARGET)
|
||||||
|
|
||||||
|
test_builder: Unity/src/unity.c test_message_builder.c ../main/message_parser.c ../main/message_builder.c
|
||||||
|
$(CC) $(CFLAGS) -o $@ $^
|
||||||
|
@echo "--- Running Builder Tests ---"
|
||||||
|
./$(BUILDER_TEST_TARGET)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(TARGET)
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#include "Unity/src/unity.h"
|
||||||
|
|
||||||
|
void setUp(void) {} // optional
|
||||||
|
void tearDown(void) {} // optional
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
UNITY_BEGIN();
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
@@ -0,0 +1,433 @@
|
|||||||
|
#include "Unity/src/unity.h"
|
||||||
|
#include "message_builder.h" // Stellt sicher, dass deine Header-Datei message_parser.h die Definitionen für build_message, StartByte, EscapeByte, EndByte, MAX_MESSAGE_PAYLOAD_LENGTH und Fehlercodes enthält
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h> // Für memcpy, memset
|
||||||
|
|
||||||
|
// --- Globale Konstanten (Annahmen aus vorheriger Konversation) ---
|
||||||
|
// Wenn diese in message_parser.h nicht definiert sind, musst du sie hier
|
||||||
|
// definieren: #define StartByte 0xAA #define EscapeByte 0xBB #define EndByte
|
||||||
|
// 0xCC
|
||||||
|
|
||||||
|
// #define MAX_MESSAGE_PAYLOAD_LENGTH 250 // Beispiel: Max. Payload-Länge
|
||||||
|
|
||||||
|
// Fehlercodes für den Builder (sollten mit deiner build_message Implementierung
|
||||||
|
// übereinstimmen) #define BUILD_ERROR_BUFFER_TOO_SMALL_INITIAL_CHECK -1 #define
|
||||||
|
// BUILD_ERROR_BUFFER_OVERFLOW -2 #define PayloadBiggerThenBuffer -3
|
||||||
|
|
||||||
|
// --- UNITY SETUP/TEARDOWN ---
|
||||||
|
void setUp(void) {
|
||||||
|
// Nichts Besonderes für den Builder-Test zu resetten
|
||||||
|
}
|
||||||
|
|
||||||
|
void tearDown(void) {} // optional
|
||||||
|
|
||||||
|
static bool needs_stuffing_byte(uint8_t byte) {
|
||||||
|
return (byte == StartByte || byte == EscapeByte || byte == EndByte);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Hilfsfunktion zur Berechnung des *zu sendenden* Checksummen-Bytes ---
|
||||||
|
// Dies ist der Wert, der im Frame an der Checksummen-Position steht,
|
||||||
|
// sodass die finale XOR-Summe der Nutzdaten (MSGID + Payload + dieses Byte)
|
||||||
|
// 0x00 ergibt.
|
||||||
|
uint8_t calculate_payload_checksum_byte(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len) {
|
||||||
|
uint8_t cs = msgid;
|
||||||
|
for (size_t i = 0; i < payload_len; ++i) {
|
||||||
|
cs ^= payload[i];
|
||||||
|
}
|
||||||
|
return cs; // Dies ist der Wert, der gesendet werden muss
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Hilfsfunktion zum Vergleichen von Hex-Arrays und Debug-Ausgabe ---
|
||||||
|
void TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(const uint8_t *expected,
|
||||||
|
const uint8_t *actual, size_t len) {
|
||||||
|
char expected_str[len * 3 + 1];
|
||||||
|
char actual_str[len * 3 + 1];
|
||||||
|
int offset_exp = 0;
|
||||||
|
int offset_act = 0;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < len; ++i) {
|
||||||
|
offset_exp +=
|
||||||
|
snprintf(expected_str + offset_exp, sizeof(expected_str) - offset_exp,
|
||||||
|
"%02X ", expected[i]);
|
||||||
|
offset_act += snprintf(actual_str + offset_act,
|
||||||
|
sizeof(actual_str) - offset_act, "%02X ", actual[i]);
|
||||||
|
}
|
||||||
|
// Stelle sicher, dass die Strings nullterminiert sind, falls der Puffer genau
|
||||||
|
// gefüllt wurde
|
||||||
|
if (offset_exp > 0)
|
||||||
|
expected_str[offset_exp - 1] = '\0';
|
||||||
|
else
|
||||||
|
expected_str[0] = '\0'; // Remove last space, null-terminate
|
||||||
|
if (offset_act > 0)
|
||||||
|
actual_str[offset_act - 1] = '\0';
|
||||||
|
else
|
||||||
|
actual_str[0] = '\0';
|
||||||
|
|
||||||
|
printf("\n"); // Neue Zeile für bessere Lesbarkeit
|
||||||
|
printf(" Expected: %s\n", expected_str);
|
||||||
|
printf(" Actual: %s\n", actual_str);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, actual, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- TESTFÄLLE FÜR build_message ---
|
||||||
|
|
||||||
|
// Test 1: Gültige Nachricht ohne Escaping
|
||||||
|
void test_builder_1_basic_message_no_escaping(void) {
|
||||||
|
uint8_t msgid = 0x01;
|
||||||
|
uint8_t payload[] = {0x10, 0x20, 0x30, 0x40};
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t output_buffer[64]; // Ausreichend großer Puffer
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
// Erwarteter Checksummen-Wert
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
// Erwartete fertige Nachricht
|
||||||
|
uint8_t expected_message[] = {
|
||||||
|
StartByte, msgid, 0x10, 0x20, 0x30, 0x40, // Payload
|
||||||
|
expected_checksum, EndByte};
|
||||||
|
size_t expected_len = sizeof(expected_message);
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 2: Gültige Nachricht mit leerem Payload
|
||||||
|
void test_builder_2_empty_payload(void) {
|
||||||
|
uint8_t msgid = 0x02;
|
||||||
|
uint8_t payload[] = {};
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t output_buffer[64];
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
uint8_t expected_message[] = {StartByte, msgid, expected_checksum, EndByte};
|
||||||
|
size_t expected_len = sizeof(expected_message);
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 3: MSGID muss escapet werden (StartByte als MSGID)
|
||||||
|
void test_builder_3_escaped_msgid(void) {
|
||||||
|
uint8_t msgid = StartByte; // MSGID = 0xAA
|
||||||
|
uint8_t payload[] = {0x11, 0x22};
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t output_buffer[64];
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
uint8_t expected_message[] = {StartByte,
|
||||||
|
EscapeByte,
|
||||||
|
StartByte, // Escaped MSGID
|
||||||
|
0x11,
|
||||||
|
0x22, // Payload
|
||||||
|
expected_checksum,
|
||||||
|
EndByte};
|
||||||
|
size_t expected_len = sizeof(expected_message);
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 4: Payload-Byte muss escapet werden (EndByte im Payload)
|
||||||
|
void test_builder_4_escaped_payload_byte(void) {
|
||||||
|
uint8_t msgid = 0x04;
|
||||||
|
uint8_t payload[] = {0x01, EndByte, 0x03}; // EndByte = 0xCC im Payload
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t output_buffer[64];
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
uint8_t expected_message[] = {StartByte, msgid,
|
||||||
|
0x01, EscapeByte,
|
||||||
|
EndByte, // Escaped payload byte
|
||||||
|
0x03, expected_checksum,
|
||||||
|
EndByte};
|
||||||
|
size_t expected_len = sizeof(expected_message);
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 5: Checksummen-Byte muss escapet werden (EscapeByte als Checksumme)
|
||||||
|
void test_builder_5_escaped_checksum_byte(void) {
|
||||||
|
uint8_t msgid = 0x05;
|
||||||
|
// Payload so wählen, dass msgid ^ payload[0] = EscapeByte (0xBB)
|
||||||
|
uint8_t payload[] = {msgid ^ EscapeByte}; // Payload ist 0x05 ^ 0xBB = 0xBE
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t output_buffer[64];
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(EscapeByte, expected_checksum); // Sanity check
|
||||||
|
|
||||||
|
uint8_t expected_message[] = {StartByte,
|
||||||
|
msgid,
|
||||||
|
payload[0],
|
||||||
|
EscapeByte,
|
||||||
|
expected_checksum, // Escaped checksum byte
|
||||||
|
EndByte};
|
||||||
|
size_t expected_len = sizeof(expected_message);
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 6: Mehrere Escapings in einer Nachricht
|
||||||
|
void test_builder_6_multiple_escapings(void) {
|
||||||
|
uint8_t msgid = StartByte; // 0xAA
|
||||||
|
uint8_t payload[] = {EscapeByte, 0x01, EndByte, StartByte, 0x02};
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t output_buffer[64];
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
// Angenommen, die berechnete Checksumme selbst ist KEIN Steuerzeichen,
|
||||||
|
// sonst müsste sie auch escapet werden.
|
||||||
|
// Wenn doch, würde expected_message noch ein EscapeByte vor der Checksumme
|
||||||
|
// bekommen.
|
||||||
|
|
||||||
|
uint8_t expected_message[] = {
|
||||||
|
StartByte, EscapeByte, StartByte, // Escaped MSGID
|
||||||
|
EscapeByte, EscapeByte, // Escaped payload[0]
|
||||||
|
0x01, EscapeByte, EndByte, // Escaped payload[2]
|
||||||
|
EscapeByte, StartByte, // Escaped payload[3]
|
||||||
|
0x02, expected_checksum, EndByte};
|
||||||
|
size_t expected_len = sizeof(expected_message);
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 7: Puffer zu klein - initiale Prüfung (Payload ist zu lang für den
|
||||||
|
// Puffer)
|
||||||
|
void test_builder_7_buffer_too_small_initial(void) {
|
||||||
|
uint8_t msgid = 0x07;
|
||||||
|
// Payload, das auch im besten Fall (ohne Stuffing) nicht in einen
|
||||||
|
// 10-Byte-Puffer passt. Minimale Länge wäre 1+1+5+1+1 = 9 Bytes (Start, ID,
|
||||||
|
// Payload, CRC, End)
|
||||||
|
uint8_t payload[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
|
||||||
|
size_t payload_len = sizeof(payload); // 8 Bytes
|
||||||
|
|
||||||
|
uint8_t output_buffer[10]; // Ein Puffer, der zu klein ist für 8 Payload-Bytes
|
||||||
|
// + Rahmung + CRC
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer); // 10 Bytes
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
// Die Berechnung für PayloadBiggerThenBuffer sollte hier greifen.
|
||||||
|
// payload_len * 2 + 5 (worst case) = 8*2+5 = 21. 21 ist > 10.
|
||||||
|
TEST_ASSERT_EQUAL_INT(PayloadBiggerThenBuffer, actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 8: Puffer zu klein - Überlauf beim Schreiben (knapper Puffer, z.B. bei
|
||||||
|
// Stuffing)
|
||||||
|
void test_builder_8_buffer_overflow_during_build(void) {
|
||||||
|
uint8_t msgid = 0x08;
|
||||||
|
// Payload so wählen, dass Stuffing stattfindet
|
||||||
|
uint8_t payload[] = {0x01, StartByte, 0x02}; // StartByte im Payload
|
||||||
|
size_t payload_len = sizeof(payload); // 3 Bytes
|
||||||
|
|
||||||
|
// Minimaler Puffer für diese Nachricht OHNE Stuffing:
|
||||||
|
// Start (1) + MSGID (1) + Payload (3) + CRC (1) + End (1) = 7 Bytes
|
||||||
|
// Mit Stuffing für payload[1] (StartByte) wird es:
|
||||||
|
// 1 (Start) + 1 (MSGID) + 1 (0x01) + 2 (Escape+StartByte) + 1 (0x02) + 1
|
||||||
|
// (CRC) + 1 (End) = 8 Bytes
|
||||||
|
uint8_t output_buffer[7]; // Puffer ist 1 Byte zu klein für die gestuffte
|
||||||
|
// Nachricht
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(BufferOverFlow, actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 9: Puffer genau groß genug (Randfall)
|
||||||
|
void test_builder_9_buffer_just_enough(void) {
|
||||||
|
uint8_t msgid = 0x09;
|
||||||
|
uint8_t payload[] = {0x01, 0x02, 0x03, 0x04}; // 4 Bytes
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
uint8_t expected_message[] = {
|
||||||
|
StartByte, msgid, 0x01, 0x02, 0x03, 0x04, expected_checksum, EndByte};
|
||||||
|
size_t expected_len = sizeof(expected_message); // 1 + 1 + 4 + 1 + 1 = 8 Bytes
|
||||||
|
|
||||||
|
uint8_t output_buffer[expected_len]; // Puffer GENAU der erwarteten Größe
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 10: Maximale Payload-Länge ohne Stuffing
|
||||||
|
void test_builder_10_max_payload_no_stuffing(void) {
|
||||||
|
uint8_t msgid = 0x10;
|
||||||
|
uint8_t payload[MAX_MESSAGE_PAYLOAD_LENGTH];
|
||||||
|
for (size_t i = 0; i < MAX_MESSAGE_PAYLOAD_LENGTH; ++i) {
|
||||||
|
// Sicherstellen, dass keine Steuerzeichen dabei sind
|
||||||
|
payload[i] = (uint8_t)(i % 0xF0 + 0x01); // Vermeidet 0xAA, 0xBB, 0xCC
|
||||||
|
}
|
||||||
|
size_t payload_len = MAX_MESSAGE_PAYLOAD_LENGTH;
|
||||||
|
|
||||||
|
// Geschätzte maximale Puffergröße für worst-case (alle Bytes gestuffed)
|
||||||
|
// 1 (Start) + 1 (MSGID) + MAX_PAYLOAD_LEN*2 (Payload worst-case) + 1 (CRC) +
|
||||||
|
// 1 (End)
|
||||||
|
// + 2 (potential stuffing for MSGID/CRC) = 2*MAX_MESSAGE_PAYLOAD_LENGTH + 5
|
||||||
|
// Aber da wir hier KEIN Stuffing haben, ist es einfacher: 1 + 1 +
|
||||||
|
// MAX_PAYLOAD_LEN + 1 + 1
|
||||||
|
size_t max_buffer_needed = 1 + 1 + MAX_MESSAGE_PAYLOAD_LENGTH + 1 + 1;
|
||||||
|
|
||||||
|
uint8_t output_buffer[max_buffer_needed + 10]; // Etwas Puffer extra
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
// Erstelle erwartete Nachricht manuell oder dynamisch
|
||||||
|
uint8_t expected_message[max_buffer_needed];
|
||||||
|
size_t exp_idx = 0;
|
||||||
|
expected_message[exp_idx++] = StartByte;
|
||||||
|
expected_message[exp_idx++] = msgid;
|
||||||
|
memcpy(&expected_message[exp_idx], payload, payload_len);
|
||||||
|
exp_idx += payload_len;
|
||||||
|
expected_message[exp_idx++] = expected_checksum;
|
||||||
|
expected_message[exp_idx++] = EndByte;
|
||||||
|
size_t expected_len = exp_idx;
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 11: Maximale Payload-Länge mit Stuffing (Worst-Case Szenario)
|
||||||
|
void test_builder_11_max_payload_with_stuffing(void) {
|
||||||
|
uint8_t msgid = StartByte; // MSGID muss gestuffed werden
|
||||||
|
uint8_t payload[MAX_MESSAGE_PAYLOAD_LENGTH];
|
||||||
|
for (size_t i = 0; i < MAX_MESSAGE_PAYLOAD_LENGTH; ++i) {
|
||||||
|
// Alle Bytes sind Steuerzeichen, müssen gestuffed werden
|
||||||
|
payload[i] =
|
||||||
|
(i % 3 == 0) ? StartByte : ((i % 3 == 1) ? EscapeByte : EndByte);
|
||||||
|
}
|
||||||
|
size_t payload_len = MAX_MESSAGE_PAYLOAD_LENGTH;
|
||||||
|
|
||||||
|
// Worst-Case Puffergröße:
|
||||||
|
// 1 (Start) + 2 (Escaped MSGID) + MAX_PAYLOAD_LEN * 2 (Escaped Payload) + 2
|
||||||
|
// (Escaped CRC) + 1 (End)
|
||||||
|
size_t max_buffer_needed_worst_case =
|
||||||
|
1 + 2 + (MAX_MESSAGE_PAYLOAD_LENGTH * 2) + 2 + 1;
|
||||||
|
|
||||||
|
uint8_t
|
||||||
|
output_buffer[max_buffer_needed_worst_case + 10]; // Etwas Puffer extra
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
// Build the expected message manually to account for all stuffing
|
||||||
|
uint8_t expected_message[max_buffer_needed_worst_case];
|
||||||
|
size_t exp_idx = 0;
|
||||||
|
|
||||||
|
// StartByte
|
||||||
|
expected_message[exp_idx++] = StartByte;
|
||||||
|
|
||||||
|
// MSGID (escaped)
|
||||||
|
expected_message[exp_idx++] = EscapeByte;
|
||||||
|
expected_message[exp_idx++] = msgid;
|
||||||
|
|
||||||
|
// Payload (all bytes escaped)
|
||||||
|
for (size_t i = 0; i < payload_len; ++i) {
|
||||||
|
expected_message[exp_idx++] = EscapeByte;
|
||||||
|
expected_message[exp_idx++] = payload[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checksumme (kann auch escapet sein)
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
if (needs_stuffing_byte(expected_checksum)) {
|
||||||
|
expected_message[exp_idx++] = EscapeByte;
|
||||||
|
}
|
||||||
|
expected_message[exp_idx++] = expected_checksum;
|
||||||
|
|
||||||
|
// EndByte
|
||||||
|
expected_message[exp_idx++] = EndByte;
|
||||||
|
size_t expected_len = exp_idx;
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- MAIN TEST RUNNER ---
|
||||||
|
int main(void) {
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
// Run Builder Tests
|
||||||
|
RUN_TEST(test_builder_1_basic_message_no_escaping);
|
||||||
|
RUN_TEST(test_builder_2_empty_payload);
|
||||||
|
RUN_TEST(test_builder_3_escaped_msgid);
|
||||||
|
RUN_TEST(test_builder_4_escaped_payload_byte);
|
||||||
|
RUN_TEST(test_builder_5_escaped_checksum_byte);
|
||||||
|
RUN_TEST(test_builder_6_multiple_escapings);
|
||||||
|
RUN_TEST(test_builder_7_buffer_too_small_initial);
|
||||||
|
RUN_TEST(test_builder_8_buffer_overflow_during_build);
|
||||||
|
RUN_TEST(test_builder_9_buffer_just_enough);
|
||||||
|
RUN_TEST(test_builder_10_max_payload_no_stuffing);
|
||||||
|
RUN_TEST(test_builder_11_max_payload_with_stuffing);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
@@ -0,0 +1,452 @@
|
|||||||
|
#include "Unity/src/unity.h"
|
||||||
|
#include "message_parser.h" // Stellt sicher, dass deine Header-Datei message_parser.h korrekt ist
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h> // Für memcpy
|
||||||
|
|
||||||
|
// Globale Variablen für Callback-Überprüfung
|
||||||
|
static uint8_t received_msgid = 0xFF;
|
||||||
|
static uint8_t
|
||||||
|
received_payload[MAX_TOTAL_CONTENT_LENGTH]; // Muss groß genug sein
|
||||||
|
static size_t received_payload_len = 0;
|
||||||
|
static enum ParserError received_error = NoError;
|
||||||
|
static bool message_received_flag = false;
|
||||||
|
static bool message_fail_flag = false;
|
||||||
|
|
||||||
|
// Mock-Implementierungen für die Callbacks
|
||||||
|
void mock_on_message_received(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len) {
|
||||||
|
received_msgid = msgid;
|
||||||
|
received_payload_len = payload_len;
|
||||||
|
// Sicherstellen, dass der Puffer nicht überläuft
|
||||||
|
memcpy(received_payload, payload,
|
||||||
|
(payload_len < MAX_TOTAL_CONTENT_LENGTH) ? payload_len
|
||||||
|
: MAX_TOTAL_CONTENT_LENGTH);
|
||||||
|
message_received_flag = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mock_on_message_fail(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len, enum ParserError error) {
|
||||||
|
received_msgid = msgid; // Auch bei Fehlern kann die ID relevant sein
|
||||||
|
received_payload_len = payload_len;
|
||||||
|
// Auch hier, um sicherzustellen, dass wir den Zustand des Puffers beim Fehler
|
||||||
|
// sehen können
|
||||||
|
memcpy(received_payload, payload,
|
||||||
|
(payload_len < MAX_TOTAL_CONTENT_LENGTH) ? payload_len
|
||||||
|
: MAX_TOTAL_CONTENT_LENGTH);
|
||||||
|
received_error = error;
|
||||||
|
message_fail_flag = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- UNITY SETUP/TEARDOWN ---
|
||||||
|
void setUp(void) {
|
||||||
|
// Reset der globalen Variablen vor jedem Test
|
||||||
|
received_msgid = 0xFF;
|
||||||
|
received_payload_len = 0;
|
||||||
|
received_error = NoError;
|
||||||
|
message_received_flag = false;
|
||||||
|
message_fail_flag = false;
|
||||||
|
memset(received_payload, 0, MAX_TOTAL_CONTENT_LENGTH);
|
||||||
|
|
||||||
|
// Registrierung der Mock-Callbacks (muss vor den Tests erfolgen)
|
||||||
|
register_message_callback(mock_on_message_received);
|
||||||
|
register_message_fail_callback(mock_on_message_fail);
|
||||||
|
}
|
||||||
|
|
||||||
|
void tearDown(void) {} // optional
|
||||||
|
|
||||||
|
// --- Hilfsfunktion zur Checksummenberechnung (für Tests) ---
|
||||||
|
// Berechnet die Checksumme für MSGID + Payload + Checksummen-Byte
|
||||||
|
// Ergibt 0x00, wenn die gesamte Kette XORiert wird
|
||||||
|
uint8_t calculate_test_checksum_final(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len,
|
||||||
|
uint8_t actual_checksum_byte) {
|
||||||
|
uint8_t cs = msgid;
|
||||||
|
for (size_t i = 0; i < payload_len; ++i) {
|
||||||
|
cs ^= payload[i];
|
||||||
|
}
|
||||||
|
cs ^=
|
||||||
|
actual_checksum_byte; // Das gesendete Checksummen-Byte wird auch XORiert
|
||||||
|
return cs;
|
||||||
|
}
|
||||||
|
// Hilfsfunktion zur Berechnung des *zu sendenden* Checksummen-Bytes
|
||||||
|
// Dies ist der Wert, der im Frame an der Checksummen-Position steht,
|
||||||
|
// sodass die finale XOR-Summe der Nutzdaten (MSGID + Payload + dieses Byte)
|
||||||
|
// 0x00 ergibt.
|
||||||
|
uint8_t calculate_payload_checksum_byte(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len) {
|
||||||
|
uint8_t cs = msgid;
|
||||||
|
for (size_t i = 0; i < payload_len; ++i) {
|
||||||
|
cs ^= payload[i];
|
||||||
|
}
|
||||||
|
return cs; // Dies ist der Wert, der gesendet werden muss, damit die finale
|
||||||
|
// XOR-Summe 0x00 wird
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 1: Gültige Nachricht mit Payload
|
||||||
|
void test_1_valid_message_parses_correctly(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msgid = 0x01;
|
||||||
|
uint8_t payload[] = {0x01, 0x02, 0x03};
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
// Berechne das Checksummen-Byte, das gesendet werden muss
|
||||||
|
uint8_t checksum_byte_to_send =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
uint8_t full_message[] = {
|
||||||
|
StartByte, // 0xAA
|
||||||
|
msgid, // 0x01
|
||||||
|
0x01,
|
||||||
|
0x02,
|
||||||
|
0x03, // Payload
|
||||||
|
checksum_byte_to_send, // Das Checksummen-Byte
|
||||||
|
EndByte // 0xCC
|
||||||
|
};
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < sizeof(full_message); ++i) {
|
||||||
|
parse_byte(&mr, full_message[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(message_received_flag);
|
||||||
|
TEST_ASSERT_FALSE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(payload_len, received_payload_len); // Payload-Länge
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY(payload, received_payload, payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(NoError, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 2: Ungültige Checksumme – Fehler gemeldet und Zustand zurückgesetzt
|
||||||
|
void test_2_invalid_checksum_resets_state(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msgid = 0x02;
|
||||||
|
uint8_t payload[] = {0x10, 0x20};
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
uint8_t wrong_checksum_byte = 0x01; // Absichtlich falsche Checksumme
|
||||||
|
|
||||||
|
uint8_t full_message[] = {StartByte,
|
||||||
|
msgid,
|
||||||
|
0x10,
|
||||||
|
0x20,
|
||||||
|
wrong_checksum_byte, // Falsches Checksummen-Byte
|
||||||
|
EndByte};
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < sizeof(full_message); ++i) {
|
||||||
|
parse_byte(&mr, full_message[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(message_received_flag);
|
||||||
|
TEST_ASSERT_TRUE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid);
|
||||||
|
// received_payload_len sollte die Länge der Daten sein, die bis zum Fehler
|
||||||
|
// empfangen wurden, d.h., Payload-Länge + das falsche Checksummen-Byte.
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(payload_len + 1, received_payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WrongCheckSum, received_error);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WrongCheckSum, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 3: Gültige Nachricht ohne Payload (Länge 0)
|
||||||
|
void test_3_zero_length_message(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msgid = 0x03;
|
||||||
|
uint8_t payload[] = {}; // Leerer Payload
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t checksum_byte_to_send =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
uint8_t full_message[] = {
|
||||||
|
StartByte, msgid,
|
||||||
|
checksum_byte_to_send, // Checksummen-Byte (hier gleich MSGID, da Payload
|
||||||
|
// leer)
|
||||||
|
EndByte};
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < sizeof(full_message); ++i) {
|
||||||
|
parse_byte(&mr, full_message[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(message_received_flag);
|
||||||
|
TEST_ASSERT_FALSE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(0, received_payload_len); // Payload-Länge ist 0
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(NoError, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 4: Escapete MSGID (0xAA im MSGID-Feld)
|
||||||
|
void test_4_escaped_message_id(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msgid = 0xAA; // MSGID ist ein Steuerzeichen, muss escapet werden
|
||||||
|
uint8_t payload[] = {0x42};
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t checksum_byte_to_send =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
uint8_t full_message[] = {StartByte,
|
||||||
|
EscapeByte,
|
||||||
|
msgid, // Escapete MSGID (0xBB 0xAA)
|
||||||
|
0x42, // Payload
|
||||||
|
checksum_byte_to_send,
|
||||||
|
EndByte};
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < sizeof(full_message); ++i) {
|
||||||
|
parse_byte(&mr, full_message[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(message_received_flag);
|
||||||
|
TEST_ASSERT_FALSE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(payload_len, received_payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY(payload, received_payload, payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(NoError, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 5: Escapetes Payload-Byte (z.B. ein StartByte im Payload)
|
||||||
|
void test_5_escaped_payload_byte(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msgid = 0x05;
|
||||||
|
uint8_t payload[] = {
|
||||||
|
0x11, StartByte}; // StartByte (0xAA) im Payload, muss escapet werden
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t checksum_byte_to_send =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
uint8_t full_message[] = {
|
||||||
|
StartByte,
|
||||||
|
msgid,
|
||||||
|
0x11,
|
||||||
|
EscapeByte,
|
||||||
|
StartByte, // Escaptes StartByte (0xBB 0xAA) im Payload
|
||||||
|
checksum_byte_to_send,
|
||||||
|
EndByte};
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < sizeof(full_message); ++i) {
|
||||||
|
parse_byte(&mr, full_message[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(message_received_flag);
|
||||||
|
TEST_ASSERT_FALSE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(payload_len, received_payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY(payload, received_payload, payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(NoError, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 6: Escapetes Checksummen-Byte (z.B. ein EndByte als Checksumme)
|
||||||
|
void test_6_escaped_checksum_byte(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msgid = 0x01;
|
||||||
|
uint8_t payload[] = {
|
||||||
|
0xCD}; // payload[0] ^ msgid = 0xCD ^ 0x01 = 0xCC (EndByte)
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t checksum_byte_to_send = calculate_payload_checksum_byte(
|
||||||
|
msgid, payload, payload_len); // Dies ist 0xCC
|
||||||
|
|
||||||
|
uint8_t full_message[] = {
|
||||||
|
StartByte,
|
||||||
|
msgid,
|
||||||
|
payload[0], // Payload
|
||||||
|
EscapeByte,
|
||||||
|
checksum_byte_to_send, // Escaptes 0xCC als Checksummen-Byte (0xBB 0xCC)
|
||||||
|
EndByte};
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < sizeof(full_message); ++i) {
|
||||||
|
parse_byte(&mr, full_message[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(message_received_flag);
|
||||||
|
TEST_ASSERT_FALSE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(payload_len, received_payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY(payload, received_payload, payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(NoError, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 7: Nachricht zu lang (Pufferüberlauf)
|
||||||
|
void test_7_message_too_long(void) {
|
||||||
|
struct MessageReceive mr =
|
||||||
|
InitMessageReceive(); // mr.max_total_content_length wird auf
|
||||||
|
// MAX_TOTAL_CONTENT_LENGTH gesetzt
|
||||||
|
|
||||||
|
uint8_t msgid = 0x07;
|
||||||
|
// Dieser Payload ist absichtlich 1 Byte zu lang für
|
||||||
|
// MAX_MESSAGE_PAYLOAD_LENGTH. D.h., der gesamte Inhalt für mr.message[]
|
||||||
|
// (Payload + Checksumme) ist MAX_TOTAL_CONTENT_LENGTH + 1 Bytes lang. Dadurch
|
||||||
|
// wird der Puffer definitiv überlaufen.
|
||||||
|
uint8_t oversized_data_for_buffer[MAX_TOTAL_CONTENT_LENGTH +
|
||||||
|
1]; // Ein Byte zu viel für mr.message[]
|
||||||
|
for (size_t i = 0; i < sizeof(oversized_data_for_buffer); ++i) {
|
||||||
|
oversized_data_for_buffer[i] = (uint8_t)(i + 1); // Beliebige Daten
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wir brauchen eine korrekte Checksumme, auch wenn die Nachricht zu lang ist,
|
||||||
|
// da der Sender diese theoretisch senden würde.
|
||||||
|
// Die Berechnung erfolgt über die tatsächlich gesendeten Payload-Daten (die
|
||||||
|
// zu lang sind).
|
||||||
|
uint8_t checksum_byte_for_oversized_msg = calculate_payload_checksum_byte(
|
||||||
|
msgid, oversized_data_for_buffer, MAX_MESSAGE_PAYLOAD_LENGTH + 1);
|
||||||
|
|
||||||
|
// Simuliere den Versand der Nachricht Byte für Byte
|
||||||
|
parse_byte(&mr, StartByte); // mr.state = GetMessageType
|
||||||
|
parse_byte(&mr, msgid); // mr.state = InPayload, mr.messageid = 0x07
|
||||||
|
|
||||||
|
// Sende MAX_TOTAL_CONTENT_LENGTH Bytes, die den Puffer `mr.message`
|
||||||
|
// vollständig füllen. Index läuft von 0 bis MAX_TOTAL_CONTENT_LENGTH-1.
|
||||||
|
for (size_t i = 0; i < MAX_TOTAL_CONTENT_LENGTH; ++i) {
|
||||||
|
// Hier schicken wir die ersten MAX_TOTAL_CONTENT_LENGTH Bytes des
|
||||||
|
// übergroßen Payloads (inklusive dem eigentlichen Checksummen-Byte an
|
||||||
|
// Position MAX_MESSAGE_PAYLOAD_LENGTH). Das wird den Puffer anfüllen, aber
|
||||||
|
// noch keinen Overflow melden.
|
||||||
|
parse_byte(&mr, oversized_data_for_buffer[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// An diesem Punkt sollte mr.index = MAX_TOTAL_CONTENT_LENGTH sein.
|
||||||
|
// Der Puffer `mr.message` ist jetzt voll.
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(MAX_TOTAL_CONTENT_LENGTH,
|
||||||
|
mr.index); // Der Puffer ist genau gefüllt.
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(InPayload, mr.state); // Noch im Payload-Zustand.
|
||||||
|
|
||||||
|
// Das nächste Byte (das letzte Byte von oversized_data_for_buffer)
|
||||||
|
// wird den Overflow auslösen, da mr->index dann mr->max_total_content_length
|
||||||
|
// überschreitet.
|
||||||
|
parse_byte(&mr, oversized_data_for_buffer[MAX_TOTAL_CONTENT_LENGTH]);
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(message_received_flag);
|
||||||
|
TEST_ASSERT_TRUE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid); // MSGID ist korrekt gesetzt
|
||||||
|
// received_payload_len sollte die max. Puffergröße sein, bis der Fehler
|
||||||
|
// auftrat
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(MAX_TOTAL_CONTENT_LENGTH, received_payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(MessageToLong, received_error);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(MessageToLong, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 8: Unerwartetes StartByte mitten im Frame
|
||||||
|
void test_8_unexpected_start_byte_in_payload(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msgid = 0x08;
|
||||||
|
|
||||||
|
uint8_t full_message[] = {
|
||||||
|
StartByte, msgid,
|
||||||
|
0x10, // Teil des Payloads
|
||||||
|
StartByte, // Unerwartetes StartByte mitten im Payload
|
||||||
|
0x20, // Dies würde danach kommen
|
||||||
|
0x00, // Dummy-Checksumme
|
||||||
|
EndByte // Dummy-EndByte
|
||||||
|
};
|
||||||
|
|
||||||
|
parse_byte(&mr, full_message[0]); // StartByte
|
||||||
|
parse_byte(&mr, full_message[1]); // MSGID
|
||||||
|
parse_byte(&mr, full_message[2]); // Payload 0x10
|
||||||
|
|
||||||
|
// Hier kommt das unerwartete StartByte, sollte den Fehler auslösen und
|
||||||
|
// resetten
|
||||||
|
parse_byte(&mr, full_message[3]);
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(message_received_flag);
|
||||||
|
TEST_ASSERT_TRUE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid);
|
||||||
|
// received_payload_len sollte die Länge der Daten sein, die vor dem Fehler
|
||||||
|
// empfangen wurden.
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(1, received_payload_len); // Nur 0x10 empfangen
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY(((uint8_t[]){0x10}), received_payload, 1);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(UnexpectedCommandByte, received_error);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(UnexpectedCommandByte, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 9: Unerwartetes EndByte an der Position der MSGID
|
||||||
|
void test_9_unexpected_end_byte_at_msgid(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t full_message[] = {StartByte, EndByte, // EndByte anstelle von MSGID
|
||||||
|
0x01, 0x02, 0x03, 0x04, EndByte};
|
||||||
|
|
||||||
|
parse_byte(&mr, full_message[0]); // StartByte
|
||||||
|
parse_byte(&mr, full_message[1]); // EndByte anstelle MSGID
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(message_received_flag);
|
||||||
|
TEST_ASSERT_TRUE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(
|
||||||
|
0x00, received_msgid); // MSGID ist noch 0, da keine empfangen
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(0, received_payload_len); // Noch kein Payload
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(UnexpectedCommandByte, received_error);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(UnexpectedCommandByte, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 10: Kein StartByte zu Beginn der Sequenz (Parser sollte ignorieren)
|
||||||
|
void test_10_no_startbyte_at_beginning_ignored(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msg[] = {0x01, 0x02, 0x03, 0x04}; // Beginnt nicht mit StartByte
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < sizeof(msg); ++i) {
|
||||||
|
parse_byte(&mr, msg[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(message_received_flag);
|
||||||
|
TEST_ASSERT_FALSE(
|
||||||
|
message_fail_flag); // Sollte keinen Fehler melden, nur ignorieren
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte,
|
||||||
|
mr.state); // Sollte im Wartezustand bleiben
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(0, mr.index); // Index sollte 0 bleiben
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(NoError, mr.error); // Kein Fehler gemeldet
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 11: Ungültige Länge (z.B. EndByte kommt zu früh, ohne Checksumme)
|
||||||
|
// Angenommen, das Protokoll erwartet immer mindestens MSGID + Checksumme.
|
||||||
|
// Ein leeres Payload ist OK (MSGID + Checksumme + EndByte), aber nur MSGID +
|
||||||
|
// EndByte ist Fehler.
|
||||||
|
void test_11_frame_too_short_no_checksum(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msgid = 0x09;
|
||||||
|
uint8_t full_message[] = {
|
||||||
|
StartByte, msgid, EndByte // Kein Payload, keine Checksumme
|
||||||
|
};
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < sizeof(full_message); ++i) {
|
||||||
|
parse_byte(&mr, full_message[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(message_received_flag);
|
||||||
|
TEST_ASSERT_TRUE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid); // MSGID ist bekannt
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(0, received_payload_len); // Payload-Puffer ist leer
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WrongCheckSum,
|
||||||
|
received_error); // Checksumme kann nicht 0x00 sein
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WrongCheckSum, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
UNITY_BEGIN();
|
||||||
|
RUN_TEST(test_1_valid_message_parses_correctly);
|
||||||
|
RUN_TEST(test_2_invalid_checksum_resets_state);
|
||||||
|
RUN_TEST(test_3_zero_length_message);
|
||||||
|
RUN_TEST(test_4_escaped_message_id);
|
||||||
|
RUN_TEST(test_5_escaped_payload_byte);
|
||||||
|
RUN_TEST(test_6_escaped_checksum_byte);
|
||||||
|
RUN_TEST(test_7_message_too_long);
|
||||||
|
RUN_TEST(test_8_unexpected_start_byte_in_payload);
|
||||||
|
RUN_TEST(test_9_unexpected_end_byte_at_msgid);
|
||||||
|
RUN_TEST(test_10_no_startbyte_at_beginning_ignored);
|
||||||
|
RUN_TEST(test_11_frame_too_short_no_checksum);
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user