]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blobdiff - rpp-test-sw/commands/cmd_nc.c
Change license to MIT
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_nc.c
index 517126ab175d8a8151212abe11caf7b1f650c75f..6812e6c2591e613d58d1a92b10235a6309f125a4 100644 (file)
@@ -1,28 +1,36 @@
 /*
- * Copyright (C) 2012-2013 Czech Technical University in Prague
+ * Copyright (C) 2012-2013, 2015 Czech Technical University in Prague
  *
  * Created on: Aug 9, 2013
  *
  * Authors:
  *     - Jan Doležal
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  *
  * File : cmd_lwip.c
  *
  * Abstract:
- *     This file contains commands for LwIP test
+ *      This file contains commands for LwIP test
  *
  */
 
@@ -61,7 +69,7 @@ uint8_t tasks_running = 0, taskNameNum = 0;
 boolean_t closeths; /* variable controling threads closing */
 
 /* argument storing parameters from command line, if started as thread, they are coppied to stack of thread and cmd_do_init_nc() is acknowledged by semaphore */
-struct nc_arg{
+struct nc_arg {
        uint16_t portNO, srcPortNO;
        ip_addr_t remoteIP;
        xSemaphoreHandle args_coppied;
@@ -72,22 +80,24 @@ struct nc_arg{
 
 
 /* nc task */
-void run_nc(void *arg){
-    err_t err = ERR_OK;
+void run_nc(void *arg)
+{
+       err_t err = ERR_OK;
        uint8_t input = 0;
        uint8_t tries;
-    uint32_t index;
-    uint8_t *payload;
+       uint32_t index;
+       uint8_t *payload;
        struct netconn *netconn, *newconn;
-    newconn = NULL;
-    struct netbuf *remoteData = NULL, *localData = NULL;
+
+       newconn = NULL;
+       struct netbuf *remoteData = NULL, *localData = NULL;
        struct pbuf *p;
-    boolean_t ncStop = FALSE;
-    boolean_t flush = FALSE;
-       struct nc_arg *nc_arg = (struct nc_arg *) arg;
-    struct netif *netif = rpp_eth_get_netif(INTERFACE_INSTANCE_NUMBER);
+       boolean_t ncStop = FALSE;
+       boolean_t flush = FALSE;
+       struct nc_arg *nc_arg = (struct nc_arg *)arg;
+       struct netif *netif = rpp_eth_get_netif(INTERFACE_INSTANCE_NUMBER);
 
-    /* fill options from argument */
+       /* fill options from argument */
        uint16_t portNO = nc_arg->portNO, srcPortNO = nc_arg->srcPortNO;
        ip_addr_t remoteIP = nc_arg->remoteIP;
        boolean_t udp = nc_arg->udp, listen = nc_arg->listen, srcPortSpec = nc_arg->srcPortSpec, netLoop = nc_arg->netLoop, test = nc_arg->test;
@@ -99,29 +109,28 @@ void run_nc(void *arg){
 
 
        /* switch off closing of tasks/threads */
-    closeths = FALSE;
+       closeths = FALSE;
 
 
-    /*** make connection ***/
-       while(!closeths){ /* shouldn't get here twice, used only for break which leads us down to unalocating of sources */
+       /*** make connection ***/
+       while (!closeths) { /* shouldn't get here twice, used only for break which leads us down to unalocating of sources */
                /* let nc initiator go */
-               if(thread && /* just in case --> */ nc_arg->args_coppied != NULL && xSemaphoreGive(nc_arg->args_coppied) != pdTRUE)break;
-           /* fill netconn struct */
-               netconn = netconn_new(udp?NETCONN_UDP:NETCONN_TCP);
-               if(netconn == NULL){
+               if (thread && /* just in case --> */ nc_arg->args_coppied != NULL && xSemaphoreGive(nc_arg->args_coppied) != pdTRUE) break;
+               /* fill netconn struct */
+               netconn = netconn_new(udp ? NETCONN_UDP : NETCONN_TCP);
+               if (netconn == NULL) {
                        err = ERR_NETCONN_NEW;
                        break;
                }
 
                tries = 15; /* how many local ports to try */
                /* bind filled netconn with local interface address and source port number */
-               while( tries-- && (err = netconn_bind(netconn, &(netif->ip_addr), srcPortNO) ) == ERR_USE && !closeths){
-                       if(srcPortSpec)break; /* if port was explicitly specified (we shouldn't change user's port number just like that ;) ) */
+               while ( tries-- && (err = netconn_bind(netconn, &(netif->ip_addr), srcPortNO) ) == ERR_USE && !closeths) {
+                       if (srcPortSpec) break;  /* if port was explicitly specified (we shouldn't change user's port number just like that ;) ) */
                        srcPort++;
                        srcPortNO = srcPort;
                }
-               if(err != ERR_OK || !tries)
-               {
+               if (err != ERR_OK || !tries) {
                        newconn = netconn; /* deleting newconn is done anyway, so delete netconn this way */
                        err = ERR_BINDING;
                        break;
@@ -131,46 +140,39 @@ void run_nc(void *arg){
                netconn_set_recvtimeout(netconn, CONNECTING_TIMEO);
                tries = CONNECTING_TRIES;
 
-               if(listen) /* -l <PORT> option */
-               {
-                       if(!udp) /* TCP - make connection */
-                       {
+               if (listen) { /* -l <PORT> option */
+                       if (!udp) { /* TCP - make connection */
                                /* make listening netconn */
                                netconn_listen(netconn);
                                /* accept connection to newconn */
-                               while( tries-- && (err = netconn_accept(netconn, &newconn)) == ERR_TIMEOUT && (thread || rpp_sci_read_nb(1, &input) != SUCCESS));
-                               if(err != ERR_OK)
-                               {
+                               while ( tries-- && (err = netconn_accept(netconn, &newconn)) == ERR_TIMEOUT && (thread || rpp_sci_read_nb(1, &input) != SUCCESS)) ;
+                               if (err != ERR_OK) {
                                        newconn = netconn;
-                               err = ERR_CONN_ACCEPT;
-                               break;
+                                       err = ERR_CONN_ACCEPT;
+                                       break;
                                }
                                else
-                               {
-                                       netconn_delete(netconn); /* we don't need listening netconn anymore */
-                               }
+                                       netconn_delete(netconn);  /* we don't need listening netconn anymore */
                        }
                        else
                        {
-                               while(tries-- && !closeths)
-                               {
-                    /* allow user to interrupt waiting for "udp connection" - in case this is not thread */
-                                       if(!thread && (rpp_sci_read_nb(1, &input) == SUCCESS)){
+                               while (tries-- && !closeths) {
+                                       /* allow user to interrupt waiting for "udp connection" - in case this is not thread */
+                                       if (!thread && (rpp_sci_read_nb(1, &input) == SUCCESS)) {
                                                newconn = netconn; /* deleting newconn is done anyway, so delete netconn this way */
-                                       err = ERR_CONNECTING;
-                                       break;
+                                               err = ERR_CONNECTING;
+                                               break;
                                        }
 
-                                       if((err = netconn_recv(netconn, &remoteData)) == ERR_OK) /* wait for data, from which we will determine our peer address and portNO */
-                                       {
+                                       if ((err = netconn_recv(netconn, &remoteData)) == ERR_OK) { /* wait for data, from which we will determine our peer address and portNO */
                                                remoteIP = remoteData->addr;
                                                portNO = remoteData->port;
-                                               if(test)netbuf_delete(remoteData);
+                                               if (test) netbuf_delete(remoteData);
                                                break;
                                        }
                                        else
                                        {
-                                               if(err == ERR_TIMEOUT)continue;
+                                               if (err == ERR_TIMEOUT) continue;
                                                else
                                                {
                                                        newconn = netconn;
@@ -178,7 +180,7 @@ void run_nc(void *arg){
                                                }
                                        }
                                }
-                               if(!tries){
+                               if (!tries) {
                                        newconn = netconn;
                                        err = ERR_CONNECTING;
                                        break;
@@ -186,293 +188,268 @@ void run_nc(void *arg){
                        }
                }
 
-               if(!listen || (listen && udp) ) /* connect to remote node */
-               {
+               if (!listen || (listen && udp) ) { /* connect to remote node */
                        newconn = netconn;
                        netconn = NULL;
-                       if( ( err = netconn_connect(newconn, &remoteIP, portNO) ) != ERR_OK ){
+                       if ( ( err = netconn_connect(newconn, &remoteIP, portNO) ) != ERR_OK )
                                break;
-                       }
                }
 
-           /* we will block for time specified on next line on netconn to receive data, if it does not come, we block for user sci input */
+               /* we will block for time specified on next line on netconn to receive data, if it does not come, we block for user sci input */
                netconn_set_recvtimeout(newconn, 50);
 
-               if(udp && (localData = netbuf_new()) == NULL){
+               if (udp && (localData = netbuf_new()) == NULL) {
                        err = ERR_MEM;
                        break;
                }
 
-               if(test) /* -d option; sends packets to the opposite node and doesn't receive anything */
-               {
+               if (test) { /* -d option; sends packets to the opposite node and doesn't receive anything */
                        index = 0;
                        char strbuf[13];
                        int strlen;
                        //netconn_set_recvtimeout(newconn, 1);
-                       while(!closeths && index++ < 4294967295){
+                       while (!closeths && index++ < 4294967295) {
                                /* destroy all incomming data to prevent pbufs from running out */
                                /*while(remoteData != NULL ||  (err = netconn_recv ( newconn, &remoteData )) != ERR_TIMEOUT){
-                               netbuf_delete(remoteData);
-                               remoteData = NULL;
-                               }*/
-                /* allow user to interrupt sending */
-                               if(!thread && (rpp_sci_read_nb(1, &input) == SUCCESS))break;
-                       strlen = sprintf(strbuf,"%d\r\n",index);
-                       if(!udp)
-                       {
-                               if((err = netconn_write(newconn, strbuf, strlen, NETCONN_COPY)) != ERR_OK)break;
-                       }
-                       else
-                       {
-                               payload = netbuf_alloc(localData,strlen);
-                               if(payload != NULL)
-                               {
-                                       netbuf_take(localData,strbuf,strlen);
-                                       netbuf_len(localData) = strlen;
-                                       if((err = netconn_send(newconn, localData)) != ERR_OK)break;
-                               }
-                               else
-                               {
-                                       err = ERR_MEM;
-                                       break;
-                               }
-                       }
+                                   netbuf_delete(remoteData);
+                                   remoteData = NULL;
+                                  }*/
+                               /* allow user to interrupt sending */
+                               if (!thread && (rpp_sci_read_nb(1, &input) == SUCCESS)) break;
+                               strlen = sprintf(strbuf,"%d\r\n",index);
+                               if (!udp) {
+                                       if ((err = netconn_write(newconn, strbuf, strlen, NETCONN_COPY)) != ERR_OK) break;
+                               }
+                               else
+                               {
+                                       payload = netbuf_alloc(localData,strlen);
+                                       if (payload != NULL) {
+                                               netbuf_take(localData,strbuf,strlen);
+                                               netbuf_len(localData) = strlen;
+                                               if ((err = netconn_send(newconn, localData)) != ERR_OK) break;
+                                       }
+                                       else
+                                       {
+                                               err = ERR_MEM;
+                                               break;
+                                       }
+                               }
                        }
                        break;
                }
 
-               if(netLoop) /* -m option */
-               {
+               if (netLoop) { /* -m option */
                        err = ERR_OK;
                        /* send some data to opposite node so it knows its peer (this node) ; ATENTION if opposite node was not running in time this is send, then it wont get known this node's connection info */
-                       if(udp && !listen){
+                       if (udp && !listen) {
                                payload = netbuf_alloc(localData,1);
-                               if(payload != NULL){
+                               if (payload != NULL) {
                                        input = 0;
-                               netbuf_take(localData,&input,1);
-                               netbuf_len(localData) = 1;
-                               if((err = netconn_send(newconn, localData)) != ERR_OK)break;
-                       }
-                       else
-                       {
-                               err = ERR_MEM;
-                       break;
-                       }
-                       }
-                       while(!closeths){
-                               if(!thread && (rpp_sci_read_nb(1, &input) == SUCCESS))break; /* allow user to interrupt operation */
-                               if( remoteData != NULL ||  (err = netconn_recv ( newconn, &remoteData )) != ERR_TIMEOUT )
+                                       netbuf_take(localData,&input,1);
+                                       netbuf_len(localData) = 1;
+                                       if ((err = netconn_send(newconn, localData)) != ERR_OK) break;
+                               }
+                               else
                                {
-                                       if(err == ERR_OK){
+                                       err = ERR_MEM;
+                                       break;
+                               }
+                       }
+                       while (!closeths) {
+                               if (!thread && (rpp_sci_read_nb(1, &input) == SUCCESS)) break;  /* allow user to interrupt operation */
+                               if ( remoteData != NULL ||  (err = netconn_recv( newconn, &remoteData )) != ERR_TIMEOUT ) {
+                                       if (err == ERR_OK) {
                                                p = remoteData->p;
                                                /**/
-                                               while(p->ref > 1) /* if (p->ref > 1) p->ref = 1; */
+                                               while (p->ref > 1) /* if (p->ref > 1) p->ref = 1; */
                                                        pbuf_free(p);
                                                /**/
-                                               while(p != NULL)
-                                               {
-                                               if(!udp)
-                                               {
-                                                       netconn_write(newconn, p->payload, p->len, NETCONN_COPY); /*NETCONN_DONTBLOCK*/
-                                               }
-                                               else
-                                               {
-                                                       payload = netbuf_alloc(localData,p->len);
-                                                       if(payload != NULL)
-                                                       {
-                                                               netbuf_take(localData,p->payload,p->len);
-                                                               netbuf_len(localData) = p->len;
-                                                               if((err = netconn_send(newconn, localData)) != ERR_OK)break;
-                                                       }
-                                                       else
-                                                       {
-                                                               err = ERR_MEM;
-                                                               break;
-                                                       }
-                                               }
-                                               p = p->next;
+                                               while (p != NULL) {
+                                                       if (!udp)
+                                                               netconn_write(newconn, p->payload, p->len, NETCONN_COPY);  /*NETCONN_DONTBLOCK*/
+                                                       else
+                                                       {
+                                                               payload = netbuf_alloc(localData,p->len);
+                                                               if (payload != NULL) {
+                                                                       netbuf_take(localData,p->payload,p->len);
+                                                                       netbuf_len(localData) = p->len;
+                                                                       if ((err = netconn_send(newconn, localData)) != ERR_OK) break;
+                                                               }
+                                                               else
+                                                               {
+                                                                       err = ERR_MEM;
+                                                                       break;
+                                                               }
+                                                       }
+                                                       p = p->next;
                                                }
-                                       netbuf_delete(remoteData);
-                                       remoteData = NULL;
+                                               netbuf_delete(remoteData);
+                                               remoteData = NULL;
                                        }
                                        else
-                                       {
                                                break;
-                                       }
                                }
                        }
-                       if(err == ERR_TIMEOUT)err = ERR_OK; /* timeout is not error here, this occurs on interrupt of while cycle */
-            break;
+                       if (err == ERR_TIMEOUT) err = ERR_OK;  /* timeout is not error here, this occurs on interrupt of while cycle */
+                       break;
                }
 
                /* only active user can control this section XXX: maybe should move to init startup condition handler */
-               if(thread)break;
+               if (thread) break;
 
                /* INTERACTIVE PART */
 
-           /* receive remote data and print them to sci, receive sci data and write them to netconn */
-               while(!ncStop)
-               {
-                       if(rpp_sci_read_nb(1, &input) != SUCCESS) {
-                           if( remoteData != NULL || (err = netconn_recv ( newconn, &remoteData )) != ERR_TIMEOUT)
-                           {
-                               if(err == ERR_OK)
-                               {
-                                       /* print remote data to SCI */
-                                       p = remoteData->p;
-                                       while(p != NULL)
-                                       {
-                                           index = 0;
-                                           payload = p->payload;
-                                           while(index<p->len)
-                                           {
-                                               rpp_sci_printf("%c",payload[index]);
-                                               index++;
-                                           }
-                                               p = p->next;
-                                       }
-                                       netbuf_delete(remoteData);
-                                       remoteData = NULL;
-                               }
-                               else
-                               {
-                                       break; /* receive error - e.g. newconn was closed */
-                               }
-                           }
-                           if(err == ERR_TIMEOUT)err = ERR_OK;
+               /* receive remote data and print them to sci, receive sci data and write them to netconn */
+               while (!ncStop) {
+                       if (rpp_sci_read_nb(1, &input) != SUCCESS) {
+                               if ( remoteData != NULL || (err = netconn_recv( newconn, &remoteData )) != ERR_TIMEOUT) {
+                                       if (err == ERR_OK) {
+                                               /* print remote data to SCI */
+                                               p = remoteData->p;
+                                               while (p != NULL) {
+                                                       index = 0;
+                                                       payload = p->payload;
+                                                       while (index < p->len) {
+                                                               rpp_sci_printf("%c",payload[index]);
+                                                               index++;
+                                                       }
+                                                       p = p->next;
+                                               }
+                                               netbuf_delete(remoteData);
+                                               remoteData = NULL;
+                                       }
+                                       else
+                                               break;  /* receive error - e.g. newconn was closed */
+                               }
+                               if (err == ERR_TIMEOUT) err = ERR_OK;
                                continue;
                        }
 
-               // Backspace and Delete
-               if(input == 8 || input == 127) {
-                   if(buff_index > 0) {
-                       buff_index--;
-                       rpp_sci_putc('\b');
-                       rpp_sci_putc(' ' );
-                       rpp_sci_putc('\b');
-                   }
-
-               // Line feed or Carriage return
-               } else if(input == 10 || input == 13) {
-                       in_buffer[buff_index] = 13;
-                       buff_index++;
-                       in_buffer[buff_index] = 10;
-                       buff_index++;
-                   flush = TRUE;
-                   rpp_sci_putc('\r');
-                   rpp_sci_putc('\n');
-
-               // If is any printable character
-               } else if(isprint(input) || input == 9) {
-
-                   // Store character and increment buffer index
-                   in_buffer[buff_index] = input;
-                   buff_index++;
-                   rpp_sci_putc(input);
-
-                   // Check if buffer is full and force flush
-                   if(buff_index == BUF_SIZE - 3) {
-                       flush = TRUE;
-                   }
-               // All other character stops nc
-               } else {
-                       ncStop = TRUE;
-                       flush = TRUE;
-               }
-
-               if(flush)
-               {
-                   if(!udp)
-                       {
-                       netconn_write(newconn, in_buffer, buff_index, NETCONN_COPY);
-                       }
-                       else
-                       {
-                               payload = netbuf_alloc(localData,buff_index);
-                               if(payload != NULL)
-                               {
-                                       netbuf_take(localData,in_buffer,buff_index);
-                                       netbuf_len(localData) = buff_index;
-                                       if((err = netconn_send(newconn, localData)) != ERR_OK)break;
-                               }
-                               else
-                               {
-                                       err = ERR_MEM;
-                                       break;
-                               }
-                       }
-                   // Reset variables
-                   buff_index = 0;
-                   flush = FALSE;
-               }
+                       // Backspace and Delete
+                       if (input == 8 || input == 127) {
+                               if (buff_index > 0) {
+                                       buff_index--;
+                                       rpp_sci_putc('\b');
+                                       rpp_sci_putc(' ' );
+                                       rpp_sci_putc('\b');
+                               }
+
+                               // Line feed or Carriage return
+                       }
+                       else if (input == 10 || input == 13) {
+                               in_buffer[buff_index] = 13;
+                               buff_index++;
+                               in_buffer[buff_index] = 10;
+                               buff_index++;
+                               flush = TRUE;
+                               rpp_sci_putc('\r');
+                               rpp_sci_putc('\n');
+
+                               // If is any printable character
+                       }
+                       else if (isprint(input) || input == 9) {
+
+                               // Store character and increment buffer index
+                               in_buffer[buff_index] = input;
+                               buff_index++;
+                               rpp_sci_putc(input);
+
+                               // Check if buffer is full and force flush
+                               if (buff_index == BUF_SIZE - 3)
+                                       flush = TRUE;
+                               // All other character stops nc
+                       }
+                       else {
+                               ncStop = TRUE;
+                               flush = TRUE;
+                       }
+
+                       if (flush) {
+                               if (!udp)
+                                       netconn_write(newconn, in_buffer, buff_index, NETCONN_COPY);
+                               else
+                               {
+                                       payload = netbuf_alloc(localData,buff_index);
+                                       if (payload != NULL) {
+                                               netbuf_take(localData,in_buffer,buff_index);
+                                               netbuf_len(localData) = buff_index;
+                                               if ((err = netconn_send(newconn, localData)) != ERR_OK) break;
+                                       }
+                                       else
+                                       {
+                                               err = ERR_MEM;
+                                               break;
+                                       }
+                               }
+                               // Reset variables
+                               buff_index = 0;
+                               flush = FALSE;
+                       }
                }
                break;
-    }
+       }
 #ifdef DEBUG
-       if(thread)rpp_sci_printf("NC: THREAD NO.%d FINISHED\r\n", thread_inst);
+       if (thread) rpp_sci_printf("NC: THREAD NO.%d FINISHED\r\n", thread_inst);
 #endif
-       if(udp && localData != NULL)netbuf_delete(localData);
-       if(remoteData != NULL)netbuf_delete(remoteData);
-       if(newconn != NULL && !udp)netconn_close(newconn);
+       if (udp && localData != NULL) netbuf_delete(localData);
+       if (remoteData != NULL) netbuf_delete(remoteData);
+       if (newconn != NULL && !udp) netconn_close(newconn);
        netconn_delete(newconn);
        tasks_running--;
-       if(!thread)
-               nc_arg->err = err; /* only user controlled task is in front */
-       if(thread)vTaskDelete(NULL);
+       if (!thread)
+               nc_arg->err = err;  /* only user controlled task is in front */
+       if (thread) vTaskDelete(NULL);
 }
-#else
+#else /* if !NO_SYS */
 boolean_t connected;
 boolean_t sent;
 boolean_t ncStop;
 
 err_t nc_recv_callback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
 {
-    uint16_t index;
-    uint8_t *payload;
-       if(err == ERR_ABRT)
-       {
+       uint16_t index;
+       uint8_t *payload;
+
+       if (err == ERR_ABRT) {
                rpp_sci_printk("recv abrt\n");
                return ERR_ABRT;
        }
        /* print remote data to SCI */
-       while(p != NULL)
-       {
-           index = 0;
-           payload = p->payload;
-           while(index<p->len)
-           {
-               rpp_sci_printk("%c",payload[index]);
-               index++;
-           }
-        p = p->next;
+       while (p != NULL) {
+               index = 0;
+               payload = p->payload;
+               while (index < p->len) {
+                       rpp_sci_printk("%c",payload[index]);
+                       index++;
+               }
+               p = p->next;
        }
-    return ERR_OK;
+       return ERR_OK;
 }
 
 void nc_udp_recv_callback(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
 {
-    uint16_t index;
-    uint8_t *payload;
+       uint16_t index;
+       uint8_t *payload;
+
        /* print remote data to SCI */
-       while(p != NULL)
-       {
-           index = 0;
-           payload = p->payload;
-           while(index<p->len)
-           {
-               rpp_sci_printk("%c",payload[index]);
-               index++;
-           }
-        p = p->next;
+       while (p != NULL) {
+               index = 0;
+               payload = p->payload;
+               while (index < p->len) {
+                       rpp_sci_printk("%c",payload[index]);
+                       index++;
+               }
+               p = p->next;
        }
-    //pbuf_free(p);
+       //pbuf_free(p);
 }
 
 void nc_udp_listen_recv_callback(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
 {
        connected = TRUE;
        err_t err = udp_connect(pcb, addr, port);
-    LWIP_ASSERT(("nc_init: udp_listen_connect failed"), err == ERR_OK);
+       LWIP_ASSERT(("nc_init: udp_listen_connect failed"), err == ERR_OK);
        udp_recv(pcb, nc_udp_recv_callback, NULL);
        nc_udp_recv_callback(arg, pcb, p, addr, port);
 }
@@ -494,76 +471,72 @@ err_t nc_sent_callback(void *arg, struct tcp_pcb *pcb, u16_t len)
 err_t nc_accept_callback(void *arg, struct tcp_pcb *newpcb, err_t err)
 {
        struct tcp_pcb **newone = arg;
+
        *newone = newpcb; /* pass accepted connection to main app loop */
 
        connected = TRUE;
 
-    /* Set up the various callback functions */
-    tcp_recv(newpcb, nc_recv_callback);
-    tcp_err(newpcb, nc_err_callback);
-    tcp_sent(newpcb, nc_sent_callback);
+       /* Set up the various callback functions */
+       tcp_recv(newpcb, nc_recv_callback);
+       tcp_err(newpcb, nc_err_callback);
+       tcp_sent(newpcb, nc_sent_callback);
 
-    return ERR_OK;
+       return ERR_OK;
 }
 
 err_t nc_connected_callback(void *arg, struct tcp_pcb *tpcb, err_t err)
 {
 
-    /* Set up the various callback functions */
-    tcp_recv(tpcb, nc_recv_callback);
-    tcp_err(tpcb, nc_err_callback);
-    tcp_sent(tpcb, nc_sent_callback);
+       /* Set up the various callback functions */
+       tcp_recv(tpcb, nc_recv_callback);
+       tcp_err(tpcb, nc_err_callback);
+       tcp_sent(tpcb, nc_sent_callback);
 
        connected = TRUE;
        rpp_sci_printk("connected\n");
-    return ERR_OK;
+       return ERR_OK;
 }
 
 err_t nc_tmirror_callback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
 {
-       if(err == ERR_ABRT)
-       {
+       if (err == ERR_ABRT) {
                rpp_sci_printk("recv abrt\n");
                return ERR_ABRT;
        }
-       while(p != NULL)
-       {
-               if(tcp_write(tpcb, p->payload, p->len, TCP_WRITE_FLAG_COPY)!=ERR_OK){
-               rpp_sci_printk("error writing to newpcb - tcp_write\n");
-               return -1;
-       }
-       if(tcp_output(tpcb)!=ERR_OK){ /* when we want to send data immediately */
-               rpp_sci_printk("newpcb output err - tcp\n");
-               return -1;
-       }
-           p = p->next;
+       while (p != NULL) {
+               if (tcp_write(tpcb, p->payload, p->len, TCP_WRITE_FLAG_COPY) != ERR_OK) {
+                       rpp_sci_printk("error writing to newpcb - tcp_write\n");
+                       return -1;
+               }
+               if (tcp_output(tpcb) != ERR_OK) { /* when we want to send data immediately */
+                       rpp_sci_printk("newpcb output err - tcp\n");
+                       return -1;
+               }
+               p = p->next;
        }
        return ERR_OK;
 }
 
 void nc_umirror_callback(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
 {
-    struct pbuf *pbuf;
-       while(p != NULL)
-       {
-        pbuf = pbuf_alloc(PBUF_TRANSPORT, p->len, PBUF_RAM);
-        if(pbuf == NULL)
-        {
-               rpp_sci_printk("error allocating pbuf - udp_send\n");
-               //p = p->next;
-               continue;
-        }
-        if(pbuf_take(pbuf, p->payload, p->len)!=ERR_OK)
-        {
-               rpp_sci_printk("pbuf mem err (small pbuf) - udp_send\n");
-               continue;
-        }
-       if(udp_send (pcb, pbuf)!=ERR_OK)
-       {
-               rpp_sci_printk("error sending to pcb - udp_send\n");
-               continue;
-       }
-        p = p->next;
+       struct pbuf *pbuf;
+
+       while (p != NULL) {
+               pbuf = pbuf_alloc(PBUF_TRANSPORT, p->len, PBUF_RAM);
+               if (pbuf == NULL) {
+                       rpp_sci_printk("error allocating pbuf - udp_send\n");
+                       //p = p->next;
+                       continue;
+               }
+               if (pbuf_take(pbuf, p->payload, p->len) != ERR_OK) {
+                       rpp_sci_printk("pbuf mem err (small pbuf) - udp_send\n");
+                       continue;
+               }
+               if (udp_send(pcb, pbuf) != ERR_OK) {
+                       rpp_sci_printk("error sending to pcb - udp_send\n");
+                       continue;
+               }
+               p = p->next;
        }
 }
 
@@ -577,119 +550,117 @@ void nc_umirror_callback(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr
  */
 int cmd_do_init_nc(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
 {
-       if(!isPostInitialized())
-       {
+       if (!isPostInitialized()) {
                rpp_sci_printf("Eth not initialized run 'ethinit' command first.\n");
                return FAILURE;
        }
-    err_t err = ERR_OK;
+       err_t err = ERR_OK;
        uint16_t portNO, srcPortNO = 0;
        ip_addr_t remoteIP;
        boolean_t help = FALSE, udp = FALSE, listen = FALSE, netLoop = FALSE, test = FALSE, srcPortSpec = FALSE;
-    uint8_t pindex;
+       uint8_t pindex;
 
        buff_index = 0;
        /* initialize arguments and thread name */
 #if !NO_SYS
-    uint8_t name[5] = "nc";
-    name[4] = '\0';
+       char name[5] = "nc";
+       name[4] = '\0';
        struct nc_arg nc_arg; /* create space for handing args to nc_run() */
        nc_arg.thread = FALSE;
 #else
        uint8_t input = 0;
-    boolean_t flush = FALSE;
-    uint32_t index;
-    struct netif *netif = rpp_eth_get_netif(INTERFACE_INSTANCE_NUMBER);
+       boolean_t flush = FALSE;
+       uint32_t index;
+       struct netif *netif = rpp_eth_get_netif(INTERFACE_INSTANCE_NUMBER);
        struct tcp_pcb *tpcb, *tnewpcb;
        struct udp_pcb *pcb;
        struct pbuf *p; /* udp send */
 #endif
 
-    /* examine parameters */
-    for(pindex = 1;param[pindex] != 0;pindex++)
-    {
-       if        (strncmp((char *) param[pindex], "-h", 3) == 0) {
-            help = TRUE;
-            break;
-       } else if (strncmp((char *) param[pindex], "-u", 3) == 0) {
-               udp = TRUE;
+       /* examine parameters */
+       for (pindex = 1; param[pindex] != 0; pindex++) {
+               if        (strncmp((char *)param[pindex], "-h", 3) == 0) {
+                       help = TRUE;
+                       break;
+               }
+               else if (strncmp((char *)param[pindex], "-u", 3) == 0) {
+                       udp = TRUE;
 #if !NO_SYS
-       } else if (strncmp((char *) param[pindex], "-t", 3) == 0) {
-               nc_arg.thread = TRUE;
-       } else if (strncmp((char *) param[pindex], "-c", 3) == 0) {
-               closeths = TRUE;
-               return ERR_OK;
+               }
+               else if (strncmp((char *)param[pindex], "-t", 3) == 0)
+                       nc_arg.thread = TRUE;
+               else if (strncmp((char *)param[pindex], "-c", 3) == 0) {
+                       closeths = TRUE;
+                       return ERR_OK;
 #endif
-       } else if (strncmp((char *) param[pindex], "-l", 3) == 0 && !srcPortSpec) {
-               listen = TRUE;
-               srcPortNO = rpp_eth_portStrToInt((uint8_t *)param[++pindex]);
-                       if(srcPortNO == 0)
-                       {
+               }
+               else if (strncmp((char *)param[pindex], "-l", 3) == 0 && !srcPortSpec) {
+                       listen = TRUE;
+                       srcPortNO = rpp_eth_portStrToInt((uint8_t *)param[++pindex]);
+                       if (srcPortNO == 0) {
                                rpp_sci_printf("E wrong portNO, portNO must follow immediately after -l option\r\n");
                                return BAD_PORT_NO;
                        }
                        srcPortSpec = TRUE;
-       } else if (strncmp((char *) param[pindex], "-p", 3) == 0 && !srcPortSpec) {
-               srcPortNO = rpp_eth_portStrToInt((uint8_t *)param[++pindex]);
-                       if(srcPortNO == 0)
-                       {
+               }
+               else if (strncmp((char *)param[pindex], "-p", 3) == 0 && !srcPortSpec) {
+                       srcPortNO = rpp_eth_portStrToInt((uint8_t *)param[++pindex]);
+                       if (srcPortNO == 0) {
                                rpp_sci_printf("E wrong portNO, portNO must follow immediately after -p option\r\n");
                                return BAD_PORT_NO;
                        }
                        srcPortSpec = TRUE;
-       } else if (strncmp((char *) param[pindex], "-m", 3) == 0) {
-               netLoop = TRUE;
-       } else if (strncmp((char *) param[pindex], "-d", 3) == 0) {
-            test = TRUE;
-       } else if ( (err = rpp_eth_stringToIP(&remoteIP, (uint8_t *) param[pindex])) == SUCCESS ) {
-               portNO = rpp_eth_portStrToInt((uint8_t *)param[++pindex]);
-                       if(portNO == 0)
-                       {
+               }
+               else if (strncmp((char *)param[pindex], "-m", 3) == 0)
+                       netLoop = TRUE;
+               else if (strncmp((char *)param[pindex], "-d", 3) == 0)
+                       test = TRUE;
+               else if ( (err = rpp_eth_stringToIP(&remoteIP, (uint8_t *)param[pindex])) == SUCCESS ) {
+                       portNO = rpp_eth_portStrToInt((uint8_t *)param[++pindex]);
+                       if (portNO == 0) {
                                rpp_sci_printf("E wrong portNO, portNO must follow immediately after IP address\r\n");
                                return BAD_PORT_NO;
                        }
-       } else {
-               rpp_sci_printf("ERR: check option name, option combination, IP address format\r\n");
-               help = TRUE;
-               err = BAD_OPTION;
-               break;
-       }
-    }
-
-    /* TODO: additional testing of parameters mutual exclusion */
-    /* valid combinations:
-nc <addr> <port> [-p <port>] [-u] [-t]
-nc -l <port> [-u] [-t]
-nc <addr> <port> -m [-p <port>] [-u] [-t]
-nc <addr> <port> -d [-p <port>] [-u] [-t]
-nc -l <port> -m [-u] [-t]
-nc -l <port> -d [-u] [-t]
-     */
-
-    /* usage of command */
-    if(help || !param[1])
-    {
-       rpp_sci_printf("-u udp, -t thread, -c close tasks, -l listen, -p source port, -m ping/mirror, -d test\r\n\n");
-        rpp_sci_printf("usage:\tnc -l <port> [-u] [-m [-t] | -d [-t]]\r\n\t\tlistens on port for connection\r\n");
-        rpp_sci_printf("\tnc <address> <port> [-p <port>] [-u] [-m [-t] | -d [-t]]\r\n\t\tconnects to the address on port NO.\r\n");
-        rpp_sci_printf("\tnc -c\r\n\t\tdeletes all running threads.\r\n");
-        rpp_sci_printf("opts:\t-t run as stand alone task\r\n\t-c close all stand alone tasks\r\n");
-        rpp_sci_printf("\t-l <port> listen on given port for incomming connection\r\n\t-p <port> use as local port when connecting to remote node\r\n\t");
-        rpp_sci_printf("\t-m send all incomming data back to sender\r\n\t-d sends many testing packets containing packet number in ASCII as data\r\n");
-        rpp_sci_printf("Note: when using udp -u and listening -l options, first node data was received from is considered as peer for communication.\r\n");
-       return err;
-    }
-
-    /* determine srcPortNO from predefined value if not done yet (from argument given) */
-    if(!srcPortSpec)
-    {
-       //udp_new_port();
-       srcPortNO = srcPort++;
-    }
+               }
+               else {
+                       rpp_sci_printf("ERR: check option name, option combination, IP address format\r\n");
+                       help = TRUE;
+                       err = BAD_OPTION;
+                       break;
+               }
+       }
+
+       /* TODO: additional testing of parameters mutual exclusion */
+       /* valid combinations:
+          nc <addr> <port> [-p <port>] [-u] [-t]
+          nc -l <port> [-u] [-t]
+          nc <addr> <port> -m [-p <port>] [-u] [-t]
+          nc <addr> <port> -d [-p <port>] [-u] [-t]
+          nc -l <port> -m [-u] [-t]
+          nc -l <port> -d [-u] [-t]
+        */
+
+       /* usage of command */
+       if (help || !param[1]) {
+               rpp_sci_printf("-u udp, -t thread, -c close tasks, -l listen, -p source port, -m ping/mirror, -d test\r\n\n");
+               rpp_sci_printf("usage:\tnc -l <port> [-u] [-m [-t] | -d [-t]]\r\n\t\tlistens on port for connection\r\n");
+               rpp_sci_printf("\tnc <address> <port> [-p <port>] [-u] [-m [-t] | -d [-t]]\r\n\t\tconnects to the address on port NO.\r\n");
+               rpp_sci_printf("\tnc -c\r\n\t\tdeletes all running threads.\r\n");
+               rpp_sci_printf("opts:\t-t run as stand alone task\r\n\t-c close all stand alone tasks\r\n");
+               rpp_sci_printf("\t-l <port> listen on given port for incomming connection\r\n\t-p <port> use as local port when connecting to remote node\r\n\t");
+               rpp_sci_printf("\t-m send all incomming data back to sender\r\n\t-d sends many testing packets containing packet number in ASCII as data\r\n");
+               rpp_sci_printf("Note: when using udp -u and listening -l options, first node data was received from is considered as peer for communication.\r\n");
+               return err;
+       }
+
+       /* determine srcPortNO from predefined value if not done yet (from argument given) */
+       if (!srcPortSpec)
+               //udp_new_port();
+               srcPortNO = srcPort++;
 
 #if !NO_SYS /* netconn api */
 
-    /* prepare args */
+       /* prepare args */
        nc_arg.srcPortNO = srcPortNO;
        nc_arg.portNO = portNO;
        nc_arg.remoteIP.addr = remoteIP.addr;
@@ -699,305 +670,281 @@ nc -l <port> -d [-u] [-t]
        nc_arg.test = test;
        nc_arg.srcPortSpec = srcPortSpec;
 
-    if(nc_arg.thread){
-        /* prepare semaphore */
-       vSemaphoreCreateBinary(nc_arg.args_coppied);
+       if (nc_arg.thread) {
+               /* prepare semaphore */
+               vSemaphoreCreateBinary(nc_arg.args_coppied);
                xSemaphoreTake(nc_arg.args_coppied, 0);
-       /* add number to task name */
-       taskNameNum = (taskNameNum%100);
-       name[2] = (taskNameNum/10) + '0';
-       name[3] = (taskNameNum%10) + '0';
-       rpp_sci_printf("STARTING THREAD: %s\r\n", name);
-       taskNameNum++;
-       /* start thread */
-        if((err = xTaskCreate(&run_nc,(const signed char *) name, ncTaskStackSize, &nc_arg, ncTaskPriority, NULL)) == pdPASS){
-               /* block on semaphore, till new thread copies arg to own stack */
-               xSemaphoreTake( nc_arg.args_coppied, portMAX_DELAY );
-               vSemaphoreDelete(nc_arg.args_coppied);
-        }else{
-               vSemaphoreDelete(nc_arg.args_coppied);
-               return err;
-        }
-    }
-    else
-    {
-       taskNameNum++;
-        run_nc(&nc_arg);
-        return nc_arg.err;
-    }
+               /* add number to task name */
+               taskNameNum = (taskNameNum%100);
+               name[2] = (taskNameNum/10) + '0';
+               name[3] = (taskNameNum%10) + '0';
+               rpp_sci_printf("STARTING THREAD: %s\r\n", name);
+               taskNameNum++;
+               /* start thread */
+               if ((err = xTaskCreate(&run_nc, name, ncTaskStackSize, &nc_arg, ncTaskPriority, NULL)) == pdPASS) {
+                       /* block on semaphore, till new thread copies arg to own stack */
+                       xSemaphoreTake( nc_arg.args_coppied, portMAX_DELAY );
+                       vSemaphoreDelete(nc_arg.args_coppied);
+               }
+               else {
+                       vSemaphoreDelete(nc_arg.args_coppied);
+                       return err;
+               }
+       }
+       else
+       {
+               taskNameNum++;
+               run_nc(&nc_arg);
+               return nc_arg.err;
+       }
 #else /* raw LwIP API */
-    ncStop = FALSE;
-    connected = FALSE;
-    sent = TRUE;
-    if(udp)
-    {
-       pcb = udp_new();
-        LWIP_ASSERT(("nc_init: udp_new failed"), pcb != NULL);
-        if(pcb == NULL)return -1;
-        err = udp_bind(pcb, &(netif->ip_addr), srcPortNO);
-        CC_ASSERT(("nc_init: udp_bind failed: %s", lwip_strerr(err)), err == ERR_OK);
-        if(err != ERR_OK)return -1;
-    }
-    else
-    {
-       tpcb = tcp_new();
-        LWIP_ASSERT(("nc_init: tcp_new failed"), tpcb != NULL);
-        if(tpcb == NULL)return -1;
-        err = tcp_bind(tpcb, &(netif->ip_addr), srcPortNO);
-        CC_ASSERT(("nc_init: tcp_bind failed: %s", lwip_strerr(err)), err == ERR_OK);
-        if(err != ERR_OK)return -1;
-    }
-
-    if(!udp)
-    {
-       if(listen)
-       {
-            tpcb = tcp_listen(tpcb);
-            LWIP_ASSERT(("nc_init: tcp_listen failed"), tpcb != NULL);
-            /* initialize callback arg and accept callback */
-            tcp_arg(tpcb, &tnewpcb);
-            tcp_accept(tpcb, nc_accept_callback);
-       }
-       else
-       {
-               tnewpcb = tpcb;
-               tpcb = NULL;
-               err = tcp_connect(tnewpcb, &remoteIP, portNO, nc_connected_callback);
-            LWIP_ASSERT(("nc_init: tcp_connect failed"), err == ERR_OK);
-       }
-    }
-    else
-    {
-       if(listen)
-       {
-               udp_recv(pcb, nc_udp_listen_recv_callback, NULL);
-       }
-       else
-       {
-               err = udp_connect(pcb, &remoteIP, portNO);
-            LWIP_ASSERT(("nc_init: udp_connect failed"), err == ERR_OK);
-               //connected = TRUE;
-               /* set udp recv callback */
-               udp_recv(pcb, nc_udp_recv_callback, NULL);
-       }
-    }
-
-    /* wait for connection to be established */
-    if(listen || !udp)
-    {
-        while(!connected)
-        {
-               if(rpp_sci_read_nb(1, &input) == SUCCESS)
-               {
-                   ncStop = TRUE;
-                   break;
-               }
-               sys_check_timeouts();
-               vTaskDelay(10); /* just because we have no other business here */
-        }
-        if(!udp && listen && tcp_close(tpcb) != ERR_OK)rpp_sci_printk("closing listening tcp pcb err\n"); /* close listening pcb */
-    }
-
-    /* mode handlers in loop, so we can jump out of it on error or finish and clean after us */
-    do{
-        /*************************************** 1 mirror mode *****************************************/
-
-       if(netLoop) /* -m option */
-       {
-               err = ERR_OK;
-               /* send some data to opposite node so it knows its peer (this node) ; ATENTION if opposite node was not running in time this is send, then it wont get known this node's connection info */
-/*             if(udp && !listen){
-                       payload = netbuf_alloc(localData,1);
-                       if(payload != NULL){
-                               input = 0;
-                       netbuf_take(localData,&input,1);
-                       netbuf_len(localData) = 1;
-                       if((err = netconn_send(newconn, localData)) != ERR_OK)break;
-               }
-               else
-               {
-                       err = ERR_MEM;
+       ncStop = FALSE;
+       connected = FALSE;
+       sent = TRUE;
+       if (udp) {
+               pcb = udp_new();
+               LWIP_ASSERT(("nc_init: udp_new failed"), pcb != NULL);
+               if (pcb == NULL) return -1;
+               err = udp_bind(pcb, &(netif->ip_addr), srcPortNO);
+               CC_ASSERT(("nc_init: udp_bind failed: %s", lwip_strerr(err)), err == ERR_OK);
+               if (err != ERR_OK) return -1;
+       }
+       else
+       {
+               tpcb = tcp_new();
+               LWIP_ASSERT(("nc_init: tcp_new failed"), tpcb != NULL);
+               if (tpcb == NULL) return -1;
+               err = tcp_bind(tpcb, &(netif->ip_addr), srcPortNO);
+               CC_ASSERT(("nc_init: tcp_bind failed: %s", lwip_strerr(err)), err == ERR_OK);
+               if (err != ERR_OK) return -1;
+       }
+
+       if (!udp) {
+               if (listen) {
+                       tpcb = tcp_listen(tpcb);
+                       LWIP_ASSERT(("nc_init: tcp_listen failed"), tpcb != NULL);
+                       /* initialize callback arg and accept callback */
+                       tcp_arg(tpcb, &tnewpcb);
+                       tcp_accept(tpcb, nc_accept_callback);
+               }
+               else
+               {
+                       tnewpcb = tpcb;
+                       tpcb = NULL;
+                       err = tcp_connect(tnewpcb, &remoteIP, portNO, nc_connected_callback);
+                       LWIP_ASSERT(("nc_init: tcp_connect failed"), err == ERR_OK);
+               }
+       }
+       else
+       {
+               if (listen)
+                       udp_recv(pcb, nc_udp_listen_recv_callback, NULL);
+               else
+               {
+                       err = udp_connect(pcb, &remoteIP, portNO);
+                       LWIP_ASSERT(("nc_init: udp_connect failed"), err == ERR_OK);
+                       //connected = TRUE;
+                       /* set udp recv callback */
+                       udp_recv(pcb, nc_udp_recv_callback, NULL);
+               }
+       }
+
+       /* wait for connection to be established */
+       if (listen || !udp) {
+               while (!connected) {
+                       if (rpp_sci_read_nb(1, &input) == SUCCESS) {
+                               ncStop = TRUE;
+                               break;
+                       }
+                       sys_check_timeouts();
+                       vTaskDelay(10); /* just because we have no other business here */
+               }
+               if (!udp && listen && tcp_close(tpcb) != ERR_OK) rpp_sci_printk("closing listening tcp pcb err\n");  /* close listening pcb */
+       }
+
+       /* mode handlers in loop, so we can jump out of it on error or finish and clean after us */
+       do {
+               /*************************************** 1 mirror mode *****************************************/
+
+               if (netLoop) { /* -m option */
+                       err = ERR_OK;
+                       /* send some data to opposite node so it knows its peer (this node) ; ATENTION if opposite node was not running in time this is send, then it wont get known this node's connection info */
+/*          if(udp && !listen){
+                payload = netbuf_alloc(localData,1);
+                if(payload != NULL){
+                    input = 0;
+                    netbuf_take(localData,&input,1);
+                    netbuf_len(localData) = 1;
+                    if((err = netconn_send(newconn, localData)) != ERR_OK)break;
+                }
+                else
+                {
+                    err = ERR_MEM;
                     break;
-               }
-               }*/
-               /* register callback function which sends all the recevd data back to remote host */
-               if(!udp)
-               {
-                       tcp_recv(tnewpcb, nc_tmirror_callback);
-               }
-               else
-               {
-                       udp_recv(pcb, nc_umirror_callback, NULL);
-               }
-
-               while(rpp_sci_read_nb(1, &input) != SUCCESS)
-               {
-                       sys_check_timeouts();
-                       vTaskDelay(10);
-               }
-            break;
-       }
-
-        /*************************************** 2 testing mode *****************************************/
-
-       if(test) /* -d option; sends packets to the opposite node and doesn't receive anything */
-       {
-               index = 0;
-               char strbuf[13];
-               int strlen;
-               //netconn_set_recvtimeout(newconn, 1);
-               while(index++ < 4294967295){
-                /* allow user to interrupt sending */
-                       if(rpp_sci_read_nb(1, &input) == SUCCESS)break;
-               strlen = sprintf(strbuf,"%d\r\n",index);
-                if(!udp)
-               {
-                       sys_check_timeouts();
-                       if(tcp_write(tnewpcb, strbuf, strlen, TCP_WRITE_FLAG_COPY)!=ERR_OK){
-                               rpp_sci_printf("error writing to newpcb - tcp_write testing\n");
-                               break;
-                       }
-                       if(tcp_output(tnewpcb)!=ERR_OK){ /* when we want to send data immediately */
-                               rpp_sci_printf("newpcb output err - tcp testing\n");
-                               break;
-                       }
-               }
-               else
-               {
-                    p = pbuf_alloc(PBUF_TRANSPORT, strlen, PBUF_RAM);
-                    if(p == NULL)
-                    {
-                               rpp_sci_printf("error allocating pbuf - udp_send testing\n");
-                               continue;
-                    }
-                    if(pbuf_take(p, strbuf, strlen)!=ERR_OK)
-                    {
-                               rpp_sci_printf("pbuf mem err (small pbuf) - udp_send testing\n");
-                               continue;
-                    }
-                       if(udp_send (pcb, p)!=ERR_OK)
-                       {
-                               rpp_sci_printf("error sending to pcb - udp_send testing\n");
-                               continue;
-                       }
-               }
-               }
-               break;
-       }
-
-        /*************************************** 3 interactive mode *****************************************/
-
-       while(!ncStop)
-       {
-               if(!udp && tnewpcb->state != ESTABLISHED)
-               {
-                   ncStop = TRUE;
-                   break;
-               }
-               if(rpp_sci_read_nb(1, &input) != SUCCESS) {
-                       /* updating lwip timers here */
-                       sys_check_timeouts();
-                       continue;
-               }
-
-            // Backspace and Delete
-            if(input == 8 || input == 127) {
-                if(buff_index > 0) {
-                    buff_index--;
-                    rpp_sci_putc('\b');
-                    rpp_sci_putc(' ' );
-                    rpp_sci_putc('\b');
                 }
+            }*/
+                       /* register callback function which sends all the recevd data back to remote host */
+                       if (!udp)
+                               tcp_recv(tnewpcb, nc_tmirror_callback);
+                       else
+                               udp_recv(pcb, nc_umirror_callback, NULL);
 
-            // Line feed or Carriage return
-            } else if(input == 10 || input == 13) {
-               in_buffer[buff_index] = 13;
-               buff_index++;
-               in_buffer[buff_index] = 10;
-               buff_index++;
-                flush = TRUE;
-                rpp_sci_putc('\r');
-                rpp_sci_putc('\n');
-
-            // If is any printable character
-            } else if(isprint(input) || input == 9) {
-
-                // Store character and increment buffer index
-                in_buffer[buff_index] = input;
-                buff_index++;
-                rpp_sci_putc(input);
-
-                // Check if buffer is full and force flush
-                if(buff_index == BUF_SIZE - 3) {
-                    flush = TRUE;
-                }
-            // All other character stops nc
-            } else {
-               ncStop = TRUE;
-               flush = TRUE;
-            }
-
-            if(flush)
-            {
-               while(!udp && !ncStop && !sent)
-               {
-                       sys_check_timeouts();
-                       vTaskDelay(10);
-               }
-               if(ncStop)break;
-                if(!udp)
-               {
-                       if(tcp_write(tnewpcb, in_buffer, buff_index, TCP_WRITE_FLAG_COPY)!=ERR_OK){
-                               rpp_sci_printf("error writing to newpcb - tcp_write\n");
-                               continue;
-                       }
-                       if(tcp_output(tnewpcb)!=ERR_OK){ /* when we want to send data immediately */
-                               rpp_sci_printf("newpcb output err - tcp\n");
-                               continue;
-                       }
-               }
-               else
-               {
-                    p = pbuf_alloc(PBUF_TRANSPORT, buff_index, PBUF_RAM);
-                    if(p == NULL)
-                    {
-                               rpp_sci_printf("error allocating pbuf - udp_send\n");
-                               continue;
-                    }
-                    if(pbuf_take(p, in_buffer, buff_index)!=ERR_OK)
-                    {
-                               rpp_sci_printf("pbuf mem err (small pbuf) - udp_send\n");
-                               continue;
-                    }
-                       if(udp_send (pcb, p)!=ERR_OK)
-                       {
-                               rpp_sci_printf("error sending to pcb - udp_send\n");
-                               continue;
-                       }
-               }
-                // Reset variables
-               sent = FALSE;
-                buff_index = 0;
-                flush = FALSE;
-            }
-       }
-
-    }while(0);
+                       while (rpp_sci_read_nb(1, &input) != SUCCESS) {
+                               sys_check_timeouts();
+                               vTaskDelay(10);
+                       }
+                       break;
+               }
+
+               /*************************************** 2 testing mode *****************************************/
+
+               if (test) { /* -d option; sends packets to the opposite node and doesn't receive anything */
+                       index = 0;
+                       char strbuf[13];
+                       int strlen;
+                       //netconn_set_recvtimeout(newconn, 1);
+                       while (index++ < 4294967295) {
+                               /* allow user to interrupt sending */
+                               if (rpp_sci_read_nb(1, &input) == SUCCESS) break;
+                               strlen = sprintf(strbuf,"%d\r\n",index);
+                               if (!udp) {
+                                       sys_check_timeouts();
+                                       if (tcp_write(tnewpcb, strbuf, strlen, TCP_WRITE_FLAG_COPY) != ERR_OK) {
+                                               rpp_sci_printf("error writing to newpcb - tcp_write testing\n");
+                                               break;
+                                       }
+                                       if (tcp_output(tnewpcb) != ERR_OK) { /* when we want to send data immediately */
+                                               rpp_sci_printf("newpcb output err - tcp testing\n");
+                                               break;
+                                       }
+                               }
+                               else
+                               {
+                                       p = pbuf_alloc(PBUF_TRANSPORT, strlen, PBUF_RAM);
+                                       if (p == NULL) {
+                                               rpp_sci_printf("error allocating pbuf - udp_send testing\n");
+                                               continue;
+                                       }
+                                       if (pbuf_take(p, strbuf, strlen) != ERR_OK) {
+                                               rpp_sci_printf("pbuf mem err (small pbuf) - udp_send testing\n");
+                                               continue;
+                                       }
+                                       if (udp_send(pcb, p) != ERR_OK) {
+                                               rpp_sci_printf("error sending to pcb - udp_send testing\n");
+                                               continue;
+                                       }
+                               }
+                       }
+                       break;
+               }
+
+               /*************************************** 3 interactive mode *****************************************/
+
+               while (!ncStop) {
+                       if (!udp && tnewpcb->state != ESTABLISHED) {
+                               ncStop = TRUE;
+                               break;
+                       }
+                       if (rpp_sci_read_nb(1, &input) != SUCCESS) {
+                               /* updating lwip timers here */
+                               sys_check_timeouts();
+                               continue;
+                       }
+
+                       // Backspace and Delete
+                       if (input == 8 || input == 127) {
+                               if (buff_index > 0) {
+                                       buff_index--;
+                                       rpp_sci_putc('\b');
+                                       rpp_sci_putc(' ' );
+                                       rpp_sci_putc('\b');
+                               }
+
+                               // Line feed or Carriage return
+                       }
+                       else if (input == 10 || input == 13) {
+                               in_buffer[buff_index] = 13;
+                               buff_index++;
+                               in_buffer[buff_index] = 10;
+                               buff_index++;
+                               flush = TRUE;
+                               rpp_sci_putc('\r');
+                               rpp_sci_putc('\n');
+
+                               // If is any printable character
+                       }
+                       else if (isprint(input) || input == 9) {
+
+                               // Store character and increment buffer index
+                               in_buffer[buff_index] = input;
+                               buff_index++;
+                               rpp_sci_putc(input);
+
+                               // Check if buffer is full and force flush
+                               if (buff_index == BUF_SIZE - 3)
+                                       flush = TRUE;
+                               // All other character stops nc
+                       }
+                       else {
+                               ncStop = TRUE;
+                               flush = TRUE;
+                       }
+
+                       if (flush) {
+                               while (!udp && !ncStop && !sent) {
+                                       sys_check_timeouts();
+                                       vTaskDelay(10);
+                               }
+                               if (ncStop) break;
+                               if (!udp) {
+                                       if (tcp_write(tnewpcb, in_buffer, buff_index, TCP_WRITE_FLAG_COPY) != ERR_OK) {
+                                               rpp_sci_printf("error writing to newpcb - tcp_write\n");
+                                               continue;
+                                       }
+                                       if (tcp_output(tnewpcb) != ERR_OK) { /* when we want to send data immediately */
+                                               rpp_sci_printf("newpcb output err - tcp\n");
+                                               continue;
+                                       }
+                               }
+                               else
+                               {
+                                       p = pbuf_alloc(PBUF_TRANSPORT, buff_index, PBUF_RAM);
+                                       if (p == NULL) {
+                                               rpp_sci_printf("error allocating pbuf - udp_send\n");
+                                               continue;
+                                       }
+                                       if (pbuf_take(p, in_buffer, buff_index) != ERR_OK) {
+                                               rpp_sci_printf("pbuf mem err (small pbuf) - udp_send\n");
+                                               continue;
+                                       }
+                                       if (udp_send(pcb, p) != ERR_OK) {
+                                               rpp_sci_printf("error sending to pcb - udp_send\n");
+                                               continue;
+                                       }
+                               }
+                               // Reset variables
+                               sent = FALSE;
+                               buff_index = 0;
+                               flush = FALSE;
+                       }
+               }
+
+       } while (0);
 
 /* close connection and clean */
-       if(!udp)
-       {
+       if (!udp) {
                tcp_recv(tnewpcb, NULL);
                err = tcp_close(tnewpcb);
                if (err != ERR_OK) {
-               rpp_sci_printf("newpcb closing error\n");
-                   /* closing failed, try again later */
-                   LWIP_DEBUGF(LWIP_DBG_ON, ("Error %s closing pcb=0x%08X\n", lwip_strerr(err), pcb));
-                   tcp_recv(tnewpcb, nc_recv_callback);
-               } else {
-                   /* closing succeeded */
-                   tcp_arg(tnewpcb, NULL);
-                   tcp_sent(tnewpcb, NULL);
+                       rpp_sci_printf("newpcb closing error\n");
+                       /* closing failed, try again later */
+                       LWIP_DEBUGF(LWIP_DBG_ON, ("Error %s closing pcb=0x%08X\n", lwip_strerr(err), pcb));
+                       tcp_recv(tnewpcb, nc_recv_callback);
+               }
+               else {
+                       /* closing succeeded */
+                       tcp_arg(tnewpcb, NULL);
+                       tcp_sent(tnewpcb, NULL);
                }
        }
        else
@@ -1005,75 +952,75 @@ nc -l <port> -d [-u] [-t]
                udp_disconnect(pcb);
                udp_remove(pcb);
        }
-    buff_index = 0;
-#endif
-    return ERR_OK;
+       buff_index = 0;
+#endif /* if !NO_SYS */
+       return ERR_OK;
 }
 
-#endif /* DOCGEN */
-
-cmd_des_t const cmd_des_nc={
-    0,CDESM_SPACE_SEP,
-    "ethnc","Start very simple netcat",
-
-    "### Command syntax ###\n"
-    "\n"
-    "     ethnc <IP> <PORT> [-p <PORT>] [-u] [-m [-t] | -d [-t]] [-c]\n"
-    "     ethnc -l <PORT> [-u] [-m [-t] | -d [-t]] [-c]\n"
-    "\n"
-    "### Description ###\n"
-    "\n"
-    "Netcat is a program which allows to communicate using TCP or UDP\n"
-    "protocols. First a connection is established by either:\n"
-    "\n"
-    "- connecting to a specified IP address and PORT (without option -l) or by\n"
-    "- listening for a new connection on a given PORT (with option -l).\n"
-    "\n"
-    "When no -u option is specified ethnc command works with TCP\n"
-    "connections. With -u option UDP communication is used. Listening for\n"
-    "connection on UDP means waiting for reception of any UDP datagram.\n"
-    "\n"
-    "Once the connection is established the command works in one of the\n"
-    "following modes:\n"
-    "\n"
-    "- interactive mode in which received data are forwarded to serial line and data received on serial line are sent to the connection when either new line is encountered or when internal buffer is full,\n"
-    "- sending of testing data (increasing ASCII formatted numbers) (option -d),\n"
-    "- looping of incoming data back to the connection's peer (option -m).\n"
-    "\n"
-    "Note: When trying to use a same local TCP port number multiple times\n"
-    "in a row (-l or -p options) there might be several minutes delay\n"
-    "before the port is available after closing the previous connection.\n"
-    "This situation is singled with ERROR 31.\n"
-    "\n"
-    "Other options:\n"
-    "\n"
-    "- -p specifies local port for outgoing connections.\n"
-    "- -u use UDP protocol instead of the default TCP.\n"
-    "- -t send and/or receive data in a background thread (works only with -d or -m options).\n"
-    "- -c stop all running background tasks\n"
-    "\n"
-    "### Examples ###\n"
-    "\n"
-    "Listen for incoming TCP connection on local port 2000:\n"
-    "     --> ethnc -l 2000\n"
-    "\n"
-    "Connect using TCP to address 192.168.247.15 to remote port 80 using local port 1500:\n"
-    "     --> ethnc 192.168.247.15 80 -p 1500\n"
-    "\n"
-    "Send testing data to the remote node:\n"
-    "     --> ethnc -d 192.168.247.2 1025\n"
-    "\n"
-    "Loop back all data coming from remote node's UDP port 1025:\n"
-    "     --> ethnc -m -u 192.168.247.2 1025\n"
-    "\n"
-    "Wait for a TCP connection on local port 30000 and loop all incoming data\n"
-    "back:\n"
-    "     --> ethnc -l 30000 -m\n",
-    CMD_HANDLER(cmd_do_init_nc), (void *)&cmd_list_nc
+#endif  /* DOCGEN */
+
+cmd_des_t const cmd_des_nc = {
+       0,CDESM_SPACE_SEP,
+       "ethnc","Start very simple netcat",
+
+       "### Command syntax ###\n"
+       "\n"
+       "     ethnc <IP> <PORT> [-p <PORT>] [-u] [-m [-t] | -d [-t]] [-c]\n"
+       "     ethnc -l <PORT> [-u] [-m [-t] | -d [-t]] [-c]\n"
+       "\n"
+       "### Description ###\n"
+       "\n"
+       "Netcat is a program which allows to communicate using TCP or UDP\n"
+       "protocols. First a connection is established by either:\n"
+       "\n"
+       "- connecting to a specified IP address and PORT (without option -l) or by\n"
+       "- listening for a new connection on a given PORT (with option -l).\n"
+       "\n"
+       "When no -u option is specified ethnc command works with TCP\n"
+       "connections. With -u option UDP communication is used. Listening for\n"
+       "connection on UDP means waiting for reception of any UDP datagram.\n"
+       "\n"
+       "Once the connection is established the command works in one of the\n"
+       "following modes:\n"
+       "\n"
+       "- interactive mode in which received data are forwarded to serial line and data received on serial line are sent to the connection when either new line is encountered or when internal buffer is full,\n"
+       "- sending of testing data (increasing ASCII formatted numbers) (option -d),\n"
+       "- looping of incoming data back to the connection's peer (option -m).\n"
+       "\n"
+       "Note: When trying to use a same local TCP port number multiple times\n"
+       "in a row (-l or -p options) there might be several minutes delay\n"
+       "before the port is available after closing the previous connection.\n"
+       "This situation is singled with ERROR 31.\n"
+       "\n"
+       "Other options:\n"
+       "\n"
+       "- -p specifies local port for outgoing connections.\n"
+       "- -u use UDP protocol instead of the default TCP.\n"
+       "- -t send and/or receive data in a background thread (works only with -d or -m options).\n"
+       "- -c stop all running background tasks\n"
+       "\n"
+       "### Examples ###\n"
+       "\n"
+       "Listen for incoming TCP connection on local port 2000:\n"
+       "     --> ethnc -l 2000\n"
+       "\n"
+       "Connect using TCP to address 192.168.247.15 to remote port 80 using local port 1500:\n"
+       "     --> ethnc 192.168.247.15 80 -p 1500\n"
+       "\n"
+       "Send testing data to the remote node:\n"
+       "     --> ethnc -d 192.168.247.2 1025\n"
+       "\n"
+       "Loop back all data coming from remote node's UDP port 1025:\n"
+       "     --> ethnc -m -u 192.168.247.2 1025\n"
+       "\n"
+       "Wait for a TCP connection on local port 30000 and loop all incoming data\n"
+       "back:\n"
+       "     --> ethnc -l 30000 -m\n",
+       CMD_HANDLER(cmd_do_init_nc), (void *)&cmd_list_nc
 };
 
 /** List of commands for lwip, defined as external */
-cmd_des_t const *cmd_list_nc[]={
-  &cmd_des_nc,
-  NULL
+cmd_des_t const *cmd_list_nc[] = {
+       &cmd_des_nc,
+       NULL
 };