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/Socket.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 | #include "Socket/Socket.h" |
apatel336 | 0:1496281373a5 | 19 | #include <cstring> |
apatel336 | 0:1496281373a5 | 20 | |
apatel336 | 0:1496281373a5 | 21 | using std::memset; |
apatel336 | 0:1496281373a5 | 22 | |
apatel336 | 0:1496281373a5 | 23 | Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) { |
apatel336 | 0:1496281373a5 | 24 | |
apatel336 | 0:1496281373a5 | 25 | } |
apatel336 | 0:1496281373a5 | 26 | |
apatel336 | 0:1496281373a5 | 27 | void Socket::set_blocking(bool blocking, unsigned int timeout) { |
apatel336 | 0:1496281373a5 | 28 | _blocking = blocking; |
apatel336 | 0:1496281373a5 | 29 | _timeout = timeout; |
apatel336 | 0:1496281373a5 | 30 | } |
apatel336 | 0:1496281373a5 | 31 | |
apatel336 | 0:1496281373a5 | 32 | int Socket::init_socket(int type) { |
apatel336 | 0:1496281373a5 | 33 | if (_sock_fd != -1) |
apatel336 | 0:1496281373a5 | 34 | return -1; |
apatel336 | 0:1496281373a5 | 35 | |
apatel336 | 0:1496281373a5 | 36 | int fd = lwip_socket(AF_INET, type, 0); |
apatel336 | 0:1496281373a5 | 37 | if (fd < 0) |
apatel336 | 0:1496281373a5 | 38 | return -1; |
apatel336 | 0:1496281373a5 | 39 | |
apatel336 | 0:1496281373a5 | 40 | _sock_fd = fd; |
apatel336 | 0:1496281373a5 | 41 | return 0; |
apatel336 | 0:1496281373a5 | 42 | } |
apatel336 | 0:1496281373a5 | 43 | |
apatel336 | 0:1496281373a5 | 44 | int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) { |
apatel336 | 0:1496281373a5 | 45 | return lwip_setsockopt(_sock_fd, level, optname, optval, optlen); |
apatel336 | 0:1496281373a5 | 46 | } |
apatel336 | 0:1496281373a5 | 47 | |
apatel336 | 0:1496281373a5 | 48 | int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) { |
apatel336 | 0:1496281373a5 | 49 | return lwip_getsockopt(_sock_fd, level, optname, optval, optlen); |
apatel336 | 0:1496281373a5 | 50 | } |
apatel336 | 0:1496281373a5 | 51 | |
apatel336 | 0:1496281373a5 | 52 | int Socket::select(struct timeval *timeout, bool read, bool write) { |
apatel336 | 0:1496281373a5 | 53 | fd_set fdSet; |
apatel336 | 0:1496281373a5 | 54 | FD_ZERO(&fdSet); |
apatel336 | 0:1496281373a5 | 55 | FD_SET(_sock_fd, &fdSet); |
apatel336 | 0:1496281373a5 | 56 | |
apatel336 | 0:1496281373a5 | 57 | fd_set* readset = (read ) ? (&fdSet) : (NULL); |
apatel336 | 0:1496281373a5 | 58 | fd_set* writeset = (write) ? (&fdSet) : (NULL); |
apatel336 | 0:1496281373a5 | 59 | |
apatel336 | 0:1496281373a5 | 60 | int ret = lwip_select(FD_SETSIZE, readset, writeset, NULL, timeout); |
apatel336 | 0:1496281373a5 | 61 | return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0); |
apatel336 | 0:1496281373a5 | 62 | } |
apatel336 | 0:1496281373a5 | 63 | |
apatel336 | 0:1496281373a5 | 64 | int Socket::wait_readable(TimeInterval& timeout) { |
apatel336 | 0:1496281373a5 | 65 | return select(&timeout._time, true, false); |
apatel336 | 0:1496281373a5 | 66 | } |
apatel336 | 0:1496281373a5 | 67 | |
apatel336 | 0:1496281373a5 | 68 | int Socket::wait_writable(TimeInterval& timeout) { |
apatel336 | 0:1496281373a5 | 69 | return select(&timeout._time, false, true); |
apatel336 | 0:1496281373a5 | 70 | } |
apatel336 | 0:1496281373a5 | 71 | |
apatel336 | 0:1496281373a5 | 72 | int Socket::close(bool shutdown) { |
apatel336 | 0:1496281373a5 | 73 | if (_sock_fd < 0) |
apatel336 | 0:1496281373a5 | 74 | return -1; |
apatel336 | 0:1496281373a5 | 75 | |
apatel336 | 0:1496281373a5 | 76 | if (shutdown) |
apatel336 | 0:1496281373a5 | 77 | lwip_shutdown(_sock_fd, SHUT_RDWR); |
apatel336 | 0:1496281373a5 | 78 | lwip_close(_sock_fd); |
apatel336 | 0:1496281373a5 | 79 | _sock_fd = -1; |
apatel336 | 0:1496281373a5 | 80 | |
apatel336 | 0:1496281373a5 | 81 | return 0; |
apatel336 | 0:1496281373a5 | 82 | } |
apatel336 | 0:1496281373a5 | 83 | |
apatel336 | 0:1496281373a5 | 84 | Socket::~Socket() { |
apatel336 | 0:1496281373a5 | 85 | close(); //Don't want to leak |
apatel336 | 0:1496281373a5 | 86 | } |
apatel336 | 0:1496281373a5 | 87 | |
apatel336 | 0:1496281373a5 | 88 | TimeInterval::TimeInterval(unsigned int ms) { |
apatel336 | 0:1496281373a5 | 89 | _time.tv_sec = ms / 1000; |
apatel336 | 0:1496281373a5 | 90 | _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000; |
apatel336 | 0:1496281373a5 | 91 | } |