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
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}
0 commit comments