-
Notifications
You must be signed in to change notification settings - Fork 9
/
homie.c
345 lines (284 loc) · 9.9 KB
/
homie.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <stdarg.h>
#include "esp_event.h"
#include "esp_log.h"
#include "esp_ota_ops.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "freertos/event_groups.h"
#include "homie.h"
#include "ota.h"
static const char *TAG = "HOMIE";
static esp_mqtt_client_handle_t client;
static homie_config_t *config;
static void homie_connected();
static bool _starts_with(const char *pre, const char *str, int lenstr)
{
size_t lenpre = strlen(pre);
return lenstr < lenpre ? false : strncmp(pre, str, lenpre) == 0;
}
#define REMOTE_LOGGING_MAX_PAYLOAD_LEN 1024
static int _homie_logger(const char *str, va_list l)
{
char buf[REMOTE_LOGGING_MAX_PAYLOAD_LEN];
vsnprintf(buf, REMOTE_LOGGING_MAX_PAYLOAD_LEN, str, l);
homie_publish("log", 1, 0, buf, 0);
return vprintf(str, l);
}
static void homie_handle_mqtt_event(esp_mqtt_event_handle_t event)
{
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
printf("DATA=%.*s\r\n", event->data_len, event->data);
// Check if it is reboot command
char topic[HOMIE_MAX_TOPIC_LEN];
homie_mktopic(topic, "$implementation/reboot");
if ((strncmp(topic, event->topic, event->topic_len) == 0) && (strncmp("true", event->data, event->data_len) == 0))
{
ESP_LOGI(TAG, "Rebooting...");
esp_restart();
return;
}
// Check if it is enable remote console
homie_mktopic(topic, "$implementation/logging");
if (strncmp(topic, event->topic, event->topic_len) == 0)
{
if (strncmp("true", event->data, event->data_len) == 0)
{
ESP_LOGI(TAG, "Enable remote logging");
esp_log_set_vprintf(_homie_logger);
ESP_LOGI(TAG, "Remote logging enabled");
}
else
{
ESP_LOGI(TAG, "Disable remote logging");
esp_log_set_vprintf(vprintf);
ESP_LOGI(TAG, "Remote logging disabled");
}
return;
}
// Check if it is a OTA update
homie_mktopic(topic, "$implementation/ota/url");
if (_starts_with(topic, event->topic, event->topic_len))
{
char *url = calloc(1, event->data_len + 1);
strncpy(url, event->data, event->data_len);
url[event->data_len] = '\0';
ota_init(url, config->cert_pem, config->ota_status_handler);
return;
}
// Call the application's handler
homie_mktopic(topic, "");
if (config->msg_handler)
{
int subtopic_len = event->topic_len - strlen(topic);
char *subtopic = calloc(1, subtopic_len + 1);
strncpy(subtopic, event->topic + strlen(topic), subtopic_len);
subtopic[subtopic_len] = '\0';
char *payload = calloc(1, event->data_len + 1);
strncpy(payload, event->data, event->data_len);
payload[event->data_len] = '\0';
config->msg_handler(subtopic, payload);
free(subtopic);
free(payload);
}
}
static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
{
switch (event->event_id)
{
case MQTT_EVENT_BEFORE_CONNECT:
ESP_LOGI(TAG, "MQTT_EVENT_BEFORE_CONNECT");
break;
case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
homie_connected();
if (config->connected_handler)
config->connected_handler();
break;
case MQTT_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
if (config->disconnected_handler)
config->disconnected_handler();
break;
case MQTT_EVENT_SUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_UNSUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_PUBLISHED:
// Disabled to avoid triggering circular events with remote logging
// ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_DATA:
ESP_LOGI(TAG, "MQTT_EVENT_DATA");
homie_handle_mqtt_event(event);
break;
case MQTT_EVENT_ERROR:
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
break;
case MQTT_EVENT_ANY:
ESP_LOGI(TAG, "MQTT_EVENT_ANY");
break;
case MQTT_EVENT_DELETED:
ESP_LOGI(TAG, "MQTT_EVENT_DELETED");
break;
}
return ESP_OK;
}
static void mqtt_app_start(void)
{
char *lwt_topic = calloc(1, HOMIE_MAX_TOPIC_LEN);
homie_mktopic(lwt_topic, "$online");
esp_mqtt_client_config_t mqtt_cfg = {
.client_id = config->client_id,
.uri = config->mqtt_uri,
.username = config->mqtt_username,
.password = config->mqtt_password,
.event_handle = mqtt_event_handler,
.lwt_msg = "false",
.lwt_retain = 1,
.lwt_topic = lwt_topic,
.keepalive = 15,
.cert_pem = config->cert_pem,
};
client = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_start(client);
}
void homie_mktopic(char *topic, const char *subtopic)
{
snprintf(topic, HOMIE_MAX_TOPIC_LEN, "%s/%s/%s", config->base_topic, config->client_id, subtopic);
}
void homie_subscribe(const char *subtopic)
{
int msg_id;
char topic[HOMIE_MAX_TOPIC_LEN];
homie_mktopic(topic, subtopic);
msg_id = esp_mqtt_client_subscribe(client, topic, 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
}
int homie_publish(const char *subtopic, int qos, int retain, const char *payload, int len)
{
if (config == NULL)
{
ESP_LOGI(TAG, "Attempted to publish before homie connected");
return -2;
}
// int msg_id;
char topic[HOMIE_MAX_TOPIC_LEN];
homie_mktopic(topic, subtopic);
return esp_mqtt_client_publish(client, topic, payload, len, qos, retain);
}
int homie_publishf(const char *subtopic, int qos, int retain, const char *format, ...)
{
char payload_string[64];
va_list argptr;
va_start(argptr, format);
vsnprintf(payload_string, 64, format, argptr);
va_end(argptr);
return homie_publish(subtopic, qos, retain, payload_string, 0);
}
int homie_publish_int(const char *subtopic, int qos, int retain, int payload)
{
char payload_string[16];
snprintf(payload_string, 16, "%d", payload);
return homie_publish(subtopic, qos, retain, payload_string, 0);
}
int homie_publish_bool(const char *subtopic, int qos, int retain, bool payload)
{
return homie_publish(subtopic, qos, retain, payload ? "true" : "false", 0);
}
static int _clamp(int n, int lower, int upper)
{
return n <= lower ? lower : n >= upper ? upper : n;
}
static int8_t _get_wifi_rssi()
{
wifi_ap_record_t info;
if (!esp_wifi_sta_get_ap_info(&info))
{
return info.rssi;
}
return 0;
}
static void _get_ip(char *ip_string)
{
tcpip_adapter_ip_info_t ip;
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip);
sprintf(ip_string, "%u.%u.%u.%u", (ip.ip.addr & 0x000000ff), (ip.ip.addr & 0x0000ff00) >> 8,
(ip.ip.addr & 0x00ff0000) >> 16, (ip.ip.addr & 0xff000000) >> 24);
}
static void _get_mac(char *mac_string, bool sep)
{
// NB: This is the base mac of the device. The actual wifi and eth MAC addresses
// will be assigned as offsets from this.
uint8_t mac[6];
esp_efuse_mac_get_default(mac);
if (sep)
sprintf(mac_string, "%X:%X:%X:%X:%X:%X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
else
sprintf(mac_string, "%x%x%x%x%x%x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
static void homie_connected()
{
char mac_address[18];
char ip_address[16];
_get_mac(mac_address, true);
_get_ip(ip_address);
homie_publish("$homie", 0, 1, "2.0.1", 0);
homie_publish("$online", 0, 1, "true", 0);
homie_publish("$name", 0, 1, config->device_name, 0);
homie_publish("$localip", 0, 1, ip_address, 0);
homie_publish("$mac", 0, 1, mac_address, 0);
homie_publish("$fw/name", 0, 1, config->firmware_name, 0);
homie_publish("$fw/version", 0, 1, config->firmware_version, 0);
homie_publish("$nodes", 0, 1, "", 0); // FIXME: needs to be extendible
homie_publish("$implementation", 0, 1, "esp32-idf", 0);
homie_publish("$implementation/version", 0, 1, "dev", 0);
homie_publish("$stats", 0, 1, "uptime,rssi,signal,freeheap", 0); // FIXME: needs to be extendible
homie_publish("$stats/interval", 0, 1, "30", 0);
homie_publish_bool("$implementation/ota/enabled", 0, 1, config->ota_enabled);
const esp_partition_t *running_partition = esp_ota_get_running_partition();
if (running_partition != NULL)
{
homie_publishf("$implementation/ota/running", 0, 1, "0x%08x", running_partition->address);
}
else
{
homie_publishf("$implementation/ota/running", 0, 1, "NULL");
}
const esp_partition_t *boot_partition = esp_ota_get_boot_partition();
if (boot_partition != NULL)
{
homie_publishf("$implementation/ota/boot", 0, 1, "0x%08x", boot_partition->address);
}
else
{
homie_publishf("$implementation/ota/boot", 0, 1, "NULL");
}
homie_subscribe("$implementation/reboot");
homie_subscribe("$implementation/logging");
if (config->ota_enabled)
homie_subscribe("$implementation/ota/url/#");
}
static void homie_task(void *pvParameter)
{
while (1)
{
homie_publish_int("$stats/uptime", 0, 0, esp_timer_get_time() / 1000000);
int rssi = _get_wifi_rssi();
homie_publish_int("$stats/rssi", 0, 0, rssi);
// Translate to "signal" percentage, assuming RSSI range of (-100,-50)
homie_publish_int("$stats/signal", 0, 0, _clamp((rssi + 100) * 2, 0, 100));
homie_publish_int("$stats/freeheap", 0, 0, esp_get_free_heap_size());
vTaskDelay(30000 / portTICK_PERIOD_MS);
}
}
void homie_init(homie_config_t *passed_config)
{
config = passed_config;
// If client_id is blank, generate one based off the mac
if (!config->client_id[0])
_get_mac(config->client_id, false);
mqtt_app_start();
xTaskCreate(&homie_task, "homie_task", 8192, NULL, 5, NULL);
}