]>
git.zerfleddert.de Git - hmcfgusb/blob - hmsniff.c
051c95cde524be68d0ea1a636e02a7ee81f71a40
1 /* HM-sniffer for HM-CFG-USB
3 * Copyright (c) 2013-16 Michael Gernoth <michael@gernoth.net>
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
34 #include <libusb-1.0/libusb.h>
40 static int verbose
= 0;
43 char *hm_message_types(uint8_t type
, uint8_t subtype
)
50 return "Configuration";
53 if (subtype
>= 0x80 && subtype
<= 0x8f) {
55 } else if (subtype
== 0x01) {
57 } else if (subtype
== 0x04) {
90 return "Water sensor";
93 return "Climate event";
96 return "Thermal control";
102 return "Weather event";
108 return "Rf configuration";
116 static void dissect_hm(uint8_t *buf
, int len
)
121 static int count
= 0;
124 gettimeofday(&tv
, NULL
);
125 tmp
= localtime(&tv
.tv_sec
);
126 memset(ts
, 0, sizeof(ts
));
127 strftime(ts
, sizeof(ts
)-1, "%Y-%m-%d %H:%M:%S", tmp
);
130 printf("%s.%06ld: ", ts
, tv
.tv_usec
);
132 for (i
= 0; i
< len
; i
++) {
133 printf("%02X", buf
[i
]);
136 printf("Packet information:\n");
137 printf("\tLength: %u\n", buf
[0]);
138 printf("\tMessage ID: %u\n", buf
[1]);
139 printf("\tSender: %02x%02x%02x\n", buf
[4], buf
[5], buf
[6]);
140 printf("\tReceiver: %02x%02x%02x\n", buf
[7], buf
[8], buf
[9]);
141 printf("\tControl Byte: 0x%02x\n", buf
[2]);
142 printf("\t\tFlags: ");
143 if (buf
[2] & (1 << 0)) printf("WAKEUP ");
144 if (buf
[2] & (1 << 1)) printf("WAKEMEUP ");
145 if (buf
[2] & (1 << 2)) printf("CFG ");
146 if (buf
[2] & (1 << 3)) printf("? ");
147 if (buf
[2] & (1 << 4)) printf("BURST ");
148 if (buf
[2] & (1 << 5)) printf("BIDI ");
149 if (buf
[2] & (1 << 6)) printf("RPTED ");
150 if (buf
[2] & (1 << 7)) printf("RPTEN ");
152 printf("\tMessage type: %s (0x%02x 0x%02x)\n", hm_message_types(buf
[3], buf
[10]), buf
[3], buf
[10]);
153 printf("\tMessage: ");
154 for (i
= 10; i
< len
; i
++) {
155 printf("%02X", buf
[i
]);
162 printf(" LL NR FL CM sender recvr payload\n");
164 printf("%s.%03ld: %02X %02X %02X %02X %02X%02X%02X %02X%02X%02X ",
166 buf
[0], buf
[1], buf
[2], buf
[3],
167 buf
[4], buf
[5], buf
[6],
168 buf
[7], buf
[8], buf
[9]);
170 for (i
= 10; i
< len
; i
++) {
171 printf("%02X", buf
[i
]);
173 printf("%s(%s)\n", (i
>10)?" ":"", hm_message_types(buf
[3], buf
[10]));
181 static int parse_hmcfgusb(uint8_t *buf
, int buf_len
, void *data
)
183 struct recv_data
*rdata
= data
;
190 dissect_hm(buf
+ 13, buf
[13] + 1);
193 if ((buf
[27] != 0x00) ||
196 printf("hmId is currently set to: %02x%02x%02x\n", buf
[27], buf
[28], buf
[29]);
197 rdata
->wrong_hmid
= 1;
205 hexdump(buf
, buf_len
, "Unknown> ");
212 void hmsniff_syntax(char *prog
)
214 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
215 fprintf(stderr
, "Possible options:\n");
216 fprintf(stderr
, "\t-f\t\tfast (100k/firmware update) mode\n");
217 fprintf(stderr
, "\t-S serial\tuse HM-CFG-USB with given serial\n");
218 fprintf(stderr
, "\t-v\t\tverbose mode\n");
219 fprintf(stderr
, "\t-V\t\tshow version (" VERSION
")\n");
223 int main(int argc
, char **argv
)
225 struct hmcfgusb_dev
*dev
;
226 struct recv_data rdata
;
230 uint8_t speed_buf
[2];
233 while((opt
= getopt(argc
, argv
, "fS:vV")) != -1) {
245 printf("hmsniff " VERSION
"\n");
246 printf("Copyright (c) 2013-16 Michael Gernoth\n\n");
252 hmsniff_syntax(argv
[0]);
258 hmcfgusb_set_debug(0);
261 memset(&rdata
, 0, sizeof(rdata
));
262 rdata
.wrong_hmid
= 0;
264 dev
= hmcfgusb_init(parse_hmcfgusb
, &rdata
, serial
);
266 fprintf(stderr
, "Can't initialize HM-CFG-USB, retrying in 1s...\n");
270 printf("HM-CFG-USB opened!\n");
272 hmcfgusb_send_null_frame(dev
, 1);
273 hmcfgusb_send(dev
, (unsigned char*)"K", 1, 1);
275 hmcfgusb_send_null_frame(dev
, 1);
277 speed_buf
[1] = speed
;
278 hmcfgusb_send(dev
, speed_buf
, 2, 1);
283 if (rdata
.wrong_hmid
) {
284 printf("changing hmId to 000000, this might reboot the device!\n");
285 hmcfgusb_send(dev
, (unsigned char*)"A\00\00\00", 4, 1);
286 rdata
.wrong_hmid
= 0;
287 hmcfgusb_send(dev
, (unsigned char*)"K", 1, 1);
289 fd
= hmcfgusb_poll(dev
, 1000);
291 fprintf(stderr
, "activity on unknown fd %d!\n", fd
);
293 } else if (fd
== -1) {
295 if (errno
!= ETIMEDOUT
) {
296 perror("hmcfgusb_poll");
299 /* periodically wakeup the device */
300 hmcfgusb_send_null_frame(dev
, 1);