bosko lekovic
/
ETHtoCOM_10
nova verzija tcp+udp->serial com
Diff: Udp.cpp
- Revision:
- 10:15f7fea18a2f
- Parent:
- 9:893843262a1f
- Child:
- 11:2cc7e6f06cff
--- a/Udp.cpp Wed Apr 22 13:21:50 2020 +0000 +++ b/Udp.cpp Mon Apr 27 20:41:57 2020 +0000 @@ -3,31 +3,63 @@ #include "EthernetSetup.h" #include "Udp.h" #include "doHTML.h" + +#define NO_UDP 3 + + struct S_EthernetAddress_Remote -/*{ +{ char ip[16]; int port; -}*/ s_EthAddRemote = {"192.168.1.14", 13000 }, *ps_ear = &s_EthAddRemote; + int validy; +} s_EthAddRemote[NO_UDP] , *ps_ear[NO_UDP];// -char lokaUdpPort[6] = "13000", localUdpPortFlash[6] = "65535"; +//char lokaUdpPort[6] = "13000", localUdpPortFlash[6] = "65535"; /* globalni pointeri*/ extern EthernetInterface *gp_eth; +extern C_HTMLparse * gpc_html; -UDPSocket *gp_udpSocket = NULL; -Thread *gp_udpRxThread = NULL; +static UDPSocket *gp_udpSocket[NO_UDP] = {NULL}; +static Thread *gp_udpRxThread[NO_UDP] = {NULL}; /************* inicijalizacija parametara **********************/ -extern C_HTMLparse * gpc_html; void init_udp_param( void ) { - strcpy( ps_ear->ip, gpc_html->get_UdpIP(1) ); - ps_ear->port = atoi( gpc_html->get_UdpPort(1) ); - - printf(" UDP IP adresa: %s %d \n\r", ps_ear->ip, ps_ear->port ); + for( int i=1; i<= NO_UDP; i++) + { + const char *sp=gpc_html->get_UdpPort(i), *sip=gpc_html->get_UdpIP(i); + 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; + } + 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 ); + } } @@ -39,24 +71,25 @@ void sendUdp( struct S_EthernetAddress_Remote *ps_ear, char *buffer, int val ) { - if( gp_udpSocket ) + if( gp_udpSocket[0] ) { printf(" UDPpaket ip=%s port=%d val=%d\n\r", ps_ear->ip, ps_ear->port, val); - gp_udpSocket->sendto(ps_ear->ip, ps_ear->port, (const uint8_t*)buffer, val); + gp_udpSocket[0]->sendto(ps_ear->ip, ps_ear->port, (const uint8_t*)buffer, val); } else printf("UDPsocket == NULL\n\r"); } void sendToUdp( char *buffer, int val ) { - sendUdp( ps_ear, buffer, val ); + for( int i=0; i< NO_UDP; i++ ) + if( ps_ear[i]->validy ) sendUdp( ps_ear[i], buffer, val ); } /*************** Prijem na UDP socket i predaja na Serial com ***************/ -void udpRx_fun( void ) +void udpRx_fun( int* i ) { //int *p = (int*)pp; nsapi_error_t err; @@ -65,14 +98,14 @@ // if(gp_udpSocket == NULL) { - gp_udpSocket = new UDPSocket; + gp_udpSocket[0] = new UDPSocket; printf("___novi UDP socket %x\n\r", gp_udpSocket); init_udp_param( ); - if( (err = gp_udpSocket->open( gp_eth )) != NSAPI_ERROR_OK ) + if( (err = gp_udpSocket[0]->open( gp_eth )) != NSAPI_ERROR_OK ) { printf(" greska otvaranja soketa %d \n\r", err); } - gp_udpSocket->bind(ps_ear->port); - gp_udpSocket->set_timeout(1000); + gp_udpSocket[0]->bind(ps_ear[0]->port); + gp_udpSocket[0]->set_timeout(1000); } printf("UDPrx inicijalizovan \n\r"); @@ -81,9 +114,9 @@ while( !(ThisThread::flags_get( ) & 1) ) { SocketAddress sockAddr; - if( (n>=0) && gp_udpSocket ) + if( (n>=0) && gp_udpSocket[0] ) { - int n = gp_udpSocket->recvfrom(&sockAddr, &inbuff, sizeof(inbuff)); + int n = gp_udpSocket[0]->recvfrom(&sockAddr, &inbuff, sizeof(inbuff)); if(n>0) { extern void sendToScom( char *buffer, int val ); sendToScom( inbuff, n); } else if(n==-3001) n = 0; @@ -92,7 +125,7 @@ //else wait(0.1); } - if(gp_udpSocket) { delete gp_udpSocket; gp_udpSocket = NULL;} + if(gp_udpSocket[0]) { delete gp_udpSocket[0]; gp_udpSocket[0] = NULL;} printf("udpSocket obrisan\n\r"); fflush(stdout); } @@ -101,10 +134,10 @@ void udpRxThreadCancel( void ) { /* Gasenje UDP threada */ - if( gp_udpRxThread->get_state( ) && gp_udpRxThread->get_state( )<16 ) - gp_udpRxThread->flags_set( 1 ); - gp_udpRxThread->join( ); - if(gp_udpRxThread) {delete gp_udpRxThread; gp_udpRxThread = NULL;} + 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"); } @@ -112,7 +145,8 @@ void udpRxThreadRiseUp( void ) { /* paljenje UDP thread-a */ - gp_udpRxThread = new Thread( udpRx_fun ); + int i =0; + gp_udpRxThread[i] = new Thread( &i, udpRx_fun ); } void udpRxThreadRestart( void )