nova verzija tcp+udp->serial com

Committer:
bosko001
Date:
Fri May 01 10:43:05 2020 +0000
Revision:
11:2cc7e6f06cff
Parent:
10:15f7fea18a2f
Child:
17:74c41e3f10b8
funkcionalna verzija. Web parametarizacija radi na TCP socketu samo ako je NO_UDP = 1 u Udp.h fajlu, tj, samo prvi udp kanal je funkcionalan. U slucaju da nam treba vise kanala (do 4) stavljamo NO_UDP 2,3, ili 4 ali programiranje na TCP tada ne radi

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 10:15f7fea18a2f 10 #define SOCKET_PORT 80
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 11:2cc7e6f06cff 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 11:2cc7e6f06cff 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 11:2cc7e6f06cff 134 fflush(stdout);
bosko001 7:7ea932eb31f6 135
bosko001 7:7ea932eb31f6 136 inbuff[n]=0;
bosko001 7:7ea932eb31f6 137 //raspakivanje sadrzaja, a ako je POST preuzimanje parametara
bosko001 7:7ea932eb31f6 138 char * str = gpc_html->doHTTP( inbuff);
bosko001 7:7ea932eb31f6 139 if( !strcmp( str, "GET_HTML") || !strcmp( str, "POST") )
bosko001 7:7ea932eb31f6 140 {
bosko001 7:7ea932eb31f6 141
bosko001 7:7ea932eb31f6 142 // putHTMLintoFLASH( gpc_html );
bosko001 7:7ea932eb31f6 143 //putFLASHintoHTML( gpc_html );
bosko001 7:7ea932eb31f6 144
bosko001 7:7ea932eb31f6 145 if(!strcmp( str, "POST") )
bosko001 7:7ea932eb31f6 146 {
bosko001 7:7ea932eb31f6 147 gpc_html->htmlPOST_fun( ); // setovanje parametara u HTML format
bosko001 7:7ea932eb31f6 148
bosko001 7:7ea932eb31f6 149 putHTMLintoFLASH( gpc_html ); // upis parametara iz HTMLa u FLASH
bosko001 9:893843262a1f 150 extern void ethRestart( void );
bosko001 9:893843262a1f 151 ethRestart( );
bosko001 9:893843262a1f 152 extern void scomRxThreadRestart( void );
bosko001 9:893843262a1f 153 scomRxThreadRestart( );
bosko001 9:893843262a1f 154 extern void udpRxThreadRestart( void );
bosko001 9:893843262a1f 155 udpRxThreadRestart( );
bosko001 9:893843262a1f 156
bosko001 7:7ea932eb31f6 157 }
bosko001 11:2cc7e6f06cff 158
bosko001 7:7ea932eb31f6 159 // ODGOVOR - slanje odgovora klijentu
bosko001 7:7ea932eb31f6 160
bosko001 7:7ea932eb31f6 161 p_soc->set_timeout(-1); // blocking
bosko001 7:7ea932eb31f6 162 // slanje HTTP headera
bosko001 7:7ea932eb31f6 163 n = p_soc->send(sendHTTPheader, strlen(sendHTTPheader));
bosko001 7:7ea932eb31f6 164 printf("PREDAJA HTTPheader: posato bajtova = %d, %d \n\r", n, strlen(sendHTTPheader));
bosko001 7:7ea932eb31f6 165
bosko001 7:7ea932eb31f6 166 // putFLASHintoHTML( gpc_html ); // preuzimanje podataka iz flasha u html
bosko001 7:7ea932eb31f6 167 auto c = gpc_html->getHTML_free( ); // konvertovanje HTMLa u niz
bosko001 7:7ea932eb31f6 168 n = p_soc->send(c, strlen(c)); // slanje HTML-a
bosko001 7:7ea932eb31f6 169
bosko001 7:7ea932eb31f6 170 // test - slanje HTMLa na serial com
bosko001 7:7ea932eb31f6 171 // extern UARTSerial *gp_scom;
bosko001 7:7ea932eb31f6 172 // extern void sendScom( struct UARTSerial *p_scom, char *buffer, int val);
bosko001 7:7ea932eb31f6 173 // sendScom( gp_scom, c, strlen(c));
bosko001 7:7ea932eb31f6 174
bosko001 7:7ea932eb31f6 175 free(c); // oslobadjanje memorije HTML niza
bosko001 7:7ea932eb31f6 176
bosko001 7:7ea932eb31f6 177
bosko001 7:7ea932eb31f6 178 printf("PREDAJA dataBuffer: posato bajtova = %d, %d \n\r", n, strlen(c));
bosko001 7:7ea932eb31f6 179
bosko001 7:7ea932eb31f6 180 }
bosko001 7:7ea932eb31f6 181 }
bosko001 2:45b351b4fc2a 182
bosko001 11:2cc7e6f06cff 183 //!!! p_soc->close(); // zatvaranje socketa
bosko001 11:2cc7e6f06cff 184 // if(n<0)
bosko001 11:2cc7e6f06cff 185 {printf("BREAK posle predaje\n\r"); break;}
bosko001 11:2cc7e6f06cff 186
bosko001 11:2cc7e6f06cff 187 } // end while
bosko001 11:2cc7e6f06cff 188
bosko001 11:2cc7e6f06cff 189 {printf("zatvaranje socketa\n\r"); fflush(stdout);}
bosko001 11:2cc7e6f06cff 190
bosko001 11:2cc7e6f06cff 191 p_soc->close(); // zatvaranje socketa
bosko001 11:2cc7e6f06cff 192 {printf("zatvaranje thread-a\n\r"); fflush(stdout);}
bosko001 11:2cc7e6f06cff 193
bosko001 7:7ea932eb31f6 194 // zatvaranje thread-a
bosko001 2:45b351b4fc2a 195 gp_thr = ps_thp->p_Thread;// poslati pointer da se delete-uje
bosko001 11:2cc7e6f06cff 196
bosko001 11:2cc7e6f06cff 197 {printf("brisanje parametara\n\r"); fflush(stdout);}
bosko001 11:2cc7e6f06cff 198
bosko001 7:7ea932eb31f6 199 delete ps_thp; // relokacija ps_thp
bosko001 2:45b351b4fc2a 200
bosko001 2:45b351b4fc2a 201 }
bosko001 2:45b351b4fc2a 202