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