以前紹介したTP-LINK WiFiスマートプラグ制御をPC(Linux/Windows等)から実行できるプログラムを作ってみた。コマンドライン・オプションによりオン・オフしたりできる。
TP-LINK WiFiスマートプラグをESP32(Arduino)から制御する
紹介元サイトで公開されているPythonで書かれたtplink_smartplug.pyをC言語に移殖(というかほぼ全部書き換え)したもので、OrangePi/RaspberryPiなどのLinuxやWindows上で実行することができる。Linuxはgcc、WindowsはMSYS2 MinGW64にてwsock32とws2_32ライブラリのリンクが必要となる。
https://github.com/softScheck/tplink-smartplug
スマートプラグのアドレスを調べるために元プログラムにデバイス検出機能を追加してみたが、実際に使う場合にはルーター等のDHCPにて固定アドレスを割り当てしたほうが良いだろう。
しかし、Pythonに比べるとコードがバカデカイ。個人的にはあまり好きではないがPythonでの開発って凄く効率よさげだなと思ったりも...
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 |
/* TP-Link Wi-Fi Smart Plug Protocol Client For use with TP-Link HS-100 or HS-105 or HS-110 Copyright 2019 Sasapea's Lab. (Porting C from Python) by Lubomir Stroetmann Copyright 2016 softScheck GmbH Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include <stdio.h> #include <stdint.h> #include <stdbool.h> #include <stdarg.h> #include <stdlib.h> #include <string.h> #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) #include <ws2tcpip.h> #define socket_error(s) wsa_error(WSAGetLastError(), s) #else #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #define SOCKET int #define socket_error(s) perror(s) #define closesocket(s) close(s) #endif #define VERSION "1.0" #define PORT "9999" #define DISCOVER_IP "255.255.255.255" #define DISCOVER_HS105 "{\"system\":{\"get_sysinfo\":null}}" #define DISCOVER_HS110 "{\"system\":{\"get_sysinfo\":null},\"emeter\":{\"get_realtime\":null}}" #define STARTING_KEY 171 /** * Predefined Smart Plug Commands * For a full list of commands, consult tplink_commands.txt **/ const char *commands[][2] = { { "info" , "{\"system\":{\"get_sysinfo\":{}}}" }, { "on" , "{\"system\":{\"set_relay_state\":{\"state\":1}}}" }, { "off" , "{\"system\":{\"set_relay_state\":{\"state\":0}}}" }, { "cloudinfo", "{\"cnCloud\":{\"get_info\":{}}}" }, { "wlanscan" , "{\"netif\":{\"get_scaninfo\":{\"refresh\":0}}}" }, { "time" , "{\"time\":{\"get_time\":{}}}" }, { "schedule" , "{\"schedule\":{\"get_rules\":{}}}" }, { "countdown", "{\"count_down\":{\"get_rules\":{}}}" }, { "antitheft", "{\"anti_theft\":{\"get_rules\":{}}}" }, { "reboot" , "{\"system\":{\"reboot\":{\"delay\":1}}}" }, { "reset" , "{\"system\":{\"reset\":{\"delay\":1}}}" }, { "energy" , "{\"emeter\":{\"get_realtime\":{}}}" }, }; #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) const struct { int err_code; char *err_str; } errors[] = { { WSAEINTR , "WSAEINTR" }, { WSAEACCES , "WSAEACCES" }, { WSAEFAULT , "WSAEFAULT" }, { WSAEINVAL , "WSAEINVAL" }, { WSAEMFILE , "WSAEMFILE" }, { WSAEWOULDBLOCK , "WSAEWOULDBLOCK" }, { WSAEINPROGRESS , "WSAEINPROGRESS" }, { WSAEALREADY , "WSAEALREADY" }, { WSAENOTSOCK , "WSAENOTSOCK" }, { WSAEDESTADDRREQ , "WSAEDESTADDRREQ" }, { WSAEMSGSIZE , "WSAEMSGSIZE" }, { WSAEPROTOTYPE , "WSAEPROTOTYPE" }, { WSAENOPROTOOPT , "WSAENOPROTOOPT" }, { WSAEPROTONOSUPPORT, "WSAEPROTONOSUPPORT" }, { WSAESOCKTNOSUPPORT, "WSAESOCKTNOSUPPORT" }, { WSAEOPNOTSUPP , "WSAEOPNOTSUPP" }, { WSAEPFNOSUPPORT , "WSAEPFNOSUPPORT" }, { WSAEAFNOSUPPORT , "WSAEAFNOSUPPORT" }, { WSAEADDRINUSE , "WSAEADDRINUSE" }, { WSAEADDRNOTAVAIL , "WSAEADDRNOTAVAIL" }, { WSAENETDOWN , "WSAENETDOWN" }, { WSAENETUNREACH , "WSAENETUNREACH" }, { WSAENETRESET , "WSAENETRESET" }, { WSAECONNABORTED , "WSAECONNABORTED" }, { WSAECONNRESET , "WSAECONNRESET" }, { WSAENOBUFS , "WSAENOBUFS" }, { WSAEISCONN , "WSAEISCONN" }, { WSAENOTCONN , "WSAENOTCONN" }, { WSAESHUTDOWN , "WSAESHUTDOWN" }, { WSAETIMEDOUT , "WSAETIMEDOUT" }, { WSAECONNREFUSED , "WSAECONNREFUSED" }, { WSAEHOSTDOWN , "WSAEHOSTDOWN" }, { WSAEHOSTUNREACH , "WSAEHOSTUNREACH" }, { WSAEPROCLIM , "WSAEPROCLIM" }, { WSASYSNOTREADY , "WSASYSNOTREADY" }, { WSAVERNOTSUPPORTED, "WSAVERNOTSUPPORTED" }, { WSANOTINITIALISED , "WSANOTINITIALISED" }, { WSAEDISCON , "WSAEDISCON" }, { WSATYPE_NOT_FOUND , "WSATYPE_NOT_FOUND" }, { WSAHOST_NOT_FOUND , "WSAHOST_NOT_FOUND" }, { WSATRY_AGAIN , "WSATRY_AGAIN" }, { WSANO_RECOVERY , "WSANO_RECOVERY" }, { WSANO_DATA , "WSANO_DATA" }, }; void wsa_error(int err_code, const char *format, ...) { va_list arg; char *err_str = NULL; int i; for (i = 0; i < sizeof(errors) / sizeof(errors[0]); i++) { if (errors[i].err_code == err_code) { err_str = errors[i].err_str; break; } } va_start(arg, format); vfprintf(stderr, format, arg); va_end(arg); if (err_str) fprintf(stderr, ": %s\n", err_str); else fprintf(stderr, ": %d\n", err_code); } #endif struct { uint32_t size; char data[768]; } packet; /** * Encryption and Decryption of TP-Link Smart Home Protocol * XOR Autokey Cipher with starting key = 171 **/ void encrypt() { char key = STARTING_KEY; uint32_t i; for (i = 0; i < packet.size; ++i) packet.data[i] = key = key ^ packet.data[i]; packet.size = htonl(packet.size); } void decrypt() { char key = STARTING_KEY; uint32_t i; packet.size = ntohl(packet.size); for (i = 0; i < packet.size; ++i) { char b = packet.data[i]; packet.data[i] = key ^ b; key = b; } packet.data[packet.size] = 0; } bool recv_fill(SOCKET sock, char *buf, size_t len) { ssize_t n; size_t i; for (i = 0; i < len; i += n) { if ((n = recv(sock, buf + i, len - i, 0)) == 0) { fprintf(stderr, "recv() aborted\n"); return false; } if (n == -1) { socket_error("recv()"); return false; } } return true; } /** * Send command and receive reply **/ int smartplug(SOCKET sock, const char *cmd) { packet.size = snprintf(packet.data, sizeof(packet.data), "%s", cmd); size_t size = sizeof(packet.size) + packet.size; encrypt(); if (send(sock, (char *)&packet, size, 0) != size) socket_error("send()"); else { if (recv_fill(sock, (char *)&packet.size, sizeof(packet.size))) { size = ntohl(packet.size); if (size < sizeof(packet.data) - 1) { if (recv_fill(sock, packet.data, size)) { decrypt(); fprintf(stdout, "%s\n", packet.data); return EXIT_SUCCESS; } } } } return EXIT_FAILURE; } struct addrinfo *gethostinfo(const char *host, const char *port) { int rv; struct addrinfo hints, *result = NULL; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; // IPv4 or IPv6 hints.ai_socktype = SOCK_STREAM; if ((rv = getaddrinfo(host, port, &hints, &result)) != 0) #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) wsa_error(rv, "getaddrinfo(\"%s\")", host); #else fprintf(stderr, "getaddrinfo(\"%s\"): %s\n", host, gai_strerror(rv)); #endif return result; } SOCKET connect_socket(const char *host, const char *port) { SOCKET sock = -1; struct addrinfo *info, *node; if (info = gethostinfo(host, port)) { for (node = info; node; node = node->ai_next) { if ((sock = socket(node->ai_family, node->ai_socktype, node->ai_protocol)) == -1) { if (!node->ai_next) socket_error("socket()"); } else if (connect(sock, node->ai_addr, node->ai_addrlen) == -1) { if (!node->ai_next) socket_error("connect()"); closesocket(sock); sock = -1; } else break; } freeaddrinfo(info); } return sock; } int scan_smartplugs() { int rv = EXIT_FAILURE; struct addrinfo hints, *result = NULL; memset(&hints, 0, sizeof(hints)); hints.ai_flags = 0; hints.ai_family = AF_INET; // IPv4 hints.ai_socktype = SOCK_DGRAM; if ((rv = getaddrinfo(DISCOVER_IP, PORT, &hints, &result)) != 0) #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) wsa_error(rv, "getaddrinfo(\"%s\")", DISCOVER_IP); #else fprintf(stderr, "getaddrinfo(\"%s\"): %s\n", DISCOVER_IP, gai_strerror(rv)); #endif else { const int on = 1; SOCKET sock; if ((sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol)) == -1) socket_error("socket()"); else { size_t size = packet.size = snprintf(packet.data, sizeof(packet.data), "%s", DISCOVER_HS105); encrypt(); if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&on, sizeof(on)) == -1) socket_error("setsockopt()"); else if (sendto(sock, packet.data, size, 0, result->ai_addr, result->ai_addrlen) == -1) socket_error("sendto()"); else { while (1) { int len; struct sockaddr_in from; socklen_t fromlen; struct timeval timeout; fd_set readfds; FD_ZERO(&readfds); FD_SET(sock, &readfds); timeout.tv_sec = 3; timeout.tv_usec = 0; if ((len = select(sock + 1, &readfds, NULL, NULL, &timeout)) <= 0) { if (len == 0) rv = EXIT_SUCCESS; else socket_error("select()"); break; } fromlen = sizeof(from); if ((len = recvfrom(sock, packet.data, sizeof(packet.data), 0, (struct sockaddr *)&from, &fromlen)) == -1) { socket_error("recvfrom()"); break; } else if (len >= 0) { printf("[%s]\n", inet_ntoa(from.sin_addr)); if (len > 0) { packet.size = htonl(len); decrypt(); printf("%s\n", packet.data); } } } } closesocket(sock); } freeaddrinfo(result); } return rv; } void usage() { fprintf(stderr, "\n"); fprintf(stderr, "TP-Link Wi-Fi Smart Plug Client v" VERSION "\n"); fprintf(stderr, "\n"); fprintf(stderr, "Usage: tplink-smartplug [-h] -t <hostname> {-c <command> | -j <JSON string>}\n"); fprintf(stderr, "\n"); fprintf(stderr, "optional arguments:\n"); fprintf(stderr, " -h Show this help message and exit\n"); fprintf(stderr, " -d Device Discovery\n"); fprintf(stderr, " -t <hostname> Target hostname or IP address\n"); fprintf(stderr, " -c <command> Preset command to send. Choices are: info, on, off,\n"); fprintf(stderr, " cloudinfo, wlanscan, time, schedule, countdown,\n"); fprintf(stderr, " antitheft, reboot, reset, energy\n"); fprintf(stderr, " -j <JSON string> Full JSON string of command to send\n"); } #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) int main0(int argc, char **argv) #else int main(int argc, char **argv) #endif { char *host = ""; char *cmd = ""; char *json = ""; bool help = false; bool scan = false; int i, rv = EXIT_FAILURE; SOCKET sock; for (i = 1; i < argc; ) { char *p = argv[i++]; if (*p == '-') { switch (p[1]) { case 't': if (i < argc) host = argv[i++]; break; case 'c': if (i < argc) cmd = argv[i++]; break; case 'j': if (i < argc) json = argv[i++]; break; case 'd': scan = true; break; default: case 'h': help = true; break; } } else help = true; } if (*cmd) { for (i = 0; i < sizeof(commands) / sizeof(commands[0]); ++i) { if (strcmp(cmd, commands[i][0]) == 0) { json = (char *)commands[i][1]; break; } } } if (help || (!scan && (!*host || !*json))) usage(); else if (scan) { scan_smartplugs(); rv = EXIT_SUCCESS; } else if ((sock = connect_socket(host, PORT)) != -1) { rv = smartplug(sock, json); closesocket(sock); } return rv; } #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) int main(int argc, char **argv) { WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 0), &wsaData) == 0) { int rv = main0(argc, argv); WSACleanup(); return rv; } socket_error("WSAStartup()"); return EXIT_FAILURE; } #endif |
【関連投稿】
TP-LINK WiFiスマートプラグをESP32(Arduino)から制御する
TP-LINK WiFiスマートプラグをLinux/Windowsから直接制御する
TP-LINK TAPO P100/P105をESP32から直接制御する
TP-LINK TAPO P100/P105をLinux/Windowsから直接制御する