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