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