nova verzija tcp+udp->serial com

Committer:
bosko001
Date:
Sun May 03 01:09:59 2020 +0200
Revision:
14:ff1df10bcd65
Parent:
12:cbfc10c15437
Child:
15:4535e7ed4585
najnovija biblioteka

Who changed what in which revision?

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