]> sigrok.org Git - libsigrok.git/blame - src/hardware/norma-dmm/api.c
Change sr_dev_inst_new() to take no parameters.
[libsigrok.git] / src / hardware / norma-dmm / api.c
CommitLineData
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
a4e435eb
MH
20/** @file
21 * Norma DM9x0/Siemens B102x DMMs driver.
22 * @internal
23 */
24
bfd48770
MH
25#include "protocol.h"
26
a0e0bb41 27static const uint32_t scanopts[] = {
f8e76e2e
MH
28 SR_CONF_CONN,
29 SR_CONF_SERIALCOMM,
30};
31
f254bc4b 32static const uint32_t devopts[] = {
f8e76e2e 33 SR_CONF_MULTIMETER,
f8e76e2e 34 SR_CONF_CONTINUOUS,
5827f61b
BV
35 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
36 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
f8e76e2e
MH
37};
38
e790bd5c 39#define BUF_MAX 50
f8e76e2e
MH
40
41#define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1"
42
bfd48770 43SR_PRIV struct sr_dev_driver norma_dmm_driver_info;
49c06128 44SR_PRIV struct sr_dev_driver siemens_b102x_driver_info;
bfd48770 45
49c06128 46static const char* get_brandstr(struct sr_dev_driver* drv)
bfd48770 47{
49c06128
MH
48 if (drv == &norma_dmm_driver_info)
49 return "Norma";
50 else
51 return "Siemens";
bfd48770
MH
52}
53
49c06128
MH
54static const char* get_typestr(int type, struct sr_dev_driver* drv)
55{
56 static const char* nameref[5][2] = {
57 {"DM910", "B1024"},
58 {"DM920", "B1025"},
59 {"DM930", "B1026"},
60 {"DM940", "B1027"},
61 {"DM950", "B1028"}};
62
63 if ((type < 1) || (type > 5))
64 return "Unknown type!";
65
66 return nameref[type-1][(drv == &siemens_b102x_driver_info)];
67}
68
69static int init_norma_dmm(struct sr_context *sr_ctx)
70{
71 return std_init(sr_ctx, &norma_dmm_driver_info, LOG_PREFIX);
72}
73
74static int init_siemens_b102x(struct sr_context *sr_ctx)
75{
76 return std_init(sr_ctx, &siemens_b102x_driver_info, LOG_PREFIX);
77}
78
79static GSList *do_scan(struct sr_dev_driver* drv, GSList *options)
bfd48770 80{
f8e76e2e 81 struct sr_dev_inst *sdi;
bfd48770 82 struct drv_context *drvc;
f8e76e2e
MH
83 struct dev_context *devc;
84 struct sr_config *src;
ba7dd8bb 85 struct sr_channel *ch;
f8e76e2e
MH
86 struct sr_serial_dev_inst *serial;
87 GSList *l, *devices;
88 int len, cnt;
89 const char *conn, *serialcomm;
90 char *buf;
f8e76e2e
MH
91 char req[10];
92 int auxtype;
93
bfd48770 94 devices = NULL;
49c06128 95 drvc = drv->priv;
bfd48770 96 drvc->instances = NULL;
f8e76e2e
MH
97 conn = serialcomm = NULL;
98
99 for (l = options; l; l = l->next) {
100 src = l->data;
101 switch (src->key) {
102 case SR_CONF_CONN:
103 conn = g_variant_get_string(src->data, NULL);
104 break;
105 case SR_CONF_SERIALCOMM:
106 serialcomm = g_variant_get_string(src->data, NULL);
107 break;
108 }
109 }
110 if (!conn)
111 return NULL;
112 if (!serialcomm)
113 serialcomm = SERIALCOMM;
114
115 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
116 return NULL;
117
d12ef776 118 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
f8e76e2e
MH
119 return NULL;
120
121 serial_flush(serial);
bfd48770 122
e790bd5c 123 if (!(buf = g_try_malloc(BUF_MAX))) {
f8e76e2e
MH
124 sr_err("Serial buffer malloc failed.");
125 return NULL;
126 }
127
128 snprintf(req, sizeof(req), "%s\r\n",
e790bd5c 129 nmadmm_requests[NMADMM_REQ_IDN].req_str);
a4e435eb 130 g_usleep(150 * 1000); /* Wait a little to allow serial port to settle. */
f8e76e2e 131 for (cnt = 0; cnt < 7; cnt++) {
eead2782 132 if (serial_write_blocking(serial, req, strlen(req), 0) < 0) {
8849f45a 133 sr_err("Unable to send identification request.");
f8e76e2e
MH
134 return NULL;
135 }
136 len = BUF_MAX;
a4e435eb 137 serial_readline(serial, &buf, &len, NMADMM_TIMEOUT_MS);
f8e76e2e
MH
138 if (!len)
139 continue;
e790bd5c 140 buf[BUF_MAX - 1] = '\0';
f8e76e2e 141
e790bd5c
UH
142 /* Match ID string, e.g. "1834 065 V1.06,IF V1.02" (DM950) */
143 if (g_regex_match_simple("^1834 [^,]*,IF V*", (char *)buf, 0, 0)) {
f8e76e2e 144 auxtype = xgittoint(buf[7]);
49c06128 145 sr_spew("%s %s DMM %s detected!", get_brandstr(drv), get_typestr(auxtype, drv), buf + 9);
f8e76e2e 146
0af636be
UH
147 sdi = sr_dev_inst_new();
148 sdi->status = SR_ST_INACTIVE;
149 sdi->vendor = g_strdup(get_brandstr(drv));
150 sdi->model = g_strdup(get_typestr(auxtype, drv));
151 sdi->version = g_strdup(buf + 9);
e790bd5c 152 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
f8e76e2e
MH
153 sr_err("Device context malloc failed.");
154 return NULL;
155 }
156 devc->type = auxtype;
157 devc->version = g_strdup(&buf[9]);
158 devc->elapsed_msec = g_timer_new();
159
160 sdi->conn = serial;
161 sdi->priv = devc;
49c06128 162 sdi->driver = drv;
3f239f08 163 if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE,
f8e76e2e
MH
164 "P1")))
165 return NULL;
ba7dd8bb 166 sdi->channels = g_slist_append(sdi->channels, ch);
f8e76e2e
MH
167 drvc->instances = g_slist_append(drvc->instances, sdi);
168 devices = g_slist_append(devices, sdi);
169 break;
170 }
e790bd5c
UH
171
172 /*
173 * The interface of the DM9x0 contains a cap that needs to
174 * charge for up to 10s before the interface works, if not
175 * powered externally. Therefore wait a little to improve
176 * chances.
177 */
f8e76e2e
MH
178 if (cnt == 3) {
179 sr_info("Waiting 5s to allow interface to settle.");
180 g_usleep(5 * 1000 * 1000);
181 }
182 }
183
184 g_free(buf);
185
186 serial_close(serial);
187 if (!devices)
188 sr_serial_dev_inst_free(serial);
bfd48770
MH
189
190 return devices;
191}
192
49c06128
MH
193static GSList *scan_norma_dmm(GSList *options)
194{
195 return do_scan(&norma_dmm_driver_info, options);
196}
197
198static GSList *scan_siemens_b102x(GSList *options)
199{
200 return do_scan(&siemens_b102x_driver_info, options);
201}
202
203static GSList *dev_list_norma_dmm(void)
204{
205 return ((struct drv_context *)(norma_dmm_driver_info.priv))->instances;
206}
207
208static GSList *dev_list_siemens_b102x(void)
bfd48770 209{
49c06128 210 return ((struct drv_context *)(siemens_b102x_driver_info.priv))->instances;
bfd48770
MH
211}
212
bfd48770
MH
213static int dev_close(struct sr_dev_inst *sdi)
214{
f8e76e2e 215 struct dev_context *devc;
bfd48770 216
bf2c987f 217 std_serial_dev_close(sdi);
bfd48770 218
e790bd5c 219 /* Free dynamically allocated resources. */
f8e76e2e
MH
220 if ((devc = sdi->priv) && devc->version) {
221 g_free(devc->version);
222 devc->version = NULL;
223 g_timer_destroy(devc->elapsed_msec);
224 }
bfd48770
MH
225
226 return SR_OK;
227}
228
49c06128
MH
229static int cleanup_norma_dmm(void)
230{
231 return std_dev_clear(&norma_dmm_driver_info, NULL);
232}
233
234static int cleanup_siemens_b102x(void)
bfd48770 235{
49c06128 236 return std_dev_clear(&siemens_b102x_driver_info, NULL);
bfd48770
MH
237}
238
584560f1 239static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 240 const struct sr_channel_group *cg)
bfd48770 241{
f8e76e2e 242 struct dev_context *devc;
bfd48770 243
53b4680f 244 (void)cg;
d3c74a6f 245
bfd48770
MH
246 if (sdi->status != SR_ST_ACTIVE)
247 return SR_ERR_DEV_CLOSED;
248
f8e76e2e
MH
249 if (!(devc = sdi->priv)) {
250 sr_err("sdi->priv was NULL.");
251 return SR_ERR_BUG;
252 }
253
bfd48770 254 switch (key) {
f8e76e2e 255 case SR_CONF_LIMIT_MSEC:
f8e76e2e
MH
256 if (g_variant_get_uint64(data) == 0) {
257 sr_err("LIMIT_MSEC can't be 0.");
258 return SR_ERR;
259 }
260 devc->limit_msec = g_variant_get_uint64(data);
261 sr_dbg("Setting time limit to %" PRIu64 "ms.",
262 devc->limit_msec);
263 break;
264 case SR_CONF_LIMIT_SAMPLES:
265 devc->limit_samples = g_variant_get_uint64(data);
266 sr_dbg("Setting sample limit to %" PRIu64 ".",
267 devc->limit_samples);
268 break;
bfd48770 269 default:
f8e76e2e 270 return SR_ERR_NA;
bfd48770
MH
271 }
272
f8e76e2e 273 return SR_OK;
bfd48770
MH
274}
275
584560f1 276static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 277 const struct sr_channel_group *cg)
bfd48770 278{
bfd48770 279 (void)sdi;
53b4680f 280 (void)cg;
bfd48770 281
bfd48770 282 switch (key) {
f8e76e2e 283 case SR_CONF_SCAN_OPTIONS:
584560f1 284 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
a0e0bb41 285 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
f8e76e2e
MH
286 break;
287 case SR_CONF_DEVICE_OPTIONS:
584560f1 288 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f254bc4b 289 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
f8e76e2e 290 break;
bfd48770
MH
291 default:
292 return SR_ERR_NA;
293 }
294
f8e76e2e 295 return SR_OK;
bfd48770
MH
296}
297
298static int dev_acquisition_start(const struct sr_dev_inst *sdi,
299 void *cb_data)
300{
f8e76e2e
MH
301 struct dev_context *devc;
302 struct sr_serial_dev_inst *serial;
303
f8e76e2e
MH
304 if (!sdi || !cb_data || !(devc = sdi->priv))
305 return SR_ERR_BUG;
306
bfd48770
MH
307 if (sdi->status != SR_ST_ACTIVE)
308 return SR_ERR_DEV_CLOSED;
309
f8e76e2e
MH
310 devc->cb_data = cb_data;
311
312 /* Send header packet to the session bus. */
313 std_session_send_df_header(cb_data, LOG_PREFIX);
314
315 /* Start timer, if required. */
316 if (devc->limit_msec)
317 g_timer_start(devc->elapsed_msec);
318
319 /* Poll every 100ms, or whenever some data comes in. */
320 serial = sdi->conn;
102f1239
BV
321 serial_source_add(sdi->session, serial, G_IO_IN, 100,
322 norma_dmm_receive_data, (void *)sdi);
bfd48770
MH
323
324 return SR_OK;
325}
326
327static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
328{
f8e76e2e 329 struct dev_context *devc;
bfd48770 330
f8e76e2e
MH
331 /* Stop timer, if required. */
332 if (sdi && (devc = sdi->priv) && devc->limit_msec)
333 g_timer_stop(devc->elapsed_msec);
bfd48770 334
d43b0908 335 return std_serial_dev_acquisition_stop(sdi, cb_data, dev_close,
f8e76e2e 336 sdi->conn, LOG_PREFIX);
bfd48770
MH
337}
338
339SR_PRIV struct sr_dev_driver norma_dmm_driver_info = {
340 .name = "norma-dmm",
49c06128
MH
341 .longname = "Norma DM9x0 DMMs",
342 .api_version = 1,
343 .init = init_norma_dmm,
344 .cleanup = cleanup_norma_dmm,
345 .scan = scan_norma_dmm,
346 .dev_list = dev_list_norma_dmm,
347 .dev_clear = NULL,
348 .config_get = NULL,
349 .config_set = config_set,
350 .config_list = config_list,
351 .dev_open = std_serial_dev_open,
352 .dev_close = dev_close,
353 .dev_acquisition_start = dev_acquisition_start,
354 .dev_acquisition_stop = dev_acquisition_stop,
355 .priv = NULL,
356};
357
358
359SR_PRIV struct sr_dev_driver siemens_b102x_driver_info = {
360 .name = "siemens-b102x",
361 .longname = "Siemens B102x DMMs",
bfd48770 362 .api_version = 1,
49c06128
MH
363 .init = init_siemens_b102x,
364 .cleanup = cleanup_siemens_b102x,
365 .scan = scan_siemens_b102x,
366 .dev_list = dev_list_siemens_b102x,
a6630742 367 .dev_clear = NULL,
f8e76e2e 368 .config_get = NULL,
bfd48770
MH
369 .config_set = config_set,
370 .config_list = config_list,
854434de 371 .dev_open = std_serial_dev_open,
bfd48770
MH
372 .dev_close = dev_close,
373 .dev_acquisition_start = dev_acquisition_start,
374 .dev_acquisition_stop = dev_acquisition_stop,
375 .priv = NULL,
376};