WSN Training: TinyOS/nesC Basic Concepts TinyOS and nesC

Slides:



Advertisements
Similar presentations
How to use TinyOS Jason Hill Rob Szewczyk Alec Woo David Culler An event based execution environment for Networked Sensors.
Advertisements

NesC Prepared for the Multimedia Networks Group University of Virginia.
Feb 2007WSN Training: First Steps in nesC Programming1 First Steps in TinyOS and nesC Programming Topics  Timer Application: MyApp  Application directory.
1 Lab 3 Objectives  Case study: “Hello world” program on motes  Write you first program on mote.
Chapter 6: Process Synchronization
1 Lab4 Objectives  Learn to read light sensor data from sensor board  Learn to transmit a message containing the sensed data  through Mote serial port.
Mote Programming. 如何 compile 程式  make [re]install. : desired device address : target platform  install vs. reinstall install : compile the application.
Feb 2007TinyOS/nesC Basic Concepts1 Introduction to TinyOS Programming Topics  TinyOS execution model  Async and atomic code  Configuration and wiring.
TinyOS Introduction Advanced Computer Networks. TinyOS Outline  Introduction to the Architecture of TinyOS and nesC  Component Model –Components, interfaces,
Static Analysis of Embedded C Code John Regehr University of Utah Joint work with Nathan Cooprider.
Contiki A Lightweight and Flexible Operating System for Tiny Networked Sensors Presented by: Jeremy Schiff.
Systems Wireless EmBedded nesC Update Eric Brewer with help from David Culler, David Gay, Phil Levis, Rob von Behren, and Matt Welsh.
How to Code on TinyOS Xufei Mao Advisor: Dr. Xiang-yang Li CS Dept. IIT.
Sample Project Ideas KD Kang. Project Idea 1: Real-time task scheduling in TinyOS EDF in TinyOS 2.x –Description is available at
Programming Motes A TinyOS and TOSSIM Tutorial By: Brent Rood.
Development of a Mica2 Mote Sensor Network Cliff Macklin Bill Ehrbar December 8, 2004 University of Colorado, Colorado Springs.
2008EECS Embedded Network Programming nesC, TinyOS, Networking, Microcontrollers Jonathan Hui University of California, Berkeley.
NesC: 1.1 Bumps and Future Directions David Gay, Intel Research, Berkeley (and the nesC and TinyOS teams)
Chapter 13 Embedded Operating Systems Eighth Edition By William Stallings Operating Systems: Internals and Design Principles.
1 Lab 3 Objectives  Case study: “Hello world” program on motes  Write you first program on mote.
1 Software Development Infrastructure for Sensor Networks  Operating systems ( TinyOS )  Resource (device) management  Basic primitives  Protocols.
The University of Iowa. Copyright© 2005 A. Kruger 1 Introduction to Wireless Sensor Networks TinyOS Overview 10 February 2005.
Programming in nesC (and TOSSIM)
By: R Jayampathi Sampath
April 15, 2005TinyOS: A Component Based OSPage 1 of 27 TinyOS A Component-Based Operating System for Networked Embedded Systems Tom Bush Graduate College.
1 Lab2 Objectives  Basics of TinyOS  Basics of nesC programming language.
1 Lab2 Objectives  Basics of TinyOS  Basics of nesC programming language.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Dhanshree Nimje Smita Khartad
CIS 798 Sensor Network Implementation. Goals Learning sensor network programming with Crossbow motes Implement reasonable sized sensor applications Develop.
Lab 3 Introduction to TinyOS and nesC How to debug programs at PC Examples –Blink Timer –Blink –Hellow World Reference: 1.x/doc/tutorial/lesson1.html.
Simulation of Distributed Application and Protocols using TOSSIM Valliappan Annamalai.
HANBACK ELECTRONICS CO., LTD. 저자권 보호됨 TinyOS & NesC.
Part 2 TinyOS and nesC Programming Selected slides from:
Feb 2007WSN Training: Creating a Simple Sensor Application1 A TinyOS Sensor Application called MyApp Objectives  How to create a simple Mote firmware.
1 Threads, SMP, and Microkernels Chapter Multithreading Operating system supports multiple threads of execution within a single process MS-DOS.
WSN Software Platforms - concepts Vinod Kulathumani Lecture uses some slides from tutorials prepared by authors of these platforms.
1 Lab2 Objectives  Basics of TinyOS  Basics of nesC programming language.
Lab 3, Part 2 Selected slides from: Wireless Sensor Networks Hardware/Software Tiny OS & NesC Programming borrowed from Turgay Korkmaz.
TinyOS Sandeep Gupta. Operating System (OS) What is an OS? Main functions  Process management  Memory management  Resource management Traditional OSs.
HANBACK ELECTRONICS CO., LTD. 저자권 보호됨 Lab1: LED Control ZigbeX mote has Red, Yellow, Green LED. This lab using LED control component provided by TinyOS.
Based on slides from Andreas Larsson Table from CY Chong, SP Kumar, BA Hamilton - Proceedings of the IEEE, 2003.
Slides created by: Professor Ian G. Harris Operating Systems  Allow the processor to perform several tasks at virtually the same time Ex. Web Controlled.
Blink Blink.nc configuration Blink { } implementation { components Main, BlinkM, SingleTimer, LedsC; Main.StdControl -> BlinkM.StdControl; Main.StdControl.
Feb 2007WSN Training: XMesh Enabled Sensor App1 Lab 5 Objectives  Use XMesh multi-hop networking service to send sensing data to a base station  Using.
Why does it need? [USN] ( 주 ) 한백전자 Background Wireless Sensor Network (WSN)  Relationship between Sensor and WSN Individual sensors are very limited.
Lecture 1 Page 1 CS 111 Summer 2013 Important OS Properties For real operating systems built and used by real people Differs depending on who you are talking.
TinyOS Sandeep Gupta. TinyOS basics TinyOS is  Single tasking OS  Interrupt driven Written using a Component based language A set of components put.
TinyOS and nesC. Outline ● Wireless sensor networks and TinyOS ● Networked embedded system C (nesC) – Components – Interfaces – Concurrency model – Tool.
Introduction to Operating Systems Concepts
Computer System Structures
Processes and threads.
Simulation of Distributed Application and Protocols using TOSSIM
Process Management Process Concept Why only the global variables?
WSN Training: XMesh Enabled Sensor App
An Introduction to nesC
TinyOS CSE466 Final Project Presentation
Lecture Topics: 11/1 General Operating System Concepts Processes
Concurrency: Mutual Exclusion and Process Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
CSE 451: Operating Systems Autumn 2001 Lecture 2 Architectural Support for Operating Systems Brian Bershad 310 Sieg Hall 1.
CSE 451: Operating Systems Winter 2007 Module 2 Architectural Support for Operating Systems Brian Bershad 562 Allen Center 1.
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
CS510 Operating System Foundations
CSE 153 Design of Operating Systems Winter 19
Foundations and Definitions
CSE 153 Design of Operating Systems Winter 2019
Modeling Event-Based Systems in Ptolemy II EE249 Project Status Report
In Today’s Class.. General Kernel Responsibilities Kernel Organization
Chapter 13: I/O Systems “The two main jobs of a computer are I/O and [CPU] processing. In many cases, the main job is I/O, and the [CPU] processing is.
Presentation transcript:

WSN Training: TinyOS/nesC Basic Concepts TinyOS and nesC Feb 2007 CS6501 – Embedded OS TinyOS and nesC TinyOS nesC Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 TinyOS and nesC TinyOS: operating system for sensor networks nesC: Programming language for sensor networks Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 Why TinyOS? Traditional OSes are not suitable for networked sensors Characteristics of networked sensors Small physical size & low power consumption Software must make efficient use of processor & memory, enable low power communication Concurrency intensive Simultaneous sensor readings, incoming data from other nodes Many low-level events, interleaved w/ high-level processing Limited physical parallelism (few controllers, limited capability) Diversity in design & usage Software modularity – application specific TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 TinyOS solution Support concurrency event-driven architecture Software modularity application = scheduler + graph of components A component contains commands, event handlers, internal storage, tasks Efficiency: get done quickly and then sleep Static memory allocation TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

TinyOS computational concepts WSN Training: TinyOS/nesC Basic Concepts Feb 2007 TinyOS computational concepts Events Time critical Caused by interrupts (Timer, ADC, Sensors) Short duration Commands Request to a component to perform service (e.g, start sensor reading) Non-blocking, need to return status Postpone time-consuming work by posting a task (split phase w/ callback event) Can call lower-level commands Tasks Time flexible (delayed processing) Run sequentially by TOS Scheduler Run to completion with respect to other tasks Can be preempted by events TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

TinyOS Execution Model WSN Training: TinyOS/nesC Basic Concepts Feb 2007 TinyOS Execution Model TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 Concurrency Two threads of execution Tasks deferred execution tasks cannot preempt other tasks Hardware event handler: respond to interrupts Interrupts can preempt tasks Scheduler Two level scheduling interrupts (vector) and tasks (queue) Task queue is FIFO Scheduler puts processor to sleep when no event/command is running and task queue is empty TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

TinyOS Execution Model (revisited) WSN Training: TinyOS/nesC Basic Concepts Feb 2007 TinyOS Execution Model (revisited) TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

TinyOS Theory of Execution: Events & Tasks WSN Training: TinyOS/nesC Basic Concepts Feb 2007 TinyOS Theory of Execution: Events & Tasks Consequences of an event Runs to completion Preempt tasks What can an event do? signal events call commands post tasks Consequences of a task No preemption mechanism Keep code as small execution pieces to not block other tasks too long To run a long operations, create a separate task for each operation, rather than using on big task What can initiate (post) tasks? Command, event, or another task TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 TinyOS summary Component-based architecture Provides reusable components Application: graph of components connected by “wiring” Three computational concepts Event, command, task Tasks and event-based concurrency Tasks: deferred computation, run to completion and do not preempt each other Tasks should be short, and used when timing is not strict Events: run to completion, may preempt tasks Events signify completion of a (split-phase) operation or events from the environment (e.g., hardware, receiving messages) TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

nesC: A programming language for sensor networks WSN Training: TinyOS/nesC Basic Concepts Feb 2007 nesC: A programming language for sensor networks Main features Support and reflect TinyOS’s design Support components, event-based concurrency model Extending C with support for components Components provide and use interfaces Application: wiring components using configurations Whole-program analysis & optimization Detect race conditions Static language No dynamic memory allocation, call-graph fully known at compilation No multiprogramming Each mote runs a single application TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 nesC model Application: graph of components Main component init, start, stop first component executed Other components Components modules configurations Interfaces: point of access to components uses provides Application Component D Component A Component C Component B Component F Component E configuration TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 nesC Component Types Components Modules Configurations Provides, uses, and implements interface(s) Contains C-like code A collection of other components put together by “wiring” syntax. Does not use C-like code Can also provide and use interfaces TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

Why modules and configurations? WSN Training: TinyOS/nesC Basic Concepts Feb 2007 Why modules and configurations? Allow a developer to “snap together” applications using pre-build components without additional programming. For example, a developer could provide a configuration that simply wires together one or more pre-existing modules. The idea is for developers to provide a set of components, a “library,” that can be re-used in a wide range of applications. TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 nesC Component Specification Identified by the keyword configuration or module List of interfaces that component uses, provides Alias interfaces as new name Implementation Identified by the keyword implementation Defines internal workings May include other components and associated “wiring” Generalized component configuration|module provides uses Interface_A Interface_B Frame Functions implementation NOTE: This model applies to both modules and configurations TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

nesC Configuration – A Bare Minimum Configuration WSN Training: TinyOS/nesC Basic Concepts Feb 2007 nesC Configuration – A Bare Minimum Configuration Component configuration ConfigurationName { provides interface …; uses interface … } implementation { // wiring …… configuration provides Interface_A uses Interface_B implementation TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

nesC Module – A Bare Minimum Module WSN Training: TinyOS/nesC Basic Concepts Feb 2007 nesC Module – A Bare Minimum Module Component module ModuleName { provides interface StdControl; } implementation { // ========== FRAME ========= // ========== FUNCTIONS ===== command result_t StdControl.init() { return SUCCESS; command result_t StdControl.start() { command result_t StdControl.stop() { module provides Interface_A uses Interface_B implementation Frame Functions TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

Example: Blink Configuration WSN Training: TinyOS/nesC Basic Concepts Feb 2007 Example: Blink Configuration configuration Blink { } implementation { components Main, BlinkM, SingleTimer, LedsC; Main.StdControl -> SingleTimer.StdControl; Main.StdControl -> BlinkM.StdControl; BlinkM.Timer -> SingleTimer.Timer; BlinkM.Leds -> LedsC; TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 Example: Blink Module module BlinkM { provides { interface StdControl; } uses { interface Timer; interface Leds; implementation { command result_t StdControl.init() { call Leds.init(); return SUCCESS; TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

Example: Blink Module (cont’d) WSN Training: TinyOS/nesC Basic Concepts Feb 2007 Example: Blink Module (cont’d) command result_t StdControl.start() { // Start a repeating timer that fires every 1000ms 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; TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 nesC Interface define “public” methods that a component can use contain one or more commands and/or events group functionality, e.g., Standard control interface Split-phase operation interface StdControl { command void init(); command void start(); command void stop(); } interface Send { command void send(TOS_Msg *m); event void sendDone(); } TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

Interface : provides & uses WSN Training: TinyOS/nesC Basic Concepts Feb 2007 Interface : provides & uses Provides: Exposes functionality to others Uses: Requires another component Interface provider must implement commands Interface user must implement events Commands Events Commands Event Provides Interface_A Interface_B configuration|module Functions Frame Uses Interface_C Command Event TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

nesC Interface Examples WSN Training: TinyOS/nesC Basic Concepts Feb 2007 nesC Interface Examples interface StdControl { command void init(); command void start(); command void stop(); } module MyAppM { provides interface StdControl; uses interface ADC; uses interface Timer; uses interface Send; } implementation { … interface ADC { command void getData(); event void dataReady(int data); } interface Timer { command void start(int interval); command void stop(); event void fired(); } interface Send { command void send(TOS_Msg *m); event void sendDone(); } Questions: what need to be implemented by MyApp? TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

Interfaces: in /MoteWorks/tos/interfaces/ WSN Training: TinyOS/nesC Basic Concepts Feb 2007 Interfaces: in /MoteWorks/tos/interfaces/ TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

nesC Module Implementation WSN Training: TinyOS/nesC Basic Concepts Feb 2007 nesC Module Implementation Frame Global variables and data One per component Statically allocated Fixed size Functions Implementation of: Commands, events, tasks Commands and Events are simply C function calls Component Frame Functions provides module implementation uses Interface_A Interface_B TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 Frame module fooM { provides interface I1; uses interface I2; } implementation { uint8_t count=0; command void I1.increment() { count++; event uint8_t I2.get() { return count; Call I1.increment(); //first call Call I1.increment(); //second call signal I2.get(); //return 2 Modules maintain local and persistent state Module states or variables are statically allocated Each Module has a separate variable name space Variables declared in Command and Event are local and allocated on the stack TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 What can a module do? module MyAppM { provides interface StdControl; uses interface Clock; … } implementation { command result_t StdControl.init(){ call Clock.setRate(TOS_I1PS, TOS_S1PS); }… Calling commands & signaling events module MyAppM { … } implementation { task void processing(){ if(state) call Leds.redOn(); else call Leds.redOff(); event result_t Timer.fired() { state = !state; post processing(); return SUCCESS; }… Posting tasks TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 nesC Configuration wiring together other components No code, just wiring GenericConfiugration.nc GenericConfiguration.nc configuration D { provides interface_A; uses interface_B; } implementation { components C1, C2; X = C1.X; X = C2.X; configuration provides Interface_A uses Interface_B implementation C1 D C2 TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 nesC Wiring Syntax Binds (connects) the User to an Provider’s implementation User.interface -> Provider.interface Example MyApp_Timer.Timer -> TimerC.Timer[unique("Timer")]; Connected elements must be compatible Interface to interface, command to command, event to event Example: you can only wire interface Send to Send, but cannot connect Send to Receive TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

Three nesC Wiring Statements WSN Training: TinyOS/nesC Basic Concepts Feb 2007 Three nesC Wiring Statements configuration C { provides interface X as Xprovider; uses interface X as Xuser; } implementation { Xuser = Xprovider; C.nc Alias or pass through linkage endpoint1 = endpoint2 Direct linkage, style 1 endpoint1 -> endpoint2 Direct linkage, style 2 endpoint1 <- endpoint2 which is equivalent to: endpoint2 -> endpoint1 D C C1 C2 D1 D2 TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

How mote application starts? WSN Training: TinyOS/nesC Basic Concepts Feb 2007 How mote application starts? int main() { call hardwareInit(); //init hardware pins TOSH_sched_init(); //init scheduler call StdControl.init(); //init user app call StdControl.start(); //start user app while(1) { TOSH_run_task(); //infinite spin loop for task } TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

nesC Filename Convention WSN Training: TinyOS/nesC Basic Concepts Feb 2007 nesC Filename Convention nesC file suffix: .nc C is for configuration (Clock, ClockC) “C” distinguishes between an interface and the component that provides it M is for module (Timer, TimerC, TimerM) “M” when a single component has both configuration and module TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

Dealing with Concurrency in nesC WSN Training: TinyOS/nesC Basic Concepts Feb 2007 Dealing with Concurrency in nesC TinyOS two execution threads: Tasks and interrupts Tasks cannot preempt other tasks Interrupts can preempt tasks Race condition Concurrent update to shared state Any update to shared state that is reachable from asynchronous code is a potential race condition Asynchronous code Code that is reachable from at least one interrupt handler Synchronous code Commands, events, or tasks only reachable from tasks TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 atomic Keyword atomic: denote a block of code that runs uninterrupted (interrupts disabled) Prevents race conditions When should it be used? Required to update global variables that are referenced in async event handlers Must use atomic block in all other functions and tasks where variable is referenced nesC compiler generates warning messages for global variables that need atomic blocks, e.g., SensorAppM.nc:44: warning: non-atomic accesses to shared variable ‘voltage’ TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

atomic Keyword (cont’d) WSN Training: TinyOS/nesC Basic Concepts Feb 2007 atomic Keyword (cont’d) Disables all interrupts, therefore use with caution and intention Affects code block within {…} that follows it Useful for locking a resource Example Compiles to: cli(); // disable interrupts lda r1, busy // load busy to register jnz r1, inUse // check busy str busy, 1 // set busy to true inUse: sbi(); // enable interrupts atomic { if (!busy) busy = TRUE; } No interrupts will disrupt code flow TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 atomic Syntax Example From MoteWorks/tos/system/TimerM.nc TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 async Syntax async attribute used to indicate that command or event is part of an asynchronous flow async processes are “decoupled” by posting a task (to return quickly) tinyos-1.x/tos/system/TimerM.nc async event result_t Clock.fire() { post HandleFire(); return SUCCESS; } Post task “decouples” the async event TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

nesC Features for Concurrency WSN Training: TinyOS/nesC Basic Concepts Feb 2007 nesC Features for Concurrency post Puts a function on a task queue Must be void foo(void) atomic Turn off interrupts async Use async to tell compiler that this code can be called from an interrupt context – used to detect potential race conditions norace Use norace to tell compiler it was wrong about a race condition existing (the compiler usually suggests several false positives) task void do-work() { //declares a task that does work} post do-work(); TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 Concurrency Example module SurgeM {…} implementation { bool busy; norace unit16_t sensorReading; event result_t Timer.fired() { bool localBusy; atomic { localBusy = busy; busy = TRUE; } if(!localBusy) call ADC.getData(); return SUCCESS; TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

Concurrency Example (cont’d) WSN Training: TinyOS/nesC Basic Concepts Feb 2007 Concurrency Example (cont’d) task void sendData() { adcPacket.data = sesnorReading; call Send.send(&adcPacket, sizeof adcPacket.data); return SUCCESS; } event result_t ADC.dataReady(unit16_t data) { sensorReading = data; post sendData(); … TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

WSN Training: TinyOS/nesC Basic Concepts Feb 2007 nesC Keywords Keyword Description component Building blocks of a nesC application. Can be a module or a configuration module A basic component implemented in nesC configuration A component made from wiring other components interface A collection of event and command definitions implementation Contains code & variables for module or configuration provides Defines interfaces provided by a component uses Defines interfaces used by a module or configuration as Alias an interface to another name command Direct function call exposed by an interface event Callback message exposed by an interface TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

Appendix: TinyOS Programming Tips WSN Training: TinyOS/nesC Basic Concepts Feb 2007 Appendix: TinyOS Programming Tips By Phil Levis guide to TinyOS 2.0 Programming Guide Note: Not all TinyOS 2.0 concepts apply to MoteWorks Last update: June 28, 2006 Crossbow Technology, Inc. Proprietary

TinyOS Programming Hints – Condensed (1 of 4) WSN Training: TinyOS/nesC Basic Concepts Feb 2007 TinyOS Programming Hints – Condensed (1 of 4) Programming Hint 1: It’s dangerous to signal events from commands, as you might cause a very long call loop, corrupt memory and crash your program. Programming Hint 2: Keep tasks short. Programming Hint 3: Keep code synchronous when you can. Code should be async only if its timing is very important or if it might be used by something whose timing is important. Programming Hint 4: Keep atomic sections short, and have as few of them as possible. Be careful about calling out to other components from within an atomic section. Programming Hint 5: Only one component should be able to modify a pointer’s data at any time. In the best case, only one component should be storing the pointer at any time. TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

TinyOS Programming Hints – Condensed (2 of 3) WSN Training: TinyOS/nesC Basic Concepts Feb 2007 TinyOS Programming Hints – Condensed (2 of 3) Programming Hint 6: Allocate all state in components. If your application requirements necessitate a dynamic memory pool, encapsulate it in a component and try to limit the set of users. Programming Hint 7: Conserve memory by using enums rather than const variables for integer constants, and don’t declare variables with an enum type. *Programming Hint 8: In the top-level configuration of a software abstraction, auto-wire init to MainC. This removes the burden of wiring init from the programmer, which removes unnecessary work from the boot sequence and removes the possibility of bugs from forgetting to wire. *Programming Hint 9: If a component is a usable abstraction by itself, its name should end with C. If it is intended to be an internal and private part of a larger abstraction, its name should end with P. Never wire to P components from outside your package (directory). *TinyOS 2.0 specific TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

TinyOS Programming Hints – Condensed (3 of 4) WSN Training: TinyOS/nesC Basic Concepts Feb 2007 TinyOS Programming Hints – Condensed (3 of 4) Programming Hint 10: Use the as keyword liberally. Programming Hint 11: Never ignore combine warnings. Programming Hint 12: If a function has an argument which is one of a small number of constants, consider defining it as a few separate functions to prevent bugs. If the functions of an interface all have an argument that’s almost always a constant within a large range, consider using a parameterized interface to save code space. If the functions of an interface all have an argument that’s a constant within a large range but only certain valid values, implement it as a parameterized interface but expose it as individual interfaces, to both minimize code size and prevent bugs. Programming Hint 13: If a component depends on unique, then #define a string to use in a header file, to prevent bugs from string typos. *TinyOS 2.0 specific TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary

TinyOS Programming Hints – Condensed (4 of 4) WSN Training: TinyOS/nesC Basic Concepts Feb 2007 TinyOS Programming Hints – Condensed (4 of 4) *Programming Hint 14: Never, ever use the “packed” attribute. *Programming Hint 15: Always use platform independent types when defining message formats. *Programming Hint 16: If you have to perform significant computation on a platform independent type or access it many (hundreds or more) times, then temporarily copying it to a native type can be a good idea. *TinyOS 2.0 specific TinyOS/nesC Basic Concepts Crossbow Technology, Inc. Proprietary