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