|
5 | 5 | #include <Client.h> |
6 | 6 | #include <Stream.h> |
7 | 7 |
|
8 | | -#include "system.h" |
| 8 | +extern "C" { |
| 9 | +#include "lwmqtt/lwmqtt.h" |
| 10 | +}; |
| 11 | + |
| 12 | +typedef struct { |
| 13 | + uint32_t end; |
| 14 | +} lwmqtt_arduino_timer_t; |
| 15 | + |
| 16 | +void lwmqtt_arduino_timer_set(void *ref, uint32_t timeout); |
| 17 | + |
| 18 | +int32_t lwmqtt_arduino_timer_get(void *ref); |
| 19 | + |
| 20 | +typedef struct { |
| 21 | + Client *client; |
| 22 | +} lwmqtt_arduino_network_t; |
| 23 | + |
| 24 | +void lwmqtt_arduino_timer_set(void *ref, uint32_t timeout) { |
| 25 | + // cast timer reference |
| 26 | + auto t = (lwmqtt_arduino_timer_t *)ref; |
| 27 | + |
| 28 | + // set future end time |
| 29 | + t->end = (uint32_t)(millis() + timeout); |
| 30 | +} |
| 31 | + |
| 32 | +int32_t lwmqtt_arduino_timer_get(void *ref) { |
| 33 | + // cast timer reference |
| 34 | + auto t = (lwmqtt_arduino_timer_t *)ref; |
| 35 | + |
| 36 | + // get difference to end time |
| 37 | + return (int32_t)t->end - (int32_t)millis(); |
| 38 | +} |
| 39 | + |
| 40 | +lwmqtt_err_t lwmqtt_arduino_network_read(void *ref, uint8_t *buffer, size_t len, size_t *read, uint32_t timeout) { |
| 41 | + // cast network reference |
| 42 | + auto n = (lwmqtt_arduino_network_t *)ref; |
| 43 | + |
| 44 | + // set timeout |
| 45 | + n->client->setTimeout(timeout); |
| 46 | + |
| 47 | + // read bytes |
| 48 | + *read = n->client->readBytes(buffer, len); |
| 49 | + if (*read <= 0) { |
| 50 | + return LWMQTT_NETWORK_FAILED_READ; |
| 51 | + } |
| 52 | + |
| 53 | + return LWMQTT_SUCCESS; |
| 54 | +} |
| 55 | + |
| 56 | +lwmqtt_err_t lwmqtt_arduino_network_write(void *ref, uint8_t *buffer, size_t len, size_t *sent, uint32_t /*timeout*/) { |
| 57 | + // cast network reference |
| 58 | + auto n = (lwmqtt_arduino_network_t *)ref; |
| 59 | + |
| 60 | + // write bytes |
| 61 | + *sent = n->client->write(buffer, len); |
| 62 | + if (*sent <= 0) { |
| 63 | + return LWMQTT_NETWORK_FAILED_WRITE; |
| 64 | + }; |
| 65 | + |
| 66 | + return LWMQTT_SUCCESS; |
| 67 | +} |
9 | 68 |
|
10 | 69 | class MQTTClient; |
11 | 70 |
|
|
0 commit comments