]> git.zerfleddert.de Git - hmcfgusb/blame - hmsniff.c
hotplug-dir needs to be created before copying stuff into it...
[hmcfgusb] / hmsniff.c
CommitLineData
d57fdaf6
MG
1/* HM-sniffer for HM-CFG-USB
2 *
3 * Copyright (c) 2013 Michael Gernoth <michael@gernoth.net>
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>
e2776af8 32#include <sys/time.h>
d57fdaf6
MG
33#include <libusb-1.0/libusb.h>
34
35#include "hexdump.h"
36#include "hmcfgusb.h"
37
38/* See HMConfig.pm */
39char *hm_message_types(uint8_t type)
40{
41 switch(type) {
42 case 0x00:
43 return "Device Info";
44 break;
45 case 0x01:
46 return "Configuration";
47 break;
48 case 0x02:
49 return "Acknowledge";
50 break;
51 case 0x03:
52 return "AES";
53 break;
54 case 0x04:
55 return "AES-Key";
56 break;
57 case 0x10:
58 return "Information";
59 break;
60 case 0x11:
61 return "SET";
62 break;
63 case 0x12:
64 return "HAVE_DATA";
65 break;
66 case 0x3e:
67 return "Switch";
68 break;
69 case 0x3f:
70 return "Timestamp";
71 break;
72 case 0x40:
73 return "Remote";
74 break;
75 case 0x41:
76 return "Sensor";
77 break;
78 case 0x53:
79 return "Water sensor";
80 break;
81 case 0x58:
82 return "Climate event";
83 break;
84 case 0x70:
85 return "Weather event";
86 break;
87 default:
88 return "?";
89 break;
90 }
91}
92
93static void dissect_hm(uint8_t *buf, int len)
94{
e2776af8
MG
95 struct timeval tv;
96 struct tm *tmp;
97 char ts[32];
d57fdaf6
MG
98 int i;
99
e2776af8
MG
100 gettimeofday(&tv, NULL);
101 tmp = localtime(&tv.tv_sec);
102 memset(ts, 0, sizeof(ts));
103 strftime(ts, sizeof(ts)-1, "%Y-%m-%d %H:%M:%S", tmp);
104 printf("%s.%06ld: ", ts, tv.tv_usec);
105
d57fdaf6
MG
106 for (i = 0; i < len; i++) {
107 printf("%02X", buf[i]);
108 }
109 printf("\n");
110 printf("Packet information:\n");
111 printf("\tLength: %u\n", buf[0]);
112 printf("\tMessage ID: %u\n", buf[1]);
113 printf("\tSender: %02x%02x%02x\n", buf[4], buf[5], buf[6]);
114 printf("\tReceiver: %02x%02x%02x\n", buf[7], buf[8], buf[9]);
115 printf("\tControl Byte: 0x%02x\n", buf[2]);
116 printf("\t\tFlags: ");
117 if (buf[2] & (1 << 0)) printf("WAKEUP ");
118 if (buf[2] & (1 << 1)) printf("WAKEMEUP ");
82922278 119 if (buf[2] & (1 << 2)) printf("CFG ");
d57fdaf6
MG
120 if (buf[2] & (1 << 3)) printf("? ");
121 if (buf[2] & (1 << 4)) printf("BURST ");
122 if (buf[2] & (1 << 5)) printf("BIDI ");
123 if (buf[2] & (1 << 6)) printf("RPTED ");
124 if (buf[2] & (1 << 7)) printf("RPTEN ");
125 printf("\n");
126 printf("\tMessage type: %s (0x%02x)\n", hm_message_types(buf[3]), buf[3]);
127 printf("\tMesage: ");
128 for (i = 10; i < len; i++) {
129 printf("%02X", buf[i]);
130 }
131 printf("\n");
132
133 printf("\n");
134
135}
136
885a84e3
MG
137struct recv_data {
138 int wrong_hmid;
139};
140
d57fdaf6
MG
141static void parse_hmcfgusb(uint8_t *buf, int buf_len, void *data)
142{
885a84e3
MG
143 struct recv_data *rdata = data;
144
d57fdaf6
MG
145 if (buf_len < 1)
146 return;
147
148 switch(buf[0]) {
149 case 'E':
150 dissect_hm(buf + 13, buf[13] + 1);
151 break;
152 case 'H':
885a84e3
MG
153 if ((buf[27] != 0x00) ||
154 (buf[28] != 0x00) ||
155 (buf[29] != 0x00)) {
156 printf("hmId is currently set to: %02x%02x%02x\n", buf[27], buf[28], buf[29]);
157 rdata->wrong_hmid = 1;
158 }
159 break;
d57fdaf6
MG
160 case 'R':
161 case 'I':
162 break;
163 default:
164 hexdump(buf, buf_len, "Unknown> ");
165 break;
166 }
167}
168
d57fdaf6
MG
169int main(int argc, char **argv)
170{
171 struct hmcfgusb_dev *dev;
885a84e3 172 struct recv_data rdata;
d57fdaf6
MG
173 int quit = 0;
174
175 hmcfgusb_set_debug(0);
176
885a84e3
MG
177 do {
178 memset(&rdata, 0, sizeof(rdata));
179 rdata.wrong_hmid = 0;
d57fdaf6 180
885a84e3
MG
181 dev = hmcfgusb_init(parse_hmcfgusb, &rdata);
182 if (!dev) {
183 fprintf(stderr, "Can't initialize HM-CFG-USB, retrying in 1s...\n");
184 sleep(1);
185 continue;
186 }
187 printf("HM-CFG-USB opened!\n");
d57fdaf6 188
885a84e3 189 hmcfgusb_send(dev, (unsigned char*)"K", 1, 1);
d57fdaf6 190
885a84e3
MG
191 while(!quit) {
192 int fd;
193
194 if (rdata.wrong_hmid) {
195 printf("changing hmId to 000000, this might reboot the device!\n");
196 hmcfgusb_send(dev, (unsigned char*)"A\00\00\00", 4, 1);
197 rdata.wrong_hmid = 0;
198 hmcfgusb_send(dev, (unsigned char*)"K", 1, 1);
199 }
200 fd = hmcfgusb_poll(dev, 10);
201 if (fd >= 0) {
202 fprintf(stderr, "activity on unknown fd %d!\n", fd);
203 continue;
204 } else if (fd == -1) {
205 if (errno) {
206 perror("hmcfgusb_poll");
207 break;
208 }
d57fdaf6
MG
209 }
210 }
d57fdaf6 211
885a84e3
MG
212 hmcfgusb_close(dev);
213 } while (!quit);
214
d57fdaf6
MG
215 return EXIT_SUCCESS;
216}
Impressum, Datenschutz