Same as mallet... but distance

Dependencies:   EthernetInterface NetworkAPI mbed-rtos mbed

Fork of MalletFirmware by Impact-Echo

Files at this revision

API Documentation at this revision

Comitter:
timmey9
Date:
Tue Nov 18 01:31:55 2014 +0000
Parent:
19:8d4e336fc723
Child:
21:1fb5023b72af
Commit message:
working server

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Nov 14 20:48:27 2014 +0000
+++ b/main.cpp	Tue Nov 18 01:31:55 2014 +0000
@@ -1,37 +1,56 @@
-#define SERVER 1
-
-
-
-#if SERVER == 1
+// Server code
 
 #include "mbed.h"
 #include "EthernetInterface.h"
- 
+
 #include "NetworkAPI/buffer.hpp"
 #include "NetworkAPI/select.hpp"
 #include "NetworkAPI/ip/address.hpp"
 #include "NetworkAPI/tcp/socket.hpp"
 
+#define STATIC 1
+#ifdef STATIC
+#define IP "127.0.0.5" // "192.168.1.5"
+#define MASK "255.255.255.0"
+#define GATEWAY "127.0.0.1"//"192.168.1.1"
+#define MAC "00:02:f7:f0:00:00"
+#endif
+
+#define PORT 23
+
 Serial pc(USBTX, USBRX);
 DigitalOut led_red(LED_RED);
 DigitalOut led_green(LED_GREEN);
 DigitalOut led_blue(LED_BLUE);
 
+uint32_t sample_array[30000];
+
 using namespace network;
  
 #define MAX_CLIENTS 5
  
 int main() {
+    for(int i = 0; i < 30000; i++) sample_array[i] = 0x3132;
     led_red = 1;
     led_green = 1;
     led_blue = 1;
     pc.baud(230400);
     pc.printf("Starting Server\r\n");
+    
     EthernetInterface interface;
+    #ifdef STATIC
+    interface.init(IP, MASK, GATEWAY);
+    #else
     interface.init();
+    #endif
     interface.connect();
-    pc.printf("IP Address is %s\n\r", interface.getIPAddress());
-     
+    pc.printf("IP Address is: %s\n\r", interface.getIPAddress());
+    pc.printf("Network Mask is: %s\n\r", interface.getNetworkMask());
+    pc.printf("MAC address is: %s\n\r", interface.getMACAddress());
+    pc.printf("Gateway is: %s\n\r", interface.getGateway());
+    
+    
+    
     Select select;
     tcp::Socket server;
     tcp::Socket client[MAX_CLIENTS];
@@ -45,7 +64,7 @@
      
     // Configure the server socket (assume everty thing works)
     server.open();
-    server.bind(1234);
+    server.bind(PORT);
     server.listen(MAX_CLIENTS);
    
     // Add sockets to the select api
@@ -80,7 +99,7 @@
                     pc.printf("Maximum connections reached\n\r");
                     continue;
                 }
-             
+                
                 // Accept the client
                 socket->accept(client[index]);
                 pc.printf("Client connected %s:%d\n\r",
@@ -89,9 +108,16 @@
                      
                 // Send a nice message to the client
                 client[index].write((void *)message.data(), message.size());
+                
+                // read some registers for some info.
+                //uint32_t* rcr = (uint32_t*) 0x400C0084;
+                //uint32_t* ecr = (uint32_t*) 0x400C0024;
+                //pc.printf("RCR register: %x\r\n", *rcr);
+                //pc.printf("ECR register: %x\r\n", *ecr);
+                
                 continue;
             }
-             
+            
             // It was not the server socket, so it must be a client talking to us.
             switch (socket->read(buffer)) {
                 case 0:
@@ -103,81 +129,38 @@
                     // Close socket
                     socket->close();
                     break;
-                 
+                
                 case -1:
                     pc.printf("Error while reading data from socket\n\r");
                     socket->close();
                     break;
-                 
+//************* this is where data is printed to the screen
                 default:
                     pc.printf("Message from %s:%d\n\r",
                         socket->getRemoteEndpoint().getAddress().toString().c_str(),
                         socket->getRemoteEndpoint().getPort());
                          
-                    pc.printf("%s\n\r", (char *)buffer.data());
+                    //pc.printf("%s\n\r", (char *)buffer.data());
+                    
+//***************** print a message back to the client
+                    Timer timeStamp;
+                    timeStamp.stop();
+                    timeStamp.reset();
+                    timeStamp.start();
+                    client[index].write((void *)&sample_array,30000);
+                    int timeStampVar = timeStamp.read_us();
+                    timeStamp.stop();
+                    wait(1);
+                    pc.printf("*******\r\nTime taken to send data: %i\r\n", timeStampVar);
+                    
+                    char premessage[40];
+                    sprintf(premessage, "Time taken to send data: %i\r\n", timeStampVar);
+                    std::string response1 = premessage;
+                    client[index].write((void *)response1.data(), response1.size());
+                    
                     break;
             }
         }
              
     } while (server.getStatus() == network::Socket::Listening);
-}
-
-
-
-#else // client
-
-#include "mbed.h"
-#include "EthernetInterface.h"
- 
-#include "NetworkAPI/buffer.hpp"
-#include "NetworkAPI/ip/address.hpp"
-#include "NetworkAPI/tcp/socket.hpp"
-
-Serial pc(USBTX, USBRX);
-DigitalOut led_red(LED_RED);
-DigitalOut led_green(LED_GREEN);
-DigitalOut led_blue(LED_BLUE);
- 
-int main() {
-    led_red = 1;
-    led_green = 1;
-    led_blue = 1;
-    pc.baud(230400);
-    pc.printf("Starting Client\r\n");
-    EthernetInterface interface;
-    interface.init();
-    interface.connect();
-    pc.printf("IP Address is %s\n\r", interface.getIPAddress());
-  
-    int result;
-  
-    network::tcp::Socket socket;
-    network::Buffer buffer(256);
-    std::string request("GET /media/uploads/donatien/hello.txt HTTP/1.1\r\nHost: %s\r\n\r\n");
-    
-    if (socket.open() < 0) {
-        pc.printf("Failed to open TCP Socket\n\r");
-        return -1;
-    }
-    
-    if (socket.connect("mbed.org", 80) < 0) {
-        pc.printf("Failed to connect with mbed.org\n\r");
-        return -1;
-    }
-    
-    if (socket.write((void *)request.data(), request.size()) < 0) {
-        pc.printf("Failed to write HTTP request\n\r");
-        return -1;
-    }
-    
-    do
-    {
-        result = socket.read(buffer);   
-        pc.printf("Received %d bytes:\n\r%s\n\r", result, (char *)buffer.data(0));
-    } while(result > 0);
-    
-    socket.close();
-    return 0;
-}
-
-#endif
\ No newline at end of file
+}
\ No newline at end of file