Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <glib.h>
00024 #include "sigrok.h"
00025 #include "sigrok-internal.h"
00026 #include "text.h"
00027
00028 SR_PRIV int init_hex(struct sr_output *o)
00029 {
00030 return init(o, DEFAULT_BPL_HEX, MODE_HEX);
00031 }
00032
00033 SR_PRIV int data_hex(struct sr_output *o, const uint8_t *data_in,
00034 uint64_t length_in, uint8_t **data_out,
00035 uint64_t *length_out)
00036 {
00037 struct context *ctx;
00038 unsigned int outsize, offset, p;
00039 int max_linelen;
00040 uint64_t sample;
00041 uint8_t *outbuf;
00042
00043 ctx = o->internal;
00044 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
00045 + ctx->samples_per_line / 2;
00046 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes
00047 / ctx->samples_per_line * max_linelen + 512;
00048
00049 if (!(outbuf = g_try_malloc0(outsize + 1))) {
00050 sr_err("hex out: %s: outbuf malloc failed", __func__);
00051 return SR_ERR_MALLOC;
00052 }
00053
00054 outbuf[0] = '\0';
00055 if (ctx->header) {
00056
00057 strncpy((char *)outbuf, ctx->header, outsize);
00058 g_free(ctx->header);
00059 ctx->header = NULL;
00060 }
00061
00062 ctx->line_offset = 0;
00063 for (offset = 0; offset <= length_in - ctx->unitsize;
00064 offset += ctx->unitsize) {
00065 memcpy(&sample, data_in + offset, ctx->unitsize);
00066 for (p = 0; p < ctx->num_enabled_probes; p++) {
00067 ctx->linevalues[p] <<= 1;
00068 if (sample & ((uint64_t) 1 << p))
00069 ctx->linevalues[p] |= 1;
00070 sprintf((char *)ctx->linebuf + (p * ctx->linebuf_len) +
00071 ctx->line_offset, "%.2x", ctx->linevalues[p]);
00072 }
00073 ctx->spl_cnt++;
00074
00075
00076 if ((ctx->spl_cnt & 7) == 0) {
00077 for (p = 0; p < ctx->num_enabled_probes; p++)
00078 ctx->linebuf[p * ctx->linebuf_len +
00079 ctx->line_offset + 2] = ' ';
00080 ctx->line_offset += 3;
00081 }
00082
00083
00084 if (ctx->spl_cnt >= ctx->samples_per_line) {
00085 flush_linebufs(ctx, outbuf);
00086 ctx->line_offset = ctx->spl_cnt = 0;
00087 }
00088 }
00089
00090 *data_out = outbuf;
00091 *length_out = strlen((const char *)outbuf);
00092
00093 return SR_OK;
00094 }
00095
00096 SR_PRIV struct sr_output_format output_text_hex = {
00097 .id = "hex",
00098 .description = "Hexadecimal",
00099 .df_type = SR_DF_LOGIC,
00100 .init = init_hex,
00101 .data = data_hex,
00102 .event = event,
00103 };