Added mutex for multiple SPI devices on the same SPI bus

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Committer:
Kojto
Date:
Thu Sep 26 19:50:37 2013 +0000
Revision:
5:245ac5b73132
Parent:
4:15b58c119a0a
Child:
7:99b75a51e168
tcp implementation

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)
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 }
Kojto 5:245ac5b73132 115 /* Copyright (C) 2013 mbed.org, MIT License
Kojto 5:245ac5b73132 116 *
Kojto 5:245ac5b73132 117 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Kojto 5:245ac5b73132 118 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Kojto 5:245ac5b73132 119 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Kojto 5:245ac5b73132 120 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Kojto 5:245ac5b73132 121 * furnished to do so, subject to the following conditions:
Kojto 5:245ac5b73132 122 *
Kojto 5:245ac5b73132 123 * The above copyright notice and this permission notice shall be included in all copies or
Kojto 5:245ac5b73132 124 * substantial portions of the Software.
Kojto 5:245ac5b73132 125 *
Kojto 5:245ac5b73132 126 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Kojto 5:245ac5b73132 127 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Kojto 5:245ac5b73132 128 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Kojto 5:245ac5b73132 129 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Kojto 5:245ac5b73132 130 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Kojto 5:245ac5b73132 131 */
Kojto 5:245ac5b73132 132
Kojto 5:245ac5b73132 133 #include "Socket.h"
Kojto 5:245ac5b73132 134 #include <cstring>
Kojto 5:245ac5b73132 135
Kojto 5:245ac5b73132 136 Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) {
Kojto 5:245ac5b73132 137 _cc3000_module = cc3000::get_instance();
Kojto 5:245ac5b73132 138 if (_cc3000_module == NULL) {
Kojto 5:245ac5b73132 139 error("Socket constructor error: no cc3000 instance available!\r\n");
Kojto 5:245ac5b73132 140 }
Kojto 5:245ac5b73132 141 }
Kojto 5:245ac5b73132 142
Kojto 5:245ac5b73132 143 int Socket::init_socket(int type, int protocol) {
Kojto 5:245ac5b73132 144 if (_sock_fd != -1) {
Kojto 5:245ac5b73132 145 #if (CC3000_DEBUG == 1)
Kojto 5:245ac5b73132 146 printf("DEBUG: Socket was initialized previously.\n");
Kojto 5:245ac5b73132 147 #endif
Kojto 5:245ac5b73132 148 return -1;
Kojto 5:245ac5b73132 149 }
Kojto 5:245ac5b73132 150
Kojto 5:245ac5b73132 151 int fd = _cc3000_module->_socket.socket(AF_INET, type, protocol);
Kojto 5:245ac5b73132 152 if (fd < -1) {
Kojto 5:245ac5b73132 153 #if (CC3000_DEBUG == 1)
Kojto 5:245ac5b73132 154 printf("DEBUG: Failed to create new socket (type: %d, protocol: %d).\n",type, protocol);
Kojto 5:245ac5b73132 155 #endif
Kojto 5:245ac5b73132 156 return -1;
Kojto 5:245ac5b73132 157 }
Kojto 5:245ac5b73132 158 #if (CC3000_DEBUG == 1)
Kojto 5:245ac5b73132 159 printf("DEBUG: Socket created (fd: %d type: %d, protocol: %d).\n",fd, type, protocol);
Kojto 5:245ac5b73132 160 #endif
Kojto 5:245ac5b73132 161 _sock_fd = fd;
Kojto 5:245ac5b73132 162
Kojto 5:245ac5b73132 163 return 0;
Kojto 5:245ac5b73132 164 }
Kojto 5:245ac5b73132 165
Kojto 5:245ac5b73132 166 void Socket::set_blocking(bool blocking, unsigned int timeout) {
Kojto 5:245ac5b73132 167 _blocking = blocking;
Kojto 5:245ac5b73132 168 _timeout = timeout;
Kojto 5:245ac5b73132 169 }
Kojto 5:245ac5b73132 170
Kojto 5:245ac5b73132 171 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
Kojto 5:245ac5b73132 172 return _cc3000_module->_socket.set_sockopt(_sock_fd, level, optname, optval, optlen);
Kojto 5:245ac5b73132 173 }
Kojto 5:245ac5b73132 174
Kojto 5:245ac5b73132 175 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
Kojto 5:245ac5b73132 176 return _cc3000_module->_socket.get_sockopt(_sock_fd, level, optname, optval, optlen);
Kojto 5:245ac5b73132 177 }
Kojto 5:245ac5b73132 178
Kojto 5:245ac5b73132 179 int Socket::select(struct timeval *timeout, bool read, bool write) {
Kojto 5:245ac5b73132 180 if (_sock_fd < 0) {
Kojto 5:245ac5b73132 181 return -1;
Kojto 5:245ac5b73132 182 }
Kojto 5:245ac5b73132 183
Kojto 5:245ac5b73132 184 fd_set fdSet;
Kojto 5:245ac5b73132 185 FD_ZERO(&fdSet);
Kojto 5:245ac5b73132 186 FD_SET(_sock_fd, &fdSet);
Kojto 5:245ac5b73132 187
Kojto 5:245ac5b73132 188 fd_set* readset = (read ) ? (&fdSet) : (NULL);
Kojto 5:245ac5b73132 189 fd_set* writeset = (write) ? (&fdSet) : (NULL);
Kojto 5:245ac5b73132 190
Kojto 5:245ac5b73132 191 int ret = _cc3000_module->_socket.select(_sock_fd+1, readset, writeset, NULL, timeout);
Kojto 5:245ac5b73132 192 #if (CC3000_DEBUG == 1)
Kojto 5:245ac5b73132 193 printf("DEBUG: Select on sock_fd: %d, returns %d. fdSet: %d\n",_sock_fd, ret, fdSet);
Kojto 5:245ac5b73132 194 #endif
Kojto 5:245ac5b73132 195 // TODO
Kojto 5:245ac5b73132 196 //return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
Kojto 5:245ac5b73132 197 if (FD_ISSET(_sock_fd, &fdSet)) {
Kojto 5:245ac5b73132 198 return 0;
Kojto 5:245ac5b73132 199 } else {
Kojto 5:245ac5b73132 200 return -1;
Kojto 5:245ac5b73132 201 }
Kojto 5:245ac5b73132 202 }
Kojto 5:245ac5b73132 203
Kojto 5:245ac5b73132 204 int Socket::wait_readable(TimeInterval& timeout) {
Kojto 5:245ac5b73132 205 return select(&timeout._time, true, false);
Kojto 5:245ac5b73132 206 }
Kojto 5:245ac5b73132 207
Kojto 5:245ac5b73132 208 int Socket::wait_writable(TimeInterval& timeout) {
Kojto 5:245ac5b73132 209 return select(&timeout._time, false, true);
Kojto 5:245ac5b73132 210 }
Kojto 5:245ac5b73132 211
Kojto 5:245ac5b73132 212 int Socket::close() {
Kojto 5:245ac5b73132 213 if (_sock_fd < 0 ) {
Kojto 5:245ac5b73132 214 return -1;
Kojto 5:245ac5b73132 215 }
Kojto 5:245ac5b73132 216
Kojto 5:245ac5b73132 217 _cc3000_module->_socket.closesocket(_sock_fd);
Kojto 5:245ac5b73132 218 return 0;
Kojto 5:245ac5b73132 219 }
Kojto 5:245ac5b73132 220
Kojto 5:245ac5b73132 221 Socket::~Socket() {
Kojto 5:245ac5b73132 222 close();
Kojto 5:245ac5b73132 223 }
Kojto 5:245ac5b73132 224
Kojto 5:245ac5b73132 225 TimeInterval::TimeInterval(unsigned int ms) {
Kojto 5:245ac5b73132 226 _time.tv_sec = ms / 1000;
Kojto 5:245ac5b73132 227 _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000;
Kojto 5:245ac5b73132 228 }