-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhousewiz_device.c
646 lines (561 loc) · 22.1 KB
/
housewiz_device.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/* HouseWiz - A simple home web server for control of Philips Wiz WiFi Devices
*
* Copyright 2020, Pascal Martin
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*
* housewiz_device.c - Control a Philips Wiz WiFi Device.
*
* SYNOPSYS:
*
* const char *housewiz_device_initialize (int argc, const char **argv);
*
* Initialize this module at startup.
*
* int housewiz_device_changed (void);
*
* Indicate if the configuration was changed due to discovery, which
* means it must be saved.
*
* const char *housewiz_device_live_config (char *buffer, int size);
*
* Recover the current live config, typically to save it to disk after
* a change has been detected.
*
* const char *housewiz_device_refresh (const char *reason);
*
* Re-evaluate the configuration after it changed.
*
* int housewiz_device_count (void);
*
* Return the number of configured devices available.
*
* const char *housewiz_device_name (int point);
*
* Return the name of a wiz device.
*
* const char *housewiz_device_failure (int point);
*
* Return a string describing the failure, or a null pointer if healthy.
*
* int housewiz_device_commanded (int point);
* time_t housewiz_device_deadline (int point);
*
* Return the last commanded state, or the command deadline, for
* the specified wiz device.
*
* int housewiz_device_get (int point);
*
* Get the actual state of the device.
*
* int housewiz_device_set (int point, int state, int pulse);
*
* Set the specified point to the on (1) or off (0) state for the pulse
* length specified. The pulse length is in seconds. If pulse is 0, the
* device is maintained to the requested state until a new state is issued.
*
* Return 1 on success, 0 if the device is not known and -1 on error.
*
* void housewiz_device_periodic (void);
*
* This function must be called every second. It runs the Wiz device
* discovery and ends the expired pulses.
*/
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <netpacket/packet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "echttp.h"
#include "echttp_json.h"
#include "houselog.h"
#include "houseconfig.h"
#include "housewiz_device.h"
// This offset is used to "sign" an ID that contains a device index.
// This is more for debug _and simulation_ purpose: this module does
// not uses the value of the ID from received messages. A simulator
// might however use it to identify the context.
//
#define WIZ_ID_OFFSET 12000
struct DeviceMap {
char name[32];
char macaddress[16];
char description[256];
struct sockaddr_in ipaddress;
time_t detected;
int status;
int commanded;
time_t pending;
time_t deadline;
time_t reboot;
time_t last_sense;
};
static int DeviceListChanged = 0;
static struct DeviceMap *Devices = 0;
static int DevicesCount = 0;
static int DevicesSpace = 0;
static int WizDevicePort = 38899;
static int WizStatusPort = 38900;
static int WizSocket = -1;
static struct sockaddr_in WizBroadcast;
struct NetworkMap {
char name[32];
char ip[20];
char mac[16];
};
#define NETWORKS_MAX 8
static struct NetworkMap Networks[NETWORKS_MAX];
static int NetworksCount = 0;
static void safecpy (char *dest, const char *src, int limit) {
strncpy (dest, src, limit-1);
dest[limit-1] = 0;
}
int housewiz_device_count (void) {
return DevicesCount;
}
int housewiz_device_changed (void) {
if (DeviceListChanged) {
DeviceListChanged = 0;
return 1;
}
return 0;
}
const char *housewiz_device_name (int point) {
if (point < 0 || point >= DevicesCount) return 0;
return Devices[point].name;
}
int housewiz_device_commanded (int point) {
if (point < 0 || point >= DevicesCount) return 0;
return Devices[point].commanded;
}
time_t housewiz_device_deadline (int point) {
if (point < 0 || point >= DevicesCount) return 0;
return Devices[point].deadline;
}
const char *housewiz_device_failure (int point) {
if (point < 0 || point >= DevicesCount) return 0;
if (!Devices[point].detected) return "silent";
return 0;
}
int housewiz_device_get (int point) {
if (point < 0 || point >= DevicesCount) return 0;
return Devices[point].status;
}
static void housewiz_device_socket (void) {
WizBroadcast.sin_family = AF_INET;
WizBroadcast.sin_port = htons(WizStatusPort);
WizBroadcast.sin_addr.s_addr = INADDR_ANY;
WizSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (WizSocket < 0) {
houselog_trace (HOUSE_FAILURE, "DEVICE",
"cannot open UDP socket: %s", strerror(errno));
exit(1);
}
if (bind(WizSocket,
(struct sockaddr *)(&WizBroadcast),
sizeof(WizBroadcast)) < 0) {
houselog_trace (HOUSE_FAILURE, "SOCKET",
"cannot bind to UDP port %d: %s",
WizStatusPort, strerror(errno));
exit(1);
}
int value = 1;
if (setsockopt(WizSocket, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value)) < 0) {
houselog_trace (HOUSE_FAILURE, "SOCKET",
"cannot broadcast: %s", strerror(errno));
exit(1);
}
WizBroadcast.sin_port = htons(WizDevicePort);
WizBroadcast.sin_addr.s_addr = INADDR_BROADCAST;
houselog_trace (HOUSE_INFO, "DEVICE", "UDP port %d is now open", WizStatusPort);
}
static void housewiz_device_send (const struct sockaddr_in *a, const char *d) {
if ((long)(a->sin_addr.s_addr) == 0) return; // Not detected.
if (echttp_isdebug()) {
long ip = ntohl((long)(a->sin_addr.s_addr));
int port = ntohs(a->sin_port);
printf ("Sending packet to %ld.%ld.%ld.%ld(port %d): %s\n",
(ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff, port, d);
}
int sent = sendto (WizSocket, d, strlen(d)+1, 0,
(struct sockaddr *)a, sizeof(struct sockaddr_in));
if (sent < 0)
houselog_trace
(HOUSE_FAILURE, "DEVICE", "sendto() error: %s", strerror(errno));
}
static void housewiz_device_sense (const struct sockaddr_in *a, int id) {
int i;
char buffer[256];
if ((long)(a->sin_addr.s_addr) == 0) return; // Not detected.
for (i = 0; i < NetworksCount; i++) {
snprintf (buffer, sizeof(buffer),
"{\"method\": \"registration\", \"id\": %d, \"params\":"
"{\"phoneIp\": \"%s\", \"register\":true, "
"\"phoneMac\":\"%s\"}}",
id, Networks[i].ip, Networks[i].mac);
housewiz_device_send (a, buffer);
}
}
static void housewiz_device_control (int device, int state) {
char buffer[256];
snprintf (buffer, sizeof(buffer),
"{\"method\": \"setPilot\", \"id\": %d, \"env\":\"pro\", \"params\": {\"state\": %s}}",
WIZ_ID_OFFSET+device, state?"true":"false");
housewiz_device_send (&(Devices[device].ipaddress), buffer);
}
int housewiz_device_set (int device, int state, int pulse) {
const char *namedstate = state?"on":"off";
time_t now = time(0);
if (device < 0 || device >= DevicesCount) return 0;
if (echttp_isdebug()) {
if (pulse) fprintf (stderr, "set %s to %s at %ld (pulse %ds)\n", Devices[device].name, namedstate, time(0), pulse);
else fprintf (stderr, "set %s to %s at %ld\n", Devices[device].name, namedstate, time(0));
}
if (pulse > 0) {
Devices[device].deadline = now + pulse;
houselog_event ("DEVICE", Devices[device].name, "SET",
"%s FOR %d SECONDS", namedstate, pulse);
} else {
Devices[device].deadline = 0;
houselog_event ("DEVICE", Devices[device].name, "SET", "%s", namedstate);
}
Devices[device].commanded = state;
Devices[device].pending = now + 5;
// Only send a command if we detected the device on the network.
//
if (Devices[device].detected) {
housewiz_device_control (device, state);
Devices[device].last_sense = 0; // Get the state update asap.
}
return 1;
}
static int housewiz_device_enumerate_add (const char *name) {
int i;
for (i = 0; i < NetworksCount; i++) {
if (!strcmp (name, Networks[i].name)) return i;
}
// Not found: add new entry if there is room, or else overwrite the last.
if (NetworksCount >= NETWORKS_MAX) NetworksCount = NETWORKS_MAX - 1;
safecpy (Networks[NetworksCount].name, name, sizeof(Networks[0].name));
Networks[NetworksCount].ip[0] = 0;
Networks[NetworksCount].mac[0]= 0;
return NetworksCount++;
}
static void housewiz_device_enumerate (void) {
static const char bin2hex[] = "0123456789abcdef";
int i;
struct ifaddrs *interfaces;
struct ifaddrs *cursor;
NetworksCount = 0; // Erase the previous list.
if (getifaddrs(&interfaces) < 0) return;
for (cursor = interfaces; cursor; cursor = cursor->ifa_next) {
if (cursor->ifa_flags & IFF_LOOPBACK) continue;
if (!cursor->ifa_addr) continue;
if (cursor->ifa_addr->sa_family == AF_INET) {
int idx = housewiz_device_enumerate_add (cursor->ifa_name);
struct sockaddr_in *ia = (struct sockaddr_in *) (cursor->ifa_addr);
long ip = ntohl((long)(ia->sin_addr.s_addr));
snprintf (Networks[idx].ip, sizeof(Networks[0].ip),
"%ld.%ld.%ld.%ld",
(ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
} else if (cursor->ifa_addr->sa_family == AF_PACKET) {
struct sockaddr_ll *mac = (struct sockaddr_ll*) (cursor->ifa_addr);
if (mac->sll_halen >= sizeof(Networks[0].mac)) continue;
int idx = housewiz_device_enumerate_add (cursor->ifa_name);
for (i = 0; i < mac->sll_halen; i++) {
Networks[idx].mac[i*2] = bin2hex[(mac->sll_addr[i])/16];
Networks[idx].mac[i*2+1] = bin2hex[(mac->sll_addr[i])&15];
}
Networks[idx].mac[i*2] = 0;
}
}
if (echttp_isdebug()) {
for (i = 0; i < NetworksCount; i++) {
fprintf (stderr, "Interface %s: IP %s, MAC %s\n",
Networks[i].name, Networks[i].ip, Networks[i].mac);
}
}
}
static void housewiz_device_reset (int i, int status) {
Devices[i].commanded = Devices[i].status = status;
Devices[i].pending = Devices[i].deadline = 0;
}
void housewiz_device_periodic (time_t now) {
static time_t LastRetry = 0;
static time_t LastSense = 0;
int i;
if (now >= LastSense + 60) {
housewiz_device_enumerate();
housewiz_device_sense(&WizBroadcast, 1);
LastSense = now;
}
if (now < LastRetry + 5) return;
LastRetry = now;
for (i = 0; i < DevicesCount; ++i) {
if (now >= Devices[i].last_sense + 35) {
housewiz_device_sense(&(Devices[i].ipaddress), WIZ_ID_OFFSET+i);
Devices[i].last_sense = now;
}
// If we did not detect a device for 3 senses, consider it failed.
if (Devices[i].detected > 0 && Devices[i].detected < now - 100) {
houselog_event ("DEVICE", Devices[i].name, "SILENT",
"MAC ADDRESS %s", Devices[i].macaddress);
housewiz_device_reset (i, 0);
Devices[i].detected = 0;
}
if (Devices[i].deadline > 0 && now >= Devices[i].deadline) {
houselog_event ("DEVICE", Devices[i].name, "RESET", "END OF PULSE");
Devices[i].commanded = 0;
Devices[i].pending = now + 5;
Devices[i].deadline = 0;
}
if (Devices[i].status != Devices[i].commanded) {
if (Devices[i].pending > now) {
if (Devices[i].detected) {
const char *state = Devices[i].commanded?"on":"off";
houselog_event ("DEVICE", Devices[i].name, "RETRY", state);
housewiz_device_control (i, Devices[i].commanded);
}
} else {
// The ongoing command timed out, forget and cleanup.
if (Devices[i].pending)
houselog_event ("DEVICE", Devices[i].name, "TIMEOUT", "");
housewiz_device_reset (i, Devices[i].status);
}
}
}
}
const char *housewiz_device_refresh (const char *reason) {
int i;
int devices;
int oldcount = DevicesCount;
struct DeviceMap *oldcfg = Devices;
struct DeviceMap *newcfg;
houselog_event ("CONFIG", "wiz", "ACTIVATING", "%s", reason);
if (houseconfig_active()) {
devices = houseconfig_array (0, ".wiz.devices");
if (devices < 0) return "cannot find devices array";
DevicesCount = houseconfig_array_length (devices);
if (echttp_isdebug()) fprintf (stderr, "found %d devices\n", DevicesCount);
} else {
DevicesCount = 0;
}
DevicesSpace = DevicesCount + 32;
newcfg = calloc(sizeof(struct DeviceMap), DevicesSpace);
if (!newcfg) return "no more memory";
Devices = newcfg;
for (i = 0; i < DevicesCount; ++i) {
int device;
char path[128];
snprintf (path, sizeof(path), "[%d]", i);
device = houseconfig_object (devices, path);
if (device <= 0) continue;
const char *name = houseconfig_string (device, ".name");
if (name)
safecpy (Devices[i].name, name, sizeof(Devices[i].name));
const char *mac = houseconfig_string (device, ".address");
if (mac)
safecpy (Devices[i].macaddress, mac, sizeof(Devices[i].macaddress));
const char *desc = houseconfig_string (device, ".description");
if (desc)
safecpy (Devices[i].description, desc,
sizeof(Devices[i].description));
Devices[i].deadline = 0;
Devices[i].pending = 0;
Devices[i].detected = 0;
Devices[i].reboot = 0;
Devices[i].last_sense = 0;
if (oldcfg) {
int j;
for (j = 0; j < oldcount; ++j) {
if (!strcmp(Devices[i].macaddress, oldcfg[j].macaddress)) {
// Recover the last status known.
Devices[i].detected = oldcfg[j].detected;
Devices[i].reboot = oldcfg[j].reboot;
Devices[i].last_sense = oldcfg[j].last_sense;
housewiz_device_reset (i, oldcfg[j].status);
break;
}
}
}
if (echttp_isdebug())
fprintf (stderr, "load device %s, MAC address %s (%s)\n",
Devices[i].name, Devices[i].macaddress,
desc?desc:"no description");
}
if (oldcfg) free(oldcfg); // This is safe now that the new config is in place.
return 0;
}
const char *housewiz_device_live_config (char *buffer, int size) {
static char pool[65537];
static ParserToken token[1024];
ParserContext context = echttp_json_start (token, 1024, pool, sizeof(pool));
int i;
int root = echttp_json_add_object (context, 0, 0);
int top = echttp_json_add_object (context, root, "wiz");
int items = echttp_json_add_array (context, top, "devices");
for (i = 0; i < DevicesCount; ++i) {
if (Devices[i].name[0] == 0 || Devices[i].macaddress[0] == 0) continue;
int device = echttp_json_add_object (context, items, 0);
echttp_json_add_string (context, device, "name", Devices[i].name);
echttp_json_add_string (context, device, "address", Devices[i].macaddress);
echttp_json_add_string
(context, device, "description", Devices[i].description);
}
return echttp_json_export (context, buffer, size);
}
static int housewiz_device_mac_search (const char *macaddress) {
int i;
for (i = 0; i < DevicesCount; ++i) {
if (!strcasecmp(macaddress, Devices[i].macaddress)) return i;
}
return -1;
}
static void housewiz_device_receive (int fd, int mode) {
time_t now = time(0);
char data[256];
struct sockaddr_in addr;
int addrlen = sizeof(addr);
int size = recvfrom (WizSocket, data, sizeof(data), 0,
(struct sockaddr *)(&addr), &addrlen);
if (size > 0) {
data[size] = 0;
if (echttp_isdebug()) fprintf (stderr, "Received: %s\n", data);
ParserToken json[256];
int jsoncount = 256;
// We need to copy to preserve the original data (JSON decoding is
// destructive).
//
char buffer[256];
safecpy (buffer, data, sizeof(buffer));
const char *error = echttp_json_parse (buffer, json, &jsoncount);
if (error) {
houselog_trace (HOUSE_FAILURE, "DEVICE", "%s: %s", error, data);
return;
}
// Identify the type of message.
// For now we only handle syncPilot and firstBeat.
//
int method = echttp_json_search (json, ".method");
if ((method < 0) || (json[method].type != PARSER_STRING)) {
houselog_trace (HOUSE_FAILURE,
"DEVICE", "no valid method in: %s", data);
return;
}
if (strcmp (json[method].value.string, "firstBeat") &&
strcmp (json[method].value.string, "syncPilot")) return;
// Retrieve the device's MAC address (used as persistent ID)
//
int macaddr = echttp_json_search (json, ".params.mac");
if ((macaddr < 0) || (json[macaddr].type != PARSER_STRING)) {
houselog_trace (HOUSE_FAILURE,
"DEVICE", "no valid MAC address in: %s", data);
return;
}
const char *mac = json[macaddr].value.string;
int device = housewiz_device_mac_search (mac);
// Record new devices.
//
if (device < 0) {
if (echttp_isdebug()) fprintf (stderr, "new device %s\n", mac);
DeviceListChanged = 1;
if (DevicesCount >= DevicesSpace) {
DevicesSpace += 32;
Devices = realloc (Devices, sizeof(struct DeviceMap) * DevicesSpace);
}
device = DevicesCount++;
snprintf (Devices[device].name, sizeof(Devices[0].name), "wiz%d", device+1);
snprintf (Devices[device].macaddress, sizeof(Devices[0].macaddress),
"%s", mac);
snprintf (Devices[device].description, sizeof(Devices[0].description),
"autogenerated");
houselog_event ("DEVICE", Devices[device].name, "ADDED",
"MAC ADDRESS %s", mac);
Devices[device].detected = now; // Skip the "DETECTED" event.
Devices[device].last_sense = 0;
}
if (device < 0) return; // Cannot add this unknown device: ignore.
if (!Devices[device].detected)
houselog_event ("DEVICE", Devices[device].name, "DETECTED",
"MAC ADDRESS %s", mac);
Devices[device].detected = now;
// Adjust to possible IP address changes.
//
memcpy (&(Devices[device].ipaddress),
&addr, sizeof(Devices[device].ipaddress));
Devices[device].ipaddress.sin_port = htons(WizDevicePort);
// Handle device reboot.
//
if (!strcmp (json[method].value.string, "firstBeat")) {
// Ignore repeated messages (they last for almost a minute).
if (Devices[device].reboot < now - 60) {
// This plug just rebooted, log and force a query soon.
int firmware = echttp_json_search (json, ".params.fwVersion");
if (firmware < 0) {
houselog_trace (HOUSE_FAILURE, "DEVICE",
"no valid firmware version in: %s", data);
return;
}
houselog_event ("DEVICE", Devices[device].name, "REBOOT",
"FIRMWARE VERSION %s", json[firmware].value.string);
Devices[device].last_sense = now - 30; // In 5 seconds.
Devices[device].reboot = now;
}
return;
}
// Now the message can only be syncPilot:
// synchronize the device state.
//
int state = echttp_json_search (json, ".params.state");
if ((state < 0) || (json[state].type != PARSER_BOOL)) {
houselog_trace (HOUSE_FAILURE,
"DEVICE", "no valid state in: %s", data);
return;
}
int status = json[state].value.bool;
if (Devices[device].status != status) {
if (Devices[device].pending) {
if (status == Devices[device].commanded) {
houselog_event ("DEVICE", Devices[device].name,
"CONFIRMED", "FROM %s TO %s",
Devices[device].status?"on":"off",
status?"on":"off");
Devices[device].pending = 0; // Command complete.
}
} else {
houselog_event ("DEVICE", Devices[device].name,
"CHANGED", "FROM %s TO %s",
Devices[device].status?"on":"off",
status?"on":"off");
Devices[device].commanded = status; // By someone else.
}
Devices[device].status = status;
}
}
}
const char *housewiz_device_initialize (int argc, const char **argv) {
housewiz_device_socket ();
echttp_listen (WizSocket, 1, housewiz_device_receive, 0);
return housewiz_device_refresh ("AT STARTUP");
}