blob: 32c48556170bc1892499605bd00812abbb0c918b [file] [log] [blame]
Stefan Richter9f6d3c42010-07-22 11:58:05 +02001/*
2 * nosy-dump - Interface to snoop mode driver for TI PCILynx 1394 controllers
3 * Copyright (C) 2002-2006 Kristian Høgsberg
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
Stefan Richter92c16f72010-07-22 11:58:05 +020020#include <byteswap.h>
21#include <endian.h>
Stefan Richter9f6d3c42010-07-22 11:58:05 +020022#include <fcntl.h>
Stefan Richter92c16f72010-07-22 11:58:05 +020023#include <poll.h>
24#include <popt.h>
25#include <signal.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Stefan Richter9f6d3c42010-07-22 11:58:05 +020029#include <sys/ioctl.h>
30#include <sys/time.h>
Stefan Richter9f6d3c42010-07-22 11:58:05 +020031#include <termios.h>
Stefan Richter92c16f72010-07-22 11:58:05 +020032#include <unistd.h>
Stefan Richter9f6d3c42010-07-22 11:58:05 +020033
34#include "list.h"
Stefan Richter9f6d3c42010-07-22 11:58:05 +020035#include "nosy-dump.h"
Stefan Richter92c16f72010-07-22 11:58:05 +020036#include "nosy-user.h"
Stefan Richter9f6d3c42010-07-22 11:58:05 +020037
38enum {
Stefan Richter92c16f72010-07-22 11:58:05 +020039 PACKET_FIELD_DETAIL = 0x01,
40 PACKET_FIELD_DATA_LENGTH = 0x02,
41 /* Marks the fields we print in transaction view. */
42 PACKET_FIELD_TRANSACTION = 0x04,
Stefan Richter9f6d3c42010-07-22 11:58:05 +020043};
44
Stefan Richter92c16f72010-07-22 11:58:05 +020045static void print_packet(uint32_t *data, size_t length);
46static void decode_link_packet(struct link_packet *packet, size_t length,
47 int include_flags, int exclude_flags);
48static int run = 1;
Stefan Richter9f6d3c42010-07-22 11:58:05 +020049sig_t sys_sigint_handler;
50
51static char *option_nosy_device = "/dev/nosy";
52static char *option_view = "packet";
Stefan Richter92c16f72010-07-22 11:58:05 +020053static char *option_output;
54static char *option_input;
55static int option_hex;
56static int option_iso;
57static int option_cycle_start;
58static int option_version;
59static int option_verbose;
Stefan Richter9f6d3c42010-07-22 11:58:05 +020060
61enum {
Stefan Richter92c16f72010-07-22 11:58:05 +020062 VIEW_TRANSACTION,
63 VIEW_PACKET,
64 VIEW_STATS,
Stefan Richter9f6d3c42010-07-22 11:58:05 +020065};
66
67static const struct poptOption options[] = {
Stefan Richter92c16f72010-07-22 11:58:05 +020068 {
69 .longName = "device",
70 .shortName = 'd',
71 .argInfo = POPT_ARG_STRING,
72 .arg = &option_nosy_device,
73 .descrip = "Path to nosy device.",
74 .argDescrip = "DEVICE"
75 },
76 {
77 .longName = "view",
78 .argInfo = POPT_ARG_STRING,
79 .arg = &option_view,
80 .descrip = "Specify view of bus traffic: packet, transaction or stats.",
81 .argDescrip = "VIEW"
82 },
83 {
84 .longName = "hex",
85 .shortName = 'x',
86 .argInfo = POPT_ARG_NONE,
87 .arg = &option_hex,
88 .descrip = "Print each packet in hex.",
89 },
90 {
91 .longName = "iso",
92 .argInfo = POPT_ARG_NONE,
93 .arg = &option_iso,
94 .descrip = "Print iso packets.",
95 },
96 {
97 .longName = "cycle-start",
98 .argInfo = POPT_ARG_NONE,
99 .arg = &option_cycle_start,
100 .descrip = "Print cycle start packets.",
101 },
102 {
103 .longName = "verbose",
104 .shortName = 'v',
105 .argInfo = POPT_ARG_NONE,
106 .arg = &option_verbose,
107 .descrip = "Verbose packet view.",
108 },
109 {
110 .longName = "output",
111 .shortName = 'o',
112 .argInfo = POPT_ARG_STRING,
113 .arg = &option_output,
114 .descrip = "Log to output file.",
115 .argDescrip = "FILENAME"
116 },
117 {
118 .longName = "input",
119 .shortName = 'i',
120 .argInfo = POPT_ARG_STRING,
121 .arg = &option_input,
122 .descrip = "Decode log from file.",
123 .argDescrip = "FILENAME"
124 },
125 {
126 .longName = "version",
127 .argInfo = POPT_ARG_NONE,
128 .arg = &option_version,
129 .descrip = "Specify print version info.",
130 },
131 POPT_AUTOHELP
132 POPT_TABLEEND
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200133};
134
Stefan Richter92c16f72010-07-22 11:58:05 +0200135/* Allow all ^C except the first to interrupt the program in the usual way. */
Stefan Richter468066f2010-07-22 11:58:05 +0200136static void
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200137sigint_handler(int signal_num)
138{
Stefan Richter92c16f72010-07-22 11:58:05 +0200139 if (run == 1) {
140 run = 0;
141 signal(SIGINT, SIG_DFL);
142 }
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200143}
144
Stefan Richter468066f2010-07-22 11:58:05 +0200145static struct subaction *
Stefan Richter1bcc69f2010-07-22 11:58:05 +0200146subaction_create(uint32_t *data, size_t length)
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200147{
Stefan Richter92c16f72010-07-22 11:58:05 +0200148 struct subaction *sa;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200149
Stefan Richter92c16f72010-07-22 11:58:05 +0200150 /* we put the ack in the subaction struct for easy access. */
151 sa = malloc(sizeof *sa - sizeof sa->packet + length);
152 sa->ack = data[length / 4 - 1];
153 sa->length = length;
154 memcpy(&sa->packet, data, length);
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200155
Stefan Richter92c16f72010-07-22 11:58:05 +0200156 return sa;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200157}
158
Stefan Richter468066f2010-07-22 11:58:05 +0200159static void
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200160subaction_destroy(struct subaction *sa)
161{
Stefan Richter92c16f72010-07-22 11:58:05 +0200162 free(sa);
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200163}
164
Stefan Richter468066f2010-07-22 11:58:05 +0200165static struct list pending_transaction_list = {
Stefan Richter92c16f72010-07-22 11:58:05 +0200166 &pending_transaction_list, &pending_transaction_list
167};
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200168
Stefan Richter468066f2010-07-22 11:58:05 +0200169static struct link_transaction *
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200170link_transaction_lookup(int request_node, int response_node, int tlabel)
171{
Stefan Richter92c16f72010-07-22 11:58:05 +0200172 struct link_transaction *t;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200173
Stefan Richter92c16f72010-07-22 11:58:05 +0200174 list_for_each_entry(t, &pending_transaction_list, link) {
175 if (t->request_node == request_node &&
176 t->response_node == response_node &&
177 t->tlabel == tlabel)
178 return t;
179 }
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200180
Stefan Richter92c16f72010-07-22 11:58:05 +0200181 t = malloc(sizeof *t);
182 t->request_node = request_node;
183 t->response_node = response_node;
184 t->tlabel = tlabel;
185 list_init(&t->request_list);
186 list_init(&t->response_list);
187
188 list_append(&pending_transaction_list, &t->link);
189
190 return t;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200191}
192
Stefan Richter468066f2010-07-22 11:58:05 +0200193static void
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200194link_transaction_destroy(struct link_transaction *t)
195{
Stefan Richter92c16f72010-07-22 11:58:05 +0200196 struct subaction *sa;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200197
Stefan Richter92c16f72010-07-22 11:58:05 +0200198 while (!list_empty(&t->request_list)) {
199 sa = list_head(&t->request_list, struct subaction, link);
200 list_remove(&sa->link);
201 subaction_destroy(sa);
202 }
203 while (!list_empty(&t->response_list)) {
204 sa = list_head(&t->response_list, struct subaction, link);
205 list_remove(&sa->link);
206 subaction_destroy(sa);
207 }
208 free(t);
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200209}
210
211struct protocol_decoder {
Stefan Richter92c16f72010-07-22 11:58:05 +0200212 const char *name;
213 int (*decode)(struct link_transaction *t);
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200214};
215
Stefan Richter468066f2010-07-22 11:58:05 +0200216static const struct protocol_decoder protocol_decoders[] = {
Stefan Richter92c16f72010-07-22 11:58:05 +0200217 { "FCP", decode_fcp }
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200218};
219
Stefan Richter468066f2010-07-22 11:58:05 +0200220static void
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200221handle_transaction(struct link_transaction *t)
222{
Stefan Richter92c16f72010-07-22 11:58:05 +0200223 struct subaction *sa;
224 int i;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200225
Stefan Richter92c16f72010-07-22 11:58:05 +0200226 if (!t->request) {
227 printf("BUG in handle_transaction\n");
228 return;
229 }
Stefan Richtera8461c02010-07-22 11:58:05 +0200230
Stefan Richter92c16f72010-07-22 11:58:05 +0200231 for (i = 0; i < array_length(protocol_decoders); i++)
232 if (protocol_decoders[i].decode(t))
233 break;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200234
Stefan Richter92c16f72010-07-22 11:58:05 +0200235 /* HACK: decode only fcp right now. */
236 return;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200237
Stefan Richter92c16f72010-07-22 11:58:05 +0200238 decode_link_packet(&t->request->packet, t->request->length,
239 PACKET_FIELD_TRANSACTION, 0);
240 if (t->response)
241 decode_link_packet(&t->response->packet, t->request->length,
242 PACKET_FIELD_TRANSACTION, 0);
243 else
244 printf("[no response]");
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200245
Stefan Richter92c16f72010-07-22 11:58:05 +0200246 if (option_verbose) {
247 list_for_each_entry(sa, &t->request_list, link)
248 print_packet((uint32_t *) &sa->packet, sa->length);
249 list_for_each_entry(sa, &t->response_list, link)
250 print_packet((uint32_t *) &sa->packet, sa->length);
251 }
252 printf("\r\n");
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200253
Stefan Richter92c16f72010-07-22 11:58:05 +0200254 link_transaction_destroy(t);
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200255}
256
Stefan Richter468066f2010-07-22 11:58:05 +0200257static void
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200258clear_pending_transaction_list(void)
259{
Stefan Richter92c16f72010-07-22 11:58:05 +0200260 struct link_transaction *t;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200261
Stefan Richter92c16f72010-07-22 11:58:05 +0200262 while (!list_empty(&pending_transaction_list)) {
263 t = list_head(&pending_transaction_list,
264 struct link_transaction, link);
265 list_remove(&t->link);
266 link_transaction_destroy(t);
267 /* print unfinished transactions */
268 }
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200269}
270
271static const char * const tcode_names[] = {
Stefan Richter92c16f72010-07-22 11:58:05 +0200272 [0x0] = "write_quadlet_request", [0x6] = "read_quadlet_response",
273 [0x1] = "write_block_request", [0x7] = "read_block_response",
274 [0x2] = "write_response", [0x8] = "cycle_start",
275 [0x3] = "reserved", [0x9] = "lock_request",
276 [0x4] = "read_quadlet_request", [0xa] = "iso_data",
277 [0x5] = "read_block_request", [0xb] = "lock_response",
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200278};
279
280static const char * const ack_names[] = {
Stefan Richter92c16f72010-07-22 11:58:05 +0200281 [0x0] = "no ack", [0x8] = "reserved (0x08)",
282 [0x1] = "ack_complete", [0x9] = "reserved (0x09)",
283 [0x2] = "ack_pending", [0xa] = "reserved (0x0a)",
284 [0x3] = "reserved (0x03)", [0xb] = "reserved (0x0b)",
285 [0x4] = "ack_busy_x", [0xc] = "reserved (0x0c)",
286 [0x5] = "ack_busy_a", [0xd] = "ack_data_error",
287 [0x6] = "ack_busy_b", [0xe] = "ack_type_error",
288 [0x7] = "reserved (0x07)", [0xf] = "reserved (0x0f)",
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200289};
290
291static const char * const rcode_names[] = {
Stefan Richter92c16f72010-07-22 11:58:05 +0200292 [0x0] = "complete", [0x4] = "conflict_error",
293 [0x1] = "reserved (0x01)", [0x5] = "data_error",
294 [0x2] = "reserved (0x02)", [0x6] = "type_error",
295 [0x3] = "reserved (0x03)", [0x7] = "address_error",
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200296};
297
298static const char * const retry_names[] = {
Stefan Richter92c16f72010-07-22 11:58:05 +0200299 [0x0] = "retry_1",
300 [0x1] = "retry_x",
301 [0x2] = "retry_a",
302 [0x3] = "retry_b",
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200303};
304
305enum {
Stefan Richter92c16f72010-07-22 11:58:05 +0200306 PACKET_RESERVED,
307 PACKET_REQUEST,
308 PACKET_RESPONSE,
309 PACKET_OTHER,
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200310};
311
312struct packet_info {
Stefan Richter92c16f72010-07-22 11:58:05 +0200313 const char *name;
314 int type;
315 int response_tcode;
Stefan Richter468066f2010-07-22 11:58:05 +0200316 const struct packet_field *fields;
Stefan Richter92c16f72010-07-22 11:58:05 +0200317 int field_count;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200318};
319
320struct packet_field {
Stefan Richter92c16f72010-07-22 11:58:05 +0200321 const char *name; /* Short name for field. */
322 int offset; /* Location of field, specified in bits; */
323 /* negative means from end of packet. */
324 int width; /* Width of field, 0 means use data_length. */
325 int flags; /* Show options. */
326 const char * const *value_names;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200327};
328
Stefan Richter92c16f72010-07-22 11:58:05 +0200329#define COMMON_REQUEST_FIELDS \
330 { "dest", 0, 16, PACKET_FIELD_TRANSACTION }, \
331 { "tl", 16, 6 }, \
332 { "rt", 22, 2, PACKET_FIELD_DETAIL, retry_names }, \
333 { "tcode", 24, 4, PACKET_FIELD_TRANSACTION, tcode_names }, \
334 { "pri", 28, 4, PACKET_FIELD_DETAIL }, \
335 { "src", 32, 16, PACKET_FIELD_TRANSACTION }, \
336 { "offs", 48, 48, PACKET_FIELD_TRANSACTION }
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200337
Stefan Richter92c16f72010-07-22 11:58:05 +0200338#define COMMON_RESPONSE_FIELDS \
339 { "dest", 0, 16 }, \
340 { "tl", 16, 6 }, \
341 { "rt", 22, 2, PACKET_FIELD_DETAIL, retry_names }, \
342 { "tcode", 24, 4, 0, tcode_names }, \
343 { "pri", 28, 4, PACKET_FIELD_DETAIL }, \
344 { "src", 32, 16 }, \
345 { "rcode", 48, 4, PACKET_FIELD_TRANSACTION, rcode_names }
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200346
Stefan Richter468066f2010-07-22 11:58:05 +0200347static const struct packet_field read_quadlet_request_fields[] = {
Stefan Richter92c16f72010-07-22 11:58:05 +0200348 COMMON_REQUEST_FIELDS,
349 { "crc", 96, 32, PACKET_FIELD_DETAIL },
350 { "ack", 156, 4, 0, ack_names },
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200351};
352
Stefan Richter468066f2010-07-22 11:58:05 +0200353static const struct packet_field read_quadlet_response_fields[] = {
Stefan Richter92c16f72010-07-22 11:58:05 +0200354 COMMON_RESPONSE_FIELDS,
355 { "data", 96, 32, PACKET_FIELD_TRANSACTION },
356 { "crc", 128, 32, PACKET_FIELD_DETAIL },
357 { "ack", 188, 4, 0, ack_names },
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200358};
359
Stefan Richter468066f2010-07-22 11:58:05 +0200360static const struct packet_field read_block_request_fields[] = {
Stefan Richter92c16f72010-07-22 11:58:05 +0200361 COMMON_REQUEST_FIELDS,
362 { "data_length", 96, 16, PACKET_FIELD_TRANSACTION },
363 { "extended_tcode", 112, 16 },
364 { "crc", 128, 32, PACKET_FIELD_DETAIL },
365 { "ack", 188, 4, 0, ack_names },
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200366};
367
Stefan Richter468066f2010-07-22 11:58:05 +0200368static const struct packet_field block_response_fields[] = {
Stefan Richter92c16f72010-07-22 11:58:05 +0200369 COMMON_RESPONSE_FIELDS,
370 { "data_length", 96, 16, PACKET_FIELD_DATA_LENGTH },
371 { "extended_tcode", 112, 16 },
372 { "crc", 128, 32, PACKET_FIELD_DETAIL },
373 { "data", 160, 0, PACKET_FIELD_TRANSACTION },
374 { "crc", -64, 32, PACKET_FIELD_DETAIL },
375 { "ack", -4, 4, 0, ack_names },
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200376};
377
Stefan Richter468066f2010-07-22 11:58:05 +0200378static const struct packet_field write_quadlet_request_fields[] = {
Stefan Richter92c16f72010-07-22 11:58:05 +0200379 COMMON_REQUEST_FIELDS,
380 { "data", 96, 32, PACKET_FIELD_TRANSACTION },
381 { "ack", -4, 4, 0, ack_names },
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200382};
383
Stefan Richter468066f2010-07-22 11:58:05 +0200384static const struct packet_field block_request_fields[] = {
Stefan Richter92c16f72010-07-22 11:58:05 +0200385 COMMON_REQUEST_FIELDS,
386 { "data_length", 96, 16, PACKET_FIELD_DATA_LENGTH | PACKET_FIELD_TRANSACTION },
387 { "extended_tcode", 112, 16, PACKET_FIELD_TRANSACTION },
388 { "crc", 128, 32, PACKET_FIELD_DETAIL },
389 { "data", 160, 0, PACKET_FIELD_TRANSACTION },
390 { "crc", -64, 32, PACKET_FIELD_DETAIL },
391 { "ack", -4, 4, 0, ack_names },
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200392};
393
Stefan Richter468066f2010-07-22 11:58:05 +0200394static const struct packet_field write_response_fields[] = {
Stefan Richter92c16f72010-07-22 11:58:05 +0200395 COMMON_RESPONSE_FIELDS,
396 { "reserved", 64, 32, PACKET_FIELD_DETAIL },
397 { "ack", -4, 4, 0, ack_names },
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200398};
399
Stefan Richter468066f2010-07-22 11:58:05 +0200400static const struct packet_field iso_data_fields[] = {
Stefan Richter92c16f72010-07-22 11:58:05 +0200401 { "data_length", 0, 16, PACKET_FIELD_DATA_LENGTH },
402 { "tag", 16, 2 },
403 { "channel", 18, 6 },
404 { "tcode", 24, 4, 0, tcode_names },
405 { "sy", 28, 4 },
406 { "crc", 32, 32, PACKET_FIELD_DETAIL },
407 { "data", 64, 0 },
408 { "crc", -64, 32, PACKET_FIELD_DETAIL },
409 { "ack", -4, 4, 0, ack_names },
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200410};
411
Stefan Richter468066f2010-07-22 11:58:05 +0200412static const struct packet_info packet_info[] = {
Stefan Richter92c16f72010-07-22 11:58:05 +0200413 {
414 .name = "write_quadlet_request",
415 .type = PACKET_REQUEST,
416 .response_tcode = TCODE_WRITE_RESPONSE,
417 .fields = write_quadlet_request_fields,
418 .field_count = array_length(write_quadlet_request_fields)
419 },
420 {
421 .name = "write_block_request",
422 .type = PACKET_REQUEST,
423 .response_tcode = TCODE_WRITE_RESPONSE,
424 .fields = block_request_fields,
425 .field_count = array_length(block_request_fields)
426 },
427 {
428 .name = "write_response",
429 .type = PACKET_RESPONSE,
430 .fields = write_response_fields,
431 .field_count = array_length(write_response_fields)
432 },
433 {
434 .name = "reserved",
435 .type = PACKET_RESERVED,
436 },
437 {
438 .name = "read_quadlet_request",
439 .type = PACKET_REQUEST,
440 .response_tcode = TCODE_READ_QUADLET_RESPONSE,
441 .fields = read_quadlet_request_fields,
442 .field_count = array_length(read_quadlet_request_fields)
443 },
444 {
445 .name = "read_block_request",
446 .type = PACKET_REQUEST,
447 .response_tcode = TCODE_READ_BLOCK_RESPONSE,
448 .fields = read_block_request_fields,
449 .field_count = array_length(read_block_request_fields)
450 },
451 {
452 .name = "read_quadlet_response",
453 .type = PACKET_RESPONSE,
454 .fields = read_quadlet_response_fields,
455 .field_count = array_length(read_quadlet_response_fields)
456 },
457 {
458 .name = "read_block_response",
459 .type = PACKET_RESPONSE,
460 .fields = block_response_fields,
461 .field_count = array_length(block_response_fields)
462 },
463 {
464 .name = "cycle_start",
465 .type = PACKET_OTHER,
466 .fields = write_quadlet_request_fields,
467 .field_count = array_length(write_quadlet_request_fields)
468 },
469 {
470 .name = "lock_request",
471 .type = PACKET_REQUEST,
472 .fields = block_request_fields,
473 .field_count = array_length(block_request_fields)
474 },
475 {
476 .name = "iso_data",
477 .type = PACKET_OTHER,
478 .fields = iso_data_fields,
479 .field_count = array_length(iso_data_fields)
480 },
481 {
482 .name = "lock_response",
483 .type = PACKET_RESPONSE,
484 .fields = block_response_fields,
485 .field_count = array_length(block_response_fields)
486 },
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200487};
488
Stefan Richter468066f2010-07-22 11:58:05 +0200489static int
Stefan Richter269fe102010-07-22 11:58:05 +0200490handle_request_packet(uint32_t *data, size_t length)
491{
492 struct link_packet *p = (struct link_packet *) data;
493 struct subaction *sa, *prev;
494 struct link_transaction *t;
495
496 t = link_transaction_lookup(p->common.source, p->common.destination,
497 p->common.tlabel);
498 sa = subaction_create(data, length);
499 t->request = sa;
500
501 if (!list_empty(&t->request_list)) {
502 prev = list_tail(&t->request_list,
503 struct subaction, link);
504
505 if (!ACK_BUSY(prev->ack)) {
506 /*
507 * error, we should only see ack_busy_* before the
508 * ack_pending/ack_complete -- this is an ack_pending
509 * instead (ack_complete would have finished the
510 * transaction).
511 */
512 }
513
514 if (prev->packet.common.tcode != sa->packet.common.tcode ||
515 prev->packet.common.tlabel != sa->packet.common.tlabel) {
516 /* memcmp() ? */
517 /* error, these should match for retries. */
518 }
519 }
520
521 list_append(&t->request_list, &sa->link);
522
523 switch (sa->ack) {
524 case ACK_COMPLETE:
525 if (p->common.tcode != TCODE_WRITE_QUADLET &&
526 p->common.tcode != TCODE_WRITE_BLOCK)
527 /* error, unified transactions only allowed for write */;
528 list_remove(&t->link);
529 handle_transaction(t);
530 break;
531
532 case ACK_NO_ACK:
533 case ACK_DATA_ERROR:
534 case ACK_TYPE_ERROR:
535 list_remove(&t->link);
536 handle_transaction(t);
537 break;
538
539 case ACK_PENDING:
540 /* request subaction phase over, wait for response. */
541 break;
542
543 case ACK_BUSY_X:
544 case ACK_BUSY_A:
545 case ACK_BUSY_B:
546 /* ok, wait for retry. */
547 /* check that retry protocol is respected. */
548 break;
549 }
550
551 return 1;
552}
553
554static int
555handle_response_packet(uint32_t *data, size_t length)
556{
557 struct link_packet *p = (struct link_packet *) data;
558 struct subaction *sa, *prev;
559 struct link_transaction *t;
560
561 t = link_transaction_lookup(p->common.destination, p->common.source,
562 p->common.tlabel);
563 if (list_empty(&t->request_list)) {
564 /* unsolicited response */
565 }
566
567 sa = subaction_create(data, length);
568 t->response = sa;
569
570 if (!list_empty(&t->response_list)) {
571 prev = list_tail(&t->response_list, struct subaction, link);
572
573 if (!ACK_BUSY(prev->ack)) {
574 /*
575 * error, we should only see ack_busy_* before the
576 * ack_pending/ack_complete
577 */
578 }
579
580 if (prev->packet.common.tcode != sa->packet.common.tcode ||
581 prev->packet.common.tlabel != sa->packet.common.tlabel) {
582 /* use memcmp() instead? */
583 /* error, these should match for retries. */
584 }
585 } else {
586 prev = list_tail(&t->request_list, struct subaction, link);
587 if (prev->ack != ACK_PENDING) {
588 /*
589 * error, should not get response unless last request got
590 * ack_pending.
591 */
592 }
593
594 if (packet_info[prev->packet.common.tcode].response_tcode !=
595 sa->packet.common.tcode) {
596 /* error, tcode mismatch */
597 }
598 }
599
600 list_append(&t->response_list, &sa->link);
601
602 switch (sa->ack) {
603 case ACK_COMPLETE:
604 case ACK_NO_ACK:
605 case ACK_DATA_ERROR:
606 case ACK_TYPE_ERROR:
607 list_remove(&t->link);
608 handle_transaction(t);
609 /* transaction complete, remove t from pending list. */
610 break;
611
612 case ACK_PENDING:
613 /* error for responses. */
614 break;
615
616 case ACK_BUSY_X:
617 case ACK_BUSY_A:
618 case ACK_BUSY_B:
619 /* no problem, wait for next retry */
620 break;
621 }
622
623 return 1;
624}
625
626static int
Stefan Richter1bcc69f2010-07-22 11:58:05 +0200627handle_packet(uint32_t *data, size_t length)
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200628{
Stefan Richter92c16f72010-07-22 11:58:05 +0200629 if (length == 0) {
630 printf("bus reset\r\n");
631 clear_pending_transaction_list();
632 } else if (length > sizeof(struct phy_packet)) {
633 struct link_packet *p = (struct link_packet *) data;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200634
Stefan Richter92c16f72010-07-22 11:58:05 +0200635 switch (packet_info[p->common.tcode].type) {
636 case PACKET_REQUEST:
Stefan Richter269fe102010-07-22 11:58:05 +0200637 return handle_request_packet(data, length);
Stefan Richter92c16f72010-07-22 11:58:05 +0200638
639 case PACKET_RESPONSE:
Stefan Richter269fe102010-07-22 11:58:05 +0200640 return handle_response_packet(data, length);
Stefan Richter92c16f72010-07-22 11:58:05 +0200641
642 case PACKET_OTHER:
643 case PACKET_RESERVED:
644 return 0;
645 }
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200646 }
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200647
Stefan Richter92c16f72010-07-22 11:58:05 +0200648 return 1;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200649}
650
Stefan Richter468066f2010-07-22 11:58:05 +0200651static unsigned int
652get_bits(struct link_packet *packet, int offset, int width)
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200653{
Stefan Richter92c16f72010-07-22 11:58:05 +0200654 uint32_t *data = (uint32_t *) packet;
655 uint32_t index, shift, mask;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200656
Stefan Richter92c16f72010-07-22 11:58:05 +0200657 index = offset / 32 + 1;
658 shift = 32 - (offset & 31) - width;
659 mask = width == 32 ? ~0 : (1 << width) - 1;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200660
Stefan Richter92c16f72010-07-22 11:58:05 +0200661 return (data[index] >> shift) & mask;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200662}
663
664#if __BYTE_ORDER == __LITTLE_ENDIAN
665#define byte_index(i) ((i) ^ 3)
666#elif __BYTE_ORDER == __BIG_ENDIAN
667#define byte_index(i) (i)
668#else
669#error unsupported byte order.
670#endif
671
Stefan Richter468066f2010-07-22 11:58:05 +0200672static void
673dump_data(unsigned char *data, int length)
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200674{
Stefan Richter92c16f72010-07-22 11:58:05 +0200675 int i, print_length;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200676
Stefan Richter92c16f72010-07-22 11:58:05 +0200677 if (length > 128)
678 print_length = 128;
679 else
680 print_length = length;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200681
Stefan Richter92c16f72010-07-22 11:58:05 +0200682 for (i = 0; i < print_length; i++)
683 printf("%s%02hhx",
684 (i % 4 == 0 && i != 0) ? " " : "",
685 data[byte_index(i)]);
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200686
Stefan Richter92c16f72010-07-22 11:58:05 +0200687 if (print_length < length)
688 printf(" (%d more bytes)", length - print_length);
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200689}
690
691static void
692decode_link_packet(struct link_packet *packet, size_t length,
693 int include_flags, int exclude_flags)
694{
Stefan Richter468066f2010-07-22 11:58:05 +0200695 const struct packet_info *pi;
Stefan Richter92c16f72010-07-22 11:58:05 +0200696 int data_length = 0;
697 int i;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200698
Stefan Richter92c16f72010-07-22 11:58:05 +0200699 pi = &packet_info[packet->common.tcode];
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200700
Stefan Richter92c16f72010-07-22 11:58:05 +0200701 for (i = 0; i < pi->field_count; i++) {
Stefan Richter468066f2010-07-22 11:58:05 +0200702 const struct packet_field *f = &pi->fields[i];
Stefan Richter92c16f72010-07-22 11:58:05 +0200703 int offset;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200704
Stefan Richter92c16f72010-07-22 11:58:05 +0200705 if (f->flags & exclude_flags)
706 continue;
707 if (include_flags && !(f->flags & include_flags))
708 continue;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200709
Stefan Richter92c16f72010-07-22 11:58:05 +0200710 if (f->offset < 0)
711 offset = length * 8 + f->offset - 32;
712 else
713 offset = f->offset;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200714
Stefan Richter92c16f72010-07-22 11:58:05 +0200715 if (f->value_names != NULL) {
716 uint32_t bits;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200717
Stefan Richter92c16f72010-07-22 11:58:05 +0200718 bits = get_bits(packet, offset, f->width);
719 printf("%s", f->value_names[bits]);
720 } else if (f->width == 0) {
721 printf("%s=[", f->name);
722 dump_data((unsigned char *) packet + (offset / 8 + 4), data_length);
723 printf("]");
724 } else {
725 unsigned long long bits;
726 int high_width, low_width;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200727
Stefan Richter92c16f72010-07-22 11:58:05 +0200728 if ((offset & ~31) != ((offset + f->width - 1) & ~31)) {
729 /* Bit field spans quadlet boundary. */
730 high_width = ((offset + 31) & ~31) - offset;
731 low_width = f->width - high_width;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200732
Stefan Richter92c16f72010-07-22 11:58:05 +0200733 bits = get_bits(packet, offset, high_width);
734 bits = (bits << low_width) |
735 get_bits(packet, offset + high_width, low_width);
736 } else {
737 bits = get_bits(packet, offset, f->width);
738 }
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200739
Stefan Richter92c16f72010-07-22 11:58:05 +0200740 printf("%s=0x%0*llx", f->name, (f->width + 3) / 4, bits);
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200741
Stefan Richter92c16f72010-07-22 11:58:05 +0200742 if (f->flags & PACKET_FIELD_DATA_LENGTH)
743 data_length = bits;
744 }
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200745
Stefan Richter92c16f72010-07-22 11:58:05 +0200746 if (i < pi->field_count - 1)
747 printf(", ");
748 }
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200749}
750
751static void
Stefan Richter1bcc69f2010-07-22 11:58:05 +0200752print_packet(uint32_t *data, size_t length)
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200753{
Stefan Richter92c16f72010-07-22 11:58:05 +0200754 int i;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200755
Stefan Richter92c16f72010-07-22 11:58:05 +0200756 printf("%6u ", data[0]);
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200757
Stefan Richter92c16f72010-07-22 11:58:05 +0200758 if (length == 4) {
759 printf("bus reset");
760 } else if (length < sizeof(struct phy_packet)) {
761 printf("short packet: ");
762 for (i = 1; i < length / 4; i++)
763 printf("%s%08x", i == 0 ? "[" : " ", data[i]);
764 printf("]");
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200765
Stefan Richter92c16f72010-07-22 11:58:05 +0200766 } else if (length == sizeof(struct phy_packet) && data[1] == ~data[2]) {
767 struct phy_packet *pp = (struct phy_packet *) data;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200768
Stefan Richter92c16f72010-07-22 11:58:05 +0200769 /* phy packet are 3 quadlets: the 1 quadlet payload,
770 * the bitwise inverse of the payload and the snoop
771 * mode ack */
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200772
Stefan Richter92c16f72010-07-22 11:58:05 +0200773 switch (pp->common.identifier) {
774 case PHY_PACKET_CONFIGURATION:
775 if (!pp->phy_config.set_root && !pp->phy_config.set_gap_count) {
776 printf("ext phy config: phy_id=%02x", pp->phy_config.root_id);
777 } else {
778 printf("phy config:");
779 if (pp->phy_config.set_root)
780 printf(" set_root_id=%02x", pp->phy_config.root_id);
781 if (pp->phy_config.set_gap_count)
782 printf(" set_gap_count=%d", pp->phy_config.gap_count);
783 }
784 break;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200785
Stefan Richter92c16f72010-07-22 11:58:05 +0200786 case PHY_PACKET_LINK_ON:
787 printf("link-on packet, phy_id=%02x", pp->link_on.phy_id);
788 break;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200789
Stefan Richter92c16f72010-07-22 11:58:05 +0200790 case PHY_PACKET_SELF_ID:
791 if (pp->self_id.extended) {
792 printf("extended self id: phy_id=%02x, seq=%d",
793 pp->ext_self_id.phy_id, pp->ext_self_id.sequence);
794 } else {
795 static const char * const speed_names[] = {
796 "S100", "S200", "S400", "BETA"
797 };
798 printf("self id: phy_id=%02x, link %s, gap_count=%d, speed=%s%s%s",
799 pp->self_id.phy_id,
800 (pp->self_id.link_active ? "active" : "not active"),
801 pp->self_id.gap_count,
802 speed_names[pp->self_id.phy_speed],
803 (pp->self_id.contender ? ", irm contender" : ""),
804 (pp->self_id.initiated_reset ? ", initiator" : ""));
805 }
806 break;
807 default:
808 printf("unknown phy packet: ");
809 for (i = 1; i < length / 4; i++)
810 printf("%s%08x", i == 0 ? "[" : " ", data[i]);
811 printf("]");
812 break;
813 }
814 } else {
815 struct link_packet *packet = (struct link_packet *) data;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200816
Stefan Richter92c16f72010-07-22 11:58:05 +0200817 decode_link_packet(packet, length, 0,
818 option_verbose ? 0 : PACKET_FIELD_DETAIL);
819 }
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200820
Stefan Richter92c16f72010-07-22 11:58:05 +0200821 if (option_hex) {
822 printf(" [");
823 dump_data((unsigned char *) data + 4, length - 4);
824 printf("]");
825 }
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200826
Stefan Richter92c16f72010-07-22 11:58:05 +0200827 printf("\r\n");
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200828}
829
830#define HIDE_CURSOR "\033[?25l"
831#define SHOW_CURSOR "\033[?25h"
832#define CLEAR "\033[H\033[2J"
833
834static void
Stefan Richter1bcc69f2010-07-22 11:58:05 +0200835print_stats(uint32_t *data, size_t length)
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200836{
Stefan Richter92c16f72010-07-22 11:58:05 +0200837 static int bus_reset_count, short_packet_count, phy_packet_count;
838 static int tcode_count[16];
839 static struct timeval last_update;
840 struct timeval now;
841 int i;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200842
Stefan Richter92c16f72010-07-22 11:58:05 +0200843 if (length == 0)
844 bus_reset_count++;
845 else if (length < sizeof(struct phy_packet))
846 short_packet_count++;
847 else if (length == sizeof(struct phy_packet) && data[1] == ~data[2])
848 phy_packet_count++;
849 else {
850 struct link_packet *packet = (struct link_packet *) data;
851 tcode_count[packet->common.tcode]++;
852 }
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200853
Stefan Richter92c16f72010-07-22 11:58:05 +0200854 gettimeofday(&now, NULL);
855 if (now.tv_sec <= last_update.tv_sec &&
856 now.tv_usec < last_update.tv_usec + 500000)
857 return;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200858
Stefan Richter92c16f72010-07-22 11:58:05 +0200859 last_update = now;
860 printf(CLEAR HIDE_CURSOR
861 " bus resets : %8d\n"
862 " short packets : %8d\n"
863 " phy packets : %8d\n",
864 bus_reset_count, short_packet_count, phy_packet_count);
865
866 for (i = 0; i < array_length(packet_info); i++)
867 if (packet_info[i].type != PACKET_RESERVED)
868 printf(" %-24s: %8d\n", packet_info[i].name, tcode_count[i]);
869 printf(SHOW_CURSOR "\n");
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200870}
871
Stefan Richter468066f2010-07-22 11:58:05 +0200872static struct termios saved_attributes;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200873
Stefan Richter468066f2010-07-22 11:58:05 +0200874static void
Stefan Richter92c16f72010-07-22 11:58:05 +0200875reset_input_mode(void)
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200876{
Stefan Richter92c16f72010-07-22 11:58:05 +0200877 tcsetattr(STDIN_FILENO, TCSANOW, &saved_attributes);
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200878}
879
Stefan Richter468066f2010-07-22 11:58:05 +0200880static void
Stefan Richter92c16f72010-07-22 11:58:05 +0200881set_input_mode(void)
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200882{
Stefan Richter92c16f72010-07-22 11:58:05 +0200883 struct termios tattr;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200884
Stefan Richter92c16f72010-07-22 11:58:05 +0200885 /* Make sure stdin is a terminal. */
886 if (!isatty(STDIN_FILENO)) {
887 fprintf(stderr, "Not a terminal.\n");
888 exit(EXIT_FAILURE);
889 }
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200890
Stefan Richter92c16f72010-07-22 11:58:05 +0200891 /* Save the terminal attributes so we can restore them later. */
892 tcgetattr(STDIN_FILENO, &saved_attributes);
893 atexit(reset_input_mode);
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200894
Stefan Richter92c16f72010-07-22 11:58:05 +0200895 /* Set the funny terminal modes. */
896 tcgetattr(STDIN_FILENO, &tattr);
897 tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
898 tattr.c_cc[VMIN] = 1;
899 tattr.c_cc[VTIME] = 0;
900 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tattr);
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200901}
902
903int main(int argc, const char *argv[])
904{
Stefan Richter92c16f72010-07-22 11:58:05 +0200905 int fd = -1;
906 FILE *output = NULL, *input = NULL;
907 poptContext con;
908 int retval;
909 int view;
910 char c;
911 struct pollfd pollfds[2];
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200912
Stefan Richter92c16f72010-07-22 11:58:05 +0200913 sys_sigint_handler = signal(SIGINT, sigint_handler);
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200914
Stefan Richter92c16f72010-07-22 11:58:05 +0200915 con = poptGetContext(NULL, argc, argv, options, 0);
916 retval = poptGetNextOpt(con);
917 if (retval < -1) {
918 poptPrintUsage(con, stdout, 0);
919 return -1;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200920 }
921
Stefan Richter92c16f72010-07-22 11:58:05 +0200922 if (option_version) {
923 printf("dump tool for nosy sniffer, version %s\n", VERSION);
924 return 0;
925 }
926
927 if (__BYTE_ORDER != __LITTLE_ENDIAN)
928 fprintf(stderr, "warning: nosy has only been tested on little "
929 "endian machines\n");
930
931 if (option_input != NULL) {
932 input = fopen(option_input, "r");
933 if (input == NULL) {
934 fprintf(stderr, "Could not open %s, %m\n", option_input);
935 return -1;
936 }
937 } else {
938 fd = open(option_nosy_device, O_RDWR);
939 if (fd < 0) {
940 fprintf(stderr, "Could not open %s, %m\n", option_nosy_device);
941 return -1;
942 }
943 set_input_mode();
944 }
945
946 if (strcmp(option_view, "transaction") == 0)
947 view = VIEW_TRANSACTION;
948 else if (strcmp(option_view, "stats") == 0)
949 view = VIEW_STATS;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200950 else
Stefan Richter92c16f72010-07-22 11:58:05 +0200951 view = VIEW_PACKET;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200952
Stefan Richter92c16f72010-07-22 11:58:05 +0200953 if (option_output) {
954 output = fopen(option_output, "w");
955 if (output == NULL) {
956 fprintf(stderr, "Could not open %s, %m\n", option_output);
957 return -1;
958 }
959 }
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200960
Stefan Richter92c16f72010-07-22 11:58:05 +0200961 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200962
Stefan Richter92c16f72010-07-22 11:58:05 +0200963 if (1) {
964 uint32_t buf[128 * 1024];
965 uint32_t filter;
966 int length;
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200967
Stefan Richter92c16f72010-07-22 11:58:05 +0200968 filter = ~0;
969 if (!option_iso)
970 filter &= ~(1 << TCODE_ISO_DATA);
971 if (!option_cycle_start)
972 filter &= ~(1 << TCODE_CYCLE_START);
973 if (view == VIEW_STATS)
974 filter = ~(1 << TCODE_CYCLE_START);
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200975
Stefan Richter92c16f72010-07-22 11:58:05 +0200976 ioctl(fd, NOSY_IOC_FILTER, filter);
Stefan Richter9f6d3c42010-07-22 11:58:05 +0200977
Stefan Richter92c16f72010-07-22 11:58:05 +0200978 ioctl(fd, NOSY_IOC_START);
979
980 pollfds[0].fd = fd;
981 pollfds[0].events = POLLIN;
982 pollfds[1].fd = STDIN_FILENO;
983 pollfds[1].events = POLLIN;
984
985 while (run) {
986 if (input != NULL) {
987 if (fread(&length, sizeof length, 1, input) != 1)
988 return 0;
989 fread(buf, 1, length, input);
990 } else {
991 poll(pollfds, 2, -1);
992 if (pollfds[1].revents) {
993 read(STDIN_FILENO, &c, sizeof c);
994 switch (c) {
995 case 'q':
996 if (output != NULL)
997 fclose(output);
998 return 0;
999 }
1000 }
1001
1002 if (pollfds[0].revents)
1003 length = read(fd, buf, sizeof buf);
1004 else
1005 continue;
1006 }
1007
1008 if (output != NULL) {
1009 fwrite(&length, sizeof length, 1, output);
1010 fwrite(buf, 1, length, output);
1011 }
1012
1013 switch (view) {
1014 case VIEW_TRANSACTION:
1015 handle_packet(buf, length);
1016 break;
1017 case VIEW_PACKET:
1018 print_packet(buf, length);
1019 break;
1020 case VIEW_STATS:
1021 print_stats(buf, length);
1022 break;
1023 }
1024 }
1025 } else {
1026 poptPrintUsage(con, stdout, 0);
1027 }
1028
1029 if (output != NULL)
1030 fclose(output);
1031
1032 close(fd);
1033
1034 poptFreeContext(con);
1035
1036 return 0;
Stefan Richter9f6d3c42010-07-22 11:58:05 +02001037}