fix cfg-flag
[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>
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <netinet/in.h>
35#include <arpa/inet.h>
36#include <libusb-1.0/libusb.h>
37
38#include "hexdump.h"
39#include "hmcfgusb.h"
40
41/* See HMConfig.pm */
42char *hm_message_types(uint8_t type)
43{
44 switch(type) {
45 case 0x00:
46 return "Device Info";
47 break;
48 case 0x01:
49 return "Configuration";
50 break;
51 case 0x02:
52 return "Acknowledge";
53 break;
54 case 0x03:
55 return "AES";
56 break;
57 case 0x04:
58 return "AES-Key";
59 break;
60 case 0x10:
61 return "Information";
62 break;
63 case 0x11:
64 return "SET";
65 break;
66 case 0x12:
67 return "HAVE_DATA";
68 break;
69 case 0x3e:
70 return "Switch";
71 break;
72 case 0x3f:
73 return "Timestamp";
74 break;
75 case 0x40:
76 return "Remote";
77 break;
78 case 0x41:
79 return "Sensor";
80 break;
81 case 0x53:
82 return "Water sensor";
83 break;
84 case 0x58:
85 return "Climate event";
86 break;
87 case 0x70:
88 return "Weather event";
89 break;
90 default:
91 return "?";
92 break;
93 }
94}
95
96static void dissect_hm(uint8_t *buf, int len)
97{
98 int i;
99
100 for (i = 0; i < len; i++) {
101 printf("%02X", buf[i]);
102 }
103 printf("\n");
104 printf("Packet information:\n");
105 printf("\tLength: %u\n", buf[0]);
106 printf("\tMessage ID: %u\n", buf[1]);
107 printf("\tSender: %02x%02x%02x\n", buf[4], buf[5], buf[6]);
108 printf("\tReceiver: %02x%02x%02x\n", buf[7], buf[8], buf[9]);
109 printf("\tControl Byte: 0x%02x\n", buf[2]);
110 printf("\t\tFlags: ");
111 if (buf[2] & (1 << 0)) printf("WAKEUP ");
112 if (buf[2] & (1 << 1)) printf("WAKEMEUP ");
82922278 113 if (buf[2] & (1 << 2)) printf("CFG ");
d57fdaf6
MG
114 if (buf[2] & (1 << 3)) printf("? ");
115 if (buf[2] & (1 << 4)) printf("BURST ");
116 if (buf[2] & (1 << 5)) printf("BIDI ");
117 if (buf[2] & (1 << 6)) printf("RPTED ");
118 if (buf[2] & (1 << 7)) printf("RPTEN ");
119 printf("\n");
120 printf("\tMessage type: %s (0x%02x)\n", hm_message_types(buf[3]), buf[3]);
121 printf("\tMesage: ");
122 for (i = 10; i < len; i++) {
123 printf("%02X", buf[i]);
124 }
125 printf("\n");
126
127 printf("\n");
128
129}
130
131static void parse_hmcfgusb(uint8_t *buf, int buf_len, void *data)
132{
133 if (buf_len < 1)
134 return;
135
136 switch(buf[0]) {
137 case 'E':
138 dissect_hm(buf + 13, buf[13] + 1);
139 break;
140 case 'H':
141 case 'R':
142 case 'I':
143 break;
144 default:
145 hexdump(buf, buf_len, "Unknown> ");
146 break;
147 }
148}
149
150
151int main(int argc, char **argv)
152{
153 struct hmcfgusb_dev *dev;
154 int quit = 0;
155
156 hmcfgusb_set_debug(0);
157
158 dev = hmcfgusb_init(parse_hmcfgusb, NULL);
159 if (!dev) {
160 fprintf(stderr, "Can't initialize HM-CFG-USB!\n");
161 return EXIT_FAILURE;
162 }
163
164 hmcfgusb_send(dev, (unsigned char*)"A\00\00\00", 3, 1);
165
166 while(!quit) {
167 int fd;
168
169 fd = hmcfgusb_poll(dev, 3600);
170 if (fd >= 0) {
171 fprintf(stderr, "activity on unknown fd %d!\n", fd);
172 continue;
173 } else if (fd == -1) {
174 if (errno) {
175 perror("hmcfgusb_poll");
176 quit = 1;
177 }
178 }
179 }
180
181 hmcfgusb_close(dev);
182 return EXIT_SUCCESS;
183}
Impressum, Datenschutz