Arduino stateful serial modem implementation

Hello,
I'm trying to implement a "stateful" communication with a serial device. The device is a modem and accepts AT commands. In order not to have the Arduino wait, either by endlessly looping in a function or by waiting a set amount of milliseconds, I decided to code a simple state machine.

Problem is, I'm lost in my code. The device accepts AT commands and responds accordingly (either instantly or after some seconds). The device can also send data when I'm not expecting to, for example when data arrive through TCP/UDP.

This is what I have currently in mind and semi-implemented. That's a simple scenario, most functions will follow the same pattern:

  1. .begin() is called, Arduino sends the device "AT+WAKEUP", normal execution follows
  2. some weird interrupt is triggered (or I trigger a .update() function) and a buffer is filled (byte by byte?)
  3. when the buffer has a specific string in it (+WAKEUP:OKDUDE), I do something (set a variable)

Is that a good way of implementing? How can I implement the buffer? Any ideas or code is highly appreciated.

Thanks!

It would perhaps be best if you sat down and wrote a list of every type of activity you're expecting your arduino to perform.

Which inputs it's likely to get, which outputs it's expected to drive or send.

Then perhaps we can work out a basic framework of how to put that together. You can practically just write a whole sketch with nothing in it but comments, then work out how to achieve those comments.

I agree with @KenF.

In addition it would be useful to know about the context in which the project operates. For example why are there AT commands, why are there TCP/UDP inputs etc. A short essay of the sort "This is a project to do X. It uses a Y to communicate with a Z. The Z does a,b,c every T minutes and ...... The role of the Arduino is ..."

...R