Added mutex for multiple SPI devices on the same SPI bus
Fork of cc3000_hostdriver_mbedsocket by
Socket/Endpoint.cpp@34:1ad18123bf11, 2013-10-08 (annotated)
- Committer:
- Kojto
- Date:
- Tue Oct 08 13:13:05 2013 +0200
- Revision:
- 34:1ad18123bf11
- Parent:
- 18:7e22775eadb9
- Child:
- 45:50ab13d8f2dc
doxygen comments added by Frank V., htonl in Endpoint
Who changed what in which revision?
User | Revision | Line number | New 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 | |
SolderSplashLabs | 18:7e22775eadb9 | 81 | int resolveRetCode; |
Kojto | 17:14b6a3a2b622 | 82 | char address[5]; |
Kojto | 17:14b6a3a2b622 | 83 | char *p_address = address; |
Kojto | 17:14b6a3a2b622 | 84 | |
Kojto | 17:14b6a3a2b622 | 85 | signed int add[5]; |
Kojto | 17:14b6a3a2b622 | 86 | |
Kojto | 17:14b6a3a2b622 | 87 | // Dot-decimal notation |
Kojto | 17:14b6a3a2b622 | 88 | int result = std::sscanf(host, "%3u.%3u.%3u.%3u", &add[0], &add[1], &add[2], &add[3]); |
Kojto | 17:14b6a3a2b622 | 89 | for (int i=0;i<4;i++) { |
Kojto | 17:14b6a3a2b622 | 90 | address[i] = add[i]; |
Kojto | 17:14b6a3a2b622 | 91 | } |
Kojto | 17:14b6a3a2b622 | 92 | std::memset(_ipAddress,0,sizeof(_ipAddress)); |
Kojto | 17:14b6a3a2b622 | 93 | |
Kojto | 17:14b6a3a2b622 | 94 | if (result != 4) { |
Kojto | 17:14b6a3a2b622 | 95 | //Resolve DNS address or populate hard-coded IP address |
Kojto | 17:14b6a3a2b622 | 96 | uint32_t address_integer; |
SolderSplashLabs | 18:7e22775eadb9 | 97 | resolveRetCode = _cc3000_module->_socket.gethostbyname((uint8_t *)host, strlen(host) , &address_integer); |
Kojto | 34:1ad18123bf11 | 98 | |
Kojto | 34:1ad18123bf11 | 99 | if ((resolveRetCode > -1) && (0 != address_integer)) { |
Kojto | 34:1ad18123bf11 | 100 | _remote_host.sin_addr.s_addr = htonl(address_integer); |
SolderSplashLabs | 18:7e22775eadb9 | 101 | inet_ntoa_r(_remote_host.sin_addr, _ipAddress, sizeof(_ipAddress)); |
Kojto | 34:1ad18123bf11 | 102 | } else { |
SolderSplashLabs | 18:7e22775eadb9 | 103 | // Failed to resolve the address |
SolderSplashLabs | 18:7e22775eadb9 | 104 | DBG_SOCKET("Failed to resolve the hostname : %s",host); |
Kojto | 34:1ad18123bf11 | 105 | return (-1); |
SolderSplashLabs | 18:7e22775eadb9 | 106 | } |
Kojto | 17:14b6a3a2b622 | 107 | } else { |
Kojto | 17:14b6a3a2b622 | 108 | std::memcpy((char*)&_remote_host.sin_addr.s_addr, p_address, 4); |
Kojto | 17:14b6a3a2b622 | 109 | } |
Kojto | 17:14b6a3a2b622 | 110 | |
Kojto | 17:14b6a3a2b622 | 111 | _remote_host.sin_family = AF_INET; |
Kojto | 17:14b6a3a2b622 | 112 | _remote_host.sin_port = htons(port); |
Kojto | 17:14b6a3a2b622 | 113 | |
Kojto | 17:14b6a3a2b622 | 114 | DBG_SOCKET("remote host address (string): %s",get_address()); |
Kojto | 17:14b6a3a2b622 | 115 | DBG_SOCKET("remote host address from s_addr : %d.%d.%d.%d", |
Kojto | 17:14b6a3a2b622 | 116 | int(_remote_host.sin_addr.s_addr & 0xFF), |
Kojto | 17:14b6a3a2b622 | 117 | int((_remote_host.sin_addr.s_addr & 0xFF00) >> 8), |
Kojto | 17:14b6a3a2b622 | 118 | int((_remote_host.sin_addr.s_addr & 0xFF0000) >> 16), |
Kojto | 17:14b6a3a2b622 | 119 | int((_remote_host.sin_addr.s_addr & 0xFF000000) >> 24)); |
Kojto | 17:14b6a3a2b622 | 120 | DBG_SOCKET("port: %d", port); |
Kojto | 34:1ad18123bf11 | 121 | |
Kojto | 17:14b6a3a2b622 | 122 | return 0; |
Kojto | 17:14b6a3a2b622 | 123 | } |
Kojto | 17:14b6a3a2b622 | 124 | |
Kojto | 17:14b6a3a2b622 | 125 | char* Endpoint::get_address() { |
Kojto | 17:14b6a3a2b622 | 126 | if ((_ipAddress[0] == '\0') && (_remote_host.sin_addr.s_addr != 0)) |
Kojto | 17:14b6a3a2b622 | 127 | inet_ntoa_r(_remote_host.sin_addr, _ipAddress, sizeof(_ipAddress)); |
Kojto | 17:14b6a3a2b622 | 128 | return _ipAddress; |
Kojto | 17:14b6a3a2b622 | 129 | } |
Kojto | 17:14b6a3a2b622 | 130 | |
Kojto | 17:14b6a3a2b622 | 131 | |
Kojto | 17:14b6a3a2b622 | 132 | int Endpoint::get_port() { |
Kojto | 17:14b6a3a2b622 | 133 | return ntohs(_remote_host.sin_port); |
Kojto | 17:14b6a3a2b622 | 134 | } |