a library to use GPRS like ethernet or wifi, which makes it possible to connect to the internet with your GPRS module

Dependencies:   BufferedSerial

Dependents:   ThinkSpeak_Test roam_v1 roam_v2 finalv3

Fork of GPRSInterface by wei zou

Committer:
Yihui Xiong
Date:
Fri Apr 03 15:44:04 2015 +0800
Revision:
13:379ce1d51b88
Parent:
9:38800611a613
rework

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lawliet 0:8ccbd963e74d 1 /*
lawliet 0:8ccbd963e74d 2 TCPSocketConnection.cpp
lawliet 0:8ccbd963e74d 3 2014 Copyright (c) Seeed Technology Inc. All right reserved.
lawliet 0:8ccbd963e74d 4
lawliet 0:8ccbd963e74d 5 Author:lawliet zou(lawliet.zou@gmail.com)
lawliet 0:8ccbd963e74d 6 2014-2-24
lawliet 0:8ccbd963e74d 7
lawliet 0:8ccbd963e74d 8 This library is free software; you can redistribute it and/or
lawliet 0:8ccbd963e74d 9 modify it under the terms of the GNU Lesser General Public
lawliet 0:8ccbd963e74d 10 License as published by the Free Software Foundation; either
lawliet 0:8ccbd963e74d 11 version 2.1 of the License, or (at your option) any later version.
lawliet 0:8ccbd963e74d 12
lawliet 0:8ccbd963e74d 13 This library is distributed in the hope that it will be useful,
lawliet 0:8ccbd963e74d 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
lawliet 0:8ccbd963e74d 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
lawliet 0:8ccbd963e74d 16 Lesser General Public License for more details.
lawliet 0:8ccbd963e74d 17
lawliet 0:8ccbd963e74d 18 You should have received a copy of the GNU Lesser General Public
lawliet 0:8ccbd963e74d 19 License along with this library; if not, write to the Free Software
lawliet 0:8ccbd963e74d 20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
lawliet 0:8ccbd963e74d 21 */
lawliet 0:8ccbd963e74d 22
lawliet 0:8ccbd963e74d 23 #include "TCPSocketConnection.h"
lawliet 0:8ccbd963e74d 24
lawliet 0:8ccbd963e74d 25 TCPSocketConnection::TCPSocketConnection()
lawliet 0:8ccbd963e74d 26 {
lawliet 0:8ccbd963e74d 27 }
lawliet 0:8ccbd963e74d 28
lawliet 0:8ccbd963e74d 29 int TCPSocketConnection::connect(const char* host, const int port)
lawliet 0:8ccbd963e74d 30 {
lawliet 0:8ccbd963e74d 31 if (_sock_fd < 0) {
lawliet 0:8ccbd963e74d 32 _sock_fd = gprs->new_socket();
lawliet 0:8ccbd963e74d 33 if (_sock_fd < 0) {
lawliet 0:8ccbd963e74d 34 return -1;
lawliet 0:8ccbd963e74d 35 }
lawliet 0:8ccbd963e74d 36 }
lawliet 0:8ccbd963e74d 37
lawliet 0:8ccbd963e74d 38 if (!gprs->connect(_sock_fd, TCP, host, port)) {
yihui 9:38800611a613 39 return -1;
lawliet 0:8ccbd963e74d 40 }
yihui 9:38800611a613 41 return 0;
lawliet 0:8ccbd963e74d 42 }
lawliet 0:8ccbd963e74d 43
lawliet 0:8ccbd963e74d 44 bool TCPSocketConnection::is_connected(void)
lawliet 0:8ccbd963e74d 45 {
yihui 9:38800611a613 46 if (_sock_fd < 0) {
yihui 9:38800611a613 47 return false;
yihui 9:38800611a613 48 }
lawliet 0:8ccbd963e74d 49 return gprs->is_connected(_sock_fd);
lawliet 0:8ccbd963e74d 50 }
lawliet 0:8ccbd963e74d 51
lawliet 0:8ccbd963e74d 52 int TCPSocketConnection::send(char* data, int length)
lawliet 0:8ccbd963e74d 53 {
Yihui Xiong 13:379ce1d51b88 54 return gprs->sock_send(_sock_fd, data, length);
lawliet 0:8ccbd963e74d 55 }
lawliet 0:8ccbd963e74d 56
lawliet 0:8ccbd963e74d 57 int TCPSocketConnection::send_all(char* data, int length)
lawliet 0:8ccbd963e74d 58 {
Yihui Xiong 13:379ce1d51b88 59 return send(data, length);
lawliet 0:8ccbd963e74d 60 }
lawliet 0:8ccbd963e74d 61
lawliet 0:8ccbd963e74d 62 int TCPSocketConnection::receive(char* data, int length)
lawliet 0:8ccbd963e74d 63 {
Yihui Xiong 13:379ce1d51b88 64 return gprs->sock_recv(_sock_fd, data, length);
lawliet 0:8ccbd963e74d 65 }
lawliet 0:8ccbd963e74d 66
lawliet 0:8ccbd963e74d 67 int TCPSocketConnection::receive_all(char* data, int length)
lawliet 0:8ccbd963e74d 68 {
lawliet 0:8ccbd963e74d 69 return receive(data,length);
lawliet 0:8ccbd963e74d 70 }