]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> | |
5 | * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> | |
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 <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 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 %02x %02x " | |
34 | "%02x %02x %02x %02x %02x %02x %02x", | |
35 | buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], | |
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]); | |
39 | } | |
40 | ||
41 | static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, | |
42 | void *info) | |
43 | { | |
44 | struct dmm_info *dmm; | |
45 | float floatval; | |
46 | struct sr_datafeed_packet packet; | |
47 | struct sr_datafeed_analog analog; | |
48 | struct sr_analog_encoding encoding; | |
49 | struct sr_analog_meaning meaning; | |
50 | struct sr_analog_spec spec; | |
51 | struct dev_context *devc; | |
52 | gboolean sent_sample; | |
53 | size_t ch_idx; | |
54 | ||
55 | dmm = (struct dmm_info *)sdi->driver; | |
56 | ||
57 | log_dmm_packet(buf); | |
58 | devc = sdi->priv; | |
59 | ||
60 | sent_sample = FALSE; | |
61 | memset(info, 0, dmm->info_size); | |
62 | for (ch_idx = 0; ch_idx < dmm->channel_count; ch_idx++) { | |
63 | /* Note: digits/spec_digits will be overridden by the DMM parsers. */ | |
64 | sr_analog_init(&analog, &encoding, &meaning, &spec, 0); | |
65 | ||
66 | analog.meaning->channels = | |
67 | g_slist_append(NULL, g_slist_nth_data(sdi->channels, ch_idx)); | |
68 | analog.num_samples = 1; | |
69 | analog.meaning->mq = 0; | |
70 | ||
71 | dmm->packet_parse(buf, &floatval, &analog, info); | |
72 | analog.data = &floatval; | |
73 | ||
74 | /* If this DMM needs additional handling, call the resp. function. */ | |
75 | if (dmm->dmm_details) | |
76 | dmm->dmm_details(&analog, info); | |
77 | ||
78 | if (analog.meaning->mq != 0) { | |
79 | /* Got a measurement. */ | |
80 | packet.type = SR_DF_ANALOG; | |
81 | packet.payload = &analog; | |
82 | sr_session_send(sdi, &packet); | |
83 | sent_sample = TRUE; | |
84 | } | |
85 | } | |
86 | ||
87 | if (sent_sample) { | |
88 | sr_sw_limits_update_samples_read(&devc->limits, 1); | |
89 | } | |
90 | } | |
91 | ||
92 | /** Request packet, if required. */ | |
93 | SR_PRIV int req_packet(struct sr_dev_inst *sdi) | |
94 | { | |
95 | struct dmm_info *dmm; | |
96 | struct dev_context *devc; | |
97 | struct sr_serial_dev_inst *serial; | |
98 | int ret; | |
99 | ||
100 | dmm = (struct dmm_info *)sdi->driver; | |
101 | ||
102 | if (!dmm->packet_request) | |
103 | return SR_OK; | |
104 | ||
105 | devc = sdi->priv; | |
106 | serial = sdi->conn; | |
107 | ||
108 | if (devc->req_next_at && (devc->req_next_at > g_get_monotonic_time())) { | |
109 | sr_spew("Not requesting new packet yet, %" PRIi64 " ms left.", | |
110 | ((devc->req_next_at - g_get_monotonic_time()) / 1000)); | |
111 | return SR_OK; | |
112 | } | |
113 | ||
114 | ret = dmm->packet_request(serial); | |
115 | if (ret < 0) { | |
116 | sr_err("Failed to request packet: %d.", ret); | |
117 | return ret; | |
118 | } | |
119 | ||
120 | if (dmm->req_timeout_ms) | |
121 | devc->req_next_at = g_get_monotonic_time() + (dmm->req_timeout_ms * 1000); | |
122 | ||
123 | return SR_OK; | |
124 | } | |
125 | ||
126 | static void handle_new_data(struct sr_dev_inst *sdi, void *info) | |
127 | { | |
128 | struct dmm_info *dmm; | |
129 | struct dev_context *devc; | |
130 | int len, offset; | |
131 | struct sr_serial_dev_inst *serial; | |
132 | ||
133 | dmm = (struct dmm_info *)sdi->driver; | |
134 | ||
135 | devc = sdi->priv; | |
136 | serial = sdi->conn; | |
137 | ||
138 | /* Try to get as much data as the buffer can hold. */ | |
139 | len = DMM_BUFSIZE - devc->buflen; | |
140 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len); | |
141 | if (len == 0) | |
142 | return; /* No new bytes, nothing to do. */ | |
143 | if (len < 0) { | |
144 | sr_err("Serial port read error: %d.", len); | |
145 | return; | |
146 | } | |
147 | devc->buflen += len; | |
148 | ||
149 | /* Now look for packets in that data. */ | |
150 | offset = 0; | |
151 | while ((devc->buflen - offset) >= dmm->packet_size) { | |
152 | if (dmm->packet_valid(devc->buf + offset)) { | |
153 | handle_packet(devc->buf + offset, sdi, info); | |
154 | offset += dmm->packet_size; | |
155 | ||
156 | /* Request next packet, if required. */ | |
157 | if (!dmm->packet_request) | |
158 | break; | |
159 | if (dmm->req_timeout_ms || dmm->req_delay_ms) | |
160 | devc->req_next_at = g_get_monotonic_time() + | |
161 | dmm->req_delay_ms * 1000; | |
162 | req_packet(sdi); | |
163 | } else { | |
164 | offset++; | |
165 | } | |
166 | } | |
167 | ||
168 | /* If we have any data left, move it to the beginning of our buffer. */ | |
169 | if (devc->buflen > offset) | |
170 | memmove(devc->buf, devc->buf + offset, devc->buflen - offset); | |
171 | devc->buflen -= offset; | |
172 | } | |
173 | ||
174 | int receive_data(int fd, int revents, void *cb_data) | |
175 | { | |
176 | struct sr_dev_inst *sdi; | |
177 | struct dev_context *devc; | |
178 | struct dmm_info *dmm; | |
179 | void *info; | |
180 | ||
181 | (void)fd; | |
182 | ||
183 | if (!(sdi = cb_data)) | |
184 | return TRUE; | |
185 | ||
186 | if (!(devc = sdi->priv)) | |
187 | return TRUE; | |
188 | ||
189 | dmm = (struct dmm_info *)sdi->driver; | |
190 | ||
191 | if (revents == G_IO_IN) { | |
192 | /* Serial data arrived. */ | |
193 | info = g_malloc(dmm->info_size); | |
194 | handle_new_data(sdi, info); | |
195 | g_free(info); | |
196 | } else { | |
197 | /* Timeout; send another packet request if DMM needs it. */ | |
198 | if (dmm->packet_request && (req_packet(sdi) < 0)) | |
199 | return FALSE; | |
200 | } | |
201 | ||
202 | if (sr_sw_limits_check(&devc->limits)) | |
203 | sr_dev_acquisition_stop(sdi); | |
204 | ||
205 | return TRUE; | |
206 | } |