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