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