nova proba

Committer:
Bosko Lekovic
Date:
Sun Jan 17 23:03:43 2021 +0100
Revision:
27:0772451fb880
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bosko Lekovic 27:0772451fb880 1 #include "mbed.h"
Bosko Lekovic 27:0772451fb880 2 //#include "string.h"
Bosko Lekovic 27:0772451fb880 3
Bosko Lekovic 27:0772451fb880 4 #include "EthernetSetup.h"
Bosko Lekovic 27:0772451fb880 5 #include "doHTML.h"
Bosko Lekovic 27:0772451fb880 6 #include "dataFlash.h"
Bosko Lekovic 27:0772451fb880 7
Bosko Lekovic 27:0772451fb880 8 #include "TcpServer.h"
Bosko Lekovic 27:0772451fb880 9 #include "SerialCom.h"
Bosko Lekovic 27:0772451fb880 10
Bosko Lekovic 27:0772451fb880 11 #include <iterator>
Bosko Lekovic 27:0772451fb880 12 #include <list>
Bosko Lekovic 27:0772451fb880 13
Bosko Lekovic 27:0772451fb880 14
Bosko Lekovic 27:0772451fb880 15
Bosko Lekovic 27:0772451fb880 16 extern EthernetInterface *gp_eth;
Bosko Lekovic 27:0772451fb880 17 extern C_HTMLparse * gpc_html;
Bosko Lekovic 27:0772451fb880 18
Bosko Lekovic 27:0772451fb880 19
Bosko Lekovic 27:0772451fb880 20
Bosko Lekovic 27:0772451fb880 21 /* globalni pointer na tcpServer RX Thread za parametarizaciju */
Bosko Lekovic 27:0772451fb880 22 static Thread *gp_tcpServerThread = NULL; //new Thread;
Bosko Lekovic 27:0772451fb880 23 static void tcpServerRx_fun( void );
Bosko Lekovic 27:0772451fb880 24
Bosko Lekovic 27:0772451fb880 25 // char remote_ip[16];
Bosko Lekovic 27:0772451fb880 26 int port;
Bosko Lekovic 27:0772451fb880 27
Bosko Lekovic 27:0772451fb880 28 static TCPSocket *gp_tcpSocket=NULL; /* socket za prijem zahteva za konekciju */
Bosko Lekovic 27:0772451fb880 29 static Thread *gp_thr = NULL; /* pointer gde se smesta thread za brisanje */
Bosko Lekovic 27:0772451fb880 30
Bosko Lekovic 27:0772451fb880 31
Bosko Lekovic 27:0772451fb880 32
Bosko Lekovic 27:0772451fb880 33 typedef void(*PF_PSTP)(struct SThreadParam *); // ne koristi se
Bosko Lekovic 27:0772451fb880 34
Bosko Lekovic 27:0772451fb880 35 // parametri thread-a za uspostavljenje konekcije
Bosko Lekovic 27:0772451fb880 36 struct SThreadParam
Bosko Lekovic 27:0772451fb880 37 {
Bosko Lekovic 27:0772451fb880 38 Thread *p_Thread; // sopstveni pointer
Bosko Lekovic 27:0772451fb880 39 void * p_param; // pointer za parametre thread-a, u ovom slucaju na socket koji je accept-van
Bosko Lekovic 27:0772451fb880 40 } ;
Bosko Lekovic 27:0772451fb880 41
Bosko Lekovic 27:0772451fb880 42 struct SThreadParam s_ThrPrm, *gps_ThPrm=&s_ThrPrm;
Bosko Lekovic 27:0772451fb880 43 list<struct SThreadParam *> lps_tp;
Bosko Lekovic 27:0772451fb880 44 void showlist( list<struct SThreadParam*> ls )
Bosko Lekovic 27:0772451fb880 45 {
Bosko Lekovic 27:0772451fb880 46 printf("socket list: ");
Bosko Lekovic 27:0772451fb880 47 for( auto it=ls.begin(); it!=ls.end(); it++)
Bosko Lekovic 27:0772451fb880 48 printf(" %d ", (int)(*it)->p_param );
Bosko Lekovic 27:0772451fb880 49 printf("\n");
Bosko Lekovic 27:0772451fb880 50 }
Bosko Lekovic 27:0772451fb880 51
Bosko Lekovic 27:0772451fb880 52
Bosko Lekovic 27:0772451fb880 53 void set_thp( struct SThreadParam * p)
Bosko Lekovic 27:0772451fb880 54 {
Bosko Lekovic 27:0772451fb880 55 gps_ThPrm->p_param = p->p_param;
Bosko Lekovic 27:0772451fb880 56 gps_ThPrm->p_Thread = p->p_Thread;
Bosko Lekovic 27:0772451fb880 57 lps_tp.push_front(p);
Bosko Lekovic 27:0772451fb880 58 showlist(lps_tp);
Bosko Lekovic 27:0772451fb880 59 }
Bosko Lekovic 27:0772451fb880 60 void del_thp( struct SThreadParam * p )
Bosko Lekovic 27:0772451fb880 61 {
Bosko Lekovic 27:0772451fb880 62 gps_ThPrm->p_param= NULL;
Bosko Lekovic 27:0772451fb880 63 gps_ThPrm->p_Thread = NULL;
Bosko Lekovic 27:0772451fb880 64 lps_tp.remove(p);
Bosko Lekovic 27:0772451fb880 65 showlist(lps_tp);
Bosko Lekovic 27:0772451fb880 66 }
Bosko Lekovic 27:0772451fb880 67
Bosko Lekovic 27:0772451fb880 68 bool isAny_thp( void )
Bosko Lekovic 27:0772451fb880 69 {
Bosko Lekovic 27:0772451fb880 70 if( gps_ThPrm->p_param ) return true;
Bosko Lekovic 27:0772451fb880 71 return false;
Bosko Lekovic 27:0772451fb880 72
Bosko Lekovic 27:0772451fb880 73 }
Bosko Lekovic 27:0772451fb880 74
Bosko Lekovic 27:0772451fb880 75
Bosko Lekovic 27:0772451fb880 76 /****************** inicijalizacija u mainu ************************/
Bosko Lekovic 27:0772451fb880 77 static char* html_string;
Bosko Lekovic 27:0772451fb880 78
Bosko Lekovic 27:0772451fb880 79 void start_TcpServer( void )
Bosko Lekovic 27:0772451fb880 80 {
Bosko Lekovic 27:0772451fb880 81 if( test_num(gpc_html->get_myTcpPort())) port = atoi( gpc_html->get_myTcpPort( ));
Bosko Lekovic 27:0772451fb880 82 else port = 11111;
Bosko Lekovic 27:0772451fb880 83
Bosko Lekovic 27:0772451fb880 84 //del_thp( NULL );
Bosko Lekovic 27:0772451fb880 85
Bosko Lekovic 27:0772451fb880 86 if( gp_tcpServerThread ) delete gp_tcpServerThread;
Bosko Lekovic 27:0772451fb880 87 gp_tcpServerThread = new Thread();
Bosko Lekovic 27:0772451fb880 88 gp_tcpServerThread->start(tcpServerRx_fun);
Bosko Lekovic 27:0772451fb880 89
Bosko Lekovic 27:0772451fb880 90 }
Bosko Lekovic 27:0772451fb880 91
Bosko Lekovic 27:0772451fb880 92
Bosko Lekovic 27:0772451fb880 93 void sentOnTcpServers( char *buffer, int val )
Bosko Lekovic 27:0772451fb880 94 {
Bosko Lekovic 27:0772451fb880 95 // if( isAny_thp() )
Bosko Lekovic 27:0772451fb880 96 // {
Bosko Lekovic 27:0772451fb880 97 // ((TCPSocket *)(gps_ThPrm->p_param))->send(buffer, val);
Bosko Lekovic 27:0772451fb880 98
Bosko Lekovic 27:0772451fb880 99 // }
Bosko Lekovic 27:0772451fb880 100
Bosko Lekovic 27:0772451fb880 101 for( auto it=lps_tp.begin(); it!=lps_tp.end(); it++)
Bosko Lekovic 27:0772451fb880 102 ((TCPSocket *)((*it)->p_param))->send(buffer, val);
Bosko Lekovic 27:0772451fb880 103
Bosko Lekovic 27:0772451fb880 104
Bosko Lekovic 27:0772451fb880 105 }
Bosko Lekovic 27:0772451fb880 106
Bosko Lekovic 27:0772451fb880 107
Bosko Lekovic 27:0772451fb880 108
Bosko Lekovic 27:0772451fb880 109
Bosko Lekovic 27:0772451fb880 110 /***************** u main-u *************************/
Bosko Lekovic 27:0772451fb880 111 // zatvaranje thread-a nakon http razmene
Bosko Lekovic 27:0772451fb880 112
Bosko Lekovic 27:0772451fb880 113 void finish_TcpServerInstance( void )
Bosko Lekovic 27:0772451fb880 114 {
Bosko Lekovic 27:0772451fb880 115 if(gp_thr)
Bosko Lekovic 27:0772451fb880 116 {
Bosko Lekovic 27:0772451fb880 117 printf("Brisanje thread-a %d\n\r", gp_thr); fflush(stdout);
Bosko Lekovic 27:0772451fb880 118 gp_thr->join();
Bosko Lekovic 27:0772451fb880 119
Bosko Lekovic 27:0772451fb880 120 delete gp_thr;
Bosko Lekovic 27:0772451fb880 121 gp_thr = NULL;
Bosko Lekovic 27:0772451fb880 122 printf("Obrisan thread %d\n\r", gp_thr);
Bosko Lekovic 27:0772451fb880 123 // s.release();
Bosko Lekovic 27:0772451fb880 124 }
Bosko Lekovic 27:0772451fb880 125 }
Bosko Lekovic 27:0772451fb880 126
Bosko Lekovic 27:0772451fb880 127
Bosko Lekovic 27:0772451fb880 128 /************** Trajni TCPServer thread *********************************/
Bosko Lekovic 27:0772451fb880 129
Bosko Lekovic 27:0772451fb880 130
Bosko Lekovic 27:0772451fb880 131
Bosko Lekovic 27:0772451fb880 132 void serverInstance_fun( SThreadParam *ps_thr );
Bosko Lekovic 27:0772451fb880 133
Bosko Lekovic 27:0772451fb880 134 static void tcpServerRx_fun( void )
Bosko Lekovic 27:0772451fb880 135 {
Bosko Lekovic 27:0772451fb880 136 while( true )
Bosko Lekovic 27:0772451fb880 137 {
Bosko Lekovic 27:0772451fb880 138 printf("____Otvoren TCP server rx thread____ \n\r");
Bosko Lekovic 27:0772451fb880 139 if( gp_tcpSocket ) {delete gp_tcpSocket; gp_tcpSocket = NULL; } /* ima smisla ako se thread nasilno zatvara */
Bosko Lekovic 27:0772451fb880 140
Bosko Lekovic 27:0772451fb880 141 gp_tcpSocket = new TCPSocket;
Bosko Lekovic 27:0772451fb880 142 if(gp_tcpSocket->open( gp_eth ) != 0) break;
Bosko Lekovic 27:0772451fb880 143 gp_tcpSocket->bind( port );
Bosko Lekovic 27:0772451fb880 144
Bosko Lekovic 27:0772451fb880 145 if(gp_tcpSocket->listen(2) != 0 )
Bosko Lekovic 27:0772451fb880 146 { printf("greska listenovanja na socketu\n\r"); break;}
Bosko Lekovic 27:0772451fb880 147
Bosko Lekovic 27:0772451fb880 148 TCPSocket *p_soc;
Bosko Lekovic 27:0772451fb880 149 nsapi_error_t err=NULL;
Bosko Lekovic 27:0772451fb880 150 int nos = 0;
Bosko Lekovic 27:0772451fb880 151
Bosko Lekovic 27:0772451fb880 152 while( (p_soc = gp_tcpSocket->accept( &err)) )
Bosko Lekovic 27:0772451fb880 153 {
Bosko Lekovic 27:0772451fb880 154
Bosko Lekovic 27:0772451fb880 155 Thread *p_thr = new Thread( );
Bosko Lekovic 27:0772451fb880 156 struct SThreadParam *ps_thp = new struct SThreadParam; // alokacija ps_thp
Bosko Lekovic 27:0772451fb880 157 ps_thp->p_param = (void*)p_soc;
Bosko Lekovic 27:0772451fb880 158 ps_thp->p_Thread = p_thr;
Bosko Lekovic 27:0772451fb880 159 set_thp(ps_thp);
Bosko Lekovic 27:0772451fb880 160
Bosko Lekovic 27:0772451fb880 161 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 27:0772451fb880 162 p_thr->start( callback( serverInstance_fun, ps_thp ));
Bosko Lekovic 27:0772451fb880 163
Bosko Lekovic 27:0772451fb880 164
Bosko Lekovic 27:0772451fb880 165 }
Bosko Lekovic 27:0772451fb880 166 printf("TCP WEB SERVER: Ispad iz accept while!!! socket = %d err = %d\n\r", p_soc, err);fflush(stdout);
Bosko Lekovic 27:0772451fb880 167 }
Bosko Lekovic 27:0772451fb880 168 printf("TCP WEB SERVER: Zaustavljen WEB server \n\r");fflush(stdout);
Bosko Lekovic 27:0772451fb880 169
Bosko Lekovic 27:0772451fb880 170 }
Bosko Lekovic 27:0772451fb880 171
Bosko Lekovic 27:0772451fb880 172
Bosko Lekovic 27:0772451fb880 173
Bosko Lekovic 27:0772451fb880 174
Bosko Lekovic 27:0772451fb880 175 /***************** privremeni TCPServer thread za klijenta ******************/
Bosko Lekovic 27:0772451fb880 176 // thread je jednokratan - primi http telegram i odgovori - ako je GET ili POST upit
Bosko Lekovic 27:0772451fb880 177
Bosko Lekovic 27:0772451fb880 178 void serverInstance_fun( struct SThreadParam *ps_thp )
Bosko Lekovic 27:0772451fb880 179 {
Bosko Lekovic 27:0772451fb880 180 TCPSocket *p_soc = (TCPSocket *)ps_thp->p_param;
Bosko Lekovic 27:0772451fb880 181
Bosko Lekovic 27:0772451fb880 182 char inbuff[1500];
Bosko Lekovic 27:0772451fb880 183
Bosko Lekovic 27:0772451fb880 184 int n;
Bosko Lekovic 27:0772451fb880 185 // printf("tcpServThread: Otvoren socket %d\n\r",(int)p_soc);
Bosko Lekovic 27:0772451fb880 186
Bosko Lekovic 27:0772451fb880 187 // printf("tcpServThread: cekam prijem od klijenta \n\r");
Bosko Lekovic 27:0772451fb880 188 SocketAddress sockAddr;
Bosko Lekovic 27:0772451fb880 189 p_soc->getpeername( &sockAddr );
Bosko Lekovic 27:0772451fb880 190
Bosko Lekovic 27:0772451fb880 191 // p_soc->set_timeout(10); // non-blocking pruzimanje bajtova na 10ms
Bosko Lekovic 27:0772451fb880 192 n=0;
Bosko Lekovic 27:0772451fb880 193 while(true)
Bosko Lekovic 27:0772451fb880 194 {
Bosko Lekovic 27:0772451fb880 195
Bosko Lekovic 27:0772451fb880 196 n = p_soc->recv( (inbuff), sizeof(inbuff)); // razmotriti obradu nr=0 !!!
Bosko Lekovic 27:0772451fb880 197
Bosko Lekovic 27:0772451fb880 198 if(n>0)
Bosko Lekovic 27:0772451fb880 199 {
Bosko Lekovic 27:0772451fb880 200 extern void sendToScom( char *buffer, int val );
Bosko Lekovic 27:0772451fb880 201 sendToScom( inbuff, n);
Bosko Lekovic 27:0772451fb880 202 }
Bosko Lekovic 27:0772451fb880 203 else
Bosko Lekovic 27:0772451fb880 204 {
Bosko Lekovic 27:0772451fb880 205 // printf("\n\rtcpServer: break na receive err=%d\n\r",n);
Bosko Lekovic 27:0772451fb880 206 break;
Bosko Lekovic 27:0772451fb880 207 }
Bosko Lekovic 27:0772451fb880 208 }
Bosko Lekovic 27:0772451fb880 209
Bosko Lekovic 27:0772451fb880 210 //{printf("tcpServThread: zatvaranje socketa\n\r"); fflush(stdout);}
Bosko Lekovic 27:0772451fb880 211
Bosko Lekovic 27:0772451fb880 212 del_thp(ps_thp);
Bosko Lekovic 27:0772451fb880 213 p_soc->close(); // zatvaranje socketa
Bosko Lekovic 27:0772451fb880 214
Bosko Lekovic 27:0772451fb880 215
Bosko Lekovic 27:0772451fb880 216
Bosko Lekovic 27:0772451fb880 217 //{printf("tcpServThread: zatvaranje thread-a\n\r"); fflush(stdout);}
Bosko Lekovic 27:0772451fb880 218 // zatvaranje thread-a
Bosko Lekovic 27:0772451fb880 219 gp_thr = ps_thp->p_Thread;// poslati pointer da se delete-uje
Bosko Lekovic 27:0772451fb880 220
Bosko Lekovic 27:0772451fb880 221 //{printf("tcpServThread: brisanje parametara\n\r"); fflush(stdout);}
Bosko Lekovic 27:0772451fb880 222 delete ps_thp; // relokacija ps_thp
Bosko Lekovic 27:0772451fb880 223
Bosko Lekovic 27:0772451fb880 224 }
Bosko Lekovic 27:0772451fb880 225