-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcetic-6lbr-client.c
197 lines (185 loc) · 6.24 KB
/
cetic-6lbr-client.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
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
/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name of the Institute 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 INSTITUTE 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 INSTITUTE OR 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.
*/
/**
* \addtogroup cc26xx-st2
* @{
*
* \file
* An implementation of a 6LBR UDP client. Is used to populate the 6LBR
* web server's 'sensors' tab
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "contiki-lib.h"
#include "contiki-net.h"
#include "net/rpl/rpl.h"
#include "net/ip/uip.h"
#include <string.h>
#include <stdio.h>
/*---------------------------------------------------------------------------*/
#define DEBUG 0
#include "net/ip/uip-debug.h"
/*---------------------------------------------------------------------------*/
#ifndef CETIC_6LBR_NODE_INFO_PORT
#define CETIC_6LBR_NODE_INFO_PORT 3000
#endif
#define MAX_PAYLOAD_LEN 40
#define MSG_INTERVAL (60 * CLOCK_SECOND)
/*---------------------------------------------------------------------------*/
static struct uip_udp_conn *client_conn = NULL;
static struct etimer et;
static uip_ip6addr_t dest_addr;
/*---------------------------------------------------------------------------*/
PROCESS(cetic_6lbr_client_process, "6LBR Client Process");
/*---------------------------------------------------------------------------*/
static void
tcpip_handler(void)
{
char *str;
if(uip_newdata()) {
str = uip_appdata;
str[uip_datalen()] = '\0';
PRINTF("Response from the server: '%s'\n", str);
}
}
/*---------------------------------------------------------------------------*/
static char *
add_ipaddr(char *buf, const uip_ipaddr_t *addr)
{
uint16_t a;
unsigned int i;
int f;
char *p = buf;
for(i = 0, f = 0; i < sizeof(uip_ipaddr_t); i += 2) {
a = (addr->u8[i] << 8) + addr->u8[i + 1];
if(a == 0 && f >= 0) {
if(f++ == 0) {
p += sprintf(p, "::");
}
} else {
if(f > 0) {
f = -1;
} else if(i > 0) {
p += sprintf(p, ":");
}
p += sprintf(p, "%04x", a);
}
}
return p;
}
/*---------------------------------------------------------------------------*/
static void
timeout_handler(void)
{
static int seq_id;
char buf[MAX_PAYLOAD_LEN];
int i;
uip_ip6addr_t *globaladdr = NULL;
uint16_t dest_port = CETIC_6LBR_NODE_INFO_PORT;
int has_dest = 0;
rpl_dag_t *dag;
uip_ds6_addr_t *addr_desc = uip_ds6_get_global(ADDR_PREFERRED);
if(addr_desc != NULL) {
globaladdr = &addr_desc->ipaddr;
dag = rpl_get_any_dag();
if(dag) {
uip_ipaddr_copy(&dest_addr, globaladdr);
memcpy(&dest_addr.u8[8], &dag->dag_id.u8[8], sizeof(uip_ipaddr_t) / 2);
has_dest = 1;
}
}
if(has_dest) {
if(client_conn == NULL) {
PRINTF("UDP-CLIENT: address destination: ");
PRINT6ADDR(&dest_addr);
PRINTF("\n");
client_conn = udp_new(&dest_addr, UIP_HTONS(dest_port), NULL);
if(client_conn != NULL) {
PRINTF("Created a connection with the server ");
PRINT6ADDR(&client_conn->ripaddr);
PRINTF(" local/remote port %u/%u\n",
UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport));
} else {
PRINTF("Could not open connection\n");
}
} else {
if(memcmp(&client_conn->ripaddr, &dest_addr, sizeof(uip_ipaddr_t)) != 0) {
PRINTF("UDP-CLIENT: new address destination: ");
PRINT6ADDR(&dest_addr);
PRINTF("\n");
uip_udp_remove(client_conn);
client_conn = udp_new(&dest_addr, UIP_HTONS(dest_port), NULL);
if(client_conn != NULL) {
PRINTF("Created a connection with the server ");
PRINT6ADDR(&client_conn->ripaddr);
PRINTF(" local/remote port %u/%u\n",
UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport));
} else {
PRINTF("Could not open connection\n");
}
}
}
if(client_conn != NULL) {
PRINTF("Client sending to: ");
PRINT6ADDR(&client_conn->ripaddr);
i = sprintf(buf, "%d | ", ++seq_id);
dag = rpl_get_any_dag();
if(dag && dag->instance->def_route) {
add_ipaddr(buf + i, &dag->instance->def_route->ipaddr);
} else {
sprintf(buf + i, "(null)");
}
PRINTF(" (msg: %s)\n", buf);
uip_udp_packet_send(client_conn, buf, strlen(buf));
} else {
PRINTF("No connection created\n");
}
} else {
PRINTF("No address configured\n");
}
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(cetic_6lbr_client_process, ev, data)
{
PROCESS_BEGIN();
printf("6LBR Client Process\n");
memset(&dest_addr, 0, sizeof(uip_ipaddr_t));
etimer_set(&et, MSG_INTERVAL);
while(1) {
PROCESS_YIELD();
if(etimer_expired(&et)) {
timeout_handler();
etimer_set(&et, MSG_INTERVAL);
} else if(ev == tcpip_event) {
tcpip_handler();
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
/**
* @}
*/