Program the control the fischertechnik robo interface or intelligent interface via tcp socket or via a java gui.

Dependencies:   mbed ConfigFile

Committer:
networker
Date:
Wed May 04 15:42:13 2011 +0000
Revision:
1:2c9d412ad471
Parent:
0:7f26f0680202
first publication (mbed challenge submission)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
networker 0:7f26f0680202 1
networker 0:7f26f0680202 2 /*
networker 0:7f26f0680202 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
networker 0:7f26f0680202 4
networker 0:7f26f0680202 5 Permission is hereby granted, free of charge, to any person obtaining a copy
networker 0:7f26f0680202 6 of this software and associated documentation files (the "Software"), to deal
networker 0:7f26f0680202 7 in the Software without restriction, including without limitation the rights
networker 0:7f26f0680202 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
networker 0:7f26f0680202 9 copies of the Software, and to permit persons to whom the Software is
networker 0:7f26f0680202 10 furnished to do so, subject to the following conditions:
networker 0:7f26f0680202 11
networker 0:7f26f0680202 12 The above copyright notice and this permission notice shall be included in
networker 0:7f26f0680202 13 all copies or substantial portions of the Software.
networker 0:7f26f0680202 14
networker 0:7f26f0680202 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
networker 0:7f26f0680202 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
networker 0:7f26f0680202 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
networker 0:7f26f0680202 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
networker 0:7f26f0680202 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
networker 0:7f26f0680202 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
networker 0:7f26f0680202 21 THE SOFTWARE.
networker 0:7f26f0680202 22 */
networker 0:7f26f0680202 23
networker 0:7f26f0680202 24 /** \file
networker 0:7f26f0680202 25 TCP Socket header file
networker 0:7f26f0680202 26 */
networker 0:7f26f0680202 27
networker 0:7f26f0680202 28 #ifndef TCPSOCKET_H
networker 0:7f26f0680202 29 #define TCPSOCKET_H
networker 0:7f26f0680202 30
networker 0:7f26f0680202 31 #include "core/net.h"
networker 0:7f26f0680202 32 #include "core/host.h"
networker 0:7f26f0680202 33 //Essentially it is a safe interface to NetTcpSocket
networker 0:7f26f0680202 34
networker 0:7f26f0680202 35 ///TCP Socket error codes
networker 0:7f26f0680202 36 enum TCPSocketErr
networker 0:7f26f0680202 37 {
networker 0:7f26f0680202 38 __TCPSOCKET_MIN = -0xFFFF,
networker 0:7f26f0680202 39 TCPSOCKET_SETUP, ///<TCPSocket not properly configured
networker 0:7f26f0680202 40 TCPSOCKET_TIMEOUT, ///<Connection timed out
networker 0:7f26f0680202 41 TCPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized
networker 0:7f26f0680202 42 TCPSOCKET_MEM, ///<Not enough mem
networker 0:7f26f0680202 43 TCPSOCKET_INUSE, ///<Interface / Port is in use
networker 0:7f26f0680202 44 TCPSOCKET_EMPTY, ///<Connections queue is empty
networker 0:7f26f0680202 45 TCPSOCKET_RST, ///<Connection was reset by remote host
networker 0:7f26f0680202 46 //...
networker 0:7f26f0680202 47 TCPSOCKET_OK = 0 ///<Success
networker 0:7f26f0680202 48 };
networker 0:7f26f0680202 49
networker 0:7f26f0680202 50 ///TCP Socket Events
networker 0:7f26f0680202 51 enum TCPSocketEvent
networker 0:7f26f0680202 52 {
networker 0:7f26f0680202 53 TCPSOCKET_CONNECTED, ///<Connected to host
networker 0:7f26f0680202 54 TCPSOCKET_ACCEPT, ///<Client is connected, must call accept() to get a new Socket
networker 0:7f26f0680202 55 TCPSOCKET_READABLE, ///<Data in buf
networker 0:7f26f0680202 56 TCPSOCKET_WRITEABLE, ///<Can write data to buf
networker 0:7f26f0680202 57 TCPSOCKET_CONTIMEOUT, ///<Connection timed out
networker 0:7f26f0680202 58 TCPSOCKET_CONRST, ///<Connection was reset by remote host
networker 0:7f26f0680202 59 TCPSOCKET_CONABRT, ///<Connection was aborted
networker 0:7f26f0680202 60 TCPSOCKET_ERROR, ///<Unknown error
networker 0:7f26f0680202 61 TCPSOCKET_DISCONNECTED ///<Disconnected
networker 0:7f26f0680202 62 };
networker 0:7f26f0680202 63
networker 0:7f26f0680202 64 class NetTcpSocket;
networker 0:7f26f0680202 65 enum NetTcpSocketEvent;
networker 0:7f26f0680202 66
networker 0:7f26f0680202 67 ///This is a simple TCP Socket class
networker 0:7f26f0680202 68 /**
networker 0:7f26f0680202 69 This class exposes an API to deal with TCP Sockets
networker 0:7f26f0680202 70 */
networker 0:7f26f0680202 71 class TCPSocket
networker 0:7f26f0680202 72 {
networker 0:7f26f0680202 73 public:
networker 0:7f26f0680202 74 ///Creates a new socket
networker 0:7f26f0680202 75 TCPSocket();
networker 0:7f26f0680202 76 protected:
networker 0:7f26f0680202 77 TCPSocket(NetTcpSocket* pNetTcpSocket);
networker 0:7f26f0680202 78 public:
networker 0:7f26f0680202 79 ///Closes if needed and destroys the socket
networker 0:7f26f0680202 80 ~TCPSocket(); //close()
networker 0:7f26f0680202 81
networker 0:7f26f0680202 82 ///Binds the socket to (local) host
networker 0:7f26f0680202 83 TCPSocketErr bind(const Host& me);
networker 0:7f26f0680202 84
networker 0:7f26f0680202 85 ///Starts listening
networker 0:7f26f0680202 86 TCPSocketErr listen();
networker 0:7f26f0680202 87
networker 0:7f26f0680202 88 ///Connects socket to host
networker 0:7f26f0680202 89 TCPSocketErr connect(const Host& host);
networker 0:7f26f0680202 90
networker 0:7f26f0680202 91 ///Accepts connection from client and gets connected socket
networker 0:7f26f0680202 92 TCPSocketErr accept(Host* pClient, TCPSocket** ppNewTcpSocket);
networker 0:7f26f0680202 93
networker 0:7f26f0680202 94 ///Sends data
networker 0:7f26f0680202 95 /*
networker 0:7f26f0680202 96 @return a negative error code or the number of bytes transmitted
networker 0:7f26f0680202 97 */
networker 0:7f26f0680202 98 int /*if < 0 : TCPSocketErr*/ send(const char* buf, int len);
networker 0:7f26f0680202 99
networker 0:7f26f0680202 100 ///Receives data
networker 0:7f26f0680202 101 /*
networker 0:7f26f0680202 102 @return a negative error code or the number of bytes received
networker 0:7f26f0680202 103 */
networker 0:7f26f0680202 104 int /*if < 0 : TCPSocketErr*/ recv(char* buf, int len);
networker 0:7f26f0680202 105
networker 0:7f26f0680202 106 /* TODO NTH : printf / scanf helpers that call send/recv */
networker 0:7f26f0680202 107
networker 0:7f26f0680202 108 ///Closes socket
networker 0:7f26f0680202 109 TCPSocketErr close();
networker 0:7f26f0680202 110
networker 0:7f26f0680202 111 //Callbacks
networker 0:7f26f0680202 112 ///Setups callback
networker 0:7f26f0680202 113 /**
networker 0:7f26f0680202 114 @param pMethod : callback function
networker 0:7f26f0680202 115 */
networker 0:7f26f0680202 116 void setOnEvent( void (*pMethod)(TCPSocketEvent) );
networker 0:7f26f0680202 117
networker 0:7f26f0680202 118 class CDummy;
networker 0:7f26f0680202 119 ///Setups callback
networker 0:7f26f0680202 120 /**
networker 0:7f26f0680202 121 @param pItem : instance of class on which to execute the callback method
networker 0:7f26f0680202 122 @param pMethod : callback method
networker 0:7f26f0680202 123 */
networker 0:7f26f0680202 124 template<class T>
networker 0:7f26f0680202 125 void setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) )
networker 0:7f26f0680202 126 {
networker 0:7f26f0680202 127 m_pCbItem = (CDummy*) pItem;
networker 0:7f26f0680202 128 m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod;
networker 0:7f26f0680202 129 }
networker 0:7f26f0680202 130
networker 0:7f26f0680202 131 ///Disables callback
networker 0:7f26f0680202 132 void resetOnEvent();
networker 0:7f26f0680202 133
networker 0:7f26f0680202 134 protected:
networker 0:7f26f0680202 135 void onNetTcpSocketEvent(NetTcpSocketEvent e);
networker 0:7f26f0680202 136 TCPSocketErr checkInst();
networker 0:7f26f0680202 137
networker 0:7f26f0680202 138 private:
networker 0:7f26f0680202 139 NetTcpSocket* m_pNetTcpSocket;
networker 0:7f26f0680202 140
networker 0:7f26f0680202 141 CDummy* m_pCbItem;
networker 0:7f26f0680202 142 void (CDummy::*m_pCbMeth)(TCPSocketEvent);
networker 0:7f26f0680202 143
networker 0:7f26f0680202 144 void (*m_pCb)(TCPSocketEvent);
networker 0:7f26f0680202 145
networker 0:7f26f0680202 146 };
networker 0:7f26f0680202 147
networker 0:7f26f0680202 148 #endif