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