CIS 542 Embedded Systems Programming – Summer 2013 Lecture ...
CIS 542 Embedded Systems Programming – Summer 2013 Lecture ...
CIS 542 Embedded Systems Programming – Summer 2013 Lecture ...
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
32<br />
33<br />
34<br />
35<br />
36<br />
37<br />
38<br />
39<br />
40<br />
41<br />
42<br />
}<br />
break;<br />
case alarm_sounding:<br />
switch (signal) {<br />
case quiet:<br />
if (password_correct()) state = armed;<br />
break;<br />
}<br />
break;<br />
}<br />
Line 1 enumerates all the possible states we could be in, and defines a global variable called state.<br />
Line 2 enumerates all the possible signals we could receive.<br />
Line 4 is the beginning of the function that would represent our controller. We can imagine that another<br />
function gets information from the outside world (either through a sensor or some input device) and<br />
then calls this function to update the state and to take whatever action is necessary.<br />
Line 6 is the start of a switch statement that runs the entire length of the function. It is used to figure<br />
out what to do for the state we're currently in.<br />
For instance, lines 8-17 handle the case where we're in the “disarmed” state. Now we switch on the<br />
signal we've received (line 9). If the signal is “arm” (line 10), we move to the “armed” state (line 11). If<br />
the signal is “panic” (line 13), we move to the “alarm_sounding” state (line 14). For any other signal,<br />
we do nothing, as shown in the EFSM.<br />
Note that, for the “disarmed” state, the number of cases in its switch statement (lines 9-16) is the same<br />
as the number of edges exiting that state in the EFSM. This makes it easy to check that you've included<br />
all transitions and successfully matched the code to the EFSM.<br />
But how do we know it correctly implements the EFSM as drawn above? That is, how do we know that<br />
the transitions it makes are the right ones?<br />
This question can be answered through software testing: we come up with inputs (signals) and, using<br />
the EFSM as our guide, make sure we're always in the state we expect to be in.<br />
If we think of the EFSM as a graph, obviously there are an infinite number of paths through it (because<br />
of the loops), so we can't test all of them. However, we don't need to: the assumption is that it doesn't<br />
matter how we got to a certain state, all that matters is that, when we're in that state, we make the<br />
correct transition for the signal we receive.<br />
We can now create test cases in which we set the state that we want to be in, call “handle_signal” with<br />
the appropriate signal, and then check which state we transitioned to.<br />
state = armed;