cc3000 hostdriver with the mbed socket interface

Fork of cc3000_hostdriver_mbedsocket by Martin Kojtal

Committer:
Kojto
Date:
Wed Oct 02 20:29:31 2013 +0200
Revision:
17:14b6a3a2b622
Parent:
16:f3676ae62f96
Child:
18:7e22775eadb9
Hci print internal, Endpoint - unix ending lines

- hci internal print function
- endpoint correction with ending lines (was PC)

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 17:14b6a3a2b622 26 static char *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 17:14b6a3a2b622 94 //Resolve DNS address or populate hard-coded IP address
Kojto 17:14b6a3a2b622 95 uint32_t address_integer;
Kojto 17:14b6a3a2b622 96 _cc3000_module->_socket.gethostbyname((uint8_t *)host, strlen(host) , &address_integer);
Kojto 17:14b6a3a2b622 97
Kojto 17:14b6a3a2b622 98 uint32_t ip = 0;
Kojto 17:14b6a3a2b622 99 ip = (ip | (address_integer >> 24));
Kojto 17:14b6a3a2b622 100 ip = (ip | ((address_integer & 0x00FF0000) >> 8));
Kojto 17:14b6a3a2b622 101 ip = (ip | ((address_integer & 0x0000FF00) << 8));
Kojto 17:14b6a3a2b622 102 ip = (ip | ((address_integer & 0x000000FF) << 24));
Kojto 17:14b6a3a2b622 103 _remote_host.sin_addr.s_addr = ip;
Kojto 17:14b6a3a2b622 104 inet_ntoa_r(_remote_host.sin_addr, _ipAddress, sizeof(_ipAddress));
Kojto 17:14b6a3a2b622 105 } else {
Kojto 17:14b6a3a2b622 106 std::memcpy((char*)&_remote_host.sin_addr.s_addr, p_address, 4);
Kojto 17:14b6a3a2b622 107 }
Kojto 17:14b6a3a2b622 108
Kojto 17:14b6a3a2b622 109 _remote_host.sin_family = AF_INET;
Kojto 17:14b6a3a2b622 110 _remote_host.sin_port = htons(port);
Kojto 17:14b6a3a2b622 111
Kojto 17:14b6a3a2b622 112 DBG_SOCKET("remote host address (string): %s",get_address());
Kojto 17:14b6a3a2b622 113 DBG_SOCKET("remote host address from s_addr : %d.%d.%d.%d",
Kojto 17:14b6a3a2b622 114 int(_remote_host.sin_addr.s_addr & 0xFF),
Kojto 17:14b6a3a2b622 115 int((_remote_host.sin_addr.s_addr & 0xFF00) >> 8),
Kojto 17:14b6a3a2b622 116 int((_remote_host.sin_addr.s_addr & 0xFF0000) >> 16),
Kojto 17:14b6a3a2b622 117 int((_remote_host.sin_addr.s_addr & 0xFF000000) >> 24));
Kojto 17:14b6a3a2b622 118 DBG_SOCKET("port: %d", port);
Kojto 17:14b6a3a2b622 119
Kojto 17:14b6a3a2b622 120 return 0;
Kojto 17:14b6a3a2b622 121 }
Kojto 17:14b6a3a2b622 122
Kojto 17:14b6a3a2b622 123 char* Endpoint::get_address() {
Kojto 17:14b6a3a2b622 124 if ((_ipAddress[0] == '\0') && (_remote_host.sin_addr.s_addr != 0))
Kojto 17:14b6a3a2b622 125 inet_ntoa_r(_remote_host.sin_addr, _ipAddress, sizeof(_ipAddress));
Kojto 17:14b6a3a2b622 126 return _ipAddress;
Kojto 17:14b6a3a2b622 127 }
Kojto 17:14b6a3a2b622 128
Kojto 17:14b6a3a2b622 129
Kojto 17:14b6a3a2b622 130 int Endpoint::get_port() {
Kojto 17:14b6a3a2b622 131 return ntohs(_remote_host.sin_port);
Kojto 17:14b6a3a2b622 132 }