nova proba

Committer:
bosko001
Date:
Sat May 23 00:30:00 2020 +0200
Revision:
19:7bdb1a10043f
Parent:
18:5ee53205451a
Child:
21:2232f5a34eb4
prva verzija, rade serial i 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 // extern C_HTMLparse *gpc_html;
Bosko Lekovic 18:5ee53205451a 51 // html_string = gpc_html->getHTML_free( ); // konvertovanje HTMLa u niz
Bosko Lekovic 18:5ee53205451a 52
Bosko Lekovic 18:5ee53205451a 53 if( gp_tcpServerThread ) delete gp_tcpServerThread;
Bosko Lekovic 18:5ee53205451a 54 gp_tcpServerThread = new Thread();
bosko001 2:45b351b4fc2a 55 gp_tcpServerThread->start(tcpServerRx_fun);
bosko001 2:45b351b4fc2a 56
bosko001 2:45b351b4fc2a 57 }
bosko001 2:45b351b4fc2a 58
bosko001 2:45b351b4fc2a 59 /***************** periodicna prozivka u mainu *************************/
bosko001 7:7ea932eb31f6 60 // zatvaranje thread-a nakon http razmene
bosko001 2:45b351b4fc2a 61
bosko001 2:45b351b4fc2a 62 void cleanTcpServerToFlashThread( void )
bosko001 2:45b351b4fc2a 63 {
bosko001 2:45b351b4fc2a 64 if(gp_thr)
bosko001 2:45b351b4fc2a 65 {
Bosko Lekovic 18:5ee53205451a 66 printf("Brisanje thread-a %d\n\r", gp_thr); fflush(stdout);
Bosko Lekovic 18:5ee53205451a 67 gp_thr->join();
Bosko Lekovic 18:5ee53205451a 68
Bosko Lekovic 18:5ee53205451a 69 delete gp_thr;
Bosko Lekovic 18:5ee53205451a 70 gp_thr = NULL;
Bosko Lekovic 18:5ee53205451a 71 printf("Obrisan thread %d\n\r", gp_thr);
Bosko Lekovic 18:5ee53205451a 72 // s.release();
bosko001 2:45b351b4fc2a 73 }
bosko001 2:45b351b4fc2a 74 }
bosko001 2:45b351b4fc2a 75
Bosko Lekovic 18:5ee53205451a 76 void _semafor_release(void ) {/* s.release();*/}
Bosko Lekovic 18:5ee53205451a 77 /************** Trajni TCPServer thread *********************************/
bosko001 2:45b351b4fc2a 78
Bosko Lekovic 18:5ee53205451a 79 volatile int bws=0; //block web server
bosko001 2:45b351b4fc2a 80
bosko001 2:45b351b4fc2a 81 extern void rcpServerInstance_fun( SThreadParam *ps_thr );
bosko001 2:45b351b4fc2a 82 void tcpServerRx_fun( void )
bosko001 2:45b351b4fc2a 83 {
Bosko Lekovic 18:5ee53205451a 84 while( true )
Bosko Lekovic 18:5ee53205451a 85 {
Bosko Lekovic 18:5ee53205451a 86 printf("____Otvoren TCP server rx thread____ \n\r");
Bosko Lekovic 18:5ee53205451a 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 gp_tcpSocket = new TCPSocket;
Bosko Lekovic 18:5ee53205451a 90 if(gp_tcpSocket->open( gp_eth ) != 0) break;
Bosko Lekovic 18:5ee53205451a 91 gp_tcpSocket->bind(SOCKET_PORT);
Bosko Lekovic 18:5ee53205451a 92
Bosko Lekovic 18:5ee53205451a 93 if(gp_tcpSocket->listen(2) != 0 )
Bosko Lekovic 18:5ee53205451a 94 { printf("greska listenovanja na socketu\n\r"); break;}
Bosko Lekovic 18:5ee53205451a 95
Bosko Lekovic 18:5ee53205451a 96 TCPSocket *p_soc;
Bosko Lekovic 18:5ee53205451a 97 nsapi_error_t err=NULL;
Bosko Lekovic 18:5ee53205451a 98 int nos = 0;
Bosko Lekovic 18:5ee53205451a 99
Bosko Lekovic 18:5ee53205451a 100 while( (p_soc = gp_tcpSocket->accept( &err)) )
bosko001 2:45b351b4fc2a 101 {
Bosko Lekovic 18:5ee53205451a 102 printf("\n\rTCP WEB SERVER: zahtev za konekciju - socket = %d err = %d\n\r", p_soc, err);fflush(stdout);
Bosko Lekovic 18:5ee53205451a 103 // s.wait(); // semafor sprecava novu konekciju dok se stara ne zavrsi
Bosko Lekovic 18:5ee53205451a 104 printf("TCP WEB SERVER: Broj %d\n\r", ++nos); fflush(stdout);
Bosko Lekovic 18:5ee53205451a 105
Bosko Lekovic 18:5ee53205451a 106 bws = 1;
bosko001 2:45b351b4fc2a 107
Bosko Lekovic 18:5ee53205451a 108 Thread *p_thr = new Thread( );
Bosko Lekovic 18:5ee53205451a 109 struct SThreadParam *ps_thp = new struct SThreadParam; // alokacija ps_thp
Bosko Lekovic 18:5ee53205451a 110 ps_thp->p_param = (void*)p_soc;
Bosko Lekovic 18:5ee53205451a 111 ps_thp->p_Thread = p_thr;
bosko001 19:7bdb1a10043f 112
Bosko Lekovic 18:5ee53205451a 113
Bosko Lekovic 18:5ee53205451a 114 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 115 p_thr->start( callback( rcpServerInstance_fun, ps_thp ));
Bosko Lekovic 18:5ee53205451a 116
Bosko Lekovic 18:5ee53205451a 117 while(bws);
bosko001 2:45b351b4fc2a 118
bosko001 2:45b351b4fc2a 119 }
Bosko Lekovic 18:5ee53205451a 120 printf("TCP WEB SERVER: Ispad iz accept while!!! socket = %d err = %d\n\r", p_soc, err);fflush(stdout);
Bosko Lekovic 18:5ee53205451a 121 }
bosko001 2:45b351b4fc2a 122
bosko001 2:45b351b4fc2a 123 }
bosko001 2:45b351b4fc2a 124
bosko001 2:45b351b4fc2a 125
bosko001 2:45b351b4fc2a 126
bosko001 2:45b351b4fc2a 127
bosko001 2:45b351b4fc2a 128 /***************** privremeni TCPServer thread za klijenta ******************/
bosko001 7:7ea932eb31f6 129 // thread je jednokratan - primi http telegram i odgovori - ako je GET ili POST upit
bosko001 2:45b351b4fc2a 130
bosko001 2:45b351b4fc2a 131 void rcpServerInstance_fun( struct SThreadParam *ps_thp )
bosko001 2:45b351b4fc2a 132 {
bosko001 2:45b351b4fc2a 133 TCPSocket *p_soc = (TCPSocket *)ps_thp->p_param;
Bosko Lekovic 18:5ee53205451a 134
Bosko Lekovic 18:5ee53205451a 135 // m.lock();
Bosko Lekovic 18:5ee53205451a 136 // tcpServerBlock++;
Bosko Lekovic 18:5ee53205451a 137
bosko001 2:45b351b4fc2a 138 char inbuff[1500];
Bosko Lekovic 18:5ee53205451a 139 int restart = 0;
bosko001 2:45b351b4fc2a 140
bosko001 2:45b351b4fc2a 141 int n, nr, nt;
Bosko Lekovic 18:5ee53205451a 142 printf("tcpServThread: Otvoren socket %d\n\r",(int)p_soc);
bosko001 2:45b351b4fc2a 143
bosko001 2:45b351b4fc2a 144 while(true)
bosko001 2:45b351b4fc2a 145 {
bosko001 7:7ea932eb31f6 146 extern C_HTMLparse *gpc_html;
bosko001 11:2cc7e6f06cff 147
Bosko Lekovic 18:5ee53205451a 148 printf("tcpServThread: cekam prijem od klijenta \n\r");
bosko001 2:45b351b4fc2a 149 SocketAddress sockAddr;
bosko001 2:45b351b4fc2a 150 p_soc->getpeername( &sockAddr );
bosko001 2:45b351b4fc2a 151
bosko001 7:7ea932eb31f6 152 p_soc->set_timeout(10); // non-blocking pruzimanje bajtova na 10ms
bosko001 2:45b351b4fc2a 153 n=0;
Bosko Lekovic 18:5ee53205451a 154 int k=10, t=0;
bosko001 2:45b351b4fc2a 155 do
bosko001 2:45b351b4fc2a 156 {
Bosko Lekovic 18:5ee53205451a 157 t++;
Bosko Lekovic 18:5ee53205451a 158 nr = p_soc->recv( &(inbuff[n]), sizeof(inbuff)-n); // razmotriti obradu nr=0 !!!
bosko001 2:45b351b4fc2a 159 if(nr > 0) n += nr;
Bosko Lekovic 18:5ee53205451a 160 else k--;
bosko001 2:45b351b4fc2a 161 if(n>= sizeof(inbuff)) break;
bosko001 2:45b351b4fc2a 162 }
Bosko Lekovic 18:5ee53205451a 163 while( ((n==0) || (nr > 0) ) && k );
Bosko Lekovic 18:5ee53205451a 164 printf( "tcpServThread: broj prolaza t=%d k=%d n=%d nr=%d\n\r ", t, k, n, nr);
bosko001 2:45b351b4fc2a 165 if( (nr < 0) && (nr != NSAPI_ERROR_WOULD_BLOCK) ) break;
Bosko Lekovic 18:5ee53205451a 166 if( !k ) break;
Bosko Lekovic 18:5ee53205451a 167
bosko001 2:45b351b4fc2a 168 if(n>0)
bosko001 2:45b351b4fc2a 169 {
bosko001 7:7ea932eb31f6 170
bosko001 7:7ea932eb31f6 171 //u inbuff-u je http paket
Bosko Lekovic 18:5ee53205451a 172 printf("tcpServThread: primljeno = %d bajtova \n\r",n);
bosko001 11:2cc7e6f06cff 173 fflush(stdout);
bosko001 7:7ea932eb31f6 174
bosko001 7:7ea932eb31f6 175 inbuff[n]=0;
bosko001 7:7ea932eb31f6 176 //raspakivanje sadrzaja, a ako je POST preuzimanje parametara
Bosko Lekovic 18:5ee53205451a 177 char ** strniz = gpc_html->doHTTP_freereturn( inbuff);
Bosko Lekovic 18:5ee53205451a 178 char *type = strniz[0];
Bosko Lekovic 18:5ee53205451a 179 char *description = strniz[1];
Bosko Lekovic 18:5ee53205451a 180 if( (!strcmp( type, "GET") && !strcmp( description, "HTTP")) || !strcmp( type, "POST") )
bosko001 7:7ea932eb31f6 181 {
bosko001 7:7ea932eb31f6 182
Bosko Lekovic 18:5ee53205451a 183
Bosko Lekovic 18:5ee53205451a 184
bosko001 7:7ea932eb31f6 185 // putHTMLintoFLASH( gpc_html );
bosko001 7:7ea932eb31f6 186 //putFLASHintoHTML( gpc_html );
bosko001 7:7ea932eb31f6 187
Bosko Lekovic 18:5ee53205451a 188 if(!strcmp( type, "POST") )
bosko001 7:7ea932eb31f6 189 {
bosko001 9:893843262a1f 190
Bosko Lekovic 18:5ee53205451a 191 tcpServerBlock++;
Bosko Lekovic 18:5ee53205451a 192 printf("tcpServThread: ___stop komunikacije\n\r");
Bosko Lekovic 18:5ee53205451a 193
Bosko Lekovic 18:5ee53205451a 194 // scomRxThreadCancel();
bosko001 19:7bdb1a10043f 195 // tcpRxThreadsCancel();
Bosko Lekovic 18:5ee53205451a 196 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 197 // udpRxThreadsCancel();
Bosko Lekovic 18:5ee53205451a 198 // wait(0.1);
Bosko Lekovic 18:5ee53205451a 199 gpc_html->htmlPOST_fun( ); // setovanje parametara u HTML format
Bosko Lekovic 18:5ee53205451a 200 putHTMLintoFLASH( gpc_html ); // upis parametara iz HTMLa u FLASH
bosko001 19:7bdb1a10043f 201
bosko001 19:7bdb1a10043f 202 //restart_TcpClientsConnection(1);
bosko001 19:7bdb1a10043f 203 restart_TcpClients();
bosko001 19:7bdb1a10043f 204
Bosko Lekovic 18:5ee53205451a 205 // vratiti podatke iz Flasha u HTML !!!!!!
Bosko Lekovic 18:5ee53205451a 206 // ethRestart( );
Bosko Lekovic 18:5ee53205451a 207 // udpRxThreadsRiseUp( );
Bosko Lekovic 18:5ee53205451a 208 // scomRxThreadRiseUp( );
bosko001 19:7bdb1a10043f 209 // tcpRxThreadsRiseUp( );
Bosko Lekovic 18:5ee53205451a 210
Bosko Lekovic 18:5ee53205451a 211 restart = 1;
Bosko Lekovic 18:5ee53205451a 212 }
bosko001 11:2cc7e6f06cff 213
bosko001 7:7ea932eb31f6 214 // ODGOVOR - slanje odgovora klijentu
bosko001 7:7ea932eb31f6 215
bosko001 7:7ea932eb31f6 216 p_soc->set_timeout(-1); // blocking
bosko001 7:7ea932eb31f6 217 // slanje HTTP headera
Bosko Lekovic 18:5ee53205451a 218 // if(strcmp( type, "POST") )
Bosko Lekovic 18:5ee53205451a 219 {
bosko001 7:7ea932eb31f6 220 n = p_soc->send(sendHTTPheader, strlen(sendHTTPheader));
Bosko Lekovic 18:5ee53205451a 221 printf("tcpServThread: predaja HTTPheader: posato bajtova = %d, %d \n\r", n, strlen(sendHTTPheader));
bosko001 7:7ea932eb31f6 222
bosko001 7:7ea932eb31f6 223 // putFLASHintoHTML( gpc_html ); // preuzimanje podataka iz flasha u html
bosko001 7:7ea932eb31f6 224
Bosko Lekovic 18:5ee53205451a 225 auto html_string = gpc_html->getHTML_free( ); // konvertovanje HTMLa u niz
Bosko Lekovic 18:5ee53205451a 226 n = p_soc->send(html_string, strlen(html_string)); // slanje HTML-a
Bosko Lekovic 18:5ee53205451a 227 printf("tcpServThread: PREDAJA dataBuffer: posato bajtova = %d, %d \n\r", n, strlen(html_string));
Bosko Lekovic 18:5ee53205451a 228 free(html_string); // oslobadjanje memorije HTML niza
Bosko Lekovic 18:5ee53205451a 229 }
bosko001 7:7ea932eb31f6 230 }
Bosko Lekovic 18:5ee53205451a 231 free(type);
Bosko Lekovic 18:5ee53205451a 232 free( description);
Bosko Lekovic 18:5ee53205451a 233 free(strniz);
bosko001 7:7ea932eb31f6 234 }
bosko001 2:45b351b4fc2a 235
Bosko Lekovic 18:5ee53205451a 236
bosko001 11:2cc7e6f06cff 237 //!!! p_soc->close(); // zatvaranje socketa
bosko001 11:2cc7e6f06cff 238 // if(n<0)
Bosko Lekovic 18:5ee53205451a 239 {printf("tcpServThread: BREAK posle predaje\n\r"); break;}
bosko001 11:2cc7e6f06cff 240
bosko001 11:2cc7e6f06cff 241 } // end while
bosko001 11:2cc7e6f06cff 242
Bosko Lekovic 18:5ee53205451a 243 {printf("tcpServThread: zatvaranje socketa\n\r"); fflush(stdout);}
Bosko Lekovic 18:5ee53205451a 244
Bosko Lekovic 18:5ee53205451a 245 p_soc->close(); // zatvaranje socketa
Bosko Lekovic 18:5ee53205451a 246
Bosko Lekovic 18:5ee53205451a 247 printf("tcpServThread: restart = %d\n\r",restart);
Bosko Lekovic 18:5ee53205451a 248 if( restart )
Bosko Lekovic 18:5ee53205451a 249 {
Bosko Lekovic 18:5ee53205451a 250 printf("tcpServThread:___restart komunikacije\n\r");
Bosko Lekovic 18:5ee53205451a 251 // ethRestart( );
Bosko Lekovic 18:5ee53205451a 252 // // udpRxThreadsRiseUp( );
Bosko Lekovic 18:5ee53205451a 253 // tcpRxThreadsRiseUp( );
Bosko Lekovic 18:5ee53205451a 254 // // scomRxThreadRestart( );
Bosko Lekovic 18:5ee53205451a 255 restart = 0;
Bosko Lekovic 18:5ee53205451a 256 }
Bosko Lekovic 18:5ee53205451a 257
Bosko Lekovic 18:5ee53205451a 258 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 259
Bosko Lekovic 18:5ee53205451a 260 {printf("tcpServThread: zatvaranje thread-a\n\r"); fflush(stdout);}
Bosko Lekovic 18:5ee53205451a 261 // zatvaranje thread-a
Bosko Lekovic 18:5ee53205451a 262 gp_thr = ps_thp->p_Thread;// poslati pointer da se delete-uje
bosko001 11:2cc7e6f06cff 263
Bosko Lekovic 18:5ee53205451a 264 {printf("tcpServThread: brisanje parametara\n\r"); fflush(stdout);}
Bosko Lekovic 18:5ee53205451a 265 delete ps_thp; // relokacija ps_thp
Bosko Lekovic 18:5ee53205451a 266
Bosko Lekovic 18:5ee53205451a 267 bws = 0;
Bosko Lekovic 18:5ee53205451a 268 //m.unlock();
Bosko Lekovic 18:5ee53205451a 269 if( tcpServerBlock) {tcpServerBlock--;printf("tcpServThread: tcpServerBlock=%d\n\r", tcpServerBlock);}
Bosko Lekovic 18:5ee53205451a 270
bosko001 2:45b351b4fc2a 271 }
bosko001 2:45b351b4fc2a 272