Added mutex for multiple SPI devices on the same SPI bus
Fork of cc3000_hostdriver_mbedsocket by
Socket/Socket.cpp@7:99b75a51e168, 2013-09-26 (annotated)
- Committer:
- Kojto
- Date:
- Thu Sep 26 20:03:30 2013 +0000
- Revision:
- 7:99b75a51e168
- Parent:
- 5:245ac5b73132
- Child:
- 8:4c42ca37ba33
correction in socket.cpp (copied text twice)
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) { |
Kojto | 4:15b58c119a0a | 31 | #if (CC3000_DEBUG == 1) |
Kojto | 4:15b58c119a0a | 32 | printf("DEBUG: Socket was initialized previously.\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) |
Kojto | 5:245ac5b73132 | 40 | printf("DEBUG: Failed to create new socket (type: %d, protocol: %d).\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) |
Kojto | 4:15b58c119a0a | 45 | printf("DEBUG: Socket created (fd: %d type: %d, protocol: %d).\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) |
Kojto | 4:15b58c119a0a | 79 | printf("DEBUG: Select on sock_fd: %d, returns %d. fdSet: %d\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 | 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 | } |