nova proba

Committer:
bosko001
Date:
Thu Jun 18 22:54:35 2020 +0200
Revision:
25:08f20fc5e5b5
Parent:
22:11279d24a105
Child:
26:c14d034d7459
sredjeni tcp klijenti

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bosko001 2:45b351b4fc2a 1 #include "mbed.h"
Bosko Lekovic 18:5ee53205451a 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
Bosko Lekovic 18:5ee53205451a 10 #include "UdpClients.h"
Bosko Lekovic 18:5ee53205451a 11 #include "TcpClients.h"
Bosko Lekovic 18:5ee53205451a 12 #include "SerialCom.h"
Bosko Lekovic 18:5ee53205451a 13
Bosko Lekovic 18:5ee53205451a 14
bosko001 10:15f7fea18a2f 15 #define SOCKET_PORT 80
bosko001 2:45b351b4fc2a 16
Bosko Lekovic 18:5ee53205451a 17 //Semaphore s(1); /* semafor da ne dozvoli vise of 1 server thred-a, upravlja delet-ovanjem threda*/
bosko001 2:45b351b4fc2a 18
Bosko Lekovic 18:5ee53205451a 19 //Mutex m;
Bosko Lekovic 18:5ee53205451a 20
Bosko Lekovic 18:5ee53205451a 21 volatile int tcpServerBlock=0; // Blokira serijsku komunikaciju tokom upisa u data flash
bosko001 2:45b351b4fc2a 22
bosko001 2:45b351b4fc2a 23 extern EthernetInterface *gp_eth;
bosko001 2:45b351b4fc2a 24
bosko001 7:7ea932eb31f6 25 /* globalni pointer na tcpServer RX Thread za parametarizaciju */
Bosko Lekovic 18:5ee53205451a 26 static Thread *gp_tcpServerThread = NULL; //new Thread;
bosko001 7:7ea932eb31f6 27 void tcpServerRx_fun( void );
bosko001 7:7ea932eb31f6 28
bosko001 7:7ea932eb31f6 29
Bosko Lekovic 18:5ee53205451a 30 static TCPSocket *gp_tcpSocket=NULL; /* socket za prijem zahteva za konekciju */
Bosko Lekovic 18:5ee53205451a 31 static Thread *gp_thr = NULL; /* pointer gde se smesta thread za brisanje */
bosko001 2:45b351b4fc2a 32
bosko001 2:45b351b4fc2a 33
bosko001 2:45b351b4fc2a 34 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 35
bosko001 19:7bdb1a10043f 36 typedef void(*PF_PSTP)(struct SThreadParam *); // ne koristi se
bosko001 2:45b351b4fc2a 37
bosko001 7:7ea932eb31f6 38 // parametri thread-a za uspostavljenje konekcije
bosko001 2:45b351b4fc2a 39 struct SThreadParam
bosko001 2:45b351b4fc2a 40 {
bosko001 7:7ea932eb31f6 41 Thread *p_Thread; // sopstveni pointer
bosko001 7:7ea932eb31f6 42 void * p_param; // pointer za parametre thread-a, u ovom slucaju na socket koji je accept-van
bosko001 2:45b351b4fc2a 43 };
bosko001 2:45b351b4fc2a 44
bosko001 2:45b351b4fc2a 45 /****************** inicijalizacija u mainu ************************/
Bosko Lekovic 18:5ee53205451a 46 char* html_string;
bosko001 2:45b351b4fc2a 47
bosko001 2:45b351b4fc2a 48 void inic_TcpServerToFlash( void )
bosko001 2:45b351b4fc2a 49 {
Bosko Lekovic 18:5ee53205451a 50
Bosko Lekovic 18:5ee53205451a 51 if( gp_tcpServerThread ) delete gp_tcpServerThread;
Bosko Lekovic 18:5ee53205451a 52 gp_tcpServerThread = new Thread();
bosko001 2:45b351b4fc2a 53 gp_tcpServerThread->start(tcpServerRx_fun);
bosko001 2:45b351b4fc2a 54
bosko001 2:45b351b4fc2a 55 }
bosko001 2:45b351b4fc2a 56
bosko001 2:45b351b4fc2a 57 /***************** periodicna prozivka u mainu *************************/
bosko001 7:7ea932eb31f6 58 // zatvaranje thread-a nakon http razmene
bosko001 2:45b351b4fc2a 59
bosko001 2:45b351b4fc2a 60 void cleanTcpServerToFlashThread( void )
bosko001 2:45b351b4fc2a 61 {
bosko001 2:45b351b4fc2a 62 if(gp_thr)
bosko001 2:45b351b4fc2a 63 {
Bosko Lekovic 18:5ee53205451a 64 printf("Brisanje thread-a %d\n\r", gp_thr); fflush(stdout);
Bosko Lekovic 18:5ee53205451a 65 gp_thr->join();
Bosko Lekovic 18:5ee53205451a 66
Bosko Lekovic 18:5ee53205451a 67 delete gp_thr;
Bosko Lekovic 18:5ee53205451a 68 gp_thr = NULL;
Bosko Lekovic 18:5ee53205451a 69 printf("Obrisan thread %d\n\r", gp_thr);
Bosko Lekovic 18:5ee53205451a 70 // s.release();
bosko001 2:45b351b4fc2a 71 }
bosko001 2:45b351b4fc2a 72 }
bosko001 2:45b351b4fc2a 73
Bosko Lekovic 18:5ee53205451a 74 void _semafor_release(void ) {/* s.release();*/}
Bosko Lekovic 18:5ee53205451a 75 /************** Trajni TCPServer thread *********************************/
bosko001 2:45b351b4fc2a 76
Bosko Lekovic 18:5ee53205451a 77 volatile int bws=0; //block web server
bosko001 2:45b351b4fc2a 78
bosko001 2:45b351b4fc2a 79 extern void rcpServerInstance_fun( SThreadParam *ps_thr );
bosko001 2:45b351b4fc2a 80 void tcpServerRx_fun( void )
bosko001 2:45b351b4fc2a 81 {
Bosko Lekovic 18:5ee53205451a 82 while( true )
Bosko Lekovic 18:5ee53205451a 83 {
Bosko Lekovic 18:5ee53205451a 84 printf("____Otvoren TCP server rx thread____ \n\r");
Bosko Lekovic 18:5ee53205451a 85 if( gp_tcpSocket ) {delete gp_tcpSocket; gp_tcpSocket = NULL; } /* ima smisla ako se thread nasilno zatvara */
bosko001 2:45b351b4fc2a 86
bosko001 2:45b351b4fc2a 87 gp_tcpSocket = new TCPSocket;
Bosko Lekovic 18:5ee53205451a 88 if(gp_tcpSocket->open( gp_eth ) != 0) break;
Bosko Lekovic 18:5ee53205451a 89 gp_tcpSocket->bind(SOCKET_PORT);
Bosko Lekovic 18:5ee53205451a 90
Bosko Lekovic 18:5ee53205451a 91 if(gp_tcpSocket->listen(2) != 0 )
Bosko Lekovic 18:5ee53205451a 92 { printf("greska listenovanja na socketu\n\r"); break;}
Bosko Lekovic 18:5ee53205451a 93
Bosko Lekovic 18:5ee53205451a 94 TCPSocket *p_soc;
Bosko Lekovic 18:5ee53205451a 95 nsapi_error_t err=NULL;
Bosko Lekovic 18:5ee53205451a 96 int nos = 0;
Bosko Lekovic 18:5ee53205451a 97
Bosko Lekovic 18:5ee53205451a 98 while( (p_soc = gp_tcpSocket->accept( &err)) )
bosko001 2:45b351b4fc2a 99 {
bosko001 22:11279d24a105 100 // printf("\n\rTCP WEB SERVER: zahtev za konekciju - socket = %d err = %d\n\r", p_soc, err);fflush(stdout);
Bosko Lekovic 18:5ee53205451a 101 // s.wait(); // semafor sprecava novu konekciju dok se stara ne zavrsi
bosko001 22:11279d24a105 102 // printf("TCP WEB SERVER: Broj %d\n\r", ++nos); fflush(stdout);
Bosko Lekovic 18:5ee53205451a 103
Bosko Lekovic 18:5ee53205451a 104 bws = 1;
bosko001 2:45b351b4fc2a 105
Bosko Lekovic 18:5ee53205451a 106 Thread *p_thr = new Thread( );
Bosko Lekovic 18:5ee53205451a 107 struct SThreadParam *ps_thp = new struct SThreadParam; // alokacija ps_thp
Bosko Lekovic 18:5ee53205451a 108 ps_thp->p_param = (void*)p_soc;
Bosko Lekovic 18:5ee53205451a 109 ps_thp->p_Thread = p_thr;
bosko001 19:7bdb1a10043f 110
Bosko Lekovic 18:5ee53205451a 111
bosko001 22:11279d24a105 112 // printf("TCPThread: NOVA KONEKCIJA! thread %d socket %d struct Sst %d\n\r", ps_thp->p_Thread, p_soc, ps_thp);fflush(stdout);
bosko001 19:7bdb1a10043f 113 p_thr->start( callback( rcpServerInstance_fun, ps_thp ));
Bosko Lekovic 18:5ee53205451a 114
Bosko Lekovic 18:5ee53205451a 115 while(bws);
bosko001 2:45b351b4fc2a 116
bosko001 2:45b351b4fc2a 117 }
Bosko Lekovic 18:5ee53205451a 118 printf("TCP WEB SERVER: Ispad iz accept while!!! socket = %d err = %d\n\r", p_soc, err);fflush(stdout);
Bosko Lekovic 18:5ee53205451a 119 }
bosko001 2:45b351b4fc2a 120
bosko001 2:45b351b4fc2a 121 }
bosko001 2:45b351b4fc2a 122
bosko001 2:45b351b4fc2a 123
bosko001 2:45b351b4fc2a 124
bosko001 2:45b351b4fc2a 125
bosko001 2:45b351b4fc2a 126 /***************** privremeni TCPServer thread za klijenta ******************/
bosko001 7:7ea932eb31f6 127 // thread je jednokratan - primi http telegram i odgovori - ako je GET ili POST upit
bosko001 2:45b351b4fc2a 128
bosko001 2:45b351b4fc2a 129 void rcpServerInstance_fun( struct SThreadParam *ps_thp )
bosko001 2:45b351b4fc2a 130 {
bosko001 2:45b351b4fc2a 131 TCPSocket *p_soc = (TCPSocket *)ps_thp->p_param;
Bosko Lekovic 18:5ee53205451a 132
Bosko Lekovic 18:5ee53205451a 133 // m.lock();
Bosko Lekovic 18:5ee53205451a 134 // tcpServerBlock++;
Bosko Lekovic 18:5ee53205451a 135
bosko001 2:45b351b4fc2a 136 char inbuff[1500];
Bosko Lekovic 18:5ee53205451a 137 int restart = 0;
bosko001 2:45b351b4fc2a 138
bosko001 2:45b351b4fc2a 139 int n, nr, nt;
Bosko Lekovic 18:5ee53205451a 140 printf("tcpServThread: Otvoren socket %d\n\r",(int)p_soc);
bosko001 2:45b351b4fc2a 141
bosko001 2:45b351b4fc2a 142 while(true)
bosko001 2:45b351b4fc2a 143 {
bosko001 7:7ea932eb31f6 144 extern C_HTMLparse *gpc_html;
bosko001 11:2cc7e6f06cff 145
bosko001 22:11279d24a105 146 // printf("tcpServThread: cekam prijem od klijenta \n\r");
bosko001 2:45b351b4fc2a 147 SocketAddress sockAddr;
bosko001 2:45b351b4fc2a 148 p_soc->getpeername( &sockAddr );
bosko001 2:45b351b4fc2a 149
bosko001 7:7ea932eb31f6 150 p_soc->set_timeout(10); // non-blocking pruzimanje bajtova na 10ms
bosko001 2:45b351b4fc2a 151 n=0;
Bosko Lekovic 18:5ee53205451a 152 int k=10, t=0;
bosko001 2:45b351b4fc2a 153 do
bosko001 2:45b351b4fc2a 154 {
Bosko Lekovic 18:5ee53205451a 155 t++;
Bosko Lekovic 18:5ee53205451a 156 nr = p_soc->recv( &(inbuff[n]), sizeof(inbuff)-n); // razmotriti obradu nr=0 !!!
bosko001 2:45b351b4fc2a 157 if(nr > 0) n += nr;
Bosko Lekovic 18:5ee53205451a 158 else k--;
bosko001 2:45b351b4fc2a 159 if(n>= sizeof(inbuff)) break;
bosko001 2:45b351b4fc2a 160 }
Bosko Lekovic 18:5ee53205451a 161 while( ((n==0) || (nr > 0) ) && k );
bosko001 22:11279d24a105 162 // printf( "tcpServThread: broj prolaza t=%d k=%d n=%d nr=%d\n\r ", t, k, n, nr);
bosko001 2:45b351b4fc2a 163 if( (nr < 0) && (nr != NSAPI_ERROR_WOULD_BLOCK) ) break;
Bosko Lekovic 18:5ee53205451a 164 if( !k ) break;
Bosko Lekovic 18:5ee53205451a 165
bosko001 2:45b351b4fc2a 166 if(n>0)
bosko001 2:45b351b4fc2a 167 {
bosko001 7:7ea932eb31f6 168
bosko001 7:7ea932eb31f6 169 //u inbuff-u je http paket
Bosko Lekovic 18:5ee53205451a 170 printf("tcpServThread: primljeno = %d bajtova \n\r",n);
bosko001 11:2cc7e6f06cff 171 fflush(stdout);
bosko001 7:7ea932eb31f6 172
bosko001 7:7ea932eb31f6 173 inbuff[n]=0;
bosko001 7:7ea932eb31f6 174 //raspakivanje sadrzaja, a ako je POST preuzimanje parametara
Bosko Lekovic 18:5ee53205451a 175 char ** strniz = gpc_html->doHTTP_freereturn( inbuff);
Bosko Lekovic 18:5ee53205451a 176 char *type = strniz[0];
Bosko Lekovic 18:5ee53205451a 177 char *description = strniz[1];
Bosko Lekovic 18:5ee53205451a 178 if( (!strcmp( type, "GET") && !strcmp( description, "HTTP")) || !strcmp( type, "POST") )
bosko001 7:7ea932eb31f6 179 {
bosko001 7:7ea932eb31f6 180
Bosko Lekovic 18:5ee53205451a 181
Bosko Lekovic 18:5ee53205451a 182
bosko001 7:7ea932eb31f6 183 // putHTMLintoFLASH( gpc_html );
bosko001 7:7ea932eb31f6 184 //putFLASHintoHTML( gpc_html );
bosko001 7:7ea932eb31f6 185
Bosko Lekovic 18:5ee53205451a 186 if(!strcmp( type, "POST") )
bosko001 7:7ea932eb31f6 187 {
bosko001 9:893843262a1f 188
bosko001 21:2232f5a34eb4 189 tcpServerBlock = 1;
Bosko Lekovic 18:5ee53205451a 190
bosko001 22:11279d24a105 191 // 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());
Bosko Lekovic 18:5ee53205451a 192 gpc_html->htmlPOST_fun( ); // setovanje parametara u HTML format
Bosko Lekovic 18:5ee53205451a 193 putHTMLintoFLASH( gpc_html ); // upis parametara iz HTMLa u FLASH
bosko001 19:7bdb1a10043f 194
bosko001 19:7bdb1a10043f 195 //restart_TcpClientsConnection(1);
bosko001 22:11279d24a105 196
bosko001 22:11279d24a105 197
bosko001 22:11279d24a105 198 // restart_TcpClients();
bosko001 22:11279d24a105 199 // restart_UdpClients();
bosko001 22:11279d24a105 200 // restart_SerialCom( );
bosko001 22:11279d24a105 201
bosko001 22:11279d24a105 202
bosko001 22:11279d24a105 203
Bosko Lekovic 18:5ee53205451a 204 // vratiti podatke iz Flasha u HTML !!!!!!
Bosko Lekovic 18:5ee53205451a 205
Bosko Lekovic 18:5ee53205451a 206 restart = 1;
Bosko Lekovic 18:5ee53205451a 207 }
bosko001 11:2cc7e6f06cff 208
bosko001 7:7ea932eb31f6 209 // ODGOVOR - slanje odgovora klijentu
bosko001 7:7ea932eb31f6 210
bosko001 7:7ea932eb31f6 211 p_soc->set_timeout(-1); // blocking
bosko001 7:7ea932eb31f6 212 // slanje HTTP headera
Bosko Lekovic 18:5ee53205451a 213 // if(strcmp( type, "POST") )
Bosko Lekovic 18:5ee53205451a 214 {
bosko001 7:7ea932eb31f6 215 n = p_soc->send(sendHTTPheader, strlen(sendHTTPheader));
Bosko Lekovic 18:5ee53205451a 216 printf("tcpServThread: predaja HTTPheader: posato bajtova = %d, %d \n\r", n, strlen(sendHTTPheader));
bosko001 7:7ea932eb31f6 217
bosko001 7:7ea932eb31f6 218 // putFLASHintoHTML( gpc_html ); // preuzimanje podataka iz flasha u html
bosko001 7:7ea932eb31f6 219
Bosko Lekovic 18:5ee53205451a 220 auto html_string = gpc_html->getHTML_free( ); // konvertovanje HTMLa u niz
Bosko Lekovic 18:5ee53205451a 221 n = p_soc->send(html_string, strlen(html_string)); // slanje HTML-a
bosko001 22:11279d24a105 222
bosko001 22:11279d24a105 223
Bosko Lekovic 18:5ee53205451a 224 printf("tcpServThread: PREDAJA dataBuffer: posato bajtova = %d, %d \n\r", n, strlen(html_string));
Bosko Lekovic 18:5ee53205451a 225 free(html_string); // oslobadjanje memorije HTML niza
Bosko Lekovic 18:5ee53205451a 226 }
bosko001 7:7ea932eb31f6 227 }
Bosko Lekovic 18:5ee53205451a 228 free(type);
Bosko Lekovic 18:5ee53205451a 229 free( description);
Bosko Lekovic 18:5ee53205451a 230 free(strniz);
bosko001 7:7ea932eb31f6 231 }
bosko001 2:45b351b4fc2a 232
Bosko Lekovic 18:5ee53205451a 233
bosko001 11:2cc7e6f06cff 234 //!!! p_soc->close(); // zatvaranje socketa
bosko001 11:2cc7e6f06cff 235 // if(n<0)
Bosko Lekovic 18:5ee53205451a 236 {printf("tcpServThread: BREAK posle predaje\n\r"); break;}
bosko001 11:2cc7e6f06cff 237
bosko001 11:2cc7e6f06cff 238 } // end while
bosko001 11:2cc7e6f06cff 239
Bosko Lekovic 18:5ee53205451a 240 {printf("tcpServThread: zatvaranje socketa\n\r"); fflush(stdout);}
Bosko Lekovic 18:5ee53205451a 241
Bosko Lekovic 18:5ee53205451a 242 p_soc->close(); // zatvaranje socketa
Bosko Lekovic 18:5ee53205451a 243
Bosko Lekovic 18:5ee53205451a 244 printf("tcpServThread: restart = %d\n\r",restart);
Bosko Lekovic 18:5ee53205451a 245 if( restart )
Bosko Lekovic 18:5ee53205451a 246 {
Bosko Lekovic 18:5ee53205451a 247 printf("tcpServThread:___restart komunikacije\n\r");
bosko001 22:11279d24a105 248 if(restart) {extern void startUp(void); startUp();}
Bosko Lekovic 18:5ee53205451a 249 // ethRestart( );
Bosko Lekovic 18:5ee53205451a 250 // // udpRxThreadsRiseUp( );
Bosko Lekovic 18:5ee53205451a 251 // tcpRxThreadsRiseUp( );
Bosko Lekovic 18:5ee53205451a 252 // // scomRxThreadRestart( );
Bosko Lekovic 18:5ee53205451a 253 restart = 0;
Bosko Lekovic 18:5ee53205451a 254 }
Bosko Lekovic 18:5ee53205451a 255
Bosko Lekovic 18:5ee53205451a 256 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());
Bosko Lekovic 18:5ee53205451a 257
Bosko Lekovic 18:5ee53205451a 258 {printf("tcpServThread: zatvaranje thread-a\n\r"); fflush(stdout);}
Bosko Lekovic 18:5ee53205451a 259 // zatvaranje thread-a
Bosko Lekovic 18:5ee53205451a 260 gp_thr = ps_thp->p_Thread;// poslati pointer da se delete-uje
bosko001 11:2cc7e6f06cff 261
Bosko Lekovic 18:5ee53205451a 262 {printf("tcpServThread: brisanje parametara\n\r"); fflush(stdout);}
Bosko Lekovic 18:5ee53205451a 263 delete ps_thp; // relokacija ps_thp
Bosko Lekovic 18:5ee53205451a 264
Bosko Lekovic 18:5ee53205451a 265 bws = 0;
Bosko Lekovic 18:5ee53205451a 266 //m.unlock();
bosko001 21:2232f5a34eb4 267 tcpServerBlock = 0;
Bosko Lekovic 18:5ee53205451a 268
bosko001 2:45b351b4fc2a 269 }
bosko001 2:45b351b4fc2a 270