TinyOS and NesC MANJUNATH D, CS 4222 SEMESTER II, 2011/2012 Hardware: Motes and Sensors Configuration of a typical mote Sensors TelosB 16-bit 8 MHz processor 10 KB RAM Microcontroller (TIMSP430) Smart dust 48 KB internal flash 12-bit ADC Radio transceiver of 250 Kbps 2 AA batteries SBT30 sensor TinyOS and NesC: Installation An overview of the steps involved in using TinyOS and NesC PC-side Install TinyOS and NesC Write and compile your programs Test your executables in a simulation environment Finally, executables are loaded on to the mote devices Mote-side Loaded executables are executed on the mote devices TinyOS and NesC: PC-Side Installation On the PC-side, we encourage you to use Ubuntu Linux: http://www.ubuntu.com/ A three-step process for installation Edit the file “/etc/apt/sources.list” to include the following line “deb http://tinyos.stanford.edu/tinyos/dists/ubuntu <version_name> main” Command “lsb_release –a” will tell you the <version_name> Execute commands “apt-get update” and “apt-get install tinyos-2.1.1” Set environment variables Add the line “source /opt/tinyos-2.1.1/tinyos.sh” to “/home/xxx/.bashrc” TinyOS and NesC: PC-Side Installation (Contd..) Installation is somewhat painful on other OSs Every package has to be installed manually Details for Windows, Redhat Linux, and MAC can be found here http://docs.tinyos.net/index.php/Getting_started Learning Unix commands is inevitable as Cygwin is mandatory on Windows so why not Ubuntu Linux ? You can use Ubuntu Linux’s virtual image containing TinyOS and NesC on Windows http://docs.tinyos.net/index.php/Getting_started TinyOS and NesC: PC-Side Installation (Contd..) Directory structure Parent directory /opt/tinyos-2.1.1/ Applications /opt/tinyos-2.1.1/apps/ System libraries /opt/tinyos-2.1.1/tos/ Supporting tools /opt/tinyos-2.1.1/support/ TinyOS: Design Process management Application plus OS is a single program and a single process Application+OS Main() { } func1(){ } func2(){ } func3(){ } func4(){ } funcN(){ } Image of a TinyOS Program TinyOS: Design Process management Multiprocessing is expensive in terms of memory RAM 0X00000 Process 1 Process 2 Process 3 Process N stack stack stack stack Global variables (BSS) Each process has to be allocated with a separate stack memory Stack TinyOS’s single stack Only interrupts are allowed to preempt the execution TinyOS: Design Memory management Virtual memory is not supported Memory is not protected File systems Proposed file systems are not popular TinyOS: Components TinyOS is a library of components A specific component implements a specific set of services A component in turn can be a collection of sub-components Routing Network MAC Temperature Light Sensors Microphone Radio USB Flash TinyOS Leds TinyOS: Components (Contd..) Building an application over TinyOS AppC TinyOS Routing Network MAC Temperature Light Sensors Microphone Radio USB Flash Leds TinyOS: Components (Contd..) Component interfaces Component services are accessed via interfaces Send Network Receive TinyOS: Components (Contd..) Interface commands Instruct components to perform its specific tasks getTemperature getLight Sensors SampleSensors getMagnetometer getAccelerometer TinyOS: Components (Contd..) Interface events Applications are responsible for specifying an action on an occurrence of an event Network Receive PacketReceive Application Send sendDone Application Events are also essential as system calls in TinyOS are non-blocking TinyOS: Components (Contd..) A few commonly used TinyOS components LedsC TimerC ActiveMessageC Some components to sample sensors TinyOS: Components (Contd..) LedsC Allows to control the 3 LEDS on the motes Provided interfaces Leds Commands • • • • • • led0Toggle() led1Toggle() led2Toggle() ledoOn led0Off ……… TinyOS: Components (Contd..) TimerC Lets to carryout a task after a specified interval or periodically Provided interfaces Timer Commands • • • • startPeriodic(int period_millisec) startOneShot(int period_millisec) stop() …… Events • fired() TinyOS: Components (Contd..) ActiveMessageC (TinyOS’s Network component) Allows to communicate over the radio Provided interfaces AMSend Commands • send(destination, packet, size) Events • sendDone(packet, error) Receive Events • receive(packet, payload, size) NesC: Design NesC is designed to implement TinyOS components NesC is a pre-processor to the C compiler NesC program NesC preprocessor C program C compiler (gcc) Binary for the mote platform NesC Compilation Process NesC Programs over TinyOS The best way to learn NesC is to dig into a few examples of NesC programs over TinyOS Hello-world program A simple application that toggles (blinks) the red LED on a mote once in every 2 seconds Configuration component components LedsC, TimerC, MainC; components BlinkC; implementation { BlinkC.Leds -> LedsC; BlinkC.Timer -> TimerC; BlinkC.Boot -> MainC; } LedsC interfaces: Leds commands: led0Toggle() led1Toggle()….. TimerC interfaces: Timer commands: startPeriodic() events: fired() Module component (BlinkC) uses { interface Leds; interface Timer; interface Boot; } implementation { event Boot.booted() { call Timer.startPeriodic(2000) ; } event Timer.fired() { call Leds.led0Toggle() ; } } NesC Programs over TinyOS (Contd..) Compile and upload the Blink application Include a Makefile containing the following two lines COMPONENT=BlinkAppC include $(MAKERULES) Compilation Type make telosb in the same directory where the Makefile and your application components are located Uploading an exe image on to a mote Type make telosb reinstall.NODEID bsl,addr_usb_port in the same directory Command “motelist” will give you the addr_usb_port e.g., make telosb reinstall.1 bsl,/dev/ttyUSB0 NesC Programs over TinyOS (Contd..) A simple networking example A packet is communicated from a sender to a receiver with the sender turning ON the red LED after transmission and the receiver switches ON the green LED on receiving the packet Configuration component components ActiveMessageC, LedsC, MainC; ActiveMessageC interface: AMSend commands: send() events: sendDone() interface: Receive events: receive () interface: SplitControl commands: start() events: startDone() LedsC interfaces: Leds commands: led0Toggle() led1Toggle()….. Module component uses { interface AMSend; interface Receive; interface SplitControl; interface Boot; } implementation { message_t packet; event Boot.booted() { call SplitControl.start() } event SplitControl.startDone() { call AMSend.send(2, &packet, 10); } Configuration component Module component components ActiveMessageC, LedsC, MainC; event AMSend.sendDone(packet, error) { if(error == SUCCESS) { call Leds.led0On(); } else { // Retransmit if you need } } ActiveMessageC interface: AMSend commands: send() events: sendDone() interface: Receive events: receive () interface: SplitControl commands: start() events: startDone() LedsC interfaces: Leds commands: led0Toggle() led1Toggle()….. event Receive.recevie(packet, payload, size) { call Leds.led1On(); } } Configuration component components ActiveMessageC, LedsC, MainC; components NetC; implementation { NetC.AMSend -> ActiveMessageC.AMSend; NetC.Receive -> ActiveMessageC.Receive; NetC.SplitControl -> ActiveMessageC; NetC.Boot -> MainC; } Module component (NetC) uses { interface AMSend; interface Receive; interface SplitControl; interface Boot; } implementation { message_t packet; event Boot.booted() { call SplitControl.start() } event SplitControl.startDone() { call AMSend.send(2, &packet, 10); } Configuration component components ActiveMessageC, LedsC, MainC; components NetC; implementation { Module component (NetC) uses { interface interface interface interface } AMSend; Receive; SplitControl; Boot; NetC.AMSend -> ActiveMessageC.AMSend; NetC.Receive -> ActiveMessageC.Receive; NetC.SplitControl -> ActiveMessageC; NetC.Boot -> MainC; } event Receive.recevie(packet, payload, size) { call Leds.led1On(); } Configuration component components ActiveMessageC, LedsC, MainC; components NetC; implementation { Module component (NetC) uses { interface interface interface interface } AMSend; Receive; SplitControl; Boot; NetC.AMSend -> ActiveMessageC.AMSend [240]; NetC.Receive -> ActiveMessageC.Receive[240]; NetC.SplitControl -> ActiveMessageC; NetC.Boot -> MainC; } event Receive.recevie(packet, payload, size) { call Leds.led1On(); } NesC Programs over TinyOS (Contd..) An example of serial communication over USB Send a command from a PC to a mote that triggers the mote to send a packet back to the PC (PC mote communication) SF-client SF SF protocol SF-client PC Required set-up for serial communication mote NesC Programs over TinyOS (Contd..) Executing serial communication programs on the PC-side SF is a gateway program that lets multiple clients to communicate with a mote ./sf tcp_port_num usb_dev_addr telosb Two programs constituting the SF-client ./send host_addr sf_port_num input_values ./receive host_addr sf_port_num These programs are available in C, Python, and Java You do not have to learn any of these languages – knowing to execute their programs is sufficient !!!! NesC Programs over TinyOS (Contd..) TinyOS component to be used on the mote-side SerialActiveMessageC (Very similar to the ActiveMessageC) Provided interfaces AMSend Receive SplitControl Configuration component components SerialActiveMessageC, Module component (SerialC) uses { components SerialC, LedsC, MainC; implementation { interface AMSend; interface Receive; SerialC.AMSend -> interface SplitControl; SerailActiveMessageC.AMSend[240]; SerialC.Receive -> SerialActiveMessageC.Receive[240]; SerialC.SplitControl -> SerialActiveMessageC; SerialC.Boot -> MainC; } event AMSend.sendDone(packet, error) { if(error == SUCCESS) { call Leds.led0On(); } else { // Retransmit if you need } } } interface Boot; } implementation { message_t packet; event Boot.booted() { call SplitControl.start(); } event SplitControl.startDone() { } event Receive.recevie(packet, payload, size) { call Leds.led1On(); call AMSend.send(addr, &packet, 10); } Programming Assignment 1 A simple application aimed to warm you up for more programming on TinyOS/NesC Students mainly learn Basic architecture of TinyOS/NesC Compilation and downloading of TinyOS code on to motes PC<->Mote communication Application is to turn a desired LED(s) ON/OFF for a desired duration of time Application involves programming on both PC- and mote-side Programming Assignment 1 (Contd..) PC-side Design a client that communicates a few parameters to the mote and displays messages received from the mote A command line interface is sufficient, no need for GUI Mote-side Program should receive the parameters that the client transmits and send an ACK back to the client LEDS must be controlled as instructed by the input parameters Programming Assignment 1 (Contd..) Submission Submit your code zipped/tarred to IVLE workbin The code should be compilable and include a README file explaining how to compile and how the program works Grading 25 points – Correct choice of components and compilation 25 points – PC to mote communication 25 points – Mote to PC communication 25 Points – Desired control of LEDS Weightage towards final assessment is 5%