M2XStreamClient fork with a workaround in M2XStreamClient.cpp for the MTS_Wifi_Connect_M2X example.
Dependents: STM32_MTS_Wifi_Connect_M2X M2X_STM32_MTS_Temp MTS_WiFi_Connect_M2X_Example
Fork of M2XStreamClient by
Client.cpp@2:6fbc104d1321, 2014-08-26 (annotated)
- Committer:
- joe_tijerina
- Date:
- Tue Aug 26 15:20:40 2014 +0000
- Revision:
- 2:6fbc104d1321
- Parent:
- 0:f479e4f4db0e
Fixed compiler warning (added ifndef MIN to avoid defining duplicate MIN define)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jb8414 | 0:f479e4f4db0e | 1 | #include "Client.h" |
jb8414 | 0:f479e4f4db0e | 2 | #include "mbed.h" |
jb8414 | 0:f479e4f4db0e | 3 | |
jb8414 | 0:f479e4f4db0e | 4 | #include <stdint.h> |
jb8414 | 0:f479e4f4db0e | 5 | |
jb8414 | 0:f479e4f4db0e | 6 | Client::Client() : _len(0), _sock() { |
jb8414 | 0:f479e4f4db0e | 7 | } |
jb8414 | 0:f479e4f4db0e | 8 | |
jb8414 | 0:f479e4f4db0e | 9 | Client::~Client() { |
jb8414 | 0:f479e4f4db0e | 10 | } |
jb8414 | 0:f479e4f4db0e | 11 | |
jb8414 | 0:f479e4f4db0e | 12 | int Client::connect(const char *host, uint16_t port) { |
jb8414 | 0:f479e4f4db0e | 13 | return _sock.connect(host, port) == 0; |
jb8414 | 0:f479e4f4db0e | 14 | } |
jb8414 | 0:f479e4f4db0e | 15 | |
jb8414 | 0:f479e4f4db0e | 16 | size_t Client::write(uint8_t b) { |
jb8414 | 0:f479e4f4db0e | 17 | return write(&b, 1); |
jb8414 | 0:f479e4f4db0e | 18 | } |
jb8414 | 0:f479e4f4db0e | 19 | |
jb8414 | 0:f479e4f4db0e | 20 | size_t Client::write(const uint8_t *buf, size_t size) { |
jb8414 | 0:f479e4f4db0e | 21 | _sock.set_blocking(false, 15000); |
jb8414 | 0:f479e4f4db0e | 22 | // NOTE: we know it's dangerous to cast from (const uint8_t *) to (char *), |
jb8414 | 0:f479e4f4db0e | 23 | // but we are trying to maintain a stable interface between the Arduino |
jb8414 | 0:f479e4f4db0e | 24 | // one and the mbed one. What's more, while TCPSocketConnection has no |
jb8414 | 0:f479e4f4db0e | 25 | // intention of modifying the data here, it requires us to send a (char *) |
jb8414 | 0:f479e4f4db0e | 26 | // typed data. So we belive it's safe to do the cast here. |
jb8414 | 0:f479e4f4db0e | 27 | return _sock.send_all(const_cast<char*>((const char*) buf), size); |
jb8414 | 0:f479e4f4db0e | 28 | } |
jb8414 | 0:f479e4f4db0e | 29 | |
jb8414 | 0:f479e4f4db0e | 30 | int Client::available() { |
jb8414 | 0:f479e4f4db0e | 31 | if (_len > 0) { return 1; } |
jb8414 | 0:f479e4f4db0e | 32 | int ret = read(_buf, 1); |
jb8414 | 0:f479e4f4db0e | 33 | if (ret <= 0) { return 0; } |
jb8414 | 0:f479e4f4db0e | 34 | _len = ret; |
jb8414 | 0:f479e4f4db0e | 35 | return 1; |
jb8414 | 0:f479e4f4db0e | 36 | } |
jb8414 | 0:f479e4f4db0e | 37 | |
jb8414 | 0:f479e4f4db0e | 38 | int Client::read() { |
jb8414 | 0:f479e4f4db0e | 39 | if (_len > 0) { |
jb8414 | 0:f479e4f4db0e | 40 | _len = 0; |
jb8414 | 0:f479e4f4db0e | 41 | return _buf[0]; |
jb8414 | 0:f479e4f4db0e | 42 | } |
jb8414 | 0:f479e4f4db0e | 43 | return -1; |
jb8414 | 0:f479e4f4db0e | 44 | } |
jb8414 | 0:f479e4f4db0e | 45 | |
jb8414 | 0:f479e4f4db0e | 46 | int Client::read(uint8_t *buf, size_t size) { |
jb8414 | 0:f479e4f4db0e | 47 | return _sock.receive_all((char*) buf, size); |
jb8414 | 0:f479e4f4db0e | 48 | } |
jb8414 | 0:f479e4f4db0e | 49 | |
jb8414 | 0:f479e4f4db0e | 50 | void Client::flush() { |
jb8414 | 0:f479e4f4db0e | 51 | // does nothing, TCP stack takes care of this |
jb8414 | 0:f479e4f4db0e | 52 | } |
jb8414 | 0:f479e4f4db0e | 53 | |
jb8414 | 0:f479e4f4db0e | 54 | void Client::stop() { |
jb8414 | 0:f479e4f4db0e | 55 | _sock.close(); |
jb8414 | 0:f479e4f4db0e | 56 | } |
jb8414 | 0:f479e4f4db0e | 57 | |
jb8414 | 0:f479e4f4db0e | 58 | uint8_t Client::connected() { |
jb8414 | 0:f479e4f4db0e | 59 | return _sock.is_connected(); |
jb8414 | 0:f479e4f4db0e | 60 | } |