version_2.0

Dependents:   cc3000_ping_demo_try_2

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Committer:
SolderSplashLabs
Date:
Tue Oct 01 21:17:44 2013 +0000
Revision:
11:5e3771b29385
Parent:
8:4c42ca37ba33
Child:
12:1c2a856c618a
Child:
13:5e36c267e62f
Added \r\n to all debug messages; Fixed debug message displaying the port no incorrectly

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) {
Kojto 4:15b58c119a0a 31 #if (CC3000_DEBUG == 1)
SolderSplashLabs 11:5e3771b29385 32 printf("DEBUG: Socket was initialized previously.\r\n");
Kojto 4:15b58c119a0a 33 #endif
Kojto 4:15b58c119a0a 34 return -1;
Kojto 4:15b58c119a0a 35 }
Kojto 4:15b58c119a0a 36
Kojto 4:15b58c119a0a 37 int fd = _cc3000_module->_socket.socket(AF_INET, type, protocol);
Kojto 4:15b58c119a0a 38 if (fd < -1) {
Kojto 4:15b58c119a0a 39 #if (CC3000_DEBUG == 1)
SolderSplashLabs 11:5e3771b29385 40 printf("DEBUG: Failed to create new socket (type: %d, protocol: %d).\r\n",type, protocol);
Kojto 4:15b58c119a0a 41 #endif
Kojto 4:15b58c119a0a 42 return -1;
Kojto 4:15b58c119a0a 43 }
Kojto 4:15b58c119a0a 44 #if (CC3000_DEBUG == 1)
SolderSplashLabs 11:5e3771b29385 45 printf("DEBUG: Socket created (fd: %d type: %d, protocol: %d).\r\n",fd, type, protocol);
Kojto 4:15b58c119a0a 46 #endif
Kojto 4:15b58c119a0a 47 _sock_fd = fd;
Kojto 4:15b58c119a0a 48
Kojto 4:15b58c119a0a 49 return 0;
Kojto 4:15b58c119a0a 50 }
Kojto 4:15b58c119a0a 51
Kojto 0:615c697c33b0 52 void Socket::set_blocking(bool blocking, unsigned int timeout) {
Kojto 0:615c697c33b0 53 _blocking = blocking;
Kojto 0:615c697c33b0 54 _timeout = timeout;
Kojto 0:615c697c33b0 55 }
Kojto 0:615c697c33b0 56
Kojto 0:615c697c33b0 57 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
Kojto 0:615c697c33b0 58 return _cc3000_module->_socket.set_sockopt(_sock_fd, level, optname, optval, optlen);
Kojto 0:615c697c33b0 59 }
Kojto 0:615c697c33b0 60
Kojto 0:615c697c33b0 61 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
Kojto 0:615c697c33b0 62 return _cc3000_module->_socket.get_sockopt(_sock_fd, level, optname, optval, optlen);
Kojto 0:615c697c33b0 63 }
Kojto 0:615c697c33b0 64
Kojto 0:615c697c33b0 65 int Socket::select(struct timeval *timeout, bool read, bool write) {
Kojto 4:15b58c119a0a 66 if (_sock_fd < 0) {
Kojto 0:615c697c33b0 67 return -1;
Kojto 0:615c697c33b0 68 }
Kojto 0:615c697c33b0 69
Kojto 0:615c697c33b0 70 fd_set fdSet;
Kojto 0:615c697c33b0 71 FD_ZERO(&fdSet);
Kojto 0:615c697c33b0 72 FD_SET(_sock_fd, &fdSet);
Kojto 0:615c697c33b0 73
Kojto 0:615c697c33b0 74 fd_set* readset = (read ) ? (&fdSet) : (NULL);
Kojto 0:615c697c33b0 75 fd_set* writeset = (write) ? (&fdSet) : (NULL);
Kojto 0:615c697c33b0 76
Kojto 0:615c697c33b0 77 int ret = _cc3000_module->_socket.select(_sock_fd+1, readset, writeset, NULL, timeout);
Kojto 4:15b58c119a0a 78 #if (CC3000_DEBUG == 1)
SolderSplashLabs 11:5e3771b29385 79 printf("DEBUG: Select on sock_fd: %d, returns %d. fdSet: %d\r\n",_sock_fd, ret, fdSet);
Kojto 4:15b58c119a0a 80 #endif
Kojto 5:245ac5b73132 81 // TODO
Kojto 4:15b58c119a0a 82 //return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
Kojto 4:15b58c119a0a 83 if (FD_ISSET(_sock_fd, &fdSet)) {
Kojto 4:15b58c119a0a 84 return 0;
Kojto 4:15b58c119a0a 85 } else {
Kojto 4:15b58c119a0a 86 return -1;
Kojto 4:15b58c119a0a 87 }
Kojto 0:615c697c33b0 88 }
Kojto 0:615c697c33b0 89
Kojto 0:615c697c33b0 90 int Socket::wait_readable(TimeInterval& timeout) {
Kojto 0:615c697c33b0 91 return select(&timeout._time, true, false);
Kojto 0:615c697c33b0 92 }
Kojto 0:615c697c33b0 93
Kojto 0:615c697c33b0 94 int Socket::wait_writable(TimeInterval& timeout) {
Kojto 0:615c697c33b0 95 return select(&timeout._time, false, true);
Kojto 0:615c697c33b0 96 }
Kojto 0:615c697c33b0 97
Kojto 0:615c697c33b0 98 int Socket::close() {
Kojto 0:615c697c33b0 99 if (_sock_fd < 0 ) {
Kojto 0:615c697c33b0 100 return -1;
Kojto 0:615c697c33b0 101 }
Kojto 0:615c697c33b0 102
Kojto 0:615c697c33b0 103 _cc3000_module->_socket.closesocket(_sock_fd);
Kojto 8:4c42ca37ba33 104 _sock_fd = -1;
Kojto 0:615c697c33b0 105 return 0;
Kojto 0:615c697c33b0 106 }
Kojto 0:615c697c33b0 107
Kojto 0:615c697c33b0 108 Socket::~Socket() {
Kojto 5:245ac5b73132 109 close();
Kojto 0:615c697c33b0 110 }
Kojto 0:615c697c33b0 111
Kojto 0:615c697c33b0 112 TimeInterval::TimeInterval(unsigned int ms) {
Kojto 0:615c697c33b0 113 _time.tv_sec = ms / 1000;
Kojto 0:615c697c33b0 114 _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
Kojto 0:615c697c33b0 115 }