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 */
Soohwan Kim 0:6f28332c466f 18
SteveKim 20:3e61863c1f67 19 #include "TCPSocketServer.hpp"
Soohwan Kim 0:6f28332c466f 20
SteveKim 20:3e61863c1f67 21 using namespace wiznet_space;
SteveKim 20:3e61863c1f67 22
Soohwan Kim 0:6f28332c466f 23 TCPSocketServer::TCPSocketServer() {}
Soohwan Kim 0:6f28332c466f 24
Soohwan Kim 0:6f28332c466f 25 // Server initialization
Soohwan Kim 0:6f28332c466f 26 int TCPSocketServer::bind(int port)
Soohwan Kim 0:6f28332c466f 27 {
Soohwan Kim 0:6f28332c466f 28 // set the listen_port for next connection.
Soohwan Kim 0:6f28332c466f 29 listen_port = port;
Soohwan Kim 0:6f28332c466f 30 if (_sock_fd < 0) {
Soohwan Kim 0:6f28332c466f 31 _sock_fd = eth->new_socket();
Soohwan Kim 0:6f28332c466f 32 if (_sock_fd < 0) {
Soohwan Kim 0:6f28332c466f 33 return -1;
Soohwan Kim 0:6f28332c466f 34 }
Soohwan Kim 0:6f28332c466f 35 }
Soohwan Kim 0:6f28332c466f 36 // set TCP protocol
Soohwan Kim 0:6f28332c466f 37 eth->setProtocol(_sock_fd, WIZnet_Chip::TCP);
Soohwan Kim 0:6f28332c466f 38 // set local port
Soohwan Kim 0:6f28332c466f 39 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
Soohwan Kim 0:6f28332c466f 40 // connect the network
Soohwan Kim 0:6f28332c466f 41 eth->scmd(_sock_fd, WIZnet_Chip::OPEN);
Soohwan Kim 0:6f28332c466f 42 return 0;
Soohwan Kim 0:6f28332c466f 43 }
Soohwan Kim 0:6f28332c466f 44
Soohwan Kim 0:6f28332c466f 45 int TCPSocketServer::listen(int backlog)
Soohwan Kim 0:6f28332c466f 46 {
Soohwan Kim 0:6f28332c466f 47 if (_sock_fd < 0) {
Soohwan Kim 0:6f28332c466f 48 return -1;
Soohwan Kim 0:6f28332c466f 49 }
Soohwan Kim 0:6f28332c466f 50 if (backlog != 1) {
Soohwan Kim 0:6f28332c466f 51 return -1;
Soohwan Kim 0:6f28332c466f 52 }
Soohwan Kim 0:6f28332c466f 53 eth->scmd(_sock_fd, WIZnet_Chip::LISTEN);
Soohwan Kim 0:6f28332c466f 54 return 0;
Soohwan Kim 0:6f28332c466f 55 }
Soohwan Kim 0:6f28332c466f 56
Soohwan Kim 0:6f28332c466f 57
Soohwan Kim 0:6f28332c466f 58 int TCPSocketServer::accept(TCPSocketConnection& connection)
Soohwan Kim 0:6f28332c466f 59 {
Soohwan Kim 0:6f28332c466f 60 if (_sock_fd < 0) {
Soohwan Kim 0:6f28332c466f 61 return -1;
Soohwan Kim 0:6f28332c466f 62 }
Soohwan Kim 0:6f28332c466f 63 Timer t;
Soohwan Kim 0:6f28332c466f 64 t.reset();
Soohwan Kim 0:6f28332c466f 65 t.start();
Soohwan Kim 0:6f28332c466f 66 while(1) {
Soohwan Kim 0:6f28332c466f 67 if (t.read_ms() > _timeout && _blocking == false) {
Soohwan Kim 0:6f28332c466f 68 return -1;
Soohwan Kim 0:6f28332c466f 69 }
Soohwan Kim 0:6f28332c466f 70 if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == WIZnet_Chip::SOCK_ESTABLISHED) {
Soohwan Kim 0:6f28332c466f 71 break;
Soohwan Kim 0:6f28332c466f 72 }
Soohwan Kim 0:6f28332c466f 73 }
Soohwan Kim 0:6f28332c466f 74 uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR);
Soohwan Kim 0:6f28332c466f 75 char host[16];
Soohwan Kim 0:6f28332c466f 76 snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
Soohwan Kim 0:6f28332c466f 77 uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
Soohwan Kim 0:6f28332c466f 78
Soohwan Kim 0:6f28332c466f 79 // change this server socket to connection socket.
Soohwan Kim 0:6f28332c466f 80 connection._sock_fd = _sock_fd;
Soohwan Kim 0:6f28332c466f 81 connection._is_connected = true;
Soohwan Kim 0:6f28332c466f 82 connection.set_address(host, port);
Soohwan Kim 0:6f28332c466f 83
Soohwan Kim 0:6f28332c466f 84 // and then, for the next connection, server socket should be assigned new one.
Soohwan Kim 0:6f28332c466f 85 _sock_fd = -1; // want to assign new available _sock_fd.
Soohwan Kim 0:6f28332c466f 86 if(bind(listen_port) < 0) {
Soohwan Kim 0:6f28332c466f 87 // modified by Patrick Pollet
Soohwan Kim 0:6f28332c466f 88 error("No more socket for listening, bind error");
Soohwan Kim 0:6f28332c466f 89 return -1;
Soohwan Kim 0:6f28332c466f 90 } else {
Soohwan Kim 0:6f28332c466f 91 //return -1;
Soohwan Kim 0:6f28332c466f 92 if(listen(1) < 0) {
Soohwan Kim 0:6f28332c466f 93 // modified by Patrick Pollet
Soohwan Kim 0:6f28332c466f 94 error("No more socket for listening, listen error");
Soohwan Kim 0:6f28332c466f 95 return -1;
Soohwan Kim 0:6f28332c466f 96 }
Soohwan Kim 0:6f28332c466f 97 }
Soohwan Kim 0:6f28332c466f 98
Soohwan Kim 0:6f28332c466f 99 return 0;
Soohwan Kim 0:6f28332c466f 100 }