bosko lekovic
/
ETHtoCOM_10
nova verzija tcp+udp->serial com
Udp.cpp@9:893843262a1f, 2020-04-22 (annotated)
- Committer:
- bosko001
- Date:
- Wed Apr 22 13:21:50 2020 +0000
- Revision:
- 9:893843262a1f
- Child:
- 10:15f7fea18a2f
novo 22.04.2020.
Who changed what in which revision?
User | Revision | Line number | New 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 | 9:893843262a1f | 6 | struct S_EthernetAddress_Remote |
bosko001 | 9:893843262a1f | 7 | /*{ |
bosko001 | 9:893843262a1f | 8 | char ip[16]; |
bosko001 | 9:893843262a1f | 9 | int port; |
bosko001 | 9:893843262a1f | 10 | }*/ s_EthAddRemote = {"192.168.1.14", 13000 }, *ps_ear = &s_EthAddRemote; |
bosko001 | 9:893843262a1f | 11 | |
bosko001 | 9:893843262a1f | 12 | char lokaUdpPort[6] = "13000", localUdpPortFlash[6] = "65535"; |
bosko001 | 9:893843262a1f | 13 | |
bosko001 | 9:893843262a1f | 14 | /* globalni pointeri*/ |
bosko001 | 9:893843262a1f | 15 | |
bosko001 | 9:893843262a1f | 16 | |
bosko001 | 9:893843262a1f | 17 | extern EthernetInterface *gp_eth; |
bosko001 | 9:893843262a1f | 18 | |
bosko001 | 9:893843262a1f | 19 | UDPSocket *gp_udpSocket = NULL; |
bosko001 | 9:893843262a1f | 20 | Thread *gp_udpRxThread = NULL; |
bosko001 | 9:893843262a1f | 21 | |
bosko001 | 9:893843262a1f | 22 | /************* inicijalizacija parametara **********************/ |
bosko001 | 9:893843262a1f | 23 | |
bosko001 | 9:893843262a1f | 24 | extern C_HTMLparse * gpc_html; |
bosko001 | 9:893843262a1f | 25 | void init_udp_param( void ) |
bosko001 | 9:893843262a1f | 26 | { |
bosko001 | 9:893843262a1f | 27 | strcpy( ps_ear->ip, gpc_html->get_UdpIP(1) ); |
bosko001 | 9:893843262a1f | 28 | ps_ear->port = atoi( gpc_html->get_UdpPort(1) ); |
bosko001 | 9:893843262a1f | 29 | |
bosko001 | 9:893843262a1f | 30 | printf(" UDP IP adresa: %s %d \n\r", ps_ear->ip, ps_ear->port ); |
bosko001 | 9:893843262a1f | 31 | } |
bosko001 | 9:893843262a1f | 32 | |
bosko001 | 9:893843262a1f | 33 | |
bosko001 | 9:893843262a1f | 34 | |
bosko001 | 9:893843262a1f | 35 | |
bosko001 | 9:893843262a1f | 36 | |
bosko001 | 9:893843262a1f | 37 | |
bosko001 | 9:893843262a1f | 38 | /***************** Predaja na UDP ****************************/ |
bosko001 | 9:893843262a1f | 39 | |
bosko001 | 9:893843262a1f | 40 | void sendUdp( struct S_EthernetAddress_Remote *ps_ear, char *buffer, int val ) |
bosko001 | 9:893843262a1f | 41 | { |
bosko001 | 9:893843262a1f | 42 | if( gp_udpSocket ) |
bosko001 | 9:893843262a1f | 43 | { |
bosko001 | 9:893843262a1f | 44 | printf(" UDPpaket ip=%s port=%d val=%d\n\r", ps_ear->ip, ps_ear->port, val); |
bosko001 | 9:893843262a1f | 45 | gp_udpSocket->sendto(ps_ear->ip, ps_ear->port, (const uint8_t*)buffer, val); |
bosko001 | 9:893843262a1f | 46 | } |
bosko001 | 9:893843262a1f | 47 | else printf("UDPsocket == NULL\n\r"); |
bosko001 | 9:893843262a1f | 48 | } |
bosko001 | 9:893843262a1f | 49 | |
bosko001 | 9:893843262a1f | 50 | void sendToUdp( char *buffer, int val ) |
bosko001 | 9:893843262a1f | 51 | { |
bosko001 | 9:893843262a1f | 52 | sendUdp( ps_ear, buffer, val ); |
bosko001 | 9:893843262a1f | 53 | } |
bosko001 | 9:893843262a1f | 54 | |
bosko001 | 9:893843262a1f | 55 | |
bosko001 | 9:893843262a1f | 56 | |
bosko001 | 9:893843262a1f | 57 | /*************** Prijem na UDP socket i predaja na Serial com ***************/ |
bosko001 | 9:893843262a1f | 58 | |
bosko001 | 9:893843262a1f | 59 | void udpRx_fun( void ) |
bosko001 | 9:893843262a1f | 60 | { |
bosko001 | 9:893843262a1f | 61 | //int *p = (int*)pp; |
bosko001 | 9:893843262a1f | 62 | nsapi_error_t err; |
bosko001 | 9:893843262a1f | 63 | |
bosko001 | 9:893843262a1f | 64 | char inbuff[1500]; |
bosko001 | 9:893843262a1f | 65 | |
bosko001 | 9:893843262a1f | 66 | // if(gp_udpSocket == NULL) |
bosko001 | 9:893843262a1f | 67 | { |
bosko001 | 9:893843262a1f | 68 | gp_udpSocket = new UDPSocket; |
bosko001 | 9:893843262a1f | 69 | printf("___novi UDP socket %x\n\r", gp_udpSocket); |
bosko001 | 9:893843262a1f | 70 | init_udp_param( ); |
bosko001 | 9:893843262a1f | 71 | |
bosko001 | 9:893843262a1f | 72 | if( (err = gp_udpSocket->open( gp_eth )) != NSAPI_ERROR_OK ) |
bosko001 | 9:893843262a1f | 73 | { printf(" greska otvaranja soketa %d \n\r", err); } |
bosko001 | 9:893843262a1f | 74 | gp_udpSocket->bind(ps_ear->port); |
bosko001 | 9:893843262a1f | 75 | gp_udpSocket->set_timeout(1000); |
bosko001 | 9:893843262a1f | 76 | } |
bosko001 | 9:893843262a1f | 77 | printf("UDPrx inicijalizovan \n\r"); |
bosko001 | 9:893843262a1f | 78 | |
bosko001 | 9:893843262a1f | 79 | |
bosko001 | 9:893843262a1f | 80 | signed int n=0; |
bosko001 | 9:893843262a1f | 81 | while( !(ThisThread::flags_get( ) & 1) ) |
bosko001 | 9:893843262a1f | 82 | { |
bosko001 | 9:893843262a1f | 83 | SocketAddress sockAddr; |
bosko001 | 9:893843262a1f | 84 | if( (n>=0) && gp_udpSocket ) |
bosko001 | 9:893843262a1f | 85 | { |
bosko001 | 9:893843262a1f | 86 | int n = gp_udpSocket->recvfrom(&sockAddr, &inbuff, sizeof(inbuff)); |
bosko001 | 9:893843262a1f | 87 | |
bosko001 | 9:893843262a1f | 88 | if(n>0) { extern void sendToScom( char *buffer, int val ); sendToScom( inbuff, n); } |
bosko001 | 9:893843262a1f | 89 | else if(n==-3001) n = 0; |
bosko001 | 9:893843262a1f | 90 | else break; |
bosko001 | 9:893843262a1f | 91 | } |
bosko001 | 9:893843262a1f | 92 | //else wait(0.1); |
bosko001 | 9:893843262a1f | 93 | } |
bosko001 | 9:893843262a1f | 94 | |
bosko001 | 9:893843262a1f | 95 | if(gp_udpSocket) { delete gp_udpSocket; gp_udpSocket = NULL;} |
bosko001 | 9:893843262a1f | 96 | printf("udpSocket obrisan\n\r"); |
bosko001 | 9:893843262a1f | 97 | fflush(stdout); |
bosko001 | 9:893843262a1f | 98 | } |
bosko001 | 9:893843262a1f | 99 | |
bosko001 | 9:893843262a1f | 100 | |
bosko001 | 9:893843262a1f | 101 | void udpRxThreadCancel( void ) |
bosko001 | 9:893843262a1f | 102 | { |
bosko001 | 9:893843262a1f | 103 | /* Gasenje UDP threada */ |
bosko001 | 9:893843262a1f | 104 | if( gp_udpRxThread->get_state( ) && gp_udpRxThread->get_state( )<16 ) |
bosko001 | 9:893843262a1f | 105 | gp_udpRxThread->flags_set( 1 ); |
bosko001 | 9:893843262a1f | 106 | gp_udpRxThread->join( ); |
bosko001 | 9:893843262a1f | 107 | if(gp_udpRxThread) {delete gp_udpRxThread; gp_udpRxThread = NULL;} |
bosko001 | 9:893843262a1f | 108 | printf("udp thread zavrsio\n\r"); |
bosko001 | 9:893843262a1f | 109 | |
bosko001 | 9:893843262a1f | 110 | } |
bosko001 | 9:893843262a1f | 111 | |
bosko001 | 9:893843262a1f | 112 | void udpRxThreadRiseUp( void ) |
bosko001 | 9:893843262a1f | 113 | { |
bosko001 | 9:893843262a1f | 114 | /* paljenje UDP thread-a */ |
bosko001 | 9:893843262a1f | 115 | gp_udpRxThread = new Thread( udpRx_fun ); |
bosko001 | 9:893843262a1f | 116 | } |
bosko001 | 9:893843262a1f | 117 | |
bosko001 | 9:893843262a1f | 118 | void udpRxThreadRestart( void ) |
bosko001 | 9:893843262a1f | 119 | { |
bosko001 | 9:893843262a1f | 120 | udpRxThreadCancel( ); |
bosko001 | 9:893843262a1f | 121 | udpRxThreadRiseUp( ); |
bosko001 | 9:893843262a1f | 122 | } |
bosko001 | 9:893843262a1f | 123 |