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

Dependencies:   HTTPClient-SSL

Dependents:   mtsas mtsas mtsas mtsas_lat3

Committer:
Vanger
Date:
Mon Aug 11 15:43:15 2014 +0000
Revision:
16:dbe80ac199f5
Parent:
8:a3b41ec82e63
Child:
21:faff2c3248ec
Removed some unecessary wait() calls in TestTCPSocketConnection.h and TestUDPSocketConnection.h; Changed TestTCPSocketConnection.h to look for whole menu before moving on, rather than just first menu line.

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 {
Mike Fiore 1:096f484f3ae6 15 if (!ip->open(host, port, IPStack::TCP)) {
Mike Fiore 1:096f484f3ae6 16 return -1;
Mike Fiore 1:096f484f3ae6 17 }
Mike Fiore 1:096f484f3ae6 18 return 0;
Mike Fiore 1:096f484f3ae6 19 }
Mike Fiore 1:096f484f3ae6 20
Mike Fiore 1:096f484f3ae6 21 bool TCPSocketConnection::is_connected(void)
Mike Fiore 1:096f484f3ae6 22 {
Mike Fiore 1:096f484f3ae6 23 return ip->isOpen();
Mike Fiore 1:096f484f3ae6 24 }
Mike Fiore 1:096f484f3ae6 25
Mike Fiore 1:096f484f3ae6 26 int TCPSocketConnection::send(char* data, int length)
Mike Fiore 1:096f484f3ae6 27 {
Mike Fiore 1:096f484f3ae6 28 Timer tmr;
Mike Fiore 1:096f484f3ae6 29
Mike Fiore 1:096f484f3ae6 30 if (!_blocking) {
Mike Fiore 1:096f484f3ae6 31 tmr.start();
Mike Fiore 1:096f484f3ae6 32 while (tmr.read_ms() < _timeout) {
Mike Fiore 1:096f484f3ae6 33 if (ip->writeable())
Mike Fiore 1:096f484f3ae6 34 break;
Mike Fiore 1:096f484f3ae6 35 }
Mike Fiore 1:096f484f3ae6 36 if (tmr.read_ms() >= _timeout) {
Mike Fiore 1:096f484f3ae6 37 return -1;
Mike Fiore 1:096f484f3ae6 38 }
Mike Fiore 1:096f484f3ae6 39 }
Mike Fiore 1:096f484f3ae6 40 return ip->write(data, length, 0);
Mike Fiore 1:096f484f3ae6 41 }
Mike Fiore 1:096f484f3ae6 42
Mike Fiore 1:096f484f3ae6 43 // -1 if unsuccessful, else number of bytes written
Mike Fiore 1:096f484f3ae6 44 int TCPSocketConnection::send_all(char* data, int length)
Mike Fiore 1:096f484f3ae6 45 {
Mike Fiore 1:096f484f3ae6 46 if (_blocking) {
Mike Fiore 1:096f484f3ae6 47 return ip->write(data, length, -1);
Mike Fiore 1:096f484f3ae6 48 } else {
Mike Fiore 1:096f484f3ae6 49 return ip->write(data, length, _timeout);
Mike Fiore 1:096f484f3ae6 50 }
Mike Fiore 1:096f484f3ae6 51 }
Mike Fiore 1:096f484f3ae6 52
Mike Fiore 1:096f484f3ae6 53 // -1 if unsuccessful, else number of bytes received
Mike Fiore 1:096f484f3ae6 54 int TCPSocketConnection::receive(char* data, int length)
Mike Fiore 1:096f484f3ae6 55 {
Mike Fiore 1:096f484f3ae6 56 Timer tmr;
mfiore 8:a3b41ec82e63 57 int bytes = 0;
mfiore 8:a3b41ec82e63 58 int totalbytes = 0;
Vanger 16:dbe80ac199f5 59
mfiore 8:a3b41ec82e63 60 if (_blocking) {
mfiore 8:a3b41ec82e63 61 return ip->read(data, length, _timeout);
mfiore 8:a3b41ec82e63 62 } else {
Mike Fiore 1:096f484f3ae6 63 tmr.start();
mfiore 8:a3b41ec82e63 64 do {
mfiore 8:a3b41ec82e63 65 bytes = ip->read(data + totalbytes, length - totalbytes, 250);
mfiore 8:a3b41ec82e63 66 if (bytes < 0) {
mfiore 8:a3b41ec82e63 67 return -1;
Mike Fiore 1:096f484f3ae6 68 }
mfiore 8:a3b41ec82e63 69 totalbytes += bytes;
mfiore 8:a3b41ec82e63 70 } while ( tmr.read() < _timeout && totalbytes < length);
mfiore 8:a3b41ec82e63 71 return totalbytes;
Mike Fiore 1:096f484f3ae6 72 }
Mike Fiore 1:096f484f3ae6 73 }
Mike Fiore 1:096f484f3ae6 74
Mike Fiore 1:096f484f3ae6 75 // -1 if unsuccessful, else number of bytes received
Mike Fiore 1:096f484f3ae6 76 int TCPSocketConnection::receive_all(char* data, int length)
Mike Fiore 1:096f484f3ae6 77 {
Mike Fiore 1:096f484f3ae6 78 if (_blocking) {
Mike Fiore 1:096f484f3ae6 79 return ip->read(data, length, -1);
Mike Fiore 1:096f484f3ae6 80 } else {
Mike Fiore 1:096f484f3ae6 81 return ip->read(data, length, _timeout);
Mike Fiore 1:096f484f3ae6 82 }
Mike Fiore 1:096f484f3ae6 83 }