nova proba

Committer:
Bosko Lekovic
Date:
Mon Jan 18 14:46:46 2021 +0100
Revision:
28:49bafc8bb056
Parent:
26:c14d034d7459
novije

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 {
bosko001 26:c14d034d7459 84 printf("____Otvoren TCP WEB 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 {
Bosko Lekovic 18:5ee53205451a 100
Bosko Lekovic 18:5ee53205451a 101 bws = 1;
bosko001 2:45b351b4fc2a 102
Bosko Lekovic 18:5ee53205451a 103 Thread *p_thr = new Thread( );
Bosko Lekovic 18:5ee53205451a 104 struct SThreadParam *ps_thp = new struct SThreadParam; // alokacija ps_thp
Bosko Lekovic 18:5ee53205451a 105 ps_thp->p_param = (void*)p_soc;
Bosko Lekovic 18:5ee53205451a 106 ps_thp->p_Thread = p_thr;
bosko001 19:7bdb1a10043f 107
Bosko Lekovic 18:5ee53205451a 108
bosko001 22:11279d24a105 109 // 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 110 p_thr->start( callback( rcpServerInstance_fun, ps_thp ));
Bosko Lekovic 18:5ee53205451a 111
Bosko Lekovic 18:5ee53205451a 112 while(bws);
bosko001 2:45b351b4fc2a 113
bosko001 2:45b351b4fc2a 114 }
Bosko Lekovic 18:5ee53205451a 115 printf("TCP WEB SERVER: Ispad iz accept while!!! socket = %d err = %d\n\r", p_soc, err);fflush(stdout);
Bosko Lekovic 18:5ee53205451a 116 }
bosko001 2:45b351b4fc2a 117
bosko001 2:45b351b4fc2a 118 }
bosko001 2:45b351b4fc2a 119
bosko001 2:45b351b4fc2a 120
bosko001 2:45b351b4fc2a 121
bosko001 2:45b351b4fc2a 122
bosko001 2:45b351b4fc2a 123 /***************** privremeni TCPServer thread za klijenta ******************/
bosko001 7:7ea932eb31f6 124 // thread je jednokratan - primi http telegram i odgovori - ako je GET ili POST upit
bosko001 2:45b351b4fc2a 125
bosko001 2:45b351b4fc2a 126 void rcpServerInstance_fun( struct SThreadParam *ps_thp )
bosko001 2:45b351b4fc2a 127 {
bosko001 2:45b351b4fc2a 128 TCPSocket *p_soc = (TCPSocket *)ps_thp->p_param;
Bosko Lekovic 18:5ee53205451a 129
Bosko Lekovic 18:5ee53205451a 130 // m.lock();
Bosko Lekovic 18:5ee53205451a 131 // tcpServerBlock++;
Bosko Lekovic 18:5ee53205451a 132
bosko001 2:45b351b4fc2a 133 char inbuff[1500];
Bosko Lekovic 18:5ee53205451a 134 int restart = 0;
bosko001 2:45b351b4fc2a 135
bosko001 2:45b351b4fc2a 136 int n, nr, nt;
bosko001 26:c14d034d7459 137 // printf("tcpServThread: Otvoren socket %d\n\r",(int)p_soc);
bosko001 2:45b351b4fc2a 138
bosko001 2:45b351b4fc2a 139 while(true)
bosko001 2:45b351b4fc2a 140 {
bosko001 7:7ea932eb31f6 141 extern C_HTMLparse *gpc_html;
bosko001 11:2cc7e6f06cff 142
bosko001 22:11279d24a105 143 // printf("tcpServThread: cekam prijem od klijenta \n\r");
bosko001 2:45b351b4fc2a 144 SocketAddress sockAddr;
bosko001 2:45b351b4fc2a 145 p_soc->getpeername( &sockAddr );
bosko001 2:45b351b4fc2a 146
bosko001 7:7ea932eb31f6 147 p_soc->set_timeout(10); // non-blocking pruzimanje bajtova na 10ms
bosko001 2:45b351b4fc2a 148 n=0;
Bosko Lekovic 18:5ee53205451a 149 int k=10, t=0;
bosko001 2:45b351b4fc2a 150 do
bosko001 2:45b351b4fc2a 151 {
Bosko Lekovic 18:5ee53205451a 152 t++;
Bosko Lekovic 18:5ee53205451a 153 nr = p_soc->recv( &(inbuff[n]), sizeof(inbuff)-n); // razmotriti obradu nr=0 !!!
bosko001 2:45b351b4fc2a 154 if(nr > 0) n += nr;
Bosko Lekovic 18:5ee53205451a 155 else k--;
bosko001 2:45b351b4fc2a 156 if(n>= sizeof(inbuff)) break;
bosko001 2:45b351b4fc2a 157 }
Bosko Lekovic 18:5ee53205451a 158 while( ((n==0) || (nr > 0) ) && k );
bosko001 22:11279d24a105 159 // printf( "tcpServThread: broj prolaza t=%d k=%d n=%d nr=%d\n\r ", t, k, n, nr);
bosko001 2:45b351b4fc2a 160 if( (nr < 0) && (nr != NSAPI_ERROR_WOULD_BLOCK) ) break;
Bosko Lekovic 18:5ee53205451a 161 if( !k ) break;
Bosko Lekovic 18:5ee53205451a 162
bosko001 2:45b351b4fc2a 163 if(n>0)
bosko001 2:45b351b4fc2a 164 {
bosko001 7:7ea932eb31f6 165
bosko001 7:7ea932eb31f6 166 //u inbuff-u je http paket
Bosko Lekovic 18:5ee53205451a 167 printf("tcpServThread: primljeno = %d bajtova \n\r",n);
bosko001 11:2cc7e6f06cff 168 fflush(stdout);
bosko001 7:7ea932eb31f6 169
bosko001 7:7ea932eb31f6 170 inbuff[n]=0;
bosko001 26:c14d034d7459 171 // printf("%s\n\r",inbuff);
bosko001 7:7ea932eb31f6 172 //raspakivanje sadrzaja, a ako je POST preuzimanje parametara
Bosko Lekovic 18:5ee53205451a 173 char ** strniz = gpc_html->doHTTP_freereturn( inbuff);
Bosko Lekovic 18:5ee53205451a 174 char *type = strniz[0];
Bosko Lekovic 18:5ee53205451a 175 char *description = strniz[1];
Bosko Lekovic 18:5ee53205451a 176 if( (!strcmp( type, "GET") && !strcmp( description, "HTTP")) || !strcmp( type, "POST") )
bosko001 7:7ea932eb31f6 177 {
bosko001 7:7ea932eb31f6 178
Bosko Lekovic 18:5ee53205451a 179
Bosko Lekovic 18:5ee53205451a 180
bosko001 7:7ea932eb31f6 181 // putHTMLintoFLASH( gpc_html );
bosko001 7:7ea932eb31f6 182 //putFLASHintoHTML( gpc_html );
bosko001 7:7ea932eb31f6 183
Bosko Lekovic 18:5ee53205451a 184 if(!strcmp( type, "POST") )
bosko001 7:7ea932eb31f6 185 {
bosko001 9:893843262a1f 186
bosko001 21:2232f5a34eb4 187 tcpServerBlock = 1;
Bosko Lekovic 18:5ee53205451a 188
bosko001 22:11279d24a105 189 // 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 190 gpc_html->htmlPOST_fun( ); // setovanje parametara u HTML format
Bosko Lekovic 18:5ee53205451a 191 putHTMLintoFLASH( gpc_html ); // upis parametara iz HTMLa u FLASH
bosko001 19:7bdb1a10043f 192
bosko001 19:7bdb1a10043f 193 //restart_TcpClientsConnection(1);
bosko001 22:11279d24a105 194
bosko001 22:11279d24a105 195
bosko001 22:11279d24a105 196 // restart_TcpClients();
bosko001 22:11279d24a105 197 // restart_UdpClients();
bosko001 22:11279d24a105 198 // restart_SerialCom( );
bosko001 22:11279d24a105 199
bosko001 22:11279d24a105 200
bosko001 22:11279d24a105 201
Bosko Lekovic 18:5ee53205451a 202 // vratiti podatke iz Flasha u HTML !!!!!!
Bosko Lekovic 18:5ee53205451a 203
Bosko Lekovic 18:5ee53205451a 204 restart = 1;
Bosko Lekovic 18:5ee53205451a 205 }
bosko001 11:2cc7e6f06cff 206
bosko001 7:7ea932eb31f6 207 // ODGOVOR - slanje odgovora klijentu
bosko001 7:7ea932eb31f6 208
bosko001 7:7ea932eb31f6 209 p_soc->set_timeout(-1); // blocking
bosko001 7:7ea932eb31f6 210 // slanje HTTP headera
Bosko Lekovic 18:5ee53205451a 211 // if(strcmp( type, "POST") )
Bosko Lekovic 18:5ee53205451a 212 {
bosko001 7:7ea932eb31f6 213 n = p_soc->send(sendHTTPheader, strlen(sendHTTPheader));
Bosko Lekovic 18:5ee53205451a 214 printf("tcpServThread: predaja HTTPheader: posato bajtova = %d, %d \n\r", n, strlen(sendHTTPheader));
bosko001 7:7ea932eb31f6 215
bosko001 7:7ea932eb31f6 216 // putFLASHintoHTML( gpc_html ); // preuzimanje podataka iz flasha u html
bosko001 7:7ea932eb31f6 217
Bosko Lekovic 18:5ee53205451a 218 auto html_string = gpc_html->getHTML_free( ); // konvertovanje HTMLa u niz
Bosko Lekovic 18:5ee53205451a 219 n = p_soc->send(html_string, strlen(html_string)); // slanje HTML-a
bosko001 22:11279d24a105 220
bosko001 22:11279d24a105 221
Bosko Lekovic 18:5ee53205451a 222 printf("tcpServThread: PREDAJA dataBuffer: posato bajtova = %d, %d \n\r", n, strlen(html_string));
Bosko Lekovic 18:5ee53205451a 223 free(html_string); // oslobadjanje memorije HTML niza
Bosko Lekovic 18:5ee53205451a 224 }
bosko001 7:7ea932eb31f6 225 }
Bosko Lekovic 18:5ee53205451a 226 free(type);
Bosko Lekovic 18:5ee53205451a 227 free( description);
Bosko Lekovic 18:5ee53205451a 228 free(strniz);
bosko001 7:7ea932eb31f6 229 }
bosko001 2:45b351b4fc2a 230
Bosko Lekovic 18:5ee53205451a 231
bosko001 11:2cc7e6f06cff 232 //!!! p_soc->close(); // zatvaranje socketa
bosko001 11:2cc7e6f06cff 233 // if(n<0)
Bosko Lekovic 18:5ee53205451a 234 {printf("tcpServThread: BREAK posle predaje\n\r"); break;}
bosko001 11:2cc7e6f06cff 235
bosko001 11:2cc7e6f06cff 236 } // end while
bosko001 11:2cc7e6f06cff 237
Bosko Lekovic 18:5ee53205451a 238 {printf("tcpServThread: zatvaranje socketa\n\r"); fflush(stdout);}
Bosko Lekovic 18:5ee53205451a 239
Bosko Lekovic 18:5ee53205451a 240 p_soc->close(); // zatvaranje socketa
Bosko Lekovic 18:5ee53205451a 241
Bosko Lekovic 18:5ee53205451a 242 printf("tcpServThread: restart = %d\n\r",restart);
Bosko Lekovic 18:5ee53205451a 243 if( restart )
Bosko Lekovic 18:5ee53205451a 244 {
Bosko Lekovic 18:5ee53205451a 245 printf("tcpServThread:___restart komunikacije\n\r");
bosko001 22:11279d24a105 246 if(restart) {extern void startUp(void); startUp();}
Bosko Lekovic 18:5ee53205451a 247 // ethRestart( );
Bosko Lekovic 18:5ee53205451a 248 // // udpRxThreadsRiseUp( );
Bosko Lekovic 18:5ee53205451a 249 // tcpRxThreadsRiseUp( );
Bosko Lekovic 18:5ee53205451a 250 // // scomRxThreadRestart( );
Bosko Lekovic 18:5ee53205451a 251 restart = 0;
Bosko Lekovic 18:5ee53205451a 252 }
Bosko Lekovic 18:5ee53205451a 253
Bosko Lekovic 18:5ee53205451a 254 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 255
Bosko Lekovic 18:5ee53205451a 256 {printf("tcpServThread: zatvaranje thread-a\n\r"); fflush(stdout);}
Bosko Lekovic 18:5ee53205451a 257 // zatvaranje thread-a
Bosko Lekovic 18:5ee53205451a 258 gp_thr = ps_thp->p_Thread;// poslati pointer da se delete-uje
bosko001 11:2cc7e6f06cff 259
Bosko Lekovic 18:5ee53205451a 260 {printf("tcpServThread: brisanje parametara\n\r"); fflush(stdout);}
Bosko Lekovic 18:5ee53205451a 261 delete ps_thp; // relokacija ps_thp
Bosko Lekovic 18:5ee53205451a 262
Bosko Lekovic 18:5ee53205451a 263 bws = 0;
Bosko Lekovic 18:5ee53205451a 264 //m.unlock();
bosko001 21:2232f5a34eb4 265 tcpServerBlock = 0;
Bosko Lekovic 18:5ee53205451a 266
bosko001 2:45b351b4fc2a 267 }
bosko001 2:45b351b4fc2a 268