21.08.2013 Views

A State-Based Programming Model for Wireless Sensor Networks

A State-Based Programming Model for Wireless Sensor Networks

A State-Based Programming Model for Wireless Sensor Networks

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

124 Chapter 7. <strong>State</strong>-based <strong>Programming</strong> in Practice<br />

eled explicitly as a NesC-language enumeration. Transitions are modeled by<br />

assigning one of the enumeration constants to the state-keeping variable. (We<br />

use the term “state-keeping variable” as opposed to “state variable” in order to<br />

distinguish them from OSM’s concept of state variables.) The code resembles<br />

the general pattern of event-driven applications as shown in Program 4.2. As<br />

such, it is very structured <strong>for</strong> an event-driven program.<br />

Program 7.1 is an excerpt of the original NesC code representing a state transition<br />

(see also Appendix B.1, lines 159-164). The MEMBER state is entered from<br />

the FOLLOWER state through a transition triggered by a join event (line 1). In<br />

order to increase the readability of the code, op7() (line 5) serves as a placeholder<br />

<strong>for</strong> the actual action to be per<strong>for</strong>med on the occurrence of the join event<br />

in state FOLLOWER. Then the transition’s target state is assigned in line 6. Finally,<br />

the timer of the target state MEMBER is initialized in line 7. We will discuss<br />

initialization of state-bound resources, such as timers, in greater detail below.<br />

Program 7.1: NesC code fragment implementing the transition from FOLLOWER<br />

to MEMBER upon a join event. The code also includes the timer<br />

initialization of the target state (line 7).<br />

1 command result_t GroupManagement.join() {<br />

2 switch( _GMStatus.status ) {<br />

3 case ...: // [...]<br />

4 case FOLLOWER:<br />

5 op7();<br />

6 _GMStatus.status = MEMBER;<br />

7 _generalTimer = wait_receive();<br />

8 break;<br />

9 case ...: // [...]<br />

10 default: // [...]<br />

11 }<br />

12 }<br />

OSM Implementation<br />

We have reimplemented EnviroTrack’s group management state machine in<br />

OSM, as shown in Appendix B.2. A small excerpt of the OSM code is shown<br />

in Prog. 7.2, which is the OSM equivalent to the NesC fragment in Prog. 7.1.<br />

Instead of using a state-keeping variable, states are specified explicitly using<br />

OSM states (indicated by the state keyword). Transitions are specified explicitly<br />

within their source state, making switch/case statements obsolete. Unlike<br />

in event-driven programs, control-flow in<strong>for</strong>mation and implementation are<br />

clearly separated in OSM programs. OSM code only contains the control flow<br />

of the group management protocol. It does not contain any program logic. The<br />

actual program is implemented in the C language in separate files.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!