How to write good cellular modem driver?
Hi guys,
I'm struggling with modem communication on embedded system that does few other tasks in realtime. I'm using TCP stack built in modem (Fibocom G510).
This seem to be not complicated, but every time I'm doing it - I'm doing it diffrent way and I end up with pretty big and not perfect code, because there is always URC or condition that I have not thought of.
To establish TCP socket connection I have to:
- reset modem hardware
- wait for "SIM READY"
- enter pin (optional)
- wait for cellular network registration
- wait for 2G/3G/4G registration
- configure APN
- configure other things (optional)
- activate internet connection
- activate TCP connection
After everything is initialized and connected I have to handle many things, including URC (unsolicited result codes):
- cellular network connection lost (so AP and TCP connection is also lost)
- AP connection lost (so TCP connection is also lost)
- TCP connection lost
- maybe even SIM LOST scenario
- errors
- incoming data from TCP socket
- incoming SMS messages (optional)
And I also have to support timeouts.
Is there any way to avoid spaghetti state machine?
Is there any reference design for embedded modem driver that I can find on Github or some manufacturer website?
