]> sigrok.org Git - libsigrok.git/blame - src/hardware/baylibre-acme/api.c
baylibre-acme: Add support for SR_CONF_POWER_OFF.
[libsigrok.git] / src / hardware / baylibre-acme / api.c
CommitLineData
dfaee1de
BG
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.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
22SR_PRIV struct sr_dev_driver baylibre_acme_driver_info;
23static struct sr_dev_driver *di = &baylibre_acme_driver_info;
24
6b80b80d
BG
25static const uint32_t devopts[] = {
26 SR_CONF_CONTINUOUS | SR_CONF_SET,
27 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
28 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
29 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
61f2b7f7 30 SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET,
740ad48a 31 SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET,
6b80b80d
BG
32};
33
34#define MAX_SAMPLE_RATE 500 /* In Hz */
35
36static const uint64_t samplerates[] = {
37 SR_HZ(1),
38 SR_HZ(MAX_SAMPLE_RATE),
39 SR_HZ(1),
40};
41
dfaee1de
BG
42static int init(struct sr_context *sr_ctx)
43{
44 return std_init(sr_ctx, di, LOG_PREFIX);
45}
46
47static GSList *scan(GSList *options)
48{
49 struct drv_context *drvc;
6b80b80d
BG
50 struct dev_context *devc;
51 struct sr_dev_inst *sdi;
dfaee1de 52 GSList *devices;
6b80b80d
BG
53 gboolean status;
54 int i;
dfaee1de
BG
55
56 (void)options;
57
dfaee1de 58 drvc = di->priv;
6b80b80d 59 devices = NULL;
dfaee1de 60
6b80b80d
BG
61 devc = g_malloc0(sizeof(struct dev_context));
62 devc->samplerate = SR_HZ(10);
63
64 sdi = g_malloc0(sizeof(struct sr_dev_inst));
65 sdi->status = SR_ST_INACTIVE;
66 sdi->model = g_strdup("Acme cape");
67 sdi->driver = di;
68 sdi->priv = devc;
69
70 status = bl_acme_is_sane();
71 if (!status)
72 goto err_out;
73
74 /*
75 * Iterate over all ACME connectors and check if any probes
76 * are present.
77 */
78 for (i = 0; i < MAX_PROBES; i++) {
79 /*
80 * First check if there's an energy probe on this connector. If
81 * not, and we're already at the fifth probe - see if we can
82 * detect a temperature probe.
83 */
84 status = bl_acme_detect_probe(bl_acme_get_enrg_addr(i),
85 PROBE_NUM(i),
86 ENRG_PROBE_NAME);
87 if (status) {
88 /* Energy probe detected. */
89 status = bl_acme_register_probe(sdi,
90 PROBE_ENRG,
91 bl_acme_get_enrg_addr(i),
92 PROBE_NUM(i));
93 if (!status) {
94 sr_err("Error registering power probe %d",
95 PROBE_NUM(i));
96 continue;
97 }
98
99 } else if (i >= TEMP_PRB_START_INDEX) {
100 status = bl_acme_detect_probe(bl_acme_get_temp_addr(i),
101 PROBE_NUM(i),
102 TEMP_PROBE_NAME);
103 if (status) {
104 /* Temperature probe detected. */
105 status = bl_acme_register_probe(sdi,PROBE_TEMP,
106 bl_acme_get_temp_addr(i),
107 PROBE_NUM(i));
108 if (!status) {
109 sr_err("Error registering temp "
110 "probe %d", PROBE_NUM(i));
111 continue;
112 }
113 }
114
115 }
116 }
117
118 devices = g_slist_append(devices, sdi);
119 drvc->instances = g_slist_append(drvc->instances, sdi);
dfaee1de
BG
120
121 return devices;
6b80b80d
BG
122
123err_out:
124 g_free(devc);
125 sr_dev_inst_free(sdi);
126
127 return NULL;
dfaee1de
BG
128}
129
130static GSList *dev_list(void)
131{
132 return ((struct drv_context *)(di->priv))->instances;
133}
134
135static int dev_clear(void)
136{
137 return std_dev_clear(di, NULL);
138}
139
140static int dev_open(struct sr_dev_inst *sdi)
141{
142 (void)sdi;
143
6b80b80d 144 /* Nothing to do here. */
dfaee1de
BG
145 sdi->status = SR_ST_ACTIVE;
146
147 return SR_OK;
148}
149
150static int dev_close(struct sr_dev_inst *sdi)
151{
152 (void)sdi;
153
6b80b80d 154 /* Nothing to do here. */
dfaee1de
BG
155 sdi->status = SR_ST_INACTIVE;
156
157 return SR_OK;
158}
159
160static int cleanup(void)
161{
162 dev_clear();
163
dfaee1de
BG
164 return SR_OK;
165}
166
6b80b80d
BG
167static int config_get(uint32_t key, GVariant **data,
168 const struct sr_dev_inst *sdi,
169 const struct sr_channel_group *cg)
dfaee1de 170{
6b80b80d 171 struct dev_context *devc;
dfaee1de 172 int ret;
61f2b7f7 173 uint64_t shunt;
740ad48a 174 gboolean power_off;
dfaee1de 175
6b80b80d
BG
176 devc = sdi->priv;
177
dfaee1de
BG
178 ret = SR_OK;
179 switch (key) {
6b80b80d
BG
180 case SR_CONF_LIMIT_SAMPLES:
181 *data = g_variant_new_uint64(devc->limit_samples);
182 break;
183 case SR_CONF_LIMIT_MSEC:
184 *data = g_variant_new_uint64(devc->limit_msec);
185 break;
186 case SR_CONF_SAMPLERATE:
187 *data = g_variant_new_uint64(devc->samplerate);
188 break;
61f2b7f7
BG
189 case SR_CONF_PROBE_FACTOR:
190 if (!cg)
191 return SR_ERR_CHANNEL_GROUP;
192 ret = bl_acme_get_shunt(cg, &shunt);
193 if (ret == SR_OK)
194 *data = g_variant_new_uint64(shunt);
195 break;
740ad48a
BG
196 case SR_CONF_POWER_OFF:
197 if (!cg)
198 return SR_ERR_CHANNEL_GROUP;
199 ret = bl_acme_read_power_state(cg, &power_off);
200 if (ret == SR_OK)
201 *data = g_variant_new_boolean(power_off);
202 break;
dfaee1de
BG
203 default:
204 return SR_ERR_NA;
205 }
206
207 return ret;
208}
209
6b80b80d
BG
210static int config_set(uint32_t key, GVariant *data,
211 const struct sr_dev_inst *sdi,
212 const struct sr_channel_group *cg)
dfaee1de 213{
6b80b80d
BG
214 struct dev_context *devc;
215 uint64_t samplerate;
dfaee1de
BG
216 int ret;
217
dfaee1de
BG
218 if (sdi->status != SR_ST_ACTIVE)
219 return SR_ERR_DEV_CLOSED;
220
6b80b80d
BG
221 devc = sdi->priv;
222
dfaee1de
BG
223 ret = SR_OK;
224 switch (key) {
6b80b80d
BG
225 case SR_CONF_LIMIT_SAMPLES:
226 devc->limit_samples = g_variant_get_uint64(data);
227 devc->limit_msec = 0;
228 sr_dbg("Setting sample limit to %" PRIu64,
229 devc->limit_samples);
230 break;
231 case SR_CONF_LIMIT_MSEC:
232 devc->limit_msec = g_variant_get_uint64(data) * 1000;
233 devc->limit_samples = 0;
234 sr_dbg("Setting time limit to %" PRIu64"ms", devc->limit_msec);
235 break;
236 case SR_CONF_SAMPLERATE:
237 samplerate = g_variant_get_uint64(data);
238 if (samplerate > MAX_SAMPLE_RATE) {
239 sr_err("Maximum sample rate is %d", MAX_SAMPLE_RATE);
240 ret = SR_ERR_SAMPLERATE;
241 break;
242 }
243 devc->samplerate = samplerate;
244 sr_dbg("Setting samplerate to %" PRIu64, devc->samplerate);
245 break;
61f2b7f7
BG
246 case SR_CONF_PROBE_FACTOR:
247 if (!cg)
248 return SR_ERR_CHANNEL_GROUP;
249 ret = bl_acme_set_shunt(cg, g_variant_get_uint64(data));
250 break;
740ad48a
BG
251 case SR_CONF_POWER_OFF:
252 if (!cg)
253 return SR_ERR_CHANNEL_GROUP;
254 ret = bl_acme_set_power_off(cg, g_variant_get_boolean(data));
255 break;
dfaee1de
BG
256 default:
257 ret = SR_ERR_NA;
258 }
259
260 return ret;
261}
262
6b80b80d
BG
263static int config_list(uint32_t key, GVariant **data,
264 const struct sr_dev_inst *sdi,
265 const struct sr_channel_group *cg)
dfaee1de 266{
6b80b80d
BG
267 GVariant *gvar;
268 GVariantBuilder gvb;
dfaee1de
BG
269 int ret;
270
271 (void)sdi;
dfaee1de
BG
272 (void)cg;
273
274 ret = SR_OK;
275 switch (key) {
6b80b80d
BG
276 case SR_CONF_DEVICE_OPTIONS:
277 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
278 devopts,
279 ARRAY_SIZE(devopts),
280 sizeof(uint32_t));
281 break;
282 case SR_CONF_SAMPLERATE:
283 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
284 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
285 samplerates,
286 ARRAY_SIZE(samplerates),
287 sizeof(uint64_t));
288 g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
289 *data = g_variant_builder_end(&gvb);
290 break;
dfaee1de
BG
291 default:
292 return SR_ERR_NA;
293 }
294
295 return ret;
296}
297
6b80b80d 298static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
dfaee1de 299{
6b80b80d
BG
300 struct dev_context *devc;
301
dfaee1de
BG
302 (void)cb_data;
303
304 if (sdi->status != SR_ST_ACTIVE)
305 return SR_ERR_DEV_CLOSED;
306
6b80b80d
BG
307 devc = sdi->priv;
308 devc->samples_read = 0;
309
310 if (pipe(devc->pipe_fds)) {
311 sr_err("Error setting up pipe");
312 return SR_ERR;
313 }
314
315 devc->channel = g_io_channel_unix_new(devc->pipe_fds[0]);
316 g_io_channel_set_flags(devc->channel, G_IO_FLAG_NONBLOCK, NULL);
317 g_io_channel_set_encoding(devc->channel, NULL, NULL);
318 g_io_channel_set_buffered(devc->channel, FALSE);
319
320 sr_session_source_add_channel(sdi->session, devc->channel,
321 G_IO_IN | G_IO_ERR, 1,
322 bl_acme_receive_data, (void *)sdi);
323
324 /* Send header packet to the session bus. */
325 std_session_send_df_header(sdi, LOG_PREFIX);
326 devc->start_time = g_get_monotonic_time();
dfaee1de
BG
327
328 return SR_OK;
329}
330
331static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
332{
6b80b80d
BG
333 struct sr_datafeed_packet packet;
334 struct dev_context *devc;
335
dfaee1de
BG
336 (void)cb_data;
337
6b80b80d
BG
338 devc = sdi->priv;
339
dfaee1de
BG
340 if (sdi->status != SR_ST_ACTIVE)
341 return SR_ERR_DEV_CLOSED;
342
6b80b80d
BG
343 sr_session_source_remove_channel(sdi->session, devc->channel);
344 g_io_channel_shutdown(devc->channel, FALSE, NULL);
345 g_io_channel_unref(devc->channel);
346 devc->channel = NULL;
347
348 /* Send last packet. */
349 packet.type = SR_DF_END;
350 sr_session_send(sdi, &packet);
dfaee1de
BG
351
352 return SR_OK;
353}
354
355SR_PRIV struct sr_dev_driver baylibre_acme_driver_info = {
356 .name = "baylibre-acme",
6b80b80d 357 .longname = "BayLibre ACME (Another Cute Measurement Equipment)",
dfaee1de
BG
358 .api_version = 1,
359 .init = init,
360 .cleanup = cleanup,
361 .scan = scan,
362 .dev_list = dev_list,
363 .dev_clear = dev_clear,
364 .config_get = config_get,
365 .config_set = config_set,
366 .config_list = config_list,
367 .dev_open = dev_open,
368 .dev_close = dev_close,
369 .dev_acquisition_start = dev_acquisition_start,
370 .dev_acquisition_stop = dev_acquisition_stop,
371 .priv = NULL,
372};