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