nova proba

Committer:
bosko001
Date:
Sat Apr 18 22:32:13 2020 +0000
Revision:
7:7ea932eb31f6
Parent:
6:5cf50f29a81f
Child:
9:893843262a1f
potpuno ispravan html - flash rad

Who changed what in which revision?

UserRevisionLine numberNew 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 7:7ea932eb31f6 8 #include "dataFlash.h"
bosko001 2:45b351b4fc2a 9
bosko001 2:45b351b4fc2a 10 #define SOCKET_PORT 11000
bosko001 2:45b351b4fc2a 11
bosko001 2:45b351b4fc2a 12 Semaphore s(1); /* semafor da ne dozvoli vise of 1 server thred-a, upravlja delet-ovanjem threda*/
bosko001 2:45b351b4fc2a 13
bosko001 2:45b351b4fc2a 14
bosko001 2:45b351b4fc2a 15 extern EthernetInterface *gp_eth;
bosko001 2:45b351b4fc2a 16
bosko001 7:7ea932eb31f6 17 /* globalni pointer na tcpServer RX Thread za parametarizaciju */
bosko001 2:45b351b4fc2a 18 Thread *gp_tcpServerThread = new Thread;
bosko001 7:7ea932eb31f6 19 void tcpServerRx_fun( void );
bosko001 7:7ea932eb31f6 20
bosko001 7:7ea932eb31f6 21
bosko001 7:7ea932eb31f6 22 TCPSocket *gp_tcpSocket=NULL; /* socket za prijem zahteva za konekciju */
bosko001 7:7ea932eb31f6 23 volatile Thread *gp_thr = NULL; /* soket za komunikaciju sa klijentom */
bosko001 2:45b351b4fc2a 24
bosko001 2:45b351b4fc2a 25
bosko001 2:45b351b4fc2a 26 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 27
bosko001 2:45b351b4fc2a 28 typedef void(*PF_PSTP)(struct SThreadParam *);
bosko001 2:45b351b4fc2a 29
bosko001 7:7ea932eb31f6 30 // parametri thread-a za uspostavljenje konekcije
bosko001 2:45b351b4fc2a 31 struct SThreadParam
bosko001 2:45b351b4fc2a 32 {
bosko001 7:7ea932eb31f6 33 Thread *p_Thread; // sopstveni pointer
bosko001 7:7ea932eb31f6 34 void * p_param; // pointer za parametre thread-a, u ovom slucaju na socket koji je accept-van
bosko001 7:7ea932eb31f6 35 PF_PSTP pf_pstp; // pointer na funkciju koju thread callback-uje
bosko001 2:45b351b4fc2a 36 };
bosko001 2:45b351b4fc2a 37
bosko001 2:45b351b4fc2a 38 /****************** inicijalizacija u mainu ************************/
bosko001 2:45b351b4fc2a 39
bosko001 2:45b351b4fc2a 40 void inic_TcpServerToFlash( void )
bosko001 2:45b351b4fc2a 41 {
bosko001 2:45b351b4fc2a 42 gp_tcpServerThread->start(tcpServerRx_fun);
bosko001 2:45b351b4fc2a 43
bosko001 2:45b351b4fc2a 44 }
bosko001 2:45b351b4fc2a 45
bosko001 2:45b351b4fc2a 46 /***************** periodicna prozivka u mainu *************************/
bosko001 7:7ea932eb31f6 47 // zatvaranje thread-a nakon http razmene
bosko001 2:45b351b4fc2a 48
bosko001 2:45b351b4fc2a 49 void cleanTcpServerToFlashThread( void )
bosko001 2:45b351b4fc2a 50 {
bosko001 2:45b351b4fc2a 51 if(gp_thr)
bosko001 2:45b351b4fc2a 52 {
bosko001 2:45b351b4fc2a 53 printf("brisanje thread-a %d\n\r", gp_thr);
bosko001 2:45b351b4fc2a 54 delete gp_thr; gp_thr = NULL;
bosko001 2:45b351b4fc2a 55 s.release();
bosko001 2:45b351b4fc2a 56 }
bosko001 2:45b351b4fc2a 57 }
bosko001 2:45b351b4fc2a 58
bosko001 2:45b351b4fc2a 59
bosko001 2:45b351b4fc2a 60 /************** Trajni TCPServer thread *********************************/
bosko001 2:45b351b4fc2a 61
bosko001 2:45b351b4fc2a 62 extern void rcpServerInstance_fun( SThreadParam *ps_thr );
bosko001 2:45b351b4fc2a 63 void tcpServerRx_fun( void )
bosko001 2:45b351b4fc2a 64 {
bosko001 2:45b351b4fc2a 65 printf("Otvoren tcp server rx thread \n\r");
bosko001 2:45b351b4fc2a 66
bosko001 2:45b351b4fc2a 67 if( gp_tcpSocket == NULL )
bosko001 2:45b351b4fc2a 68 {
bosko001 2:45b351b4fc2a 69 gp_tcpSocket = new TCPSocket;
bosko001 2:45b351b4fc2a 70 gp_tcpSocket->open( gp_eth );
bosko001 2:45b351b4fc2a 71 if(gp_tcpSocket->bind(SOCKET_PORT) !=0) printf("greska bindovanja socketa\n\r");
bosko001 2:45b351b4fc2a 72 }
bosko001 2:45b351b4fc2a 73 if(gp_tcpSocket->listen(1) != 0 ) printf("greska listenovanja na socketu\n\r");;
bosko001 2:45b351b4fc2a 74
bosko001 2:45b351b4fc2a 75 TCPSocket *p_soc;
bosko001 2:45b351b4fc2a 76 nsapi_error_t err=NULL;
bosko001 2:45b351b4fc2a 77
bosko001 2:45b351b4fc2a 78 while( (p_soc = gp_tcpSocket->accept( &err)) || (err == NSAPI_ERROR_NO_SOCKET) )
bosko001 2:45b351b4fc2a 79 {
bosko001 7:7ea932eb31f6 80 s.wait(); // semafor sprecava novu konekciju dok se stara ne zavrsi
bosko001 2:45b351b4fc2a 81
bosko001 2:45b351b4fc2a 82 Thread *p_thr = new Thread;
bosko001 7:7ea932eb31f6 83 struct SThreadParam *ps_thp = new struct SThreadParam; // alokacija ps_thp
bosko001 2:45b351b4fc2a 84 ps_thp->p_param = (void*)p_soc;
bosko001 2:45b351b4fc2a 85 ps_thp->p_Thread = p_thr;
bosko001 2:45b351b4fc2a 86 ps_thp->pf_pstp = rcpServerInstance_fun;
bosko001 2:45b351b4fc2a 87
bosko001 2:45b351b4fc2a 88 printf("TCPThread: NOVA KONEKCIJA! thread %d socket %d struct Sst %d\n\r", ps_thp->p_Thread, p_soc, ps_thp);
bosko001 2:45b351b4fc2a 89 p_thr->start( callback( ps_thp->pf_pstp, ps_thp ));
bosko001 2:45b351b4fc2a 90
bosko001 2:45b351b4fc2a 91 }
bosko001 2:45b351b4fc2a 92
bosko001 2:45b351b4fc2a 93 if( gp_tcpSocket ) {delete gp_tcpSocket; gp_tcpSocket = NULL; } /* ima smisla ako se thread nasilno zatvara */
bosko001 2:45b351b4fc2a 94 }
bosko001 2:45b351b4fc2a 95
bosko001 2:45b351b4fc2a 96
bosko001 2:45b351b4fc2a 97
bosko001 2:45b351b4fc2a 98
bosko001 2:45b351b4fc2a 99 /***************** privremeni TCPServer thread za klijenta ******************/
bosko001 7:7ea932eb31f6 100 // thread je jednokratan - primi http telegram i odgovori - ako je GET ili POST upit
bosko001 2:45b351b4fc2a 101
bosko001 2:45b351b4fc2a 102 void rcpServerInstance_fun( struct SThreadParam *ps_thp )
bosko001 2:45b351b4fc2a 103 {
bosko001 2:45b351b4fc2a 104 TCPSocket *p_soc = (TCPSocket *)ps_thp->p_param;
bosko001 2:45b351b4fc2a 105 char inbuff[1500];
bosko001 2:45b351b4fc2a 106
bosko001 2:45b351b4fc2a 107 int n, nr, nt;
bosko001 2:45b351b4fc2a 108 printf("thread: Otvoren socket %d\n\r",(int)p_soc);
bosko001 2:45b351b4fc2a 109
bosko001 2:45b351b4fc2a 110 while(true)
bosko001 2:45b351b4fc2a 111 {
bosko001 7:7ea932eb31f6 112 extern C_HTMLparse *gpc_html;
bosko001 7:7ea932eb31f6 113
bosko001 2:45b351b4fc2a 114 printf("\n\rcekam prijem od klijenta \n\r");
bosko001 2:45b351b4fc2a 115 SocketAddress sockAddr;
bosko001 2:45b351b4fc2a 116 p_soc->getpeername( &sockAddr );
bosko001 2:45b351b4fc2a 117
bosko001 7:7ea932eb31f6 118 p_soc->set_timeout(10); // non-blocking pruzimanje bajtova na 10ms
bosko001 2:45b351b4fc2a 119 n=0;
bosko001 2:45b351b4fc2a 120 do
bosko001 2:45b351b4fc2a 121 {
bosko001 2:45b351b4fc2a 122 nr = p_soc->recv( &(inbuff[n]), sizeof(inbuff)-n);
bosko001 2:45b351b4fc2a 123 if(nr > 0) n += nr;
bosko001 2:45b351b4fc2a 124 if(n>= sizeof(inbuff)) break;
bosko001 2:45b351b4fc2a 125 }
bosko001 2:45b351b4fc2a 126 while( (n==0) || (nr > 0) );
bosko001 2:45b351b4fc2a 127 if( (nr < 0) && (nr != NSAPI_ERROR_WOULD_BLOCK) ) break;
bosko001 2:45b351b4fc2a 128
bosko001 2:45b351b4fc2a 129 if(n>0)
bosko001 2:45b351b4fc2a 130 {
bosko001 7:7ea932eb31f6 131
bosko001 7:7ea932eb31f6 132 //u inbuff-u je http paket
bosko001 5:c9a908749d4c 133 printf("PRIJEM tcpSOCKET: primljeno = %d bajtova \n\r",n);
bosko001 7:7ea932eb31f6 134
bosko001 7:7ea932eb31f6 135 // { int i; for(i=0;i<n;i++) printf("%c", inbuff[i] );}
bosko001 7:7ea932eb31f6 136 // printf("KRAJ PRIJEMA\n\r");
bosko001 7:7ea932eb31f6 137 // fflush(stdout);
bosko001 7:7ea932eb31f6 138 inbuff[n]=0;
bosko001 7:7ea932eb31f6 139 //raspakivanje sadrzaja, a ako je POST preuzimanje parametara
bosko001 7:7ea932eb31f6 140 char * str = gpc_html->doHTTP( inbuff);
bosko001 7:7ea932eb31f6 141 if( !strcmp( str, "GET_HTML") || !strcmp( str, "POST") )
bosko001 7:7ea932eb31f6 142 {
bosko001 7:7ea932eb31f6 143
bosko001 7:7ea932eb31f6 144 // putHTMLintoFLASH( gpc_html );
bosko001 7:7ea932eb31f6 145 //putFLASHintoHTML( gpc_html );
bosko001 7:7ea932eb31f6 146
bosko001 7:7ea932eb31f6 147 if(!strcmp( str, "POST") )
bosko001 7:7ea932eb31f6 148 {
bosko001 7:7ea932eb31f6 149 gpc_html->htmlPOST_fun( ); // setovanje parametara u HTML format
bosko001 7:7ea932eb31f6 150
bosko001 7:7ea932eb31f6 151 putHTMLintoFLASH( gpc_html ); // upis parametara iz HTMLa u FLASH
bosko001 7:7ea932eb31f6 152 }
bosko001 2:45b351b4fc2a 153
bosko001 7:7ea932eb31f6 154 // ODGOVOR - slanje odgovora klijentu
bosko001 7:7ea932eb31f6 155
bosko001 7:7ea932eb31f6 156 p_soc->set_timeout(-1); // blocking
bosko001 7:7ea932eb31f6 157 // slanje HTTP headera
bosko001 7:7ea932eb31f6 158 n = p_soc->send(sendHTTPheader, strlen(sendHTTPheader));
bosko001 7:7ea932eb31f6 159 printf("PREDAJA HTTPheader: posato bajtova = %d, %d \n\r", n, strlen(sendHTTPheader));
bosko001 7:7ea932eb31f6 160
bosko001 7:7ea932eb31f6 161 // putFLASHintoHTML( gpc_html ); // preuzimanje podataka iz flasha u html
bosko001 7:7ea932eb31f6 162 auto c = gpc_html->getHTML_free( ); // konvertovanje HTMLa u niz
bosko001 7:7ea932eb31f6 163 n = p_soc->send(c, strlen(c)); // slanje HTML-a
bosko001 7:7ea932eb31f6 164
bosko001 7:7ea932eb31f6 165 // test - slanje HTMLa na serial com
bosko001 7:7ea932eb31f6 166 // extern UARTSerial *gp_scom;
bosko001 7:7ea932eb31f6 167 // extern void sendScom( struct UARTSerial *p_scom, char *buffer, int val);
bosko001 7:7ea932eb31f6 168 // sendScom( gp_scom, c, strlen(c));
bosko001 7:7ea932eb31f6 169
bosko001 7:7ea932eb31f6 170 free(c); // oslobadjanje memorije HTML niza
bosko001 7:7ea932eb31f6 171
bosko001 7:7ea932eb31f6 172
bosko001 7:7ea932eb31f6 173 printf("PREDAJA dataBuffer: posato bajtova = %d, %d \n\r", n, strlen(c));
bosko001 7:7ea932eb31f6 174
bosko001 7:7ea932eb31f6 175 }
bosko001 7:7ea932eb31f6 176 }
bosko001 2:45b351b4fc2a 177
bosko001 7:7ea932eb31f6 178 p_soc->close(); // zatvaranje socketa
bosko001 2:45b351b4fc2a 179 /* if(n<0)*/ {printf("BREAK posle predaje\n\r"); break;}
bosko001 2:45b351b4fc2a 180 }
bosko001 7:7ea932eb31f6 181 // zatvaranje thread-a
bosko001 2:45b351b4fc2a 182 gp_thr = ps_thp->p_Thread;// poslati pointer da se delete-uje
bosko001 7:7ea932eb31f6 183
bosko001 7:7ea932eb31f6 184 delete ps_thp; // relokacija ps_thp
bosko001 2:45b351b4fc2a 185
bosko001 2:45b351b4fc2a 186 }
bosko001 2:45b351b4fc2a 187