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 UDPSocket.cpp Source File

UDPSocket.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 
00030 #include "Socket/UDPSocket.h"
00031 #include "wrapper.h"
00032 #include "proxy_endpoint.h"
00033 
00034 #include <cstring>
00035 
00036 using std::memset;
00037 
00038 UDPSocket::UDPSocket() {
00039 }
00040 
00041 int UDPSocket::init(void) {
00042     return init_socket(SOCK_DGRAM);
00043 }
00044 
00045 // Server initialization
00046 int UDPSocket::bind(int port) {
00047     if (init_socket(SOCK_DGRAM) < 0)
00048         return -1;
00049 
00050     struct sockaddr_in localHost; 
00051 
00052     std::memset(&localHost, 0, sizeof(localHost));
00053     
00054     localHost.sin_family = AF_INET;
00055     localHost.sin_port = short_be(port);
00056     localHost.sin_addr.s_addr = INADDR_ANY;
00057     
00058     if (picotcp_bind(_ep, (struct sockaddr *) &localHost, (socklen_t)sizeof(localHost)) < 0) {
00059         close();
00060         return -1;
00061     }
00062     
00063     return 0;
00064 }
00065 
00066 int UDPSocket::join_multicast_group(EthernetInterface& eth, const char* address) {
00067     
00068     return picotcp_join_multicast(_ep,address,eth.getIPAddress());
00069 }
00070 
00071 int UDPSocket::set_broadcasting(void) {
00072     int option = 1;
00073     return set_option(SOL_SOCKET, SO_BROADCAST, &option, sizeof(option));
00074 }
00075 
00076 // -1 if unsuccessful, else number of bytes written
00077 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) {
00078     if (!_ep)
00079     {
00080         mbed_dbg("Error on socket descriptor \n");
00081         return -1;
00082     }
00083     
00084     return picotcp_sendto(_ep, packet, length, (struct sockaddr *) &remote._remoteHost, sizeof(struct sockaddr_in));
00085 }
00086 
00087 // -1 if unsuccessful, else number of bytes received
00088 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) {
00089     socklen_t remoteHostLen = sizeof(remote._remoteHost);  
00090     int ret;
00091     
00092     if (!_ep)
00093         return -1;
00094     _ep->revents &= ~PICO_SOCK_EV_ERR;
00095     ret = picotcp_recvfrom(_ep, buffer, length, (struct sockaddr*) &remote._remoteHost, &remoteHostLen);
00096     if(ret < length)
00097         _ep->revents &= (~PICO_SOCK_EV_RD);
00098     if(ret) // data was read or error was reported
00099         return ret;
00100         
00101     TimeInterval timeout(!_blocking ? _timeout : osWaitForever);
00102     if (wait_readable(timeout) != 0)
00103         return 0;
00104 
00105 
00106     ret = picotcp_recvfrom(_ep, buffer, length, (struct sockaddr*) &remote._remoteHost, &remoteHostLen);
00107     if(ret < length)
00108         _ep->revents &= (~PICO_SOCK_EV_RD);
00109     
00110     return ret;
00111 }