Temp fork

Dependents:   Seeed_GPRS_Shield

Fork of GPRSInterface by wei zou

Files at this revision

API Documentation at this revision

Comitter:
lawliet
Date:
Tue Feb 25 02:52:48 2014 +0000
Child:
1:7298a7950f65
Commit message:
Initial version of GPRS Interface

Changed in this revision

GPRS/GPRS.cpp Show annotated file Show diff for this revision Revisions of this file
GPRS/GPRS.h Show annotated file Show diff for this revision Revisions of this file
GPRS/modem/modem.cpp Show annotated file Show diff for this revision Revisions of this file
GPRS/modem/modem.h Show annotated file Show diff for this revision Revisions of this file
GPRSInterface.cpp Show annotated file Show diff for this revision Revisions of this file
GPRSInterface.h Show annotated file Show diff for this revision Revisions of this file
Socket/Endpoint.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Endpoint.h Show annotated file Show diff for this revision Revisions of this file
Socket/Socket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Socket.h Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketConnection.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketConnection.h Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketServer.h Show annotated file Show diff for this revision Revisions of this file
pico_string.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPRS/GPRS.cpp	Tue Feb 25 02:52:48 2014 +0000
@@ -0,0 +1,280 @@
+/*
+  GPRS.cpp
+  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
+
+  Author:lawliet zou(lawliet.zou@gmail.com)
+  2014-2-24
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#include "mbed.h"
+#include "GPRS.h"
+
+GPRS* GPRS::inst;
+
+GPRS::GPRS(PinName tx, PinName rx, int baudRate, const char* apn, const char* userName, const char* passWord) : Modem(tx,rx,baudRate)
+{
+    inst = this;
+    _apn = apn;
+    _userName = userName;
+    _passWord = passWord;
+    socketID = -1;
+}
+
+bool GPRS::preInit()
+{
+    for(int i = 0; i < 2; i++) {
+        sendCmd("AT\r\n");
+        wait(1);
+    }
+    return checkSIMStatus();
+}
+
+bool GPRS::checkSIMStatus(void)
+{
+    char gprsBuffer[32];
+    int count = 0;
+    cleanBuffer(gprsBuffer,32);
+    while(count < 3) {
+        sendCmd("AT+CPIN?\r\n");
+        readBuffer(gprsBuffer,32,DEFAULT_TIMEOUT);
+        if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) {
+            break;
+        }
+        count++;
+        wait(1);
+    }
+    if(count == 3) {
+        return false;
+    }
+    return true;
+}
+
+bool GPRS::join()
+{
+    char cmd[64];
+    char ipAddr[32];
+    //Select multiple connection
+    sendCmdAndWaitForResp("AT+CIPMUX=1\r\n","OK",DEFAULT_TIMEOUT,CMD);
+
+    //set APN
+    snprintf(cmd,sizeof(cmd),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",_apn,_userName,_passWord);
+    sendCmdAndWaitForResp(cmd, "OK", DEFAULT_TIMEOUT,CMD);
+
+    //Brings up wireless connection
+    sendCmdAndWaitForResp("AT+CIICR\r\n","OK",DEFAULT_TIMEOUT,CMD);
+
+    //Get local IP address
+    sendCmd("AT+CIFSR\r\n");
+    readBuffer(ipAddr,32,2);
+
+    if(NULL != strstr(ipAddr,"AT+CIFSR")) {
+        _ip = str_to_ip(ipAddr+12);
+        if(_ip != 0) {
+            return true;
+        }
+    }
+    return false;
+}
+
+bool GPRS::setProtocol(int socket, Protocol p)
+{
+    if (socket < 0 || socket > MAX_SOCK_NUM-1) {
+        return false;
+    }
+    //ToDo: setProtocol
+    return true;
+}
+
+bool GPRS::connect(int socket, Protocol ptl,const char * host, int port, int timeout)
+{
+    char cmd[64];
+    char resp[96];
+    if (socket < 0 || socket > MAX_SOCK_NUM-1) {
+        return false;
+    }
+    if(ptl == TCP) {
+        sprintf(cmd, "AT+CIPSTART=%d,\"TCP\",\"%s\",%d\r\n",socket, host, port);
+    } else if(ptl == UDP) {
+        sprintf(cmd, "AT+CIPSTART=%d,\"UDP\",\"%s\",%d\r\n",socket, host, port);
+    } else {
+        return false;
+    }
+    sendCmd(cmd);
+    readBuffer(resp,96,2*DEFAULT_TIMEOUT);
+    if(NULL != strstr(resp,"CONNECT")) { //ALREADY CONNECT or CONNECT OK
+        return true;
+    }
+    return false;//ERROR
+}
+
+bool GPRS::gethostbyname(const char* host, uint32_t* ip)
+{
+    uint32_t addr = str_to_ip(host);
+    char buf[17];
+    snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
+    if (strcmp(buf, host) == 0) {
+        *ip = addr;
+        return true;
+    }
+    return false;
+}
+
+bool GPRS::disconnect()
+{
+    sendCmd("AT+CIPSHUT\r\n");
+    return true;
+}
+
+bool GPRS::is_connected(int socket)
+{
+    char cmd[16];
+    char resp[64];
+    snprintf(cmd,16,"AT+CIPSTAUTS=%d\r\n",socket);
+    sendCmd(cmd);
+    readBuffer(resp,sizeof(resp),DEFAULT_TIMEOUT);
+    if(NULL != strstr(resp,"CONNECTED")) {
+        //+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CONNECTED"
+        return true;
+    } else {
+        //+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CLOSED"
+        //+CIPSTATUS: 0,,"","","","INITIAL"
+        return false;
+    }
+}
+
+void GPRS::reset()
+{
+
+}
+
+bool GPRS::close(int socket)
+{
+    char cmd[16];
+    char resp[16];
+
+    if (socket < 0 || socket > MAX_SOCK_NUM-1) {
+        return false;
+    }
+    // if not connected, return
+    if (is_connected(socket) == false) {
+        return true;
+    }
+    snprintf(cmd, sizeof(cmd),"AT+CIPCOLSE=%d\r\n",socket);
+    snprintf(resp,sizeof(resp),"%d, CLOSE OK",socket);
+    if(0 != sendCmdAndWaitForResp(cmd, resp, DEFAULT_TIMEOUT,CMD)) {
+        return false;
+    }
+    return true;
+}
+
+bool GPRS::readable(void)
+{
+    return readable();
+}
+
+int GPRS::wait_readable(int socket, int wait_time)
+{
+    if (socket < 0 || socket > MAX_SOCK_NUM-1) {
+        return -1;
+    }
+    char resp[16];
+    snprintf(resp,sizeof(resp),"\r\n\r\n+RECEIVE,%d",socket);//"+RECEIVE:<socketID>,<length>"
+    int len = strlen(resp);
+
+    if(false == respCmp(resp,len,wait_time)) {
+        return -1;
+    }
+    char c = readByte();//','
+    char dataLen[4];
+    int i = 0;
+    c = readByte();
+    while((c >= '0') && (c <= '9')) {
+        dataLen[i++] = c;
+        c = readByte();
+    }
+    c = readByte();//'\n'
+    len = atoi(dataLen);
+    return len;
+}
+
+int GPRS::wait_writeable(int socket, int req_size)
+{
+    if (socket < 0 || socket > MAX_SOCK_NUM-1) {
+        return -1;
+    }
+    return req_size>256?256:req_size+1;
+}
+
+int GPRS::send(int socket, const char * str, int len)
+{
+    if (socket < 0 || socket > MAX_SOCK_NUM-1) {
+        return -1;
+    }
+
+    char cmd[32];
+    char resp[16];
+    wait(1);
+    snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d,%d\r\n",socket,len);
+    if(0 != sendCmdAndWaitForResp(cmd,">",DEFAULT_TIMEOUT,CMD)) {
+        return false;
+    }
+    snprintf(resp,sizeof(resp),"%d, SEND OK",socket);
+    if(0 != sendCmdAndWaitForResp(str,resp,DEFAULT_TIMEOUT,DATA)) {
+        return -1;
+    }
+
+    return len;
+}
+
+int GPRS::recv(int socket, char* buf, int len)
+{
+    if (socket < 0 || socket > MAX_SOCK_NUM-1) {
+        return -1;
+    }
+    cleanBuffer(buf,len);
+    readBuffer(buf,len,DEFAULT_TIMEOUT/2);
+    return len;
+    //return strlen(buf);
+}
+
+int GPRS::new_socket()
+{
+    return socketID >= (MAX_SOCK_NUM-1)?(-1):(++socketID);
+}
+
+uint16_t GPRS::new_port()
+{
+    uint16_t port = rand();
+    port |= 49152;
+    return port;
+}
+
+uint32_t GPRS::str_to_ip(const char* str)
+{
+    uint32_t ip = 0;
+    char* p = (char*)str;
+    for(int i = 0; i < 4; i++) {
+        ip |= atoi(p);
+        p = strchr(p, '.');
+        if (p == NULL) {
+            break;
+        }
+        ip <<= 8;
+        p++;
+    }
+    return ip;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPRS/GPRS.h	Tue Feb 25 02:52:48 2014 +0000
@@ -0,0 +1,155 @@
+/*
+  GPRS.h
+  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
+
+  Author:lawliet zou(lawliet.zou@gmail.com)
+  2014-2-24
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#ifndef __GPRS_H__
+#define __GPRS_H__
+
+#include "mbed.h"
+#include "modem.h"
+
+#define DEFAULT_WAIT_RESP_TIMEOUT 500
+#define MAX_SOCK_NUM 7 //(0~6)
+
+enum Protocol {
+    CLOSED = 0,
+    TCP    = 1,
+    UDP    = 2,
+};
+
+class GPRS: public Modem
+{
+
+public:
+    /**	Constructor
+     *	@param tx mbed pin to use for tx line of Serial interface
+     * 	@param rx mbed pin to use for rx line of Serial interface
+     * 	@param baudRate serial communicate baud rate
+     * 	@param apn name of the gateway for GPRS to connect to the network
+     * 	@param userName apn's username, usually is NULL
+     * 	@param passWord apn's password, usually is NULL
+     */
+    GPRS(PinName tx, PinName rx, int baudRate, const char* apn, const char* userName = NULL, const char *passWord = NULL);
+
+    /**	Connect the GPRS module to the network.
+     * 	@return true if connected, false otherwise
+     */
+    bool join(void);
+
+    /** Disconnect the GPRS module from the network
+     * 	@returns true if successful
+     */
+    bool disconnect(void);
+
+    /** Open a tcp/udp connection with the specified host on the specified port
+     * 	@param socket an endpoint of an inter-process communication flow of GPRS module,for SIM900 module, it is in [0,6]
+     * 	@param ptl protocol for socket, TCP/UDP can be choosen
+     * 	@param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
+     * 	@param port port
+     * 	@param timeout wait seconds till connected
+     * 	@returns true if successful
+     */
+    bool connect(int socket, Protocol ptl, const char * host, int port, int timeout = DEFAULT_TIMEOUT);
+
+    /** Set the protocol (UDP or TCP)
+     * 	@param socket socket
+     * 	@param p protocol
+     * 	@returns true if successful
+     */
+    bool setProtocol(int socket, Protocol p);
+
+    /** Reset the GPRS module
+     */
+    void reset();
+
+    /**	check if GPRS module is readable or not
+     *	@returns true if readable
+     */
+    bool readable(void);
+
+    /**	wait a few time to check if GPRS module is readable or not
+     *	@param socket socket
+     *	@param wait_time time of waiting
+     */
+    int wait_readable(int socket, int wait_time);
+
+    /**	wait a few time to check if GPRS module is writeable or not
+     *	@param socket socket
+     *	@param wait_time time of waiting
+     */
+    int wait_writeable(int socket, int req_size);
+
+    /** Check if a tcp link is active
+     * 	@returns true if successful
+     */
+    bool is_connected(int socket);
+
+    /** Close a tcp connection
+     * 	@returns true if successful
+     */
+    bool close(int socket);
+
+    /** send data to socket
+     *	@param socket socket
+     *	@param str string to be sent
+     * 	@param len string length
+     * 	@returns return bytes that actually been send
+     */
+    int send(int socket, const char * str, int len);
+
+    /** read data from socket
+     * 	@param socket socket
+     *	@param buf buffer that will store the data read from socket
+     *	@param len string length need to read from socket
+     *	@returns bytes that actually read
+     */
+    int recv(int socket, char* buf, int len);
+
+    /** convert the host to ip
+     *  @param host host ip string, ex. 10.11.12.13
+     *  @param ip long int ip address, ex. 0x11223344
+     *  @returns true if successful
+     */
+    bool gethostbyname(const char* host, uint32_t* ip);
+
+    /** get instance of GPRS class
+     */
+    static GPRS* getInstance() {
+        return inst;
+    };
+
+    int new_socket();
+    uint16_t new_port();
+    uint32_t _ip;
+
+protected:
+
+    bool preInit();
+    bool checkSIMStatus(void);
+    uint32_t str_to_ip(const char* str);
+    static GPRS* inst;
+    int socketID;
+    const char* _apn;
+    const char* _userName;
+    const char* _passWord;
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPRS/modem/modem.cpp	Tue Feb 25 02:52:48 2014 +0000
@@ -0,0 +1,129 @@
+/*
+  modem.cpp
+  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
+
+  Author:lawliet zou(lawliet.zou@gmail.com)
+  2014-2-24
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#include "modem.h"
+
+char Modem::readByte(void)
+{
+    return serialModem.getc();
+}
+
+bool Modem::readable()
+{
+    return serialModem.readable();
+}
+
+int Modem::readBuffer(char *buffer,int count, unsigned int timeOut)
+{
+    int i = 0;
+    timeCnt.start();
+    while(1) {
+        while (serialModem.readable()) {
+            char c = serialModem.getc();
+            buffer[i++] = c;
+            if(i >= count)break;
+        }
+        if(i >= count)break;
+        if(timeCnt.read() > timeOut) {
+            timeCnt.stop();
+            timeCnt.reset();
+            break;
+        }
+    }
+    return 0;
+}
+
+void Modem::cleanBuffer(char *buffer, int count)
+{
+    for(int i=0; i < count; i++) {
+        buffer[i] = '\0';
+    }
+}
+
+void Modem::sendCmd(const char* cmd)
+{
+    serialModem.puts(cmd);
+}
+
+void Modem::sendATTest(void)
+{
+    sendCmdAndWaitForResp("AT\r\n","OK",DEFAULT_TIMEOUT,CMD);
+}
+
+bool Modem::respCmp(const char *resp, unsigned int len, unsigned int timeout)
+{
+    int sum=0;
+    timeCnt.start();
+
+    while(1) {
+        if(serialModem.readable()) {
+            char c = serialModem.getc();
+            sum = (c==resp[sum]) ? sum+1 : 0;
+            if(sum == len)break;
+        }
+        if(timeCnt.read() > timeout) {
+            timeCnt.stop();
+            timeCnt.reset();
+            return false;
+        }
+    }
+    timeCnt.stop();
+    timeCnt.reset();
+
+    return true;
+}
+
+int Modem::waitForResp(const char *resp, unsigned int timeout,DataType type)
+{
+    int len = strlen(resp);
+    int sum=0;
+    timeCnt.start();
+
+    while(1) {
+        if(serialModem.readable()) {
+            char c = serialModem.getc();
+            sum = (c==resp[sum]) ? sum+1 : 0;
+            if(sum == len)break;
+        }
+        if(timeCnt.read() > timeout) {
+            timeCnt.stop();
+            timeCnt.reset();
+            return -1;
+        }
+    }
+    timeCnt.stop();
+    timeCnt.reset();
+
+    if(type == CMD) {
+        while(serialModem.readable()) {
+            char c = serialModem.getc();
+        }
+    }
+
+    return 0;
+}
+
+int Modem::sendCmdAndWaitForResp(const char* data, const char *resp, unsigned timeout,DataType type)
+{
+    sendCmd(data);
+    return waitForResp(resp,timeout,type);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPRS/modem/modem.h	Tue Feb 25 02:52:48 2014 +0000
@@ -0,0 +1,132 @@
+/*
+  modem.h
+  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
+
+  Author:lawliet zou(lawliet.zou@gmail.com)
+  2014-2-24
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#ifndef __MODEM_H__
+#define __MODEM_H__
+
+#include "mbed.h"
+
+#define DEFAULT_TIMEOUT   	5
+
+enum DataType {
+    CMD		= 0,
+    DATA	= 1,
+};
+
+/** Modem class.
+ *  Used for Modem communication. attention that Modem module communicate with MCU in serial protocol
+ */
+class Modem
+{
+
+public:
+    /**	Create Modem Instance
+     *  @param tx	uart transmit pin to communicate with Modem
+     *  @param rx	uart receive pin to communicate with Modem
+     *  @param baudRate	baud rate of uart communication
+     */
+    Modem(PinName tx, PinName rx, int baudRate) : serialModem(tx, rx) {
+        serialModem.baud(baudRate);
+    };
+
+    /** Power on Modem
+     */
+    void preInit(void);
+
+    /** check serialModem is readable or not
+     *	@returns
+     *		true on readable
+     *		false on not readable
+     */
+    bool readable();
+
+    /** read one byte from serialModem
+     *	@returns
+     *		one byte read from serialModem
+     */
+    char readByte(void);
+
+    /** read from Modem module and save to buffer array
+     *  @param  buffer	buffer array to save what read from Modem module
+     *  @param  count 	the maximal bytes number read from Modem module
+     *  @param  timeOut	time to wait for reading from Modem module
+     *  @returns
+     *      0 on success
+     *      -1 on error
+     */
+    int readBuffer(char* buffer,int count, unsigned int timeOut);
+
+
+    /** clean Buffer
+     *	@param buffer	buffer to clean
+     *	@param count	number of bytes to clean
+     */
+    void cleanBuffer(char* buffer, int count);
+
+    /** send AT command to Modem module
+     *  @param cmd	command array which will be send to GPRS module
+     */
+    void sendCmd(const char* cmd);
+
+    /**send "AT" to Modem module
+     */
+    void sendATTest(void);
+
+    /**	compare the response from GPRS module with a string
+     *	@param resp	buffer to be compared
+     *	@param len length that will be compared
+     *	@param timeout	waiting seconds till timeout
+     */
+    bool respCmp(const char *resp, unsigned int len, unsigned int timeout);
+
+    /** check Modem module response before time out
+     *  @param  *resp   correct response which Modem module will return
+     *  @param  *timeout    waiting seconds till timeout
+     *  @returns
+     *      0 on success
+     *      -1 on error
+     */
+    int waitForResp(const char *resp, unsigned int timeout,DataType type);
+
+    /** send AT command to GPRS module and wait for correct response
+     *  @param  *cmd 	AT command which will be send to GPRS module
+     *  @param  *resp   correct response which GPRS module will return
+     *  @param  *timeout 	waiting seconds till timeout
+     *  @returns
+     *      0 on success
+     *      -1 on error
+     */
+    int sendCmdAndWaitForResp(const char* data, const char *resp, unsigned timeout,DataType type);
+
+
+    /** used for serial debug, you can specify tx and rx pin and then communicate with GPRS module with common AT commands
+     */
+    void serialDebug(void);
+
+    Serial serialModem;
+    Timer timeCnt;
+
+private:
+
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPRSInterface.cpp	Tue Feb 25 02:52:48 2014 +0000
@@ -0,0 +1,51 @@
+/*
+  GPRSInterface.cpp
+  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
+
+  Author:lawliet zou(lawliet.zou@gmail.com)
+  2014-2-24
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#include "GPRSInterface.h"
+
+GPRSInterface::GPRSInterface(PinName tx, PinName rx, int baudRate, const char* apn, const char* userName, const char *passWord) : GPRS(tx,rx,baudRate,apn,userName,passWord)
+{
+    ip_set = false;
+}
+
+int GPRSInterface::init()
+{
+    reset();
+    return 0;
+}
+
+int GPRSInterface::connect()
+{
+    return GPRS::join();
+}
+
+int GPRSInterface::disconnect()
+{
+    return GPRS::disconnect();
+}
+
+char* GPRSInterface::getIPAddress()
+{
+    snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (_ip>>24)&0xff,(_ip>>16)&0xff,(_ip>>8)&0xff,_ip&0xff); 
+    ip_set = true;
+    return ip_string;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPRSInterface.h	Tue Feb 25 02:52:48 2014 +0000
@@ -0,0 +1,72 @@
+/*
+  GPRSInterface.h
+  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
+
+  Author:lawliet zou(lawliet.zou@gmail.com)
+  2014-2-24
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+#ifndef __GPRSINTERFACE_H__
+
+#include "GPRS.h"
+#include "TCPSocketConnection.h"
+#include "TCPSocketServer.h"
+
+/** Interface using GPRS to connect to an IP-based network
+ *
+ */
+class GPRSInterface: public GPRS
+{
+
+public:
+
+    /** Constructor
+     *  @param tx mbed pin to use for tx line of Serial interface
+     *  @param rx mbed pin to use for rx line of Serial interface
+     *  @param baudRate serial communicate baud rate
+     *  @param apn name of the gateway for GPRS to connect to the network
+     *  @param userName apn's username, usually is NULL
+     *  @param passWord apn's password, usually is NULL
+     */
+    GPRSInterface(PinName tx, PinName rx, int baudRate, const char* apn, const char* userName = NULL, const char *passWord = NULL);
+
+    /** Initialize the interface(no connection at this point).
+     *  @return 0 on success, a negative number on failure
+     */
+    int init();
+
+    /** Connect to the network and get IP address
+     *  @returns 0 on success, a negative number on failure
+     */
+    int connect();
+
+    /** Disconnect with the network
+     *  @returns 0 on success, a negative number on failure
+     */
+    int disconnect();
+
+    /** Get IP address
+     *  @returns ip address
+     */
+    char* getIPAddress();
+
+private:
+
+    char ip_string[20];
+    bool ip_set;
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/Endpoint.cpp	Tue Feb 25 02:52:48 2014 +0000
@@ -0,0 +1,61 @@
+/*
+  Endpoint.cpp
+  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
+
+  Author:lawliet zou(lawliet.zou@gmail.com)
+  2014-2-24
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+#include "Socket/Socket.h"
+#include "Socket/Endpoint.h"
+
+Endpoint::Endpoint()
+{
+    reset_address();
+}
+Endpoint::~Endpoint() {}
+
+void Endpoint::reset_address(void)
+{
+    _ipAddress[0] = '\0';
+    _port = 0;
+}
+
+int Endpoint::set_address(const char* host, const int port)
+{
+    //Resolve DNS address or populate hard-coded IP address
+    GPRS* gprs = GPRS::getInstance();
+    if (gprs == NULL) {
+        return -1;
+    }
+    uint32_t addr;
+    if (!gprs->gethostbyname(host, &addr)) {
+        return -1;
+    }
+    snprintf(_ipAddress, sizeof(_ipAddress), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
+    _port = port;
+    return 0;
+}
+
+char* Endpoint::get_address()
+{
+    return _ipAddress;
+}
+
+int Endpoint::get_port()
+{
+    return _port;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/Endpoint.h	Tue Feb 25 02:52:48 2014 +0000
@@ -0,0 +1,68 @@
+/*
+  Endpoint.h
+  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
+
+  Author:lawliet zou(lawliet.zou@gmail.com)
+  2014-2-24
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+#ifndef ENDPOINT_H
+#define ENDPOINT_H
+
+#include "GPRS.h"
+
+class UDPSocket;
+
+/**
+IP Endpoint (address, port)
+*/
+class Endpoint {
+    friend class UDPSocket;
+
+public:
+    /** IP Endpoint (address, port)
+     */
+    Endpoint(void);
+    
+    ~Endpoint(void);
+    
+    /** Reset the address of this endpoint
+     */
+    void reset_address(void);
+    
+    /** Set the address of this endpoint
+    \param host The endpoint address (it can either be an IP Address or a hostname that will be resolved with DNS).
+    \param port The endpoint port
+    \return 0 on success, -1 on failure (when an hostname cannot be resolved by DNS).
+     */
+    int  set_address(const char* host, const int port);
+    
+    /** Get the IP address of this endpoint
+    \return The IP address of this endpoint.
+     */
+    char* get_address(void);
+    
+    /** Get the port of this endpoint
+    \return The port of this endpoint
+     */
+    int get_port(void);
+
+protected:
+    char _ipAddress[16];
+    int _port;
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/Socket.cpp	Tue Feb 25 02:52:48 2014 +0000
@@ -0,0 +1,43 @@
+/*
+  Socket.cpp
+  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
+
+  Author:lawliet zou(lawliet.zou@gmail.com)
+  2014-2-24
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+ 
+#include "Socket.h"
+
+Socket::Socket() : _sock_fd(-1),_blocking(true), _timeout(1500) {
+    gprs = GPRS::getInstance();
+    if (gprs == NULL) {
+        error("Socket constructor error: no WIZ8820io instance available!\r\n");
+    }       
+}
+
+void Socket::set_blocking(bool blocking, unsigned int timeout) {
+    _blocking = blocking;
+    _timeout = timeout;
+}
+
+int Socket::close() {
+    return (gprs->close(_sock_fd)) ? 0 : -1;
+}
+
+Socket::~Socket() {
+    close(); //Don't want to leak
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/Socket.h	Tue Feb 25 02:52:48 2014 +0000
@@ -0,0 +1,56 @@
+/*
+  Socket.h
+  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
+
+  Author:lawliet zou(lawliet.zou@gmail.com)
+  2014-2-24
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+#ifndef SOCKET_H_
+#define SOCKET_H_
+
+#include "GPRS.h"
+
+/** Socket file descriptor and select wrapper
+  */
+class Socket {
+public:
+    /** Socket
+     */
+    Socket();
+    
+    /** Set blocking or non-blocking mode of the socket and a timeout on
+        blocking socket operations
+    \param blocking  true for blocking mode, false for non-blocking mode.
+    \param timeout   timeout in ms [Default: (1500)ms].
+    */
+    void set_blocking(bool blocking, unsigned int timeout=1500);
+    
+    /** Close the socket file descriptor
+     */
+    int close();
+    
+    ~Socket();
+    
+protected:
+    int _sock_fd;
+    bool _blocking;
+    int _timeout;
+    GPRS* gprs;
+};
+
+
+#endif /* SOCKET_H_ */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/TCPSocketConnection.cpp	Tue Feb 25 02:52:48 2014 +0000
@@ -0,0 +1,89 @@
+/*
+  TCPSocketConnection.cpp
+  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
+
+  Author:lawliet zou(lawliet.zou@gmail.com)
+  2014-2-24
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#include "TCPSocketConnection.h"
+
+static int size;
+TCPSocketConnection::TCPSocketConnection()
+{
+    size = 0;
+}
+
+int TCPSocketConnection::connect(const char* host, const int port)
+{
+    if (_sock_fd < 0) {
+        _sock_fd = gprs->new_socket();
+        if (_sock_fd < 0) {
+            return -1;
+        }
+    }
+
+    if (!gprs->connect(_sock_fd, TCP, host, port)) {
+        return false;
+    }
+    return true;
+}
+
+bool TCPSocketConnection::is_connected(void)
+{
+    return gprs->is_connected(_sock_fd);
+}
+
+int TCPSocketConnection::send(char* data, int length)
+{
+    int size = gprs->wait_writeable(_sock_fd, length);
+    if (size < 0) {
+        return -1;
+    }
+    if (size > length) {
+        size = length;
+    }
+    return gprs->send(_sock_fd, data, size);
+}
+
+int TCPSocketConnection::send_all(char* data, int length)
+{
+    return send(data,length);
+}
+
+int TCPSocketConnection::receive(char* data, int length)
+{
+    if (size < 0) {
+        return -1;
+    }
+    if(size == 0) {
+        size = gprs->wait_readable(_sock_fd, DEFAULT_TIMEOUT);
+    }
+
+    if(size > length) {
+        size = size - length;
+    } else {
+        length = size;
+        size = -1;
+    }
+    return gprs->recv(_sock_fd, data, length);
+}
+
+int TCPSocketConnection::receive_all(char* data, int length)
+{
+    return receive(data,length);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/TCPSocketConnection.h	Tue Feb 25 02:52:48 2014 +0000
@@ -0,0 +1,82 @@
+/*
+  TCPSocketConnection.h
+  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
+
+  Author:lawliet zou(lawliet.zou@gmail.com)
+  2014-2-24
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#ifndef TCPSOCKET_H
+#define TCPSOCKET_H
+
+#include "Socket.h"
+#include "Endpoint.h"
+
+/**
+TCP socket connection
+*/
+class TCPSocketConnection: public Socket, public Endpoint
+{
+    friend class TCPSocketServer;
+
+public:
+    /** TCP socket connection
+    */
+    TCPSocketConnection();
+
+    /** Connects this TCP socket to the server
+    \param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS.
+    \param port The host's port to connect to.
+    \return 0 on success, -1 on failure.
+    */
+    int connect(const char* host, const int port);
+
+    /** Check if the socket is connected
+    \return true if connected, false otherwise.
+    */
+    bool is_connected(void);
+
+    /** Send data to the remote host.
+    \param data The buffer to send to the host.
+    \param length The length of the buffer to send.
+    \return the number of written bytes on success (>=0) or -1 on failure
+     */
+    int send(char* data, int length);
+
+    /** Send all the data to the remote host.
+    \param data The buffer to send to the host.
+    \param length The length of the buffer to send.
+    \return the number of written bytes on success (>=0) or -1 on failure
+    */
+    int send_all(char* data, int length);
+
+    /** Receive data from the remote host.
+    \param data The buffer in which to store the data received from the host.
+    \param length The maximum length of the buffer.
+    \return the number of received bytes on success (>=0) or -1 on failure
+     */
+    int receive(char* data, int length);
+
+    /** Receive all the data from the remote host.
+    \param data The buffer in which to store the data received from the host.
+    \param length The maximum length of the buffer.
+    \return the number of received bytes on success (>=0) or -1 on failure
+    */
+    int receive_all(char* data, int length);
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Socket/TCPSocketServer.h	Tue Feb 25 02:52:48 2014 +0000
@@ -0,0 +1,57 @@
+/*
+  TCPSocketServer.h
+  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
+
+  Author:lawliet zou(lawliet.zou@gmail.com)
+  2014-2-24
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+#ifndef TCPSOCKETSERVER_H
+#define TCPSOCKETSERVER_H
+
+#include "Socket/Socket.h"
+#include "TCPSocketConnection.h"
+
+/** TCP Server.
+  */
+class TCPSocketServer : public Socket
+{
+public:
+    /** Instantiate a TCP Server.
+    */
+    TCPSocketServer();
+
+    /** Bind a socket to a specific port.
+    \param port The port to listen for incoming connections on.
+    \return 0 on success, -1 on failure.
+    */
+    int bind(int port);
+
+    /** Start listening for incoming connections.
+    \param backlog number of pending connections that can be queued up at any
+                   one time [Default: 1].
+    \return 0 on success, -1 on failure.
+    */
+    int listen(int backlog=1);
+
+    /** Accept a new connection.
+    \param connection A TCPSocketConnection instance that will handle the incoming connection.
+    \return 0 on success, -1 on failure.
+    */
+    int accept(TCPSocketConnection& connection);
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pico_string.h	Tue Feb 25 02:52:48 2014 +0000
@@ -0,0 +1,70 @@
+/*
+  pico_string.h
+  2014 Copyright (c) Seeed Technology Inc.  All right reserved.
+
+  Author:lawliet zou(lawliet.zou@gmail.com)
+  2014-2-24
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public
+  License along with this library; if not, write to the Free Software
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+#ifndef __PICO_STRING_H__
+
+class pico_string {
+
+public:
+    pico_string(){
+        _len = 0;
+        _buf = (char*)malloc(1);
+        if (_buf) {
+            _buf[0] = '\0';
+        }
+    }
+    ~pico_string() {
+        if (_buf) {
+            free(_buf);
+        }
+    }
+    bool empty() {
+        return _len == 0;
+    }
+    void append(const char* s, int len) {
+        if (_buf == NULL) {
+            return;
+        }
+        char* p = (char*)malloc(_len+len+1);
+        if (p == NULL) {
+            return;
+        }
+        memcpy(p, _buf, _len);
+        memcpy(p+_len, s, len);
+        p[_len+len] = '\0';
+        free(_buf);
+        _buf = p;
+    }
+    void append(const char* s) {
+        append(s, strlen(s));
+    }
+    char* c_str() {
+        if (_buf) {
+            return _buf;
+        }
+        return "";
+    }
+private:
+    char* _buf;
+    int _len;
+};
+
+#endif