]> sigrok.org Git - libsigrok.git/blob - output/text/hex.c
Add SR_ prefix for MAX_NUM_PROBES/MAX_PROBENAME_LEN.
[libsigrok.git] / output / text / hex.c
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>
24 #include <sigrok.h>
25 #include "text.h"
26
27
28 int init_hex(struct sr_output *o)
29 {
30         return init(o, DEFAULT_BPL_HEX, MODE_HEX);
31 }
32
33 int data_hex(struct sr_output *o, const char *data_in, uint64_t length_in,
34              char **data_out, uint64_t *length_out)
35 {
36         struct context *ctx;
37         unsigned int outsize, offset, p;
38         int max_linelen;
39         uint64_t sample;
40         char *outbuf;
41
42         ctx = o->internal;
43         max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
44                         + ctx->samples_per_line / 2;
45         outsize = length_in / ctx->unitsize * ctx->num_enabled_probes
46                         / ctx->samples_per_line * max_linelen + 512;
47
48         if (!(outbuf = calloc(1, outsize + 1)))
49                 return SR_ERR_MALLOC;
50
51         outbuf[0] = '\0';
52         if (ctx->header) {
53                 /* The header is still here, this must be the first packet. */
54                 strncpy(outbuf, ctx->header, outsize);
55                 free(ctx->header);
56                 ctx->header = NULL;
57         }
58
59         ctx->line_offset = 0;
60         for (offset = 0; offset <= length_in - ctx->unitsize;
61              offset += ctx->unitsize) {
62                 memcpy(&sample, data_in + offset, ctx->unitsize);
63                 for (p = 0; p < ctx->num_enabled_probes; p++) {
64                         ctx->linevalues[p] <<= 1;
65                         if (sample & ((uint64_t) 1 << p))
66                                 ctx->linevalues[p] |= 1;
67                         sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
68                                 ctx->line_offset, "%.2x", ctx->linevalues[p]);
69                 }
70                 ctx->spl_cnt++;
71
72                 /* Add a space after every complete hex byte. */
73                 if ((ctx->spl_cnt & 7) == 0) {
74                         for (p = 0; p < ctx->num_enabled_probes; p++)
75                                 ctx->linebuf[p * ctx->linebuf_len +
76                                              ctx->line_offset + 2] = ' ';
77                         ctx->line_offset += 3;
78                 }
79
80                 /* End of line. */
81                 if (ctx->spl_cnt >= ctx->samples_per_line) {
82                         flush_linebufs(ctx, outbuf);
83                         ctx->line_offset = ctx->spl_cnt = 0;
84                 }
85         }
86
87         *data_out = outbuf;
88         *length_out = strlen(outbuf);
89
90         return SR_OK;
91 }
92
93
94 struct sr_output_format output_text_hex = {
95         "hex",
96         "Hexadecimal (takes argument, default 192)",
97         SR_DF_LOGIC,
98         init_hex,
99         data_hex,
100         event,
101 };
102