Programming Tinyos Can Be Challenging Because It Requires Using a New Language, Nesc
Total Page:16
File Type:pdf, Size:1020Kb
TinyOS Programming Philip Levis June 28, 2006 2 This text is copyright c Philip Levis, 2006. All rights reserved. You may distribute and reproduce it freely for non-commercial purposes as long as you do not modify it in any way. Revision 1.2 (June 28 2006) Contents 1 Preface 13 2 Programming Hints, Condensed 15 3 Linking and Namespaces 17 3.1 C................................................. 17 3.2 C++............................................... 20 3.3 Java ............................................... 22 3.4 Components and Interfaces . 22 3.5 Why? .............................................. 24 4 Interfaces and Modules 27 4.1 Split Phase . 27 4.2 Interfaces with Arguments . 29 4.3 Module Implementation . 30 4.4 Tasks . 33 4.5 Concurrency . 38 4.6 Allocation . 45 5 Configurations and Wiring 53 5.1 Pass Through Wiring . 62 5.2 Combine Functions . 66 6 Parameterized Wiring 69 6.1 Defaults . 75 6.2 unique() and uniqueCount() . 77 3 4 CONTENTS 7 Generic Components 81 7.1 Generic Modules . 83 7.2 HilTimerMilliC: An Example Use of Generic Components . 84 7.3 Generic Configurations . 87 7.4 Examples . 89 7.4.1 TimerMilliC . 89 7.4.2 AMSenderC . 91 7.4.3 CC2420SpiC . 95 7.4.4 BlockStorageC . 100 7.5 Configurations, revisited . 103 8 Design Patterns 105 8.1 Behavioural: Dispatcher . 106 8.2 Structural: Service Instance . 110 8.3 Namespace: Keysets . 114 8.4 Namespace: Keymap . 117 8.5 Structural: Placeholder . 120 8.6 Structural: Facade . 124 8.7 Behavioural: Decorator . 128 8.8 Behavioural: Adapter . 131 9 Advanced Topics 135 9.1 Attributes . 135 9.2 Platform Independent Types . 137 Code Examples 3.1 The POSIX read(2) function . 17 3.2 Calling read(2) . 18 3.3 Reading a byte with read(2) . 18 3.4 Naming scopes in C . 18 3.5 C++ namespaces . 20 3.6 The namespace of a C++ class . 20 3.7 Private fields in C++ . 21 3.8 Instantiating a Piece object in C++ . 21 3.9 A C++ Piece factory . 21 3.10 Function binding in Java . 22 3.11 SmoothingFilterC, a simple nesC module . 23 3.12 The StdControl interface . 23 3.13 Interfaces in component signatures . 24 4.1 The split–phase Send interface . 28 4.2 The Read interface . 29 4.3 Using the Read interface in MagnetometerC . 29 4.4 The Timer interface and its typing . 29 4.5 Using the Timer interface . 30 4.6 Providing the Timer interface . 30 4.7 Providing a Timer with microsecond precision . 30 4.8 PeriodicReaderC signature . 30 4.9 Complete PeriodicReaderC component . 31 4.10 PeriodicSenseAndSendC component . 31 4.11 A troublesome implementation of PeriodicReaderC . 33 5 6 CODE EXAMPLES 4.12 Signal handler that can lead to an infinite loop . 34 4.13 Declaring a task . 34 4.14 Posting a task . 35 4.15 An improved implementation of PeriodicReaderC . 35 4.16 One–to–one binding of a split–phase call . 37 4.17 Many–to–one binding of a split–phase call . 37 4.18 The Send interface . 38 4.19 The Leds interface . 39 4.20 Toggling a state variable . 39 4.21 A call sequence that could corrupt a variable . 40 4.22 State transition that is not async–safe . 40 4.23 Incrementing with an atomic section . 41 4.24 Incrementing with two independent atomic sections . 41 4.25 Atomically turning off the SPI bus . 42 4.26 State transition that requires a large atomic section . 42 4.27 A fast and atomic state transition . 43 4.28 An unnecessary conditional . 43 4.29 The first step of starting the CC2420 radio . 44 4.30 The handler that the first step of starting the CC2420 is complete . 44 4.31 The handler that the second step of starting the CC2420 is complete . 44 4.32 Handler that the third step of starting the CC2420 radio is complete . 44 4.33 State transition so components can send and receive packets . 45 4.34 An alternative state transition implementation . 45 4.35 Signature of BitVectorC . 46 4.36 The BitVector interface . 46 4.37 Representing an ADT though an interface in TinyOS 1.x . 47 4.39 The Receive interface . 48 4.40 The simplest receive handler . 48 4.41 A broken receive handler that doesn’t respect buffer swapping . 49 4.42 Wasting memory by defining a constant as an integer . 50 4.43 Defining a constant as an enum . 50 4.44 An example enum . 51 CODE EXAMPLES 7 4.45 Allocating a state variable . 51 5.1 The LedsC configuration . 54 5.2 Wiring directionality . ..