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

Committer:
dan_ackme
Date:
Sat Aug 23 05:39:17 2014 -0700
Revision:
17:7268f365676b
Fixes and documentation updates

Who changed what in which revision?

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