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.

7.3. Memory-Efficient Programs 133<br />

Program 7.4: Reimplementation of SURGE in OSM. A graphical representation<br />

of the OSM state machine is depicted to the left.<br />

SURGE<br />

IDLE<br />

BUSY<br />

W4_DATA<br />

W4_SEND<br />

W4_ACK<br />

timeout<br />

dataReady<br />

sendData<br />

sendDone<br />

1 state SURGE {<br />

7.3 Memory-Efficient Programs<br />

2 initial state IDLE {<br />

3 timeout/ ADC_getData() -> BUSY;<br />

4 }<br />

5 state BUSY {<br />

6 uint16_t sample;<br />

7<br />

8 initial state W4_DATA {<br />

9 dataReady/<br />

10 sample=dataReady.data,<br />

11 post( sendData ) -> W4_SEND;<br />

12 }<br />

13 state W4_SEND {<br />

14 sendData/ send(sample)->W4_ACK;<br />

15 }<br />

16 state W4_ACK {<br />

17 // do nothing<br />

18 }<br />

19 sendDone/ -> IDLE;<br />

20 } // end BUSY<br />

21 } // end SURGE<br />

In the previous section we have shown that partitioning sensor-node programs<br />

in the time domain through explicitly modeled states notation benefits modularity<br />

and structure of the program code. In this section we show that OSM’s<br />

concept of state variables—variables the lifetime of which are associated with<br />

states—can also benefit memory efficiency.<br />

The basic idea of saving memory is to allocate memory only <strong>for</strong> those variables,<br />

that are required in the currently active state of the program. When the<br />

program state changes, the memory used by variables of the old program state<br />

is automatically reused <strong>for</strong> the variables of the new state. In our state-based approach,<br />

states not only serve as a structuring mechanism <strong>for</strong> the program, they<br />

also serve as a scoping and lifetime qualifier <strong>for</strong> variables. Regular event-driven<br />

programming models lack this abstraction and thus <strong>for</strong>ce programmers to use<br />

global variables with global lifetime, as described in Sect. 4.1.<br />

Using an example based on the program to calculate the average of remote<br />

temperature sensors from Sect. 4.1, we now show how the state-based programming<br />

model can be used in practice to save memory by storing temporary data<br />

in state variables. Then we answer the question how this technique is generally<br />

applicable to sensor-node programming and how much memory can be saved<br />

in real-world programs.

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

Saved successfully!

Ooh no, something went wrong!