]>
cvs.zerfleddert.de Git - hmcfgusb/blob - hmsniff.c
1 /* HM-sniffer for HM-CFG-USB
3 * Copyright (c) 2013 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
33 #include <libusb-1.0/libusb.h>
40 char *hm_message_types(uint8_t type
)
47 return "Configuration";
80 return "Water sensor";
83 return "Climate event";
86 return "Weather event";
94 static void dissect_hm(uint8_t *buf
, int len
)
101 gettimeofday(&tv
, NULL
);
102 tmp
= localtime(&tv
.tv_sec
);
103 memset(ts
, 0, sizeof(ts
));
104 strftime(ts
, sizeof(ts
)-1, "%Y-%m-%d %H:%M:%S", tmp
);
105 printf("%s.%06ld: ", ts
, tv
.tv_usec
);
107 for (i
= 0; i
< len
; i
++) {
108 printf("%02X", buf
[i
]);
111 printf("Packet information:\n");
112 printf("\tLength: %u\n", buf
[0]);
113 printf("\tMessage ID: %u\n", buf
[1]);
114 printf("\tSender: %02x%02x%02x\n", buf
[4], buf
[5], buf
[6]);
115 printf("\tReceiver: %02x%02x%02x\n", buf
[7], buf
[8], buf
[9]);
116 printf("\tControl Byte: 0x%02x\n", buf
[2]);
117 printf("\t\tFlags: ");
118 if (buf
[2] & (1 << 0)) printf("WAKEUP ");
119 if (buf
[2] & (1 << 1)) printf("WAKEMEUP ");
120 if (buf
[2] & (1 << 2)) printf("CFG ");
121 if (buf
[2] & (1 << 3)) printf("? ");
122 if (buf
[2] & (1 << 4)) printf("BURST ");
123 if (buf
[2] & (1 << 5)) printf("BIDI ");
124 if (buf
[2] & (1 << 6)) printf("RPTED ");
125 if (buf
[2] & (1 << 7)) printf("RPTEN ");
127 printf("\tMessage type: %s (0x%02x)\n", hm_message_types(buf
[3]), buf
[3]);
128 printf("\tMesage: ");
129 for (i
= 10; i
< len
; i
++) {
130 printf("%02X", buf
[i
]);
142 static int parse_hmcfgusb(uint8_t *buf
, int buf_len
, void *data
)
144 struct recv_data
*rdata
= data
;
151 dissect_hm(buf
+ 13, buf
[13] + 1);
154 if ((buf
[27] != 0x00) ||
157 printf("hmId is currently set to: %02x%02x%02x\n", buf
[27], buf
[28], buf
[29]);
158 rdata
->wrong_hmid
= 1;
165 hexdump(buf
, buf_len
, "Unknown> ");
172 int main(int argc
, char **argv
)
174 struct hmcfgusb_dev
*dev
;
175 struct recv_data rdata
;
178 hmcfgusb_set_debug(0);
181 memset(&rdata
, 0, sizeof(rdata
));
182 rdata
.wrong_hmid
= 0;
184 dev
= hmcfgusb_init(parse_hmcfgusb
, &rdata
);
186 fprintf(stderr
, "Can't initialize HM-CFG-USB, retrying in 1s...\n");
190 printf("HM-CFG-USB opened!\n");
192 hmcfgusb_send_null_frame(dev
, 1);
193 hmcfgusb_send(dev
, (unsigned char*)"K", 1, 1);
198 if (rdata
.wrong_hmid
) {
199 printf("changing hmId to 000000, this might reboot the device!\n");
200 hmcfgusb_send(dev
, (unsigned char*)"A\00\00\00", 4, 1);
201 rdata
.wrong_hmid
= 0;
202 hmcfgusb_send(dev
, (unsigned char*)"K", 1, 1);
204 fd
= hmcfgusb_poll(dev
, 1000);
206 fprintf(stderr
, "activity on unknown fd %d!\n", fd
);
208 } else if (fd
== -1) {
210 if (errno
!= ETIMEDOUT
) {
211 perror("hmcfgusb_poll");
214 /* periodically wakeup the device */
215 hmcfgusb_send_null_frame(dev
, 1);