nova proba

Committer:
Bosko Lekovic
Date:
Mon Jan 18 14:46:46 2021 +0100
Revision:
28:49bafc8bb056
Parent:
27:0772451fb880
novije

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 "platform/mbed_thread.h"
Bosko Lekovic 27:0772451fb880 3 #include "clubbing.h"
Bosko Lekovic 27:0772451fb880 4 #include "EthernetSetup.h"
Bosko Lekovic 27:0772451fb880 5 #include "TcpClients.h"
Bosko Lekovic 27:0772451fb880 6 #include "doHTML.h"
Bosko Lekovic 27:0772451fb880 7
Bosko Lekovic 27:0772451fb880 8 extern EthernetInterface *gp_eth;
Bosko Lekovic 27:0772451fb880 9 extern C_HTMLparse * gpc_html;
Bosko Lekovic 27:0772451fb880 10
Bosko Lekovic 27:0772451fb880 11
Bosko Lekovic 27:0772451fb880 12
Bosko Lekovic 27:0772451fb880 13 struct SThreadParam
Bosko Lekovic 27:0772451fb880 14 {
Bosko Lekovic 27:0772451fb880 15 Thread *p_Thread; // sopstveni pointer
Bosko Lekovic 27:0772451fb880 16 void * p_param; // pointer za parametre thread-a, u ovom slucaju na socket koji je accept-van
Bosko Lekovic 27:0772451fb880 17 };
Bosko Lekovic 27:0772451fb880 18
Bosko Lekovic 27:0772451fb880 19 volatile static struct S_EthernetAddress_Remote
Bosko Lekovic 27:0772451fb880 20 {
Bosko Lekovic 27:0772451fb880 21 char ip[16];
Bosko Lekovic 27:0772451fb880 22 int port;
Bosko Lekovic 27:0772451fb880 23 int validy;
Bosko Lekovic 27:0772451fb880 24 TCPSocket *p_tcpSocket;
Bosko Lekovic 27:0772451fb880 25 Thread *p_tcpThread;
Bosko Lekovic 27:0772451fb880 26 int txError;
Bosko Lekovic 27:0772451fb880 27 int rxStatus;
Bosko Lekovic 27:0772451fb880 28 } s_EthAddRemote[NO_TCP] , *ps_ear[NO_TCP];//
Bosko Lekovic 27:0772451fb880 29
Bosko Lekovic 27:0772451fb880 30 //static TCPSocket *gp_tcpSocket[NO_TCP] = {NULL};
Bosko Lekovic 27:0772451fb880 31 //static Thread *gp_tcpRxThread[NO_TCP] = {NULL};
Bosko Lekovic 27:0772451fb880 32
Bosko Lekovic 27:0772451fb880 33
Bosko Lekovic 27:0772451fb880 34 static int myTcpPort = 11000;
Bosko Lekovic 27:0772451fb880 35
Bosko Lekovic 27:0772451fb880 36 void tcpRx_fun( int* par_i );
Bosko Lekovic 27:0772451fb880 37
Bosko Lekovic 27:0772451fb880 38 /************* inicijalizacija parametara **********************/
Bosko Lekovic 27:0772451fb880 39
Bosko Lekovic 27:0772451fb880 40 int get_rxStatus( int i )
Bosko Lekovic 27:0772451fb880 41 {
Bosko Lekovic 27:0772451fb880 42 return s_EthAddRemote[i].rxStatus;
Bosko Lekovic 27:0772451fb880 43 }
Bosko Lekovic 27:0772451fb880 44 int get_rxValidy( int i )
Bosko Lekovic 27:0772451fb880 45 {
Bosko Lekovic 27:0772451fb880 46 return s_EthAddRemote[i].validy;
Bosko Lekovic 27:0772451fb880 47 }
Bosko Lekovic 27:0772451fb880 48
Bosko Lekovic 27:0772451fb880 49 void init_tcp_param( void )
Bosko Lekovic 27:0772451fb880 50 {
Bosko Lekovic 27:0772451fb880 51 // printf("my tcp port = %s\n\r", gpc_html->get_myTcpPort( )); F
Bosko Lekovic 27:0772451fb880 52
Bosko Lekovic 27:0772451fb880 53 if( test_num(gpc_html->get_myTcpPort( ))) myTcpPort = atoi( gpc_html->get_myTcpPort( ));
Bosko Lekovic 27:0772451fb880 54 printf( " myTcpPort = %d\n\r", myTcpPort);
Bosko Lekovic 27:0772451fb880 55
Bosko Lekovic 27:0772451fb880 56 #if NO_TCP>0
Bosko Lekovic 27:0772451fb880 57
Bosko Lekovic 27:0772451fb880 58 for( int i=1; i<= NO_TCP; i++)
Bosko Lekovic 27:0772451fb880 59 {
Bosko Lekovic 27:0772451fb880 60 const char *sp, *sip=gpc_html->get_TcpIP(i);
Bosko Lekovic 27:0772451fb880 61
Bosko Lekovic 27:0772451fb880 62 // treba popraviti, IP(4) i IP(5) su opseg adresa
Bosko Lekovic 27:0772451fb880 63 if( i < 5 ) sp = gpc_html->get_TcpPort(i);
Bosko Lekovic 27:0772451fb880 64 else
Bosko Lekovic 27:0772451fb880 65 {
Bosko Lekovic 27:0772451fb880 66 sp = gpc_html->get_TcpPort(4);
Bosko Lekovic 27:0772451fb880 67 }
Bosko Lekovic 27:0772451fb880 68
Bosko Lekovic 27:0772451fb880 69 ps_ear[i-1] = &(s_EthAddRemote[i-1]);
Bosko Lekovic 27:0772451fb880 70
Bosko Lekovic 27:0772451fb880 71 if( test_ip( sip ) && test_num( sp ) )
Bosko Lekovic 27:0772451fb880 72 {
Bosko Lekovic 27:0772451fb880 73 strncpy( (char *)ps_ear[i-1]->ip, sip, 16 );
Bosko Lekovic 27:0772451fb880 74 ps_ear[i-1]->port = atoi( sp );
Bosko Lekovic 27:0772451fb880 75 ps_ear[i-1]->validy = 1;
Bosko Lekovic 27:0772451fb880 76 }
Bosko Lekovic 27:0772451fb880 77 else
Bosko Lekovic 27:0772451fb880 78 {
Bosko Lekovic 27:0772451fb880 79 ps_ear[i-1]->validy = 0;
Bosko Lekovic 27:0772451fb880 80 }
Bosko Lekovic 27:0772451fb880 81
Bosko Lekovic 27:0772451fb880 82 ps_ear[i-1]->rxStatus = 0;
Bosko Lekovic 27:0772451fb880 83 // printf(" TCP IP adresa: %s port = %d validy=%d \n\r", ps_ear[i-1]->ip, ps_ear[i-1]->port, ps_ear[i-1]->validy );
Bosko Lekovic 27:0772451fb880 84
Bosko Lekovic 27:0772451fb880 85 }
Bosko Lekovic 27:0772451fb880 86 #endif
Bosko Lekovic 27:0772451fb880 87 }
Bosko Lekovic 27:0772451fb880 88
Bosko Lekovic 27:0772451fb880 89 //
Bosko Lekovic 27:0772451fb880 90
Bosko Lekovic 27:0772451fb880 91 void start_TcpClients( void )
Bosko Lekovic 27:0772451fb880 92 {
Bosko Lekovic 27:0772451fb880 93 printf( "start tcp clients\n\r");
Bosko Lekovic 27:0772451fb880 94
Bosko Lekovic 27:0772451fb880 95 init_tcp_param( );
Bosko Lekovic 27:0772451fb880 96
Bosko Lekovic 27:0772451fb880 97 for( int i=0; i< NO_TCP; i++)
Bosko Lekovic 27:0772451fb880 98 {
Bosko Lekovic 27:0772451fb880 99 int *p_i = new int;
Bosko Lekovic 27:0772451fb880 100 *p_i = i;
Bosko Lekovic 27:0772451fb880 101 ps_ear[i]->p_tcpThread = new Thread;
Bosko Lekovic 27:0772451fb880 102 ps_ear[i]->p_tcpThread->start(callback( tcpRx_fun, p_i));
Bosko Lekovic 27:0772451fb880 103 //printf(" TCP Clients start: Thread[%d]=%d\n\r", i, ps_ear[i]->p_tcpThread );
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 /***************** Predaja na TCP ****************************/
Bosko Lekovic 27:0772451fb880 109
Bosko Lekovic 27:0772451fb880 110
Bosko Lekovic 27:0772451fb880 111 void sendTcpi( int i, char *buffer, int val )
Bosko Lekovic 27:0772451fb880 112 {
Bosko Lekovic 27:0772451fb880 113 #if NO_TCP>0
Bosko Lekovic 27:0772451fb880 114
Bosko Lekovic 27:0772451fb880 115 if( ps_ear[i]->p_tcpSocket && ps_ear[i]->validy && (ps_ear[i]->rxStatus == 3))
Bosko Lekovic 27:0772451fb880 116 {
Bosko Lekovic 27:0772451fb880 117 ps_ear[i]->txError = ps_ear[i]->p_tcpSocket->send(buffer, val);
Bosko Lekovic 27:0772451fb880 118 // static int i = 0; printf("tcp %d\n\r", i++);
Bosko Lekovic 27:0772451fb880 119 // printf(" TCPpaket ip=%s port=%d val=%d err=%d stat=%d\n\r", ps_ear[i]->ip, ps_ear[i]->port, val, ps_ear[i]->txError, getEthernetStatus( ) );
Bosko Lekovic 27:0772451fb880 120 }
Bosko Lekovic 27:0772451fb880 121 else printf("TCPsocket[%d] == NULL\n\r", i);
Bosko Lekovic 27:0772451fb880 122 #endif
Bosko Lekovic 27:0772451fb880 123 }
Bosko Lekovic 27:0772451fb880 124
Bosko Lekovic 27:0772451fb880 125 void sendToTcp( char *buffer, int val )
Bosko Lekovic 27:0772451fb880 126 {
Bosko Lekovic 27:0772451fb880 127
Bosko Lekovic 27:0772451fb880 128 #if NO_TCP>0
Bosko Lekovic 27:0772451fb880 129 for( int i=0; i< NO_TCP; i++ )
Bosko Lekovic 27:0772451fb880 130 if( ps_ear[i]->validy ) {sendTcpi( i, buffer, val ); ThisThread::yield(); /* thread_sleep_for(200);*/}
Bosko Lekovic 27:0772451fb880 131
Bosko Lekovic 27:0772451fb880 132 #endif
Bosko Lekovic 27:0772451fb880 133 }
Bosko Lekovic 27:0772451fb880 134
Bosko Lekovic 27:0772451fb880 135
Bosko Lekovic 27:0772451fb880 136
Bosko Lekovic 27:0772451fb880 137 /*************** Prijem na TCP socket i predaja na Serial com ***************/
Bosko Lekovic 27:0772451fb880 138
Bosko Lekovic 27:0772451fb880 139 void tcpRx_fun( int* par_i )
Bosko Lekovic 27:0772451fb880 140 {
Bosko Lekovic 27:0772451fb880 141 int i = *par_i;
Bosko Lekovic 27:0772451fb880 142 delete par_i;
Bosko Lekovic 27:0772451fb880 143
Bosko Lekovic 27:0772451fb880 144 char inbuff[1500];
Bosko Lekovic 27:0772451fb880 145 ps_ear[i]->rxStatus = 0;
Bosko Lekovic 27:0772451fb880 146 //printf("tcpClient[%d]: rx thread inicijalizovan \n\r",i);
Bosko Lekovic 27:0772451fb880 147
Bosko Lekovic 27:0772451fb880 148 while( !(ThisThread::flags_get( ) & 4) ) // flag za izlaz iz Thread-a
Bosko Lekovic 27:0772451fb880 149 {
Bosko Lekovic 27:0772451fb880 150 ps_ear[i]->rxStatus=1;
Bosko Lekovic 27:0772451fb880 151 nsapi_error_t err;
Bosko Lekovic 27:0772451fb880 152
Bosko Lekovic 27:0772451fb880 153 if(ps_ear[i]->validy)
Bosko Lekovic 27:0772451fb880 154 {
Bosko Lekovic 27:0772451fb880 155
Bosko Lekovic 27:0772451fb880 156 TCPSocket *p_sock = ps_ear[i]->p_tcpSocket = new TCPSocket;
Bosko Lekovic 27:0772451fb880 157 // printf("\n\rtcpClient[%d]: novi TCP socket %x\n\r", i, p_sock);
Bosko Lekovic 27:0772451fb880 158
Bosko Lekovic 27:0772451fb880 159 while( !(ThisThread::flags_get( ) & 2) ) // fleg za gasenje socket-a
Bosko Lekovic 27:0772451fb880 160 {
Bosko Lekovic 27:0772451fb880 161 ps_ear[i]->rxStatus = 2;
Bosko Lekovic 27:0772451fb880 162 if( (err = p_sock->open( gp_eth )) == NSAPI_ERROR_OK )
Bosko Lekovic 27:0772451fb880 163 {
Bosko Lekovic 27:0772451fb880 164 int port = ps_ear[i]->port;
Bosko Lekovic 27:0772451fb880 165 char *ip = (char*)ps_ear[i]->ip;
Bosko Lekovic 27:0772451fb880 166 p_sock->bind( port );
Bosko Lekovic 27:0772451fb880 167 if( (err = p_sock->connect(ip, port)) == NSAPI_ERROR_OK)
Bosko Lekovic 27:0772451fb880 168 {
Bosko Lekovic 27:0772451fb880 169 printf("tcpClient[%d]:konektovan na ip=%s, port=%d\n\r",i, ip, port);
Bosko Lekovic 27:0772451fb880 170 //!!! p_sock->set_timeout(100);
Bosko Lekovic 27:0772451fb880 171 signed int n=0;
Bosko Lekovic 27:0772451fb880 172 while(!(ThisThread::flags_get( ) & 1) ) // fleg za izlaz iz konekcije
Bosko Lekovic 27:0772451fb880 173 {
Bosko Lekovic 27:0772451fb880 174 ps_ear[i]->rxStatus = 3;
Bosko Lekovic 27:0772451fb880 175 if( (n>=0) )
Bosko Lekovic 27:0772451fb880 176 {
Bosko Lekovic 27:0772451fb880 177 n = p_sock->recv( inbuff, sizeof(inbuff));
Bosko Lekovic 27:0772451fb880 178
Bosko Lekovic 27:0772451fb880 179 if(n>0) { extern void sendToScom( char *buffer, int val ); sendToScom( inbuff, n); }
Bosko Lekovic 27:0772451fb880 180 //!!! else if(n==-3001) n = 0; // nije prava greska, isteklo vreme timeouta
Bosko Lekovic 27:0772451fb880 181 else {printf("\n\rtcpClient[%d]: break na receive err=%d\n\r", i,n); break; }
Bosko Lekovic 27:0772451fb880 182 }
Bosko Lekovic 27:0772451fb880 183
Bosko Lekovic 27:0772451fb880 184 } //while 1
Bosko Lekovic 27:0772451fb880 185 ps_ear[i]->rxStatus = 2;
Bosko Lekovic 27:0772451fb880 186 ThisThread::flags_clear(1);
Bosko Lekovic 27:0772451fb880 187 //!!! p_sock->set_timeout(-1);
Bosko Lekovic 27:0772451fb880 188 p_sock->close();
Bosko Lekovic 27:0772451fb880 189 } // connected
Bosko Lekovic 27:0772451fb880 190 else printf("tcpClient[%d]: greska konekcije rxThread %d\n\r", i, err);
Bosko Lekovic 27:0772451fb880 191 } // open
Bosko Lekovic 27:0772451fb880 192 else printf("tcpClient[%d]: greska otvaranja soketa err=%d \n\r",i, err);
Bosko Lekovic 27:0772451fb880 193 thread_sleep_for(1000); // dremni pre ponovnog pokusaja konekcije
Bosko Lekovic 27:0772451fb880 194 break; // izlaz iz while 2, brisanje socket-a i ponovno otvaranje
Bosko Lekovic 27:0772451fb880 195 } // while 2
Bosko Lekovic 27:0772451fb880 196 ps_ear[i]->rxStatus = 1;
Bosko Lekovic 27:0772451fb880 197 ThisThread::flags_clear(2);
Bosko Lekovic 27:0772451fb880 198
Bosko Lekovic 27:0772451fb880 199 printf("tcpClient[%d]:___zatvaranje TCP socket= %x\n\r",i, p_sock);
Bosko Lekovic 27:0772451fb880 200 delete p_sock;
Bosko Lekovic 27:0772451fb880 201 p_sock = NULL;
Bosko Lekovic 27:0772451fb880 202 thread_sleep_for(1000); // dremni pre ponovnog konstrukcije socketa
Bosko Lekovic 27:0772451fb880 203
Bosko Lekovic 27:0772451fb880 204 } // if validy
Bosko Lekovic 27:0772451fb880 205
Bosko Lekovic 27:0772451fb880 206
Bosko Lekovic 27:0772451fb880 207 }// while 4
Bosko Lekovic 27:0772451fb880 208 ThisThread::flags_clear(4);
Bosko Lekovic 27:0772451fb880 209 ps_ear[i]->rxStatus = 0;
Bosko Lekovic 27:0772451fb880 210
Bosko Lekovic 27:0772451fb880 211 printf("tcpClient[%d]: izlaz iz rxThread\n\r", i);
Bosko Lekovic 27:0772451fb880 212 fflush(stdout);
Bosko Lekovic 27:0772451fb880 213
Bosko Lekovic 27:0772451fb880 214 }
Bosko Lekovic 27:0772451fb880 215
Bosko Lekovic 27:0772451fb880 216 void restart_TcpClientConnection( int noClient, int restartType )
Bosko Lekovic 27:0772451fb880 217 {
Bosko Lekovic 27:0772451fb880 218 if( (noClient < NO_TCP) && (restartType <= (1+2+4)) )
Bosko Lekovic 27:0772451fb880 219 {
Bosko Lekovic 27:0772451fb880 220 ps_ear[noClient]->p_tcpThread->flags_set(restartType);
Bosko Lekovic 27:0772451fb880 221 if( restartType & 4 )
Bosko Lekovic 27:0772451fb880 222 {
Bosko Lekovic 27:0772451fb880 223 ps_ear[noClient]->p_tcpThread->join();
Bosko Lekovic 27:0772451fb880 224 printf("TCP Client Thread[%d]=%d zavrsen\n\r",noClient, ps_ear[noClient]->p_tcpThread);
Bosko Lekovic 27:0772451fb880 225 }
Bosko Lekovic 27:0772451fb880 226 }
Bosko Lekovic 27:0772451fb880 227 }
Bosko Lekovic 27:0772451fb880 228
Bosko Lekovic 27:0772451fb880 229 void restart_TcpClientsConnection( int restartType )
Bosko Lekovic 27:0772451fb880 230 {
Bosko Lekovic 27:0772451fb880 231 init_tcp_param( );
Bosko Lekovic 27:0772451fb880 232 for(int i=0; i<NO_TCP; i++)
Bosko Lekovic 27:0772451fb880 233 {
Bosko Lekovic 27:0772451fb880 234 if(ps_ear[i]->rxStatus)
Bosko Lekovic 27:0772451fb880 235 restart_TcpClientConnection( i, restartType);
Bosko Lekovic 27:0772451fb880 236 }
Bosko Lekovic 27:0772451fb880 237 }
Bosko Lekovic 27:0772451fb880 238
Bosko Lekovic 27:0772451fb880 239 void restart_TcpClients( void )
Bosko Lekovic 27:0772451fb880 240 {
Bosko Lekovic 27:0772451fb880 241 restart_TcpClientsConnection(7);
Bosko Lekovic 27:0772451fb880 242 start_TcpClients();
Bosko Lekovic 27:0772451fb880 243 }