- struct datafeed_packet *packet;
- struct datafeed_header *header;
- struct databag *mydata;
-
- mydata = malloc(sizeof(struct databag));
- if (!mydata)
- return SIGROK_ERR_MALLOC;
-
- mydata->sample_generator = default_genmode;
- 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))
- return SIGROK_ERR;
-
- 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;
- my_thread =
- g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL);
- if (!my_thread)
- return SIGROK_ERR;
-
- packet = malloc(sizeof(struct datafeed_packet));
- header = malloc(sizeof(struct datafeed_header));
- if (!packet || !header)
- return SIGROK_ERR_MALLOC;
-
- packet->type = DF_HEADER;
- packet->length = sizeof(struct datafeed_header);
- packet->payload = (unsigned char *)header;
- header->feed_version = 1;
- gettimeofday(&header->starttime, NULL);
- header->samplerate = cur_samplerate;
- header->protocol_id = PROTO_RAW;
- header->num_logic_probes = NUM_PROBES;
- header->num_analog_probes = 0;
- session_bus(session_device_id, packet);
- free(header);
- free(packet);
-
- return SIGROK_OK;
+ struct dev_context *devc;
+
+ (void)sdi;
+
+ /* TODO: 'devc' is never g_free()'d? */
+ if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
+ sr_err("%s: devc malloc failed", __func__);
+ return SR_ERR_MALLOC;
+ }
+
+ devc->sample_generator = default_pattern;
+ devc->cb_data = cb_data;
+ devc->samples_counter = 0;
+
+ /*
+ * Setting two channels connected by a pipe is a remnant from when the
+ * demo driver generated data in a thread, and collected and sent the
+ * data in the main program loop.
+ * They are kept here because it provides a convenient way of setting
+ * up a timeout-based polling mechanism.
+ */
+ if (pipe(devc->pipe_fds)) {
+ /* TODO: Better error message. */
+ sr_err("%s: pipe() failed", __func__);
+ return SR_ERR;
+ }
+
+ devc->channels[0] = g_io_channel_unix_new(devc->pipe_fds[0]);
+ devc->channels[1] = g_io_channel_unix_new(devc->pipe_fds[1]);
+
+ g_io_channel_set_flags(devc->channels[0], G_IO_FLAG_NONBLOCK, NULL);
+
+ /* Set channel encoding to binary (default is UTF-8). */
+ g_io_channel_set_encoding(devc->channels[0], NULL, NULL);
+ g_io_channel_set_encoding(devc->channels[1], NULL, NULL);
+
+ /* Make channels to unbuffered. */
+ g_io_channel_set_buffered(devc->channels[0], FALSE);
+ g_io_channel_set_buffered(devc->channels[1], FALSE);
+
+ sr_session_source_add_channel(devc->channels[0], G_IO_IN | G_IO_ERR,
+ 40, receive_data, devc);
+
+ /* Send header packet to the session bus. */
+ std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
+
+ /* We use this timestamp to decide how many more samples to send. */
+ devc->starttime = g_get_monotonic_time();
+
+ return SR_OK;