![](/media/cache/profiles/3f4243a6f681f2920ac3e95a63517274.50x50_q85.jpg)
code that uses Watchdog to reset Mbed every 30seconds. After 30-60mins, the ethernet interface fails to setup() after WatchDog reset.
EthernetNetIf/LPC1768/if/net/netudpsocket.h@0:0ce833f21e63, 2012-06-07 (annotated)
- Committer:
- eqon
- Date:
- Thu Jun 07 05:44:29 2012 +0000
- Revision:
- 0:0ce833f21e63
Who changed what in which revision?
User | Revision | Line number | New 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 | #ifndef NETUDPSOCKET_H |
eqon | 0:0ce833f21e63 | 25 | #define NETUDPSOCKET_H |
eqon | 0:0ce833f21e63 | 26 | |
eqon | 0:0ce833f21e63 | 27 | #include "net.h" |
eqon | 0:0ce833f21e63 | 28 | #include "host.h" |
eqon | 0:0ce833f21e63 | 29 | |
eqon | 0:0ce833f21e63 | 30 | #include <queue> |
eqon | 0:0ce833f21e63 | 31 | using std::queue; |
eqon | 0:0ce833f21e63 | 32 | |
eqon | 0:0ce833f21e63 | 33 | //Implements a Berkeley-like socket if |
eqon | 0:0ce833f21e63 | 34 | //Can be interfaced either to lwip or a Telit module |
eqon | 0:0ce833f21e63 | 35 | |
eqon | 0:0ce833f21e63 | 36 | enum NetUdpSocketErr |
eqon | 0:0ce833f21e63 | 37 | { |
eqon | 0:0ce833f21e63 | 38 | __NETUDPSOCKET_MIN = -0xFFFF, |
eqon | 0:0ce833f21e63 | 39 | NETUDPSOCKET_SETUP, //NetUdpSocket not properly configured |
eqon | 0:0ce833f21e63 | 40 | NETUDPSOCKET_IF, //If has problems |
eqon | 0:0ce833f21e63 | 41 | NETUDPSOCKET_MEM, //Not enough mem |
eqon | 0:0ce833f21e63 | 42 | NETUDPSOCKET_INUSE, //If/Port is in use |
eqon | 0:0ce833f21e63 | 43 | //... |
eqon | 0:0ce833f21e63 | 44 | NETUDPSOCKET_OK = 0 |
eqon | 0:0ce833f21e63 | 45 | }; |
eqon | 0:0ce833f21e63 | 46 | |
eqon | 0:0ce833f21e63 | 47 | enum NetUdpSocketEvent //Only one lonely event here... but who knows, maybe some day there'll be another one! |
eqon | 0:0ce833f21e63 | 48 | { |
eqon | 0:0ce833f21e63 | 49 | NETUDPSOCKET_READABLE, //Data in buf |
eqon | 0:0ce833f21e63 | 50 | }; |
eqon | 0:0ce833f21e63 | 51 | |
eqon | 0:0ce833f21e63 | 52 | |
eqon | 0:0ce833f21e63 | 53 | class NetUdpSocket |
eqon | 0:0ce833f21e63 | 54 | { |
eqon | 0:0ce833f21e63 | 55 | public: |
eqon | 0:0ce833f21e63 | 56 | NetUdpSocket(); |
eqon | 0:0ce833f21e63 | 57 | virtual ~NetUdpSocket(); //close() |
eqon | 0:0ce833f21e63 | 58 | |
eqon | 0:0ce833f21e63 | 59 | virtual NetUdpSocketErr bind(const Host& me) = 0; |
eqon | 0:0ce833f21e63 | 60 | |
eqon | 0:0ce833f21e63 | 61 | virtual int /*if < 0 : NetUdpSocketErr*/ sendto(const char* buf, int len, Host* pHost) = 0; |
eqon | 0:0ce833f21e63 | 62 | virtual int /*if < 0 : NetUdpSocketErr*/ recvfrom(char* buf, int len, Host* pHost) = 0; |
eqon | 0:0ce833f21e63 | 63 | |
eqon | 0:0ce833f21e63 | 64 | /* TODO NTH : printf / scanf helpers that call send/recv */ |
eqon | 0:0ce833f21e63 | 65 | |
eqon | 0:0ce833f21e63 | 66 | virtual NetUdpSocketErr close() = 0; |
eqon | 0:0ce833f21e63 | 67 | |
eqon | 0:0ce833f21e63 | 68 | virtual NetUdpSocketErr poll() = 0; |
eqon | 0:0ce833f21e63 | 69 | |
eqon | 0:0ce833f21e63 | 70 | class CDummy; |
eqon | 0:0ce833f21e63 | 71 | //Callbacks |
eqon | 0:0ce833f21e63 | 72 | template<class T> |
eqon | 0:0ce833f21e63 | 73 | //Linker bug : Must be defined here :( |
eqon | 0:0ce833f21e63 | 74 | void setOnEvent( T* pItem, void (T::*pMethod)(NetUdpSocketEvent) ) |
eqon | 0:0ce833f21e63 | 75 | { |
eqon | 0:0ce833f21e63 | 76 | m_pCbItem = (CDummy*) pItem; |
eqon | 0:0ce833f21e63 | 77 | m_pCbMeth = (void (CDummy::*)(NetUdpSocketEvent)) pMethod; |
eqon | 0:0ce833f21e63 | 78 | } |
eqon | 0:0ce833f21e63 | 79 | |
eqon | 0:0ce833f21e63 | 80 | void resetOnEvent(); //Disable callback |
eqon | 0:0ce833f21e63 | 81 | |
eqon | 0:0ce833f21e63 | 82 | protected: |
eqon | 0:0ce833f21e63 | 83 | void queueEvent(NetUdpSocketEvent e); |
eqon | 0:0ce833f21e63 | 84 | void discardEvents(); |
eqon | 0:0ce833f21e63 | 85 | void flushEvents(); //to be called during polling |
eqon | 0:0ce833f21e63 | 86 | |
eqon | 0:0ce833f21e63 | 87 | Host m_host; |
eqon | 0:0ce833f21e63 | 88 | Host m_client; |
eqon | 0:0ce833f21e63 | 89 | |
eqon | 0:0ce833f21e63 | 90 | friend class Net; |
eqon | 0:0ce833f21e63 | 91 | int m_refs; |
eqon | 0:0ce833f21e63 | 92 | |
eqon | 0:0ce833f21e63 | 93 | bool m_closed; |
eqon | 0:0ce833f21e63 | 94 | bool m_removed; |
eqon | 0:0ce833f21e63 | 95 | |
eqon | 0:0ce833f21e63 | 96 | private: |
eqon | 0:0ce833f21e63 | 97 | //We do not want to execute user code in interrupt routines, so we queue events until the server is polled |
eqon | 0:0ce833f21e63 | 98 | //If we port this to a multithreaded OS, we could avoid this (however some functions here are not thread-safe, so beware ;) ) |
eqon | 0:0ce833f21e63 | 99 | void onEvent(NetUdpSocketEvent e); //To be called on poll |
eqon | 0:0ce833f21e63 | 100 | CDummy* m_pCbItem; |
eqon | 0:0ce833f21e63 | 101 | void (CDummy::*m_pCbMeth)(NetUdpSocketEvent); |
eqon | 0:0ce833f21e63 | 102 | queue<NetUdpSocketEvent> m_events; |
eqon | 0:0ce833f21e63 | 103 | |
eqon | 0:0ce833f21e63 | 104 | }; |
eqon | 0:0ce833f21e63 | 105 | |
eqon | 0:0ce833f21e63 | 106 | #endif |