Скачать презентацию Wireless Embedded Inter-Networking Foundations of Ubiquitous Sensor Networks Скачать презентацию Wireless Embedded Inter-Networking Foundations of Ubiquitous Sensor Networks

8688228c52e8623c2706df1b0ac66fd3.ppt

  • Количество слайдов: 81

Wireless Embedded Inter-Networking Foundations of Ubiquitous Sensor Networks Tiny. OS 2. 0 Programing – Wireless Embedded Inter-Networking Foundations of Ubiquitous Sensor Networks Tiny. OS 2. 0 Programing – An IPv 6 Kernel Approach David E. Culler University of California, Berkeley June 2008 WEI L 4 - Tiny. OS Prog 1

Example Complete Network Embedded System Flash Radio Serial Motor Light Vibration Links Sensor / Example Complete Network Embedded System Flash Radio Serial Motor Light Vibration Links Sensor / Actuator Management & Power Discovery Events Attributes Init/Boot Net Prog Network Epidemics and Routing Device Attributes & Event Streams Domain-Specific Device Drivers Device Abstraction Interface Logs Files Core OS Interface Messages Persistent Attributes & Event Streams Blocks Service Interface Commands Domain-Specific Application Components resource Microcontroller Abstraction Interface sched Telos June 2008 timer uart SPI mica. Z imote 2 WEI L 4 - Tiny. OS Prog i 2 c ADC other 2

Outline • • • Key Tiny. OS Concepts Tiny. OS Abstraction Architecture A Simple Outline • • • Key Tiny. OS Concepts Tiny. OS Abstraction Architecture A Simple Event-Driven Example Execution Model Critical system elements – Timers, Sensors, Communication • Service Architecture June 2008 WEI L 4 - Tiny. OS Prog 3

Tiny. OS 2. 0 • Primary Reference: http: //www. tinyos. net/tinyos 2. x/doc/ • Tiny. OS 2. 0 • Primary Reference: http: //www. tinyos. net/tinyos 2. x/doc/ • http: //www. tinyos. net/tinyos-2. x/doc/html/tutorial/ • http: //nescc. sourceforge. net/papers/nesc-ref. pdf June 2008 WEI L 4 - Tiny. OS Prog 4

Key Tiny. OS Concepts • Application / System = Graph of Components + Scheduler Key Tiny. OS Concepts • Application / System = Graph of Components + Scheduler • Module: component that implements functionality directly • Configuration: component that composes components into a larger component by connecting their interfaces • Interface: Logically related collection of commands and events with a strongly typed (polymorphic) signature – May be parameterized by type argument – Provided to components or Used by components • Command: Operation performed (called) across components to initiate action. • Event: Operation performed (signaled) across components for notification. • Task: Independent thread of control instantiated within a component. Non-preemptive relative to other task. • Synchronous and Asynchronous contexts of execution. June 2008 WEI L 4 - Tiny. OS Prog 5

Tiny. OS Abstraction Architecture • HPL – Hardware Presentation Layer – – Components that Tiny. OS Abstraction Architecture • HPL – Hardware Presentation Layer – – Components that encapsulate physical hardware units Provide convenient software interface to the hardware. The hardware is the state and computational processes. Commands and events map to toggling pins and wires • HAL –Hardware Abstraction Layer – Components that provide useful services upon the basic HW – Permitted to expose any capabilities of the hardware » Some platforms have more ADC channels, Timers, DMA channels, capture registers, … – Logically consistent, but unconstrained • HIL – Hardware Independent Layer – Components that provide well-defined services in a manner that is the same across hardware platforms. – Implement common interfaces over available HAL June 2008 WEI L 4 - Tiny. OS Prog 6

Illustration June 2008 WEI L 4 - Tiny. OS Prog 7 Illustration June 2008 WEI L 4 - Tiny. OS Prog 7

Tiny. OS – a tool for defining abstractions • All of these layers are Tiny. OS – a tool for defining abstractions • All of these layers are constructed with the same Tiny. OS primitives. • We’ll illustrate them from a simple application down. • Note, components are not objects, but they have strong similarities. – Some components encapsulate physical hardware. – All components are allocated statically (compile time) » Whole system analysis and optimization – Logically, all components have internal state, internal concurrency, and external interfaces (Commands and Events) – Command & Event handlers are essentially public methods – Locally scoped » Method invocation and method hander need not have same name (like libraries and objects) » Resolved statically by wiring • Permits interpositioning June 2008 WEI L 4 - Tiny. OS Prog 8

Platform is a collection of Chips • Tiny. OS 2. x components provide the Platform is a collection of Chips • Tiny. OS 2. x components provide the capabilities of the chips. • Tiny. OS 2. x components glue to together those capabilities into a consistent system. • Tiny. OS 2. x components provide higher level capabilities and services June 2008 WEI L 4 - Tiny. OS Prog 9

Overall System Configuration (std) June 2008 WEI L 4 - Tiny. OS Prog 10 Overall System Configuration (std) June 2008 WEI L 4 - Tiny. OS Prog 10

Tiny. OS IPv 6 Network Kernel • Network Kernel – Manages communication and storage Tiny. OS IPv 6 Network Kernel • Network Kernel – Manages communication and storage – Scheduler (decides when to signal events) Application Driver IPv 6 Network Kernel Radio June 2008 Timer Flash WEI L 4 - Tiny. OS Prog Driver Sensors Sensor Actuator 11

Illustration of Tiny. OS Programming Concepts June 2008 WEI L 4 - Tiny. OS Illustration of Tiny. OS Programming Concepts June 2008 WEI L 4 - Tiny. OS Prog 12

A simple event-driven module – Blink. M. nc #include A simple event-driven module – Blink. M. nc #include "Timer. h" module Blink. M { uses interface Boot; uses interface Timer as Timer 0; uses interface Leds; } implementation { event void Boot. booted() { call Timer 0. start. Periodic( 250 ); } event void Timer 0. fired() { call Leds. led 0 Toggle(); } Module name Internal name of external interface Blink. M Boot Timer 0 Leds Interfaces • Boot • Timer • Leds • Coding conventions: TEP 3 June 2008 WEI L 4 - Tiny. OS Prog 13

A simple event-drvien module (cont) #include A simple event-drvien module (cont) #include "Timer. h" module Blink. M { uses interface Boot; uses interface Timer as Timer 0; uses interface Leds; } implementation { event void Boot. booted() { call Timer 0. start. Periodic( 250 ); } event void Timer 0. fired() { call Leds. led 0 Toggle(); } June 2008 WEI L 4 - Tiny. OS Prog Blink. M Boot Timer 0 Leds ? ? ? Two Event Handlers Each services external event by calling command on some subsystem 14

Simple example: Boot interface Boot { /** * Signaled when the system has booted Simple example: Boot interface Boot { /** * Signaled when the system has booted successfully. Components can * assume the system has been initialized properly. Services may * need to be started to work, however. * * @see Std. Control * @see Split. Conrol * @see TEP 107: Boot Sequence */ event void booted(); } • • $tiny. OS-2. x/tos/interfaces/ Defined in TEP 107 – Boot Sequence Consists of a single event. Hardware and operating system actions prior to this simple event may vary widely from platform to platform. • Allows module to initialize itself, which may require actions in various other parts of the system. June 2008 WEI L 4 - Tiny. OS Prog 15

Simple example: LEDs interface #include Simple example: LEDs interface #include "Leds. h" interface Leds { async command void /* * @param */ val led 0 On(); led 0 Off(); led 0 Toggle(); led 1 On(); . . . a bitmask describing the on/off settings of the LEDs async command uint 8_t get(); async command void set(uint 8_t val); } • $tiny. OS-2. x/tos/interfaces/ • set of Commands – Cause action – get/set a physical attribute (3 bits) • async => OK to use even within interrupt handlers • Physical wiring of LEDs to microcontroller IO pins may vary => We will implement a simplified version June 2008 WEI L 4 - Tiny. OS Prog 16

Timer interface Timer<precision_tag> { command void start. Periodic(uint 32_t dt); event void fired(); command Timer interface Timer { command void start. Periodic(uint 32_t dt); event void fired(); command void start. One. Shot(uint 32_t dt); command void stop(); command command bool is. Running(); bool is. One. Shot(); void start. Periodic. At(uint 32_t t 0, uint 32_t dt); void start. One. Shot. At(uint 32_t t 0, uint 32_t dt); uint 32_t get. Now(); uint 32_t gett 0(); uint 32_t getdt(); } • • $tiny. OS-2. x/tos/lib/timer/Timer. nc Rich application timer service built upon lower level capabilities that may be very different on different platform – Microcontrollers have very idiosyncratic timers • Parameterized by precision June 2008 WEI L 4 - Tiny. OS Prog 17

Tiny. OS Directory Structure • tos/system/ - Core Tiny. OS components. This directory's – Tiny. OS Directory Structure • tos/system/ - Core Tiny. OS components. This directory's – components are the ones necessary for Tiny. OS to actually run. • tos/interfaces/ - Core Tiny. OS interfaces, including – hardware-independent abstractions. Expected to be heavily used not just by tos/system but throughout all other code. tos/interfaces should only contain interfaces named in TEPs. • tos/platforms/ - code specific to mote platforms, but chipindependent. • tos/chips/***/ - code specific to particular chips and to chips on particular platforms. • tos/lib/***/ - interfaces and components which extend the usefulness of Tiny. OS but which are not viewed as essential to its operation. • apps/, apps/demos, apps/tests, apps/tutorials. June 2008 WEI L 4 - Tiny. OS Prog 18

Kernel Overlay for this course • Trunk – Kernel » Include » Interfaces » Kernel Overlay for this course • Trunk – Kernel » Include » Interfaces » Lib » Make » Src » Tools – TOS: Tiny. OS code that we will work with » Drivers – abstractions of phyisical hardware • Epic • Telosb » Lib – Useful serves » App* – Binaries – precompiled tinyos Apps – Unix June 2008 WEI L 4 - Tiny. OS Prog 19

Timers #include Timers #include "Timer. h“ … typedef struct { } TMilli; // 1024 ticks per second typedef struct { } T 32 khz; // 32768 ticks per second typedef struct { } TMicro; // 1048576 ticks per second • Timers are a fundamental element of Embedded Systems – Microcontrollers offer a wide range of different hardware features – Idiosyncratic • Logically Timers have – Precision – Width – Accuracy - unit of time the present - # bits in the value - how close to the precision they obtain • TEP 102 defines complete Tiny. OS timer architecture • Direct access to low-level hardware • Clean virtualized access to application level timers June 2008 WEI L 4 - Tiny. OS Prog 20

Example – multiple virtual timers #include Example – multiple virtual timers #include "Timer. h" module Blink 3 M { uses interface Timer uses interface Leds; uses interface Boot; } implementation { event void Boot. booted() { call Timer 0. start. Periodic( call Timer 1. start. Periodic( call Timer 2. start. Periodic( } as Timer 0; as Timer 1; as Timer 2; 250 ); 500 ); 1000 ); event void Timer 0. fired() { call Leds. led 0 Toggle(); } event void Timer 1. fired() { call Leds. led 1 Toggle(); } event void Timer 2. fired() { call Leds. led 2 Toggle(); } } June 2008 WEI L 4 - Tiny. OS Prog 21

Composition • Our event-driven component, Blink, may be built directly on the hardware – Composition • Our event-driven component, Blink, may be built directly on the hardware – For a particular microcontroller on a particular platform • or on a simple layer for a variety of platforms • or on a full-function kernel • Or it may run in a simulator on a PC, • Or… • As long as it is wired to components that provide the interfaces that this component uses. • And it can be used in a large system or application June 2008 WEI L 4 - Tiny. OS Prog 22

Configuration Blink. App. C Blink. M Boot Timer 0 Leds configuration Blink. App. C Configuration Blink. App. C Blink. M Boot Timer 0 Leds configuration Blink. App. C Boot { Main. C } implementation { components Main. C, Blink. M, Leds. C; components new Timer. Milli. C() as Timer; Timer Leds. C Blink. M -> Main. C. Boot; Blink. M. Leds -> Leds. C; Blink. M. Timer 0 -> Timer; } • Generic components create service instances of an underlying service. Here, a virtual timer. • If the interface name is same in the two components, only one need be specified. June 2008 WEI L 4 - Tiny. OS Prog 23

A Different Configuration Blink. C Blink. M configuration blink. C{ } Boot Timer 0 A Different Configuration Blink. C Blink. M configuration blink. C{ } Boot Timer 0 Leds implementation{ components blink. M; components Main. C; components Kernel; Boot blink. M. Boot -> Kernel. Boot; blink. M. Leds -> Kernel. Leds; components new Timer. Milli. C(); blink. M. Timer 0 -> Timer. Milli. C. Timer; Timer. Millic Leds Kernel } • Same module configured to utilize a very different system substrate. June 2008 WEI L 4 - Tiny. OS Prog 24

Execution Behavior • Timer interrupt is mapped to a Tiny. OS event. – Handled Execution Behavior • Timer interrupt is mapped to a Tiny. OS event. – Handled in a safe context • Performs simple operations. • When activity stops, entire system sleeps – In the lowest possible sleep state • Never wait, never spin. Automated, wholesystem power management. June 2008 WEI L 4 - Tiny. OS Prog 25

Module state module Blink. C { uses interface Timer<TMilli> as Timer 0; uses interface Module state module Blink. C { uses interface Timer as Timer 0; uses interface Leds; users interface Boot; } implementation • Private scope • Sharing through explicit interface only! – Concurrency, concurrency! – Robustness, robustness { uint 8_t counter = 0; event void Boot. booted() { call Timer 0. start. Periodic( 250 ); • Static extent • HW independent type – unlike int, long, char } event void Timer 0. fired() { counter++; call Leds. set(counter); } } June 2008 WEI L 4 - Tiny. OS Prog 26

Tiny. OS / Nes. C Platform Independent Types • Common numeric types • Bool, Tiny. OS / Nes. C Platform Independent Types • Common numeric types • Bool, … • Network Types http: //nescc. sourceforge. net – Compiler does the grunt work to map to canonical form June 2008 WEI L 4 - Tiny. OS Prog 27

Events module Blink. M { uses interface Timer<TMilli> as Timer 0; uses interface Leds; Events module Blink. M { uses interface Timer as Timer 0; uses interface Leds; uses interface Boot; provides interface Notify as Rollover; } implementation { uint 8_t counter = 0; event void Boot. booted() • Call commands • Signal events • Provider of interface handles calls and signals events • User of interface calls commands and handles signals { call Timer 0. start. Periodic( 250 ); } event void Timer 0. fired() Notify { Rollover counter++; call Leds. set(counter); Blink. M if (!counter) signal Rollover. notify(TRUE); } Boot Timer 0 Leds } June 2008 WEI L 4 - Tiny. OS Prog 28

Examples - Event-Driven Execution t ar St t ar r St me Ti P Examples - Event-Driven Execution t ar St t ar r St me Ti P TC nd Bi June 2008 Service request recv Fire WEI L 4 - Tiny. OS Prog 29

Split-Phase Operations • For potentially long latency operations – Don’t want to spin-wait, polling Split-Phase Operations • For potentially long latency operations – Don’t want to spin-wait, polling for completion – Don’t want blocking call - hangs till completion – Don’t want to sprinkle the code with explicit sleeps and yields • Instead, – Want to service other concurrent activities will waiting – Want to go sleep if there are none, and wake up upon completion • Split-phase operation – Call command to initiate action – Subsystem will signal event when complete • The classic concurrent I/O problem, but also want energy efficiency. – Parallelism, or sleep. – Event-driven execution is fast and low power! June 2008 WEI L 4 - Tiny. OS Prog 30

Examples /* Power-hog Blocking Call */ if (send() == SUCCESS) { send. Count++; } Examples /* Power-hog Blocking Call */ if (send() == SUCCESS) { send. Count++; } /* Programmed delay */ state = WAITING; /* Split-phase call */ // start phase … call send(); … } //completion phase void send. Done(error_t err) { if (err == SUCCESS) { send. Count++; } } op 1(); sleep(500); call Timer. start. One. Shot(500); command void Timer. fired() { op 2(); state = RUNNING; June 2008 WEI L 4 - Tiny. OS Prog 31

Examples - Split-Phase ne Do ad Re WEI L 4 - Tiny. OS Prog Examples - Split-Phase ne Do ad Re WEI L 4 - Tiny. OS Prog y ad Re e pl m Sa June 2008 32

Sensor Readings • Sensors are embedded I/O devices – Analog, digital, … many forms Sensor Readings • Sensors are embedded I/O devices – Analog, digital, … many forms with many interfaces • To obtain a reading – configure the sensor » and/or the hardware module it is attached to, • ADC and associated analog electronics • SPI bus, I 2 C, UART – Read the sensor data • Tiny. OS 2. x allows applications to do this in a platform-independent manner June 2008 WEI L 4 - Tiny. OS Prog 33

Read Interface interface Read<val_t> { /* Initiates a read of the value. * @return Read Interface interface Read { /* Initiates a read of the value. * @return SUCCESS if a read. Done() event will eventually come back. */ command error_t read(); /** * Signals the completion of the read(). * * @param result SUCCESS if the read() was successful * @param val the value that has been read */ event void read. Done( error_t result, val_t val ); } • Split-phase data acquisition of typed values • Flow-control handshake between concurrent processed – Hardware or software • $tiny. OS-2. x/tos/interface/read. nc June 2008 WEI L 4 - Tiny. OS Prog 34

Example #include Example #include "Timer. h" module Sense. M { uses { interface Boot; interface Leds; interface Timer; interface Read; } } implementation { #define SAMPLING_FREQUENCY 100 event void Boot. booted() { call Timer. start. Periodic(SAMPLING_FREQUENCY); } event void Timer. fired() { call Read. read(); } event void Read. read. Done(error_t result, uint 16_t data) { if (result == SUCCESS){ call Leds. set(data & 0 x 07); } } } • What does it sense? June 2008 WEI L 4 - Tiny. OS Prog 35

Temp example configuration Temp. Disp. App. C { } implementation { components Sense. M, Temp example configuration Temp. Disp. App. C { } implementation { components Sense. M, Main. C, Leds. C, new Timer. Milli. C() as Timer, Temp. C ; Sense. M. Boot Sense. M. Leds Sense. M. Timer Sense. M. Read -> -> Main. C; Leds. C; Timer. Milli. C; Temp. C; } Sense. M Temp. Disp. App. C Boot Read Timer 0 Leds Main. C June 2008 Boot WEI L 4 - Tiny. OS Prog Read Timer Leds Temp. C Timer Leds. C 36

Concurrency • Commands and event glue together concurrent activities • Hardware units operate on Concurrency • Commands and event glue together concurrent activities • Hardware units operate on parallel – Commands used to initiate activity – Events used to signal completion, etc. • System software components are very similar – But they don’t have dedicated hardware to keep working on the command. – Tasks are used for that • Decouple execution and leave room for juggling – Use lots of little tasks to keep things flowing • Preempted by async events (interrupts) – Not other tasks June 2008 WEI L 4 - Tiny. OS Prog 37

Tasks – Crossing the Asynch / Synch Boundary module User. P { provides interface Tasks – Crossing the Asynch / Synch Boundary module User. P { provides interface Button; uses interface Boot; uses interface Msp 430 Port as Pin; uses interface Msp 430 Port. Interrupt as Pin. Int; } implementation { event void Boot. booted() { call Pin. set. Direction(0); /* Input */ call Pin. Int. edge(1); /* Rising edge, button release */ call Pin. Int. enable(1); /* Enable interrupts */ } task void fire() { signal Button. pressed(); } /* Signal event to upper layers */ async event void Pin. Int. fired() { post fire(); } } June 2008 WEI L 4 - Tiny. OS Prog 38

Examples - Tasks notify y ad read. Done Re e pl m Sa service. Examples - Tasks notify y ad read. Done Re e pl m Sa service. Req fired June 2008 WEI L 4 - Tiny. OS Prog 39

Tasks /* BAD TIMER EVENT HANDLER */ event void Timer 0. fired() { uint Tasks /* BAD TIMER EVENT HANDLER */ event void Timer 0. fired() { uint 32_t i; for (i = 0; i < 400001; i++) { call Leds. led 0 Toggle(); • Need to juggle many potentially bursty events. • If you cannot get the job done quickly, record the parameters locally and post a task to do it later. • Tasks are preempted by lower level (async) events. – Allow other parts of the system to get the processor. – Without complex critical semaphores, critical sections, priority inversion, schedulers, etc. } } /* Better way to do a silly thing */ task void compute. Task() { Tasks uint 32_t i; event s for (i = 0; i < 400001; i++) {} } commands event void Timer 0. fired() { call Leds. led 0 Toggle(); Interrupts post compute. Task(); } June 2008 Hardware WEI L 4 - Tiny. OS Prog 40

Uses of tasks (? ? ? ) • • June 2008 High speed sampling Uses of tasks (? ? ? ) • • June 2008 High speed sampling Filtering Queueing Smoothing Detection Classification … WEI L 4 - Tiny. OS Prog 41

Networking • Traditional Tiny. OS communication is based on active messages – Send a Networking • Traditional Tiny. OS communication is based on active messages – Send a structure to – Receive is just a network event that signals the hander with a message • Earliest Tiny. OS (0. 3) was essentially moving Active Messages and the Threaded Abstract Machine (TAM) from the MPP domain to emdedded devices. – Parallel Machine communication is internal! – Sensor networks are networks, not distributed computers – They connect to OTHER NETWORKS • Active Messages covered in Appendix – But I don’t use them any more June 2008 WEI L 4 - Tiny. OS Prog 42

Canonical Sensor. Network Architecture Patch Network Sensor Node Sensor Patch Gateway Transit Network (IP Canonical Sensor. Network Architecture Patch Network Sensor Node Sensor Patch Gateway Transit Network (IP or not) Access point - Base station - Proxy Client Data Browsing and Processing Intranet/Internet (IP) Verification links Other information sources Data Service June 2008 WEI L 4 - Tiny. OS Prog 43

Typical IP Network Internet VPN tunnels ISP External networks Company Router / Firewall Internal Typical IP Network Internet VPN tunnels ISP External networks Company Router / Firewall Internal Private networks externally accessible hosts ethernet internally accessible hosts DMZ Wi. Fi Stand-alone networks Wi. FI serial lines leased links point-point links inaccessible hosts June 2008 WEI L 4 - Tiny. OS Prog 44

Tiny. OS 2 x Embedded IP Architecture Higher Level Embedded Web Services SPI, i Tiny. OS 2 x Embedded IP Architecture Higher Level Embedded Web Services SPI, i 2 c, arbiters UART June 2008 s Timer RTC Pwr Mgr Low-Power 802. 15. 4 Basic Configuration Services Scheduler IP 6 Low. PAN L 2 OTA Virtual ms Timer IP route L 3 Basic Health & Mgmt Services Flash Storage UDP/TCP L 4 Ext. INT WEI L 4 - Tiny. OS Prog Sensor Drivers GPIO Pins ADC 45

WSNs in an IP context Internet IP-based corporate networks gateway computer controllers & analytics WSNs in an IP context Internet IP-based corporate networks gateway computer controllers & analytics ad hoc embedded network IP-based embedded network ad hoc embedded network monitoring devices IP-based embedded network monitoring devices Stand-alone embedded networks June 2008 Router / Firewall controllers & analytics Internally connected embedded networks WEI L 4 - Tiny. OS Prog 46

Issues in Communication Abstraction • Names – – What are they? What do they Issues in Communication Abstraction • Names – – What are they? What do they mean? What is their scope? How are they allocated? How are they translated into useful properties » Address? Physical Resource? • Storage – Transmit » When can it be reused? How do you know? What do you do when it is full? – Receive » When is it allocated? Reclaimed? By whom? What happens when it overflows? – Asynchronous event » How do you know that something has arrived? • Many more issues in actually performing the requested communication June 2008 WEI L 4 - Tiny. OS Prog 47

Answers: Naming • Tiny. OS Active Messages – TOS_local_addr, 16 bits, only within “a Answers: Naming • Tiny. OS Active Messages – TOS_local_addr, 16 bits, only within “a network”, allocated at program download time – Scope limited to TOS_group, within PAN, Channel, and physical extent. – The standards that try to do it all with IEEE 802. 15. 4 16 bit short address are not much better • IP Architecture – Link name: IEEE 802. 15. 4 => EUID 64 & SA 16 » EUID 64 globally unique, SA 16 assigned to be unique over the PAN – Net name: IP address » Prefix | Interface Id » Must be unique within routability extent » Several IP address: Link Local, Global, Multicast, … - Service name: Port - Hostname Translated to IP by DNS June 2008 WEI L 4 - Tiny. OS Prog 48

Answers: Storage • Tiny. OS Active Messages – Sender app allocates send buffer, OS Answers: Storage • Tiny. OS Active Messages – Sender app allocates send buffer, OS owns it till send. Done event – OS provides App with recv buffer, app must return it or a replacement • BSD Sockets – Sender app allocate buffer, copied to kernel on send – Receiver allocates buffer and passes pointer to kernel, kernel copies – Additional sets of buffer managed within the kernel • Tiny. OS Sockets ? ? ? June 2008 WEI L 4 - Tiny. OS Prog 49

UDP Interface interface command Udp { error_t bind( uint 16_t port ); uint 16_t UDP Interface interface command Udp { error_t bind( uint 16_t port ); uint 16_t get. Max. Payload. Length( const sockaddr_in 6_t *to ); error_t sendto( const void *buf, uint 16_t len, const sockaddr_in 6_t *to ); event void recvfrom( void *buf, uint 16_t len, sockaddr_in 6_t *from, link_metadata_t *metadata ); } • Wiring to a UDP interface is enough for sendto • Standard Unix sockaddr_in 6_t • Bind – associates the component that uses the interface with the port – Starts receiving whatever datagrams come in on it • Sendto sends a datagram from a buf to a IP destination – – Neighbor, intra-PAN, inter-network (wherever it needs to go!) Linklocal address is 1 -hop neighbor Pan-wide routable prefix Error when not in network or overrun transmit resources • Recvfrom gets a buffer with a datagram and metadata about it • Able to fill the 15. 4 frame without knowing details of header compression June 2008 WEI L 4 - Tiny. OS Prog 50

And what about TCP • UDP is a datagram transport – Essentially the same And what about TCP • UDP is a datagram transport – Essentially the same as AM – Fits naturally in event-driven model – Can use Nes. C platform independent data structure to have unambiguous packet formats without hton / ntoh error prone bit twiddling. • But TCP is a stream protocol… June 2008 WEI L 4 - Tiny. OS Prog 51

TCP Interface interface Tcp { command error_t bind(uint 16_t port, uint 8_t *buf, uint TCP Interface interface Tcp { command error_t bind(uint 16_t port, uint 8_t *buf, uint 16_t bufsize); event bool accept( sockaddr_in 6_t *to ); command error_t connect( const sockaddr_in 6_t *to, uint 8_t *buf, uint 16_t bufsize ); event void connected(); command error_t send( const void *buf, uint 16_t len ); event void acked(); event uint 16_t recv( void *buf, uint 16_t len ); command error_t close( bool force ); event void closed(); } • Transitions of the TCP protocol reflected as events • Recv per segment • Send is NOT SPLIT PHASE !!! – More on this later June 2008 WEI L 4 - Tiny. OS Prog 52

Example Tiny. OS Service Architecture Hardware Abstraction Layer Other Sounder Light RF Discovery Events Example Tiny. OS Service Architecture Hardware Abstraction Layer Other Sounder Light RF Discovery Events Attributes Commands Network Volumes Links Buses & ADCs Flash Radio / Uart Sensor/Actuator Management & Pwr Basic OS interface Messages Blocks Logs Files Service API OTA program Application Hardware June 2008 WEI L 4 - Tiny. OS Prog 53

Permanent Data Storage • Tiny. OS 2. x provides three basic storage abstractions: – Permanent Data Storage • Tiny. OS 2. x provides three basic storage abstractions: – small objects, – circular logs, and – large objects. • also provides interfaces the underlying storage services and components that provide these interfaces. • Flash devices – ST Microelectronics M 25 Pxx family of flash memories used in the Telos family of motes (tos/chips/stm 25 p) – Atmel AT 45 DB family of flash memories used in the Mica 2/Mica. Z motes (tos/chips/at 45 b) – Special pxa 271 p 30 versions for the Intel Mote 2 contributed by Arch Rock. (tos/platforms/intelmote 2) • TEP 103 June 2008 WEI L 4 - Tiny. OS Prog 54

Storage Interfaces and Components • Interfaces – – – – Block. Read Block. Write Storage Interfaces and Components • Interfaces – – – – Block. Read Block. Write Mount Config. Storage Log. Read Log. Write Storage. h Components – Config. Storage. C - Configuration Data » calibration, identity, location, sensing configuration, . . – Log. Storage. C » data – Block. Storage. C » Code, … June 2008 WEI L 4 - Tiny. OS Prog 55

Volumes • Tiny. OS 2. x divides a flash chip into one or more Volumes • Tiny. OS 2. x divides a flash chip into one or more fixed-sized volumes that are specified at compile -time using an XML file. June 2008 WEI L 4 - Tiny. OS Prog 56

Example – blink period config Define config storage object chipname. xml file Added to Example – blink period config Define config storage object chipname. xml file Added to the Tiny. OS configuration New interfaces for the module Wire to the new interfaces June 2008 WEI L 4 - Tiny. OS Prog 57

On boot – Mount and Read June 2008 WEI L 4 - Tiny. OS On boot – Mount and Read June 2008 WEI L 4 - Tiny. OS Prog 58

Config data – done, write, commit June 2008 WEI L 4 - Tiny. OS Config data – done, write, commit June 2008 WEI L 4 - Tiny. OS Prog 59

Network Embedded Systems Hardware Abstraction Layer Other Sounder Light RF Discovery Events Attributes Commands Network Embedded Systems Hardware Abstraction Layer Other Sounder Light RF Discovery Events Attributes Commands Network Volumes Links Buses & ADCs Flash Radio / Uart Sensor/Actuator Management & Pwr Basic OS interface Messages Blocks Logs Files Service API OTA program Application Hardware June 2008 WEI L 4 - Tiny. OS Prog 60

IP/6 Lo. WPAN “Kernel Component” component Kernel. C { provides interface Boot; provides interface IP/6 Lo. WPAN “Kernel Component” component Kernel. C { provides interface Boot; provides interface Timer as Timer. Milli[ uint 8_t id ]; provides interface Task. Basic[ uint 8_t id ]; provides interface Global. Time; /* Global Time – NTP */ provides interface Local. Ieee. Eui 64; /* EUI MAC Address */ provides interface Udp as Udp 0; /* Udp */ provides interface Udp as Udp 1; provides interface Tcp as Tcp 0; /* Tcp */ provides interface Tcp as Tcp 1; provides interface IPv 6 Addresses; /* IPv 6 Utility Functions */ provides interface IPv 6 Notify; /* MCU generated interrupts */ provides interface Hpl. Signal as Hpl. Signal. Adc 12; provides interface Hpl. Signal as Hpl. Signal. Port 1; provides interface Hpl. Signal as Hpl. Signal. Port 2; provides interface Hpl. Signal as Hpl. Signal. Timer. A 0; provides interface Hpl. Signal as Hpl. Signal. Timer. A 1; provides interface Hpl. Signal as Hpl. Signal. Timer. B 0; provides interface Hpl. Signal as Hpl. Signal. Timer. B 1; } June 2008 WEI L 4 - Tiny. OS Prog 61

Tiny. OS IPv 6 Network Kernel • Network Kernel – Manages communication and storage Tiny. OS IPv 6 Network Kernel • Network Kernel – Manages communication and storage – Scheduler (decides when to signal events) Application Driver IPv 6 Network Kernel Radio June 2008 Timer Flash WEI L 4 - Tiny. OS Prog Driver Sensors Sensor Actuator 62

Network Embedded Systems Our kernel boundary June 2008 Volumes Links Buses & ADCs Flash Network Embedded Systems Our kernel boundary June 2008 Volumes Links Buses & ADCs Flash Radio / Uart Sensor/Actuator Management & Pwr Other Sounder Light RF netstat Echo ICMP / UDP / TCP 6 Lo. WPAN Network Basic OS interface Hardware Abstraction Layer OTA Blocks Logs Config Service API Systat Application Hardware WEI L 4 - Tiny. OS Prog 63

Tiny. OS Concepts not yet covered • Generic Components – – – Generic modules Tiny. OS Concepts not yet covered • Generic Components – – – Generic modules and generic configurations Multiple instances Type polymorphism Storage and configuration Discovery • Parameterized interfaces – Index array of interfaces to other components – Dispatch, services – Virtualization • Platform independent structs – Replace xdr and all that – Eliminate error prone bit twiddling June 2008 WEI L 4 - Tiny. OS Prog 64

Discussion June 2008 WEI L 4 - Tiny. OS Prog 65 Discussion June 2008 WEI L 4 - Tiny. OS Prog 65

Traditional Tiny. OS Active Messages June 2008 WEI L 4 - Tiny. OS Prog Traditional Tiny. OS Active Messages June 2008 WEI L 4 - Tiny. OS Prog 66

Sensor NETWORK • We have a flexible, low-power, event-driven sensor / actuator platform. • Sensor NETWORK • We have a flexible, low-power, event-driven sensor / actuator platform. • Let’s add the network • Send / Receive of information • Dispatching incoming data to computation processes that will handle it. – Automate in a systematic fashion • Parsing the packet – Define the structure, let the compiler do the work. – Handler knows what it should be receiving June 2008 WEI L 4 - Tiny. OS Prog 67

message_t structure • Packet - Provides the basic accessors for the message_t abstract data message_t structure • Packet - Provides the basic accessors for the message_t abstract data type. This interface provides commands for clearing a message's contents, getting its payload length, and getting a pointer to its payload area. • Send - Provides the basic address-free message sending interface. This interface provides commands for sending a message and canceling a pending message send. The interface provides an event to indicate whether a message was sent successfully or not. It also provides convenience functions for getting the message's maximum payload as well as a pointer to a message's payload area. • Receive - Provides the basic message reception interface. This interface provides an event for receiving messages. It also provides, for convenience, commands for getting a message's payload length and getting a pointer to a message's payload area. • Packet. Acknowledgements - Provides a mechanism for requesting acknowledgements on a per-packet basis. • Radio. Time. Stamping - Provides time stamping information for radio transmission and reception. June 2008 WEI L 4 - Tiny. OS Prog 68

Active Messages - Dispatching messages to their handlers • AM type – dispatch selector Active Messages - Dispatching messages to their handlers • AM type – dispatch selector – Frame_type at link layer – IP Protocol Field at network layer – Port at Transport layer • AM_address • AMPacket - Similar to Packet, provides the basic AM accessors for the message_t abstract data type. This interface provides commands for getting a node's AM address, an AM packet's destination, and an AM packet's type. Commands are also provides for setting an AM packet's destination and type, and checking whether the destination is the local node. • AMSend - Similar to Send, provides the basic Active Message sending interface. The key difference between AMSend and Send is that AMSend takes a destination AM address in its send command. June 2008 WEI L 4 - Tiny. OS Prog 69

Communication Components • AMReceiver. C - Provides the following interfaces: Receive, Packet, and AMPacket. Communication Components • AMReceiver. C - Provides the following interfaces: Receive, Packet, and AMPacket. • AMSender. C - Provides AMSend, Packet, AMPacket, and Packet. Acknowledgements as Acks. • AMSnooper. C - Provides Receive, Packet, and AMPacket. • AMSnooping. Receiver. C - Provides Receive, Packet, and AMPacket. • Active. Message. Address. C - Provides commands to get and set the node's active message address. This interface is not for general use and changing the a node's active message address can break the network stack, so avoid using it unless you know what you are doing. June 2008 WEI L 4 - Tiny. OS Prog 70

HAL to HIL • Since Tiny. OS supports multiple platforms, each of which might HAL to HIL • Since Tiny. OS supports multiple platforms, each of which might have their own implementation of the radio drivers, an additional, platform-specific, naming wrapper called Active. Message. C is used to bridge these interfaces to their underlying, platform-specific implementations. Active. Message. C provides most of the communication interfaces presented above. • Platform-specific versions of Active. Message. C, as well the underlying implementations which may be shared by multiple platforms (e. g. Telos and Mica. Z) include: – Active. Message. C for the intelmote 2, micaz, telosa, and telosb are all implemented by CC 2420 Active. Message. C. – Active. Message. C for the mica 2 platform is implemented by CC 1000 Active. Message. C. – Active. Message. C for the eyes. IFX platform is implemented by Tda 5250 Active. Message. C. June 2008 WEI L 4 - Tiny. OS Prog 71

tos/types/message. h. typedef nx_struct message_t { nx_uint 8_t header[sizeof(message_header_t)]; nx_uint 8_t data[TOSH_DATA_LENGTH]; nx_uint 8_t tos/types/message. h. typedef nx_struct message_t { nx_uint 8_t header[sizeof(message_header_t)]; nx_uint 8_t data[TOSH_DATA_LENGTH]; nx_uint 8_t footer[sizeof(message_header_t)]; nx_uint 8_t metadata[sizeof(message_metadata_t)]; } message_t; • Link level concept used throughout the Tiny. OS research community and industry. • How does this move forward to IP/WSN? June 2008 WEI L 4 - Tiny. OS Prog 72

Sending a packet to the neighborhood #include <Timer. h> #include Sending a packet to the neighborhood #include #include "Blink. To. Radio. h" module Blink. To. Radio. C { uses interface Boot; uses interface Leds; uses interface Timer as Timer 0; uses interface Packet; uses interface AMSend; uses interface Receive; uses interface Split. Control as AMControl; } implementation { uint 16_t counter; message_t pkt; bool busy = FALSE; June 2008 event void Boot. booted() { call AMControl. start(); } event void AMControl. start. Done(error_t err) { if (err == SUCCESS) { call Timer 0. start. Periodic(TIMER_PERIOD_MILLI); } WEI L 4 - Tiny. OS Prog } 73

Sending a packet to the neighborhood event void Timer 0. fired() { counter++; if Sending a packet to the neighborhood event void Timer 0. fired() { counter++; if (!busy) { Blink. To. Radio. Msg* btrpkt = (Blink. To. Radio. Msg*)(call Packet. get. Payload(&pkt, NULL)); btrpkt->nodeid = TOS_NODE_ID; btrpkt->counter = counter; if (call AMSend. send(AM_BROADCAST_ADDR, &pkt, sizeof(Blink. To. Radio. Msg)) == SUCCESS) { busy = TRUE; } } } event void AMSend. send. Done(message_t* msg, error_t err) { if (&pkt == msg) { busy = FALSE; } } } event message_t* Receive. receive(message_t* msg, void* payload, uint 8_t len){ if (len == sizeof(Blink. To. Radio. Msg)) { Blink. To. Radio. Msg* btrpkt = (Blink. To. Radio. Msg*)payload; call Leds. set(btrpkt->counter & 0 x 7); } return msg; } June 2008 WEI L 4 - Tiny. OS Prog 74

Receive – a network event message_t* Receive. receive(message_t* msg, void* payload, uint 8_t len) Receive – a network event message_t* Receive. receive(message_t* msg, void* payload, uint 8_t len) { if (len == sizeof(Blink. To. Radio. Msg)) { Blink. To. Radio. Msg* btrpkt = (Blink. To. Radio. Msg*)payload; call Leds. set(btrpkt->counter); } return msg; enum { } AM_BLINKTORADIO = 6, }; typedef nx_struct Blink. To. Radio. Msg { nx_uint 16_t nodeid; nx_uint 16_t counter; • Service the incoming message } Blink. To. Radio. Msg; – Automatically dispatched by type to the handler • Return the buffer – Or if you want to keep it, you need to return another one. • Overlay a network type structure on the packet so the compiler does the parsing. June 2008 WEI L 4 - Tiny. OS Prog 75

Communication-Centric Operating Systems Design Elements • Packets cross between different kinds of machines – Communication-Centric Operating Systems Design Elements • Packets cross between different kinds of machines – Small Endian vs Large Endian – Representation of data types – int, long, char, … • Protocols have specific message formats Þ Network Types Þ Overlay network type struct on the packet and let the compiler do the shifting, masking, and endian conversion Þ Eliminate error-prone parsing code • Protocols are realized by state machines that advance on message events – Make external communication events as simple as internal events • Network stacks involve dispatching on types at several levels – Frame type, Protocol Number, port number – Provide support for dispatch • Packet events are intrinsically connected with buffer management – Make is a robust and simple as possible. June 2008 WEI L 4 - Tiny. OS Prog 76

AM_ID – port number #include <Timer. h> #include AM_ID – port number #include #include "Blink. To. Radio. h" configuration Blink. To. Radio. App. C { } implementation { components Main. C, Leds. C; components Blink. To. Radio. C as App; components new Timer. Milli. C() as Timer 0; components Active. Message. C; components new AMSender. C(AM_BLINKTORADIO); components new AMReceiver. C(AM_BLINKTORADIO); App. Boot -> Main. C; App. Leds -> Leds. C; App. Timer 0 -> Timer 0; App. Packet -> AMSender. C; App. AMControl -> Active. Message. C; App. AMSend -> AMSender. C; App. Receive -> AMReceiver. C; } June 2008 WEI L 4 - Tiny. OS Prog 77

Indexed interfaces for dispatch SVC x SVC y SVC z SVC w Active Message Indexed interfaces for dispatch SVC x SVC y SVC z SVC w Active Message Network Stack Implementation Hardware • Clients & Applications have individual view • Provider has global view June 2008 WEI L 4 - Tiny. OS Prog 78

Active Messages • Concept – message carries unique identifier of handler that is to Active Messages • Concept – message carries unique identifier of handler that is to process it – Directly off the link – Compile time formation and parsing » It is a Structure – Bounded storage » Consume the receive buffer immediately • Has been the central networking abstraction in all version of Tiny. OS – Used for all the multihop routing protocols, … • Link frame derived from SW usage • TCP/UDP / IP / 6 Lo. WPAN will cause first serious re-examination of Message_T June 2008 WEI L 4 - Tiny. OS Prog 79

Parameterized Wiring • Active Message clients wire to distinct AM_id – Unique ID of Parameterized Wiring • Active Message clients wire to distinct AM_id – Unique ID of the service handler • Many other system services provided to multiple clients (independently) but do not need a “protocol id” • Nes. C “Unique” allocates new interfaces at compile time – Relative to a key set – Whole system compilation => tight resource allocation June 2008 WEI L 4 - Tiny. OS Prog 80

Wiring Examples ? ? ? June 2008 WEI L 4 - Tiny. OS Prog Wiring Examples ? ? ? June 2008 WEI L 4 - Tiny. OS Prog 81