M2X with Seeed Ethernet using Seeed Accelerometer demo

Dependencies:   LM75B M2XStreamClient jsonlite mbed-rtos mbed

Fork of m2x-seeed_ethernet_demo by Sean Newton

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCPSocketServer.cpp Source File

TCPSocketServer.cpp

00001 /* Copyright (C) 2012 mbed.org, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 #include "TCPSocketServer.h"
00020 
00021 TCPSocketServer::TCPSocketServer() {}
00022 
00023 // Server initialization
00024 int TCPSocketServer::bind(int port)
00025 {
00026 
00027     if (_sock_fd < 0) {
00028         _sock_fd = eth->new_socket();
00029         if (_sock_fd < 0) {
00030             return -1;
00031         }
00032     }
00033     // set the listen_port for next connection.
00034     listen_port = port;
00035     // set TCP protocol
00036     eth->setProtocol(_sock_fd, TCP);
00037     // set local port
00038     eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
00039     // connect the network
00040     eth->scmd(_sock_fd, OPEN);
00041     return 0;
00042 }
00043 
00044 int TCPSocketServer::listen(int backlog)
00045 {
00046     if (_sock_fd < 0) {
00047         return -1;
00048     }
00049     if (backlog != 1) {
00050         return -1;
00051     }
00052     eth->scmd(_sock_fd, LISTEN);
00053     return 0;
00054 }
00055 
00056 
00057 int TCPSocketServer::accept(TCPSocketConnection& connection)
00058 {
00059     if (_sock_fd < 0) {
00060         return -1;
00061     }
00062     Timer t;
00063     t.reset();
00064     t.start();
00065     while(1) {
00066         if (t.read_ms() > _timeout && _blocking == false) {
00067             return -1;
00068         }
00069         if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) {
00070             break;
00071         }
00072     }
00073     uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR);
00074     char host[16];
00075     snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
00076     uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
00077     // change this server socket to connection socket.
00078     connection._sock_fd = _sock_fd;
00079     connection.set_address(host, port);
00080 
00081     // and then, for the next connection, server socket should be assigned new one.
00082     _sock_fd = -1; // want to assign new available _sock_fd.
00083     if(bind(listen_port) < 0) {
00084         error("No more socket for listening");
00085     } else {
00086         //return -1;
00087         if(listen(1) < 0) {
00088             error("No more socket for listening");
00089         }
00090     }
00091 
00092     return 0;
00093 }
00094