]> sigrok.org Git - libsigrok.git/blob - hardware/demo/demo.c
cleaned up demo driver
[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 #include "config.h"
27
28 #define NUM_PROBES             8
29 #define DEMONAME               "Demo device"
30 /* size of chunks to send through the session bus */
31 #define BUFSIZE                4096
32
33
34 enum {
35         GENMODE_RANDOM,
36         GENMODE_INC,
37 };
38
39 struct databag {
40         int pipe_fds[2];
41         uint8_t sample_generator;
42         uint8_t thread_running;
43         uint64_t samples_counter;
44         int device_index;
45         int loop_sleep;
46         gpointer session_device_id;
47 };
48
49 static GThread *my_thread;
50 static int thread_running;
51
52 static int capabilities[] = {
53         HWCAP_LOGIC_ANALYZER,
54         HWCAP_PATTERN_MODE,
55         HWCAP_LIMIT_SAMPLES,
56         HWCAP_CONTINUOUS
57 };
58
59 static char *patternmodes[] = {
60         "random",
61         "incremental",
62         NULL
63 };
64
65 /* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
66 static GSList *device_instances = NULL;
67 static uint64_t cur_samplerate = 0;
68 static uint64_t limit_samples = -1;
69 static int default_genmode = GENMODE_RANDOM;
70
71
72 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
73
74 static int hw_init(char *deviceinfo)
75 {
76         struct sigrok_device_instance *sdi;
77
78         /* Avoid compiler warnings. */
79         deviceinfo = deviceinfo;
80
81         sdi = sigrok_device_instance_new(0, ST_ACTIVE, DEMONAME, NULL, NULL);
82         if (!sdi)
83                 return 0;
84
85         device_instances = g_slist_append(device_instances, sdi);
86
87         return 1;
88 }
89
90 static int hw_opendev(int device_index)
91 {
92         /* Avoid compiler warnings. */
93         device_index = device_index;
94
95         /* Nothing needed so far. */
96         return SIGROK_OK;
97 }
98
99 static void hw_closedev(int device_index)
100 {
101         /* Avoid compiler warnings. */
102         device_index = device_index;
103
104         /* Nothing needed so far. */
105 }
106
107 static void hw_cleanup(void)
108 {
109         /* Nothing needed so far. */
110 }
111
112 static void *hw_get_device_info(int device_index, int device_info_id)
113 {
114         struct sigrok_device_instance *sdi;
115         void *info = NULL;
116
117         if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
118                 return NULL;
119
120         switch (device_info_id) {
121         case DI_INSTANCE:
122                 info = sdi;
123                 break;
124         case DI_NUM_PROBES:
125                 info = GINT_TO_POINTER(NUM_PROBES);
126                 break;
127         case DI_CUR_SAMPLERATE:
128                 info = &cur_samplerate;
129                 break;
130         case DI_PATTERNMODES:
131                 info = &patternmodes;
132                 break;
133         }
134
135         return info;
136 }
137
138 static int hw_get_status(int device_index)
139 {
140         /* Avoid compiler warnings. */
141         device_index = device_index;
142
143         return ST_ACTIVE;
144 }
145
146 static int *hw_get_capabilities(void)
147 {
148
149         return capabilities;
150 }
151
152 static int hw_set_configuration(int device_index, int capability, void *value)
153 {
154         int ret;
155         uint64_t *tmp_u64;
156         char *stropt;
157
158         /* Avoid compiler warnings. */
159         device_index = device_index;
160
161         if (capability == HWCAP_PROBECONFIG) {
162                 /* nothing to do */
163                 ret = SIGROK_OK;
164         } else if (capability == HWCAP_LIMIT_SAMPLES) {
165                 tmp_u64 = value;
166                 limit_samples = *tmp_u64;
167                 ret = SIGROK_OK;
168         } else if (capability == HWCAP_PATTERN_MODE) {
169                 stropt = value;
170                 if (!strcmp(stropt, "random")) {
171                         default_genmode = GENMODE_RANDOM;
172                         ret = SIGROK_OK;
173                 } else if (!strcmp(stropt, "incremental")) {
174                         default_genmode = GENMODE_INC;
175                         ret = SIGROK_OK;
176                 } else {
177                         ret = SIGROK_ERR;
178                 }
179         } else {
180                 ret = SIGROK_ERR;
181         }
182
183         return ret;
184 }
185
186 static void samples_generator(uint8_t *buf, uint64_t size, void *data)
187 {
188         struct databag *mydata = data;
189         uint64_t i;
190
191         memset(buf, 0, size);
192
193         switch (mydata->sample_generator) {
194         case GENMODE_RANDOM: /* Random */
195                 for (i = 0; i < size; i++)
196                         *(buf + i) = (uint8_t)(rand() & 0xff);
197                 break;
198         case GENMODE_INC: /* Simple increment */
199                 for (i = 0; i < size; i++)
200                         *(buf + i) = i;
201                 break;
202         }
203 }
204
205 /* Thread function */
206 static void thread_func(void *data)
207 {
208         struct databag *mydata = data;
209         uint8_t buf[BUFSIZE];
210         uint64_t nb_to_send = 0;
211
212         while (thread_running) {
213                 if (limit_samples)
214                         nb_to_send = limit_samples - mydata->samples_counter;
215                 else
216                         nb_to_send = BUFSIZE;  // CONTINUOUS MODE
217
218                 if (nb_to_send == 0) {
219                         close(mydata->pipe_fds[1]);
220                         thread_running = 0;
221                         hw_stop_acquisition(mydata->device_index,
222                                             mydata->session_device_id);
223                 } else if (nb_to_send > BUFSIZE) {
224                         nb_to_send = BUFSIZE;
225                 }
226
227                 samples_generator(buf, nb_to_send, data);
228                 mydata->samples_counter += nb_to_send;
229
230                 write(mydata->pipe_fds[1], &buf, nb_to_send);
231                 g_usleep(mydata->loop_sleep);
232         }
233 }
234
235 /* Callback handling data */
236 static int receive_data(int fd, int revents, void *user_data)
237 {
238         struct datafeed_packet packet;
239         /* uint16_t samples[1000]; */
240         char c[BUFSIZE];
241         uint64_t z;
242
243         /* Avoid compiler warnings. */
244         revents = revents;
245
246         z = read(fd, &c, BUFSIZE);
247         if (z > 0) {
248                 packet.type = DF_LOGIC;
249                 packet.length = z;
250                 packet.unitsize = 1;
251                 packet.payload = c;
252                 session_bus(user_data, &packet);
253         }
254         return TRUE;
255 }
256
257 static int hw_start_acquisition(int device_index, gpointer session_device_id)
258 {
259         struct datafeed_packet *packet;
260         struct datafeed_header *header;
261         struct databag *mydata;
262
263         mydata = malloc(sizeof(struct databag));
264         if (!mydata)
265                 return SIGROK_ERR_MALLOC;
266
267         mydata->sample_generator = default_genmode;
268         mydata->session_device_id = session_device_id;
269         mydata->device_index = device_index;
270         mydata->samples_counter = 0;
271         mydata->loop_sleep = 100000;
272
273         if (pipe(mydata->pipe_fds))
274                 return SIGROK_ERR;
275
276         source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data,
277                    session_device_id);
278
279         /* Run the demo thread. */
280         g_thread_init(NULL);
281         thread_running = 1;
282         my_thread =
283             g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
284         if (!my_thread)
285                 return SIGROK_ERR;
286
287         packet = malloc(sizeof(struct datafeed_packet));
288         header = malloc(sizeof(struct datafeed_header));
289         if (!packet || !header)
290                 return SIGROK_ERR_MALLOC;
291
292         packet->type = DF_HEADER;
293         packet->length = sizeof(struct datafeed_header);
294         packet->payload = (unsigned char *)header;
295         header->feed_version = 1;
296         gettimeofday(&header->starttime, NULL);
297         header->samplerate = cur_samplerate;
298         header->protocol_id = PROTO_RAW;
299         header->num_logic_probes = NUM_PROBES;
300         header->num_analog_probes = 0;
301         session_bus(session_device_id, packet);
302         free(header);
303         free(packet);
304
305         return SIGROK_OK;
306 }
307
308 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
309 {
310         struct datafeed_packet packet;
311
312         /* Avoid compiler warnings. */
313         device_index = device_index;
314
315         /* Send last packet. */
316         packet.type = DF_END;
317         session_bus(session_device_id, &packet);
318
319 }
320
321 struct device_plugin demo_plugin_info = {
322         "demo",
323         1,
324         hw_init,
325         hw_cleanup,
326         hw_opendev,
327         hw_closedev,
328         hw_get_device_info,
329         hw_get_status,
330         hw_get_capabilities,
331         hw_set_configuration,
332         hw_start_acquisition,
333         hw_stop_acquisition,
334 };