]> sigrok.org Git - libsigrok.git/blame - src/hardware/kern-scale/protocol.c
SR_DF_ANALOG_OLD and sr_datafeed_analog_old renames.
[libsigrok.git] / src / hardware / kern-scale / protocol.c
CommitLineData
9380ec2f
UH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2015 Uwe Hermann <uwe@hermann-uwe.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
6ec6c43b 21#include <config.h>
607dcdea
UH
22#include <stdlib.h>
23#include <math.h>
24#include <string.h>
25#include <glib.h>
26#include <libsigrok/libsigrok.h>
27#include "libsigrok-internal.h"
9380ec2f
UH
28#include "protocol.h"
29
607dcdea
UH
30static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi,
31 void *info)
32{
33 struct scale_info *scale;
34 float floatval;
35 struct sr_datafeed_packet packet;
5faebab2 36 struct sr_datafeed_analog_old analog;
607dcdea
UH
37 struct dev_context *devc;
38
39 scale = (struct scale_info *)sdi->driver;
40
41 devc = sdi->priv;
42
5faebab2 43 memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
607dcdea
UH
44
45 analog.channels = sdi->channels;
46 analog.num_samples = 1;
47 analog.mq = -1;
48
49 scale->packet_parse(buf, &floatval, &analog, info);
50 analog.data = &floatval;
51
52 if (analog.mq != -1) {
53 /* Got a measurement. */
5faebab2 54 packet.type = SR_DF_ANALOG_OLD;
607dcdea
UH
55 packet.payload = &analog;
56 sr_session_send(devc->cb_data, &packet);
57 devc->num_samples++;
58 }
59}
60
61static void handle_new_data(struct sr_dev_inst *sdi, void *info)
62{
63 struct scale_info *scale;
64 struct dev_context *devc;
65 int len, i, offset = 0;
66 struct sr_serial_dev_inst *serial;
67
68 scale = (struct scale_info *)sdi->driver;
69
70 devc = sdi->priv;
71 serial = sdi->conn;
72
73 /* Try to get as much data as the buffer can hold. */
74 len = SCALE_BUFSIZE - devc->buflen;
75 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
76 if (len == 0)
77 return; /* No new bytes, nothing to do. */
78 if (len < 0) {
79 sr_err("Serial port read error: %d.", len);
80 return;
81 }
82 devc->buflen += len;
83
84 /* Now look for packets in that data. */
85 while ((devc->buflen - offset) >= scale->packet_size) {
86 if (scale->packet_valid(devc->buf + offset)) {
87 handle_packet(devc->buf + offset, sdi, info);
88 offset += scale->packet_size;
89 } else {
90 offset++;
91 }
92 }
93
94 /* If we have any data left, move it to the beginning of our buffer. */
95 for (i = 0; i < devc->buflen - offset; i++)
96 devc->buf[i] = devc->buf[offset + i];
97 devc->buflen -= offset;
98}
99
9380ec2f
UH
100SR_PRIV int kern_scale_receive_data(int fd, int revents, void *cb_data)
101{
607dcdea 102 struct sr_dev_inst *sdi;
9380ec2f 103 struct dev_context *devc;
607dcdea
UH
104 struct scale_info *scale;
105 int64_t time;
106 void *info;
9380ec2f
UH
107
108 (void)fd;
109
110 if (!(sdi = cb_data))
111 return TRUE;
112
113 if (!(devc = sdi->priv))
114 return TRUE;
115
607dcdea
UH
116 scale = (struct scale_info *)sdi->driver;
117
9380ec2f 118 if (revents == G_IO_IN) {
607dcdea
UH
119 /* Serial data arrived. */
120 info = g_malloc(scale->info_size);
121 handle_new_data(sdi, info);
122 g_free(info);
123 }
124
125 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
126 sr_info("Requested number of samples reached.");
127 sdi->driver->dev_acquisition_stop(sdi, cb_data);
128 return TRUE;
129 }
130
131 if (devc->limit_msec) {
132 time = (g_get_monotonic_time() - devc->starttime) / 1000;
133 if (time > (int64_t)devc->limit_msec) {
134 sr_info("Requested time limit reached.");
135 sdi->driver->dev_acquisition_stop(sdi, cb_data);
136 return TRUE;
137 }
9380ec2f
UH
138 }
139
140 return TRUE;
141}