Design PatternsFinally, there’s a book about patterns for firmware.
Well-known UML guru Bruce Powel Douglass has published a book titled “Design Patterns for Embedded Systems in C.” Just the title hits all of the right notes. Patterns are a hot topic in CS today, but so far have been largely neglected in the embedded space. And the patterns that are published are more likely to be C++, while C still dominates the firmware.
Patterns do lend themselves to an OO approach, and some works spend time showing how to adapt C code to the C++ world. In a nice reversal Bruce shows how to use OO ideas with C. His ideas and examples are clear and innovative with useful example code. That chapter alone is thought-provoking enough to justify the book.
Another chapter discusses Harmony, his agile approach. Harmony resembles Scrum, but, to me, offers an appealing level of discipline and detail. There are a number of steps one must follow, even in architecture and design. Sometimes I find those are shortchanged with other agile methods, or, at the least, there’s little direction given on how to approach these tasks. Harmony has very specific guidelines.
Patterns are given for a number of important embedded tasks, like the creation of state machines and working with multitasking. There were two I found particularly appealing. The first is the observer pattern. This is another name for publish/subscribe, an approach that is increasingly found in complex systems.
When many activities need information it’s often inefficient to have them stuck in waiting loops. Instead, the consumers of the info (the “subscribers”) tell the “publisher” to let them know when the data is available. The subscribe to the publishers channel, in effect. When the information is available the publisher notifies all of the subscribers. This decouples the system tremendously.
The second pattern that I particularly liked is really a collection called the channel patterns. One of the most vexing problems in firmware is how to respond to errors. Sure, malloc() may let the caller know that there’s no memory available… but what then? The channel patterns are designed to allow processing to continue, perhaps in a degraded mode, when things go awry. It’s quite complex but very thought-provoking.
UML diagrams are used throughout, and an appendix provides all of the background even a UML neophyte needs to understand the book. Read that first.
This is not a recipe book, as some of the other works on patterns are. Each pattern is coupled to an example that is useful in furthering your understanding but that won’t make it into your code. View the patterns as the general design that you’ll adapt to your project.
Unlike Betty Crocker’s works much of the code will make little sense unless you read the first couple of chapters for background. But those chapters are worthwhile even taken alone.
I think many readers won’t like the book. It pushes one away from the comfort zone of implementing just barely enough to sort of make a system work. Some will complain about the code being overly complex.
For instance, the CRC pattern is about 11 pages of code. But that’s because it is generalized (a hallmark of code slated for reuse) and a lot of the listing is an example use of the pattern. So far patterns have found little traction in the embedded world as few are available in C, and there just aren’t that many that address the unique needs of firmware. Bruce’s book addresses both of those problems. I found it refreshing and very thought-provoking.
Jack G. Ganssle is a lecturer and consultant on embedded development issues. He conducts seminars on embedded systems and helps companies with their embedded challenges. Contact him at email@example.com. His website is www.ganssle.com.