]> sigrok.org Git - libsigrok.git/blob - hardware/demo/demo.c
Revert "re-enable filter and datastore for DF_LOGIC"
[libsigrok.git] / hardware / demo / demo.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2011 Olivier Fauchon <olivier@aixmarseille.com>
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 #define DEMO_ANALOG
23
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <sigrok.h>
28 #include <math.h>
29 #ifdef _WIN32
30 #include <io.h>
31 #include <fcntl.h>
32 #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
33 #endif
34 #include "config.h"
35
36 #ifdef DEMO_ANALOG
37 #define NUM_PROBES             9
38 #else
39 #define NUM_PROBES             8
40 #endif
41 #define DEMONAME               "Demo device"
42 /* size of chunks to send through the session bus */
43 #ifdef DEMO_ANALOG
44 #define BUFSIZE                32768
45 #else
46 #define BUFSIZE                4096
47 #endif
48
49 enum {
50         GENMODE_DEFAULT,
51         GENMODE_RANDOM,
52         GENMODE_INC,
53         GENMODE_SINE,
54 };
55
56 GIOChannel *channels[2];
57
58 struct databag {
59         int pipe_fds[2];
60         uint8_t sample_generator;
61         uint8_t thread_running;
62         uint64_t samples_counter;
63         int device_index;
64         gpointer session_device_id;
65         GTimer *timer;
66 };
67
68 static int capabilities[] = {
69         HWCAP_LOGIC_ANALYZER,
70         HWCAP_PATTERN_MODE,
71         HWCAP_LIMIT_SAMPLES,
72         HWCAP_LIMIT_MSEC,
73         HWCAP_CONTINUOUS
74 };
75
76 static const char *patternmodes[] = {
77         "random",
78         "incremental",
79         "sine",
80         NULL,
81 };
82
83 #ifndef DEMO_ANALOG
84 static uint8_t genmode_default[] = {
85         0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00,
86         0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00,
87         0x7c, 0x82, 0x82, 0x92, 0x74, 0x00, 0x00, 0x00,
88         0xfe, 0x12, 0x12, 0x32, 0xcc, 0x00, 0x00, 0x00,
89         0x7c, 0x82, 0x82, 0x82, 0x7c, 0x00, 0x00, 0x00,
90         0xfe, 0x10, 0x28, 0x44, 0x82, 0x00, 0x00, 0x00,
91         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92         0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93 };
94 #endif
95
96 /* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
97 static GSList *device_instances = NULL;
98 static uint64_t cur_samplerate = KHZ(200);
99 static uint64_t limit_samples = 0;
100 static uint64_t limit_msec = 0;
101 static int default_genmode = GENMODE_DEFAULT;
102 static GThread *my_thread;
103 static int thread_running;
104
105 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
106
107 static int hw_init(char *deviceinfo)
108 {
109         struct sigrok_device_instance *sdi;
110
111         /* Avoid compiler warnings. */
112         deviceinfo = deviceinfo;
113
114         sdi = sigrok_device_instance_new(0, ST_ACTIVE, DEMONAME, NULL, NULL);
115         if (!sdi)
116                 return 0;
117
118         device_instances = g_slist_append(device_instances, sdi);
119
120         return 1;
121 }
122
123 static int hw_opendev(int device_index)
124 {
125         /* Avoid compiler warnings. */
126         device_index = device_index;
127
128         /* Nothing needed so far. */
129         return SIGROK_OK;
130 }
131
132 static void hw_closedev(int device_index)
133 {
134         /* Avoid compiler warnings. */
135         device_index = device_index;
136
137         /* Nothing needed so far. */
138 }
139
140 static void hw_cleanup(void)
141 {
142         /* Nothing needed so far. */
143 }
144
145 static void *hw_get_device_info(int device_index, int device_info_id)
146 {
147         struct sigrok_device_instance *sdi;
148         void *info = NULL;
149
150         if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
151                 return NULL;
152
153         switch (device_info_id) {
154         case DI_INSTANCE:
155                 info = sdi;
156                 break;
157         case DI_NUM_PROBES:
158                 info = GINT_TO_POINTER(NUM_PROBES);
159                 break;
160         case DI_CUR_SAMPLERATE:
161                 info = &cur_samplerate;
162                 break;
163         case DI_PATTERNMODES:
164                 info = &patternmodes;
165                 break;
166         }
167
168         return info;
169 }
170
171 static int hw_get_status(int device_index)
172 {
173         /* Avoid compiler warnings. */
174         device_index = device_index;
175
176         return ST_ACTIVE;
177 }
178
179 static int *hw_get_capabilities(void)
180 {
181         return capabilities;
182 }
183
184 static int hw_set_configuration(int device_index, int capability, void *value)
185 {
186         int ret;
187         uint64_t *tmp_u64;
188         char *stropt;
189
190         /* Avoid compiler warnings. */
191         device_index = device_index;
192
193         if (capability == HWCAP_PROBECONFIG) {
194                 /* Nothing to do. */
195                 ret = SIGROK_OK;
196         } else if (capability == HWCAP_LIMIT_SAMPLES) {
197                 tmp_u64 = value;
198                 limit_samples = *tmp_u64;
199                 ret = SIGROK_OK;
200         } else if (capability == HWCAP_LIMIT_MSEC) {
201                 tmp_u64 = value;
202                 limit_msec = *tmp_u64;
203                 ret = SIGROK_OK;
204         } else if (capability == HWCAP_PATTERN_MODE) {
205                 stropt = value;
206                 if (!strcmp(stropt, "random")) {
207                         default_genmode = GENMODE_RANDOM;
208                         ret = SIGROK_OK;
209                 } else if (!strcmp(stropt, "incremental")) {
210                         default_genmode = GENMODE_INC;
211                         ret = SIGROK_OK;
212                 } else if (!strcmp(stropt, "sine")) {
213                         default_genmode = GENMODE_SINE;
214                         ret = SIGROK_OK;
215                 } else {
216                         ret = SIGROK_ERR;
217                 }
218         } else {
219                 ret = SIGROK_ERR;
220         }
221
222         return ret;
223 }
224
225 static void samples_generator(uint8_t *buf, uint64_t size, void *data)
226 {
227         struct databag *mydata = data;
228         uint64_t p, i;
229 #ifdef DEMO_ANALOG
230         /*
231          * We will simulate a device with 8 logic probes and 1 analog probe.
232          * This fictional device sends the data packed: 8 bits for 8 logic
233          * probes and 16 bits for the analog probe, in this order.
234          * Total of 24 bits.
235          * I could just generate a properly formatted DF_ANALOG packet here,
236          * but I will leave the formatting to receive_data() to make its code
237          * more like a real hardware driver.
238          */
239         memset(buf, 0, size * 3);
240
241         switch (mydata->sample_generator) {
242         default:
243         case GENMODE_DEFAULT:
244         case GENMODE_SINE:
245                 for (i = 0; i < size * 3; i += 3) {
246                         *(buf + i) = i / 3;
247                         *(uint16_t *) (buf + i + 1) =
248                                 (uint16_t) (sin(i / 3) * 256 * 30);
249                 }
250                 break;
251         case GENMODE_RANDOM:
252                 for (i = 0; i < size * 3; i += 3) {
253                         *(buf + i) = (uint8_t)(rand() & 0xff);
254                         *(uint16_t *) (buf + i + 1) = (uint16_t)(rand() & 0xffff);
255                 }
256                 break;
257         case GENMODE_INC:
258                 for (i = 0; i < size * 3; i += 3) {
259                         *(buf + i) = i / 3;
260                         *(uint16_t *)(buf + i + 1) = i / 3 * 256 * 10;
261                 }
262                 break;
263         }
264 #else
265
266         memset(buf, 0, size);
267
268         switch (mydata->sample_generator) {
269         case GENMODE_DEFAULT:
270                 p = 0;
271                 for (i = 0; i < size; i++) {
272                         *(buf + i) = ~(genmode_default[p] >> 1);
273                         if (++p == 64)
274                                 p = 0;
275                 }
276                 break;
277         case GENMODE_RANDOM: /* Random */
278                 for (i = 0; i < size; i++)
279                         *(buf + i) = (uint8_t)(rand() & 0xff);
280                 break;
281         case GENMODE_INC: /* Simple increment */
282                 for (i = 0; i < size; i++)
283                         *(buf + i) = i;
284                 break;
285         }
286 #endif
287 }
288
289 /* Thread function */
290 static void thread_func(void *data)
291 {
292         struct databag *mydata = data;
293         uint8_t buf[BUFSIZE];
294         uint64_t nb_to_send = 0;
295         int bytes_written;
296
297         double time_cur, time_last, time_diff;
298
299         time_last = g_timer_elapsed(mydata->timer, NULL);
300
301         while (thread_running) {
302                 /* Rate control */
303                 time_cur = g_timer_elapsed(mydata->timer, NULL);
304
305                 time_diff = time_cur - time_last;
306                 time_last = time_cur;
307
308                 nb_to_send = cur_samplerate * time_diff;
309
310                 if (limit_samples)
311                         nb_to_send = MIN(nb_to_send,
312                                         limit_samples - mydata->samples_counter);
313
314                 /* Make sure we don't overflow. */
315 #ifdef DEMO_ANALOG
316                 nb_to_send = MIN(nb_to_send, BUFSIZE / 3);
317 #else
318                 nb_to_send = MIN(nb_to_send, BUFSIZE);
319 #endif
320
321                 if (nb_to_send) {
322                         samples_generator(buf, nb_to_send, data);
323                         mydata->samples_counter += nb_to_send;
324 #ifdef DEMO_ANALOG
325                         g_io_channel_write_chars(channels[1], (gchar *) &buf,
326                                 nb_to_send * 3, (gsize *) &bytes_written, NULL);
327 #else
328                         g_io_channel_write_chars(channels[1], (gchar *) &buf,
329                                 nb_to_send, (gsize *) &bytes_written, NULL);
330 #endif
331                 }
332
333                 /* Check if we're done. */
334                 if ((limit_msec && time_cur * 1000 > limit_msec) ||
335                     (limit_samples && mydata->samples_counter >= limit_samples))
336                 {
337                         close(mydata->pipe_fds[1]);
338                         thread_running = 0;
339                 }
340
341                 g_usleep(10);
342         }
343 }
344
345 /* Callback handling data */
346 static int receive_data(int fd, int revents, void *user_data)
347 {
348         struct datafeed_packet packet;
349         char c[BUFSIZE];
350         uint64_t z;
351 #ifdef DEMO_ANALOG
352         struct analog_sample *sample;
353         unsigned int i, x;
354         int sample_size = sizeof(struct analog_sample) +
355                 (NUM_PROBES * sizeof(struct analog_probe));
356         char *buf;
357 #endif
358
359         /* Avoid compiler warnings. */
360         fd = fd;
361         revents = revents;
362
363         do {
364                 g_io_channel_read_chars(channels[0],
365                                         (gchar *) &c, BUFSIZE, (gsize *) &z, NULL);
366
367                 if (z > 0) {
368 #ifdef DEMO_ANALOG
369                         packet.type = DF_ANALOG;
370
371                         packet.length = (z / 3) * sample_size;
372                         packet.unitsize = sample_size;
373
374                         buf = malloc(sample_size * packet.length);
375                         if (!buf)
376                                 return FALSE;
377
378                         /* Craft our packet. */
379                         for (i = 0; i < z / 3; i++) {
380                                 sample = (struct analog_sample *) (buf + (i * sample_size));
381                                 sample->num_probes = NUM_PROBES;
382
383                                 /* 8 Logic probes */
384                                 for (x = 0; x < NUM_PROBES - 1; x++) {
385                                         sample->probes[x].val =
386                                                 (c[i * 3] >> x) & 1;
387                                         sample->probes[x].res = 1;
388                                 }
389
390                                 /* 1 Analog probe, 16 bit adc */
391                                 for (; x < NUM_PROBES; x++) {
392                                         sample->probes[x].val =
393                                                 *(uint16_t *) (c + i * 3 + 1);
394                                         sample->probes[x].val &= ((1 << 16) - 1);
395                                         sample->probes[x].res = 16;
396                                 }
397
398                         }
399
400                         packet.payload = buf;
401                         session_bus(user_data, &packet);
402                         free(buf);
403 #else
404                         packet.type = DF_LOGIC;
405                         packet.length = z;
406                         packet.unitsize = 1;
407                         packet.payload = c;
408                         session_bus(user_data, &packet);
409 #endif
410                 }
411         } while (z > 0);
412
413         if (!thread_running && z <= 0)
414         {
415                 /* Make sure we don't receive more packets */
416                 g_io_channel_close(channels[0]);
417
418                 /* Send last packet. */
419                 packet.type = DF_END;
420                 session_bus(user_data, &packet);
421
422                 return FALSE;
423         }
424
425         return TRUE;
426 }
427
428 static int hw_start_acquisition(int device_index, gpointer session_device_id)
429 {
430         struct datafeed_packet *packet;
431         struct datafeed_header *header;
432         struct databag *mydata;
433
434         mydata = malloc(sizeof(struct databag));
435         if (!mydata)
436                 return SIGROK_ERR_MALLOC;
437
438         mydata->sample_generator = default_genmode;
439         mydata->session_device_id = session_device_id;
440         mydata->device_index = device_index;
441         mydata->samples_counter = 0;
442
443         if (pipe(mydata->pipe_fds))
444                 return SIGROK_ERR;
445
446         channels[0] = g_io_channel_unix_new(mydata->pipe_fds[0]);
447         channels[1] = g_io_channel_unix_new(mydata->pipe_fds[1]);
448
449         /* Set channel encoding to binary (default is UTF-8). */
450         g_io_channel_set_encoding(channels[0], NULL, NULL);
451         g_io_channel_set_encoding(channels[1], NULL, NULL);
452
453         /* Make channels to unbuffered. */
454         g_io_channel_set_buffered(channels[0], FALSE);
455         g_io_channel_set_buffered(channels[1], FALSE);
456
457         source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data,
458                    session_device_id);
459
460         /* Run the demo thread. */
461         g_thread_init(NULL);
462         mydata->timer = g_timer_new();
463         thread_running = 1;
464         my_thread =
465             g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
466         if (!my_thread)
467                 return SIGROK_ERR;
468
469         packet = malloc(sizeof(struct datafeed_packet));
470         header = malloc(sizeof(struct datafeed_header));
471         if (!packet || !header)
472                 return SIGROK_ERR_MALLOC;
473
474         packet->type = DF_HEADER;
475         packet->length = sizeof(struct datafeed_header);
476         packet->payload = (unsigned char *)header;
477 #ifdef DEMO_ANALOG
478         packet->unitsize = sizeof(struct analog_sample) +
479                 (NUM_PROBES * sizeof(struct analog_probe));
480 #endif
481         header->feed_version = 1;
482         gettimeofday(&header->starttime, NULL);
483         header->samplerate = cur_samplerate;
484         header->protocol_id = PROTO_RAW;
485         header->num_logic_probes = NUM_PROBES;
486         header->num_analog_probes = 0;
487         session_bus(session_device_id, packet);
488         free(header);
489         free(packet);
490
491         return SIGROK_OK;
492 }
493
494 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
495 {
496         /* Avoid compiler warnings. */
497         device_index = device_index;
498         session_device_id = session_device_id;
499
500         /* Stop generate thread. */
501         thread_running = 0;
502 }
503
504 struct device_plugin demo_plugin_info = {
505         "demo",
506         1,
507         hw_init,
508         hw_cleanup,
509         hw_opendev,
510         hw_closedev,
511         hw_get_device_info,
512         hw_get_status,
513         hw_get_capabilities,
514         hw_set_configuration,
515         hw_start_acquisition,
516         hw_stop_acquisition,
517 };