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 #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
00035 (void)filename;
00036
00037
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
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
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
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
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
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 };