bosko lekovic
/
ETHtoCOM_10
nova verzija tcp+udp->serial com
Udp.cpp
- Committer:
- bosko001
- Date:
- 2020-04-27
- Revision:
- 10:15f7fea18a2f
- Parent:
- 9:893843262a1f
- Child:
- 11:2cc7e6f06cff
File content as of revision 10:15f7fea18a2f:
#include "mbed.h" #include "clubbing.h" #include "EthernetSetup.h" #include "Udp.h" #include "doHTML.h" #define NO_UDP 3 struct S_EthernetAddress_Remote { char ip[16]; int port; int validy; } s_EthAddRemote[NO_UDP] , *ps_ear[NO_UDP];// //char lokaUdpPort[6] = "13000", localUdpPortFlash[6] = "65535"; /* globalni pointeri*/ extern EthernetInterface *gp_eth; extern C_HTMLparse * gpc_html; static UDPSocket *gp_udpSocket[NO_UDP] = {NULL}; static Thread *gp_udpRxThread[NO_UDP] = {NULL}; /************* inicijalizacija parametara **********************/ void init_udp_param( void ) { 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 ); } } /***************** Predaja na UDP ****************************/ void sendUdp( struct S_EthernetAddress_Remote *ps_ear, char *buffer, int val ) { if( gp_udpSocket[0] ) { printf(" UDPpaket ip=%s port=%d val=%d\n\r", ps_ear->ip, ps_ear->port, 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 ) { 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( int* i ) { //int *p = (int*)pp; nsapi_error_t err; 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); } printf("UDPrx inicijalizovan \n\r"); signed int n=0; while( !(ThisThread::flags_get( ) & 1) ) { SocketAddress sockAddr; if( (n>=0) && gp_udpSocket[0] ) { 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; else break; } //else wait(0.1); } if(gp_udpSocket[0]) { delete gp_udpSocket[0]; gp_udpSocket[0] = NULL;} printf("udpSocket obrisan\n\r"); fflush(stdout); } 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"); } void udpRxThreadRiseUp( void ) { /* paljenje UDP thread-a */ int i =0; gp_udpRxThread[i] = new Thread( &i, udpRx_fun ); } void udpRxThreadRestart( void ) { udpRxThreadCancel( ); udpRxThreadRiseUp( ); }