]> sigrok.org Git - libsigrok.git/blob - hardware/norma-dmm/api.c
Route sr_source_add for all serial devices through a serial_source_add wrapper.
[libsigrok.git] / hardware / norma-dmm / api.c
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
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
34 #define BUF_MAX 50
35
36 #define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1"
37
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 {
48         struct sr_dev_inst *sdi;
49         struct drv_context *drvc;
50         struct dev_context *devc;
51         struct sr_config *src;
52         struct sr_probe *probe;
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
62         devices = NULL;
63         drvc = di->priv;
64         drvc->instances = NULL;
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);
90
91         if (!(buf = g_try_malloc(BUF_MAX))) {
92                 sr_err("Serial buffer malloc failed.");
93                 return NULL;
94         }
95
96         snprintf(req, sizeof(req), "%s\r\n",
97                  nmadmm_requests[NMADMM_REQ_IDN].req_str);
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.",
101                                errno, strerror(errno));
102                         return NULL;
103                 }
104                 len = BUF_MAX;
105                 serial_readline(serial, &buf, &len, 1500);
106                 if (!len)
107                         continue;
108                 buf[BUF_MAX - 1] = '\0';
109
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)) {
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;
120                         if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
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;
131                         if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
132                                 "P1")))
133                                 return NULL;
134                         sdi->probes = g_slist_append(sdi->probes, probe);
135                         drvc->instances = g_slist_append(drvc->instances, sdi);
136                         devices = g_slist_append(devices, sdi);
137                         break;
138                 }
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                  */
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);
157
158         return devices;
159 }
160
161 static GSList *dev_list(void)
162 {
163         return ((struct drv_context *)(di->priv))->instances;
164 }
165
166 static int dev_clear(void)
167 {
168         return std_dev_clear(di, NULL);
169 }
170
171 static int dev_open(struct sr_dev_inst *sdi)
172 {
173         struct sr_serial_dev_inst *serial;
174
175         serial = sdi->conn;
176         if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
177                 return SR_ERR;
178
179         sdi->status = SR_ST_ACTIVE;
180
181         return SR_OK;
182 }
183
184 static int dev_close(struct sr_dev_inst *sdi)
185 {
186         struct sr_serial_dev_inst *serial;
187         struct dev_context *devc;
188
189         serial = sdi->conn;
190         if (serial && serial->fd != -1) {
191                 serial_close(serial);
192                 sdi->status = SR_ST_INACTIVE;
193         }
194
195         /* Free dynamically allocated resources. */
196         if ((devc = sdi->priv) && devc->version) {
197                 g_free(devc->version);
198                 devc->version = NULL;
199                 g_timer_destroy(devc->elapsed_msec);
200         }
201
202         return SR_OK;
203 }
204
205 static int cleanup(void)
206 {
207         return dev_clear();
208 }
209
210 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
211                 const struct sr_probe_group *probe_group)
212 {
213         struct dev_context *devc;
214
215         (void)probe_group;
216
217         if (sdi->status != SR_ST_ACTIVE)
218                 return SR_ERR_DEV_CLOSED;
219
220         if (!(devc = sdi->priv)) {
221                 sr_err("sdi->priv was NULL.");
222                 return SR_ERR_BUG;
223         }
224
225         switch (key) {
226         case SR_CONF_LIMIT_MSEC:
227                 /* TODO: not yet implemented */
228                 if (g_variant_get_uint64(data) == 0) {
229                         sr_err("LIMIT_MSEC can't be 0.");
230                         return SR_ERR;
231                 }
232                 devc->limit_msec = g_variant_get_uint64(data);
233                 sr_dbg("Setting time limit to %" PRIu64 "ms.",
234                        devc->limit_msec);
235                 break;
236         case SR_CONF_LIMIT_SAMPLES:
237                 devc->limit_samples = g_variant_get_uint64(data);
238                 sr_dbg("Setting sample limit to %" PRIu64 ".",
239                        devc->limit_samples);
240                 break;
241         default:
242                 return SR_ERR_NA;
243         }
244
245         return SR_OK;
246 }
247
248 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
249                 const struct sr_probe_group *probe_group)
250 {
251         (void)sdi;
252         (void)probe_group;
253
254         switch (key) {
255         case SR_CONF_SCAN_OPTIONS:
256                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
257                                 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
258                 break;
259         case SR_CONF_DEVICE_OPTIONS:
260                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
261                                 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
262                 break;
263         default:
264                 return SR_ERR_NA;
265         }
266
267         return SR_OK;
268 }
269
270 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
271                                     void *cb_data)
272 {
273         struct dev_context *devc;
274         struct sr_serial_dev_inst *serial;
275
276         (void)sdi;
277         (void)cb_data;
278
279         if (!sdi || !cb_data || !(devc = sdi->priv))
280                 return SR_ERR_BUG;
281
282         if (sdi->status != SR_ST_ACTIVE)
283                 return SR_ERR_DEV_CLOSED;
284
285         devc->cb_data = cb_data;
286
287         /* Send header packet to the session bus. */
288         std_session_send_df_header(cb_data, LOG_PREFIX);
289
290         /* Start timer, if required. */
291         if (devc->limit_msec)
292                 g_timer_start(devc->elapsed_msec);
293
294         /* Poll every 100ms, or whenever some data comes in. */
295         serial = sdi->conn;
296         serial_source_add(serial, G_IO_IN, 100, norma_dmm_receive_data,
297                       (void *)sdi);
298
299         return SR_OK;
300 }
301
302 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
303 {
304         struct dev_context *devc;
305
306         /* Stop timer, if required. */
307         if (sdi && (devc = sdi->priv) && devc->limit_msec)
308                 g_timer_stop(devc->elapsed_msec);
309
310         return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
311                         sdi->conn, LOG_PREFIX);
312 }
313
314 SR_PRIV struct sr_dev_driver norma_dmm_driver_info = {
315         .name = "norma-dmm",
316         .longname = "Norma DM9x0 / Siemens B102x DMMs",
317         .api_version = 1,
318         .init = init,
319         .cleanup = cleanup,
320         .scan = scan,
321         .dev_list = dev_list,
322         .dev_clear = dev_clear,
323         .config_get = NULL,
324         .config_set = config_set,
325         .config_list = config_list,
326         .dev_open = dev_open,
327         .dev_close = dev_close,
328         .dev_acquisition_start = dev_acquisition_start,
329         .dev_acquisition_stop = dev_acquisition_stop,
330         .priv = NULL,
331 };