HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mr_q 0:d8f2f7d5f31b 1
mr_q 0:d8f2f7d5f31b 2 /*
mr_q 0:d8f2f7d5f31b 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
mr_q 0:d8f2f7d5f31b 4
mr_q 0:d8f2f7d5f31b 5 Permission is hereby granted, free of charge, to any person obtaining a copy
mr_q 0:d8f2f7d5f31b 6 of this software and associated documentation files (the "Software"), to deal
mr_q 0:d8f2f7d5f31b 7 in the Software without restriction, including without limitation the rights
mr_q 0:d8f2f7d5f31b 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
mr_q 0:d8f2f7d5f31b 9 copies of the Software, and to permit persons to whom the Software is
mr_q 0:d8f2f7d5f31b 10 furnished to do so, subject to the following conditions:
mr_q 0:d8f2f7d5f31b 11
mr_q 0:d8f2f7d5f31b 12 The above copyright notice and this permission notice shall be included in
mr_q 0:d8f2f7d5f31b 13 all copies or substantial portions of the Software.
mr_q 0:d8f2f7d5f31b 14
mr_q 0:d8f2f7d5f31b 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
mr_q 0:d8f2f7d5f31b 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
mr_q 0:d8f2f7d5f31b 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
mr_q 0:d8f2f7d5f31b 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mr_q 0:d8f2f7d5f31b 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mr_q 0:d8f2f7d5f31b 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
mr_q 0:d8f2f7d5f31b 21 THE SOFTWARE.
mr_q 0:d8f2f7d5f31b 22 */
mr_q 0:d8f2f7d5f31b 23 #include "mbed.h"
mr_q 0:d8f2f7d5f31b 24 #include "TCPSocket.h"
mr_q 0:d8f2f7d5f31b 25 #include "if/net/nettcpsocket.h"
mr_q 0:d8f2f7d5f31b 26
mr_q 0:d8f2f7d5f31b 27 TCPSocket::TCPSocket() : m_pNetTcpSocket(NULL), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL)
mr_q 0:d8f2f7d5f31b 28 {
mr_q 0:d8f2f7d5f31b 29
mr_q 0:d8f2f7d5f31b 30 }
mr_q 0:d8f2f7d5f31b 31
mr_q 0:d8f2f7d5f31b 32 TCPSocket::TCPSocket(NetTcpSocket* pNetTcpSocket) : m_pNetTcpSocket(pNetTcpSocket), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL)
mr_q 0:d8f2f7d5f31b 33 {
mr_q 0:d8f2f7d5f31b 34 m_pNetTcpSocket->setOnEvent(this, &TCPSocket::onNetTcpSocketEvent);
mr_q 0:d8f2f7d5f31b 35 }
mr_q 0:d8f2f7d5f31b 36
mr_q 0:d8f2f7d5f31b 37 TCPSocket::~TCPSocket() //close()
mr_q 0:d8f2f7d5f31b 38 {
mr_q 0:d8f2f7d5f31b 39 close();
mr_q 0:d8f2f7d5f31b 40 }
mr_q 0:d8f2f7d5f31b 41
mr_q 0:d8f2f7d5f31b 42 TCPSocketErr TCPSocket::bind(const Host& me)
mr_q 0:d8f2f7d5f31b 43 {
mr_q 0:d8f2f7d5f31b 44 printf("Enter TCPSocket::bind()\r\n");
mr_q 0:d8f2f7d5f31b 45 TCPSocketErr tcpSocketErr = checkInst();
mr_q 0:d8f2f7d5f31b 46 if(tcpSocketErr)
mr_q 0:d8f2f7d5f31b 47 return tcpSocketErr;
mr_q 0:d8f2f7d5f31b 48 return (TCPSocketErr) m_pNetTcpSocket->bind(me);
mr_q 0:d8f2f7d5f31b 49 }
mr_q 0:d8f2f7d5f31b 50
mr_q 0:d8f2f7d5f31b 51 TCPSocketErr TCPSocket::listen()
mr_q 0:d8f2f7d5f31b 52 {
mr_q 0:d8f2f7d5f31b 53 printf("Enter TCPSocket::listen()\r\n");
mr_q 0:d8f2f7d5f31b 54 TCPSocketErr tcpSocketErr = checkInst();
mr_q 0:d8f2f7d5f31b 55 if(tcpSocketErr)
mr_q 0:d8f2f7d5f31b 56 return tcpSocketErr;
mr_q 0:d8f2f7d5f31b 57 return (TCPSocketErr) m_pNetTcpSocket->listen();
mr_q 0:d8f2f7d5f31b 58 }
mr_q 0:d8f2f7d5f31b 59
mr_q 0:d8f2f7d5f31b 60 TCPSocketErr TCPSocket::connect(const Host& host)
mr_q 0:d8f2f7d5f31b 61 {
mr_q 0:d8f2f7d5f31b 62 printf("Enter TCPSocket::connect()\r\n");
mr_q 0:d8f2f7d5f31b 63 TCPSocketErr tcpSocketErr = checkInst();
mr_q 0:d8f2f7d5f31b 64 if(tcpSocketErr)
mr_q 0:d8f2f7d5f31b 65 return tcpSocketErr;
mr_q 0:d8f2f7d5f31b 66 return (TCPSocketErr) m_pNetTcpSocket->connect(host);
mr_q 0:d8f2f7d5f31b 67 }
mr_q 0:d8f2f7d5f31b 68
mr_q 0:d8f2f7d5f31b 69 TCPSocketErr TCPSocket::accept(Host* pClient, TCPSocket** ppNewTCPSocket)
mr_q 0:d8f2f7d5f31b 70 {
mr_q 0:d8f2f7d5f31b 71 printf("Enter TCPSocket::accept()\r\n");
mr_q 0:d8f2f7d5f31b 72 TCPSocketErr tcpSocketErr = checkInst();
mr_q 0:d8f2f7d5f31b 73 if(tcpSocketErr)
mr_q 0:d8f2f7d5f31b 74 return tcpSocketErr;
mr_q 0:d8f2f7d5f31b 75 NetTcpSocket* pNewNetTcpSocket;
mr_q 0:d8f2f7d5f31b 76 tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->accept(pClient, &pNewNetTcpSocket);
mr_q 0:d8f2f7d5f31b 77 if(pNewNetTcpSocket)
mr_q 0:d8f2f7d5f31b 78 *ppNewTCPSocket = new TCPSocket(pNewNetTcpSocket);
mr_q 0:d8f2f7d5f31b 79 return tcpSocketErr;
mr_q 0:d8f2f7d5f31b 80 }
mr_q 0:d8f2f7d5f31b 81
mr_q 0:d8f2f7d5f31b 82 int /*if < 0 : TCPSocketErr*/ TCPSocket::send(const char* buf, int len)
mr_q 0:d8f2f7d5f31b 83 {
mr_q 0:d8f2f7d5f31b 84 printf("Enter TCPSocket::send()\r\n");
mr_q 0:d8f2f7d5f31b 85 TCPSocketErr tcpSocketErr = checkInst();
mr_q 0:d8f2f7d5f31b 86 if(tcpSocketErr)
mr_q 0:d8f2f7d5f31b 87 return tcpSocketErr;
mr_q 0:d8f2f7d5f31b 88 return m_pNetTcpSocket->send(buf, len);
mr_q 0:d8f2f7d5f31b 89 }
mr_q 0:d8f2f7d5f31b 90
mr_q 0:d8f2f7d5f31b 91 int /*if < 0 : TCPSocketErr*/ TCPSocket::recv(char* buf, int len)
mr_q 0:d8f2f7d5f31b 92 {
mr_q 0:d8f2f7d5f31b 93 printf("Enter TCPSocket::receive()\r\n");
mr_q 0:d8f2f7d5f31b 94 TCPSocketErr tcpSocketErr = checkInst();
mr_q 0:d8f2f7d5f31b 95 if(tcpSocketErr)
mr_q 0:d8f2f7d5f31b 96 return tcpSocketErr;
mr_q 0:d8f2f7d5f31b 97 return m_pNetTcpSocket->recv(buf, len);
mr_q 0:d8f2f7d5f31b 98 }
mr_q 0:d8f2f7d5f31b 99
mr_q 0:d8f2f7d5f31b 100 TCPSocketErr TCPSocket::close()
mr_q 0:d8f2f7d5f31b 101 {
mr_q 0:d8f2f7d5f31b 102 printf("Enter TCPSocket::close()\r\n");
mr_q 0:d8f2f7d5f31b 103 if(!m_pNetTcpSocket)
mr_q 0:d8f2f7d5f31b 104 return TCPSOCKET_SETUP;
mr_q 0:d8f2f7d5f31b 105 m_pNetTcpSocket->resetOnEvent();
mr_q 0:d8f2f7d5f31b 106 TCPSocketErr tcpSocketErr = (TCPSocketErr) m_pNetTcpSocket->close(); //Close (can already be closed)
mr_q 0:d8f2f7d5f31b 107 Net::releaseTcpSocket(m_pNetTcpSocket); //And release it so it can be freed when properly removed
mr_q 0:d8f2f7d5f31b 108 m_pNetTcpSocket = NULL;
mr_q 0:d8f2f7d5f31b 109 return tcpSocketErr;
mr_q 0:d8f2f7d5f31b 110 }
mr_q 0:d8f2f7d5f31b 111
mr_q 0:d8f2f7d5f31b 112 //Callbacks
mr_q 0:d8f2f7d5f31b 113 void TCPSocket::setOnEvent( void (*pMethod)(TCPSocketEvent) )
mr_q 0:d8f2f7d5f31b 114 {
mr_q 0:d8f2f7d5f31b 115 printf("Enter TCPSocket::setOnEvent()\r\n");
mr_q 0:d8f2f7d5f31b 116 m_pCb = pMethod;
mr_q 0:d8f2f7d5f31b 117 }
mr_q 0:d8f2f7d5f31b 118
mr_q 0:d8f2f7d5f31b 119 #if 0 //For info only
mr_q 0:d8f2f7d5f31b 120 template<class T>
mr_q 0:d8f2f7d5f31b 121 void TCPSocket::setOnEvent( T* pItem, void (T::*pMethod)(TCPSocketEvent) )
mr_q 0:d8f2f7d5f31b 122 {
mr_q 0:d8f2f7d5f31b 123 printf("Enter TCPSocket::setOnEvent()\r\n");
mr_q 0:d8f2f7d5f31b 124 m_pCbItem = (CDummy*) pItem;
mr_q 0:d8f2f7d5f31b 125 m_pCbMeth = (void (CDummy::*)(TCPSocketEvent)) pMethod;
mr_q 0:d8f2f7d5f31b 126 }
mr_q 0:d8f2f7d5f31b 127 #endif
mr_q 0:d8f2f7d5f31b 128
mr_q 0:d8f2f7d5f31b 129 void TCPSocket::resetOnEvent() //Disable callback
mr_q 0:d8f2f7d5f31b 130 {
mr_q 0:d8f2f7d5f31b 131 printf("Enter TCPSocket::resetOnEvent()\r\n");
mr_q 0:d8f2f7d5f31b 132 m_pCb = NULL;
mr_q 0:d8f2f7d5f31b 133 m_pCbItem = NULL;
mr_q 0:d8f2f7d5f31b 134 m_pCbMeth = NULL;
mr_q 0:d8f2f7d5f31b 135 }
mr_q 0:d8f2f7d5f31b 136
mr_q 0:d8f2f7d5f31b 137 void TCPSocket::onNetTcpSocketEvent(NetTcpSocketEvent e)
mr_q 0:d8f2f7d5f31b 138 {
mr_q 0:d8f2f7d5f31b 139 printf("Enter TCPSocket::onNetTcpSpcketEvent()\r\n");
mr_q 0:d8f2f7d5f31b 140 if(m_pCbItem && m_pCbMeth)
mr_q 0:d8f2f7d5f31b 141 (m_pCbItem->*m_pCbMeth)((TCPSocketEvent) e);
mr_q 0:d8f2f7d5f31b 142 else if(m_pCb)
mr_q 0:d8f2f7d5f31b 143 m_pCb((TCPSocketEvent) e);
mr_q 0:d8f2f7d5f31b 144 }
mr_q 0:d8f2f7d5f31b 145
mr_q 0:d8f2f7d5f31b 146 TCPSocketErr TCPSocket::checkInst()
mr_q 0:d8f2f7d5f31b 147 {
mr_q 0:d8f2f7d5f31b 148 printf("Enter TCPSocket::checkInst()\r\n");
mr_q 0:d8f2f7d5f31b 149 if(!m_pNetTcpSocket)
mr_q 0:d8f2f7d5f31b 150 {
mr_q 0:d8f2f7d5f31b 151 m_pNetTcpSocket = Net::tcpSocket();
mr_q 0:d8f2f7d5f31b 152 if(!m_pNetTcpSocket)
mr_q 0:d8f2f7d5f31b 153 {
mr_q 0:d8f2f7d5f31b 154 return TCPSOCKET_IF; //Interface did not return a socket (usually because a default interface does not exist)
mr_q 0:d8f2f7d5f31b 155 }
mr_q 0:d8f2f7d5f31b 156 printf("!m_pNetTcpSocket\r\n");
mr_q 0:d8f2f7d5f31b 157 m_pNetTcpSocket->setOnEvent(this, &TCPSocket::onNetTcpSocketEvent);
mr_q 0:d8f2f7d5f31b 158 }
mr_q 0:d8f2f7d5f31b 159 return TCPSOCKET_OK;
mr_q 0:d8f2f7d5f31b 160 }