hideakitai / ArduinoOSC

OSC subscriber / publisher for Arduino
MIT License
200 stars 19 forks source link

Bug for AVR boards in which memory layout is not correct #12

Closed cansik closed 4 years ago

cansik commented 4 years ago

I am currently working with the Arduino Uno WiFI Revision 2 and experience weird bugs and behaviours. It really has problems to parse the incoming data which seems to be the ceil4 related problem.

I guess it has to do with the following todo line:

// TODO: fix bug for AVR boards in which memory layout is not correct.

Is it possible to create a workaround for this (even we have some performance issues)?

cansik commented 4 years ago

Wow...it seems that it depends on how the source code is structured. Adding some Serial prints to the decoder, and everything works fine:

Address: /hello/world
Address Begin:16152
Address End:16164
TypeTags Begin: 16168
TypeTags End: 16171
TypTags Length: 12
IsZeroPadding Correct: 1
First Char: ,

Removing them breaks the memory model.

Now I understand this line of comment:

// if bool dummy; is added in this class, building from raw data fails because of ceil4 return not correct pointer address
cansik commented 4 years ago

Ok, here is the bug:

RAW: 2F(/) 68(h) 65(e) 6C(l) 6C(l) 6F(o) 2F(/) 77(w) 6F(o) 72(r) 6C(l) 64(d) 0(�) 0(�) 0(�) 0(�) 2C(,) 69(i) 69(i) 0(�) 
Address Begin:16153
Address End:16165
Address Length:12
Address Begin:/
Address: /hello/world
TypeTags Begin: 16168
TypeTags End: 16168
TypTags Max Length: 13
IsZeroPadding Correct: 0
0. Char: �
1. Char: ,
2. Char: i
3. Char: i
4. Char: �

The problem is that you are doing wrong math with your pointers. The code is rounding up the pointers to a multiple of 4, but by its direct address in the memory:

ceil4(16165 + 1) = 16168

But it should have to ceil4 the bits relative to it's starting:

ceil4(16165 + 1 - 16153) + 16153 = 16169

And the same thing then again for the parameter parsing.

hideakitai commented 4 years ago

Thanks for your PR! @cansik