copy lib

Fork of GMMP by SKTelecom_ThingPlug

Committer:
lesmin
Date:
Thu Jul 23 00:55:15 2015 +0000
Revision:
0:32c20cfb2b30
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lesmin 0:32c20cfb2b30 1 #include "Client.h"
lesmin 0:32c20cfb2b30 2 #include "mbed.h"
lesmin 0:32c20cfb2b30 3
lesmin 0:32c20cfb2b30 4 #include <stdint.h>
lesmin 0:32c20cfb2b30 5
lesmin 0:32c20cfb2b30 6 Client::Client() : _len(0), _sock() {
lesmin 0:32c20cfb2b30 7 }
lesmin 0:32c20cfb2b30 8
lesmin 0:32c20cfb2b30 9 Client::~Client() {
lesmin 0:32c20cfb2b30 10 }
lesmin 0:32c20cfb2b30 11
lesmin 0:32c20cfb2b30 12 int Client::connect(const char *host, uint16_t port) {
lesmin 0:32c20cfb2b30 13 return _sock.connect(host, port) == 0;
lesmin 0:32c20cfb2b30 14 }
lesmin 0:32c20cfb2b30 15
lesmin 0:32c20cfb2b30 16 size_t Client::write(uint8_t b) {
lesmin 0:32c20cfb2b30 17 //return write(&b, 1);
lesmin 0:32c20cfb2b30 18 return write((char*)&b, 1); //lesmin
lesmin 0:32c20cfb2b30 19 }
lesmin 0:32c20cfb2b30 20
lesmin 0:32c20cfb2b30 21 //size_t Client::write(const uint8_t *buf, size_t size) {
lesmin 0:32c20cfb2b30 22 size_t Client::write(char *buf, size_t size) {
lesmin 0:32c20cfb2b30 23 _sock.set_blocking(false, 15000);
lesmin 0:32c20cfb2b30 24 // NOTE: we know it's dangerous to cast from (const uint8_t *) to (char *),
lesmin 0:32c20cfb2b30 25 // but we are trying to maintain a stable interface between the Arduino
lesmin 0:32c20cfb2b30 26 // one and the mbed one. What's more, while TCPSocketConnection has no
lesmin 0:32c20cfb2b30 27 // intention of modifying the data here, it requires us to send a (char *)
lesmin 0:32c20cfb2b30 28 // typed data. So we belive it's safe to do the cast here.
lesmin 0:32c20cfb2b30 29 //return _sock.send_all(const_cast<char*>((const char*) buf), size);
lesmin 0:32c20cfb2b30 30 return _sock.send_all(buf, size);
lesmin 0:32c20cfb2b30 31 }
lesmin 0:32c20cfb2b30 32
lesmin 0:32c20cfb2b30 33 int Client::available() {
lesmin 0:32c20cfb2b30 34 if (_len > 0) { return 1; }
lesmin 0:32c20cfb2b30 35 int ret = read(_buf, 1);
lesmin 0:32c20cfb2b30 36 if (ret <= 0) { return 0; }
lesmin 0:32c20cfb2b30 37 _len = ret;
lesmin 0:32c20cfb2b30 38 return 1;
lesmin 0:32c20cfb2b30 39 }
lesmin 0:32c20cfb2b30 40
lesmin 0:32c20cfb2b30 41 int Client::read() {
lesmin 0:32c20cfb2b30 42 if (_len > 0) {
lesmin 0:32c20cfb2b30 43 _len = 0;
lesmin 0:32c20cfb2b30 44 return _buf[0];
lesmin 0:32c20cfb2b30 45 }
lesmin 0:32c20cfb2b30 46 return -1;
lesmin 0:32c20cfb2b30 47 }
lesmin 0:32c20cfb2b30 48
lesmin 0:32c20cfb2b30 49 int Client::read(uint8_t *buf, size_t size) {
lesmin 0:32c20cfb2b30 50 return _sock.receive_all((char*) buf, size);
lesmin 0:32c20cfb2b30 51 }
lesmin 0:32c20cfb2b30 52
lesmin 0:32c20cfb2b30 53 void Client::flush() {
lesmin 0:32c20cfb2b30 54 // does nothing, TCP stack takes care of this
lesmin 0:32c20cfb2b30 55 }
lesmin 0:32c20cfb2b30 56
lesmin 0:32c20cfb2b30 57 void Client::stop() {
lesmin 0:32c20cfb2b30 58 _sock.close();
lesmin 0:32c20cfb2b30 59 }
lesmin 0:32c20cfb2b30 60
lesmin 0:32c20cfb2b30 61 uint8_t Client::connected() {
lesmin 0:32c20cfb2b30 62 return _sock.is_connected();
lesmin 0:32c20cfb2b30 63 }