void OPCEthernet::processOPCCommands() { client = internal_ethernet_server->available(); if (client) { boolean currentLineIsBlank = true; byte s = 0; boolean responsed = false; while (!responsed && client.connected()) { if (client.available()) { char c = client.read(); if (c == '\n' && currentLineIsBlank) { processClientCommand(); responsed = true; } else if (c == '\n') { currentLineIsBlank = true; } else if (c != '\r') { currentLineIsBlank = false; switch (s) { case 0 : if (c == 'G') s++; break; case 1 : if (c == 'E') s++; else s = 0; break; case 2 : if (c == 'T') s++; else s = 0; break; case 3 : if (c == ' ') s++; else s = 0; break; case 4 : if (c == '/') { s++; bufPos = 0;} else s = 0; break; case 5 : if (c != ' ') { buffer[bufPos++] = c; buffer[bufPos] = '\0'; } else s = 0; } } } } delay(1); // Espera para dar tiempo al navegador a recibir los datos. client.stop(); // Cierra la conexión. } }
// The nonsecure server thread static void tcp_server_nonsecure_thread_main(wiced_thread_arg_t arg) { wiced_result_t result; wiced_tcp_stream_t stream; // The TCP stream wiced_tcp_socket_t socket; uint8_t rbuffer[MAX_LEGAL_MSG]; char returnMessage[128]; // better use less than 128 bytes // setup the server by creating the socket and hooking it to the correct TCP Port result = wiced_tcp_create_socket(&socket, INTERFACE); if(WICED_SUCCESS != result) { WPRINT_APP_INFO(("Create socket failed\n")); return; // this is a bad outcome } wiced_tcp_stream_init(&stream,&socket); if(WICED_SUCCESS != result) { WPRINT_APP_INFO(("Init stream failed\n")); return; // this is a bad outcome } result = wiced_tcp_listen( &socket, TCP_SERVER_NONSECURE_LISTEN_PORT ); if(WICED_SUCCESS != result) { WPRINT_APP_INFO(("Listen socket failed\n")); return; } while (1 ) { result = wiced_tcp_accept( &socket ); // this halts the thread until there is a connection if(result != WICED_SUCCESS) // this occurs if the accept times out continue; nonsecureConnectionCount += 1; /// Figure out which client is talking to us... and on which port wiced_ip_address_t peerAddress; uint16_t peerPort; wiced_tcp_server_peer(&socket,&peerAddress,&peerPort); uint32_t dataReadCount; wiced_tcp_stream_read_with_count(&stream,&rbuffer,MAX_LEGAL_MSG,100,&dataReadCount); // timeout in 100 ms processClientCommand(rbuffer, dataReadCount ,returnMessage); displayResult(peerAddress,peerPort,returnMessage); // send response and close things up wiced_tcp_stream_write(&stream,returnMessage,strlen(returnMessage)); wiced_tcp_stream_flush(&stream); wiced_tcp_disconnect(&socket); // disconnect the connection wiced_tcp_stream_deinit(&stream); // clear the stream if any crap left wiced_tcp_stream_init(&stream,&socket); // setup for next connection } }
static void tcp_server_thread_main(wiced_thread_arg_t arg) { wiced_bool_t wwepSecurity = (wiced_bool_t)arg; wiced_result_t result; wiced_tcp_stream_t stream; // The TCP stream wiced_tcp_socket_t socket; platform_dct_security_t *dct_security; wiced_tls_identity_t tls_identity; wiced_tls_context_t tls_context; uint8_t rbuffer[MAX_LEGAL_MSG]; char returnMessage[128]; // better use less than 128 bytes // setup the server by creating the socket and hooking it to the correct TCP Port result = wiced_tcp_create_socket(&socket, INTERFACE); if(WICED_SUCCESS != result) { WPRINT_APP_INFO(("Create socket failed\n")); return; // this is a bad outcome } if(wwepSecurity == WICED_TRUE) { WPRINT_APP_INFO(("Starting secure\n")); } else { WPRINT_APP_INFO(("Starting non-secure\n")); } result = wiced_tcp_listen( &socket, (wwepSecurity == WICED_TRUE)?TCP_SERVER_SECURE_LISTEN_PORT:TCP_SERVER_NONSECURE_LISTEN_PORT ); if(WICED_SUCCESS != result) { WPRINT_APP_INFO(("Listen socket failed\n")); return; } if(wwepSecurity == WICED_TRUE) { /* Lock the DCT to allow us to access the certificate and key */ WPRINT_APP_INFO(( "Read the certificate Key from DCT\n" )); result = wiced_dct_read_lock( (void**) &dct_security, WICED_FALSE, DCT_SECURITY_SECTION, 0, sizeof( *dct_security ) ); if ( result != WICED_SUCCESS ) { WPRINT_APP_INFO(("Unable to lock DCT to read certificate\n")); return; } /* Setup TLS identity */ result = wiced_tls_init_identity( &tls_identity, dct_security->private_key, strlen( dct_security->private_key ), (uint8_t*) dct_security->certificate, strlen( dct_security->certificate ) ); if ( result != WICED_SUCCESS ) { WPRINT_APP_INFO(( "Unable to initialize TLS identity. Error = [%d]\n", result )); return; } } else { wiced_tcp_stream_init(&stream,&socket); if(WICED_SUCCESS != result) { WPRINT_APP_INFO(("Init stream failed\n")); return; // this is a bad outcome } } while (1 ) { if(wwepSecurity == WICED_TRUE) { result = wiced_tls_init_context( &tls_context, &tls_identity, NULL ); if(result != WICED_SUCCESS) { WPRINT_APP_INFO(("Init context failed %d",result)); return; } result = wiced_tcp_enable_tls(&socket,&tls_context); if(result != WICED_SUCCESS) { WPRINT_APP_INFO(("Enabling TLS failed %d",result)); return; } wiced_tcp_stream_init(&stream,&socket); if(WICED_SUCCESS != result) { WPRINT_APP_INFO(("Init stream failed\n")); return; // this is a bad outcome } } result = wiced_tcp_accept( &socket ); // this halts the thread until there is a connection if(result != WICED_SUCCESS) // this occurs if the accept times out continue; if(wwepSecurity == WICED_TRUE) secureConnectionCount += 1; else nonsecureConnectionCount += 1; /// Figure out which client is talking to us... and on which port wiced_ip_address_t peerAddress; uint16_t peerPort; wiced_tcp_server_peer(&socket,&peerAddress,&peerPort); uint32_t dataReadCount; wiced_tcp_stream_read_with_count(&stream,&rbuffer,MAX_LEGAL_MSG,100,&dataReadCount); // timeout in 100 ms processClientCommand(rbuffer, dataReadCount ,returnMessage); displayResult(peerAddress,peerPort,returnMessage); // send response and close things up wiced_tcp_stream_write(&stream,returnMessage,strlen(returnMessage)); wiced_tcp_stream_flush(&stream); wiced_tcp_disconnect(&socket); // disconnect the connection if(wwepSecurity == WICED_TRUE) { wiced_tls_deinit_context(&tls_context); } wiced_tcp_stream_deinit(&stream); // clear the stream if any crap left wiced_tcp_stream_init(&stream,&socket); // setup for next connection } }