TCP/IP based digital io controller for operating DigitalsOuts and reading DigitalIns.
Dependencies: EthernetInterface NetworkAPI mbed-rtos mbed
Fork of NetRelais by
Diff: main.cpp
- Revision:
- 10:22d49341340c
- Parent:
- 9:a4c85bea2d77
--- a/main.cpp Wed Jul 18 18:45:57 2012 +0000 +++ b/main.cpp Thu Jul 19 11:13:50 2012 +0000 @@ -1,13 +1,33 @@ +/** + * Copyright (c) 2012, Roy van Dam <roy@vandam-innovations.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + #include "mbed.h" #include "EthernetInterface.h" -#include "NetworkAPI/buffer.hpp" -#include "NetworkAPI/select.hpp" -#include "NetworkAPI/ip/address.hpp" -#include "NetworkAPI/tcp/socket.hpp" -using namespace network; - -#define MAX_CLIENTS 5 +#include "io.hpp" +#include "controller.hpp" int main() @@ -15,94 +35,22 @@ EthernetInterface interface; interface.init(); interface.connect(); - printf("IP Address is %s\n\r", interface.getIPAddress()); - Select select; - tcp::Socket server; - tcp::Socket client[MAX_CLIENTS]; - tcp::Socket *socket = NULL; - - int result = 0; - int index = 0; + printf("IP Address: %s\n\r", interface.getIPAddress()); - network::Buffer buffer(256); - std::string message("Hello world!"); - - // Configure the server socket (assume everty thing works) - server.open(); - server.bind(1234); - server.listen(MAX_CLIENTS); - - // Add sockets to the select api - select.set(&server, Select::Read); - for (index = 0; index < MAX_CLIENTS; index++) { - select.set(&client[index], Select::Read); + Controller controller; + for (int i = 0; i < 5; i++) { + controller.addOutput(&io::output[i]); } - do { - // Wait for activity - result = select.wait(); - if (result < -1) { - printf("Failed to select\n\r"); - break; - } - - // Get the first socket - socket = (tcp::Socket *)select.getReadable(); - - for (; socket != NULL; socket = (tcp::Socket *)select.getReadable()) { - // Check if there was a connection request. - if (socket->getHandle() == server.getHandle()) { - // Find an unused client - for (index = 0; index < MAX_CLIENTS; index++) { - if (client[index].getStatus() == Socket::Closed) { - break; - } - } - - // Maximum connections reached - if (index == MAX_CLIENTS) { - printf("Maximum connections reached\n\r"); - continue; - } - - // Accept the client - socket->accept(client[index]); - printf("Client connected %s:%d\n\r", - client[index].getRemoteEndpoint().getAddress().toString().c_str(), - client[index].getRemoteEndpoint().getPort()); - - // Send a nice message to the client - client[index].write((void *)message.data(), message.size()); - continue; - } - - // It was not the server socket, so it must be a client talking to us. - switch (socket->read(buffer)) { - case 0: - // Remote end disconnected - printf("Client disconnected %s:%d\n\r", - socket->getRemoteEndpoint().getAddress().toString().c_str(), - socket->getRemoteEndpoint().getPort()); - - // Close socket - socket->close(); - break; - - case -1: - printf("Error while reading data from socket\n\r"); - socket->close(); - break; - - default: - printf("Message from %s:%d\n\r", - socket->getRemoteEndpoint().getAddress().toString().c_str(), - socket->getRemoteEndpoint().getPort()); - - printf("%s\n\r", (char *)buffer.pointer()); - break; - } - } - - } while (server.getStatus() == Socket::Listening); + for (int i = 0; i < 4; i++) { + controller.addOutput(&io::led[i]); + } + + for (int i = 0; i < 4; i++) { + controller.addInput(&io::input[i]); + } + + controller.start(); + controller.dispatch(); } \ No newline at end of file