]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/brymen-dmm/api.c
drivers: return SR_ERR_NA on unsupported config key
[libsigrok.git] / hardware / brymen-dmm / api.c
... / ...
CommitLineData
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
20#include "protocol.h"
21
22static const int32_t hwopts[] = {
23 SR_CONF_CONN,
24 SR_CONF_SERIALCOMM,
25};
26
27static const int32_t hwcaps[] = {
28 SR_CONF_MULTIMETER,
29 SR_CONF_LIMIT_SAMPLES,
30 SR_CONF_CONTINUOUS,
31 SR_CONF_LIMIT_MSEC,
32};
33
34SR_PRIV struct sr_dev_driver brymen_bm857_driver_info;
35static struct sr_dev_driver *di = &brymen_bm857_driver_info;
36
37static int hw_init(struct sr_context *sr_ctx)
38{
39 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
40}
41
42static void free_instance(void *inst)
43{
44 struct sr_dev_inst *sdi;
45 struct dev_context *devc;
46
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
55/* Properly close and free all devices. */
56static int clear_instances(void)
57{
58 struct drv_context *drvc;
59
60 if (!(drvc = di->priv))
61 return SR_OK;
62
63 g_slist_free_full(drvc->instances, free_instance);
64 drvc->instances = NULL;
65
66 return SR_OK;
67}
68
69static GSList *brymen_scan(const char *conn, const char *serialcomm)
70{
71 struct sr_dev_inst *sdi;
72 struct dev_context *devc;
73 struct drv_context *drvc;
74 struct sr_probe *probe;
75 struct sr_serial_dev_inst *serial;
76 GSList *devices;
77 int ret;
78 uint8_t buf[128];
79 size_t len;
80
81 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
82 return NULL;
83
84 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
85 return NULL;
86
87 sr_info("Probing port %s.", conn);
88
89 devices = NULL;
90
91 /* Request reading */
92 if ((ret = brymen_packet_request(serial)) < 0) {
93 sr_err("Unable to send command: %d.", ret);
94 goto scan_cleanup;
95 }
96
97 len = 128;
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;
102
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
125scan_cleanup:
126 serial_close(serial);
127
128 return devices;
129}
130
131static GSList *hw_scan(GSList *options)
132{
133 struct drv_context *drvc;
134 struct sr_config *src;
135 GSList *devices, *l;
136 const char *conn, *serialcomm;
137
138 (void)options;
139
140 devices = NULL;
141 drvc = di->priv;
142 drvc->instances = NULL;
143
144 conn = serialcomm = NULL;
145 for (l = options; l; l = l->next) {
146 src = l->data;
147 switch (src->key) {
148 case SR_CONF_CONN:
149 conn = g_variant_get_string(src->data, NULL);
150 break;
151 case SR_CONF_SERIALCOMM:
152 serialcomm = g_variant_get_string(src->data, NULL);
153 break;
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 {
164 /* But 9600/8n1 should work all of the time. */
165 devices = brymen_scan(conn, "9600/8n1/dtr=1/rts=1");
166 }
167
168 return devices;
169}
170
171static GSList *hw_dev_list(void)
172{
173 return ((struct drv_context *)(di->priv))->instances;
174}
175
176static int hw_dev_open(struct sr_dev_inst *sdi)
177{
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;
189
190 return SR_OK;
191}
192
193static int hw_dev_close(struct sr_dev_inst *sdi)
194{
195 struct dev_context *devc;
196
197 devc = sdi->priv;
198
199 if (devc->serial && devc->serial->fd != -1) {
200 serial_close(devc->serial);
201 sdi->status = SR_ST_INACTIVE;
202 }
203
204 return SR_OK;
205}
206
207static int hw_cleanup(void)
208{
209 clear_instances();
210
211 return SR_OK;
212}
213
214static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
215{
216 struct dev_context *devc;
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
224 if (!(devc = sdi->priv)) {
225 sr_err("sdi->priv was NULL.");
226 return SR_ERR_BUG;
227 }
228
229 ret = SR_OK;
230 switch (id) {
231 case SR_CONF_LIMIT_SAMPLES:
232 devc->limit_samples = g_variant_get_uint64(data);
233 break;
234 case SR_CONF_LIMIT_MSEC:
235 devc->limit_msec = g_variant_get_uint64(data);
236 break;
237 default:
238 ret = SR_ERR_NA;
239 }
240
241 return ret;
242}
243
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:
258 return SR_ERR_NA;
259 }
260
261 return SR_OK;
262}
263
264static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
265 void *cb_data)
266{
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
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. */
285 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
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);
290
291 return SR_OK;
292}
293
294static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
295{
296 return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close,
297 ((struct dev_context *)(sdi->priv))->serial, DRIVER_LOG_DOMAIN);
298}
299
300SR_PRIV struct sr_dev_driver brymen_bm857_driver_info = {
301 .name = "brymen-bm857",
302 .longname = "Brymen BM857",
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,
309 .config_get = NULL,
310 .config_set = config_set,
311 .config_list = config_list,
312 .dev_open = hw_dev_open,
313 .dev_close = hw_dev_close,
314 .dev_acquisition_start = hw_dev_acquisition_start,
315 .dev_acquisition_stop = hw_dev_acquisition_stop,
316 .priv = NULL,
317};