libsigrok
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
hex.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of the sigrok project.
00003  *
00004  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
00005  * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no>
00006  *
00007  * This program is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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                 /* The header is still here, this must be the first packet. */
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                 /* Add a space after every complete hex byte. */
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                 /* End of line. */
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 };
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines