nova verzija tcp+udp->serial com

Committer:
Bosko Lekovic
Date:
Sun May 17 17:16:03 2020 +0200
Revision:
18:5ee53205451a
Parent:
17:74c41e3f10b8
Child:
19:7bdb1a10043f
udp i serial zakomentarisani

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