Here is the patch.

On Sat, 08 Jan 2011 16:59:04 +0100, Olivier Fauchon
<[email protected]> wrote:
> Hi,
> 
> The attached patch fixes the demo driver.
> 
> When initialized, the driver starts a thread that generates signal 
> datas.
> These data are writen them to a pipe (write file descriptor).
> The other end of the pipe (read file descriptor), is connected to the 
> main polling code, like any other driver.
> 
> Note this patch adds a new dependancy with libgthreads.
> 
> 
> At the moment, you can list the driver's device:
> 
>  ./cli/sigrok-cli -D
> The following devices were found:
> ID  Device
> 0   Sigrok project Demo Driver v1.0 with 8 probes
> 
> 
> And use it for random signal generation
> 
> [oliv...@cooper sigrok]$ /opt/sigrok/bin/sigrok-cli  -d 0  --samples 
> 50   -f bits -p 1-8
> sigrok 0.1pre2
> Acquisition with 8/8 probes at 0 Hz
> 1:10111100 11010110 00001011 00011110 00111010 11110100 10
> 2:11010110 00111111 01001010 11111101 11010011 00010010 11
> 3:11000101 01000001 10100011 10100100 10110000 11110011 00
> 4:00100111 11110100 10011101 01100111 00100101 01001110 10
> 5:00011100 00101100 10111000 11001101 01011101 01011011 01
> 6:10110101 10111110 10010110 10111000 11011010 10000100 11
> 7:11111111 01001111 11110110 11010010 10000101 01001111 00
> 8:01000101 01111110 01010111 00000111 00010010 00000101 11
> 
> 
> Next step is to make demo driver customisable (per-probe signal clock, 
> reference sample signals : serial, i2x, can...)
> 
> 
> --
> Olivier Fauchon
> www.oflabs.com
> 
> 
> ------------------------------------------------------------------------------
> Gaining the trust of online customers is vital for the success of any company
> that requires sensitive data to be transmitted over the Web.   Learn how to 
> best implement a security strategy that keeps consumers' information secure 
> and instills the confidence they need to proceed with transactions.
> http://p.sf.net/sfu/oracle-sfdevnl 
> _______________________________________________
> sigrok-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/sigrok-devel

-- 
Olivier Fauchon
www.oflabs.com
diff --git a/configure.ac b/configure.ac
index 6965115..d2ea6d9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -82,6 +82,8 @@ fi
 AM_PATH_GLIB_2_0(2.0.0,
 	CFLAGS="$CFLAGS $GLIB_CFLAGS"; LIBS="$LIBS $GLIB_LIBS")
 
+AC_CHECK_LIB(gthread-2.0, g_thread_init)
+
 PKG_CHECK_MODULES([libusb], [libusb-1.0 >= 1.0.5],
 	[CFLAGS="$CFLAGS $libusb_CFLAGS";
 	LIBS="$LIBS $libusb_LIBS"])
diff --git a/libsigrok/hardware/demo/demo.c b/libsigrok/hardware/demo/demo.c
index b8f36fb..2cdd01a 100644
--- a/libsigrok/hardware/demo/demo.c
+++ b/libsigrok/hardware/demo/demo.c
@@ -19,10 +19,14 @@
  */
 
 #include <stdlib.h>
+#include <unistd.h>
 #include <string.h>
 #include <sigrok.h>
 #include "config.h"
 
+
+#define BUFSIZE 4096
+
 #define NUM_PROBES			8
 #define NUM_TRIGGER_STAGES		4
 #define TRIGGER_TYPES			"01"
@@ -30,6 +34,17 @@
 /* Software trigger implementation: positive values indicate trigger stage. */
 #define TRIGGER_FIRED			-1
 
+#define USB_MODEL_NAME "Demo Driver"
+#define USB_VENDOR_NAME "Sigrok project"
+#define USB_MODEL_VERSION "v1.0"
+
+#define GENMODE_RANDOM 1
+#define GENMODE_INC 2
+
+
+static GThread	*myThread;
+static int  thread_running;
+
 static int capabilities[] = {
 	HWCAP_LOGIC_ANALYZER,
 	HWCAP_SAMPLERATE,
@@ -54,6 +69,25 @@ static struct samplerates samplerates = {
 	supported_samplerates,
 };
 
+struct databag {
+	int pipe_fds[2];
+	uint8_t sample_generator;
+	uint8_t thread_running;
+	uint64_t samples_counter;
+	int device_index; 
+	int loop_sleep; 
+	gpointer session_device_id;
+};
+
+
+
+
+
+/* List of struct sigrok_device_instance, maintained by opendev()/closedev(). */
+static GSList *device_instances = NULL;
+
+
+
 /* TODO: All of these should go in a device-specific struct. */
 static uint64_t cur_samplerate = 0;
 static uint64_t limit_samples = 0;
@@ -67,61 +101,22 @@ static uint64_t limit_samples = 0;
 static int hw_set_configuration(int device_index, int capability, void *value);
 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
 
-#if 0
-static int configure_probes(GSList *probes)
-{
-	struct probe *probe;
-	GSList *l;
-	int probe_bit, stage, i;
-	char *tc;
-
-	probe_mask = 0;
-	for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
-		trigger_mask[i] = 0;
-		trigger_value[i] = 0;
-	}
-
-	stage = -1;
-	for (l = probes; l; l = l->next) {
-		probe = (struct probe *)l->data;
-		if (!(probe->enabled))
-			continue;
-		probe_bit = 1 << (probe->index - 1);
-		probe_mask |= probe_bit;
-		if (!(probe->trigger))
-			continue;
-
-		stage = 0;
-		for (tc = probe->trigger; *tc; tc++) {
-			trigger_mask[stage] |= probe_bit;
-			if (*tc == '1')
-				trigger_value[stage] |= probe_bit;
-			stage++;
-			if (stage > NUM_TRIGGER_STAGES)
-				return SIGROK_ERR;
-		}
-	}
-
-	if (stage == -1)
-		/*
-		 * We didn't configure any triggers, make sure acquisition
-		 * doesn't wait for any.
-		 */
-		trigger_stage = TRIGGER_FIRED;
-	else
-		trigger_stage = 0;
-
-	return SIGROK_OK;
-}
-#endif
 
 static int hw_init(char *deviceinfo)
 {
 	/* Avoid compiler warning. */
 	deviceinfo = deviceinfo;
 
-	/* Nothing needed so far. */
-	return 1; /* FIXME? */
+	struct sigrok_device_instance *sdi;
+
+	sdi = sigrok_device_instance_new(0, ST_ACTIVE,
+	      	USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
+
+	if (!sdi)
+                 return 0;
+	device_instances = g_slist_append(device_instances, sdi);
+
+	return 1; 
 }
 
 static int hw_opendev(int device_index)
@@ -148,15 +143,19 @@ static void hw_cleanup(void)
 
 static void *hw_get_device_info(int device_index, int device_info_id)
 {
-	void *info = NULL;
-
 	/* Avoid compiler warning. */
 	device_index = device_index;
 
+        struct sigrok_device_instance *sdi;
+	void *info = NULL;
+
+
+        if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
+                return NULL;
+
 	switch (device_info_id) {
 	case DI_INSTANCE:
-		/// info = sdi;
-		/* TODO */
+		info = sdi;
 		break;
 	case DI_NUM_PROBES:
 		info = GINT_TO_POINTER(NUM_PROBES);
@@ -179,7 +178,6 @@ static int hw_get_status(int device_index)
 {
 	/* Avoid compiler warning. */
 	device_index = device_index;
-
 	return 0; /* FIXME */
 }
 
@@ -191,6 +189,8 @@ static int *hw_get_capabilities(void)
 static int hw_set_configuration(int device_index, int capability, void *value)
 {
 	int ret;
+        uint64_t *tmp_u64;
+
 
 	/* Avoid compiler warning. */
 	device_index = device_index;
@@ -199,10 +199,11 @@ static int hw_set_configuration(int device_index, int capability, void *value)
 		cur_samplerate = *(uint64_t *)value;
 		ret = SIGROK_OK;
 	} else if (capability == HWCAP_PROBECONFIG) {
-		// ret = configure_probes((GSList *) value);
-		ret = SIGROK_ERR;
+		// ret = configure_probes((GSList *) value); FIXME 
+		ret = SIGROK_OK;
 	} else if (capability == HWCAP_LIMIT_SAMPLES) {
-		limit_samples = strtoull(value, NULL, 10);
+                tmp_u64 = value;
+                limit_samples = *tmp_u64;
 		ret = SIGROK_OK;
 	} else {
 		ret = SIGROK_ERR;
@@ -211,12 +212,116 @@ static int hw_set_configuration(int device_index, int capability, void *value)
 	return ret;
 }
 
+static void 
+samples_generator(uint8_t *buf, uint64_t sz, void *data){
+	struct databag *mydata = data;
+
+	uint64_t i;
+	uint8_t val; 
+	memset(buf,0,sz);
+	switch (mydata->sample_generator){
+		// Random
+		case GENMODE_RANDOM:
+			for (i=0; i < sz; i++) {
+				val=rand() & 0xFF ;
+				*(buf+i) = val;
+			}
+			break;
+	
+		// Simple increment
+		case GENMODE_INC:
+			for (i=0; i < sz; i++) {
+				*(buf+i) = i;
+			}
+			break;
+	}
+}
+
+/* Thread function */
+static void thread_func (void *data)
+{
+	struct databag *mydata = data;
+
+	uint8_t buf[BUFSIZE];
+	uint64_t nb_to_send = 0;
+
+	while(thread_running){
+
+		nb_to_send =  limit_samples - mydata->samples_counter;
+		
+		if (nb_to_send == 0) {
+			close(mydata->pipe_fds[1]);
+			thread_running = 0;
+			hw_stop_acquisition(mydata->device_index, mydata->session_device_id);
+		}
+		else if (nb_to_send > BUFSIZE) 
+			nb_to_send = BUFSIZE; 
+
+		samples_generator(buf, nb_to_send, data);
+		mydata->samples_counter += nb_to_send;
+
+		write (mydata->pipe_fds[1], &buf , nb_to_send);
+		g_usleep(mydata->loop_sleep);
+	}
+}
+
+
+/* Callback handling datas */
+static int receive_data(int fd, int revents, void *user_data)
+{
+	revents=revents;
+
+        struct datafeed_packet packet;
+        //uint16_t samples[1000];
+
+	char c[BUFSIZE] ; 
+	uint64_t z = read(fd, &c, BUFSIZE);
+
+	if (z>0){
+		packet.type = DF_LOGIC8;
+		packet.length = z;
+		packet.payload = c;
+		session_bus(user_data, &packet);
+	}
+        return TRUE;
+}
+
+
 static int hw_start_acquisition(int device_index, gpointer session_device_id)
 {
 	struct datafeed_packet *packet;
 	struct datafeed_header *header;
 	unsigned char *buf;
 
+	struct databag *mydata; 
+	mydata  = malloc(sizeof(struct databag));
+	
+
+	mydata->sample_generator = GENMODE_RANDOM;
+	mydata->session_device_id = session_device_id;
+	mydata->device_index = device_index;
+	mydata->samples_counter=0;
+	mydata->loop_sleep = 100000;
+
+
+       if ( pipe(mydata->pipe_fds) )
+         {
+		
+           fprintf (stderr, "Pipe failed.\n");
+		return SIGROK_ERR_MALLOC; // FIXME
+
+         }
+	source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR , 40, receive_data, session_device_id );
+
+	// Run the demo thread
+	g_thread_init(NULL);
+	thread_running = 1;
+	myThread = g_thread_create ((GThreadFunc)thread_func, mydata, TRUE, NULL);
+	if (!myThread) {
+           fprintf (stderr, "demo: Thread creation failed.\n");
+		return SIGROK_ERR_MALLOC; // FIXME
+	}
+
 	/* Avoid compiler warning. */
 	device_index = device_index;
 
@@ -244,61 +349,6 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
 	return SIGROK_OK;
 }
 
-#if 0
-void receive_transfer(struct libusb_transfer *transfer)
-{
-	static int num_samples = 0;
-	static int empty_transfer_count = 0;
-	struct datafeed_packet packet;
-	void *user_data;
-	int cur_buflen, trigger_offset, i;
-	unsigned char *cur_buf, *new_buf;
-
-	g_message("receive_transfer(): status %d received %d bytes",
-		  transfer->status, transfer->actual_length);
-
-	/* Save incoming transfer before reusing the transfer struct. */
-	cur_buf = transfer->buffer;
-	cur_buflen = transfer->actual_length;
-	user_data = transfer->user_data;
-
-	/* Fire off a new request. */
-	new_buf = g_malloc(4096);
-	transfer->buffer = new_buf;
-	transfer->length = 4096;
-	if (libusb_submit_transfer(transfer) != 0) {
-		/* TODO: Stop session? */
-		g_warning("eek");
-	}
-
-	trigger_offset = 0;
-	if (trigger_stage >= 0) {
-		for (i = 0; i < cur_buflen; i++) {
-			trigger_helper(i, cur_buf, &packet, user_data,
-				       &trigger_offset);
-		}
-	}
-
-	if (trigger_stage == TRIGGER_FIRED) {
-		/* Send the incoming transfer to the session bus. */
-		packet.type = DF_LOGIC8;
-		packet.length = cur_buflen - trigger_offset;
-		packet.payload = cur_buf + trigger_offset;
-		session_bus(user_data, &packet);
-		free(cur_buf);
-
-		num_samples += cur_buflen;
-		if ((unsigned int)num_samples > limit_samples) {
-			hw_stop_acquisition(-1, user_data);
-		}
-	} else {
-		/*
-		 * TODO: Buffer pre-trigger data in capture
-		 * ratio-sized buffer.
-		 */
-	}
-}
-#endif
 
 /* This stops acquisition on ALL devices, ignoring device_index. */
 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
@@ -308,12 +358,9 @@ static void hw_stop_acquisition(int device_index, gpointer session_device_id)
 	/* QUICK HACK */
 	device_index = device_index;
 
+	// Send last packet
 	packet.type = DF_END;
 	session_bus(session_device_id, &packet);
-
-	/// receive_transfer(NULL);
-
-	/* TODO: Need to cancel and free any queued up transfers. */
 }
 
 struct device_plugin demo_plugin_info = {
------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web.   Learn how to 
best implement a security strategy that keeps consumers' information secure 
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl 
_______________________________________________
sigrok-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sigrok-devel

Reply via email to