2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
28 /* Message logging helpers with driver-specific prefix string. */
29 #define DRIVER_LOG_DOMAIN "output/hex: "
30 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
31 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
32 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
33 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
34 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
35 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
37 SR_PRIV int init_hex(struct sr_output *o)
39 return init(o, DEFAULT_BPL_HEX, MODE_HEX);
42 SR_PRIV int data_hex(struct sr_output *o, const uint8_t *data_in,
43 uint64_t length_in, uint8_t **data_out,
47 unsigned int outsize, offset, p;
53 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
54 + ctx->samples_per_line / 2;
55 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes
56 / ctx->samples_per_line * max_linelen + 512;
58 if (!(outbuf = g_try_malloc0(outsize + 1))) {
59 sr_err("%s: outbuf malloc failed", __func__);
65 /* The header is still here, this must be the first packet. */
66 strncpy((char *)outbuf, ctx->header, outsize);
72 for (offset = 0; offset <= length_in - ctx->unitsize;
73 offset += ctx->unitsize) {
74 memcpy(&sample, data_in + offset, ctx->unitsize);
75 for (p = 0; p < ctx->num_enabled_probes; p++) {
76 ctx->linevalues[p] <<= 1;
77 if (sample & ((uint64_t) 1 << p))
78 ctx->linevalues[p] |= 1;
79 sprintf((char *)ctx->linebuf + (p * ctx->linebuf_len) +
80 ctx->line_offset, "%.2x", ctx->linevalues[p]);
84 /* Add a space after every complete hex byte. */
85 if ((ctx->spl_cnt & 7) == 0) {
86 for (p = 0; p < ctx->num_enabled_probes; p++)
87 ctx->linebuf[p * ctx->linebuf_len +
88 ctx->line_offset + 2] = ' ';
89 ctx->line_offset += 3;
93 if (ctx->spl_cnt >= ctx->samples_per_line) {
94 flush_linebufs(ctx, outbuf);
95 ctx->line_offset = ctx->spl_cnt = 0;
100 *length_out = strlen((const char *)outbuf);
105 SR_PRIV struct sr_output_format output_text_hex = {
107 .description = "Hexadecimal",
108 .df_type = SR_DF_LOGIC,