Control a robot over the internet using UDP and a Ethernet interface.
Dependencies: EthernetInterface Motor TextLCD mbed-rtos mbed Socket lwip-eth lwip-sys lwip
EthernetInterface/Socket/UDPSocket.cpp@0:1496281373a5, 2013-10-17 (annotated)
- Committer:
- apatel336
- Date:
- Thu Oct 17 13:26:38 2013 +0000
- Revision:
- 0:1496281373a5
Initial Release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
apatel336 | 0:1496281373a5 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
apatel336 | 0:1496281373a5 | 2 | * |
apatel336 | 0:1496281373a5 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
apatel336 | 0:1496281373a5 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
apatel336 | 0:1496281373a5 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
apatel336 | 0:1496281373a5 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
apatel336 | 0:1496281373a5 | 7 | * furnished to do so, subject to the following conditions: |
apatel336 | 0:1496281373a5 | 8 | * |
apatel336 | 0:1496281373a5 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
apatel336 | 0:1496281373a5 | 10 | * substantial portions of the Software. |
apatel336 | 0:1496281373a5 | 11 | * |
apatel336 | 0:1496281373a5 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
apatel336 | 0:1496281373a5 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
apatel336 | 0:1496281373a5 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
apatel336 | 0:1496281373a5 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
apatel336 | 0:1496281373a5 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
apatel336 | 0:1496281373a5 | 17 | */ |
apatel336 | 0:1496281373a5 | 18 | |
apatel336 | 0:1496281373a5 | 19 | #include "Socket/UDPSocket.h" |
apatel336 | 0:1496281373a5 | 20 | |
apatel336 | 0:1496281373a5 | 21 | #include <cstring> |
apatel336 | 0:1496281373a5 | 22 | |
apatel336 | 0:1496281373a5 | 23 | using std::memset; |
apatel336 | 0:1496281373a5 | 24 | |
apatel336 | 0:1496281373a5 | 25 | UDPSocket::UDPSocket() { |
apatel336 | 0:1496281373a5 | 26 | } |
apatel336 | 0:1496281373a5 | 27 | |
apatel336 | 0:1496281373a5 | 28 | int UDPSocket::init(void) { |
apatel336 | 0:1496281373a5 | 29 | return init_socket(SOCK_DGRAM); |
apatel336 | 0:1496281373a5 | 30 | } |
apatel336 | 0:1496281373a5 | 31 | |
apatel336 | 0:1496281373a5 | 32 | // Server initialization |
apatel336 | 0:1496281373a5 | 33 | int UDPSocket::bind(int port) { |
apatel336 | 0:1496281373a5 | 34 | if (init_socket(SOCK_DGRAM) < 0) |
apatel336 | 0:1496281373a5 | 35 | return -1; |
apatel336 | 0:1496281373a5 | 36 | |
apatel336 | 0:1496281373a5 | 37 | struct sockaddr_in localHost; |
apatel336 | 0:1496281373a5 | 38 | std::memset(&localHost, 0, sizeof(localHost)); |
apatel336 | 0:1496281373a5 | 39 | |
apatel336 | 0:1496281373a5 | 40 | localHost.sin_family = AF_INET; |
apatel336 | 0:1496281373a5 | 41 | localHost.sin_port = htons(port); |
apatel336 | 0:1496281373a5 | 42 | localHost.sin_addr.s_addr = INADDR_ANY; |
apatel336 | 0:1496281373a5 | 43 | |
apatel336 | 0:1496281373a5 | 44 | if (lwip_bind(_sock_fd, (const struct sockaddr *) &localHost, sizeof(localHost)) < 0) { |
apatel336 | 0:1496281373a5 | 45 | close(); |
apatel336 | 0:1496281373a5 | 46 | return -1; |
apatel336 | 0:1496281373a5 | 47 | } |
apatel336 | 0:1496281373a5 | 48 | |
apatel336 | 0:1496281373a5 | 49 | return 0; |
apatel336 | 0:1496281373a5 | 50 | } |
apatel336 | 0:1496281373a5 | 51 | |
apatel336 | 0:1496281373a5 | 52 | int UDPSocket::join_multicast_group(const char* address) { |
apatel336 | 0:1496281373a5 | 53 | struct ip_mreq mreq; |
apatel336 | 0:1496281373a5 | 54 | |
apatel336 | 0:1496281373a5 | 55 | // Set up group address |
apatel336 | 0:1496281373a5 | 56 | mreq.imr_multiaddr.s_addr = inet_addr(address); |
apatel336 | 0:1496281373a5 | 57 | mreq.imr_interface.s_addr = htonl(INADDR_ANY); |
apatel336 | 0:1496281373a5 | 58 | |
apatel336 | 0:1496281373a5 | 59 | return set_option(IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); |
apatel336 | 0:1496281373a5 | 60 | } |
apatel336 | 0:1496281373a5 | 61 | |
apatel336 | 0:1496281373a5 | 62 | int UDPSocket::set_broadcasting(bool broadcast) { |
apatel336 | 0:1496281373a5 | 63 | int option = (broadcast) ? (1) : (0); |
apatel336 | 0:1496281373a5 | 64 | return set_option(SOL_SOCKET, SO_BROADCAST, &option, sizeof(option)); |
apatel336 | 0:1496281373a5 | 65 | } |
apatel336 | 0:1496281373a5 | 66 | |
apatel336 | 0:1496281373a5 | 67 | // -1 if unsuccessful, else number of bytes written |
apatel336 | 0:1496281373a5 | 68 | int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) { |
apatel336 | 0:1496281373a5 | 69 | if (_sock_fd < 0) |
apatel336 | 0:1496281373a5 | 70 | return -1; |
apatel336 | 0:1496281373a5 | 71 | |
apatel336 | 0:1496281373a5 | 72 | if (!_blocking) { |
apatel336 | 0:1496281373a5 | 73 | TimeInterval timeout(_timeout); |
apatel336 | 0:1496281373a5 | 74 | if (wait_writable(timeout) != 0) |
apatel336 | 0:1496281373a5 | 75 | return 0; |
apatel336 | 0:1496281373a5 | 76 | } |
apatel336 | 0:1496281373a5 | 77 | |
apatel336 | 0:1496281373a5 | 78 | return lwip_sendto(_sock_fd, packet, length, 0, (const struct sockaddr *) &remote._remoteHost, sizeof(remote._remoteHost)); |
apatel336 | 0:1496281373a5 | 79 | } |
apatel336 | 0:1496281373a5 | 80 | |
apatel336 | 0:1496281373a5 | 81 | // -1 if unsuccessful, else number of bytes received |
apatel336 | 0:1496281373a5 | 82 | int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) { |
apatel336 | 0:1496281373a5 | 83 | if (_sock_fd < 0) |
apatel336 | 0:1496281373a5 | 84 | return -1; |
apatel336 | 0:1496281373a5 | 85 | |
apatel336 | 0:1496281373a5 | 86 | if (!_blocking) { |
apatel336 | 0:1496281373a5 | 87 | TimeInterval timeout(_timeout); |
apatel336 | 0:1496281373a5 | 88 | if (wait_readable(timeout) != 0) |
apatel336 | 0:1496281373a5 | 89 | return 0; |
apatel336 | 0:1496281373a5 | 90 | } |
apatel336 | 0:1496281373a5 | 91 | remote.reset_address(); |
apatel336 | 0:1496281373a5 | 92 | socklen_t remoteHostLen = sizeof(remote._remoteHost); |
apatel336 | 0:1496281373a5 | 93 | return lwip_recvfrom(_sock_fd, buffer, length, 0, (struct sockaddr*) &remote._remoteHost, &remoteHostLen); |
apatel336 | 0:1496281373a5 | 94 | } |