UDP comms to ROV

Revision:
1:f51739cada16
Parent:
0:4a75d653c18c
Child:
2:48524892982c
diff -r 4a75d653c18c -r f51739cada16 RovComms.cpp
--- a/RovComms.cpp	Fri Jan 13 20:24:19 2012 +0000
+++ b/RovComms.cpp	Mon Jan 16 17:17:58 2012 +0000
@@ -32,60 +32,32 @@
         printf("Error %d in setup.\n", ethErr);
         //return -1;
     }
-   InitRx(addr);
+   debug("Setting up Tx");
    if (addr == ROV_ADDR)
-       InitTx(HANDSET_ADDR);
+       InitUDP(HANDSET_ADDR);
    else
-       InitTx(ROV_ADDR);
+       InitUDP(ROV_ADDR);
+
+   debug("ROV Comms done");
 } // RovComms()
 
-void RovComms::InitRx(int addr)
+
+void RovComms::InitUDP(int addr)
 {
-    TCPSocketErr err;
-    RxSock = new TCPSocket();
-    RxSock->setOnEvent(this, &RovComms::onRxTCPSocketEvent);
-    err = RxSock->bind(Host(IpAddr(), RX_TCP_LISTENING_PORT));
-    if(err)
-    {
-        //Deal with that error...
-    } else {
-        // set the socket to listen
-        err = RxSock->listen(); //Starts listening
-        if (err)
-            CloseSocket(RxSock, &RxNetworkState);
-    }
+    UdpSkt = new UDPSocket();
+    server = Host( IpAddr(/*10,0,0,addr*/), RX_TCP_LISTENING_PORT, NULL);
+    UdpSkt->setOnEvent(this, &RovComms::onUDPSocketEvent);
+    UdpSkt->bind(server);
 }
 
-void RovComms::CloseSocket(TCPSocket *s, int *state)
+
+void RovComms::CloseSocket(UDPSocket *s, int *state)
 {
     s->close();
     *state = CLOSED;
 }
 
 
-void RovComms::InitTx(int addr)
-{
-    TCPSocketErr bindErr;
-    Host server(IpAddr(10,0,0,addr), RX_TCP_LISTENING_PORT); // ip addr and port nos
-            
-    bindErr = TxSock->connect(server);
-    switch(bindErr)
-    {
-        case TCPSOCKET_CONNECTED:
-            TxNetworkState = CONNECTED;
-            debug("RovComms::InitSlave -> connected");
-            break;
-            
-        case TCPSOCKET_CONTIMEOUT:
-        case TCPSOCKET_CONRST:
-        case TCPSOCKET_CONABRT:
-        case TCPSOCKET_ERROR:
-        default:
-            CloseSocket(TxSock, &TxNetworkState);
-            break;
-    } // switch
-}
- 
 /* @fn Transmit(char *data)
  * Sends the buffer to the other end
  */
@@ -137,8 +109,8 @@
             return;
     } // switch pktType
        
-    int  errCode = TxSock->send(iTxData, MAX_BUFFER_SIZE);
-    if (errCode < 0) // TCPSocketErr
+    int  errCode = UdpSkt->sendto(iTxData, MAX_BUFFER_SIZE, NULL);
+    if (errCode < 0) // UDPSocketErr
     {
     }
     Net::poll(); // tells stack to process the info (transfer from here to the stack and tx it)
@@ -146,98 +118,37 @@
 
 char *RovComms::Receive(void)
 {
-    char *buf={0};
     int errCode = 0;
     Net::poll(); // tell the stack to transfer the data from it's stack to the readable one here
-    errCode = RxSock->recv(buf, MAX_BUFFER_SIZE);
+    errCode = UdpSkt->recvfrom(iRxData, MAX_BUFFER_SIZE, &server);
     if (errCode < 0) // TCPSocketErr
     {
     }
     // decode the packet.....
-    debugInt("Pkt type = ", buf[0]);
+    debugInt("Pkt type = ", iRxData[0]);
     for (int i=1;i<MAX_BUFFER_SIZE;i++)
-        debugInt("Pkt data = ", buf[i]);
-    return buf;
+        debugInt("Pkt data = ", iRxData[i]);
+    return iRxData;
 }
 
-
-void RovComms::onConnectedTCPSocketEvent(TCPSocketEvent e)
-{
-   switch(e)
-   {
-    case TCPSOCKET_ACCEPT:
-        break;
-
-    case TCPSOCKET_READABLE: 
-        Receive();
-        break;
-        
-    default:
-        break;
-   }
-}
- 
-void RovComms::onRxTCPSocketEvent(TCPSocketEvent e)
+void RovComms::onUDPSocketEvent(UDPSocketEvent e)
 {
-   TCPSocket* pConnectedSock;
-   Host client;
-   TCPSocketErr err;
-   switch (e)
-   {
-    case TCPSOCKET_ACCEPT:
-        err = RxSock->accept(&client, &pConnectedSock);
-        if(!err)
-            pConnectedSock->setOnEvent(this, &RovComms::onConnectedTCPSocketEvent); //Setup the new socket events   case TCPSOCKET_READABLE: //The only event for now
-        break;
-     
-    case TCPSOCKET_READABLE: 
-        Receive();
-        break;
-        
-    case TCPSOCKET_WRITEABLE: 
-        // continue the write process
-        break;
+    switch (e)
+    {
+        case UDPSOCKET_READABLE:
+        {
+            Host client;
+            int len = UdpSkt->recvfrom(iRxData, MAX_BUFFER_SIZE, &client);
+            //printf("Connected from %d.%d.%d.%d\r\n", (unsigned char)client->getIp()[0], (unsigned char)client->getIp()[1], 
+            //   (unsigned char)client->getIp()[2], (unsigned char)client->getIp()[3]);
+            break;
+        }
         
-    case TCPSOCKET_CONNECTED:
-    case TCPSOCKET_CONTIMEOUT:
-    case TCPSOCKET_CONRST:
-    case TCPSOCKET_CONABRT:
-    case TCPSOCKET_ERROR:
-    default:
-        break;
-   }     // switch(e)
-} // onRxTCPSocketEvent()
+        default:
+            break;
+    } // end switch
+}
 
-// Slave socket event handler
-void RovComms::onTxTCPSocketEvent(TCPSocketEvent e)
-{
-   TCPSocket* pConnectedSock;
-   Host client;
-   TCPSocketErr err;
-   switch (e)
-   {
-    case TCPSOCKET_ACCEPT:
-        // this event should never happen as a slave!!!
-        break;
-     
-    case TCPSOCKET_READABLE: 
-        // don't think this event should happen
-        Receive();
-        break;
-        
-    case TCPSOCKET_WRITEABLE: 
-        // continue the write process
-        break;
-        
-    case TCPSOCKET_CONNECTED:
-    case TCPSOCKET_CONTIMEOUT:
-    case TCPSOCKET_CONRST:
-    case TCPSOCKET_CONABRT:
-    case TCPSOCKET_ERROR:
-    default:
-        break;
-   }     // switch(e)
-} // onSlaveTCPSocketEvent()
 
 void RovComms::ResetValues()
 {