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