code that uses Watchdog to reset Mbed every 30seconds. After 30-60mins, the ethernet interface fails to setup() after WatchDog reset.

Dependencies:   mbed

Committer:
eqon
Date:
Thu Jun 07 05:44:29 2012 +0000
Revision:
0:0ce833f21e63

        

Who changed what in which revision?

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