For mbed OS-5 version for WIZnet Ethernet Interface, this is Library using Hardware TCP/IP chip, W5500 and TCP/IP Offload Engine, W7500.

Dependents:   ledMapperTest

Warning

  • If you want to use existing codes, you need to change the class used as EthernetInterface to WIZnetInterface.

This is WIZnet Ethernet Interface using Hardware TCP/IP chip, W5500 and TCP/IP Offload Engine, W7500.

https://developer.mbed.org/media/cache/platforms/WIZwiki_W7500_enabled.JPG.250x250_q85.jpg

https://developer.mbed.org/media/cache/platforms/WIZwiki_W7500P_enabled2.JPG.250x250_q85.jpg

https://developer.mbed.org/media/cache/platforms/WIZwiki_W7500ECO_enabled2.JPG.250x250_q85.jpg

https://developer.mbed.org/media/cache/components/components/fetch.phpmediaoshw5500_ethernet_shieldw5500_main_picture2.png.200x200_q85.jpg

This library is an Ethernet Interface library port-based on [EthernetInterface](https://developer.mbed.org/users/mbed_official/code/EthernetInterface/docs/tip/).

For more detail, visit http://embeddist.blogspot.kr/2015/06/wiznetinterface-for-armmbed.html

Committer:
justinkim
Date:
Mon Sep 04 00:23:04 2017 +0000
Revision:
0:d4c8fe4d9b29
mbed OS 5 version migration...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
justinkim 0:d4c8fe4d9b29 1 /* Copyright (C) 2012 mbed.org, MIT License
justinkim 0:d4c8fe4d9b29 2 *
justinkim 0:d4c8fe4d9b29 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
justinkim 0:d4c8fe4d9b29 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
justinkim 0:d4c8fe4d9b29 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
justinkim 0:d4c8fe4d9b29 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
justinkim 0:d4c8fe4d9b29 7 * furnished to do so, subject to the following conditions:
justinkim 0:d4c8fe4d9b29 8 *
justinkim 0:d4c8fe4d9b29 9 * The above copyright notice and this permission notice shall be included in all copies or
justinkim 0:d4c8fe4d9b29 10 * substantial portions of the Software.
justinkim 0:d4c8fe4d9b29 11 *
justinkim 0:d4c8fe4d9b29 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
justinkim 0:d4c8fe4d9b29 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
justinkim 0:d4c8fe4d9b29 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
justinkim 0:d4c8fe4d9b29 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
justinkim 0:d4c8fe4d9b29 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
justinkim 0:d4c8fe4d9b29 17 */
justinkim 0:d4c8fe4d9b29 18
justinkim 0:d4c8fe4d9b29 19 #include "TCPSocketConnection.h"
justinkim 0:d4c8fe4d9b29 20 #include <cstring>
justinkim 0:d4c8fe4d9b29 21
justinkim 0:d4c8fe4d9b29 22 using std::memset;
justinkim 0:d4c8fe4d9b29 23 using std::memcpy;
justinkim 0:d4c8fe4d9b29 24
justinkim 0:d4c8fe4d9b29 25 // not a big code.
justinkim 0:d4c8fe4d9b29 26 // refer from EthernetInterface by mbed official driver
justinkim 0:d4c8fe4d9b29 27 TCPSocketConnection::TCPSocketConnection() :
justinkim 0:d4c8fe4d9b29 28 _is_connected(false)
justinkim 0:d4c8fe4d9b29 29 {
justinkim 0:d4c8fe4d9b29 30 }
justinkim 0:d4c8fe4d9b29 31
justinkim 0:d4c8fe4d9b29 32 int TCPSocketConnection::connect(const char* host, const int port)
justinkim 0:d4c8fe4d9b29 33 {
justinkim 0:d4c8fe4d9b29 34 if (_sock_fd < 0) {
justinkim 0:d4c8fe4d9b29 35 _sock_fd = eth->new_socket();
justinkim 0:d4c8fe4d9b29 36 if (_sock_fd < 0) {
justinkim 0:d4c8fe4d9b29 37 return -1;
justinkim 0:d4c8fe4d9b29 38 }
justinkim 0:d4c8fe4d9b29 39 }
justinkim 0:d4c8fe4d9b29 40 if (set_address(host, port) != 0) {
justinkim 0:d4c8fe4d9b29 41 return -1;
justinkim 0:d4c8fe4d9b29 42 }
justinkim 0:d4c8fe4d9b29 43 if (!eth->connect(_sock_fd, get_address(), port)) {
justinkim 0:d4c8fe4d9b29 44 return -1;
justinkim 0:d4c8fe4d9b29 45 }
justinkim 0:d4c8fe4d9b29 46 set_blocking(false);
justinkim 0:d4c8fe4d9b29 47 // add code refer from EthernetInterface.
justinkim 0:d4c8fe4d9b29 48 _is_connected = true;
justinkim 0:d4c8fe4d9b29 49
justinkim 0:d4c8fe4d9b29 50 return 0;
justinkim 0:d4c8fe4d9b29 51 }
justinkim 0:d4c8fe4d9b29 52
justinkim 0:d4c8fe4d9b29 53 bool TCPSocketConnection::is_connected(void)
justinkim 0:d4c8fe4d9b29 54 {
justinkim 0:d4c8fe4d9b29 55 // force update recent state.
justinkim 0:d4c8fe4d9b29 56 _is_connected = eth->is_connected(_sock_fd);
justinkim 0:d4c8fe4d9b29 57 return _is_connected;
justinkim 0:d4c8fe4d9b29 58 }
justinkim 0:d4c8fe4d9b29 59
justinkim 0:d4c8fe4d9b29 60 int TCPSocketConnection::send(char* data, int length)
justinkim 0:d4c8fe4d9b29 61 {
justinkim 0:d4c8fe4d9b29 62 if((_sock_fd<0) || !(eth->is_connected(_sock_fd)))
justinkim 0:d4c8fe4d9b29 63 return -1;
justinkim 0:d4c8fe4d9b29 64
justinkim 0:d4c8fe4d9b29 65 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
justinkim 0:d4c8fe4d9b29 66 if (size < 0)
justinkim 0:d4c8fe4d9b29 67 return -1;
justinkim 0:d4c8fe4d9b29 68
justinkim 0:d4c8fe4d9b29 69 if (size > length)
justinkim 0:d4c8fe4d9b29 70 size = length;
justinkim 0:d4c8fe4d9b29 71
justinkim 0:d4c8fe4d9b29 72 return eth->send(_sock_fd, data, size);
justinkim 0:d4c8fe4d9b29 73 }
justinkim 0:d4c8fe4d9b29 74
justinkim 0:d4c8fe4d9b29 75 // -1 if unsuccessful, else number of bytes written
justinkim 0:d4c8fe4d9b29 76 int TCPSocketConnection::send_all(char* data, int length)
justinkim 0:d4c8fe4d9b29 77 {
justinkim 0:d4c8fe4d9b29 78 int writtenLen = 0;
justinkim 0:d4c8fe4d9b29 79
justinkim 0:d4c8fe4d9b29 80 if(_sock_fd<0)
justinkim 0:d4c8fe4d9b29 81 return -1;
justinkim 0:d4c8fe4d9b29 82
justinkim 0:d4c8fe4d9b29 83 while (writtenLen < length) {
justinkim 0:d4c8fe4d9b29 84
justinkim 0:d4c8fe4d9b29 85 if(!(eth->is_connected(_sock_fd)))
justinkim 0:d4c8fe4d9b29 86 return -1;
justinkim 0:d4c8fe4d9b29 87
justinkim 0:d4c8fe4d9b29 88 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
justinkim 0:d4c8fe4d9b29 89 if (size < 0) {
justinkim 0:d4c8fe4d9b29 90 return -1;
justinkim 0:d4c8fe4d9b29 91 }
justinkim 0:d4c8fe4d9b29 92 if (size > (length-writtenLen)) {
justinkim 0:d4c8fe4d9b29 93 size = (length-writtenLen);
justinkim 0:d4c8fe4d9b29 94 }
justinkim 0:d4c8fe4d9b29 95 int ret = eth->send(_sock_fd, data + writtenLen, size);
justinkim 0:d4c8fe4d9b29 96 if (ret < 0) {
justinkim 0:d4c8fe4d9b29 97 return -1;
justinkim 0:d4c8fe4d9b29 98 }
justinkim 0:d4c8fe4d9b29 99 writtenLen += ret;
justinkim 0:d4c8fe4d9b29 100 }
justinkim 0:d4c8fe4d9b29 101 return writtenLen;
justinkim 0:d4c8fe4d9b29 102 }
justinkim 0:d4c8fe4d9b29 103
justinkim 0:d4c8fe4d9b29 104 // -1 if unsuccessful, else number of bytes received
justinkim 0:d4c8fe4d9b29 105 int TCPSocketConnection::receive(char* data, int length)
justinkim 0:d4c8fe4d9b29 106 {
justinkim 0:d4c8fe4d9b29 107 if((_sock_fd<0) || !(eth->is_connected(_sock_fd)))
justinkim 0:d4c8fe4d9b29 108 return -1;
justinkim 0:d4c8fe4d9b29 109
justinkim 0:d4c8fe4d9b29 110 int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout);
justinkim 0:d4c8fe4d9b29 111 if (size < 0) {
justinkim 0:d4c8fe4d9b29 112 return -1;
justinkim 0:d4c8fe4d9b29 113 }
justinkim 0:d4c8fe4d9b29 114 if (size > length) {
justinkim 0:d4c8fe4d9b29 115 size = length;
justinkim 0:d4c8fe4d9b29 116 }
justinkim 0:d4c8fe4d9b29 117 return eth->recv(_sock_fd, data, size);
justinkim 0:d4c8fe4d9b29 118 }
justinkim 0:d4c8fe4d9b29 119
justinkim 0:d4c8fe4d9b29 120 // -1 if unsuccessful, else number of bytes received
justinkim 0:d4c8fe4d9b29 121 int TCPSocketConnection::receive_all(char* data, int length)
justinkim 0:d4c8fe4d9b29 122 {
justinkim 0:d4c8fe4d9b29 123 if(_sock_fd<0)
justinkim 0:d4c8fe4d9b29 124 return -1;
justinkim 0:d4c8fe4d9b29 125
justinkim 0:d4c8fe4d9b29 126 int readLen = 0;
justinkim 0:d4c8fe4d9b29 127 while (readLen < length) {
justinkim 0:d4c8fe4d9b29 128
justinkim 0:d4c8fe4d9b29 129 if(!(eth->is_connected(_sock_fd)))
justinkim 0:d4c8fe4d9b29 130 return -1;
justinkim 0:d4c8fe4d9b29 131
justinkim 0:d4c8fe4d9b29 132 int size = eth->wait_readable(_sock_fd, _blocking ? -1 :_timeout);
justinkim 0:d4c8fe4d9b29 133 if (size <= 0) {
justinkim 0:d4c8fe4d9b29 134 break;
justinkim 0:d4c8fe4d9b29 135 }
justinkim 0:d4c8fe4d9b29 136 if (size > (length - readLen)) {
justinkim 0:d4c8fe4d9b29 137 size = length - readLen;
justinkim 0:d4c8fe4d9b29 138 }
justinkim 0:d4c8fe4d9b29 139 int ret = eth->recv(_sock_fd, data + readLen, size);
justinkim 0:d4c8fe4d9b29 140 if (ret < 0) {
justinkim 0:d4c8fe4d9b29 141 return -1;
justinkim 0:d4c8fe4d9b29 142 }
justinkim 0:d4c8fe4d9b29 143 readLen += ret;
justinkim 0:d4c8fe4d9b29 144 }
justinkim 0:d4c8fe4d9b29 145 return readLen;
justinkim 0:d4c8fe4d9b29 146 }