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