]> sigrok.org Git - libsigrok.git/blame - output/text/hex.c
sr: input/output: Mark more symbols with SR_PRIV.
[libsigrok.git] / output / text / hex.c
CommitLineData
97554432
BV
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2011 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no>
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21#include <stdlib.h>
22#include <string.h>
23#include <glib.h>
cd315a80 24#include "sigrok.h"
97554432
BV
25#include "text.h"
26
7c1d391c 27SR_PRIV int init_hex(struct sr_output *o)
97554432
BV
28{
29 return init(o, DEFAULT_BPL_HEX, MODE_HEX);
30}
31
7c1d391c
UH
32SR_PRIV int data_hex(struct sr_output *o, const char *data_in,
33 uint64_t length_in, char **data_out, uint64_t *length_out)
97554432
BV
34{
35 struct context *ctx;
36 unsigned int outsize, offset, p;
37 int max_linelen;
38 uint64_t sample;
39 char *outbuf;
40
41 ctx = o->internal;
9688b443 42 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
97554432
BV
43 + ctx->samples_per_line / 2;
44 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes
45 / ctx->samples_per_line * max_linelen + 512;
46
47 if (!(outbuf = calloc(1, outsize + 1)))
e46b8fb1 48 return SR_ERR_MALLOC;
97554432
BV
49
50 outbuf[0] = '\0';
51 if (ctx->header) {
52 /* The header is still here, this must be the first packet. */
53 strncpy(outbuf, ctx->header, outsize);
54 free(ctx->header);
55 ctx->header = NULL;
56 }
57
58 ctx->line_offset = 0;
59 for (offset = 0; offset <= length_in - ctx->unitsize;
60 offset += ctx->unitsize) {
61 memcpy(&sample, data_in + offset, ctx->unitsize);
62 for (p = 0; p < ctx->num_enabled_probes; p++) {
63 ctx->linevalues[p] <<= 1;
64 if (sample & ((uint64_t) 1 << p))
65 ctx->linevalues[p] |= 1;
66 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
67 ctx->line_offset, "%.2x", ctx->linevalues[p]);
68 }
69 ctx->spl_cnt++;
70
71 /* Add a space after every complete hex byte. */
72 if ((ctx->spl_cnt & 7) == 0) {
73 for (p = 0; p < ctx->num_enabled_probes; p++)
74 ctx->linebuf[p * ctx->linebuf_len +
75 ctx->line_offset + 2] = ' ';
76 ctx->line_offset += 3;
77 }
78
79 /* End of line. */
80 if (ctx->spl_cnt >= ctx->samples_per_line) {
81 flush_linebufs(ctx, outbuf);
82 ctx->line_offset = ctx->spl_cnt = 0;
83 }
84 }
85
86 *data_out = outbuf;
87 *length_out = strlen(outbuf);
88
e46b8fb1 89 return SR_OK;
97554432
BV
90}
91
7c1d391c 92SR_PRIV struct sr_output_format output_text_hex = {
cdb3573c 93 .id = "hex",
d494a4aa
UH
94 .description = "Hexadecimal (takes argument, default 192)",
95 .df_type = SR_DF_LOGIC,
96 .init = init_hex,
97 .data = data_hex,
98 .event = event,
97554432 99};