Home Alert System

Dependencies:   PWM_Tone_Library DHT

Committer:
aziz111
Date:
Fri Mar 08 17:15:02 2019 +0000
Revision:
5:569a4894abc1
Parent:
3:78f223d34f36
Final

Who changed what in which revision?

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