Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 110f8d4

Browse files
author
Juan Flores
committed
Autoconfiguration for nodes and coordinator, implementation of reading
1 parent 2eb83bc commit 110f8d4

File tree

6 files changed

+296
-195
lines changed

6 files changed

+296
-195
lines changed

examples/udp_6lowpan/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ STACKSIZE = $(CONFIG_EXAMPLES_UDP_6LOWPAN_STACKSIZE)
4949

5050
ASRCS =
5151
CSRCS =
52-
MAINSRC = udp_6lowpan_main.c
52+
MAINSRC = udp_6lowpan_main.c wpan_config.c udp_write.c udp_read.c
5353

5454
CONFIG_EXAMPLES_UDP_6LOWPAN_PROGNAME ?= udp_6lowpan$(EXEEXT)
5555
PROGNAME = $(CONFIG_EXAMPLES_UDP_6LOWPAN_PROGNAME)
Lines changed: 18 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/****************************************************************************
2-
* examples/hello/hello_main.c
2+
* examples/udp_6lowpan/udp_6lowpan_main.c
33
*
4-
* Copyright (C) 2008, 2011-2012 Gregory Nutt. All rights reserved.
5-
* Author: Gregory Nutt <gnutt@nuttx.org>
4+
* Copyright (C) 2019 Acutronics Robotics. All rights reserved.
5+
* Author: Acutronics Robotics <juan@erlerobotics.com>
66
*
77
* Redistribution and use in source and binary forms, with or without
88
* modification, are permitted provided that the following conditions
@@ -38,20 +38,10 @@
3838
****************************************************************************/
3939

4040
#include <nuttx/config.h>
41-
42-
#include <stdio.h>
43-
#include <stdlib.h>
44-
#include <unistd.h>
4541
#include <string.h>
46-
#include <errno.h>
4742

48-
#include <stdlib.h>
4943
#include <stdio.h>
50-
#include <arpa/inet.h>
51-
52-
#include <sys/types.h>
53-
#include <sys/socket.h>
54-
44+
#include <stdlib.h>
5545

5646
/****************************************************************************
5747
* Public Functions
@@ -64,196 +54,30 @@ void lowpan_configuration();
6454
* Variables
6555
****************************************************************************/
6656

67-
uint16_t g_udpserver_ipv6[8] =
68-
{
69-
HTONS(0),
70-
HTONS(0),
71-
HTONS(0),
72-
HTONS(0),
73-
HTONS(0),
74-
HTONS(0),
75-
HTONS(0),
76-
HTONS(0)
77-
};
78-
7957
/****************************************************************************
8058
* Main Functions
8159
****************************************************************************/
82-
int udp_6lowpan_main(int argc, char *argv[]){
60+
int udp_6lowpan_main(int argc, char *argv[]) {
8361

8462
printf("Do you want to execute the automatic WPAN configuration? (y/n)\n");
85-
char aux=getchar();
86-
if(aux=='y') lowpan_configuration();
63+
char aux = getchar();
64+
if (aux == 'y')
65+
lowpan_configuration();
8766

88-
while(1){
89-
printf("\nAvailable commands\n -To send a package type: write \n -To receive a package type: read \n -To exit type: quit\n");
67+
while (1) {
68+
printf("\nAvailable commands\n -To send a package type: write \n -To "
69+
"receive a package type: read \n -To exit type: quit\n");
9070
char buffer[256];
91-
scanf("%255s",buffer);
92-
if(strcmp(buffer, "write") == 0){
71+
scanf("%255s", buffer);
72+
if (strcmp(buffer, "write") == 0) {
9373
udp_write();
94-
}
95-
else if(strcmp(buffer, "read") == 0){
74+
} else if (strcmp(buffer, "read") == 0) {
9675
udp_read();
97-
}
98-
else if(strcmp(buffer, "quit") == 0){
99-
return 0;
100-
}
101-
else{
102-
printf("\nWrong command.\n -To send a package type: write \n -To receive a package type: read \n -To exit type: quit\n");
103-
}
104-
}
105-
106-
}
107-
108-
int udp_write(){
109-
//Get IP and destination port
110-
struct sockaddr_in6 server;
111-
112-
unsigned char outbuf[256];
113-
socklen_t addrlen;
114-
int sockfd;
115-
int nbytes;
116-
int offset;
117-
118-
119-
//Buffers
120-
char buffer[256];//Buffer to save the income data
121-
char ip_buffer[256];
122-
char port_origin[10],port_destination[10];
123-
int aux=0;
124-
uint8_t char_rec=0;
125-
126-
//Getting IPV6 destination
127-
printf("Introduce the IVP6 Destination\r\n");
128-
char_rec=scanf("%255s",ip_buffer);
129-
if(char_rec==0){
130-
printf("Error: Wrong IP\r\n");
131-
return 0;
132-
}
133-
inet_pton(AF_INET6, ip_buffer, g_udpserver_ipv6);
134-
135-
//Getting port destination
136-
printf("Introduce the port destination\r\n");
137-
char_rec=scanf("%10s",port_destination);
138-
if(char_rec==0){
139-
printf("Error: Wrong IP\r\n");
140-
return 0;
141-
}
142-
143-
printf("Introduce the port origin\r\n");
144-
char_rec=scanf("%10s",port_origin);
145-
if(char_rec==0){
146-
printf("Error: Wrong IP\r\n");
147-
return 0;
148-
}
149-
150-
printf("Conection data: \r\n -Dest_IP: %s \r\n -Dest_Port: %s\r\n -Origin_Port: %s\r\n",ip_buffer,port_destination,port_origin);
151-
152-
/**********************Creating client connection***************************/
153-
154-
//Creating socket
155-
sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
156-
if (sockfd < 0){
157-
printf("Error: Socket creation failure %d\n", errno);
158-
return 0;
159-
}
160-
161-
/* Bind the UDP socket to a IPv6 port */
162-
163-
server.sin6_family = AF_INET6;
164-
server.sin6_port = HTONS(atoi(port_origin));
165-
memset(server.sin6_addr.s6_addr, 0, sizeof(struct in6_addr));
166-
addrlen = sizeof(struct sockaddr_in6);
167-
168-
if (bind(sockfd, (FAR struct sockaddr *)&server, addrlen) < 0){
169-
printf("Error: Bind failure: %d\n", errno);
170-
return -1;
171-
}
172-
173-
/**********************Creating client connection***************************/
174-
175-
//Copy the server configuration
176-
server.sin6_family = AF_INET6;
177-
server.sin6_port = HTONS(atoi(port_destination));
178-
memcpy(server.sin6_addr.s6_addr16, g_udpserver_ipv6, 8 * sizeof(uint16_t));
179-
addrlen = sizeof(struct sockaddr_in6);
180-
181-
//Message to be send
182-
while(1){
183-
char buffer[256];
184-
printf("Introduce a message to send:\r\n");
185-
char_rec=scanf("%255s",buffer);
186-
if(strcmp("quit",buffer)==0){
187-
printf("Closing connection\n");
188-
close(sockfd);
189-
return;
190-
}
191-
printf("Sending %i characters: %s \n\n",strlen(buffer),buffer);
192-
sendto(sockfd, buffer, strlen(buffer), 0,
193-
(struct sockaddr*)&server, addrlen);
194-
}
195-
196-
197-
}
198-
199-
void udp_read(){
200-
201-
}
202-
203-
void lowpan_configuration(){
204-
char buffer[256];
205-
int id=0;
206-
printf("Starting WPAN configuration\n");
207-
printf("Type C to be coordinator\nType N to be node\n\n\n");
208-
209-
scanf("%1s",buffer);
210-
system("ifdown wpan0");
211-
if(strcmp("c",buffer)==0){
212-
//Set coordinator
213-
system("i8sak wpan0 startpan cd:ab");
214-
system("i8sak set chan 26");
215-
sleep(1);
216-
printf("Choose your ID (00 to FF)\n\n");
217-
scanf("%s",buffer);
218-
if(strlen(buffer)<2 ){
219-
printf("Error ID must be between 00 and FF\n");
220-
return 0;
221-
}
222-
system("i8sak set saddr 42:01");
223-
sprintf(buffer,"i8sak set eaddr 00:fa:de:00:de:ad:be:%c%c",buffer[0],buffer[1]);
224-
printf("Your hardware address is: %s\n\n",buffer);
225-
system(buffer);
226-
system("i8sak acceptassoc");
227-
228-
}
229-
else if(strcmp("n",buffer)==0){
230-
//Set node
231-
system("i8sak wpan0");
232-
system("i8sak set chan 26");
233-
system("i8sak set panid cd:ab");
234-
sleep(1);
235-
printf("Choose your ID (00 to FF)\n\n");
236-
scanf("%s",buffer);
237-
if(strlen(buffer)<2 ){
238-
printf("Error ID must be between 00 and FF\n");
76+
} else if (strcmp(buffer, "quit") == 0) {
23977
return 0;
78+
} else {
79+
printf("\nWrong command.\n -To send a package type: write \n -To receive "
80+
"a package type: read \n -To exit type: quit\n");
24081
}
241-
242-
sprintf(buffer,"i8sak set eaddr 00:fa:de:00:de:ad:be:%c%c",buffer[0],buffer[1]);
243-
system(buffer);
244-
system("i8sak set ep_saddr 42:01");
245-
system("i8sak set saddr 42:02");
246-
printf("Your hardware address is: %s\n\n",buffer);
247-
system("i8sak assoc");
24882
}
249-
else{
250-
printf("Wrong profile\r\n");
251-
}
252-
system("ifup wpan0");
253-
254-
printf("Mounting proc file system\n");
255-
system("mount -t procfs /proc");
256-
system("cat proc/net/wpan0");
257-
printf("\n\n");
258-
25983
}

examples/udp_6lowpan/udp_read.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
#include <errno.h>
3+
#include <string.h>
4+
#include <unistd.h>
5+
6+
#include <arpa/inet.h>
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
10+
#include <sys/socket.h>
11+
#include <sys/types.h>
12+
13+
#include <nuttx/config.h>
14+
15+
char port_origin[10];
16+
17+
int optval;
18+
int char_rec;
19+
20+
socklen_t addrlen;
21+
socklen_t recvlen;
22+
23+
struct sockaddr_in6 server;
24+
struct sockaddr_in6 client;
25+
26+
int sockfd;
27+
int nbytes;
28+
29+
int offset;
30+
31+
void udp_read() {
32+
33+
printf("Introduce the reception port\r\n");
34+
char_rec = scanf("%10s", port_origin);
35+
if (char_rec == 0) {
36+
printf("Error: Wrong port\r\n");
37+
return 0;
38+
}
39+
/* Create a new UDP socket */
40+
41+
sockfd = socket(PF_INET6, SOCK_DGRAM, 0);
42+
if (sockfd < 0) {
43+
printf("server: socket failure: %d\n", errno);
44+
exit(1);
45+
}
46+
47+
optval = 1;
48+
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *)&optval,
49+
sizeof(int)) < 0) {
50+
printf("server: setsockopt SO_REUSEADDR failure: %d\n", errno);
51+
exit(1);
52+
}
53+
54+
/* Bind the socket to a local address */
55+
56+
server.sin6_family = AF_INET6;
57+
server.sin6_port = HTONS(atoi(port_origin));
58+
59+
memset(&server.sin6_addr, 0, sizeof(struct in6_addr));
60+
61+
addrlen = sizeof(struct sockaddr_in6);
62+
63+
if (bind(sockfd, (struct sockaddr *)&server, addrlen) < 0) {
64+
printf("server: bind failure: %d\n", errno);
65+
exit(1);
66+
}
67+
68+
printf("Listening on %s for input packets\n", port_origin);
69+
70+
sleep(5);
71+
recvlen = addrlen;
72+
while (1) {
73+
unsigned char inbuf[1024];
74+
memset(&inbuf, 0, 1024);
75+
nbytes =
76+
recvfrom(sockfd, inbuf, 1024, 0, (struct sockaddr *)&client, &recvlen);
77+
78+
printf("Received %d bytes from %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
79+
"port %d\n",
80+
nbytes, client.sin6_addr.s6_addr16[0], client.sin6_addr.s6_addr16[1],
81+
client.sin6_addr.s6_addr16[2], client.sin6_addr.s6_addr16[3],
82+
client.sin6_addr.s6_addr16[4], client.sin6_addr.s6_addr16[5],
83+
client.sin6_addr.s6_addr16[6], client.sin6_addr.s6_addr16[7],
84+
ntohs(client.sin6_port));
85+
printf("Received packet: ");
86+
printf(inbuf);
87+
printf("\n");
88+
89+
usleep(100);
90+
}
91+
}

0 commit comments

Comments
 (0)