WIZ820io(W5200) network interface, EthernetInterface compatible.

Dependents:   Seeed_Ethernet_Shield_V2_HelloWorld Seeed_Ethernet_Shield Cayenne-WIZ820ioInterface Seeed_Ethernet_Shield

Fork of WiflyInterface by mbed official

WIZ820io

Committer:
va009039
Date:
Tue Aug 27 12:50:11 2013 +0000
Revision:
5:fb15c35d1e28
Parent:
1:fb4494783863
WIZ820ioInterface?first commit

Who changed what in which revision?

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