]> sigrok.org Git - libsigrok.git/blame - hardware/demo/demo.c
disable MSO-19 specific stuff for 0.1 release
[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>
d35aaf02
UH
26#ifdef _WIN32
27#include <io.h>
28#include <fcntl.h>
29#define pipe(fds) _pipe(fds, 4096, _O_BINARY)
30#endif
6239c175
UH
31#include "config.h"
32
e15f48c2
BV
33#define NUM_PROBES 8
34#define DEMONAME "Demo device"
35/* size of chunks to send through the session bus */
36#define BUFSIZE 4096
85b5af06 37
e15f48c2 38enum {
917e0e71 39 GENMODE_DEFAULT,
e15f48c2
BV
40 GENMODE_RANDOM,
41 GENMODE_INC,
42};
85b5af06 43
d35aaf02
UH
44GIOChannel *channels[2];
45
e15f48c2
BV
46struct databag {
47 int pipe_fds[2];
48 uint8_t sample_generator;
49 uint8_t thread_running;
50 uint64_t samples_counter;
51 int device_index;
52 int loop_sleep;
53 gpointer session_device_id;
b9cc3629 54 GTimer *timer;
e15f48c2 55};
85b5af06 56
6239c175
UH
57static int capabilities[] = {
58 HWCAP_LOGIC_ANALYZER,
e15f48c2 59 HWCAP_PATTERN_MODE,
6239c175 60 HWCAP_LIMIT_SAMPLES,
b9cc3629 61 HWCAP_LIMIT_MSEC,
ba3d481b 62 HWCAP_CONTINUOUS
6239c175
UH
63};
64
02440dd8 65static const char *patternmodes[] = {
e15f48c2
BV
66 "random",
67 "incremental",
02440dd8 68 NULL,
85b5af06
UH
69};
70
917e0e71
BV
71static uint8_t genmode_default[] = {
72 0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00,
73 0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00,
74 0x7c, 0x82, 0x82, 0x92, 0x74, 0x00, 0x00, 0x00,
75 0xfe, 0x12, 0x12, 0x32, 0xcc, 0x00, 0x00, 0x00,
76 0x7c, 0x82, 0x82, 0x82, 0x7c, 0x00, 0x00, 0x00,
77 0xfe, 0x10, 0x28, 0x44, 0x82, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80};
81
85b5af06
UH
82/* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
83static GSList *device_instances = NULL;
6239c175 84static uint64_t cur_samplerate = 0;
b9cc3629
BV
85static uint64_t limit_samples = 0;
86static uint64_t limit_msec = 0;
917e0e71 87static int default_genmode = GENMODE_DEFAULT;
b9cc3629
BV
88static GThread *my_thread;
89static int thread_running;
6239c175 90
6239c175
UH
91static void hw_stop_acquisition(int device_index, gpointer session_device_id);
92
6239c175
UH
93static int hw_init(char *deviceinfo)
94{
85b5af06
UH
95 struct sigrok_device_instance *sdi;
96
e15f48c2
BV
97 /* Avoid compiler warnings. */
98 deviceinfo = deviceinfo;
85b5af06 99
e15f48c2 100 sdi = sigrok_device_instance_new(0, ST_ACTIVE, DEMONAME, NULL, NULL);
85b5af06
UH
101 if (!sdi)
102 return 0;
e15f48c2 103
85b5af06
UH
104 device_instances = g_slist_append(device_instances, sdi);
105
106 return 1;
6239c175
UH
107}
108
109static int hw_opendev(int device_index)
110{
17e1afcb 111 /* Avoid compiler warnings. */
6239c175
UH
112 device_index = device_index;
113
114 /* Nothing needed so far. */
115 return SIGROK_OK;
116}
117
118static void hw_closedev(int device_index)
119{
17e1afcb 120 /* Avoid compiler warnings. */
6239c175
UH
121 device_index = device_index;
122
123 /* Nothing needed so far. */
124}
125
126static void hw_cleanup(void)
127{
128 /* Nothing needed so far. */
129}
130
131static void *hw_get_device_info(int device_index, int device_info_id)
132{
85b5af06
UH
133 struct sigrok_device_instance *sdi;
134 void *info = NULL;
135
136 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
137 return NULL;
138
6239c175
UH
139 switch (device_info_id) {
140 case DI_INSTANCE:
85b5af06 141 info = sdi;
6239c175
UH
142 break;
143 case DI_NUM_PROBES:
144 info = GINT_TO_POINTER(NUM_PROBES);
145 break;
6239c175
UH
146 case DI_CUR_SAMPLERATE:
147 info = &cur_samplerate;
148 break;
e15f48c2
BV
149 case DI_PATTERNMODES:
150 info = &patternmodes;
151 break;
6239c175
UH
152 }
153
154 return info;
155}
156
157static int hw_get_status(int device_index)
158{
17e1afcb 159 /* Avoid compiler warnings. */
6239c175 160 device_index = device_index;
5096c6a6 161
e15f48c2 162 return ST_ACTIVE;
6239c175
UH
163}
164
165static int *hw_get_capabilities(void)
166{
167 return capabilities;
168}
169
170static int hw_set_configuration(int device_index, int capability, void *value)
171{
172 int ret;
85b5af06 173 uint64_t *tmp_u64;
e15f48c2 174 char *stropt;
6239c175 175
17e1afcb 176 /* Avoid compiler warnings. */
6239c175
UH
177 device_index = device_index;
178
e15f48c2 179 if (capability == HWCAP_PROBECONFIG) {
02440dd8 180 /* Nothing to do. */
85b5af06 181 ret = SIGROK_OK;
6239c175 182 } else if (capability == HWCAP_LIMIT_SAMPLES) {
85b5af06
UH
183 tmp_u64 = value;
184 limit_samples = *tmp_u64;
6239c175 185 ret = SIGROK_OK;
b9cc3629
BV
186 } else if (capability == HWCAP_LIMIT_MSEC) {
187 tmp_u64 = value;
188 limit_msec = *tmp_u64;
189 ret = SIGROK_OK;
e15f48c2
BV
190 } else if (capability == HWCAP_PATTERN_MODE) {
191 stropt = value;
192 if (!strcmp(stropt, "random")) {
193 default_genmode = GENMODE_RANDOM;
194 ret = SIGROK_OK;
195 } else if (!strcmp(stropt, "incremental")) {
196 default_genmode = GENMODE_INC;
197 ret = SIGROK_OK;
198 } else {
199 ret = SIGROK_ERR;
200 }
6239c175
UH
201 } else {
202 ret = SIGROK_ERR;
203 }
204
205 return ret;
206}
207
5096c6a6 208static void samples_generator(uint8_t *buf, uint64_t size, void *data)
85b5af06
UH
209{
210 struct databag *mydata = data;
917e0e71 211 uint64_t p, i;
85b5af06 212
5096c6a6 213 memset(buf, 0, size);
85b5af06
UH
214
215 switch (mydata->sample_generator) {
917e0e71
BV
216 case GENMODE_DEFAULT:
217 p = 0;
218 for (i = 0; i < size; i++) {
219 *(buf + i) = ~(genmode_default[p] >> 1);
220 if (++p == 64)
221 p = 0;
222 }
223 break;
85b5af06 224 case GENMODE_RANDOM: /* Random */
5096c6a6
UH
225 for (i = 0; i < size; i++)
226 *(buf + i) = (uint8_t)(rand() & 0xff);
85b5af06
UH
227 break;
228 case GENMODE_INC: /* Simple increment */
5096c6a6 229 for (i = 0; i < size; i++)
85b5af06
UH
230 *(buf + i) = i;
231 break;
232 }
233}
234
235/* Thread function */
236static void thread_func(void *data)
237{
238 struct databag *mydata = data;
239 uint8_t buf[BUFSIZE];
240 uint64_t nb_to_send = 0;
d35aaf02 241 int bytes_written;
b9cc3629 242 unsigned int msec_elapsed;
85b5af06
UH
243
244 while (thread_running) {
ba3d481b
OF
245 if (limit_samples)
246 nb_to_send = limit_samples - mydata->samples_counter;
247 else
02440dd8 248 nb_to_send = BUFSIZE; /* Continuous mode */
85b5af06 249
b9cc3629
BV
250 if (limit_msec) {
251 msec_elapsed = g_timer_elapsed(mydata->timer, NULL) * 1000;
252 if (msec_elapsed > limit_msec)
253 nb_to_send = 0;
254 }
255
85b5af06
UH
256 if (nb_to_send == 0) {
257 close(mydata->pipe_fds[1]);
258 thread_running = 0;
259 hw_stop_acquisition(mydata->device_index,
260 mydata->session_device_id);
261 } else if (nb_to_send > BUFSIZE) {
262 nb_to_send = BUFSIZE;
263 }
264
265 samples_generator(buf, nb_to_send, data);
266 mydata->samples_counter += nb_to_send;
267
d35aaf02 268 g_io_channel_write_chars(channels[1], (gchar *)&buf,
10c471ab 269 nb_to_send, (gsize *)&bytes_written, NULL);
d35aaf02 270
85b5af06
UH
271 g_usleep(mydata->loop_sleep);
272 }
273}
274
275/* Callback handling data */
276static int receive_data(int fd, int revents, void *user_data)
277{
278 struct datafeed_packet packet;
85b5af06
UH
279 char c[BUFSIZE];
280 uint64_t z;
281
282 /* Avoid compiler warnings. */
10c471ab 283 fd = fd;
85b5af06
UH
284 revents = revents;
285
01bd1ed3
UH
286 g_io_channel_read_chars(channels[0], (gchar *)&c, BUFSIZE,
287 (gsize *)&z, NULL);
d35aaf02 288
85b5af06 289 if (z > 0) {
4c046c6b 290 packet.type = DF_LOGIC;
85b5af06 291 packet.length = z;
4c046c6b 292 packet.unitsize = 1;
85b5af06
UH
293 packet.payload = c;
294 session_bus(user_data, &packet);
295 }
296 return TRUE;
297}
298
6239c175
UH
299static int hw_start_acquisition(int device_index, gpointer session_device_id)
300{
301 struct datafeed_packet *packet;
302 struct datafeed_header *header;
85b5af06 303 struct databag *mydata;
6239c175 304
85b5af06 305 mydata = malloc(sizeof(struct databag));
5096c6a6
UH
306 if (!mydata)
307 return SIGROK_ERR_MALLOC;
85b5af06 308
e15f48c2 309 mydata->sample_generator = default_genmode;
85b5af06
UH
310 mydata->session_device_id = session_device_id;
311 mydata->device_index = device_index;
312 mydata->samples_counter = 0;
313 mydata->loop_sleep = 100000;
314
e15f48c2
BV
315 if (pipe(mydata->pipe_fds))
316 return SIGROK_ERR;
85b5af06 317
d35aaf02
UH
318 channels[0] = g_io_channel_unix_new(mydata->pipe_fds[0]);
319 channels[1] = g_io_channel_unix_new(mydata->pipe_fds[1]);
320
321 /* Set channel encoding to binary (default is UTF-8). */
322 g_io_channel_set_encoding(channels[0], NULL, NULL);
323 g_io_channel_set_encoding(channels[1], NULL, NULL);
324
325 /* Make channels to unbuffered. */
326 g_io_channel_set_buffered(channels[0], FALSE);
327 g_io_channel_set_buffered(channels[1], FALSE);
328
85b5af06
UH
329 source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data,
330 session_device_id);
331
332 /* Run the demo thread. */
333 g_thread_init(NULL);
b9cc3629
BV
334 if (limit_msec)
335 mydata->timer = g_timer_new();
85b5af06
UH
336 thread_running = 1;
337 my_thread =
338 g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
e15f48c2
BV
339 if (!my_thread)
340 return SIGROK_ERR;
6239c175
UH
341
342 packet = malloc(sizeof(struct datafeed_packet));
343 header = malloc(sizeof(struct datafeed_header));
e15f48c2 344 if (!packet || !header)
6239c175
UH
345 return SIGROK_ERR_MALLOC;
346
6239c175
UH
347 packet->type = DF_HEADER;
348 packet->length = sizeof(struct datafeed_header);
349 packet->payload = (unsigned char *)header;
350 header->feed_version = 1;
351 gettimeofday(&header->starttime, NULL);
352 header->samplerate = cur_samplerate;
353 header->protocol_id = PROTO_RAW;
c2616fb9
DR
354 header->num_logic_probes = NUM_PROBES;
355 header->num_analog_probes = 0;
6239c175
UH
356 session_bus(session_device_id, packet);
357 free(header);
358 free(packet);
359
360 return SIGROK_OK;
361}
362
6239c175
UH
363static void hw_stop_acquisition(int device_index, gpointer session_device_id)
364{
365 struct datafeed_packet packet;
366
17e1afcb 367 /* Avoid compiler warnings. */
6239c175
UH
368 device_index = device_index;
369
85b5af06 370 /* Send last packet. */
6239c175
UH
371 packet.type = DF_END;
372 session_bus(session_device_id, &packet);
6239c175
UH
373}
374
375struct device_plugin demo_plugin_info = {
376 "demo",
377 1,
378 hw_init,
379 hw_cleanup,
380 hw_opendev,
381 hw_closedev,
382 hw_get_device_info,
383 hw_get_status,
384 hw_get_capabilities,
385 hw_set_configuration,
386 hw_start_acquisition,
387 hw_stop_acquisition,
388};