]> sigrok.org Git - libsigrok.git/blob - src/hardware/kern-scale/protocol.c
Remove unnecessary std_serial_dev_acquisition_stop() wrappers
[libsigrok.git] / src / hardware / kern-scale / protocol.c
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
21 #include <config.h>
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"
28 #include "protocol.h"
29
30 static 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;
36         struct sr_datafeed_analog_old analog;
37         struct dev_context *devc;
38
39         scale = (struct scale_info *)sdi->driver;
40
41         devc = sdi->priv;
42
43         memset(&analog, 0, sizeof(struct sr_datafeed_analog_old));
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. */
54                 packet.type = SR_DF_ANALOG_OLD;
55                 packet.payload = &analog;
56                 sr_session_send(sdi, &packet);
57                 sr_sw_limits_update_samples_read(&devc->limits, 1);
58         }
59 }
60
61 static 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
100 SR_PRIV int kern_scale_receive_data(int fd, int revents, void *cb_data)
101 {
102         struct sr_dev_inst *sdi;
103         struct dev_context *devc;
104         struct scale_info *scale;
105         void *info;
106
107         (void)fd;
108
109         if (!(sdi = cb_data))
110                 return TRUE;
111
112         if (!(devc = sdi->priv))
113                 return TRUE;
114
115         scale = (struct scale_info *)sdi->driver;
116
117         if (revents == G_IO_IN) {
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 (sr_sw_limits_check(&devc->limits))
125                 sdi->driver->dev_acquisition_stop(sdi);
126
127         return TRUE;
128 }