]> sigrok.org Git - libsigrok.git/blob - hardware/demo/demo.c
5227b800fdbf000238d5457b03fb319bd8cd3573
[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 };
55
56 static GThread *my_thread;
57 static int thread_running;
58
59 static int capabilities[] = {
60         HWCAP_LOGIC_ANALYZER,
61         HWCAP_PATTERN_MODE,
62         HWCAP_LIMIT_SAMPLES,
63         HWCAP_CONTINUOUS
64 };
65
66 static const char *patternmodes[] = {
67         "random",
68         "incremental",
69         NULL,
70 };
71
72 static 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
83 /* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
84 static GSList *device_instances = NULL;
85 static uint64_t cur_samplerate = 0;
86 static uint64_t limit_samples = -1;
87 static int default_genmode = GENMODE_DEFAULT;
88
89 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
90
91 static int hw_init(char *deviceinfo)
92 {
93         struct sigrok_device_instance *sdi;
94
95         /* Avoid compiler warnings. */
96         deviceinfo = deviceinfo;
97
98         sdi = sigrok_device_instance_new(0, ST_ACTIVE, DEMONAME, NULL, NULL);
99         if (!sdi)
100                 return 0;
101
102         device_instances = g_slist_append(device_instances, sdi);
103
104         return 1;
105 }
106
107 static int hw_opendev(int device_index)
108 {
109         /* Avoid compiler warnings. */
110         device_index = device_index;
111
112         /* Nothing needed so far. */
113         return SIGROK_OK;
114 }
115
116 static void hw_closedev(int device_index)
117 {
118         /* Avoid compiler warnings. */
119         device_index = device_index;
120
121         /* Nothing needed so far. */
122 }
123
124 static void hw_cleanup(void)
125 {
126         /* Nothing needed so far. */
127 }
128
129 static void *hw_get_device_info(int device_index, int device_info_id)
130 {
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
137         switch (device_info_id) {
138         case DI_INSTANCE:
139                 info = sdi;
140                 break;
141         case DI_NUM_PROBES:
142                 info = GINT_TO_POINTER(NUM_PROBES);
143                 break;
144         case DI_CUR_SAMPLERATE:
145                 info = &cur_samplerate;
146                 break;
147         case DI_PATTERNMODES:
148                 info = &patternmodes;
149                 break;
150         }
151
152         return info;
153 }
154
155 static int hw_get_status(int device_index)
156 {
157         /* Avoid compiler warnings. */
158         device_index = device_index;
159
160         return ST_ACTIVE;
161 }
162
163 static int *hw_get_capabilities(void)
164 {
165         return capabilities;
166 }
167
168 static int hw_set_configuration(int device_index, int capability, void *value)
169 {
170         int ret;
171         uint64_t *tmp_u64;
172         char *stropt;
173
174         /* Avoid compiler warnings. */
175         device_index = device_index;
176
177         if (capability == HWCAP_PROBECONFIG) {
178                 /* Nothing to do. */
179                 ret = SIGROK_OK;
180         } else if (capability == HWCAP_LIMIT_SAMPLES) {
181                 tmp_u64 = value;
182                 limit_samples = *tmp_u64;
183                 ret = SIGROK_OK;
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                 }
195         } else {
196                 ret = SIGROK_ERR;
197         }
198
199         return ret;
200 }
201
202 static void samples_generator(uint8_t *buf, uint64_t size, void *data)
203 {
204         struct databag *mydata = data;
205         uint64_t p, i;
206
207         memset(buf, 0, size);
208
209         switch (mydata->sample_generator) {
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;
218         case GENMODE_RANDOM: /* Random */
219                 for (i = 0; i < size; i++)
220                         *(buf + i) = (uint8_t)(rand() & 0xff);
221                 break;
222         case GENMODE_INC: /* Simple increment */
223                 for (i = 0; i < size; i++)
224                         *(buf + i) = i;
225                 break;
226         }
227 }
228
229 /* Thread function */
230 static void thread_func(void *data)
231 {
232         struct databag *mydata = data;
233         uint8_t buf[BUFSIZE];
234         uint64_t nb_to_send = 0;
235         int bytes_written;
236
237         while (thread_running) {
238                 if (limit_samples)
239                         nb_to_send = limit_samples - mydata->samples_counter;
240                 else
241                         nb_to_send = BUFSIZE;  /* Continuous mode */
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
255                 g_io_channel_write_chars(channels[1], (gchar *)&buf,
256                                          nb_to_send, &bytes_written, NULL);
257
258                 g_usleep(mydata->loop_sleep);
259         }
260 }
261
262 /* Callback handling data */
263 static int receive_data(int fd, int revents, void *user_data)
264 {
265         struct datafeed_packet packet;
266         char c[BUFSIZE];
267         uint64_t z;
268
269         /* Avoid compiler warnings. */
270         revents = revents;
271
272         g_io_channel_read_chars(channels[0], (gchar *)&c, BUFSIZE, &z, NULL);
273
274         if (z > 0) {
275                 packet.type = DF_LOGIC;
276                 packet.length = z;
277                 packet.unitsize = 1;
278                 packet.payload = c;
279                 session_bus(user_data, &packet);
280         }
281         return TRUE;
282 }
283
284 static int hw_start_acquisition(int device_index, gpointer session_device_id)
285 {
286         struct datafeed_packet *packet;
287         struct datafeed_header *header;
288         struct databag *mydata;
289
290         mydata = malloc(sizeof(struct databag));
291         if (!mydata)
292                 return SIGROK_ERR_MALLOC;
293
294         mydata->sample_generator = default_genmode;
295         mydata->session_device_id = session_device_id;
296         mydata->device_index = device_index;
297         mydata->samples_counter = 0;
298         mydata->loop_sleep = 100000;
299
300         if (pipe(mydata->pipe_fds))
301                 return SIGROK_ERR;
302
303         channels[0] = g_io_channel_unix_new(mydata->pipe_fds[0]);
304         channels[1] = g_io_channel_unix_new(mydata->pipe_fds[1]);
305
306         /* Set channel encoding to binary (default is UTF-8). */
307         g_io_channel_set_encoding(channels[0], NULL, NULL);
308         g_io_channel_set_encoding(channels[1], NULL, NULL);
309
310         /* Make channels to unbuffered. */
311         g_io_channel_set_buffered(channels[0], FALSE);
312         g_io_channel_set_buffered(channels[1], FALSE);
313
314         source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data,
315                    session_device_id);
316
317         /* Run the demo thread. */
318         g_thread_init(NULL);
319         thread_running = 1;
320         my_thread =
321             g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
322         if (!my_thread)
323                 return SIGROK_ERR;
324
325         packet = malloc(sizeof(struct datafeed_packet));
326         header = malloc(sizeof(struct datafeed_header));
327         if (!packet || !header)
328                 return SIGROK_ERR_MALLOC;
329
330         packet->type = DF_HEADER;
331         packet->length = sizeof(struct datafeed_header);
332         packet->payload = (unsigned char *)header;
333         header->feed_version = 1;
334         gettimeofday(&header->starttime, NULL);
335         header->samplerate = cur_samplerate;
336         header->protocol_id = PROTO_RAW;
337         header->num_logic_probes = NUM_PROBES;
338         header->num_analog_probes = 0;
339         session_bus(session_device_id, packet);
340         free(header);
341         free(packet);
342
343         return SIGROK_OK;
344 }
345
346 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
347 {
348         struct datafeed_packet packet;
349
350         /* Avoid compiler warnings. */
351         device_index = device_index;
352
353         /* Send last packet. */
354         packet.type = DF_END;
355         session_bus(session_device_id, &packet);
356 }
357
358 struct device_plugin demo_plugin_info = {
359         "demo",
360         1,
361         hw_init,
362         hw_cleanup,
363         hw_opendev,
364         hw_closedev,
365         hw_get_device_info,
366         hw_get_status,
367         hw_get_capabilities,
368         hw_set_configuration,
369         hw_start_acquisition,
370         hw_stop_acquisition,
371 };