]> sigrok.org Git - libsigrok.git/blame - hardware/serial-dmm/protocol.c
metex14: Add sr_metex14_packet_request().
[libsigrok.git] / hardware / serial-dmm / protocol.c
CommitLineData
7dc55d93
AG
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
729b01f9 5 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
7dc55d93
AG
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <glib.h>
7dc55d93
AG
22#include <stdlib.h>
23#include <math.h>
24#include <string.h>
25#include <errno.h>
bbabddbd
UH
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
28#include "protocol.h"
7dc55d93 29
6bef68a7
UH
30static void log_dmm_packet(const uint8_t *buf)
31{
32 sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x"
33 " %02x %02x %02x %02x %02x %02x %02x",
34 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
35 buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
36}
37
729b01f9
UH
38SR_PRIV void dmm_details_tp4000zc(struct sr_datafeed_analog *analog, void *info)
39{
40 struct fs9721_info *info_local;
41
42 info_local = (struct fs9721_info *)info;
43
44 /* User-defined FS9721_LP3 flag 'c2c1_10' means temperature. */
45 if (info_local->is_c2c1_10) {
46 analog->mq = SR_MQ_TEMPERATURE;
47 /* No Kelvin or Fahrenheit from the device, just Celsius. */
48 analog->unit = SR_UNIT_CELSIUS;
49 }
50}
51
52static void handle_packet(const uint8_t *buf, struct dev_context *devc,
53 int dmm, void *info)
7dc55d93 54{
8c1adf37 55 float floatval;
7dc55d93
AG
56 struct sr_datafeed_packet packet;
57 struct sr_datafeed_analog *analog;
58
6bef68a7
UH
59 log_dmm_packet(buf);
60
bbabddbd 61 if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)))) {
8c1adf37 62 sr_err("Analog packet malloc failed.");
7dc55d93
AG
63 return;
64 }
bbabddbd 65
bbabddbd 66 analog->num_samples = 1;
7dc55d93
AG
67 analog->mq = -1;
68
729b01f9 69 dmms[dmm].packet_parse(buf, &floatval, analog, info);
d84fc9cb 70 analog->data = &floatval;
be5c1d3b 71
729b01f9 72 dmms[dmm].dmm_details(analog, info);
7dc55d93 73
7dc55d93
AG
74 if (analog->mq != -1) {
75 /* Got a measurement. */
7dc55d93
AG
76 packet.type = SR_DF_ANALOG;
77 packet.payload = analog;
78 sr_session_send(devc->cb_data, &packet);
79 devc->num_samples++;
80 }
bbabddbd 81
7dc55d93
AG
82 g_free(analog);
83}
84
729b01f9 85static void handle_new_data(struct dev_context *devc, int dmm, void *info)
7dc55d93 86{
bbabddbd 87 int len, i, offset = 0;
bbabddbd
UH
88
89 /* Try to get as much data as the buffer can hold. */
7dc55d93 90 len = DMM_BUFSIZE - devc->buflen;
d35afa87 91 len = serial_read(devc->serial, devc->buf + devc->buflen, len);
7dc55d93 92 if (len < 1) {
bbabddbd 93 sr_err("Serial port read error: %d.", len);
7dc55d93
AG
94 return;
95 }
96 devc->buflen += len;
97
bbabddbd 98 /* Now look for packets in that data. */
729b01f9
UH
99 while ((devc->buflen - offset) >= dmms[dmm].packet_size) {
100 if (dmms[dmm].packet_valid(devc->buf + offset)) {
101 handle_packet(devc->buf + offset, devc, dmm, info);
102 offset += dmms[dmm].packet_size;
7dc55d93
AG
103 } else {
104 offset++;
105 }
106 }
107
bbabddbd
UH
108 /* If we have any data left, move it to the beginning of our buffer. */
109 for (i = 0; i < devc->buflen - offset; i++)
7dc55d93
AG
110 devc->buf[i] = devc->buf[offset + i];
111 devc->buflen -= offset;
112}
113
729b01f9 114static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data)
7dc55d93 115{
642e9d62 116 struct sr_dev_inst *sdi;
7dc55d93
AG
117 struct dev_context *devc;
118
d35afa87
BV
119 (void)fd;
120
7dc55d93
AG
121 if (!(sdi = cb_data))
122 return TRUE;
123
124 if (!(devc = sdi->priv))
125 return TRUE;
126
bbabddbd 127 if (revents == G_IO_IN) {
7dc55d93 128 /* Serial data arrived. */
729b01f9 129 handle_new_data(devc, dmm, info);
7dc55d93
AG
130 }
131
132 if (devc->num_samples >= devc->limit_samples) {
6bef68a7 133 sr_info("Requested number of samples reached, stopping.");
7dc55d93
AG
134 sdi->driver->dev_acquisition_stop(sdi, cb_data);
135 return TRUE;
136 }
137
138 return TRUE;
139}
729b01f9
UH
140
141SR_PRIV int tekpower_tp4000zc_receive_data(int fd, int revents, void *cb_data)
142{
143 struct fs9721_info info;
144
145 return receive_data(fd, revents, TEKPOWER_TP4000ZC, &info, cb_data);
146}