Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of WIZnetInterface by
Socket/TCPSocketConnection.cpp@30:6feeaebad180, 2016-11-04 (annotated)
- Committer:
- jh_ndm
- Date:
- Fri Nov 04 02:28:28 2016 +0000
- Revision:
- 30:6feeaebad180
- Parent:
- 0:6f28332c466f
- Child:
- 31:fc14e4330419
http to esp
Who changed what in which revision?
| User | Revision | Line number | New 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 | */ |
| Soohwan Kim |
0:6f28332c466f | 18 | |
| Soohwan Kim |
0:6f28332c466f | 19 | #include "TCPSocketConnection.h" |
| Soohwan Kim |
0:6f28332c466f | 20 | #include <cstring> |
| Soohwan Kim |
0:6f28332c466f | 21 | |
| Soohwan Kim |
0:6f28332c466f | 22 | using std::memset; |
| Soohwan Kim |
0:6f28332c466f | 23 | using std::memcpy; |
| Soohwan Kim |
0:6f28332c466f | 24 | |
| Soohwan Kim |
0:6f28332c466f | 25 | // not a big code. |
| Soohwan Kim |
0:6f28332c466f | 26 | // refer from EthernetInterface by mbed official driver |
| Soohwan Kim |
0:6f28332c466f | 27 | TCPSocketConnection::TCPSocketConnection() : |
| Soohwan Kim |
0:6f28332c466f | 28 | _is_connected(false) |
| Soohwan Kim |
0:6f28332c466f | 29 | { |
| Soohwan Kim |
0:6f28332c466f | 30 | } |
| Soohwan Kim |
0:6f28332c466f | 31 | |
| Soohwan Kim |
0:6f28332c466f | 32 | int TCPSocketConnection::connect(const char* host, const int port) |
| Soohwan Kim |
0:6f28332c466f | 33 | { |
| jh_ndm | 30:6feeaebad180 | 34 | Serial pc5(PA_13,PA_14); |
| jh_ndm | 30:6feeaebad180 | 35 | pc5.baud(115200); |
| jh_ndm | 30:6feeaebad180 | 36 | pc5.printf("in connect\r\n"); |
| Soohwan Kim |
0:6f28332c466f | 37 | if (_sock_fd < 0) { |
| Soohwan Kim |
0:6f28332c466f | 38 | _sock_fd = eth->new_socket(); |
| Soohwan Kim |
0:6f28332c466f | 39 | if (_sock_fd < 0) { |
| jh_ndm | 30:6feeaebad180 | 40 | pc5.printf("in connect 1 return -1\r\n"); |
| Soohwan Kim |
0:6f28332c466f | 41 | return -1; |
| Soohwan Kim |
0:6f28332c466f | 42 | } |
| Soohwan Kim |
0:6f28332c466f | 43 | } |
| Soohwan Kim |
0:6f28332c466f | 44 | if (set_address(host, port) != 0) { |
| jh_ndm | 30:6feeaebad180 | 45 | pc5.printf("in connect 2 return -1\r\n"); |
| Soohwan Kim |
0:6f28332c466f | 46 | return -1; |
| Soohwan Kim |
0:6f28332c466f | 47 | } |
| jh_ndm | 30:6feeaebad180 | 48 | if (!eth->connect(_sock_fd, get_address(), port)) {//should jion the timeout |
| jh_ndm | 30:6feeaebad180 | 49 | pc5.printf("in connect 3 return -1\r\n"); |
| Soohwan Kim |
0:6f28332c466f | 50 | return -1; |
| Soohwan Kim |
0:6f28332c466f | 51 | } |
| Soohwan Kim |
0:6f28332c466f | 52 | set_blocking(false); |
| Soohwan Kim |
0:6f28332c466f | 53 | // add code refer from EthernetInterface. |
| Soohwan Kim |
0:6f28332c466f | 54 | _is_connected = true; |
| Soohwan Kim |
0:6f28332c466f | 55 | |
| Soohwan Kim |
0:6f28332c466f | 56 | return 0; |
| Soohwan Kim |
0:6f28332c466f | 57 | } |
| Soohwan Kim |
0:6f28332c466f | 58 | |
| Soohwan Kim |
0:6f28332c466f | 59 | bool TCPSocketConnection::is_connected(void) |
| Soohwan Kim |
0:6f28332c466f | 60 | { |
| jh_ndm | 30:6feeaebad180 | 61 | Serial pc2(PA_13,PA_14); |
| jh_ndm | 30:6feeaebad180 | 62 | pc2.baud(115200); |
| jh_ndm | 30:6feeaebad180 | 63 | pc2.printf("in is_connected()\r\n"); |
| Soohwan Kim |
0:6f28332c466f | 64 | // force update recent state. |
| Soohwan Kim |
0:6f28332c466f | 65 | _is_connected = eth->is_connected(_sock_fd); |
| Soohwan Kim |
0:6f28332c466f | 66 | return _is_connected; |
| Soohwan Kim |
0:6f28332c466f | 67 | } |
| Soohwan Kim |
0:6f28332c466f | 68 | |
| Soohwan Kim |
0:6f28332c466f | 69 | int TCPSocketConnection::send(char* data, int length) |
| Soohwan Kim |
0:6f28332c466f | 70 | { |
| Soohwan Kim |
0:6f28332c466f | 71 | if((_sock_fd<0) || !(eth->is_connected(_sock_fd))) |
| Soohwan Kim |
0:6f28332c466f | 72 | return -1; |
| Soohwan Kim |
0:6f28332c466f | 73 | |
| Soohwan Kim |
0:6f28332c466f | 74 | int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout); |
| Soohwan Kim |
0:6f28332c466f | 75 | if (size < 0) |
| Soohwan Kim |
0:6f28332c466f | 76 | return -1; |
| Soohwan Kim |
0:6f28332c466f | 77 | |
| Soohwan Kim |
0:6f28332c466f | 78 | if (size > length) |
| Soohwan Kim |
0:6f28332c466f | 79 | size = length; |
| Soohwan Kim |
0:6f28332c466f | 80 | |
| Soohwan Kim |
0:6f28332c466f | 81 | return eth->send(_sock_fd, data, size); |
| Soohwan Kim |
0:6f28332c466f | 82 | } |
| Soohwan Kim |
0:6f28332c466f | 83 | |
| Soohwan Kim |
0:6f28332c466f | 84 | // -1 if unsuccessful, else number of bytes written |
| Soohwan Kim |
0:6f28332c466f | 85 | int TCPSocketConnection::send_all(char* data, int length) |
| Soohwan Kim |
0:6f28332c466f | 86 | { |
| Soohwan Kim |
0:6f28332c466f | 87 | int writtenLen = 0; |
| Soohwan Kim |
0:6f28332c466f | 88 | |
| Soohwan Kim |
0:6f28332c466f | 89 | if(_sock_fd<0) |
| Soohwan Kim |
0:6f28332c466f | 90 | return -1; |
| Soohwan Kim |
0:6f28332c466f | 91 | |
| Soohwan Kim |
0:6f28332c466f | 92 | while (writtenLen < length) { |
| Soohwan Kim |
0:6f28332c466f | 93 | |
| Soohwan Kim |
0:6f28332c466f | 94 | if(!(eth->is_connected(_sock_fd))) |
| Soohwan Kim |
0:6f28332c466f | 95 | return -1; |
| Soohwan Kim |
0:6f28332c466f | 96 | |
| Soohwan Kim |
0:6f28332c466f | 97 | int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout); |
| Soohwan Kim |
0:6f28332c466f | 98 | if (size < 0) { |
| Soohwan Kim |
0:6f28332c466f | 99 | return -1; |
| Soohwan Kim |
0:6f28332c466f | 100 | } |
| Soohwan Kim |
0:6f28332c466f | 101 | if (size > (length-writtenLen)) { |
| Soohwan Kim |
0:6f28332c466f | 102 | size = (length-writtenLen); |
| Soohwan Kim |
0:6f28332c466f | 103 | } |
| Soohwan Kim |
0:6f28332c466f | 104 | int ret = eth->send(_sock_fd, data + writtenLen, size); |
| Soohwan Kim |
0:6f28332c466f | 105 | if (ret < 0) { |
| Soohwan Kim |
0:6f28332c466f | 106 | return -1; |
| Soohwan Kim |
0:6f28332c466f | 107 | } |
| Soohwan Kim |
0:6f28332c466f | 108 | writtenLen += ret; |
| Soohwan Kim |
0:6f28332c466f | 109 | } |
| Soohwan Kim |
0:6f28332c466f | 110 | return writtenLen; |
| Soohwan Kim |
0:6f28332c466f | 111 | } |
| Soohwan Kim |
0:6f28332c466f | 112 | |
| Soohwan Kim |
0:6f28332c466f | 113 | // -1 if unsuccessful, else number of bytes received |
| Soohwan Kim |
0:6f28332c466f | 114 | int TCPSocketConnection::receive(char* data, int length) |
| Soohwan Kim |
0:6f28332c466f | 115 | { |
| Soohwan Kim |
0:6f28332c466f | 116 | if((_sock_fd<0) || !(eth->is_connected(_sock_fd))) |
| Soohwan Kim |
0:6f28332c466f | 117 | return -1; |
| Soohwan Kim |
0:6f28332c466f | 118 | |
| Soohwan Kim |
0:6f28332c466f | 119 | int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout); |
| Soohwan Kim |
0:6f28332c466f | 120 | if (size < 0) { |
| Soohwan Kim |
0:6f28332c466f | 121 | return -1; |
| Soohwan Kim |
0:6f28332c466f | 122 | } |
| Soohwan Kim |
0:6f28332c466f | 123 | if (size > length) { |
| Soohwan Kim |
0:6f28332c466f | 124 | size = length; |
| Soohwan Kim |
0:6f28332c466f | 125 | } |
| Soohwan Kim |
0:6f28332c466f | 126 | return eth->recv(_sock_fd, data, size); |
| Soohwan Kim |
0:6f28332c466f | 127 | } |
| Soohwan Kim |
0:6f28332c466f | 128 | |
| Soohwan Kim |
0:6f28332c466f | 129 | // -1 if unsuccessful, else number of bytes received |
| Soohwan Kim |
0:6f28332c466f | 130 | int TCPSocketConnection::receive_all(char* data, int length) |
| Soohwan Kim |
0:6f28332c466f | 131 | { |
| Soohwan Kim |
0:6f28332c466f | 132 | if(_sock_fd<0) |
| Soohwan Kim |
0:6f28332c466f | 133 | return -1; |
| Soohwan Kim |
0:6f28332c466f | 134 | |
| Soohwan Kim |
0:6f28332c466f | 135 | int readLen = 0; |
| Soohwan Kim |
0:6f28332c466f | 136 | while (readLen < length) { |
| Soohwan Kim |
0:6f28332c466f | 137 | |
| Soohwan Kim |
0:6f28332c466f | 138 | if(!(eth->is_connected(_sock_fd))) |
| Soohwan Kim |
0:6f28332c466f | 139 | return -1; |
| Soohwan Kim |
0:6f28332c466f | 140 | |
| Soohwan Kim |
0:6f28332c466f | 141 | int size = eth->wait_readable(_sock_fd, _blocking ? -1 :_timeout); |
| Soohwan Kim |
0:6f28332c466f | 142 | if (size <= 0) { |
| Soohwan Kim |
0:6f28332c466f | 143 | break; |
| Soohwan Kim |
0:6f28332c466f | 144 | } |
| Soohwan Kim |
0:6f28332c466f | 145 | if (size > (length - readLen)) { |
| Soohwan Kim |
0:6f28332c466f | 146 | size = length - readLen; |
| Soohwan Kim |
0:6f28332c466f | 147 | } |
| Soohwan Kim |
0:6f28332c466f | 148 | int ret = eth->recv(_sock_fd, data + readLen, size); |
| Soohwan Kim |
0:6f28332c466f | 149 | if (ret < 0) { |
| Soohwan Kim |
0:6f28332c466f | 150 | return -1; |
| Soohwan Kim |
0:6f28332c466f | 151 | } |
| Soohwan Kim |
0:6f28332c466f | 152 | readLen += ret; |
| Soohwan Kim |
0:6f28332c466f | 153 | } |
| Soohwan Kim |
0:6f28332c466f | 154 | return readLen; |
| Soohwan Kim |
0:6f28332c466f | 155 | } |
