Socket library for MTS Socket Modem Arduino Shield devices from Multi-Tech Systems

Dependencies:   HTTPClient-SSL

Dependents:   mtsas mtsas mtsas mtsas_lat3

Committer:
mfiore
Date:
Thu Jun 25 13:56:41 2015 +0000
Revision:
43:2b78cadc0894
Parent:
22:966373d2930c
update to MultiTech official version of HTTPClient-SSL library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mike Fiore 2:ebc6129de4e8 1 #include "mbed.h"
Mike Fiore 1:096f484f3ae6 2 #include "TCPSocketConnection.h"
Mike Fiore 1:096f484f3ae6 3 #include <algorithm>
Mike Fiore 1:096f484f3ae6 4
mfiore 8:a3b41ec82e63 5 #include "MTSLog.h"
mfiore 8:a3b41ec82e63 6
mfiore 8:a3b41ec82e63 7 using namespace mts;
mfiore 8:a3b41ec82e63 8
Mike Fiore 1:096f484f3ae6 9 TCPSocketConnection::TCPSocketConnection()
Mike Fiore 1:096f484f3ae6 10 {
Mike Fiore 1:096f484f3ae6 11 }
Mike Fiore 1:096f484f3ae6 12
Mike Fiore 1:096f484f3ae6 13 int TCPSocketConnection::connect(const char* host, const int port)
Mike Fiore 1:096f484f3ae6 14 {
Vanger 21:faff2c3248ec 15 if(!ip) {
Vanger 22:966373d2930c 16 logError("IPstack object pointer is NULL");
Vanger 21:faff2c3248ec 17 return -1;
Vanger 21:faff2c3248ec 18 }
Mike Fiore 1:096f484f3ae6 19 if (!ip->open(host, port, IPStack::TCP)) {
Mike Fiore 1:096f484f3ae6 20 return -1;
Mike Fiore 1:096f484f3ae6 21 }
Mike Fiore 1:096f484f3ae6 22 return 0;
Mike Fiore 1:096f484f3ae6 23 }
Mike Fiore 1:096f484f3ae6 24
Mike Fiore 1:096f484f3ae6 25 bool TCPSocketConnection::is_connected(void)
Mike Fiore 1:096f484f3ae6 26 {
Mike Fiore 1:096f484f3ae6 27 return ip->isOpen();
Mike Fiore 1:096f484f3ae6 28 }
Mike Fiore 1:096f484f3ae6 29
Mike Fiore 1:096f484f3ae6 30 int TCPSocketConnection::send(char* data, int length)
Mike Fiore 1:096f484f3ae6 31 {
Mike Fiore 1:096f484f3ae6 32 Timer tmr;
Mike Fiore 1:096f484f3ae6 33
Mike Fiore 1:096f484f3ae6 34 if (!_blocking) {
Mike Fiore 1:096f484f3ae6 35 tmr.start();
Mike Fiore 1:096f484f3ae6 36 while (tmr.read_ms() < _timeout) {
Mike Fiore 1:096f484f3ae6 37 if (ip->writeable())
Mike Fiore 1:096f484f3ae6 38 break;
Mike Fiore 1:096f484f3ae6 39 }
Mike Fiore 1:096f484f3ae6 40 if (tmr.read_ms() >= _timeout) {
Mike Fiore 1:096f484f3ae6 41 return -1;
Mike Fiore 1:096f484f3ae6 42 }
Mike Fiore 1:096f484f3ae6 43 }
Mike Fiore 1:096f484f3ae6 44 return ip->write(data, length, 0);
Mike Fiore 1:096f484f3ae6 45 }
Mike Fiore 1:096f484f3ae6 46
Mike Fiore 1:096f484f3ae6 47 // -1 if unsuccessful, else number of bytes written
Mike Fiore 1:096f484f3ae6 48 int TCPSocketConnection::send_all(char* data, int length)
Mike Fiore 1:096f484f3ae6 49 {
Mike Fiore 1:096f484f3ae6 50 if (_blocking) {
Mike Fiore 1:096f484f3ae6 51 return ip->write(data, length, -1);
Mike Fiore 1:096f484f3ae6 52 } else {
Mike Fiore 1:096f484f3ae6 53 return ip->write(data, length, _timeout);
Mike Fiore 1:096f484f3ae6 54 }
Mike Fiore 1:096f484f3ae6 55 }
Mike Fiore 1:096f484f3ae6 56
Mike Fiore 1:096f484f3ae6 57 // -1 if unsuccessful, else number of bytes received
Mike Fiore 1:096f484f3ae6 58 int TCPSocketConnection::receive(char* data, int length)
Mike Fiore 1:096f484f3ae6 59 {
Mike Fiore 1:096f484f3ae6 60 Timer tmr;
mfiore 8:a3b41ec82e63 61 int bytes = 0;
mfiore 8:a3b41ec82e63 62 int totalbytes = 0;
Vanger 16:dbe80ac199f5 63
mfiore 8:a3b41ec82e63 64 if (_blocking) {
mfiore 8:a3b41ec82e63 65 return ip->read(data, length, _timeout);
mfiore 8:a3b41ec82e63 66 } else {
Mike Fiore 1:096f484f3ae6 67 tmr.start();
mfiore 8:a3b41ec82e63 68 do {
mfiore 8:a3b41ec82e63 69 bytes = ip->read(data + totalbytes, length - totalbytes, 250);
mfiore 8:a3b41ec82e63 70 if (bytes < 0) {
mfiore 8:a3b41ec82e63 71 return -1;
Mike Fiore 1:096f484f3ae6 72 }
mfiore 8:a3b41ec82e63 73 totalbytes += bytes;
mfiore 8:a3b41ec82e63 74 } while ( tmr.read() < _timeout && totalbytes < length);
mfiore 8:a3b41ec82e63 75 return totalbytes;
Mike Fiore 1:096f484f3ae6 76 }
Mike Fiore 1:096f484f3ae6 77 }
Mike Fiore 1:096f484f3ae6 78
Mike Fiore 1:096f484f3ae6 79 // -1 if unsuccessful, else number of bytes received
Mike Fiore 1:096f484f3ae6 80 int TCPSocketConnection::receive_all(char* data, int length)
Mike Fiore 1:096f484f3ae6 81 {
Mike Fiore 1:096f484f3ae6 82 if (_blocking) {
Mike Fiore 1:096f484f3ae6 83 return ip->read(data, length, -1);
Mike Fiore 1:096f484f3ae6 84 } else {
Mike Fiore 1:096f484f3ae6 85 return ip->read(data, length, _timeout);
Mike Fiore 1:096f484f3ae6 86 }
Mike Fiore 1:096f484f3ae6 87 }