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