2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2011 Olivier Fauchon <olivier@aixmarseille.com>
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.
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.
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
29 #define DEMONAME "Demo device"
30 /* size of chunks to send through the session bus */
41 uint8_t sample_generator;
42 uint8_t thread_running;
43 uint64_t samples_counter;
46 gpointer session_device_id;
49 static GThread *my_thread;
50 static int thread_running;
52 static int capabilities[] = {
59 static char *patternmodes[] = {
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;
72 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
74 static int hw_init(char *deviceinfo)
76 struct sigrok_device_instance *sdi;
78 /* Avoid compiler warnings. */
79 deviceinfo = deviceinfo;
81 sdi = sigrok_device_instance_new(0, ST_ACTIVE, DEMONAME, NULL, NULL);
85 device_instances = g_slist_append(device_instances, sdi);
90 static int hw_opendev(int device_index)
92 /* Avoid compiler warnings. */
93 device_index = device_index;
95 /* Nothing needed so far. */
99 static void hw_closedev(int device_index)
101 /* Avoid compiler warnings. */
102 device_index = device_index;
104 /* Nothing needed so far. */
107 static void hw_cleanup(void)
109 /* Nothing needed so far. */
112 static void *hw_get_device_info(int device_index, int device_info_id)
114 struct sigrok_device_instance *sdi;
117 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
120 switch (device_info_id) {
125 info = GINT_TO_POINTER(NUM_PROBES);
127 case DI_CUR_SAMPLERATE:
128 info = &cur_samplerate;
130 case DI_PATTERNMODES:
131 info = &patternmodes;
138 static int hw_get_status(int device_index)
140 /* Avoid compiler warnings. */
141 device_index = device_index;
146 static int *hw_get_capabilities(void)
152 static int hw_set_configuration(int device_index, int capability, void *value)
158 /* Avoid compiler warnings. */
159 device_index = device_index;
161 if (capability == HWCAP_PROBECONFIG) {
164 } else if (capability == HWCAP_LIMIT_SAMPLES) {
166 limit_samples = *tmp_u64;
168 } else if (capability == HWCAP_PATTERN_MODE) {
170 if (!strcmp(stropt, "random")) {
171 default_genmode = GENMODE_RANDOM;
173 } else if (!strcmp(stropt, "incremental")) {
174 default_genmode = GENMODE_INC;
186 static void samples_generator(uint8_t *buf, uint64_t size, void *data)
188 struct databag *mydata = data;
191 memset(buf, 0, size);
193 switch (mydata->sample_generator) {
194 case GENMODE_RANDOM: /* Random */
195 for (i = 0; i < size; i++)
196 *(buf + i) = (uint8_t)(rand() & 0xff);
198 case GENMODE_INC: /* Simple increment */
199 for (i = 0; i < size; i++)
205 /* Thread function */
206 static void thread_func(void *data)
208 struct databag *mydata = data;
209 uint8_t buf[BUFSIZE];
210 uint64_t nb_to_send = 0;
212 while (thread_running) {
214 nb_to_send = limit_samples - mydata->samples_counter;
216 nb_to_send = BUFSIZE; // CONTINUOUS MODE
218 if (nb_to_send == 0) {
219 close(mydata->pipe_fds[1]);
221 hw_stop_acquisition(mydata->device_index,
222 mydata->session_device_id);
223 } else if (nb_to_send > BUFSIZE) {
224 nb_to_send = BUFSIZE;
227 samples_generator(buf, nb_to_send, data);
228 mydata->samples_counter += nb_to_send;
230 write(mydata->pipe_fds[1], &buf, nb_to_send);
231 g_usleep(mydata->loop_sleep);
235 /* Callback handling data */
236 static int receive_data(int fd, int revents, void *user_data)
238 struct datafeed_packet packet;
239 /* uint16_t samples[1000]; */
243 /* Avoid compiler warnings. */
246 z = read(fd, &c, BUFSIZE);
248 packet.type = DF_LOGIC;
252 session_bus(user_data, &packet);
257 static int hw_start_acquisition(int device_index, gpointer session_device_id)
259 struct datafeed_packet *packet;
260 struct datafeed_header *header;
261 struct databag *mydata;
263 mydata = malloc(sizeof(struct databag));
265 return SIGROK_ERR_MALLOC;
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;
273 if (pipe(mydata->pipe_fds))
276 source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data,
279 /* Run the demo thread. */
283 g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
287 packet = malloc(sizeof(struct datafeed_packet));
288 header = malloc(sizeof(struct datafeed_header));
289 if (!packet || !header)
290 return SIGROK_ERR_MALLOC;
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);
308 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
310 struct datafeed_packet packet;
312 /* Avoid compiler warnings. */
313 device_index = device_index;
315 /* Send last packet. */
316 packet.type = DF_END;
317 session_bus(session_device_id, &packet);
321 struct device_plugin demo_plugin_info = {
331 hw_set_configuration,
332 hw_start_acquisition,