]> sigrok.org Git - libsigrok.git/blob - hardware/demo/demo.c
Fix warnings.
[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, (gsize *)&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         fd = fd;
271         revents = revents;
272
273         g_io_channel_read_chars(channels[0], (gchar *)&c, BUFSIZE, &z, NULL);
274
275         if (z > 0) {
276                 packet.type = DF_LOGIC;
277                 packet.length = z;
278                 packet.unitsize = 1;
279                 packet.payload = c;
280                 session_bus(user_data, &packet);
281         }
282         return TRUE;
283 }
284
285 static int hw_start_acquisition(int device_index, gpointer session_device_id)
286 {
287         struct datafeed_packet *packet;
288         struct datafeed_header *header;
289         struct databag *mydata;
290
291         mydata = malloc(sizeof(struct databag));
292         if (!mydata)
293                 return SIGROK_ERR_MALLOC;
294
295         mydata->sample_generator = default_genmode;
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
301         if (pipe(mydata->pipe_fds))
302                 return SIGROK_ERR;
303
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
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);
323         if (!my_thread)
324                 return SIGROK_ERR;
325
326         packet = malloc(sizeof(struct datafeed_packet));
327         header = malloc(sizeof(struct datafeed_header));
328         if (!packet || !header)
329                 return SIGROK_ERR_MALLOC;
330
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;
338         header->num_logic_probes = NUM_PROBES;
339         header->num_analog_probes = 0;
340         session_bus(session_device_id, packet);
341         free(header);
342         free(packet);
343
344         return SIGROK_OK;
345 }
346
347 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
348 {
349         struct datafeed_packet packet;
350
351         /* Avoid compiler warnings. */
352         device_index = device_index;
353
354         /* Send last packet. */
355         packet.type = DF_END;
356         session_bus(session_device_id, &packet);
357 }
358
359 struct 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 };