-
Notifications
You must be signed in to change notification settings - Fork 0
/
kasa.c
286 lines (254 loc) · 8.54 KB
/
kasa.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
/* kasa - A simple program to control a TP-Link Kasa device
*
* 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.
*
*
* kasa.c - control a TP-Link Kasa device
*
* SYNOPSYS:
*
* kasa
* kasa <host>
* kasa <host> alias <name>
* kasa <host> on [<model> [<outlet>]]
* kasa <host> off [<model> [<outlet>]]
*
* Supported commands are:
* alias: change the alias name configured in the device.
* on: turn the device on.
* off: turn the device off.
*
* With no parameter specified, the program send a broadcast to discover all
* devices present, and prints system information from all responding devices.
*
* With no command specified, the program requests and prints system
* information for the specified device only.
*
* In both cases the output is the raw message from the device. No formatting.
*
* The supported models are:
* kp400 (dual outlet: outlet ID is required)
* hs220 (single outlet: outlet ID ignored)
*
* If the model is not specified, the program uses the most commonly supported
* variant of the protocol. It might not always work.
*/
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netdb.h>
#include <arpa/inet.h>
static int KasaPort = 9999;
static int KasaSocket = -1;
static struct sockaddr_in KasaAddress;
static void kasa_socket (void) {
KasaAddress.sin_family = AF_INET;
KasaAddress.sin_port = htons(KasaPort);
KasaAddress.sin_addr.s_addr = INADDR_BROADCAST;
KasaSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (KasaSocket < 0) {
printf ("cannot open UDP socket: %s\n", strerror(errno));
exit(1);
}
int value = 1;
if (setsockopt(KasaSocket, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value)) < 0) {
printf ("cannot broadcast: %s\n", strerror(errno));
exit(1);
}
printf ("UDP socket is ready.\n");
}
static unsigned char hex2bin(char data) {
if (data >= '0' && data <= '9')
return data - '0';
if (data >= 'a' && data <= 'f')
return data - 'a' + 10;
if (data >= 'A' && data <= 'F')
return data - 'A' + 10;
return 0;
}
static char bin2hex (unsigned char d) {
d &= 0x0f;
if (d >= 0 && d <= 9) return '0' + d;
if (d >= 10 && d <= 15) return ('a' - 10) + d;
return '0';
}
static void kasa_send (const char *data) {
char encoded[1024];
int i;
char key = 0xab;
int length = strlen(data);
if (length > sizeof(encoded)) {
printf ("Data too large to encode: %d is greater than %d\n",
length, sizeof(encoded));
exit(1);
}
for (i = 0; i < length; ++i) {
key = encoded[i] = key ^ data[i];
}
printf ("Sending %s\n", data);
int sent = sendto (KasaSocket, encoded, length, 0,
(struct sockaddr *)(&KasaAddress),
sizeof(struct sockaddr_in));
if (sent < 0) {
printf ("** sendto() error: %s\n", strerror(errno));
exit(1);
}
}
static int kasa_wait (void) {
fd_set receive;
struct timeval timeout;
FD_ZERO(&receive);
FD_SET(KasaSocket, &receive);
timeout.tv_sec = 2;
timeout.tv_usec = 0;
return select (KasaSocket+1, &receive, 0, 0, &timeout);
}
static void kasa_receive (void) {
char encoded[1024];
char data[1025];
char source[100];
struct sockaddr_in addr;
int addrlen = sizeof(addr);
int size = recvfrom (KasaSocket, encoded, sizeof(encoded), 0,
(struct sockaddr *)(&addr), &addrlen);
if (size <= 0) {
printf ("** recvfrom() error: %s\n", strerror(errno));
return;
}
int i;
char key = 0xab;
for (i = 0; i < size; ++i) {
data[i] = key ^ encoded[i];
key = encoded[i];
}
data[i] = 0;
int ip = htonl(addr.sin_addr.s_addr);
printf ("Received from %d.%d.%d.%d: %s\n",
0xff & (ip >> 24), 0xff & (ip >> 16), 0xff & (ip >> 8), 0xff & ip,
data);
}
static int kasa_resolve (const char *host) {
int result = 0;
struct addrinfo hints;
struct addrinfo *resolved;
struct addrinfo *cursor;
hints.ai_flags = AI_ADDRCONFIG;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
if (getaddrinfo (host, 0, &hints, &resolved)) return 0;
for (cursor = resolved; cursor; cursor = cursor->ai_next) {
if (cursor->ai_family != AF_INET) continue;
memcpy (&KasaAddress, cursor->ai_addr, sizeof(KasaAddress));
KasaAddress.sin_port = htons(KasaPort);
result = 1;
}
freeaddrinfo(resolved);
return result;
}
static void kasa_help (int status) {
printf ("kasa: query the status of all devices\n");
printf ("kasa <host>: query the status of the specified device\n");
printf ("kasa <host> alias <name>: set an alias for the specified device\n");
printf ("kasa <host> on [hs220]: turn the specified device on\n");
printf ("kasa <host> off [hs220]: turn the specified device off\n");
printf ("kasa <host> on [kp400 <id>]: turn the specified subdevice on\n");
printf ("kasa <host> off [kp400 <id>]: turn the specified subdevice off\n");
printf ("kasa -h|--help|help: show this help text\n");
exit (status);
}
int main (int argc, char **argv) {
const char *host = 0;
const char *cmd = 0;
const char *model = 0;
const char *id = 0;
char buffer[256];
if (argc >= 2) {
if (!strcmp(argv[1], "-h")) kasa_help (0);
if (!strcmp(argv[1], "--help")) kasa_help (0);
if (!strcmp(argv[1], "help")) kasa_help (0);
host = argv[1];
if (argc >= 3) {
cmd = argv[2];
if (argc >= 4) {
model = argv[3];
if (argc >= 5) {
id = argv[4];
}
}
}
}
kasa_socket ();
if (host) {
if (!kasa_resolve (host)) {
printf ("Cannot resolve %s\n", host);
kasa_help(1);
}
}
if (!cmd) {
kasa_send ("{\"system\":{\"get_sysinfo\":{}}}");
} else if (!strcmp (cmd, "alias")) {
if (model) {
char buffer[200];
snprintf (buffer, sizeof(buffer),
"{\"system\":{\"set_dev_alias\":{\"alias\":\"%s\"}}}",
model);
kasa_send (buffer);
}
} else if (!strcmp (cmd, "on")) {
if (!model) {
kasa_send ("{\"system\":{\"set_relay_state\":{\"state\":1}}}");
} else if (!strcmp (model, "kp400")) {
if (!id) {
printf ("Outlet ID is required\n");
exit (1);
}
char buffer[200];
snprintf (buffer, sizeof(buffer),
"{\"context\":{\"child_ids\":[\"%s\"]},\"system\":{\"set_relay_state\":{\"state\":1}}}", id);
kasa_send (buffer);
} else if (!strcmp (model, "hs220")) {
kasa_send ("{\"smartlife.iot.dimmer\":{\"set_switch_state\":{\"state\":1}}}");
}
} else if (!strcmp (cmd, "off")) {
if (!model) {
kasa_send ("{\"system\":{\"set_relay_state\":{\"state\":0}}}");
} else if (!strcmp (model, "kp400")) {
if (!id) {
printf ("Outlet ID is required\n");
exit (1);
}
char buffer[200];
snprintf (buffer, sizeof(buffer),
"{\"context\":{\"child_ids\":[\"%s\"]},\"system\":{\"set_relay_state\":{\"state\":0}}}", id);
kasa_send (buffer);
} else if (!strcmp (model, "hs220")) {
kasa_send ("{\"smartlife.iot.dimmer\":{\"set_switch_state\":{\"state\":0}}}");
}
} else {
printf ("Invalid command %s\n", cmd);
kasa_help(1);
}
while (kasa_wait() > 0) kasa_receive();
return 0;
}