Counter

Dependencies:   EthernetInterface NTPClient SDFileSystem TextLCD WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip FATFileSystem

Committer:
Tuxitheone
Date:
Mon Feb 29 18:59:15 2016 +0000
Revision:
0:ecaf3e593122
TankCounter

Who changed what in which revision?

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