bosko lekovic
/
ETHtoCOM_10
nova verzija tcp+udp->serial com
TcpServerToFlash.cpp@2:45b351b4fc2a, 2020-04-05 (annotated)
- Committer:
- bosko001
- Date:
- Sun Apr 05 12:56:41 2020 +0000
- Revision:
- 2:45b351b4fc2a
- Child:
- 5:c9a908749d4c
n0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bosko001 | 2:45b351b4fc2a | 1 | #include "mbed.h" |
bosko001 | 2:45b351b4fc2a | 2 | #include "string.h" |
bosko001 | 2:45b351b4fc2a | 3 | |
bosko001 | 2:45b351b4fc2a | 4 | #include "EthernetSetup.h" |
bosko001 | 2:45b351b4fc2a | 5 | #include "dxml.h" |
bosko001 | 2:45b351b4fc2a | 6 | #include "TcpServerToFlash.h" |
bosko001 | 2:45b351b4fc2a | 7 | #include "doHTML.h" |
bosko001 | 2:45b351b4fc2a | 8 | |
bosko001 | 2:45b351b4fc2a | 9 | #define SOCKET_PORT 11000 |
bosko001 | 2:45b351b4fc2a | 10 | |
bosko001 | 2:45b351b4fc2a | 11 | Semaphore s(1); /* semafor da ne dozvoli vise of 1 server thred-a, upravlja delet-ovanjem threda*/ |
bosko001 | 2:45b351b4fc2a | 12 | |
bosko001 | 2:45b351b4fc2a | 13 | |
bosko001 | 2:45b351b4fc2a | 14 | extern EthernetInterface *gp_eth; |
bosko001 | 2:45b351b4fc2a | 15 | |
bosko001 | 2:45b351b4fc2a | 16 | void tcpServerRx_fun( void ); |
bosko001 | 2:45b351b4fc2a | 17 | Thread *gp_tcpServerThread = new Thread; |
bosko001 | 2:45b351b4fc2a | 18 | TCPSocket *gp_tcpSocket=NULL; |
bosko001 | 2:45b351b4fc2a | 19 | volatile Thread *gp_thr = NULL; |
bosko001 | 2:45b351b4fc2a | 20 | |
bosko001 | 2:45b351b4fc2a | 21 | |
bosko001 | 2:45b351b4fc2a | 22 | const char sendHTTPheader[]= "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE html>"; |
bosko001 | 2:45b351b4fc2a | 23 | |
bosko001 | 2:45b351b4fc2a | 24 | typedef void(*PF_PSTP)(struct SThreadParam *); |
bosko001 | 2:45b351b4fc2a | 25 | |
bosko001 | 2:45b351b4fc2a | 26 | struct SThreadParam |
bosko001 | 2:45b351b4fc2a | 27 | { |
bosko001 | 2:45b351b4fc2a | 28 | Thread *p_Thread; |
bosko001 | 2:45b351b4fc2a | 29 | void * p_param; |
bosko001 | 2:45b351b4fc2a | 30 | PF_PSTP pf_pstp; |
bosko001 | 2:45b351b4fc2a | 31 | }; |
bosko001 | 2:45b351b4fc2a | 32 | |
bosko001 | 2:45b351b4fc2a | 33 | /****************** inicijalizacija u mainu ************************/ |
bosko001 | 2:45b351b4fc2a | 34 | |
bosko001 | 2:45b351b4fc2a | 35 | void inic_TcpServerToFlash( void ) |
bosko001 | 2:45b351b4fc2a | 36 | { |
bosko001 | 2:45b351b4fc2a | 37 | gp_tcpServerThread->start(tcpServerRx_fun); |
bosko001 | 2:45b351b4fc2a | 38 | |
bosko001 | 2:45b351b4fc2a | 39 | } |
bosko001 | 2:45b351b4fc2a | 40 | |
bosko001 | 2:45b351b4fc2a | 41 | /***************** periodicna prozivka u mainu *************************/ |
bosko001 | 2:45b351b4fc2a | 42 | |
bosko001 | 2:45b351b4fc2a | 43 | void cleanTcpServerToFlashThread( void ) |
bosko001 | 2:45b351b4fc2a | 44 | { |
bosko001 | 2:45b351b4fc2a | 45 | if(gp_thr) |
bosko001 | 2:45b351b4fc2a | 46 | { |
bosko001 | 2:45b351b4fc2a | 47 | printf("brisanje thread-a %d\n\r", gp_thr); |
bosko001 | 2:45b351b4fc2a | 48 | delete gp_thr; gp_thr = NULL; |
bosko001 | 2:45b351b4fc2a | 49 | s.release(); |
bosko001 | 2:45b351b4fc2a | 50 | } |
bosko001 | 2:45b351b4fc2a | 51 | } |
bosko001 | 2:45b351b4fc2a | 52 | |
bosko001 | 2:45b351b4fc2a | 53 | |
bosko001 | 2:45b351b4fc2a | 54 | /************** Trajni TCPServer thread *********************************/ |
bosko001 | 2:45b351b4fc2a | 55 | |
bosko001 | 2:45b351b4fc2a | 56 | extern void rcpServerInstance_fun( SThreadParam *ps_thr ); |
bosko001 | 2:45b351b4fc2a | 57 | void tcpServerRx_fun( void ) |
bosko001 | 2:45b351b4fc2a | 58 | { |
bosko001 | 2:45b351b4fc2a | 59 | printf("Otvoren tcp server rx thread \n\r"); |
bosko001 | 2:45b351b4fc2a | 60 | |
bosko001 | 2:45b351b4fc2a | 61 | if( gp_tcpSocket == NULL ) |
bosko001 | 2:45b351b4fc2a | 62 | { |
bosko001 | 2:45b351b4fc2a | 63 | gp_tcpSocket = new TCPSocket; |
bosko001 | 2:45b351b4fc2a | 64 | gp_tcpSocket->open( gp_eth ); |
bosko001 | 2:45b351b4fc2a | 65 | if(gp_tcpSocket->bind(SOCKET_PORT) !=0) printf("greska bindovanja socketa\n\r"); |
bosko001 | 2:45b351b4fc2a | 66 | } |
bosko001 | 2:45b351b4fc2a | 67 | if(gp_tcpSocket->listen(1) != 0 ) printf("greska listenovanja na socketu\n\r");; |
bosko001 | 2:45b351b4fc2a | 68 | |
bosko001 | 2:45b351b4fc2a | 69 | TCPSocket *p_soc; |
bosko001 | 2:45b351b4fc2a | 70 | nsapi_error_t err=NULL; |
bosko001 | 2:45b351b4fc2a | 71 | |
bosko001 | 2:45b351b4fc2a | 72 | while( (p_soc = gp_tcpSocket->accept( &err)) || (err == NSAPI_ERROR_NO_SOCKET) ) |
bosko001 | 2:45b351b4fc2a | 73 | { |
bosko001 | 2:45b351b4fc2a | 74 | s.wait(); |
bosko001 | 2:45b351b4fc2a | 75 | |
bosko001 | 2:45b351b4fc2a | 76 | Thread *p_thr = new Thread; |
bosko001 | 2:45b351b4fc2a | 77 | struct SThreadParam *ps_thp = new struct SThreadParam; |
bosko001 | 2:45b351b4fc2a | 78 | ps_thp->p_param = (void*)p_soc; |
bosko001 | 2:45b351b4fc2a | 79 | ps_thp->p_Thread = p_thr; |
bosko001 | 2:45b351b4fc2a | 80 | ps_thp->pf_pstp = rcpServerInstance_fun; |
bosko001 | 2:45b351b4fc2a | 81 | |
bosko001 | 2:45b351b4fc2a | 82 | printf("TCPThread: NOVA KONEKCIJA! thread %d socket %d struct Sst %d\n\r", ps_thp->p_Thread, p_soc, ps_thp); |
bosko001 | 2:45b351b4fc2a | 83 | p_thr->start( callback( ps_thp->pf_pstp, ps_thp )); |
bosko001 | 2:45b351b4fc2a | 84 | |
bosko001 | 2:45b351b4fc2a | 85 | } |
bosko001 | 2:45b351b4fc2a | 86 | |
bosko001 | 2:45b351b4fc2a | 87 | if( gp_tcpSocket ) {delete gp_tcpSocket; gp_tcpSocket = NULL; } /* ima smisla ako se thread nasilno zatvara */ |
bosko001 | 2:45b351b4fc2a | 88 | } |
bosko001 | 2:45b351b4fc2a | 89 | |
bosko001 | 2:45b351b4fc2a | 90 | |
bosko001 | 2:45b351b4fc2a | 91 | |
bosko001 | 2:45b351b4fc2a | 92 | |
bosko001 | 2:45b351b4fc2a | 93 | /***************** privremeni TCPServer thread za klijenta ******************/ |
bosko001 | 2:45b351b4fc2a | 94 | |
bosko001 | 2:45b351b4fc2a | 95 | void rcpServerInstance_fun( struct SThreadParam *ps_thp ) |
bosko001 | 2:45b351b4fc2a | 96 | { |
bosko001 | 2:45b351b4fc2a | 97 | TCPSocket *p_soc = (TCPSocket *)ps_thp->p_param; |
bosko001 | 2:45b351b4fc2a | 98 | char inbuff[1500]; |
bosko001 | 2:45b351b4fc2a | 99 | |
bosko001 | 2:45b351b4fc2a | 100 | int n, nr, nt; |
bosko001 | 2:45b351b4fc2a | 101 | printf("thread: Otvoren socket %d\n\r",(int)p_soc); |
bosko001 | 2:45b351b4fc2a | 102 | |
bosko001 | 2:45b351b4fc2a | 103 | |
bosko001 | 2:45b351b4fc2a | 104 | while(true) |
bosko001 | 2:45b351b4fc2a | 105 | { |
bosko001 | 2:45b351b4fc2a | 106 | printf("\n\rcekam prijem od klijenta \n\r"); |
bosko001 | 2:45b351b4fc2a | 107 | SocketAddress sockAddr; |
bosko001 | 2:45b351b4fc2a | 108 | p_soc->getpeername( &sockAddr ); |
bosko001 | 2:45b351b4fc2a | 109 | |
bosko001 | 2:45b351b4fc2a | 110 | p_soc->set_timeout(10); |
bosko001 | 2:45b351b4fc2a | 111 | n=0; |
bosko001 | 2:45b351b4fc2a | 112 | do |
bosko001 | 2:45b351b4fc2a | 113 | { |
bosko001 | 2:45b351b4fc2a | 114 | nr = p_soc->recv( &(inbuff[n]), sizeof(inbuff)-n); |
bosko001 | 2:45b351b4fc2a | 115 | if(nr > 0) n += nr; |
bosko001 | 2:45b351b4fc2a | 116 | if(n>= sizeof(inbuff)) break; |
bosko001 | 2:45b351b4fc2a | 117 | } |
bosko001 | 2:45b351b4fc2a | 118 | while( (n==0) || (nr > 0) ); |
bosko001 | 2:45b351b4fc2a | 119 | if( (nr < 0) && (nr != NSAPI_ERROR_WOULD_BLOCK) ) break; |
bosko001 | 2:45b351b4fc2a | 120 | |
bosko001 | 2:45b351b4fc2a | 121 | if(n>0) |
bosko001 | 2:45b351b4fc2a | 122 | { |
bosko001 | 2:45b351b4fc2a | 123 | printf("PRIJEM: primljeno = %d bajtova \n\r",n); |
bosko001 | 2:45b351b4fc2a | 124 | // {int i; for(i=0;i<n;i++) printf("%c", inbuff[i] );} |
bosko001 | 2:45b351b4fc2a | 125 | // printf("\n\r"); |
bosko001 | 2:45b351b4fc2a | 126 | // fflush(stdout); |
bosko001 | 2:45b351b4fc2a | 127 | } |
bosko001 | 2:45b351b4fc2a | 128 | |
bosko001 | 2:45b351b4fc2a | 129 | p_soc->set_timeout(-1); |
bosko001 | 2:45b351b4fc2a | 130 | n = p_soc->send(sendHTTPheader, strlen(sendHTTPheader)); |
bosko001 | 2:45b351b4fc2a | 131 | printf("PREDAJA HTTPheader: posato bajtova = %d, %d \n\r", n, strlen(sendHTTPheader)); |
bosko001 | 2:45b351b4fc2a | 132 | |
bosko001 | 2:45b351b4fc2a | 133 | auto c = getHTML_free( ); |
bosko001 | 2:45b351b4fc2a | 134 | n = p_soc->send(c, strlen(c)); |
bosko001 | 2:45b351b4fc2a | 135 | free(c); |
bosko001 | 2:45b351b4fc2a | 136 | printf("PREDAJA dataBuffer: posato bajtova = %d, %d \n\r", n, strlen(c)); |
bosko001 | 2:45b351b4fc2a | 137 | |
bosko001 | 2:45b351b4fc2a | 138 | |
bosko001 | 2:45b351b4fc2a | 139 | p_soc->close(); |
bosko001 | 2:45b351b4fc2a | 140 | /* if(n<0)*/ {printf("BREAK posle predaje\n\r"); break;} |
bosko001 | 2:45b351b4fc2a | 141 | } |
bosko001 | 2:45b351b4fc2a | 142 | |
bosko001 | 2:45b351b4fc2a | 143 | gp_thr = ps_thp->p_Thread;// poslati pointer da se delete-uje |
bosko001 | 2:45b351b4fc2a | 144 | delete ps_thp; |
bosko001 | 2:45b351b4fc2a | 145 | |
bosko001 | 2:45b351b4fc2a | 146 | } |
bosko001 | 2:45b351b4fc2a | 147 |