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