Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Socket.cpp
00001 /* Copyright (C) 2013 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #include "Socket.h" 00020 #include <cstring> 00021 00022 Socket::Socket() : _sock_fd(-1), _blocking(true), _timeout(1500) { 00023 _cc3000_module = cc3000::get_instance(); 00024 if (_cc3000_module == NULL) { 00025 error("Socket constructor error: no cc3000 instance available!\r\n"); 00026 } 00027 } 00028 00029 int Socket::init_socket(int type, int protocol) { 00030 if (_sock_fd != -1) { 00031 DBG_SOCKET("Socket was initialized previously"); 00032 return -1; 00033 } 00034 00035 int fd = _cc3000_module->_socket.socket(AF_INET, type, protocol); 00036 if (fd < -1) { 00037 DBG_SOCKET("Failed to create new socket (type: %d, protocol: %d)",type, protocol); 00038 return -1; 00039 } 00040 00041 DBG_SOCKET("Socket created (fd: %d type: %d, protocol: %d)",fd, type, protocol); 00042 _sock_fd = fd; 00043 00044 return 0; 00045 } 00046 00047 void Socket::set_blocking(bool blocking, unsigned int timeout) { 00048 _blocking = blocking; 00049 _timeout = timeout; 00050 } 00051 00052 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) { 00053 #ifndef CC3000_TINY_DRIVER 00054 return _cc3000_module->_socket.setsockopt(_sock_fd, level, optname, optval, optlen); 00055 #else 00056 return -1; 00057 #endif 00058 } 00059 00060 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) { 00061 return _cc3000_module->_socket.getsockopt(_sock_fd, level, optname, optval, optlen); 00062 } 00063 00064 int Socket::select(struct timeval *timeout, bool read, bool write) { 00065 if (_sock_fd < 0) { 00066 return -1; 00067 } 00068 00069 fd_set fdSet; 00070 FD_ZERO(&fdSet); 00071 FD_SET(_sock_fd, &fdSet); 00072 00073 fd_set* readset = (read ) ? (&fdSet) : (NULL); 00074 fd_set* writeset = (write) ? (&fdSet) : (NULL); 00075 00076 int ret = _cc3000_module->_socket.select(_sock_fd+1, readset, writeset, NULL, timeout); 00077 00078 DBG_SOCKET("Select on sock_fd: %d, returns %d. fdSet: %d", _sock_fd, ret, FD_ISSET(_sock_fd, &fdSet)); 00079 00080 // TODO 00081 //return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0); 00082 if (FD_ISSET(_sock_fd, &fdSet)) { 00083 return 0; 00084 } else { 00085 return -1; 00086 } 00087 } 00088 00089 int Socket::wait_readable(TimeInterval& timeout) { 00090 return select(&timeout._time, true, false); 00091 } 00092 00093 int Socket::wait_writable(TimeInterval& timeout) { 00094 return select(&timeout._time, false, true); 00095 } 00096 00097 int Socket::close() { 00098 if (_sock_fd < 0 ) { 00099 return -1; 00100 } 00101 00102 _cc3000_module->_socket.closesocket(_sock_fd); 00103 _sock_fd = -1; 00104 return 0; 00105 } 00106 00107 Socket::~Socket() { 00108 close(); 00109 } 00110 00111 TimeInterval::TimeInterval(unsigned int ms) { 00112 _time.tv_sec = ms / 1000; 00113 _time.tv_usec = (ms - (_time.tv_sec * 1000)) * 1000; 00114 } 00115
Generated on Tue Jul 12 2022 18:55:01 by
