Porting a library means knowing what it expects: coreMQTT on the ISM43362
Summary
coreMQTT reads a packet a few bytes at a time. The ISM43362 Wi-Fi module on the STM32L475 IoT Discovery kit returns everything it has received in one read command. A small receive buffer in the transport lets the two work together.
The problem
After sending CONNECT, coreMQTT waits for CONNACK. The Wi-Fi module receives the whole response at once, but coreMQTT reads only a few bytes of it. When coreMQTT needs more bytes, the transport sends another read command to the module, even though the first read had already received those bytes.
The rest of the CONNACK was already gone, so every new read returned nothing. coreMQTT waited until its timeout and the connect failed.
Two ideas of “read”
coreMQTT treats the transport as a byte stream. It asks for 1 byte, then 1 more, then the rest of the packet, and expects 0 when nothing has arrived yet.
The ISM43362 works with commands. One R0 command over SPI returns all the bytes
the module has buffered. Bytes the transport doesn’t keep can’t be read again.
Before: the rest of CONNACK is dropped and later reads return nothing. After: one R0 fills the buffer.
The fix
So we added a receive buffer between coreMQTT and the Wi-Fi module. If the buffer
has bytes, recv serves them from there. Only when it is empty does the transport
send a new R0 command to the module.
recv(dst, len):
if buffer is not empty:
copy up to len bytes from buffer to dst
return bytes copied
n = module_read(buffer, BUFFER_SIZE) // one R0 command
if connection lost: return -1
if n == 0: return 0 // nothing yet, coreMQTT retries
copy up to len bytes from buffer to dst
keep the rest in buffer for the next call
return bytes copied
With the buffer in place, coreMQTT processes the whole CONNACK and the application carries on.
Takeaways
- Before porting a library, read what it expects from the platform. For coreMQTT,
that’s the
recvinTransportInterface_t: how it is called, and what 0 and a negative return mean. - Know what your hardware returns per command. A socket-based Wi-Fi module is not a byte stream.
- When the two don’t match, fix it in the transport layer, not in the library.