5858 ****************************************************************************/
5959int udp_write ();
6060void udp_read ();
61+ void lowpan_configuration ();
6162
6263/****************************************************************************
6364 * Variables
@@ -79,22 +80,29 @@ uint16_t g_udpserver_ipv6[8] =
7980 * Main Functions
8081 ****************************************************************************/
8182int udp_6lowpan_main (int argc , char * argv []){
82- if (argc < 2 ){
83- //Mostrar el eror
84- printf ("Bad ussage \r\n" );
85- return 0 ;
86- }
8783
88- if (strcmp (argv [1 ], "write" ) == 0 ){
89- udp_write ();
90- }
91- else if (strcmp (argv [1 ], "read" ) == 0 ){
92- udp_read ();
93- }
94- else {
95- printf ("Error\r\n" );
96- return 0 ;
84+ printf ("Do you want to execute the automatic WPAN configuration? (y/n)\n" );
85+ char aux = getchar ();
86+ if (aux == 'y' ) lowpan_configuration ();
87+
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" );
90+ char buffer [256 ];
91+ scanf ("%255s" ,buffer );
92+ if (strcmp (buffer , "write" ) == 0 ){
93+ udp_write ();
94+ }
95+ else if (strcmp (buffer , "read" ) == 0 ){
96+ 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+ }
97104 }
105+
98106}
99107
100108int udp_write (){
@@ -153,7 +161,7 @@ int udp_write(){
153161 /* Bind the UDP socket to a IPv6 port */
154162
155163 server .sin6_family = AF_INET6 ;
156- server .sin6_port = HTONS (8080 );
164+ server .sin6_port = HTONS (atoi ( port_origin ) );
157165 memset (server .sin6_addr .s6_addr , 0 , sizeof (struct in6_addr ));
158166 addrlen = sizeof (struct sockaddr_in6 );
159167
@@ -166,7 +174,7 @@ int udp_write(){
166174
167175 //Copy the server configuration
168176 server .sin6_family = AF_INET6 ;
169- server .sin6_port = HTONS (61616 );
177+ server .sin6_port = HTONS (atoi ( port_destination ) );
170178 memcpy (server .sin6_addr .s6_addr16 , g_udpserver_ipv6 , 8 * sizeof (uint16_t ));
171179 addrlen = sizeof (struct sockaddr_in6 );
172180
@@ -175,9 +183,13 @@ int udp_write(){
175183 char buffer [256 ];
176184 printf ("Introduce a message to send:\r\n" );
177185 char_rec = scanf ("%255s" ,buffer );
178- if (strcmp ("quit" ,buffer )== 0 ) return 0 ;
179- printf ("Sending %i characters: %s \r\n" ,char_rec ,buffer );
180- sendto (sockfd , buffer , 4 , 0 ,
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 ,
181193 (struct sockaddr * )& server , addrlen );
182194 }
183195
@@ -189,5 +201,59 @@ void udp_read(){
189201}
190202
191203void 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" );
239+ return 0 ;
240+ }
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" );
248+ }
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" );
192258
193259}
0 commit comments