Dependents:   W5500-SNTPClient-example

Committer:
star297
Date:
Thu May 02 20:47:25 2019 +0000
Revision:
0:e9275bdfa393
First commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:e9275bdfa393 1 /* Copyright (C) 2012 mbed.org, MIT License
star297 0:e9275bdfa393 2 *
star297 0:e9275bdfa393 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
star297 0:e9275bdfa393 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
star297 0:e9275bdfa393 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
star297 0:e9275bdfa393 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
star297 0:e9275bdfa393 7 * furnished to do so, subject to the following conditions:
star297 0:e9275bdfa393 8 *
star297 0:e9275bdfa393 9 * The above copyright notice and this permission notice shall be included in all copies or
star297 0:e9275bdfa393 10 * substantial portions of the Software.
star297 0:e9275bdfa393 11 *
star297 0:e9275bdfa393 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
star297 0:e9275bdfa393 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
star297 0:e9275bdfa393 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
star297 0:e9275bdfa393 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
star297 0:e9275bdfa393 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
star297 0:e9275bdfa393 17 */
star297 0:e9275bdfa393 18 #include "Socket/Socket.h"
star297 0:e9275bdfa393 19 #include "Socket/Endpoint.h"
star297 0:e9275bdfa393 20 //#include <cstring>
star297 0:e9275bdfa393 21 //#include <cstdio>
star297 0:e9275bdfa393 22
star297 0:e9275bdfa393 23 Endpoint::Endpoint()
star297 0:e9275bdfa393 24 {
star297 0:e9275bdfa393 25 reset_address();
star297 0:e9275bdfa393 26 }
star297 0:e9275bdfa393 27 Endpoint::~Endpoint() {}
star297 0:e9275bdfa393 28
star297 0:e9275bdfa393 29 void Endpoint::reset_address(void)
star297 0:e9275bdfa393 30 {
star297 0:e9275bdfa393 31 _ipAddress[0] = '\0';
star297 0:e9275bdfa393 32 _port = 0;
star297 0:e9275bdfa393 33 }
star297 0:e9275bdfa393 34
star297 0:e9275bdfa393 35 int Endpoint::set_address(const char* host, const int port)
star297 0:e9275bdfa393 36 {
star297 0:e9275bdfa393 37 //Resolve DNS address or populate hard-coded IP address
star297 0:e9275bdfa393 38 // added code for searching instance of WIZnet chip. refer from Wifly Library.
star297 0:e9275bdfa393 39 WIZnet_Chip* eth = WIZnet_Chip::getInstance();
star297 0:e9275bdfa393 40 if (eth == NULL) {
star297 0:e9275bdfa393 41 error("Endpoint constructor error: no WIZnet chip instance available!\r\n");
star297 0:e9275bdfa393 42 return -1;
star297 0:e9275bdfa393 43 }
star297 0:e9275bdfa393 44 uint32_t addr;
star297 0:e9275bdfa393 45 if (!eth->gethostbyname(host, &addr)) {
star297 0:e9275bdfa393 46 return -1;
star297 0:e9275bdfa393 47 }
star297 0:e9275bdfa393 48 snprintf(_ipAddress, sizeof(_ipAddress), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
star297 0:e9275bdfa393 49 _port = port;
star297 0:e9275bdfa393 50 return 0;
star297 0:e9275bdfa393 51 }
star297 0:e9275bdfa393 52
star297 0:e9275bdfa393 53 char* Endpoint::get_address()
star297 0:e9275bdfa393 54 {
star297 0:e9275bdfa393 55 return _ipAddress;
star297 0:e9275bdfa393 56 }
star297 0:e9275bdfa393 57
star297 0:e9275bdfa393 58 int Endpoint::get_port()
star297 0:e9275bdfa393 59 {
star297 0:e9275bdfa393 60 return _port;
star297 0:e9275bdfa393 61 }