hmsniff: add power event
[hmcfgusb] / hmsniff.c
CommitLineData
d57fdaf6
MG
1/* HM-sniffer for HM-CFG-USB
2 *
cd45e4af 3 * Copyright (c) 2013-15 Michael Gernoth <michael@gernoth.net>
d57fdaf6
MG
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <stdint.h>
28#include <string.h>
29#include <strings.h>
30#include <poll.h>
31#include <errno.h>
cd45e4af 32#include <time.h>
e2776af8 33#include <sys/time.h>
d57fdaf6
MG
34#include <libusb-1.0/libusb.h>
35
c44f15b8 36#include "version.h"
d57fdaf6
MG
37#include "hexdump.h"
38#include "hmcfgusb.h"
39
cd45e4af
MG
40static int verbose = 0;
41
d57fdaf6 42/* See HMConfig.pm */
cd45e4af 43char *hm_message_types(uint8_t type, uint8_t subtype)
d57fdaf6
MG
44{
45 switch(type) {
46 case 0x00:
47 return "Device Info";
48 break;
49 case 0x01:
50 return "Configuration";
51 break;
52 case 0x02:
cd45e4af
MG
53 if (subtype >= 0x80 && subtype <= 0x8f) {
54 return "NACK";
55 } else if (subtype == 0x01) {
56 return "ACKinfo";
57 } else if (subtype == 0x04) {
58 return "AESrequest";
59 }
60 return "ACK";
d57fdaf6
MG
61 break;
62 case 0x03:
cd45e4af 63 return "AESreply";
d57fdaf6
MG
64 break;
65 case 0x04:
cd45e4af 66 return "AESkey";
d57fdaf6
MG
67 break;
68 case 0x10:
69 return "Information";
70 break;
71 case 0x11:
72 return "SET";
73 break;
74 case 0x12:
75 return "HAVE_DATA";
76 break;
77 case 0x3e:
78 return "Switch";
79 break;
80 case 0x3f:
81 return "Timestamp";
82 break;
83 case 0x40:
84 return "Remote";
85 break;
86 case 0x41:
87 return "Sensor";
88 break;
89 case 0x53:
90 return "Water sensor";
91 break;
92 case 0x58:
93 return "Climate event";
94 break;
cd45e4af
MG
95 case 0x5a:
96 return "Thermal control";
97 break;
920d34e0
MG
98 case 0x5e:
99 return "Power event";
100 break;
d57fdaf6
MG
101 case 0x70:
102 return "Weather event";
103 break;
104 default:
105 return "?";
106 break;
107 }
108}
109
110static void dissect_hm(uint8_t *buf, int len)
111{
e2776af8
MG
112 struct timeval tv;
113 struct tm *tmp;
114 char ts[32];
cd45e4af 115 static int count = 0;
d57fdaf6
MG
116 int i;
117
e2776af8
MG
118 gettimeofday(&tv, NULL);
119 tmp = localtime(&tv.tv_sec);
120 memset(ts, 0, sizeof(ts));
121 strftime(ts, sizeof(ts)-1, "%Y-%m-%d %H:%M:%S", tmp);
e2776af8 122
cd45e4af
MG
123 if (verbose) {
124 printf("%s.%06ld: ", ts, tv.tv_usec);
125
126 for (i = 0; i < len; i++) {
127 printf("%02X", buf[i]);
128 }
129 printf("\n");
130 printf("Packet information:\n");
131 printf("\tLength: %u\n", buf[0]);
132 printf("\tMessage ID: %u\n", buf[1]);
133 printf("\tSender: %02x%02x%02x\n", buf[4], buf[5], buf[6]);
134 printf("\tReceiver: %02x%02x%02x\n", buf[7], buf[8], buf[9]);
135 printf("\tControl Byte: 0x%02x\n", buf[2]);
136 printf("\t\tFlags: ");
137 if (buf[2] & (1 << 0)) printf("WAKEUP ");
138 if (buf[2] & (1 << 1)) printf("WAKEMEUP ");
139 if (buf[2] & (1 << 2)) printf("CFG ");
140 if (buf[2] & (1 << 3)) printf("? ");
141 if (buf[2] & (1 << 4)) printf("BURST ");
142 if (buf[2] & (1 << 5)) printf("BIDI ");
143 if (buf[2] & (1 << 6)) printf("RPTED ");
144 if (buf[2] & (1 << 7)) printf("RPTEN ");
145 printf("\n");
146 printf("\tMessage type: %s (0x%02x 0x%02x)\n", hm_message_types(buf[3], buf[10]), buf[3], buf[10]);
147 printf("\tMessage: ");
148 for (i = 10; i < len; i++) {
149 printf("%02X", buf[i]);
150 }
151 printf("\n");
152
153 printf("\n");
154 } else {
155 if (!(count++ % 20))
156 printf(" LL NR FL CM sender recvr payload\n");
920d34e0 157
cd45e4af
MG
158 printf("%s.%03ld: %02X %02X %02X %02X %02X%02X%02X %02X%02X%02X ",
159 ts, tv.tv_usec/1000,
160 buf[0], buf[1], buf[2], buf[3],
161 buf[4], buf[5], buf[6],
162 buf[7], buf[8], buf[9]);
163
164 for (i = 10; i < len; i++) {
165 printf("%02X", buf[i]);
166 }
167 printf("%s(%s)\n", (i>10)?" ":"", hm_message_types(buf[3], buf[10]));
d57fdaf6 168 }
d57fdaf6
MG
169}
170
885a84e3
MG
171struct recv_data {
172 int wrong_hmid;
173};
174
4371275b 175static int parse_hmcfgusb(uint8_t *buf, int buf_len, void *data)
d57fdaf6 176{
885a84e3
MG
177 struct recv_data *rdata = data;
178
d57fdaf6 179 if (buf_len < 1)
4371275b 180 return 1;
d57fdaf6
MG
181
182 switch(buf[0]) {
183 case 'E':
184 dissect_hm(buf + 13, buf[13] + 1);
185 break;
186 case 'H':
885a84e3
MG
187 if ((buf[27] != 0x00) ||
188 (buf[28] != 0x00) ||
189 (buf[29] != 0x00)) {
190 printf("hmId is currently set to: %02x%02x%02x\n", buf[27], buf[28], buf[29]);
191 rdata->wrong_hmid = 1;
192 }
193 break;
d57fdaf6
MG
194 case 'R':
195 case 'I':
196 break;
197 default:
198 hexdump(buf, buf_len, "Unknown> ");
199 break;
200 }
4371275b
MG
201
202 return 1;
d57fdaf6
MG
203}
204
cd45e4af
MG
205void hmsniff_syntax(char *prog)
206{
207 fprintf(stderr, "Syntax: %s options\n\n", prog);
208 fprintf(stderr, "Possible options:\n");
209 fprintf(stderr, "\t-v\t\tverbose mode\n");
210 fprintf(stderr, "\t-V\t\tshow version (" VERSION ")\n");
211
212}
213
d57fdaf6
MG
214int main(int argc, char **argv)
215{
216 struct hmcfgusb_dev *dev;
885a84e3 217 struct recv_data rdata;
d57fdaf6 218 int quit = 0;
cd45e4af
MG
219 int opt;
220
221 while((opt = getopt(argc, argv, "vV")) != -1) {
222 switch (opt) {
223 case 'v':
224 verbose = 1;
225 break;
226 case 'V':
227 printf("hmsniff " VERSION "\n");
228 printf("Copyright (c) 2013-15 Michael Gernoth\n\n");
229 exit(EXIT_SUCCESS);
230 case 'h':
231 case ':':
232 case '?':
233 default:
234 hmsniff_syntax(argv[0]);
235 exit(EXIT_FAILURE);
236 break;
237 }
238 }
d57fdaf6
MG
239
240 hmcfgusb_set_debug(0);
241
885a84e3
MG
242 do {
243 memset(&rdata, 0, sizeof(rdata));
244 rdata.wrong_hmid = 0;
d57fdaf6 245
885a84e3
MG
246 dev = hmcfgusb_init(parse_hmcfgusb, &rdata);
247 if (!dev) {
248 fprintf(stderr, "Can't initialize HM-CFG-USB, retrying in 1s...\n");
249 sleep(1);
250 continue;
251 }
252 printf("HM-CFG-USB opened!\n");
d57fdaf6 253
6262005e 254 hmcfgusb_send_null_frame(dev, 1);
885a84e3 255 hmcfgusb_send(dev, (unsigned char*)"K", 1, 1);
d57fdaf6 256
885a84e3
MG
257 while(!quit) {
258 int fd;
259
260 if (rdata.wrong_hmid) {
261 printf("changing hmId to 000000, this might reboot the device!\n");
262 hmcfgusb_send(dev, (unsigned char*)"A\00\00\00", 4, 1);
263 rdata.wrong_hmid = 0;
264 hmcfgusb_send(dev, (unsigned char*)"K", 1, 1);
265 }
3b35a8c1 266 fd = hmcfgusb_poll(dev, 1000);
885a84e3
MG
267 if (fd >= 0) {
268 fprintf(stderr, "activity on unknown fd %d!\n", fd);
269 continue;
270 } else if (fd == -1) {
271 if (errno) {
1e79d00a
MG
272 if (errno != ETIMEDOUT) {
273 perror("hmcfgusb_poll");
274 break;
275 } else {
276 /* periodically wakeup the device */
277 hmcfgusb_send_null_frame(dev, 1);
278 }
885a84e3 279 }
d57fdaf6
MG
280 }
281 }
d57fdaf6 282
885a84e3
MG
283 hmcfgusb_close(dev);
284 } while (!quit);
285
018f85fa
MG
286 hmcfgusb_exit();
287
d57fdaf6
MG
288 return EXIT_SUCCESS;
289}
Impressum, Datenschutz