Free (GPLv2) TCP/IP stack developed by TASS Belgium

Dependents:   lpc1768-picotcp-demo ZeroMQ_PicoTCP_Publisher_demo TCPSocket_HelloWorld_PicoTCP Pico_TCP_UDP_Test ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Socket.cpp Source File

Socket.cpp

00001 /* 
00002  *
00003  * PicoTCP Socket interface for mbed.
00004  * Copyright (C) 2013 TASS Belgium NV
00005  * 
00006  * Released under GPL v2
00007  *
00008  * Other licensing models might apply at the sole discretion of the copyright holders.
00009  *
00010  *
00011  * This software is based on the mbed.org EthernetInterface implementation:
00012  * Copyright (C) 2012 mbed.org, MIT License
00013  *
00014  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00015  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00016  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00017  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00018  * furnished to do so, subject to the following conditions:
00019  *
00020  * The above copyright notice and this permission notice shall be included in all copies or
00021  * substantial portions of the Software.
00022  *
00023  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00024  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00025  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00026  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00027  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00028  */
00029 #include "Socket/Socket.h"
00030 #include "wrapper.h"
00031 #include "proxy_endpoint.h"
00032 #include <cstring>
00033 
00034 using std::memset;
00035 
00036 Socket::Socket() : _ep(NULL), _blocking(true),_timeout(1500)
00037 {
00038     //set_blocking(true,1500); // ?
00039 }
00040 
00041 void Socket::set_blocking(bool blocking, unsigned int timeout) {
00042     _blocking = blocking;
00043     _timeout = timeout;
00044 }
00045 
00046 int Socket::init_socket(int type) {
00047     if (_ep != NULL)
00048     {
00049         mbed_dbg("Sock open already...\n");
00050         return -1;
00051     }
00052     struct stack_endpoint *ep  = picotcp_socket(AF_INET, type, 0);
00053     if (!ep)
00054     {
00055         mbed_dbg("Error opening socket...\n");
00056         return -1;
00057     }
00058     _ep = ep;
00059     return 0;
00060 }
00061 
00062 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
00063     (void)level;
00064     return picotcp_setsockopt(_ep, optname, (void *)optval);
00065 }
00066 
00067 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
00068     (void)level;
00069     return picotcp_getsockopt(_ep, optname, optval);
00070 }
00071 
00072 int Socket::select(struct timeval *timeout, bool read, bool write) {
00073     return picotcp_select(_ep, timeout, read, write);
00074 }
00075 
00076 int Socket::wait_readable(TimeInterval& timeout) {
00077     return (select(&timeout._time, true, false) == 0 ? -1 : 0);
00078 }
00079 
00080 int Socket::wait_writable(TimeInterval& timeout) {
00081     return (select(&timeout._time, false, true) == 0 ? -1 : 0);
00082 }
00083 
00084 int Socket::is_readable(void) {
00085     return (this->_ep->revents & PICO_SOCK_EV_RD);
00086 }
00087 
00088 int Socket::is_writable(void) {
00089     return (this->_ep->revents & PICO_SOCK_EV_WR);
00090 }
00091 
00092 int Socket::close() {
00093     if (_ep == NULL)
00094         return -1;
00095     picotcp_close(_ep);
00096     _ep = NULL;
00097     return 0;
00098 }
00099 
00100 Socket::~Socket() {
00101     if(_ep)
00102     {
00103         picotcp_close(_ep);
00104         _ep = NULL;
00105     }
00106 }
00107 
00108 TimeInterval::TimeInterval(unsigned int ms) {
00109     _time.tv_sec = ms / 1000;
00110     _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
00111 }