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