]> sigrok.org Git - libsigrok.git/blame - hardware/brymen-dmm/api.c
drivers: return SR_ERR_NA on unsupported config key
[libsigrok.git] / hardware / brymen-dmm / api.c
CommitLineData
20cbc785
AG
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
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
20cbc785
AG
20#include "protocol.h"
21
510b3e69 22static const int32_t hwopts[] = {
601fb67c
AG
23 SR_CONF_CONN,
24 SR_CONF_SERIALCOMM,
601fb67c
AG
25};
26
510b3e69 27static const int32_t hwcaps[] = {
601fb67c
AG
28 SR_CONF_MULTIMETER,
29 SR_CONF_LIMIT_SAMPLES,
30 SR_CONF_CONTINUOUS,
31 SR_CONF_LIMIT_MSEC,
601fb67c
AG
32};
33
c5d6f5cc
UH
34SR_PRIV struct sr_dev_driver brymen_bm857_driver_info;
35static struct sr_dev_driver *di = &brymen_bm857_driver_info;
20cbc785 36
601fb67c
AG
37static int hw_init(struct sr_context *sr_ctx)
38{
943e94f5 39 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
601fb67c
AG
40}
41
42static void free_instance(void *inst)
43{
44 struct sr_dev_inst *sdi;
45 struct dev_context *devc;
c5d6f5cc 46
601fb67c
AG
47 if (!(sdi = inst))
48 return;
49 if (!(devc = sdi->priv))
50 return;
51 sr_serial_dev_inst_free(devc->serial);
52 sr_dev_inst_free(sdi);
53}
54
20cbc785
AG
55/* Properly close and free all devices. */
56static int clear_instances(void)
57{
20cbc785 58 struct drv_context *drvc;
20cbc785
AG
59
60 if (!(drvc = di->priv))
61 return SR_OK;
62
601fb67c 63 g_slist_free_full(drvc->instances, free_instance);
20cbc785
AG
64 drvc->instances = NULL;
65
66 return SR_OK;
67}
68
601fb67c 69static GSList *brymen_scan(const char *conn, const char *serialcomm)
20cbc785 70{
601fb67c
AG
71 struct sr_dev_inst *sdi;
72 struct dev_context *devc;
20cbc785 73 struct drv_context *drvc;
601fb67c
AG
74 struct sr_probe *probe;
75 struct sr_serial_dev_inst *serial;
76 GSList *devices;
77 int ret;
c5d6f5cc
UH
78 uint8_t buf[128];
79 size_t len;
20cbc785 80
601fb67c
AG
81 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
82 return NULL;
83
c5d6f5cc 84 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
601fb67c
AG
85 return NULL;
86
87 sr_info("Probing port %s.", conn);
88
89 devices = NULL;
90
91 /* Request reading */
c5d6f5cc
UH
92 if ((ret = brymen_packet_request(serial)) < 0) {
93 sr_err("Unable to send command: %d.", ret);
601fb67c 94 goto scan_cleanup;
20cbc785
AG
95 }
96
c5d6f5cc 97 len = 128;
601fb67c
AG
98 ret = brymen_stream_detect(serial, buf, &len, brymen_packet_length,
99 brymen_packet_is_valid, 1000, 9600);
100 if (ret != SR_OK)
101 goto scan_cleanup;
20cbc785 102
601fb67c
AG
103 sr_info("Found device on port %s.", conn);
104
105 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Brymen", "BM85x", "")))
106 goto scan_cleanup;
107
108 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
109 sr_err("Device context malloc failed.");
110 goto scan_cleanup;
111 }
112
113 devc->serial = serial;
114 drvc = di->priv;
115 sdi->priv = devc;
116 sdi->driver = di;
117
118 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
119 goto scan_cleanup;
120
121 sdi->probes = g_slist_append(sdi->probes, probe);
122 drvc->instances = g_slist_append(drvc->instances, sdi);
123 devices = g_slist_append(devices, sdi);
124
601fb67c
AG
125scan_cleanup:
126 serial_close(serial);
127
128 return devices;
20cbc785
AG
129}
130
131static GSList *hw_scan(GSList *options)
132{
133 struct drv_context *drvc;
601fb67c
AG
134 struct sr_config *src;
135 GSList *devices, *l;
136 const char *conn, *serialcomm;
20cbc785
AG
137
138 (void)options;
139
140 devices = NULL;
141 drvc = di->priv;
142 drvc->instances = NULL;
143
601fb67c
AG
144 conn = serialcomm = NULL;
145 for (l = options; l; l = l->next) {
146 src = l->data;
147 switch (src->key) {
c5d6f5cc 148 case SR_CONF_CONN:
510b3e69 149 conn = g_variant_get_string(src->data, NULL);
c5d6f5cc
UH
150 break;
151 case SR_CONF_SERIALCOMM:
510b3e69 152 serialcomm = g_variant_get_string(src->data, NULL);
c5d6f5cc 153 break;
601fb67c
AG
154 }
155 }
156 if (!conn) {
157 return NULL;
158 }
159
160 if (serialcomm) {
161 /* Use the provided comm specs. */
162 devices = brymen_scan(conn, serialcomm);
163 } else {
c5d6f5cc 164 /* But 9600/8n1 should work all of the time. */
601fb67c
AG
165 devices = brymen_scan(conn, "9600/8n1/dtr=1/rts=1");
166 }
20cbc785
AG
167
168 return devices;
169}
170
171static GSList *hw_dev_list(void)
172{
0e94d524 173 return ((struct drv_context *)(di->priv))->instances;
20cbc785
AG
174}
175
176static int hw_dev_open(struct sr_dev_inst *sdi)
177{
601fb67c
AG
178 struct dev_context *devc;
179
180 if (!(devc = sdi->priv)) {
181 sr_err("sdi->priv was NULL.");
182 return SR_ERR_BUG;
183 }
184
185 if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
186 return SR_ERR;
187
188 sdi->status = SR_ST_ACTIVE;
20cbc785
AG
189
190 return SR_OK;
191}
192
193static int hw_dev_close(struct sr_dev_inst *sdi)
194{
601fb67c
AG
195 struct dev_context *devc;
196
961009b0 197 devc = sdi->priv;
601fb67c
AG
198
199 if (devc->serial && devc->serial->fd != -1) {
200 serial_close(devc->serial);
201 sdi->status = SR_ST_INACTIVE;
202 }
20cbc785
AG
203
204 return SR_OK;
205}
206
207static int hw_cleanup(void)
208{
209 clear_instances();
210
20cbc785
AG
211 return SR_OK;
212}
213
510b3e69 214static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
20cbc785 215{
601fb67c 216 struct dev_context *devc;
20cbc785
AG
217 int ret;
218
219 if (sdi->status != SR_ST_ACTIVE) {
220 sr_err("Device inactive, can't set config options.");
221 return SR_ERR;
222 }
223
601fb67c
AG
224 if (!(devc = sdi->priv)) {
225 sr_err("sdi->priv was NULL.");
226 return SR_ERR_BUG;
227 }
228
20cbc785 229 ret = SR_OK;
601fb67c 230 switch (id) {
c5d6f5cc 231 case SR_CONF_LIMIT_SAMPLES:
510b3e69 232 devc->limit_samples = g_variant_get_uint64(data);
601fb67c 233 break;
c5d6f5cc 234 case SR_CONF_LIMIT_MSEC:
510b3e69 235 devc->limit_msec = g_variant_get_uint64(data);
601fb67c 236 break;
20cbc785 237 default:
bd6fbf62 238 ret = SR_ERR_NA;
20cbc785
AG
239 }
240
241 return ret;
242}
243
510b3e69
BV
244static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
245{
246 (void)sdi;
247
248 switch (key) {
249 case SR_CONF_SCAN_OPTIONS:
250 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
251 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
252 break;
253 case SR_CONF_DEVICE_OPTIONS:
254 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
255 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
256 break;
257 default:
bd6fbf62 258 return SR_ERR_NA;
510b3e69
BV
259 }
260
261 return SR_OK;
262}
263
20cbc785
AG
264static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
265 void *cb_data)
266{
601fb67c
AG
267 struct dev_context *devc;
268
269 if (!(devc = sdi->priv)) {
270 sr_err("sdi->priv was NULL.");
271 return SR_ERR_BUG;
272 }
273
601fb67c
AG
274 devc->cb_data = cb_data;
275
276 /*
277 * Reset the number of samples to take. If we've already collected our
278 * quota, but we start a new session, and don't reset this, we'll just
279 * quit without acquiring any new samples.
280 */
281 devc->num_samples = 0;
282 devc->starttime = g_get_monotonic_time();
283
284 /* Send header packet to the session bus. */
4afdfd46 285 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
601fb67c
AG
286
287 /* Poll every 50ms, or whenever some data comes in. */
288 sr_source_add(devc->serial->fd, G_IO_IN, 50,
289 brymen_dmm_receive_data, (void *)sdi);
20cbc785
AG
290
291 return SR_OK;
292}
293
601fb67c 294static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
20cbc785 295{
cd2f0fe2
UH
296 return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close,
297 ((struct dev_context *)(sdi->priv))->serial, DRIVER_LOG_DOMAIN);
20cbc785
AG
298}
299
c5d6f5cc
UH
300SR_PRIV struct sr_dev_driver brymen_bm857_driver_info = {
301 .name = "brymen-bm857",
302 .longname = "Brymen BM857",
20cbc785
AG
303 .api_version = 1,
304 .init = hw_init,
305 .cleanup = hw_cleanup,
306 .scan = hw_scan,
307 .dev_list = hw_dev_list,
308 .dev_clear = clear_instances,
6fab7b8f
UH
309 .config_get = NULL,
310 .config_set = config_set,
311 .config_list = config_list,
20cbc785
AG
312 .dev_open = hw_dev_open,
313 .dev_close = hw_dev_close,
20cbc785
AG
314 .dev_acquisition_start = hw_dev_acquisition_start,
315 .dev_acquisition_stop = hw_dev_acquisition_stop,
316 .priv = NULL,
317};