File: UARTDebugSenderP.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 (218 lines) | stat: -rw-r--r-- 6,830 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
207
208
209
210
211
212
213
214
215
216
217
218
#include <CollectionDebugMsg.h>
module UARTDebugSenderP {
    provides {
        interface CollectionDebug;
    }
    uses {
        interface Boot;
        interface Pool<message_t> as MessagePool;
        interface Queue<message_t*> as SendQueue;
        interface AMSend as UARTSend;
    }
} 
implementation {
    message_t uartPacket;
    bool sending;
    uint8_t len;
    uint16_t statLogReceived = 0;
    uint16_t statEnqueueFail = 0;
    uint16_t statSendFail = 0;
    uint16_t statSendDoneFail = 0;
    uint16_t statSendDoneOk = 0;
    uint16_t statSendDoneBug = 0;
 

    event void Boot.booted() {
        sending = FALSE;
        len = sizeof(CollectionDebugMsg);
        statSendFail = 0;
        statLogReceived = 0;
        statEnqueueFail = 0;
        statSendDoneOk = 0;
        statSendDoneFail = 0;
        statSendDoneBug = 0;
    }

    task void sendTask() {
        if (sending) {
            return;
        } else if (call SendQueue.empty()) {
            return;
        } else {
            message_t* smsg = call SendQueue.head();
            error_t eval = call UARTSend.send(AM_BROADCAST_ADDR, smsg, len);
            if (eval == SUCCESS) {
                sending = TRUE;
                return;
            } else {
                //Drop packet. Don't retry.
                statSendFail++;
                call SendQueue.dequeue();
                call MessagePool.put(smsg);
                if (! call SendQueue.empty())
                    post sendTask();
            }
        }
    }

    event void UARTSend.sendDone(message_t *msg, error_t error) {
        message_t* qh = call SendQueue.head();
        if (qh == NULL || qh != msg) {
            //bad mojo
            statSendDoneBug++;
        } else {
            call SendQueue.dequeue();
            call MessagePool.put(msg);  
            if (error == SUCCESS) 
                statSendDoneOk++;
            else 
                statSendDoneFail++;
        }
        sending = FALSE;
        if (!call SendQueue.empty()) 
            post sendTask();
    }

    command error_t CollectionDebug.logEvent(uint8_t type) {
        statLogReceived++;
        if (call MessagePool.empty()) {
            return FAIL;
        } else {
            message_t* msg = call MessagePool.get();
            CollectionDebugMsg* dbg_msg = call UARTSend.getPayload(msg, sizeof(CollectionDebugMsg));
	    if (dbg_msg == NULL) {
	      return FAIL;
	    }
	    
            memset(dbg_msg, 0, len);

            dbg_msg->type = type;
            dbg_msg->seqno = statLogReceived;

            if (call SendQueue.enqueue(msg) == SUCCESS) {
                post sendTask();
                return SUCCESS;
            } else {
                statEnqueueFail++;
                call MessagePool.put(msg);
                return FAIL;
            }
        }
    }
    /* Used for FE_SENT_MSG, FE_RCV_MSG, FE_FWD_MSG, FE_DST_MSG */
    command error_t TRUSTEDBLOCK CollectionDebug.logEventMsg(uint8_t type, uint16_t msg_id, am_addr_t origin, am_addr_t node) {
        statLogReceived++;
        if (call MessagePool.empty()) {
            return FAIL;
        } else {
            message_t* msg = call MessagePool.get();
            CollectionDebugMsg* dbg_msg = call UARTSend.getPayload(msg, sizeof(CollectionDebugMsg));
	    if (dbg_msg == NULL) {
	      return FAIL;
	    }
            memset(dbg_msg, 0, len);

            dbg_msg->type = type;
            dbg_msg->data.msg.msg_uid = msg_id;
            dbg_msg->data.msg.origin = origin;
            dbg_msg->data.msg.other_node = node;
            dbg_msg->seqno = statLogReceived;

            if (call SendQueue.enqueue(msg) == SUCCESS) {
                post sendTask();
                return SUCCESS;
            } else {
                statEnqueueFail++;
                call MessagePool.put(msg);
                return FAIL;
            }
        }
    }
    /* Used for TREE_NEW_PARENT, TREE_ROUTE_INFO */
    command error_t TRUSTEDBLOCK CollectionDebug.logEventRoute(uint8_t type, am_addr_t parent, uint8_t hopcount, uint16_t metric) {
        statLogReceived++;
        if (call MessagePool.empty()) {
            return FAIL;
        } else {
            message_t* msg = call MessagePool.get();
            CollectionDebugMsg* dbg_msg = call UARTSend.getPayload(msg, sizeof(CollectionDebugMsg));
	    if (dbg_msg == NULL) {
	      return FAIL;
	    }
            memset(dbg_msg, 0, len);

            dbg_msg->type = type;
            dbg_msg->data.route_info.parent = parent;
            dbg_msg->data.route_info.hopcount = hopcount;
            dbg_msg->data.route_info.metric = metric;
            dbg_msg->seqno = statLogReceived;

            if (call SendQueue.enqueue(msg) == SUCCESS) {
                post sendTask();
                return SUCCESS;
            } else {
                statEnqueueFail++;
                call MessagePool.put(msg);
                return FAIL;
            }
        }
    }
    /* Used for DBG_1 */ 
    command error_t CollectionDebug.logEventSimple(uint8_t type, uint16_t arg) {
        statLogReceived++;
        if (call MessagePool.empty()) {
            return FAIL;
        } else {
            message_t* msg = call MessagePool.get();
            CollectionDebugMsg* dbg_msg = call UARTSend.getPayload(msg, sizeof(CollectionDebugMsg));
	    if (dbg_msg == NULL) {
	      return FAIL;
	    }
            memset(dbg_msg, 0, len);

            dbg_msg->type = type;
            dbg_msg->data.arg = arg;
            dbg_msg->seqno = statLogReceived;

            if (call SendQueue.enqueue(msg) == SUCCESS) {
                post sendTask();
                return SUCCESS;
            } else {
                statEnqueueFail++;
                call MessagePool.put(msg);
                return FAIL;
            }
        }
    }
    /* Used for DBG_2, DBG_3 */
    command TRUSTEDBLOCK error_t CollectionDebug.logEventDbg(uint8_t type, uint16_t arg1, uint16_t arg2, uint16_t arg3) {
        statLogReceived++;
        if (call MessagePool.empty()) {
            return FAIL;
        } else {
            message_t* msg = call MessagePool.get();
            CollectionDebugMsg* dbg_msg = call UARTSend.getPayload(msg, sizeof(CollectionDebugMsg));
	    if (dbg_msg == NULL) {
	      return FAIL;
	    }
            memset(dbg_msg, 0, len);

            dbg_msg->type = type;
            dbg_msg->data.dbg.a = arg1;
            dbg_msg->data.dbg.b = arg2;
            dbg_msg->data.dbg.c = arg3;
            dbg_msg->seqno = statLogReceived;

            if (call SendQueue.enqueue(msg) == SUCCESS) {
                post sendTask();
                return SUCCESS;
            } else {
                statEnqueueFail++;
                call MessagePool.put(msg);
                return FAIL;
            }
        }
    }

}