]> sigrok.org Git - libsigrok.git/blame - hardware/brymen-dmm/api.c
Minor whitespace fixes.
[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;
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
132static GSList *hw_scan(GSList *options)
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
169static GSList *hw_dev_list(void)
170{
0e94d524 171 return ((struct drv_context *)(di->priv))->instances;
20cbc785
AG
172}
173
174static int hw_dev_open(struct sr_dev_inst *sdi)
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
187static int hw_dev_close(struct sr_dev_inst *sdi)
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
200static int hw_cleanup(void)
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
20cbc785
AG
255static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
256 void *cb_data)
257{
601fb67c 258 struct dev_context *devc;
d9a7c349 259 struct sr_serial_dev_inst *serial;
601fb67c 260
e73ffd42
BV
261 if (sdi->status != SR_ST_ACTIVE)
262 return SR_ERR_DEV_CLOSED;
263
601fb67c
AG
264 if (!(devc = sdi->priv)) {
265 sr_err("sdi->priv was NULL.");
266 return SR_ERR_BUG;
267 }
268
601fb67c
AG
269 devc->cb_data = cb_data;
270
271 /*
272 * Reset the number of samples to take. If we've already collected our
273 * quota, but we start a new session, and don't reset this, we'll just
274 * quit without acquiring any new samples.
275 */
276 devc->num_samples = 0;
277 devc->starttime = g_get_monotonic_time();
278
279 /* Send header packet to the session bus. */
4afdfd46 280 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
601fb67c
AG
281
282 /* Poll every 50ms, or whenever some data comes in. */
d9a7c349
UH
283 serial = sdi->conn;
284 sr_source_add(serial->fd, G_IO_IN, 50,
601fb67c 285 brymen_dmm_receive_data, (void *)sdi);
20cbc785
AG
286
287 return SR_OK;
288}
289
601fb67c 290static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
20cbc785 291{
cd2f0fe2 292 return std_hw_dev_acquisition_stop_serial(sdi, cb_data, hw_dev_close,
d9a7c349 293 sdi->conn, DRIVER_LOG_DOMAIN);
20cbc785
AG
294}
295
c5d6f5cc
UH
296SR_PRIV struct sr_dev_driver brymen_bm857_driver_info = {
297 .name = "brymen-bm857",
298 .longname = "Brymen BM857",
20cbc785
AG
299 .api_version = 1,
300 .init = hw_init,
301 .cleanup = hw_cleanup,
302 .scan = hw_scan,
303 .dev_list = hw_dev_list,
304 .dev_clear = clear_instances,
6fab7b8f
UH
305 .config_get = NULL,
306 .config_set = config_set,
307 .config_list = config_list,
20cbc785
AG
308 .dev_open = hw_dev_open,
309 .dev_close = hw_dev_close,
20cbc785
AG
310 .dev_acquisition_start = hw_dev_acquisition_start,
311 .dev_acquisition_stop = hw_dev_acquisition_stop,
312 .priv = NULL,
313};