]>
Commit | Line | Data |
---|---|---|
bfd48770 MH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include "protocol.h" | |
21 | ||
f8e76e2e MH |
22 | static const int32_t hwopts[] = { |
23 | SR_CONF_CONN, | |
24 | SR_CONF_SERIALCOMM, | |
25 | }; | |
26 | ||
27 | static const int32_t hwcaps[] = { | |
28 | SR_CONF_MULTIMETER, | |
29 | SR_CONF_LIMIT_SAMPLES, | |
30 | SR_CONF_LIMIT_MSEC, | |
31 | SR_CONF_CONTINUOUS, | |
32 | }; | |
33 | ||
e790bd5c | 34 | #define BUF_MAX 50 |
f8e76e2e MH |
35 | |
36 | #define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1" | |
37 | ||
bfd48770 MH |
38 | SR_PRIV struct sr_dev_driver norma_dmm_driver_info; |
39 | static struct sr_dev_driver *di = &norma_dmm_driver_info; | |
40 | ||
41 | static int init(struct sr_context *sr_ctx) | |
42 | { | |
43 | return std_init(sr_ctx, di, LOG_PREFIX); | |
44 | } | |
45 | ||
46 | static GSList *scan(GSList *options) | |
47 | { | |
f8e76e2e | 48 | struct sr_dev_inst *sdi; |
bfd48770 | 49 | struct drv_context *drvc; |
f8e76e2e MH |
50 | struct dev_context *devc; |
51 | struct sr_config *src; | |
ba7dd8bb | 52 | struct sr_channel *ch; |
f8e76e2e MH |
53 | struct sr_serial_dev_inst *serial; |
54 | GSList *l, *devices; | |
55 | int len, cnt; | |
56 | const char *conn, *serialcomm; | |
57 | char *buf; | |
58 | char fmttype[10]; | |
59 | char req[10]; | |
60 | int auxtype; | |
61 | ||
bfd48770 MH |
62 | devices = NULL; |
63 | drvc = di->priv; | |
64 | drvc->instances = NULL; | |
f8e76e2e MH |
65 | conn = serialcomm = NULL; |
66 | ||
67 | for (l = options; l; l = l->next) { | |
68 | src = l->data; | |
69 | switch (src->key) { | |
70 | case SR_CONF_CONN: | |
71 | conn = g_variant_get_string(src->data, NULL); | |
72 | break; | |
73 | case SR_CONF_SERIALCOMM: | |
74 | serialcomm = g_variant_get_string(src->data, NULL); | |
75 | break; | |
76 | } | |
77 | } | |
78 | if (!conn) | |
79 | return NULL; | |
80 | if (!serialcomm) | |
81 | serialcomm = SERIALCOMM; | |
82 | ||
83 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) | |
84 | return NULL; | |
85 | ||
86 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) | |
87 | return NULL; | |
88 | ||
89 | serial_flush(serial); | |
bfd48770 | 90 | |
e790bd5c | 91 | if (!(buf = g_try_malloc(BUF_MAX))) { |
f8e76e2e MH |
92 | sr_err("Serial buffer malloc failed."); |
93 | return NULL; | |
94 | } | |
95 | ||
96 | snprintf(req, sizeof(req), "%s\r\n", | |
e790bd5c | 97 | nmadmm_requests[NMADMM_REQ_IDN].req_str); |
f8e76e2e MH |
98 | for (cnt = 0; cnt < 7; cnt++) { |
99 | if (serial_write(serial, req, strlen(req)) == -1) { | |
100 | sr_err("Unable to send identification request: %d %s.", | |
e790bd5c | 101 | errno, strerror(errno)); |
f8e76e2e MH |
102 | return NULL; |
103 | } | |
104 | len = BUF_MAX; | |
105 | serial_readline(serial, &buf, &len, 1500); | |
106 | if (!len) | |
107 | continue; | |
e790bd5c | 108 | buf[BUF_MAX - 1] = '\0'; |
f8e76e2e | 109 | |
e790bd5c UH |
110 | /* Match ID string, e.g. "1834 065 V1.06,IF V1.02" (DM950) */ |
111 | if (g_regex_match_simple("^1834 [^,]*,IF V*", (char *)buf, 0, 0)) { | |
f8e76e2e MH |
112 | auxtype = xgittoint(buf[7]); |
113 | // TODO: Will this work with non-DM950? | |
114 | snprintf(fmttype, sizeof(fmttype), "DM9%d0", auxtype); | |
115 | sr_spew("Norma %s DMM %s detected!", fmttype, &buf[9]); | |
116 | ||
117 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, | |
118 | "Norma", fmttype, buf + 9))) | |
119 | return NULL; | |
e790bd5c | 120 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { |
f8e76e2e MH |
121 | sr_err("Device context malloc failed."); |
122 | return NULL; | |
123 | } | |
124 | devc->type = auxtype; | |
125 | devc->version = g_strdup(&buf[9]); | |
126 | devc->elapsed_msec = g_timer_new(); | |
127 | ||
128 | sdi->conn = serial; | |
129 | sdi->priv = devc; | |
130 | sdi->driver = di; | |
3f239f08 | 131 | if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, |
f8e76e2e MH |
132 | "P1"))) |
133 | return NULL; | |
ba7dd8bb | 134 | sdi->channels = g_slist_append(sdi->channels, ch); |
f8e76e2e MH |
135 | drvc->instances = g_slist_append(drvc->instances, sdi); |
136 | devices = g_slist_append(devices, sdi); | |
137 | break; | |
138 | } | |
e790bd5c UH |
139 | |
140 | /* | |
141 | * The interface of the DM9x0 contains a cap that needs to | |
142 | * charge for up to 10s before the interface works, if not | |
143 | * powered externally. Therefore wait a little to improve | |
144 | * chances. | |
145 | */ | |
f8e76e2e MH |
146 | if (cnt == 3) { |
147 | sr_info("Waiting 5s to allow interface to settle."); | |
148 | g_usleep(5 * 1000 * 1000); | |
149 | } | |
150 | } | |
151 | ||
152 | g_free(buf); | |
153 | ||
154 | serial_close(serial); | |
155 | if (!devices) | |
156 | sr_serial_dev_inst_free(serial); | |
bfd48770 MH |
157 | |
158 | return devices; | |
159 | } | |
160 | ||
161 | static GSList *dev_list(void) | |
162 | { | |
163 | return ((struct drv_context *)(di->priv))->instances; | |
164 | } | |
165 | ||
bfd48770 MH |
166 | static int dev_close(struct sr_dev_inst *sdi) |
167 | { | |
f8e76e2e | 168 | struct dev_context *devc; |
bfd48770 | 169 | |
bf2c987f | 170 | std_serial_dev_close(sdi); |
bfd48770 | 171 | |
e790bd5c | 172 | /* Free dynamically allocated resources. */ |
f8e76e2e MH |
173 | if ((devc = sdi->priv) && devc->version) { |
174 | g_free(devc->version); | |
175 | devc->version = NULL; | |
176 | g_timer_destroy(devc->elapsed_msec); | |
177 | } | |
bfd48770 MH |
178 | |
179 | return SR_OK; | |
180 | } | |
181 | ||
182 | static int cleanup(void) | |
183 | { | |
a6630742 | 184 | return std_dev_clear(di, NULL); |
bfd48770 MH |
185 | } |
186 | ||
d3c74a6f | 187 | static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 188 | const struct sr_channel_group *cg) |
bfd48770 | 189 | { |
f8e76e2e | 190 | struct dev_context *devc; |
bfd48770 | 191 | |
53b4680f | 192 | (void)cg; |
d3c74a6f | 193 | |
bfd48770 MH |
194 | if (sdi->status != SR_ST_ACTIVE) |
195 | return SR_ERR_DEV_CLOSED; | |
196 | ||
f8e76e2e MH |
197 | if (!(devc = sdi->priv)) { |
198 | sr_err("sdi->priv was NULL."); | |
199 | return SR_ERR_BUG; | |
200 | } | |
201 | ||
bfd48770 | 202 | switch (key) { |
f8e76e2e MH |
203 | case SR_CONF_LIMIT_MSEC: |
204 | /* TODO: not yet implemented */ | |
205 | if (g_variant_get_uint64(data) == 0) { | |
206 | sr_err("LIMIT_MSEC can't be 0."); | |
207 | return SR_ERR; | |
208 | } | |
209 | devc->limit_msec = g_variant_get_uint64(data); | |
210 | sr_dbg("Setting time limit to %" PRIu64 "ms.", | |
211 | devc->limit_msec); | |
212 | break; | |
213 | case SR_CONF_LIMIT_SAMPLES: | |
214 | devc->limit_samples = g_variant_get_uint64(data); | |
215 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
216 | devc->limit_samples); | |
217 | break; | |
bfd48770 | 218 | default: |
f8e76e2e | 219 | return SR_ERR_NA; |
bfd48770 MH |
220 | } |
221 | ||
f8e76e2e | 222 | return SR_OK; |
bfd48770 MH |
223 | } |
224 | ||
d3c74a6f | 225 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 226 | const struct sr_channel_group *cg) |
bfd48770 | 227 | { |
bfd48770 | 228 | (void)sdi; |
53b4680f | 229 | (void)cg; |
bfd48770 | 230 | |
bfd48770 | 231 | switch (key) { |
f8e76e2e MH |
232 | case SR_CONF_SCAN_OPTIONS: |
233 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
234 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
235 | break; | |
236 | case SR_CONF_DEVICE_OPTIONS: | |
237 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
238 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
239 | break; | |
bfd48770 MH |
240 | default: |
241 | return SR_ERR_NA; | |
242 | } | |
243 | ||
f8e76e2e | 244 | return SR_OK; |
bfd48770 MH |
245 | } |
246 | ||
247 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, | |
248 | void *cb_data) | |
249 | { | |
f8e76e2e MH |
250 | struct dev_context *devc; |
251 | struct sr_serial_dev_inst *serial; | |
252 | ||
f8e76e2e MH |
253 | if (!sdi || !cb_data || !(devc = sdi->priv)) |
254 | return SR_ERR_BUG; | |
255 | ||
bfd48770 MH |
256 | if (sdi->status != SR_ST_ACTIVE) |
257 | return SR_ERR_DEV_CLOSED; | |
258 | ||
f8e76e2e MH |
259 | devc->cb_data = cb_data; |
260 | ||
261 | /* Send header packet to the session bus. */ | |
262 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
263 | ||
264 | /* Start timer, if required. */ | |
265 | if (devc->limit_msec) | |
266 | g_timer_start(devc->elapsed_msec); | |
267 | ||
268 | /* Poll every 100ms, or whenever some data comes in. */ | |
269 | serial = sdi->conn; | |
abc4b335 | 270 | serial_source_add(serial, G_IO_IN, 100, norma_dmm_receive_data, |
f8e76e2e | 271 | (void *)sdi); |
bfd48770 MH |
272 | |
273 | return SR_OK; | |
274 | } | |
275 | ||
276 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
277 | { | |
f8e76e2e | 278 | struct dev_context *devc; |
bfd48770 | 279 | |
f8e76e2e MH |
280 | /* Stop timer, if required. */ |
281 | if (sdi && (devc = sdi->priv) && devc->limit_msec) | |
282 | g_timer_stop(devc->elapsed_msec); | |
bfd48770 | 283 | |
d43b0908 | 284 | return std_serial_dev_acquisition_stop(sdi, cb_data, dev_close, |
f8e76e2e | 285 | sdi->conn, LOG_PREFIX); |
bfd48770 MH |
286 | } |
287 | ||
288 | SR_PRIV struct sr_dev_driver norma_dmm_driver_info = { | |
289 | .name = "norma-dmm", | |
e790bd5c | 290 | .longname = "Norma DM9x0 / Siemens B102x DMMs", |
bfd48770 MH |
291 | .api_version = 1, |
292 | .init = init, | |
293 | .cleanup = cleanup, | |
294 | .scan = scan, | |
295 | .dev_list = dev_list, | |
a6630742 | 296 | .dev_clear = NULL, |
f8e76e2e | 297 | .config_get = NULL, |
bfd48770 MH |
298 | .config_set = config_set, |
299 | .config_list = config_list, | |
854434de | 300 | .dev_open = std_serial_dev_open, |
bfd48770 MH |
301 | .dev_close = dev_close, |
302 | .dev_acquisition_start = dev_acquisition_start, | |
303 | .dev_acquisition_stop = dev_acquisition_stop, | |
304 | .priv = NULL, | |
305 | }; |