Jared Baxter
/
Impedance_Fast_Circuitry
Impedance Fast Circuitry Software
Fork of DSP_200kHz by
main.cpp@20:f533b3c9296f, 2014-11-18 (annotated)
- Committer:
- timmey9
- Date:
- Tue Nov 18 01:31:55 2014 +0000
- Revision:
- 20:f533b3c9296f
- Parent:
- 18:b17ddeeb1c09
- Child:
- 21:1fb5023b72af
working server
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
timmey9 | 20:f533b3c9296f | 1 | // Server code |
timmey9 | 16:c3f922f61b8f | 2 | |
donatien | 0:bb128f0e952f | 3 | #include "mbed.h" |
donatien | 0:bb128f0e952f | 4 | #include "EthernetInterface.h" |
timmey9 | 20:f533b3c9296f | 5 | |
timmey9 | 17:2f978f823020 | 6 | #include "NetworkAPI/buffer.hpp" |
timmey9 | 17:2f978f823020 | 7 | #include "NetworkAPI/select.hpp" |
timmey9 | 17:2f978f823020 | 8 | #include "NetworkAPI/ip/address.hpp" |
timmey9 | 17:2f978f823020 | 9 | #include "NetworkAPI/tcp/socket.hpp" |
timmey9 | 18:b17ddeeb1c09 | 10 | |
timmey9 | 20:f533b3c9296f | 11 | #define STATIC 1 |
timmey9 | 20:f533b3c9296f | 12 | #ifdef STATIC |
timmey9 | 20:f533b3c9296f | 13 | #define IP "127.0.0.5" // "192.168.1.5" |
timmey9 | 20:f533b3c9296f | 14 | #define MASK "255.255.255.0" |
timmey9 | 20:f533b3c9296f | 15 | #define GATEWAY "127.0.0.1"//"192.168.1.1" |
timmey9 | 20:f533b3c9296f | 16 | #define MAC "00:02:f7:f0:00:00" |
timmey9 | 20:f533b3c9296f | 17 | #endif |
timmey9 | 20:f533b3c9296f | 18 | |
timmey9 | 20:f533b3c9296f | 19 | #define PORT 23 |
timmey9 | 20:f533b3c9296f | 20 | |
timmey9 | 18:b17ddeeb1c09 | 21 | Serial pc(USBTX, USBRX); |
timmey9 | 18:b17ddeeb1c09 | 22 | DigitalOut led_red(LED_RED); |
timmey9 | 18:b17ddeeb1c09 | 23 | DigitalOut led_green(LED_GREEN); |
timmey9 | 18:b17ddeeb1c09 | 24 | DigitalOut led_blue(LED_BLUE); |
timmey9 | 18:b17ddeeb1c09 | 25 | |
timmey9 | 20:f533b3c9296f | 26 | uint32_t sample_array[30000]; |
timmey9 | 20:f533b3c9296f | 27 | |
timmey9 | 17:2f978f823020 | 28 | using namespace network; |
timmey9 | 17:2f978f823020 | 29 | |
timmey9 | 17:2f978f823020 | 30 | #define MAX_CLIENTS 5 |
timmey9 | 17:2f978f823020 | 31 | |
emilmont | 7:65188f4a8c25 | 32 | int main() { |
timmey9 | 20:f533b3c9296f | 33 | for(int i = 0; i < 30000; i++) sample_array[i] = 0x3132; |
timmey9 | 18:b17ddeeb1c09 | 34 | led_red = 1; |
timmey9 | 18:b17ddeeb1c09 | 35 | led_green = 1; |
timmey9 | 18:b17ddeeb1c09 | 36 | led_blue = 1; |
timmey9 | 18:b17ddeeb1c09 | 37 | pc.baud(230400); |
timmey9 | 18:b17ddeeb1c09 | 38 | pc.printf("Starting Server\r\n"); |
timmey9 | 20:f533b3c9296f | 39 | |
timmey9 | 17:2f978f823020 | 40 | EthernetInterface interface; |
timmey9 | 20:f533b3c9296f | 41 | #ifdef STATIC |
timmey9 | 20:f533b3c9296f | 42 | interface.init(IP, MASK, GATEWAY); |
timmey9 | 20:f533b3c9296f | 43 | #else |
timmey9 | 17:2f978f823020 | 44 | interface.init(); |
timmey9 | 20:f533b3c9296f | 45 | #endif |
timmey9 | 17:2f978f823020 | 46 | interface.connect(); |
timmey9 | 20:f533b3c9296f | 47 | pc.printf("IP Address is: %s\n\r", interface.getIPAddress()); |
timmey9 | 20:f533b3c9296f | 48 | pc.printf("Network Mask is: %s\n\r", interface.getNetworkMask()); |
timmey9 | 20:f533b3c9296f | 49 | pc.printf("MAC address is: %s\n\r", interface.getMACAddress()); |
timmey9 | 20:f533b3c9296f | 50 | pc.printf("Gateway is: %s\n\r", interface.getGateway()); |
timmey9 | 20:f533b3c9296f | 51 | |
timmey9 | 20:f533b3c9296f | 52 | |
timmey9 | 20:f533b3c9296f | 53 | |
timmey9 | 17:2f978f823020 | 54 | Select select; |
timmey9 | 17:2f978f823020 | 55 | tcp::Socket server; |
timmey9 | 17:2f978f823020 | 56 | tcp::Socket client[MAX_CLIENTS]; |
timmey9 | 17:2f978f823020 | 57 | tcp::Socket *socket = NULL; |
timmey9 | 17:2f978f823020 | 58 | |
timmey9 | 17:2f978f823020 | 59 | int result = 0; |
timmey9 | 17:2f978f823020 | 60 | int index = 0; |
timmey9 | 17:2f978f823020 | 61 | |
timmey9 | 17:2f978f823020 | 62 | network::Buffer buffer(256); |
timmey9 | 17:2f978f823020 | 63 | std::string message("Hello world!"); |
timmey9 | 17:2f978f823020 | 64 | |
timmey9 | 17:2f978f823020 | 65 | // Configure the server socket (assume everty thing works) |
timmey9 | 17:2f978f823020 | 66 | server.open(); |
timmey9 | 20:f533b3c9296f | 67 | server.bind(PORT); |
timmey9 | 17:2f978f823020 | 68 | server.listen(MAX_CLIENTS); |
timmey9 | 17:2f978f823020 | 69 | |
timmey9 | 17:2f978f823020 | 70 | // Add sockets to the select api |
timmey9 | 17:2f978f823020 | 71 | select.set(&server, Select::Read); |
timmey9 | 17:2f978f823020 | 72 | for (index = 0; index < MAX_CLIENTS; index++) { |
timmey9 | 17:2f978f823020 | 73 | select.set(&client[index], Select::Read); |
timmey9 | 17:2f978f823020 | 74 | } |
timmey9 | 17:2f978f823020 | 75 | |
timmey9 | 17:2f978f823020 | 76 | do { |
timmey9 | 17:2f978f823020 | 77 | // Wait for activity |
timmey9 | 17:2f978f823020 | 78 | result = select.wait(); |
timmey9 | 17:2f978f823020 | 79 | if (result < -1) { |
timmey9 | 18:b17ddeeb1c09 | 80 | pc.printf("Failed to select\n\r"); |
emilmont | 7:65188f4a8c25 | 81 | break; |
timmey9 | 16:c3f922f61b8f | 82 | } |
timmey9 | 17:2f978f823020 | 83 | |
timmey9 | 17:2f978f823020 | 84 | // Get the first socket |
timmey9 | 17:2f978f823020 | 85 | socket = (tcp::Socket *)select.getReadable(); |
timmey9 | 17:2f978f823020 | 86 | |
timmey9 | 17:2f978f823020 | 87 | for (; socket != NULL; socket = (tcp::Socket *)select.getReadable()) { |
timmey9 | 17:2f978f823020 | 88 | // Check if there was a connection request. |
timmey9 | 17:2f978f823020 | 89 | if (socket->getHandle() == server.getHandle()) { |
timmey9 | 17:2f978f823020 | 90 | // Find an unused client |
timmey9 | 17:2f978f823020 | 91 | for (index = 0; index < MAX_CLIENTS; index++) { |
timmey9 | 17:2f978f823020 | 92 | if (client[index].getStatus() == network::Socket::Closed) { |
timmey9 | 17:2f978f823020 | 93 | break; |
timmey9 | 17:2f978f823020 | 94 | } |
timmey9 | 17:2f978f823020 | 95 | } |
timmey9 | 17:2f978f823020 | 96 | |
timmey9 | 17:2f978f823020 | 97 | // Maximum connections reached |
timmey9 | 17:2f978f823020 | 98 | if (index == MAX_CLIENTS) { |
timmey9 | 18:b17ddeeb1c09 | 99 | pc.printf("Maximum connections reached\n\r"); |
timmey9 | 17:2f978f823020 | 100 | continue; |
timmey9 | 17:2f978f823020 | 101 | } |
timmey9 | 20:f533b3c9296f | 102 | |
timmey9 | 17:2f978f823020 | 103 | // Accept the client |
timmey9 | 17:2f978f823020 | 104 | socket->accept(client[index]); |
timmey9 | 18:b17ddeeb1c09 | 105 | pc.printf("Client connected %s:%d\n\r", |
timmey9 | 17:2f978f823020 | 106 | client[index].getRemoteEndpoint().getAddress().toString().c_str(), |
timmey9 | 17:2f978f823020 | 107 | client[index].getRemoteEndpoint().getPort()); |
timmey9 | 17:2f978f823020 | 108 | |
timmey9 | 17:2f978f823020 | 109 | // Send a nice message to the client |
timmey9 | 17:2f978f823020 | 110 | client[index].write((void *)message.data(), message.size()); |
timmey9 | 20:f533b3c9296f | 111 | |
timmey9 | 20:f533b3c9296f | 112 | // read some registers for some info. |
timmey9 | 20:f533b3c9296f | 113 | //uint32_t* rcr = (uint32_t*) 0x400C0084; |
timmey9 | 20:f533b3c9296f | 114 | //uint32_t* ecr = (uint32_t*) 0x400C0024; |
timmey9 | 20:f533b3c9296f | 115 | //pc.printf("RCR register: %x\r\n", *rcr); |
timmey9 | 20:f533b3c9296f | 116 | //pc.printf("ECR register: %x\r\n", *ecr); |
timmey9 | 20:f533b3c9296f | 117 | |
timmey9 | 17:2f978f823020 | 118 | continue; |
timmey9 | 16:c3f922f61b8f | 119 | } |
timmey9 | 20:f533b3c9296f | 120 | |
timmey9 | 17:2f978f823020 | 121 | // It was not the server socket, so it must be a client talking to us. |
timmey9 | 17:2f978f823020 | 122 | switch (socket->read(buffer)) { |
timmey9 | 17:2f978f823020 | 123 | case 0: |
timmey9 | 17:2f978f823020 | 124 | // Remote end disconnected |
timmey9 | 18:b17ddeeb1c09 | 125 | pc.printf("Client disconnected %s:%d\n\r", |
timmey9 | 17:2f978f823020 | 126 | socket->getRemoteEndpoint().getAddress().toString().c_str(), |
timmey9 | 17:2f978f823020 | 127 | socket->getRemoteEndpoint().getPort()); |
timmey9 | 17:2f978f823020 | 128 | |
timmey9 | 17:2f978f823020 | 129 | // Close socket |
timmey9 | 17:2f978f823020 | 130 | socket->close(); |
timmey9 | 17:2f978f823020 | 131 | break; |
timmey9 | 20:f533b3c9296f | 132 | |
timmey9 | 17:2f978f823020 | 133 | case -1: |
timmey9 | 18:b17ddeeb1c09 | 134 | pc.printf("Error while reading data from socket\n\r"); |
timmey9 | 17:2f978f823020 | 135 | socket->close(); |
timmey9 | 17:2f978f823020 | 136 | break; |
timmey9 | 20:f533b3c9296f | 137 | //************* this is where data is printed to the screen |
timmey9 | 17:2f978f823020 | 138 | default: |
timmey9 | 18:b17ddeeb1c09 | 139 | pc.printf("Message from %s:%d\n\r", |
timmey9 | 17:2f978f823020 | 140 | socket->getRemoteEndpoint().getAddress().toString().c_str(), |
timmey9 | 17:2f978f823020 | 141 | socket->getRemoteEndpoint().getPort()); |
timmey9 | 17:2f978f823020 | 142 | |
timmey9 | 20:f533b3c9296f | 143 | //pc.printf("%s\n\r", (char *)buffer.data()); |
timmey9 | 20:f533b3c9296f | 144 | |
timmey9 | 20:f533b3c9296f | 145 | //***************** print a message back to the client |
timmey9 | 20:f533b3c9296f | 146 | Timer timeStamp; |
timmey9 | 20:f533b3c9296f | 147 | timeStamp.stop(); |
timmey9 | 20:f533b3c9296f | 148 | timeStamp.reset(); |
timmey9 | 20:f533b3c9296f | 149 | timeStamp.start(); |
timmey9 | 20:f533b3c9296f | 150 | client[index].write((void *)&sample_array,30000); |
timmey9 | 20:f533b3c9296f | 151 | int timeStampVar = timeStamp.read_us(); |
timmey9 | 20:f533b3c9296f | 152 | timeStamp.stop(); |
timmey9 | 20:f533b3c9296f | 153 | wait(1); |
timmey9 | 20:f533b3c9296f | 154 | pc.printf("*******\r\nTime taken to send data: %i\r\n", timeStampVar); |
timmey9 | 20:f533b3c9296f | 155 | |
timmey9 | 20:f533b3c9296f | 156 | char premessage[40]; |
timmey9 | 20:f533b3c9296f | 157 | sprintf(premessage, "Time taken to send data: %i\r\n", timeStampVar); |
timmey9 | 20:f533b3c9296f | 158 | std::string response1 = premessage; |
timmey9 | 20:f533b3c9296f | 159 | client[index].write((void *)response1.data(), response1.size()); |
timmey9 | 20:f533b3c9296f | 160 | |
timmey9 | 17:2f978f823020 | 161 | break; |
timmey9 | 17:2f978f823020 | 162 | } |
timmey9 | 16:c3f922f61b8f | 163 | } |
timmey9 | 17:2f978f823020 | 164 | |
timmey9 | 17:2f978f823020 | 165 | } while (server.getStatus() == network::Socket::Listening); |
timmey9 | 20:f533b3c9296f | 166 | } |