bosko lekovic
/
EthToCom_11
nova proba
Diff: TcpServerToFlash.cpp
- Revision:
- 18:5ee53205451a
- Parent:
- 17:74c41e3f10b8
- Child:
- 19:7bdb1a10043f
diff -r 74c41e3f10b8 -r 5ee53205451a TcpServerToFlash.cpp --- a/TcpServerToFlash.cpp Wed May 06 00:42:53 2020 +0200 +++ b/TcpServerToFlash.cpp Sun May 17 17:16:03 2020 +0200 @@ -1,5 +1,5 @@ #include "mbed.h" -#include "string.h" +//#include "string.h" #include "EthernetSetup.h" #include "dxml.h" @@ -7,20 +7,28 @@ #include "doHTML.h" #include "dataFlash.h" +#include "UdpClients.h" +#include "TcpClients.h" +#include "SerialCom.h" + + #define SOCKET_PORT 80 -Semaphore s(1); /* semafor da ne dozvoli vise of 1 server thred-a, upravlja delet-ovanjem threda*/ +//Semaphore s(1); /* semafor da ne dozvoli vise of 1 server thred-a, upravlja delet-ovanjem threda*/ +//Mutex m; + +volatile int tcpServerBlock=0; // Blokira serijsku komunikaciju tokom upisa u data flash extern EthernetInterface *gp_eth; /* globalni pointer na tcpServer RX Thread za parametarizaciju */ -Thread *gp_tcpServerThread = new Thread; +static Thread *gp_tcpServerThread = NULL; //new Thread; void tcpServerRx_fun( void ); -TCPSocket *gp_tcpSocket=NULL; /* socket za prijem zahteva za konekciju */ -volatile Thread *gp_thr = NULL; /* soket za komunikaciju sa klijentom */ +static TCPSocket *gp_tcpSocket=NULL; /* socket za prijem zahteva za konekciju */ +static Thread *gp_thr = NULL; /* pointer gde se smesta thread za brisanje */ const char sendHTTPheader[]= "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE html>"; @@ -36,9 +44,15 @@ }; /****************** inicijalizacija u mainu ************************/ +char* html_string; void inic_TcpServerToFlash( void ) { +// extern C_HTMLparse *gpc_html; +// html_string = gpc_html->getHTML_free( ); // konvertovanje HTMLa u niz + + if( gp_tcpServerThread ) delete gp_tcpServerThread; + gp_tcpServerThread = new Thread(); gp_tcpServerThread->start(tcpServerRx_fun); } @@ -50,47 +64,63 @@ { if(gp_thr) { - printf("brisanje thread-a %d\n\r", gp_thr); - delete gp_thr; gp_thr = NULL; - s.release(); + printf("Brisanje thread-a %d\n\r", gp_thr); fflush(stdout); + gp_thr->join(); + + delete gp_thr; + gp_thr = NULL; + printf("Obrisan thread %d\n\r", gp_thr); +// s.release(); } } +void _semafor_release(void ) {/* s.release();*/} +/************** Trajni TCPServer thread *********************************/ -/************** Trajni TCPServer thread *********************************/ +volatile int bws=0; //block web server extern void rcpServerInstance_fun( SThreadParam *ps_thr ); void tcpServerRx_fun( void ) { - printf("Otvoren tcp server rx thread \n\r"); + while( true ) + { + printf("____Otvoren TCP server rx thread____ \n\r"); + if( gp_tcpSocket ) {delete gp_tcpSocket; gp_tcpSocket = NULL; } /* ima smisla ako se thread nasilno zatvara */ - if( gp_tcpSocket == NULL ) - { gp_tcpSocket = new TCPSocket; - gp_tcpSocket->open( gp_eth ); - if(gp_tcpSocket->bind(SOCKET_PORT) !=0) printf("greska bindovanja socketa\n\r"); - } - if(gp_tcpSocket->listen(1) != 0 ) printf("greska listenovanja na socketu\n\r");; - - TCPSocket *p_soc; - nsapi_error_t err=NULL; - - while( (p_soc = gp_tcpSocket->accept( &err)) || (err == NSAPI_ERROR_NO_SOCKET) ) + if(gp_tcpSocket->open( gp_eth ) != 0) break; + gp_tcpSocket->bind(SOCKET_PORT); + + if(gp_tcpSocket->listen(2) != 0 ) + { printf("greska listenovanja na socketu\n\r"); break;} + + TCPSocket *p_soc; + nsapi_error_t err=NULL; + int nos = 0; + + while( (p_soc = gp_tcpSocket->accept( &err)) ) { - s.wait(); // semafor sprecava novu konekciju dok se stara ne zavrsi + printf("\n\rTCP WEB SERVER: zahtev za konekciju - socket = %d err = %d\n\r", p_soc, err);fflush(stdout); + // s.wait(); // semafor sprecava novu konekciju dok se stara ne zavrsi + printf("TCP WEB SERVER: Broj %d\n\r", ++nos); fflush(stdout); + +bws = 1; - Thread *p_thr = new Thread; - struct SThreadParam *ps_thp = new struct SThreadParam; // alokacija ps_thp - ps_thp->p_param = (void*)p_soc; - ps_thp->p_Thread = p_thr; - ps_thp->pf_pstp = rcpServerInstance_fun; - - printf("TCPThread: NOVA KONEKCIJA! thread %d socket %d struct Sst %d\n\r", ps_thp->p_Thread, p_soc, ps_thp); - p_thr->start( callback( ps_thp->pf_pstp, ps_thp )); + Thread *p_thr = new Thread( ); + struct SThreadParam *ps_thp = new struct SThreadParam; // alokacija ps_thp + ps_thp->p_param = (void*)p_soc; + ps_thp->p_Thread = p_thr; + ps_thp->pf_pstp = rcpServerInstance_fun; + + printf("TCPThread: NOVA KONEKCIJA! thread %d socket %d struct Sst %d\n\r", ps_thp->p_Thread, p_soc, ps_thp);fflush(stdout); + p_thr->start( callback( ps_thp->pf_pstp, ps_thp )); + +while(bws); } + printf("TCP WEB SERVER: Ispad iz accept while!!! socket = %d err = %d\n\r", p_soc, err);fflush(stdout); + } - if( gp_tcpSocket ) {delete gp_tcpSocket; gp_tcpSocket = NULL; } /* ima smisla ako se thread nasilno zatvara */ } @@ -102,101 +132,138 @@ void rcpServerInstance_fun( struct SThreadParam *ps_thp ) { TCPSocket *p_soc = (TCPSocket *)ps_thp->p_param; + +// m.lock(); +// tcpServerBlock++; + char inbuff[1500]; +int restart = 0; int n, nr, nt; - printf("thread: Otvoren socket %d\n\r",(int)p_soc); + printf("tcpServThread: Otvoren socket %d\n\r",(int)p_soc); while(true) { extern C_HTMLparse *gpc_html; - printf("\n\rcekam prijem od klijenta \n\r"); + printf("tcpServThread: cekam prijem od klijenta \n\r"); SocketAddress sockAddr; p_soc->getpeername( &sockAddr ); p_soc->set_timeout(10); // non-blocking pruzimanje bajtova na 10ms n=0; + int k=10, t=0; do { - nr = p_soc->recv( &(inbuff[n]), sizeof(inbuff)-n); + t++; + nr = p_soc->recv( &(inbuff[n]), sizeof(inbuff)-n); // razmotriti obradu nr=0 !!! if(nr > 0) n += nr; + else k--; if(n>= sizeof(inbuff)) break; } - while( (n==0) || (nr > 0) ); + while( ((n==0) || (nr > 0) ) && k ); + printf( "tcpServThread: broj prolaza t=%d k=%d n=%d nr=%d\n\r ", t, k, n, nr); if( (nr < 0) && (nr != NSAPI_ERROR_WOULD_BLOCK) ) break; - + if( !k ) break; + if(n>0) { //u inbuff-u je http paket - printf("PRIJEM tcpSOCKET: primljeno = %d bajtova \n\r",n); + printf("tcpServThread: primljeno = %d bajtova \n\r",n); fflush(stdout); inbuff[n]=0; //raspakivanje sadrzaja, a ako je POST preuzimanje parametara - char * str = gpc_html->doHTTP( inbuff); - if( !strcmp( str, "GET_HTML") || !strcmp( str, "POST") ) + char ** strniz = gpc_html->doHTTP_freereturn( inbuff); + char *type = strniz[0]; + char *description = strniz[1]; + if( (!strcmp( type, "GET") && !strcmp( description, "HTTP")) || !strcmp( type, "POST") ) { + + // putHTMLintoFLASH( gpc_html ); //putFLASHintoHTML( gpc_html ); - if(!strcmp( str, "POST") ) + if(!strcmp( type, "POST") ) { - gpc_html->htmlPOST_fun( ); // setovanje parametara u HTML format - - putHTMLintoFLASH( gpc_html ); // upis parametara iz HTMLa u FLASH - extern void ethRestart( void ); - ethRestart( ); - extern void scomRxThreadRestart( void ); - scomRxThreadRestart( ); - extern void udpRxThreadsRestart( void ); - udpRxThreadsRestart( ); - } + tcpServerBlock++; + printf("tcpServThread: ___stop komunikacije\n\r"); + +// scomRxThreadCancel(); + tcpRxThreadsCancel(); + printf(" STACK: size=%d free=%d used=%d max used=%d\n\r", ps_thp->p_Thread->stack_size(),ps_thp->p_Thread->free_stack(),ps_thp->p_Thread->used_stack(),ps_thp->p_Thread->max_stack()); + // udpRxThreadsCancel(); + // wait(0.1); + gpc_html->htmlPOST_fun( ); // setovanje parametara u HTML format + putHTMLintoFLASH( gpc_html ); // upis parametara iz HTMLa u FLASH + // vratiti podatke iz Flasha u HTML !!!!!! +// ethRestart( ); + // udpRxThreadsRiseUp( ); + // scomRxThreadRiseUp( ); + tcpRxThreadsRiseUp( ); + + restart = 1; + } // ODGOVOR - slanje odgovora klijentu p_soc->set_timeout(-1); // blocking // slanje HTTP headera +// if(strcmp( type, "POST") ) + { n = p_soc->send(sendHTTPheader, strlen(sendHTTPheader)); - printf("PREDAJA HTTPheader: posato bajtova = %d, %d \n\r", n, strlen(sendHTTPheader)); + printf("tcpServThread: predaja HTTPheader: posato bajtova = %d, %d \n\r", n, strlen(sendHTTPheader)); // putFLASHintoHTML( gpc_html ); // preuzimanje podataka iz flasha u html - auto c = gpc_html->getHTML_free( ); // konvertovanje HTMLa u niz - n = p_soc->send(c, strlen(c)); // slanje HTML-a - - // test - slanje HTMLa na serial com - // extern UARTSerial *gp_scom; -// extern void sendScom( struct UARTSerial *p_scom, char *buffer, int val); -// sendScom( gp_scom, c, strlen(c)); - - free(c); // oslobadjanje memorije HTML niza - - printf("PREDAJA dataBuffer: posato bajtova = %d, %d \n\r", n, strlen(c)); - + auto html_string = gpc_html->getHTML_free( ); // konvertovanje HTMLa u niz + n = p_soc->send(html_string, strlen(html_string)); // slanje HTML-a + printf("tcpServThread: PREDAJA dataBuffer: posato bajtova = %d, %d \n\r", n, strlen(html_string)); + free(html_string); // oslobadjanje memorije HTML niza + } } + free(type); + free( description); + free(strniz); } + //!!! p_soc->close(); // zatvaranje socketa // if(n<0) - {printf("BREAK posle predaje\n\r"); break;} + {printf("tcpServThread: BREAK posle predaje\n\r"); break;} } // end while - {printf("zatvaranje socketa\n\r"); fflush(stdout);} - - p_soc->close(); // zatvaranje socketa -{printf("zatvaranje thread-a\n\r"); fflush(stdout);} - - // zatvaranje thread-a - gp_thr = ps_thp->p_Thread;// poslati pointer da se delete-uje + {printf("tcpServThread: zatvaranje socketa\n\r"); fflush(stdout);} + + p_soc->close(); // zatvaranje socketa + + printf("tcpServThread: restart = %d\n\r",restart); + if( restart ) + { + printf("tcpServThread:___restart komunikacije\n\r"); + // ethRestart( ); + // // udpRxThreadsRiseUp( ); + // tcpRxThreadsRiseUp( ); + // // scomRxThreadRestart( ); + restart = 0; + } + + printf(" STACK: size=%d free=%d used=%d max used=%d\n\r", ps_thp->p_Thread->stack_size(),ps_thp->p_Thread->free_stack(),ps_thp->p_Thread->used_stack(),ps_thp->p_Thread->max_stack()); + + {printf("tcpServThread: zatvaranje thread-a\n\r"); fflush(stdout);} + // zatvaranje thread-a + gp_thr = ps_thp->p_Thread;// poslati pointer da se delete-uje - {printf("brisanje parametara\n\r"); fflush(stdout);} - - delete ps_thp; // relokacija ps_thp - + {printf("tcpServThread: brisanje parametara\n\r"); fflush(stdout);} + delete ps_thp; // relokacija ps_thp + +bws = 0; +//m.unlock(); + if( tcpServerBlock) {tcpServerBlock--;printf("tcpServThread: tcpServerBlock=%d\n\r", tcpServerBlock);} + }