cc3000 hostdriver with the mbed socket interface
Dependents: cc3000_hello_world_demo cc3000_simple_socket_demo cc3000_ntp_demo cc3000_ping_demo ... more
Revision 4:15b58c119a0a, committed 2013-09-21
- Comitter:
- Kojto
- Date:
- Sat Sep 21 15:01:05 2013 +0000
- Parent:
- 3:ad95e296bfbf
- Child:
- 5:245ac5b73132
- Commit message:
- mbed socket interface changes; - socket init (creates upd/tcp socket); - debug prints; host driver changes; - blocking call when closing socket (Frank's recommedation); - delete profiles (user info retrieved, then erased)
Changed in this revision
--- a/Socket/Endpoint.cpp Thu Sep 19 08:04:22 2013 +0000 +++ b/Socket/Endpoint.cpp Sat Sep 21 15:01:05 2013 +0000 @@ -1,123 +1,142 @@ -/* Copyright (C) 2012 mbed.org, MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -#include "Socket/Socket.h" -#include "Socket/Endpoint.h" -#include <cstring> - - #include "cc3000.h" - -using std::memset; - -Endpoint::Endpoint() { - _cc3000_module = cc3000::get_instance(); - if (_cc3000_module == NULL) { - error("Endpoint constructor error: no cc3000 instance available!\r\n"); - } - reset_address(); -} -Endpoint::~Endpoint() {} - -void Endpoint::reset_address(void) { - _ipAddress[0] = '\0'; - std::memset(&_remote_host,0, sizeof(sockaddr)); -} - -int Endpoint::set_address(const char* host, const int port) { - reset_address(); - - // IP Address - char address[5]; - char *p_address = address; - - // Dot-decimal notation - int result = std::sscanf(host, "%3u.%3u.%3u.%3u", - (unsigned int*)&address[0], (unsigned int*)&address[1], - (unsigned int*)&address[2], (unsigned int*)&address[3]); - - if (result != 4) { - //Resolve DNS address or populate hard-coded IP address - uint32_t address_integer; - _cc3000_module->_socket.get_host_by_name((uint8_t *)host, strlen(host) , &address_integer); - - address[0] = (address_integer >> 24); - address[1] = (address_integer >> 16); - address[2] = (address_integer >> 8); - address[3] = (address_integer >> 0); - sprintf(_ipAddress,"%3u.%3u.%3u.%3u", address[0],address[1],address[2],address[3]); - _remote_host.sin_addr.s_addr = address_integer; - } else { - std::memcpy((char*)&_remote_host.sin_addr.s_addr, p_address, 4); - } - - /* store address*/ - _remote_host.sin_family = AF_INET; - - // Set port - _remote_host.sin_port = htons(port); - - return 0; -} +/* Copyright (C) 2012 mbed.org, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#include "Socket/Socket.h" +#include "Socket/Endpoint.h" +#include "Helper/def.h" +#include <cstring> + + #include "cc3000.h" + +/* Copied from lwip */ +static char *inet_ntoa_r(const in_addr addr, char *buf, int buflen) +{ + uint32_t s_addr; + char inv[3]; + char *rp; + uint8_t *ap; + uint8_t rem; + uint8_t n; + uint8_t i; + int len = 0; + + s_addr = addr.s_addr; + + rp = buf; + ap = (uint8_t *)&s_addr; + for(n = 0; n < 4; n++) { + i = 0; + do { + rem = *ap % (uint8_t)10; + *ap /= (uint8_t)10; + inv[i++] = '0' + rem; + } while(*ap); + while(i--) { + if (len++ >= buflen) { + return NULL; + } + *rp++ = inv[i]; + } + if (len++ >= buflen) { + return NULL; + } + *rp++ = '.'; + ap++; + } + *--rp = 0; + return buf; +} -static char *inet_ntoa_r(const in_addr addr, char *buf, int buflen) -{ - uint32_t s_addr; - char inv[3]; - char *rp; - uint8_t *ap; - uint8_t rem; - uint8_t n; - uint8_t i; - int len = 0; - - s_addr = addr.s_addr; - - rp = buf; - ap = (uint8_t *)&s_addr; - for(n = 0; n < 4; n++) { - i = 0; - do { - rem = *ap % (uint8_t)10; - *ap /= (uint8_t)10; - inv[i++] = '0' + rem; - } while(*ap); - while(i--) { - if (len++ >= buflen) { - return NULL; - } - *rp++ = inv[i]; - } - if (len++ >= buflen) { - return NULL; - } - *rp++ = '.'; - ap++; - } - *--rp = 0; - return buf; -} - -char* Endpoint::get_address() { - if ((_ipAddress[0] == '\0') && (_remote_host.sin_addr.s_addr != 0)) - inet_ntoa_r(_remote_host.sin_addr, _ipAddress, sizeof(_ipAddress)); - return _ipAddress; -} - - -int Endpoint::get_port() { - return ntohs(_remote_host.sin_port); -} +Endpoint::Endpoint() { + _cc3000_module = cc3000::get_instance(); + if (_cc3000_module == NULL) { + error("Endpoint constructor error: no cc3000 instance available!\r\n"); + } + reset_address(); +} +Endpoint::~Endpoint() {} + +void Endpoint::reset_address(void) { + _ipAddress[0] = '\0'; + std::memset(&_remote_host,0, sizeof(sockaddr_in)); +} + +int Endpoint::set_address(const char* host, const int port) { + reset_address(); + + // IP Address + char address[5]; + char *p_address = address; + + signed int add[5]; + + // Dot-decimal notation + int result = std::sscanf(host, "%3u.%3u.%3u.%3u", &add[0], &add[1], &add[2], &add[3]); + for (int i=0;i<4;i++) { + address[i] = add[i]; + } + + if (result != 4) { + //Resolve DNS address or populate hard-coded IP address + uint32_t address_integer; + _cc3000_module->_socket.get_host_by_name((uint8_t *)host, strlen(host) , &address_integer); + + _remote_host.sin_addr.s_addr = address_integer; + inet_ntoa_r(_remote_host.sin_addr, _ipAddress, sizeof(_ipAddress)); + // address[0] = (address_integer >> 24); + // address[1] = (address_integer >> 16); + // address[2] = (address_integer >> 8); + // address[3] = (address_integer >> 0); + // sprintf(_ipAddress,"%3u.%3u.%3u.%3u", address[0],address[1],address[2],address[3]); + // p_address = _ipAddress; + // _remote_host.sin_addr.s_addr = address_integer; + } else { + std::memcpy((char*)&_remote_host.sin_addr.s_addr, p_address, 4); + } + +#if (CC3000_DEBUG == 1) + printf("DEBUG: remote host address (string): %s\n",_ipAddress); + printf("DEBUG: remote host address from s_addr : %d.%d.%d.%d\n", + int(_remote_host.sin_addr.s_addr & 0xFF), + int((_remote_host.sin_addr.s_addr & 0xFF00)>>8), + int((_remote_host.sin_addr.s_addr & 0xFF0000)>>16), + int((_remote_host.sin_addr.s_addr & 0xFF000000)>>24)); + // address[0] = (_remote_host.sin_addr.s_addr >> 24); + // address[1] = (_remote_host.sin_addr.s_addr >> 16); + // address[2] = (_remote_host.sin_addr.s_addr >> 8); + // address[3] = (_remote_host.sin_addr.s_addr >> 0); + // printf("DEBUG: remote host address from s_addr: %3u.%3u.%3u.%3u\n",address[0],address[1],address[2],address[3]); +#endif + /* store address*/ + _remote_host.sin_family = AF_INET; + + // Set port + _remote_host.sin_port = htons(port); + + return 0; +} + +char* Endpoint::get_address() { + if ((_ipAddress[0] == '\0') && (_remote_host.sin_addr.s_addr != 0)) + inet_ntoa_r(_remote_host.sin_addr, _ipAddress, sizeof(_ipAddress)); + return _ipAddress; +} + + +int Endpoint::get_port() { + return ntohs(_remote_host.sin_port); +}
--- a/Socket/Socket.cpp Thu Sep 19 08:04:22 2013 +0000 +++ b/Socket/Socket.cpp Sat Sep 21 15:01:05 2013 +0000 @@ -26,6 +26,29 @@ } } +int Socket::init_socket(int type, int protocol) { + if (_sock_fd != -1) { +#if (CC3000_DEBUG == 1) + printf("DEBUG: Socket was initialized previously.\n"); +#endif + return -1; + } + + int fd = _cc3000_module->_socket.socket(AF_INET, type, protocol); + if (fd < -1) { +#if (CC3000_DEBUG == 1) + printf("DEBUG: Failed to create new socket (type:%d, protocol:%d).\n",type, protocol); +#endif + return -1; + } +#if (CC3000_DEBUG == 1) + printf("DEBUG: Socket created (fd: %d type: %d, protocol: %d).\n",fd, type, protocol); +#endif + _sock_fd = fd; + + return 0; +} + void Socket::set_blocking(bool blocking, unsigned int timeout) { _blocking = blocking; _timeout = timeout; @@ -40,7 +63,7 @@ } int Socket::select(struct timeval *timeout, bool read, bool write) { - if (_sock_fd < 0 ) { + if (_sock_fd < 0) { return -1; } @@ -52,7 +75,15 @@ fd_set* writeset = (write) ? (&fdSet) : (NULL); int ret = _cc3000_module->_socket.select(_sock_fd+1, readset, writeset, NULL, timeout); - return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0); +#if (CC3000_DEBUG == 1) + printf("DEBUG: Select on sock_fd: %d, returns %d. fdSet: %d\n",_sock_fd, ret, fdSet); +#endif + //return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0); + if (FD_ISSET(_sock_fd, &fdSet)) { + return 0; + } else { + return -1; + } } int Socket::wait_readable(TimeInterval& timeout) {
--- a/Socket/Socket.h Thu Sep 19 08:04:22 2013 +0000 +++ b/Socket/Socket.h Sat Sep 21 15:01:05 2013 +0000 @@ -67,6 +67,8 @@ protected: int _sock_fd; + int init_socket(int type, int protocol); + int wait_readable(TimeInterval& timeout); int wait_writable(TimeInterval& timeout);
--- a/Socket/UDPSocket.cpp Thu Sep 19 08:04:22 2013 +0000 +++ b/Socket/UDPSocket.cpp Sat Sep 21 15:01:05 2013 +0000 @@ -26,36 +26,41 @@ } int UDPSocket::init(void) { - /* open upd socket */ - _sock_fd = _cc3000_module->_socket.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (_sock_fd == -1) { -#if (CC3000_DEBUG == 1) - printf("DEBUG: Failed to create new socket (udp).\n"); -#endif - return 0; - } - - return 1; + return init_socket(SOCK_DGRAM, IPPROTO_UDP); } // Server initialization int UDPSocket::bind(int port) { - init(); + if (init_socket(SOCK_DGRAM, IPPROTO_UDP) < 0) { + return -1; + } sockaddr_in localHost; - std::memset(&localHost, 0, sizeof(localHost)); + std::memset(&localHost, 0, sizeof(sockaddr_in)); localHost.sin_family = AF_INET; localHost.sin_port = htons(port); - localHost.sin_addr.s_addr = htons(0); + localHost.sin_addr.s_addr = 0; + // sockaddr localHost; + // localHost.family = AF_INET; + // localHost.data[0] = (port & 0xFF00)>> 8; + // localHost.data[1] = (port & 0x00FF); + // localHost.data[2] = 0; + // localHost.data[3] = 0; + // localHost.data[4] = 0; + // localHost.data[5] = 0; - if (_cc3000_module->_socket.bind(_sock_fd, (sockaddr *)&localHost, sizeof(localHost)) != 0) { + if (_cc3000_module->_socket.bind(_sock_fd, (sockaddr *)&localHost, sizeof(sockaddr_in)) != 0) { #if (CC3000_DEBUG == 1) printf("DEBUG: Failed to bind a socket (udp). Closing socket.\n"); #endif _cc3000_module->_socket.closesocket(_sock_fd); + _sock_fd = -1; return -1; } +// #if (CC3000_DEBUG == 1) +// printf("DEBUG: local add: %d\n",localHost.sin_addr.s_addr); +// #endif return 0; } @@ -68,11 +73,15 @@ if (!_blocking) { TimeInterval timeout(_timeout); - if (wait_writable(timeout) != 0) + if (wait_writable(timeout) != 0) { +#if (CC3000_DEBUG == 1) + printf("DEBUG: The socket is not writeable. _sock_fd: %d.\n", _sock_fd); +#endif return 0; + } } - return _cc3000_module->_socket.sendto(_sock_fd, packet, length, 0, (sockaddr *)&remote._remote_host, sizeof(remote._remote_host)); + return _cc3000_module->_socket.sendto(_sock_fd, packet, length, 0, (sockaddr *)&remote._remote_host, sizeof(sockaddr)); } // -1 if unsuccessful, else number of bytes received @@ -85,6 +94,9 @@ if (!_blocking) { TimeInterval timeout(_timeout); if (wait_readable(timeout) != 0) { +#if (CC3000_DEBUG == 1) + printf("DEBUG: The socket is not readable. _sock_fd: %d.\n", _sock_fd); +#endif return 0; } }
--- a/cc3000.cpp Thu Sep 19 08:04:22 2013 +0000 +++ b/cc3000.cpp Sat Sep 21 15:01:05 2013 +0000 @@ -386,6 +386,7 @@ _wlan.ioctl_set_connection_policy(0, 0, 0); _wlan.ioctl_del_profile(255); + get_user_file_info((uint8_t *)&user_info, sizeof(user_info)); user_info.FTC = 0; set_user_file_info((uint8_t *)&user_info, sizeof(user_info)); }
--- a/cc3000_socket.cpp Thu Sep 19 08:04:22 2013 +0000 +++ b/cc3000_socket.cpp Sat Sep 21 15:01:05 2013 +0000 @@ -101,7 +101,7 @@ } else { - uint16 free_buffer = _simple_link.get_number_free_buffers(); + uint16_t free_buffer = _simple_link.get_number_free_buffers(); free_buffer--; _simple_link.set_number_free_buffers(free_buffer); return 0; @@ -140,6 +140,7 @@ int32_t ret; uint8_t *ptr, *args; + while(_simple_link.get_number_free_buffers() != SOCKET_MAX_FREE_BUFFERS); ret = EFAIL; ptr = _simple_link.get_transmit_buffer(); args = (ptr + HEADERS_SIZE_CMD);
--- a/cc3000_socket.h Thu Sep 19 08:04:22 2013 +0000 +++ b/cc3000_socket.h Sat Sep 21 15:01:05 2013 +0000 @@ -41,6 +41,8 @@ #ifndef CC3000_SOCKET_H #define CC3000_SOCKET_H +#define SOCKET_MAX_FREE_BUFFERS 6 + #define SOCKET_STATUS_ACTIVE 0 #define SOCKET_STATUS_INACTIVE 1