-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomDTN.c
88 lines (73 loc) · 2.08 KB
/
randomDTN.c
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
#include "contiki.h"
#include "net/rime/trickle.h"
#include "shared.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "lib/random.h"
static process_event_t event_data_ready;
static struct etimer nodeSuccess;
char *messageReceived = "Test Message";
int n = 1;
/*---------------------------------------------------------------------------*/
PROCESS(beacon, "beacon");
PROCESS(packageSend, "Package sending");
AUTOSTART_PROCESSES(&beacon, &packageSend);
/*---------------------------------------------------------------------------*/
static void
trickle_recv(struct trickle_conn *c)
{
printf("Node detected: %s\r\n", (char *)packetbuf_dataptr());
int data = 5;
process_post(&packageSend, event_data_ready, &data);
}
static void
abc_recv(struct abc_conn *c)
{
printf("Message received: %s\r\n", (char *)packetbuf_dataptr());
if (n == 0)
{
messageReceived = (char *)packetbuf_dataptr();
}
else
{
printf("Already have\n");
}
}
const static struct trickle_callbacks trickle_call = {trickle_recv};
const static struct abc_callbacks abc_call = {abc_recv};
static struct trickle_conn trickle;
static struct abc_conn abc;
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(beacon, ev, data)
{
static struct etimer et;
PROCESS_EXITHANDLER(trickle_close(&trickle);)
PROCESS_BEGIN();
trickle_open(&trickle, CLOCK_SECOND, 130, &trickle_call);
while(1) {
etimer_set(&et, CLOCK_SECOND * 2 + random_rand() % (CLOCK_SECOND * 2));
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
packetbuf_copyfrom("Node x", 7);
printf("Transmission request from %s\n", (char *)packetbuf_dataptr());
trickle_send(&trickle);
}
PROCESS_END();
}
PROCESS_THREAD(packageSend, ev, data)
{
PROCESS_EXITHANDLER(abc_close(&abc);)
PROCESS_BEGIN();
abc_open(&abc, 128, &abc_call);
PROCESS_WAIT_EVENT_UNTIL(ev == event_data_ready);
if (n == 1)
{
if ((random_rand()%2) == 2)
{
packetbuf_copyfrom(messageReceived, 13);
abc_send(&abc);
n = 0;
}
}
PROCESS_END();
}