Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

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