]>
Commit | Line | Data |
---|---|---|
7dc55d93 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
7dc55d93 AG |
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 | ||
6ec6c43b | 21 | #include <config.h> |
7dc55d93 AG |
22 | #include <stdlib.h> |
23 | #include <math.h> | |
24 | #include <string.h> | |
22f54192 | 25 | #include <glib.h> |
c1aae900 | 26 | #include <libsigrok/libsigrok.h> |
bbabddbd UH |
27 | #include "libsigrok-internal.h" |
28 | #include "protocol.h" | |
7dc55d93 | 29 | |
6bef68a7 UH |
30 | static void log_dmm_packet(const uint8_t *buf) |
31 | { | |
c36f78f7 UH |
32 | sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x " |
33 | "%02x %02x %02x %02x %02x %02x %02x %02x %02x " | |
34 | "%02x %02x %02x %02x %02x %02x %02x", | |
6bef68a7 | 35 | buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], |
c36f78f7 UH |
36 | buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], |
37 | buf[14], buf[15], buf[16], buf[17], buf[18], buf[19], buf[20], | |
38 | buf[21], buf[22]); | |
6bef68a7 UH |
39 | } |
40 | ||
69e19dd7 | 41 | static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, |
bcbef5ed | 42 | void *info) |
7dc55d93 | 43 | { |
1a863916 | 44 | struct dmm_info *dmm; |
8c1adf37 | 45 | float floatval; |
7dc55d93 | 46 | struct sr_datafeed_packet packet; |
ffa2b6f9 UH |
47 | struct sr_datafeed_analog analog; |
48 | struct sr_analog_encoding encoding; | |
49 | struct sr_analog_meaning meaning; | |
50 | struct sr_analog_spec spec; | |
69e19dd7 | 51 | struct dev_context *devc; |
7dc55d93 | 52 | |
1a863916 UH |
53 | dmm = (struct dmm_info *)sdi->driver; |
54 | ||
6bef68a7 | 55 | log_dmm_packet(buf); |
69e19dd7 | 56 | devc = sdi->priv; |
6bef68a7 | 57 | |
ffa2b6f9 | 58 | sr_analog_init(&analog, &encoding, &meaning, &spec, 0); |
bbabddbd | 59 | |
ffa2b6f9 | 60 | analog.meaning->channels = sdi->channels; |
7c8a9e1e | 61 | analog.num_samples = 1; |
ffa2b6f9 | 62 | analog.meaning->mq = 0; |
7dc55d93 | 63 | |
bcbef5ed | 64 | dmm->packet_parse(buf, &floatval, &analog, info); |
7c8a9e1e | 65 | analog.data = &floatval; |
be5c1d3b | 66 | |
22f54192 | 67 | /* If this DMM needs additional handling, call the resp. function. */ |
bcbef5ed ML |
68 | if (dmm->dmm_details) |
69 | dmm->dmm_details(&analog, info); | |
7dc55d93 | 70 | |
ffa2b6f9 | 71 | if (analog.meaning->mq != 0) { |
7dc55d93 | 72 | /* Got a measurement. */ |
ffa2b6f9 | 73 | packet.type = SR_DF_ANALOG; |
7c8a9e1e | 74 | packet.payload = &analog; |
695dc859 | 75 | sr_session_send(sdi, &packet); |
9f51c463 | 76 | sr_sw_limits_update_samples_read(&devc->limits, 1); |
7dc55d93 | 77 | } |
7dc55d93 AG |
78 | } |
79 | ||
2baac44d | 80 | /** Request packet, if required. */ |
bcbef5ed | 81 | SR_PRIV int req_packet(struct sr_dev_inst *sdi) |
2baac44d | 82 | { |
1a863916 | 83 | struct dmm_info *dmm; |
2baac44d MH |
84 | struct dev_context *devc; |
85 | struct sr_serial_dev_inst *serial; | |
86 | int ret; | |
87 | ||
1a863916 UH |
88 | dmm = (struct dmm_info *)sdi->driver; |
89 | ||
bcbef5ed | 90 | if (!dmm->packet_request) |
2baac44d MH |
91 | return SR_OK; |
92 | ||
93 | devc = sdi->priv; | |
94 | serial = sdi->conn; | |
95 | ||
96 | if (devc->req_next_at && (devc->req_next_at > g_get_monotonic_time())) { | |
97 | sr_spew("Not requesting new packet yet, %" PRIi64 " ms left.", | |
98 | ((devc->req_next_at - g_get_monotonic_time()) / 1000)); | |
99 | return SR_OK; | |
100 | } | |
101 | ||
bcbef5ed | 102 | ret = dmm->packet_request(serial); |
2baac44d MH |
103 | if (ret < 0) { |
104 | sr_err("Failed to request packet: %d.", ret); | |
105 | return ret; | |
106 | } | |
107 | ||
bcbef5ed ML |
108 | if (dmm->req_timeout_ms) |
109 | devc->req_next_at = g_get_monotonic_time() + (dmm->req_timeout_ms * 1000); | |
2baac44d MH |
110 | |
111 | return SR_OK; | |
112 | } | |
113 | ||
bcbef5ed | 114 | static void handle_new_data(struct sr_dev_inst *sdi, void *info) |
7dc55d93 | 115 | { |
1a863916 | 116 | struct dmm_info *dmm; |
69e19dd7 | 117 | struct dev_context *devc; |
bbabddbd | 118 | int len, i, offset = 0; |
1e1bfcd0 | 119 | struct sr_serial_dev_inst *serial; |
bbabddbd | 120 | |
1a863916 UH |
121 | dmm = (struct dmm_info *)sdi->driver; |
122 | ||
69e19dd7 | 123 | devc = sdi->priv; |
1e1bfcd0 | 124 | serial = sdi->conn; |
69e19dd7 | 125 | |
bbabddbd | 126 | /* Try to get as much data as the buffer can hold. */ |
7dc55d93 | 127 | len = DMM_BUFSIZE - devc->buflen; |
4277ac34 | 128 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len); |
66a43576 UH |
129 | if (len == 0) |
130 | return; /* No new bytes, nothing to do. */ | |
131 | if (len < 0) { | |
bbabddbd | 132 | sr_err("Serial port read error: %d.", len); |
7dc55d93 AG |
133 | return; |
134 | } | |
135 | devc->buflen += len; | |
136 | ||
bbabddbd | 137 | /* Now look for packets in that data. */ |
bcbef5ed ML |
138 | while ((devc->buflen - offset) >= dmm->packet_size) { |
139 | if (dmm->packet_valid(devc->buf + offset)) { | |
140 | handle_packet(devc->buf + offset, sdi, info); | |
141 | offset += dmm->packet_size; | |
2baac44d MH |
142 | |
143 | /* Request next packet, if required. */ | |
bcbef5ed | 144 | if (!dmm->packet_request) |
2baac44d | 145 | break; |
bcbef5ed | 146 | if (dmm->req_timeout_ms || dmm->req_delay_ms) |
2baac44d | 147 | devc->req_next_at = g_get_monotonic_time() + |
bcbef5ed ML |
148 | dmm->req_delay_ms * 1000; |
149 | req_packet(sdi); | |
7dc55d93 AG |
150 | } else { |
151 | offset++; | |
152 | } | |
153 | } | |
154 | ||
bbabddbd UH |
155 | /* If we have any data left, move it to the beginning of our buffer. */ |
156 | for (i = 0; i < devc->buflen - offset; i++) | |
7dc55d93 AG |
157 | devc->buf[i] = devc->buf[offset + i]; |
158 | devc->buflen -= offset; | |
159 | } | |
160 | ||
bcbef5ed | 161 | int receive_data(int fd, int revents, void *cb_data) |
7dc55d93 | 162 | { |
642e9d62 | 163 | struct sr_dev_inst *sdi; |
7dc55d93 | 164 | struct dev_context *devc; |
bcbef5ed | 165 | struct dmm_info *dmm; |
bcbef5ed | 166 | void *info; |
7dc55d93 | 167 | |
d35afa87 BV |
168 | (void)fd; |
169 | ||
7dc55d93 AG |
170 | if (!(sdi = cb_data)) |
171 | return TRUE; | |
172 | ||
173 | if (!(devc = sdi->priv)) | |
174 | return TRUE; | |
175 | ||
1a863916 | 176 | dmm = (struct dmm_info *)sdi->driver; |
bcbef5ed | 177 | |
bbabddbd | 178 | if (revents == G_IO_IN) { |
7dc55d93 | 179 | /* Serial data arrived. */ |
145d794f | 180 | info = g_malloc(dmm->info_size); |
bcbef5ed | 181 | handle_new_data(sdi, info); |
145d794f | 182 | g_free(info); |
ce3777ad | 183 | } else { |
2baac44d | 184 | /* Timeout; send another packet request if DMM needs it. */ |
bcbef5ed | 185 | if (dmm->packet_request && (req_packet(sdi) < 0)) |
2baac44d | 186 | return FALSE; |
7dc55d93 AG |
187 | } |
188 | ||
9f51c463 | 189 | if (sr_sw_limits_check(&devc->limits)) |
695dc859 | 190 | sdi->driver->dev_acquisition_stop(sdi); |
f9b9bd63 | 191 | |
7dc55d93 AG |
192 | return TRUE; |
193 | } |