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