nova proba

Committer:
bosko001
Date:
Mon Apr 27 20:41:57 2020 +0000
Revision:
10:15f7fea18a2f
Parent:
9:893843262a1f
Child:
11:2cc7e6f06cff
radi, samo jedan udp kanal

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bosko001 9:893843262a1f 1 #include "mbed.h"
bosko001 9:893843262a1f 2 #include "clubbing.h"
bosko001 9:893843262a1f 3 #include "EthernetSetup.h"
bosko001 9:893843262a1f 4 #include "Udp.h"
bosko001 9:893843262a1f 5 #include "doHTML.h"
bosko001 10:15f7fea18a2f 6
bosko001 10:15f7fea18a2f 7 #define NO_UDP 3
bosko001 10:15f7fea18a2f 8
bosko001 10:15f7fea18a2f 9
bosko001 9:893843262a1f 10 struct S_EthernetAddress_Remote
bosko001 10:15f7fea18a2f 11 {
bosko001 9:893843262a1f 12 char ip[16];
bosko001 9:893843262a1f 13 int port;
bosko001 10:15f7fea18a2f 14 int validy;
bosko001 10:15f7fea18a2f 15 } s_EthAddRemote[NO_UDP] , *ps_ear[NO_UDP];//
bosko001 9:893843262a1f 16
bosko001 10:15f7fea18a2f 17 //char lokaUdpPort[6] = "13000", localUdpPortFlash[6] = "65535";
bosko001 9:893843262a1f 18
bosko001 9:893843262a1f 19 /* globalni pointeri*/
bosko001 9:893843262a1f 20
bosko001 9:893843262a1f 21
bosko001 9:893843262a1f 22 extern EthernetInterface *gp_eth;
bosko001 10:15f7fea18a2f 23 extern C_HTMLparse * gpc_html;
bosko001 9:893843262a1f 24
bosko001 10:15f7fea18a2f 25 static UDPSocket *gp_udpSocket[NO_UDP] = {NULL};
bosko001 10:15f7fea18a2f 26 static Thread *gp_udpRxThread[NO_UDP] = {NULL};
bosko001 9:893843262a1f 27
bosko001 9:893843262a1f 28 /************* inicijalizacija parametara **********************/
bosko001 9:893843262a1f 29
bosko001 9:893843262a1f 30 void init_udp_param( void )
bosko001 9:893843262a1f 31 {
bosko001 10:15f7fea18a2f 32 for( int i=1; i<= NO_UDP; i++)
bosko001 10:15f7fea18a2f 33 {
bosko001 10:15f7fea18a2f 34 const char *sp=gpc_html->get_UdpPort(i), *sip=gpc_html->get_UdpIP(i);
bosko001 10:15f7fea18a2f 35 printf( " i = %d, port = %s, ip = %s\n\r", i, sp, sip );
bosko001 10:15f7fea18a2f 36 fflush(stdout);
bosko001 10:15f7fea18a2f 37
bosko001 10:15f7fea18a2f 38 ps_ear[i-1] = &(s_EthAddRemote[i-1]);
bosko001 10:15f7fea18a2f 39
bosko001 10:15f7fea18a2f 40 if( test_ip( sip ) && test_num( sp ) )
bosko001 10:15f7fea18a2f 41 {
bosko001 10:15f7fea18a2f 42 printf("printanje i=%d\n\r", i );
bosko001 10:15f7fea18a2f 43 fflush(stdout);
bosko001 10:15f7fea18a2f 44
bosko001 10:15f7fea18a2f 45 printf("printanje ethadrstruct=%d\n\r", ps_ear[i-1] );
bosko001 10:15f7fea18a2f 46 fflush(stdout);
bosko001 10:15f7fea18a2f 47 strncpy( ps_ear[i-1]->ip, gpc_html->get_UdpIP(i), 16 );
bosko001 10:15f7fea18a2f 48 printf("printanje ip=%s\n\r", ps_ear[i-1]->ip );
bosko001 10:15f7fea18a2f 49 fflush(stdout);
bosko001 10:15f7fea18a2f 50 ps_ear[i-1]->port = atoi( gpc_html->get_UdpPort(i) );
bosko001 10:15f7fea18a2f 51 printf("printanje i=%d\n\r", ps_ear[i-1]->port );
bosko001 10:15f7fea18a2f 52 fflush(stdout);
bosko001 10:15f7fea18a2f 53 ps_ear[i-1]->validy = 1;
bosko001 10:15f7fea18a2f 54 }
bosko001 10:15f7fea18a2f 55 else
bosko001 10:15f7fea18a2f 56 {
bosko001 10:15f7fea18a2f 57 printf("printanje validija=%d\n\r", ps_ear[i-1]->validy );
bosko001 10:15f7fea18a2f 58 fflush(stdout);
bosko001 10:15f7fea18a2f 59 ps_ear[i-1]->validy = 0;
bosko001 10:15f7fea18a2f 60 }
bosko001 10:15f7fea18a2f 61 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 );
bosko001 10:15f7fea18a2f 62 }
bosko001 9:893843262a1f 63 }
bosko001 9:893843262a1f 64
bosko001 9:893843262a1f 65
bosko001 9:893843262a1f 66
bosko001 9:893843262a1f 67
bosko001 9:893843262a1f 68
bosko001 9:893843262a1f 69
bosko001 9:893843262a1f 70 /***************** Predaja na UDP ****************************/
bosko001 9:893843262a1f 71
bosko001 9:893843262a1f 72 void sendUdp( struct S_EthernetAddress_Remote *ps_ear, char *buffer, int val )
bosko001 9:893843262a1f 73 {
bosko001 10:15f7fea18a2f 74 if( gp_udpSocket[0] )
bosko001 9:893843262a1f 75 {
bosko001 9:893843262a1f 76 printf(" UDPpaket ip=%s port=%d val=%d\n\r", ps_ear->ip, ps_ear->port, val);
bosko001 10:15f7fea18a2f 77 gp_udpSocket[0]->sendto(ps_ear->ip, ps_ear->port, (const uint8_t*)buffer, val);
bosko001 9:893843262a1f 78 }
bosko001 9:893843262a1f 79 else printf("UDPsocket == NULL\n\r");
bosko001 9:893843262a1f 80 }
bosko001 9:893843262a1f 81
bosko001 9:893843262a1f 82 void sendToUdp( char *buffer, int val )
bosko001 9:893843262a1f 83 {
bosko001 10:15f7fea18a2f 84 for( int i=0; i< NO_UDP; i++ )
bosko001 10:15f7fea18a2f 85 if( ps_ear[i]->validy ) sendUdp( ps_ear[i], buffer, val );
bosko001 9:893843262a1f 86 }
bosko001 9:893843262a1f 87
bosko001 9:893843262a1f 88
bosko001 9:893843262a1f 89
bosko001 9:893843262a1f 90 /*************** Prijem na UDP socket i predaja na Serial com ***************/
bosko001 9:893843262a1f 91
bosko001 10:15f7fea18a2f 92 void udpRx_fun( int* i )
bosko001 9:893843262a1f 93 {
bosko001 9:893843262a1f 94 //int *p = (int*)pp;
bosko001 9:893843262a1f 95 nsapi_error_t err;
bosko001 9:893843262a1f 96
bosko001 9:893843262a1f 97 char inbuff[1500];
bosko001 9:893843262a1f 98
bosko001 9:893843262a1f 99 // if(gp_udpSocket == NULL)
bosko001 9:893843262a1f 100 {
bosko001 10:15f7fea18a2f 101 gp_udpSocket[0] = new UDPSocket;
bosko001 9:893843262a1f 102 printf("___novi UDP socket %x\n\r", gp_udpSocket);
bosko001 9:893843262a1f 103 init_udp_param( );
bosko001 9:893843262a1f 104
bosko001 10:15f7fea18a2f 105 if( (err = gp_udpSocket[0]->open( gp_eth )) != NSAPI_ERROR_OK )
bosko001 9:893843262a1f 106 { printf(" greska otvaranja soketa %d \n\r", err); }
bosko001 10:15f7fea18a2f 107 gp_udpSocket[0]->bind(ps_ear[0]->port);
bosko001 10:15f7fea18a2f 108 gp_udpSocket[0]->set_timeout(1000);
bosko001 9:893843262a1f 109 }
bosko001 9:893843262a1f 110 printf("UDPrx inicijalizovan \n\r");
bosko001 9:893843262a1f 111
bosko001 9:893843262a1f 112
bosko001 9:893843262a1f 113 signed int n=0;
bosko001 9:893843262a1f 114 while( !(ThisThread::flags_get( ) & 1) )
bosko001 9:893843262a1f 115 {
bosko001 9:893843262a1f 116 SocketAddress sockAddr;
bosko001 10:15f7fea18a2f 117 if( (n>=0) && gp_udpSocket[0] )
bosko001 9:893843262a1f 118 {
bosko001 10:15f7fea18a2f 119 int n = gp_udpSocket[0]->recvfrom(&sockAddr, &inbuff, sizeof(inbuff));
bosko001 9:893843262a1f 120
bosko001 9:893843262a1f 121 if(n>0) { extern void sendToScom( char *buffer, int val ); sendToScom( inbuff, n); }
bosko001 9:893843262a1f 122 else if(n==-3001) n = 0;
bosko001 9:893843262a1f 123 else break;
bosko001 9:893843262a1f 124 }
bosko001 9:893843262a1f 125 //else wait(0.1);
bosko001 9:893843262a1f 126 }
bosko001 9:893843262a1f 127
bosko001 10:15f7fea18a2f 128 if(gp_udpSocket[0]) { delete gp_udpSocket[0]; gp_udpSocket[0] = NULL;}
bosko001 9:893843262a1f 129 printf("udpSocket obrisan\n\r");
bosko001 9:893843262a1f 130 fflush(stdout);
bosko001 9:893843262a1f 131 }
bosko001 9:893843262a1f 132
bosko001 9:893843262a1f 133
bosko001 9:893843262a1f 134 void udpRxThreadCancel( void )
bosko001 9:893843262a1f 135 {
bosko001 9:893843262a1f 136 /* Gasenje UDP threada */
bosko001 10:15f7fea18a2f 137 if( gp_udpRxThread[0]->get_state( ) && gp_udpRxThread[0]->get_state( )<16 )
bosko001 10:15f7fea18a2f 138 gp_udpRxThread[0]->flags_set( 1 );
bosko001 10:15f7fea18a2f 139 gp_udpRxThread[0]->join( );
bosko001 10:15f7fea18a2f 140 if(gp_udpRxThread[0]) {delete gp_udpRxThread[0]; gp_udpRxThread[0] = NULL;}
bosko001 9:893843262a1f 141 printf("udp thread zavrsio\n\r");
bosko001 9:893843262a1f 142
bosko001 9:893843262a1f 143 }
bosko001 9:893843262a1f 144
bosko001 9:893843262a1f 145 void udpRxThreadRiseUp( void )
bosko001 9:893843262a1f 146 {
bosko001 9:893843262a1f 147 /* paljenje UDP thread-a */
bosko001 10:15f7fea18a2f 148 int i =0;
bosko001 10:15f7fea18a2f 149 gp_udpRxThread[i] = new Thread( &i, udpRx_fun );
bosko001 9:893843262a1f 150 }
bosko001 9:893843262a1f 151
bosko001 9:893843262a1f 152 void udpRxThreadRestart( void )
bosko001 9:893843262a1f 153 {
bosko001 9:893843262a1f 154 udpRxThreadCancel( );
bosko001 9:893843262a1f 155 udpRxThreadRiseUp( );
bosko001 9:893843262a1f 156 }
bosko001 9:893843262a1f 157