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:
Mon Feb 23 20:30:18 2015 -0800
Revision:
39:a963f69cb2de
added httpAcceptWebsocket API

Who changed what in which revision?

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