-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAppP.nc
67 lines (58 loc) · 1.7 KB
/
AppP.nc
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
#include <Timer.h>
#include <AM.h>
#include "messages.h"
module AppP {
uses interface Boot;
uses interface Timer < TMilli > as StartTimer;
uses interface Timer < TMilli > as PeriodicTimer;
uses interface Timer < TMilli > as JitterTimer;
uses interface Random;
uses interface AMSend;
uses interface Receive;
uses interface tdma;
uses interface AMPacket;
}
implementation {
#define IMI (1024L)
#define SECOND 32768L
#define JITTER (512L)
#define IS_MASTER (TOS_NODE_ID==1)
uint16_t DataCnt = 1; // a simple counter to make fake data
message_t message;
event void Boot.booted() {
if (IS_MASTER)
call tdma.init();
else{
call StartTimer.startOneShot(10 * 1024);
}
}
event void StartTimer.fired() {
if (!IS_MASTER) {
call PeriodicTimer.startPeriodic(IMI);
}
}
event void PeriodicTimer.fired() {
call JitterTimer.startOneShot(call Random.rand16() % JITTER);
}
event void JitterTimer.fired() {
DataMsg *m = call AMSend.getPayload(&message, sizeof(DataMsg));
m->data = DataCnt;
//I don't use flag for data senging, just periodically send data, because only if
//this node receives Ack from master then I update DataCnt, otherwise just repeatly
// send the same data
call AMSend.send(1, &message, sizeof(DataMsg));
}
event void AMSend.sendDone(message_t * msg, error_t err) {
//only if data is successfully sent, we update data counter
if (err == SUCCESS) {
printf("Data is successfully sent by App\n");
DataCnt ++;
}
}
event message_t *Receive.receive(message_t * msg, void *payload, uint8_t length) {
am_addr_t from = call AMPacket.source(msg);
DataMsg *m = (DataMsg*) payload;
printf("Sink receives data %d from node %d\n", m->data, from);
return msg;
}
}