Added mutex for multiple SPI devices on the same SPI bus
Fork of cc3000_hostdriver_mbedsocket by
Socket/Socket.cpp@13:5e36c267e62f, 2013-10-02 (annotated)
- Committer:
- SolderSplashLabs
- Date:
- Wed Oct 02 15:00:07 2013 +0000
- Revision:
- 13:5e36c267e62f
- Parent:
- 11:5e3771b29385
- Child:
- 16:f3676ae62f96
Now using a #define for debug printing control, allowing redirection and modification in a single place
Who changed what in which revision?
User | Revision | Line number | New 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 | } |
SolderSplashLabs | 13:5e36c267e62f | 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 | 0:615c697c33b0 | 53 | return _cc3000_module->_socket.set_sockopt(_sock_fd, level, optname, optval, optlen); |
Kojto | 0:615c697c33b0 | 54 | } |
Kojto | 0:615c697c33b0 | 55 | |
Kojto | 0:615c697c33b0 | 56 | int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) { |
Kojto | 0:615c697c33b0 | 57 | return _cc3000_module->_socket.get_sockopt(_sock_fd, level, optname, optval, optlen); |
Kojto | 0:615c697c33b0 | 58 | } |
Kojto | 0:615c697c33b0 | 59 | |
Kojto | 0:615c697c33b0 | 60 | int Socket::select(struct timeval *timeout, bool read, bool write) { |
Kojto | 4:15b58c119a0a | 61 | if (_sock_fd < 0) { |
Kojto | 0:615c697c33b0 | 62 | return -1; |
Kojto | 0:615c697c33b0 | 63 | } |
Kojto | 0:615c697c33b0 | 64 | |
Kojto | 0:615c697c33b0 | 65 | fd_set fdSet; |
Kojto | 0:615c697c33b0 | 66 | FD_ZERO(&fdSet); |
Kojto | 0:615c697c33b0 | 67 | FD_SET(_sock_fd, &fdSet); |
Kojto | 0:615c697c33b0 | 68 | |
Kojto | 0:615c697c33b0 | 69 | fd_set* readset = (read ) ? (&fdSet) : (NULL); |
Kojto | 0:615c697c33b0 | 70 | fd_set* writeset = (write) ? (&fdSet) : (NULL); |
Kojto | 0:615c697c33b0 | 71 | |
Kojto | 0:615c697c33b0 | 72 | int ret = _cc3000_module->_socket.select(_sock_fd+1, readset, writeset, NULL, timeout); |
SolderSplashLabs | 13:5e36c267e62f | 73 | |
SolderSplashLabs | 13:5e36c267e62f | 74 | DBG_SOCKET("Select on sock_fd: %d, returns %d. fdSet: %d", _sock_fd, ret, fdSet); |
SolderSplashLabs | 13:5e36c267e62f | 75 | |
Kojto | 5:245ac5b73132 | 76 | // TODO |
Kojto | 4:15b58c119a0a | 77 | //return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0); |
Kojto | 4:15b58c119a0a | 78 | if (FD_ISSET(_sock_fd, &fdSet)) { |
Kojto | 4:15b58c119a0a | 79 | return 0; |
Kojto | 4:15b58c119a0a | 80 | } else { |
Kojto | 4:15b58c119a0a | 81 | return -1; |
Kojto | 4:15b58c119a0a | 82 | } |
Kojto | 0:615c697c33b0 | 83 | } |
Kojto | 0:615c697c33b0 | 84 | |
Kojto | 0:615c697c33b0 | 85 | int Socket::wait_readable(TimeInterval& timeout) { |
Kojto | 0:615c697c33b0 | 86 | return select(&timeout._time, true, false); |
Kojto | 0:615c697c33b0 | 87 | } |
Kojto | 0:615c697c33b0 | 88 | |
Kojto | 0:615c697c33b0 | 89 | int Socket::wait_writable(TimeInterval& timeout) { |
Kojto | 0:615c697c33b0 | 90 | return select(&timeout._time, false, true); |
Kojto | 0:615c697c33b0 | 91 | } |
Kojto | 0:615c697c33b0 | 92 | |
Kojto | 0:615c697c33b0 | 93 | int Socket::close() { |
Kojto | 0:615c697c33b0 | 94 | if (_sock_fd < 0 ) { |
Kojto | 0:615c697c33b0 | 95 | return -1; |
Kojto | 0:615c697c33b0 | 96 | } |
Kojto | 0:615c697c33b0 | 97 | |
Kojto | 0:615c697c33b0 | 98 | _cc3000_module->_socket.closesocket(_sock_fd); |
Kojto | 8:4c42ca37ba33 | 99 | _sock_fd = -1; |
Kojto | 0:615c697c33b0 | 100 | return 0; |
Kojto | 0:615c697c33b0 | 101 | } |
Kojto | 0:615c697c33b0 | 102 | |
Kojto | 0:615c697c33b0 | 103 | Socket::~Socket() { |
Kojto | 5:245ac5b73132 | 104 | close(); |
Kojto | 0:615c697c33b0 | 105 | } |
Kojto | 0:615c697c33b0 | 106 | |
Kojto | 0:615c697c33b0 | 107 | TimeInterval::TimeInterval(unsigned int ms) { |
Kojto | 0:615c697c33b0 | 108 | _time.tv_sec = ms / 1000; |
Kojto | 0:615c697c33b0 | 109 | _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000; |
Kojto | 0:615c697c33b0 | 110 | } |