Host library for controlling a WiConnect enabled Wi-Fi module.
Dependents: wiconnect-ota_example wiconnect-web_setup_example wiconnect-test-console wiconnect-tcp_server_example ... more
Endpoint.cpp@29:b6af04b77a56, 2014-10-27 (annotated)
- Committer:
- dan_ackme
- Date:
- Mon Oct 27 13:42:26 2014 -0700
- Revision:
- 29:b6af04b77a56
refactored library layout
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dan_ackme | 29:b6af04b77a56 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
dan_ackme | 29:b6af04b77a56 | 2 | * |
dan_ackme | 29:b6af04b77a56 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
dan_ackme | 29:b6af04b77a56 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
dan_ackme | 29:b6af04b77a56 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
dan_ackme | 29:b6af04b77a56 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
dan_ackme | 29:b6af04b77a56 | 7 | * furnished to do so, subject to the following conditions: |
dan_ackme | 29:b6af04b77a56 | 8 | * |
dan_ackme | 29:b6af04b77a56 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
dan_ackme | 29:b6af04b77a56 | 10 | * substantial portions of the Software. |
dan_ackme | 29:b6af04b77a56 | 11 | * |
dan_ackme | 29:b6af04b77a56 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
dan_ackme | 29:b6af04b77a56 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
dan_ackme | 29:b6af04b77a56 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
dan_ackme | 29:b6af04b77a56 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
dan_ackme | 29:b6af04b77a56 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
dan_ackme | 29:b6af04b77a56 | 17 | */ |
dan_ackme | 29:b6af04b77a56 | 18 | #include "api/types/Socket/Socket.h" |
dan_ackme | 29:b6af04b77a56 | 19 | #include "api/types/Socket/Endpoint.h" |
dan_ackme | 29:b6af04b77a56 | 20 | #include <cstring> |
dan_ackme | 29:b6af04b77a56 | 21 | #include <cstdio> |
dan_ackme | 29:b6af04b77a56 | 22 | |
dan_ackme | 29:b6af04b77a56 | 23 | |
dan_ackme | 29:b6af04b77a56 | 24 | |
dan_ackme | 29:b6af04b77a56 | 25 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 26 | Endpoint::Endpoint() |
dan_ackme | 29:b6af04b77a56 | 27 | { |
dan_ackme | 29:b6af04b77a56 | 28 | reset_address(); |
dan_ackme | 29:b6af04b77a56 | 29 | } |
dan_ackme | 29:b6af04b77a56 | 30 | |
dan_ackme | 29:b6af04b77a56 | 31 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 32 | Endpoint::~Endpoint() {} |
dan_ackme | 29:b6af04b77a56 | 33 | |
dan_ackme | 29:b6af04b77a56 | 34 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 35 | void Endpoint::reset_address(void) |
dan_ackme | 29:b6af04b77a56 | 36 | { |
dan_ackme | 29:b6af04b77a56 | 37 | std::memset(&_remoteHost, 0, sizeof(struct sockaddr_in)); |
dan_ackme | 29:b6af04b77a56 | 38 | _ipAddress[0] = '\0'; |
dan_ackme | 29:b6af04b77a56 | 39 | } |
dan_ackme | 29:b6af04b77a56 | 40 | |
dan_ackme | 29:b6af04b77a56 | 41 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 42 | int Endpoint::set_address(const char* host, const int port) |
dan_ackme | 29:b6af04b77a56 | 43 | { |
dan_ackme | 29:b6af04b77a56 | 44 | reset_address(); |
dan_ackme | 29:b6af04b77a56 | 45 | |
dan_ackme | 29:b6af04b77a56 | 46 | // IP Address |
dan_ackme | 29:b6af04b77a56 | 47 | char address[5]; |
dan_ackme | 29:b6af04b77a56 | 48 | char *p_address = address; |
dan_ackme | 29:b6af04b77a56 | 49 | |
dan_ackme | 29:b6af04b77a56 | 50 | // Dot-decimal notation |
dan_ackme | 29:b6af04b77a56 | 51 | int result = std::sscanf(host, "%3u.%3u.%3u.%3u", |
dan_ackme | 29:b6af04b77a56 | 52 | (unsigned int*)&address[0], (unsigned int*)&address[1], |
dan_ackme | 29:b6af04b77a56 | 53 | (unsigned int*)&address[2], (unsigned int*)&address[3]); |
dan_ackme | 29:b6af04b77a56 | 54 | |
dan_ackme | 29:b6af04b77a56 | 55 | if (result != 4) |
dan_ackme | 29:b6af04b77a56 | 56 | { |
dan_ackme | 29:b6af04b77a56 | 57 | // Resolve address with DNS |
dan_ackme | 29:b6af04b77a56 | 58 | struct hostent *host_address = gethostbyname(host); |
dan_ackme | 29:b6af04b77a56 | 59 | if (host_address == NULL) |
dan_ackme | 29:b6af04b77a56 | 60 | return -1; //Could not resolve address |
dan_ackme | 29:b6af04b77a56 | 61 | p_address = (char*)host_address->h_addr_list[0]; |
dan_ackme | 29:b6af04b77a56 | 62 | } |
dan_ackme | 29:b6af04b77a56 | 63 | std::memcpy((char*)&_remoteHost.sin_addr.s_addr, p_address, 4); |
dan_ackme | 29:b6af04b77a56 | 64 | |
dan_ackme | 29:b6af04b77a56 | 65 | // Address family |
dan_ackme | 29:b6af04b77a56 | 66 | _remoteHost.sin_family = AF_INET; |
dan_ackme | 29:b6af04b77a56 | 67 | |
dan_ackme | 29:b6af04b77a56 | 68 | // Set port |
dan_ackme | 29:b6af04b77a56 | 69 | _remoteHost.sin_port = htons(port); |
dan_ackme | 29:b6af04b77a56 | 70 | |
dan_ackme | 29:b6af04b77a56 | 71 | return 0; |
dan_ackme | 29:b6af04b77a56 | 72 | } |
dan_ackme | 29:b6af04b77a56 | 73 | |
dan_ackme | 29:b6af04b77a56 | 74 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 75 | char* Endpoint::get_address() |
dan_ackme | 29:b6af04b77a56 | 76 | { |
dan_ackme | 29:b6af04b77a56 | 77 | if ((_ipAddress[0] == '\0') && (_remoteHost.sin_addr.s_addr != 0)) |
dan_ackme | 29:b6af04b77a56 | 78 | inet_ntoa_r(_remoteHost.sin_addr, _ipAddress, sizeof(_ipAddress)); |
dan_ackme | 29:b6af04b77a56 | 79 | return _ipAddress; |
dan_ackme | 29:b6af04b77a56 | 80 | } |
dan_ackme | 29:b6af04b77a56 | 81 | |
dan_ackme | 29:b6af04b77a56 | 82 | /*************************************************************************************************/ |
dan_ackme | 29:b6af04b77a56 | 83 | int Endpoint::get_port() |
dan_ackme | 29:b6af04b77a56 | 84 | { |
dan_ackme | 29:b6af04b77a56 | 85 | return ntohs(_remoteHost.sin_port); |
dan_ackme | 29:b6af04b77a56 | 86 | } |