cc3000 hostdriver with the mbed socket interface

Dependents:   cc3000_hello_world_demo cc3000_simple_socket_demo cc3000_ntp_demo cc3000_ping_demo ... more

Committer:
Kojto
Date:
Thu Sep 19 07:55:14 2013 +0000
Revision:
0:615c697c33b0
Child:
4:15b58c119a0a
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 0:615c697c33b0 1 /* Copyright (C) 2012 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 0:615c697c33b0 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 0:615c697c33b0 29 void Socket::set_blocking(bool blocking, unsigned int timeout) {
Kojto 0:615c697c33b0 30 _blocking = blocking;
Kojto 0:615c697c33b0 31 _timeout = timeout;
Kojto 0:615c697c33b0 32 }
Kojto 0:615c697c33b0 33
Kojto 0:615c697c33b0 34 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
Kojto 0:615c697c33b0 35 return _cc3000_module->_socket.set_sockopt(_sock_fd, level, optname, optval, optlen);
Kojto 0:615c697c33b0 36 }
Kojto 0:615c697c33b0 37
Kojto 0:615c697c33b0 38 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
Kojto 0:615c697c33b0 39 return _cc3000_module->_socket.get_sockopt(_sock_fd, level, optname, optval, optlen);
Kojto 0:615c697c33b0 40 }
Kojto 0:615c697c33b0 41
Kojto 0:615c697c33b0 42 int Socket::select(struct timeval *timeout, bool read, bool write) {
Kojto 0:615c697c33b0 43 if (_sock_fd < 0 ) {
Kojto 0:615c697c33b0 44 return -1;
Kojto 0:615c697c33b0 45 }
Kojto 0:615c697c33b0 46
Kojto 0:615c697c33b0 47 fd_set fdSet;
Kojto 0:615c697c33b0 48 FD_ZERO(&fdSet);
Kojto 0:615c697c33b0 49 FD_SET(_sock_fd, &fdSet);
Kojto 0:615c697c33b0 50
Kojto 0:615c697c33b0 51 fd_set* readset = (read ) ? (&fdSet) : (NULL);
Kojto 0:615c697c33b0 52 fd_set* writeset = (write) ? (&fdSet) : (NULL);
Kojto 0:615c697c33b0 53
Kojto 0:615c697c33b0 54 int ret = _cc3000_module->_socket.select(_sock_fd+1, readset, writeset, NULL, timeout);
Kojto 0:615c697c33b0 55 return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
Kojto 0:615c697c33b0 56 }
Kojto 0:615c697c33b0 57
Kojto 0:615c697c33b0 58 int Socket::wait_readable(TimeInterval& timeout) {
Kojto 0:615c697c33b0 59 return select(&timeout._time, true, false);
Kojto 0:615c697c33b0 60 }
Kojto 0:615c697c33b0 61
Kojto 0:615c697c33b0 62 int Socket::wait_writable(TimeInterval& timeout) {
Kojto 0:615c697c33b0 63 return select(&timeout._time, false, true);
Kojto 0:615c697c33b0 64 }
Kojto 0:615c697c33b0 65
Kojto 0:615c697c33b0 66 int Socket::close() {
Kojto 0:615c697c33b0 67 if (_sock_fd < 0 ) {
Kojto 0:615c697c33b0 68 return -1;
Kojto 0:615c697c33b0 69 }
Kojto 0:615c697c33b0 70
Kojto 0:615c697c33b0 71 _cc3000_module->_socket.closesocket(_sock_fd);
Kojto 0:615c697c33b0 72 return 0;
Kojto 0:615c697c33b0 73 }
Kojto 0:615c697c33b0 74
Kojto 0:615c697c33b0 75 Socket::~Socket() {
Kojto 0:615c697c33b0 76 close(); //Don't want to leak
Kojto 0:615c697c33b0 77 }
Kojto 0:615c697c33b0 78
Kojto 0:615c697c33b0 79 TimeInterval::TimeInterval(unsigned int ms) {
Kojto 0:615c697c33b0 80 _time.tv_sec = ms / 1000;
Kojto 0:615c697c33b0 81 _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
Kojto 0:615c697c33b0 82 }