]>
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; |
556a926d | 52 | gboolean sent_sample; |
e91c9f6e | 53 | struct sr_channel *channel; |
556a926d | 54 | size_t ch_idx; |
7dc55d93 | 55 | |
1a863916 UH |
56 | dmm = (struct dmm_info *)sdi->driver; |
57 | ||
6bef68a7 | 58 | log_dmm_packet(buf); |
69e19dd7 | 59 | devc = sdi->priv; |
6bef68a7 | 60 | |
556a926d GS |
61 | sent_sample = FALSE; |
62 | memset(info, 0, dmm->info_size); | |
63 | for (ch_idx = 0; ch_idx < dmm->channel_count; ch_idx++) { | |
64 | /* Note: digits/spec_digits will be overridden by the DMM parsers. */ | |
65 | sr_analog_init(&analog, &encoding, &meaning, &spec, 0); | |
66 | ||
e91c9f6e GS |
67 | channel = g_slist_nth_data(sdi->channels, ch_idx); |
68 | analog.meaning->channels = g_slist_append(NULL, channel); | |
556a926d GS |
69 | analog.num_samples = 1; |
70 | analog.meaning->mq = 0; | |
71 | ||
72 | dmm->packet_parse(buf, &floatval, &analog, info); | |
73 | analog.data = &floatval; | |
74 | ||
75 | /* If this DMM needs additional handling, call the resp. function. */ | |
76 | if (dmm->dmm_details) | |
77 | dmm->dmm_details(&analog, info); | |
78 | ||
e91c9f6e | 79 | if (analog.meaning->mq != 0 && channel->enabled) { |
556a926d GS |
80 | /* Got a measurement. */ |
81 | packet.type = SR_DF_ANALOG; | |
82 | packet.payload = &analog; | |
83 | sr_session_send(sdi, &packet); | |
84 | sent_sample = TRUE; | |
85 | } | |
86 | } | |
7dc55d93 | 87 | |
556a926d | 88 | if (sent_sample) { |
9f51c463 | 89 | sr_sw_limits_update_samples_read(&devc->limits, 1); |
7dc55d93 | 90 | } |
7dc55d93 AG |
91 | } |
92 | ||
2baac44d | 93 | /** Request packet, if required. */ |
bcbef5ed | 94 | SR_PRIV int req_packet(struct sr_dev_inst *sdi) |
2baac44d | 95 | { |
1a863916 | 96 | struct dmm_info *dmm; |
2baac44d MH |
97 | struct dev_context *devc; |
98 | struct sr_serial_dev_inst *serial; | |
99 | int ret; | |
100 | ||
1a863916 UH |
101 | dmm = (struct dmm_info *)sdi->driver; |
102 | ||
bcbef5ed | 103 | if (!dmm->packet_request) |
2baac44d MH |
104 | return SR_OK; |
105 | ||
106 | devc = sdi->priv; | |
107 | serial = sdi->conn; | |
108 | ||
109 | if (devc->req_next_at && (devc->req_next_at > g_get_monotonic_time())) { | |
110 | sr_spew("Not requesting new packet yet, %" PRIi64 " ms left.", | |
111 | ((devc->req_next_at - g_get_monotonic_time()) / 1000)); | |
112 | return SR_OK; | |
113 | } | |
114 | ||
bcbef5ed | 115 | ret = dmm->packet_request(serial); |
2baac44d MH |
116 | if (ret < 0) { |
117 | sr_err("Failed to request packet: %d.", ret); | |
118 | return ret; | |
119 | } | |
120 | ||
bcbef5ed ML |
121 | if (dmm->req_timeout_ms) |
122 | devc->req_next_at = g_get_monotonic_time() + (dmm->req_timeout_ms * 1000); | |
2baac44d MH |
123 | |
124 | return SR_OK; | |
125 | } | |
126 | ||
bcbef5ed | 127 | static void handle_new_data(struct sr_dev_inst *sdi, void *info) |
7dc55d93 | 128 | { |
1a863916 | 129 | struct dmm_info *dmm; |
69e19dd7 | 130 | struct dev_context *devc; |
51e1f566 | 131 | int len, offset; |
1e1bfcd0 | 132 | struct sr_serial_dev_inst *serial; |
bbabddbd | 133 | |
1a863916 UH |
134 | dmm = (struct dmm_info *)sdi->driver; |
135 | ||
69e19dd7 | 136 | devc = sdi->priv; |
1e1bfcd0 | 137 | serial = sdi->conn; |
69e19dd7 | 138 | |
bbabddbd | 139 | /* Try to get as much data as the buffer can hold. */ |
7dc55d93 | 140 | len = DMM_BUFSIZE - devc->buflen; |
4277ac34 | 141 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len); |
66a43576 UH |
142 | if (len == 0) |
143 | return; /* No new bytes, nothing to do. */ | |
144 | if (len < 0) { | |
bbabddbd | 145 | sr_err("Serial port read error: %d.", len); |
7dc55d93 AG |
146 | return; |
147 | } | |
148 | devc->buflen += len; | |
149 | ||
bbabddbd | 150 | /* Now look for packets in that data. */ |
51e1f566 | 151 | offset = 0; |
bcbef5ed ML |
152 | while ((devc->buflen - offset) >= dmm->packet_size) { |
153 | if (dmm->packet_valid(devc->buf + offset)) { | |
154 | handle_packet(devc->buf + offset, sdi, info); | |
155 | offset += dmm->packet_size; | |
2baac44d MH |
156 | |
157 | /* Request next packet, if required. */ | |
bcbef5ed | 158 | if (!dmm->packet_request) |
2baac44d | 159 | break; |
bcbef5ed | 160 | if (dmm->req_timeout_ms || dmm->req_delay_ms) |
2baac44d | 161 | devc->req_next_at = g_get_monotonic_time() + |
bcbef5ed ML |
162 | dmm->req_delay_ms * 1000; |
163 | req_packet(sdi); | |
7dc55d93 AG |
164 | } else { |
165 | offset++; | |
166 | } | |
167 | } | |
168 | ||
bbabddbd | 169 | /* If we have any data left, move it to the beginning of our buffer. */ |
51e1f566 GS |
170 | if (devc->buflen > offset) |
171 | memmove(devc->buf, devc->buf + offset, devc->buflen - offset); | |
7dc55d93 AG |
172 | devc->buflen -= offset; |
173 | } | |
174 | ||
bcbef5ed | 175 | int receive_data(int fd, int revents, void *cb_data) |
7dc55d93 | 176 | { |
642e9d62 | 177 | struct sr_dev_inst *sdi; |
7dc55d93 | 178 | struct dev_context *devc; |
bcbef5ed | 179 | struct dmm_info *dmm; |
bcbef5ed | 180 | void *info; |
7dc55d93 | 181 | |
d35afa87 BV |
182 | (void)fd; |
183 | ||
7dc55d93 AG |
184 | if (!(sdi = cb_data)) |
185 | return TRUE; | |
186 | ||
187 | if (!(devc = sdi->priv)) | |
188 | return TRUE; | |
189 | ||
1a863916 | 190 | dmm = (struct dmm_info *)sdi->driver; |
bcbef5ed | 191 | |
bbabddbd | 192 | if (revents == G_IO_IN) { |
7dc55d93 | 193 | /* Serial data arrived. */ |
145d794f | 194 | info = g_malloc(dmm->info_size); |
bcbef5ed | 195 | handle_new_data(sdi, info); |
145d794f | 196 | g_free(info); |
ce3777ad | 197 | } else { |
2baac44d | 198 | /* Timeout; send another packet request if DMM needs it. */ |
bcbef5ed | 199 | if (dmm->packet_request && (req_packet(sdi) < 0)) |
2baac44d | 200 | return FALSE; |
7dc55d93 AG |
201 | } |
202 | ||
9f51c463 | 203 | if (sr_sw_limits_check(&devc->limits)) |
d2f7c417 | 204 | sr_dev_acquisition_stop(sdi); |
f9b9bd63 | 205 | |
7dc55d93 AG |
206 | return TRUE; |
207 | } |