libsigrok
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
binary.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  *
00006  * This program is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 3 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <stdlib.h>
00021 #include <sys/types.h>
00022 #include <sys/stat.h>
00023 #include <fcntl.h>
00024 #include <unistd.h>
00025 #include <sys/time.h>
00026 #include "sigrok.h"
00027 #include "sigrok-internal.h"
00028 
00029 #define CHUNKSIZE             (512 * 1024)
00030 #define DEFAULT_NUM_PROBES    8
00031 
00032 static int format_match(const char *filename)
00033 {
00034         /* suppress compiler warning */
00035         (void)filename;
00036 
00037         /* this module will handle anything you throw at it */
00038         return TRUE;
00039 }
00040 
00041 static int init(struct sr_input *in)
00042 {
00043         int num_probes, i;
00044         char name[SR_MAX_PROBENAME_LEN + 1];
00045 
00046         if (in->param && in->param[0]) {
00047                 num_probes = strtoul(in->param, NULL, 10);
00048                 if (num_probes < 1)
00049                         return SR_ERR;
00050         } else {
00051                 num_probes = DEFAULT_NUM_PROBES;
00052         }
00053 
00054         /* Create a virtual device. */
00055         in->vdev = sr_dev_new(NULL, 0);
00056 
00057         for (i = 0; i < num_probes; i++) {
00058                 snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i);
00059                 /* TODO: Check return value. */
00060                 sr_dev_probe_add(in->vdev, name);
00061         }
00062 
00063         return SR_OK;
00064 }
00065 
00066 static int loadfile(struct sr_input *in, const char *filename)
00067 {
00068         struct sr_datafeed_header header;
00069         struct sr_datafeed_packet packet;
00070         struct sr_datafeed_logic logic;
00071         unsigned char buffer[CHUNKSIZE];
00072         int fd, size, num_probes;
00073 
00074         if ((fd = open(filename, O_RDONLY)) == -1)
00075                 return SR_ERR;
00076 
00077         num_probes = g_slist_length(in->vdev->probes);
00078 
00079         /* send header */
00080         header.feed_version = 1;
00081         header.num_logic_probes = num_probes;
00082         header.samplerate = 0;
00083         gettimeofday(&header.starttime, NULL);
00084         packet.type = SR_DF_HEADER;
00085         packet.payload = &header;
00086         sr_session_send(in->vdev, &packet);
00087 
00088         /* chop up the input file into chunks and feed it into the session bus */
00089         packet.type = SR_DF_LOGIC;
00090         packet.payload = &logic;
00091         logic.unitsize = (num_probes + 7) / 8;
00092         logic.data = buffer;
00093         while ((size = read(fd, buffer, CHUNKSIZE)) > 0) {
00094                 logic.length = size;
00095                 sr_session_send(in->vdev, &packet);
00096         }
00097         close(fd);
00098 
00099         /* end of stream */
00100         packet.type = SR_DF_END;
00101         sr_session_send(in->vdev, &packet);
00102 
00103         return SR_OK;
00104 }
00105 
00106 SR_PRIV struct sr_input_format input_binary = {
00107         .id = "binary",
00108         .description = "Raw binary",
00109         .format_match = format_match,
00110         .init = init,
00111         .loadfile = loadfile,
00112 };
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines