]> sigrok.org Git - libsigrok.git/blame - hardware/demo/demo.c
fully implement input_binary module
[libsigrok.git] / hardware / demo / demo.c
CommitLineData
6239c175
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
fc96e6f8 5 * Copyright (C) 2011 Olivier Fauchon <olivier@aixmarseille.com>
6239c175
UH
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 2 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, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <stdlib.h>
85b5af06 23#include <unistd.h>
6239c175
UH
24#include <string.h>
25#include <sigrok.h>
26#include "config.h"
27
85b5af06
UH
28#define BUFSIZE 4096
29
6239c175
UH
30#define NUM_PROBES 8
31#define NUM_TRIGGER_STAGES 4
32#define TRIGGER_TYPES "01"
33
34/* Software trigger implementation: positive values indicate trigger stage. */
35#define TRIGGER_FIRED -1
36
5096c6a6
UH
37#define USB_MODEL_NAME "demodevice"
38#define USB_VENDOR_NAME "demovendor"
39#define USB_MODEL_VERSION "1.0"
85b5af06
UH
40
41#define GENMODE_RANDOM 1
42#define GENMODE_INC 2
43
44static GThread *my_thread;
45static int thread_running;
46
6239c175
UH
47static int capabilities[] = {
48 HWCAP_LOGIC_ANALYZER,
49 HWCAP_SAMPLERATE,
50 HWCAP_LIMIT_SAMPLES,
51};
52
53/* Random selection of samplerates this "device" shall support. */
54static uint64_t supported_samplerates[] = {
55 KHZ(100),
56 KHZ(500),
57 MHZ(1),
58 MHZ(2),
59 MHZ(12),
60 MHZ(24),
61 0,
62};
63
64static struct samplerates samplerates = {
65 KHZ(100),
66 MHZ(24),
67 0,
68 supported_samplerates,
69};
70
85b5af06
UH
71struct databag {
72 int pipe_fds[2];
73 uint8_t sample_generator;
74 uint8_t thread_running;
75 uint64_t samples_counter;
76 int device_index;
77 int loop_sleep;
78 gpointer session_device_id;
79};
80
81/* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
82static GSList *device_instances = NULL;
83
6239c175
UH
84/* TODO: All of these should go in a device-specific struct. */
85static uint64_t cur_samplerate = 0;
86static uint64_t limit_samples = 0;
87// static uint8_t probe_mask = 0;
88// static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 };
89// static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 };
90// static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 };
91
92// static int trigger_stage = TRIGGER_FIRED;
93
94static int hw_set_configuration(int device_index, int capability, void *value);
95static void hw_stop_acquisition(int device_index, gpointer session_device_id);
96
6239c175
UH
97static int hw_init(char *deviceinfo)
98{
99 /* Avoid compiler warning. */
100 deviceinfo = deviceinfo;
101
85b5af06
UH
102 struct sigrok_device_instance *sdi;
103
104 sdi = sigrok_device_instance_new(0, ST_ACTIVE,
105 USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
106
107 if (!sdi)
108 return 0;
109 device_instances = g_slist_append(device_instances, sdi);
110
111 return 1;
6239c175
UH
112}
113
114static int hw_opendev(int device_index)
115{
116 /* Avoid compiler warning. */
117 device_index = device_index;
118
119 /* Nothing needed so far. */
120 return SIGROK_OK;
121}
122
123static void hw_closedev(int device_index)
124{
125 /* Avoid compiler warning. */
126 device_index = device_index;
127
128 /* Nothing needed so far. */
129}
130
131static void hw_cleanup(void)
132{
133 /* Nothing needed so far. */
134}
135
136static void *hw_get_device_info(int device_index, int device_info_id)
137{
85b5af06
UH
138 struct sigrok_device_instance *sdi;
139 void *info = NULL;
140
141 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
142 return NULL;
143
6239c175
UH
144 switch (device_info_id) {
145 case DI_INSTANCE:
85b5af06 146 info = sdi;
6239c175
UH
147 break;
148 case DI_NUM_PROBES:
149 info = GINT_TO_POINTER(NUM_PROBES);
150 break;
151 case DI_SAMPLERATES:
152 info = &samplerates;
153 break;
154 case DI_TRIGGER_TYPES:
155 info = TRIGGER_TYPES;
156 break;
157 case DI_CUR_SAMPLERATE:
158 info = &cur_samplerate;
159 break;
160 }
161
162 return info;
163}
164
165static int hw_get_status(int device_index)
166{
167 /* Avoid compiler warning. */
168 device_index = device_index;
5096c6a6 169
6239c175
UH
170 return 0; /* FIXME */
171}
172
173static int *hw_get_capabilities(void)
174{
175 return capabilities;
176}
177
178static int hw_set_configuration(int device_index, int capability, void *value)
179{
180 int ret;
85b5af06 181 uint64_t *tmp_u64;
6239c175
UH
182
183 /* Avoid compiler warning. */
184 device_index = device_index;
185
186 if (capability == HWCAP_SAMPLERATE) {
85b5af06 187 cur_samplerate = *(uint64_t *) value;
6239c175
UH
188 ret = SIGROK_OK;
189 } else if (capability == HWCAP_PROBECONFIG) {
85b5af06
UH
190 // ret = configure_probes((GSList *) value); FIXME
191 ret = SIGROK_OK;
6239c175 192 } else if (capability == HWCAP_LIMIT_SAMPLES) {
85b5af06
UH
193 tmp_u64 = value;
194 limit_samples = *tmp_u64;
6239c175
UH
195 ret = SIGROK_OK;
196 } else {
197 ret = SIGROK_ERR;
198 }
199
200 return ret;
201}
202
5096c6a6 203static void samples_generator(uint8_t *buf, uint64_t size, void *data)
85b5af06
UH
204{
205 struct databag *mydata = data;
206 uint64_t i;
85b5af06 207
5096c6a6 208 memset(buf, 0, size);
85b5af06
UH
209
210 switch (mydata->sample_generator) {
211 case GENMODE_RANDOM: /* Random */
5096c6a6
UH
212 for (i = 0; i < size; i++)
213 *(buf + i) = (uint8_t)(rand() & 0xff);
85b5af06
UH
214 break;
215 case GENMODE_INC: /* Simple increment */
5096c6a6 216 for (i = 0; i < size; i++)
85b5af06
UH
217 *(buf + i) = i;
218 break;
219 }
220}
221
222/* Thread function */
223static void thread_func(void *data)
224{
225 struct databag *mydata = data;
226 uint8_t buf[BUFSIZE];
227 uint64_t nb_to_send = 0;
228
229 while (thread_running) {
230 nb_to_send = limit_samples - mydata->samples_counter;
231
232 if (nb_to_send == 0) {
233 close(mydata->pipe_fds[1]);
234 thread_running = 0;
235 hw_stop_acquisition(mydata->device_index,
236 mydata->session_device_id);
237 } else if (nb_to_send > BUFSIZE) {
238 nb_to_send = BUFSIZE;
239 }
240
241 samples_generator(buf, nb_to_send, data);
242 mydata->samples_counter += nb_to_send;
243
244 write(mydata->pipe_fds[1], &buf, nb_to_send);
245 g_usleep(mydata->loop_sleep);
246 }
247}
248
249/* Callback handling data */
250static int receive_data(int fd, int revents, void *user_data)
251{
252 struct datafeed_packet packet;
253 /* uint16_t samples[1000]; */
254 char c[BUFSIZE];
255 uint64_t z;
256
257 /* Avoid compiler warnings. */
258 revents = revents;
259
260 z = read(fd, &c, BUFSIZE);
261 if (z > 0) {
4c046c6b 262 packet.type = DF_LOGIC;
85b5af06 263 packet.length = z;
4c046c6b 264 packet.unitsize = 1;
85b5af06
UH
265 packet.payload = c;
266 session_bus(user_data, &packet);
267 }
268 return TRUE;
269}
270
6239c175
UH
271static int hw_start_acquisition(int device_index, gpointer session_device_id)
272{
273 struct datafeed_packet *packet;
274 struct datafeed_header *header;
275 unsigned char *buf;
85b5af06 276 struct databag *mydata;
6239c175 277
85b5af06 278 mydata = malloc(sizeof(struct databag));
5096c6a6
UH
279 if (!mydata)
280 return SIGROK_ERR_MALLOC;
85b5af06
UH
281
282 mydata->sample_generator = GENMODE_RANDOM;
283 mydata->session_device_id = session_device_id;
284 mydata->device_index = device_index;
285 mydata->samples_counter = 0;
286 mydata->loop_sleep = 100000;
287
288 if (pipe(mydata->pipe_fds)) {
289 fprintf(stderr, "Pipe failed.\n");
290 return SIGROK_ERR_MALLOC; /* FIXME */
291
292 }
293 source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data,
294 session_device_id);
295
296 /* Run the demo thread. */
297 g_thread_init(NULL);
298 thread_running = 1;
299 my_thread =
300 g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
301 if (!my_thread) {
302 fprintf(stderr, "demo: Thread creation failed.\n");
303 return SIGROK_ERR_MALLOC; /* FIXME */
304 }
6239c175
UH
305
306 packet = malloc(sizeof(struct datafeed_packet));
307 header = malloc(sizeof(struct datafeed_header));
308 buf = malloc(2048);
309 if (!packet || !header || !buf)
310 return SIGROK_ERR_MALLOC;
311
312 /* FIXME */
313 memset(buf, 0x55, 2048);
314
315 packet->type = DF_HEADER;
316 packet->length = sizeof(struct datafeed_header);
317 packet->payload = (unsigned char *)header;
318 header->feed_version = 1;
319 gettimeofday(&header->starttime, NULL);
320 header->samplerate = cur_samplerate;
321 header->protocol_id = PROTO_RAW;
c2616fb9
DR
322 header->num_logic_probes = NUM_PROBES;
323 header->num_analog_probes = 0;
6239c175
UH
324 session_bus(session_device_id, packet);
325 free(header);
326 free(packet);
327
328 return SIGROK_OK;
329}
330
6239c175
UH
331/* This stops acquisition on ALL devices, ignoring device_index. */
332static void hw_stop_acquisition(int device_index, gpointer session_device_id)
333{
334 struct datafeed_packet packet;
335
336 /* QUICK HACK */
337 device_index = device_index;
338
85b5af06 339 /* Send last packet. */
6239c175
UH
340 packet.type = DF_END;
341 session_bus(session_device_id, &packet);
6239c175
UH
342}
343
344struct device_plugin demo_plugin_info = {
345 "demo",
346 1,
347 hw_init,
348 hw_cleanup,
349 hw_opendev,
350 hw_closedev,
351 hw_get_device_info,
352 hw_get_status,
353 hw_get_capabilities,
354 hw_set_configuration,
355 hw_start_acquisition,
356 hw_stop_acquisition,
357};