Kojto CC3000 Hostdriver With Mbed Socket interface
Dependents: WiFiDip-KitchenSink WiFiDip-UsbKitchenSink WiFiDipCortexSensor WifiDipCortex-UDPDemo
Fork of cc3000_hostdriver_mbedsocket by
Revision 15:1c2a856c618a, committed 2013-10-02
- Comitter:
- Martin Kojtal
- Date:
- Wed Oct 02 17:02:34 2013 +0200
- Parent:
- 11:5e3771b29385
- Child:
- 16:f3676ae62f96
- Commit message:
- Debug messages (\r\n), socket bsd naming
Changed in this revision
--- a/Helper/def.h Tue Oct 01 21:17:44 2013 +0000 +++ b/Helper/def.h Wed Oct 02 17:02:34 2013 +0200 @@ -1,28 +1,24 @@ -/* Copyright (C) 2013 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. - */ - -#ifndef DEF_H -#define DEF_H - -#include "cmsis.h" -//#define htons(x) __REV16(x) -//#define ntohs(x) __REV16(x) -//#define htonl(x) __REV(x) -//#define ntohl(x) __REV(x) - -#endif +/* Copyright (C) 2013 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. + */ + +#ifndef DEF_H +#define DEF_H + +#include "cmsis.h" + +#endif
--- a/Socket/Endpoint.cpp Tue Oct 01 21:17:44 2013 +0000 +++ b/Socket/Endpoint.cpp Wed Oct 02 17:02:34 2013 +0200 @@ -1,133 +1,133 @@ -/* Copyright (C) 2013 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; -} - -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(); - - 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]; - } - std::memset(_ipAddress,0,sizeof(_ipAddress)); - - 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); - - uint32_t ip = 0; - ip = (ip | (address_integer >> 24)); - ip = (ip | ((address_integer & 0x00FF0000) >> 8)); - ip = (ip | ((address_integer & 0x0000FF00) << 8)); - ip = (ip | ((address_integer & 0x000000FF) << 24)); - _remote_host.sin_addr.s_addr = ip; - inet_ntoa_r(_remote_host.sin_addr, _ipAddress, sizeof(_ipAddress)); - } else { - std::memcpy((char*)&_remote_host.sin_addr.s_addr, p_address, 4); - } - - _remote_host.sin_family = AF_INET; - _remote_host.sin_port = htons(port); - -#if (CC3000_DEBUG == 1) - printf("DEBUG: remote host address (string): %s\r\n",get_address()); - printf("DEBUG: remote host address from s_addr : %d.%d.%d.%d\r\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)); - printf("DEBUG: port: %d \r\n", port); -#endif - 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); -} +/* Copyright (C) 2013 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; +} + +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(); + + 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]; + } + std::memset(_ipAddress,0,sizeof(_ipAddress)); + + if (result != 4) { + //Resolve DNS address or populate hard-coded IP address + uint32_t address_integer; + _cc3000_module->_socket.gethostbyname((uint8_t *)host, strlen(host) , &address_integer); + + uint32_t ip = 0; + ip = (ip | (address_integer >> 24)); + ip = (ip | ((address_integer & 0x00FF0000) >> 8)); + ip = (ip | ((address_integer & 0x0000FF00) << 8)); + ip = (ip | ((address_integer & 0x000000FF) << 24)); + _remote_host.sin_addr.s_addr = ip; + inet_ntoa_r(_remote_host.sin_addr, _ipAddress, sizeof(_ipAddress)); + } else { + std::memcpy((char*)&_remote_host.sin_addr.s_addr, p_address, 4); + } + + _remote_host.sin_family = AF_INET; + _remote_host.sin_port = htons(port); + +#if (CC3000_DEBUG == 1) + printf("DEBUG: remote host address (string): %s \r\n",get_address()); + printf("DEBUG: remote host address from s_addr : %d.%d.%d.%d \r\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)); + printf("DEBUG: port: 0x%x \r\n", _remote_host.sin_port); +#endif + 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 Tue Oct 01 21:17:44 2013 +0000 +++ b/Socket/Socket.cpp Wed Oct 02 17:02:34 2013 +0200 @@ -55,11 +55,11 @@ } int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) { - return _cc3000_module->_socket.set_sockopt(_sock_fd, level, optname, optval, optlen); + return _cc3000_module->_socket.setsockopt(_sock_fd, level, optname, optval, optlen); } int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) { - return _cc3000_module->_socket.get_sockopt(_sock_fd, level, optname, optval, optlen); + return _cc3000_module->_socket.getsockopt(_sock_fd, level, optname, optval, optlen); } int Socket::select(struct timeval *timeout, bool read, bool write) { @@ -76,7 +76,7 @@ int ret = _cc3000_module->_socket.select(_sock_fd+1, readset, writeset, NULL, timeout); #if (CC3000_DEBUG == 1) - printf("DEBUG: Select on sock_fd: %d, returns %d. fdSet: %d\r\n",_sock_fd, ret, fdSet); + printf("DEBUG: Select on sock_fd: %d, returns %d. fdSet: %d \r\n",_sock_fd, ret, fdSet); #endif // TODO //return (ret <= 0 || !FD_ISSET(_sock_fd, &fdSet)) ? (-1) : (0);
--- a/Socket/UDPSocket.cpp Tue Oct 01 21:17:44 2013 +0000 +++ b/Socket/UDPSocket.cpp Wed Oct 02 17:02:34 2013 +0200 @@ -59,15 +59,15 @@ return -1; } -// if (!_blocking) { -// TimeInterval timeout(_timeout); -// 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; -// } -// } + if (!_blocking) { + TimeInterval timeout(_timeout); + if (wait_writable(timeout) != 0) { +#if (CC3000_DEBUG == 1) + printf("DEBUG: The socket is not writeable. _sock_fd: %d.\r\n", _sock_fd); +#endif + return 0; + } + } return _cc3000_module->_socket.sendto(_sock_fd, packet, length, 0, (sockaddr *)&remote._remote_host, sizeof(sockaddr)); }
--- a/cc3000.cpp Tue Oct 01 21:17:44 2013 +0000 +++ b/cc3000.cpp Wed Oct 02 17:02:34 2013 +0200 @@ -134,7 +134,7 @@ _wlan.smart_config_start(0); #if (CC3000_DEBUG == 1) - printf("DEBUG: Waiting for smartconfig to be completed.\n"); + printf("DEBUG: Waiting for smartconfig to be completed. \r\n"); #endif // Wait for Smart config finished while (_status.smart_config_complete == 0) @@ -143,7 +143,7 @@ } #if (CC3000_DEBUG == 1) - printf("DEBUG: Smartconfig finished.\n"); + printf("DEBUG: Smartconfig finished.\r\n"); #endif #ifndef CC3000_UNENCRYPTED_SMART_CONFIG // create new entry for AES encryption key @@ -206,7 +206,7 @@ if (t.read_ms() > 10000){ ret = false; #if (CC3000_DEBUG == 1) - printf("Connection to AP failed.\n"); + printf("Connection to AP failed.\r\n"); #endif break; } @@ -290,7 +290,7 @@ tcp_socket = _socket.socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (tcp_socket == -1) { #if (CC3000_DEBUG == 1) - printf("DEBUG: Failed to create new socket (tcp).\n"); + printf("DEBUG: Failed to create new socket (tcp).\r\n"); #endif return cc3000_client(*this); } @@ -305,7 +305,7 @@ if (_socket.connect(tcp_socket, &socket_address, sizeof(socket_address)) == -1) { #if (CC3000_DEBUG == 1) - printf("DEBUG: Failed to connect (tcp).\n"); + printf("DEBUG: Failed to connect (tcp).\r\n"); #endif _socket.closesocket(tcp_socket); return cc3000_client(*this); @@ -320,7 +320,7 @@ udp_socket = _socket.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (udp_socket == -1) { #if (CC3000_DEBUG == 1) - printf("DEBUG: Failed to create new socket (udp).\n"); + printf("DEBUG: Failed to create new socket (udp).\r\n"); #endif return cc3000_client(*this); } @@ -335,7 +335,7 @@ if (_socket.connect(udp_socket, &socket_address, sizeof(socket_address)) == -1) { #if (CC3000_DEBUG == 1) - printf("DEBUG: Failed to connect (udp).\n"); + printf("DEBUG: Failed to connect (udp).\r\n"); #endif _socket.closesocket(udp_socket); return cc3000_client(*this); @@ -351,7 +351,7 @@ tcp_socket = _socket.socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (tcp_socket == -1) { #if (CC3000_DEBUG == 1) - printf("Failed to create new socket.\n"); + printf("Failed to create new socket.\r\n"); #endif return cc3000_server(*this, socket_address); } @@ -366,13 +366,13 @@ if (_socket.bind(tcp_socket, &socket_address, sizeof(socket_address)) != 0) { #if (CC3000_DEBUG == 1) - printf("DEBUG: Failed to bind the new socket.\n"); + printf("DEBUG: Failed to bind the new socket.\r\n"); #endif return cc3000_server(*this, socket_address); } if (_socket.listen(tcp_socket, 1) != 0) { /* 1 client */ #if (CC3000_DEBUG == 1) - printf("DEBUG: Failed to listen on the new socket.\n"); + printf("DEBUG: Failed to listen on the new socket.\r\n"); #endif return cc3000_server(*this, socket_address); } @@ -409,7 +409,7 @@ _ping_report.packets_received = 0; if (_netapp.ping_send(&reversed_ip, attempts, size, timeout) == -1) { #if (CC3000_DEBUG == 1) - printf("DEBUG: Failed to send ping.\n"); + printf("DEBUG: Failed to send ping.\r\n"); #endif return 0; }
--- a/cc3000.h Tue Oct 01 21:17:44 2013 +0000 +++ b/cc3000.h Wed Oct 02 17:02:34 2013 +0200 @@ -260,27 +260,26 @@ public: cc3000_socket(cc3000_simple_link &simplelink, cc3000_hci &hci, cc3000_event &event); ~cc3000_socket(); - int32_t HostFlowControlConsumeBuff(int32_t sd); int32_t socket(int32_t domain, int32_t type, int32_t protocol); - int32_t closesocket(int32_t sd); int32_t accept(int32_t sd, sockaddr *addr, socklen_t *addrlen); int32_t bind(int32_t sd, const sockaddr *addr, int32_t addrlen); + int32_t HostFlowControlConsumeBuff(int32_t sd); + int32_t closesocket(int32_t sd); int32_t listen(int32_t sd, int32_t backlog); -#ifndef CC3000_TINY_DRIVER - int32_t gethostbyname(uint8_t * hostname, uint16_t name_length, uint32_t* out_ip_addr); - int32_t get_host_by_name(uint8_t * hostname, uint16_t name_length, uint32_t* out_ip_addr); - int32_t set_sockopt(int32_t sd, int32_t level, int32_t optname, const void *optval, socklen_t optlen); -#endif int32_t connect(int32_t sd, const sockaddr *addr, int32_t addrlen); int32_t select(int32_t nfds, fd_set *readsds, fd_set *writesds, fd_set *exceptsds, struct timeval *timeout); - int32_t get_sockopt (int32_t sd, int32_t level, int32_t optname, void *optval, socklen_t *optlen); + int32_t getsockopt (int32_t sd, int32_t level, int32_t optname, void *optval, socklen_t *optlen); int32_t simple_link_recv(int32_t sd, void *buf, int32_t len, int32_t flags, sockaddr *from, socklen_t *fromlen, int32_t opcode); + int32_t simple_link_send(int32_t sd, const void *buf, int32_t len, int32_t flags, const sockaddr *to, int32_t tolen, int32_t opcode); int32_t recv(int32_t sd, void *buf, int32_t len, int32_t flags); int32_t recvfrom(int32_t sd, void *buf, int32_t len, int32_t flags, sockaddr *from, socklen_t *fromlen); - int32_t simple_link_send(int32_t sd, const void *buf, int32_t len, int32_t flags, const sockaddr *to, int32_t tolen, int32_t opcode); int32_t send(int32_t sd, const void *buf, int32_t len, int32_t flags); int32_t sendto(int32_t sd, const void *buf, int32_t len, int32_t flags, const sockaddr *to, socklen_t tolen); int32_t mdns_advertiser(uint16_t mdns_enabled, uint8_t * device_service_name, uint16_t device_service_name_length); +#ifndef CC3000_TINY_DRIVER + int32_t gethostbyname(uint8_t *hostname, uint16_t name_length, uint32_t *out_ip_addr); + int32_t setsockopt(int32_t sd, int32_t level, int32_t optname, const void *optval, socklen_t optlen); +#endif private: cc3000_simple_link &_simple_link; cc3000_hci &_hci;
--- a/cc3000_socket.cpp Tue Oct 01 21:17:44 2013 +0000 +++ b/cc3000_socket.cpp Wed Oct 02 17:02:34 2013 +0200 @@ -347,7 +347,7 @@ } } -int32_t cc3000_socket::get_sockopt (int32_t sd, int32_t level, int32_t optname, void *optval, socklen_t *optlen) { +int32_t cc3000_socket::getsockopt (int32_t sd, int32_t level, int32_t optname, void *optval, socklen_t *optlen) { uint8_t *ptr, *args; tBsdGetSockOptReturnParams tRetParams; @@ -534,12 +534,7 @@ #ifndef CC3000_TINY_DRIVER - int32_t cc3000_socket::gethostbyname(uint8_t *hostname, uint16_t name_length, uint32_t *out_ip_addr) { - get_host_by_name(hostname, name_length, out_ip_addr); -} - -int32_t cc3000_socket::get_host_by_name(uint8_t *hostname, uint16_t name_length, uint32_t *out_ip_addr) { tBsdGethostbynameParams ret; uint8_t *ptr, *args; @@ -571,7 +566,7 @@ return (errno); } -int32_t cc3000_socket::set_sockopt(int32_t sd, int32_t level, int32_t optname, const void *optval, socklen_t optlen) { +int32_t cc3000_socket::setsockopt(int32_t sd, int32_t level, int32_t optname, const void *optval, socklen_t optlen) { int32_t ret; uint8_t *ptr, *args;