]> sigrok.org Git - libsigrok.git/blob - src/hardware/baylibre-acme/api.c
Add helper function for scan completion
[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 <config.h>
21 #include "protocol.h"
22 #include <time.h>
23 #include <sys/timerfd.h>
24
25 static const uint32_t devopts[] = {
26         SR_CONF_CONTINUOUS,
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,
30 };
31
32 /*
33  * Currently there are two channel-group/probe options for ACME:
34  *   - SR_CONF_PROBE_FACTOR - allows to modify current shunt resistance
35  *     calibration
36  *   - SR_CONF_POWER_OFF - allows to remotely cut-off/restore power to
37  *     measured devices
38  *
39  * They are not static - we have to check each probe's capabilities in
40  * config_list().
41  */
42 #define MAX_DEVOPTS_CG          2
43 #define HAS_PROBE_FACTOR        (SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET)
44 #define HAS_POWER_OFF           (SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET)
45
46 #define MAX_SAMPLE_RATE 500 /* In Hz */
47
48 static const uint64_t samplerates[] = {
49         SR_HZ(1),
50         SR_HZ(MAX_SAMPLE_RATE),
51         SR_HZ(1),
52 };
53
54 static GSList *scan(struct sr_dev_driver *di, GSList *options)
55 {
56         struct dev_context *devc;
57         struct sr_dev_inst *sdi;
58         GSList *devices;
59         gboolean status;
60         int i;
61
62         (void)options;
63
64         devices = NULL;
65
66         devc = g_malloc0(sizeof(struct dev_context));
67         devc->samplerate = SR_HZ(10);
68
69         sdi = g_malloc0(sizeof(struct sr_dev_inst));
70         sdi->status = SR_ST_INACTIVE;
71         sdi->vendor = g_strdup("BayLibre");
72         sdi->model = g_strdup("ACME");
73         sdi->priv = devc;
74
75         status = bl_acme_is_sane();
76         if (!status)
77                 goto err_out;
78
79         /*
80          * Iterate over all ACME connectors and check if any probes
81          * are present.
82          */
83         for (i = 0; i < MAX_PROBES; i++) {
84                 /*
85                  * First check if there's an energy probe on this connector. If
86                  * not, and we're already at the fifth probe - see if we can
87                  * detect a temperature probe.
88                  */
89                 status = bl_acme_detect_probe(bl_acme_get_enrg_addr(i),
90                                               PROBE_NUM(i), ENRG_PROBE_NAME);
91                 if (status) {
92                         /* Energy probe detected. */
93                         status = bl_acme_register_probe(sdi, PROBE_ENRG,
94                                         bl_acme_get_enrg_addr(i), PROBE_NUM(i));
95                         if (!status) {
96                                 sr_err("Error registering power probe %d",
97                                        PROBE_NUM(i));
98                                 continue;
99                         }
100                 } else if (i >= TEMP_PRB_START_INDEX) {
101                         status = bl_acme_detect_probe(bl_acme_get_temp_addr(i),
102                                               PROBE_NUM(i), 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), PROBE_NUM(i));
107                                 if (!status) {
108                                         sr_err("Error registering temp "
109                                                "probe %d", PROBE_NUM(i));
110                                         continue;
111                                 }
112                         }
113                 }
114         }
115
116         /*
117          * Let's assume there's no ACME device present if no probe
118          * has been registered.
119          */
120         if (!sdi->channel_groups)
121                 goto err_out;
122
123         devices = g_slist_append(devices, sdi);
124
125         return std_scan_complete(di, devices);
126
127 err_out:
128         g_free(devc);
129         sr_dev_inst_free(sdi);
130
131         return NULL;
132 }
133
134 static int dev_open(struct sr_dev_inst *sdi)
135 {
136         (void)sdi;
137
138         sdi->status = SR_ST_ACTIVE;
139
140         return SR_OK;
141 }
142
143 static int dev_close(struct sr_dev_inst *sdi)
144 {
145         (void)sdi;
146
147         sdi->status = SR_ST_INACTIVE;
148
149         return SR_OK;
150 }
151
152 static int config_get(uint32_t key, GVariant **data,
153                       const struct sr_dev_inst *sdi,
154                       const struct sr_channel_group *cg)
155 {
156         struct dev_context *devc;
157         int ret;
158         uint64_t shunt;
159         gboolean power_off;
160
161         devc = sdi->priv;
162
163         ret = SR_OK;
164         switch (key) {
165         case SR_CONF_LIMIT_SAMPLES:
166         case SR_CONF_LIMIT_MSEC:
167                 ret = sr_sw_limits_config_get(&devc->limits, key, data);
168                 break;
169         case SR_CONF_SAMPLERATE:
170                 *data = g_variant_new_uint64(devc->samplerate);
171                 break;
172         case SR_CONF_PROBE_FACTOR:
173                 if (!cg)
174                         return SR_ERR_CHANNEL_GROUP;
175                 ret = bl_acme_get_shunt(cg, &shunt);
176                 if (ret == SR_OK)
177                         *data = g_variant_new_uint64(shunt);
178                 break;
179         case SR_CONF_POWER_OFF:
180                 if (!cg)
181                         return SR_ERR_CHANNEL_GROUP;
182                 ret = bl_acme_read_power_state(cg, &power_off);
183                 if (ret == SR_OK)
184                         *data = g_variant_new_boolean(power_off);
185                 break;
186         default:
187                 return SR_ERR_NA;
188         }
189
190         return ret;
191 }
192
193 static int config_set(uint32_t key, GVariant *data,
194                       const struct sr_dev_inst *sdi,
195                       const struct sr_channel_group *cg)
196 {
197         struct dev_context *devc;
198         uint64_t samplerate;
199         int ret;
200
201         if (sdi->status != SR_ST_ACTIVE)
202                 return SR_ERR_DEV_CLOSED;
203
204         devc = sdi->priv;
205
206         ret = SR_OK;
207         switch (key) {
208         case SR_CONF_LIMIT_SAMPLES:
209         case SR_CONF_LIMIT_MSEC:
210                 ret = sr_sw_limits_config_set(&devc->limits, key, data);
211                 break;
212         case SR_CONF_SAMPLERATE:
213                 samplerate = g_variant_get_uint64(data);
214                 if (samplerate > MAX_SAMPLE_RATE) {
215                         sr_err("Maximum sample rate is %d", MAX_SAMPLE_RATE);
216                         ret = SR_ERR_SAMPLERATE;
217                         break;
218                 }
219                 devc->samplerate = samplerate;
220                 bl_acme_maybe_set_update_interval(sdi, samplerate);
221                 break;
222         case SR_CONF_PROBE_FACTOR:
223                 if (!cg)
224                         return SR_ERR_CHANNEL_GROUP;
225                 ret = bl_acme_set_shunt(cg, g_variant_get_uint64(data));
226                 break;
227         case SR_CONF_POWER_OFF:
228                 if (!cg)
229                         return SR_ERR_CHANNEL_GROUP;
230                 ret = bl_acme_set_power_off(cg, g_variant_get_boolean(data));
231                 break;
232         default:
233                 ret = SR_ERR_NA;
234         }
235
236         return ret;
237 }
238
239 static int config_list(uint32_t key, GVariant **data,
240                        const struct sr_dev_inst *sdi,
241                        const struct sr_channel_group *cg)
242 {
243         uint32_t devopts_cg[MAX_DEVOPTS_CG];
244         GVariant *gvar;
245         GVariantBuilder gvb;
246         int ret, num_devopts_cg = 0;
247
248         (void)sdi;
249         (void)cg;
250
251         ret = SR_OK;
252         if (!cg) {
253                 switch (key) {
254                 case SR_CONF_DEVICE_OPTIONS:
255                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
256                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
257                         break;
258                 case SR_CONF_SAMPLERATE:
259                         g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
260                         gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
261                                 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
262                         g_variant_builder_add(&gvb, "{sv}",
263                                               "samplerate-steps", gvar);
264                         *data = g_variant_builder_end(&gvb);
265                         break;
266                 default:
267                         return SR_ERR_NA;
268                 }
269         } else {
270                 switch (key) {
271                 case SR_CONF_DEVICE_OPTIONS:
272                         if (bl_acme_get_probe_type(cg) == PROBE_ENRG)
273                                 devopts_cg[num_devopts_cg++] = HAS_PROBE_FACTOR;
274                         if (bl_acme_probe_has_pws(cg))
275                                 devopts_cg[num_devopts_cg++] = HAS_POWER_OFF;
276
277                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
278                                 devopts_cg, num_devopts_cg, sizeof(uint32_t));
279                         break;
280                 default:
281                         return SR_ERR_NA;
282                 }
283         }
284
285         return ret;
286 }
287
288 static void dev_acquisition_close(const struct sr_dev_inst *sdi)
289 {
290         GSList *chl;
291         struct sr_channel *ch;
292
293         for (chl = sdi->channels; chl; chl = chl->next) {
294                 ch = chl->data;
295                 bl_acme_close_channel(ch);
296         }
297 }
298
299 static int dev_acquisition_open(const struct sr_dev_inst *sdi)
300 {
301         GSList *chl;
302         struct sr_channel *ch;
303
304         for (chl = sdi->channels; chl; chl = chl->next) {
305                 ch = chl->data;
306                 if (bl_acme_open_channel(ch)) {
307                         sr_err("Error opening channel %s", ch->name);
308                         dev_acquisition_close(sdi);
309                         return SR_ERR;
310                 }
311         }
312
313         return 0;
314 }
315
316 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
317 {
318         struct dev_context *devc;
319         struct itimerspec tspec = {
320                 .it_interval = { 0, 0 },
321                 .it_value = { 0, 0 }
322         };
323
324         if (sdi->status != SR_ST_ACTIVE)
325                 return SR_ERR_DEV_CLOSED;
326
327         if (dev_acquisition_open(sdi))
328                 return SR_ERR;
329
330         devc = sdi->priv;
331         devc->samples_missed = 0;
332         devc->timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
333         if (devc->timer_fd < 0) {
334                 sr_err("Error creating timer fd");
335                 return SR_ERR;
336         }
337
338         tspec.it_interval.tv_sec = 0;
339         tspec.it_interval.tv_nsec = SR_HZ_TO_NS(devc->samplerate);
340         tspec.it_value = tspec.it_interval;
341
342         if (timerfd_settime(devc->timer_fd, 0, &tspec, NULL)) {
343                 sr_err("Failed to set timer");
344                 close(devc->timer_fd);
345                 return SR_ERR;
346         }
347
348         devc->channel = g_io_channel_unix_new(devc->timer_fd);
349         g_io_channel_set_flags(devc->channel, G_IO_FLAG_NONBLOCK, NULL);
350         g_io_channel_set_encoding(devc->channel, NULL, NULL);
351         g_io_channel_set_buffered(devc->channel, FALSE);
352
353         sr_session_source_add_channel(sdi->session, devc->channel,
354                 G_IO_IN | G_IO_ERR, 1000, bl_acme_receive_data, (void *)sdi);
355
356         std_session_send_df_header(sdi, LOG_PREFIX);
357         sr_sw_limits_acquisition_start(&devc->limits);
358
359         return SR_OK;
360 }
361
362 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
363 {
364         struct dev_context *devc;
365
366         devc = sdi->priv;
367
368         if (sdi->status != SR_ST_ACTIVE)
369                 return SR_ERR_DEV_CLOSED;
370
371         dev_acquisition_close(sdi);
372         sr_session_source_remove_channel(sdi->session, devc->channel);
373         g_io_channel_shutdown(devc->channel, FALSE, NULL);
374         g_io_channel_unref(devc->channel);
375         devc->channel = NULL;
376
377         std_session_send_df_end(sdi, LOG_PREFIX);
378
379         if (devc->samples_missed > 0)
380                 sr_warn("%" PRIu64 " samples missed", devc->samples_missed);
381
382         return SR_OK;
383 }
384
385 static struct sr_dev_driver baylibre_acme_driver_info = {
386         .name = "baylibre-acme",
387         .longname = "BayLibre ACME (Another Cute Measurement Equipment)",
388         .api_version = 1,
389         .init = std_init,
390         .cleanup = std_cleanup,
391         .scan = scan,
392         .dev_list = std_dev_list,
393         .config_get = config_get,
394         .config_set = config_set,
395         .config_list = config_list,
396         .dev_open = dev_open,
397         .dev_close = dev_close,
398         .dev_acquisition_start = dev_acquisition_start,
399         .dev_acquisition_stop = dev_acquisition_stop,
400         .context = NULL,
401 };
402 SR_REGISTER_DEV_DRIVER(baylibre_acme_driver_info);