3 * Unigraf DisplayPort AUX channel monitor output parser
4 * Copyright 2018, Dirk Eibach, Guntermann & Drunck GmbH <dirk.eibach@gdsys.cc>
6 * SPDX-License-Identifier: GPL-2.0-or-later
14 #include "file_wrappers.h"
19 DPA400_DATA_END = 0x01,
23 DPA400_TS_OVERFLOW = 0x84,
26 struct dpa400_header {
35 static gboolean dpa400_read_header(FILE_T fh, struct dpa400_header *hdr, int *err, gchar **err_info)
37 if (!wtap_read_bytes_or_eof(fh, hdr, sizeof(struct dpa400_header), err, err_info))
40 if (hdr->sb0 || hdr->sb1 || hdr->sb2) {
41 *err = WTAP_ERR_BAD_FILE;
42 *err_info = g_strdup("dpa400: malformed packet header");
49 static void get_ts(struct dpa400_header *hdr, nstime_t *ts)
53 val = (hdr->t0 | (hdr->t1 << 8) | ((hdr->t2 & 0x7f) << 16)) << 5;
55 ts->secs = val / 1000000;
56 ts->nsecs = (val % 1000000) * 1000;
59 static void get_ts_overflow(nstime_t *ts)
61 guint32 val = 0x7fffff << 5;
63 ts->secs = val / 1000000;
64 ts->nsecs = (val % 1000000) * 1000;
67 static guint8 get_from(struct dpa400_header *hdr)
69 return hdr->t2 & 0x80;
72 static gboolean dpa400_read_packet(wtap *wth, FILE_T fh, wtap_rec *rec,
73 Buffer *buf, int *err, gchar **err_info)
78 if (!wth || !rec || !buf)
81 if (!wtap_read_bytes_or_eof(fh, chunk, sizeof(chunk), err, err_info))
85 *err = WTAP_ERR_BAD_FILE;
86 *err_info = g_strdup("dpa400: malformed packet framing");
92 ws_buffer_append(buf, &chunk[0], 1);
97 struct dpa400_header hdr;
99 if (!dpa400_read_header(fh, &hdr, err, err_info))
102 get_ts(&hdr, &rec->ts);
104 rec->rec_type = REC_TYPE_PACKET;
105 rec->presence_flags = WTAP_HAS_TS;
106 rec->rec_header.packet_header.caplen = rec->rec_header.packet_header.len = 0;
113 struct dpa400_header hdr;
115 if (!dpa400_read_header(fh, &hdr, err, err_info))
118 get_ts(&hdr, &rec->ts);
120 if (!wtap_read_bytes_or_eof(fh, chunk, sizeof(chunk), err, err_info))
124 *err = WTAP_ERR_BAD_FILE;
125 *err_info = g_strdup("dpa400: malformed packet");
129 ws_buffer_append(buf, &chunk[0], 1);
132 rec->rec_type = REC_TYPE_PACKET;
133 rec->presence_flags = WTAP_HAS_TS;
134 rec->rec_header.packet_header.caplen = rec->rec_header.packet_header.len = ctr;
140 struct dpa400_header hdr;
143 if (!dpa400_read_header(fh, &hdr, err, err_info))
146 get_ts(&hdr, &rec->ts);
148 from_source = !get_from(&hdr);
149 ws_buffer_append(buf, &from_source, 1);
153 if (!wtap_read_bytes_or_eof(fh, chunk, sizeof(chunk), err, err_info))
159 if (++ctr > WTAP_MAX_PACKET_SIZE_STANDARD) {
160 *err = WTAP_ERR_BAD_FILE;
161 *err_info = g_strdup_printf("dpa400: File has data record bigger than maximum of %u",
162 WTAP_MAX_PACKET_SIZE_STANDARD);
166 ws_buffer_append(buf, &chunk[0], 1);
169 rec->rec_type = REC_TYPE_PACKET;
170 rec->presence_flags = WTAP_HAS_TS;
171 rec->rec_header.packet_header.caplen = rec->rec_header.packet_header.len = ctr;
176 case DPA400_TS_OVERFLOW: {
177 get_ts_overflow(&rec->ts);
179 rec->rec_type = REC_TYPE_PACKET;
180 rec->presence_flags = WTAP_HAS_TS;
181 rec->rec_header.packet_header.caplen = rec->rec_header.packet_header.len = ctr;
187 *err = WTAP_ERR_BAD_FILE;
188 *err_info = g_strdup_printf("dpa400: unknown packet type %02x", chunk[0]);
195 static gboolean dpa400_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec, Buffer *buf,
196 int *err, gchar **err_info)
198 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
201 return dpa400_read_packet(wth, wth->random_fh, rec, buf, err, err_info);
204 static gboolean dpa400_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
206 *data_offset = file_tell(wth->fh);
208 return dpa400_read_packet(wth, wth->fh, &wth->rec, wth->rec_data, err, err_info);
211 wtap_open_return_val dpa400_open(wtap *wth, int *err, gchar **err_info)
214 const char dpa_magic[] = { 'D', 'B', 'F', 'R' };
216 /* Read in the number that should be at the start of a "dpa-400" file */
217 if (!wtap_read_bytes(wth->fh, &magic, sizeof magic, err, err_info)) {
218 if (*err != WTAP_ERR_SHORT_READ)
219 return WTAP_OPEN_ERROR;
220 return WTAP_OPEN_NOT_MINE;
223 if (memcmp(magic, dpa_magic, sizeof(dpa_magic)))
224 return WTAP_OPEN_NOT_MINE;
226 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_DPA400;
227 wth->file_encap = WTAP_ENCAP_DPAUXMON;
228 wth->file_tsprec = WTAP_TSPREC_USEC;
229 wth->subtype_read = dpa400_read;
230 wth->subtype_seek_read = dpa400_seek_read;
231 wth->snapshot_length = 0;
233 return WTAP_OPEN_MINE;