]> sigrok.org Git - libsigrok.git/blame - hardware/norma-dmm/api.c
norma dmm: Implemented driver.
[libsigrok.git] / 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
f8e76e2e
MH
20#include <errno.h>
21#include <stdio.h>
22#include <string.h>
23
bfd48770
MH
24#include "protocol.h"
25
f8e76e2e
MH
26
27static const int32_t hwopts[] = {
28 SR_CONF_CONN,
29 SR_CONF_SERIALCOMM,
30};
31
32static const int32_t hwcaps[] = {
33 SR_CONF_MULTIMETER,
34 SR_CONF_LIMIT_SAMPLES,
35 SR_CONF_LIMIT_MSEC,
36 SR_CONF_CONTINUOUS,
37};
38
39
40#define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1"
41
42
bfd48770
MH
43SR_PRIV struct sr_dev_driver norma_dmm_driver_info;
44static struct sr_dev_driver *di = &norma_dmm_driver_info;
45
f8e76e2e 46
bfd48770
MH
47static int init(struct sr_context *sr_ctx)
48{
49 return std_init(sr_ctx, di, LOG_PREFIX);
50}
51
f8e76e2e 52
bfd48770
MH
53static GSList *scan(GSList *options)
54{
f8e76e2e 55 struct sr_dev_inst *sdi;
bfd48770 56 struct drv_context *drvc;
f8e76e2e
MH
57 struct dev_context *devc;
58 struct sr_config *src;
59 struct sr_probe *probe;
60 struct sr_serial_dev_inst *serial;
61 GSList *l, *devices;
62 int len, cnt;
63 const char *conn, *serialcomm;
64 char *buf;
65 char fmttype[10];
66 char req[10];
67 int auxtype;
68
69 #define BUF_MAX (50)
bfd48770
MH
70
71 devices = NULL;
72 drvc = di->priv;
73 drvc->instances = NULL;
f8e76e2e
MH
74 conn = serialcomm = NULL;
75
76 for (l = options; l; l = l->next) {
77 src = l->data;
78 switch (src->key) {
79 case SR_CONF_CONN:
80 conn = g_variant_get_string(src->data, NULL);
81 break;
82 case SR_CONF_SERIALCOMM:
83 serialcomm = g_variant_get_string(src->data, NULL);
84 break;
85 }
86 }
87 if (!conn)
88 return NULL;
89 if (!serialcomm)
90 serialcomm = SERIALCOMM;
91
92 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
93 return NULL;
94
95 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
96 return NULL;
97
98 serial_flush(serial);
bfd48770 99
f8e76e2e
MH
100 len = BUF_MAX;
101 if (!(buf = g_try_malloc(len))) {
102 sr_err("Serial buffer malloc failed.");
103 return NULL;
104 }
105
106 snprintf(req, sizeof(req), "%s\r\n",
107 nmadmm_requests[NMADMM_REQ_IDN].reqstr);
108 for (cnt = 0; cnt < 7; cnt++) {
109 if (serial_write(serial, req, strlen(req)) == -1) {
110 sr_err("Unable to send identification request: %d %s.",
111 errno, strerror(errno));
112 return NULL;
113 }
114 len = BUF_MAX;
115 serial_readline(serial, &buf, &len, 1500);
116 if (!len)
117 continue;
118 buf[BUF_MAX-1] = '\0';
119
120 /* Match id string, e.g. "1834 065 V1.06,IF V1.02" (DM950) */
121 if (g_regex_match_simple("^1834 [^,]*,IF V*", (char*)buf,0,0)) {
122 auxtype = xgittoint(buf[7]);
123 // TODO: Will this work with non-DM950?
124 snprintf(fmttype, sizeof(fmttype), "DM9%d0", auxtype);
125 sr_spew("Norma %s DMM %s detected!", fmttype, &buf[9]);
126
127 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
128 "Norma", fmttype, buf + 9)))
129 return NULL;
130 if (!(devc = g_try_malloc0(sizeof(*devc)))) {
131 sr_err("Device context malloc failed.");
132 return NULL;
133 }
134 devc->type = auxtype;
135 devc->version = g_strdup(&buf[9]);
136 devc->elapsed_msec = g_timer_new();
137
138 sdi->conn = serial;
139 sdi->priv = devc;
140 sdi->driver = di;
141 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
142 "P1")))
143 return NULL;
144 sdi->probes = g_slist_append(sdi->probes, probe);
145 drvc->instances = g_slist_append(drvc->instances, sdi);
146 devices = g_slist_append(devices, sdi);
147 break;
148 }
149 /* The interface of the DM9x0 contains a cap that needs to
150 charge for up to 10s before the interface works, if not powered
151 externally. Therefore wait a little to improve chances. */
152 if (cnt == 3) {
153 sr_info("Waiting 5s to allow interface to settle.");
154 g_usleep(5 * 1000 * 1000);
155 }
156 }
157
158 g_free(buf);
159
160 serial_close(serial);
161 if (!devices)
162 sr_serial_dev_inst_free(serial);
bfd48770
MH
163
164 return devices;
165}
166
f8e76e2e 167
bfd48770
MH
168static GSList *dev_list(void)
169{
170 return ((struct drv_context *)(di->priv))->instances;
171}
172
f8e76e2e 173
bfd48770
MH
174static int dev_clear(void)
175{
176 return std_dev_clear(di, NULL);
177}
178
f8e76e2e 179
bfd48770
MH
180static int dev_open(struct sr_dev_inst *sdi)
181{
f8e76e2e 182 struct sr_serial_dev_inst *serial;
bfd48770 183
f8e76e2e
MH
184 serial = sdi->conn;
185 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
186 return SR_ERR;
bfd48770
MH
187
188 sdi->status = SR_ST_ACTIVE;
189
190 return SR_OK;
191}
192
f8e76e2e 193
bfd48770
MH
194static int dev_close(struct sr_dev_inst *sdi)
195{
f8e76e2e
MH
196 struct sr_serial_dev_inst *serial;
197 struct dev_context *devc;
bfd48770 198
f8e76e2e
MH
199 serial = sdi->conn;
200 if (serial && serial->fd != -1) {
201 serial_close(serial);
202 sdi->status = SR_ST_INACTIVE;
203 }
bfd48770 204
f8e76e2e
MH
205 // Free dynamically allocated resources.
206 if ((devc = sdi->priv) && devc->version) {
207 g_free(devc->version);
208 devc->version = NULL;
209 g_timer_destroy(devc->elapsed_msec);
210 }
bfd48770
MH
211
212 return SR_OK;
213}
214
f8e76e2e 215
bfd48770
MH
216static int cleanup(void)
217{
f8e76e2e 218 return dev_clear();
bfd48770
MH
219}
220
bfd48770
MH
221
222static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
223{
f8e76e2e 224 struct dev_context *devc;
bfd48770
MH
225
226 if (sdi->status != SR_ST_ACTIVE)
227 return SR_ERR_DEV_CLOSED;
228
f8e76e2e
MH
229 if (!(devc = sdi->priv)) {
230 sr_err("sdi->priv was NULL.");
231 return SR_ERR_BUG;
232 }
233
bfd48770 234 switch (key) {
f8e76e2e
MH
235 case SR_CONF_LIMIT_MSEC:
236 /* TODO: not yet implemented */
237 if (g_variant_get_uint64(data) == 0) {
238 sr_err("LIMIT_MSEC can't be 0.");
239 return SR_ERR;
240 }
241 devc->limit_msec = g_variant_get_uint64(data);
242 sr_dbg("Setting time limit to %" PRIu64 "ms.",
243 devc->limit_msec);
244 break;
245 case SR_CONF_LIMIT_SAMPLES:
246 devc->limit_samples = g_variant_get_uint64(data);
247 sr_dbg("Setting sample limit to %" PRIu64 ".",
248 devc->limit_samples);
249 break;
bfd48770 250 default:
f8e76e2e 251 return SR_ERR_NA;
bfd48770
MH
252 }
253
f8e76e2e 254 return SR_OK;
bfd48770
MH
255}
256
f8e76e2e 257
bfd48770
MH
258static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
259{
bfd48770 260 (void)sdi;
bfd48770 261
bfd48770 262 switch (key) {
f8e76e2e
MH
263 case SR_CONF_SCAN_OPTIONS:
264 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
265 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
266 break;
267 case SR_CONF_DEVICE_OPTIONS:
268 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
269 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
270 break;
bfd48770
MH
271 default:
272 return SR_ERR_NA;
273 }
274
f8e76e2e 275 return SR_OK;
bfd48770
MH
276}
277
f8e76e2e 278
bfd48770
MH
279static int dev_acquisition_start(const struct sr_dev_inst *sdi,
280 void *cb_data)
281{
f8e76e2e
MH
282 struct dev_context *devc;
283 struct sr_serial_dev_inst *serial;
284
bfd48770
MH
285 (void)sdi;
286 (void)cb_data;
287
f8e76e2e
MH
288 if (!sdi || !cb_data || !(devc = sdi->priv))
289 return SR_ERR_BUG;
290
bfd48770
MH
291 if (sdi->status != SR_ST_ACTIVE)
292 return SR_ERR_DEV_CLOSED;
293
f8e76e2e
MH
294 devc->cb_data = cb_data;
295
296 /* Send header packet to the session bus. */
297 std_session_send_df_header(cb_data, LOG_PREFIX);
298
299 /* Start timer, if required. */
300 if (devc->limit_msec)
301 g_timer_start(devc->elapsed_msec);
302
303 /* Poll every 100ms, or whenever some data comes in. */
304 serial = sdi->conn;
305 sr_source_add(serial->fd, G_IO_IN, 100, norma_dmm_receive_data,
306 (void *)sdi);
bfd48770
MH
307
308 return SR_OK;
309}
310
f8e76e2e 311
bfd48770
MH
312static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
313{
f8e76e2e 314 struct dev_context *devc;
bfd48770 315
f8e76e2e
MH
316 /* Stop timer, if required. */
317 if (sdi && (devc = sdi->priv) && devc->limit_msec)
318 g_timer_stop(devc->elapsed_msec);
bfd48770 319
f8e76e2e
MH
320 return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
321 sdi->conn, LOG_PREFIX);
bfd48770
MH
322}
323
f8e76e2e 324
bfd48770
MH
325SR_PRIV struct sr_dev_driver norma_dmm_driver_info = {
326 .name = "norma-dmm",
f8e76e2e 327 .longname = "Norma DM910..950, Siemens B1024..1028 DMMs",
bfd48770
MH
328 .api_version = 1,
329 .init = init,
330 .cleanup = cleanup,
331 .scan = scan,
332 .dev_list = dev_list,
333 .dev_clear = dev_clear,
f8e76e2e 334 .config_get = NULL,
bfd48770
MH
335 .config_set = config_set,
336 .config_list = config_list,
337 .dev_open = dev_open,
338 .dev_close = dev_close,
339 .dev_acquisition_start = dev_acquisition_start,
340 .dev_acquisition_stop = dev_acquisition_stop,
341 .priv = NULL,
342};