WIZnetInterface using namespace

Dependents:   DualNetworkInterface-Basic

Fork of WIZnetInterface by WIZnet

Committer:
SteveKim
Date:
Tue Jul 14 10:16:16 2015 +0000
Revision:
20:3e61863c1f67
Parent:
0:6f28332c466f
Dual Network Interface for mbed;  - WIZwiki-W7500;  - ESP8266

Who changed what in which revision?

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