version_2.0

Dependents:   cc3000_ping_demo_try_2

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Committer:
erezi
Date:
Wed Jun 25 06:08:39 2014 +0000
Revision:
47:e9d12b82813e
Parent:
45:50ab13d8f2dc
version_2.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 5:245ac5b73132 1 /* Copyright (C) 2013 mbed.org, MIT License
Kojto 0:615c697c33b0 2 *
Kojto 0:615c697c33b0 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Kojto 0:615c697c33b0 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Kojto 0:615c697c33b0 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Kojto 0:615c697c33b0 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Kojto 0:615c697c33b0 7 * furnished to do so, subject to the following conditions:
Kojto 0:615c697c33b0 8 *
Kojto 0:615c697c33b0 9 * The above copyright notice and this permission notice shall be included in all copies or
Kojto 0:615c697c33b0 10 * substantial portions of the Software.
Kojto 0:615c697c33b0 11 *
Kojto 0:615c697c33b0 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Kojto 0:615c697c33b0 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Kojto 0:615c697c33b0 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Kojto 0:615c697c33b0 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Kojto 0:615c697c33b0 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Kojto 0:615c697c33b0 17 */
Kojto 0:615c697c33b0 18
Kojto 0:615c697c33b0 19 #include "Socket.h"
Kojto 0:615c697c33b0 20 #include <cstring>
Kojto 0:615c697c33b0 21
Kojto 5:245ac5b73132 22 Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) {
Kojto 0:615c697c33b0 23 _cc3000_module = cc3000::get_instance();
Kojto 0:615c697c33b0 24 if (_cc3000_module == NULL) {
Kojto 0:615c697c33b0 25 error("Socket constructor error: no cc3000 instance available!\r\n");
Kojto 0:615c697c33b0 26 }
Kojto 0:615c697c33b0 27 }
Kojto 0:615c697c33b0 28
Kojto 4:15b58c119a0a 29 int Socket::init_socket(int type, int protocol) {
Kojto 4:15b58c119a0a 30 if (_sock_fd != -1) {
SolderSplashLabs 13:5e36c267e62f 31 DBG_SOCKET("Socket was initialized previously");
Kojto 4:15b58c119a0a 32 return -1;
Kojto 4:15b58c119a0a 33 }
Kojto 4:15b58c119a0a 34
Kojto 4:15b58c119a0a 35 int fd = _cc3000_module->_socket.socket(AF_INET, type, protocol);
Kojto 4:15b58c119a0a 36 if (fd < -1) {
SolderSplashLabs 13:5e36c267e62f 37 DBG_SOCKET("Failed to create new socket (type: %d, protocol: %d)",type, protocol);
Kojto 4:15b58c119a0a 38 return -1;
Kojto 4:15b58c119a0a 39 }
Kojto 45:50ab13d8f2dc 40
SolderSplashLabs 13:5e36c267e62f 41 DBG_SOCKET("Socket created (fd: %d type: %d, protocol: %d)",fd, type, protocol);
Kojto 4:15b58c119a0a 42 _sock_fd = fd;
Kojto 4:15b58c119a0a 43
Kojto 4:15b58c119a0a 44 return 0;
Kojto 4:15b58c119a0a 45 }
Kojto 4:15b58c119a0a 46
Kojto 0:615c697c33b0 47 void Socket::set_blocking(bool blocking, unsigned int timeout) {
Kojto 0:615c697c33b0 48 _blocking = blocking;
Kojto 0:615c697c33b0 49 _timeout = timeout;
Kojto 0:615c697c33b0 50 }
Kojto 0:615c697c33b0 51
Kojto 0:615c697c33b0 52 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
Kojto 45:50ab13d8f2dc 53 #ifndef CC3000_TINY_DRIVER
Martin Kojtal 12:1c2a856c618a 54 return _cc3000_module->_socket.setsockopt(_sock_fd, level, optname, optval, optlen);
Kojto 45:50ab13d8f2dc 55 #else
Kojto 45:50ab13d8f2dc 56 return -1;
Kojto 45:50ab13d8f2dc 57 #endif
Kojto 0:615c697c33b0 58 }
Kojto 0:615c697c33b0 59
Kojto 0:615c697c33b0 60 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
Martin Kojtal 12:1c2a856c618a 61 return _cc3000_module->_socket.getsockopt(_sock_fd, level, optname, optval, optlen);
Kojto 0:615c697c33b0 62 }
Kojto 0:615c697c33b0 63
Kojto 0:615c697c33b0 64 int Socket::select(struct timeval *timeout, bool read, bool write) {
Kojto 4:15b58c119a0a 65 if (_sock_fd < 0) {
Kojto 0:615c697c33b0 66 return -1;
Kojto 0:615c697c33b0 67 }
Kojto 0:615c697c33b0 68
Kojto 0:615c697c33b0 69 fd_set fdSet;
Kojto 0:615c697c33b0 70 FD_ZERO(&fdSet);
Kojto 0:615c697c33b0 71 FD_SET(_sock_fd, &fdSet);
Kojto 0:615c697c33b0 72
Kojto 0:615c697c33b0 73 fd_set* readset = (read ) ? (&fdSet) : (NULL);
Kojto 0:615c697c33b0 74 fd_set* writeset = (write) ? (&fdSet) : (NULL);
Kojto 0:615c697c33b0 75
Kojto 0:615c697c33b0 76 int ret = _cc3000_module->_socket.select(_sock_fd+1, readset, writeset, NULL, timeout);
Kojto 45:50ab13d8f2dc 77
Kojto 17:14b6a3a2b622 78 DBG_SOCKET("Select on sock_fd: %d, returns %d. fdSet: %d", _sock_fd, ret, FD_ISSET(_sock_fd, &fdSet));
Kojto 45:50ab13d8f2dc 79
Kojto 5:245ac5b73132 80 // TODO
Kojto 4:15b58c119a0a 81 //return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
Kojto 4:15b58c119a0a 82 if (FD_ISSET(_sock_fd, &fdSet)) {
Kojto 4:15b58c119a0a 83 return 0;
Kojto 4:15b58c119a0a 84 } else {
Kojto 4:15b58c119a0a 85 return -1;
Kojto 4:15b58c119a0a 86 }
Kojto 0:615c697c33b0 87 }
Kojto 0:615c697c33b0 88
Kojto 0:615c697c33b0 89 int Socket::wait_readable(TimeInterval& timeout) {
Kojto 0:615c697c33b0 90 return select(&timeout._time, true, false);
Kojto 0:615c697c33b0 91 }
Kojto 0:615c697c33b0 92
Kojto 0:615c697c33b0 93 int Socket::wait_writable(TimeInterval& timeout) {
Kojto 0:615c697c33b0 94 return select(&timeout._time, false, true);
Kojto 0:615c697c33b0 95 }
Kojto 0:615c697c33b0 96
Kojto 0:615c697c33b0 97 int Socket::close() {
Kojto 0:615c697c33b0 98 if (_sock_fd < 0 ) {
Kojto 0:615c697c33b0 99 return -1;
Kojto 0:615c697c33b0 100 }
Kojto 0:615c697c33b0 101
Kojto 0:615c697c33b0 102 _cc3000_module->_socket.closesocket(_sock_fd);
Kojto 8:4c42ca37ba33 103 _sock_fd = -1;
Kojto 0:615c697c33b0 104 return 0;
Kojto 0:615c697c33b0 105 }
Kojto 0:615c697c33b0 106
Kojto 0:615c697c33b0 107 Socket::~Socket() {
Kojto 5:245ac5b73132 108 close();
Kojto 0:615c697c33b0 109 }
Kojto 0:615c697c33b0 110
Kojto 0:615c697c33b0 111 TimeInterval::TimeInterval(unsigned int ms) {
Kojto 0:615c697c33b0 112 _time.tv_sec = ms / 1000;
Kojto 0:615c697c33b0 113 _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
Kojto 0:615c697c33b0 114 }