cc3000 hostdriver with the mbed socket interface

Dependents:   cc3000_hello_world_demo cc3000_simple_socket_demo cc3000_ntp_demo cc3000_ping_demo ... more

Committer:
frankvnk
Date:
Sat Feb 28 16:40:39 2015 +0000
Revision:
48:192e2fde71e9
Parent:
45:50ab13d8f2dc
Have TCPSocketServer::accept return the real accept values as defined in http://processors.wiki.ti.com/index.php/Non_blocking_accept; Fixed cc3000::init(const char *ip, const char *mask, const char *gateway) - missing _wlan.start(0);

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 17:14b6a3a2b622 1 /* Copyright (C) 2013 mbed.org, MIT License
Kojto 17:14b6a3a2b622 2 *
Kojto 17:14b6a3a2b622 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Kojto 17:14b6a3a2b622 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Kojto 17:14b6a3a2b622 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Kojto 17:14b6a3a2b622 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Kojto 17:14b6a3a2b622 7 * furnished to do so, subject to the following conditions:
Kojto 17:14b6a3a2b622 8 *
Kojto 17:14b6a3a2b622 9 * The above copyright notice and this permission notice shall be included in all copies or
Kojto 17:14b6a3a2b622 10 * substantial portions of the Software.
Kojto 17:14b6a3a2b622 11 *
Kojto 17:14b6a3a2b622 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Kojto 17:14b6a3a2b622 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Kojto 17:14b6a3a2b622 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Kojto 17:14b6a3a2b622 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Kojto 17:14b6a3a2b622 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Kojto 17:14b6a3a2b622 17 */
Kojto 17:14b6a3a2b622 18 #include "Socket/Socket.h"
Kojto 17:14b6a3a2b622 19 #include "Socket/Endpoint.h"
Kojto 17:14b6a3a2b622 20 #include "Helper/def.h"
Kojto 17:14b6a3a2b622 21 #include <cstring>
Kojto 17:14b6a3a2b622 22
Kojto 17:14b6a3a2b622 23 #include "cc3000.h"
Kojto 17:14b6a3a2b622 24
Kojto 17:14b6a3a2b622 25 /* Copied from lwip */
Kojto 45:50ab13d8f2dc 26 static char *cc3000_inet_ntoa_r(const in_addr addr, char *buf, int buflen)
Kojto 17:14b6a3a2b622 27 {
Kojto 17:14b6a3a2b622 28 uint32_t s_addr;
Kojto 17:14b6a3a2b622 29 char inv[3];
Kojto 17:14b6a3a2b622 30 char *rp;
Kojto 17:14b6a3a2b622 31 uint8_t *ap;
Kojto 17:14b6a3a2b622 32 uint8_t rem;
Kojto 17:14b6a3a2b622 33 uint8_t n;
Kojto 17:14b6a3a2b622 34 uint8_t i;
Kojto 17:14b6a3a2b622 35 int len = 0;
Kojto 17:14b6a3a2b622 36
Kojto 17:14b6a3a2b622 37 s_addr = addr.s_addr;
Kojto 17:14b6a3a2b622 38
Kojto 17:14b6a3a2b622 39 rp = buf;
Kojto 17:14b6a3a2b622 40 ap = (uint8_t *)&s_addr;
Kojto 17:14b6a3a2b622 41 for(n = 0; n < 4; n++) {
Kojto 17:14b6a3a2b622 42 i = 0;
Kojto 17:14b6a3a2b622 43 do {
Kojto 17:14b6a3a2b622 44 rem = *ap % (uint8_t)10;
Kojto 17:14b6a3a2b622 45 *ap /= (uint8_t)10;
Kojto 17:14b6a3a2b622 46 inv[i++] = '0' + rem;
Kojto 17:14b6a3a2b622 47 } while(*ap);
Kojto 17:14b6a3a2b622 48 while(i--) {
Kojto 17:14b6a3a2b622 49 if (len++ >= buflen) {
Kojto 17:14b6a3a2b622 50 return NULL;
Kojto 17:14b6a3a2b622 51 }
Kojto 17:14b6a3a2b622 52 *rp++ = inv[i];
Kojto 17:14b6a3a2b622 53 }
Kojto 17:14b6a3a2b622 54 if (len++ >= buflen) {
Kojto 17:14b6a3a2b622 55 return NULL;
Kojto 17:14b6a3a2b622 56 }
Kojto 17:14b6a3a2b622 57 *rp++ = '.';
Kojto 17:14b6a3a2b622 58 ap++;
Kojto 17:14b6a3a2b622 59 }
Kojto 17:14b6a3a2b622 60 *--rp = 0;
Kojto 17:14b6a3a2b622 61 return buf;
Kojto 17:14b6a3a2b622 62 }
Kojto 17:14b6a3a2b622 63
Kojto 17:14b6a3a2b622 64 Endpoint::Endpoint() {
Kojto 17:14b6a3a2b622 65 _cc3000_module = cc3000::get_instance();
Kojto 17:14b6a3a2b622 66 if (_cc3000_module == NULL) {
Kojto 17:14b6a3a2b622 67 error("Endpoint constructor error: no cc3000 instance available!\r\n");
Kojto 17:14b6a3a2b622 68 }
Kojto 17:14b6a3a2b622 69 reset_address();
Kojto 17:14b6a3a2b622 70 }
Kojto 17:14b6a3a2b622 71 Endpoint::~Endpoint() {}
Kojto 17:14b6a3a2b622 72
Kojto 17:14b6a3a2b622 73 void Endpoint::reset_address(void) {
Kojto 17:14b6a3a2b622 74 _ipAddress[0] = '\0';
Kojto 17:14b6a3a2b622 75 std::memset(&_remote_host, 0, sizeof(sockaddr_in));
Kojto 17:14b6a3a2b622 76 }
Kojto 17:14b6a3a2b622 77
Kojto 17:14b6a3a2b622 78 int Endpoint::set_address(const char* host, const int port) {
Kojto 17:14b6a3a2b622 79 reset_address();
Kojto 17:14b6a3a2b622 80
Kojto 17:14b6a3a2b622 81 char address[5];
Kojto 17:14b6a3a2b622 82 char *p_address = address;
Kojto 17:14b6a3a2b622 83
Kojto 17:14b6a3a2b622 84 signed int add[5];
Kojto 17:14b6a3a2b622 85
Kojto 17:14b6a3a2b622 86 // Dot-decimal notation
Kojto 17:14b6a3a2b622 87 int result = std::sscanf(host, "%3u.%3u.%3u.%3u", &add[0], &add[1], &add[2], &add[3]);
Kojto 17:14b6a3a2b622 88 for (int i=0;i<4;i++) {
Kojto 17:14b6a3a2b622 89 address[i] = add[i];
Kojto 17:14b6a3a2b622 90 }
Kojto 17:14b6a3a2b622 91 std::memset(_ipAddress,0,sizeof(_ipAddress));
Kojto 17:14b6a3a2b622 92
Kojto 17:14b6a3a2b622 93 if (result != 4) {
Kojto 45:50ab13d8f2dc 94 #ifndef CC3000_TINY_DRIVER
Kojto 17:14b6a3a2b622 95 //Resolve DNS address or populate hard-coded IP address
Kojto 17:14b6a3a2b622 96 uint32_t address_integer;
Kojto 45:50ab13d8f2dc 97 int resolveRetCode;
SolderSplashLabs 18:7e22775eadb9 98 resolveRetCode = _cc3000_module->_socket.gethostbyname((uint8_t *)host, strlen(host) , &address_integer);
Kojto 34:1ad18123bf11 99
Kojto 34:1ad18123bf11 100 if ((resolveRetCode > -1) && (0 != address_integer)) {
Kojto 34:1ad18123bf11 101 _remote_host.sin_addr.s_addr = htonl(address_integer);
Kojto 45:50ab13d8f2dc 102 cc3000_inet_ntoa_r(_remote_host.sin_addr, _ipAddress, sizeof(_ipAddress));
Kojto 34:1ad18123bf11 103 } else {
SolderSplashLabs 18:7e22775eadb9 104 // Failed to resolve the address
SolderSplashLabs 18:7e22775eadb9 105 DBG_SOCKET("Failed to resolve the hostname : %s",host);
Kojto 34:1ad18123bf11 106 return (-1);
SolderSplashLabs 18:7e22775eadb9 107 }
Kojto 45:50ab13d8f2dc 108 #else
Kojto 45:50ab13d8f2dc 109 return -1;
Kojto 45:50ab13d8f2dc 110 #endif
Kojto 17:14b6a3a2b622 111 } else {
Kojto 17:14b6a3a2b622 112 std::memcpy((char*)&_remote_host.sin_addr.s_addr, p_address, 4);
Kojto 17:14b6a3a2b622 113 }
Kojto 17:14b6a3a2b622 114
Kojto 17:14b6a3a2b622 115 _remote_host.sin_family = AF_INET;
Kojto 17:14b6a3a2b622 116 _remote_host.sin_port = htons(port);
Kojto 17:14b6a3a2b622 117
Kojto 17:14b6a3a2b622 118 DBG_SOCKET("remote host address (string): %s",get_address());
Kojto 17:14b6a3a2b622 119 DBG_SOCKET("remote host address from s_addr : %d.%d.%d.%d",
Kojto 17:14b6a3a2b622 120 int(_remote_host.sin_addr.s_addr & 0xFF),
Kojto 17:14b6a3a2b622 121 int((_remote_host.sin_addr.s_addr & 0xFF00) >> 8),
Kojto 17:14b6a3a2b622 122 int((_remote_host.sin_addr.s_addr & 0xFF0000) >> 16),
Kojto 17:14b6a3a2b622 123 int((_remote_host.sin_addr.s_addr & 0xFF000000) >> 24));
Kojto 17:14b6a3a2b622 124 DBG_SOCKET("port: %d", port);
Kojto 34:1ad18123bf11 125
Kojto 17:14b6a3a2b622 126 return 0;
Kojto 17:14b6a3a2b622 127 }
Kojto 17:14b6a3a2b622 128
Kojto 17:14b6a3a2b622 129 char* Endpoint::get_address() {
Kojto 17:14b6a3a2b622 130 if ((_ipAddress[0] == '\0') && (_remote_host.sin_addr.s_addr != 0))
Kojto 45:50ab13d8f2dc 131 cc3000_inet_ntoa_r(_remote_host.sin_addr, _ipAddress, sizeof(_ipAddress));
Kojto 17:14b6a3a2b622 132 return _ipAddress;
Kojto 17:14b6a3a2b622 133 }
Kojto 17:14b6a3a2b622 134
Kojto 17:14b6a3a2b622 135
Kojto 17:14b6a3a2b622 136 int Endpoint::get_port() {
Kojto 17:14b6a3a2b622 137 return ntohs(_remote_host.sin_port);
Kojto 17:14b6a3a2b622 138 }