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