Ethernet test for ECE 4180 and others to find your IP address and do a simple HTTP GET request over port 80.

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Committer:
mkersh3
Date:
Thu Apr 04 05:26:09 2013 +0000
Revision:
0:e7ca326e76ee
Ethernet Test for ECE4180 and others to find their IP Address and do a simple HTTP GET request over port 80.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mkersh3 0:e7ca326e76ee 1 /* Copyright (C) 2012 mbed.org, MIT License
mkersh3 0:e7ca326e76ee 2 *
mkersh3 0:e7ca326e76ee 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mkersh3 0:e7ca326e76ee 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
mkersh3 0:e7ca326e76ee 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
mkersh3 0:e7ca326e76ee 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
mkersh3 0:e7ca326e76ee 7 * furnished to do so, subject to the following conditions:
mkersh3 0:e7ca326e76ee 8 *
mkersh3 0:e7ca326e76ee 9 * The above copyright notice and this permission notice shall be included in all copies or
mkersh3 0:e7ca326e76ee 10 * substantial portions of the Software.
mkersh3 0:e7ca326e76ee 11 *
mkersh3 0:e7ca326e76ee 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mkersh3 0:e7ca326e76ee 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mkersh3 0:e7ca326e76ee 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mkersh3 0:e7ca326e76ee 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mkersh3 0:e7ca326e76ee 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mkersh3 0:e7ca326e76ee 17 */
mkersh3 0:e7ca326e76ee 18 #include "Socket/Socket.h"
mkersh3 0:e7ca326e76ee 19 #include <cstring>
mkersh3 0:e7ca326e76ee 20
mkersh3 0:e7ca326e76ee 21 using std::memset;
mkersh3 0:e7ca326e76ee 22
mkersh3 0:e7ca326e76ee 23 Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) {
mkersh3 0:e7ca326e76ee 24
mkersh3 0:e7ca326e76ee 25 }
mkersh3 0:e7ca326e76ee 26
mkersh3 0:e7ca326e76ee 27 void Socket::set_blocking(bool blocking, unsigned int timeout) {
mkersh3 0:e7ca326e76ee 28 _blocking = blocking;
mkersh3 0:e7ca326e76ee 29 _timeout = timeout;
mkersh3 0:e7ca326e76ee 30 }
mkersh3 0:e7ca326e76ee 31
mkersh3 0:e7ca326e76ee 32 int Socket::init_socket(int type) {
mkersh3 0:e7ca326e76ee 33 if (_sock_fd != -1)
mkersh3 0:e7ca326e76ee 34 return -1;
mkersh3 0:e7ca326e76ee 35
mkersh3 0:e7ca326e76ee 36 int fd = lwip_socket(AF_INET, type, 0);
mkersh3 0:e7ca326e76ee 37 if (fd < 0)
mkersh3 0:e7ca326e76ee 38 return -1;
mkersh3 0:e7ca326e76ee 39
mkersh3 0:e7ca326e76ee 40 _sock_fd = fd;
mkersh3 0:e7ca326e76ee 41 return 0;
mkersh3 0:e7ca326e76ee 42 }
mkersh3 0:e7ca326e76ee 43
mkersh3 0:e7ca326e76ee 44 int Socket::select(struct timeval *timeout, bool read, bool write) {
mkersh3 0:e7ca326e76ee 45 fd_set fdSet;
mkersh3 0:e7ca326e76ee 46 FD_ZERO(&fdSet);
mkersh3 0:e7ca326e76ee 47 FD_SET(_sock_fd, &fdSet);
mkersh3 0:e7ca326e76ee 48
mkersh3 0:e7ca326e76ee 49 fd_set* readset = (read ) ? (&fdSet) : (NULL);
mkersh3 0:e7ca326e76ee 50 fd_set* writeset = (write) ? (&fdSet) : (NULL);
mkersh3 0:e7ca326e76ee 51
mkersh3 0:e7ca326e76ee 52 int ret = lwip_select(FD_SETSIZE, readset, writeset, NULL, timeout);
mkersh3 0:e7ca326e76ee 53 return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
mkersh3 0:e7ca326e76ee 54 }
mkersh3 0:e7ca326e76ee 55
mkersh3 0:e7ca326e76ee 56 int Socket::wait_readable(TimeInterval& timeout) {
mkersh3 0:e7ca326e76ee 57 return select(&timeout._time, true, false);
mkersh3 0:e7ca326e76ee 58 }
mkersh3 0:e7ca326e76ee 59
mkersh3 0:e7ca326e76ee 60 int Socket::wait_writable(TimeInterval& timeout) {
mkersh3 0:e7ca326e76ee 61 return select(&timeout._time, false, true);
mkersh3 0:e7ca326e76ee 62 }
mkersh3 0:e7ca326e76ee 63
mkersh3 0:e7ca326e76ee 64 int Socket::close() {
mkersh3 0:e7ca326e76ee 65 if (_sock_fd < 0)
mkersh3 0:e7ca326e76ee 66 return -1;
mkersh3 0:e7ca326e76ee 67
mkersh3 0:e7ca326e76ee 68 lwip_close(_sock_fd);
mkersh3 0:e7ca326e76ee 69 _sock_fd = -1;
mkersh3 0:e7ca326e76ee 70
mkersh3 0:e7ca326e76ee 71 return 0;
mkersh3 0:e7ca326e76ee 72 }
mkersh3 0:e7ca326e76ee 73
mkersh3 0:e7ca326e76ee 74 Socket::~Socket() {
mkersh3 0:e7ca326e76ee 75 close(); //Don't want to leak
mkersh3 0:e7ca326e76ee 76 }
mkersh3 0:e7ca326e76ee 77
mkersh3 0:e7ca326e76ee 78 TimeInterval::TimeInterval(unsigned int ms) {
mkersh3 0:e7ca326e76ee 79 _time.tv_sec = ms / 1000;
mkersh3 0:e7ca326e76ee 80 _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
mkersh3 0:e7ca326e76ee 81 }