]> sigrok.org Git - libsigrok.git/blame - src/hardware/kern-scale/protocol.c
libsigrok-internal.h: Remove unused prototypes
[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
607dcdea
UH
21#include <stdlib.h>
22#include <math.h>
23#include <string.h>
24#include <glib.h>
25#include <libsigrok/libsigrok.h>
26#include "libsigrok-internal.h"
9380ec2f
UH
27#include "protocol.h"
28
607dcdea
UH
29static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi,
30 void *info)
31{
32 struct scale_info *scale;
33 float floatval;
34 struct sr_datafeed_packet packet;
35 struct sr_datafeed_analog analog;
36 struct dev_context *devc;
37
38 scale = (struct scale_info *)sdi->driver;
39
40 devc = sdi->priv;
41
42 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
43
44 analog.channels = sdi->channels;
45 analog.num_samples = 1;
46 analog.mq = -1;
47
48 scale->packet_parse(buf, &floatval, &analog, info);
49 analog.data = &floatval;
50
51 if (analog.mq != -1) {
52 /* Got a measurement. */
53 packet.type = SR_DF_ANALOG;
54 packet.payload = &analog;
55 sr_session_send(devc->cb_data, &packet);
56 devc->num_samples++;
57 }
58}
59
60static void handle_new_data(struct sr_dev_inst *sdi, void *info)
61{
62 struct scale_info *scale;
63 struct dev_context *devc;
64 int len, i, offset = 0;
65 struct sr_serial_dev_inst *serial;
66
67 scale = (struct scale_info *)sdi->driver;
68
69 devc = sdi->priv;
70 serial = sdi->conn;
71
72 /* Try to get as much data as the buffer can hold. */
73 len = SCALE_BUFSIZE - devc->buflen;
74 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
75 if (len == 0)
76 return; /* No new bytes, nothing to do. */
77 if (len < 0) {
78 sr_err("Serial port read error: %d.", len);
79 return;
80 }
81 devc->buflen += len;
82
83 /* Now look for packets in that data. */
84 while ((devc->buflen - offset) >= scale->packet_size) {
85 if (scale->packet_valid(devc->buf + offset)) {
86 handle_packet(devc->buf + offset, sdi, info);
87 offset += scale->packet_size;
88 } else {
89 offset++;
90 }
91 }
92
93 /* If we have any data left, move it to the beginning of our buffer. */
94 for (i = 0; i < devc->buflen - offset; i++)
95 devc->buf[i] = devc->buf[offset + i];
96 devc->buflen -= offset;
97}
98
9380ec2f
UH
99SR_PRIV int kern_scale_receive_data(int fd, int revents, void *cb_data)
100{
607dcdea 101 struct sr_dev_inst *sdi;
9380ec2f 102 struct dev_context *devc;
607dcdea
UH
103 struct scale_info *scale;
104 int64_t time;
105 void *info;
9380ec2f
UH
106
107 (void)fd;
108
109 if (!(sdi = cb_data))
110 return TRUE;
111
112 if (!(devc = sdi->priv))
113 return TRUE;
114
607dcdea
UH
115 scale = (struct scale_info *)sdi->driver;
116
9380ec2f 117 if (revents == G_IO_IN) {
607dcdea
UH
118 /* Serial data arrived. */
119 info = g_malloc(scale->info_size);
120 handle_new_data(sdi, info);
121 g_free(info);
122 }
123
124 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
125 sr_info("Requested number of samples reached.");
126 sdi->driver->dev_acquisition_stop(sdi, cb_data);
127 return TRUE;
128 }
129
130 if (devc->limit_msec) {
131 time = (g_get_monotonic_time() - devc->starttime) / 1000;
132 if (time > (int64_t)devc->limit_msec) {
133 sr_info("Requested time limit reached.");
134 sdi->driver->dev_acquisition_stop(sdi, cb_data);
135 return TRUE;
136 }
9380ec2f
UH
137 }
138
139 return TRUE;
140}