+ GSList *l;
+ int ret;
+
+ (void)idx;
+
+ devc = sdi->priv;
+
+ ret = packet_parse((const char *)buf, idx, &temperature, &humidity);
+ if (ret < 0) {
+ sr_err("Failed to parse packet.");
+ return SR_ERR;
+ }
+
+ /* Clear 'analog', otherwise it'll contain random garbage. */
+ memset(&analog, 0, sizeof(struct sr_datafeed_analog));
+
+ /* Common values for both probes. */
+ packet.type = SR_DF_ANALOG;
+ packet.payload = &analog;
+ analog.num_samples = 1;
+
+ /* Temperature. */
+ l = g_slist_copy(sdi->probes);
+ l = g_slist_remove_link(l, g_slist_nth(l, 1));
+ analog.probes = l;
+ analog.mq = SR_MQ_TEMPERATURE;
+ analog.unit = SR_UNIT_CELSIUS; /* TODO: Use C/F correctly. */
+ analog.data = &temperature;
+ sr_session_send(devc->cb_data, &packet);
+ g_slist_free(l);
+
+ /* Humidity. */
+ if (mic_devs[idx].has_humidity) {
+ l = g_slist_copy(sdi->probes);
+ l = g_slist_remove_link(l, g_slist_nth(l, 0));
+ analog.probes = l;
+ analog.mq = SR_MQ_RELATIVE_HUMIDITY;
+ analog.unit = SR_UNIT_PERCENTAGE;
+ analog.data = &humidity;
+ sr_session_send(devc->cb_data, &packet);
+ g_slist_free(l);
+ }
+
+ devc->num_samples++;
+
+ return SR_OK;
+}
+
+static void handle_new_data(struct sr_dev_inst *sdi, int idx)
+{
+ struct dev_context *devc;
+ struct sr_serial_dev_inst *serial;
+ int len, i, offset = 0;
+
+ devc = sdi->priv;
+ serial = sdi->conn;
+
+ /* Try to get as much data as the buffer can hold. */
+ len = SERIAL_BUFSIZE - devc->buflen;
+ len = serial_read(serial, devc->buf + devc->buflen, len);
+ if (len < 1) {
+ sr_err("Serial port read error: %d.", len);
+ return;
+ }
+
+ devc->buflen += len;
+
+ /* Now look for packets in that data. */
+ while ((devc->buflen - offset) >= mic_devs[idx].packet_size) {
+ if (mic_devs[idx].packet_valid(devc->buf + offset)) {
+ handle_packet(devc->buf + offset, sdi, idx);
+ offset += mic_devs[idx].packet_size;
+ } else {
+ offset++;
+ }
+ }
+
+ /* If we have any data left, move it to the beginning of our buffer. */
+ for (i = 0; i < devc->buflen - offset; i++)
+ devc->buf[i] = devc->buf[offset + i];
+ devc->buflen -= offset;
+}
+
+static int receive_data(int fd, int revents, int idx, void *cb_data)
+{
+ struct sr_dev_inst *sdi;
+ struct dev_context *devc;
+ int64_t t;
+ static gboolean first_time = TRUE;
+ struct sr_serial_dev_inst *serial;
+
+ (void)fd;