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