nova verzija tcp+udp->serial com

Committer:
bosko001
Date:
Fri May 01 23:15:28 2020 +0000
Revision:
12:cbfc10c15437
Parent:
11:2cc7e6f06cff
Child:
14:ff1df10bcd65
popravljeno

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
bosko001 10:15f7fea18a2f 8
bosko001 9:893843262a1f 9 struct S_EthernetAddress_Remote
bosko001 10:15f7fea18a2f 10 {
bosko001 9:893843262a1f 11 char ip[16];
bosko001 9:893843262a1f 12 int port;
bosko001 10:15f7fea18a2f 13 int validy;
bosko001 10:15f7fea18a2f 14 } s_EthAddRemote[NO_UDP] , *ps_ear[NO_UDP];//
bosko001 9:893843262a1f 15
bosko001 10:15f7fea18a2f 16 //char lokaUdpPort[6] = "13000", localUdpPortFlash[6] = "65535";
bosko001 9:893843262a1f 17
bosko001 9:893843262a1f 18 /* globalni pointeri*/
bosko001 9:893843262a1f 19
bosko001 9:893843262a1f 20
bosko001 9:893843262a1f 21 extern EthernetInterface *gp_eth;
bosko001 10:15f7fea18a2f 22 extern C_HTMLparse * gpc_html;
bosko001 9:893843262a1f 23
bosko001 10:15f7fea18a2f 24 static UDPSocket *gp_udpSocket[NO_UDP] = {NULL};
bosko001 10:15f7fea18a2f 25 static Thread *gp_udpRxThread[NO_UDP] = {NULL};
bosko001 9:893843262a1f 26
bosko001 9:893843262a1f 27 /************* inicijalizacija parametara **********************/
bosko001 9:893843262a1f 28
bosko001 9:893843262a1f 29 void init_udp_param( void )
bosko001 9:893843262a1f 30 {
bosko001 10:15f7fea18a2f 31 for( int i=1; i<= NO_UDP; i++)
bosko001 10:15f7fea18a2f 32 {
bosko001 11:2cc7e6f06cff 33 const char *sp, *sip=gpc_html->get_UdpIP(i);
bosko001 11:2cc7e6f06cff 34
bosko001 11:2cc7e6f06cff 35 if( i < 5 ) sp = gpc_html->get_UdpPort(i);
bosko001 11:2cc7e6f06cff 36 else
bosko001 11:2cc7e6f06cff 37 {
bosko001 11:2cc7e6f06cff 38 sp = gpc_html->get_UdpPort(4);
bosko001 11:2cc7e6f06cff 39 sip=gpc_html->get_UdpIP(4);
bosko001 11:2cc7e6f06cff 40 }
bosko001 11:2cc7e6f06cff 41
bosko001 10:15f7fea18a2f 42 printf( " i = %d, port = %s, ip = %s\n\r", i, sp, sip );
bosko001 10:15f7fea18a2f 43 fflush(stdout);
bosko001 10:15f7fea18a2f 44
bosko001 11:2cc7e6f06cff 45
bosko001 11:2cc7e6f06cff 46
bosko001 10:15f7fea18a2f 47 ps_ear[i-1] = &(s_EthAddRemote[i-1]);
bosko001 10:15f7fea18a2f 48
bosko001 10:15f7fea18a2f 49 if( test_ip( sip ) && test_num( sp ) )
bosko001 10:15f7fea18a2f 50 {
bosko001 11:2cc7e6f06cff 51 strncpy( ps_ear[i-1]->ip, sip, 16 );
bosko001 11:2cc7e6f06cff 52 ps_ear[i-1]->port = atoi( sp );
bosko001 11:2cc7e6f06cff 53 ps_ear[i-1]->validy = 1;
bosko001 10:15f7fea18a2f 54 }
bosko001 10:15f7fea18a2f 55 else
bosko001 10:15f7fea18a2f 56 {
bosko001 10:15f7fea18a2f 57 ps_ear[i-1]->validy = 0;
bosko001 10:15f7fea18a2f 58 }
bosko001 10:15f7fea18a2f 59 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 60 }
bosko001 9:893843262a1f 61 }
bosko001 9:893843262a1f 62
bosko001 9:893843262a1f 63
bosko001 9:893843262a1f 64 /***************** Predaja na UDP ****************************/
bosko001 9:893843262a1f 65
bosko001 9:893843262a1f 66 void sendUdp( struct S_EthernetAddress_Remote *ps_ear, char *buffer, int val )
bosko001 9:893843262a1f 67 {
bosko001 10:15f7fea18a2f 68 if( gp_udpSocket[0] )
bosko001 9:893843262a1f 69 {
bosko001 9:893843262a1f 70 printf(" UDPpaket ip=%s port=%d val=%d\n\r", ps_ear->ip, ps_ear->port, val);
bosko001 10:15f7fea18a2f 71 gp_udpSocket[0]->sendto(ps_ear->ip, ps_ear->port, (const uint8_t*)buffer, val);
bosko001 9:893843262a1f 72 }
bosko001 9:893843262a1f 73 else printf("UDPsocket == NULL\n\r");
bosko001 9:893843262a1f 74 }
bosko001 9:893843262a1f 75
bosko001 11:2cc7e6f06cff 76 void sendUdpi( int i, char *buffer, int val )
bosko001 11:2cc7e6f06cff 77 {
bosko001 11:2cc7e6f06cff 78 if( gp_udpSocket[i] )
bosko001 11:2cc7e6f06cff 79 {
bosko001 11:2cc7e6f06cff 80 printf(" UDPpaket ip=%s port=%d val=%d\n\r", ps_ear[i]->ip, ps_ear[i]->port, val);
bosko001 11:2cc7e6f06cff 81 gp_udpSocket[i]->sendto(ps_ear[i]->ip, ps_ear[i]->port, (const uint8_t*)buffer, val);
bosko001 11:2cc7e6f06cff 82 }
bosko001 11:2cc7e6f06cff 83 else printf("UDPsocket[%d] == NULL\n\r", i);
bosko001 11:2cc7e6f06cff 84 }
bosko001 11:2cc7e6f06cff 85
bosko001 9:893843262a1f 86 void sendToUdp( char *buffer, int val )
bosko001 9:893843262a1f 87 {
bosko001 10:15f7fea18a2f 88 for( int i=0; i< NO_UDP; i++ )
bosko001 11:2cc7e6f06cff 89 if( ps_ear[i]->validy ) sendUdpi( i, buffer, val );
bosko001 9:893843262a1f 90 }
bosko001 9:893843262a1f 91
bosko001 9:893843262a1f 92
bosko001 9:893843262a1f 93
bosko001 9:893843262a1f 94 /*************** Prijem na UDP socket i predaja na Serial com ***************/
bosko001 9:893843262a1f 95
bosko001 11:2cc7e6f06cff 96 void udpRx_fun( int* par_i )
bosko001 9:893843262a1f 97 {
bosko001 11:2cc7e6f06cff 98 int i = *par_i;
bosko001 11:2cc7e6f06cff 99 // nsapi_error_t err;
bosko001 11:2cc7e6f06cff 100 printf("udp kanal %d\n\r", i);
bosko001 9:893843262a1f 101 char inbuff[1500];
bosko001 9:893843262a1f 102
bosko001 9:893843262a1f 103 // if(gp_udpSocket == NULL)
bosko001 9:893843262a1f 104 {
bosko001 9:893843262a1f 105
bosko001 11:2cc7e6f06cff 106 // gp_udpSocket[i] = new UDPSocket;
bosko001 11:2cc7e6f06cff 107 // printf("___novi UDP socket[%d] %x\n\r", i, gp_udpSocket[i]);
bosko001 11:2cc7e6f06cff 108 // if( (err = gp_udpSocket[i]->open( gp_eth )) != NSAPI_ERROR_OK )
bosko001 11:2cc7e6f06cff 109 // { printf(" greska otvaranja soketa %d \n\r", err); }
bosko001 11:2cc7e6f06cff 110 // gp_udpSocket[i]->bind(ps_ear[0]->port);
bosko001 11:2cc7e6f06cff 111 // gp_udpSocket[i]->set_timeout(1000);
bosko001 9:893843262a1f 112 }
bosko001 9:893843262a1f 113 printf("UDPrx inicijalizovan \n\r");
bosko001 9:893843262a1f 114
bosko001 9:893843262a1f 115
bosko001 9:893843262a1f 116 signed int n=0;
bosko001 9:893843262a1f 117 while( !(ThisThread::flags_get( ) & 1) )
bosko001 9:893843262a1f 118 {
bosko001 9:893843262a1f 119 SocketAddress sockAddr;
bosko001 11:2cc7e6f06cff 120 if( (n>=0) && gp_udpSocket[i] )
bosko001 9:893843262a1f 121 {
bosko001 11:2cc7e6f06cff 122 int n = gp_udpSocket[i]->recvfrom(&sockAddr, &inbuff, sizeof(inbuff));
bosko001 9:893843262a1f 123
bosko001 9:893843262a1f 124 if(n>0) { extern void sendToScom( char *buffer, int val ); sendToScom( inbuff, n); }
bosko001 9:893843262a1f 125 else if(n==-3001) n = 0;
bosko001 9:893843262a1f 126 else break;
bosko001 9:893843262a1f 127 }
bosko001 9:893843262a1f 128 //else wait(0.1);
bosko001 9:893843262a1f 129 }
bosko001 9:893843262a1f 130
bosko001 11:2cc7e6f06cff 131 if(gp_udpSocket[i]) { delete gp_udpSocket[i]; gp_udpSocket[i] = NULL;}
bosko001 9:893843262a1f 132 printf("udpSocket obrisan\n\r");
bosko001 9:893843262a1f 133 fflush(stdout);
bosko001 9:893843262a1f 134 }
bosko001 9:893843262a1f 135
bosko001 9:893843262a1f 136
bosko001 9:893843262a1f 137 void udpRxThreadCancel( void )
bosko001 9:893843262a1f 138 {
bosko001 9:893843262a1f 139 /* Gasenje UDP threada */
bosko001 11:2cc7e6f06cff 140 for(int i =0; i<NO_UDP; i++ )
bosko001 11:2cc7e6f06cff 141 {
bosko001 12:cbfc10c15437 142 if(ps_ear[i]->validy )
bosko001 12:cbfc10c15437 143 {
bosko001 11:2cc7e6f06cff 144 if( gp_udpRxThread[i]->get_state( ) && gp_udpRxThread[i]->get_state( )<16 )
bosko001 11:2cc7e6f06cff 145 gp_udpRxThread[i]->flags_set( 1 );
bosko001 12:cbfc10c15437 146 gp_udpRxThread[i]->join( );
bosko001 12:cbfc10c15437 147
bosko001 11:2cc7e6f06cff 148 if(gp_udpRxThread[i]) {delete gp_udpRxThread[i]; gp_udpRxThread[i] = NULL;}
bosko001 12:cbfc10c15437 149 }
bosko001 12:cbfc10c15437 150
bosko001 11:2cc7e6f06cff 151 printf("udp thread[%d] zavrsio\n\r", i);
bosko001 11:2cc7e6f06cff 152 ps_ear[i]->validy = 0;
bosko001 11:2cc7e6f06cff 153
bosko001 11:2cc7e6f06cff 154 // !!!!! ugasiti i sockete
bosko001 11:2cc7e6f06cff 155 }
bosko001 9:893843262a1f 156
bosko001 9:893843262a1f 157 }
bosko001 9:893843262a1f 158
bosko001 11:2cc7e6f06cff 159
bosko001 11:2cc7e6f06cff 160 TCPSocket *p_tcpSocket[2]; /* socket za prijem zahteva za konekciju */
bosko001 9:893843262a1f 161 void udpRxThreadRiseUp( void )
bosko001 9:893843262a1f 162 {
bosko001 9:893843262a1f 163 /* paljenje UDP thread-a */
bosko001 11:2cc7e6f06cff 164 nsapi_error_t err;
bosko001 11:2cc7e6f06cff 165
bosko001 11:2cc7e6f06cff 166 init_udp_param( );
bosko001 11:2cc7e6f06cff 167
bosko001 11:2cc7e6f06cff 168 for(int i =0; i<NO_UDP; i++ )
bosko001 11:2cc7e6f06cff 169 if( ps_ear[i]->validy )
bosko001 11:2cc7e6f06cff 170 {
bosko001 11:2cc7e6f06cff 171
bosko001 11:2cc7e6f06cff 172
bosko001 11:2cc7e6f06cff 173 gp_udpSocket[i] = new UDPSocket;
bosko001 11:2cc7e6f06cff 174 printf("___novi UDP socket[%d] %x\n\r", i, gp_udpSocket[i]);
bosko001 11:2cc7e6f06cff 175
bosko001 11:2cc7e6f06cff 176 if( (err = gp_udpSocket[i]->open( gp_eth )) != NSAPI_ERROR_OK )
bosko001 11:2cc7e6f06cff 177 { printf(" greska otvaranja UDP soketa %d err=%d \n\r", i, err); }
bosko001 11:2cc7e6f06cff 178 gp_udpSocket[i]->bind(ps_ear[0]->port);
bosko001 11:2cc7e6f06cff 179 gp_udpSocket[i]->set_timeout(1000);
bosko001 11:2cc7e6f06cff 180
bosko001 11:2cc7e6f06cff 181 gp_udpRxThread[i] = new Thread( &i, udpRx_fun );
bosko001 11:2cc7e6f06cff 182 printf( " dprxthread [%d] =%x\n\r", i, &gp_udpRxThread[i] );
bosko001 11:2cc7e6f06cff 183 }
bosko001 11:2cc7e6f06cff 184 // gp_udpRxThread[i]->start( callback( udpRx_fun, &i ) );
bosko001 11:2cc7e6f06cff 185 //
bosko001 9:893843262a1f 186 }
bosko001 9:893843262a1f 187
bosko001 9:893843262a1f 188 void udpRxThreadRestart( void )
bosko001 9:893843262a1f 189 {
bosko001 12:cbfc10c15437 190 printf("gasi udp\n\r"); fflush(stdout);
bosko001 9:893843262a1f 191 udpRxThreadCancel( );
bosko001 12:cbfc10c15437 192 printf("pali udp\n\r"); fflush(stdout);
bosko001 9:893843262a1f 193 udpRxThreadRiseUp( );
bosko001 9:893843262a1f 194 }
bosko001 9:893843262a1f 195