Home Alert System
Dependencies: PWM_Tone_Library DHT
M2XStreamClient/Client.cpp@5:569a4894abc1, 2019-03-08 (annotated)
- Committer:
- aziz111
- Date:
- Fri Mar 08 17:15:02 2019 +0000
- Revision:
- 5:569a4894abc1
- Parent:
- 3:78f223d34f36
Final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ethaderu | 3:78f223d34f36 | 1 | #include "Client.h" |
ethaderu | 3:78f223d34f36 | 2 | #include "mbed.h" |
ethaderu | 3:78f223d34f36 | 3 | |
ethaderu | 3:78f223d34f36 | 4 | #include <stdint.h> |
ethaderu | 3:78f223d34f36 | 5 | |
ethaderu | 3:78f223d34f36 | 6 | Client::Client() : _incnt(0), _outcnt(0), _sock() { |
ethaderu | 3:78f223d34f36 | 7 | _sock.set_blocking(false, 1500); |
ethaderu | 3:78f223d34f36 | 8 | } |
ethaderu | 3:78f223d34f36 | 9 | |
ethaderu | 3:78f223d34f36 | 10 | Client::~Client() { |
ethaderu | 3:78f223d34f36 | 11 | } |
ethaderu | 3:78f223d34f36 | 12 | |
ethaderu | 3:78f223d34f36 | 13 | int Client::connect(const char *host, uint16_t port) { |
ethaderu | 3:78f223d34f36 | 14 | return _sock.connect(host, port) == 0; |
ethaderu | 3:78f223d34f36 | 15 | } |
ethaderu | 3:78f223d34f36 | 16 | |
ethaderu | 3:78f223d34f36 | 17 | size_t Client::write(uint8_t b) { |
ethaderu | 3:78f223d34f36 | 18 | return write(&b, 1); |
ethaderu | 3:78f223d34f36 | 19 | } |
ethaderu | 3:78f223d34f36 | 20 | |
ethaderu | 3:78f223d34f36 | 21 | size_t Client::write(const uint8_t *buf, size_t size) { |
ethaderu | 3:78f223d34f36 | 22 | size_t cnt = 0; |
ethaderu | 3:78f223d34f36 | 23 | while (size) { |
ethaderu | 3:78f223d34f36 | 24 | int tmp = sizeof(_outbuf) - _outcnt; |
ethaderu | 3:78f223d34f36 | 25 | if (tmp > size) tmp = size; |
ethaderu | 3:78f223d34f36 | 26 | memcpy(_outbuf + _outcnt, buf, tmp); |
ethaderu | 3:78f223d34f36 | 27 | _outcnt += tmp; |
ethaderu | 3:78f223d34f36 | 28 | buf += tmp; |
ethaderu | 3:78f223d34f36 | 29 | size -= tmp; |
ethaderu | 3:78f223d34f36 | 30 | cnt += tmp; |
ethaderu | 3:78f223d34f36 | 31 | // if no space flush it |
ethaderu | 3:78f223d34f36 | 32 | if (_outcnt == sizeof(_outbuf)) |
ethaderu | 3:78f223d34f36 | 33 | _flushout(); |
ethaderu | 3:78f223d34f36 | 34 | } |
ethaderu | 3:78f223d34f36 | 35 | return cnt; |
ethaderu | 3:78f223d34f36 | 36 | } |
ethaderu | 3:78f223d34f36 | 37 | |
ethaderu | 3:78f223d34f36 | 38 | void Client::_flushout(void) |
ethaderu | 3:78f223d34f36 | 39 | { |
ethaderu | 3:78f223d34f36 | 40 | if (_outcnt > 0) { |
ethaderu | 3:78f223d34f36 | 41 | // NOTE: we know it's dangerous to cast from (const uint8_t *) to (char *), |
ethaderu | 3:78f223d34f36 | 42 | // but we are trying to maintain a stable interface between the Arduino |
ethaderu | 3:78f223d34f36 | 43 | // one and the mbed one. What's more, while TCPSocketConnection has no |
ethaderu | 3:78f223d34f36 | 44 | // intention of modifying the data here, it requires us to send a (char *) |
ethaderu | 3:78f223d34f36 | 45 | // typed data. So we belive it's safe to do the cast here. |
ethaderu | 3:78f223d34f36 | 46 | _sock.send_all(const_cast<char*>((const char*) _outbuf), _outcnt); |
ethaderu | 3:78f223d34f36 | 47 | _outcnt = 0; |
ethaderu | 3:78f223d34f36 | 48 | } |
ethaderu | 3:78f223d34f36 | 49 | } |
ethaderu | 3:78f223d34f36 | 50 | |
ethaderu | 3:78f223d34f36 | 51 | void Client::_fillin(void) |
ethaderu | 3:78f223d34f36 | 52 | { |
ethaderu | 3:78f223d34f36 | 53 | int tmp = sizeof(_inbuf) - _incnt; |
ethaderu | 3:78f223d34f36 | 54 | if (tmp) { |
ethaderu | 3:78f223d34f36 | 55 | tmp = _sock.receive_all((char*)_inbuf + _incnt, tmp); |
ethaderu | 3:78f223d34f36 | 56 | if (tmp > 0) |
ethaderu | 3:78f223d34f36 | 57 | _incnt += tmp; |
ethaderu | 3:78f223d34f36 | 58 | } |
ethaderu | 3:78f223d34f36 | 59 | } |
ethaderu | 3:78f223d34f36 | 60 | |
ethaderu | 3:78f223d34f36 | 61 | void Client::flush() { |
ethaderu | 3:78f223d34f36 | 62 | _flushout(); |
ethaderu | 3:78f223d34f36 | 63 | } |
ethaderu | 3:78f223d34f36 | 64 | |
ethaderu | 3:78f223d34f36 | 65 | int Client::available() { |
ethaderu | 3:78f223d34f36 | 66 | if (_incnt == 0) { |
ethaderu | 3:78f223d34f36 | 67 | _flushout(); |
ethaderu | 3:78f223d34f36 | 68 | _fillin(); |
ethaderu | 3:78f223d34f36 | 69 | } |
ethaderu | 3:78f223d34f36 | 70 | return (_incnt > 0) ? 1 : 0; |
ethaderu | 3:78f223d34f36 | 71 | } |
ethaderu | 3:78f223d34f36 | 72 | |
ethaderu | 3:78f223d34f36 | 73 | int Client::read() { |
ethaderu | 3:78f223d34f36 | 74 | uint8_t ch; |
ethaderu | 3:78f223d34f36 | 75 | return (read(&ch, 1) == 1) ? ch : -1; |
ethaderu | 3:78f223d34f36 | 76 | } |
ethaderu | 3:78f223d34f36 | 77 | |
ethaderu | 3:78f223d34f36 | 78 | int Client::read(uint8_t *buf, size_t size) { |
ethaderu | 3:78f223d34f36 | 79 | int cnt = 0; |
ethaderu | 3:78f223d34f36 | 80 | while (size) { |
ethaderu | 3:78f223d34f36 | 81 | // need more |
ethaderu | 3:78f223d34f36 | 82 | if (size > _incnt) { |
ethaderu | 3:78f223d34f36 | 83 | _flushout(); |
ethaderu | 3:78f223d34f36 | 84 | _fillin(); |
ethaderu | 3:78f223d34f36 | 85 | } |
ethaderu | 3:78f223d34f36 | 86 | if (_incnt > 0) { |
ethaderu | 3:78f223d34f36 | 87 | int tmp = _incnt; |
ethaderu | 3:78f223d34f36 | 88 | if (tmp > size) tmp = size; |
ethaderu | 3:78f223d34f36 | 89 | memcpy(buf, _inbuf, tmp); |
ethaderu | 3:78f223d34f36 | 90 | if (tmp != _incnt) |
ethaderu | 3:78f223d34f36 | 91 | memmove(_inbuf, _inbuf + tmp, _incnt - tmp); |
ethaderu | 3:78f223d34f36 | 92 | _incnt -= tmp; |
ethaderu | 3:78f223d34f36 | 93 | size -= tmp; |
ethaderu | 3:78f223d34f36 | 94 | buf += tmp; |
ethaderu | 3:78f223d34f36 | 95 | cnt += tmp; |
ethaderu | 3:78f223d34f36 | 96 | } else // no data |
ethaderu | 3:78f223d34f36 | 97 | break; |
ethaderu | 3:78f223d34f36 | 98 | } |
ethaderu | 3:78f223d34f36 | 99 | return cnt; |
ethaderu | 3:78f223d34f36 | 100 | } |
ethaderu | 3:78f223d34f36 | 101 | |
ethaderu | 3:78f223d34f36 | 102 | void Client::stop() { |
ethaderu | 3:78f223d34f36 | 103 | _sock.close(); |
ethaderu | 3:78f223d34f36 | 104 | } |
ethaderu | 3:78f223d34f36 | 105 | |
ethaderu | 3:78f223d34f36 | 106 | uint8_t Client::connected() { |
ethaderu | 3:78f223d34f36 | 107 | return _sock.is_connected() ? 1 : 0; |
ethaderu | 3:78f223d34f36 | 108 | } |