]>
Commit | Line | Data |
---|---|---|
97554432 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
97554432 | 3 | * |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> |
97554432 BV |
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> | |
45c59c8b BV |
24 | #include "libsigrok.h" |
25 | #include "libsigrok-internal.h" | |
97554432 BV |
26 | #include "text.h" |
27 | ||
3544f848 | 28 | #define LOG_PREFIX "output/hex" |
a944a84b | 29 | |
7c1d391c | 30 | SR_PRIV int init_hex(struct sr_output *o) |
97554432 BV |
31 | { |
32 | return init(o, DEFAULT_BPL_HEX, MODE_HEX); | |
33 | } | |
34 | ||
054e6709 UH |
35 | SR_PRIV int data_hex(struct sr_output *o, const uint8_t *data_in, |
36 | uint64_t length_in, uint8_t **data_out, | |
37 | uint64_t *length_out) | |
97554432 BV |
38 | { |
39 | struct context *ctx; | |
40 | unsigned int outsize, offset, p; | |
41 | int max_linelen; | |
9275d232 | 42 | const uint8_t *sample; |
054e6709 | 43 | uint8_t *outbuf; |
97554432 BV |
44 | |
45 | ctx = o->internal; | |
3f239f08 | 46 | max_linelen = SR_MAX_CHANNELNAME_LEN + 3 + ctx->samples_per_line |
97554432 | 47 | + ctx->samples_per_line / 2; |
ba7dd8bb | 48 | outsize = length_in / ctx->unitsize * ctx->num_enabled_channels |
97554432 BV |
49 | / ctx->samples_per_line * max_linelen + 512; |
50 | ||
133a37bf | 51 | if (!(outbuf = g_try_malloc0(outsize + 1))) { |
a944a84b | 52 | sr_err("%s: outbuf malloc failed", __func__); |
e46b8fb1 | 53 | return SR_ERR_MALLOC; |
133a37bf | 54 | } |
97554432 BV |
55 | |
56 | outbuf[0] = '\0'; | |
57 | if (ctx->header) { | |
58 | /* The header is still here, this must be the first packet. */ | |
054e6709 | 59 | strncpy((char *)outbuf, ctx->header, outsize); |
133a37bf | 60 | g_free(ctx->header); |
97554432 BV |
61 | ctx->header = NULL; |
62 | } | |
63 | ||
64 | ctx->line_offset = 0; | |
65 | for (offset = 0; offset <= length_in - ctx->unitsize; | |
66 | offset += ctx->unitsize) { | |
9275d232 | 67 | sample = data_in + offset; |
ba7dd8bb | 68 | for (p = 0; p < ctx->num_enabled_channels; p++) { |
97554432 | 69 | ctx->linevalues[p] <<= 1; |
9275d232 | 70 | if (sample[p / 8] & ((uint8_t) 1 << (p % 8))) |
97554432 | 71 | ctx->linevalues[p] |= 1; |
054e6709 | 72 | sprintf((char *)ctx->linebuf + (p * ctx->linebuf_len) + |
97554432 BV |
73 | ctx->line_offset, "%.2x", ctx->linevalues[p]); |
74 | } | |
75 | ctx->spl_cnt++; | |
76 | ||
77 | /* Add a space after every complete hex byte. */ | |
78 | if ((ctx->spl_cnt & 7) == 0) { | |
ba7dd8bb | 79 | for (p = 0; p < ctx->num_enabled_channels; p++) |
97554432 BV |
80 | ctx->linebuf[p * ctx->linebuf_len + |
81 | ctx->line_offset + 2] = ' '; | |
82 | ctx->line_offset += 3; | |
83 | } | |
84 | ||
85 | /* End of line. */ | |
86 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
87 | flush_linebufs(ctx, outbuf); | |
88 | ctx->line_offset = ctx->spl_cnt = 0; | |
89 | } | |
90 | } | |
91 | ||
92 | *data_out = outbuf; | |
054e6709 | 93 | *length_out = strlen((const char *)outbuf); |
97554432 | 94 | |
e46b8fb1 | 95 | return SR_OK; |
97554432 BV |
96 | } |
97 | ||
7c1d391c | 98 | SR_PRIV struct sr_output_format output_text_hex = { |
cdb3573c | 99 | .id = "hex", |
2e7cb004 | 100 | .description = "Hexadecimal", |
d494a4aa UH |
101 | .df_type = SR_DF_LOGIC, |
102 | .init = init_hex, | |
103 | .data = data_hex, | |
104 | .event = event, | |
8c273ac5 | 105 | .cleanup = text_cleanup, |
97554432 | 106 | }; |