-
Notifications
You must be signed in to change notification settings - Fork 0
/
w1d.c
369 lines (294 loc) · 7.88 KB
/
w1d.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* w1d.c
*
* Copyright (c) 2004 Evgeniy Polyakov <[email protected]>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <asm/byteorder.h>
#include <asm/types.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include "w1_netlink.h"
static int need_exit;
static int send_seq;
static int send_cmd(FILE *out, int s, struct w1_netlink_msg *msg)
{
struct cn_msg *cmsg;
struct w1_netlink_msg *m;
struct nlmsghdr *nlh;
int size, err;
size = NLMSG_SPACE(sizeof(struct cn_msg) + sizeof(struct w1_netlink_msg) + msg->len);
nlh = malloc(size);
if (!nlh)
return -ENOMEM;
memset(nlh, 0, size);
nlh->nlmsg_seq = send_seq++;
nlh->nlmsg_pid = getpid();
nlh->nlmsg_type = NLMSG_DONE;
nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(struct nlmsghdr));
nlh->nlmsg_flags = 0;
cmsg = NLMSG_DATA(nlh);
cmsg->id.idx = CN_W1_IDX;
cmsg->id.val = CN_W1_VAL;
cmsg->seq = nlh->nlmsg_seq;
cmsg->ack = 0;
cmsg->len = sizeof(struct w1_netlink_msg) + msg->len;
m = (struct w1_netlink_msg *)(cmsg + 1);
memcpy(m, msg, sizeof(struct w1_netlink_msg));
memcpy(m+1, msg->data, msg->len);
err = send(s, nlh, size, 0);
if (err == -1) {
fprintf(out, "Failed to send: %s [%d].\n", strerror(errno), errno);
free(nlh);
return err;
}
free(nlh);
return err;
}
static int w1_test_create_cmd(struct w1_netlink_msg *m, __u16 maxlen)
{
char *cmd_data;
__u16 i, newlen;
struct w1_netlink_cmd *cmd;
cmd_data = (char *)m->data;
for (i=0; i<3; ++i) {
newlen = (i+1)*10;
if (newlen + sizeof(struct w1_netlink_cmd) + m->len > maxlen)
break;
cmd = (struct w1_netlink_cmd *)cmd_data;
cmd->cmd = i;
cmd->len = newlen;
cmd_data += cmd->len + sizeof(struct w1_netlink_cmd);
m->len += cmd->len + sizeof(struct w1_netlink_cmd);
}
return m->len;
}
static int w1_test_cmd_master(FILE *out, int s, __u32 id)
{
char buf[1024];
struct w1_netlink_msg *m;
memset(buf, 0, sizeof(buf));
m = (struct w1_netlink_msg *)buf;
m->type = W1_MASTER_CMD;
m->len = 0;
m->id.mst.id = id;
w1_test_create_cmd(m, sizeof(buf) - sizeof(struct w1_netlink_msg));
return send_cmd(out, s, m);
}
static int w1_test_cmd_slave(FILE *out, int s, struct w1_reg_num *id)
{
char buf[1024];
struct w1_netlink_msg *m;
struct w1_netlink_cmd *cmd;
char *cmd_data;
int i;
memset(buf, 0, sizeof(buf));
m = (struct w1_netlink_msg *)buf;
m->type = W1_SLAVE_CMD;
m->len = 0;
memcpy(m->id.id, id, sizeof(m->id.id));
cmd_data = (char *)m->data;
for (i=0; i<3; ++i) {
cmd = (struct w1_netlink_cmd *)cmd_data;
cmd->cmd = i;
cmd->len = (i+1)*10;
cmd_data += cmd->len + sizeof(struct w1_netlink_cmd);
m->len += cmd->len + sizeof(struct w1_netlink_cmd);
}
return send_cmd(out, s, m);
}
static void w1_dump_reply(FILE *out, char *prefix, struct w1_netlink_msg *hdr)
{
struct w1_netlink_cmd *cmd;
unsigned char *hdr_data = hdr->data;
unsigned int hdr_len = hdr->len;
time_t tm;
int i;
time(&tm);
while (hdr_len) {
cmd = (struct w1_netlink_cmd *)hdr_data;
fprintf(out, "%.24s : %s ", ctime(&tm), prefix);
if (cmd->len + sizeof(struct w1_netlink_cmd) > hdr_len) {
fprintf(out, "Malformed message.\n");
break;
}
switch (cmd->cmd) {
case W1_CMD_READ:
fprintf(out, "READ: ");
for (i=0; i<cmd->len; ++i)
fprintf(out, "%02x ", cmd->data[i]);
fprintf(out, "\n");
break;
default:
fprintf(out, "cmd=%02x, len=%u.\n", cmd->cmd, cmd->len);
break;
}
hdr_data += cmd->len + sizeof(struct w1_netlink_cmd);
hdr_len -= cmd->len + sizeof(struct w1_netlink_cmd);
}
}
static void w1_parse_master_reply(FILE *out, struct w1_netlink_msg *hdr)
{
char prefix[128];
snprintf(prefix, sizeof(prefix), "master: id=%08x", hdr->id.mst.id);
w1_dump_reply(out, prefix, hdr);
}
static void w1_parse_slave_reply(FILE *out, struct w1_netlink_msg *hdr)
{
char prefix[128];
struct w1_reg_num id;
memcpy(&id, hdr->id.id, sizeof(id));
snprintf(prefix, sizeof(prefix), "slave: id=%02x.%012llx.%02x",
id.family, (unsigned long long)id.id, id.crc);
w1_dump_reply(out, prefix, hdr);
}
void w1_dump_message(FILE *out, int s, struct cn_msg *msg)
{
time_t tm;
struct w1_netlink_msg *data = (struct w1_netlink_msg *)(msg + 1);
unsigned int i;
while (msg->len) {
struct w1_reg_num id;
time(&tm);
#if 0
fprintf(out, "%.24s : %08x.%08x, len=%u, seq=%u, ack=%u, data->len=%u.\n",
ctime(&tm), msg->id.idx, msg->id.val, msg->len, msg->seq, msg->ack, data->len);
#endif
switch (data->type) {
case W1_MASTER_ADD:
case W1_MASTER_REMOVE:
if (data->type == W1_MASTER_ADD)
w1_test_cmd_master(out, s, data->id.mst.id);
fprintf(out, "%.24s : master has been %.8s: id=%08x.\n",
ctime(&tm),
(data->type == W1_MASTER_ADD)?"added":"removed",
data->id.mst.id);
break;
case W1_SLAVE_ADD:
case W1_SLAVE_REMOVE:
memcpy(&id, data->id.id, sizeof(id));
if (data->type == W1_SLAVE_ADD)
w1_test_cmd_slave(out, s, &id);
fprintf(out, "%.24s : slave has been %.8s: id=%02x.%012llx.%02x\n",
ctime(&tm),
(data->type == W1_SLAVE_ADD)?"added":"removed",
id.family, (unsigned long long)id.id, id.crc);
break;
case W1_MASTER_CMD:
w1_parse_master_reply(out, data);
break;
case W1_SLAVE_CMD:
w1_parse_slave_reply(out, data);
break;
default:
fprintf(out, "%.24s : type=%02x", ctime(&tm), data->type);
for (i=0; i<sizeof(data->id.id); ++i)
fprintf(out, "%02x.", data->id.id[i]);
fprintf(out, "\n");
}
fflush(out);
msg->len -= sizeof(struct w1_netlink_msg) + data->len;
data = (struct w1_netlink_msg *)(((char *)data) + sizeof(struct w1_netlink_msg) + data->len);
}
}
int main(int argc, char *argv[])
{
int s;
unsigned char buf[1024];
int len;
struct sockaddr_nl l_local;
struct cn_msg *msg;
FILE *out;
struct pollfd pfd;
struct nlmsghdr *reply;
if (argc < 2)
out = stdout;
else {
out = fopen(argv[1], "a+");
if (!out) {
fprintf(stderr, "Unable to open %s for writing: %s\n",
argv[1], strerror(errno));
out = stdout;
}
}
memset(buf, 0, sizeof(buf));
s = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
if (s == -1) {
perror("socket");
return -1;
}
l_local.nl_family = AF_NETLINK;
l_local.nl_groups = 23;
l_local.nl_pid = getpid();
if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) {
perror("bind");
close(s);
return -1;
}
pfd.fd = s;
while (!need_exit)
{
pfd.events = POLLIN;
pfd.revents = 0;
switch (poll(&pfd, 1, -1)) {
case 0:
need_exit = 1;
break;
case -1:
if (errno != EINTR) {
need_exit = 1;
break;
}
continue;
default:
break;
}
if (need_exit)
break;
len = recv(s, buf, sizeof(buf), 0);
if (len == -1) {
perror("recv buf");
close(s);
return -1;
}
reply = (struct nlmsghdr *)buf;
switch (reply->nlmsg_type) {
case NLMSG_ERROR:
fprintf(out, "Error message received.\n");
fflush(out);
break;
case NLMSG_DONE:
msg = (struct cn_msg *)NLMSG_DATA(reply);
w1_dump_message(out, s, msg);
break;
default:
break;
}
}
close(s);
return 0;
}