Jisu Oh
Dept. of Computer Science
SUNY-Binghamton
Outline
• TinyOS Program Architecture
• NesC application: interfaces and components
• Concurrency Model: Two-level scheduling
• Component Development
• TinyOS installation
• Configuration file vs. Module file
• NesC application
= components with well-defined, bidirectional interfaces
• Concurrency Model
• Two-level scheduling
Interfaces
• /opt/tinyos-1.x/tos/interfaces
• The only point of access to the component
• Bi-directional
• /opt/tinyos-1.x/tos/interfaces/Timer.nc
• interface Timer{
• Example: TimerC.nc
configuration TimerC{
provides interface Timer[uint8_t id];
provides interface StdControl;
}
implementation{
components TimerM, ClockC;
TimerM.Clock → ClockC;
Timer = TimerM;
}
Components
• Modules
• Provide application code
• Implement one or more interface
• Example CC1000RadioIntM.nc
module CC1000RadioIntM{
provides interface StdControl;
uses interface StdControl as TimerControl;
uses interface Timer as WakeupTimer;
}
implementation{
task void PacketSent(){}
command result_t StdControl.init(){}
event result_t WakeupTimer.fired(){}
}
Mica2 CC1000 Radio Stack Architecture
Provider, User and Interface
interface Timer{
command result_t start(char type, uint32_t interval);
command result_t stop();
event result_t fired();
}
StdControl User
Provider TimerControl
TimerC Interface
CC1000RadioIntM
Timer WakeupTimer
• Interfaces
• Verbs or nouns
• In mixed case with the first letter of each internal word capitalized
• ex) ADC, Range, SendMsg
• Components
• Nouns
• In mixed case with the first letter of each internal word capitalized
• ex) Counter, TimerC, TimerM
• Files
• “.nc” as a suffix
• ex) ADC.nc, TimerC.nc
Concurrency Model
• Two-level scheduling
• Hardware event handlers
• Call commands, post tasks, and signal other events
• Preempt tasks
• Preempted by asynchronous hardware event handlers
(events and commands)
• Tasks
• computations Tasks
Preempt Tasks
POST FIFO
events
Hardware commands
Event
commands
handler
Interrupts
Time
Hardware
Concurrency Model (cont.)
Preempt Tasks
POST FIFO
events
commands
commands
Interrupts
Time
Hardware
Two-level scheduling
FIFO
…
Post Task 2
Post Task 1
command 1
• Component Development
• TinyOS installation
• Configuration file: wiring
• Module file: application code
Application code!
• Module file – BlinkM.nc
• the actual implementation of the component itself
• deal with event handling, command implementation,
task posting, etc.
A component graph of Blink application
Blink.nc – A frame of Configuration files
configuration Blink
Interfaces which this
{ configuration
provide interfaces
provides or uses
use interfaces
}
// wiring Connecting
components
}
Blink.nc – in detail
implementation
{ components Main, BlinkM, SingleTimer, LedsC;
Component.interface
// wiring
Main.StdControl → BlinkM.StdControl;
Main.StdControl → SingleTimer.StdControl;
BlinkM.Timer → SingleTimer.Timer;
BlinkM.Leds → LedsC;
} interfaces BIND implementation
Blink.nc – Main and StdControl
• A component “Main”
• Executed first in a TinyOS application
• A TinyOS application must have Main component in its
configuration
• An interface “StdControl”
• Used to initialize and start TinyOS components
• /opt/tinyos-1.x/tos/interfaces/StdControl.nc
interface StdControl
{
command result_t init();
command result_t start();
command result_t stop();
}
BlinkM.nc – A frame of Module files
module BlinkM
Interfaces which this
{ module provides or
provides interfaces
uses
uses interfaces
}
Application code
implementation
{
// implementation of
commands, //events or any tasks
if needed
}
Implementation
BlinkM.nc – inside of Module part needed
interface StdControl
module BlinkM { command result_t init();
{ provides interface StdControl ; command result_t start();
command result_t stop();
uses interface Timer ; }
interface Timer
{ command result_t start(char type, uint32_t, interval);
command result_t stop();
event result_t fired();
}
implementation
{ command result_t StdControl.init() {
call Leds.init();
return SUCCESS;
}
command result_t StdControl.start() {
return call Timer.start(TIMER_REPEAT, 1000);
}
command result_t StdControl.stop() {
return call Timer.stop();
}
event result_t Timer.fired() {
call Leds.yellowToggle();
return SUCCESS;
}
}
Message Communication Example
• A component “MsgComm”
• A example application which shows a message
communication in TinyOS
• http://bingweb.binghamton.edu/~joh3/CS580S/MsgComm.tgz
MsgComm
• MsgComm.h
//Message id for this application
enum{ AM_MYMSG = 10 };
• MsgComm.nc
Includes MsgComm;
configuration MsgComm {
}
Implementation{
components Main, MsgCommM, TimerC, Photo, GenericComm as Comm;
Main.StdControl → MsgCommM.StdControl;
Main.StdControl → TimerC;
Main.StdControl → Photo;
Main.StdControl → Comm;
MsgCommM.ADC → Photo;
MsgCommM.Timer → TimerC.Timer[unique (“Timer”)];
MsgCommM.SendMsg → Comm.SendMsg[AM_MYMSG];
MsgCommM.ReceiveMsg → Comm.ReceiveMsg[AM_MYMSG];
}
MsgComm (cont.)
• MsgCommM.nc (1)
Includes MsgComm; command result_t StdControl.init()
{
module MsgCommM { atomic ch_busy = FALSE;
provides interface StdControl; return SUCCESS;
} // end of StdContro.init()
uses interface SendMsg;
uses interface ReceiveMsg;
uses interface ADC; command result_t StdControl.start()
uses interface Timer; {
} return call Timer.start(TIMER_REPEAT, 1000);
} // end of StdControl.start()
implementation
{ command result_t StdControl.stop()
bool ch_busy; {
TOS_Msg tos_msg_buf; return call Timer.stop();
MsgCommMsg* send_buf; }//end of StdControl.stop()
MsgCommMsg msg_buf;
event result_t Timer.fired()
{
call ADC.getData();
return SUCCESS;
} // end of Timer.fired()
MsgComm (cont.)
• MsgCommM.nc (2)
task void sendData()
{
atomic
{
if(!ch_busy)
{
send_buf = (MsgCommMsg*)tos_msg_buf.data;
send_buf->src = msg_buf.src;
send_buf->reading = msg_buf.reading;
post sendData();
return m;
}// end of ReceiveMsg.receive()
} // end of implementation
Multihop Routing in TinyOS
• Uses a shortest-path-first algorithm with a single
destination node (the root) and active two-way link
estimation
• MultiHopEngineM: the overall packet movement logic for
multi-hop functionality (determines the next-hop path)
• MultiHopLEPSM
• the Link Estimation and Parent Selection(LEPS) mechanisms
• You can import your own link estimation and parent selection
mechanism instead of using this module
• Works on real motes, not on TOSSIM
• MultiHopRouter: connects above two components
• Reference:
http://www.tinyos.net/tinyos-1.x/doc/multihop/multihop_r
outing.html
• Multihop component : /opt/tinyos-1.x/lib/Route
• example application: Surge
• /opt/tinyos-1.x/apps/Surge
Next
• TinyOS Program Architecture
• NesC application: interfaces and components
• Concurrency Model: Two-level scheduling
• Component Development
• TinyOS installation
• Configuration file: wiring
• Module file: application code
• Why simulation?
• Simulation provides:
• controlled, reproducible testing environment
• means to explore and improve design space
• TOSSIM
• P. Levis, N. Lee, M. Welsh, and D. Culler,
TOSSIM: Accurate and Scalable Simulation of Entire Tiny
OS Applications,
ACM Conference on Embedded Networked Sensor
Systems (SenSys), November 2003.
Running on a PC: TOSSIM (cont.)
• TOSSIM
• TinyOS mote simulator
• Manual: /opt/tinyos-1.x/doc/nido.pdf
TOSSIM Architecture
How to use TOSSIM?
• Generate Makefile # Makefile for Blink
• component name COMPONENT=Blink
• include ../Makerules # PFLAGS = -I%T/lib/Counters
• optional flags Include ../Makerules
• Available DBG_[mode]
• all, boot, clock, task, sched, sensor, led, crypto,
route, am, crc, packet, encode, radio, logger, etc.
• PowerTossim
• a power modeling extension to TOSSIM developed by
Harvard University
• Models power consumed by TinyOS applications
• Integrated into TOSSIM in the TinyOS 1.1.10 or above
• PowerTossim references
• http://www.eecs.harvard.edu/~shnayder/ptossim/
PowerTossim (cont.)
• Enable power mode for dbg
export DBG=power
• Run your application with the –p flag and save the output to
a file
./build/pc/main.exe –t=60 –p 2 > out.trace
• Then
PowerTossim (cont.)
• You will see
Let’s run MsgComm!
• http://bingweb.binghamton.edu/~joh3/CS580S
• Download MsgComm.tgz, upzip and compile it
Let’s run MsgComm! (cont.)
• Run!
← Compile on the PC
From DBG_POWER
From DBG_USR1
Let’s run MsgComm! (cont.)
• 10output.power