bosko lekovic
/
EthToCom_11
nova proba
Diff: Udp.cpp
- Revision:
- 11:2cc7e6f06cff
- Parent:
- 10:15f7fea18a2f
- Child:
- 12:cbfc10c15437
--- a/Udp.cpp Mon Apr 27 20:41:57 2020 +0000 +++ b/Udp.cpp Fri May 01 10:43:05 2020 +0000 @@ -4,7 +4,6 @@ #include "Udp.h" #include "doHTML.h" -#define NO_UDP 3 struct S_EthernetAddress_Remote @@ -31,31 +30,30 @@ { for( int i=1; i<= NO_UDP; i++) { - const char *sp=gpc_html->get_UdpPort(i), *sip=gpc_html->get_UdpIP(i); + const char *sp, *sip=gpc_html->get_UdpIP(i); + + if( i < 5 ) sp = gpc_html->get_UdpPort(i); + else + { + sp = gpc_html->get_UdpPort(4); + sip=gpc_html->get_UdpIP(4); + } + printf( " i = %d, port = %s, ip = %s\n\r", i, sp, sip ); fflush(stdout); + + ps_ear[i-1] = &(s_EthAddRemote[i-1]); if( test_ip( sip ) && test_num( sp ) ) { - printf("printanje i=%d\n\r", i ); - fflush(stdout); - - printf("printanje ethadrstruct=%d\n\r", ps_ear[i-1] ); - fflush(stdout); - strncpy( ps_ear[i-1]->ip, gpc_html->get_UdpIP(i), 16 ); - printf("printanje ip=%s\n\r", ps_ear[i-1]->ip ); - fflush(stdout); - ps_ear[i-1]->port = atoi( gpc_html->get_UdpPort(i) ); - printf("printanje i=%d\n\r", ps_ear[i-1]->port ); - fflush(stdout); - ps_ear[i-1]->validy = 1; + strncpy( ps_ear[i-1]->ip, sip, 16 ); + ps_ear[i-1]->port = atoi( sp ); + ps_ear[i-1]->validy = 1; } else { - printf("printanje validija=%d\n\r", ps_ear[i-1]->validy ); - fflush(stdout); ps_ear[i-1]->validy = 0; } printf(" UDP IP adresa: %s port = %d validy %d\n\r", ps_ear[i-1]->ip, ps_ear[i-1]->port, ps_ear[i-1]->validy ); @@ -63,10 +61,6 @@ } - - - - /***************** Predaja na UDP ****************************/ void sendUdp( struct S_EthernetAddress_Remote *ps_ear, char *buffer, int val ) @@ -79,33 +73,42 @@ else printf("UDPsocket == NULL\n\r"); } +void sendUdpi( int i, char *buffer, int val ) +{ + if( gp_udpSocket[i] ) + { + printf(" UDPpaket ip=%s port=%d val=%d\n\r", ps_ear[i]->ip, ps_ear[i]->port, val); + gp_udpSocket[i]->sendto(ps_ear[i]->ip, ps_ear[i]->port, (const uint8_t*)buffer, val); + } + else printf("UDPsocket[%d] == NULL\n\r", i); +} + void sendToUdp( char *buffer, int val ) { for( int i=0; i< NO_UDP; i++ ) - if( ps_ear[i]->validy ) sendUdp( ps_ear[i], buffer, val ); + if( ps_ear[i]->validy ) sendUdpi( i, buffer, val ); } /*************** Prijem na UDP socket i predaja na Serial com ***************/ -void udpRx_fun( int* i ) +void udpRx_fun( int* par_i ) { -//int *p = (int*)pp; - nsapi_error_t err; - + int i = *par_i; +// nsapi_error_t err; +printf("udp kanal %d\n\r", i); char inbuff[1500]; // if(gp_udpSocket == NULL) { - gp_udpSocket[0] = new UDPSocket; - printf("___novi UDP socket %x\n\r", gp_udpSocket); - init_udp_param( ); - if( (err = gp_udpSocket[0]->open( gp_eth )) != NSAPI_ERROR_OK ) - { printf(" greska otvaranja soketa %d \n\r", err); } - gp_udpSocket[0]->bind(ps_ear[0]->port); - gp_udpSocket[0]->set_timeout(1000); +// gp_udpSocket[i] = new UDPSocket; +// printf("___novi UDP socket[%d] %x\n\r", i, gp_udpSocket[i]); +// if( (err = gp_udpSocket[i]->open( gp_eth )) != NSAPI_ERROR_OK ) +// { printf(" greska otvaranja soketa %d \n\r", err); } +// gp_udpSocket[i]->bind(ps_ear[0]->port); +// gp_udpSocket[i]->set_timeout(1000); } printf("UDPrx inicijalizovan \n\r"); @@ -114,9 +117,9 @@ while( !(ThisThread::flags_get( ) & 1) ) { SocketAddress sockAddr; - if( (n>=0) && gp_udpSocket[0] ) + if( (n>=0) && gp_udpSocket[i] ) { - int n = gp_udpSocket[0]->recvfrom(&sockAddr, &inbuff, sizeof(inbuff)); + int n = gp_udpSocket[i]->recvfrom(&sockAddr, &inbuff, sizeof(inbuff)); if(n>0) { extern void sendToScom( char *buffer, int val ); sendToScom( inbuff, n); } else if(n==-3001) n = 0; @@ -125,7 +128,7 @@ //else wait(0.1); } - if(gp_udpSocket[0]) { delete gp_udpSocket[0]; gp_udpSocket[0] = NULL;} + if(gp_udpSocket[i]) { delete gp_udpSocket[i]; gp_udpSocket[i] = NULL;} printf("udpSocket obrisan\n\r"); fflush(stdout); } @@ -134,19 +137,47 @@ void udpRxThreadCancel( void ) { /* Gasenje UDP threada */ - if( gp_udpRxThread[0]->get_state( ) && gp_udpRxThread[0]->get_state( )<16 ) - gp_udpRxThread[0]->flags_set( 1 ); - gp_udpRxThread[0]->join( ); - if(gp_udpRxThread[0]) {delete gp_udpRxThread[0]; gp_udpRxThread[0] = NULL;} - printf("udp thread zavrsio\n\r"); + for(int i =0; i<NO_UDP; i++ ) + { + if( gp_udpRxThread[i]->get_state( ) && gp_udpRxThread[i]->get_state( )<16 ) + gp_udpRxThread[i]->flags_set( 1 ); + gp_udpRxThread[i]->join( ); + if(gp_udpRxThread[i]) {delete gp_udpRxThread[i]; gp_udpRxThread[i] = NULL;} + printf("udp thread[%d] zavrsio\n\r", i); + ps_ear[i]->validy = 0; + + // !!!!! ugasiti i sockete + } } + + TCPSocket *p_tcpSocket[2]; /* socket za prijem zahteva za konekciju */ void udpRxThreadRiseUp( void ) { /* paljenje UDP thread-a */ - int i =0; - gp_udpRxThread[i] = new Thread( &i, udpRx_fun ); + nsapi_error_t err; + + init_udp_param( ); + + for(int i =0; i<NO_UDP; i++ ) + if( ps_ear[i]->validy ) + { + + + gp_udpSocket[i] = new UDPSocket; + printf("___novi UDP socket[%d] %x\n\r", i, gp_udpSocket[i]); + + if( (err = gp_udpSocket[i]->open( gp_eth )) != NSAPI_ERROR_OK ) + { printf(" greska otvaranja UDP soketa %d err=%d \n\r", i, err); } + gp_udpSocket[i]->bind(ps_ear[0]->port); + gp_udpSocket[i]->set_timeout(1000); + + gp_udpRxThread[i] = new Thread( &i, udpRx_fun ); + printf( " dprxthread [%d] =%x\n\r", i, &gp_udpRxThread[i] ); + } +// gp_udpRxThread[i]->start( callback( udpRx_fun, &i ) ); +// } void udpRxThreadRestart( void )