sparkfun / SparkFun_u-blox_Cellular_Arduino_Library

Other
0 stars 2 forks source link

Refactor URC handling #16

Closed sfe-SparkFro closed 10 months ago

sfe-SparkFro commented 10 months ago

URCs need to get handled differently, for a couple reasons:

  1. There isn't really a way for subclasses to add new URCs. For instance, the voice class needs to be able to process the RING URC, but the current implementation doesn't really allow for that, at least not easily.
  2. Maintenance is tricky. processURCEvent() is very long, and adding new handlers requires remembering to update pruneBacklog() too (I almost missed it myself).

This new implementation splits up processURCEvent() into individual handler methods for each URC. In the constructor, those handlers are added to a vector using the new addURCHandler(). Then processURCEvent() simply iterates through each handler, and only returns true if the event gets handled. This enables subclasses to add URC handlers within their constructors, and everything should be happy. This was @gigapod's suggestion, seems to work fine.

Additionally, pruneBacklog() needs to be aware of what URC strings are "actionable events", so I added a second vector to remember those. They're set when calling addURCHandler().

Now to handle a new URC, you must create a method to do so, then add it (and the string it's looking for) with addURCHandler(). This should be more maintainable moving forward, and allow subclasses to easily add new URC handlers.