MQTT client test with W5200 ethernet shield

Dependents:   IBMIoTClientEthernetExample_W5200

Fork of W5500Interface by W5500-Ethernet-Interface Makers

Committer:
kaizen
Date:
Fri Sep 26 08:05:41 2014 +0000
Revision:
5:8aefaef88f79
Parent:
4:af0ed4fbca02
Modified for using MQTT protocol ( IBMIoTClient )

Who changed what in which revision?

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