|
| 1 | +// Copyright (c) 2016 by B. Runnels and T. von Eicken |
| 2 | + |
| 3 | +#include <Arduino.h> |
1 | 4 | #include "ELClientMqtt.h"
|
2 | 5 |
|
| 6 | +// constructor |
3 | 7 | ELClientMqtt::ELClientMqtt(ELClient* elc) :_elc(elc) {}
|
4 | 8 |
|
5 |
| -boolean ELClientMqtt::init() { |
6 |
| - uint16_t crc; |
7 |
| - uint32_t cb_data; |
8 |
| - |
9 |
| - crc = _elc->Request(CMD_MQTT_INIT, 0, 1, 4); |
10 |
| - |
11 |
| - cb_data = (uint32_t)&connectedCb; |
12 |
| - crc = _elc->Request(crc, (uint8_t*)&cb_data, 4); |
13 |
| - |
14 |
| - cb_data = (uint32_t)&disconnectedCb; |
15 |
| - crc = _elc->Request(crc, (uint8_t*)&cb_data, 4); |
16 |
| - |
17 |
| - cb_data = (uint32_t)&publishedCb; |
18 |
| - crc = _elc->Request(crc, (uint8_t*)&cb_data, 4); |
19 |
| - |
20 |
| - cb_data = (uint32_t)&dataCb; |
21 |
| - crc = _elc->Request(crc, (uint8_t*)&cb_data, 4); |
22 |
| - |
23 |
| - _elc->Request(crc); |
24 |
| - |
25 |
| - if (_elc->WaitReturn() == false || _elc->return_cmd == 0 || _elc->return_value == 0) |
26 |
| - return false; |
27 |
| - remote_instance = _elc->return_value; |
28 |
| - return true; |
29 |
| -} |
30 |
| - |
31 |
| -boolean ELClientMqtt::lwt(const char* topic, const char* message, uint8_t qos, uint8_t retain) { |
32 |
| - uint16_t crc; |
33 |
| - |
34 |
| - crc = _elc->Request(CMD_MQTT_LWT, 0, 1, 5); |
35 |
| - crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4); |
36 |
| - crc = _elc->Request(crc, (uint8_t*)topic, strlen(topic)); |
37 |
| - crc = _elc->Request(crc, (uint8_t*)message, strlen(message)); |
38 |
| - crc = _elc->Request(crc, (uint8_t*)&qos, 1); |
39 |
| - crc = _elc->Request(crc, (uint8_t*)&retain, 1); |
40 |
| - _elc->Request(crc); |
41 |
| - if (_elc->WaitReturn() && _elc->return_value) |
42 |
| - return true; |
43 |
| - return false; |
44 |
| -} |
45 |
| - |
46 |
| -boolean ELClientMqtt::lwt(const __FlashStringHelper* topic, const __FlashStringHelper* message, uint8_t qos, uint8_t retain) { |
47 |
| - uint16_t crc; |
48 |
| - |
49 |
| - crc = _elc->Request(CMD_MQTT_LWT, 0, 1, 5); |
50 |
| - crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4); |
51 |
| - crc = _elc->Request(crc, topic, strlen_P((const char*)topic)); |
52 |
| - crc = _elc->Request(crc, message, strlen_P((const char*)message)); |
53 |
| - crc = _elc->Request(crc, (uint8_t*)&qos, 1); |
54 |
| - crc = _elc->Request(crc, (uint8_t*)&retain, 1); |
55 |
| - _elc->Request(crc); |
56 |
| - if (_elc->WaitReturn() && _elc->return_value) |
57 |
| - return true; |
58 |
| - return false; |
59 |
| -} |
60 |
| - |
61 |
| -boolean ELClientMqtt::lwt(const char* topic, const char* message) { |
62 |
| - return lwt(topic, message, 0, 0); |
63 |
| -} |
64 |
| - |
65 |
| -boolean ELClientMqtt::lwt(const __FlashStringHelper* topic, const __FlashStringHelper* message) { |
66 |
| - return lwt(topic, message, 0, 0); |
67 |
| -} |
68 |
| - |
69 |
| -//void MQTT::connect(const char* host, uint32_t port, boolean security) { |
70 |
| -// uint16_t crc; |
71 |
| -// crc = _elc->Request(CMD_MQTT_CONNECT, 0, 0, 4); |
72 |
| -// crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4); |
73 |
| -// crc = _elc->Request(crc, (uint8_t*)host, strlen(host)); |
74 |
| -// crc = _elc->Request(crc, (uint8_t*)&port, 4); |
75 |
| -// crc = _elc->Request(crc, (uint8_t*)&security, 1); |
76 |
| -// _elc->Request(crc); |
77 |
| -//} |
78 |
| -// |
79 |
| -//void MQTT::connect(const __FlashStringHelper* host, uint32_t port, boolean security) { |
80 |
| -// uint16_t crc; |
81 |
| -// crc = _elc->Request(CMD_MQTT_CONNECT, 0, 0, 4); |
82 |
| -// crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4); |
83 |
| -// crc = _elc->Request(crc, host, strlen_P((const char*)host)); |
84 |
| -// crc = _elc->Request(crc, (uint8_t*)&port, 4); |
85 |
| -// crc = _elc->Request(crc, (uint8_t*)&security, 1); |
86 |
| -// _elc->Request(crc); |
87 |
| -//} |
88 |
| -// |
89 |
| -//void MQTT::connect(const char* host, uint32_t port) { |
90 |
| -// connect(host, port, false); |
91 |
| -//} |
92 |
| -// |
93 |
| -//void MQTT::connect(const __FlashStringHelper* host, uint32_t port) { |
94 |
| -// connect(host, port, false); |
95 |
| -//} |
96 |
| - |
97 |
| -//void MQTT::disconnect() { |
98 |
| -// uint16_t crc; |
99 |
| -// crc = _elc->Request(CMD_MQTT_DISCONNECT, 0, 0, 1); |
100 |
| -// crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4); |
101 |
| -// _elc->Request(crc); |
102 |
| -//} |
| 9 | +// setup -- push callbacks to esp-link |
| 10 | +void ELClientMqtt::setup(void) { |
| 11 | + Serial.print("ConnectedCB is 0x"); Serial.println((uint32_t)&connectedCb, 16); |
| 12 | + _elc->Request(CMD_MQTT_SETUP, 0, 4); |
| 13 | + uint32_t cb = (uint32_t)&connectedCb; |
| 14 | + _elc->Request(&cb, 4); |
| 15 | + cb = (uint32_t)&disconnectedCb; |
| 16 | + _elc->Request(&cb, 4); |
| 17 | + cb = (uint32_t)&publishedCb; |
| 18 | + _elc->Request(&cb, 4); |
| 19 | + cb = (uint32_t)&dataCb; |
| 20 | + _elc->Request(&cb, 4); |
| 21 | + _elc->Request(); |
| 22 | +} |
| 23 | + |
| 24 | +// LWT |
| 25 | + |
| 26 | +void ELClientMqtt::lwt(const char* topic, const char* message, uint8_t qos, uint8_t retain) { |
| 27 | + _elc->Request(CMD_MQTT_LWT, 0, 4); |
| 28 | + _elc->Request(topic, strlen(topic)); |
| 29 | + _elc->Request(message, strlen(message)); |
| 30 | + _elc->Request(&qos, 1); |
| 31 | + _elc->Request(&retain, 1); |
| 32 | + _elc->Request(); |
| 33 | +} |
| 34 | + |
| 35 | +void ELClientMqtt::lwt(const __FlashStringHelper* topic, const __FlashStringHelper* message, |
| 36 | + uint8_t qos, uint8_t retain) |
| 37 | +{ |
| 38 | + _elc->Request(CMD_MQTT_LWT, 0, 4); |
| 39 | + _elc->Request(topic, strlen_P((const char*)topic)); |
| 40 | + _elc->Request(message, strlen_P((const char*)message)); |
| 41 | + _elc->Request(&qos, 1); |
| 42 | + _elc->Request(&retain, 1); |
| 43 | + _elc->Request(); |
| 44 | +} |
| 45 | + |
| 46 | +// SUBSCRIBE |
103 | 47 |
|
104 | 48 | void ELClientMqtt::subscribe(const char* topic, uint8_t qos) {
|
105 |
| - uint16_t crc; |
106 |
| - crc = _elc->Request(CMD_MQTT_SUBSCRIBE, 0, 0, 3); |
107 |
| - crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4); |
108 |
| - crc = _elc->Request(crc, (uint8_t*)topic, strlen(topic)); |
109 |
| - crc = _elc->Request(crc, (uint8_t*)&qos, 1); |
110 |
| - _elc->Request(crc); |
| 49 | + _elc->Request(CMD_MQTT_SUBSCRIBE, 0, 2); |
| 50 | + _elc->Request(topic, strlen(topic)); |
| 51 | + _elc->Request(&qos, 1); |
| 52 | + _elc->Request(); |
111 | 53 | }
|
112 | 54 |
|
113 | 55 | void ELClientMqtt::subscribe(const __FlashStringHelper* topic, uint8_t qos) {
|
114 |
| - uint16_t crc; |
115 |
| - crc = _elc->Request(CMD_MQTT_SUBSCRIBE, 0, 0, 3); |
116 |
| - crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4); |
117 |
| - crc = _elc->Request(crc, topic, strlen_P((const char*)topic)); |
118 |
| - crc = _elc->Request(crc, (uint8_t*)&qos, 1); |
119 |
| - _elc->Request(crc); |
120 |
| -} |
121 |
| - |
122 |
| -void ELClientMqtt::subscribe(const char* topic) { |
123 |
| - subscribe(topic, 0); |
124 |
| -} |
125 |
| - |
126 |
| -void ELClientMqtt::subscribe(const __FlashStringHelper* topic) { |
127 |
| - subscribe(topic, 0); |
128 |
| -} |
129 |
| - |
130 |
| -void ELClientMqtt::publish(const char* topic, const uint8_t* data, const uint16_t len, uint8_t qos, uint8_t retain) { |
131 |
| - uint16_t crc; |
132 |
| - crc = _elc->Request(CMD_MQTT_PUBLISH, 0, 0, 6); |
133 |
| - crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4); |
134 |
| - crc = _elc->Request(crc, (uint8_t*)topic, strlen(topic)); |
135 |
| - crc = _elc->Request(crc, (uint8_t*)data, len); |
136 |
| - crc = _elc->Request(crc, (uint8_t*)&len, 2); |
137 |
| - crc = _elc->Request(crc, (uint8_t*)&qos, 1); |
138 |
| - crc = _elc->Request(crc, (uint8_t*)&retain, 1); |
139 |
| - _elc->Request(crc); |
140 |
| -} |
141 |
| - |
142 |
| -void ELClientMqtt::publish(const __FlashStringHelper* topic, const __FlashStringHelper* data, const uint16_t len, uint8_t qos, uint8_t retain) { |
143 |
| - uint16_t crc; |
144 |
| - crc = _elc->Request(CMD_MQTT_PUBLISH, 0, 0, 6); |
145 |
| - crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4); |
146 |
| - crc = _elc->Request(crc, topic, strlen_P((const char*)topic)); |
147 |
| - crc = _elc->Request(crc, data, len); |
148 |
| - crc = _elc->Request(crc, (uint8_t*)&len, 2); |
149 |
| - crc = _elc->Request(crc, (uint8_t*)&qos, 1); |
150 |
| - crc = _elc->Request(crc, (uint8_t*)&retain, 1); |
151 |
| - _elc->Request(crc); |
152 |
| -} |
153 |
| - |
154 |
| -void ELClientMqtt::publish(const char* topic, const __FlashStringHelper* data, const uint16_t len, uint8_t qos, uint8_t retain) { |
155 |
| - uint16_t crc; |
156 |
| - crc = _elc->Request(CMD_MQTT_PUBLISH, 0, 0, 6); |
157 |
| - crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4); |
158 |
| - crc = _elc->Request(crc, (uint8_t*)topic, strlen(topic)); |
159 |
| - crc = _elc->Request(crc, data, len); |
160 |
| - crc = _elc->Request(crc, (uint8_t*)&len, 2); |
161 |
| - crc = _elc->Request(crc, (uint8_t*)&qos, 1); |
162 |
| - crc = _elc->Request(crc, (uint8_t*)&retain, 1); |
163 |
| - _elc->Request(crc); |
164 |
| -} |
165 |
| - |
166 |
| -void ELClientMqtt::publish(const __FlashStringHelper* topic, const uint8_t* data, const uint16_t len, uint8_t qos, uint8_t retain) { |
167 |
| - uint16_t crc; |
168 |
| - crc = _elc->Request(CMD_MQTT_PUBLISH, 0, 0, 6); |
169 |
| - crc = _elc->Request(crc, (uint8_t*)&remote_instance, 4); |
170 |
| - crc = _elc->Request(crc, topic, strlen_P((const char*)topic)); |
171 |
| - crc = _elc->Request(crc, (uint8_t*)data, len); |
172 |
| - crc = _elc->Request(crc, (uint8_t*)&len, 2); |
173 |
| - crc = _elc->Request(crc, (uint8_t*)&qos, 1); |
174 |
| - crc = _elc->Request(crc, (uint8_t*)&retain, 1); |
175 |
| - _elc->Request(crc); |
| 56 | + _elc->Request(CMD_MQTT_SUBSCRIBE, 0, 2); |
| 57 | + _elc->Request(topic, strlen_P((const char*)topic)); |
| 58 | + _elc->Request(&qos, 1); |
| 59 | + _elc->Request(); |
| 60 | +} |
| 61 | + |
| 62 | +// PUBLISH |
| 63 | + |
| 64 | +void ELClientMqtt::publish(const char* topic, const uint8_t* data, const uint16_t len, |
| 65 | + uint8_t qos, uint8_t retain) |
| 66 | +{ |
| 67 | + _elc->Request(CMD_MQTT_PUBLISH, 0, 5); |
| 68 | + _elc->Request(topic, strlen(topic)); |
| 69 | + _elc->Request(data, len); |
| 70 | + _elc->Request(&len, 2); |
| 71 | + _elc->Request(&qos, 1); |
| 72 | + _elc->Request(&retain, 1); |
| 73 | + _elc->Request(); |
| 74 | +} |
| 75 | + |
| 76 | +void ELClientMqtt::publish(const char* topic, const char* data, uint8_t qos, uint8_t retain) |
| 77 | +{ |
| 78 | + publish(topic, (uint8_t*)data, strlen(data), qos, retain); |
| 79 | +} |
| 80 | + |
| 81 | +void ELClientMqtt::publish(const __FlashStringHelper* topic, const __FlashStringHelper* data, |
| 82 | + const uint16_t len, uint8_t qos, uint8_t retain) |
| 83 | +{ |
| 84 | + _elc->Request(CMD_MQTT_PUBLISH, 0, 5); |
| 85 | + _elc->Request(topic, strlen_P((const char*)topic)); |
| 86 | + _elc->Request(data, len); |
| 87 | + _elc->Request(&len, 2); |
| 88 | + _elc->Request(&qos, 1); |
| 89 | + _elc->Request(&retain, 1); |
| 90 | + _elc->Request(); |
| 91 | +} |
| 92 | + |
| 93 | +void ELClientMqtt::publish(const char* topic, const __FlashStringHelper* data, |
| 94 | + const uint16_t len, uint8_t qos, uint8_t retain) |
| 95 | +{ |
| 96 | + _elc->Request(CMD_MQTT_PUBLISH, 0, 5); |
| 97 | + _elc->Request(topic, strlen(topic)); |
| 98 | + _elc->Request(data, len); |
| 99 | + _elc->Request(&len, 2); |
| 100 | + _elc->Request(&qos, 1); |
| 101 | + _elc->Request(&retain, 1); |
| 102 | + _elc->Request(); |
| 103 | +} |
| 104 | + |
| 105 | +void ELClientMqtt::publish(const __FlashStringHelper* topic, const uint8_t* data, |
| 106 | + const uint16_t len, uint8_t qos, uint8_t retain) |
| 107 | +{ |
| 108 | + _elc->Request(CMD_MQTT_PUBLISH, 0, 5); |
| 109 | + _elc->Request(topic, strlen_P((const char*)topic)); |
| 110 | + _elc->Request(data, len); |
| 111 | + _elc->Request(&len, 2); |
| 112 | + _elc->Request(&qos, 1); |
| 113 | + _elc->Request(&retain, 1); |
| 114 | + _elc->Request(); |
176 | 115 | }
|
177 |
| - |
178 |
| -void ELClientMqtt::publish(const char* topic, const char* data, uint8_t qos, uint8_t retain) { |
179 |
| - publish(topic, (const uint8_t*)data, strlen(data), qos, retain); |
180 |
| -} |
181 |
| - |
182 |
| -void ELClientMqtt::publish(const char* topic, const char* data) { |
183 |
| - publish(topic, (const uint8_t*)data, strlen(data), 0, 0); |
184 |
| -} |
185 |
| - |
186 |
| -void ELClientMqtt::publish(const __FlashStringHelper* topic, const __FlashStringHelper* data) { |
187 |
| - publish(topic, data, strlen_P((const char*)data), 0, 0); |
188 |
| -} |
189 |
| - |
190 |
| -void ELClientMqtt::publish(const char* topic, const __FlashStringHelper* data) { |
191 |
| - publish(topic, data, strlen_P((const char*)data), 0, 0); |
192 |
| -} |
193 |
| - |
194 |
| -void ELClientMqtt::publish(const __FlashStringHelper* topic, const char* data) { |
195 |
| - publish(topic, (const uint8_t*)data, strlen(data), 0, 0); |
196 |
| -} |
197 |
| - |
0 commit comments