nesc prepared for the multimedia networks group university of virginia

24
nesC nesC Prepared for the Prepared for the Multimedia Networks Multimedia Networks Group Group University of Virginia University of Virginia

Upload: beatriz-spratley

Post on 14-Jan-2016

221 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: NesC Prepared for the Multimedia Networks Group University of Virginia

nesCnesC

Prepared for the Multimedia Prepared for the Multimedia Networks GroupNetworks Group

University of VirginiaUniversity of Virginia

Page 2: NesC Prepared for the Multimedia Networks Group University of Virginia

Quick Review: TinyOSQuick Review: TinyOS

““Operating system” for wireless embedded Operating system” for wireless embedded sensor networkssensor networksActually a set of software components that Actually a set of software components that can be “wired” together into a single binary can be “wired” together into a single binary which is run on the moteswhich is run on the motesMinimal OS functionsMinimal OS functions– 2 threads of execution: tasks and hardware 2 threads of execution: tasks and hardware

event handlersevent handlers– No memory management…No memory management…

Page 3: NesC Prepared for the Multimedia Networks Group University of Virginia

Quick Review: nesCQuick Review: nesC

Pronounced “”NES-see”Pronounced “”NES-see”

Extension of CExtension of C– Supports C syntaxSupports C syntax– Compiled into CCompiled into C

““designed to embody the structuring designed to embody the structuring concepts and execution model of TinyOS”concepts and execution model of TinyOS”

Page 4: NesC Prepared for the Multimedia Networks Group University of Virginia

nesC and TinyOSnesC and TinyOS

TinyOS was originally written in C, TinyOS was originally written in C, applications were combinations applications were combinations of .c, .comp, and .desc filesof .c, .comp, and .desc files

TinyOS (components) have been TinyOS (components) have been reimplemented in nesCreimplemented in nesC

A new language for mote programming is A new language for mote programming is currently being developedcurrently being developed

nesC is a temporary solutionnesC is a temporary solution

Page 5: NesC Prepared for the Multimedia Networks Group University of Virginia

VocabularyVocabulary

Application – one or more Application – one or more componentscomponents wired wired together to form an executabletogether to form an executableComponent – basic building blocks for nesC Component – basic building blocks for nesC apps. Two types: apps. Two types: modulesmodules and and configurationsconfigurationsModule – Module – componentcomponent that implements one or that implements one or more more interfacesinterfacesConfiguration – Configuration – componentcomponent that wires other that wires other componentscomponents together togetherInterface – provides an abstract definition of the Interface – provides an abstract definition of the interaction between two interaction between two componentscomponents

Page 6: NesC Prepared for the Multimedia Networks Group University of Virginia

Visualizing modulesVisualizing modules

modules:modules:module module C1C1 { {

requires interface triangle;requires interface triangle;

} implementation { ... }} implementation { ... }

module module C2C2 { {

provides interface triangle in;provides interface triangle in;

requires {requires {

interface triangle out;interface triangle out;

interface rectangle side; }interface rectangle side; }

} implementation { ... }} implementation { ... }

module module C3C3 { {

provides interface triangle;provides interface triangle;

provides interface rectangle;provides interface rectangle;

} implementation { ... }} implementation { ... }

C1

C2

C3

Page 7: NesC Prepared for the Multimedia Networks Group University of Virginia

Visualizing configurationsVisualizing configurations

Connect configurations:Connect configurations:configuration app { }configuration app { }implementation {implementation {

uses uses c1c1, , c2c2, , c3c3;;c1 -> c2; // implicit interface sel.c1 -> c2; // implicit interface sel.c2.out -> c3.triangle;c2.out -> c3.triangle;c3 <- c2.side;c3 <- c2.side;

}}

Partial configurations:Partial configurations:component c2c3 {component c2c3 { provides interface triangle t1;provides interface triangle t1;}}implementation {implementation { uses uses c2c2, , c3c3;;

t1 -> c2.in;t1 -> c2.in;c2.out -> c3.triangle;c2.out -> c3.triangle;c3 <- c2.side;c3 <- c2.side;

}}

C1

C2

C3

C2

C3

Page 8: NesC Prepared for the Multimedia Networks Group University of Virginia

More on wiringMore on wiringconfiguration C { provides interface X;} implementation { components C1, C2; X = C1.X; C1.Y -> C2.Y; C1.Z <- C2.Z;}

““=“ used when any endpoint is external (a =“ used when any endpoint is external (a specification element – specification element – providesprovides or or usesuses))““->” or “<-” used when both endpoints are ->” or “<-” used when both endpoints are internalinternalA -> B is equivalent to B <- AA -> B is equivalent to B <- A

Page 9: NesC Prepared for the Multimedia Networks Group University of Virginia

Fan-in, fan-outFan-in, fan-outconfiguration C {

provides interface X;

} implementation {

components C1, C2;

X = C1.X;

X = C2.X;

}

Endpoints can be connected multiple timesEndpoints can be connected multiple times

In this case, multiple functions will be executed In this case, multiple functions will be executed when C.X’s commands are called and multiple when C.X’s commands are called and multiple signalers will issue callbacks for subscribers to signalers will issue callbacks for subscribers to C.X’s eventsC.X’s events

Page 10: NesC Prepared for the Multimedia Networks Group University of Virginia

Implicit connectionsImplicit connectionsconfiguration C {

} implementation {

components C1, C2;

C1 <- C2.X;

C2.Y <- C2;

C1.Z -> C2;

}

When only there is only one specification element of a When only there is only one specification element of a given type in the component being mapped to or from, it given type in the component being mapped to or from, it doesn’t need to be explicitly specified in connectionsdoesn’t need to be explicitly specified in connections

C1.X <- C2.X is equivalent to C1 <- C2.X as long as C1.X <- C2.X is equivalent to C1 <- C2.X as long as there is only one specification element of type X in C1there is only one specification element of type X in C1

Page 11: NesC Prepared for the Multimedia Networks Group University of Virginia

Interfaces, commands, eventsInterfaces, commands, events

Interfaces are bidirectional: “they specify a Interfaces are bidirectional: “they specify a set of functions to be implemented by the set of functions to be implemented by the interface’s provider (interface’s provider (commandscommands) and a set ) and a set to be implemented by the interface’s user to be implemented by the interface’s user ((eventsevents))Commands typically call downwards (from Commands typically call downwards (from application components to components application components to components closer to hardware) while events call closer to hardware) while events call upwardsupwards

Page 12: NesC Prepared for the Multimedia Networks Group University of Virginia

Example interfaceExample interface

// can include c files

interface SendMsg {

command result_t send(uint16_t address, uint8_t length, TOS_MsgPtr msg);

event result_t sendDone(TOS_MsgPtr msg, result_t success);

}

Page 13: NesC Prepared for the Multimedia Networks Group University of Virginia

More on commands and eventsMore on commands and events

Calling a commandCalling a commandint x = ...;

call Send.send[x + 1](1, sizeof(Message), &msg1);

Signaling an eventSignaling an eventint x = ...;

signal Send.sendDone[x + 1](&msg1, SUCCESS);

Page 14: NesC Prepared for the Multimedia Networks Group University of Virginia

TasksTasks

““A task is an independent locus of control defined by a function of storage class task returning void and with no arguments: task void myTask() { ... }”

“Tasks are posted by prefixing a call to the task with post, e.g., post myTask();”

Page 15: NesC Prepared for the Multimedia Networks Group University of Virginia

TinyOS Execution ContextsTinyOS Execution Contexts

Events generated by interrupts preempt tasksEvents generated by interrupts preempt tasksTasks do not preempt tasksTasks do not preempt tasksBoth essential process state transitionsBoth essential process state transitions

Hardware

Interrupts

eve

nts

commands

Tasks

Page 16: NesC Prepared for the Multimedia Networks Group University of Virginia

Atomic statementsAtomic statementsbool busy; // globalvoid f() { bool available; atomic { available = !busy; busy = TRUE; } if (available) do_something; atomic busy = FALSE;}

“guarantee that the statement is executed “as-if” no other computation occurred simultaneously”Should be short!nesC forbids: call, signal, goto, return, break, continue, case, default, label

Page 17: NesC Prepared for the Multimedia Networks Group University of Virginia

Misc: AttributesMisc: Attributes

Uses gcc’s __attribute__ syntax to declare properties of Uses gcc’s __attribute__ syntax to declare properties of functions, variables, and typedefsfunctions, variables, and typedefs““C” – element should appear in global C scope rather C” – element should appear in global C scope rather than module scopethan module scope““spontaneous” – functions only – there may be calls to spontaneous” – functions only – there may be calls to the function that don’t appear in the source code (e.g. the function that don’t appear in the source code (e.g. interrupt handlers or C main function)interrupt handlers or C main function)

module RealMain { ... } implementation { int main(int argc, char **argv) __attribute__((C, spontaneous)) { ... }}

Page 18: NesC Prepared for the Multimedia Networks Group University of Virginia

Misc: Attributes continuedMisc: Attributes continued

““combine” – specify the combining function for a combine” – specify the combining function for a type in a typedef declarationtype in a typedef declarationCombining function specifies how to combine Combining function specifies how to combine multiple results from a command or event which multiple results from a command or event which has fan-outhas fan-out

typedef uint8_t result_t __attribute__((combine(rcombine)));

result_t rcombine(result_t r1, result_t r2){ return r1 == FAIL ? FAIL : r2;}

Page 19: NesC Prepared for the Multimedia Networks Group University of Virginia

Misc: Compile time constant Misc: Compile time constant functionsfunctions

Evaluate to a constant at compile timeEvaluate to a constant at compile time

unsigned int unique(char *identifier)unsigned int unique(char *identifier)– If the program contains n calls to unique with If the program contains n calls to unique with

the same identifier string, returns a unique the same identifier string, returns a unique integer in the range 0..n-1integer in the range 0..n-1

unsigned int uniqueCount(char *identifier)unsigned int uniqueCount(char *identifier)– If the program contains n calls to unique with If the program contains n calls to unique with

the same identifier string, returns nthe same identifier string, returns n

Page 20: NesC Prepared for the Multimedia Networks Group University of Virginia

Blink ExampleBlink Example

Program toggles the red LED every Program toggles the red LED every secondsecond

Page 21: NesC Prepared for the Multimedia Networks Group University of Virginia

Blink.ncBlink.nc

configuration Blink { configuration Blink {

}}

implementation {implementation {

components Main, BlinkM, SingleTimer, LedsC;components Main, BlinkM, SingleTimer, LedsC;

Main.StdControl -> SingleTimer.StdControl;Main.StdControl -> SingleTimer.StdControl;

Main.StdControl -> BlinkM.StdControl;Main.StdControl -> BlinkM.StdControl;

BlinkM.Timer -> SingleTimer.Timer;BlinkM.Timer -> SingleTimer.Timer;

BlinkM.Leds -> LedsC;BlinkM.Leds -> LedsC;

}}

Page 22: NesC Prepared for the Multimedia Networks Group University of Virginia

BlinkM.nc (specification)BlinkM.nc (specification)

module BlinkM {module BlinkM { provides {provides { interface StdControl;interface StdControl; } uses {} uses { interface Timer;interface Timer; interface Leds;interface Leds; }}}}

Page 23: NesC Prepared for the Multimedia Networks Group University of Virginia

BlinkM.nc (implementation)BlinkM.nc (implementation)implementation {implementation {

command result_t StdControl.init() {command result_t StdControl.init() { call Leds.init();call Leds.init(); return SUCCESS;return SUCCESS; }}

command result_t StdControl.start() {command result_t StdControl.start() { // Start a repeating timer that fires every 1000ms// Start a repeating timer that fires every 1000ms return call Timer.start(TIMER_REPEAT, 1000);return call Timer.start(TIMER_REPEAT, 1000); }}

command result_t StdControl.stop() {command result_t StdControl.stop() { return call Timer.stop();return call Timer.stop(); }}

event result_t Timer.fired() {event result_t Timer.fired() { call Leds.redToggle();call Leds.redToggle(); return SUCCESS;return SUCCESS; }}}}

Page 24: NesC Prepared for the Multimedia Networks Group University of Virginia

SingleTimer.ncSingleTimer.nc

configuration SingleTimer {configuration SingleTimer {

provides interface Timer;provides interface Timer;

provides interface StdControl;provides interface StdControl;

}}

implementation {implementation {

components TimerC;components TimerC;

Timer = TimerC.Timer[unique("Timer")];Timer = TimerC.Timer[unique("Timer")];

StdControl = TimerC;StdControl = TimerC;

}}