Skip to content

Refactor URC handling #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2023
Merged

Refactor URC handling #16

merged 1 commit into from
Dec 13, 2023

Conversation

sfe-SparkFro
Copy link
Collaborator

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.

Now uses a vector to store individual URC handlers, and iterate through each one by one
Also uses a separate vector for the URC strings within pruneBacklog
This should make the URCs easier to maintain and expand, particularly within derived classes
sfe-SparkFro added a commit that referenced this pull request Dec 13, 2023
Pending #16, this should work after that gets merged
@sfe-SparkFro sfe-SparkFro mentioned this pull request Dec 13, 2023
@@ -313,445 +328,525 @@ bool UBX_CELL::bufferedPoll(void)
return handled;
} // /bufferedPoll

// Parse incoming URC's - the associated parse functions pass the data to the user via the callbacks (if defined)
bool UBX_CELL::processURCEvent(const char *event)
bool UBX_CELL::urcHandlerReadSocket(const char* event)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of where I was heading with the u-blox GNSS library refactoring. Except I was trying to go one step further and not define methods like this individually. Instead is there a more elegant way where you could have a single common add-handler method and pass in: a pointer to the const char AT command; the search string; and some kind of common way of passing the data parsed / extracted from the URC to the callback. I was heading in the direction of using const uint8_t * for all callbacks, and provide helper methods to extract the individual 'fields' inside the callback...

In the case of this library, there are relatively few of these. And relatively few callbacks (each with their own parameter). So, I'm OK with you merging this as-is. But, for bonus points, you could spend five minutes thinking about that possible more elegant solution?!

(In the case of the GNSS library, I absolutely need the elegant method because there are so many individual messages (== URCs), each containing many many fields... That's where I was last week when I threw the towel in (again)...)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By eye, this looks great. I need to test it on SARA-R5. But I think I need you to merge it before I test it...

Please spend (no more than) 5 mins thinking about the above, then go ahead and merge it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Paul! I agree, there's definitely better solutions to this (eg. there's a lot of repeated code, such as searching for the URC strings or the values that follow). I do have a few ideas on how to further improve this (and other aspects of this library), but the goal right now is to just get it working "good enough" with our current products. If desired, we can always refactor it later.

@sfe-SparkFro sfe-SparkFro merged commit 4c995f0 into v1.0.0 Dec 13, 2023
@sfe-SparkFro sfe-SparkFro deleted the refactor_urc_handling branch December 13, 2023 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants