This commit implements a byte-by-byte alignment approach.
It consists on a state machine that consumes the UART output byte-by-byte, and that will only accept input of the form:
`0xAB 0xCB, 0xXX, 0x00` .
Additionally:
* Corrects several memory issues (segmentation faults & leaks).
* Reduces timeout lengths as they hinder the retry-realign process.
Tested on Linux with bootloaders
* 4.00.01 (UV-5R Plus), and
* 2.00.06 (UV-K5)
and adaptors based chipsets
* CH341, and
* PL2303
When not plugging in the radio we expect to only see timeout errors.
However due to timeout being checked AFTER the read() call the len parameter
was updated and no longer was 0 returned.
If less then 4 was read (a short read) then uninitialized data was used,
for length and/or magic check.
If the magic byte does not match then the rx buffer should be flushed before
trying again. Otherwise sync will probably never be achieved. This
solves the bug where plugging in the radio show continues streams of
magic packet errors. For me k5prog -Y -D now always works, while before
it could fail.