libsigrok
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
hex.c
Go to the documentation of this file.
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010-2012 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 "sigrok-internal.h"
26 #include "text.h"
27 
28 SR_PRIV int init_hex(struct sr_output *o)
29 {
30  return init(o, DEFAULT_BPL_HEX, MODE_HEX);
31 }
32 
33 SR_PRIV int data_hex(struct sr_output *o, const uint8_t *data_in,
34  uint64_t length_in, uint8_t **data_out,
35  uint64_t *length_out)
36 {
37  struct context *ctx;
38  unsigned int outsize, offset, p;
39  int max_linelen;
40  uint64_t sample;
41  uint8_t *outbuf;
42 
43  ctx = o->internal;
44  max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
45  + ctx->samples_per_line / 2;
46  outsize = length_in / ctx->unitsize * ctx->num_enabled_probes
47  / ctx->samples_per_line * max_linelen + 512;
48 
49  if (!(outbuf = g_try_malloc0(outsize + 1))) {
50  sr_err("hex out: %s: outbuf malloc failed", __func__);
51  return SR_ERR_MALLOC;
52  }
53 
54  outbuf[0] = '\0';
55  if (ctx->header) {
56  /* The header is still here, this must be the first packet. */
57  strncpy((char *)outbuf, ctx->header, outsize);
58  g_free(ctx->header);
59  ctx->header = NULL;
60  }
61 
62  ctx->line_offset = 0;
63  for (offset = 0; offset <= length_in - ctx->unitsize;
64  offset += ctx->unitsize) {
65  memcpy(&sample, data_in + offset, ctx->unitsize);
66  for (p = 0; p < ctx->num_enabled_probes; p++) {
67  ctx->linevalues[p] <<= 1;
68  if (sample & ((uint64_t) 1 << p))
69  ctx->linevalues[p] |= 1;
70  sprintf((char *)ctx->linebuf + (p * ctx->linebuf_len) +
71  ctx->line_offset, "%.2x", ctx->linevalues[p]);
72  }
73  ctx->spl_cnt++;
74 
75  /* Add a space after every complete hex byte. */
76  if ((ctx->spl_cnt & 7) == 0) {
77  for (p = 0; p < ctx->num_enabled_probes; p++)
78  ctx->linebuf[p * ctx->linebuf_len +
79  ctx->line_offset + 2] = ' ';
80  ctx->line_offset += 3;
81  }
82 
83  /* End of line. */
84  if (ctx->spl_cnt >= ctx->samples_per_line) {
85  flush_linebufs(ctx, outbuf);
86  ctx->line_offset = ctx->spl_cnt = 0;
87  }
88  }
89 
90  *data_out = outbuf;
91  *length_out = strlen((const char *)outbuf);
92 
93  return SR_OK;
94 }
95 
97  .id = "hex",
98  .description = "Hexadecimal",
99  .df_type = SR_DF_LOGIC,
100  .init = init_hex,
101  .data = data_hex,
102  .event = event,
103 };