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