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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

80 Chapter 4. Event-driven <strong>Programming</strong> in Practice<br />

4.3.1 Automatic <strong>State</strong> Management<br />

First off all, to avoid manual state management, a sensor-node programming<br />

model requires an explicit abstraction of what we have presented as phases: a<br />

common context in which multiple actions can per<strong>for</strong>m. Furthermore it requires<br />

mechanisms to compose phases sequentially, hierarchically, and concurrently.<br />

A natural abstraction of program phases are states of finite state machines<br />

(FSM). Finite state machines—in their various representations—have been extensively<br />

used <strong>for</strong> modeling and programming reactive embedded systems. Reactive<br />

embedded systems share many of the characteristics and requirements of<br />

sensor nodes, such as resource efficiency and complex program anatomies.<br />

Particularly the <strong>State</strong>charts model and its many descendants have seen<br />

widespread use. The <strong>State</strong>charts model combines elements <strong>for</strong>m the eventdriven<br />

programming model (events and associated actions) as well as from finite<br />

state machines (states and transitions among states). To these elements it<br />

adds state hierarchy and concurrency. As such, the <strong>State</strong>charts model is well<br />

suited to describe sensor-node programs. <strong>State</strong>charts can help to solve the manual<br />

state-management issues. Particularly, it allows to specify the program’s<br />

control flow in terms of the model’s abstractions (namely transitions between<br />

possibly nested and concurrent states), rather then requiring the programmer to<br />

specify the control flow programmatically. Indeed, we have already used the<br />

graphical <strong>State</strong>chart notation to illustrate the flow of control through phases in<br />

the examples throughout this chapter. The <strong>State</strong>chart model builds the foundation<br />

<strong>for</strong> our sensor-node programming model, which we will present in the next<br />

chapter.<br />

4.3.2 Automatic Stack Management<br />

However, neither the <strong>State</strong>charts model nor any of its descendants do solve the<br />

issue of manual stack management. As we have detailed previously, phases of<br />

sensor-node programs have associated data state. <strong>State</strong>charts and <strong>State</strong>chartlike<br />

models typically do not provide an abstraction <strong>for</strong> such data state. The few<br />

models which do, however, lack a mechanism to manage such state automatically,<br />

that is, to initialize the data state upon state entry (e.g., by allocating and<br />

initializing a state specific variable) and to release it upon exit. The same holds<br />

<strong>for</strong> all (de)initializations related to phases, such as setting up or shutting down<br />

timers, switching on or off radios and sensors, etc. In general, <strong>State</strong>chart and<br />

descendant programming models do not support the (de)initialization of states.<br />

To fix these issues, we propose to make two important additions to the basic<br />

<strong>State</strong>chart model: Firstly, states have dedicated initialization and deinitialization<br />

functions. These functions are modeled as regular actions. They are associated<br />

with both a state and a transition, and are executed within the context of the<br />

associated state. Secondly, states can have associated data state. This state can<br />

be considered as the local variables of states. The lifetime and scope of state<br />

variables are bound to their state. <strong>State</strong> variables are an application of general<br />

(de)initialization functions: data state is allocated and initialized in the state’s<br />

initialization function and released upon the state’s exit in its deinitialization<br />

function. (In our proposed model however, initialization and release of state<br />

variables is transparent to the programmer.) However, state variables are more

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

Saved successfully!

Ooh no, something went wrong!