]>
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 <stdlib.h> | |
22 | #include <math.h> | |
23 | #include <string.h> | |
24 | #include <errno.h> | |
25 | #include <glib.h> | |
26 | #include "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", | |
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 | ||
38 | static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, | |
39 | int dmm, void *info) | |
40 | { | |
41 | float floatval; | |
42 | struct sr_datafeed_packet packet; | |
43 | struct sr_datafeed_analog analog; | |
44 | struct dev_context *devc; | |
45 | ||
46 | log_dmm_packet(buf); | |
47 | devc = sdi->priv; | |
48 | ||
49 | memset(&analog, 0, sizeof(struct sr_datafeed_analog)); | |
50 | ||
51 | analog.channels = sdi->channels; | |
52 | analog.num_samples = 1; | |
53 | analog.mq = -1; | |
54 | ||
55 | dmms[dmm].packet_parse(buf, &floatval, &analog, info); | |
56 | analog.data = &floatval; | |
57 | ||
58 | /* If this DMM needs additional handling, call the resp. function. */ | |
59 | if (dmms[dmm].dmm_details) | |
60 | dmms[dmm].dmm_details(&analog, info); | |
61 | ||
62 | if (analog.mq != -1) { | |
63 | /* Got a measurement. */ | |
64 | packet.type = SR_DF_ANALOG; | |
65 | packet.payload = &analog; | |
66 | sr_session_send(devc->cb_data, &packet); | |
67 | devc->num_samples++; | |
68 | } | |
69 | } | |
70 | ||
71 | /** Request packet, if required. */ | |
72 | SR_PRIV int req_packet(struct sr_dev_inst *sdi, int dmm) | |
73 | { | |
74 | struct dev_context *devc; | |
75 | struct sr_serial_dev_inst *serial; | |
76 | int ret; | |
77 | ||
78 | if (!dmms[dmm].packet_request) | |
79 | return SR_OK; | |
80 | ||
81 | devc = sdi->priv; | |
82 | serial = sdi->conn; | |
83 | ||
84 | if (devc->req_next_at && (devc->req_next_at > g_get_monotonic_time())) { | |
85 | sr_spew("Not requesting new packet yet, %" PRIi64 " ms left.", | |
86 | ((devc->req_next_at - g_get_monotonic_time()) / 1000)); | |
87 | return SR_OK; | |
88 | } | |
89 | ||
90 | ret = dmms[dmm].packet_request(serial); | |
91 | if (ret < 0) { | |
92 | sr_err("Failed to request packet: %d.", ret); | |
93 | return ret; | |
94 | } | |
95 | ||
96 | if (dmms[dmm].req_timeout_ms) | |
97 | devc->req_next_at = g_get_monotonic_time() + (dmms[dmm].req_timeout_ms * 1000); | |
98 | ||
99 | return SR_OK; | |
100 | } | |
101 | ||
102 | static void handle_new_data(struct sr_dev_inst *sdi, int dmm, void *info) | |
103 | { | |
104 | struct dev_context *devc; | |
105 | int len, i, offset = 0; | |
106 | struct sr_serial_dev_inst *serial; | |
107 | ||
108 | devc = sdi->priv; | |
109 | serial = sdi->conn; | |
110 | ||
111 | /* Try to get as much data as the buffer can hold. */ | |
112 | len = DMM_BUFSIZE - devc->buflen; | |
113 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len); | |
114 | if (len == 0) | |
115 | return; /* No new bytes, nothing to do. */ | |
116 | if (len < 0) { | |
117 | sr_err("Serial port read error: %d.", len); | |
118 | return; | |
119 | } | |
120 | devc->buflen += len; | |
121 | ||
122 | /* Now look for packets in that data. */ | |
123 | while ((devc->buflen - offset) >= dmms[dmm].packet_size) { | |
124 | if (dmms[dmm].packet_valid(devc->buf + offset)) { | |
125 | handle_packet(devc->buf + offset, sdi, dmm, info); | |
126 | offset += dmms[dmm].packet_size; | |
127 | ||
128 | /* Request next packet, if required. */ | |
129 | if (!dmms[dmm].packet_request) | |
130 | break; | |
131 | if (dmms[dmm].req_timeout_ms || dmms[dmm].req_delay_ms) | |
132 | devc->req_next_at = g_get_monotonic_time() + | |
133 | dmms[dmm].req_delay_ms * 1000; | |
134 | req_packet(sdi, dmm); | |
135 | } else { | |
136 | offset++; | |
137 | } | |
138 | } | |
139 | ||
140 | /* If we have any data left, move it to the beginning of our buffer. */ | |
141 | for (i = 0; i < devc->buflen - offset; i++) | |
142 | devc->buf[i] = devc->buf[offset + i]; | |
143 | devc->buflen -= offset; | |
144 | } | |
145 | ||
146 | static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data) | |
147 | { | |
148 | struct sr_dev_inst *sdi; | |
149 | struct dev_context *devc; | |
150 | int64_t time; | |
151 | ||
152 | (void)fd; | |
153 | ||
154 | if (!(sdi = cb_data)) | |
155 | return TRUE; | |
156 | ||
157 | if (!(devc = sdi->priv)) | |
158 | return TRUE; | |
159 | ||
160 | if (revents == G_IO_IN) { | |
161 | /* Serial data arrived. */ | |
162 | handle_new_data(sdi, dmm, info); | |
163 | } else { | |
164 | /* Timeout; send another packet request if DMM needs it. */ | |
165 | if (dmms[dmm].packet_request && (req_packet(sdi, dmm) < 0)) | |
166 | return FALSE; | |
167 | } | |
168 | ||
169 | if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { | |
170 | sr_info("Requested number of samples reached."); | |
171 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
172 | return TRUE; | |
173 | } | |
174 | ||
175 | if (devc->limit_msec) { | |
176 | time = (g_get_monotonic_time() - devc->starttime) / 1000; | |
177 | if (time > (int64_t)devc->limit_msec) { | |
178 | sr_info("Requested time limit reached."); | |
179 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
180 | return TRUE; | |
181 | } | |
182 | } | |
183 | ||
184 | return TRUE; | |
185 | } | |
186 | ||
187 | #define RECEIVE_DATA(ID_UPPER, DMM_DRIVER) \ | |
188 | SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \ | |
189 | struct DMM_DRIVER##_info info; \ | |
190 | return receive_data(fd, revents, ID_UPPER, &info, cb_data); } | |
191 | ||
192 | /* Driver-specific receive_data() wrappers */ | |
193 | RECEIVE_DATA(BBCGM_M2110, metex14) /* metex14_info used as a dummy. */ | |
194 | RECEIVE_DATA(DIGITEK_DT4000ZC, fs9721) | |
195 | RECEIVE_DATA(TEKPOWER_TP4000ZC, fs9721) | |
196 | RECEIVE_DATA(METEX_ME31, metex14) | |
197 | RECEIVE_DATA(PEAKTECH_3410, metex14) | |
198 | RECEIVE_DATA(MASTECH_MAS345, metex14) | |
199 | RECEIVE_DATA(VA_VA18B, fs9721) | |
200 | RECEIVE_DATA(VA_VA40B, fs9721) | |
201 | RECEIVE_DATA(METEX_M3640D, metex14) | |
202 | RECEIVE_DATA(METEX_M4650CR, metex14) | |
203 | RECEIVE_DATA(PEAKTECH_4370, metex14) | |
204 | RECEIVE_DATA(PCE_PCE_DM32, fs9721) | |
205 | RECEIVE_DATA(RADIOSHACK_22_168, metex14) | |
206 | RECEIVE_DATA(RADIOSHACK_22_805, metex14) | |
207 | RECEIVE_DATA(RADIOSHACK_22_812, rs9lcd) | |
208 | RECEIVE_DATA(TECPEL_DMM_8061_SER, fs9721) | |
209 | RECEIVE_DATA(VOLTCRAFT_M3650CR, metex14) | |
210 | RECEIVE_DATA(VOLTCRAFT_M3650D, metex14) | |
211 | RECEIVE_DATA(VOLTCRAFT_M4650CR, metex14) | |
212 | RECEIVE_DATA(VOLTCRAFT_ME42, metex14) | |
213 | RECEIVE_DATA(VOLTCRAFT_VC820_SER, fs9721) | |
214 | RECEIVE_DATA(VOLTCRAFT_VC830_SER, fs9922) | |
215 | RECEIVE_DATA(VOLTCRAFT_VC840_SER, fs9721) | |
216 | RECEIVE_DATA(VOLTCRAFT_VC920_SER, ut71x) | |
217 | RECEIVE_DATA(VOLTCRAFT_VC940_SER, ut71x) | |
218 | RECEIVE_DATA(VOLTCRAFT_VC960_SER, ut71x) | |
219 | RECEIVE_DATA(UNI_T_UT60A_SER, fs9721) | |
220 | RECEIVE_DATA(UNI_T_UT60E_SER, fs9721) | |
221 | RECEIVE_DATA(UNI_T_UT60G_SER, es519xx) | |
222 | RECEIVE_DATA(UNI_T_UT61B_SER, fs9922) | |
223 | RECEIVE_DATA(UNI_T_UT61C_SER, fs9922) | |
224 | RECEIVE_DATA(UNI_T_UT61D_SER, fs9922) | |
225 | RECEIVE_DATA(UNI_T_UT61E_SER, es519xx) | |
226 | RECEIVE_DATA(UNI_T_UT71A_SER, ut71x) | |
227 | RECEIVE_DATA(UNI_T_UT71B_SER, ut71x) | |
228 | RECEIVE_DATA(UNI_T_UT71C_SER, ut71x) | |
229 | RECEIVE_DATA(UNI_T_UT71D_SER, ut71x) | |
230 | RECEIVE_DATA(UNI_T_UT71E_SER, ut71x) | |
231 | RECEIVE_DATA(ISO_TECH_IDM103N, es519xx) | |
232 | RECEIVE_DATA(TENMA_72_7745_SER, fs9721) | |
233 | RECEIVE_DATA(TENMA_72_7750_SER, es519xx) | |
234 | RECEIVE_DATA(BRYMEN_BM25X, bm25x) |