]> sigrok.org Git - libsigrok.git/blame - hardware/brymen-dmm/api.c
Use consistent API callback function names.
[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
6078d2c9 37static int init(struct sr_context *sr_ctx)
601fb67c 38{
29a27196 39 return std_hw_init(sr_ctx, di, LOG_PREFIX);
601fb67c
AG
40}
41
42static void free_instance(void *inst)
43{
44 struct sr_dev_inst *sdi;
d9a7c349 45 struct sr_serial_dev_inst *serial;
c5d6f5cc 46
601fb67c
AG
47 if (!(sdi = inst))
48 return;
d9a7c349
UH
49
50 serial = sdi->conn;
51 sr_serial_dev_inst_free(serial);
601fb67c
AG
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
d9a7c349
UH
113 sdi->inst_type = SR_INST_SERIAL;
114 sdi->conn = serial;
601fb67c
AG
115 drvc = di->priv;
116 sdi->priv = devc;
117 sdi->driver = di;
118
119 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
120 goto scan_cleanup;
121
122 sdi->probes = g_slist_append(sdi->probes, probe);
123 drvc->instances = g_slist_append(drvc->instances, sdi);
124 devices = g_slist_append(devices, sdi);
125
601fb67c
AG
126scan_cleanup:
127 serial_close(serial);
128
129 return devices;
20cbc785
AG
130}
131
6078d2c9 132static GSList *scan(GSList *options)
20cbc785
AG
133{
134 struct drv_context *drvc;
601fb67c
AG
135 struct sr_config *src;
136 GSList *devices, *l;
137 const char *conn, *serialcomm;
20cbc785 138
20cbc785
AG
139 devices = NULL;
140 drvc = di->priv;
141 drvc->instances = NULL;
142
601fb67c
AG
143 conn = serialcomm = NULL;
144 for (l = options; l; l = l->next) {
145 src = l->data;
146 switch (src->key) {
c5d6f5cc 147 case SR_CONF_CONN:
510b3e69 148 conn = g_variant_get_string(src->data, NULL);
c5d6f5cc
UH
149 break;
150 case SR_CONF_SERIALCOMM:
510b3e69 151 serialcomm = g_variant_get_string(src->data, NULL);
c5d6f5cc 152 break;
601fb67c
AG
153 }
154 }
d9a7c349 155 if (!conn)
601fb67c 156 return NULL;
601fb67c
AG
157
158 if (serialcomm) {
159 /* Use the provided comm specs. */
160 devices = brymen_scan(conn, serialcomm);
161 } else {
c5d6f5cc 162 /* But 9600/8n1 should work all of the time. */
601fb67c
AG
163 devices = brymen_scan(conn, "9600/8n1/dtr=1/rts=1");
164 }
20cbc785
AG
165
166 return devices;
167}
168
6078d2c9 169static GSList *dev_list(void)
20cbc785 170{
0e94d524 171 return ((struct drv_context *)(di->priv))->instances;
20cbc785
AG
172}
173
6078d2c9 174static int dev_open(struct sr_dev_inst *sdi)
20cbc785 175{
d9a7c349 176 struct sr_serial_dev_inst *serial;
601fb67c 177
d9a7c349
UH
178 serial = sdi->conn;
179 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
601fb67c
AG
180 return SR_ERR;
181
182 sdi->status = SR_ST_ACTIVE;
20cbc785
AG
183
184 return SR_OK;
185}
186
6078d2c9 187static int dev_close(struct sr_dev_inst *sdi)
20cbc785 188{
d9a7c349 189 struct sr_serial_dev_inst *serial;
601fb67c 190
d9a7c349
UH
191 serial = sdi->conn;
192 if (serial && serial->fd != -1) {
193 serial_close(serial);
601fb67c
AG
194 sdi->status = SR_ST_INACTIVE;
195 }
20cbc785
AG
196
197 return SR_OK;
198}
199
6078d2c9 200static int cleanup(void)
20cbc785
AG
201{
202 clear_instances();
203
20cbc785
AG
204 return SR_OK;
205}
206
510b3e69 207static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
20cbc785 208{
601fb67c 209 struct dev_context *devc;
20cbc785
AG
210 int ret;
211
e73ffd42
BV
212 if (sdi->status != SR_ST_ACTIVE)
213 return SR_ERR_DEV_CLOSED;
20cbc785 214
601fb67c
AG
215 if (!(devc = sdi->priv)) {
216 sr_err("sdi->priv was NULL.");
217 return SR_ERR_BUG;
218 }
219
20cbc785 220 ret = SR_OK;
601fb67c 221 switch (id) {
c5d6f5cc 222 case SR_CONF_LIMIT_SAMPLES:
510b3e69 223 devc->limit_samples = g_variant_get_uint64(data);
601fb67c 224 break;
c5d6f5cc 225 case SR_CONF_LIMIT_MSEC:
510b3e69 226 devc->limit_msec = g_variant_get_uint64(data);
601fb67c 227 break;
20cbc785 228 default:
bd6fbf62 229 ret = SR_ERR_NA;
20cbc785
AG
230 }
231
232 return ret;
233}
234
510b3e69
BV
235static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
236{
237 (void)sdi;
238
239 switch (key) {
240 case SR_CONF_SCAN_OPTIONS:
241 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
242 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
243 break;
244 case SR_CONF_DEVICE_OPTIONS:
245 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
246 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
247 break;
248 default:
bd6fbf62 249 return SR_ERR_NA;
510b3e69
BV
250 }
251
252 return SR_OK;
253}
254
6078d2c9 255static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
20cbc785 256{
601fb67c 257 struct dev_context *devc;
d9a7c349 258 struct sr_serial_dev_inst *serial;
601fb67c 259
e73ffd42
BV
260 if (sdi->status != SR_ST_ACTIVE)
261 return SR_ERR_DEV_CLOSED;
262
601fb67c
AG
263 if (!(devc = sdi->priv)) {
264 sr_err("sdi->priv was NULL.");
265 return SR_ERR_BUG;
266 }
267
601fb67c
AG
268 devc->cb_data = cb_data;
269
270 /*
271 * Reset the number of samples to take. If we've already collected our
272 * quota, but we start a new session, and don't reset this, we'll just
273 * quit without acquiring any new samples.
274 */
275 devc->num_samples = 0;
276 devc->starttime = g_get_monotonic_time();
277
278 /* Send header packet to the session bus. */
29a27196 279 std_session_send_df_header(cb_data, LOG_PREFIX);
601fb67c
AG
280
281 /* Poll every 50ms, or whenever some data comes in. */
d9a7c349
UH
282 serial = sdi->conn;
283 sr_source_add(serial->fd, G_IO_IN, 50,
601fb67c 284 brymen_dmm_receive_data, (void *)sdi);
20cbc785
AG
285
286 return SR_OK;
287}
288
6078d2c9 289static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
20cbc785 290{
6078d2c9 291 return std_hw_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
29a27196 292 sdi->conn, LOG_PREFIX);
20cbc785
AG
293}
294
c5d6f5cc
UH
295SR_PRIV struct sr_dev_driver brymen_bm857_driver_info = {
296 .name = "brymen-bm857",
297 .longname = "Brymen BM857",
20cbc785 298 .api_version = 1,
6078d2c9
UH
299 .init = init,
300 .cleanup = cleanup,
301 .scan = scan,
302 .dev_list = dev_list,
20cbc785 303 .dev_clear = clear_instances,
6fab7b8f
UH
304 .config_get = NULL,
305 .config_set = config_set,
306 .config_list = config_list,
6078d2c9
UH
307 .dev_open = dev_open,
308 .dev_close = dev_close,
309 .dev_acquisition_start = dev_acquisition_start,
310 .dev_acquisition_stop = dev_acquisition_stop,
20cbc785
AG
311 .priv = NULL,
312};