]> sigrok.org Git - libsigrok.git/blame - src/hardware/norma-dmm/api.c
hwdriver.c: Fix two scan-build warnings.
[libsigrok.git] / src / 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
6ec6c43b 20#include <config.h>
bfd48770
MH
21#include "protocol.h"
22
a0e0bb41 23static const uint32_t scanopts[] = {
f8e76e2e
MH
24 SR_CONF_CONN,
25 SR_CONF_SERIALCOMM,
26};
27
05199c0a 28static const uint32_t drvopts[] = {
f8e76e2e 29 SR_CONF_MULTIMETER,
05199c0a
UH
30};
31
32static const uint32_t devopts[] = {
f8e76e2e 33 SR_CONF_CONTINUOUS,
5827f61b
BV
34 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
35 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
f8e76e2e
MH
36};
37
e790bd5c 38#define BUF_MAX 50
f8e76e2e
MH
39
40#define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1"
41
dd5c48a6
LPC
42static struct sr_dev_driver norma_dmm_driver_info;
43static struct sr_dev_driver siemens_b102x_driver_info;
bfd48770 44
c442ffda 45static const char *get_brandstr(struct sr_dev_driver *drv)
bfd48770 46{
0cadb8a3 47 return (drv == &norma_dmm_driver_info) ? "Norma" : "Siemens";
bfd48770
MH
48}
49
c442ffda 50static const char *get_typestr(int type, struct sr_dev_driver *drv)
49c06128 51{
c442ffda 52 static const char *nameref[5][2] = {
49c06128
MH
53 {"DM910", "B1024"},
54 {"DM920", "B1025"},
55 {"DM930", "B1026"},
56 {"DM940", "B1027"},
0cadb8a3
UH
57 {"DM950", "B1028"},
58 };
49c06128
MH
59
60 if ((type < 1) || (type > 5))
61 return "Unknown type!";
62
0cadb8a3 63 return nameref[type - 1][(drv == &siemens_b102x_driver_info)];
49c06128
MH
64}
65
c442ffda 66static GSList *scan(struct sr_dev_driver *drv, GSList *options)
bfd48770 67{
f8e76e2e 68 struct sr_dev_inst *sdi;
f8e76e2e
MH
69 struct dev_context *devc;
70 struct sr_config *src;
f8e76e2e
MH
71 struct sr_serial_dev_inst *serial;
72 GSList *l, *devices;
0cadb8a3 73 int len, cnt, auxtype;
f8e76e2e
MH
74 const char *conn, *serialcomm;
75 char *buf;
f8e76e2e 76 char req[10];
f8e76e2e 77
bfd48770 78 devices = NULL;
f8e76e2e
MH
79 conn = serialcomm = NULL;
80
81 for (l = options; l; l = l->next) {
82 src = l->data;
83 switch (src->key) {
84 case SR_CONF_CONN:
85 conn = g_variant_get_string(src->data, NULL);
86 break;
87 case SR_CONF_SERIALCOMM:
88 serialcomm = g_variant_get_string(src->data, NULL);
89 break;
90 }
91 }
92 if (!conn)
93 return NULL;
94 if (!serialcomm)
95 serialcomm = SERIALCOMM;
96
91219afc 97 serial = sr_serial_dev_inst_new(conn, serialcomm);
f8e76e2e 98
d12ef776 99 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
f8e76e2e
MH
100 return NULL;
101
102 serial_flush(serial);
bfd48770 103
a95f142e 104 buf = g_malloc(BUF_MAX);
f8e76e2e
MH
105
106 snprintf(req, sizeof(req), "%s\r\n",
e790bd5c 107 nmadmm_requests[NMADMM_REQ_IDN].req_str);
a4e435eb 108 g_usleep(150 * 1000); /* Wait a little to allow serial port to settle. */
f8e76e2e 109 for (cnt = 0; cnt < 7; cnt++) {
d545c81c
UH
110 if (serial_write_blocking(serial, req, strlen(req),
111 serial_timeout(serial, strlen(req))) < 0) {
8849f45a 112 sr_err("Unable to send identification request.");
8eadb70a 113 g_free(buf);
f8e76e2e
MH
114 return NULL;
115 }
116 len = BUF_MAX;
a4e435eb 117 serial_readline(serial, &buf, &len, NMADMM_TIMEOUT_MS);
f8e76e2e
MH
118 if (!len)
119 continue;
e790bd5c 120 buf[BUF_MAX - 1] = '\0';
f8e76e2e 121
0cadb8a3 122 /* Match ID string, e.g. "1834 065 V1.06,IF V1.02" (DM950). */
e790bd5c 123 if (g_regex_match_simple("^1834 [^,]*,IF V*", (char *)buf, 0, 0)) {
f8e76e2e 124 auxtype = xgittoint(buf[7]);
49c06128 125 sr_spew("%s %s DMM %s detected!", get_brandstr(drv), get_typestr(auxtype, drv), buf + 9);
f8e76e2e 126
aac29cc1 127 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
128 sdi->status = SR_ST_INACTIVE;
129 sdi->vendor = g_strdup(get_brandstr(drv));
130 sdi->model = g_strdup(get_typestr(auxtype, drv));
131 sdi->version = g_strdup(buf + 9);
f57d8ffe 132 devc = g_malloc0(sizeof(struct dev_context));
37dbffd1 133 sr_sw_limits_init(&devc->limits);
f8e76e2e 134 devc->type = auxtype;
f8e76e2e
MH
135 sdi->conn = serial;
136 sdi->priv = devc;
5e23fcab 137 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
f8e76e2e
MH
138 devices = g_slist_append(devices, sdi);
139 break;
140 }
e790bd5c
UH
141
142 /*
143 * The interface of the DM9x0 contains a cap that needs to
144 * charge for up to 10s before the interface works, if not
145 * powered externally. Therefore wait a little to improve
146 * chances.
147 */
f8e76e2e
MH
148 if (cnt == 3) {
149 sr_info("Waiting 5s to allow interface to settle.");
150 g_usleep(5 * 1000 * 1000);
151 }
152 }
153
154 g_free(buf);
155
156 serial_close(serial);
157 if (!devices)
158 sr_serial_dev_inst_free(serial);
bfd48770 159
15a5bfe4 160 return std_scan_complete(drv, devices);
bfd48770
MH
161}
162
dd7a72ea
UH
163static int config_set(uint32_t key, GVariant *data,
164 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
bfd48770 165{
f8e76e2e 166 struct dev_context *devc;
bfd48770 167
53b4680f 168 (void)cg;
d3c74a6f 169
b0baddef 170 devc = sdi->priv;
f8e76e2e 171
37dbffd1 172 return sr_sw_limits_config_set(&devc->limits, key, data);
bfd48770
MH
173}
174
dd7a72ea
UH
175static int config_list(uint32_t key, GVariant **data,
176 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
bfd48770 177{
05199c0a 178 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
bfd48770
MH
179}
180
695dc859 181static int dev_acquisition_start(const struct sr_dev_inst *sdi)
bfd48770 182{
f8e76e2e
MH
183 struct dev_context *devc;
184 struct sr_serial_dev_inst *serial;
185
208c1d35 186 devc = sdi->priv;
f8e76e2e 187
37dbffd1 188 sr_sw_limits_acquisition_start(&devc->limits);
bee2b016 189 std_session_send_df_header(sdi);
f8e76e2e 190
f8e76e2e 191 serial = sdi->conn;
102f1239
BV
192 serial_source_add(sdi->session, serial, G_IO_IN, 100,
193 norma_dmm_receive_data, (void *)sdi);
bfd48770
MH
194
195 return SR_OK;
196}
197
dd5c48a6 198static struct sr_dev_driver norma_dmm_driver_info = {
bfd48770 199 .name = "norma-dmm",
49c06128
MH
200 .longname = "Norma DM9x0 DMMs",
201 .api_version = 1,
c2fdcc25 202 .init = std_init,
700d6b64 203 .cleanup = std_cleanup,
4f840ce9 204 .scan = scan,
c01bf34c 205 .dev_list = std_dev_list,
f778bf02 206 .dev_clear = std_dev_clear,
49c06128
MH
207 .config_get = NULL,
208 .config_set = config_set,
209 .config_list = config_list,
210 .dev_open = std_serial_dev_open,
eabfed20 211 .dev_close = std_serial_dev_close,
49c06128 212 .dev_acquisition_start = dev_acquisition_start,
4b1a9d5d 213 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
41812aca 214 .context = NULL,
49c06128 215};
dd5c48a6 216SR_REGISTER_DEV_DRIVER(norma_dmm_driver_info);
49c06128 217
dd5c48a6 218static struct sr_dev_driver siemens_b102x_driver_info = {
49c06128
MH
219 .name = "siemens-b102x",
220 .longname = "Siemens B102x DMMs",
bfd48770 221 .api_version = 1,
c2fdcc25 222 .init = std_init,
700d6b64 223 .cleanup = std_cleanup,
4f840ce9 224 .scan = scan,
c01bf34c 225 .dev_list = std_dev_list,
f778bf02 226 .dev_clear = std_dev_clear,
f8e76e2e 227 .config_get = NULL,
bfd48770
MH
228 .config_set = config_set,
229 .config_list = config_list,
854434de 230 .dev_open = std_serial_dev_open,
eabfed20 231 .dev_close = std_serial_dev_close,
bfd48770 232 .dev_acquisition_start = dev_acquisition_start,
4b1a9d5d 233 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
41812aca 234 .context = NULL,
bfd48770 235};
dd5c48a6 236SR_REGISTER_DEV_DRIVER(siemens_b102x_driver_info);