KL25 driver for Tango Control System

Dependencies:   mbed

Committer:
jskl
Date:
Thu Aug 28 07:50:06 2014 +0000
Revision:
2:9fe6f1e273b4
Parent:
0:5d27c333afa6
Fixed bugs in JSON format replies

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jskl 0:5d27c333afa6 1 /* Copyright (C) 2012 mbed.org, MIT License
jskl 0:5d27c333afa6 2 *
jskl 0:5d27c333afa6 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
jskl 0:5d27c333afa6 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
jskl 0:5d27c333afa6 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
jskl 0:5d27c333afa6 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
jskl 0:5d27c333afa6 7 * furnished to do so, subject to the following conditions:
jskl 0:5d27c333afa6 8 *
jskl 0:5d27c333afa6 9 * The above copyright notice and this permission notice shall be included in all copies or
jskl 0:5d27c333afa6 10 * substantial portions of the Software.
jskl 0:5d27c333afa6 11 *
jskl 0:5d27c333afa6 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
jskl 0:5d27c333afa6 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
jskl 0:5d27c333afa6 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
jskl 0:5d27c333afa6 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jskl 0:5d27c333afa6 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jskl 0:5d27c333afa6 17 */
jskl 0:5d27c333afa6 18
jskl 0:5d27c333afa6 19 #include "TCPSocketServer.h"
jskl 0:5d27c333afa6 20
jskl 0:5d27c333afa6 21 TCPSocketServer::TCPSocketServer() {}
jskl 0:5d27c333afa6 22
jskl 0:5d27c333afa6 23 // Server initialization
jskl 0:5d27c333afa6 24 int TCPSocketServer::bind(int port)
jskl 0:5d27c333afa6 25 {
jskl 0:5d27c333afa6 26
jskl 0:5d27c333afa6 27 if (_sock_fd < 0) {
jskl 0:5d27c333afa6 28 _sock_fd = eth->new_socket();
jskl 0:5d27c333afa6 29 if (_sock_fd < 0) {
jskl 0:5d27c333afa6 30 return -1;
jskl 0:5d27c333afa6 31 }
jskl 0:5d27c333afa6 32 }
jskl 0:5d27c333afa6 33 // set the listen_port for next connection.
jskl 0:5d27c333afa6 34 listen_port = port;
jskl 0:5d27c333afa6 35 // set TCP protocol
jskl 0:5d27c333afa6 36 eth->setProtocol(_sock_fd, TCP);
jskl 0:5d27c333afa6 37 // set local port
jskl 0:5d27c333afa6 38 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
jskl 0:5d27c333afa6 39 // connect the network
jskl 0:5d27c333afa6 40 eth->scmd(_sock_fd, OPEN);
jskl 0:5d27c333afa6 41 return 0;
jskl 0:5d27c333afa6 42 }
jskl 0:5d27c333afa6 43
jskl 0:5d27c333afa6 44 int TCPSocketServer::listen(int backlog)
jskl 0:5d27c333afa6 45 {
jskl 0:5d27c333afa6 46 if (_sock_fd < 0) {
jskl 0:5d27c333afa6 47 return -1;
jskl 0:5d27c333afa6 48 }
jskl 0:5d27c333afa6 49 if (backlog != 1) {
jskl 0:5d27c333afa6 50 return -1;
jskl 0:5d27c333afa6 51 }
jskl 0:5d27c333afa6 52 eth->scmd(_sock_fd, LISTEN);
jskl 0:5d27c333afa6 53 return 0;
jskl 0:5d27c333afa6 54 }
jskl 0:5d27c333afa6 55
jskl 0:5d27c333afa6 56
jskl 0:5d27c333afa6 57 int TCPSocketServer::accept(TCPSocketConnection& connection)
jskl 0:5d27c333afa6 58 {
jskl 0:5d27c333afa6 59 if (_sock_fd < 0) {
jskl 0:5d27c333afa6 60 return -1;
jskl 0:5d27c333afa6 61 }
jskl 0:5d27c333afa6 62 Timer t;
jskl 0:5d27c333afa6 63 t.reset();
jskl 0:5d27c333afa6 64 t.start();
jskl 0:5d27c333afa6 65 while(1) {
jskl 0:5d27c333afa6 66 if (t.read_ms() > _timeout && _blocking == false) {
jskl 0:5d27c333afa6 67 return -1;
jskl 0:5d27c333afa6 68 }
jskl 0:5d27c333afa6 69 if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) {
jskl 0:5d27c333afa6 70 break;
jskl 0:5d27c333afa6 71 }
jskl 0:5d27c333afa6 72 }
jskl 0:5d27c333afa6 73 uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR);
jskl 0:5d27c333afa6 74 char host[16];
jskl 0:5d27c333afa6 75 snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
jskl 0:5d27c333afa6 76 uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
jskl 0:5d27c333afa6 77 // change this server socket to connection socket.
jskl 0:5d27c333afa6 78 connection._sock_fd = _sock_fd;
jskl 0:5d27c333afa6 79 connection.set_address(host, port);
jskl 0:5d27c333afa6 80
jskl 0:5d27c333afa6 81 // and then, for the next connection, server socket should be assigned new one.
jskl 0:5d27c333afa6 82 _sock_fd = -1; // want to assign new available _sock_fd.
jskl 0:5d27c333afa6 83 if(bind(listen_port) < 0) {
jskl 0:5d27c333afa6 84 error("No more socket for listening");
jskl 0:5d27c333afa6 85 } else {
jskl 0:5d27c333afa6 86 //return -1;
jskl 0:5d27c333afa6 87 if(listen(1) < 0) {
jskl 0:5d27c333afa6 88 error("No more socket for listening");
jskl 0:5d27c333afa6 89 }
jskl 0:5d27c333afa6 90 }
jskl 0:5d27c333afa6 91
jskl 0:5d27c333afa6 92 return 0;
jskl 0:5d27c333afa6 93 }
jskl 0:5d27c333afa6 94