MySensors
MySensors copied to clipboard
If .eeprom file is not present, the AES key in .config file is not considered at first run
I'm trying to run mysgw in a docker container (the issue can be reproduced also without docker), you can see the Dockerfile
here: https://github.com/mmojana/home-automation/tree/master/mysensors-gateway . The gateway was unable to decode the incoming messages. Running it on the docker host was working perfectly, so I decided to debug it. I've found out that the gateway does the following:
- Reads the config file
- Loads the EEPROM file in memory and, if not present, uses a block of "1"s as content
- Initializes the transport
- The AES key found in the config file is saved in the EEPROM file and in memory
I would have expected this sequence:
- Reads the config file
- Loads the EEPROM file in memory and, if not present, uses a block of "1"s as content
- The AES key found in the config file is saved in the EEPROM file and in memory
- Initializes the transport
Here is how to reproduce the issue:
git clone https://github.com/mmojana/home-automation/
cd home-automation/mysensors-gateway/
create a file mysensors.conf
with a content such as:
verbose=debug
log_file=0
log_filepath=/tmp/mysgw.log
log_pipe=0
log_pipe_file=/tmp/mysgw.pipe
syslog=0
eeprom_file=/etc/mysensors.eeprom
eeprom_size=1024
aes_key=3D9912EC9A015AD46B8DC12F4EEB8659
Build the docker container:
docker image build -t mysensors-gateway:1.0 .
And run it overrriding the entrypoint:
sudo docker run -it --privileged --entrypoint /bin/bash -p 5003:5003 -v /dev/mem:/dev/mem mysensors-gateway:1.0 -s
Install and run the GDB debugger:
apt-get install -y gdb
gdb /usr/local/bin/mysgw
Set the breakpoint where the transport is initialized and when the EEPROM file is read and run:
b MyTransportRFM69.cpp:42
b MyMainLinuxGeneric.cpp:470
r
At the first run:
Dec 10 04:39:12 INFO Starting gateway...
Dec 10 04:39:12 INFO Protocol version - 2.3.1
Dec 10 04:39:12 INFO EEPROM file /etc/mysensors.eeprom does not exist, creating new file.
Dec 10 04:39:12 DEBUG MCO:BGN:INIT GW,CP=RPNGL--X,REL=255,VER=2.3.1
Dec 10 04:39:12 DEBUG TSF:LRT:OK
Dec 10 04:39:12 DEBUG TSM:INIT
Dec 10 04:39:12 DEBUG TSF:WUR:MS=0
Dec 10 04:39:12 DEBUG RFM69:INIT
Dec 10 04:39:12 DEBUG RFM69:INIT:PIN,CS=24,IQP=22,IQN=25,RST=11
Dec 10 04:39:12 DEBUG RFM69:PTX:LEVEL=5 dBm
[New Thread 0x75d07450 (LWP 137)]
Thread 1 "mysgw" hit Breakpoint 1, transportInit () at ./hal/transport/RFM69/MyTransportRFM69.cpp:47
47 ./hal/transport/RFM69/MyTransportRFM69.cpp: No such file or directory.
(gdb) p/x RFM69_psk
$1 = {0xff <repeats 16 times>}
After continuing (press c
(gdb) c
Continuing.
Dec 10 04:40:07 DEBUG TSM:INIT:TSP OK
Dec 10 04:40:07 DEBUG TSM:INIT:GW MODE
Dec 10 04:40:07 DEBUG TSM:READY:ID=0,PAR=0,DIS=0
Dec 10 04:40:07 DEBUG MCO:REG:NOT NEEDED
Dec 10 04:40:07 DEBUG Listening for connections on ���~:5003
Dec 10 04:40:07 DEBUG MCO:BGN:STP
Dec 10 04:40:07 DEBUG MCO:BGN:INIT OK,TSP=1
Thread 1 "mysgw" hit Breakpoint 2, main (argc=1, argv=0x7efffe04) at ./hal/architecture/Linux/MyMainLinuxGeneric.cpp:470
470 ./hal/architecture/Linux/MyMainLinuxGeneric.cpp: No such file or directory.
(gdb) p conf.aes_key
$3 = 0x430468 "3D9912EC9A015AD46B8DC12F4EEB8659"
So we see that the transport is initialized with an empty AES key and only afterwards, it's loaded from the config file. What happens when we continue:
(gdb) c
Continuing.
[...]
Dec 10 04:41:52 DEBUG TSF:MSG:READ,20-140-88,s=241,c=4,t=255,pt=7,l=9,sg=0: 0
Dec 10 04:41:52 DEBUG !TSF:MSG:LEN=7,EXP=16
Dec 10 04:41:54 DEBUG TSF:MSG:READ,208-154-251,s=43,c=3,t=137,pt=0,l=4,sg=0:
Dec 10 04:41:54 DEBUG !TSF:MSG:LEN=7,EXP=11
Dec 10 04:41:58 DEBUG TSF:MSG:READ,68-106-55,s=53,c=6,t=65,pt=5,l=23,sg=0:0
Dec 10 04:41:58 DEBUG !TSF:MSG:LEN=7,EXP=30
As you can see the gateway cannot decode the packets anymore. Give a CTRL-C to interrupt the process and restart it from the beginning:
^C
Thread 1 "mysgw" received signal SIGINT, Interrupt.
0x76d1f744 in ?? () from /lib/arm-linux-gnueabihf/libc.so.6
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /usr/local/bin/mysgw
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/arm-linux-gnueabihf/libthread_db.so.1".
Dec 10 04:42:33 INFO Starting gateway...
Dec 10 04:42:33 INFO Protocol version - 2.3.1
Dec 10 04:42:33 DEBUG MCO:BGN:INIT GW,CP=RPNGL--X,REL=255,VER=2.3.1
Dec 10 04:42:33 DEBUG TSF:LRT:OK
Dec 10 04:42:33 DEBUG TSM:INIT
Dec 10 04:42:33 DEBUG TSF:WUR:MS=0
Dec 10 04:42:33 DEBUG RFM69:INIT
Dec 10 04:42:33 DEBUG RFM69:INIT:PIN,CS=24,IQP=22,IQN=25,RST=11
Dec 10 04:42:33 DEBUG RFM69:PTX:LEVEL=5 dBm
[New Thread 0x75d07450 (LWP 139)]
Thread 1 "mysgw" hit Breakpoint 1, transportInit () at ./hal/transport/RFM69/MyTransportRFM69.cpp:47
47 ./hal/transport/RFM69/MyTransportRFM69.cpp: No such file or directory.
(gdb) p/x RFM69_psk
$4 = {0x3d, 0x99, 0x12, 0xec, 0x9a, 0x1, 0x5a, 0xd4, 0x6b, 0x8d, 0xc1, 0x2f, 0x4e, 0xeb, 0x86, 0x59}
(gdb)
As you can see, from the second run, the key is correctly loaded, because the EEPROM file that was generated in the first execution has been read and contained the correct AES key already.