]> sigrok.org Git - libsigrok.git/blame - src/hardware/kern-scale/protocol.c
license: remove FSF postal address from boiler plate license text
[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
2ea1fdf1 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
9380ec2f
UH
18 */
19
6ec6c43b 20#include <config.h>
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;
563ba4a5
UH
35 struct sr_datafeed_analog analog;
36 struct sr_analog_encoding encoding;
37 struct sr_analog_meaning meaning;
38 struct sr_analog_spec spec;
607dcdea
UH
39 struct dev_context *devc;
40
41 scale = (struct scale_info *)sdi->driver;
42
43 devc = sdi->priv;
44
869c8375 45 /* Note: digits/spec_digits will be overridden later. */
563ba4a5 46 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
607dcdea 47
563ba4a5 48 analog.meaning->channels = sdi->channels;
607dcdea 49 analog.num_samples = 1;
563ba4a5 50 analog.meaning->mq = 0;
607dcdea
UH
51
52 scale->packet_parse(buf, &floatval, &analog, info);
53 analog.data = &floatval;
54
563ba4a5 55 if (analog.meaning->mq != 0) {
607dcdea 56 /* Got a measurement. */
563ba4a5 57 packet.type = SR_DF_ANALOG;
607dcdea 58 packet.payload = &analog;
695dc859 59 sr_session_send(sdi, &packet);
7fc9dc31 60 sr_sw_limits_update_samples_read(&devc->limits, 1);
607dcdea
UH
61 }
62}
63
64static void handle_new_data(struct sr_dev_inst *sdi, void *info)
65{
66 struct scale_info *scale;
67 struct dev_context *devc;
68 int len, i, offset = 0;
69 struct sr_serial_dev_inst *serial;
70
71 scale = (struct scale_info *)sdi->driver;
72
73 devc = sdi->priv;
74 serial = sdi->conn;
75
76 /* Try to get as much data as the buffer can hold. */
77 len = SCALE_BUFSIZE - devc->buflen;
78 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
79 if (len == 0)
80 return; /* No new bytes, nothing to do. */
81 if (len < 0) {
82 sr_err("Serial port read error: %d.", len);
83 return;
84 }
85 devc->buflen += len;
86
87 /* Now look for packets in that data. */
88 while ((devc->buflen - offset) >= scale->packet_size) {
89 if (scale->packet_valid(devc->buf + offset)) {
90 handle_packet(devc->buf + offset, sdi, info);
91 offset += scale->packet_size;
92 } else {
93 offset++;
94 }
95 }
96
97 /* If we have any data left, move it to the beginning of our buffer. */
98 for (i = 0; i < devc->buflen - offset; i++)
99 devc->buf[i] = devc->buf[offset + i];
100 devc->buflen -= offset;
101}
102
9380ec2f
UH
103SR_PRIV int kern_scale_receive_data(int fd, int revents, void *cb_data)
104{
607dcdea 105 struct sr_dev_inst *sdi;
9380ec2f 106 struct dev_context *devc;
607dcdea 107 struct scale_info *scale;
607dcdea 108 void *info;
9380ec2f
UH
109
110 (void)fd;
111
112 if (!(sdi = cb_data))
113 return TRUE;
114
115 if (!(devc = sdi->priv))
116 return TRUE;
117
607dcdea
UH
118 scale = (struct scale_info *)sdi->driver;
119
9380ec2f 120 if (revents == G_IO_IN) {
607dcdea
UH
121 /* Serial data arrived. */
122 info = g_malloc(scale->info_size);
123 handle_new_data(sdi, info);
124 g_free(info);
125 }
126
7fc9dc31 127 if (sr_sw_limits_check(&devc->limits))
695dc859 128 sdi->driver->dev_acquisition_stop(sdi);
9380ec2f
UH
129
130 return TRUE;
131}