File: TestPacketLinkP.nc

package info (click to toggle)
tinyos 2.1.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 47,476 kB
  • ctags: 36,607
  • sloc: ansic: 63,646; cpp: 14,974; java: 10,358; python: 5,215; makefile: 1,724; sh: 902; asm: 597; xml: 392; perl: 74; awk: 46
file content (206 lines) | stat: -rw-r--r-- 6,568 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Copyright (c) 2005-2006 Rincon Research Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the
 *   distribution.
 * - Neither the name of the Rincon Research Corporation nor the names of
 *   its contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * RINCON RESEARCH OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE
 */
 
#include "TestPacketLink.h"

/**
 * Test the effectiveness of the PacketLink layer
 *
 * Transmitter == 1, 2, 3, 4, or 5 (up to MAX_TRANSMITTERS)
 * Receiver == 0
 *
 * Expect:
 *   Transmitter (ID not 0) -
 *     led1 toggling on every successfully delivered message
 *     led0 toggling on every unsuccessfully delivered message (and stay on
 *       until the next dropped packet)
 *   
 *   Receiver (ID 0) -
 *     Leds represent the binary count of sets of messages that were dropped
 *     or duplicated.
 * 
 *     Ideally, if the transmitter and receiver are in range of each other, 
 *     the receiver's LEDs should never turn on.  You can pull the receiver
 *     out of range for up to two seconds before the transmission will fail.
 *     If you aren't convinced the receiver is doing anything because its 
 *     leds aren't flashing, just turn it off and watch the transmitter's
 *     reaction.
 *
 * @author David Moss
 */
  
module TestPacketLinkP {
  uses {
    interface Boot;
    interface SplitControl as RadioSplitControl;
    interface SplitControl as SerialSplitControl;
    interface AMSend;
    interface AMSend as SerialAMSend;
    interface AMPacket;
    interface Receive;
    interface PacketLink;
    interface Leds;
    interface Timer<TMilli>;
  }
}

implementation {

  /** The message we'll be sending */
  message_t myMsg;
  
  /** Serial message for status */
  message_t serialMsg;
  
  /** The local count we're sending or should receive on each unique message */
  uint32_t count[MAX_TRANSMITTERS];
  
  /** The total number of packets missed by the receiver */
  uint8_t missedPackets;
  
  /** True if this mote is the transmitter mote */
  bool transmitter;
  
  enum {
    MSG_DESTINATION = 0,
  };
  
  /***************** Prototypes ****************/
  task void send();
  task void sendSerial();
  
  /***************** Boot Events ****************/
  event void Boot.booted() {
    int i;
    
    /*
     * Setup this message in advance to retry up to 50 times with 40 ms of
     * delay between each message.  50 * 40 ms = 2 seconds before it quits.
     * It only needs to be setup once to be stored in the msg's metadata.
     */
    call PacketLink.setRetries(&myMsg, 50);
    call PacketLink.setRetryDelay(&myMsg, 40);
    missedPackets = 0;
    
    for(i = 0; i < MAX_TRANSMITTERS; i++) {
      count[i] = 0;
    }
    
    transmitter = (call AMPacket.address() != 0);
    call RadioSplitControl.start();
    
    if(!transmitter) {
      call SerialSplitControl.start();
    }
  }
  
  /***************** SplitControl Events *****************/
  event void RadioSplitControl.startDone(error_t error) {
    if(transmitter) {
      post send();
    }
  }
  
  event void RadioSplitControl.stopDone(error_t error) {
  }

  /***************** SerialSplitControl Events ****************/
  event void SerialSplitControl.startDone(error_t error) {
  }
  
  event void SerialSplitControl.stopDone(error_t error) {
  }
  
  /***************** AMSend Events ****************/
  event void AMSend.sendDone(message_t *msg, error_t error) {
    if(call PacketLink.wasDelivered(msg)) {
      count[0]++;
      call Leds.led1Toggle();
    } else {
      call Leds.led0Toggle();
    }
    
    ((PacketLinkMsg *) call AMSend.getPayload(&myMsg, sizeof(PacketLinkMsg)))->count = count[0];
    call Timer.startOneShot(50);
  }
  
  /***************** SerialAMSend Events ****************/
  event void SerialAMSend.sendDone(message_t *msg, error_t error) {
  }
  
  /***************** Receive Events ****************/
  event message_t *Receive.receive(message_t *msg, void *payload, uint8_t len) {
    PacketLinkMsg *linkMsg = (PacketLinkMsg *) payload;
    uint16_t source = call AMPacket.source(msg);
    
    if(transmitter || source > MAX_TRANSMITTERS - 1) {
      return msg;
    }
    
    if(linkMsg->count != count[source]) {
      ((PacketLinkMsg *) (call SerialAMSend.getPayload(&serialMsg, sizeof(PacketLinkMsg))))->src = source;
      if(linkMsg->count > count[source]) {
        ((PacketLinkMsg *) (call SerialAMSend.getPayload(&serialMsg, sizeof(PacketLinkMsg))))->cmd = CMD_DROPPED_PACKET;
      } else {
        ((PacketLinkMsg *) (call SerialAMSend.getPayload(&serialMsg, sizeof(PacketLinkMsg))))->cmd = CMD_DUPLICATE_PACKET;
      }
      post sendSerial();
      
      if(count[source] != 0) {
        missedPackets++;
        call Leds.set(missedPackets);
      }
    }
    
    count[source] = linkMsg->count;
    count[source]++;
    return msg;
  }
  
  /***************** Timer Events ***************/
  event void Timer.fired() {
    post send();
  }
  
  /***************** Tasks ****************/
  task void send() {
    if(call AMSend.send(MSG_DESTINATION, &myMsg, sizeof(PacketLinkMsg)) != SUCCESS) {
      post send();
    }
  }
  
  task void sendSerial() {
    if(call SerialAMSend.send(0, &serialMsg, sizeof(PacketLinkMsg)) != SUCCESS) {
      post sendSerial();
    }
  }
}